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