From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095857264940.8798000485561; Thu, 30 Nov 2017 18:37:37 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 114A5220F3C4F; Thu, 30 Nov 2017 18:33:08 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B0EB2221786B0 for ; Thu, 30 Nov 2017 18:33:06 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:32 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:31 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199500" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:18 +0800 Message-Id: <20171201023728.4680-2-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 01/11] MdeModulePkg/metafile: Add PCD PcdCpuStackGuard X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eric Dong , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" PcdCpuStackGuard is introduced to enable/disable Stack Guard feature. Its value is FALSE by default. This feature is suggested to be enabled only if the cpu driver and CpuExceptionHandlerLib have supported stack switch for the processor used in platform. Otherwise the exception dump message won't be printed out when there's a stack overflow happened. Cc: Star Zeng Cc: Eric Dong Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- MdeModulePkg/MdeModulePkg.dec | 7 +++++++ MdeModulePkg/MdeModulePkg.uni | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 856d67aceb..b3831a21ad 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -949,6 +949,13 @@ # @Prompt The Heap Guard feature mask gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask|0x0|UINT8|0x3000= 1054 =20 + ## Indicates if UEFI Stack Guard will be enabled. + # If enabled, stack overflow in UEFI can be caught, preventing chaotic = consequences.

+ # TRUE - UEFI Stack Guard will be enabled.
+ # FALSE - UEFI Stack Guard will be disabled.
+ # @Prompt Enable UEFI Stack Guard. + gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard|FALSE|BOOLEAN|0x30001055 + [PcdsFixedAtBuild, PcdsPatchableInModule] ## Dynamic type PCD can be registered callback function for Pcd setting = action. # PcdMaxPeiPcdCallBackNumberPerPcdEntry indicates the maximum number of= callback function diff --git a/MdeModulePkg/MdeModulePkg.uni b/MdeModulePkg/MdeModulePkg.uni index 588905a9a1..43dd5103be 100644 --- a/MdeModulePkg/MdeModulePkg.uni +++ b/MdeModulePkg/MdeModulePkg.uni @@ -1204,3 +1204,10 @@ = " 0 - The returned pool is adjacent to the botto= m guard page.
\n" = " 1 - The returned pool is adjacent to the top g= uard page.
" =20 +#string STR_gEfiMdeModulePkgTokenSpaceGuid_PcdCpuStackGuard_PROMPT #langu= age en-US "Enable UEFI Stack Guard" + +#string STR_gEfiMdeModulePkgTokenSpaceGuid_PcdCpuStackGuard_HELP #langu= age en-US "Indicates if UEFI Stack Guard will be enabled.\n" + = " If enabled, stack overflow in UEFI can be caught, preventing c= haotic consequences.

