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 - 2022, Intel Corporation. All rights reserved.<BR>
7 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
10 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include "ScriptExecute.h"
16 // 8 extra pages for PF handler.
18 #define EXTRA_PAGE_TABLE_PAGES 8
20 #define IA32_PG_P BIT0
21 #define IA32_PG_RW BIT1
22 #define IA32_PG_PS BIT7
25 VOID
*mOriginalHandler
;
26 UINTN mPageFaultBuffer
;
27 UINTN mPageFaultIndex
= 0;
29 // Store the uplink information for each page being used.
31 UINT64
*mPageFaultUplink
[EXTRA_PAGE_TABLE_PAGES
];
39 PageFaultHandlerHook (
44 Hook IDT with our page fault handler so that the on-demand paging works on page fault.
46 @param IdtEntry a pointer to IDT entry
50 HookPageFaultHandler (
51 IN IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
55 UINT8 PhysicalAddressBits
;
56 UINTN PageFaultHandlerHookAddress
;
58 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
59 if (RegEax
>= 0x80000008) {
60 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
61 PhysicalAddressBits
= (UINT8
)RegEax
;
63 PhysicalAddressBits
= 36;
66 mPhyMask
= LShiftU64 (1, PhysicalAddressBits
) - 1;
67 mPhyMask
&= (1ull << 48) - SIZE_4KB
;
70 // Set Page Fault entry to catch >4G access
72 PageFaultHandlerHookAddress
= (UINTN
)PageFaultHandlerHook
;
73 mOriginalHandler
= (VOID
*)(UINTN
)(LShiftU64 (IdtEntry
->Bits
.OffsetUpper
, 32) + IdtEntry
->Bits
.OffsetLow
+ (IdtEntry
->Bits
.OffsetHigh
<< 16));
74 IdtEntry
->Bits
.OffsetLow
= (UINT16
)PageFaultHandlerHookAddress
;
75 IdtEntry
->Bits
.Selector
= (UINT16
)AsmReadCs ();
76 IdtEntry
->Bits
.Reserved_0
= 0;
77 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
78 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(PageFaultHandlerHookAddress
>> 16);
79 IdtEntry
->Bits
.OffsetUpper
= (UINT32
)(PageFaultHandlerHookAddress
>> 32);
80 IdtEntry
->Bits
.Reserved_1
= 0;
83 mPageFaultBuffer
= (UINTN
)(AsmReadCr3 () & mPhyMask
) + EFI_PAGES_TO_SIZE (2);
85 mPageFaultBuffer
= (UINTN
)(AsmReadCr3 () & mPhyMask
) + EFI_PAGES_TO_SIZE (6);
88 ZeroMem (mPageFaultUplink
, sizeof (mPageFaultUplink
));
92 The function will check if current waking vector is long mode.
94 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
96 @retval TRUE Current context need long mode waking vector.
97 @retval FALSE Current context need not long mode waking vector.
100 IsLongModeWakingVector (
101 IN ACPI_S3_CONTEXT
*AcpiS3Context
104 EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*Facs
;
106 Facs
= (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*)((UINTN
)(AcpiS3Context
->AcpiFacsTable
));
107 if ((Facs
== NULL
) ||
108 (Facs
->Signature
!= EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE
) ||
109 ((Facs
->FirmwareWakingVector
== 0) && (Facs
->XFirmwareWakingVector
== 0)))
111 // 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
->OspmFlags
& EFI_ACPI_4_0_OSPM_64BIT_WAKE__F
) != 0))
120 // Both BIOS and OS wants 64bit vector
121 if (sizeof (UINTN
) == sizeof (UINT64
)) {
131 Set a IDT entry for interrupt vector 3 for debug purpose.
133 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
138 IN ACPI_S3_CONTEXT
*AcpiS3Context
141 IA32_IDT_GATE_DESCRIPTOR
*IdtEntry
;
142 IA32_DESCRIPTOR
*IdtDescriptor
;
147 // Restore IDT for debug
149 IdtDescriptor
= (IA32_DESCRIPTOR
*)(UINTN
)(AcpiS3Context
->IdtrProfile
);
150 AsmWriteIdtr (IdtDescriptor
);
153 // Setup the default CPU exception handlers
155 Status
= InitializeCpuExceptionHandlers (NULL
);
156 ASSERT_EFI_ERROR (Status
);
160 // Update IDT entry INT3 if the instruction is valid in it
162 S3DebugBuffer
= (UINTN
)(AcpiS3Context
->S3DebugBufferAddress
);
163 if (*(UINTN
*)S3DebugBuffer
!= (UINTN
)-1) {
164 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*)(IdtDescriptor
->Base
+ (3 * sizeof (IA32_IDT_GATE_DESCRIPTOR
)));
165 IdtEntry
->Bits
.OffsetLow
= (UINT16
)S3DebugBuffer
;
166 IdtEntry
->Bits
.Selector
= (UINT16
)AsmReadCs ();
167 IdtEntry
->Bits
.Reserved_0
= 0;
168 IdtEntry
->Bits
.GateType
= IA32_IDT_GATE_TYPE_INTERRUPT_32
;
169 IdtEntry
->Bits
.OffsetHigh
= (UINT16
)(S3DebugBuffer
>> 16);
170 IdtEntry
->Bits
.OffsetUpper
= (UINT32
)(S3DebugBuffer
>> 32);
171 IdtEntry
->Bits
.Reserved_1
= 0;
177 // If both BIOS and OS wants long mode waking vector,
178 // S3ResumePei should have established 1:1 Virtual to Physical identity mapping page table,
179 // no need to hook page fault handler.
181 if (!IsLongModeWakingVector (AcpiS3Context
)) {
182 IdtEntry
= (IA32_IDT_GATE_DESCRIPTOR
*)(IdtDescriptor
->Base
+ (14 * sizeof (IA32_IDT_GATE_DESCRIPTOR
)));
183 HookPageFaultHandler (IdtEntry
);
188 Acquire page for page fault.
190 @param[in, out] Uplink Pointer to up page table entry.
195 IN OUT UINT64
*Uplink
200 Address
= mPageFaultBuffer
+ EFI_PAGES_TO_SIZE (mPageFaultIndex
);
201 ZeroMem ((VOID
*)Address
, EFI_PAGES_TO_SIZE (1));
204 // Cut the previous uplink if it exists and wasn't overwritten.
206 if ((mPageFaultUplink
[mPageFaultIndex
] != NULL
) &&
207 ((*mPageFaultUplink
[mPageFaultIndex
] & ~mAddressEncMask
& mPhyMask
) == Address
))
209 *mPageFaultUplink
[mPageFaultIndex
] = 0;
213 // Link & Record the current uplink.
215 *Uplink
= Address
| mAddressEncMask
| IA32_PG_P
| IA32_PG_RW
;
216 mPageFaultUplink
[mPageFaultIndex
] = Uplink
;
218 mPageFaultIndex
= (mPageFaultIndex
+ 1) % EXTRA_PAGE_TABLE_PAGES
;
222 The page fault handler that on-demand read >4G memory/MMIO.
224 @retval TRUE The page fault is correctly handled.
225 @retval FALSE The page fault is not handled and is passed through to original handler.
238 PFAddress
= AsmReadCr2 ();
239 DEBUG ((DEBUG_INFO
, "BootScript - PageFaultHandler: Cr2 - %lx\n", PFAddress
));
241 if (PFAddress
>= mPhyMask
+ SIZE_4KB
) {
245 PFAddress
&= mPhyMask
;
247 PageTable
= (UINT64
*)(UINTN
)(AsmReadCr3 () & mPhyMask
);
249 PTIndex
= BitFieldRead64 (PFAddress
, 39, 47);
251 if ((PageTable
[PTIndex
] & IA32_PG_P
) == 0) {
252 AcquirePage (&PageTable
[PTIndex
]);
255 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & ~mAddressEncMask
& mPhyMask
);
256 PTIndex
= BitFieldRead64 (PFAddress
, 30, 38);
258 if (mPage1GSupport
) {
259 PageTable
[PTIndex
] = ((PFAddress
| mAddressEncMask
) & ~((1ull << 30) - 1)) | IA32_PG_P
| IA32_PG_RW
| IA32_PG_PS
;
261 if ((PageTable
[PTIndex
] & IA32_PG_P
) == 0) {
262 AcquirePage (&PageTable
[PTIndex
]);
265 PageTable
= (UINT64
*)(UINTN
)(PageTable
[PTIndex
] & ~mAddressEncMask
& mPhyMask
);
266 PTIndex
= BitFieldRead64 (PFAddress
, 21, 29);
268 PageTable
[PTIndex
] = ((PFAddress
| mAddressEncMask
) & ~((1ull << 21) - 1)) | IA32_PG_P
| IA32_PG_RW
| IA32_PG_PS
;