From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mx.groups.io with SMTP id smtpd.web10.83549.1675777797372123216 for ; Tue, 07 Feb 2023 05:50:05 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=Rlo89R6J; spf=pass (domain: intel.com, ip: 192.55.52.115, mailfrom: yuanhao.xie@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675777805; x=1707313805; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nk7RBNh2m/lNkNaHir1d8uGrbX2c53O5O0qGRltMhv8=; b=Rlo89R6Jk+6tEVq1fTpqRdp44dS7jvh8tYWHbXqqhBn3b1RkuD3Vj7xY zf5Hep/UBjUcSc1dw9/S5J8tcGyd1EqAm+mqJdDRGdvLS32bJcK05AUYN cBv4iFO7XSqVDtqQqIZ7gr/ElTjedxZo/tPbEH8WB/IB+FruwaCJe4bGv VhXJpHKu8VGWY3eebyQAKv6xAh5sph0paTVfTtbT/P6feWA10LC9+PGAx lw8b81fANv/ZOv8oWiuAZfQZxv6onb4raLCF2RkOclVxp/qND/zxcBNWI x2ES8r3zH4H8SJzPrflvSWwlLx7EXefBDjeCvytryG7UOf34z1VBUM6rM w==; X-IronPort-AV: E=McAfee;i="6500,9779,10614"; a="329524629" X-IronPort-AV: E=Sophos;i="5.97,278,1669104000"; d="scan'208";a="329524629" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2023 05:50:05 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10614"; a="660234278" X-IronPort-AV: E=Sophos;i="5.97,278,1669104000"; d="scan'208";a="660234278" Received: from xieyuanh-mobl.ccr.corp.intel.com ([10.255.31.127]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2023 05:50:03 -0800 From: "Yuanhao Xie" To: devel@edk2.groups.io Cc: Guo Dong , Ray Ni , Sean Rhodes , James Lu , Gua Guo Subject: [PATCH 2/5] UefiCpuPkg: Contiguous memory allocation and code clean-up. Date: Tue, 7 Feb 2023 21:49:36 +0800 Message-Id: <20230207134939.273-3-yuanhao.xie@intel.com> X-Mailer: git-send-email 2.36.1.windows.1 In-Reply-To: <20230207134939.273-1-yuanhao.xie@intel.com> References: <20230207134939.273-1-yuanhao.xie@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Contiguous memory allocation and This patch includes the code refactoring to eliminate the duplication, non-descriptive variable, etc. The memory is calculated taking into account the size difference of RelocateApLoopFunc under different cases. Allocate the memory for stacks and AP loop at contiguous address. Cc: Guo Dong Cc: Ray Ni Cc: Sean Rhodes Cc: James Lu Cc: Gua Guo Signed-off-by: Yuanhao Xie --- UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 157 ++++++++++++------------ UefiCpuPkg/Library/MpInitLib/MpLib.h | 9 ++ 2 files changed, 85 insertions(+), 81 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c index fd94652e5b..7b51c5c7f7 100644 --- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c @@ -20,14 +20,14 @@ #define AP_SAFE_STACK_SIZE 128 -CPU_MP_DATA *mCpuMpData = NULL; -EFI_EVENT mCheckAllApsEvent = NULL; -EFI_EVENT mMpInitExitBootServicesEvent = NULL; -EFI_EVENT mLegacyBootEvent = NULL; -volatile BOOLEAN mStopCheckAllApsStatus = TRUE; -VOID *mReservedApLoopFunc = NULL; -UINTN mReservedTopOfApStack; -volatile UINT32 mNumberToFinish = 0; +CPU_MP_DATA *mCpuMpData = NULL; +EFI_EVENT mCheckAllApsEvent = NULL; +EFI_EVENT mMpInitExitBootServicesEvent = NULL; +EFI_EVENT mLegacyBootEvent = NULL; +volatile BOOLEAN mStopCheckAllApsStatus = TRUE; +UINTN mReservedTopOfApStack; +volatile UINT32 mNumberToFinish = 0; +RELOCATE_AP_LOOP_ENTRY mReservedApLoop; // // Begin wakeup buffer allocation below 0x88000 @@ -380,8 +380,6 @@ RelocateApLoop ( { CPU_MP_DATA *CpuMpData; BOOLEAN MwaitSupport; - ASM_RELOCATE_AP_LOOP AsmRelocateApLoopFunc; - ASM_RELOCATE_AP_LOOP_AMD64 AsmRelocateApLoopFuncAmd64; UINTN ProcessorNumber; UINTN StackStart; @@ -390,30 +388,28 @@ RelocateApLoop ( MwaitSupport = IsMwaitSupport (); if (StandardSignatureIsAuthenticAMD () && (sizeof (UINTN) == sizeof (UINT64))) { StackStart = CpuMpData->UseSevEsAPMethod ? CpuMpData->SevEsAPResetStackStart : mReservedTopOfApStack; - AsmRelocateApLoopFuncAmd64 = (ASM_RELOCATE_AP_LOOP)(UINTN)mReservedApLoopFunc; - AsmRelocateApLoopFuncAmd64 ( - MwaitSupport, - CpuMpData->ApTargetCState, - CpuMpData->PmCodeSegment, - StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE, - (UINTN)&mNumberToFinish, - CpuMpData->Pm16CodeSegment, - CpuMpData->SevEsAPBuffer, - CpuMpData->WakeupBuffer - ); + mReservedApLoop.Amd64Entry ( + MwaitSupport, + CpuMpData->ApTargetCState, + CpuMpData->PmCodeSegment, + StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE, + (UINTN)&mNumberToFinish, + CpuMpData->Pm16CodeSegment, + CpuMpData->SevEsAPBuffer, + CpuMpData->WakeupBuffer + ); } else { - StackStart = mReservedTopOfApStack; - AsmRelocateApLoopFunc = (ASM_RELOCATE_AP_LOOP)(UINTN)mReservedApLoopFunc; - AsmRelocateApLoopFunc ( - MwaitSupport, - CpuMpData->ApTargetCState, - CpuMpData->PmCodeSegment, - StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE, - (UINTN)&mNumberToFinish, - CpuMpData->Pm16CodeSegment, - CpuMpData->SevEsAPBuffer, - CpuMpData->WakeupBuffer - ); + StackStart = mReservedTopOfApStack; + mReservedApLoop.GenericEntry ( + MwaitSupport, + CpuMpData->ApTargetCState, + CpuMpData->PmCodeSegment, + StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE, + (UINTN)&mNumberToFinish, + CpuMpData->Pm16CodeSegment, + CpuMpData->SevEsAPBuffer, + CpuMpData->WakeupBuffer + ); } // @@ -477,12 +473,15 @@ InitMpGlobalData ( ) { EFI_STATUS Status; - EFI_PHYSICAL_ADDRESS Address; - UINTN ApSafeBufferSize; + MP_ASSEMBLY_ADDRESS_MAP *AddressMap; + UINTN AllocSize; UINTN Index; EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc; UINTN StackBase; CPU_INFO_IN_HOB *CpuInfoInHob; + EFI_PHYSICAL_ADDRESS Address; + UINT8 *ApLoopFuncData; + UINTN ApLoopFuncSize; SaveCpuMpData (CpuMpData); @@ -537,6 +536,21 @@ InitMpGlobalData ( } } + AddressMap = &CpuMpData->AddressMap; + if (StandardSignatureIsAuthenticAMD () && (sizeof (UINTN) == sizeof (UINT64))) { + // + // 64-bit AMD Processor + // + ApLoopFuncData = AddressMap->RelocateApLoopFuncAddressAmd64; + ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeAmd64; + } else { + // + // Intel Processor (32-bit or 64-bit), or 32-bit AMD Processor + // + ApLoopFuncData = AddressMap->RelocateApLoopFuncAddress; + ApLoopFuncSize = AddressMap->RelocateApLoopFuncSize; + } + // // Avoid APs access invalid buffer data which allocated by BootServices, // so we will allocate reserved data for AP loop code. We also need to @@ -545,26 +559,32 @@ InitMpGlobalData ( // Allocating it in advance since memory services are not available in // Exit Boot Services callback function. // - ApSafeBufferSize = EFI_PAGES_TO_SIZE ( - EFI_SIZE_TO_PAGES ( - CpuMpData->AddressMap.RelocateApLoopFuncSize - ) - ); + // +------------+ + // | Ap Loop | + // +------------+ + // | Stack * N | + // +------------+ (low address) + // Address = BASE_4GB - 1; - Status = gBS->AllocatePages ( - AllocateMaxAddress, - EfiReservedMemoryType, - EFI_SIZE_TO_PAGES (ApSafeBufferSize), - &Address - ); + STATIC_ASSERT ((AP_SAFE_STACK_SIZE & (CPU_STACK_ALIGNMENT - 1)) == 0, "AP_SAFE_STACK_SIZE is not aligned with CPU_STACK_ALIGNMENT"); + AllocSize = EFI_PAGES_TO_SIZE ( + EFI_SIZE_TO_PAGES ( + CpuMpData->CpuCount * AP_SAFE_STACK_SIZE + ApLoopFuncSize + ) + ); + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiReservedMemoryType, + EFI_SIZE_TO_PAGES (AllocSize), + &Address + ); ASSERT_EFI_ERROR (Status); - - mReservedApLoopFunc = (VOID *)(UINTN)Address; - ASSERT (mReservedApLoopFunc != NULL); - + // If a memory range has the EFI_MEMORY_XP attribute, OS loader + // may set the IA32_EFER.NXE (No-eXecution Enable) bit in IA32_EFER MSR, + // then set the XD (eXecution Disable) bit in the CPU PAE page table. // - // Make sure that the buffer memory is executable if NX protection is enabled - // for EfiReservedMemoryType. + // Here is to make sure that the memory is executable if NX protection is + // enabled for EfiReservedMemoryType. // // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD // service. @@ -573,40 +593,15 @@ InitMpGlobalData ( if (!EFI_ERROR (Status)) { gDS->SetMemorySpaceAttributes ( Address, - ApSafeBufferSize, + ApLoopFuncSize, MemDesc.Attributes & (~EFI_MEMORY_XP) ); } - ApSafeBufferSize = EFI_PAGES_TO_SIZE ( - EFI_SIZE_TO_PAGES ( - CpuMpData->CpuCount * AP_SAFE_STACK_SIZE - ) - ); - Address = BASE_4GB - 1; - Status = gBS->AllocatePages ( - AllocateMaxAddress, - EfiReservedMemoryType, - EFI_SIZE_TO_PAGES (ApSafeBufferSize), - &Address - ); - ASSERT_EFI_ERROR (Status); - - mReservedTopOfApStack = (UINTN)Address + ApSafeBufferSize; + mReservedTopOfApStack = ((UINTN)Address + CpuMpData->CpuCount * AP_SAFE_STACK_SIZE); ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0); - if (StandardSignatureIsAuthenticAMD () && (sizeof (UINTN) == sizeof (UINT64))) { - CopyMem ( - mReservedApLoopFunc, - CpuMpData->AddressMap.RelocateApLoopFuncAddress, - CpuMpData->AddressMap.RelocateApLoopFuncSize - ); - } else { - CopyMem ( - mReservedApLoopFunc, - CpuMpData->AddressMap.RelocateApLoopFuncAddressAmd64, - CpuMpData->AddressMap.RelocateApLoopFuncSizeAmd64 - ); - } + mReservedApLoop.Data = (VOID *)mReservedTopOfApStack; + CopyMem (mReservedApLoop.Data, ApLoopFuncData, ApLoopFuncSize); Status = gBS->CreateEvent ( EVT_TIMER | EVT_NOTIFY_SIGNAL, diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpInitLib/MpLib.h index 4f6146e30d..f1b9063e78 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -401,6 +401,15 @@ typedef IN UINTN WakeupBuffer ); +// +// Union holds the relocate APs loop entries for different cases +// +typedef union { + VOID *Data; + ASM_RELOCATE_AP_LOOP_AMD64 Amd64Entry; // 64-bit AMD Processor + ASM_RELOCATE_AP_LOOP GenericEntry; // Intel Processor (32-bit or 64-bit), or 32-bit AMD Processor +} RELOCATE_AP_LOOP_ENTRY; + /** Assembly code to get starting address and size of the rendezvous entry for APs. Information for fixing a jump instruction in the code is also returned. -- 2.36.1.windows.1