From nobody Tue Feb 10 05:43:53 2026 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+92227+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+92227+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1660008372; cv=none; d=zohomail.com; s=zohoarc; b=fuk5E9JmDn2wVw1EecaCv4ulfGAR03SQOqJLn1ED2wDg0ZoB9DDI0y8bkFjosu9CNGG0MX8MIUYZ1B2axjMU8DZRtuh474f0vd6uApyqG8mPUo60xnb/tvPtC2XYGR2wh2gdGCE3Bp3vbWgnVedKza+WheTCp4Evj5hX33j1cKw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1660008372; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=+0x2WCrG5DtPb+ogVSNEQ6oabxERSEUd8sKEK3Wzxj8=; b=H3qd1FwEaKjl1Tn3V49n+sN5/92XAVqoxIBMXAGVICV8D3vneJl7Yz/xzNxQtKtpKlDliVM6vP50ZfaAgJgqGsEqbDz0so3BQUeAoRmFqqf7st+GH4SBwmrM0oojQKU4bpuGi0Yh9Mj3J1j4Tw4Q8jXD64JJT3RD6oZcgB/fplI= 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+92227+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 1660008372332710.0622936188631; Mon, 8 Aug 2022 18:26:12 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id r3exYY1788612xLgTWQU15vD; Mon, 08 Aug 2022 18:26:10 -0700 X-Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web12.6742.1660008368004129018 for ; Mon, 08 Aug 2022 18:26:10 -0700 X-IronPort-AV: E=McAfee;i="6400,9594,10433"; a="377020601" X-IronPort-AV: E=Sophos;i="5.93,223,1654585200"; d="scan'208";a="377020601" X-Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Aug 2022 18:26:10 -0700 X-IronPort-AV: E=Sophos;i="5.93,223,1654585200"; d="scan'208";a="932291957" X-Received: from unknown (HELO shwdesfp01.ccr.corp.intel.com) ([10.239.158.151]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Aug 2022 18:26:07 -0700 From: "Zhiguang Liu" To: devel@edk2.groups.io Cc: Zhiguang Liu , Eric Dong , Ray Ni , Rahul Kumar , Leif Lindholm , Dandan Bi , Liming Gao , Jian J Wang , Ard Biesheuvel , Sami Mujawar Subject: [edk2-devel] [PATCH v3 1/3] UefiCpuPkg: Simplify InitializeSeparateExceptionStacks Date: Tue, 9 Aug 2022 09:25:35 +0800 Message-Id: <20220809012537.1513-2-zhiguang.liu@intel.com> In-Reply-To: <20220809012537.1513-1-zhiguang.liu@intel.com> References: <20220809012537.1513-1-zhiguang.liu@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,zhiguang.liu@intel.com X-Gm-Message-State: 2azYe9pFtvNhOirjFUu8A0Rtx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1660008370; bh=bkIuo5S8Vt4+3vPMa95csiBnhjTCxHN2jI5wGkKCqck=; h=Cc:Date:From:Reply-To:Subject:To; b=IoVu7FerYR60aS9rKA5rmm9pf4jFKZCl4QE6EW0+AOeBv/128RV1bZFRGFfi7vOHE+u +LNyJO5GzkYdx+5TH1YqJNCCEO/KGrPwe4/0mvfHiwMYenOYcBEBvE5MLotRe+Cjxq1NC x5j96Lr0dUtcELoHaaokCnG+q13SQ4sULUQ= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1660008372640100005 Content-Type: text/plain; charset="utf-8" Hide the Exception implementation details in CpuExcetionHandlerLib and caller only need to provide buffer Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Leif Lindholm Cc: Dandan Bi Cc: Liming Gao Cc: Jian J Wang Cc: Ard Biesheuvel Reviewed-by: Sami Mujawar Signed-off-by: Zhiguang Liu --- .../Library/ArmExceptionLib/ArmExceptionLib.c | 15 +- MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c | 4 +- .../Include/Library/CpuExceptionHandlerLib.h | 15 +- .../CpuExceptionHandlerLibNull.c | 15 +- UefiCpuPkg/CpuDxe/CpuMp.c | 162 ++++------------ UefiCpuPkg/CpuMpPei/CpuMpPei.c | 176 ++++-------------- .../CpuExceptionHandlerLib/DxeException.c | 113 ++++++++--- .../CpuExceptionHandlerLib/PeiCpuException.c | 95 +++++++++- .../PeiCpuExceptionHandlerLib.inf | 4 +- .../SecPeiCpuException.c | 15 +- .../CpuExceptionHandlerLib/SmmException.c | 15 +- 11 files changed, 289 insertions(+), 340 deletions(-) diff --git a/ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c b/ArmPkg/Libr= ary/ArmExceptionLib/ArmExceptionLib.c index 2c7bc66aa7..a521c33f32 100644 --- a/ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c +++ b/ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c @@ -288,20 +288,23 @@ CommonCExceptionHandler ( =20 /** Setup separate stacks for certain exception handlers. + If the input Buffer and BufferSize are both NULL, use global variable if= possible. =20 - InitData is optional and processor arch dependent. - - @param[in] InitData Pointer to data optional for information about= how - to assign stacks for certain exception handler= s. + @param[in] Buffer Point to buffer used to separate exceptio= n stack. + @param[in, out] BufferSize On input, it indicates the byte size of B= uffer. + If the size is not enough, the return sta= tus will + be EFI_BUFFER_TOO_SMALL, and output Buffe= rSize + will be the size it needs. =20 @retval EFI_SUCCESS The stacks are assigned successfully. @retval EFI_UNSUPPORTED This function is not supported. - + @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small. **/ EFI_STATUS EFIAPI InitializeSeparateExceptionStacks ( - IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL + IN VOID *Buffer, + IN OUT UINTN *BufferSize ) { return EFI_SUCCESS; diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c b/MdeModulePkg/Core/Dx= e/DxeMain/DxeMain.c index 0a1f3d79e2..5733f0c8ec 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c +++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c @@ -1,7 +1,7 @@ /** @file DXE Core Main Entry Point =20 -Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -260,7 +260,7 @@ DxeMain ( // Setup Stack Guard // if (PcdGetBool (PcdCpuStackGuard)) { - Status =3D InitializeSeparateExceptionStacks (NULL); + Status =3D InitializeSeparateExceptionStacks (NULL, NULL); ASSERT_EFI_ERROR (Status); } =20 diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h b/MdeMod= ulePkg/Include/Library/CpuExceptionHandlerLib.h index 9a495081f7..8d44ed916a 100644 --- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h +++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h @@ -104,20 +104,23 @@ InitializeCpuExceptionHandlers ( =20 /** Setup separate stacks for certain exception handlers. + If the input Buffer and BufferSize are both NULL, use global variable if= possible. =20 - InitData is optional and processor arch dependent. - - @param[in] InitData Pointer to data optional for information about= how - to assign stacks for certain exception handler= s. + @param[in] Buffer Point to buffer used to separate exceptio= n stack. + @param[in, out] BufferSize On input, it indicates the byte size of B= uffer. + If the size is not enough, the return sta= tus will + be EFI_BUFFER_TOO_SMALL, and output Buffe= rSize + will be the size it needs. =20 @retval EFI_SUCCESS The stacks are assigned successfully. @retval EFI_UNSUPPORTED This function is not supported. - + @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small. **/ EFI_STATUS EFIAPI InitializeSeparateExceptionStacks ( - IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL + IN VOID *Buffer, + IN OUT UINTN *BufferSize ); =20 /** diff --git a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHa= ndlerLibNull.c b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExcepti= onHandlerLibNull.c index 8aeedcb4d1..74908a379b 100644 --- a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLi= bNull.c +++ b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLi= bNull.c @@ -83,20 +83,23 @@ DumpCpuContext ( =20 /** Setup separate stacks for certain exception handlers. + If the input Buffer and BufferSize are both NULL, use global variable if= possible. =20 - InitData is optional and processor arch dependent. - - @param[in] InitData Pointer to data optional for information about= how - to assign stacks for certain exception handler= s. + @param[in] Buffer Point to buffer used to separate exceptio= n stack. + @param[in, out] BufferSize On input, it indicates the byte size of B= uffer. + If the size is not enough, the return sta= tus will + be EFI_BUFFER_TOO_SMALL, and output Buffe= rSize + will be the size it needs. =20 @retval EFI_SUCCESS The stacks are assigned successfully. @retval EFI_UNSUPPORTED This function is not supported. - + @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small. **/ EFI_STATUS EFIAPI InitializeSeparateExceptionStacks ( - IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL + IN VOID *Buffer, + IN OUT UINTN *BufferSize ) { return EFI_UNSUPPORTED; diff --git a/UefiCpuPkg/CpuDxe/CpuMp.c b/UefiCpuPkg/CpuDxe/CpuMp.c index e385f585c7..f3ca813d2a 100644 --- a/UefiCpuPkg/CpuDxe/CpuMp.c +++ b/UefiCpuPkg/CpuDxe/CpuMp.c @@ -596,23 +596,13 @@ CollectBistDataFromHob ( } } =20 -/** - Get GDT register value. - - This function is mainly for AP purpose because AP may have different GDT - table than BSP. - - @param[in,out] Buffer The pointer to private data buffer. - -**/ -VOID -EFIAPI -GetGdtr ( - IN OUT VOID *Buffer - ) -{ - AsmReadGdtr ((IA32_DESCRIPTOR *)Buffer); -} +// +// Structure for InitializeSeparateExceptionStacks +// +typedef struct { + VOID *Buffer; + UINTN *BufferSize; +} EXCEPTION_STACK_SWITCH_CONTEXT; =20 /** Initializes CPU exceptions handlers for the sake of stack switch require= ment. @@ -629,27 +619,17 @@ InitializeExceptionStackSwitchHandlers ( IN OUT VOID *Buffer ) { - CPU_EXCEPTION_INIT_DATA *EssData; - IA32_DESCRIPTOR Idtr; - EFI_STATUS Status; + EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; =20 - EssData =3D Buffer; - // - // We don't plan to replace IDT table with a new one, but we should not = assume - // the AP's IDT is the same as BSP's IDT either. - // - AsmReadIdtr (&Idtr); - EssData->Ia32.IdtTable =3D (VOID *)Idtr.Base; - EssData->Ia32.IdtTableSize =3D Idtr.Limit + 1; - Status =3D InitializeSeparateExceptionStacks (EssDat= a); - ASSERT_EFI_ERROR (Status); + SwitchStackData =3D (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer; + InitializeSeparateExceptionStacks (SwitchStackData->Buffer, SwitchStackD= ata->BufferSize); } =20 /** Initializes MP exceptions handlers for the sake of stack switch requirem= ent. =20 This function will allocate required resources required to setup stack s= witch - and pass them through CPU_EXCEPTION_INIT_DATA to each logic processor. + and pass them through SwitchStackData to each logic processor. =20 **/ VOID @@ -657,129 +637,53 @@ InitializeMpExceptionStackSwitchHandlers ( VOID ) { - UINTN Index; - UINTN Bsp; - UINTN ExceptionNumber; - UINTN OldGdtSize; - UINTN NewGdtSize; - UINTN NewStackSize; - IA32_DESCRIPTOR Gdtr; - CPU_EXCEPTION_INIT_DATA EssData; - UINT8 *GdtBuffer; - UINT8 *StackTop; - - ExceptionNumber =3D FixedPcdGetSize (PcdCpuStackSwitchExceptionList); - NewStackSize =3D FixedPcdGet32 (PcdCpuKnownGoodStackSize) * Exception= Number; - - StackTop =3D AllocateRuntimeZeroPool (NewStackSize * mNumberOfProcessors= ); - ASSERT (StackTop !=3D NULL); - StackTop +=3D NewStackSize * mNumberOfProcessors; - - // - // The default exception handlers must have been initialized. Let's just= skip - // it in this method. - // - EssData.Ia32.Revision =3D CPU_EXCEPTION_INIT_DATA_REV; - EssData.Ia32.InitDefaultHandlers =3D FALSE; - - EssData.Ia32.StackSwitchExceptions =3D FixedPcdGetPtr (PcdCpuStackS= witchExceptionList); - EssData.Ia32.StackSwitchExceptionNumber =3D ExceptionNumber; - EssData.Ia32.KnownGoodStackSize =3D FixedPcdGet32 (PcdCpuKnownGo= odStackSize); + UINTN Index; + UINTN Bsp; + EXCEPTION_STACK_SWITCH_CONTEXT SwitchStackData; + UINTN BufferSize; =20 - // - // Initialize Gdtr to suppress incorrect compiler/analyzer warnings. - // - Gdtr.Base =3D 0; - Gdtr.Limit =3D 0; + SwitchStackData.BufferSize =3D &BufferSize; MpInitLibWhoAmI (&Bsp); + for (Index =3D 0; Index < mNumberOfProcessors; ++Index) { - // - // To support stack switch, we need to re-construct GDT but not IDT. - // + SwitchStackData.Buffer =3D NULL; + BufferSize =3D 0; + if (Index =3D=3D Bsp) { - GetGdtr (&Gdtr); + InitializeExceptionStackSwitchHandlers (&SwitchStackData); } else { // - // AP might have different size of GDT from BSP. + // AP might need different buffer size from BSP. // - MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL= ); + MpInitLibStartupThisAP (InitializeExceptionStackSwitchHandlers, Inde= x, NULL, 0, (VOID *)&SwitchStackData, NULL); } =20 - // - // X64 needs only one TSS of current task working for all exceptions - // because of its IST feature. IA32 needs one TSS for each exception - // in addition to current task. Since AP is not supposed to allocate - // memory, we have to do it in BSP. To simplify the code, we allocate - // memory for IA32 case to cover both IA32 and X64 exception stack - // switch. - // - // Layout of memory to allocate for each processor: - // -------------------------------- - // | Alignment | (just in case) - // -------------------------------- - // | | - // | Original GDT | - // | | - // -------------------------------- - // | Current task descriptor | - // -------------------------------- - // | | - // | Exception task descriptors | X ExceptionNumber - // | | - // -------------------------------- - // | Current task-state segment | - // -------------------------------- - // | | - // | Exception task-state segment | X ExceptionNumber - // | | - // -------------------------------- - // - OldGdtSize =3D Gdtr.Limit + 1; - EssData.Ia32.ExceptionTssDescSize =3D sizeof (IA32_TSS_DESCRIPTOR) * - (ExceptionNumber + 1); - EssData.Ia32.ExceptionTssSize =3D sizeof (IA32_TASK_STATE_SEGMENT) * - (ExceptionNumber + 1); - NewGdtSize =3D sizeof (IA32_TSS_DESCRIPTOR) + - OldGdtSize + - EssData.Ia32.ExceptionTssDescSize + - EssData.Ia32.ExceptionTssSize; - - GdtBuffer =3D AllocateRuntimeZeroPool (NewGdtSize); - ASSERT (GdtBuffer !=3D NULL); - - // - // Make sure GDT table alignment - // - EssData.Ia32.GdtTable =3D ALIGN_POINTER (GdtBuffer, sizeof (IA32_T= SS_DESCRIPTOR)); - NewGdtSize -=3D ((UINT8 *)EssData.Ia32.GdtTable - GdtBuf= fer); - EssData.Ia32.GdtTableSize =3D NewGdtSize; - - EssData.Ia32.ExceptionTssDesc =3D ((UINT8 *)EssData.Ia32.GdtTable + Ol= dGdtSize); - EssData.Ia32.ExceptionTss =3D ((UINT8 *)EssData.Ia32.GdtTable + Ol= dGdtSize + - EssData.Ia32.ExceptionTssDescSize); + if (BufferSize =3D=3D 0) { + continue; + } =20 - EssData.Ia32.KnownGoodStackTop =3D (UINTN)StackTop; + SwitchStackData.Buffer =3D AllocateRuntimeZeroPool (BufferSize); + ASSERT (SwitchStackData.Buffer !=3D NULL); DEBUG (( DEBUG_INFO, - "Exception stack top[cpu%lu]: 0x%lX\n", + "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX wi= th size 0x%x\n", (UINT64)(UINTN)Index, - (UINT64)(UINTN)StackTop + (UINT64)(UINTN)SwitchStackData.Buffer, + (UINT32)BufferSize )); =20 if (Index =3D=3D Bsp) { - InitializeExceptionStackSwitchHandlers (&EssData); + InitializeExceptionStackSwitchHandlers (&SwitchStackData); } else { MpInitLibStartupThisAP ( InitializeExceptionStackSwitchHandlers, Index, NULL, 0, - (VOID *)&EssData, + (VOID *)&SwitchStackData, NULL ); } - - StackTop -=3D NewStackSize; } } =20 diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.c b/UefiCpuPkg/CpuMpPei/CpuMpPei.c index d4786979fa..c0be11d3ad 100644 --- a/UefiCpuPkg/CpuMpPei/CpuMpPei.c +++ b/UefiCpuPkg/CpuMpPei/CpuMpPei.c @@ -1,7 +1,7 @@ /** @file CPU PEI Module installs CPU Multiple Processor PPI. =20 - Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.
+ Copyright (c) 2015 - 2022, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -411,23 +411,13 @@ PeiWhoAmI ( return MpInitLibWhoAmI (ProcessorNumber); } =20 -/** - Get GDT register value. - - This function is mainly for AP purpose because AP may have different GDT - table than BSP. - - @param[in,out] Buffer The pointer to private data buffer. - -**/ -VOID -EFIAPI -GetGdtr ( - IN OUT VOID *Buffer - ) -{ - AsmReadGdtr ((IA32_DESCRIPTOR *)Buffer); -} +// +// Structure for InitializeSeparateExceptionStacks +// +typedef struct { + VOID *Buffer; + UINTN *BufferSize; +} EXCEPTION_STACK_SWITCH_CONTEXT; =20 /** Initializes CPU exceptions handlers for the sake of stack switch require= ment. @@ -444,27 +434,17 @@ InitializeExceptionStackSwitchHandlers ( IN OUT VOID *Buffer ) { - CPU_EXCEPTION_INIT_DATA *EssData; - IA32_DESCRIPTOR Idtr; - EFI_STATUS Status; + EXCEPTION_STACK_SWITCH_CONTEXT *SwitchStackData; =20 - EssData =3D Buffer; - // - // We don't plan to replace IDT table with a new one, but we should not = assume - // the AP's IDT is the same as BSP's IDT either. - // - AsmReadIdtr (&Idtr); - EssData->Ia32.IdtTable =3D (VOID *)Idtr.Base; - EssData->Ia32.IdtTableSize =3D Idtr.Limit + 1; - Status =3D InitializeSeparateExceptionStacks (EssDat= a); - ASSERT_EFI_ERROR (Status); + SwitchStackData =3D (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer; + InitializeSeparateExceptionStacks (SwitchStackData->Buffer, SwitchStackD= ata->BufferSize); } =20 /** Initializes MP exceptions handlers for the sake of stack switch requirem= ent. =20 This function will allocate required resources required to setup stack s= witch - and pass them through CPU_EXCEPTION_INIT_DATA to each logic processor. + and pass them through SwitchStackData to each logic processor. =20 **/ VOID @@ -472,148 +452,60 @@ InitializeMpExceptionStackSwitchHandlers ( VOID ) { - EFI_STATUS Status; - UINTN Index; - UINTN Bsp; - UINTN ExceptionNumber; - UINTN OldGdtSize; - UINTN NewGdtSize; - UINTN NewStackSize; - IA32_DESCRIPTOR Gdtr; - CPU_EXCEPTION_INIT_DATA EssData; - UINT8 *GdtBuffer; - UINT8 *StackTop; - UINTN NumberOfProcessors; + UINTN Index; + UINTN Bsp; + EXCEPTION_STACK_SWITCH_CONTEXT SwitchStackData; + UINTN BufferSize; + UINTN NumberOfProcessors; =20 if (!PcdGetBool (PcdCpuStackGuard)) { return; } =20 + SwitchStackData.BufferSize =3D &BufferSize; MpInitLibGetNumberOfProcessors (&NumberOfProcessors, NULL); MpInitLibWhoAmI (&Bsp); =20 - ExceptionNumber =3D FixedPcdGetSize (PcdCpuStackSwitchExceptionList); - NewStackSize =3D FixedPcdGet32 (PcdCpuKnownGoodStackSize) * Exception= Number; - - StackTop =3D AllocatePages (EFI_SIZE_TO_PAGES (NewStackSize * NumberOfPr= ocessors)); - ASSERT (StackTop !=3D NULL); - if (StackTop =3D=3D NULL) { - return; - } - - StackTop +=3D NewStackSize * NumberOfProcessors; - - // - // The default exception handlers must have been initialized. Let's just= skip - // it in this method. - // - EssData.Ia32.Revision =3D CPU_EXCEPTION_INIT_DATA_REV; - EssData.Ia32.InitDefaultHandlers =3D FALSE; - - EssData.Ia32.StackSwitchExceptions =3D FixedPcdGetPtr (PcdCpuStackS= witchExceptionList); - EssData.Ia32.StackSwitchExceptionNumber =3D ExceptionNumber; - EssData.Ia32.KnownGoodStackSize =3D FixedPcdGet32 (PcdCpuKnownGo= odStackSize); - - // - // Initialize Gdtr to suppress incorrect compiler/analyzer warnings. - // - Gdtr.Base =3D 0; - Gdtr.Limit =3D 0; for (Index =3D 0; Index < NumberOfProcessors; ++Index) { - // - // To support stack switch, we need to re-construct GDT but not IDT. - // + SwitchStackData.Buffer =3D NULL; + BufferSize =3D 0; + if (Index =3D=3D Bsp) { - GetGdtr (&Gdtr); + InitializeExceptionStackSwitchHandlers (&SwitchStackData); } else { // - // AP might have different size of GDT from BSP. + // AP might need different buffer size from BSP. // - MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL= ); + MpInitLibStartupThisAP (InitializeExceptionStackSwitchHandlers, Inde= x, NULL, 0, (VOID *)&SwitchStackData, NULL); } =20 - // - // X64 needs only one TSS of current task working for all exceptions - // because of its IST feature. IA32 needs one TSS for each exception - // in addition to current task. Since AP is not supposed to allocate - // memory, we have to do it in BSP. To simplify the code, we allocate - // memory for IA32 case to cover both IA32 and X64 exception stack - // switch. - // - // Layout of memory to allocate for each processor: - // -------------------------------- - // | Alignment | (just in case) - // -------------------------------- - // | | - // | Original GDT | - // | | - // -------------------------------- - // | Current task descriptor | - // -------------------------------- - // | | - // | Exception task descriptors | X ExceptionNumber - // | | - // -------------------------------- - // | Current task-state segment | - // -------------------------------- - // | | - // | Exception task-state segment | X ExceptionNumber - // | | - // -------------------------------- - // - OldGdtSize =3D Gdtr.Limit + 1; - EssData.Ia32.ExceptionTssDescSize =3D sizeof (IA32_TSS_DESCRIPTOR) * - (ExceptionNumber + 1); - EssData.Ia32.ExceptionTssSize =3D sizeof (IA32_TASK_STATE_SEGMENT) * - (ExceptionNumber + 1); - NewGdtSize =3D sizeof (IA32_TSS_DESCRIPTOR) + - OldGdtSize + - EssData.Ia32.ExceptionTssDescSize + - EssData.Ia32.ExceptionTssSize; - - Status =3D PeiServicesAllocatePool ( - NewGdtSize, - (VOID **)&GdtBuffer - ); - ASSERT (GdtBuffer !=3D NULL); - if (EFI_ERROR (Status)) { - ASSERT_EFI_ERROR (Status); - return; + if (BufferSize =3D=3D 0) { + continue; } =20 - // - // Make sure GDT table alignment - // - EssData.Ia32.GdtTable =3D ALIGN_POINTER (GdtBuffer, sizeof (IA32_T= SS_DESCRIPTOR)); - NewGdtSize -=3D ((UINT8 *)EssData.Ia32.GdtTable - GdtBuf= fer); - EssData.Ia32.GdtTableSize =3D NewGdtSize; - - EssData.Ia32.ExceptionTssDesc =3D ((UINT8 *)EssData.Ia32.GdtTable + Ol= dGdtSize); - EssData.Ia32.ExceptionTss =3D ((UINT8 *)EssData.Ia32.GdtTable + Ol= dGdtSize + - EssData.Ia32.ExceptionTssDescSize); - - EssData.Ia32.KnownGoodStackTop =3D (UINTN)StackTop; + SwitchStackData.Buffer =3D AllocatePages (EFI_SIZE_TO_PAGES (BufferSiz= e)); + ASSERT (SwitchStackData.Buffer !=3D NULL); + ZeroMem (SwitchStackData.Buffer, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES = (BufferSize))); DEBUG (( DEBUG_INFO, - "Exception stack top[cpu%lu]: 0x%lX\n", + "Buffer[cpu%lu] for InitializeExceptionStackSwitchHandlers: 0x%lX wi= th size 0x%x\n", (UINT64)(UINTN)Index, - (UINT64)(UINTN)StackTop + (UINT64)(UINTN)SwitchStackData.Buffer, + (UINT32)BufferSize )); =20 if (Index =3D=3D Bsp) { - InitializeExceptionStackSwitchHandlers (&EssData); + InitializeExceptionStackSwitchHandlers (&SwitchStackData); } else { MpInitLibStartupThisAP ( InitializeExceptionStackSwitchHandlers, Index, NULL, 0, - (VOID *)&EssData, + (VOID *)&SwitchStackData, NULL ); } - - StackTop -=3D NewStackSize; } } =20 diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/Uef= iCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c index e62bb5e6c0..04e8409922 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c @@ -104,48 +104,105 @@ RegisterCpuInterruptHandler ( =20 /** Setup separate stacks for certain exception handlers. + If the input Buffer and BufferSize are both NULL, use global variable if= possible. =20 - InitData is optional and processor arch dependent. - - @param[in] InitData Pointer to data optional for information about= how - to assign stacks for certain exception handler= s. + @param[in] Buffer Point to buffer used to separate exceptio= n stack. + @param[in, out] BufferSize On input, it indicates the byte size of B= uffer. + If the size is not enough, the return sta= tus will + be EFI_BUFFER_TOO_SMALL, and output Buffe= rSize + will be the size it needs. =20 @retval EFI_SUCCESS The stacks are assigned successfully. @retval EFI_UNSUPPORTED This function is not supported. - + @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small. **/ EFI_STATUS EFIAPI InitializeSeparateExceptionStacks ( - IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL + IN VOID *Buffer, + IN OUT UINTN *BufferSize ) { CPU_EXCEPTION_INIT_DATA EssData; IA32_DESCRIPTOR Idtr; IA32_DESCRIPTOR Gdtr; - - if (InitData =3D=3D NULL) { + UINTN NeedBufferSize; + UINTN StackTop; + UINT8 *NewGdtTable; + + // + // X64 needs only one TSS of current task working for all exceptions + // because of its IST feature. IA32 needs one TSS for each exception + // in addition to current task. To simplify the code, we report the + // needed memory for IA32 case to cover both IA32 and X64 exception + // stack switch. + // + // Layout of memory needed for each processor: + // -------------------------------- + // | Alignment | (just in case) + // -------------------------------- + // | | + // | Original GDT | + // | | + // -------------------------------- + // | Current task descriptor | + // -------------------------------- + // | | + // | Exception task descriptors | X ExceptionNumber + // | | + // -------------------------------- + // | Current task-state segment | + // -------------------------------- + // | | + // | Exception task-state segment | X ExceptionNumber + // | | + // -------------------------------- + // + AsmReadGdtr (&Gdtr); + if ((Buffer =3D=3D NULL) && (BufferSize =3D=3D NULL)) { SetMem (mNewGdt, sizeof (mNewGdt), 0); - - AsmReadIdtr (&Idtr); - AsmReadGdtr (&Gdtr); - - EssData.X64.Revision =3D CPU_EXCEPTION_INIT_DATA_REV; - EssData.X64.KnownGoodStackTop =3D (UINTN)mNewStack + sizeof (= mNewStack); - EssData.X64.KnownGoodStackSize =3D CPU_KNOWN_GOOD_STACK_SIZE; - EssData.X64.StackSwitchExceptions =3D CPU_STACK_SWITCH_EXCEPTION_= LIST; - EssData.X64.StackSwitchExceptionNumber =3D CPU_STACK_SWITCH_EXCEPTION_= NUMBER; - EssData.X64.IdtTable =3D (VOID *)Idtr.Base; - EssData.X64.IdtTableSize =3D Idtr.Limit + 1; - EssData.X64.GdtTable =3D mNewGdt; - EssData.X64.GdtTableSize =3D sizeof (mNewGdt); - EssData.X64.ExceptionTssDesc =3D mNewGdt + Gdtr.Limit + 1; - EssData.X64.ExceptionTssDescSize =3D CPU_TSS_DESC_SIZE; - EssData.X64.ExceptionTss =3D mNewGdt + Gdtr.Limit + 1 + = CPU_TSS_DESC_SIZE; - EssData.X64.ExceptionTssSize =3D CPU_TSS_SIZE; - - InitData =3D &EssData; + StackTop =3D (UINTN)mNewStack + sizeof (mNewStack); + NewGdtTable =3D mNewGdt; + } else { + if (BufferSize =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Total needed size includes stack size, new GDT table size, TSS size. + // Add another DESCRIPTOR size for alignment requiremet. + // + NeedBufferSize =3D CPU_STACK_SWITCH_EXCEPTION_NUMBER * CPU_KNOWN_GOOD_= STACK_SIZE + + CPU_TSS_DESC_SIZE + Gdtr.Limit + 1 + + CPU_TSS_SIZE + + sizeof (IA32_TSS_DESCRIPTOR); + if (*BufferSize < NeedBufferSize) { + *BufferSize =3D NeedBufferSize; + return EFI_BUFFER_TOO_SMALL; + } + + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + StackTop =3D (UINTN)Buffer + CPU_STACK_SWITCH_EXCEPTION_NUMBER * CP= U_KNOWN_GOOD_STACK_SIZE; + NewGdtTable =3D ALIGN_POINTER (StackTop, sizeof (IA32_TSS_DESCRIPTOR)); } =20 - return ArchSetupExceptionStack (InitData); + AsmReadIdtr (&Idtr); + EssData.X64.Revision =3D CPU_EXCEPTION_INIT_DATA_REV; + EssData.X64.KnownGoodStackTop =3D StackTop; + EssData.X64.KnownGoodStackSize =3D CPU_KNOWN_GOOD_STACK_SIZE; + EssData.X64.StackSwitchExceptions =3D CPU_STACK_SWITCH_EXCEPTION_LI= ST; + EssData.X64.StackSwitchExceptionNumber =3D CPU_STACK_SWITCH_EXCEPTION_NU= MBER; + EssData.X64.IdtTable =3D (VOID *)Idtr.Base; + EssData.X64.IdtTableSize =3D Idtr.Limit + 1; + EssData.X64.GdtTable =3D NewGdtTable; + EssData.X64.GdtTableSize =3D CPU_TSS_DESC_SIZE + Gdtr.Limi= t + 1; + EssData.X64.ExceptionTssDesc =3D NewGdtTable + Gdtr.Limit + 1; + EssData.X64.ExceptionTssDescSize =3D CPU_TSS_DESC_SIZE; + EssData.X64.ExceptionTss =3D NewGdtTable + Gdtr.Limit + 1 = + CPU_TSS_DESC_SIZE; + EssData.X64.ExceptionTssSize =3D CPU_TSS_SIZE; + + return ArchSetupExceptionStack (&EssData); } diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c b/= UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c index 494c2ab433..52ec0fb803 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c @@ -151,25 +151,104 @@ InitializeCpuExceptionHandlers ( =20 /** Setup separate stacks for certain exception handlers. + If the input Buffer and BufferSize are both NULL, use global variable if= possible. =20 - InitData is optional and processor arch dependent. - - @param[in] InitData Pointer to data optional for information about= how - to assign stacks for certain exception handler= s. + @param[in] Buffer Point to buffer used to separate exceptio= n stack. + @param[in, out] BufferSize On input, it indicates the byte size of B= uffer. + If the size is not enough, the return sta= tus will + be EFI_BUFFER_TOO_SMALL, and output Buffe= rSize + will be the size it needs. =20 @retval EFI_SUCCESS The stacks are assigned successfully. @retval EFI_UNSUPPORTED This function is not supported. - + @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small. **/ EFI_STATUS EFIAPI InitializeSeparateExceptionStacks ( - IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL + IN VOID *Buffer, + IN OUT UINTN *BufferSize ) { - if (InitData =3D=3D NULL) { + CPU_EXCEPTION_INIT_DATA EssData; + IA32_DESCRIPTOR Idtr; + IA32_DESCRIPTOR Gdtr; + UINTN NeedBufferSize; + UINTN StackTop; + UINT8 *NewGdtTable; + + // + // X64 needs only one TSS of current task working for all exceptions + // because of its IST feature. IA32 needs one TSS for each exception + // in addition to current task. To simplify the code, we report the + // needed memory for IA32 case to cover both IA32 and X64 exception + // stack switch. + // + // Layout of memory needed for each processor: + // -------------------------------- + // | Alignment | (just in case) + // -------------------------------- + // | | + // | Original GDT | + // | | + // -------------------------------- + // | Current task descriptor | + // -------------------------------- + // | | + // | Exception task descriptors | X ExceptionNumber + // | | + // -------------------------------- + // | Current task-state segment | + // -------------------------------- + // | | + // | Exception task-state segment | X ExceptionNumber + // | | + // -------------------------------- + // + + if ((Buffer =3D=3D NULL) && (BufferSize =3D=3D NULL)) { return EFI_UNSUPPORTED; } =20 - return ArchSetupExceptionStack (InitData); + if (BufferSize =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + AsmReadGdtr (&Gdtr); + // + // Total needed size includes stack size, new GDT table size, TSS size. + // Add another DESCRIPTOR size for alignment requiremet. + // + NeedBufferSize =3D CPU_STACK_SWITCH_EXCEPTION_NUMBER * CPU_KNOWN_GOOD_ST= ACK_SIZE + + CPU_TSS_DESC_SIZE + Gdtr.Limit + 1 + + CPU_TSS_SIZE + + sizeof (IA32_TSS_DESCRIPTOR); + if (*BufferSize < NeedBufferSize) { + *BufferSize =3D NeedBufferSize; + return EFI_BUFFER_TOO_SMALL; + } + + if (Buffer =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + StackTop =3D (UINTN)Buffer + CPU_STACK_SWITCH_EXCEPTION_NUMBER * CPU_= KNOWN_GOOD_STACK_SIZE; + NewGdtTable =3D ALIGN_POINTER (StackTop, sizeof (IA32_TSS_DESCRIPTOR)); + + AsmReadIdtr (&Idtr); + EssData.X64.Revision =3D CPU_EXCEPTION_INIT_DATA_REV; + EssData.X64.KnownGoodStackTop =3D StackTop; + EssData.X64.KnownGoodStackSize =3D CPU_KNOWN_GOOD_STACK_SIZE; + EssData.X64.StackSwitchExceptions =3D CPU_STACK_SWITCH_EXCEPTION_LI= ST; + EssData.X64.StackSwitchExceptionNumber =3D CPU_STACK_SWITCH_EXCEPTION_NU= MBER; + EssData.X64.IdtTable =3D (VOID *)Idtr.Base; + EssData.X64.IdtTableSize =3D Idtr.Limit + 1; + EssData.X64.GdtTable =3D NewGdtTable; + EssData.X64.GdtTableSize =3D CPU_TSS_DESC_SIZE + Gdtr.Limi= t + 1; + EssData.X64.ExceptionTssDesc =3D NewGdtTable + Gdtr.Limit + 1; + EssData.X64.ExceptionTssDescSize =3D CPU_TSS_DESC_SIZE; + EssData.X64.ExceptionTss =3D NewGdtTable + Gdtr.Limit + 1 = + CPU_TSS_DESC_SIZE; + EssData.X64.ExceptionTssSize =3D CPU_TSS_SIZE; + + return ArchSetupExceptionStack (&EssData); } diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandl= erLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandle= rLib.inf index cf5bfe4083..7c2ec3b2db 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.i= nf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.i= nf @@ -1,7 +1,7 @@ ## @file # CPU Exception Handler library instance for PEI module. # -# Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+# Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent # ## @@ -56,6 +56,8 @@ =20 [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard # CONSUMES + gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize + gUefiCpuPkgTokenSpaceGuid.PcdCpuStackSwitchExceptionList =20 [FeaturePcd] gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmStackGuard ## CONS= UMES diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c index 4313cc5582..ad5e0e9ed4 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c @@ -201,20 +201,23 @@ RegisterCpuInterruptHandler ( =20 /** Setup separate stacks for certain exception handlers. + If the input Buffer and BufferSize are both NULL, use global variable if= possible. =20 - InitData is optional and processor arch dependent. - - @param[in] InitData Pointer to data optional for information about= how - to assign stacks for certain exception handler= s. + @param[in] Buffer Point to buffer used to separate exceptio= n stack. + @param[in, out] BufferSize On input, it indicates the byte size of B= uffer. + If the size is not enough, the return sta= tus will + be EFI_BUFFER_TOO_SMALL, and output Buffe= rSize + will be the size it needs. =20 @retval EFI_SUCCESS The stacks are assigned successfully. @retval EFI_UNSUPPORTED This function is not supported. - + @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small. **/ EFI_STATUS EFIAPI InitializeSeparateExceptionStacks ( - IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL + IN VOID *Buffer, + IN OUT UINTN *BufferSize ) { return EFI_UNSUPPORTED; diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c b/Uef= iCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c index 1c97dab926..46a86ad2c6 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c @@ -97,20 +97,23 @@ RegisterCpuInterruptHandler ( =20 /** Setup separate stacks for certain exception handlers. + If the input Buffer and BufferSize are both NULL, use global variable if= possible. =20 - InitData is optional and processor arch dependent. - - @param[in] InitData Pointer to data optional for information about= how - to assign stacks for certain exception handler= s. + @param[in] Buffer Point to buffer used to separate exceptio= n stack. + @param[in, out] BufferSize On input, it indicates the byte size of B= uffer. + If the size is not enough, the return sta= tus will + be EFI_BUFFER_TOO_SMALL, and output Buffe= rSize + will be the size it needs. =20 @retval EFI_SUCCESS The stacks are assigned successfully. @retval EFI_UNSUPPORTED This function is not supported. - + @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small. **/ EFI_STATUS EFIAPI InitializeSeparateExceptionStacks ( - IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL + IN VOID *Buffer, + IN OUT UINTN *BufferSize ) { return EFI_UNSUPPORTED; --=20 2.31.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 (#92227): https://edk2.groups.io/g/devel/message/92227 Mute This Topic: https://groups.io/mt/92907049/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-