]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/X64/SetIdtEntry.c
Create 4G page table by default, and using PF to handle >4G MMIO access, to improve...
[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
1e172d6b 6Copyright (c) 2006 - 2012, 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
18//\r
19// INTERRUPT_GATE_DESCRIPTOR and SetIdtEntry () are used to setup IDT to do debug\r
20//\r
21\r
22#pragma pack(1)\r
23\r
24typedef struct {\r
25 UINT16 Offset15To0;\r
26 UINT16 SegmentSelector;\r
27 UINT16 Attributes;\r
28 UINT16 Offset31To16;\r
29 UINT32 Offset63To32;\r
30 UINT32 Reserved;\r
31} INTERRUPT_GATE_DESCRIPTOR;\r
32\r
33#define INTERRUPT_GATE_ATTRIBUTE 0x8e00\r
34\r
35#pragma pack()\r
d0bf5623
JY
36\r
37#define IA32_PG_P BIT0\r
38#define IA32_PG_RW BIT1\r
39#define IA32_PG_PS BIT7\r
40\r
41UINT64 mPhyMask;\r
42BOOLEAN mPage1GSupport;\r
43VOID *mOriginalHandler;\r
44UINTN mS3NvsPageTableAddress;\r
45\r
46VOID\r
47EFIAPI\r
48PageFaultHandlerHook (\r
49 VOID\r
50 );\r
51\r
52VOID\r
53HookPageFaultHandler (\r
54 IN INTERRUPT_GATE_DESCRIPTOR *IdtEntry\r
55 )\r
56{\r
57 UINT32 RegEax;\r
58 UINT32 RegEdx;\r
59\r
60 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
61 mPhyMask = LShiftU64 (1, (UINT8)RegEax) - 1;\r
62 mPhyMask &= (1ull << 48) - SIZE_4KB;\r
63\r
64 mPage1GSupport = FALSE;\r
65 if (PcdGetBool(PcdUse1GPageTable)) {\r
66 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
67 if (RegEax >= 0x80000001) {\r
68 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
69 if ((RegEdx & BIT26) != 0) {\r
70 mPage1GSupport = TRUE;\r
71 }\r
72 }\r
73 }\r
74\r
75 //\r
76 // Set Page Fault entry to catch >4G access\r
77 //\r
78 mOriginalHandler = (VOID *)(UINTN)(LShiftU64 (IdtEntry->Offset63To32, 32) + IdtEntry->Offset15To0 + (IdtEntry->Offset31To16 << 16));\r
79 IdtEntry->Offset15To0 = (UINT16)((UINTN)PageFaultHandlerHook);\r
80 IdtEntry->SegmentSelector = (UINT16)AsmReadCs ();\r
81 IdtEntry->Attributes = (UINT16)INTERRUPT_GATE_ATTRIBUTE;\r
82 IdtEntry->Offset31To16 = (UINT16)((UINTN)PageFaultHandlerHook >> 16);\r
83 IdtEntry->Offset63To32 = (UINT32)((UINTN)PageFaultHandlerHook >> 32);\r
84 IdtEntry->Reserved = 0;\r
85\r
86 if (mPage1GSupport) {\r
87 mS3NvsPageTableAddress = (UINTN)(AsmReadCr3 () & mPhyMask) + EFI_PAGES_TO_SIZE(2);\r
88 }else {\r
89 mS3NvsPageTableAddress = (UINTN)(AsmReadCr3 () & mPhyMask) + EFI_PAGES_TO_SIZE(6);\r
90 }\r
91}\r
92\r
be46cd5f 93/**\r
94 Set a IDT entry for interrupt vector 3 for debug purpose.\r
95\r
96 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT\r
97\r
98**/\r
99VOID\r
100SetIdtEntry (\r
101 IN ACPI_S3_CONTEXT *AcpiS3Context\r
102 )\r
103{\r
104 INTERRUPT_GATE_DESCRIPTOR *IdtEntry;\r
105 IA32_DESCRIPTOR *IdtDescriptor;\r
106 UINTN S3DebugBuffer;\r
107\r
108 //\r
109 // Restore IDT for debug\r
110 //\r
111 IdtDescriptor = (IA32_DESCRIPTOR *) (UINTN) (AcpiS3Context->IdtrProfile);\r
1e172d6b 112 AsmWriteIdtr (IdtDescriptor);\r
113\r
114 //\r
115 // Setup the default CPU exception handlers\r
116 //\r
117 SetupCpuExceptionHandlers ();\r
118\r
119 //\r
120 // Update IDT entry INT3\r
121 //\r
be46cd5f 122 IdtEntry = (INTERRUPT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (3 * sizeof (INTERRUPT_GATE_DESCRIPTOR)));\r
123 S3DebugBuffer = (UINTN) (AcpiS3Context->S3DebugBufferAddress);\r
124\r
125 IdtEntry->Offset15To0 = (UINT16)S3DebugBuffer;\r
d0bf5623 126 IdtEntry->SegmentSelector = (UINT16)AsmReadCs ();\r
be46cd5f 127 IdtEntry->Attributes = (UINT16)INTERRUPT_GATE_ATTRIBUTE;\r
128 IdtEntry->Offset31To16 = (UINT16)(S3DebugBuffer >> 16);\r
129 IdtEntry->Offset63To32 = (UINT32)(S3DebugBuffer >> 32);\r
130 IdtEntry->Reserved = 0;\r
131\r
d0bf5623
JY
132 IdtEntry = (INTERRUPT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (14 * sizeof (INTERRUPT_GATE_DESCRIPTOR)));\r
133 HookPageFaultHandler (IdtEntry);\r
134\r
135 AsmWriteIdtr (IdtDescriptor);\r
136}\r
137\r
138UINTN\r
139GetNewPage (\r
140 IN UINTN PageNum\r
141 )\r
142{\r
143 UINTN NewPage;\r
144 NewPage = mS3NvsPageTableAddress;\r
145 ZeroMem ((VOID *)NewPage, EFI_PAGES_TO_SIZE(PageNum));\r
146 mS3NvsPageTableAddress += EFI_PAGES_TO_SIZE(PageNum);\r
147 return NewPage;\r
be46cd5f 148}\r
149\r
d0bf5623
JY
150BOOLEAN\r
151EFIAPI\r
152PageFaultHandler (\r
153 VOID\r
154 )\r
155{\r
156 UINT64 *PageTable;\r
157 UINT64 PFAddress;\r
158 UINTN PTIndex;\r
159\r
160 PFAddress = AsmReadCr2 ();\r
161 DEBUG ((EFI_D_ERROR, "BootScript - PageFaultHandler: Cr2 - %lx\n", PFAddress));\r
162\r
163 if (PFAddress >= mPhyMask + SIZE_4KB) {\r
164 return FALSE;\r
165 }\r
166 PFAddress &= mPhyMask;\r
167\r
168 PageTable = (UINT64*)(UINTN)(AsmReadCr3 () & mPhyMask);\r
169\r
170 PTIndex = BitFieldRead64 (PFAddress, 39, 47);\r
171 // PML4E\r
172 if ((PageTable[PTIndex] & IA32_PG_P) == 0) {\r
173 PageTable[PTIndex] = GetNewPage (1) | IA32_PG_P | IA32_PG_RW;\r
174 }\r
175 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & mPhyMask);\r
176 PTIndex = BitFieldRead64 (PFAddress, 30, 38);\r
177 // PDPTE\r
178 if (mPage1GSupport) {\r
179 PageTable[PTIndex] = PFAddress | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;\r
180 } else {\r
181 if ((PageTable[PTIndex] & IA32_PG_P) == 0) {\r
182 PageTable[PTIndex] = GetNewPage (1) | IA32_PG_P | IA32_PG_RW;\r
183 }\r
184 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & mPhyMask);\r
185 PTIndex = BitFieldRead64 (PFAddress, 21, 29);\r
186 // PD\r
187 PageTable[PTIndex] = PFAddress | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;\r
188 }\r
189\r
190 return TRUE;\r
191}\r