]>
Commit | Line | Data |
---|---|---|
18b144ea | 1 | /** @file\r |
2 | PE/Coff Extra Action library instances.\r | |
3 | \r | |
b422b62c | 4 | Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>\r |
18b144ea | 5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
b422b62c | 15 | #include <PeCoffExtraActionLib.h>\r |
18b144ea | 16 | \r |
17 | /**\r | |
18 | Check if the hardware breakpoint in Drx is enabled by checking the Lx and Gx bit in Dr7.\r | |
19 | \r | |
20 | It assumes that DebugAgent will set both Lx and Gx bit when setting up the hardware breakpoint.\r | |
21 | \r | |
22 | \r | |
23 | @param RegisterIndex Index of Dr register. The value range is from 0 to 3.\r | |
24 | @param Dr7 Value of Dr7 register.\r | |
25 | \r | |
26 | @return TRUE The hardware breakpoint specified in the Drx is enabled.\r | |
27 | @return FALSE The hardware breakpoint specified in the Drx is disabled.\r | |
28 | \r | |
29 | **/\r | |
30 | BOOLEAN\r | |
31 | IsDrxEnabled (\r | |
32 | IN UINT8 RegisterIndex,\r | |
33 | IN UINTN Dr7\r | |
34 | )\r | |
35 | {\r | |
36 | return (BOOLEAN) (((Dr7 >> (RegisterIndex * 2)) & (BIT0 | BIT1)) == (BIT0 | BIT1));\r | |
37 | }\r | |
38 | \r | |
39 | /**\r | |
93c0bdec | 40 | Common routine to report the PE/COFF image loading/relocating or unloading event.\r |
18b144ea | 41 | \r |
42 | If ImageContext is NULL, then ASSERT().\r | |
93c0bdec | 43 | \r |
18b144ea | 44 | @param ImageContext Pointer to the image context structure that describes the\r |
93c0bdec | 45 | PE/COFF image.\r |
46 | @param Signature IMAGE_LOAD_SIGNATURE or IMAGE_UNLOAD_SIGNATURE.\r | |
18b144ea | 47 | \r |
48 | **/\r | |
49 | VOID\r | |
93c0bdec | 50 | PeCoffLoaderExtraActionCommon (\r |
51 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r | |
52 | IN UINTN Signature\r | |
18b144ea | 53 | )\r |
54 | {\r | |
b422b62c | 55 | BOOLEAN InterruptState;\r |
56 | UINTN Dr0;\r | |
57 | UINTN Dr1;\r | |
58 | UINTN Dr2;\r | |
59 | UINTN Dr3;\r | |
60 | UINTN Dr7;\r | |
61 | UINTN Cr4;\r | |
62 | UINTN NewDr7;\r | |
63 | UINT8 LoadImageMethod;\r | |
64 | UINT8 DebugAgentStatus;\r | |
65 | IA32_DESCRIPTOR IdtDescriptor;\r | |
66 | IA32_IDT_GATE_DESCRIPTOR OriginalIdtEntry;\r | |
67 | BOOLEAN IdtEntryHooked;\r | |
18b144ea | 68 | \r |
69 | ASSERT (ImageContext != NULL);\r | |
70 | \r | |
71 | if (ImageContext->PdbPointer != NULL) {\r | |
72 | DEBUG((EFI_D_ERROR, " PDB = %a\n", ImageContext->PdbPointer));\r | |
73 | }\r | |
74 | \r | |
75 | //\r | |
76 | // Disable interrupts and save the current interrupt state\r | |
77 | //\r | |
78 | InterruptState = SaveAndDisableInterrupts ();\r | |
79 | \r | |
b422b62c | 80 | IdtEntryHooked = FALSE;\r |
81 | LoadImageMethod = PcdGet8 (PcdDebugLoadImageMethod);\r | |
82 | AsmReadIdtr (&IdtDescriptor);\r | |
83 | if (!CheckDebugAgentHandler (&IdtDescriptor)) {\r | |
84 | if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {\r | |
85 | //\r | |
86 | // Do not trigger INT3 if Debug Agent did not setup IDT entries.\r | |
87 | //\r | |
88 | return;\r | |
89 | }\r | |
90 | //\r | |
91 | // Save and update IDT entry for INT1\r | |
92 | //\r | |
93 | SaveAndUpdateIdtEntry1 (&IdtDescriptor, &OriginalIdtEntry);\r | |
94 | IdtEntryHooked = TRUE;\r | |
95 | }\r | |
96 | \r | |
18b144ea | 97 | //\r |
98 | // Save Debug Register State\r | |
99 | //\r | |
100 | Dr0 = AsmReadDr0 ();\r | |
101 | Dr1 = AsmReadDr1 ();\r | |
102 | Dr2 = AsmReadDr2 ();\r | |
103 | Dr3 = AsmReadDr3 ();\r | |
104 | Dr7 = AsmReadDr7 ();\r | |
105 | Cr4 = AsmReadCr4 ();\r | |
106 | \r | |
107 | //\r | |
93c0bdec | 108 | // DR0 = Signature\r |
18b144ea | 109 | // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name\r |
110 | // DR2 = The pointer to the ImageContext structure\r | |
111 | // DR3 = IO_PORT_BREAKPOINT_ADDRESS\r | |
112 | // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte\r | |
113 | // CR4 = Make sure DE(BIT3) is set\r | |
114 | //\r | |
115 | AsmWriteDr7 (0);\r | |
93c0bdec | 116 | AsmWriteDr0 (Signature);\r |
117 | AsmWriteDr1 ((UINTN) ImageContext->PdbPointer);\r | |
118 | AsmWriteDr2 ((UINTN) ImageContext);\r | |
18b144ea | 119 | AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS);\r |
120 | \r | |
18b144ea | 121 | if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT) {\r |
122 | AsmWriteDr7 (0x20000480);\r | |
123 | AsmWriteCr4 (Cr4 | BIT3);\r | |
124 | //\r | |
125 | // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port\r | |
126 | // returns a read value other than DEBUG_AGENT_IMAGE_WAIT\r | |
127 | //\r | |
128 | do {\r | |
129 | DebugAgentStatus = IoRead8 (IO_PORT_BREAKPOINT_ADDRESS);\r | |
130 | } while (DebugAgentStatus == DEBUG_AGENT_IMAGE_WAIT);\r | |
131 | \r | |
132 | } else if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {\r | |
133 | //\r | |
134 | // Generate a software break point.\r | |
135 | //\r | |
136 | CpuBreakpoint ();\r | |
137 | }\r | |
138 | \r | |
139 | //\r | |
140 | // Restore Debug Register State only when Host didn't change it inside exception handler.\r | |
141 | // E.g.: User halts the target and sets the HW breakpoint while target is \r | |
142 | // in the above exception handler\r | |
143 | //\r | |
144 | NewDr7 = AsmReadDr7 ();\r | |
93c0bdec | 145 | if (!IsDrxEnabled (0, NewDr7) && (AsmReadDr0 () == 0 || AsmReadDr0 () == Signature)) {\r |
146 | //\r | |
147 | // If user changed Dr3 (by setting HW bp in the above exception handler,\r | |
148 | // we will not set Dr0 to 0 in GO/STEP handler because the break cause is not IMAGE_LOAD/_UNLOAD.\r | |
149 | //\r | |
18b144ea | 150 | AsmWriteDr0 (Dr0);\r |
151 | }\r | |
93c0bdec | 152 | if (!IsDrxEnabled (1, NewDr7) && (AsmReadDr1 () == (UINTN) ImageContext->PdbPointer)) {\r |
18b144ea | 153 | AsmWriteDr1 (Dr1);\r |
154 | }\r | |
93c0bdec | 155 | if (!IsDrxEnabled (2, NewDr7) && (AsmReadDr2 () == (UINTN) ImageContext)) {\r |
18b144ea | 156 | AsmWriteDr2 (Dr2);\r |
157 | }\r | |
93c0bdec | 158 | if (!IsDrxEnabled (3, NewDr7) && (AsmReadDr3 () == IO_PORT_BREAKPOINT_ADDRESS)) {\r |
18b144ea | 159 | AsmWriteDr3 (Dr3);\r |
160 | }\r | |
161 | if (AsmReadCr4 () == (Cr4 | BIT3)) {\r | |
162 | AsmWriteCr4 (Cr4);\r | |
163 | }\r | |
164 | if (NewDr7 == 0x20000480) {\r | |
165 | AsmWriteDr7 (Dr7);\r | |
166 | }\r | |
167 | //\r | |
b422b62c | 168 | // Restore original IDT entry for INT1 if it was hooked.\r |
169 | //\r | |
170 | if (IdtEntryHooked) {\r | |
171 | RestoreIdtEntry1 (&IdtDescriptor, &OriginalIdtEntry);\r | |
172 | }\r | |
173 | //\r | |
18b144ea | 174 | // Restore the interrupt state\r |
175 | //\r | |
176 | SetInterruptState (InterruptState);\r | |
177 | }\r | |
178 | \r | |
93c0bdec | 179 | /**\r |
180 | Performs additional actions after a PE/COFF image has been loaded and relocated.\r | |
181 | \r | |
182 | @param ImageContext Pointer to the image context structure that describes the\r | |
183 | PE/COFF image that has already been loaded and relocated.\r | |
184 | \r | |
185 | **/\r | |
186 | VOID\r | |
187 | EFIAPI\r | |
188 | PeCoffLoaderRelocateImageExtraAction (\r | |
189 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
190 | )\r | |
191 | {\r | |
192 | PeCoffLoaderExtraActionCommon (ImageContext, IMAGE_LOAD_SIGNATURE);\r | |
193 | }\r | |
194 | \r | |
18b144ea | 195 | /**\r |
196 | Performs additional actions just before a PE/COFF image is unloaded. Any resources\r | |
197 | that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.\r | |
198 | \r | |
18b144ea | 199 | @param ImageContext Pointer to the image context structure that describes the\r |
200 | PE/COFF image that is being unloaded.\r | |
201 | \r | |
202 | **/\r | |
203 | VOID\r | |
204 | EFIAPI\r | |
205 | PeCoffLoaderUnloadImageExtraAction (\r | |
206 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
207 | )\r | |
208 | {\r | |
93c0bdec | 209 | PeCoffLoaderExtraActionCommon (ImageContext, IMAGE_UNLOAD_SIGNATURE);\r |
18b144ea | 210 | }\r |