From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by mx.groups.io with SMTP id smtpd.web11.10693.1599007439067585208 for ; Tue, 01 Sep 2020 17:43:59 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.24, mailfrom: eric.dong@intel.com) IronPort-SDR: IQ4Cgk+t3SXXH+OCmwbahvpnDE3nd5bJ3d11Dmflu9wEmm4lXY0Bg5Z0johgZYIBz1OxpcONqn +ttIkd1KG1YA== X-IronPort-AV: E=McAfee;i="6000,8403,9731"; a="158289599" X-IronPort-AV: E=Sophos;i="5.76,381,1592895600"; d="scan'208";a="158289599" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2020 17:43:57 -0700 IronPort-SDR: j3dwDUAY8hIPfjceJjPfDU+tbHBr+P9rvQBSsV5dYgQpZjfTNma1jq+pD+W0gimhbHdI6/Dp8W EgDqxUQVKirQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.76,381,1592895600"; d="scan'208";a="502458441" Received: from ydong10-desktop.ccr.corp.intel.com ([10.239.154.145]) by fmsmga005.fm.intel.com with ESMTP; 01 Sep 2020 17:43:55 -0700 From: "Dong, Eric" To: devel@edk2.groups.io Cc: Ray Ni , Laszlo Ersek Subject: [PATCH] UefiCpuPkg/MpInitLib: Add check for CR3/GDT/IDT. Date: Wed, 2 Sep 2020 08:43:53 +0800 Message-Id: <20200902004353.1515-1-eric.dong@intel.com> X-Mailer: git-send-email 2.23.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable AP needs to run from real mode to 32bit mode to LONG mode. Page table (pointed by CR3) and GDT are necessary to set up to correct value when CPU execution mode is switched to LONG mode. AP uses the same location page table (CR3) and GDT as what BSP uses. But when the page table or GDT is above 4GB, it's impossible for CPU to use because GDTR.base and CR3 are 32bits before switching to LONG mode. This patch adds check for the CR3, GDT.Base and IDT.Base to not above 32 bits restriction. Signed-off-by: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek --- UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 8 +- UefiCpuPkg/Library/MpInitLib/MpLib.c | 108 +++++++++++++++++++----- UefiCpuPkg/Library/MpInitLib/MpLib.h | 6 +- 3 files changed, 97 insertions(+), 25 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/M= pInitLib/DxeMpLib.c index 2c00d72dde..27f12a75a8 100644 --- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c @@ -393,13 +393,19 @@ MpInitChangeApLoopCallback ( )=0D {=0D CPU_MP_DATA *CpuMpData;=0D + EFI_STATUS Status;=0D =0D CpuMpData =3D GetCpuMpData ();=0D CpuMpData->PmCodeSegment =3D GetProtectedModeCS ();=0D CpuMpData->Pm16CodeSegment =3D GetProtectedMode16CS ();=0D CpuMpData->ApLoopMode =3D PcdGet8 (PcdCpuApLoopMode);=0D mNumberToFinish =3D CpuMpData->CpuCount - 1;=0D - WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);=0D + Status =3D WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_ERROR, "ERROR :: %a() Change Ap Loop Mode failed!\n", __= FUNCTION__));=0D + return;=0D + }=0D +=0D while (mNumberToFinish > 0) {=0D CpuPause ();=0D }=0D diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 07426274f6..21b17a7b40 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -470,7 +470,7 @@ GetProcessorNumber ( =0D @return CPU count detected=0D **/=0D -UINTN=0D +EFI_STATUS=0D CollectProcessorCount (=0D IN CPU_MP_DATA *CpuMpData=0D )=0D @@ -478,12 +478,17 @@ CollectProcessorCount ( UINTN Index;=0D CPU_INFO_IN_HOB *CpuInfoInHob;=0D BOOLEAN X2Apic;=0D + EFI_STATUS Status;=0D =0D //=0D // Send 1st broadcast IPI to APs to wakeup APs=0D //=0D CpuMpData->InitFlag =3D ApInitConfig;=0D - WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL, TRUE);=0D + Status =3D WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL, TRUE);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D CpuMpData->InitFlag =3D ApInitDone;=0D ASSERT (CpuMpData->CpuCount <=3D PcdGet32 (PcdCpuMaxLogicalProcessorNumb= er));=0D //=0D @@ -520,7 +525,11 @@ CollectProcessorCount ( //=0D // Wakeup all APs to enable x2APIC mode=0D //=0D - WakeUpAP (CpuMpData, TRUE, 0, ApFuncEnableX2Apic, NULL, TRUE);=0D + Status =3D WakeUpAP (CpuMpData, TRUE, 0, ApFuncEnableX2Apic, NULL, TRU= E);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D //=0D // Wait for all known APs finished=0D //=0D @@ -546,7 +555,7 @@ CollectProcessorCount ( =0D DEBUG ((DEBUG_INFO, "MpInitLib: Find %d processors in system.\n", CpuMpD= ata->CpuCount));=0D =0D - return CpuMpData->CpuCount;=0D + return EFI_SUCCESS;=0D }=0D =0D /**=0D @@ -990,7 +999,7 @@ WaitApWakeup ( @param[in] CpuMpData Pointer to CPU MP Data=0D =0D **/=0D -VOID=0D +EFI_STATUS=0D FillExchangeInfoData (=0D IN CPU_MP_DATA *CpuMpData=0D )=0D @@ -1001,6 +1010,35 @@ FillExchangeInfoData ( IA32_CR4 Cr4;=0D =0D ExchangeInfo =3D CpuMpData->MpCpuExchangeInfo;=0D + ExchangeInfo->Cr3 =3D AsmReadCr3 ();=0D + if (ExchangeInfo->Cr3 > 0xFFFFFFFF) {=0D + //=0D + // AP needs to run from real mode to 32bit mode to LONG mode. Page tab= le=0D + // (pointed by CR3) and GDT are necessary to set up to correct value w= hen=0D + // CPU execution mode is switched to LONG mode.=0D + // AP uses the same location page table (CR3) and GDT as what BSP uses= .=0D + // But when the page table or GDT is above 4GB, it's impossible for CP= U=0D + // to use because GDTR.base and CR3 are 32bits before switching to LON= G=0D + // mode.=0D + // Here add check for the CR3, GDT.Base and IDT.Base to not above 32 b= its=0D + // limitation.=0D + //=0D + return EFI_UNSUPPORTED;=0D + }=0D +=0D + //=0D + // Get the BSP's data of GDT and IDT=0D + //=0D + AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);=0D + if (ExchangeInfo->GdtrProfile.Base > 0xFFFFFFFF) {=0D + return EFI_UNSUPPORTED;=0D + }=0D +=0D + AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);=0D + if (ExchangeInfo->IdtrProfile.Base > 0xFFFFFFFF) {=0D + return EFI_UNSUPPORTED;=0D + }=0D +=0D ExchangeInfo->Lock =3D 0;=0D ExchangeInfo->StackStart =3D CpuMpData->Buffer;=0D ExchangeInfo->StackSize =3D CpuMpData->CpuApStackSize;=0D @@ -1009,9 +1047,6 @@ FillExchangeInfoData ( =0D ExchangeInfo->CodeSegment =3D AsmReadCs ();=0D ExchangeInfo->DataSegment =3D AsmReadDs ();=0D -=0D - ExchangeInfo->Cr3 =3D AsmReadCr3 ();=0D -=0D ExchangeInfo->CFunction =3D (UINTN) ApWakeupFunction;=0D ExchangeInfo->ApIndex =3D 0;=0D ExchangeInfo->NumApsExecuting =3D 0;=0D @@ -1037,13 +1072,6 @@ FillExchangeInfoData ( =0D ExchangeInfo->SevEsIsEnabled =3D CpuMpData->SevEsIsEnabled;=0D ExchangeInfo->GhcbBase =3D (UINTN) CpuMpData->GhcbBase;=0D -=0D - //=0D - // Get the BSP's data of GDT and IDT=0D - //=0D - AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);=0D - AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);=0D -=0D //=0D // Find a 32-bit code segment=0D //=0D @@ -1084,6 +1112,8 @@ FillExchangeInfoData ( (UINT32)ExchangeInfo->ModeOffset -=0D (UINT32)CpuMpData->AddressMap.ModeTransitionOffse= t;=0D ExchangeInfo->ModeHighSegment =3D (UINT16)ExchangeInfo->CodeSegment;=0D +=0D + return EFI_SUCCESS;=0D }=0D =0D /**=0D @@ -1308,8 +1338,12 @@ SetSevEsJumpTable ( @param[in] Procedure The function to be invoked by AP=0D @param[in] ProcedureArgument The argument to be passed into AP function= =0D @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in br= oadcast mode.=0D +=0D + @retval EFI_SUCCESS Wake up the AP success.=0D + @retval EFI_UNSUPPORTED Invalid CR3, IDT, GDT value caused fail to= wake up AP.=0D +=0D **/=0D -VOID=0D +EFI_STATUS=0D WakeUpAP (=0D IN CPU_MP_DATA *CpuMpData,=0D IN BOOLEAN Broadcast,=0D @@ -1324,6 +1358,7 @@ WakeUpAP ( CPU_AP_DATA *CpuData;=0D BOOLEAN ResetVectorRequired;=0D CPU_INFO_IN_HOB *CpuInfoInHob;=0D + EFI_STATUS Status;=0D =0D CpuMpData->FinishedCount =3D 0;=0D ResetVectorRequired =3D FALSE;=0D @@ -1333,7 +1368,10 @@ WakeUpAP ( ResetVectorRequired =3D TRUE;=0D AllocateResetVector (CpuMpData);=0D AllocateSevEsAPMemory (CpuMpData);=0D - FillExchangeInfoData (CpuMpData);=0D + Status =3D FillExchangeInfoData (CpuMpData);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D SaveLocalApicTimerSetting (CpuMpData);=0D }=0D =0D @@ -1500,6 +1538,8 @@ WakeUpAP ( // S3SmmInitDone Ppi.=0D //=0D CpuMpData->WakeUpByInitSipiSipi =3D (CpuMpData->ApLoopMode =3D=3D ApInHl= tLoop);=0D +=0D + return EFI_SUCCESS;=0D }=0D =0D /**=0D @@ -1945,6 +1985,7 @@ MpInitLibInitialize ( UINTN ApResetVectorSize;=0D UINTN BackupBufferAddr;=0D UINTN ApIdtBase;=0D + EFI_STATUS Status;=0D =0D OldCpuMpData =3D GetCpuMpDataFromGuidedHob ();=0D if (OldCpuMpData =3D=3D NULL) {=0D @@ -2067,7 +2108,10 @@ MpInitLibInitialize ( //=0D // Wakeup all APs and calculate the processor count in system=0D //=0D - CollectProcessorCount (CpuMpData);=0D + Status =3D CollectProcessorCount (CpuMpData);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D }=0D } else {=0D //=0D @@ -2118,7 +2162,11 @@ MpInitLibInitialize ( //=0D CpuMpData->InitFlag =3D ApInitReconfig;=0D }=0D - WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData, TRUE);=0D + Status =3D WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData, = TRUE);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D //=0D // Wait for all APs finished initialization=0D //=0D @@ -2262,6 +2310,7 @@ SwitchBSPWorker ( MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;=0D BOOLEAN OldInterruptState;=0D BOOLEAN OldTimerInterruptState;=0D + EFI_STATUS Status;=0D =0D //=0D // Save and Disable Local APIC timer interrupt=0D @@ -2333,7 +2382,10 @@ SwitchBSPWorker ( //=0D // Need to wakeUp AP (future BSP).=0D //=0D - WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData, T= RUE);=0D + Status =3D WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, C= puMpData, TRUE);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D =0D AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);=0D =0D @@ -2669,14 +2721,21 @@ StartupAllCPUsWorker ( CpuMpData->WaitEvent =3D WaitEvent;=0D =0D if (!SingleThread) {=0D - WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument, FALSE);=0D + Status =3D WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument,= FALSE);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D } else {=0D for (ProcessorNumber =3D 0; ProcessorNumber < ProcessorCount; Processo= rNumber++) {=0D if (ProcessorNumber =3D=3D CallerNumber) {=0D continue;=0D }=0D if (CpuMpData->CpuData[ProcessorNumber].Waiting) {=0D - WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureA= rgument, TRUE);=0D + Status =3D WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure,= ProcedureArgument, TRUE);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D +=0D break;=0D }=0D }=0D @@ -2795,7 +2854,10 @@ StartupThisAPWorker ( CpuData->ExpectedTime =3D CalculateTimeout (TimeoutInMicroseconds, &CpuD= ata->CurrentTime);=0D CpuData->TotalTime =3D 0;=0D =0D - WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgumen= t, TRUE);=0D + Status =3D WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, Proce= dureArgument, TRUE);=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D + }=0D =0D //=0D // If WaitEvent is NULL, execute in blocking mode.=0D diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h index 02652eaae1..9cf5c0f9b4 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -459,8 +459,12 @@ GetSevEsAPMemory ( @param[in] Procedure The function to be invoked by AP=0D @param[in] ProcedureArgument The argument to be passed into AP function= =0D @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in br= oadcast mode.=0D +=0D + @retval EFI_SUCCESS Wake up the AP success.=0D + @retval EFI_UNSUPPORTED Invalid CR3, IDT, GDT value caused fail to= wake up AP.=0D +=0D **/=0D -VOID=0D +EFI_STATUS=0D WakeUpAP (=0D IN CPU_MP_DATA *CpuMpData,=0D IN BOOLEAN Broadcast,=0D --=20 2.23.0.windows.1