]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c
UefiCpuPkg/PiSmmCpu: Always set RW+P bit for page table by default
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / Ia32 / SmmFuncsArch.c
CommitLineData
fe5f1949
JY
1/** @file\r
2 SMM CPU misc functions for Ia32 arch specific.\r
3 \r
4Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "PiSmmCpuDxeSmm.h"\r
16\r
17/**\r
18 Initialize Gdt for all processors.\r
19 \r
20 @param[in] Cr3 CR3 value.\r
21 @param[out] GdtStepSize The step size for GDT table.\r
22\r
23 @return GdtBase for processor 0.\r
24 GdtBase for processor X is: GdtBase + (GdtStepSize * X)\r
25**/\r
26VOID *\r
27InitGdt (\r
28 IN UINTN Cr3,\r
29 OUT UINTN *GdtStepSize\r
30 )\r
31{\r
32 UINTN Index;\r
33 IA32_SEGMENT_DESCRIPTOR *GdtDescriptor;\r
34 UINTN TssBase;\r
35 UINTN GdtTssTableSize;\r
36 UINT8 *GdtTssTables;\r
37 UINTN GdtTableStepSize;\r
38\r
39 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
40 //\r
41 // For IA32 SMM, if SMM Stack Guard feature is enabled, we use 2 TSS.\r
42 // in this case, we allocate separate GDT/TSS for each CPUs to avoid TSS load contention\r
43 // on each SMI entry.\r
44 //\r
45\r
46 //\r
47 // Enlarge GDT to contain 2 TSS descriptors\r
48 //\r
49 gcSmiGdtr.Limit += (UINT16)(2 * sizeof (IA32_SEGMENT_DESCRIPTOR));\r
50\r
51 GdtTssTableSize = (gcSmiGdtr.Limit + 1 + TSS_SIZE * 2 + 7) & ~7; // 8 bytes aligned\r
52 GdtTssTables = (UINT8*)AllocatePages (EFI_SIZE_TO_PAGES (GdtTssTableSize * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus));\r
53 ASSERT (GdtTssTables != NULL);\r
54 GdtTableStepSize = GdtTssTableSize;\r
55\r
56 for (Index = 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; Index++) {\r
57 CopyMem (GdtTssTables + GdtTableStepSize * Index, (VOID*)(UINTN)gcSmiGdtr.Base, gcSmiGdtr.Limit + 1 + TSS_SIZE * 2);\r
58 //\r
59 // Fixup TSS descriptors\r
60 //\r
61 TssBase = (UINTN)(GdtTssTables + GdtTableStepSize * Index + gcSmiGdtr.Limit + 1);\r
62 GdtDescriptor = (IA32_SEGMENT_DESCRIPTOR *)(TssBase) - 2;\r
63 GdtDescriptor->Bits.BaseLow = (UINT16)TssBase;\r
64 GdtDescriptor->Bits.BaseMid = (UINT8)(TssBase >> 16);\r
65 GdtDescriptor->Bits.BaseHigh = (UINT8)(TssBase >> 24);\r
66\r
67 TssBase += TSS_SIZE;\r
68 GdtDescriptor++;\r
69 GdtDescriptor->Bits.BaseLow = (UINT16)TssBase;\r
70 GdtDescriptor->Bits.BaseMid = (UINT8)(TssBase >> 16);\r
71 GdtDescriptor->Bits.BaseHigh = (UINT8)(TssBase >> 24);\r
72 //\r
73 // Fixup TSS segments\r
74 //\r
75 // ESP as known good stack\r
76 //\r
77 *(UINTN *)(TssBase + TSS_IA32_ESP_OFFSET) = mSmmStackArrayBase + EFI_PAGE_SIZE + Index * mSmmStackSize;\r
78 *(UINT32 *)(TssBase + TSS_IA32_CR3_OFFSET) = Cr3;\r
79 }\r
80 } else {\r
81 //\r
82 // Just use original table, AllocatePage and copy them here to make sure GDTs are covered in page memory.\r
83 //\r
84 GdtTssTableSize = gcSmiGdtr.Limit + 1;\r
85 GdtTssTables = (UINT8*)AllocatePages (EFI_SIZE_TO_PAGES (GdtTssTableSize * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus));\r
86 ASSERT (GdtTssTables != NULL);\r
87 GdtTableStepSize = GdtTssTableSize;\r
88\r
89 for (Index = 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; Index++) {\r
90 CopyMem (GdtTssTables + GdtTableStepSize * Index, (VOID*)(UINTN)gcSmiGdtr.Base, gcSmiGdtr.Limit + 1);\r
91 }\r
92 }\r
93\r
94 *GdtStepSize = GdtTableStepSize;\r
95 return GdtTssTables;\r
96}\r