From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web11.7279.1672899678439664522 for ; Wed, 04 Jan 2023 22:21:19 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=bHaEffpI; spf=pass (domain: intel.com, ip: 192.55.52.43, 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=1672899678; x=1704435678; h=from:to:subject:date:message-id:mime-version: content-transfer-encoding; bh=2y/6E8g/YZwQoGi7VWSZ2p3f0PVJ217MDAKaCK/uoRo=; b=bHaEffpIvpMYEwYA6UVa2ysHSoZRXxdRySrExgKoaetZ7JGz4c6Kjz6V zVgn6tsp14Ho9q9xVJ4v3LA+9PUzGK51WImZVua5anHv5lwD8h0Lnd5EI 6ZqocoGDT1Gg+S27J246UyC8Oyfk538MEK19Y/VoCqe87BdgLj7kHswTT bs2MoTBI0FXtoE4+9q5fWld7vx1RG4oEeGiNdbrpmXcCroNjybO7cjsRd gmfElUFJNjxSXyAunMbsUQtAP+qY/jkFTNYJ4ZTRiEyxVhNmpb3H/xMz2 ID82zl9KMp5sSZla6LKTYmuXKhbeOIjGDy3fzRYPjOoTkfgjqxSsyt5BU w==; X-IronPort-AV: E=McAfee;i="6500,9779,10580"; a="408378434" X-IronPort-AV: E=Sophos;i="5.96,302,1665471600"; d="scan'208";a="408378434" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jan 2023 22:21:18 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10580"; a="657388051" X-IronPort-AV: E=Sophos;i="5.96,302,1665471600"; d="scan'208";a="657388051" Received: from shwdeopenlab705.ccr.corp.intel.com ([10.239.182.166]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jan 2023 22:21:17 -0800 From: "Yuanhao Xie" To: devel@edk2.groups.io Subject: [PATCH] UefiCpuPkg:Fixed AsmRelocateApLoopStart and ensure allocated memory <4GB Date: Thu, 5 Jan 2023 14:21:08 +0800 Message-Id: <20230105062108.1796-1-yuanhao.xie@intel.com> X-Mailer: git-send-email 2.36.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Kept 4GB allocation limit for the case that APs are still transferred to 32-bit protected mode on long mode DXE. Fixed AsmRelocateApLoopStart stack offset in Ia32. Bugzilla: https://bugzilla.tianocore.org/show_bug.cgi?id=4234 Cc: Eric Dong eric.dong@intel.com Cc: Ray Ni ray.ni@intel.com Cc: Rahul Kumar rahul1.kumar@intel.com Signed-off-by: Yuanhao Xie --- UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 35 ++++++++++++------- .../Library/MpInitLib/Ia32/MpFuncs.nasm | 9 ++--- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c index beab06a5b1..1994ee44c2 100644 --- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c @@ -389,7 +389,7 @@ RelocateApLoop ( MpInitLibWhoAmI (&ProcessorNumber); CpuMpData = GetCpuMpData (); MwaitSupport = IsMwaitSupport (); - if (StandardSignatureIsAuthenticAMD ()) { + if (StandardSignatureIsAuthenticAMD () && (sizeof (UINTN) == sizeof (UINT64))) { StackStart = CpuMpData->UseSevEsAPMethod ? CpuMpData->SevEsAPResetStackStart : mReservedTopOfApStack; AsmRelocateApLoopFuncAmd = (ASM_RELOCATE_AP_LOOP_AMD)(UINTN)mReservedApLoopFunc; AsmRelocateApLoopFuncAmd ( @@ -480,6 +480,7 @@ InitMpGlobalData ( EFI_GCD_MEMORY_SPACE_DESCRIPTOR MemDesc; UINTN StackBase; CPU_INFO_IN_HOB *CpuInfoInHob; + EFI_PHYSICAL_ADDRESS Address; SaveCpuMpData (CpuMpData); @@ -536,9 +537,9 @@ InitMpGlobalData ( // // Avoid APs access invalid buffer data which allocated by BootServices, - // so we will allocate reserved data for AP loop code. We also need to - // allocate this buffer below 4GB due to APs may be transferred to 32bit - // protected mode on long mode DXE. + // so we will allocate reserved data for AP loop code. We need to + // allocate this buffer below 4GB for the case that APs are transferred + // to 32-bit protected mode on long mode DXE. // Allocating it in advance since memory services are not available in // Exit Boot Services callback function. // @@ -555,19 +556,25 @@ InitMpGlobalData ( ) ); - mReservedTopOfApStack = (UINTN)AllocateReservedPages (EFI_SIZE_TO_PAGES (ApSafeBufferSize)); - ASSERT (mReservedTopOfApStack != 0); - ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0); - ASSERT ((AP_SAFE_STACK_SIZE & (CPU_STACK_ALIGNMENT - 1)) == 0); - - mReservedApLoopFunc = (VOID *)(mReservedTopOfApStack + CpuMpData->CpuCount * AP_SAFE_STACK_SIZE); - if (StandardSignatureIsAuthenticAMD ()) { + if (StandardSignatureIsAuthenticAMD () && (sizeof (UINTN) == sizeof (UINT64))) { + Address = BASE_4GB - 1; + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiReservedMemoryType, + EFI_SIZE_TO_PAGES (ApSafeBufferSize), + &Address + ); + ASSERT_EFI_ERROR (Status); + mReservedApLoopFunc = (VOID *)((UINTN)Address + CpuMpData->CpuCount * AP_SAFE_STACK_SIZE); CopyMem ( mReservedApLoopFunc, CpuMpData->AddressMap.RelocateApLoopFuncAddressAmd, CpuMpData->AddressMap.RelocateApLoopFuncSizeAmd ); } else { + Address = (UINTN)AllocateReservedPages (EFI_SIZE_TO_PAGES (ApSafeBufferSize)); + ASSERT (Address != 0); + mReservedApLoopFunc = (VOID *)((UINTN)Address + CpuMpData->CpuCount * AP_SAFE_STACK_SIZE); CopyMem ( mReservedApLoopFunc, CpuMpData->AddressMap.RelocateApLoopFuncAddress, @@ -575,12 +582,14 @@ InitMpGlobalData ( ); mApPageTable = CreatePageTable ( - mReservedTopOfApStack, + (UINTN)Address, ApSafeBufferSize ); } - mReservedTopOfApStack += CpuMpData->CpuCount * AP_SAFE_STACK_SIZE; + mReservedTopOfApStack = (UINTN)Address + CpuMpData->CpuCount * AP_SAFE_STACK_SIZE; + ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0); + ASSERT ((AP_SAFE_STACK_SIZE & (CPU_STACK_ALIGNMENT - 1)) == 0); Status = gBS->CreateEvent ( EVT_TIMER | EVT_NOTIFY_SIGNAL, diff --git a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm index bfcdbd31c1..5cffa632ab 100644 --- a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm +++ b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm @@ -219,20 +219,17 @@ SwitchToRealProcEnd: RendezvousFunnelProcEnd: ;------------------------------------------------------------------------------------- -; AsmRelocateApLoop (MwaitSupport, ApTargetCState, PmCodeSegment, TopOfApStack, CountTofinish, Pm16CodeSegment, SevEsAPJumpTable, WakeupBuffer); -; -; The last three parameters (Pm16CodeSegment, SevEsAPJumpTable and WakeupBuffer) are -; specific to SEV-ES support and are not applicable on IA32. +; AsmRelocateApLoop (MwaitSupport, ApTargetCState, TopOfApStack, CountTofinish, Cr3); ;------------------------------------------------------------------------------------- AsmRelocateApLoopStart: mov eax, esp - mov esp, [eax + 16] ; TopOfApStack + mov esp, [eax + 12] ; TopOfApStack push dword [eax] ; push return address for stack trace push ebp mov ebp, esp mov ebx, [eax + 8] ; ApTargetCState mov ecx, [eax + 4] ; MwaitSupport - mov eax, [eax + 20] ; CountTofinish + mov eax, [eax + 16] ; CountTofinish lock dec dword [eax] ; (*CountTofinish)-- cmp cl, 1 ; Check mwait-monitor support jnz HltLoop -- 2.36.1.windows.1