2 Ia32-specific functionality for DxeLoad.
4 Copyright (c) 2006 - 2010, 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
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.
16 #include "VirtualMemory.h"
19 // Global Descriptor Table (GDT)
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
37 GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR gGdt
= {
38 sizeof (gGdtEntries
) - 1,
42 GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR gLidtDescriptor
= {
43 sizeof (X64_IDT_GATE_DESCRIPTOR
) * 32 - 1,
48 Transfers control to DxeCore.
50 This function performs a CPU architecture specific operations to execute
51 the entry point of DxeCore with the parameters of HobList.
52 It also installs EFI_END_OF_PEI_PPI to signal the end of PEI phase.
54 @param DxeCoreEntryPoint The entry point of DxeCore.
55 @param HobList The start of HobList passed to DxeCore.
60 IN EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint
,
61 IN EFI_PEI_HOB_POINTERS HobList
65 EFI_PHYSICAL_ADDRESS BaseOfStack
;
66 EFI_PHYSICAL_ADDRESS TopOfStack
;
68 X64_IDT_GATE_DESCRIPTOR
*IdtTable
;
71 EFI_PHYSICAL_ADDRESS VectorAddress
;
74 Status
= PeiServicesAllocatePages (EfiBootServicesData
, EFI_SIZE_TO_PAGES (STACK_SIZE
), &BaseOfStack
);
75 ASSERT_EFI_ERROR (Status
);
77 if (FeaturePcdGet(PcdDxeIplSwitchToLongMode
)) {
79 // Compute the top of the stack we were allocated, which is used to load X64 dxe core.
80 // Pre-allocate a 32 bytes which confroms to x64 calling convention.
82 // The first four parameters to a function are passed in rcx, rdx, r8 and r9.
83 // Any further parameters are pushed on the stack. Furthermore, space (4 * 8bytes) for the
84 // register parameters is reserved on the stack, in case the called function
85 // wants to spill them; this is important if the function is variadic.
87 TopOfStack
= BaseOfStack
+ EFI_SIZE_TO_PAGES (STACK_SIZE
) * EFI_PAGE_SIZE
- 32;
90 // x64 Calling Conventions requires that the stack must be aligned to 16 bytes
92 TopOfStack
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) ALIGN_POINTER (TopOfStack
, 16);
95 // Load the GDT of Go64. Since the GDT of 32-bit Tiano locates in the BS_DATA
96 // memory, it may be corrupted when copying FV to high-end memory
100 // Create page table and save PageMapLevel4 to CR3
102 PageTables
= CreateIdentityMappingPageTables ();
105 // End of PEI phase signal
107 Status
= PeiServicesInstallPpi (&gEndOfPeiSignalPpi
);
108 ASSERT_EFI_ERROR (Status
);
110 AsmWriteCr3 (PageTables
);
113 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.
115 UpdateStackHob (BaseOfStack
, STACK_SIZE
);
117 SizeOfTemplate
= AsmGetVectorTemplatInfo (&TemplateBase
);
119 Status
= PeiServicesAllocatePages (
121 EFI_SIZE_TO_PAGES((SizeOfTemplate
+ sizeof (X64_IDT_GATE_DESCRIPTOR
)) * 32),
124 ASSERT_EFI_ERROR (Status
);
126 IdtTable
= (X64_IDT_GATE_DESCRIPTOR
*) (UINTN
) (VectorAddress
+ SizeOfTemplate
* 32);
127 for (Index
= 0; Index
< 32; Index
++) {
128 IdtTable
[Index
].Ia32IdtEntry
.Bits
.GateType
= 0x8e;
129 IdtTable
[Index
].Ia32IdtEntry
.Bits
.Reserved_0
= 0;
130 IdtTable
[Index
].Ia32IdtEntry
.Bits
.Selector
= SYS_CODE64_SEL
;
132 IdtTable
[Index
].Ia32IdtEntry
.Bits
.OffsetLow
= (UINT16
) VectorAddress
;
133 IdtTable
[Index
].Ia32IdtEntry
.Bits
.OffsetHigh
= (UINT16
) (RShiftU64 (VectorAddress
, 16));
134 IdtTable
[Index
].Offset32To63
= (UINT32
) (RShiftU64 (VectorAddress
, 32));
135 IdtTable
[Index
].Reserved
= 0;
137 CopyMem ((VOID
*) (UINTN
) VectorAddress
, TemplateBase
, SizeOfTemplate
);
138 AsmVectorFixup ((VOID
*) (UINTN
) VectorAddress
, (UINT8
) Index
);
140 VectorAddress
+= SizeOfTemplate
;
143 gLidtDescriptor
.Base
= (UINTN
) IdtTable
;
145 AsmWriteIdtr (&gLidtDescriptor
);
148 // Go to Long Mode and transfer control to DxeCore.
149 // Interrupts will not get turned on until the CPU AP is loaded.
150 // Call x64 drivers passing in single argument, a pointer to the HOBs.
155 (EFI_PHYSICAL_ADDRESS
)(UINTN
)(HobList
.Raw
),
161 // Compute the top of the stack we were allocated. Pre-allocate a UINTN
164 TopOfStack
= BaseOfStack
+ EFI_SIZE_TO_PAGES (STACK_SIZE
) * EFI_PAGE_SIZE
- CPU_STACK_ALIGNMENT
;
165 TopOfStack
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) ALIGN_POINTER (TopOfStack
, CPU_STACK_ALIGNMENT
);
168 // End of PEI phase signal
170 Status
= PeiServicesInstallPpi (&gEndOfPeiSignalPpi
);
171 ASSERT_EFI_ERROR (Status
);
174 // Update the contents of BSP stack HOB to reflect the real stack info passed to DxeCore.
176 UpdateStackHob (BaseOfStack
, STACK_SIZE
);
179 // Transfer the control to the entry point of DxeCore.
182 (SWITCH_STACK_ENTRY_POINT
)(UINTN
)DxeCoreEntryPoint
,
185 (VOID
*) (UINTN
) TopOfStack