]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupportIa32.c
code scrub for DebugSpport Module.
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / Ia32 / PlDebugSupportIa32.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 #include "DebugSupport.h"
16
17 /**
18 Get Interrupt Handle from IDT Gate Descriptor.
19
20 @param IdtGateDescriptor IDT Gate Descriptor.
21
22 @return Interrupt Handle stored in IDT Gate Descriptor.
23
24 **/
25 UINTN
26 GetInterruptHandleFromIdt (
27 IN IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor
28 )
29 {
30 UINTN InterruptHandle;
31
32 ((UINT16 *) &InterruptHandle)[0] = (UINT16) IdtGateDescriptor->Bits.OffsetLow;
33 ((UINT16 *) &InterruptHandle)[1] = (UINT16) IdtGateDescriptor->Bits.OffsetHigh;
34
35 return InterruptHandle;
36 }
37
38 /**
39 Allocate pool for a new IDT entry stub.
40
41 Copy the generic stub into the new buffer and fixup the vector number
42 and jump target address.
43
44 @param ExceptionType This is the exception type that the new stub will be created
45 for.
46 @param Stub On successful exit, *Stub contains the newly allocated entry stub.
47
48 **/
49 VOID
50 CreateEntryStub (
51 IN EFI_EXCEPTION_TYPE ExceptionType,
52 OUT VOID **Stub
53 )
54 {
55 UINT8 *StubCopy;
56
57 StubCopy = *Stub;
58
59 //
60 // Fixup the stub code for this vector
61 //
62
63 // The stub code looks like this:
64 //
65 // 00000000 89 25 00000004 R mov AppEsp, esp ; save stack top
66 // 00000006 BC 00008014 R mov esp, offset DbgStkBot ; switch to debugger stack
67 // 0000000B 6A 00 push 0 ; push vector number - will be modified before installed
68 // 0000000D E9 db 0e9h ; jump rel32
69 // 0000000E 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
70 //
71
72 //
73 // poke in the exception type so the second push pushes the exception type
74 //
75 StubCopy[0x0c] = (UINT8) ExceptionType;
76
77 //
78 // fixup the jump target to point to the common entry
79 //
80 *(UINT32 *) &StubCopy[0x0e] = (UINT32) CommonIdtEntry - (UINT32) &StubCopy[StubSize];
81
82 return ;
83 }
84
85 /**
86 This is the main worker function that manages the state of the interrupt
87 handlers. It both installs and uninstalls interrupt handlers based on the
88 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
89 If NewCallback is non-NULL, then install is indicated.
90
91 @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
92 If NULL, specifies that the previously registered handler should
93 be uninstalled.
94 @param ExceptionType Indicates which entry to manage.
95
96 @retval EFI_SUCCESS Installing or Uninstalling operation is ok.
97 @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
98 no handler registered for it
99 @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
100
101 **/
102 EFI_STATUS
103 ManageIdtEntryTable (
104 VOID (*NewCallback)(),
105 EFI_EXCEPTION_TYPE ExceptionType
106 )
107 {
108 EFI_STATUS Status;
109
110 Status = EFI_SUCCESS;
111
112 if (!FeaturePcdGet (PcdNtEmulatorEnable)) {
113 if (CompareMem (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc, sizeof (IA32_IDT_GATE_DESCRIPTOR)) != 0) {
114 //
115 // we've already installed to this vector
116 //
117 if (NewCallback != NULL) {
118 //
119 // if the input handler is non-null, error
120 //
121 Status = EFI_ALREADY_STARTED;
122 } else {
123 UnhookEntry (ExceptionType);
124 }
125 } else {
126 //
127 // no user handler installed on this vector
128 //
129 if (NewCallback == NULL) {
130 //
131 // if the input handler is null, error
132 //
133 Status = EFI_INVALID_PARAMETER;
134 } else {
135 HookEntry (ExceptionType, NewCallback);
136 }
137 }
138 }
139
140 return Status;
141 }