From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mx.groups.io with SMTP id smtpd.web09.6129.1665748691086868597 for ; Fri, 14 Oct 2022 04:58:11 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=m5tOO2li; spf=pass (domain: intel.com, ip: 192.55.52.151, mailfrom: ray.ni@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1665748691; x=1697284691; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=22X8fvD5IlyPvNhmtjNuoDmORJzytz9zxz057DRViMA=; b=m5tOO2liYed+P4ceyfoB2DqNoFJiBmaq6fDQm9pQvnxshFqna9L/ilWj XK9wWRvU+jma13aUmiAOf6WBnNdXofiE4NCDw6ibJzUiV0iF0mFXx6ReN nrd5hD3P68IvzRchDp8NKuLOvrNtcV027VRQxf46koAGGHwOaaegvMpCC KGtnAtHURJMbaetrTHr3Nn+Bvo1XM6pSmo3iffeSyQ/Kd4glXCh1tuIH/ mcm14/d2BkwUPQUbtJXq63YOC9ACEjz79Jo/geMpf+LEuKt2WaJc28Qvu Vq/vALgum8jwx3OVMq6WAjyvuPCWXwSCryZR+H68qvPXraVapLb/OyXMA w==; X-IronPort-AV: E=McAfee;i="6500,9779,10499"; a="285746985" X-IronPort-AV: E=Sophos;i="5.95,184,1661842800"; d="scan'208";a="285746985" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Oct 2022 04:58:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10499"; a="627549912" X-IronPort-AV: E=Sophos;i="5.95,184,1661842800"; d="scan'208";a="627549912" Received: from fmsmsx601.amr.corp.intel.com ([10.18.126.81]) by orsmga002.jf.intel.com with ESMTP; 14 Oct 2022 04:58:10 -0700 Received: from fmsmsx612.amr.corp.intel.com (10.18.126.92) by fmsmsx601.amr.corp.intel.com (10.18.126.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 14 Oct 2022 04:58:09 -0700 Received: from fmsmsx608.amr.corp.intel.com (10.18.126.88) by fmsmsx612.amr.corp.intel.com (10.18.126.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 14 Oct 2022 04:58:09 -0700 Received: from fmsedg601.ED.cps.intel.com (10.1.192.135) by fmsmsx608.amr.corp.intel.com (10.18.126.88) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31 via Frontend Transport; Fri, 14 Oct 2022 04:58:09 -0700 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (104.47.66.40) by edgegateway.intel.com (192.55.55.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2375.31; Fri, 14 Oct 2022 04:58:08 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=izfhYmE39IZeXJduyVohQz9n8v2Qr3UcdzgwpOxhH/70AwsFSjZzvIG1NB23cTNcZ5eTias7HdcURgLg3S8U8WhrgIC3Gt6UX1hfzv6AkFeO+2DQNQrqliRPndiIoWyc4bNf6CkOR1FwvN3bhPlPquTrc8Bwd87u+AyuP/gc7rIt/uVEBpXwmpm11z104sJP+lxk4ShSS/wwBbOBxz2LEVja1S1st7FfRv/BUWraTRcufPzcN13CXGQINM7bH7/hjt8aawBKZFFy0JAraDZTCUUiJ7aGOUvXGf0xCDu7PBaz+KcyGjRtZFNPCAhRgxXoc9bKvqCJQs6d4nwBhPXyjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ilJl0q6F7BQu53McNQ89rrrHNMOdua0TxMhHIODTVxE=; b=IZrYMlxDGTvr6xcNFqTWeXzI+F4SrkasYOW1/v0wlAly63i/OJtqu5cYQD6KosWnX4/IVjeBu2B98m0Xpcq17T9wx67+5H6l0FWHE4LdJkJBmRE4xx0RjT95aZDVtRdPt7k1HEQe1UAc8mcekm/GMviq2CuP0/1/2u50QaLtlMU2UKW3enf89CpRe9fxBLAgF5dyBmNAsNuXdGDy3IlJIG6CWk8+/TQCxg6E13jpXIVXCKHa1MUCpgEOFt79zAGRlJmfG4bGks89ZaaF4Pp3s3tELRMFDSHX5U9376UxOZyjdcNy5Mf5dNaE8D4f8mnGJCQBKOEgTh37fPft/A5ueQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from MWHPR11MB1631.namprd11.prod.outlook.com (2603:10b6:301:10::10) by DM4PR11MB5485.namprd11.prod.outlook.com (2603:10b6:5:388::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.29; Fri, 14 Oct 2022 11:58:06 +0000 Received: from MWHPR11MB1631.namprd11.prod.outlook.com ([fe80::483f:4bb5:a15f:f571]) by MWHPR11MB1631.namprd11.prod.outlook.com ([fe80::483f:4bb5:a15f:f571%11]) with mapi id 15.20.5723.029; Fri, 14 Oct 2022 11:58:06 +0000 From: "Ni, Ray" To: "Tan, Dun" , "devel@edk2.groups.io" CC: "Dong, Eric" , "Kumar, Rahul R" Subject: Re: [Patch V3 2/4] UefiCpuPkg: Add Unit tests for PeiCpuExceptionHandlerLib Thread-Topic: [Patch V3 2/4] UefiCpuPkg: Add Unit tests for PeiCpuExceptionHandlerLib Thread-Index: AQHY365DRFb28PhxTkG9tw00nyJvea4NyQkQ Date: Fri, 14 Oct 2022 11:58:06 +0000 Message-ID: References: <20221014091931.847-1-dun.tan@intel.com> <20221014091931.847-3-dun.tan@intel.com> In-Reply-To: <20221014091931.847-3-dun.tan@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MWHPR11MB1631:EE_|DM4PR11MB5485:EE_ x-ms-office365-filtering-correlation-id: ef9ea890-a396-4b5d-914b-08daaddb5b60 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: wdm77o5lJCpchetr6Eqj5VFHApSQ4EhKnQgUTU810oGjrSFYgmRoyKpByGG5uav9NdSYms7yzv0nGIYBqy++GkRfxn7AkFL4SslatkUKWKwtMDRKZQRihWx8l+MeUXZtONmNmAEg4oYgEyucIOXEmo5tGcw+bhk3IB/VEcPBWXm5IMq5jyzT4641h23Wn2fbafm/6gboCJoCsusfQJYSI4nzI0V/i7OgkuuVBdHGX3gLcmbo7P3a5f13V5XNJtvV2h385Mt+vcR1a3hOojNnndNqjYTJShZaNhMcnSjeOPEp8qAmNi1svbhCgPVZnaryzPw3yvfce14pCpZkmRMZAEBhYApQBv0ar3+nRUSEi28AEgTdQs6CTzf3/CgcXZubd0/6faETHMyGTIROGxKbGwd09MIol1aaflSsr8IkVfj/QCxy6hxUkuS1mffwKAWUrPZiwN7X67+xoGK+hOMm/zO17Ll6B6VnnPu34g0Pe5cY6JLN6r9OQ744xhmVyaryVK3jCzNIosFPwnqOk4Wqw3R9IZE5u4d0lvJklQcBBxFQVU6p5QZifin2Ff0/w5oI7kTryDnRl5PV7XqJjGWR8P5KvWi6UwGIgkmnDAVl2KmUsTKBNjz1FhOh0C8KF/cezjdhjWbU9dYmRVIp99KjSBm1W9HXU/DT0+FsamNlc8ACZJxGxqSkd6bTFQ/GRMt9PdX6oXJcHrOJfW5HuMuWvs7rbs8z9iQvm5THO/d1UISFruGUGS8OvOBw9lSd73VUsisWz1CNFXPSsxZM+p/oIyeZJSViDwHh/3H9UupXLNk= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MWHPR11MB1631.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(366004)(39860400002)(396003)(376002)(346002)(136003)(451199015)(478600001)(66446008)(64756008)(53546011)(8936002)(66556008)(66476007)(41300700001)(9686003)(5660300002)(33656002)(82960400001)(26005)(2906002)(71200400001)(4326008)(55016003)(8676002)(66946007)(38070700005)(76116006)(83380400001)(316002)(30864003)(54906003)(7696005)(38100700002)(19627235002)(86362001)(6506007)(52536014)(186003)(122000001)(107886003)(110136005)(213903007)(559001)(579004);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?XBgKu5ARDFmnR4jdL+aRVXTnf/xbYGNMYARx+0pfqzWGhgqeSGXwfE/ROp7S?= =?us-ascii?Q?Io6Bw7tUQwpknYSEGSrEX5mzXBbBC3gIQD+GU54OqnTAD6hbJVrvzV34IiUJ?= =?us-ascii?Q?0fcTjzqUGZHMtNJaR4cWkuFNN6KsGnYN+R1adqiO2Mmvujo85xLXsgKh1amh?= =?us-ascii?Q?Rajypx/VtwuJm6SuYQSKwlArS728XK+TAi0hQOju0G6DAySRVtM8p1l5zin+?= =?us-ascii?Q?NXKZGwdYd4nz942Jxzfspub0FcWyctaK4h9QEAanlLoNkx0sbLy5gFocW3Ad?= =?us-ascii?Q?y0D6r9ixYr5wGepiLbRyACbSopm2nCWxnQy1/OArzMAP8yzsgoB0g1xraWdE?= =?us-ascii?Q?C8Qsu0OgkwyrBW9lwHBtdeyA+/Hk9vnrGeqjs/sDtruAL/C776KWVVRE2ZuC?= =?us-ascii?Q?AluMQR7vsMmHm6evGn9JZeDw6Ba/BfQD/Zi8lGnhcxvCW9NNhpnwuxsXHOJC?= =?us-ascii?Q?E/t+fce5xYrZViupNCA/yNBatgwV3mzKGEzXGxv0QZXFbi4e4F69zTkIPl/S?= =?us-ascii?Q?qNZe6S7PAODNzXGgY2kwTXkHLwEcJAOfHC8+4Lf84XeVgNmdDMWZGw1OiCeE?= =?us-ascii?Q?yHZTK6dfWNzh636qUi80NfkWN85auqrlWeEii3UiQmDr0EQINyltgzY6OQ02?= =?us-ascii?Q?jE3Dl56dWc1sjUgm5dF1y0ec7rHEHlhBZrw+kinorrBtVepXKhXVfH640gQ+?= =?us-ascii?Q?6GL7dH9Hx0KrNbZY4b4vnI4jTUmSD1rUB5ZSwQ1zbQYf/5r0vyLNm2FEDuNE?= =?us-ascii?Q?R8GV+oc//+yfdTx/Q3SE7DXpsvKEsFMrN6rptUJfA/RaBYQRy0hQFv+0Q1f+?= =?us-ascii?Q?0rcWDPKaNQgLKB+roU4iH+5/mB9kUCN2FqZQasrYD9Hbuwi+BanYR+EojplE?= =?us-ascii?Q?Fr59PZI+gOlcHWUt8pQ8Hd8yeGUhwYsvRVToTuol1vigoMhe6vQyu/2ntaDi?= =?us-ascii?Q?faGzZCfrC/VTS54CjkchcE2slXKNQpjMj3yQoD5LicTO+WYB/XCuNyrLSjul?= =?us-ascii?Q?jSuET66aBtj+LHdjhx92vy0PKLwwaHnNYaeLya/O1IUDAQajRDlqlsImOu+s?= =?us-ascii?Q?kl1VOENYl0jf5AsRbD+qUVaWg2tJLZsjla8MTJmZLD5o8PyrHFSS8JgfcuMN?= =?us-ascii?Q?LjGcc5JkkJsblqhw2b5/p9kDZeeUKyyxSz4yW3PK5EJV6jMLSWBRIK1BAMb0?= =?us-ascii?Q?eVa1mfklaX74gMRiE68fjS7tEc1VintBKGueehbd//zM41zmRya+qCH52p4b?= =?us-ascii?Q?DdnI+wIIjzkTJORv3IF4az1b3DxAKDVdVSEVvNvJfZn6LoGXHSxIZeXnvfGa?= =?us-ascii?Q?X5+XmZXAJzQReI9/aLvR1S/GkGUNTgE4rZ5/u+6Qk/rGAgy70lMgfKCo0RRL?= =?us-ascii?Q?De6dtGrfqtyoTdUNqi8tTWTpkKZvPK3E6m9uOnUqXUOs4SDCvFFARrhghtFp?= =?us-ascii?Q?m3GK39iGkPCaE7uk5EXZFSqODjUPouznWIENlIeh7mKDDSShBqmMj+ZkbaLC?= =?us-ascii?Q?NKRmEVDypj1zesUC6nIBOkXMlsrap4SYlObFv+ydbUHjefAqCZExSqxv/KVX?= =?us-ascii?Q?2waLoG1QqHmq2lJ1qwk=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MWHPR11MB1631.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: ef9ea890-a396-4b5d-914b-08daaddb5b60 X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Oct 2022 11:58:06.8688 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: VoXbP0VnS/m6HwY5IOaPENDUZlvaLnUh29gnuYHNuso7wg0+5Ga/c/C8Vm58m6gHvBHxBm8c3dcrez080gOzLg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR11MB5485 Return-Path: ray.ni@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Ray Ni > -----Original Message----- > From: Tan, Dun > Sent: Friday, October 14, 2022 5:19 PM > To: devel@edk2.groups.io > Cc: Dong, Eric ; Ni, Ray ; Kumar, = Rahul R > Subject: [Patch V3 2/4] UefiCpuPkg: Add Unit tests for PeiCpuExceptionHan= dlerLib >=20 > The previous change adds unit test for DxeCpuExeptionHandlerLib > in 64bit mode. This change create a PEIM to add unit test for > PeiCpuExceptionHandlerLib based on previous change.It can run > in both 32bit and 64bit modes. >=20 > Signed-off-by: Dun Tan > Cc: Eric Dong > Cc: Ray Ni > Cc: Rahul Kumar > --- > UefiCpuPkg/CpuExceptionHandlerUnitTest/CpuExceptionHandlerTest.h = | 9 +++++++++ > UefiCpuPkg/CpuExceptionHandlerUnitTest/Ia32/ArchExceptionHandlerTest.c = | 135 > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++++++++++++++++++ > ++++++++++++++++++++++++++++++++++++++++++ > UefiCpuPkg/CpuExceptionHandlerUnitTest/Ia32/ArchExceptionHandlerTestAsm.= nasm | 208 > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++++++++++++++++++ > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++++++++++++++++++ > ++++++++++++++++++++++ > UefiCpuPkg/CpuExceptionHandlerUnitTest/PeiCpuExceptionHandlerLibUnitTest= .inf | 61 > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > UefiCpuPkg/CpuExceptionHandlerUnitTest/PeiCpuExceptionHandlerUnitTest.c = | 204 > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++++++++++++++++++ > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++++++++++++++++++ > ++++++++++++++++++ > 5 files changed, 617 insertions(+) >=20 > diff --git a/UefiCpuPkg/CpuExceptionHandlerUnitTest/CpuExceptionHandlerTe= st.h > b/UefiCpuPkg/CpuExceptionHandlerUnitTest/CpuExceptionHandlerTest.h > index 936098fde8..bad3387db5 100644 > --- a/UefiCpuPkg/CpuExceptionHandlerUnitTest/CpuExceptionHandlerTest.h > +++ b/UefiCpuPkg/CpuExceptionHandlerUnitTest/CpuExceptionHandlerTest.h > @@ -93,6 +93,15 @@ typedef struct { > UINT64 R15; > } GENERAL_REGISTER; >=20 > +typedef struct { > + UINT32 Edi; > + UINT32 Esi; > + UINT32 Ebx; > + UINT32 Edx; > + UINT32 Ecx; > + UINT32 Eax; > +} GENERAL_REGISTER_IA32; > + > extern UINTN mFaultInstructionLength; > extern EFI_EXCEPTION_TYPE mExceptionType; > extern UINTN mRspAddress[]; > diff --git a/UefiCpuPkg/CpuExceptionHandlerUnitTest/Ia32/ArchExceptionHan= dlerTest.c > b/UefiCpuPkg/CpuExceptionHandlerUnitTest/Ia32/ArchExceptionHandlerTest.c > new file mode 100644 > index 0000000000..8bb27249dc > --- /dev/null > +++ b/UefiCpuPkg/CpuExceptionHandlerUnitTest/Ia32/ArchExceptionHandlerTes= t.c > @@ -0,0 +1,135 @@ > +/** @file > + Unit tests of the CpuExceptionHandlerLib. > + > + Copyright (c) 2022, Intel Corporation. All rights reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include "CpuExceptionHandlerTest.h" > + > +GENERAL_REGISTER_IA32 mActualContextInHandler; > +GENERAL_REGISTER_IA32 mActualContextAfterException; > + > +// > +// In TestCpuContextConsistency, Cpu registers will be set to > mExpectedContextInHandler/mExpectedContextAfterException. > +// Ecx in mExpectedContextInHandler is set runtime since Ecx is needed i= n assembly code. > +// For GP and PF, Ecx is set to FaultParameter. For other exception trig= gered by INTn, Ecx is set to ExceptionType. > +// > +GENERAL_REGISTER_IA32 mExpectedContextInHandler =3D { 1, 2, 3, 4, = 5, 0 }; > +GENERAL_REGISTER_IA32 mExpectedContextAfterException =3D { 11, 12, 13, = 14, 15, 16 }; > + > +/** > + Special handler for fault exception. > + Rip/Eip in SystemContext will be modified to the instruction after the= exception instruction. > + > + @param ExceptionType Exception type. > + @param SystemContext Pointer to EFI_SYSTEM_CONTEXT. > + > +**/ > +VOID > +EFIAPI > +AdjustRipForFaultHandler ( > + IN EFI_EXCEPTION_TYPE ExceptionType, > + IN EFI_SYSTEM_CONTEXT SystemContext > + ) > +{ > + mExceptionType =3D ExceptionType; > + SystemContext.SystemContextIa32->Eip +=3D mFaultInstructionLength; > +} > + > +/** > + Special handler for ConsistencyOfCpuContext test case. > + > + @param ExceptionType Exception type. > + @param SystemContext Pointer to EFI_SYSTEM_CONTEXT. > +**/ > +VOID > +EFIAPI > +AdjustCpuContextHandler ( > + IN EFI_EXCEPTION_TYPE ExceptionType, > + IN EFI_SYSTEM_CONTEXT SystemContext > + ) > +{ > + // > + // Store SystemContext in exception handler. > + // > + mActualContextInHandler.Edi =3D SystemContext.SystemContextIa32->Edi; > + mActualContextInHandler.Esi =3D SystemContext.SystemContextIa32->Esi; > + mActualContextInHandler.Ebx =3D SystemContext.SystemContextIa32->Ebx; > + mActualContextInHandler.Edx =3D SystemContext.SystemContextIa32->Edx; > + mActualContextInHandler.Ecx =3D SystemContext.SystemContextIa32->Ecx; > + mActualContextInHandler.Eax =3D SystemContext.SystemContextIa32->Eax; > + > + // > + // Modify cpu context. These registers will be stored in mActualContex= tAfterException. > + // Do not handle Esp and Ebp in SystemContext. CpuExceptionHandlerLib = doesn't set Esp and > + // Esp register to the value in SystemContext. > + // > + SystemContext.SystemContextIa32->Edi =3D mExpectedContextAfterExceptio= n.Edi; > + SystemContext.SystemContextIa32->Esi =3D mExpectedContextAfterExceptio= n.Esi; > + SystemContext.SystemContextIa32->Ebx =3D mExpectedContextAfterExceptio= n.Ebx; > + SystemContext.SystemContextIa32->Edx =3D mExpectedContextAfterExceptio= n.Edx; > + SystemContext.SystemContextIa32->Ecx =3D mExpectedContextAfterExceptio= n.Ecx; > + SystemContext.SystemContextIa32->Eax =3D mExpectedContextAfterExceptio= n.Eax; > + > + // > + // When fault exception happens, eip/rip points to the faulting instru= ction. > + // For now, olny GP and PF are tested in fault exception. > + // > + if ((ExceptionType =3D=3D EXCEPT_IA32_PAGE_FAULT) || (ExceptionType = =3D=3D EXCEPT_IA32_GP_FAULT)) { > + AdjustRipForFaultHandler (ExceptionType, SystemContext); > + } > +} > + > +/** > + Compare cpu context in ConsistencyOfCpuContext test case. > + 1.Compare mActualContextInHandler with mExpectedContextInHandler. > + 2.Compare mActualContextAfterException with mExpectedContextAfterExcep= tion. > + > + @retval UNIT_TEST_PASSED The Unit test has completed and = it was successful. > + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed= . > +**/ > +UNIT_TEST_STATUS > +CompareCpuContext ( > + VOID > + ) > +{ > + UT_ASSERT_EQUAL (mActualContextInHandler.Edi, mExpectedContextInHandle= r.Edi); > + UT_ASSERT_EQUAL (mActualContextInHandler.Esi, mExpectedContextInHandle= r.Esi); > + UT_ASSERT_EQUAL (mActualContextInHandler.Ebx, mExpectedContextInHandle= r.Ebx); > + UT_ASSERT_EQUAL (mActualContextInHandler.Edx, mExpectedContextInHandle= r.Edx); > + UT_ASSERT_EQUAL (mActualContextInHandler.Ecx, mExpectedContextInHandle= r.Ecx); > + UT_ASSERT_EQUAL (mActualContextInHandler.Eax, mExpectedContextInHandle= r.Eax); > + > + UT_ASSERT_EQUAL (mActualContextAfterException.Edi, mExpectedContextAft= erException.Edi); > + UT_ASSERT_EQUAL (mActualContextAfterException.Esi, mExpectedContextAft= erException.Esi); > + UT_ASSERT_EQUAL (mActualContextAfterException.Ebx, mExpectedContextAft= erException.Ebx); > + UT_ASSERT_EQUAL (mActualContextAfterException.Edx, mExpectedContextAft= erException.Edx); > + UT_ASSERT_EQUAL (mActualContextAfterException.Ecx, mExpectedContextAft= erException.Ecx); > + UT_ASSERT_EQUAL (mActualContextAfterException.Eax, mExpectedContextAft= erException.Eax); > + return UNIT_TEST_PASSED; > +} > + > +/** > + Special handler for CpuStackGuard test case. > + > + @param ExceptionType Exception type. > + @param SystemContext Pointer to EFI_SYSTEM_CONTEXT. > + > +**/ > +VOID > +EFIAPI > +CpuStackGuardExceptionHandler ( > + IN EFI_EXCEPTION_TYPE ExceptionType, > + IN EFI_SYSTEM_CONTEXT SystemContext > + ) > +{ > + UINTN LocalVariable; > + > + AdjustRipForFaultHandler (ExceptionType, SystemContext); > + mRspAddress[0] =3D (UINTN)SystemContext.SystemContextIa32->Esp; > + mRspAddress[1] =3D (UINTN)(&LocalVariable); > + > + return; > +} > diff --git a/UefiCpuPkg/CpuExceptionHandlerUnitTest/Ia32/ArchExceptionHan= dlerTestAsm.nasm > b/UefiCpuPkg/CpuExceptionHandlerUnitTest/Ia32/ArchExceptionHandlerTestAsm= .nasm > new file mode 100644 > index 0000000000..48031a5109 > --- /dev/null > +++ b/UefiCpuPkg/CpuExceptionHandlerUnitTest/Ia32/ArchExceptionHandlerTes= tAsm.nasm > @@ -0,0 +1,208 @@ > +;-----------------------------------------------------------------------= ------- > +; > +; Copyright (c) 2022, Intel Corporation. All rights reserved.
> +; SPDX-License-Identifier: BSD-2-Clause-Patent > +; > +; Module Name: > +; > +; ArchExceptionHandlerTestAsm.nasm > +; > +; Abstract: > +; > +; ia32 CPU Exception Handler Lib Unit test > +; > +;-----------------------------------------------------------------------= ------- > + > + SECTION .text > + > +struc GENERAL_REGISTER_IA32 > + .Edi: resd 1 > + .Esi: resd 1 > + .Ebx: resd 1 > + .Edx: resd 1 > + .Ecx: resd 1 > + .Eax: resd 1 > + > +endstruc > + > +extern ASM_PFX(mExpectedContextInHandler) > +extern ASM_PFX(mActualContextAfterException) > +extern ASM_PFX(mFaultInstructionLength) > + > +;-----------------------------------------------------------------------= ------- > +; VOID > +; EFIAPI > +; TriggerGPException ( > +; UINTN Cr4ReservedBit > +; ); > +;-----------------------------------------------------------------------= ------- > +global ASM_PFX(TriggerGPException) > +ASM_PFX(TriggerGPException): > + ; > + ; Set reserved bit 15 of cr4 to 1 > + ; > + lea ecx, [ASM_PFX(mFaultInstructionLength)] > + mov dword[ecx], TriggerGPExceptionAfter - TriggerGPExceptionBefore > + mov ecx, dword [esp + 0x4] > +TriggerGPExceptionBefore: > + mov cr4, ecx > +TriggerGPExceptionAfter: > + ret > + > +;-----------------------------------------------------------------------= ------- > +; VOID > +; EFIAPI > +; TriggerPFException ( > +; UINTN PfAddress > +; ); > +;-----------------------------------------------------------------------= ------- > +global ASM_PFX(TriggerPFException) > +ASM_PFX(TriggerPFException): > + lea ecx, [ASM_PFX(mFaultInstructionLength)] > + mov dword[ecx], TriggerPFExceptionAfter - TriggerPFExceptionBefore > + mov ecx, dword [esp + 0x4] > +TriggerPFExceptionBefore: > + mov dword[ecx], 0x1 > +TriggerPFExceptionAfter: > + ret > + > +;-----------------------------------------------------------------------= ------- > +; ModifyEcxInGlobalBeforeException; > +; This function is writed by assebly code because it's only called in th= is file. > +; It's used to set Ecx in mExpectedContextInHandler for different except= ion. > +;-----------------------------------------------------------------------= ------- > +global ASM_PFX(ModifyEcxInGlobalBeforeException) > +ASM_PFX(ModifyEcxInGlobalBeforeException): > + push eax > + lea eax, [ASM_PFX(mExpectedContextInHandler)] > + mov [eax + GENERAL_REGISTER_IA32.Ecx], ecx > + pop eax > + ret > + > +;-----------------------------------------------------------------------= ------- > +;VOID > +;EFIAPI > +;AsmTestConsistencyOfCpuContext ( > +; IN EFI_EXCEPTION_TYPE ExceptionType > +; IN UINTN FaultParameter OPTIONAL > +; ); > +;-----------------------------------------------------------------------= ------- > +global ASM_PFX(AsmTestConsistencyOfCpuContext) > +ASM_PFX(AsmTestConsistencyOfCpuContext): > + ; > + ; push 7 general register plus 4 bytes > + ; > + pushad > + > + ; > + ; Modify register to mExpectedContextInHandler. Do not handle Esp an= d Ebp. > + ; CpuExceptionHandlerLib doesn't set Esp and Esp register to the val= ue in SystemContext. > + ; > + lea eax, [ASM_PFX(mExpectedContextInHandler)] > + mov edi, [eax + GENERAL_REGISTER_IA32.Edi] > + mov esi, [eax + GENERAL_REGISTER_IA32.Esi] > + mov ebx, [eax + GENERAL_REGISTER_IA32.Ebx] > + mov edx, [eax + GENERAL_REGISTER_IA32.Edx] > + ; > + ; Set ecx to ExceptionType > + ; > + mov ecx, dword [esp + 0x24] > + mov eax, [eax + GENERAL_REGISTER_IA32.Eax] > + > + cmp ecx, 0xd > + jz GPException > + cmp ecx, 0xe > + jz PFException > + jmp INTnException > + > +PFException: > + mov ecx, dword [esp + 0x28] ; Set ecx to PFAddre= ss. > + call ASM_PFX(ModifyEcxInGlobalBeforeException) ; Set mExpectedConte= xtInHandler.Ecx to PFAddress. > + push ecx ; Push PfAddress int= o stack. > + call ASM_PFX(TriggerPFException) > + jmp AfterException > + > +GPException: > + mov ecx, dword [esp + 0x28] ; Set ecx to CR4_RES= ERVED_BIT. > + call ASM_PFX(ModifyEcxInGlobalBeforeException) ; Set mExpectedConte= xtInHandler.Ecx to CR4_RESERVED_BIT. > + push ecx ; Push CR4_RESERVED_= BIT into stack. > + call ASM_PFX(TriggerGPException) > + jmp AfterException > + > +INTnException: > + call ASM_PFX(ModifyEcxInGlobalBeforeException) ; Set mExpectedConte= xtInHandler.Ecx to ExceptionType. > + push ecx ; Push ExceptionType= into stack. > + call ASM_PFX(TriggerINTnException) > + > +AfterException: > + ; > + ; Save register in mActualContextAfterException. > + ; > + push eax > + lea eax, [ASM_PFX(mActualContextAfterException)] > + mov [eax + GENERAL_REGISTER_IA32.Edi], edi > + mov [eax + GENERAL_REGISTER_IA32.Esi], esi > + mov [eax + GENERAL_REGISTER_IA32.Ebx], ebx > + mov [eax + GENERAL_REGISTER_IA32.Edx], edx > + mov [eax + GENERAL_REGISTER_IA32.Ecx], ecx > + pop ecx > + mov [eax + GENERAL_REGISTER_IA32.Eax], ecx > + add esp, 4 > + > + ; > + ; restore original register > + ; > + popad > + ret > + > +;-----------------------------------------------------------------------= ------- > +; VOID > +; EFIAPI > +; TriggerStackOverflow ( > +; VOID > +; ); > +;-----------------------------------------------------------------------= ------- > +global ASM_PFX(TriggerStackOverflow) > +ASM_PFX(TriggerStackOverflow): > + lea ecx, [ASM_PFX(mFaultInstructionLength)] > + mov dword[ecx], TriggerCpuStackGuardAfter - TriggerCpuStackGuardBef= ore > +TriggerCpuStackGuardBefore: > + ; > + ; Clear CR0.TS since it is set after return from a nested DF > + ; > + call TriggerCpuStackGuardBefore > + clts > +TriggerCpuStackGuardAfter: > + ret > + > +;-----------------------------------------------------------------------= ------- > +; VOID > +; EFIAPI > +; TriggerINTnException ( > +; IN EFI_EXCEPTION_TYPE ExceptionType > +; ); > +;-----------------------------------------------------------------------= ------- > +global ASM_PFX(TriggerINTnException) > +ASM_PFX(TriggerINTnException): > + push eax > + push edx > + lea eax, [AsmTriggerException1 - AsmTriggerException0] > + mov ecx, dword [esp + 0xc] > + push ecx > + mul ecx > + mov ecx, AsmTriggerException0 > + add eax, ecx > + pop ecx > + pop edx > + jmp eax > + ; > + ; eax =3D AsmTriggerException0 + (AsmTriggerException1 - AsmTriggerE= xception0) * ecx > + ; > +%assign Vector 0 > +%rep 22 > +AsmTriggerException %+ Vector: > + pop eax > + INT Vector > + ret > +%assign Vector Vector+1 > +%endrep > diff --git a/UefiCpuPkg/CpuExceptionHandlerUnitTest/PeiCpuExceptionHandle= rLibUnitTest.inf > b/UefiCpuPkg/CpuExceptionHandlerUnitTest/PeiCpuExceptionHandlerLibUnitTes= t.inf > new file mode 100644 > index 0000000000..25f8f8dbe0 > --- /dev/null > +++ b/UefiCpuPkg/CpuExceptionHandlerUnitTest/PeiCpuExceptionHandlerLibUni= tTest.inf > @@ -0,0 +1,61 @@ > +## @file > +# Unit tests of the PeiCpuExceptionHandlerLib instance. > +# > +# Copyright (c) 2022, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +## > + > +[Defines] > + INF_VERSION =3D 0x00010006 > + BASE_NAME =3D CpuExceptionHandlerPeiTest > + FILE_GUID =3D 39A96CF7-F369-4357-9234-4B52F98A007= F > + MODULE_TYPE =3D PEIM > + VERSION_STRING =3D 1.0 > + ENTRY_POINT =3D PeiEntryPoint > + > +# > +# The following information is for reference only and not required by th= e build tools. > +# > +# VALID_ARCHITECTURES =3D IA32 X64 > +# > +[Sources.Ia32] > + Ia32/ArchExceptionHandlerTestAsm.nasm > + Ia32/ArchExceptionHandlerTest.c > + > +[Sources.X64] > + X64/ArchExceptionHandlerTestAsm.nasm > + X64/ArchExceptionHandlerTest.c > + > +[Sources.common] > + CpuExceptionHandlerTest.h > + CpuExceptionHandlerTestCommon.c > + PeiCpuExceptionHandlerUnitTest.c > + > +[Packages] > + MdePkg/MdePkg.dec > + MdeModulePkg/MdeModulePkg.dec > + UefiCpuPkg/UefiCpuPkg.dec > + > +[LibraryClasses] > + BaseLib > + BaseMemoryLib > + DebugLib > + UnitTestLib > + MemoryAllocationLib > + CpuExceptionHandlerLib > + PeimEntryPoint > + HobLib > + PeiServicesLib > + CpuPageTableLib > + PeiServicesTablePointerLib > + > +[Pcd] > + gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard ## CONSUMES > + gUefiCpuPkgTokenSpaceGuid.PcdCpuApStackSize ## CONSUMES > + > +[Ppis] > + gEdkiiPeiMpServices2PpiGuid ## CONSUMES > + > +[Depex] > + gEdkiiPeiMpServices2PpiGuid AND > + gEfiPeiMemoryDiscoveredPpiGuid > diff --git a/UefiCpuPkg/CpuExceptionHandlerUnitTest/PeiCpuExceptionHandle= rUnitTest.c > b/UefiCpuPkg/CpuExceptionHandlerUnitTest/PeiCpuExceptionHandlerUnitTest.c > new file mode 100644 > index 0000000000..d9408d2f5e > --- /dev/null > +++ b/UefiCpuPkg/CpuExceptionHandlerUnitTest/PeiCpuExceptionHandlerUnitTe= st.c > @@ -0,0 +1,204 @@ > +/** @file > + Unit tests of the CpuExceptionHandlerLib. > + > + Copyright (c) 2022, Intel Corporation. All rights reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include "CpuExceptionHandlerTest.h" > +#include > +#include > +#include > + > +/** > + Initialize Bsp Idt with a new Idt table and return the IA32_DESCRIPTOR= buffer. > + In PEIM, store original PeiServicePointer before new Idt table. > + > + @return Pointer to the allocated IA32_DESCRIPTOR buffer. > +**/ > +VOID * > +InitializeBspIdt ( > + VOID > + ) > +{ > + UINTN *NewIdtTable; > + IA32_DESCRIPTOR *Idtr; > + > + Idtr =3D AllocateZeroPool (sizeof (IA32_DESCRIPTOR)); > + ASSERT (Idtr !=3D NULL); > + NewIdtTable =3D AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * = CPU_INTERRUPT_NUM + sizeof (UINTN)); > + ASSERT (NewIdtTable !=3D NULL); > + // > + // Store original PeiServicePointer before new Idt table > + // > + *NewIdtTable =3D (UINTN)GetPeiServicesTablePointer (); > + NewIdtTable =3D (UINTN *)((UINTN)NewIdtTable + sizeof (UINTN)); > + > + Idtr->Base =3D (UINTN)NewIdtTable; > + Idtr->Limit =3D (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTER= RUPT_NUM - 1); > + > + AsmWriteIdtr (Idtr); > + return Idtr; > +} > + > +/** > + Retrieve the number of logical processor in the platform and the numbe= r of those logical processors that > + are enabled on this boot. > + > + @param[in] MpServices MP_SERVICES structure. > + @param[out] NumberOfProcessors Pointer to the total number of logical= processors in the system, including > + the BSP and disabled APs. > + @param[out] NumberOfEnabledProcessors Pointer to the number of process= ors in the system that are enabled. > + > + @retval EFI_SUCCESS Retrieve the number of logical processor suc= cessfully > + @retval Others Retrieve the number of logical processor uns= uccessfully > +**/ > +EFI_STATUS > +MpServicesUnitTestGetNumberOfProcessors ( > + IN MP_SERVICES MpServices, > + OUT UINTN *NumberOfProcessors, > + OUT UINTN *NumberOfEnabledProcessors > + ) > +{ > + return MpServices.Ppi->GetNumberOfProcessors (MpServices.Ppi, NumberOf= Processors, > NumberOfEnabledProcessors); > +} > + > +/** > + Caller gets one enabled AP to execute a caller-provided function. > + > + @param[in] MpServices MP_SERVICES structure. > + @param[in] Procedure Pointer to the function to be run on enabled= APs of the system. > + @param[in] ProcessorNumber The handle number of the AP. > + @param[in] TimeoutInMicroSeconds Indicates the time limit in microsec= onds for APs to return from Procedure, > + for blocking mode only. Zero means i= nfinity. > + @param[in] ProcedureArgument The parameter passed into Procedure = for all APs. > + > + > + @retval EFI_SUCCESS Caller gets one enabled AP to execute a call= er-provided function successfully > + @retval Others Caller gets one enabled AP to execute a call= er-provided function unsuccessfully > +**/ > +EFI_STATUS > +MpServicesUnitTestStartupThisAP ( > + IN MP_SERVICES MpServices, > + IN EFI_AP_PROCEDURE Procedure, > + IN UINTN ProcessorNumber, > + IN UINTN TimeoutInMicroSeconds, > + IN VOID *ProcedureArgument > + ) > +{ > + return MpServices.Ppi->StartupThisAP (MpServices.Ppi, Procedure, Proce= ssorNumber, TimeoutInMicroSeconds, > ProcedureArgument); > +} > + > +/** > + Execute a caller provided function on all enabled APs. > + > + @param[in] MpServices MP_SERVICES structure. > + @param[in] Procedure Pointer to the function to be run on enabled= APs of the system. > + @param[in] SingleThread If TRUE, then all the enabled APs execute th= e function specified by Procedure > + one by one, in ascending order of processor = handle number. > + If FALSE, then all the enabled APs execute t= he function specified by Procedure > + simultaneously. > + @param[in] TimeoutInMicroSeconds Indicates the time limit in microsec= onds for APs to return from Procedure, > + for blocking mode only. Zero means i= nfinity. > + @param[in] ProcedureArgument The parameter passed into Procedure = for all APs. > + > + @retval EFI_SUCCESS Execute a caller provided function on all en= abled APs successfully > + @retval Others Execute a caller provided function on all en= abled APs unsuccessfully > +**/ > +EFI_STATUS > +MpServicesUnitTestStartupAllAPs ( > + IN MP_SERVICES MpServices, > + IN EFI_AP_PROCEDURE Procedure, > + IN BOOLEAN SingleThread, > + IN UINTN TimeoutInMicroSeconds, > + IN VOID *ProcedureArgument > + ) > +{ > + return MpServices.Ppi->StartupAllAPs (MpServices.Ppi, Procedure, Singl= eThread, TimeoutInMicroSeconds, > ProcedureArgument); > +} > + > +/** > + Get the handle number for the calling processor. > + > + @param[in] MpServices MP_SERVICES structure. > + @param[out] ProcessorNumber The handle number for the calling processo= r. > + > + @retval EFI_SUCCESS Get the handle number for the calling proces= sor successfully. > + @retval Others Get the handle number for the calling proces= sor unsuccessfully. > +**/ > +EFI_STATUS > +MpServicesUnitTestWhoAmI ( > + IN MP_SERVICES MpServices, > + OUT UINTN *ProcessorNumber > + ) > +{ > + return MpServices.Ppi->WhoAmI (MpServices.Ppi, ProcessorNumber); > +} > + > +/** > + Get EDKII_PEI_MP_SERVICES2_PPI pointer. > + > + @param[out] MpServices Pointer to the buffer where EDKII_PEI_MP_SER= VICES2_PPI is stored > + > + @retval EFI_SUCCESS EDKII_PEI_MP_SERVICES2_PPI interface is retu= rned > + @retval EFI_NOT_FOUND EDKII_PEI_MP_SERVICES2_PPI interface is not = found > +**/ > +EFI_STATUS > +GetMpServices ( > + OUT MP_SERVICES *MpServices > + ) > +{ > + return PeiServicesLocatePpi (&gEdkiiPeiMpServices2PpiGuid, 0, NULL, (V= OID **)&MpServices->Ppi); > +} > + > +/** > + Entry point of CpuExceptionHandlerPeiTest PEIM. > + > + @param[in] FileHandle Handle of the file being invoked. > + @param[in] PeiServices Describes the list of possible PEI Services. > + > + @retval EFI_SUCCESS The PEIM executed normally. > + > +**/ > +EFI_STATUS > +EFIAPI > +PeiEntryPoint ( > + IN EFI_PEI_FILE_HANDLE FileHandle, > + IN CONST EFI_PEI_SERVICES **PeiServices > + ) > +{ > + EFI_STATUS Status; > + UNIT_TEST_FRAMEWORK_HANDLE Framework; > + > + Framework =3D NULL; > + > + DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERS= ION)); > + > + // > + // Start setting up the test framework for running the tests. > + // > + Status =3D InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfi= CallerBaseName, > UNIT_TEST_APP_VERSION); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status =3D %r= \n", Status)); > + goto EXIT; > + } > + > + Status =3D AddCommonTestCase (Framework); > + if (EFI_ERROR (Status)) { > + DEBUG ((DEBUG_ERROR, "Failed in AddCommonTestCase. Status =3D %r\n",= Status)); > + goto EXIT; > + } > + > + // > + // Execute the tests. > + // > + Status =3D RunAllTestSuites (Framework); > + > +EXIT: > + if (Framework) { > + FreeUnitTestFramework (Framework); > + } > + > + return Status; > +} > -- > 2.31.1.windows.1