1 | /** @file
|
---|
2 | PE/Coff Extra Action library instances.
|
---|
3 |
|
---|
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.
|
---|
9 |
|
---|
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.
|
---|
12 |
|
---|
13 | **/
|
---|
14 |
|
---|
15 | #include <Base.h>
|
---|
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>
|
---|
21 |
|
---|
22 | #include <ImageDebugSupport.h>
|
---|
23 |
|
---|
24 | #define DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT 1
|
---|
25 | #define DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3 2
|
---|
26 |
|
---|
27 | /**
|
---|
28 | Check if the hardware breakpoint in Drx is enabled by checking the Lx and Gx bit in Dr7.
|
---|
29 |
|
---|
30 | It assumes that DebugAgent will set both Lx and Gx bit when setting up the hardware breakpoint.
|
---|
31 |
|
---|
32 |
|
---|
33 | @param RegisterIndex Index of Dr register. The value range is from 0 to 3.
|
---|
34 | @param Dr7 Value of Dr7 register.
|
---|
35 |
|
---|
36 | @return TRUE The hardware breakpoint specified in the Drx is enabled.
|
---|
37 | @return FALSE The hardware breakpoint specified in the Drx is disabled.
|
---|
38 |
|
---|
39 | **/
|
---|
40 | BOOLEAN
|
---|
41 | IsDrxEnabled (
|
---|
42 | IN UINT8 RegisterIndex,
|
---|
43 | IN UINTN Dr7
|
---|
44 | )
|
---|
45 | {
|
---|
46 | return (BOOLEAN) (((Dr7 >> (RegisterIndex * 2)) & (BIT0 | BIT1)) == (BIT0 | BIT1));
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | Performs additional actions after a PE/COFF image has been loaded and relocated.
|
---|
51 |
|
---|
52 | If ImageContext is NULL, then ASSERT().
|
---|
53 |
|
---|
54 | @param ImageContext Pointer to the image context structure that describes the
|
---|
55 | PE/COFF image that has already been loaded and relocated.
|
---|
56 |
|
---|
57 | **/
|
---|
58 | VOID
|
---|
59 | EFIAPI
|
---|
60 | PeCoffLoaderRelocateImageExtraAction (
|
---|
61 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
|
---|
62 | )
|
---|
63 | {
|
---|
64 | BOOLEAN InterruptState;
|
---|
65 | UINTN Dr0;
|
---|
66 | UINTN Dr1;
|
---|
67 | UINTN Dr2;
|
---|
68 | UINTN Dr3;
|
---|
69 | UINTN Dr7;
|
---|
70 | UINTN Cr4;
|
---|
71 | UINTN NewDr7;
|
---|
72 | UINT8 LoadImageMethod;
|
---|
73 | UINT8 DebugAgentStatus;
|
---|
74 |
|
---|
75 | ASSERT (ImageContext != NULL);
|
---|
76 |
|
---|
77 | if (ImageContext->PdbPointer != NULL) {
|
---|
78 | DEBUG((EFI_D_ERROR, " PDB = %a\n", ImageContext->PdbPointer));
|
---|
79 | }
|
---|
80 |
|
---|
81 | //
|
---|
82 | // Disable interrupts and save the current interrupt state
|
---|
83 | //
|
---|
84 | InterruptState = SaveAndDisableInterrupts ();
|
---|
85 |
|
---|
86 | //
|
---|
87 | // Save Debug Register State
|
---|
88 | //
|
---|
89 | Dr0 = AsmReadDr0 ();
|
---|
90 | Dr1 = AsmReadDr1 ();
|
---|
91 | Dr2 = AsmReadDr2 ();
|
---|
92 | Dr3 = AsmReadDr3 ();
|
---|
93 | Dr7 = AsmReadDr7 ();
|
---|
94 | Cr4 = AsmReadCr4 ();
|
---|
95 |
|
---|
96 | //
|
---|
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
|
---|
103 | //
|
---|
104 | AsmWriteDr7 (0);
|
---|
105 | AsmWriteDr0 (IMAGE_LOAD_SIGNATURE);
|
---|
106 | AsmWriteDr1 ((UINTN)ImageContext->PdbPointer);
|
---|
107 | AsmWriteDr2 ((UINTN)ImageContext);
|
---|
108 | AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS);
|
---|
109 |
|
---|
110 | LoadImageMethod = PcdGet8 (PcdDebugLoadImageMethod);
|
---|
111 | if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT) {
|
---|
112 | AsmWriteDr7 (0x20000480);
|
---|
113 | AsmWriteCr4 (Cr4 | BIT3);
|
---|
114 | //
|
---|
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
|
---|
117 | //
|
---|
118 | do {
|
---|
119 | DebugAgentStatus = IoRead8 (IO_PORT_BREAKPOINT_ADDRESS);
|
---|
120 | } while (DebugAgentStatus == DEBUG_AGENT_IMAGE_WAIT);
|
---|
121 |
|
---|
122 | } else if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {
|
---|
123 | //
|
---|
124 | // Generate a software break point.
|
---|
125 | //
|
---|
126 | CpuBreakpoint ();
|
---|
127 | }
|
---|
128 |
|
---|
129 | //
|
---|
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
|
---|
133 | //
|
---|
134 | NewDr7 = AsmReadDr7 ();
|
---|
135 | if (!IsDrxEnabled (0, NewDr7)) {
|
---|
136 | AsmWriteDr0 (Dr0);
|
---|
137 | }
|
---|
138 | if (!IsDrxEnabled (1, NewDr7)) {
|
---|
139 | AsmWriteDr1 (Dr1);
|
---|
140 | }
|
---|
141 | if (!IsDrxEnabled (2, NewDr7)) {
|
---|
142 | AsmWriteDr2 (Dr2);
|
---|
143 | }
|
---|
144 | if (!IsDrxEnabled (3, NewDr7)) {
|
---|
145 | AsmWriteDr3 (Dr3);
|
---|
146 | }
|
---|
147 | if (AsmReadCr4 () == (Cr4 | BIT3)) {
|
---|
148 | AsmWriteCr4 (Cr4);
|
---|
149 | }
|
---|
150 | if (NewDr7 == 0x20000480) {
|
---|
151 | AsmWriteDr7 (Dr7);
|
---|
152 | }
|
---|
153 | //
|
---|
154 | // Restore the interrupt state
|
---|
155 | //
|
---|
156 | SetInterruptState (InterruptState);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | Performs additional actions just before a PE/COFF image is unloaded. Any resources
|
---|
161 | that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
|
---|
162 |
|
---|
163 | If ImageContext is NULL, then ASSERT().
|
---|
164 |
|
---|
165 | @param ImageContext Pointer to the image context structure that describes the
|
---|
166 | PE/COFF image that is being unloaded.
|
---|
167 |
|
---|
168 | **/
|
---|
169 | VOID
|
---|
170 | EFIAPI
|
---|
171 | PeCoffLoaderUnloadImageExtraAction (
|
---|
172 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
|
---|
173 | )
|
---|
174 | {
|
---|
175 | BOOLEAN InterruptState;
|
---|
176 | UINTN Dr0;
|
---|
177 | UINTN Dr1;
|
---|
178 | UINTN Dr2;
|
---|
179 | UINTN Dr3;
|
---|
180 | UINTN Dr7;
|
---|
181 | UINTN Cr4;
|
---|
182 | UINTN NewDr7;
|
---|
183 | UINT8 LoadImageMethod;
|
---|
184 | UINT8 DebugAgentStatus;
|
---|
185 |
|
---|
186 | ASSERT (ImageContext != NULL);
|
---|
187 |
|
---|
188 | if (ImageContext->PdbPointer != NULL) {
|
---|
189 | DEBUG((EFI_D_ERROR, " PDB = %a\n", ImageContext->PdbPointer));
|
---|
190 | }
|
---|
191 |
|
---|
192 | //
|
---|
193 | // Disable interrupts and save the current interrupt state
|
---|
194 | //
|
---|
195 | InterruptState = SaveAndDisableInterrupts ();
|
---|
196 |
|
---|
197 | //
|
---|
198 | // Save Debug Register State
|
---|
199 | //
|
---|
200 | Dr0 = AsmReadDr0 ();
|
---|
201 | Dr1 = AsmReadDr1 ();
|
---|
202 | Dr2 = AsmReadDr2 ();
|
---|
203 | Dr3 = AsmReadDr3 ();
|
---|
204 | Dr7 = AsmReadDr7 ();
|
---|
205 | Cr4 = AsmReadCr4 ();
|
---|
206 |
|
---|
207 | //
|
---|
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
|
---|
214 | //
|
---|
215 | AsmWriteDr7 (0);
|
---|
216 | AsmWriteDr0 (IMAGE_UNLOAD_SIGNATURE);
|
---|
217 | AsmWriteDr1 ((UINTN)ImageContext->PdbPointer);
|
---|
218 | AsmWriteDr2 ((UINTN)ImageContext);
|
---|
219 | AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS);
|
---|
220 |
|
---|
221 | LoadImageMethod = PcdGet8 (PcdDebugLoadImageMethod);
|
---|
222 | if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT) {
|
---|
223 | AsmWriteDr7 (0x20000480);
|
---|
224 | AsmWriteCr4 (Cr4 | BIT3);
|
---|
225 | //
|
---|
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
|
---|
228 | //
|
---|
229 | do {
|
---|
230 | DebugAgentStatus = IoRead8 (IO_PORT_BREAKPOINT_ADDRESS);
|
---|
231 | } while (DebugAgentStatus == DEBUG_AGENT_IMAGE_WAIT);
|
---|
232 |
|
---|
233 | } else if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {
|
---|
234 | //
|
---|
235 | // Generate a software break point.
|
---|
236 | //
|
---|
237 | CpuBreakpoint ();
|
---|
238 | }
|
---|
239 |
|
---|
240 | //
|
---|
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
|
---|
244 | //
|
---|
245 | NewDr7 = AsmReadDr7 ();
|
---|
246 | if (!IsDrxEnabled (0, NewDr7)) {
|
---|
247 | AsmWriteDr0 (Dr0);
|
---|
248 | }
|
---|
249 | if (!IsDrxEnabled (1, NewDr7)) {
|
---|
250 | AsmWriteDr1 (Dr1);
|
---|
251 | }
|
---|
252 | if (!IsDrxEnabled (2, NewDr7)) {
|
---|
253 | AsmWriteDr2 (Dr2);
|
---|
254 | }
|
---|
255 | if (!IsDrxEnabled (3, NewDr7)) {
|
---|
256 | AsmWriteDr3 (Dr3);
|
---|
257 | }
|
---|
258 | if (AsmReadCr4 () == (Cr4 | BIT3)) {
|
---|
259 | AsmWriteCr4 (Cr4);
|
---|
260 | }
|
---|
261 | if (NewDr7 == 0x20000480) {
|
---|
262 | AsmWriteDr7 (Dr7);
|
---|
263 | }
|
---|
264 |
|
---|
265 | //
|
---|
266 | // Restore the interrupt state
|
---|
267 | //
|
---|
268 | SetInterruptState (InterruptState);
|
---|
269 | }
|
---|