]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DebugSupportDxe/X64/PlDebugSupportX64.c
e898d822719adb133fbf2802b08450b102595928
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / X64 / PlDebugSupportX64.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 #include "DebugSupport.h"
16
17 /**
18 Get Interrupt Handle from IDT Gate Descriptor.
19
20 @param IdtGateDecriptor 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 *IdtGateDecriptor
28 )
29 {
30 UINTN InterruptHandle;
31
32 ((UINT16 *) &InterruptHandle)[0] = (UINT16) IdtGateDecriptor->Bits.OffsetLow;
33 ((UINT16 *) &InterruptHandle)[1] = (UINT16) IdtGateDecriptor->Bits.OffsetHigh;
34 ((UINT32 *) &InterruptHandle)[1] = (UINT32) IdtGateDecriptor->Bits.OffsetUpper;
35
36 return InterruptHandle;
37 }
38
39 /**
40 Allocate pool for a new IDT entry stub.
41
42 Copy the generic stub into the new buffer and fixup the vector number
43 and jump target address.
44
45 @param ExceptionType This is the exception type that the new stub will be created
46 for.
47 @param Stub On successful exit, *Stub contains the newly allocated entry stub.
48
49 **/
50 VOID
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 6A 00 push 0 ; push vector number - will be modified before installed
67 // 00000002 E9 db 0e9h ; jump rel32
68 // 00000003 00000000 dd 0 ; fixed up to relative address of CommonIdtEntry
69 //
70
71 //
72 // poke in the exception type so the second push pushes the exception type
73 //
74 StubCopy[0x1] = (UINT8) ExceptionType;
75
76 //
77 // fixup the jump target to point to the common entry
78 //
79 *(UINT32 *) &StubCopy[0x3] = (UINT32)((UINTN) CommonIdtEntry - (UINTN) &StubCopy[StubSize]);
80
81 return;
82 }
83
84 /**
85 This is the main worker function that manages the state of the interrupt
86 handlers. It both installs and uninstalls interrupt handlers based on the
87 value of NewCallback. If NewCallback is NULL, then uninstall is indicated.
88 If NewCallback is non-NULL, then install is indicated.
89
90 @param NewCallback If non-NULL, NewCallback specifies the new handler to register.
91 If NULL, specifies that the previously registered handler should
92 be uninstalled.
93 @param ExceptionType Indicates which entry to manage.
94
95 @retval EFI_SUCCESS Process is ok.
96 @retval EFI_INVALID_PARAMETER Requested uninstalling a handler from a vector that has
97 no handler registered for it
98 @retval EFI_ALREADY_STARTED Requested install to a vector that already has a handler registered.
99 @retval others Possible return values are passed through from UnHookEntry and HookEntry.
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 (CompareMem (&IdtEntryTable[ExceptionType].NewDesc, &NullDesc, sizeof (IA32_IDT_GATE_DESCRIPTOR)) != 0) {
113 //
114 // we've already installed to this vector
115 //
116 if (NewCallback != NULL) {
117 //
118 // if the input handler is non-null, error
119 //
120 Status = EFI_ALREADY_STARTED;
121 } else {
122 UnhookEntry (ExceptionType);
123 }
124 } else {
125 //
126 // no user handler installed on this vector
127 //
128 if (NewCallback == NULL) {
129 //
130 // if the input handler is null, error
131 //
132 Status = EFI_INVALID_PARAMETER;
133 } else {
134 HookEntry (ExceptionType, NewCallback);
135 }
136 }
137
138 return Status;
139 }