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