270a158b7972e221e63723ecc67128a6e16c38ed
[mirror_edk2.git] / EdkModulePkg / Universal / DebugSupport / Dxe / Ia32 / plDebugSupport.c
1 /**@file
2 IA32 specific debug support functions
3
4 Copyright (c) 2006 Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 //
16 // private header files
17 //
18 #include "plDebugSupport.h"
19
20 //
21 // This the global main table to keep track of the interrupts
22 //
23 IDT_ENTRY *IdtEntryTable = NULL;
24 DESCRIPTOR NullDesc = 0;
25
26 #ifndef EFI_NT_EMULATOR
27 STATIC
28 EFI_STATUS
29 CreateEntryStub (
30 IN EFI_EXCEPTION_TYPE ExceptionType,
31 OUT VOID **Stub
32 )
33 /*++
34
35 Routine Description: Allocate pool for a new IDT entry stub. Copy the generic
36 stub into the new buffer and fixup the vector number and jump target address.
37
38 Arguments:
39 ExceptionType - This is the exception type that the new stub will be created
40 for.
41 Stub - On successful exit, *Stub contains the newly allocated entry stub.
42 Returns:
43 Typically EFI_SUCCESS
44 other possibilities are passed through from AllocatePool
45
46 --*/
47 {
48 UINT8 *StubCopy;
49
50 //
51 // First, allocate a new buffer and copy the stub code into it
52 //
53 *Stub = AllocatePool (StubSize);
54 if (*Stub != NULL) {
55 StubCopy = *Stub;
56 CopyMem (StubCopy, InterruptEntryStub, StubSize);
57
58 //
59 // Next fixup the stub code for this vector
60 //
61
62 // The stub code looks like this:
63 //
64 // 00000000 89 25 00000004 R mov AppEsp, esp ; save stack top
65 // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack
66 // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed
67 // 0000000D E9 db 0e9h ; jump rel32
68 // 0000000E 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
69 //
70
71 //
72 // poke in the exception type so the second push pushes the exception type
73 //
74 StubCopy[0x0c] = (UINT8) ExceptionType;
75
76 //
77 // fixup the jump target to point to the common entry
78 //
79 *(UINT32 *) &StubCopy[0x0e] = (UINT32) CommonIdtEntry - (UINT32) &StubCopy[StubSize];
80
81 return EFI_SUCCESS;
82 }
83
84 return EFI_OUT_OF_RESOURCES;
85 }
86
87 STATIC
88 EFI_STATUS
89 HookEntry (
90 IN EFI_EXCEPTION_TYPE ExceptionType,
91 IN VOID (*NewCallback) ()
92 )
93 /*++
94
95 Routine Description:
96 Creates a nes entry stub. Then saves the current IDT entry and replaces it
97 with an interrupt gate for the new entry point. The IdtEntryTable is updated
98 with the new registered function.
99
100 This code executes in boot services context. The stub entry executes in interrupt
101 context.
102
103 Arguments:
104 ExceptionType - specifies which vector to hook.
105 NewCallback - a pointer to the new function to be registered.
106
107 Returns:
108 EFI_SUCCESS
109 Other possibilities are passed through by CreateEntryStub
110
111 --*/
112 {
113 BOOLEAN OldIntFlagState;
114 EFI_STATUS Status;
115
116 Status = CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);
117 if (Status == EFI_SUCCESS) {
118 OldIntFlagState = WriteInterruptFlag (0);
119 ReadIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
120
121 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[0] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[0];
122 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[3];
123
124 Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);
125 IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;
126 WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));
127 WriteInterruptFlag (OldIntFlagState);
128 }
129
130 return Status;
131 }
132
133 STATIC
134 EFI_STATUS
135 UnhookEntry (
136 IN EFI_EXCEPTION_TYPE ExceptionType
137 )
138 /*++
139
140 Routine Description:
141 Undoes HookEntry. This code executes in boot services context.
142
143 Arguments:
144 ExceptionType - specifies which entry to unhook
145
146 Returns:
147 EFI_SUCCESS
148
149 --*/
150 {
151 BOOLEAN OldIntFlagState;
152
153 OldIntFlagState = WriteInterruptFlag (0);
154 WriteIdt (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
155 FreePool ((VOID *) (UINTN) IdtEntryTable[ExceptionType].StubEntry);
156 ZeroMem (&IdtEntryTable[ExceptionType], sizeof (IDT_ENTRY));
157 WriteInterruptFlag (OldIntFlagState);
158
159 return EFI_SUCCESS;
160 }
161 #endif
162
163 EFI_STATUS
164 ManageIdtEntryTable (
165 VOID (*NewCallback)(),
166 EFI_EXCEPTION_TYPE ExceptionType
167 )
168 /*++
169
170 Routine Description:
171 This is the main worker function that manages the state of the interrupt
172 handlers. It both installs and uninstalls interrupt handlers based on the
173 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
174 If NewCallback is non-NULL, then install is indicated.
175
176 Arguments:
177 NewCallback - If non-NULL, NewCallback specifies the new handler to register.
178 If NULL, specifies that the previously registered handler should
179 be uninstalled.
180 ExceptionType - Indicates which entry to manage
181
182 Returns:
183 EFI_SUCCESS
184 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has
185 no handler registered for it
186 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.
187
188 Other possible return values are passed through from UnHookEntry and HookEntry.
189
190 --*/
191 {
192 EFI_STATUS Status;
193
194 Status = EFI_SUCCESS;
195
196 #ifndef EFI_NT_EMULATOR
197 if (CompareDescriptor (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {
198 //
199 // we've already installed to this vector
200 //
201 if (NewCallback != NULL) {
202 //
203 // if the input handler is non-null, error
204 //
205 Status = EFI_ALREADY_STARTED;
206 } else {
207 Status = UnhookEntry (ExceptionType);
208 }
209 } else {
210 //
211 // no user handler installed on this vector
212 //
213 if (NewCallback == NULL) {
214 //
215 // if the input handler is null, error
216 //
217 Status = EFI_INVALID_PARAMETER;
218 } else {
219 Status = HookEntry (ExceptionType, NewCallback);
220 }
221 }
222 #endif
223 return Status;
224 }
225
226 EFI_STATUS
227 EFIAPI
228 GetMaximumProcessorIndex (
229 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
230 OUT UINTN *MaxProcessorIndex
231 )
232 /*++
233
234 Routine Description: This is a DebugSupport protocol member function.
235
236 Arguments:
237 This - The DebugSupport instance
238 MaxProcessorIndex - The maximuim supported processor index
239
240 Returns:
241 Always returns EFI_SUCCESS with *MaxProcessorIndex set to 0
242
243 --*/
244 {
245 *MaxProcessorIndex = 0;
246 return (EFI_SUCCESS);
247 }
248
249 EFI_STATUS
250 EFIAPI
251 RegisterPeriodicCallback (
252 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
253 IN UINTN ProcessorIndex,
254 IN EFI_PERIODIC_CALLBACK PeriodicCallback
255 )
256 /*++
257
258 Routine Description: This is a DebugSupport protocol member function.
259
260 Arguments:
261 This - The DebugSupport instance
262 ProcessorIndex - Which processor the callback applies to.
263 PeriodicCallback - Callback function
264
265 Returns:
266
267 EFI_SUCCESS
268 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has
269 no handler registered for it
270 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.
271
272 Other possible return values are passed through from UnHookEntry and HookEntry.
273
274 --*/
275 {
276 return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);
277 }
278
279 EFI_STATUS
280 EFIAPI
281 RegisterExceptionCallback (
282 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
283 IN UINTN ProcessorIndex,
284 IN EFI_EXCEPTION_CALLBACK NewCallback,
285 IN EFI_EXCEPTION_TYPE ExceptionType
286 )
287 /*++
288
289 Routine Description:
290 This is a DebugSupport protocol member function.
291
292 This code executes in boot services context.
293
294 Arguments:
295 This - The DebugSupport instance
296 ProcessorIndex - Which processor the callback applies to.
297 NewCallback - Callback function
298 ExceptionType - Which exception to hook
299
300 Returns:
301
302 EFI_SUCCESS
303 EFI_INVALID_PARAMETER - requested uninstalling a handler from a vector that has
304 no handler registered for it
305 EFI_ALREADY_STARTED - requested install to a vector that already has a handler registered.
306
307 Other possible return values are passed through from UnHookEntry and HookEntry.
308
309 --*/
310 {
311 return ManageIdtEntryTable (NewCallback, ExceptionType);
312 }
313
314 EFI_STATUS
315 EFIAPI
316 InvalidateInstructionCache (
317 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
318 IN UINTN ProcessorIndex,
319 IN VOID *Start,
320 IN UINT64 Length
321 )
322 /*++
323
324 Routine Description:
325 This is a DebugSupport protocol member function.
326 Calls assembly routine to flush cache.
327
328 Arguments:
329 This - The DebugSupport instance
330 ProcessorIndex - Which processor the callback applies to.
331 Start - Physical base of the memory range to be invalidated
332 Length - mininum number of bytes in instruction cache to invalidate
333
334 Returns:
335
336 EFI_SUCCESS - always return success
337
338 --*/
339 {
340 AsmWbinvd ();
341 return EFI_SUCCESS;
342 }
343
344 EFI_STATUS
345 plInitializeDebugSupportDriver (
346 VOID
347 )
348 /*++
349
350 Routine Description:
351 Initializes driver's handler registration database.
352
353 This code executes in boot services context.
354
355 Arguments:
356 None
357
358 Returns:
359 EFI_SUCCESS
360 EFI_UNSUPPORTED - if IA32 processor does not support FXSTOR/FXRSTOR instructions,
361 the context save will fail, so these processor's are not supported.
362 EFI_OUT_OF_RESOURCES - not resource to finish initialization
363
364 --*/
365 {
366 if (!FxStorSupport ()) {
367 return EFI_UNSUPPORTED;
368 } else {
369 IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);
370 if (IdtEntryTable != NULL) {
371 return EFI_SUCCESS;
372 } else {
373 return EFI_OUT_OF_RESOURCES;
374 }
375 }
376 }
377
378 EFI_STATUS
379 EFIAPI
380 plUnloadDebugSupportDriver (
381 IN EFI_HANDLE ImageHandle
382 )
383 /*++
384
385 Routine Description:
386 This is the callback that is written to the LoadedImage protocol instance
387 on the image handle. It uninstalls all registered handlers and frees all entry
388 stub memory.
389
390 This code executes in boot services context.
391
392 Arguments:
393 ImageHandle - The image handle of the unload handler
394
395 Returns:
396
397 EFI_SUCCESS - always return success
398
399 --*/
400 {
401 EFI_EXCEPTION_TYPE ExceptionType;
402
403 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
404 ManageIdtEntryTable (NULL, ExceptionType);
405 }
406
407 FreePool (IdtEntryTable);
408 return EFI_SUCCESS;
409 }
410
411 VOID
412 InterruptDistrubutionHub (
413 EFI_EXCEPTION_TYPE ExceptionType,
414 EFI_SYSTEM_CONTEXT_IA32 *ContextRecord
415 )
416 /*++
417
418 Routine Description: Common piece of code that invokes the registered handlers.
419
420 This code executes in exception context so no efi calls are allowed.
421
422 Arguments:
423 ExceptionType - exception type
424 ContextRecord - system context
425
426 Returns:
427
428 None
429
430 --*/
431 {
432 if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {
433 if (ExceptionType != SYSTEM_TIMER_VECTOR) {
434 IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);
435 } else {
436 OrigVector = IdtEntryTable[ExceptionType].OrigVector;
437 IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);
438 }
439 }
440 }