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