2 Set a IDT entry for debug purpose
4 Set a IDT entry for interrupt vector 3 for debug purpose for x64 platform
6 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "ScriptExecute.h"
20 // 8 extra pages for PF handler.
22 #define EXTRA_PAGE_TABLE_PAGES 8
24 #define IA32_PG_P BIT0
25 #define IA32_PG_RW BIT1
26 #define IA32_PG_PS BIT7
29 VOID
*mOriginalHandler
;
30 UINTN mPageFaultBuffer
;
31 UINTN mPageFaultIndex
= 0;
33 // Store the uplink information for each page being used.
35 UINT64
*mPageFaultUplink
[EXTRA_PAGE_TABLE_PAGES
];
43 PageFaultHandlerHook (
48 Hook IDT with our page fault handler so that the on-demand paging works on page fault.
50 @param IdtEntry a pointer to IDT entry
54 HookPageFaultHandler (
55 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
59 UINT8 PhysicalAddressBits
;
60 UINTN PageFaultHandlerHookAddress
;
62 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
63 if (RegEax
>= 0x80000008) {
64 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
65 PhysicalAddressBits
= (UINT8
) RegEax
;
67 PhysicalAddressBits
= 36;
69 mPhyMask
= LShiftU64 (1, PhysicalAddressBits
) - 1;
70 mPhyMask
&= (1ull << 48) - SIZE_4KB
;
73 // Set Page Fault entry to catch >4G access
75 PageFaultHandlerHookAddress
= (UINTN
)PageFaultHandlerHook
;
76 mOriginalHandler
= (VOID
*)(UINTN
)(LShiftU64 (IdtEntry
->Bits
.OffsetUpper
, 32) + IdtEntry
->Bits
.OffsetLow
+ (IdtEntry
->Bits
.OffsetHigh
<< 16));
77 IdtEntry
->Bits
.OffsetLow
= (UINT16
)PageFaultHandlerHookAddress
;
78 IdtEntry
->Bits
.Selector
= (UINT16
)AsmReadCs ();
79 IdtEntry
->Bits
.Reserved_0
= 0;
80 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
81 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(PageFaultHandlerHookAddress
>> 16);
82 IdtEntry
->Bits
.OffsetUpper
= (UINT32
)(PageFaultHandlerHookAddress
>> 32);
83 IdtEntry
->Bits
.Reserved_1
= 0;
86 mPageFaultBuffer
= (UINTN
)(AsmReadCr3 () & mPhyMask
) + EFI_PAGES_TO_SIZE(2);
88 mPageFaultBuffer
= (UINTN
)(AsmReadCr3 () & mPhyMask
) + EFI_PAGES_TO_SIZE(6);
90 ZeroMem (mPageFaultUplink
, sizeof (mPageFaultUplink
));
94 The function will check if current waking vector is long mode.
96 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
98 @retval TRUE Current context need long mode waking vector.
99 @retval FALSE Current context need not long mode waking vector.
102 IsLongModeWakingVector (
103 IN ACPI_S3_CONTEXT
*AcpiS3Context
106 EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*Facs
;
108 Facs
= (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*) ((UINTN
) (AcpiS3Context
->AcpiFacsTable
));
109 if ((Facs
== NULL
) ||
110 (Facs
->Signature
!= EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE
) ||
111 ((Facs
->FirmwareWakingVector
== 0) && (Facs
->XFirmwareWakingVector
== 0)) ) {
112 // Something wrong with FACS
115 if (Facs
->XFirmwareWakingVector
!= 0) {
116 if ((Facs
->Version
== EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION
) &&
117 ((Facs
->Flags
& EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F
) != 0) &&
118 ((Facs
->Flags
& EFI_ACPI_4_0_OSPM_64BIT_WAKE__F
) != 0)) {
119 // Both BIOS and OS wants 64bit vector
120 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
129 Set a IDT entry for interrupt vector 3 for debug purpose.
131 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
136 IN ACPI_S3_CONTEXT
*AcpiS3Context
139 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
140 IA32_DESCRIPTOR
*IdtDescriptor
;
145 // Restore IDT for debug
147 IdtDescriptor
= (IA32_DESCRIPTOR
*) (UINTN
) (AcpiS3Context
->IdtrProfile
);
148 AsmWriteIdtr (IdtDescriptor
);
151 // Setup the default CPU exception handlers
153 Status
= InitializeCpuExceptionHandlers (NULL
);
154 ASSERT_EFI_ERROR (Status
);
158 // Update IDT entry INT3 if the instruction is valid in it
160 S3DebugBuffer
= (UINTN
) (AcpiS3Context
->S3DebugBufferAddress
);
161 if (*(UINTN
*)S3DebugBuffer
!= (UINTN
) -1) {
162 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*)(IdtDescriptor
->Base
+ (3 * sizeof (IA32_IDT_GATE_DESCRIPTOR
)));
163 IdtEntry
->Bits
.OffsetLow
= (UINT16
)S3DebugBuffer
;
164 IdtEntry
->Bits
.Selector
= (UINT16
)AsmReadCs ();
165 IdtEntry
->Bits
.Reserved_0
= 0;
166 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
167 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(S3DebugBuffer
>> 16);
168 IdtEntry
->Bits
.OffsetUpper
= (UINT32
)(S3DebugBuffer
>> 32);
169 IdtEntry
->Bits
.Reserved_1
= 0;
174 // If both BIOS and OS wants long mode waking vector,
175 // S3ResumePei should have established 1:1 Virtual to Physical identity mapping page table,
176 // no need to hook page fault handler.
178 if (!IsLongModeWakingVector (AcpiS3Context
)) {
179 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*)(IdtDescriptor
->Base
+ (14 * sizeof (IA32_IDT_GATE_DESCRIPTOR
)));
180 HookPageFaultHandler (IdtEntry
);
185 Acquire page for page fault.
187 @param[in, out] Uplink Pointer to up page table entry.
192 IN OUT UINT64
*Uplink
197 Address
= mPageFaultBuffer
+ EFI_PAGES_TO_SIZE (mPageFaultIndex
);
198 ZeroMem ((VOID
*) Address
, EFI_PAGES_TO_SIZE (1));
201 // Cut the previous uplink if it exists and wasn't overwritten.
203 if ((mPageFaultUplink
[mPageFaultIndex
] != NULL
) && ((*mPageFaultUplink
[mPageFaultIndex
] & mPhyMask
) == Address
)) {
204 *mPageFaultUplink
[mPageFaultIndex
] = 0;
208 // Link & Record the current uplink.
210 *Uplink
= Address
| IA32_PG_P
| IA32_PG_RW
;
211 mPageFaultUplink
[mPageFaultIndex
] = Uplink
;
213 mPageFaultIndex
= (mPageFaultIndex
+ 1) % EXTRA_PAGE_TABLE_PAGES
;
217 The page fault handler that on-demand read >4G memory/MMIO.
219 @retval TRUE The page fault is correctly handled.
220 @retval FALSE The page fault is not handled and is passed through to original handler.
233 PFAddress
= AsmReadCr2 ();
234 DEBUG ((EFI_D_ERROR
, "BootScript - PageFaultHandler: Cr2 - %lx\n", PFAddress
));
236 if (PFAddress
>= mPhyMask
+ SIZE_4KB
) {
239 PFAddress
&= mPhyMask
;
241 PageTable
= (UINT64
*)(UINTN
)(AsmReadCr3 () & mPhyMask
);
243 PTIndex
= BitFieldRead64 (PFAddress
, 39, 47);
245 if ((PageTable
[PTIndex
] & IA32_PG_P
) == 0) {
246 AcquirePage (&PageTable
[PTIndex
]);
248 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & mPhyMask
);
249 PTIndex
= BitFieldRead64 (PFAddress
, 30, 38);
251 if (mPage1GSupport
) {
252 PageTable
[PTIndex
] = (PFAddress
& ~((1ull << 30) - 1)) | IA32_PG_P
| IA32_PG_RW
| IA32_PG_PS
;
254 if ((PageTable
[PTIndex
] & IA32_PG_P
) == 0) {
255 AcquirePage (&PageTable
[PTIndex
]);
257 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & mPhyMask
);
258 PTIndex
= BitFieldRead64 (PFAddress
, 21, 29);
260 PageTable
[PTIndex
] = (PFAddress
& ~((1ull << 21) - 1)) | IA32_PG_P
| IA32_PG_RW
| IA32_PG_PS
;