]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c
UefiCpuPkg/dec: Add PcdCpuSmmStaticPageTable.
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / X64 / SmmFuncsArch.c
CommitLineData
fe5f1949
JY
1/** @file\r
2 SMM CPU misc functions for x64 arch specific.\r
3 \r
4a0f88dd 4Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
fe5f1949
JY
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 //\r
40 // For X64 SMM, we allocate separate GDT/TSS for each CPUs to avoid TSS load contention\r
41 // on each SMI entry.\r
42 //\r
43 GdtTssTableSize = (gcSmiGdtr.Limit + 1 + TSS_SIZE + 7) & ~7; // 8 bytes aligned\r
44 GdtTssTables = (UINT8*)AllocatePages (EFI_SIZE_TO_PAGES (GdtTssTableSize * gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus));\r
45 ASSERT (GdtTssTables != NULL);\r
46 GdtTableStepSize = GdtTssTableSize;\r
47\r
48 for (Index = 0; Index < gSmmCpuPrivate->SmmCoreEntryContext.NumberOfCpus; Index++) {\r
49 CopyMem (GdtTssTables + GdtTableStepSize * Index, (VOID*)(UINTN)gcSmiGdtr.Base, gcSmiGdtr.Limit + 1 + TSS_SIZE);\r
50\r
51 //\r
52 // Fixup TSS descriptors\r
53 //\r
54 TssBase = (UINTN)(GdtTssTables + GdtTableStepSize * Index + gcSmiGdtr.Limit + 1);\r
55 GdtDescriptor = (IA32_SEGMENT_DESCRIPTOR *)(TssBase) - 2;\r
56 GdtDescriptor->Bits.BaseLow = (UINT16)(UINTN)TssBase;\r
57 GdtDescriptor->Bits.BaseMid = (UINT8)((UINTN)TssBase >> 16);\r
58 GdtDescriptor->Bits.BaseHigh = (UINT8)((UINTN)TssBase >> 24);\r
59\r
60 if (FeaturePcdGet (PcdCpuSmmStackGuard)) {\r
61 //\r
62 // Setup top of known good stack as IST1 for each processor.\r
63 //\r
64 *(UINTN *)(TssBase + TSS_X64_IST1_OFFSET) = (mSmmStackArrayBase + EFI_PAGE_SIZE + Index * mSmmStackSize);\r
65 }\r
66 }\r
67\r
68 *GdtStepSize = GdtTableStepSize;\r
69 return GdtTssTables;\r
70}\r
4a0f88dd 71\r
45e3440a
JF
72/**\r
73 Get Protected mode code segment from current GDT table.\r
74\r
75 @return Protected mode code segment value.\r
76**/\r
77UINT16\r
78GetProtectedModeCS (\r
79 VOID\r
80 )\r
81{\r
82 IA32_DESCRIPTOR GdtrDesc;\r
83 IA32_SEGMENT_DESCRIPTOR *GdtEntry;\r
84 UINTN GdtEntryCount;\r
85 UINT16 Index;\r
86\r
87 Index = (UINT16) -1;\r
88 AsmReadGdtr (&GdtrDesc);\r
89 GdtEntryCount = (GdtrDesc.Limit + 1) / sizeof (IA32_SEGMENT_DESCRIPTOR);\r
90 GdtEntry = (IA32_SEGMENT_DESCRIPTOR *) GdtrDesc.Base;\r
91 for (Index = 0; Index < GdtEntryCount; Index++) {\r
92 if (GdtEntry->Bits.L == 0) {\r
93 if (GdtEntry->Bits.Type > 8 && GdtEntry->Bits.L == 0) {\r
94 break;\r
95 }\r
96 }\r
97 GdtEntry++;\r
98 }\r
99 ASSERT (Index != -1);\r
100 return Index * 8;\r
101}\r
102\r
4a0f88dd
JF
103/**\r
104 Transfer AP to safe hlt-loop after it finished restore CPU features on S3 patch.\r
105\r
106 @param[in] ApHltLoopCode The 32-bit address of the safe hlt-loop function.\r
107 @param[in] TopOfStack A pointer to the new stack to use for the ApHltLoopCode.\r
ec8a3877 108 @param[in] NumberToFinish Semaphore of APs finish count.\r
4a0f88dd
JF
109\r
110**/\r
111VOID\r
112TransferApToSafeState (\r
113 IN UINT32 ApHltLoopCode,\r
ec8a3877
JF
114 IN UINT32 TopOfStack,\r
115 IN UINT32 *NumberToFinish\r
4a0f88dd
JF
116 )\r
117{\r
45e3440a
JF
118 AsmDisablePaging64 (\r
119 GetProtectedModeCS (),\r
120 (UINT32) (UINTN) ApHltLoopCode,\r
ec8a3877 121 (UINT32) (UINTN) NumberToFinish,\r
45e3440a
JF
122 0,\r
123 TopOfStack\r
4a0f88dd
JF
124 );\r
125 //\r
126 // It should never reach here\r
127 //\r
128 ASSERT (FALSE);\r
129}\r
130\r
131\r