2 PE/Coff Extra Action library instances.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <PeCoffExtraActionLib.h>
12 Check if the hardware breakpoint in Drx is enabled by checking the Lx and Gx bit in Dr7.
14 It assumes that DebugAgent will set both Lx and Gx bit when setting up the hardware breakpoint.
17 @param RegisterIndex Index of Dr register. The value range is from 0 to 3.
18 @param Dr7 Value of Dr7 register.
20 @return TRUE The hardware breakpoint specified in the Drx is enabled.
21 @return FALSE The hardware breakpoint specified in the Drx is disabled.
26 IN UINT8 RegisterIndex
,
30 return (BOOLEAN
) (((Dr7
>> (RegisterIndex
* 2)) & (BIT0
| BIT1
)) == (BIT0
| BIT1
));
34 Common routine to report the PE/COFF image loading/relocating or unloading event.
36 If ImageContext is NULL, then ASSERT().
38 @param ImageContext Pointer to the image context structure that describes the
40 @param Signature IMAGE_LOAD_SIGNATURE or IMAGE_UNLOAD_SIGNATURE.
44 PeCoffLoaderExtraActionCommon (
45 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
49 BOOLEAN InterruptState
;
57 UINT8 LoadImageMethod
;
58 UINT8 DebugAgentStatus
;
59 IA32_DESCRIPTOR IdtDescriptor
;
60 IA32_IDT_GATE_DESCRIPTOR OriginalIdtEntry
;
61 BOOLEAN IdtEntryHooked
;
64 ASSERT (ImageContext
!= NULL
);
66 if (ImageContext
->PdbPointer
!= NULL
) {
67 DEBUG((EFI_D_ERROR
, " PDB = %a\n", ImageContext
->PdbPointer
));
71 // Disable interrupts and save the current interrupt state
73 InterruptState
= SaveAndDisableInterrupts ();
75 IdtEntryHooked
= FALSE
;
76 LoadImageMethod
= PcdGet8 (PcdDebugLoadImageMethod
);
77 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
79 // If the CPU does not support Debug Extensions(CPUID:01 EDX:BIT2)
80 // then force use of DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
82 AsmCpuid (1, NULL
, NULL
, NULL
, &RegEdx
);
83 if ((RegEdx
& BIT2
) == 0) {
84 LoadImageMethod
= DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
;
87 AsmReadIdtr (&IdtDescriptor
);
88 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
89 if (!CheckDebugAgentHandler (&IdtDescriptor
, SOFT_INT_VECTOR_NUM
)) {
91 // Do not trigger INT3 if Debug Agent did not setup IDT entries.
96 if (!CheckDebugAgentHandler (&IdtDescriptor
, IO_HW_BREAKPOINT_VECTOR_NUM
)) {
98 // Save and update IDT entry for INT1
100 SaveAndUpdateIdtEntry1 (&IdtDescriptor
, &OriginalIdtEntry
);
101 IdtEntryHooked
= TRUE
;
106 // Save Debug Register State
112 Dr7
= AsmReadDr7 () | BIT10
; // H/w sets bit 10, some simulators don't
117 // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name
118 // DR2 = The pointer to the ImageContext structure
119 // DR3 = IO_PORT_BREAKPOINT_ADDRESS
120 // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte
121 // CR4 = Make sure DE(BIT3) is set
124 AsmWriteDr0 (Signature
);
125 AsmWriteDr1 ((UINTN
) ImageContext
->PdbPointer
);
126 AsmWriteDr2 ((UINTN
) ImageContext
);
127 AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS
);
129 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
130 AsmWriteDr7 (0x20000480);
131 AsmWriteCr4 (Cr4
| BIT3
);
133 // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port
134 // returns a read value other than DEBUG_AGENT_IMAGE_WAIT
137 DebugAgentStatus
= IoRead8 (IO_PORT_BREAKPOINT_ADDRESS
);
138 } while (DebugAgentStatus
== DEBUG_AGENT_IMAGE_WAIT
);
140 } else if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
142 // Generate a software break point.
148 // Restore Debug Register State only when Host didn't change it inside exception handler.
149 // E.g.: User halts the target and sets the HW breakpoint while target is
150 // in the above exception handler
152 NewDr7
= AsmReadDr7 () | BIT10
; // H/w sets bit 10, some simulators don't
153 if (!IsDrxEnabled (0, NewDr7
) && (AsmReadDr0 () == 0 || AsmReadDr0 () == Signature
)) {
155 // If user changed Dr3 (by setting HW bp in the above exception handler,
156 // we will not set Dr0 to 0 in GO/STEP handler because the break cause is not IMAGE_LOAD/_UNLOAD.
160 if (!IsDrxEnabled (1, NewDr7
) && (AsmReadDr1 () == (UINTN
) ImageContext
->PdbPointer
)) {
163 if (!IsDrxEnabled (2, NewDr7
) && (AsmReadDr2 () == (UINTN
) ImageContext
)) {
166 if (!IsDrxEnabled (3, NewDr7
) && (AsmReadDr3 () == IO_PORT_BREAKPOINT_ADDRESS
)) {
169 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
170 if (AsmReadCr4 () == (Cr4
| BIT3
)) {
173 if (NewDr7
== 0x20000480) {
176 } else if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
177 if (NewDr7
== BIT10
) {
182 // Restore original IDT entry for INT1 if it was hooked.
184 if (IdtEntryHooked
) {
185 RestoreIdtEntry1 (&IdtDescriptor
, &OriginalIdtEntry
);
188 // Restore the interrupt state
190 SetInterruptState (InterruptState
);
194 Performs additional actions after a PE/COFF image has been loaded and relocated.
196 @param ImageContext Pointer to the image context structure that describes the
197 PE/COFF image that has already been loaded and relocated.
202 PeCoffLoaderRelocateImageExtraAction (
203 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
206 PeCoffLoaderExtraActionCommon (ImageContext
, IMAGE_LOAD_SIGNATURE
);
210 Performs additional actions just before a PE/COFF image is unloaded. Any resources
211 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
213 @param ImageContext Pointer to the image context structure that describes the
214 PE/COFF image that is being unloaded.
219 PeCoffLoaderUnloadImageExtraAction (
220 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
223 PeCoffLoaderExtraActionCommon (ImageContext
, IMAGE_UNLOAD_SIGNATURE
);