2 PE/Coff Extra Action library instances.
4 Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
5 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.
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.
15 #include <PeCoffExtraActionLib.h>
18 Check if the hardware breakpoint in Drx is enabled by checking the Lx and Gx bit in Dr7.
20 It assumes that DebugAgent will set both Lx and Gx bit when setting up the hardware breakpoint.
23 @param RegisterIndex Index of Dr register. The value range is from 0 to 3.
24 @param Dr7 Value of Dr7 register.
26 @return TRUE The hardware breakpoint specified in the Drx is enabled.
27 @return FALSE The hardware breakpoint specified in the Drx is disabled.
32 IN UINT8 RegisterIndex
,
36 return (BOOLEAN
) (((Dr7
>> (RegisterIndex
* 2)) & (BIT0
| BIT1
)) == (BIT0
| BIT1
));
40 Common routine to report the PE/COFF image loading/relocating or unloading event.
42 If ImageContext is NULL, then ASSERT().
44 @param ImageContext Pointer to the image context structure that describes the
46 @param Signature IMAGE_LOAD_SIGNATURE or IMAGE_UNLOAD_SIGNATURE.
50 PeCoffLoaderExtraActionCommon (
51 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
,
55 BOOLEAN InterruptState
;
63 UINT8 LoadImageMethod
;
64 UINT8 DebugAgentStatus
;
65 IA32_DESCRIPTOR IdtDescriptor
;
66 IA32_IDT_GATE_DESCRIPTOR OriginalIdtEntry
;
67 BOOLEAN IdtEntryHooked
;
69 ASSERT (ImageContext
!= NULL
);
71 if (ImageContext
->PdbPointer
!= NULL
) {
72 DEBUG((EFI_D_ERROR
, " PDB = %a\n", ImageContext
->PdbPointer
));
76 // Disable interrupts and save the current interrupt state
78 InterruptState
= SaveAndDisableInterrupts ();
80 IdtEntryHooked
= FALSE
;
81 LoadImageMethod
= PcdGet8 (PcdDebugLoadImageMethod
);
82 AsmReadIdtr (&IdtDescriptor
);
83 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
84 if (!CheckDebugAgentHandler (&IdtDescriptor
, SOFT_INT_VECTOR_NUM
)) {
86 // Do not trigger INT3 if Debug Agent did not setup IDT entries.
91 if (!CheckDebugAgentHandler (&IdtDescriptor
, IO_HW_BREAKPOINT_VECTOR_NUM
)) {
93 // Save and update IDT entry for INT1
95 SaveAndUpdateIdtEntry1 (&IdtDescriptor
, &OriginalIdtEntry
);
96 IdtEntryHooked
= TRUE
;
101 // Save Debug Register State
107 Dr7
= AsmReadDr7 () | BIT10
; // H/w sets bit 10, some simulators don't
112 // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name
113 // DR2 = The pointer to the ImageContext structure
114 // DR3 = IO_PORT_BREAKPOINT_ADDRESS
115 // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte
116 // CR4 = Make sure DE(BIT3) is set
119 AsmWriteDr0 (Signature
);
120 AsmWriteDr1 ((UINTN
) ImageContext
->PdbPointer
);
121 AsmWriteDr2 ((UINTN
) ImageContext
);
122 AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS
);
124 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
125 AsmWriteDr7 (0x20000480);
126 AsmWriteCr4 (Cr4
| BIT3
);
128 // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port
129 // returns a read value other than DEBUG_AGENT_IMAGE_WAIT
132 DebugAgentStatus
= IoRead8 (IO_PORT_BREAKPOINT_ADDRESS
);
133 } while (DebugAgentStatus
== DEBUG_AGENT_IMAGE_WAIT
);
135 } else if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
137 // Generate a software break point.
143 // Restore Debug Register State only when Host didn't change it inside exception handler.
144 // E.g.: User halts the target and sets the HW breakpoint while target is
145 // in the above exception handler
147 NewDr7
= AsmReadDr7 () | BIT10
; // H/w sets bit 10, some simulators don't
148 if (!IsDrxEnabled (0, NewDr7
) && (AsmReadDr0 () == 0 || AsmReadDr0 () == Signature
)) {
150 // If user changed Dr3 (by setting HW bp in the above exception handler,
151 // we will not set Dr0 to 0 in GO/STEP handler because the break cause is not IMAGE_LOAD/_UNLOAD.
155 if (!IsDrxEnabled (1, NewDr7
) && (AsmReadDr1 () == (UINTN
) ImageContext
->PdbPointer
)) {
158 if (!IsDrxEnabled (2, NewDr7
) && (AsmReadDr2 () == (UINTN
) ImageContext
)) {
161 if (!IsDrxEnabled (3, NewDr7
) && (AsmReadDr3 () == IO_PORT_BREAKPOINT_ADDRESS
)) {
164 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
165 if (AsmReadCr4 () == (Cr4
| BIT3
)) {
168 if (NewDr7
== 0x20000480) {
171 } else if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
172 if (NewDr7
== BIT10
) {
177 // Restore original IDT entry for INT1 if it was hooked.
179 if (IdtEntryHooked
) {
180 RestoreIdtEntry1 (&IdtDescriptor
, &OriginalIdtEntry
);
183 // Restore the interrupt state
185 SetInterruptState (InterruptState
);
189 Performs additional actions after a PE/COFF image has been loaded and relocated.
191 @param ImageContext Pointer to the image context structure that describes the
192 PE/COFF image that has already been loaded and relocated.
197 PeCoffLoaderRelocateImageExtraAction (
198 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
201 PeCoffLoaderExtraActionCommon (ImageContext
, IMAGE_LOAD_SIGNATURE
);
205 Performs additional actions just before a PE/COFF image is unloaded. Any resources
206 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
208 @param ImageContext Pointer to the image context structure that describes the
209 PE/COFF image that is being unloaded.
214 PeCoffLoaderUnloadImageExtraAction (
215 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
218 PeCoffLoaderExtraActionCommon (ImageContext
, IMAGE_UNLOAD_SIGNATURE
);