]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | IA32/x64 generic functions to support Debug Support protocol.\r | |
3 | \r | |
4 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | \r | |
7 | **/\r | |
8 | \r | |
9 | #include "DebugSupport.h"\r | |
10 | \r | |
11 | //\r | |
12 | // This the global main table to keep track of the interrupts\r | |
13 | //\r | |
14 | IDT_ENTRY *IdtEntryTable = NULL;\r | |
15 | \r | |
16 | /**\r | |
17 | Read IDT Gate Descriptor from IDT Table.\r | |
18 | \r | |
19 | @param Vector Specifies vector number.\r | |
20 | @param IdtGateDescriptor Pointer to IDT Gate Descriptor read from IDT Table.\r | |
21 | \r | |
22 | **/\r | |
23 | VOID\r | |
24 | ReadIdtGateDescriptor (\r | |
25 | IN EFI_EXCEPTION_TYPE Vector,\r | |
26 | OUT IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor\r | |
27 | )\r | |
28 | {\r | |
29 | IA32_DESCRIPTOR IdtrValue;\r | |
30 | IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r | |
31 | \r | |
32 | AsmReadIdtr (&IdtrValue);\r | |
33 | IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtrValue.Base;\r | |
34 | \r | |
35 | CopyMem ((VOID *)IdtGateDescriptor, (VOID *)&(IdtTable)[Vector], sizeof (IA32_IDT_GATE_DESCRIPTOR));\r | |
36 | }\r | |
37 | \r | |
38 | /**\r | |
39 | Write IDT Gate Descriptor into IDT Table.\r | |
40 | \r | |
41 | @param Vector Specifies vector number.\r | |
42 | @param IdtGateDescriptor Pointer to IDT Gate Descriptor written into IDT Table.\r | |
43 | \r | |
44 | **/\r | |
45 | VOID\r | |
46 | WriteIdtGateDescriptor (\r | |
47 | EFI_EXCEPTION_TYPE Vector,\r | |
48 | IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor\r | |
49 | )\r | |
50 | {\r | |
51 | IA32_DESCRIPTOR IdtrValue;\r | |
52 | IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r | |
53 | \r | |
54 | AsmReadIdtr (&IdtrValue);\r | |
55 | IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtrValue.Base;\r | |
56 | \r | |
57 | CopyMem ((VOID *)&(IdtTable)[Vector], (VOID *)IdtGateDescriptor, sizeof (IA32_IDT_GATE_DESCRIPTOR));\r | |
58 | }\r | |
59 | \r | |
60 | /**\r | |
61 | Creates a nes entry stub. Then saves the current IDT entry and replaces it\r | |
62 | with an interrupt gate for the new entry point. The IdtEntryTable is updated\r | |
63 | with the new registered function.\r | |
64 | \r | |
65 | This code executes in boot services context. The stub entry executes in interrupt\r | |
66 | context.\r | |
67 | \r | |
68 | @param ExceptionType Specifies which vector to hook.\r | |
69 | @param NewCallback A pointer to the new function to be registered.\r | |
70 | \r | |
71 | **/\r | |
72 | VOID\r | |
73 | HookEntry (\r | |
74 | IN EFI_EXCEPTION_TYPE ExceptionType,\r | |
75 | IN CALLBACK_FUNC NewCallback\r | |
76 | )\r | |
77 | {\r | |
78 | BOOLEAN OldIntFlagState;\r | |
79 | \r | |
80 | CreateEntryStub (ExceptionType, (VOID **)&IdtEntryTable[ExceptionType].StubEntry);\r | |
81 | \r | |
82 | //\r | |
83 | // Disables CPU interrupts and returns the previous interrupt state\r | |
84 | //\r | |
85 | OldIntFlagState = SaveAndDisableInterrupts ();\r | |
86 | \r | |
87 | //\r | |
88 | // gets IDT Gate descriptor by index\r | |
89 | //\r | |
90 | ReadIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r | |
91 | //\r | |
92 | // stores orignal interrupt handle\r | |
93 | //\r | |
94 | IdtEntryTable[ExceptionType].OrigVector = (DEBUG_PROC)GetInterruptHandleFromIdt (&(IdtEntryTable[ExceptionType].OrigDesc));\r | |
95 | \r | |
96 | //\r | |
97 | // encodes new IDT Gate descriptor by stub entry\r | |
98 | //\r | |
99 | Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);\r | |
100 | //\r | |
101 | // stores NewCallback\r | |
102 | //\r | |
103 | IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;\r | |
104 | \r | |
105 | //\r | |
106 | // writes back new IDT Gate descriptor\r | |
107 | //\r | |
108 | WriteIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));\r | |
109 | \r | |
110 | //\r | |
111 | // restore interrupt state\r | |
112 | //\r | |
113 | SetInterruptState (OldIntFlagState);\r | |
114 | \r | |
115 | return;\r | |
116 | }\r | |
117 | \r | |
118 | /**\r | |
119 | Undoes HookEntry. This code executes in boot services context.\r | |
120 | \r | |
121 | @param ExceptionType Specifies which entry to unhook\r | |
122 | \r | |
123 | **/\r | |
124 | VOID\r | |
125 | UnhookEntry (\r | |
126 | IN EFI_EXCEPTION_TYPE ExceptionType\r | |
127 | )\r | |
128 | {\r | |
129 | BOOLEAN OldIntFlagState;\r | |
130 | \r | |
131 | //\r | |
132 | // Disables CPU interrupts and returns the previous interrupt state\r | |
133 | //\r | |
134 | OldIntFlagState = SaveAndDisableInterrupts ();\r | |
135 | \r | |
136 | //\r | |
137 | // restore the default IDT Date Descriptor\r | |
138 | //\r | |
139 | WriteIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));\r | |
140 | \r | |
141 | //\r | |
142 | // restore interrupt state\r | |
143 | //\r | |
144 | SetInterruptState (OldIntFlagState);\r | |
145 | \r | |
146 | return;\r | |
147 | }\r | |
148 | \r | |
149 | /**\r | |
150 | Returns the maximum value that may be used for the ProcessorIndex parameter in\r | |
151 | RegisterPeriodicCallback() and RegisterExceptionCallback().\r | |
152 | \r | |
153 | Hard coded to support only 1 processor for now.\r | |
154 | \r | |
155 | @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.\r | |
156 | @param MaxProcessorIndex Pointer to a caller-allocated UINTN in which the maximum supported\r | |
157 | processor index is returned. Always 0 returned.\r | |
158 | \r | |
159 | @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.\r | |
160 | \r | |
161 | **/\r | |
162 | EFI_STATUS\r | |
163 | EFIAPI\r | |
164 | GetMaximumProcessorIndex (\r | |
165 | IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r | |
166 | OUT UINTN *MaxProcessorIndex\r | |
167 | )\r | |
168 | {\r | |
169 | *MaxProcessorIndex = 0;\r | |
170 | return EFI_SUCCESS;\r | |
171 | }\r | |
172 | \r | |
173 | /**\r | |
174 | Registers a function to be called back periodically in interrupt context.\r | |
175 | \r | |
176 | @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.\r | |
177 | @param ProcessorIndex Specifies which processor the callback function applies to.\r | |
178 | @param PeriodicCallback A pointer to a function of type PERIODIC_CALLBACK that is the main\r | |
179 | periodic entry point of the debug agent.\r | |
180 | \r | |
181 | @retval EFI_SUCCESS The function completed successfully.\r | |
182 | @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback\r | |
183 | function was previously registered.\r | |
184 | @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback\r | |
185 | function.\r | |
186 | **/\r | |
187 | EFI_STATUS\r | |
188 | EFIAPI\r | |
189 | RegisterPeriodicCallback (\r | |
190 | IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r | |
191 | IN UINTN ProcessorIndex,\r | |
192 | IN EFI_PERIODIC_CALLBACK PeriodicCallback\r | |
193 | )\r | |
194 | {\r | |
195 | return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);\r | |
196 | }\r | |
197 | \r | |
198 | /**\r | |
199 | Registers a function to be called when a given processor exception occurs.\r | |
200 | \r | |
201 | This code executes in boot services context.\r | |
202 | \r | |
203 | @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.\r | |
204 | @param ProcessorIndex Specifies which processor the callback function applies to.\r | |
205 | @param ExceptionCallback A pointer to a function of type EXCEPTION_CALLBACK that is called\r | |
206 | when the processor exception specified by ExceptionType occurs.\r | |
207 | @param ExceptionType Specifies which processor exception to hook.\r | |
208 | \r | |
209 | @retval EFI_SUCCESS The function completed successfully.\r | |
210 | @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback\r | |
211 | function was previously registered.\r | |
212 | @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback\r | |
213 | function.\r | |
214 | **/\r | |
215 | EFI_STATUS\r | |
216 | EFIAPI\r | |
217 | RegisterExceptionCallback (\r | |
218 | IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r | |
219 | IN UINTN ProcessorIndex,\r | |
220 | IN EFI_EXCEPTION_CALLBACK ExceptionCallback,\r | |
221 | IN EFI_EXCEPTION_TYPE ExceptionType\r | |
222 | )\r | |
223 | {\r | |
224 | return ManageIdtEntryTable (ExceptionCallback, ExceptionType);\r | |
225 | }\r | |
226 | \r | |
227 | /**\r | |
228 | Invalidates processor instruction cache for a memory range. Subsequent execution in this range\r | |
229 | causes a fresh memory fetch to retrieve code to be executed.\r | |
230 | \r | |
231 | @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.\r | |
232 | @param ProcessorIndex Specifies which processor's instruction cache is to be invalidated.\r | |
233 | @param Start Specifies the physical base of the memory range to be invalidated.\r | |
234 | @param Length Specifies the minimum number of bytes in the processor's instruction\r | |
235 | cache to invalidate.\r | |
236 | \r | |
237 | @retval EFI_SUCCESS Always returned.\r | |
238 | \r | |
239 | **/\r | |
240 | EFI_STATUS\r | |
241 | EFIAPI\r | |
242 | InvalidateInstructionCache (\r | |
243 | IN EFI_DEBUG_SUPPORT_PROTOCOL *This,\r | |
244 | IN UINTN ProcessorIndex,\r | |
245 | IN VOID *Start,\r | |
246 | IN UINT64 Length\r | |
247 | )\r | |
248 | {\r | |
249 | AsmWbinvd ();\r | |
250 | return EFI_SUCCESS;\r | |
251 | }\r | |
252 | \r | |
253 | /**\r | |
254 | Common piece of code that invokes the registered handlers.\r | |
255 | \r | |
256 | This code executes in exception context so no efi calls are allowed.\r | |
257 | This code is called from assembly file.\r | |
258 | \r | |
259 | @param ExceptionType Exception type\r | |
260 | @param ContextRecord System context\r | |
261 | \r | |
262 | **/\r | |
263 | VOID\r | |
264 | InterruptDistrubutionHub (\r | |
265 | EFI_EXCEPTION_TYPE ExceptionType,\r | |
266 | EFI_SYSTEM_CONTEXT_IA32 *ContextRecord\r | |
267 | )\r | |
268 | {\r | |
269 | if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {\r | |
270 | if (ExceptionType != SYSTEM_TIMER_VECTOR) {\r | |
271 | IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);\r | |
272 | } else {\r | |
273 | OrigVector = IdtEntryTable[ExceptionType].OrigVector;\r | |
274 | IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);\r | |
275 | }\r | |
276 | }\r | |
277 | }\r | |
278 | \r | |
279 | /**\r | |
280 | This is the callback that is written to the Loaded Image protocol instance\r | |
281 | on the image handle. It uninstalls all registered handlers and frees all entry\r | |
282 | stub memory.\r | |
283 | \r | |
284 | @param ImageHandle The firmware allocated handle for the EFI image.\r | |
285 | \r | |
286 | @retval EFI_SUCCESS Always.\r | |
287 | \r | |
288 | **/\r | |
289 | EFI_STATUS\r | |
290 | EFIAPI\r | |
291 | PlUnloadDebugSupportDriver (\r | |
292 | IN EFI_HANDLE ImageHandle\r | |
293 | )\r | |
294 | {\r | |
295 | EFI_EXCEPTION_TYPE ExceptionType;\r | |
296 | \r | |
297 | for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r | |
298 | ManageIdtEntryTable (NULL, ExceptionType);\r | |
299 | //\r | |
300 | // Free space for each Interrupt Stub precedure.\r | |
301 | //\r | |
302 | if (IdtEntryTable[ExceptionType].StubEntry != NULL) {\r | |
303 | FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);\r | |
304 | }\r | |
305 | }\r | |
306 | \r | |
307 | FreePool (IdtEntryTable);\r | |
308 | \r | |
309 | return EFI_SUCCESS;\r | |
310 | }\r | |
311 | \r | |
312 | /**\r | |
313 | Initializes driver's handler registration database.\r | |
314 | \r | |
315 | This code executes in boot services context.\r | |
316 | Must be public because it's referenced from DebugSupport.c\r | |
317 | \r | |
318 | @retval EFI_UNSUPPORTED If IA32/x64 processor does not support FXSTOR/FXRSTOR instructions,\r | |
319 | the context save will fail, so these processors are not supported.\r | |
320 | @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.\r | |
321 | @retval EFI_SUCCESS Initializes successfully.\r | |
322 | \r | |
323 | **/\r | |
324 | EFI_STATUS\r | |
325 | PlInitializeDebugSupportDriver (\r | |
326 | VOID\r | |
327 | )\r | |
328 | {\r | |
329 | EFI_EXCEPTION_TYPE ExceptionType;\r | |
330 | \r | |
331 | //\r | |
332 | // Check whether FxStor instructions are supported.\r | |
333 | //\r | |
334 | if (!FxStorSupport ()) {\r | |
335 | return EFI_UNSUPPORTED;\r | |
336 | }\r | |
337 | \r | |
338 | IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);\r | |
339 | if (IdtEntryTable == NULL) {\r | |
340 | return EFI_OUT_OF_RESOURCES;\r | |
341 | }\r | |
342 | \r | |
343 | for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r | |
344 | IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC)(UINTN)AllocatePool (StubSize);\r | |
345 | if (IdtEntryTable[ExceptionType].StubEntry == NULL) {\r | |
346 | goto ErrorCleanup;\r | |
347 | }\r | |
348 | \r | |
349 | //\r | |
350 | // Copy Interrupt stub code.\r | |
351 | //\r | |
352 | CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);\r | |
353 | }\r | |
354 | \r | |
355 | return EFI_SUCCESS;\r | |
356 | \r | |
357 | ErrorCleanup:\r | |
358 | \r | |
359 | for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {\r | |
360 | if (IdtEntryTable[ExceptionType].StubEntry != NULL) {\r | |
361 | FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);\r | |
362 | }\r | |
363 | }\r | |
364 | \r | |
365 | FreePool (IdtEntryTable);\r | |
366 | \r | |
367 | return EFI_OUT_OF_RESOURCES;\r | |
368 | }\r |