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