]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
PI spec will be updated to make it clear that this HOB should describe the stack...
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / Ia32 / DxeLoadFunc.c
... / ...
CommitLineData
1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 DxeLoadFunc.c\r
15\r
16Abstract:\r
17\r
18 Ia32-specifc functionality for DxeLoad.\r
19\r
20--*/\r
21\r
22#include "DxeIpl.h"\r
23#include "VirtualMemory.h"\r
24\r
25//\r
26// Global Descriptor Table (GDT)\r
27//\r
28GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT gGdtEntries [] = {\r
29/* selector { Global Segment Descriptor } */ \r
30/* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //null descriptor \r
31/* 0x08 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear data segment descriptor\r
32/* 0x10 */ {{0xffff, 0, 0, 0xf, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear code segment descriptor\r
33/* 0x18 */ {{0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor\r
34/* 0x20 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system code segment descriptor\r
35/* 0x28 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor\r
36/* 0x30 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor\r
37/* 0x38 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 1, 0, 1, 0}}, //system code segment descriptor\r
38/* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor\r
39};\r
40\r
41//\r
42// IA32 Gdt register\r
43//\r
44GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR gGdt = {\r
45 sizeof (gGdtEntries) - 1,\r
46 (UINTN) gGdtEntries\r
47 };\r
48\r
49GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR gLidtDescriptor = {\r
50 sizeof (X64_IDT_GATE_DESCRIPTOR) * 32 - 1,\r
51 0\r
52};\r
53\r
54VOID\r
55HandOffToDxeCore (\r
56 IN EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint,\r
57 IN EFI_PEI_HOB_POINTERS HobList,\r
58 IN EFI_PEI_PPI_DESCRIPTOR *EndOfPeiSignal\r
59 )\r
60{\r
61 EFI_STATUS Status;\r
62 EFI_PHYSICAL_ADDRESS BaseOfStack;\r
63 EFI_PHYSICAL_ADDRESS TopOfStack;\r
64 UINTN PageTables;\r
65 X64_IDT_GATE_DESCRIPTOR *IdtTable;\r
66 UINTN SizeOfTemplate;\r
67 VOID *TemplateBase;\r
68 EFI_PHYSICAL_ADDRESS VectorAddress;\r
69 UINT32 Index;\r
70\r
71 Status = PeiServicesAllocatePages (EfiBootServicesData, EFI_SIZE_TO_PAGES (STACK_SIZE), &BaseOfStack);\r
72 ASSERT_EFI_ERROR (Status);\r
73 \r
74 if (FeaturePcdGet(PcdDxeIplSwitchToLongMode)) {\r
75 //\r
76 // Compute the top of the stack we were allocated, which is used to load X64 dxe core. \r
77 // Pre-allocate a 32 bytes which confroms to x64 calling convention.\r
78 //\r
79 // The first four parameters to a function are passed in rcx, rdx, r8 and r9. \r
80 // Any further parameters are pushed on the stack. Furthermore, space (4 * 8bytes) for the \r
81 // register parameters is reserved on the stack, in case the called function \r
82 // wants to spill them; this is important if the function is variadic. \r
83 //\r
84 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - 32;\r
85\r
86 //\r
87 // X64 Calling Conventions requires that the stack must be aligned to 16 bytes\r
88 //\r
89 TopOfStack = (EFI_PHYSICAL_ADDRESS) (UINTN) ALIGN_POINTER (TopOfStack, 16);\r
90\r
91 //\r
92 // Load the GDT of Go64. Since the GDT of 32-bit Tiano locates in the BS_DATA\r
93 // memory, it may be corrupted when copying FV to high-end memory \r
94 //\r
95 AsmWriteGdtr (&gGdt);\r
96 //\r
97 // Create page table and save PageMapLevel4 to CR3\r
98 //\r
99 PageTables = CreateIdentityMappingPageTables ();\r
100\r
101 //\r
102 // End of PEI phase singal\r
103 //\r
104 Status = PeiServicesInstallPpi (EndOfPeiSignal);\r
105 ASSERT_EFI_ERROR (Status);\r
106 \r
107 AsmWriteCr3 (PageTables);\r
108\r
109 //\r
110 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.\r
111 // \r
112 UpdateStackHob (BaseOfStack, STACK_SIZE);\r
113\r
114 if (FeaturePcdGet (PcdDxeIplEnableIdt)) {\r
115 SizeOfTemplate = AsmGetVectorTemplatInfo (&TemplateBase);\r
116 \r
117 Status = PeiServicesAllocatePages (\r
118 EfiBootServicesData, \r
119 EFI_SIZE_TO_PAGES((SizeOfTemplate + sizeof (X64_IDT_GATE_DESCRIPTOR)) * 32), \r
120 &VectorAddress\r
121 );\r
122 \r
123 ASSERT_EFI_ERROR (Status);\r
124 \r
125 IdtTable = (X64_IDT_GATE_DESCRIPTOR *) (UINTN) (VectorAddress + SizeOfTemplate * 32);\r
126 for (Index = 0; Index < 32; Index++) {\r
127 IdtTable[Index].Ia32IdtEntry.Bits.GateType = 0x8e;\r
128 IdtTable[Index].Ia32IdtEntry.Bits.Reserved_0 = 0;\r
129 IdtTable[Index].Ia32IdtEntry.Bits.Selector = SYS_CODE64_SEL;\r
130 \r
131 IdtTable[Index].Ia32IdtEntry.Bits.OffsetLow = (UINT16) VectorAddress;\r
132 IdtTable[Index].Ia32IdtEntry.Bits.OffsetHigh = (UINT16) (RShiftU64 (VectorAddress, 16));\r
133 IdtTable[Index].Offset32To63 = (UINT32) (RShiftU64 (VectorAddress, 32));\r
134 IdtTable[Index].Reserved = 0;\r
135 \r
136 CopyMem ((VOID *) (UINTN) VectorAddress, TemplateBase, SizeOfTemplate);\r
137 AsmVectorFixup ((VOID *) (UINTN) VectorAddress, (UINT8) Index);\r
138 \r
139 VectorAddress += SizeOfTemplate;\r
140 }\r
141 \r
142 gLidtDescriptor.Base = (UINTN) IdtTable;\r
143 AsmWriteIdtr (&gLidtDescriptor);\r
144 }\r
145 //\r
146 // Go to Long Mode. Interrupts will not get turned on until the CPU AP is loaded.\r
147 // Call x64 drivers passing in single argument, a pointer to the HOBs.\r
148 // \r
149 AsmEnablePaging64 (\r
150 SYS_CODE64_SEL,\r
151 DxeCoreEntryPoint,\r
152 (EFI_PHYSICAL_ADDRESS)(UINTN)(HobList.Raw),\r
153 0,\r
154 TopOfStack\r
155 );\r
156 } else {\r
157 //\r
158 // Compute the top of the stack we were allocated. Pre-allocate a UINTN\r
159 // for safety.\r
160 //\r
161 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - CPU_STACK_ALIGNMENT;\r
162 TopOfStack = (EFI_PHYSICAL_ADDRESS) (UINTN) ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);\r
163\r
164 //\r
165 // End of PEI phase singal\r
166 //\r
167 Status = PeiServicesInstallPpi (EndOfPeiSignal);\r
168 ASSERT_EFI_ERROR (Status);\r
169\r
170 //\r
171 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.\r
172 // \r
173 UpdateStackHob (BaseOfStack, STACK_SIZE);\r
174\r
175 SwitchStack (\r
176 (SWITCH_STACK_ENTRY_POINT)(UINTN)DxeCoreEntryPoint,\r
177 HobList.Raw,\r
178 NULL,\r
179 (VOID *) (UINTN) TopOfStack\r
180 );\r
181 } \r
182}\r
183\r