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