]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/PiSmmCpuDxeSmm/SmramSaveState.c
UefiCpuPkg/PiSmmCpu: Add Shadow Stack Support for X86 SMM.
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / SmramSaveState.c
index 3188d438181ccbed40e9d981e62dd4b8dfe792a9..8f34875e1789ce85fbe48753d71a7fae24a48f2c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Provides services to access SMRAM Save State Map\r
 \r
-Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -105,11 +105,11 @@ typedef struct {
 ///\r
 /// Variables from SMI Handler\r
 ///\r
-extern UINT32           gSmbase;\r
-extern volatile UINT32  gSmiStack;\r
-extern UINT32           gSmiCr3;\r
-extern volatile UINT8   gcSmiHandlerTemplate[];\r
-extern CONST UINT16     gcSmiHandlerSize;\r
+X86_ASSEMBLY_PATCH_LABEL gPatchSmbase;\r
+X86_ASSEMBLY_PATCH_LABEL gPatchSmiStack;\r
+X86_ASSEMBLY_PATCH_LABEL gPatchSmiCr3;\r
+extern volatile UINT8    gcSmiHandlerTemplate[];\r
+extern CONST UINT16      gcSmiHandlerSize;\r
 \r
 //\r
 // Variables used by SMI Handler\r
@@ -686,6 +686,7 @@ InstallSmiHandler (
   )\r
 {\r
   PROCESSOR_SMM_DESCRIPTOR  *Psd;\r
+  UINT32                    CpuSmiStack;\r
 \r
   //\r
   // Initialize PROCESSOR_SMM_DESCRIPTOR\r
@@ -713,19 +714,22 @@ InstallSmiHandler (
     return;\r
   }\r
 \r
+  InitShadowStack (CpuIndex, (VOID *)((UINTN)SmiStack + StackSize));\r
+\r
   //\r
   // Initialize values in template before copy\r
   //\r
-  gSmiStack             = (UINT32)((UINTN)SmiStack + StackSize - sizeof (UINTN));\r
-  gSmiCr3               = Cr3;\r
-  gSmbase               = SmBase;\r
+  CpuSmiStack = (UINT32)((UINTN)SmiStack + StackSize - sizeof (UINTN));\r
+  PatchInstructionX86 (gPatchSmiStack, CpuSmiStack, 4);\r
+  PatchInstructionX86 (gPatchSmiCr3, Cr3, 4);\r
+  PatchInstructionX86 (gPatchSmbase, SmBase, 4);\r
   gSmiHandlerIdtr.Base  = IdtBase;\r
   gSmiHandlerIdtr.Limit = (UINT16)(IdtSize - 1);\r
 \r
   //\r
   // Set the value at the top of the CPU stack to the CPU Index\r
   //\r
-  *(UINTN*)(UINTN)gSmiStack = CpuIndex;\r
+  *(UINTN*)(UINTN)CpuSmiStack = CpuIndex;\r
 \r
   //\r
   // Copy template to CPU specific SMI handler location\r