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