From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail04.groups.io (mail04.groups.io [45.79.224.9]) by spool.mail.gandi.net (Postfix) with ESMTPS id F24A2941627 for ; Tue, 16 Apr 2024 10:20:41 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=3zeZTJ+jrIfiZNpPb26cKXRuiLZGfZlwZ1UDUAN4jZc=; c=relaxed/simple; d=groups.io; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From:In-Reply-To:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Type:Content-Language; s=20240206; t=1713262840; v=1; b=fMTylKUb3mgi5m2wPtvvzChpywe8lZXKgpRV9Aizk2nmB78zaSnzDoj0Miosby3SxYadFYH9 uhEOr4RX49SxyoiQhb+wYs69AmGAXBI4cTNyIcpfuG8eDp6qqz1Q2zFKWT6JMX7X1Avx7Aj4zM1 RE3i1uI9pO8xTBqFHIFFh9Z8NHL/3Bech3d+ck7WEd6+Se3csQSHk0EqzGSzLHvPBTDfghibY5i 4gZA+7Fun1xbcVsFXRGJTez9mJ1yCWlQu+xqQ9ObioEelMgVJu0CShSHwyb48fiTPY1WkdaZRU9 qN5koOxoo3E8p4a+TPPG+NRrGmwMWKoc5fWqM2jOhF1vQ== X-Received: by 127.0.0.2 with SMTP id CtOQYY7687511xkTqnMYc0Lr; Tue, 16 Apr 2024 03:20:40 -0700 X-Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.107.223.67]) by mx.groups.io with SMTP id smtpd.web10.16842.1713262839626309497 for ; Tue, 16 Apr 2024 03:20:39 -0700 X-Received: from IA1PR12MB6458.namprd12.prod.outlook.com (2603:10b6:208:3aa::22) by MW4PR12MB6924.namprd12.prod.outlook.com (2603:10b6:303:207::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7452.50; Tue, 16 Apr 2024 10:20:35 +0000 X-Received: from IA1PR12MB6458.namprd12.prod.outlook.com ([fe80::1a52:ef3a:cf04:e56d]) by IA1PR12MB6458.namprd12.prod.outlook.com ([fe80::1a52:ef3a:cf04:e56d%3]) with mapi id 15.20.7452.049; Tue, 16 Apr 2024 10:20:35 +0000 Message-ID: <96af112c-1fef-4799-babc-e33e7eff8a1e@amd.com> Date: Tue, 16 Apr 2024 15:50:27 +0530 User-Agent: Mozilla Thunderbird Subject: Re: [edk2-devel] [PATCH v1 04/13] UefiCpuPkg/SmmRelocationLib: Add library instance for AMD To: Jiaxin Wu , devel@edk2.groups.io Cc: Abner Chang , Ray Ni , Zeng Star , Gerd Hoffmann , Rahul Kumar References: <20240410135724.15344-1-jiaxin.wu@intel.com> <20240410135724.15344-5-jiaxin.wu@intel.com> From: "Abdul Lateef Attar via groups.io" In-Reply-To: <20240410135724.15344-5-jiaxin.wu@intel.com> X-ClientProxiedBy: PN2P287CA0008.INDP287.PROD.OUTLOOK.COM (2603:1096:c01:21b::13) To IA1PR12MB6458.namprd12.prod.outlook.com (2603:10b6:208:3aa::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: IA1PR12MB6458:EE_|MW4PR12MB6924:EE_ X-MS-Office365-Filtering-Correlation-Id: 61021eb0-1ca6-4d7e-973f-08dc5dfeda6e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: TdwDdBJu38u2UIJJPu7MJCZbNAqxd9aSYQPDh98sRbH45g/8m2zy/NIb6O36wFWd0NKMhgAy81qDpITnDlrJCbt0qXY1+iImhy158UaLDwuhAp6AdOSRK7fq1+/yaqAr+6nWWmQlcZd5lpySgWTt3ZRFptktVGGIH/bJlbWgKc1Ee1RmXq13XF7i6NhBM5E8/m91wNR3p7ByaZSnNcWROez4aVWSJpjMUskb+PZ79GbsGLK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dZbnph?= =?utf-8?B?c2J6RGtSSDY3eVNqa2p4QitONzhsOCsra0pyeEg4Y01KcE9ZTnV2QVkwaU01?= =?utf-8?B?NE9tbDNQMkJ1Rm9CR1Z4bmw2Vk10Q2RReWlGbzg4R0JxVU95Skc0RzQvd2FT?= =?utf-8?B?U3UvbVVXc09xK0N1WGUzNjdiUFNjc3U0anZLb3ZqY05uN3l1b0Q1VDdiajJ5?= =?utf-8?B?dnhkbnNKZE11YjN0UGpNTmUzZXUzNXQ0R2IwcktIYm9GNWRoYlgrb1VZM0x0?= =?utf-8?B?dWRzU3JIemF5amErbXhoZFQxZ21wdTcrMUEwOG1OR3dWQld5S2RaaisrUzlz?= =?utf-8?B?STRJemU2L1c1Y25GWUJuM2pxMDk1RTZZRU96b0dzMzFzRlFhV2NnS2dWUVVC?= =?utf-8?B?Y1gydXFMNzVFY1MwVGJnaFMrVVkyM1dnSUkvU3hzblJlMkNrU0dUWWk1TEVD?= =?utf-8?B?NzRERUNKY1BCT0VNYkFPcWU3aHR1YmNISGl6VzI1M1c4T3V1azRzS1RwM1hK?= =?utf-8?B?YUNWWVZrbFJUSFJWampodkpzUCtnZnNVKy9WMWNncWNzbTByVGVudFJ6NU11?= =?utf-8?B?Yk55M2ZDOFNzNXFXZGV0S3o5Sk9iMFYreVhZM1NKRFRzc1lmbWJzczZiaU96?= =?utf-8?B?VjB3dlVSYmFnMHd4cWZOOTdhNTZtQk50dWpvQlBsY0lyanJQZ3kzNDJGWkRL?= =?utf-8?B?OEF0YWdmaUFJWUhlSzhUYUpSSUlhZmh0YjR0NXFxTWhzRjh4cjBWeFg4OFNn?= =?utf-8?Q?gv4jSq6b1mwwjUN6MRFxTePmJ?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 61021eb0-1ca6-4d7e-973f-08dc5dfeda6e X-MS-Exchange-CrossTenant-AuthSource: IA1PR12MB6458.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Apr 2024 10:20:35.0461 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: n32fxyKgFt7y7LkbSvTTxUfDFcPuuT75C9lqfcPWULY14TAtIO9cOcuDL3X2K0RkzNlVJf4k1RXXVsApkluOAg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB6924 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Tue, 16 Apr 2024 03:20:39 -0700 Resent-From: AbdulLateef.Attar@amd.com Reply-To: devel@edk2.groups.io,AbdulLateef.Attar@amd.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: b2DxrlFnSKGa2prfqheCBbzcx7686176AA= Content-Type: multipart/alternative; boundary="------------YjNsu9HnUSLfr8C02YGIe0bB" Content-Language: en-US X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=fMTylKUb; dmarc=pass (policy=none) header.from=groups.io; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.9 as permitted sender) smtp.mailfrom=bounce@groups.io --------------YjNsu9HnUSLfr8C02YGIe0bB Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Reviewed-by: Abdul Lateef Attar On 10-04-2024 19:27, Jiaxin Wu wrote: > Caution: This message originated from an External Source. Use proper caut= ion when opening attachments, clicking links, or responding. > > > Due to the definition difference of SMRAM Save State, > SmmBase config in SMRAM Save State for AMD is also different. > > This patch provides the AmdSmmRelocationLib library instance > to handle the SMRAM Save State difference. > > Cc: Abdul Lateef Attar > Cc: Abner Chang > Cc: Ray Ni > Cc: Zeng Star > Cc: Gerd Hoffmann > Cc: Rahul Kumar > Signed-off-by: Jiaxin Wu > --- > .../SmmRelocationLib/AmdSmmRelocationLib.inf | 61 ++++++++++++ > .../SmmRelocationLib/AmdSmramSaveStateConfig.c | 109 ++++++++++++++= +++++++ > 2 files changed, 170 insertions(+) > create mode 100644 UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocation= Lib.inf > create mode 100644 UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStat= eConfig.c > > diff --git a/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf = b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf > new file mode 100644 > index 0000000000..710cd1948b > --- /dev/null > +++ b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf > @@ -0,0 +1,61 @@ > +## @file > +# SMM Relocation Lib for each processor. > +# > +# This Lib produces the SMM_BASE_HOB in HOB database which tells > +# the PiSmmCpuDxeSmm driver (runs at a later phase) about the new > +# SMBASE for each processor. PiSmmCpuDxeSmm driver installs the > +# SMI handler at the SMM_BASE_HOB.SmBase[Index]+0x8000 for processor > +# Index. > +# > +# Copyright (c) 2024, Intel Corporation. All rights reserved.
> +# SPDX-License-Identifier: BSD-2-Clause-Patent > +# > +## > + > +[Defines] > + INF_VERSION =3D 0x00010005 > + BASE_NAME =3D SmmRelocationLib > + FILE_GUID =3D 65C74DCD-0D09-494A-8BFF-A64226EB805= 4 > + MODULE_TYPE =3D PEIM > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D SmmRelocationLib > + > +[Sources] > + InternalSmmRelocationLib.h > + AmdSmramSaveStateConfig.c > + SmmRelocationLib.c > + > +[Sources.Ia32] > + Ia32/Semaphore.c > + Ia32/SmmInit.nasm > + > +[Sources.X64] > + X64/Semaphore.c > + X64/SmmInit.nasm > + > +[Packages] > + MdePkg/MdePkg.dec > + MdeModulePkg/MdeModulePkg.dec > + UefiCpuPkg/UefiCpuPkg.dec > + > +[LibraryClasses] > + BaseLib > + BaseMemoryLib > + CpuExceptionHandlerLib > + DebugLib > + HobLib > + LocalApicLib > + MemoryAllocationLib > + PcdLib > + PeiServicesLib > + > +[Guids] > + gSmmBaseHobGuid ## HOB ALWAYS_PRODUCED > + gEfiSmmSmramMemoryGuid ## CONSUMES > + > +[Pcd] > + gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber > + gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmStackSize ## CO= NSUMES > + > +[FeaturePcd] > + gUefiCpuPkgTokenSpaceGuid.PcdCpuHotPlugSupport = ## CONSUMES > diff --git a/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.= c b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c > new file mode 100644 > index 0000000000..fbcf347f9b > --- /dev/null > +++ b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c > @@ -0,0 +1,109 @@ > +/** @file > + Config SMRAM Save State for SmmBases Relocation. > + > + Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved. > + Copyright (c) 2024, Intel Corporation. All rights reserved.
> + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > +#include "InternalSmmRelocationLib.h" > +#include > + > +/** > + This function configures the SmBase on the currently executing CPU. > + > + @param[in] CpuIndex The index of the CPU. > + @param[in,out] CpuState Pointer to SMRAM Save State Map fo= r the > + currently executing CPU. On out, S= mBase is > + updated to the new value. > + > +**/ > +VOID > +EFIAPI > +ConfigureSmBase ( > + IN UINTN CpuIndex, > + IN OUT SMRAM_SAVE_STATE_MAP *CpuState > + ) > +{ > + AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; > + > + AmdCpuState =3D (AMD_SMRAM_SAVE_STATE_MAP *)CpuState; > + > + if (mSmmSaveStateRegisterLma =3D=3D EFI_MM_SAVE_STATE_REGISTER_LMA_32B= IT) { > + AmdCpuState->x86.SMBASE =3D (UINT32)mSmBaseForAllCpus[CpuIndex]; > + } else { > + AmdCpuState->x64.SMBASE =3D (UINT32)mSmBaseForAllCpus[CpuIndex]; > + } > +} > + > +/** > + This function updates the SMRAM save state on the currently executing = CPU > + to resume execution at a specific address after an RSM instruction. T= his > + function must evaluate the SMRAM save state to determine the execution= mode > + the RSM instruction resumes and update the resume execution address wi= th > + either NewInstructionPointer32 or NewInstructionPoint. The auto HALT = restart > + flag in the SMRAM save state must always be cleared. This function re= turns > + the value of the instruction pointer from the SMRAM save state that wa= s > + replaced. If this function returns 0, then the SMRAM save state was n= ot > + modified. > + > + This function is called during the very first SMI on each CPU after > + SmmCpuFeaturesInitializeProcessor() to set a flag in normal execution = mode > + to signal that the SMBASE of each CPU has been updated before the defa= ult > + SMBASE address is used for the first SMI to the next CPU. > + > + @param[in] CpuIndex The processor index for the cu= rrently > + executing CPU. > + @param[in,out] CpuState Pointer to SMRAM Save State Ma= p for the > + currently executing CPU. > + @param[in] NewInstructionPointer32 Instruction pointer to use if = resuming to > + 32-bit mode from 64-bit SMM. > + @param[in] NewInstructionPointer Instruction pointer to use if = resuming to > + same mode as SMM. > + > + @retval The value of the original instruction pointer before it was ho= oked. > + > +**/ > +UINT64 > +EFIAPI > +HookReturnFromSmm ( > + IN UINTN CpuIndex, > + IN OUT SMRAM_SAVE_STATE_MAP *CpuState, > + IN UINT64 NewInstructionPointer32, > + IN UINT64 NewInstructionPointer > + ) > +{ > + UINT64 OriginalInstructionPointer; > + AMD_SMRAM_SAVE_STATE_MAP *AmdCpuState; > + > + AmdCpuState =3D (AMD_SMRAM_SAVE_STATE_MAP *)CpuState; > + > + if (mSmmSaveStateRegisterLma =3D=3D EFI_MM_SAVE_STATE_REGISTER_LMA_32B= IT) { > + OriginalInstructionPointer =3D (UINT64)AmdCpuState->x86._EIP; > + AmdCpuState->x86._EIP =3D (UINT32)NewInstructionPointer; > + // > + // Clear the auto HALT restart flag so the RSM instruction returns > + // program control to the instruction following the HLT instruction. > + // > + if ((AmdCpuState->x86.AutoHALTRestart & BIT0) !=3D 0) { > + AmdCpuState->x86.AutoHALTRestart &=3D ~BIT0; > + } > + } else { > + OriginalInstructionPointer =3D AmdCpuState->x64._RIP; > + if ((AmdCpuState->x64.EFER & LMA) =3D=3D 0) { > + AmdCpuState->x64._RIP =3D (UINT32)NewInstructionPointer32; > + } else { > + AmdCpuState->x64._RIP =3D (UINT32)NewInstructionPointer; > + } > + > + // > + // Clear the auto HALT restart flag so the RSM instruction returns > + // program control to the instruction following the HLT instruction. > + // > + if ((AmdCpuState->x64.AutoHALTRestart & BIT0) !=3D 0) { > + AmdCpuState->x64.AutoHALTRestart &=3D ~BIT0; > + } > + } > + > + return OriginalInstructionPointer; > +} > -- > 2.16.2.windows.1 > -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117876): https://edk2.groups.io/g/devel/message/117876 Mute This Topic: https://groups.io/mt/105441993/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- --------------YjNsu9HnUSLfr8C02YGIe0bB Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Reviewed-by: Abdul Lateef Atta= r <AbdulLateef.Attar@amd.com>

On 10-04-2024 19:27, Jiaxin Wu wrote:
Caution: This message originat=
ed from an External Source. Use proper caution when opening attachments, cl=
icking links, or responding.


Due to the definition difference of SMRAM Save State,
SmmBase config in SMRAM Save State for AMD is also different.

This patch provides the AmdSmmRelocationLib library instance
to handle the SMRAM Save State difference.

Cc: Abdul Lateef Attar <AbdulLateef.Attar@amd.com>
Cc: Abner Chang <abner.chang@amd.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Zeng Star <star.zeng@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Signed-off-by: Jiaxin Wu <jiaxin.wu@intel.com>
---
 .../SmmRelocationLib/AmdSmmRelocationLib.inf       |  61 ++++++++++++
 .../SmmRelocationLib/AmdSmramSaveStateConfig.c     | 109 +++++++++++++++++=
++++
 2 files changed, 170 insertions(+)
 create mode 100644 UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib=
.inf
 create mode 100644 UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateCo=
nfig.c

diff --git a/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf b/=
UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf
new file mode 100644
index 0000000000..710cd1948b
--- /dev/null
+++ b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmmRelocationLib.inf
@@ -0,0 +1,61 @@
+## @file
+# SMM Relocation Lib for each processor.
+#
+# This Lib produces the SMM_BASE_HOB in HOB database which tells
+# the PiSmmCpuDxeSmm driver (runs at a later phase) about the new
+# SMBASE for each processor. PiSmmCpuDxeSmm driver installs the
+# SMI handler at the SMM_BASE_HOB.SmBase[Index]+0x8000 for processor
+# Index.
+#
+# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    =3D 0x00010005
+  BASE_NAME                      =3D SmmRelocationLib
+  FILE_GUID                      =3D 65C74DCD-0D09-494A-8BFF-A64226EB8054
+  MODULE_TYPE                    =3D PEIM
+  VERSION_STRING                 =3D 1.0
+  LIBRARY_CLASS                  =3D SmmRelocationLib
+
+[Sources]
+  InternalSmmRelocationLib.h
+  AmdSmramSaveStateConfig.c
+  SmmRelocationLib.c
+
+[Sources.Ia32]
+  Ia32/Semaphore.c
+  Ia32/SmmInit.nasm
+
+[Sources.X64]
+  X64/Semaphore.c
+  X64/SmmInit.nasm
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  UefiCpuPkg/UefiCpuPkg.dec
+
+[LibraryClasses]
+  BaseLib
+  BaseMemoryLib
+  CpuExceptionHandlerLib
+  DebugLib
+  HobLib
+  LocalApicLib
+  MemoryAllocationLib
+  PcdLib
+  PeiServicesLib
+
+[Guids]
+  gSmmBaseHobGuid                               ## HOB ALWAYS_PRODUCED
+  gEfiSmmSmramMemoryGuid                        ## CONSUMES
+
+[Pcd]
+  gUefiCpuPkgTokenSpaceGuid.PcdCpuMaxLogicalProcessorNumber
+  gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmStackSize                     ## CONS=
UMES
+
+[FeaturePcd]
+  gUefiCpuPkgTokenSpaceGuid.PcdCpuHotPlugSupport                        ##=
 CONSUMES
diff --git a/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c =
b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c
new file mode 100644
index 0000000000..fbcf347f9b
--- /dev/null
+++ b/UefiCpuPkg/Library/SmmRelocationLib/AmdSmramSaveStateConfig.c
@@ -0,0 +1,109 @@
+/** @file
+  Config SMRAM Save State for SmmBases Relocation.
+
+  Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved.<=
BR>
+  Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+#include "InternalSmmRelocationLib.h"
+#include <Register/Amd/SmramSaveStateMap.h>
+
+/**
+  This function configures the SmBase on the currently executing CPU.
+
+  @param[in]     CpuIndex             The index of the CPU.
+  @param[in,out] CpuState             Pointer to SMRAM Save State Map for =
the
+                                      currently executing CPU. On out, SmB=
ase is
+                                      updated to the new value.
+
+**/
+VOID
+EFIAPI
+ConfigureSmBase (
+  IN     UINTN                 CpuIndex,
+  IN OUT SMRAM_SAVE_STATE_MAP  *CpuState
+  )
+{
+  AMD_SMRAM_SAVE_STATE_MAP  *AmdCpuState;
+
+  AmdCpuState =3D (AMD_SMRAM_SAVE_STATE_MAP *)CpuState;
+
+  if (mSmmSaveStateRegisterLma =3D=3D EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT=
) {
+    AmdCpuState->x86.SMBASE =3D (UINT32)mSmBaseForAllCpus[CpuIndex];
+  } else {
+    AmdCpuState->x64.SMBASE =3D (UINT32)mSmBaseForAllCpus[CpuIndex];
+  }
+}
+
+/**
+  This function updates the SMRAM save state on the currently executing CP=
U
+  to resume execution at a specific address after an RSM instruction.  Thi=
s
+  function must evaluate the SMRAM save state to determine the execution m=
ode
+  the RSM instruction resumes and update the resume execution address with
+  either NewInstructionPointer32 or NewInstructionPoint.  The auto HALT re=
start
+  flag in the SMRAM save state must always be cleared.  This function retu=
rns
+  the value of the instruction pointer from the SMRAM save state that was
+  replaced.  If this function returns 0, then the SMRAM save state was not
+  modified.
+
+  This function is called during the very first SMI on each CPU after
+  SmmCpuFeaturesInitializeProcessor() to set a flag in normal execution mo=
de
+  to signal that the SMBASE of each CPU has been updated before the defaul=
t
+  SMBASE address is used for the first SMI to the next CPU.
+
+  @param[in]     CpuIndex                 The processor index for the curr=
ently
+                                          executing CPU.
+  @param[in,out] CpuState                 Pointer to SMRAM Save State Map =
for the
+                                          currently executing CPU.
+  @param[in]     NewInstructionPointer32  Instruction pointer to use if re=
suming to
+                                          32-bit mode from 64-bit SMM.
+  @param[in]     NewInstructionPointer    Instruction pointer to use if re=
suming to
+                                          same mode as SMM.
+
+  @retval The value of the original instruction pointer before it was hook=
ed.
+
+**/
+UINT64
+EFIAPI
+HookReturnFromSmm (
+  IN     UINTN                 CpuIndex,
+  IN OUT SMRAM_SAVE_STATE_MAP  *CpuState,
+  IN     UINT64                NewInstructionPointer32,
+  IN     UINT64                NewInstructionPointer
+  )
+{
+  UINT64                    OriginalInstructionPointer;
+  AMD_SMRAM_SAVE_STATE_MAP  *AmdCpuState;
+
+  AmdCpuState =3D (AMD_SMRAM_SAVE_STATE_MAP *)CpuState;
+
+  if (mSmmSaveStateRegisterLma =3D=3D EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT=
) {
+    OriginalInstructionPointer =3D (UINT64)AmdCpuState->x86._EIP;
+    AmdCpuState->x86._EIP      =3D (UINT32)NewInstructionPointer;
+    //
+    // Clear the auto HALT restart flag so the RSM instruction returns
+    // program control to the instruction following the HLT instruction.
+    //
+    if ((AmdCpuState->x86.AutoHALTRestart & BIT0) !=3D 0) {
+      AmdCpuState->x86.AutoHALTRestart &=3D ~BIT0;
+    }
+  } else {
+    OriginalInstructionPointer =3D AmdCpuState->x64._RIP;
+    if ((AmdCpuState->x64.EFER & LMA) =3D=3D 0) {
+      AmdCpuState->x64._RIP =3D (UINT32)NewInstructionPointer32;
+    } else {
+      AmdCpuState->x64._RIP =3D (UINT32)NewInstructionPointer;
+    }
+
+    //
+    // Clear the auto HALT restart flag so the RSM instruction returns
+    // program control to the instruction following the HLT instruction.
+    //
+    if ((AmdCpuState->x64.AutoHALTRestart & BIT0) !=3D 0) {
+      AmdCpuState->x64.AutoHALTRestart &=3D ~BIT0;
+    }
+  }
+
+  return OriginalInstructionPointer;
+}
--
2.16.2.windows.1

_._,_._,_

Groups.io Links:

=20 You receive all messages sent to this group. =20 =20

View/Reply Online (#117876) | =20 | Mute= This Topic | New Topic
Your Subscriptio= n | Contact Group Owner | Unsubscribe [rebecca@openfw.io]

_._,_._,_
--------------YjNsu9HnUSLfr8C02YGIe0bB--