]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
1. DxeIplPeim will locate Vector Handoff Table PPI and build GUIDed HOB if it has.
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / Ia32 / DxeLoadFunc.c
1 /** @file
2 Ia32-specific functionality for DxeLoad.
3
4 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "DxeIpl.h"
16 #include "VirtualMemory.h"
17
18 #define IDT_ENTRY_COUNT 32
19
20 typedef struct _X64_IDT_TABLE {
21 //
22 // Reserved 4 bytes preceding PeiService and IdtTable,
23 // since IDT base address should be 8-byte alignment.
24 //
25 UINT32 Reserved;
26 CONST EFI_PEI_SERVICES **PeiService;
27 X64_IDT_GATE_DESCRIPTOR IdtTable[IDT_ENTRY_COUNT];
28 } X64_IDT_TABLE;
29
30 //
31 // Global Descriptor Table (GDT)
32 //
33 GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT gGdtEntries[] = {
34 /* selector { Global Segment Descriptor } */
35 /* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //null descriptor
36 /* 0x08 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear data segment descriptor
37 /* 0x10 */ {{0xffff, 0, 0, 0xf, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear code segment descriptor
38 /* 0x18 */ {{0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor
39 /* 0x20 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system code segment descriptor
40 /* 0x28 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor
41 /* 0x30 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor
42 /* 0x38 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 1, 0, 1, 0}}, //system code segment descriptor
43 /* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor
44 };
45
46 //
47 // IA32 Gdt register
48 //
49 GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR gGdt = {
50 sizeof (gGdtEntries) - 1,
51 (UINTN) gGdtEntries
52 };
53
54 GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR gLidtDescriptor = {
55 sizeof (X64_IDT_GATE_DESCRIPTOR) * IDT_ENTRY_COUNT - 1,
56 0
57 };
58
59 /**
60 Transfers control to DxeCore.
61
62 This function performs a CPU architecture specific operations to execute
63 the entry point of DxeCore with the parameters of HobList.
64 It also installs EFI_END_OF_PEI_PPI to signal the end of PEI phase.
65
66 @param DxeCoreEntryPoint The entry point of DxeCore.
67 @param HobList The start of HobList passed to DxeCore.
68
69 **/
70 VOID
71 HandOffToDxeCore (
72 IN EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint,
73 IN EFI_PEI_HOB_POINTERS HobList
74 )
75 {
76 EFI_STATUS Status;
77 EFI_PHYSICAL_ADDRESS BaseOfStack;
78 EFI_PHYSICAL_ADDRESS TopOfStack;
79 UINTN PageTables;
80 X64_IDT_GATE_DESCRIPTOR *IdtTable;
81 UINTN SizeOfTemplate;
82 VOID *TemplateBase;
83 EFI_PHYSICAL_ADDRESS VectorAddress;
84 UINT32 Index;
85 X64_IDT_TABLE *IdtTableForX64;
86 EFI_VECTOR_HANDOFF_INFO *VectorInfo;
87 EFI_PEI_VECTOR_HANDOFF_INFO_PPI *VectorHandoffInfoPpi;
88
89 Status = PeiServicesAllocatePages (EfiBootServicesData, EFI_SIZE_TO_PAGES (STACK_SIZE), &BaseOfStack);
90 ASSERT_EFI_ERROR (Status);
91
92 if (FeaturePcdGet(PcdDxeIplSwitchToLongMode)) {
93 //
94 // Compute the top of the stack we were allocated, which is used to load X64 dxe core.
95 // Pre-allocate a 32 bytes which confroms to x64 calling convention.
96 //
97 // The first four parameters to a function are passed in rcx, rdx, r8 and r9.
98 // Any further parameters are pushed on the stack. Furthermore, space (4 * 8bytes) for the
99 // register parameters is reserved on the stack, in case the called function
100 // wants to spill them; this is important if the function is variadic.
101 //
102 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - 32;
103
104 //
105 // x64 Calling Conventions requires that the stack must be aligned to 16 bytes
106 //
107 TopOfStack = (EFI_PHYSICAL_ADDRESS) (UINTN) ALIGN_POINTER (TopOfStack, 16);
108
109 //
110 // Load the GDT of Go64. Since the GDT of 32-bit Tiano locates in the BS_DATA
111 // memory, it may be corrupted when copying FV to high-end memory
112 //
113 AsmWriteGdtr (&gGdt);
114 //
115 // Create page table and save PageMapLevel4 to CR3
116 //
117 PageTables = CreateIdentityMappingPageTables ();
118
119 //
120 // End of PEI phase signal
121 //
122 Status = PeiServicesInstallPpi (&gEndOfPeiSignalPpi);
123 ASSERT_EFI_ERROR (Status);
124
125 AsmWriteCr3 (PageTables);
126
127 //
128 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.
129 //
130 UpdateStackHob (BaseOfStack, STACK_SIZE);
131
132 SizeOfTemplate = AsmGetVectorTemplatInfo (&TemplateBase);
133
134 Status = PeiServicesAllocatePages (
135 EfiBootServicesData,
136 EFI_SIZE_TO_PAGES(sizeof (X64_IDT_TABLE) + SizeOfTemplate * IDT_ENTRY_COUNT),
137 (EFI_PHYSICAL_ADDRESS *) &IdtTableForX64
138 );
139 ASSERT_EFI_ERROR (Status);
140
141 //
142 // Store EFI_PEI_SERVICES** in the 4 bytes immediately preceding IDT to avoid that
143 // it may not be gotten correctly after IDT register is re-written.
144 //
145 IdtTableForX64->PeiService = GetPeiServicesTablePointer ();
146
147 VectorAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) (IdtTableForX64 + 1);
148 IdtTable = IdtTableForX64->IdtTable;
149 for (Index = 0; Index < IDT_ENTRY_COUNT; Index++) {
150 IdtTable[Index].Ia32IdtEntry.Bits.GateType = 0x8e;
151 IdtTable[Index].Ia32IdtEntry.Bits.Reserved_0 = 0;
152 IdtTable[Index].Ia32IdtEntry.Bits.Selector = SYS_CODE64_SEL;
153
154 IdtTable[Index].Ia32IdtEntry.Bits.OffsetLow = (UINT16) VectorAddress;
155 IdtTable[Index].Ia32IdtEntry.Bits.OffsetHigh = (UINT16) (RShiftU64 (VectorAddress, 16));
156 IdtTable[Index].Offset32To63 = (UINT32) (RShiftU64 (VectorAddress, 32));
157 IdtTable[Index].Reserved = 0;
158
159 CopyMem ((VOID *) (UINTN) VectorAddress, TemplateBase, SizeOfTemplate);
160 AsmVectorFixup ((VOID *) (UINTN) VectorAddress, (UINT8) Index);
161
162 VectorAddress += SizeOfTemplate;
163 }
164
165 gLidtDescriptor.Base = (UINTN) IdtTable;
166
167 //
168 // Disable interrupt of Debug timer, since new IDT table cannot handle it.
169 //
170 SaveAndSetDebugTimerInterrupt (FALSE);
171
172 AsmWriteIdtr (&gLidtDescriptor);
173
174 //
175 // Go to Long Mode and transfer control to DxeCore.
176 // Interrupts will not get turned on until the CPU AP is loaded.
177 // Call x64 drivers passing in single argument, a pointer to the HOBs.
178 //
179 AsmEnablePaging64 (
180 SYS_CODE64_SEL,
181 DxeCoreEntryPoint,
182 (EFI_PHYSICAL_ADDRESS)(UINTN)(HobList.Raw),
183 0,
184 TopOfStack
185 );
186 } else {
187 //
188 // Get Vector Hand-off Info PPI and build Guided HOB
189 //
190 Status = PeiServicesLocatePpi (
191 &gEfiVectorHandoffInfoPpiGuid,
192 0,
193 NULL,
194 (VOID **)&VectorHandoffInfoPpi
195 );
196 if (Status == EFI_SUCCESS) {
197 DEBUG ((EFI_D_INFO, "Vector Hand-off Info PPI is gotten, GUIDed HOB is created!\n"));
198 VectorInfo = VectorHandoffInfoPpi->Info;
199 Index = 1;
200 while (VectorInfo->Attribute != EFI_VECTOR_HANDOFF_LAST_ENTRY) {
201 VectorInfo ++;
202 Index ++;
203 }
204 BuildGuidDataHob (
205 &gEfiVectorHandoffInfoPpiGuid,
206 VectorHandoffInfoPpi->Info,
207 sizeof (EFI_VECTOR_HANDOFF_INFO) * Index
208 );
209 }
210
211 //
212 // Compute the top of the stack we were allocated. Pre-allocate a UINTN
213 // for safety.
214 //
215 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - CPU_STACK_ALIGNMENT;
216 TopOfStack = (EFI_PHYSICAL_ADDRESS) (UINTN) ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);
217
218 //
219 // End of PEI phase signal
220 //
221 Status = PeiServicesInstallPpi (&gEndOfPeiSignalPpi);
222 ASSERT_EFI_ERROR (Status);
223
224 //
225 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.
226 //
227 UpdateStackHob (BaseOfStack, STACK_SIZE);
228
229 //
230 // Transfer the control to the entry point of DxeCore.
231 //
232 SwitchStack (
233 (SWITCH_STACK_ENTRY_POINT)(UINTN)DxeCoreEntryPoint,
234 HobList.Raw,
235 NULL,
236 (VOID *) (UINTN) TopOfStack
237 );
238 }
239 }
240