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 ((DEBUG_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
;
88 AsmReadIdtr (&IdtDescriptor
);
89 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
90 if (!CheckDebugAgentHandler (&IdtDescriptor
, SOFT_INT_VECTOR_NUM
)) {
92 // Do not trigger INT3 if Debug Agent did not setup IDT entries.
97 if (!CheckDebugAgentHandler (&IdtDescriptor
, IO_HW_BREAKPOINT_VECTOR_NUM
)) {
99 // Save and update IDT entry for INT1
101 SaveAndUpdateIdtEntry1 (&IdtDescriptor
, &OriginalIdtEntry
);
102 IdtEntryHooked
= TRUE
;
107 // Save Debug Register State
113 Dr7
= AsmReadDr7 () | BIT10
; // H/w sets bit 10, some simulators don't
118 // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name
119 // DR2 = The pointer to the ImageContext structure
120 // DR3 = IO_PORT_BREAKPOINT_ADDRESS
121 // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte
122 // CR4 = Make sure DE(BIT3) is set
125 AsmWriteDr0 (Signature
);
126 AsmWriteDr1 ((UINTN
)ImageContext
->PdbPointer
);
127 AsmWriteDr2 ((UINTN
)ImageContext
);
128 AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS
);
130 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
131 AsmWriteDr7 (0x20000480);
132 AsmWriteCr4 (Cr4
| BIT3
);
134 // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port
135 // returns a read value other than DEBUG_AGENT_IMAGE_WAIT
138 DebugAgentStatus
= IoRead8 (IO_PORT_BREAKPOINT_ADDRESS
);
139 } 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.
161 if (!IsDrxEnabled (1, NewDr7
) && (AsmReadDr1 () == (UINTN
)ImageContext
->PdbPointer
)) {
165 if (!IsDrxEnabled (2, NewDr7
) && (AsmReadDr2 () == (UINTN
)ImageContext
)) {
169 if (!IsDrxEnabled (3, NewDr7
) && (AsmReadDr3 () == IO_PORT_BREAKPOINT_ADDRESS
)) {
173 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
174 if (AsmReadCr4 () == (Cr4
| BIT3
)) {
178 if (NewDr7
== 0x20000480) {
181 } else if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
182 if (NewDr7
== BIT10
) {
188 // Restore original IDT entry for INT1 if it was hooked.
190 if (IdtEntryHooked
) {
191 RestoreIdtEntry1 (&IdtDescriptor
, &OriginalIdtEntry
);
195 // Restore the interrupt state
197 SetInterruptState (InterruptState
);
201 Performs additional actions after a PE/COFF image has been loaded and relocated.
203 @param ImageContext Pointer to the image context structure that describes the
204 PE/COFF image that has already been loaded and relocated.
209 PeCoffLoaderRelocateImageExtraAction (
210 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
213 PeCoffLoaderExtraActionCommon (ImageContext
, IMAGE_LOAD_SIGNATURE
);
217 Performs additional actions just before a PE/COFF image is unloaded. Any resources
218 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
220 @param ImageContext Pointer to the image context structure that describes the
221 PE/COFF image that is being unloaded.
226 PeCoffLoaderUnloadImageExtraAction (
227 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
230 PeCoffLoaderExtraActionCommon (ImageContext
, IMAGE_UNLOAD_SIGNATURE
);