From nobody Wed May 8 02:28:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+92567+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92567+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1660889862; cv=none; d=zohomail.com; s=zohoarc; b=FiShqk+PBN09HyCIJhpaJLv/nuBkz6rBYvKI+TEi56iryrh4TsSPPIcGX+n/1LL3F3sKu3Zbjhus4oS7NI+Q9Z2dYlufiFvlJl1TsNOiNMpRsJ3VIEuCifRLL87oPnPGROFdj1As/rGnnMZHYOACq/d3DcAqu8Ynkb7fa6BCMs0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660889862; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=cicDL75xN++lGf8frPmA18IyvmG1ojCGl32dtCX9V8c=; b=ADZQGAAK+Wai38QB7BDr42gQg3ZPhf68/fihpLf2xu5d888aATbTJl0CNBZL5DEz1FwHwFgBbE4FvcwhDnTjNWrR7Ni7hguZ++0eiPBpnetOebCydfTV9Sq0VoP9o+KOg6Ins9kt6oA5f2/t1qpJFF+pWGFz4xK3VMk0eSYcNEg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92567+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1660889862210185.94337773487598; Thu, 18 Aug 2022 23:17:42 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id IE27YY1788612xnXLYTaQVER; Thu, 18 Aug 2022 23:17:41 -0700 X-Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by mx.groups.io with SMTP id smtpd.web12.157.1660889860436391998 for ; Thu, 18 Aug 2022 23:17:40 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10443"; a="294221942" X-IronPort-AV: E=Sophos;i="5.93,247,1654585200"; d="scan'208";a="294221942" X-Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Aug 2022 23:17:39 -0700 X-IronPort-AV: E=Sophos;i="5.93,247,1654585200"; d="scan'208";a="936105476" X-Received: from shwdeopenlab705.ccr.corp.intel.com ([10.239.182.56]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Aug 2022 23:17:38 -0700 From: "Yuanhao Xie" To: devel@edk2.groups.io Cc: Yuanhao Xie , Eric Dong , Ray Ni , Rahul Kumar Subject: [edk2-devel] [Patch V2] UefiCpuPkg: Use Top of each AP's stack to save CpuMpData Date: Fri, 19 Aug 2022 14:17:28 +0800 Message-Id: <20220819061728.389-1-yuanhao.xie@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,yuanhao.xie@intel.com X-Gm-Message-State: WMgTPdBbVrThuacGHO5RJDndx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1660889861; bh=di7pacLH09RAtwTtqhUbiP1+PcTEqutBNNXLDqmMYUM=; h=Cc:Date:From:Reply-To:Subject:To; b=Zx9uyUhiFS+of2zSVn50otA7G6WBtMrzg9AeA6vRekYPE4XRvwnRFxtG917vZZ8oxb6 h02tOrIogVtZJXjCHgq4jsXl2pkH4Qt6MBRAsijQzp0O5A0Qor91RjuFyKjgSd0NkGaG1 DkY9tfddVufQjaQRYubf9GR5R7isg+FfNH0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1660889863405100002 Content-Type: text/plain; charset="utf-8" From: Yuanhao Xie To remove the dependency of CPU register, 4/8 byte at the top of the stack is occupied for CpuMpData. BIST information is also taken care here. This modification is only for PEI phase, since in DXE phase CpuMpData is accessed via global variable. Change-Id: I7564279544622617c322310b4c7028ac0e11a9c4 Signed-off-by: Yuanhao Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Reviewed-by: Ray Ni --- .../Library/MpInitLib/Ia32/MpFuncs.nasm | 8 ++++ UefiCpuPkg/Library/MpInitLib/MpLib.c | 37 ++++++++++++++----- UefiCpuPkg/Library/MpInitLib/MpLib.h | 8 ++++ UefiCpuPkg/Library/MpInitLib/PeiMpLib.c | 10 +++-- UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm | 9 +++++ 5 files changed, 59 insertions(+), 13 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm b/UefiCpuPkg/Li= brary/MpInitLib/Ia32/MpFuncs.nasm index 28301bb8f0..2625d28401 100644 --- a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm +++ b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm @@ -179,6 +179,14 @@ ProgramStack: mov esp, dword [edi + CPU_INFO_IN_HOB.ApTopOfStack] =20 CProcedureInvoke: + ; + ; Reserve 4 bytes for CpuMpData. + ; When the AP wakes up again via INIT-SIPI-SIPI, push 0 will cause the= existing CpuMpData to be overwritten with 0. + ; CpuMpData is filled in via InitializeApData() during the first time = INIT-SIPI-SIPI, + ; while overwirrten may occurs when under ApInHltLoop but InitFlag is = not set to ApInitConfig. + ; Therefore reservation is implemented by sub esp instead of push 0. + ; + sub esp, 4 push ebp ; push BIST data at top of AP stack xor ebp, ebp ; clear ebp for call stack trace push ebp diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 8d1f24370a..a4ce1c6d2e 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -571,6 +571,7 @@ InitializeApData ( { CPU_INFO_IN_HOB *CpuInfoInHob; MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr; + AP_STACK_DATA *ApStackData; =20 CpuInfoInHob =3D (CPU_INFO_IN_HOB *)(UINT= N)CpuMpData->CpuInfoInHob; CpuInfoInHob[ProcessorNumber].InitialApicId =3D GetInitialApicId (); @@ -578,6 +579,12 @@ InitializeApData ( CpuInfoInHob[ProcessorNumber].Health =3D BistData; CpuInfoInHob[ProcessorNumber].ApTopOfStack =3D ApTopOfStack; =20 + // + // AP_STACK_DATA is stored at the top of AP Stack + // + ApStackData =3D (AP_STACK_DATA *)((UINTN)ApTopOfStack - sizeof (= AP_STACK_DATA)); + ApStackData->MpData =3D CpuMpData; + CpuMpData->CpuData[ProcessorNumber].Waiting =3D FALSE; CpuMpData->CpuData[ProcessorNumber].CpuHealthy =3D (BistData =3D=3D 0) ?= TRUE : FALSE; =20 @@ -623,6 +630,7 @@ ApWakeupFunction ( CPU_INFO_IN_HOB *CpuInfoInHob; UINT64 ApTopOfStack; UINTN CurrentApicMode; + AP_STACK_DATA *ApStackData; =20 // // AP finished assembly code and begin to execute C code @@ -648,7 +656,9 @@ ApWakeupFunction ( // This is first time AP wakeup, get BIST information from AP stack // ApTopOfStack =3D CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpDa= ta->CpuApStackSize; - BistData =3D *(UINT32 *)((UINTN)ApTopOfStack - sizeof (UINTN)); + ApStackData =3D (AP_STACK_DATA *)((UINTN)ApTopOfStack - sizeof (AP_= STACK_DATA)); + BistData =3D (UINT32)ApStackData->Bist; + // // CpuMpData->CpuData[0].VolatileRegisters is initialized based on B= SP environment, // to initialize AP in InitConfig path. @@ -1796,14 +1806,22 @@ MpInitLibInitialize ( AsmGetAddressMap (&AddressMap); GetApResetVectorSize (&AddressMap, &ApResetVectorSizeBelow1Mb, &ApResetV= ectorSizeAbove1Mb); ApStackSize =3D PcdGet32 (PcdCpuApStackSize); - ApLoopMode =3D GetApLoopMode (&MonitorFilterSize); + // + // ApStackSize must be power of 2 + // + ASSERT ((ApStackSize & (ApStackSize - 1)) =3D=3D 0); + ApLoopMode =3D GetApLoopMode (&MonitorFilterSize); =20 // // Save BSP's Control registers for APs. // SaveVolatileRegisters (&VolatileRegisters); =20 - BufferSize =3D ApStackSize * MaxLogicalProcessorNumber; + BufferSize =3D ApStackSize * MaxLogicalProcessorNumber; + // + // Allocate extra ApStackSize to let AP stack align on ApStackSize bound= ay + // + BufferSize +=3D ApStackSize; BufferSize +=3D MonitorFilterSize * MaxLogicalProcessorNumber; BufferSize +=3D ApResetVectorSizeBelow1Mb; BufferSize =3D ALIGN_VALUE (BufferSize, 8); @@ -1813,13 +1831,13 @@ MpInitLibInitialize ( MpBuffer =3D AllocatePages (EFI_SIZE_TO_PAGES (BufferSize)); ASSERT (MpBuffer !=3D NULL); ZeroMem (MpBuffer, BufferSize); - Buffer =3D (UINTN)MpBuffer; + Buffer =3D ALIGN_VALUE ((UINTN)MpBuffer, ApStackSize); =20 // - // The layout of the Buffer is as below: + // The layout of the Buffer is as below (lower address on top): // - // +--------------------+ <-- Buffer - // AP Stacks (N) + // +--------------------+ <-- Buffer (Pointer of CpuMpData is stored = in the top of each AP's stack.) + // AP Stacks (N) (StackTop =3D (RSP + ApStackSize= ) & ~ApStackSize)) // +--------------------+ <-- MonitorBuffer // AP Monitor Filters (N) // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuff= er) @@ -1827,7 +1845,7 @@ MpInitLibInitialize ( // +--------------------+ // Padding // +--------------------+ <-- ApIdtBase (8-byte boundary) - // AP IDT All APs share one separate IDT. So AP can g= et address of CPU_MP_DATA from IDT Base. + // AP IDT All APs share one separate IDT. // +--------------------+ <-- CpuMpData // CPU_MP_DATA // +--------------------+ <-- CpuMpData->CpuData @@ -1864,10 +1882,11 @@ MpInitLibInitialize ( =20 // // Make sure no memory usage outside of the allocated buffer. + // (ApStackSize - (Buffer - (UINTN)MpBuffer)) is the redundant caused by= alignment // ASSERT ( (CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProces= sorNumber) =3D=3D - Buffer + BufferSize + (UINTN)MpBuffer + BufferSize - (ApStackSize - Buffer + (UINTN)MpBuffer) ); =20 // diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h index 974fb76019..69b621a340 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -302,6 +302,14 @@ struct _CPU_MP_DATA { UINT64 GhcbBase; }; =20 +// +// AP_STACK_DATA is stored at the top of each AP stack. +// +typedef struct { + UINTN Bist; + CPU_MP_DATA *MpData; +} AP_STACK_DATA; + #define AP_SAFE_STACK_SIZE 128 #define AP_RESET_STACK_SIZE AP_SAFE_STACK_SIZE =20 diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c b/UefiCpuPkg/Library/M= pInitLib/PeiMpLib.c index 65400b95a2..e732371ddd 100644 --- a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c @@ -89,7 +89,7 @@ EnableDebugAgent ( /** Get pointer to CPU MP Data structure. For BSP, the pointer is retrieved from HOB. - For AP, the structure is just after IDT. + For AP, the structure is stored in the top of each AP's stack. =20 @return The pointer to CPU MP Data structure. **/ @@ -100,15 +100,17 @@ GetCpuMpData ( { CPU_MP_DATA *CpuMpData; MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr; - IA32_DESCRIPTOR Idtr; + UINTN ApTopOfStack; + AP_STACK_DATA *ApStackData; =20 ApicBaseMsr.Uint64 =3D AsmReadMsr64 (MSR_IA32_APIC_BASE); if (ApicBaseMsr.Bits.BSP =3D=3D 1) { CpuMpData =3D GetCpuMpDataFromGuidedHob (); ASSERT (CpuMpData !=3D NULL); } else { - AsmReadIdtr (&Idtr); - CpuMpData =3D (CPU_MP_DATA *)(Idtr.Base + Idtr.Limit + 1); + ApTopOfStack =3D ALIGN_VALUE ((UINTN)&ApTopOfStack, (UINTN)PcdGet32 (P= cdCpuApStackSize)); + ApStackData =3D (AP_STACK_DATA *)((UINTN)ApTopOfStack- sizeof (AP_STA= CK_DATA)); + CpuMpData =3D (CPU_MP_DATA *)ApStackData->MpData; } =20 return CpuMpData; diff --git a/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm b/UefiCpuPkg/Lib= rary/MpInitLib/X64/MpFuncs.nasm index 1daaa72b1e..6751cae6f1 100644 --- a/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm +++ b/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm @@ -237,11 +237,20 @@ ProgramStack: mov rsp, qword [rdi + CPU_INFO_IN_HOB.ApTopOfStack] =20 CProcedureInvoke: + ; + ; Reserve 8 bytes for CpuMpData. + ; When the AP wakes up again via INIT-SIPI-SIPI, push 0 will cause the= existing CpuMpData to be overwritten with 0. + ; CpuMpData is filled in via InitializeApData() during the first time = INIT-SIPI-SIPI, + ; while overwirrten may occurs when under ApInHltLoop but InitFlag is = not set to ApInitConfig. + ; Therefore reservation is implemented by sub rsp instead of push 0. + ; + sub rsp, 8 push rbp ; Push BIST data at top of AP stack xor rbp, rbp ; Clear ebp for call stack trace push rbp mov rbp, rsp =20 + push qword 0 ; Push 8 bytes for alignment mov rax, qword [esi + MP_CPU_EXCHANGE_INFO_FIELD (InitializeFlo= atingPointUnits)] sub rsp, 20h call rax ; Call assembly function to initialize FP= U per UEFI spec --=20 2.36.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#92567): https://edk2.groups.io/g/devel/message/92567 Mute This Topic: https://groups.io/mt/93119724/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-