]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupportX64.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / X64 / PlDebugSupportX64.c
1 /** @file
2 X64 specific functions to support Debug Support protocol.
3
4 Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "PlDebugSupport.h"
10
11 IA32_IDT_GATE_DESCRIPTOR NullDesc = {{0,0}};
12
13 /**
14 Get Interrupt Handle from IDT Gate Descriptor.
15
16 @param IdtGateDecriptor IDT Gate Descriptor.
17
18 @return Interrupt Handle stored in IDT Gate Descriptor.
19
20 **/
21 UINTN
22 GetInterruptHandleFromIdt (
23 IN IA32_IDT_GATE_DESCRIPTOR *IdtGateDecriptor
24 )
25 {
26 UINTN InterruptHandle;
27
28 //
29 // InterruptHandle 0-15 : OffsetLow
30 // InterruptHandle 16-31 : OffsetHigh
31 // InterruptHandle 32-63 : OffsetUpper
32 //
33 InterruptHandle = ((UINTN) IdtGateDecriptor->Bits.OffsetLow) |
34 (((UINTN) IdtGateDecriptor->Bits.OffsetHigh) << 16) |
35 (((UINTN) IdtGateDecriptor->Bits.OffsetUpper) << 32) ;
36
37 return InterruptHandle;
38 }
39
40 /**
41 Allocate pool for a new IDT entry stub.
42
43 Copy the generic stub into the new buffer and fixup the vector number
44 and jump target address.
45
46 @param ExceptionType This is the exception type that the new stub will be created
47 for.
48 @param Stub On successful exit, *Stub contains the newly allocated entry stub.
49
50 **/
51 VOID
52 CreateEntryStub (
53 IN EFI_EXCEPTION_TYPE ExceptionType,
54 OUT VOID **Stub
55 )
56 {
57 UINT8 *StubCopy;
58
59 StubCopy = *Stub;
60
61 //
62 // Fixup the stub code for this vector
63 //
64
65 // The stub code looks like this:
66 //
67 // 00000000 6A 00 push 0 ; push vector number - will be modified before installed
68 // 00000002 E9 db 0e9h ; jump rel32
69 // 00000003 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[0x1] = (UINT8) ExceptionType;
76
77 //
78 // fixup the jump target to point to the common entry
79 //
80 *(UINT32 *) &StubCopy[0x3] = (UINT32)((UINTN) CommonIdtEntry - (UINTN) &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 Process 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 @retval others Possible return values are passed through from UnHookEntry and HookEntry.
101
102 **/
103 EFI_STATUS
104 ManageIdtEntryTable (
105 CALLBACK_FUNC NewCallback,
106 EFI_EXCEPTION_TYPE ExceptionType
107 )
108 {
109 EFI_STATUS Status;
110
111 Status = EFI_SUCCESS;
112
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 return Status;
140 }