\n" + = " TRUE - UEFI Stack Guard will be enabled.
\n" + = " FALSE - UEFI Stack Guard will be disabled.
" + --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 15120958584341017.0448665901457; Thu, 30 Nov 2017 18:37:38 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 4C3C5220F3C5B; Thu, 30 Nov 2017 18:33:11 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id BB907220C1C5C for ; Thu, 30 Nov 2017 18:33:07 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:33 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:32 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199505" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:19 +0800 Message-Id: <20171201023728.4680-3-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 02/11] UefiCpuPkg/UefiCpuPkg.dec: Add two new PCDs for stack switch X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > No change > v2: > Add two new PCDs to configure exception stack switch. Stack switch is required by Stack Guard feature. Following two PCDs are introduced to simplify the resource allocation for initializing stack switc= h. gUefiCpuPkgTokenSpaceGuid.PcdCpuStackSwitchExceptionList gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize PcdCpuStackSwitchExceptionList is used to specify which exception will have separate stack for its handler. For Stack Guard feature, #PF must be specified at least. PcdCpuKnownGoodStackSize is used to specify the size of knwon good stack fo= r an exception handler. Cpu driver or other drivers should use this PCD to reser= ve new stack memory for exceptions specified by above PCD. Cc: Eric Dong Cc: Laszlo Ersek Cc: Jiewen Yao Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- UefiCpuPkg/UefiCpuPkg.dec | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec index d2965ba14c..83eb6eed40 100644 --- a/UefiCpuPkg/UefiCpuPkg.dec +++ b/UefiCpuPkg/UefiCpuPkg.dec @@ -137,6 +137,18 @@ # @Prompt Lock SMM Feature Control MSR. gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmFeatureControlMsrLock|TRUE|BOOLEAN|0x= 3213210B =20 +[PcdsFixedAtBuild] + ## List of exception vectors which need switching stack. + # This PCD will only take into effect if PcdCpuStackGuard is enabled. + # By default exception #DD(8), #PF(14) are supported. + # @Prompt Specify exception vectors which need switching stack. + gUefiCpuPkgTokenSpaceGuid.PcdCpuStackSwitchExceptionList|{0x08, 0x0E}|VO= ID*|0x30002000 + + ## Size of good stack for an exception. + # This PCD will only take into effect if PcdCpuStackGuard is enabled. + # @Prompt Specify size of good stack of exception which need switching s= tack. + gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize|2048|UINT32|0x30002001 + [PcdsFixedAtBuild, PcdsPatchableInModule] ## This value is the CPU Local APIC base address, which aligns the addre= ss on a 4-KByte boundary. # @Prompt Configure base address of CPU Local APIC --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095860578452.27113584192966; Thu, 30 Nov 2017 18:37:40 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 867812211B41F; Thu, 30 Nov 2017 18:33:11 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id BE358220C1C5A for ; Thu, 30 Nov 2017 18:33:08 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:34 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:33 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199510" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:20 +0800 Message-Id: <20171201023728.4680-4-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 03/11] MdeModulePkg/CpuExceptionHandlerLib.h: Add a new API InitializeCpuExceptionHandlersEx X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , Eric Dong , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > a. Change InitializeCpuExceptionStackSwitchHandlers() to > InitializeCpuExceptionHandlersEx() to be more general. > b. Add structure definition CPU_EXCEPTION_INIT_DATA_EX for new API > v2: > Add prototype definition of InitializeCpuExceptionStackSwitchHandlers() A new API InitializeCpuExceptionHandlersEx() is introduced to support initializing exception handlers with extra functionalities which need extra init data, such as stack switch for Stack Guard feature. EFI_STATUS EFIAPI InitializeCpuExceptionHandlersEx ( IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL ); By default, this method should include all functionalities implemented by InitializeCpuExceptionHandlers(), plus extra initialization works, if any. This is could be done by calling InitializeCpuExceptionHandlers() directly in this method besides the extra works. InitDataEx is optional and its use and content are processor arch dependent. The typical usage of it is to convey resources which have to be reserved elsewhere and are necessary for the extra initialization of exception. Cc: Star Zeng Cc: Eric Dong Cc: Jiewen Yao Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- .../Include/Library/CpuExceptionHandlerLib.h | 78 ++++++++++++++++++= ++++ 1 file changed, 78 insertions(+) diff --git a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h b/MdeMod= ulePkg/Include/Library/CpuExceptionHandlerLib.h index 6cd8230127..a513ec697c 100644 --- a/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h +++ b/MdeModulePkg/Include/Library/CpuExceptionHandlerLib.h @@ -19,6 +19,54 @@ #include #include =20 +typedef union { + struct { + // + // The address of top of known good stack reserved for *ALL* exceptions + // needing switching stack. + // + UINTN KnownGoodStackTop; + // + // The size of known good stack for *ONE* exception only. + // + UINTN KnownGoodStackSize; + // + // Buffer of exception vector list for stack switch. + // + UINT8 *StackSwitchExceptions; + // + // Number of exception vectors in StackSwitchExceptions. + // + UINTN StackSwitchExceptionNumber; + // + // Buffer of IDT table. It must be type of IA32_IDT_GATE_DESCRIPTOR. + // Normally there's no need to change IDT table size. + // + VOID *IdtTable; + // + // Buffer of GDT table. It must be type of IA32_SEGMENT_DESCRIPTOR. + // + VOID *GdtTable; + // + // Size of buffer GdtTable. + // + UINTN GdtSize; + // + // Pointer to start address of task gate descriptor in the GDT table. + // It must be type of IA32_TSS_DESCRIPTOR. + // + VOID *TssDesc; + // + // Buffer of task-state segment. It must be type of IA32_TASK_STATE_SE= GMENT. + // + VOID *Tss; + // + // Flag to indicate if default handlers should be initialized or not. + // + BOOLEAN InitDefaultHandlers; + } Ia32; +} CPU_EXCEPTION_INIT_DATA_EX; + /** Initializes all CPU exceptions entries and provides the default exceptio= n handlers. =20 @@ -41,6 +89,36 @@ InitializeCpuExceptionHandlers ( IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL ); =20 +/** + Initializes all CPU exceptions entries with optional extra initializatio= ns. + + By default, this method should include all functionalities implemented by + InitializeCpuExceptionHandlers(), plus extra initialization works, if an= y. + This is could be done by calling InitializeCpuExceptionHandlers() direct= ly + in this method besides the extra works. + + InitDataEx is optional and its use and content are processor arch depend= ent. + The typical usage of it is to convey resources which have to be reserved + elsewhere and are necessary for the extra initializations of exception. + + @param[in] VectorInfo Pointer to reserved vector list. + @param[in] InitDataEx Pointer to data optional for extra initializat= ions + of exception. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized. + @retval EFI_INVALID_PARAMETER VectorInfo or InitDataEx contains invalid + content. + @retval EFI_UNSUPPORTED This function is not supported. + +**/ +EFI_STATUS +EFIAPI +InitializeCpuExceptionHandlersEx ( + IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, + IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL + ); + /** Initializes all CPU interrupt/exceptions entries and provides the defaul= t interrupt/exception handlers. =20 --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095862890310.99241300109554; Thu, 30 Nov 2017 18:37:42 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id C4A342211B435; Thu, 30 Nov 2017 18:33:11 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id CA643220C1C5A for ; Thu, 30 Nov 2017 18:33:09 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:35 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:34 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199519" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:21 +0800 Message-Id: <20171201023728.4680-5-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 04/11] MdePkg/BaseLib: Add stack switch related definitions for IA32 X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael D Kinney , Jiewen Yao , Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > Move two general macros here from CpuExceptionHandlerLib > v2: > Add new definitions required by stack switch in IA32 The new definitions include two structures IA32_TASK_STATE_SEGMENT IA32_TSS_DESCRIPTOR two macros IA32_GDT_TYPE_TSS IA32_GDT_ALIGNMENT and one API VOID EFIAPI AsmWriteTr ( IN UINT16 Selector ); They're needed to setup task gate and interrupt stack table for stack switc= h. Cc: Michael D Kinney Cc: Liming Gao Cc: Jiewen Yao Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- MdePkg/Include/Library/BaseLib.h | 117 +++++++++++++++++++++++++++= ++++ MdePkg/Library/BaseLib/BaseLib.inf | 3 + MdePkg/Library/BaseLib/Ia32/WriteTr.nasm | 36 ++++++++++ MdePkg/Library/BaseLib/X64/WriteTr.nasm | 37 ++++++++++ 4 files changed, 193 insertions(+) create mode 100644 MdePkg/Library/BaseLib/Ia32/WriteTr.nasm create mode 100644 MdePkg/Library/BaseLib/X64/WriteTr.nasm diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/Base= Lib.h index d33c3b6b38..2b98af4cd1 100644 --- a/MdePkg/Include/Library/BaseLib.h +++ b/MdePkg/Include/Library/BaseLib.h @@ -6647,6 +6647,8 @@ typedef struct { #define IA32_IDT_GATE_TYPE_INTERRUPT_32 0x8E #define IA32_IDT_GATE_TYPE_TRAP_32 0x8F =20 +#define IA32_GDT_TYPE_TSS 0x9 +#define IA32_GDT_ALIGNMENT 8 =20 #if defined (MDE_CPU_IA32) /// @@ -6663,6 +6665,70 @@ typedef union { UINT64 Uint64; } IA32_IDT_GATE_DESCRIPTOR; =20 +#pragma pack (1) +// +// IA32 Task-State Segment Definition +// +typedef struct { + UINT16 PreviousTaskLink; + UINT16 Reserved_2; + UINT32 ESP0; + UINT16 SS0; + UINT16 Reserved_10; + UINT32 ESP1; + UINT16 SS1; + UINT16 Reserved_18; + UINT32 ESP2; + UINT16 SS2; + UINT16 Reserved_26; + UINT32 CR3; + UINT32 EIP; + UINT32 EFLAGS; + UINT32 EAX; + UINT32 ECX; + UINT32 EDX; + UINT32 EBX; + UINT32 ESP; + UINT32 EBP; + UINT32 ESI; + UINT32 EDI; + UINT16 ES; + UINT16 Reserved_74; + UINT16 CS; + UINT16 Reserved_78; + UINT16 SS; + UINT16 Reserved_82; + UINT16 DS; + UINT16 Reserved_86; + UINT16 FS; + UINT16 Reserved_90; + UINT16 GS; + UINT16 Reserved_94; + UINT16 LDTSegmentSelector; + UINT16 Reserved_98; + UINT16 T; + UINT16 IOMapBaseAddress; +} IA32_TASK_STATE_SEGMENT; + +typedef union { + struct { + UINT32 LimitLow:16; ///< Segment Limit 15..00 + UINT32 BaseLow:16; ///< Base Address 15..00 + UINT32 BaseMid:8; ///< Base Address 23..16 + UINT32 Type:4; ///< Type (1 0 B 1) + UINT32 Reserved_43:1; ///< 0 + UINT32 DPL:2; ///< Descriptor Privilege Level + UINT32 P:1; ///< Segment Present + UINT32 LimitHigh:4; ///< Segment Limit 19..16 + UINT32 AVL:1; ///< Available for use by system software + UINT32 Reserved_52:2; ///< 0 0 + UINT32 G:1; ///< Granularity + UINT32 BaseHigh:8; ///< Base Address 31..24 + } Bits; + UINT64 Uint64; +} IA32_TSS_DESCRIPTOR; +#pragma pack () + #endif =20 #if defined (MDE_CPU_X64) @@ -6685,6 +6751,46 @@ typedef union { } Uint128; =20 } IA32_IDT_GATE_DESCRIPTOR; =20 +#pragma pack (1) +// +// IA32 Task-State Segment Definition +// +typedef struct { + UINT32 Reserved_0; + UINT64 RSP0; + UINT64 RSP1; + UINT64 RSP2; + UINT64 Reserved_28; + UINT64 IST[7]; + UINT64 Reserved_92; + UINT16 Reserved_100; + UINT16 IOMapBaseAddress; +} IA32_TASK_STATE_SEGMENT; + +typedef union { + struct { + UINT32 LimitLow:16; ///< Segment Limit 15..00 + UINT32 BaseLow:16; ///< Base Address 15..00 + UINT32 BaseMidl:8; ///< Base Address 23..16 + UINT32 Type:4; ///< Type (1 0 B 1) + UINT32 Reserved_43:1; ///< 0 + UINT32 DPL:2; ///< Descriptor Privilege Level + UINT32 P:1; ///< Segment Present + UINT32 LimitHigh:4; ///< Segment Limit 19..16 + UINT32 AVL:1; ///< Available for use by system software + UINT32 Reserved_52:2; ///< 0 0 + UINT32 G:1; ///< Granularity + UINT32 BaseMidh:8; ///< Base Address 31..24 + UINT32 BaseHigh:32; ///< Base Address 63..32 + UINT32 Reserved_96:32; ///< Reserved + } Bits; + struct { + UINT64 Uint64; + UINT64 Uint64_1; + } Uint128; +} IA32_TSS_DESCRIPTOR; +#pragma pack () + #endif =20 /// @@ -8950,6 +9056,17 @@ AsmRdRand64 ( OUT UINT64 *Rand ); =20 +/** + Load given selector into TR register + + @param[in] Selector Task segment selector +**/ +VOID +EFIAPI +AsmWriteTr ( + IN UINT16 Selector + ); + #endif #endif =20 diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/Ba= seLib.inf index 320ac457ea..fbfb0063b7 100644 --- a/MdePkg/Library/BaseLib/BaseLib.inf +++ b/MdePkg/Library/BaseLib/BaseLib.inf @@ -67,6 +67,8 @@ BaseLibInternals.h =20 [Sources.Ia32] + Ia32/WriteTr.nasm + Ia32/Wbinvd.c | MSFT=20 Ia32/WriteMm7.c | MSFT=20 Ia32/WriteMm6.c | MSFT=20 @@ -447,6 +449,7 @@ X64/EnableCache.asm X64/DisableCache.nasm X64/DisableCache.asm + X64/WriteTr.nasm =20 X64/CpuBreakpoint.c | MSFT=20 X64/WriteMsr64.c | MSFT=20 diff --git a/MdePkg/Library/BaseLib/Ia32/WriteTr.nasm b/MdePkg/Library/Base= Lib/Ia32/WriteTr.nasm new file mode 100644 index 0000000000..cde5a3a423 --- /dev/null +++ b/MdePkg/Library/BaseLib/Ia32/WriteTr.nasm @@ -0,0 +1,36 @@ +;-------------------------------------------------------------------------= ----- ; +; Copyright (c) 2017, Intel Corporation. All rights reserved.
+; This program and the accompanying materials +; are licensed and made available under the terms and conditions of the BS= D License +; which accompanies this distribution. The full text of the license may b= e found at +; http://opensource.org/licenses/bsd-license.php. +; +; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. +; +; Module Name: +; +; WriteTr.nasm +; +; Abstract: +; +; Write TR register +; +; Notes: +; +;-------------------------------------------------------------------------= ----- + + SECTION .text + +;-------------------------------------------------------------------------= ----- +; VOID +; AsmWriteTr ( +; UINT16 Selector +; ); +;-------------------------------------------------------------------------= ----- +global ASM_PFX(AsmWriteTr) +ASM_PFX(AsmWriteTr): + mov eax, [esp+4] + ltr ax + ret + diff --git a/MdePkg/Library/BaseLib/X64/WriteTr.nasm b/MdePkg/Library/BaseL= ib/X64/WriteTr.nasm new file mode 100644 index 0000000000..3bf1500c36 --- /dev/null +++ b/MdePkg/Library/BaseLib/X64/WriteTr.nasm @@ -0,0 +1,37 @@ +;-------------------------------------------------------------------------= ----- ; +; Copyright (c) 2017, Intel Corporation. All rights reserved.
+; This program and the accompanying materials +; are licensed and made available under the terms and conditions of the BS= D License +; which accompanies this distribution. The full text of the license may b= e found at +; http://opensource.org/licenses/bsd-license.php. +; +; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. +; +; Module Name: +; +; WriteTr.nasm +; +; Abstract: +; +; Write TR register +; +; Notes: +; +;-------------------------------------------------------------------------= ----- + + DEFAULT REL + SECTION .text + +;-------------------------------------------------------------------------= ----- +; VOID +; AsmWriteTr ( +; UINT16 Selector +; ); +;-------------------------------------------------------------------------= ----- +global ASM_PFX(AsmWriteTr) +ASM_PFX(AsmWriteTr): + mov eax, ecx + ltr ax + ret + --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 15120958660511009.0901247703932; Thu, 30 Nov 2017 18:37:46 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 10D612211B43B; Thu, 30 Nov 2017 18:33:16 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A7DAE2211B42D for ; Thu, 30 Nov 2017 18:33:11 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:37 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:35 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199524" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:22 +0800 Message-Id: <20171201023728.4680-6-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 05/11] UefiCpuPkg/CpuExceptionHandlerLib: Add stack switch support X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Kinney , Laszlo Ersek , Jiewen Yao , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > a. Move more general definitions to BaseLib.h > b. Update code to use new data structure CPU_EXCEPTION_INIT_DATA_EX > c. Implement new API InitializeCpuExceptionHandlersEx for DxeException > d. Add dummy InitializeCpuExceptionHandlersEx for SEC, PEI and SMM > v2: > a. Move common TSS structure and API definitions to BaseLib.h > b. Add EXCEPTION_STACK_SWITCH_DATA to convery data used to setup stack > switch. This can avoid allocating memory for it in this library. > c. Add globals to reserve memory for stack switch initialized in early > phase of DXE core. > d. Remove the filter code used to exclude boot modes which doesn't suppo= rt > memory allocation because those memory can passed in by parameter now. > e. Remove the nasm macro to define exception handler one by one and add a > function to return the start address of each handler. If Stack Guard is enabled and there's really a stack overflow happened duri= ng boot, a Page Fault exception will be triggered. Because the stack is out of usage, the exception handler, which shares the stack with normal UEFI drive= r, cannot be executed and cannot dump the processor information. Without those information, it's very difficult for the BIOS developers loca= te the root cause of stack overflow. And without a workable stack, the develop= er cannot event use single step to debug the UEFI driver with JTAG debugger. In order to make sure the exception handler to execute normally after stack overflow. We need separate stacks for exception handlers in case of unusable stack. IA processor allows to switch to a new stack during handling interrupt and exception. But X64 and IA32 provides different ways to make it. X64 provides interrupt stack table (IST) to allow maximum 7 different exceptions to have new stack for its handler. IA32 doesn't have IST mechanism and can only use task gate to do it since task switch allows to load a new stack through its task-state segment (TSS). The new API, InitializeCpuExceptionHandlersEx, is implemented to complete extra initialization for stack switch of exception handler. Since setting up stack switch needs allocating new memory for new stack, new GDT table and task-state segment but the initialization method will be called in different phases which have no consistent way to reserve those memory, this new API is allowed to pass the reserved resources to complete the extra works. This is cannot be done by original InitializeCpuExceptionHandlers. Considering exception handler initialization for MP situation, this new API is also necessary, because AP is not supposed to allocate memory. So the memory needed for stack switch have to be reserved in BSP before waking up AP and then pass them to InitializeCpuExceptionHandlersEx afterwards. Since Stack Guard feature is available only for DXE phase at this time, the new API is fully implemented for DXE only. Other phases implement a dummy one which just calls InitializeCpuExceptionHandlers(). Cc: Jiewen Yao Cc: Eric Dong Cc: Laszlo Ersek Cc: Michael Kinney Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- .../CpuExceptionHandlerLib/CpuExceptionCommon.h | 39 ++ .../DxeCpuExceptionHandlerLib.inf | 6 + .../Library/CpuExceptionHandlerLib/DxeException.c | 79 ++++ .../Ia32/ArchExceptionHandler.c | 167 +++++++++ .../Ia32/ArchInterruptDefs.h | 8 + .../Ia32/ExceptionTssEntryAsm.nasm | 398 +++++++++++++++++= ++++ .../CpuExceptionHandlerLib/PeiCpuException.c | 34 +- .../PeiCpuExceptionHandlerLib.inf | 1 + .../CpuExceptionHandlerLib/SecPeiCpuException.c | 34 +- .../SecPeiCpuExceptionHandlerLib.inf | 1 + .../SmmCpuExceptionHandlerLib.inf | 1 + .../Library/CpuExceptionHandlerLib/SmmException.c | 34 +- .../X64/ArchExceptionHandler.c | 134 +++++++ .../CpuExceptionHandlerLib/X64/ArchInterruptDefs.h | 3 + 14 files changed, 936 insertions(+), 3 deletions(-) create mode 100644 UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/Exceptio= nTssEntryAsm.nasm diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h index 740a58828b..9ad6c5f54f 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h @@ -48,6 +48,17 @@ 0xb21d9148, 0x9211, 0x4d8f, { 0xad, 0xd3, 0x66, 0xb1, 0x89, 0xc9, 0x2c= , 0x83 } \ } =20 +#define CPU_STACK_SWITCH_EXCEPTION_NUMBER \ + FixedPcdGetSize (PcdCpuStackSwitchExceptionList) + +#define CPU_STACK_SWITCH_EXCEPTION_LIST \ + FixedPcdGetPtr (PcdCpuStackSwitchExceptionList) + +#define CPU_KNOWN_GOOD_STACK_SIZE \ + FixedPcdGet32 (PcdCpuKnownGoodStackSize) + +#define CPU_TSS_GDT_SIZE (SIZE_2KB + CPU_TSS_DESC_SIZE + CPU_TSS_SIZE) + // // Record exception handler information // @@ -288,5 +299,33 @@ CommonExceptionHandlerWorker ( IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData ); =20 +/** + Setup separate stack for specific exceptions. + + @param[in] StackSwitchData Pointer to data required for setuping up + stack switch. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized with new stack. + @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content. +**/ +EFI_STATUS +ArchSetupExcpetionStack ( + IN CPU_EXCEPTION_INIT_DATA_EX *StackSwitchData + ); + +/** + Return address map of exception handler template so that C code can gene= rate + exception tables. The template is only for exceptions using task gate in= stead + of interrupt gate. + + @param AddressMap Pointer to a buffer where the address map is returned. +**/ +VOID +EFIAPI +AsmGetTssTemplateMap ( + OUT EXCEPTION_HANDLER_TEMPLATE_MAP *AddressMap + ); + #endif =20 diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandl= erLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandle= rLib.inf index f4a8d01c80..58e55a8a2e 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.i= nf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.i= nf @@ -30,6 +30,7 @@ [Sources.Ia32] Ia32/ExceptionHandlerAsm.asm Ia32/ExceptionHandlerAsm.nasm + Ia32/ExceptionTssEntryAsm.nasm Ia32/ExceptionHandlerAsm.S Ia32/ArchExceptionHandler.c Ia32/ArchInterruptDefs.h @@ -47,6 +48,11 @@ PeiDxeSmmCpuException.c DxeException.c =20 +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard + gUefiCpuPkgTokenSpaceGuid.PcdCpuStackSwitchExceptionList + gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize + [Packages] MdePkg/MdePkg.dec MdeModulePkg/MdeModulePkg.dec diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c b/Uef= iCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c index 31febec976..dcd8d9bb98 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c @@ -25,6 +25,10 @@ UINTN mEnabledInterruptNum =3D 0; =20 EXCEPTION_HANDLER_DATA mExceptionHandlerData; =20 +UINT8 mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER * + CPU_KNOWN_GOOD_STACK_SIZE]; +UINT8 mNewGdt[CPU_TSS_GDT_SIZE]; + /** Common exception handler. =20 @@ -197,3 +201,78 @@ RegisterCpuInterruptHandler ( { return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandle= r, &mExceptionHandlerData); } + +/** + Initializes CPU exceptions entries and setup stack switch for given exce= ptions. + + This method will call InitializeCpuExceptionHandlers() to setup default + exception handlers unless indicated not to do it explicitly. + + If InitDataEx is passed with NULL, this method will use the resource res= erved + by global variables to initialize it; Otherwise it will use data in Init= DataEx + to setup stack switch. This is for the different use cases in DxeCore and + Cpu MP exception initialization. + + @param[in] VectorInfo Pointer to reserved vector list. + @param[in] InitDataEx Pointer to data required to setup stack switch= for + given exceptions. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized. + @retval EFI_INVALID_PARAMETER VectorInfo or InitDataEx contains invalid + content. + +**/ +EFI_STATUS +EFIAPI +InitializeCpuExceptionHandlersEx ( + IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, + IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL + ) +{ + EFI_STATUS Status; + CPU_EXCEPTION_INIT_DATA_EX EssData; + IA32_DESCRIPTOR Idtr; + IA32_DESCRIPTOR Gdtr; + + // + // To avoid repeat initialization of default handlers, the caller should= pass + // an extended init data with InitDefaultHandlers set to FALSE. There's = no + // need to call this method to just initialize default handlers. Call no= n-ex + // version instead; or this method must be implemented as a simple wrapp= er of + // non-ex version of it, if this version has to be called. + // + if (InitDataEx =3D=3D NULL || InitDataEx->Ia32.InitDefaultHandlers) { + Status =3D InitializeCpuExceptionHandlers (VectorInfo); + } else { + Status =3D EFI_SUCCESS; + } + + if (!EFI_ERROR (Status)) { + // + // Initializing stack switch is only necessary for Stack Guard functio= nality. + // + if (PcdGetBool (PcdCpuStackGuard)) { + if (InitDataEx =3D=3D NULL) { + SetMem (mNewGdt, sizeof (mNewGdt), 0); + + AsmReadIdtr (&Idtr); + AsmReadGdtr (&Gdtr); + + EssData.Ia32.KnownGoodStackTop =3D (UINTN)mNewStack; + EssData.Ia32.KnownGoodStackSize =3D CPU_KNOWN_GOOD_STACK_SIZE; + EssData.Ia32.StackSwitchExceptions =3D CPU_STACK_SWITCH_EXCEPTION_= LIST; + EssData.Ia32.StackSwitchExceptionNumber =3D CPU_STACK_SWITCH_EXCEP= TION_NUMBER; + EssData.Ia32.IdtTable =3D (VOID *)Idtr.Base; + EssData.Ia32.GdtTable =3D mNewGdt; + EssData.Ia32.GdtSize =3D sizeof (mNewGdt); + EssData.Ia32.TssDesc =3D mNewGdt + Gdtr.Limit + 1; + EssData.Ia32.Tss =3D mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE; + InitDataEx =3D &EssData; + } + Status =3D ArchSetupExcpetionStack (InitDataEx); + } + } + + return Status; +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHa= ndler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandl= er.c index f2c39eb193..e656eed842 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchExceptionHandler.c @@ -107,6 +107,173 @@ ArchRestoreExceptionContext ( SystemContext.SystemContextIa32->ExceptionData =3D ReservedVectors[Excep= tionType].ExceptionData; } =20 +/** + Setup separate stack for given exceptions. + + @param[in] StackSwitchData Pointer to data required for setuping up + stack switch. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized with new stack. + @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content. + +**/ +EFI_STATUS +ArchSetupExcpetionStack ( + IN CPU_EXCEPTION_INIT_DATA_EX *StackSwitchData + ) +{ + IA32_DESCRIPTOR Gdtr; + IA32_DESCRIPTOR Idtr; + IA32_IDT_GATE_DESCRIPTOR *IdtTable; + IA32_TSS_DESCRIPTOR *TssDesc; + IA32_TASK_STATE_SEGMENT *Tss; + UINTN StackTop; + UINTN Index; + UINTN Vector; + UINTN TssBase; + UINTN GdtSize; + EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap; + + if (StackSwitchData =3D=3D NULL || + StackSwitchData->Ia32.KnownGoodStackTop =3D=3D 0 || + StackSwitchData->Ia32.KnownGoodStackSize =3D=3D 0 || + StackSwitchData->Ia32.StackSwitchExceptions =3D=3D NULL || + StackSwitchData->Ia32.StackSwitchExceptionNumber =3D=3D 0 || + StackSwitchData->Ia32.GdtTable =3D=3D NULL || + StackSwitchData->Ia32.IdtTable =3D=3D NULL || + StackSwitchData->Ia32.TssDesc =3D=3D NULL || + StackSwitchData->Ia32.Tss =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // The caller is responsible for that the GDT table, no matter the exist= ing + // one or newly allocated, has enough space to hold descriptors for exce= ption + // task-state segments. + // + if (((UINTN)StackSwitchData->Ia32.GdtTable & (IA32_GDT_ALIGNMENT - 1)) != =3D 0) { + return EFI_INVALID_PARAMETER; + } + + if ((UINTN)StackSwitchData->Ia32.TssDesc < (UINTN)(StackSwitchData->Ia32= .GdtTable)) { + return EFI_INVALID_PARAMETER; + } + + if ((UINTN)StackSwitchData->Ia32.TssDesc >=3D + ((UINTN)(StackSwitchData->Ia32.GdtTable) + StackSwitchData->Ia32.Gdt= Size)) { + return EFI_INVALID_PARAMETER; + } + + GdtSize =3D (UINTN)StackSwitchData->Ia32.TssDesc - + (UINTN)(StackSwitchData->Ia32.GdtTable) + + sizeof (IA32_TSS_DESCRIPTOR) * + (StackSwitchData->Ia32.StackSwitchExceptionNumber + 1); + if (GdtSize > StackSwitchData->Ia32.GdtSize) { + return EFI_INVALID_PARAMETER; + } + + // + // Initialize new GDT table and/or IDT table, if any + // + AsmReadIdtr (&Idtr); + AsmReadGdtr (&Gdtr); + if ((UINTN)StackSwitchData->Ia32.GdtTable !=3D Gdtr.Base) { + CopyMem (StackSwitchData->Ia32.GdtTable, (VOID *)Gdtr.Base, Gdtr.Limit= + 1); + Gdtr.Base =3D (UINTN)StackSwitchData->Ia32.GdtTable; + Gdtr.Limit =3D (UINT16)GdtSize - 1; + } + + if ((UINTN)StackSwitchData->Ia32.IdtTable !=3D Idtr.Base) { + Idtr.Base =3D (UINTN)StackSwitchData->Ia32.IdtTable; + } + + // + // Fixup current task descriptor. Task-state segment for current task wi= ll + // be filled by processor during task switching. + // + TssDesc =3D StackSwitchData->Ia32.TssDesc; + Tss =3D StackSwitchData->Ia32.Tss; + + TssBase =3D (UINTN)Tss; + TssDesc->Bits.LimitLow =3D sizeof(IA32_TASK_STATE_SEGMENT) - 1; + TssDesc->Bits.BaseLow =3D (UINT16)TssBase; + TssDesc->Bits.BaseMid =3D (UINT8)(TssBase >> 16); + TssDesc->Bits.Type =3D IA32_GDT_TYPE_TSS; + TssDesc->Bits.P =3D 1; + TssDesc->Bits.LimitHigh =3D 0; + TssDesc->Bits.BaseHigh =3D (UINT8)(TssBase >> 24); + + // + // Fixup exception task descriptor and task-state segment + // + AsmGetTssTemplateMap (&TemplateMap); + StackTop =3D StackSwitchData->Ia32.KnownGoodStackTop - CPU_STACK_ALIGNME= NT; + StackTop =3D (UINTN)ALIGN_POINTER (StackTop, CPU_STACK_ALIGNMENT); + IdtTable =3D StackSwitchData->Ia32.IdtTable; + for (Index =3D 0; Index < StackSwitchData->Ia32.StackSwitchExceptionNumb= er; ++Index) { + TssDesc +=3D 1; + Tss +=3D 1; + + // + // Fixup TSS descriptor + // + TssBase =3D (UINTN)Tss; + + TssDesc->Bits.LimitLow =3D sizeof(IA32_TASK_STATE_SEGMENT) - 1; + TssDesc->Bits.BaseLow =3D (UINT16)TssBase; + TssDesc->Bits.BaseMid =3D (UINT8)(TssBase >> 16); + TssDesc->Bits.Type =3D IA32_GDT_TYPE_TSS; + TssDesc->Bits.P =3D 1; + TssDesc->Bits.LimitHigh =3D 0; + TssDesc->Bits.BaseHigh =3D (UINT8)(TssBase >> 24); + + // + // Fixup TSS + // + Vector =3D StackSwitchData->Ia32.StackSwitchExceptions[Index]; + Tss->EIP =3D (UINT32)(TemplateMap.ExceptionStart + + Vector * TemplateMap.ExceptionStubHeaderSize); + Tss->EFLAGS =3D 0x2; + Tss->ESP =3D StackTop; + Tss->CR3 =3D AsmReadCr3 (); + Tss->ES =3D AsmReadEs (); + Tss->CS =3D AsmReadCs (); + Tss->SS =3D AsmReadSs (); + Tss->DS =3D AsmReadDs (); + Tss->FS =3D AsmReadFs (); + Tss->GS =3D AsmReadGs (); + + StackTop -=3D StackSwitchData->Ia32.KnownGoodStackSize; + + // + // Update IDT to use Task Gate for given exception + // + IdtTable[Vector].Bits.OffsetLow =3D 0; + IdtTable[Vector].Bits.Selector =3D (UINT16)((UINTN)TssDesc - Gdtr.Ba= se); + IdtTable[Vector].Bits.Reserved_0 =3D 0; + IdtTable[Vector].Bits.GateType =3D IA32_IDT_GATE_TYPE_TASK; + IdtTable[Vector].Bits.OffsetHigh =3D 0; + } + + // + // Publish GDT + // + AsmWriteGdtr (&Gdtr); + + // + // Load current task + // + AsmWriteTr ((UINT16)((UINTN)StackSwitchData->Ia32.TssDesc - Gdtr.Base)); + + // + // Publish IDT + // + AsmWriteIdtr (&Idtr); + + return EFI_SUCCESS; +} + /** Display processor context. =20 diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchInterruptDe= fs.h b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchInterruptDefs.h index a8d3556a80..d9ded5977f 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchInterruptDefs.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ArchInterruptDefs.h @@ -41,4 +41,12 @@ typedef struct { UINT8 HookAfterStubHeaderCode[HOOKAFTER_STUB_SIZE]; } RESERVED_VECTORS_DATA; =20 +#define CPU_TSS_DESC_SIZE \ + (sizeof (IA32_TSS_DESCRIPTOR) * \ + (PcdGetSize (PcdCpuStackSwitchExceptionList) + 1)) + +#define CPU_TSS_SIZE \ + (sizeof (IA32_TASK_STATE_SEGMENT) * \ + (PcdGetSize (PcdCpuStackSwitchExceptionList) + 1)) + #endif diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionTssEnt= ryAsm.nasm b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionTssEnt= ryAsm.nasm new file mode 100644 index 0000000000..62bcedea1a --- /dev/null +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/Ia32/ExceptionTssEntryAsm.n= asm @@ -0,0 +1,398 @@ +;-------------------------------------------------------------------------= ----- ; +; Copyright (c) 2017, Intel Corporation. All rights reserved.
+; This program and the accompanying materials +; are licensed and made available under the terms and conditions of the BS= D License +; which accompanies this distribution. The full text of the license may b= e found at +; http://opensource.org/licenses/bsd-license.php. +; +; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. +; +; Module Name: +; +; ExceptionTssEntryAsm.Asm +; +; Abstract: +; +; IA32 CPU Exception Handler with Separate Stack +; +; Notes: +; +;-------------------------------------------------------------------------= ----- + +; +; IA32 TSS Memory Layout Description +; +struc IA32_TSS + resw 1 + resw 1 + .ESP0: resd 1 + .SS0: resw 1 + resw 1 + .ESP1: resd 1 + .SS1: resw 1 + resw 1 + .ESP2: resd 1 + .SS2: resw 1 + resw 1 + ._CR3: resd 1 + .EIP: resd 1 + .EFLAGS: resd 1 + ._EAX: resd 1 + ._ECX: resd 1 + ._EDX: resd 1 + ._EBX: resd 1 + ._ESP: resd 1 + ._EBP: resd 1 + ._ESI: resd 1 + ._EDI: resd 1 + ._ES: resw 1 + resw 1 + ._CS: resw 1 + resw 1 + ._SS: resw 1 + resw 1 + ._DS: resw 1 + resw 1 + ._FS: resw 1 + resw 1 + ._GS: resw 1 + resw 1 + .LDT: resw 1 + resw 1 + resw 1 + resw 1 +endstruc + +; +; CommonExceptionHandler() +; +extern ASM_PFX(CommonExceptionHandler) + +SECTION .data + +SECTION .text + +ALIGN 8 + +; +; Exception handler stub table +; +AsmExceptionEntryBegin: +%assign Vector 0 +%rep 32 + +DoIret%[Vector]: + iretd +ASM_PFX(ExceptionTaskSwtichEntry%[Vector]): + db 0x6a ; push #VectorNum + db %[Vector] + mov eax, ASM_PFX(CommonTaskSwtichEntryPoint) + call eax + mov esp, eax ; Restore stack top + jmp DoIret%[Vector] + +%assign Vector Vector+1 +%endrep +AsmExceptionEntryEnd: + +; +; Common part of exception handler +; +global ASM_PFX(CommonTaskSwtichEntryPoint) +ASM_PFX(CommonTaskSwtichEntryPoint): + ; + ; Stack: + ; +---------------------+ <-- EBP - 8 + ; + TSS Base + + ; +---------------------+ <-- EBP - 4 + ; + CPUID.EDX + + ; +---------------------+ <-- EBP + ; + EIP + + ; +---------------------+ <-- EBP + 4 + ; + Vector Number + + ; +---------------------+ <-- EBP + 8 + ; + Error Code + + ; +---------------------+ + ; + + mov ebp, esp ; Stack frame + +; Use CPUID to determine if FXSAVE/FXRESTOR and DE are supported + mov eax, 1 + cpuid + push edx + +; Get TSS base of interrupted task through PreviousTaskLink field in +; current TSS base + sub esp, 8 + sgdt [esp + 2] + mov eax, [esp + 4] ; GDT base + add esp, 8 + + xor ebx, ebx + str bx ; Current TR + + mov ecx, [eax + ebx + 2] + shl ecx, 8 + mov cl, [eax + ebx + 7] + ror ecx, 8 ; ecx =3D Current TSS base + push ecx ; keep it in stack for later use + + movzx ebx, word [ecx] ; Previous Task Link + mov ecx, [eax + ebx + 2] + shl ecx, 8 + mov cl, [eax + ebx + 7] + ror ecx, 8 ; ecx =3D Previous TSS base + +; +; Align stack to make sure that EFI_FX_SAVE_STATE_IA32 of EFI_SYSTEM_CONTE= XT_IA32 +; is 16-byte aligned +; + and esp, 0xfffffff0 + sub esp, 12 + +;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax; + push dword [ecx + IA32_TSS._EAX] + push dword [ecx + IA32_TSS._ECX] + push dword [ecx + IA32_TSS._EDX] + push dword [ecx + IA32_TSS._EBX] + push dword [ecx + IA32_TSS._ESP] + push dword [ecx + IA32_TSS._EBP] + push dword [ecx + IA32_TSS._ESI] + push dword [ecx + IA32_TSS._EDI] + +;; UINT32 Gs, Fs, Es, Ds, Cs, Ss; + movzx eax, word [ecx + IA32_TSS._SS] + push eax + movzx eax, word [ecx + IA32_TSS._CS] + push eax + movzx eax, word [ecx + IA32_TSS._DS] + push eax + movzx eax, word [ecx + IA32_TSS._ES] + push eax + movzx eax, word [ecx + IA32_TSS._FS] + push eax + movzx eax, word [ecx + IA32_TSS._GS] + push eax + +;; UINT32 Eip; + push dword [ecx + IA32_TSS.EIP] + +;; UINT32 Gdtr[2], Idtr[2]; + sub esp, 8 + sidt [esp] + mov eax, [esp + 2] + xchg eax, [esp] + and eax, 0xFFFF + mov [esp+4], eax + + sub esp, 8 + sgdt [esp] + mov eax, [esp + 2] + xchg eax, [esp] + and eax, 0xFFFF + mov [esp+4], eax + +;; UINT32 Ldtr, Tr; + mov eax, ebx ; ebx still keeps selector of interrupted task + push eax + movzx eax, word [ecx + IA32_TSS.LDT] + push eax + +;; UINT32 EFlags; + push dword [ecx + IA32_TSS.EFLAGS] + +;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4; + mov eax, cr4 + push eax ; push cr4 firstly + + mov edx, [ebp - 4] ; cpuid.edx + test edx, BIT24 ; Test for FXSAVE/FXRESTOR support + jz .1 + or eax, BIT9 ; Set CR4.OSFXSR +.1: + test edx, BIT2 ; Test for Debugging Extensions support + jz .2 + or eax, BIT3 ; Set CR4.DE +.2: + mov cr4, eax + + mov eax, cr3 + push eax + mov eax, cr2 + push eax + xor eax, eax + push eax + mov eax, cr0 + push eax + +;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7; + mov eax, dr7 + push eax + mov eax, dr6 + push eax + mov eax, dr3 + push eax + mov eax, dr2 + push eax + mov eax, dr1 + push eax + mov eax, dr0 + push eax + +;; FX_SAVE_STATE_IA32 FxSaveState; +;; Clear TS bit in CR0 to avoid Device Not Available Exception (#NM) +;; when executing fxsave/fxrstor instruction + test edx, BIT24 ; Test for FXSAVE/FXRESTOR support. + ; edx still contains result from CPUID above + jz .3 + clts + sub esp, 512 + mov edi, esp + db 0xf, 0xae, 0x7 ;fxsave [edi] +.3: + +;; UINT32 ExceptionData; + push dword [ebp + 8] + +;; UEFI calling convention for IA32 requires that Direction flag in EFLAGs= is clear + cld + +;; call into exception handler + mov esi, ecx ; Keep TSS base to avoid overwrite + mov eax, ASM_PFX(CommonExceptionHandler) + +;; Prepare parameter and call + mov edx, esp + push edx ; EFI_SYSTEM_CONTEXT + push dword [ebp + 4] ; EFI_EXCEPTION_TYPE (vector number) + + ; + ; Call External Exception Handler + ; + call eax + add esp, 8 ; Restore stack before calling + mov ecx, esi ; Restore TSS base + +;; UINT32 ExceptionData; + add esp, 4 + +;; FX_SAVE_STATE_IA32 FxSaveState; + mov edx, [ebp - 4] ; cpuid.edx + test edx, BIT24 ; Test for FXSAVE/FXRESTOR support + jz .4 + mov esi, esp + db 0xf, 0xae, 0xe ; fxrstor [esi] +.4: + add esp, 512 + +;; UINT32 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7; +;; Skip restoration of DRx registers to support debuggers +;; that set breakpoints in interrupt/exception context + add esp, 4 * 6 + +;; UINT32 Cr0, Cr1, Cr2, Cr3, Cr4; + pop eax + mov cr0, eax + add esp, 4 ; not for Cr1 + pop eax + mov cr2, eax + pop eax + mov dword [ecx + IA32_TSS._CR3], eax + pop eax + mov cr4, eax + +;; UINT32 EFlags; + pop dword [ecx + IA32_TSS.EFLAGS] + mov ebx, dword [ecx + IA32_TSS.EFLAGS] + btr ebx, 9 ; Do 'cli' + mov dword [ecx + IA32_TSS.EFLAGS], ebx + +;; UINT32 Ldtr, Tr; +;; UINT32 Gdtr[2], Idtr[2]; +;; Best not let anyone mess with these particular registers... + add esp, 24 + +;; UINT32 Eip; + pop dword [ecx + IA32_TSS.EIP] + +;; UINT32 Gs, Fs, Es, Ds, Cs, Ss; +;; NOTE - modified segment registers could hang the debugger... We +;; could attempt to insulate ourselves against this possibility, +;; but that poses risks as well. +;; + pop eax +o16 mov [ecx + IA32_TSS._GS], ax + pop eax +o16 mov [ecx + IA32_TSS._FS], ax + pop eax +o16 mov [ecx + IA32_TSS._ES], ax + pop eax +o16 mov [ecx + IA32_TSS._DS], ax + pop eax +o16 mov [ecx + IA32_TSS._CS], ax + pop eax +o16 mov [ecx + IA32_TSS._SS], ax + +;; UINT32 Edi, Esi, Ebp, Esp, Ebx, Edx, Ecx, Eax; + pop dword [ecx + IA32_TSS._EDI] + pop dword [ecx + IA32_TSS._ESI] + add esp, 4 ; not for ebp + add esp, 4 ; not for esp + pop dword [ecx + IA32_TSS._EBX] + pop dword [ecx + IA32_TSS._EDX] + pop dword [ecx + IA32_TSS._ECX] + pop dword [ecx + IA32_TSS._EAX] + +; Set single step DB# to allow debugger to able to go back to the EIP +; where the exception is triggered. + +;; Create return context for iretd in stub function + mov eax, dword [ecx + IA32_TSS._ESP] ; Get old stack pointer + mov ebx, dword [ecx + IA32_TSS.EIP] + mov [eax - 0xc], ebx ; create EIP in old stack + movzx ebx, word [ecx + IA32_TSS._CS] + mov [eax - 0x8], ebx ; create CS in old stack + mov ebx, dword [ecx + IA32_TSS.EFLAGS] + bts ebx, 8 + mov [eax - 0x4], ebx ; create eflags in old st= ack + mov dword [ecx + IA32_TSS.EFLAGS], ebx ; update eflags in old TSS + mov eax, dword [ecx + IA32_TSS._ESP] ; Get old stack pointer + sub eax, 0xc ; minus 12 byte + mov dword [ecx + IA32_TSS._ESP], eax ; Set new stack pointer + +;; Replace the EIP of interrupted task with stub function + mov eax, ASM_PFX(SingleStepStubFunction) + mov dword [ecx + IA32_TSS.EIP], eax + + mov ecx, [ebp - 8] ; Get current TSS base + mov eax, dword [ecx + IA32_TSS._ESP] ; Return current stack top + mov esp, ebp + + ret + +global ASM_PFX(SingleStepStubFunction) +ASM_PFX(SingleStepStubFunction): +; +; we need clean TS bit in CR0 to execute +; x87 FPU/MMX/SSE/SSE2/SSE3/SSSE3/SSE4 instructions. +; + clts + iretd + +global ASM_PFX(AsmGetTssTemplateMap) +ASM_PFX(AsmGetTssTemplateMap): + push ebp ; C prolog + mov ebp, esp + pushad + + mov ebx, dword [ebp + 0x8] + mov dword [ebx], ASM_PFX(ExceptionTaskSwtichEntry0) + mov dword [ebx + 0x4], (AsmExceptionEntryEnd - AsmExceptionEntryBegin)= / 32 + mov dword [ebx + 0x8], 0 + + popad + pop ebp + ret + diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c b/= UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c index 8d8d16ecbd..6dc0decbc5 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c @@ -177,4 +177,36 @@ RegisterCpuInterruptHandler ( ) { return EFI_UNSUPPORTED; -} \ No newline at end of file +} + +/** + Initializes all CPU exceptions entries with optional extra initializatio= ns. + + By default, this method should include all functionalities implemented by + InitializeCpuExceptionHandlers(), plus extra initialization works, if an= y. + This is could be done by calling InitializeCpuExceptionHandlers() direct= ly + in this method besides the extra works. + + InitDataEx is optional and its use and content are processor arch depend= ent. + The typical usage of it is to convey resources which have to be reserved + elsewhere and are necessary for the extra initializations of exception. + + @param[in] VectorInfo Pointer to reserved vector list. + @param[in] InitDataEx Pointer to data optional for extra initializat= ions + of exception. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized. + @retval EFI_INVALID_PARAMETER VectorInfo or InitDataEx contains invalid + content. + +**/ +EFI_STATUS +EFIAPI +InitializeCpuExceptionHandlersEx ( + IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, + IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL + ) +{ + return InitializeCpuExceptionHandlers (VectorInfo); +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandl= erLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandle= rLib.inf index 75443288a9..4c0d435136 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.i= nf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuExceptionHandlerLib.i= nf @@ -30,6 +30,7 @@ [Sources.Ia32] Ia32/ExceptionHandlerAsm.asm Ia32/ExceptionHandlerAsm.nasm + Ia32/ExceptionTssEntryAsm.nasm Ia32/ExceptionHandlerAsm.S Ia32/ArchExceptionHandler.c Ia32/ArchInterruptDefs.h diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c index af608bffb6..56b87efdc7 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c @@ -176,4 +176,36 @@ RegisterCpuInterruptHandler ( ) { return EFI_UNSUPPORTED; -} \ No newline at end of file +} + +/** + Initializes all CPU exceptions entries with optional extra initializatio= ns. + + By default, this method should include all functionalities implemented by + InitializeCpuExceptionHandlers(), plus extra initialization works, if an= y. + This is could be done by calling InitializeCpuExceptionHandlers() direct= ly + in this method besides the extra works. + + InitDataEx is optional and its use and content are processor arch depend= ent. + The typical usage of it is to convey resources which have to be reserved + elsewhere and are necessary for the extra initializations of exception. + + @param[in] VectorInfo Pointer to reserved vector list. + @param[in] InitDataEx Pointer to data optional for extra initializat= ions + of exception. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized. + @retval EFI_INVALID_PARAMETER VectorInfo or InitDataEx contains invalid + content. + +**/ +EFI_STATUS +EFIAPI +InitializeCpuExceptionHandlersEx ( + IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, + IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL + ) +{ + return InitializeCpuExceptionHandlers (VectorInfo); +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHa= ndlerLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException= HandlerLib.inf index d70a99c100..e5c03c16c9 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLi= b.inf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuExceptionHandlerLi= b.inf @@ -30,6 +30,7 @@ [Sources.Ia32] Ia32/ExceptionHandlerAsm.asm Ia32/ExceptionHandlerAsm.nasm + Ia32/ExceptionTssEntryAsm.nasm Ia32/ExceptionHandlerAsm.S Ia32/ArchExceptionHandler.c Ia32/ArchInterruptDefs.h diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandl= erLib.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandle= rLib.inf index 634ffcb21d..56b875b7c8 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.i= nf +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuExceptionHandlerLib.i= nf @@ -30,6 +30,7 @@ [Sources.Ia32] Ia32/ExceptionHandlerAsm.asm Ia32/ExceptionHandlerAsm.nasm + Ia32/ExceptionTssEntryAsm.nasm Ia32/ExceptionHandlerAsm.S Ia32/ArchExceptionHandler.c Ia32/ArchInterruptDefs.h diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c b/Uef= iCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c index 7414d3f773..0bdda6bcd0 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmException.c @@ -125,4 +125,36 @@ RegisterCpuInterruptHandler ( ) { return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandle= r, &mExceptionHandlerData); -} \ No newline at end of file +} + +/** + Initializes all CPU exceptions entries with optional extra initializatio= ns. + + By default, this method should include all functionalities implemented by + InitializeCpuExceptionHandlers(), plus extra initialization works, if an= y. + This is could be done by calling InitializeCpuExceptionHandlers() direct= ly + in this method besides the extra works. + + InitDataEx is optional and its use and content are processor arch depend= ent. + The typical usage of it is to convey resources which have to be reserved + elsewhere and are necessary for the extra initializations of exception. + + @param[in] VectorInfo Pointer to reserved vector list. + @param[in] InitDataEx Pointer to data optional for extra initializat= ions + of exception. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized. + @retval EFI_INVALID_PARAMETER VectorInfo or InitDataEx contains invalid + content. + +**/ +EFI_STATUS +EFIAPI +InitializeCpuExceptionHandlersEx ( + IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, + IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL + ) +{ + return InitializeCpuExceptionHandlers (VectorInfo); +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHan= dler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler= .c index 65f0cff680..5bc04656c7 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -112,6 +112,140 @@ ArchRestoreExceptionContext ( SystemContext.SystemContextX64->ExceptionData =3D ReservedVectors[Except= ionType].ExceptionData; } =20 +/** + Setup separate stack for given exceptions. + + @param[in] StackSwitchData Pointer to data required for setuping up + stack switch. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized with new stack. + @retval EFI_INVALID_PARAMETER StackSwitchData contains invalid content. + +**/ +EFI_STATUS +ArchSetupExcpetionStack ( + IN CPU_EXCEPTION_INIT_DATA_EX *StackSwitchData + ) +{ + IA32_DESCRIPTOR Gdtr; + IA32_DESCRIPTOR Idtr; + IA32_IDT_GATE_DESCRIPTOR *IdtTable; + IA32_TSS_DESCRIPTOR *TssDesc; + IA32_TASK_STATE_SEGMENT *Tss; + UINTN StackTop; + UINTN Index; + UINTN Vector; + UINTN TssBase; + UINTN GdtSize; + + if (StackSwitchData =3D=3D NULL || + StackSwitchData->Ia32.KnownGoodStackTop =3D=3D 0 || + StackSwitchData->Ia32.KnownGoodStackSize =3D=3D 0 || + StackSwitchData->Ia32.StackSwitchExceptions =3D=3D NULL || + StackSwitchData->Ia32.StackSwitchExceptionNumber =3D=3D 0 || + StackSwitchData->Ia32.GdtTable =3D=3D NULL || + StackSwitchData->Ia32.IdtTable =3D=3D NULL || + StackSwitchData->Ia32.TssDesc =3D=3D NULL || + StackSwitchData->Ia32.Tss =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // The caller is responsible for that the GDT table, no matter the exist= ing + // one or newly allocated, has enough space to hold descriptors for exce= ption + // task-state segments. + // + if (((UINTN)StackSwitchData->Ia32.GdtTable & (IA32_GDT_ALIGNMENT - 1)) != =3D 0) { + return EFI_INVALID_PARAMETER; + } + + if ((UINTN)StackSwitchData->Ia32.TssDesc < (UINTN)(StackSwitchData->Ia32= .GdtTable)) { + return EFI_INVALID_PARAMETER; + } + + if ((UINTN)StackSwitchData->Ia32.TssDesc >=3D + ((UINTN)(StackSwitchData->Ia32.GdtTable) + StackSwitchData->Ia32.Gdt= Size)) { + return EFI_INVALID_PARAMETER; + } + + GdtSize =3D (UINTN)StackSwitchData->Ia32.TssDesc - + (UINTN)(StackSwitchData->Ia32.GdtTable) + + sizeof (IA32_TSS_DESCRIPTOR); + if (GdtSize > StackSwitchData->Ia32.GdtSize) { + return EFI_INVALID_PARAMETER; + } + + // + // Initialize new GDT table and/or IDT table, if any + // + AsmReadIdtr (&Idtr); + AsmReadGdtr (&Gdtr); + if ((UINTN)StackSwitchData->Ia32.GdtTable !=3D Gdtr.Base) { + CopyMem (StackSwitchData->Ia32.GdtTable, (VOID *)Gdtr.Base, Gdtr.Limit= + 1); + Gdtr.Base =3D (UINTN)StackSwitchData->Ia32.GdtTable; + Gdtr.Limit =3D (UINT16)GdtSize - 1; + } + + if ((UINTN)StackSwitchData->Ia32.IdtTable !=3D Idtr.Base) { + Idtr.Base =3D (UINTN)StackSwitchData->Ia32.IdtTable; + } + + // + // Fixup current task descriptor. Task-state segment for current task wi= ll + // be filled by processor during task switching. + // + TssDesc =3D StackSwitchData->Ia32.TssDesc; + Tss =3D StackSwitchData->Ia32.Tss; + + TssBase =3D (UINTN)Tss; + TssDesc->Bits.LimitLow =3D sizeof(IA32_TASK_STATE_SEGMENT) - 1; + TssDesc->Bits.BaseLow =3D (UINT16)TssBase; + TssDesc->Bits.BaseMidl =3D (UINT8)(TssBase >> 16); + TssDesc->Bits.Type =3D IA32_GDT_TYPE_TSS; + TssDesc->Bits.P =3D 1; + TssDesc->Bits.LimitHigh =3D 0; + TssDesc->Bits.BaseMidh =3D (UINT8)(TssBase >> 24); + TssDesc->Bits.BaseHigh =3D (UINT32)(TssBase >> 32); + + // + // Fixup exception task descriptor and task-state segment + // + StackTop =3D StackSwitchData->Ia32.KnownGoodStackTop - CPU_STACK_ALIGNME= NT; + StackTop =3D (UINTN)ALIGN_POINTER (StackTop, CPU_STACK_ALIGNMENT); + IdtTable =3D StackSwitchData->Ia32.IdtTable; + for (Index =3D 0; Index < StackSwitchData->Ia32.StackSwitchExceptionNumb= er; ++Index) { + // + // Fixup IST + // + Tss->IST[Index] =3D StackTop; + StackTop -=3D StackSwitchData->Ia32.KnownGoodStackSize; + + // + // Set the IST field to enable corresponding IST + // + Vector =3D StackSwitchData->Ia32.StackSwitchExceptions[Index]; + IdtTable[Vector].Bits.Reserved_0 =3D (UINT8)(Index + 1); + } + + // + // Publish GDT + // + AsmWriteGdtr (&Gdtr); + + // + // Load current task + // + AsmWriteTr ((UINT16)((UINTN)StackSwitchData->Ia32.TssDesc - Gdtr.Base)); + + // + // Publish IDT + // + AsmWriteIdtr (&Idtr); + + return EFI_SUCCESS; +} + /** Display CPU information. =20 diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchInterruptDef= s.h b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchInterruptDefs.h index 906480134a..c88be46286 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchInterruptDefs.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchInterruptDefs.h @@ -43,4 +43,7 @@ typedef struct { UINT8 HookAfterStubHeaderCode[HOOKAFTER_STUB_SIZE]; } RESERVED_VECTORS_DATA; =20 +#define CPU_TSS_DESC_SIZE sizeof (IA32_TSS_DESCRIPTOR) +#define CPU_TSS_SIZE sizeof (IA32_TASK_STATE_SEGMENT) + #endif --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095868997787.7951284790781; Thu, 30 Nov 2017 18:37:48 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 4A6F92211B43D; Thu, 30 Nov 2017 18:33:16 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A3BC8220F3C2C for ; Thu, 30 Nov 2017 18:33:12 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:38 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:37 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199527" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:23 +0800 Message-Id: <20171201023728.4680-7-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 06/11] MdeModulePkg/CpuExceptionHandlerLibNull: Add new API implementation X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , Eric Dong , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > Newly added This patch add implementation of new API InitializeCpuExceptionHandlersEx introduced into CpuExceptionHandlerLib on behalf of Stack Guard feature. Cc: Star Zeng Cc: Eric Dong Cc: Jiewen Yao Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- .../CpuExceptionHandlerLibNull.c | 34 ++++++++++++++++++= ++++ 1 file changed, 34 insertions(+) diff --git a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHa= ndlerLibNull.c b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExcepti= onHandlerLibNull.c index cbe4768633..6ea71972aa 100644 --- a/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLi= bNull.c +++ b/MdeModulePkg/Library/CpuExceptionHandlerLibNull/CpuExceptionHandlerLi= bNull.c @@ -111,3 +111,37 @@ DumpCpuContext ( ) { } + +/** + Initializes all CPU exceptions entries with optional extra initializatio= ns. + + By default, this method should include all functionalities implemented by + InitializeCpuExceptionHandlers(), plus extra initialization works, if an= y. + This is could be done by calling InitializeCpuExceptionHandlers() direct= ly + in this method besides the extra works. + + InitDataEx is optional and its use and content are processor arch depend= ent. + The typical usage of it is to convey resources which have to be reserved + elsewhere and are necessary for the extra initializations of exception. + + @param[in] VectorInfo Pointer to reserved vector list. + @param[in] InitDataEx Pointer to data optional for extra initializat= ions + of exception. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized. + @retval EFI_INVALID_PARAMETER VectorInfo or InitDataEx contains invalid + content. + @retval EFI_UNSUPPORTED This function is not supported. + +**/ +EFI_STATUS +EFIAPI +InitializeCpuExceptionHandlersEx ( + IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, + IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL + ) +{ + return InitializeCpuExceptionHandlers (VectorInfo); +} + --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095871641568.1992445299765; Thu, 30 Nov 2017 18:37:51 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 83348221660CD; Thu, 30 Nov 2017 18:33:16 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B3B392211B438 for ; Thu, 30 Nov 2017 18:33:13 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:39 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:38 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199532" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:24 +0800 Message-Id: <20171201023728.4680-8-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 07/11] ArmPkg/ArmExceptionLib: Add implementation of new API X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , Leif Lindholm , Ard Biesheuvel MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > Newly added This patch add implementation of following new API introduced into CpuExceptionHandlerLib. Since this lib hasn't support Stack Guard and stack switch, the new method just calls original InitializeCpuExceptionHandlers. EFI_STATUS EFIAPI InitializeCpuExceptionHandlersEx ( IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL ); Cc: Leif Lindholm Cc: Ard Biesheuvel Cc: Jiewen Yao Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang Reviewed-by: Ard Biesheuvel --- ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c | 33 ++++++++++++++++++++= ++++ 1 file changed, 33 insertions(+) diff --git a/ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c b/ArmPkg/Libr= ary/ArmExceptionLib/ArmExceptionLib.c index e8ea1f159d..9fb4a05845 100644 --- a/ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c +++ b/ArmPkg/Library/ArmExceptionLib/ArmExceptionLib.c @@ -320,3 +320,36 @@ CommonCExceptionHandler( =20 DefaultExceptionHandler(ExceptionType, SystemContext); } + +/** + Initializes all CPU exceptions entries with optional extra initializatio= ns. + + By default, this method should include all functionalities implemented by + InitializeCpuExceptionHandlers(), plus extra initialization works, if an= y. + This is could be done by calling InitializeCpuExceptionHandlers() direct= ly + in this method besides the extra works. + + InitDataEx is optional and its use and content are processor arch depend= ent. + The typical usage of it is to convey resources which have to be reserved + elsewhere and are necessary for the extra initializations of exception. + + @param[in] VectorInfo Pointer to reserved vector list. + @param[in] InitDataEx Pointer to data optional for extra initializat= ions + of exception. + + @retval EFI_SUCCESS The exceptions have been successfully + initialized. + @retval EFI_INVALID_PARAMETER VectorInfo or InitDataEx contains invalid + content. + +**/ +EFI_STATUS +EFIAPI +InitializeCpuExceptionHandlersEx ( + IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, + IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL + ) +{ + return InitializeCpuExceptionHandlers (VectorInfo); +} + --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095874526627.7807490634353; Thu, 30 Nov 2017 18:37:54 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id B9F97221660D0; Thu, 30 Nov 2017 18:33:16 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id CB7F5221786B0 for ; Thu, 30 Nov 2017 18:33:14 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:40 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:39 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199537" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:25 +0800 Message-Id: <20171201023728.4680-9-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 08/11] UefiCpuPkg/MpLib: Add GDTR, IDTR and TR in saved AP data X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > No change > v2: > Add code to save/restore GDTR, IDTR and TR for AP. In current implementation of CPU MP service, AP is initialized with data copied from BSP. Stack switch required by Stack Guard feature needs differe= nt GDT, IDT table and task gates for each logic processor. This patch adds GDT= R, IDTR and TR into structure CPU_VOLATILE_REGISTERS and related code in save and restore methods. This can make sure that any changes to GDT, IDT and ta= sk gate for an AP will be kept from overwritten by BSP settings. Cc: Eric Dong Cc: Laszlo Ersek Cc: Jiewen Yao Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- UefiCpuPkg/Library/MpInitLib/MpLib.c | 17 +++++++++++++++++ UefiCpuPkg/Library/MpInitLib/MpLib.h | 3 +++ 2 files changed, 20 insertions(+) diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpIn= itLib/MpLib.c index 61b14c9843..0c2058a7b0 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c @@ -195,6 +195,10 @@ SaveVolatileRegisters ( VolatileRegisters->Dr6 =3D AsmReadDr6 (); VolatileRegisters->Dr7 =3D AsmReadDr7 (); } + + AsmReadGdtr (&VolatileRegisters->Gdtr); + AsmReadIdtr (&VolatileRegisters->Idtr); + VolatileRegisters->Tr =3D AsmReadTr (); } =20 /** @@ -211,6 +215,7 @@ RestoreVolatileRegisters ( ) { CPUID_VERSION_INFO_EDX VersionInfoEdx; + IA32_TSS_DESCRIPTOR *Tss; =20 AsmWriteCr0 (VolatileRegisters->Cr0); AsmWriteCr3 (VolatileRegisters->Cr3); @@ -231,6 +236,18 @@ RestoreVolatileRegisters ( AsmWriteDr7 (VolatileRegisters->Dr7); } } + + AsmWriteGdtr (&VolatileRegisters->Gdtr); + AsmWriteIdtr (&VolatileRegisters->Idtr); + if (VolatileRegisters->Tr !=3D 0 && + VolatileRegisters->Tr < VolatileRegisters->Gdtr.Limit) { + Tss =3D (IA32_TSS_DESCRIPTOR *)(VolatileRegisters->Gdtr.Base + + VolatileRegisters->Tr); + if (Tss->Bits.P =3D=3D 1) { + Tss->Bits.Type &=3D 0xD; // 1101 - Clear busy bit just in case + AsmWriteTr (VolatileRegisters->Tr); + } + } } =20 /** diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpIn= itLib/MpLib.h index d13d5c06f5..685e96cbac 100644 --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h @@ -102,6 +102,9 @@ typedef struct { UINTN Dr3; UINTN Dr6; UINTN Dr7; + IA32_DESCRIPTOR Gdtr; + IA32_DESCRIPTOR Idtr; + UINT16 Tr; } CPU_VOLATILE_REGISTERS; =20 // --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095876983821.6780871017078; Thu, 30 Nov 2017 18:37:56 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id F3E102211B43A; Thu, 30 Nov 2017 18:33:20 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id F019A2211B438 for ; Thu, 30 Nov 2017 18:33:15 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:41 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:40 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199543" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:26 +0800 Message-Id: <20171201023728.4680-10-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 09/11] UefiCpuPkg/CpuDxe: Initialize stack switch for MP X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Kinney , Jiewen Yao , Laszlo Ersek , Eric Dong MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > a. Remove definition of EXCEPTION_STACK_SWITCH_DATA. > Use CPU_EXCEPTION_INIT_DATA_EX defined in CpuExceptionHandlerLib.h > intead. > b. Call new API InitializeCpuExceptionHandlersEx instead to initialize > stack switch for BSP and AP > v2: > Add code to reserve resources and initialize AP exception with stack > switch besides BSP, if PcdCpuStackGuard is enabled. In current MP implementation, BSP and AP shares the same exception configuration. Stack switch required by Stack Guard feature needs that BSP and AP have their own configuration. This patch adds code to ask BSP and AP to do exception handler initialization separately. Since AP is not supposed to do memory allocation, all memory needed to setup stack switch will be reserved in BSP and pass to AP via new API EFI_STATUS EFIAPI InitializeCpuExceptionHandlersEx ( IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL, IN CPU_EXCEPTION_INIT_DATA_EX *InitDataEx OPTIONAL ); Following two new PCDs are introduced to configure how to setup new stack for specified exception handlers. gUefiCpuPkgTokenSpaceGuid.PcdCpuStackSwitchExceptionList gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize Cc: Eric Dong Cc: Laszlo Ersek Cc: Jiewen Yao Cc: Michael Kinney Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- UefiCpuPkg/CpuDxe/CpuDxe.inf | 3 + UefiCpuPkg/CpuDxe/CpuMp.c | 177 +++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 180 insertions(+) diff --git a/UefiCpuPkg/CpuDxe/CpuDxe.inf b/UefiCpuPkg/CpuDxe/CpuDxe.inf index 3e8d196739..02f86b774c 100644 --- a/UefiCpuPkg/CpuDxe/CpuDxe.inf +++ b/UefiCpuPkg/CpuDxe/CpuDxe.inf @@ -81,6 +81,9 @@ =20 [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask ##= CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard ##= CONSUMES + gUefiCpuPkgTokenSpaceGuid.PcdCpuStackSwitchExceptionList ##= CONSUMES + gUefiCpuPkgTokenSpaceGuid.PcdCpuKnownGoodStackSize ##= CONSUMES =20 [Depex] TRUE diff --git a/UefiCpuPkg/CpuDxe/CpuMp.c b/UefiCpuPkg/CpuDxe/CpuMp.c index b3c0178d07..2e388879fa 100644 --- a/UefiCpuPkg/CpuDxe/CpuMp.c +++ b/UefiCpuPkg/CpuDxe/CpuMp.c @@ -601,6 +601,178 @@ 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); +} + +/** + Initializes CPU exceptions handlers for the sake of stack switch require= ment. + + This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mai= nly + for the sake of AP's init because of EFI_AP_PROCEDURE API requirement. + + @param[in,out] Buffer The pointer to private data buffer. + +**/ +VOID +EFIAPI +InitializeExceptionStackSwitchHandlers ( + IN OUT VOID *Buffer + ) +{ + CPU_EXCEPTION_INIT_DATA_EX *EssData; + IA32_DESCRIPTOR Idtr; + EFI_STATUS Status; + + EssData =3D Buffer; + // + // We don't plan to replace IDT table with a new one, and 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; + Status =3D InitializeCpuExceptionHandlersEx (NULL, EssData); + ASSERT_EFI_ERROR (Status); +} + +/** + Initializes MP exceptions handlers for the sake of stack switch requirem= ent. + + This function will allocate required resources required to setup stack s= witch + and pass them through CPU_EXCEPTION_INIT_DATA_EX to each logic processor. + +**/ +VOID +InitializeMpExceptionStackSwitchHandlers ( + VOID + ) +{ + UINTN Index; + UINTN Bsp; + UINTN ExceptionNumber; + UINTN NewGdtSize; + UINTN NewStackSize; + IA32_DESCRIPTOR Gdtr; + CPU_EXCEPTION_INIT_DATA_EX EssData; + UINT8 *GdtBuffer; + UINT8 *StackTop; + + if (!PcdGetBool (PcdCpuStackGuard)) { + return; + } + + ExceptionNumber =3D FixedPcdGetSize (PcdCpuStackSwitchExceptionList); + NewStackSize =3D FixedPcdGet32 (PcdCpuKnownGoodStackSize) * ExceptionNum= ber; + + 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.InitDefaultHandlers =3D FALSE; + + EssData.Ia32.StackSwitchExceptions =3D FixedPcdGetPtr(PcdCpuStackSwitchE= xceptionList); + EssData.Ia32.StackSwitchExceptionNumber =3D ExceptionNumber; + EssData.Ia32.KnownGoodStackSize =3D FixedPcdGet32(PcdCpuKnownGoodStackSi= ze); + + MpInitLibWhoAmI (&Bsp); + for (Index =3D 0; Index < mNumberOfProcessors; ++Index) { + // + // To support stack switch, we need to re-construct GDT but not IDT. + // + if (Index =3D=3D Bsp) { + GetGdtr (&Gdtr); + } else { + // + // AP might have different size of GDT from BSP. + // + MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL= ); + } + + // + // 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 + // | | + // -------------------------------- + // + NewGdtSize =3D sizeof (IA32_TSS_DESCRIPTOR) + + (Gdtr.Limit + 1) + + sizeof (IA32_TSS_DESCRIPTOR) * (ExceptionNumber + 1) + + sizeof (IA32_TASK_STATE_SEGMENT) * (ExceptionNumber + 1); + GdtBuffer =3D AllocateRuntimeZeroPool (NewGdtSize); + ASSERT (GdtBuffer !=3D NULL); + + EssData.Ia32.GdtTable =3D ALIGN_POINTER(GdtBuffer, sizeof (IA32_TSS_DE= SCRIPTOR)); + NewGdtSize -=3D ((UINT8 *)EssData.Ia32.GdtTable - GdtBuffer); + EssData.Ia32.GdtSize =3D NewGdtSize; + + EssData.Ia32.TssDesc =3D ((UINT8 *)EssData.Ia32.GdtTable + Gdtr.Limit = + 1); + EssData.Ia32.Tss =3D ((UINT8 *)EssData.Ia32.GdtTable + Gdtr.Limit + 1 + + sizeof (IA32_TSS_DESCRIPTOR) * (ExceptionNumber + = 1)); + + EssData.Ia32.KnownGoodStackTop =3D (UINTN)StackTop; + DEBUG ((DEBUG_INFO, + "Exception stack top[cpu%lu]: 0x%lX\n", + (UINT64)(UINTN)Index, + (UINT64)(UINTN)StackTop)); + + if (Index =3D=3D Bsp) { + InitializeExceptionStackSwitchHandlers (&EssData); + } else { + MpInitLibStartupThisAP ( + InitializeExceptionStackSwitchHandlers, + Index, + NULL, + 0, + (VOID *)&EssData, + NULL + ); + } + + StackTop -=3D NewStackSize; + } +} + /** Initialize Multi-processor support. =20 @@ -624,6 +796,11 @@ InitializeMpSupport ( mNumberOfProcessors =3D NumberOfProcessors; DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mNumberOfProcessors)); =20 + // + // Initialize exception stack switch handlers for each logic processor. + // + InitializeMpExceptionStackSwitchHandlers (); + // // Update CPU healthy information from Guided HOB // --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095879648967.4700972132441; Thu, 30 Nov 2017 18:37:59 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 3922A221660DC; Thu, 30 Nov 2017 18:33:21 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id F0A83220F3C5A for ; Thu, 30 Nov 2017 18:33:16 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:42 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:42 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199551" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:27 +0800 Message-Id: <20171201023728.4680-11-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 10/11] MdeModulePkg/Core/Dxe: Call new API InitializeCpuExceptionHandlersEx instead X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , Eric Dong , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" > v3: > Newly added Original API InitializeCpuExceptionHandlers is used in DxeMain to initialize exception handlers but it does not support setting up stack switch required by Stack Guard feature. Using the new API instead to make sure Stack Guard feature is applicable to most part of code. Since this API is called before memory service initialization, there's no way to call AllocateXxx API to reserve memory. Global variables are used for this special case. GDT table is reserved at least 2KB which should be big enough for all current use cases. Cc: Star Zeng Cc: Eric Dong Cc: Jiewen Yao Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c b/MdeModulePkg/Core/Dx= e/DxeMain/DxeMain.c index 433cca3a80..62a597edaf 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c +++ b/MdeModulePkg/Core/Dxe/DxeMain/DxeMain.c @@ -258,7 +258,7 @@ DxeMain ( if (GuidHob !=3D NULL) { VectorInfoList =3D (EFI_VECTOR_HANDOFF_INFO *) (GET_GUID_HOB_DATA(Guid= Hob)); } - Status =3D InitializeCpuExceptionHandlers (VectorInfoList); + Status =3D InitializeCpuExceptionHandlersEx (VectorInfoList, NULL); ASSERT_EFI_ERROR (Status); =20 // --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Tue Dec 24 13:08:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1512095882540438.1075473961291; Thu, 30 Nov 2017 18:38:02 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 71F4E221660E6; Thu, 30 Nov 2017 18:33:21 -0800 (PST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 03AEA220F3C5A for ; Thu, 30 Nov 2017 18:33:17 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 30 Nov 2017 18:37:43 -0800 Received: from jwang36-mobl2.ccr.corp.intel.com ([10.239.192.33]) by fmsmga004.fm.intel.com with ESMTP; 30 Nov 2017 18:37:43 -0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.43; helo=mga05.intel.com; envelope-from=jian.j.wang@intel.com; receiver=edk2-devel@lists.01.org X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,343,1508828400"; d="scan'208";a="8199555" From: Jian J Wang To: edk2-devel@lists.01.org Date: Fri, 1 Dec 2017 10:37:28 +0800 Message-Id: <20171201023728.4680-12-jian.j.wang@intel.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20171201023728.4680-1-jian.j.wang@intel.com> References: <20171201023728.4680-1-jian.j.wang@intel.com> Subject: [edk2] [PATCH v3 11/11] MdeModulePkg/DxeIpl: Enable paging for Stack Guard X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , Eric Dong , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Stack guard feature makes use of paging mechanism to monitor if there's a stack overflow occurred during boot. This patch will check setting of PCD PcdCpuStackGuard. If it's TRUE, DxeIpl will setup page table and set the page at which the stack base locates to be NOT PRESENT. If stack is used up and memory access cross into the last page of it, #PF exception will be triggered. Cc: Star Zeng Cc: Eric Dong Cc: Jiewen Yao Suggested-by: Ayellet Wolman Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang --- MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf | 5 ++- MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c | 4 ++ MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c | 1 + MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c | 51 ++++++++++++++++++--= ---- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf b/MdeModulePkg/Core/Dx= eIplPeim/DxeIpl.inf index a1b8748432..ba1d9c6b05 100644 --- a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf +++ b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf @@ -49,7 +49,7 @@ [Sources.X64] X64/VirtualMemory.h X64/VirtualMemory.c - X64/DxeLoadFunc.c =20 + X64/DxeLoadFunc.c =20 [Sources.IPF] Ipf/DxeLoadFunc.c @@ -117,6 +117,7 @@ gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrMask ##= CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask ##= CONSUMES gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask ##= CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard ##= CONSUMES =20 [Pcd.IA32,Pcd.X64,Pcd.ARM,Pcd.AARCH64] gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack ## SOMETIM= ES_CONSUMES @@ -132,7 +133,7 @@ # # [Hob] # MEMORY_ALLOCATION ## SOMETIMES_PRODUCES # MEMORY_ALLOCAT= ION_MODULE for DxeCore -# MEMORY_ALLOCATION ## SOMETIMES_PRODUCES # New Stack HoB = =20 +# MEMORY_ALLOCATION ## SOMETIMES_PRODUCES # New Stack HoB # MEMORY_ALLOCATION ## SOMETIMES_PRODUCES # Old Stack HOB # # [Hob.IPF] diff --git a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c b/MdeModulePkg= /Core/DxeIplPeim/Ia32/DxeLoadFunc.c index 5649265367..441096ad0f 100644 --- a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c +++ b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c @@ -235,6 +235,10 @@ ToBuildPageTable ( return TRUE; } =20 + if (PcdGetBool (PcdCpuStackGuard)) { + return TRUE; + } + if (PcdGetBool (PcdSetNxForStack) && IsExecuteDisableBitAvailable ()) { return TRUE; } diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c b/MdeModulePkg/= Core/DxeIplPeim/X64/DxeLoadFunc.c index f613221b81..b75a4489bf 100644 --- a/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c +++ b/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c @@ -95,6 +95,7 @@ HandOffToDxeCore ( // for the DxeIpl and the DxeCore are both X64. // ASSERT (PcdGetBool (PcdSetNxForStack) =3D=3D FALSE); + ASSERT (PcdGetBool (PcdCpuStackGuard) =3D=3D FALSE); } =20 // diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c b/MdeModulePk= g/Core/DxeIplPeim/X64/VirtualMemory.c index 29b6205e88..a2466b7766 100644 --- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c +++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c @@ -117,6 +117,39 @@ EnableExecuteDisableBit ( AsmWriteMsr64 (0xC0000080, MsrRegisters); } =20 +/** + The function will check if page table entry should be splitted to smaller + granularity. + + @retval TRUE Page table should be created. + @retval FALSE Page table should not be created. +**/ +BOOLEAN +ToSplitPageTable ( + IN EFI_PHYSICAL_ADDRESS Address, + IN UINTN Size, + IN EFI_PHYSICAL_ADDRESS StackBase, + IN UINTN StackSize + ) +{ + if (IsNullDetectionEnabled () && Address =3D=3D 0) { + return TRUE; + } + + if (PcdGetBool (PcdCpuStackGuard)) { + if (StackBase >=3D Address && StackBase < (Address + Size)) { + return TRUE; + } + } + + if (PcdGetBool (PcdSetNxForStack)) { + if ((Address < StackBase + StackSize) && ((Address + Size) > StackBase= )) { + return TRUE; + } + } + + return FALSE; +} /** Split 2M page to 4K. =20 @@ -160,7 +193,8 @@ Split2MPageTo4K ( PageTableEntry->Uint64 =3D (UINT64) PhysicalAddress4K | AddressEncMask; PageTableEntry->Bits.ReadWrite =3D 1; =20 - if (IsNullDetectionEnabled () && PhysicalAddress4K =3D=3D 0) { + if ((IsNullDetectionEnabled () && PhysicalAddress4K =3D=3D 0) || + (PcdGetBool (PcdCpuStackGuard) && PhysicalAddress4K =3D=3D StackBa= se)) { PageTableEntry->Bits.Present =3D 0; } else { PageTableEntry->Bits.Present =3D 1; @@ -214,10 +248,7 @@ Split1GPageTo2M ( =20 PhysicalAddress2M =3D PhysicalAddress; for (IndexOfPageDirectoryEntries =3D 0; IndexOfPageDirectoryEntries < 51= 2; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress2M += =3D SIZE_2MB) { - if ((IsNullDetectionEnabled () && PhysicalAddress2M =3D=3D 0) - || (PcdGetBool (PcdSetNxForStack) - && (PhysicalAddress2M < StackBase + StackSize) - && ((PhysicalAddress2M + SIZE_2MB) > StackBase))) { + if (ToSplitPageTable (PhysicalAddress2M, SIZE_2MB, StackBase, StackSiz= e)) { // // Need to split this 2M page that covers NULL or stack range. // @@ -359,10 +390,7 @@ CreateIdentityMappingPageTables ( PageDirectory1GEntry =3D (VOID *) PageDirectoryPointerEntry; =20 for (IndexOfPageDirectoryEntries =3D 0; IndexOfPageDirectoryEntries = < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += =3D SIZE_1GB) { - if ((IsNullDetectionEnabled () && PageAddress =3D=3D 0) - || (PcdGetBool (PcdSetNxForStack) - && (PageAddress < StackBase + StackSize) - && ((PageAddress + SIZE_1GB) > StackBase))) { + if (ToSplitPageTable (PageAddress, SIZE_1GB, StackBase, StackSize)= ) { Split1GPageTo2M (PageAddress, (UINT64 *) PageDirectory1GEntry, S= tackBase, StackSize); } else { // @@ -391,10 +419,7 @@ CreateIdentityMappingPageTables ( PageDirectoryPointerEntry->Bits.Present =3D 1; =20 for (IndexOfPageDirectoryEntries =3D 0; IndexOfPageDirectoryEntrie= s < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += =3D SIZE_2MB) { - if ((IsNullDetectionEnabled () && PageAddress =3D=3D 0) - || (PcdGetBool (PcdSetNxForStack) - && (PageAddress < StackBase + StackSize) - && ((PageAddress + SIZE_2MB) > StackBase))) { + if (ToSplitPageTable (PageAddress, SIZE_2MB, StackBase, StackSiz= e)) { // // Need to split this 2M page that covers NULL or stack range. // --=20 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel