]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Core / DxeIplPeim / Ia32 / DxeLoadFunc.c
1 /** @file
2 Ia32-specific functionality for DxeLoad.
3
4 Copyright (c) 2006 - 2010, 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 //
21 // Global Descriptor Table (GDT)
22 //
23 GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT gGdtEntries[] = {
24 /* selector { Global Segment Descriptor } */
25 /* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //null descriptor
26 /* 0x08 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear data segment descriptor
27 /* 0x10 */ {{0xffff, 0, 0, 0xf, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //linear code segment descriptor
28 /* 0x18 */ {{0xffff, 0, 0, 0x3, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor
29 /* 0x20 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system code segment descriptor
30 /* 0x28 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor
31 /* 0x30 */ {{0xffff, 0, 0, 0x2, 1, 0, 1, 0xf, 0, 0, 1, 1, 0}}, //system data segment descriptor
32 /* 0x38 */ {{0xffff, 0, 0, 0xa, 1, 0, 1, 0xf, 0, 1, 0, 1, 0}}, //system code segment descriptor
33 /* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, //spare segment descriptor
34 };
35
36 //
37 // IA32 Gdt register
38 //
39 GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR gGdt = {
40 sizeof (gGdtEntries) - 1,
41 (UINTN) gGdtEntries
42 };
43
44 GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR gLidtDescriptor = {
45 sizeof (X64_IDT_GATE_DESCRIPTOR) * IDT_ENTRY_COUNT - 1,
46 0
47 };
48
49 /**
50 Transfers control to DxeCore.
51
52 This function performs a CPU architecture specific operations to execute
53 the entry point of DxeCore with the parameters of HobList.
54 It also installs EFI_END_OF_PEI_PPI to signal the end of PEI phase.
55
56 @param DxeCoreEntryPoint The entry point of DxeCore.
57 @param HobList The start of HobList passed to DxeCore.
58
59 **/
60 VOID
61 HandOffToDxeCore (
62 IN EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint,
63 IN EFI_PEI_HOB_POINTERS HobList
64 )
65 {
66 EFI_STATUS Status;
67 EFI_PHYSICAL_ADDRESS BaseOfStack;
68 EFI_PHYSICAL_ADDRESS TopOfStack;
69 UINTN PageTables;
70 X64_IDT_GATE_DESCRIPTOR *IdtTable;
71 UINTN SizeOfTemplate;
72 VOID *TemplateBase;
73 EFI_PHYSICAL_ADDRESS VectorAddress;
74 UINT32 Index;
75
76 Status = PeiServicesAllocatePages (EfiBootServicesData, EFI_SIZE_TO_PAGES (STACK_SIZE), &BaseOfStack);
77 ASSERT_EFI_ERROR (Status);
78
79 if (FeaturePcdGet(PcdDxeIplSwitchToLongMode)) {
80 //
81 // Compute the top of the stack we were allocated, which is used to load X64 dxe core.
82 // Pre-allocate a 32 bytes which confroms to x64 calling convention.
83 //
84 // The first four parameters to a function are passed in rcx, rdx, r8 and r9.
85 // Any further parameters are pushed on the stack. Furthermore, space (4 * 8bytes) for the
86 // register parameters is reserved on the stack, in case the called function
87 // wants to spill them; this is important if the function is variadic.
88 //
89 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - 32;
90
91 //
92 // x64 Calling Conventions requires that the stack must be aligned to 16 bytes
93 //
94 TopOfStack = (EFI_PHYSICAL_ADDRESS) (UINTN) ALIGN_POINTER (TopOfStack, 16);
95
96 //
97 // Load the GDT of Go64. Since the GDT of 32-bit Tiano locates in the BS_DATA
98 // memory, it may be corrupted when copying FV to high-end memory
99 //
100 AsmWriteGdtr (&gGdt);
101 //
102 // Create page table and save PageMapLevel4 to CR3
103 //
104 PageTables = CreateIdentityMappingPageTables ();
105
106 //
107 // End of PEI phase signal
108 //
109 Status = PeiServicesInstallPpi (&gEndOfPeiSignalPpi);
110 ASSERT_EFI_ERROR (Status);
111
112 AsmWriteCr3 (PageTables);
113
114 //
115 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.
116 //
117 UpdateStackHob (BaseOfStack, STACK_SIZE);
118
119 SizeOfTemplate = AsmGetVectorTemplatInfo (&TemplateBase);
120
121 Status = PeiServicesAllocatePages (
122 EfiBootServicesData,
123 EFI_SIZE_TO_PAGES((SizeOfTemplate + sizeof (X64_IDT_GATE_DESCRIPTOR)) * IDT_ENTRY_COUNT),
124 &VectorAddress
125 );
126 ASSERT_EFI_ERROR (Status);
127
128 IdtTable = (X64_IDT_GATE_DESCRIPTOR *) (UINTN) (VectorAddress + SizeOfTemplate * IDT_ENTRY_COUNT);
129 for (Index = 0; Index < IDT_ENTRY_COUNT; Index++) {
130 IdtTable[Index].Ia32IdtEntry.Bits.GateType = 0x8e;
131 IdtTable[Index].Ia32IdtEntry.Bits.Reserved_0 = 0;
132 IdtTable[Index].Ia32IdtEntry.Bits.Selector = SYS_CODE64_SEL;
133
134 IdtTable[Index].Ia32IdtEntry.Bits.OffsetLow = (UINT16) VectorAddress;
135 IdtTable[Index].Ia32IdtEntry.Bits.OffsetHigh = (UINT16) (RShiftU64 (VectorAddress, 16));
136 IdtTable[Index].Offset32To63 = (UINT32) (RShiftU64 (VectorAddress, 32));
137 IdtTable[Index].Reserved = 0;
138
139 CopyMem ((VOID *) (UINTN) VectorAddress, TemplateBase, SizeOfTemplate);
140 AsmVectorFixup ((VOID *) (UINTN) VectorAddress, (UINT8) Index);
141
142 VectorAddress += SizeOfTemplate;
143 }
144
145 gLidtDescriptor.Base = (UINTN) IdtTable;
146
147 //
148 // Disable interrupt of Debug timer, since new IDT table cannot handle it.
149 //
150 SaveAndSetDebugTimerInterrupt (FALSE);
151
152 AsmWriteIdtr (&gLidtDescriptor);
153
154 //
155 // Go to Long Mode and transfer control to DxeCore.
156 // Interrupts will not get turned on until the CPU AP is loaded.
157 // Call x64 drivers passing in single argument, a pointer to the HOBs.
158 //
159 AsmEnablePaging64 (
160 SYS_CODE64_SEL,
161 DxeCoreEntryPoint,
162 (EFI_PHYSICAL_ADDRESS)(UINTN)(HobList.Raw),
163 0,
164 TopOfStack
165 );
166 } else {
167 //
168 // Compute the top of the stack we were allocated. Pre-allocate a UINTN
169 // for safety.
170 //
171 TopOfStack = BaseOfStack + EFI_SIZE_TO_PAGES (STACK_SIZE) * EFI_PAGE_SIZE - CPU_STACK_ALIGNMENT;
172 TopOfStack = (EFI_PHYSICAL_ADDRESS) (UINTN) ALIGN_POINTER (TopOfStack, CPU_STACK_ALIGNMENT);
173
174 //
175 // End of PEI phase signal
176 //
177 Status = PeiServicesInstallPpi (&gEndOfPeiSignalPpi);
178 ASSERT_EFI_ERROR (Status);
179
180 //
181 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.
182 //
183 UpdateStackHob (BaseOfStack, STACK_SIZE);
184
185 //
186 // Transfer the control to the entry point of DxeCore.
187 //
188 SwitchStack (
189 (SWITCH_STACK_ENTRY_POINT)(UINTN)DxeCoreEntryPoint,
190 HobList.Raw,
191 NULL,
192 (VOID *) (UINTN) TopOfStack
193 );
194 }
195 }
196