1 /** @file
2 PE/Coff Extra Action library instances.
3
4 Copyright (c) 2010 - 2015, 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 <PeCoffExtraActionLib.h>
16
17 /**
18 Check if the hardware breakpoint in Drx is enabled by checking the Lx and Gx bit in Dr7.
19
20 It assumes that DebugAgent will set both Lx and Gx bit when setting up the hardware breakpoint.
21
22
23 @param RegisterIndex Index of Dr register. The value range is from 0 to 3.
24 @param Dr7 Value of Dr7 register.
25
26 @return TRUE The hardware breakpoint specified in the Drx is enabled.
27 @return FALSE The hardware breakpoint specified in the Drx is disabled.
28
29 **/
30 BOOLEAN
IsDrxEnabled(IN UINT8 RegisterIndex,IN UINTN Dr7)31 IsDrxEnabled (
32 IN UINT8 RegisterIndex,
33 IN UINTN Dr7
34 )
35 {
36 return (BOOLEAN) (((Dr7 >> (RegisterIndex * 2)) & (BIT0 | BIT1)) == (BIT0 | BIT1));
37 }
38
39 /**
40 Common routine to report the PE/COFF image loading/relocating or unloading event.
41
42 If ImageContext is NULL, then ASSERT().
43
44 @param ImageContext Pointer to the image context structure that describes the
45 PE/COFF image.
46 @param Signature IMAGE_LOAD_SIGNATURE or IMAGE_UNLOAD_SIGNATURE.
47
48 **/
49 VOID
PeCoffLoaderExtraActionCommon(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT * ImageContext,IN UINTN Signature)50 PeCoffLoaderExtraActionCommon (
51 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
52 IN UINTN Signature
53 )
54 {
55 BOOLEAN InterruptState;
56 UINTN Dr0;
57 UINTN Dr1;
58 UINTN Dr2;
59 UINTN Dr3;
60 UINTN Dr7;
61 UINTN Cr4;
62 UINTN NewDr7;
63 UINT8 LoadImageMethod;
64 UINT8 DebugAgentStatus;
65 IA32_DESCRIPTOR IdtDescriptor;
66 IA32_IDT_GATE_DESCRIPTOR OriginalIdtEntry;
67 BOOLEAN IdtEntryHooked;
68 UINT32 RegEdx;
69
70 ASSERT (ImageContext != NULL);
71
72 if (ImageContext->PdbPointer != NULL) {
73 DEBUG((EFI_D_ERROR, " PDB = %a\n", ImageContext->PdbPointer));
74 }
75
76 //
77 // Disable interrupts and save the current interrupt state
78 //
79 InterruptState = SaveAndDisableInterrupts ();
80
81 IdtEntryHooked = FALSE;
82 LoadImageMethod = PcdGet8 (PcdDebugLoadImageMethod);
83 if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT) {
84 //
85 // If the CPU does not support Debug Extensions(CPUID:01 EDX:BIT2)
86 // then force use of DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3
87 //
88 AsmCpuid (1, NULL, NULL, NULL, &RegEdx);
89 if ((RegEdx & BIT2) == 0) {
90 LoadImageMethod = DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3;
91 }
92 }
93 AsmReadIdtr (&IdtDescriptor);
94 if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {
95 if (!CheckDebugAgentHandler (&IdtDescriptor, SOFT_INT_VECTOR_NUM)) {
96 //
97 // Do not trigger INT3 if Debug Agent did not setup IDT entries.
98 //
99 return;
100 }
101 } else {
102 if (!CheckDebugAgentHandler (&IdtDescriptor, IO_HW_BREAKPOINT_VECTOR_NUM)) {
103 //
104 // Save and update IDT entry for INT1
105 //
106 SaveAndUpdateIdtEntry1 (&IdtDescriptor, &OriginalIdtEntry);
107 IdtEntryHooked = TRUE;
108 }
109 }
110
111 //
112 // Save Debug Register State
113 //
114 Dr0 = AsmReadDr0 ();
115 Dr1 = AsmReadDr1 ();
116 Dr2 = AsmReadDr2 ();
117 Dr3 = AsmReadDr3 ();
118 Dr7 = AsmReadDr7 () | BIT10; // H/w sets bit 10, some simulators don't
119 Cr4 = AsmReadCr4 ();
120
121 //
122 // DR0 = Signature
123 // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name
124 // DR2 = The pointer to the ImageContext structure
125 // DR3 = IO_PORT_BREAKPOINT_ADDRESS
126 // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte
127 // CR4 = Make sure DE(BIT3) is set
128 //
129 AsmWriteDr7 (BIT10);
130 AsmWriteDr0 (Signature);
131 AsmWriteDr1 ((UINTN) ImageContext->PdbPointer);
132 AsmWriteDr2 ((UINTN) ImageContext);
133 AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS);
134
135 if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT) {
136 AsmWriteDr7 (0x20000480);
137 AsmWriteCr4 (Cr4 | BIT3);
138 //
139 // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port
140 // returns a read value other than DEBUG_AGENT_IMAGE_WAIT
141 //
142 do {
143 DebugAgentStatus = IoRead8 (IO_PORT_BREAKPOINT_ADDRESS);
144 } while (DebugAgentStatus == DEBUG_AGENT_IMAGE_WAIT);
145
146 } else if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {
147 //
148 // Generate a software break point.
149 //
150 CpuBreakpoint ();
151 }
152
153 //
154 // Restore Debug Register State only when Host didn't change it inside exception handler.
155 // E.g.: User halts the target and sets the HW breakpoint while target is
156 // in the above exception handler
157 //
158 NewDr7 = AsmReadDr7 () | BIT10; // H/w sets bit 10, some simulators don't
159 if (!IsDrxEnabled (0, NewDr7) && (AsmReadDr0 () == 0 || AsmReadDr0 () == Signature)) {
160 //
161 // If user changed Dr3 (by setting HW bp in the above exception handler,
162 // we will not set Dr0 to 0 in GO/STEP handler because the break cause is not IMAGE_LOAD/_UNLOAD.
163 //
164 AsmWriteDr0 (Dr0);
165 }
166 if (!IsDrxEnabled (1, NewDr7) && (AsmReadDr1 () == (UINTN) ImageContext->PdbPointer)) {
167 AsmWriteDr1 (Dr1);
168 }
169 if (!IsDrxEnabled (2, NewDr7) && (AsmReadDr2 () == (UINTN) ImageContext)) {
170 AsmWriteDr2 (Dr2);
171 }
172 if (!IsDrxEnabled (3, NewDr7) && (AsmReadDr3 () == IO_PORT_BREAKPOINT_ADDRESS)) {
173 AsmWriteDr3 (Dr3);
174 }
175 if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT) {
176 if (AsmReadCr4 () == (Cr4 | BIT3)) {
177 AsmWriteCr4 (Cr4);
178 }
179 if (NewDr7 == 0x20000480) {
180 AsmWriteDr7 (Dr7);
181 }
182 } else if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {
183 if (NewDr7 == BIT10) {
184 AsmWriteDr7 (Dr7);
185 }
186 }
187 //
188 // Restore original IDT entry for INT1 if it was hooked.
189 //
190 if (IdtEntryHooked) {
191 RestoreIdtEntry1 (&IdtDescriptor, &OriginalIdtEntry);
192 }
193 //
194 // Restore the interrupt state
195 //
196 SetInterruptState (InterruptState);
197 }
198
199 /**
200 Performs additional actions after a PE/COFF image has been loaded and relocated.
201
202 @param ImageContext Pointer to the image context structure that describes the
203 PE/COFF image that has already been loaded and relocated.
204
205 **/
206 VOID
207 EFIAPI
PeCoffLoaderRelocateImageExtraAction(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT * ImageContext)208 PeCoffLoaderRelocateImageExtraAction (
209 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
210 )
211 {
212 PeCoffLoaderExtraActionCommon (ImageContext, IMAGE_LOAD_SIGNATURE);
213 }
214
215 /**
216 Performs additional actions just before a PE/COFF image is unloaded. Any resources
217 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
218
219 @param ImageContext Pointer to the image context structure that describes the
220 PE/COFF image that is being unloaded.
221
222 **/
223 VOID
224 EFIAPI
PeCoffLoaderUnloadImageExtraAction(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT * ImageContext)225 PeCoffLoaderUnloadImageExtraAction (
226 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
227 )
228 {
229 PeCoffLoaderExtraActionCommon (ImageContext, IMAGE_UNLOAD_SIGNATURE);
230 }
231