]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupport.c
Synchronize MdePkg/Library *.h files with c files
[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 /**
27 Allocate pool for a new IDT entry stub.
28
29 Copy the generic stub into the new buffer and fixup the vector number
30 and jump target address.
31
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.
35
36 @retval EFI_SUCCESS Always.
37
38 **/
39 EFI_STATUS
40 CreateEntryStub (
41 IN EFI_EXCEPTION_TYPE ExceptionType,
42 OUT VOID **Stub
43 )
44 {
45 UINT8 *StubCopy;
46
47 StubCopy = *Stub;
48
49 //
50 // Fixup the stub code for this vector
51 //
52
53 // The stub code looks like this:
54 //
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
58 //
59
60 //
61 // poke in the exception type so the second push pushes the exception type
62 //
63 StubCopy[0x1] = (UINT8) ExceptionType;
64
65 //
66 // fixup the jump target to point to the common entry
67 //
68 *(UINT32 *) &StubCopy[0x3] = (UINT32)((UINTN) CommonIdtEntry - (UINTN) &StubCopy[StubSize]);
69
70 return EFI_SUCCESS;
71 }
72
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.
77
78 This code executes in boot services context. The stub entry executes in interrupt
79 context.
80
81 @param ExceptionType Specifies which vector to hook.
82 @param NewCallback A pointer to the new function to be registered.
83
84 @retval EFI_SUCCESS Always.
85
86 **/
87 EFI_STATUS
88 HookEntry (
89 IN EFI_EXCEPTION_TYPE ExceptionType,
90 IN VOID (*NewCallback) ()
91 )
92 {
93 BOOLEAN OldIntFlagState;
94 EFI_STATUS Status;
95
96 Status = CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);
97 if (Status == EFI_SUCCESS) {
98 OldIntFlagState = WriteInterruptFlag (0);
99 READ_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
100
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];
104
105 Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);
106 IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;
107 WRITE_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));
108 WriteInterruptFlag (OldIntFlagState);
109 }
110
111 return Status;
112 }
113
114 /**
115 Undoes HookEntry. This code executes in boot services context.
116
117 @param ExceptionType Specifies which entry to unhook
118
119 @retval EFI_SUCCESS Always.
120
121 **/
122 EFI_STATUS
123 UnhookEntry (
124 IN EFI_EXCEPTION_TYPE ExceptionType
125 )
126 {
127 BOOLEAN OldIntFlagState;
128
129 OldIntFlagState = WriteInterruptFlag (0);
130 WRITE_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
131 WriteInterruptFlag (OldIntFlagState);
132
133 return EFI_SUCCESS;
134 }
135
136 /**
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.
141
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.
146
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.
152
153 **/
154 EFI_STATUS
155 ManageIdtEntryTable (
156 VOID (*NewCallback)(),
157 EFI_EXCEPTION_TYPE ExceptionType
158 )
159 {
160 EFI_STATUS Status;
161
162 Status = EFI_SUCCESS;
163
164 if (COMPARE_DESCRIPTOR (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {
165 //
166 // we've already installed to this vector
167 //
168 if (NewCallback != NULL) {
169 //
170 // if the input handler is non-null, error
171 //
172 Status = EFI_ALREADY_STARTED;
173 } else {
174 Status = UnhookEntry (ExceptionType);
175 }
176 } else {
177 //
178 // no user handler installed on this vector
179 //
180 if (NewCallback == NULL) {
181 //
182 // if the input handler is null, error
183 //
184 Status = EFI_INVALID_PARAMETER;
185 } else {
186 Status = HookEntry (ExceptionType, NewCallback);
187 }
188 }
189
190 return Status;
191 }
192
193 /**
194 This is a DebugSupport protocol member function, hard
195 coded to support only 1 processor for now.
196
197 @param This The DebugSupport instance
198 @param MaxProcessorIndex The maximuim supported processor index
199
200 @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
201
202 **/
203 EFI_STATUS
204 EFIAPI
205 GetMaximumProcessorIndex (
206 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
207 OUT UINTN *MaxProcessorIndex
208 )
209 {
210 *MaxProcessorIndex = 0;
211 return (EFI_SUCCESS);
212 }
213
214 /**
215 DebugSupport protocol member function.
216
217 @param This The DebugSupport instance
218 @param ProcessorIndex Which processor the callback applies to.
219 @param PeriodicCallback Callback function
220
221 @retval EFI_SUCCESS Indicates the callback was registered.
222 @retval others Callback was not registered.
223
224 **/
225 EFI_STATUS
226 EFIAPI
227 RegisterPeriodicCallback (
228 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
229 IN UINTN ProcessorIndex,
230 IN EFI_PERIODIC_CALLBACK PeriodicCallback
231 )
232 {
233 return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);
234 }
235
236 /**
237 DebugSupport protocol member function.
238
239 This code executes in boot services context.
240
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
245
246 @retval EFI_SUCCESS Indicates the callback was registered.
247 @retval others Callback was not registered.
248
249 **/
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
257 )
258 {
259 return ManageIdtEntryTable (NewCallback, ExceptionType);
260 }
261
262 /**
263 DebugSupport protocol member function. Calls assembly routine to flush cache.
264
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
269
270 @retval EFI_SUCCESS Always returned.
271
272 **/
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
280 )
281 {
282 AsmWbinvd ();
283 return EFI_SUCCESS;
284 }
285
286 /**
287 Initializes driver's handler registration databas.
288
289 This code executes in boot services context
290 Must be public because it's referenced from DebugSupport.c
291
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.
296
297 **/
298 EFI_STATUS
299 PlInitializeDebugSupportDriver (
300 VOID
301 )
302 {
303 EFI_EXCEPTION_TYPE ExceptionType;
304
305 if (!FxStorSupport ()) {
306 return EFI_UNSUPPORTED;
307 }
308
309 IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);
310 if (IdtEntryTable == NULL) {
311 return EFI_OUT_OF_RESOURCES;
312 }
313
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;
318 }
319
320 CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);
321 }
322 return EFI_SUCCESS;
323
324 ErrorCleanup:
325
326 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
327 if (IdtEntryTable[ExceptionType].StubEntry != NULL) {
328 FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);
329 }
330 }
331 FreePool (IdtEntryTable);
332
333 return EFI_OUT_OF_RESOURCES;
334 }
335
336 /**
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.
340
341 @param ImageHandle The firmware allocated handle for the EFI image.
342
343 @retval EFI_SUCCESS Always.
344
345 **/
346 EFI_STATUS
347 EFIAPI
348 PlUnloadDebugSupportDriver (
349 IN EFI_HANDLE ImageHandle
350 )
351 {
352 EFI_EXCEPTION_TYPE ExceptionType;
353
354 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
355 ManageIdtEntryTable (NULL, ExceptionType);
356 }
357
358 FreePool (IdtEntryTable);
359 return EFI_SUCCESS;
360 }
361
362 /**
363 Common piece of code that invokes the registered handlers.
364
365 This code executes in exception context so no efi calls are allowed.
366
367 @param ExceptionType Exception type
368 @param ContextRecord System context
369
370 **/
371 VOID
372 InterruptDistrubutionHub (
373 EFI_EXCEPTION_TYPE ExceptionType,
374 EFI_SYSTEM_CONTEXT_IA32 *ContextRecord
375 )
376 {
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);
383 }
384 }
385 }