Synchronize MdePkg/Library *.h files with c files
[edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / X64 / PlDebugSupport.c
blob24e5acc74a756fe64eae428d8a694e5d6f790450
1 /** @file
2 X64 specific debug support functions
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
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.
13 **/
16 // private header files
18 #include "PlDebugSupport.h"
21 // This the global main table to keep track of the interrupts
23 IDT_ENTRY *IdtEntryTable = NULL;
24 DESCRIPTOR NullDesc = {0, 0};
26 /**
27 Allocate pool for a new IDT entry stub.
29 Copy the generic stub into the new buffer and fixup the vector number
30 and jump target address.
32 @param ExceptionType This is the exception type that the new stub will be created
33 for.
34 @param Stub On successful exit, *Stub contains the newly allocated entry stub.
36 @retval EFI_SUCCESS Always.
38 **/
39 EFI_STATUS
40 CreateEntryStub (
41 IN EFI_EXCEPTION_TYPE ExceptionType,
42 OUT VOID **Stub
45 UINT8 *StubCopy;
47 StubCopy = *Stub;
50 // Fixup the stub code for this vector
53 // The stub code looks like this:
55 // 00000000 6A 00 push 0 ; push vector number - will be modified before installed
56 // 00000002 E9 db 0e9h ; jump rel32
57 // 00000003 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
61 // poke in the exception type so the second push pushes the exception type
63 StubCopy[0x1] = (UINT8) ExceptionType;
66 // fixup the jump target to point to the common entry
68 *(UINT32 *) &StubCopy[0x3] = (UINT32)((UINTN) CommonIdtEntry - (UINTN) &StubCopy[StubSize]);
70 return EFI_SUCCESS;
73 /**
74 Creates a nes entry stub. Then saves the current IDT entry and replaces it
75 with an interrupt gate for the new entry point. The IdtEntryTable is updated
76 with the new registered function.
78 This code executes in boot services context. The stub entry executes in interrupt
79 context.
81 @param ExceptionType Specifies which vector to hook.
82 @param NewCallback A pointer to the new function to be registered.
84 @retval EFI_SUCCESS Always.
86 **/
87 EFI_STATUS
88 HookEntry (
89 IN EFI_EXCEPTION_TYPE ExceptionType,
90 IN VOID (*NewCallback) ()
93 BOOLEAN OldIntFlagState;
94 EFI_STATUS Status;
96 Status = CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);
97 if (Status == EFI_SUCCESS) {
98 OldIntFlagState = WriteInterruptFlag (0);
99 READ_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
101 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[0] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc.Low)[0];
102 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc.Low)[3];
103 ((UINT32 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT32 *) &IdtEntryTable[ExceptionType].OrigDesc.High)[0];
105 Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);
106 IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;
107 WRITE_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));
108 WriteInterruptFlag (OldIntFlagState);
111 return Status;
115 Undoes HookEntry. This code executes in boot services context.
117 @param ExceptionType Specifies which entry to unhook
119 @retval EFI_SUCCESS Always.
122 EFI_STATUS
123 UnhookEntry (
124 IN EFI_EXCEPTION_TYPE ExceptionType
127 BOOLEAN OldIntFlagState;
129 OldIntFlagState = WriteInterruptFlag (0);
130 WRITE_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
131 WriteInterruptFlag (OldIntFlagState);
133 return EFI_SUCCESS;
137 This is the main worker function that manages the state of the interrupt
138 handlers. It both installs and uninstalls interrupt handlers based on the
139 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
140 If NewCallback is non-NULL, then install is indicated.
142 @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
143 If NULL, specifies that the previously registered handler should
144 be uninstalled.
145 @param ExceptionType Indicates which entry to manage.
147 @retval EFI_SUCCESS Process is ok.
148 @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
149 no handler registered for it
150 @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
151 @retval others Possible return values are passed through from UnHookEntry and HookEntry.
154 EFI_STATUS
155 ManageIdtEntryTable (
156 VOID (*NewCallback)(),
157 EFI_EXCEPTION_TYPE ExceptionType
160 EFI_STATUS Status;
162 Status = EFI_SUCCESS;
164 if (COMPARE_DESCRIPTOR (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {
166 // we've already installed to this vector
168 if (NewCallback != NULL) {
170 // if the input handler is non-null, error
172 Status = EFI_ALREADY_STARTED;
173 } else {
174 Status = UnhookEntry (ExceptionType);
176 } else {
178 // no user handler installed on this vector
180 if (NewCallback == NULL) {
182 // if the input handler is null, error
184 Status = EFI_INVALID_PARAMETER;
185 } else {
186 Status = HookEntry (ExceptionType, NewCallback);
190 return Status;
194 This is a DebugSupport protocol member function, hard
195 coded to support only 1 processor for now.
197 @param This The DebugSupport instance
198 @param MaxProcessorIndex The maximuim supported processor index
200 @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
203 EFI_STATUS
204 EFIAPI
205 GetMaximumProcessorIndex (
206 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
207 OUT UINTN *MaxProcessorIndex
210 *MaxProcessorIndex = 0;
211 return (EFI_SUCCESS);
215 DebugSupport protocol member function.
217 @param This The DebugSupport instance
218 @param ProcessorIndex Which processor the callback applies to.
219 @param PeriodicCallback Callback function
221 @retval EFI_SUCCESS Indicates the callback was registered.
222 @retval others Callback was not registered.
225 EFI_STATUS
226 EFIAPI
227 RegisterPeriodicCallback (
228 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
229 IN UINTN ProcessorIndex,
230 IN EFI_PERIODIC_CALLBACK PeriodicCallback
233 return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);
237 DebugSupport protocol member function.
239 This code executes in boot services context.
241 @param This The DebugSupport instance
242 @param ProcessorIndex Which processor the callback applies to.
243 @param NewCallback Callback function
244 @param ExceptionType Which exception to hook
246 @retval EFI_SUCCESS Indicates the callback was registered.
247 @retval others Callback was not registered.
250 EFI_STATUS
251 EFIAPI
252 RegisterExceptionCallback (
253 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
254 IN UINTN ProcessorIndex,
255 IN EFI_EXCEPTION_CALLBACK NewCallback,
256 IN EFI_EXCEPTION_TYPE ExceptionType
259 return ManageIdtEntryTable (NewCallback, ExceptionType);
263 DebugSupport protocol member function. Calls assembly routine to flush cache.
265 @param This The DebugSupport instance
266 @param ProcessorIndex Which processor the callback applies to.
267 @param Start Physical base of the memory range to be invalidated
268 @param Length mininum number of bytes in instruction cache to invalidate
270 @retval EFI_SUCCESS Always returned.
273 EFI_STATUS
274 EFIAPI
275 InvalidateInstructionCache (
276 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
277 IN UINTN ProcessorIndex,
278 IN VOID *Start,
279 IN UINT64 Length
282 AsmWbinvd ();
283 return EFI_SUCCESS;
287 Initializes driver's handler registration databas.
289 This code executes in boot services context
290 Must be public because it's referenced from DebugSupport.c
292 @retval EFI_UNSUPPORTED If x64 processor does not support FXSTOR/FXRSTOR instructions,
293 the context save will fail, so these processor's are not supported.
294 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
295 @retval EFI_SUCCESS Initializes successfully.
298 EFI_STATUS
299 PlInitializeDebugSupportDriver (
300 VOID
303 EFI_EXCEPTION_TYPE ExceptionType;
305 if (!FxStorSupport ()) {
306 return EFI_UNSUPPORTED;
309 IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);
310 if (IdtEntryTable == NULL) {
311 return EFI_OUT_OF_RESOURCES;
314 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
315 IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC) (UINTN) AllocatePool (StubSize);
316 if (IdtEntryTable[ExceptionType].StubEntry == NULL) {
317 goto ErrorCleanup;
320 CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);
322 return EFI_SUCCESS;
324 ErrorCleanup:
326 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
327 if (IdtEntryTable[ExceptionType].StubEntry != NULL) {
328 FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);
331 FreePool (IdtEntryTable);
333 return EFI_OUT_OF_RESOURCES;
337 This is the callback that is written to the LoadedImage protocol instance
338 on the image handle. It uninstalls all registered handlers and frees all entry
339 stub memory.
341 @param ImageHandle The firmware allocated handle for the EFI image.
343 @retval EFI_SUCCESS Always.
346 EFI_STATUS
347 EFIAPI
348 PlUnloadDebugSupportDriver (
349 IN EFI_HANDLE ImageHandle
352 EFI_EXCEPTION_TYPE ExceptionType;
354 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
355 ManageIdtEntryTable (NULL, ExceptionType);
358 FreePool (IdtEntryTable);
359 return EFI_SUCCESS;
363 Common piece of code that invokes the registered handlers.
365 This code executes in exception context so no efi calls are allowed.
367 @param ExceptionType Exception type
368 @param ContextRecord System context
371 VOID
372 InterruptDistrubutionHub (
373 EFI_EXCEPTION_TYPE ExceptionType,
374 EFI_SYSTEM_CONTEXT_IA32 *ContextRecord
377 if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {
378 if (ExceptionType != SYSTEM_TIMER_VECTOR) {
379 IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);
380 } else {
381 OrigVector = IdtEntryTable[ExceptionType].OrigVector;
382 IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);