]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/X64/SetIdtEntry.c
1. DxeIplPeim will locate Vector Handoff Table PPI and build GUIDed HOB if it has.
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / BootScriptExecutorDxe / X64 / SetIdtEntry.c
CommitLineData
be46cd5f 1/** @file\r
2 Set a IDT entry for debug purpose\r
3\r
4 Set a IDT entry for interrupt vector 3 for debug purpose for x64 platform\r
5\r
73f0127f 6Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
be46cd5f 7\r
8This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17#include "ScriptExecute.h"\r
d0bf5623
JY
18\r
19#define IA32_PG_P BIT0\r
20#define IA32_PG_RW BIT1\r
21#define IA32_PG_PS BIT7\r
22\r
23UINT64 mPhyMask;\r
24BOOLEAN mPage1GSupport;\r
25VOID *mOriginalHandler;\r
26UINTN mS3NvsPageTableAddress;\r
27\r
54e27ada
JY
28/**\r
29 Page fault handler.\r
30\r
31**/\r
d0bf5623
JY
32VOID\r
33EFIAPI\r
34PageFaultHandlerHook (\r
35 VOID\r
36 );\r
37\r
54e27ada
JY
38/**\r
39 Hook IDT with our page fault handler so that the on-demand paging works on page fault.\r
40\r
41 @param IdtEntry a pointer to IDT entry\r
42\r
43**/\r
d0bf5623
JY
44VOID\r
45HookPageFaultHandler (\r
68cc1ba3 46 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry\r
d0bf5623
JY
47 )\r
48{\r
49 UINT32 RegEax;\r
50 UINT32 RegEdx;\r
73f0127f 51 UINTN PageFaultHandlerHookAddress;\r
d0bf5623
JY
52\r
53 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
54 mPhyMask = LShiftU64 (1, (UINT8)RegEax) - 1;\r
55 mPhyMask &= (1ull << 48) - SIZE_4KB;\r
56\r
57 mPage1GSupport = FALSE;\r
58 if (PcdGetBool(PcdUse1GPageTable)) {\r
59 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
60 if (RegEax >= 0x80000001) {\r
61 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
62 if ((RegEdx & BIT26) != 0) {\r
63 mPage1GSupport = TRUE;\r
64 }\r
65 }\r
66 }\r
67\r
68 //\r
69 // Set Page Fault entry to catch >4G access\r
70 //\r
73f0127f 71 PageFaultHandlerHookAddress = (UINTN)PageFaultHandlerHook;\r
68cc1ba3 72 mOriginalHandler = (VOID *)(UINTN)(LShiftU64 (IdtEntry->Bits.OffsetUpper, 32) + IdtEntry->Bits.OffsetLow + (IdtEntry->Bits.OffsetHigh << 16));\r
73f0127f 73 IdtEntry->Bits.OffsetLow = (UINT16)PageFaultHandlerHookAddress;\r
68cc1ba3
SZ
74 IdtEntry->Bits.Selector = (UINT16)AsmReadCs ();\r
75 IdtEntry->Bits.Reserved_0 = 0;\r
76 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;\r
73f0127f
SZ
77 IdtEntry->Bits.OffsetHigh = (UINT16)(PageFaultHandlerHookAddress >> 16);\r
78 IdtEntry->Bits.OffsetUpper = (UINT32)(PageFaultHandlerHookAddress >> 32);\r
68cc1ba3 79 IdtEntry->Bits.Reserved_1 = 0;\r
d0bf5623
JY
80\r
81 if (mPage1GSupport) {\r
82 mS3NvsPageTableAddress = (UINTN)(AsmReadCr3 () & mPhyMask) + EFI_PAGES_TO_SIZE(2);\r
83 }else {\r
84 mS3NvsPageTableAddress = (UINTN)(AsmReadCr3 () & mPhyMask) + EFI_PAGES_TO_SIZE(6);\r
85 }\r
86}\r
87\r
be46cd5f 88/**\r
89 Set a IDT entry for interrupt vector 3 for debug purpose.\r
90\r
91 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT\r
92\r
93**/\r
94VOID\r
95SetIdtEntry (\r
96 IN ACPI_S3_CONTEXT *AcpiS3Context\r
97 )\r
98{\r
68cc1ba3 99 IA32_IDT_GATE_DESCRIPTOR *IdtEntry;\r
be46cd5f 100 IA32_DESCRIPTOR *IdtDescriptor;\r
101 UINTN S3DebugBuffer;\r
57f360f2 102 EFI_STATUS Status;\r
be46cd5f 103\r
104 //\r
105 // Restore IDT for debug\r
106 //\r
107 IdtDescriptor = (IA32_DESCRIPTOR *) (UINTN) (AcpiS3Context->IdtrProfile);\r
1e172d6b 108 AsmWriteIdtr (IdtDescriptor);\r
109\r
110 //\r
111 // Setup the default CPU exception handlers\r
112 //\r
57f360f2
JF
113 Status = InitializeCpuExceptionHandlers (NULL);\r
114 ASSERT_EFI_ERROR (Status);\r
1e172d6b 115\r
f4a25e81 116 DEBUG_CODE (\r
117 //\r
118 // Update IDT entry INT3 if the instruction is valid in it\r
119 //\r
120 S3DebugBuffer = (UINTN) (AcpiS3Context->S3DebugBufferAddress);\r
121 if (*(UINTN *)S3DebugBuffer != (UINTN) -1) {\r
68cc1ba3
SZ
122 IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (3 * sizeof (IA32_IDT_GATE_DESCRIPTOR)));\r
123 IdtEntry->Bits.OffsetLow = (UINT16)S3DebugBuffer;\r
124 IdtEntry->Bits.Selector = (UINT16)AsmReadCs ();\r
125 IdtEntry->Bits.Reserved_0 = 0;\r
126 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;\r
127 IdtEntry->Bits.OffsetHigh = (UINT16)(S3DebugBuffer >> 16);\r
128 IdtEntry->Bits.OffsetUpper = (UINT32)(S3DebugBuffer >> 32);\r
129 IdtEntry->Bits.Reserved_1 = 0;\r
f4a25e81 130 }\r
131 );\r
be46cd5f 132\r
68cc1ba3 133 IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (14 * sizeof (IA32_IDT_GATE_DESCRIPTOR)));\r
d0bf5623 134 HookPageFaultHandler (IdtEntry);\r
d0bf5623
JY
135}\r
136\r
54e27ada
JY
137/**\r
138 Get new page address.\r
139\r
140 @param PageNum new page number needed\r
141\r
142 @return new page address\r
143**/\r
d0bf5623
JY
144UINTN\r
145GetNewPage (\r
146 IN UINTN PageNum\r
147 )\r
148{\r
149 UINTN NewPage;\r
150 NewPage = mS3NvsPageTableAddress;\r
151 ZeroMem ((VOID *)NewPage, EFI_PAGES_TO_SIZE(PageNum));\r
152 mS3NvsPageTableAddress += EFI_PAGES_TO_SIZE(PageNum);\r
153 return NewPage;\r
be46cd5f 154}\r
155\r
54e27ada
JY
156/**\r
157 The page fault handler that on-demand read >4G memory/MMIO.\r
158 \r
159 @retval TRUE The page fault is correctly handled.\r
160 @retval FALSE The page fault is not handled and is passed through to original handler.\r
161\r
162**/\r
d0bf5623
JY
163BOOLEAN\r
164EFIAPI\r
165PageFaultHandler (\r
166 VOID\r
167 )\r
168{\r
169 UINT64 *PageTable;\r
170 UINT64 PFAddress;\r
171 UINTN PTIndex;\r
172\r
173 PFAddress = AsmReadCr2 ();\r
174 DEBUG ((EFI_D_ERROR, "BootScript - PageFaultHandler: Cr2 - %lx\n", PFAddress));\r
175\r
176 if (PFAddress >= mPhyMask + SIZE_4KB) {\r
177 return FALSE;\r
178 }\r
179 PFAddress &= mPhyMask;\r
180\r
181 PageTable = (UINT64*)(UINTN)(AsmReadCr3 () & mPhyMask);\r
182\r
183 PTIndex = BitFieldRead64 (PFAddress, 39, 47);\r
184 // PML4E\r
185 if ((PageTable[PTIndex] & IA32_PG_P) == 0) {\r
186 PageTable[PTIndex] = GetNewPage (1) | IA32_PG_P | IA32_PG_RW;\r
187 }\r
188 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & mPhyMask);\r
189 PTIndex = BitFieldRead64 (PFAddress, 30, 38);\r
190 // PDPTE\r
191 if (mPage1GSupport) {\r
192 PageTable[PTIndex] = PFAddress | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;\r
193 } else {\r
194 if ((PageTable[PTIndex] & IA32_PG_P) == 0) {\r
195 PageTable[PTIndex] = GetNewPage (1) | IA32_PG_P | IA32_PG_RW;\r
196 }\r
197 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & mPhyMask);\r
198 PTIndex = BitFieldRead64 (PFAddress, 21, 29);\r
199 // PD\r
200 PageTable[PTIndex] = PFAddress | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;\r
201 }\r
202\r
203 return TRUE;\r
204}\r