]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.c
ECC clean up.
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / Ia32 / PlDebugSupport.c
1 /** @file
2 IA32 specific debug support functions
3
4 Copyright (c) 2006 - 2008, 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 /**
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 89 25 00000004 R mov AppEsp, esp ; save stack top
56 // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack
57 // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed
58 // 0000000D E9 db 0e9h ; jump rel32
59 // 0000000E 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[0x0c] = (UINT8) ExceptionType;
66
67 //
68 // fixup the jump target to point to the common entry
69 //
70 *(UINT32 *) &StubCopy[0x0e] = (UINT32) CommonIdtEntry - (UINT32) &StubCopy[StubSize];
71
72 return EFI_SUCCESS;
73 }
74
75 /**
76 Creates a nes entry stub. Then saves the current IDT entry and replaces it
77 with an interrupt gate for the new entry point. The IdtEntryTable is updated
78 with the new registered function.
79
80 This code executes in boot services context. The stub entry executes in interrupt
81 context.
82
83 @param ExceptionType Specifies which vector to hook.
84 @param NewCallback A pointer to the new function to be registered.
85
86 @retval EFI_SUCCESS Always.
87
88 **/
89 EFI_STATUS
90 HookEntry (
91 IN EFI_EXCEPTION_TYPE ExceptionType,
92 IN VOID (*NewCallback) ()
93 )
94 {
95 BOOLEAN OldIntFlagState;
96 EFI_STATUS Status;
97
98 Status = CreateEntryStub (ExceptionType, (VOID **) &IdtEntryTable[ExceptionType].StubEntry);
99 if (Status == EFI_SUCCESS) {
100 OldIntFlagState = WriteInterruptFlag (0);
101 READ_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
102
103 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[0] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[0];
104 ((UINT16 *) &IdtEntryTable[ExceptionType].OrigVector)[1] = ((UINT16 *) &IdtEntryTable[ExceptionType].OrigDesc)[3];
105
106 Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);
107 IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;
108 WRITE_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));
109 WriteInterruptFlag (OldIntFlagState);
110 }
111
112 return Status;
113 }
114
115 /**
116 Undoes HookEntry. This code executes in boot services context.
117
118 @param ExceptionType Specifies which entry to unhook
119
120 @retval EFI_SUCCESS Always.
121
122 **/
123 EFI_STATUS
124 UnhookEntry (
125 IN EFI_EXCEPTION_TYPE ExceptionType
126 )
127 {
128 BOOLEAN OldIntFlagState;
129
130 OldIntFlagState = WriteInterruptFlag (0);
131 WRITE_IDT (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
132 WriteInterruptFlag (OldIntFlagState);
133
134 return EFI_SUCCESS;
135 }
136
137 /**
138 This is the main worker function that manages the state of the interrupt
139 handlers. It both installs and uninstalls interrupt handlers based on the
140 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
141 If NewCallback is non-NULL, then install is indicated.
142
143 @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
144 If NULL, specifies that the previously registered handler should
145 be uninstalled.
146 @param ExceptionType Indicates which entry to manage.
147
148 @retval EFI_SUCCESS Process is ok.
149 @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
150 no handler registered for it
151 @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
152 @retval others Possible return values are passed through from UnHookEntry and HookEntry.
153
154 **/
155 EFI_STATUS
156 ManageIdtEntryTable (
157 VOID (*NewCallback)(),
158 EFI_EXCEPTION_TYPE ExceptionType
159 )
160 {
161 EFI_STATUS Status;
162
163 Status = EFI_SUCCESS;
164
165 if (!FeaturePcdGet (PcdNtEmulatorEnable)) {
166 if (COMPARE_DESCRIPTOR (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc)) {
167 //
168 // we've already installed to this vector
169 //
170 if (NewCallback != NULL) {
171 //
172 // if the input handler is non-null, error
173 //
174 Status = EFI_ALREADY_STARTED;
175 } else {
176 Status = UnhookEntry (ExceptionType);
177 }
178 } else {
179 //
180 // no user handler installed on this vector
181 //
182 if (NewCallback == NULL) {
183 //
184 // if the input handler is null, error
185 //
186 Status = EFI_INVALID_PARAMETER;
187 } else {
188 Status = HookEntry (ExceptionType, NewCallback);
189 }
190 }
191 }
192
193 return Status;
194 }
195
196 /**
197 This is a DebugSupport protocol member function, hard
198 coded to support only 1 processor for now.
199
200 @param This The DebugSupport instance
201 @param MaxProcessorIndex The maximuim supported processor index
202
203 @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
204
205 **/
206 EFI_STATUS
207 EFIAPI
208 GetMaximumProcessorIndex (
209 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
210 OUT UINTN *MaxProcessorIndex
211 )
212 {
213 *MaxProcessorIndex = 0;
214 return (EFI_SUCCESS);
215 }
216
217 /**
218 DebugSupport protocol member function.
219
220 @param This The DebugSupport instance
221 @param ProcessorIndex Which processor the callback applies to.
222 @param PeriodicCallback Callback function
223
224 @retval EFI_SUCCESS Indicates the callback was registered.
225 @retval others Callback was not registered.
226
227 **/
228 EFI_STATUS
229 EFIAPI
230 RegisterPeriodicCallback (
231 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
232 IN UINTN ProcessorIndex,
233 IN EFI_PERIODIC_CALLBACK PeriodicCallback
234 )
235 {
236 return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);
237 }
238
239 /**
240 DebugSupport protocol member function.
241
242 This code executes in boot services context.
243
244 @param This The DebugSupport instance
245 @param ProcessorIndex Which processor the callback applies to.
246 @param NewCallback Callback function
247 @param ExceptionType Which exception to hook
248
249 @retval EFI_SUCCESS Indicates the callback was registered.
250 @retval others Callback was not registered.
251
252 **/
253 EFI_STATUS
254 EFIAPI
255 RegisterExceptionCallback (
256 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
257 IN UINTN ProcessorIndex,
258 IN EFI_EXCEPTION_CALLBACK NewCallback,
259 IN EFI_EXCEPTION_TYPE ExceptionType
260 )
261 {
262 return ManageIdtEntryTable (NewCallback, ExceptionType);
263 }
264
265 /**
266 DebugSupport protocol member function. Calls assembly routine to flush cache.
267
268 @param This The DebugSupport instance
269 @param ProcessorIndex Which processor the callback applies to.
270 @param Start Physical base of the memory range to be invalidated
271 @param Length mininum number of bytes in instruction cache to invalidate
272
273 @retval EFI_SUCCESS Always returned.
274
275 **/
276 EFI_STATUS
277 EFIAPI
278 InvalidateInstructionCache (
279 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
280 IN UINTN ProcessorIndex,
281 IN VOID *Start,
282 IN UINT64 Length
283 )
284 {
285 AsmWbinvd ();
286 return EFI_SUCCESS;
287 }
288
289 /**
290 Initializes driver's handler registration databas.
291
292 This code executes in boot services context
293 Must be public because it's referenced from DebugSupport.c
294
295 @retval EFI_UNSUPPORTED If IA32 processor does not support FXSTOR/FXRSTOR instructions,
296 the context save will fail, so these processor's are not supported.
297 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
298 @retval EFI_SUCCESS Initializes successfully.
299
300 **/
301 EFI_STATUS
302 PlInitializeDebugSupportDriver (
303 VOID
304 )
305 {
306 EFI_EXCEPTION_TYPE ExceptionType;
307
308 if (!FxStorSupport ()) {
309 return EFI_UNSUPPORTED;
310 }
311
312 IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);
313 if (IdtEntryTable == NULL) {
314 return EFI_OUT_OF_RESOURCES;
315 }
316
317 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
318 IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC) (UINTN) AllocatePool (StubSize);
319 if (IdtEntryTable[ExceptionType].StubEntry == NULL) {
320 goto ErrorCleanup;
321 }
322
323 CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);
324 }
325 return EFI_SUCCESS;
326
327 ErrorCleanup:
328
329 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
330 if (IdtEntryTable[ExceptionType].StubEntry != NULL) {
331 FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);
332 }
333 }
334 FreePool (IdtEntryTable);
335
336 return EFI_OUT_OF_RESOURCES;
337 }
338
339 /**
340 This is the callback that is written to the LoadedImage protocol instance
341 on the image handle. It uninstalls all registered handlers and frees all entry
342 stub memory.
343
344 @param ImageHandle The firmware allocated handle for the EFI image.
345
346 @retval EFI_SUCCESS Always.
347
348 **/
349 EFI_STATUS
350 EFIAPI
351 PlUnloadDebugSupportDriver (
352 IN EFI_HANDLE ImageHandle
353 )
354 {
355 EFI_EXCEPTION_TYPE ExceptionType;
356
357 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
358 ManageIdtEntryTable (NULL, ExceptionType);
359 }
360
361 FreePool (IdtEntryTable);
362 return EFI_SUCCESS;
363 }
364
365 /**
366 Common piece of code that invokes the registered handlers.
367
368 This code executes in exception context so no efi calls are allowed.
369
370 @param ExceptionType Exception type
371 @param ContextRecord System context
372
373 **/
374 VOID
375 InterruptDistrubutionHub (
376 EFI_EXCEPTION_TYPE ExceptionType,
377 EFI_SYSTEM_CONTEXT_IA32 *ContextRecord
378 )
379 {
380 if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {
381 if (ExceptionType != SYSTEM_TIMER_VECTOR) {
382 IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);
383 } else {
384 OrigVector = IdtEntryTable[ExceptionType].OrigVector;
385 IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);
386 }
387 }
388 }