]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c
UefiCpuPkg/PiSmmCpuDxeSmm: Fix ASSERT for success.
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / X64 / SmmFuncsArch.c
... / ...
CommitLineData
1/** @file\r
2 SMM CPU misc functions for x64 arch specific.\r
3\r
4Copyright (c) 2015 - 2018, 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
17EFI_PHYSICAL_ADDRESS mGdtBuffer;\r
18UINTN mGdtBufferSize;\r
19\r
20/**\r
21 Initialize IDT for SMM Stack Guard.\r
22\r
23**/\r
24VOID\r
25EFIAPI\r
26InitializeIDTSmmStackGuard (\r
27 VOID\r
28 )\r
29{\r
30 IA32_IDT_GATE_DESCRIPTOR *IdtGate;\r
31\r
32 //\r
33 // If SMM Stack Guard feature is enabled, set the IST field of\r
34 // the interrupt gate for Page Fault Exception to be 1\r
35 //\r
36 IdtGate = (IA32_IDT_GATE_DESCRIPTOR *)gcSmiIdtr.Base;\r
37 IdtGate += EXCEPT_IA32_PAGE_FAULT;\r
38 IdtGate->Bits.Reserved_0 = 1;\r
39}\r
40\r
41/**\r
42 Initialize Gdt for all processors.\r
43\r
44 @param[in] Cr3 CR3 value.\r
45 @param[out] GdtStepSize The step size for GDT table.\r
46\r
47 @return GdtBase for processor 0.\r
48 GdtBase for processor X is: GdtBase + (GdtStepSize * X)\r
49**/\r
50VOID *\r
51InitGdt (\r
52 IN UINTN Cr3,\r
53 OUT UINTN *GdtStepSize\r
54 )\r
55{\r
56 UINTN Index;\r
57 IA32_SEGMENT_DESCRIPTOR *GdtDescriptor;\r
58 UINTN TssBase;\r
59 UINTN GdtTssTableSize;\r
60 UINT8 *GdtTssTables;\r
61 UINTN GdtTableStepSize;\r
62\r
63 //\r
64 // For X64 SMM, we allocate separate GDT/TSS for each CPUs to avoid TSS load contention\r
65 // on each SMI entry.\r
66 //\r
67 GdtTssTableSize = (gcSmiGdtr.Limit + 1 + TSS_SIZE + 7) & ~7; // 8 bytes aligned\r
68 mGdtBufferSize = GdtTssTableSize * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus;\r
69 GdtTssTables = (UINT8*)AllocateCodePages (EFI_SIZE_TO_PAGES (mGdtBufferSize));\r
70 ASSERT (GdtTssTables != NULL);\r
71 mGdtBuffer = (UINTN)GdtTssTables;\r
72 GdtTableStepSize = GdtTssTableSize;\r
73\r
74 for (Index = 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; Index++) {\r
75 CopyMem (GdtTssTables + GdtTableStepSize * Index, (VOID*)(UINTN)gcSmiGdtr.Base, gcSmiGdtr.Limit + 1 + TSS_SIZE);\r
76\r
77 //\r
78 // Fixup TSS descriptors\r
79 //\r
80 TssBase = (UINTN)(GdtTssTables + GdtTableStepSize * Index + gcSmiGdtr.Limit + 1);\r
81 GdtDescriptor = (IA32_SEGMENT_DESCRIPTOR *)(TssBase) - 2;\r
82 GdtDescriptor->Bits.BaseLow = (UINT16)(UINTN)TssBase;\r
83 GdtDescriptor->Bits.BaseMid = (UINT8)((UINTN)TssBase >> 16);\r
84 GdtDescriptor->Bits.BaseHigh = (UINT8)((UINTN)TssBase >> 24);\r
85\r
86 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
87 //\r
88 // Setup top of known good stack as IST1 for each processor.\r
89 //\r
90 *(UINTN *)(TssBase + TSS_X64_IST1_OFFSET) = (mSmmStackArrayBase + EFI_PAGE_SIZE + Index * mSmmStackSize);\r
91 }\r
92 }\r
93\r
94 *GdtStepSize = GdtTableStepSize;\r
95 return GdtTssTables;\r
96}\r
97\r
98/**\r
99 Get Protected mode code segment from current GDT table.\r
100\r
101 @return Protected mode code segment value.\r
102**/\r
103UINT16\r
104GetProtectedModeCS (\r
105 VOID\r
106 )\r
107{\r
108 IA32_DESCRIPTOR GdtrDesc;\r
109 IA32_SEGMENT_DESCRIPTOR *GdtEntry;\r
110 UINTN GdtEntryCount;\r
111 UINT16 Index;\r
112\r
113 AsmReadGdtr (&GdtrDesc);\r
114 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
115 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;\r
116 for (Index = 0; Index < GdtEntryCount; Index++) {\r
117 if (GdtEntry->Bits.L == 0) {\r
118 if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.L == 0) {\r
119 break;\r
120 }\r
121 }\r
122 GdtEntry++;\r
123 }\r
124 ASSERT (Index != GdtEntryCount);\r
125 return Index * 8;\r
126}\r
127\r
128/**\r
129 Transfer AP to safe hlt-loop after it finished restore CPU features on S3 patch.\r
130\r
131 @param[in] ApHltLoopCode The address of the safe hlt-loop function.\r
132 @param[in] TopOfStack A pointer to the new stack to use for the ApHltLoopCode.\r
133 @param[in] NumberToFinishAddress Address of Semaphore of APs finish count.\r
134\r
135**/\r
136VOID\r
137TransferApToSafeState (\r
138 IN UINTN ApHltLoopCode,\r
139 IN UINTN TopOfStack,\r
140 IN UINTN NumberToFinishAddress\r
141 )\r
142{\r
143 AsmDisablePaging64 (\r
144 GetProtectedModeCS (),\r
145 (UINT32)ApHltLoopCode,\r
146 (UINT32)NumberToFinishAddress,\r
147 0,\r
148 (UINT32)TopOfStack\r
149 );\r
150 //\r
151 // It should never reach here\r
152 //\r
153 ASSERT (FALSE);\r
154}\r
155\r