2 PE/Coff Extra Action library instances.
4 Copyright (c) 2010, 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.
16 #include <Library/PeCoffExtraActionLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/IoLib.h>
20 #include <Library/PcdLib.h>
22 #include <ImageDebugSupport.h>
24 #define DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT 1
25 #define DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3 2
28 Check if the hardware breakpoint in Drx is enabled by checking the Lx and Gx bit in Dr7.
30 It assumes that DebugAgent will set both Lx and Gx bit when setting up the hardware breakpoint.
33 @param RegisterIndex Index of Dr register. The value range is from 0 to 3.
34 @param Dr7 Value of Dr7 register.
36 @return TRUE The hardware breakpoint specified in the Drx is enabled.
37 @return FALSE The hardware breakpoint specified in the Drx is disabled.
42 IN UINT8 RegisterIndex
,
46 return (BOOLEAN
) (((Dr7
>> (RegisterIndex
* 2)) & (BIT0
| BIT1
)) == (BIT0
| BIT1
));
50 Performs additional actions after a PE/COFF image has been loaded and relocated.
52 If ImageContext is NULL, then ASSERT().
54 @param ImageContext Pointer to the image context structure that describes the
55 PE/COFF image that has already been loaded and relocated.
60 PeCoffLoaderRelocateImageExtraAction (
61 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
64 BOOLEAN InterruptState
;
72 UINT8 LoadImageMethod
;
73 UINT8 DebugAgentStatus
;
75 ASSERT (ImageContext
!= NULL
);
77 if (ImageContext
->PdbPointer
!= NULL
) {
78 DEBUG((EFI_D_ERROR
, " PDB = %a\n", ImageContext
->PdbPointer
));
82 // Disable interrupts and save the current interrupt state
84 InterruptState
= SaveAndDisableInterrupts ();
87 // Save Debug Register State
97 // DR0 = IMAGE_LOAD_SIGNATURE
98 // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name
99 // DR2 = The pointer to the ImageContext structure
100 // DR3 = IO_PORT_BREAKPOINT_ADDRESS
101 // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte
102 // CR4 = Make sure DE(BIT3) is set
105 AsmWriteDr0 (IMAGE_LOAD_SIGNATURE
);
106 AsmWriteDr1 ((UINTN
)ImageContext
->PdbPointer
);
107 AsmWriteDr2 ((UINTN
)ImageContext
);
108 AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS
);
110 LoadImageMethod
= PcdGet8 (PcdDebugLoadImageMethod
);
111 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
112 AsmWriteDr7 (0x20000480);
113 AsmWriteCr4 (Cr4
| BIT3
);
115 // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port
116 // returns a read value other than DEBUG_AGENT_IMAGE_WAIT
119 DebugAgentStatus
= IoRead8 (IO_PORT_BREAKPOINT_ADDRESS
);
120 } while (DebugAgentStatus
== DEBUG_AGENT_IMAGE_WAIT
);
122 } else if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
124 // Generate a software break point.
130 // Restore Debug Register State only when Host didn't change it inside exception handler.
131 // E.g.: User halts the target and sets the HW breakpoint while target is
132 // in the above exception handler
134 NewDr7
= AsmReadDr7 ();
135 if (!IsDrxEnabled (0, NewDr7
)) {
138 if (!IsDrxEnabled (1, NewDr7
)) {
141 if (!IsDrxEnabled (2, NewDr7
)) {
144 if (!IsDrxEnabled (3, NewDr7
)) {
147 if (AsmReadCr4 () == (Cr4
| BIT3
)) {
150 if (NewDr7
== 0x20000480) {
154 // Restore the interrupt state
156 SetInterruptState (InterruptState
);
160 Performs additional actions just before a PE/COFF image is unloaded. Any resources
161 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
163 If ImageContext is NULL, then ASSERT().
165 @param ImageContext Pointer to the image context structure that describes the
166 PE/COFF image that is being unloaded.
171 PeCoffLoaderUnloadImageExtraAction (
172 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
175 BOOLEAN InterruptState
;
183 UINT8 LoadImageMethod
;
184 UINT8 DebugAgentStatus
;
186 ASSERT (ImageContext
!= NULL
);
188 if (ImageContext
->PdbPointer
!= NULL
) {
189 DEBUG((EFI_D_ERROR
, " PDB = %a\n", ImageContext
->PdbPointer
));
193 // Disable interrupts and save the current interrupt state
195 InterruptState
= SaveAndDisableInterrupts ();
198 // Save Debug Register State
208 // DR0 = IMAGE_UNLOAD_SIGNATURE
209 // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name
210 // DR2 = The pointer to the ImageContext structure
211 // DR3 = IO_PORT_BREAKPOINT_ADDRESS
212 // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte
213 // CR4 = Make sure DE(BIT3) is set
216 AsmWriteDr0 (IMAGE_UNLOAD_SIGNATURE
);
217 AsmWriteDr1 ((UINTN
)ImageContext
->PdbPointer
);
218 AsmWriteDr2 ((UINTN
)ImageContext
);
219 AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS
);
221 LoadImageMethod
= PcdGet8 (PcdDebugLoadImageMethod
);
222 if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT
) {
223 AsmWriteDr7 (0x20000480);
224 AsmWriteCr4 (Cr4
| BIT3
);
226 // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port
227 // returns a read value other than DEBUG_AGENT_IMAGE_WAIT
230 DebugAgentStatus
= IoRead8 (IO_PORT_BREAKPOINT_ADDRESS
);
231 } while (DebugAgentStatus
== DEBUG_AGENT_IMAGE_WAIT
);
233 } else if (LoadImageMethod
== DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
) {
235 // Generate a software break point.
241 // Restore Debug Register State only when Host didn't change it inside exception handler.
242 // E.g.: User halts the target and sets the HW breakpoint while target is
243 // in the above exception handler
245 NewDr7
= AsmReadDr7 ();
246 if (!IsDrxEnabled (0, NewDr7
)) {
249 if (!IsDrxEnabled (1, NewDr7
)) {
252 if (!IsDrxEnabled (2, NewDr7
)) {
255 if (!IsDrxEnabled (3, NewDr7
)) {
258 if (AsmReadCr4 () == (Cr4
| BIT3
)) {
261 if (NewDr7
== 0x20000480) {
266 // Restore the interrupt state
268 SetInterruptState (InterruptState
);