]>
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 | |
8cc26df4 JF |
83 | if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {\r |
84 | if (!CheckDebugAgentHandler (&IdtDescriptor, SOFT_INT_VECTOR_NUM)) {\r | |
b422b62c | 85 | //\r |
86 | // Do not trigger INT3 if Debug Agent did not setup IDT entries.\r | |
87 | //\r | |
88 | return;\r | |
89 | }\r | |
8cc26df4 JF |
90 | } else {\r |
91 | if (!CheckDebugAgentHandler (&IdtDescriptor, IO_HW_BREAKPOINT_VECTOR_NUM)) {\r | |
92 | //\r | |
93 | // Save and update IDT entry for INT1\r | |
94 | //\r | |
95 | SaveAndUpdateIdtEntry1 (&IdtDescriptor, &OriginalIdtEntry);\r | |
96 | IdtEntryHooked = TRUE;\r | |
97 | }\r | |
b422b62c | 98 | }\r |
99 | \r | |
18b144ea | 100 | //\r |
101 | // Save Debug Register State\r | |
102 | //\r | |
103 | Dr0 = AsmReadDr0 ();\r | |
104 | Dr1 = AsmReadDr1 ();\r | |
105 | Dr2 = AsmReadDr2 ();\r | |
106 | Dr3 = AsmReadDr3 ();\r | |
107 | Dr7 = AsmReadDr7 ();\r | |
108 | Cr4 = AsmReadCr4 ();\r | |
109 | \r | |
110 | //\r | |
93c0bdec | 111 | // DR0 = Signature\r |
18b144ea | 112 | // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name\r |
113 | // DR2 = The pointer to the ImageContext structure\r | |
114 | // DR3 = IO_PORT_BREAKPOINT_ADDRESS\r | |
115 | // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte\r | |
116 | // CR4 = Make sure DE(BIT3) is set\r | |
117 | //\r | |
118 | AsmWriteDr7 (0);\r | |
93c0bdec | 119 | AsmWriteDr0 (Signature);\r |
120 | AsmWriteDr1 ((UINTN) ImageContext->PdbPointer);\r | |
121 | AsmWriteDr2 ((UINTN) ImageContext);\r | |
18b144ea | 122 | AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS);\r |
123 | \r | |
18b144ea | 124 | if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT) {\r |
125 | AsmWriteDr7 (0x20000480);\r | |
126 | AsmWriteCr4 (Cr4 | BIT3);\r | |
127 | //\r | |
128 | // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port\r | |
129 | // returns a read value other than DEBUG_AGENT_IMAGE_WAIT\r | |
130 | //\r | |
131 | do {\r | |
132 | DebugAgentStatus = IoRead8 (IO_PORT_BREAKPOINT_ADDRESS);\r | |
133 | } while (DebugAgentStatus == DEBUG_AGENT_IMAGE_WAIT);\r | |
134 | \r | |
135 | } else if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {\r | |
136 | //\r | |
137 | // Generate a software break point.\r | |
138 | //\r | |
139 | CpuBreakpoint ();\r | |
140 | }\r | |
141 | \r | |
142 | //\r | |
143 | // Restore Debug Register State only when Host didn't change it inside exception handler.\r | |
144 | // E.g.: User halts the target and sets the HW breakpoint while target is \r | |
145 | // in the above exception handler\r | |
146 | //\r | |
147 | NewDr7 = AsmReadDr7 ();\r | |
93c0bdec | 148 | if (!IsDrxEnabled (0, NewDr7) && (AsmReadDr0 () == 0 || AsmReadDr0 () == Signature)) {\r |
149 | //\r | |
150 | // If user changed Dr3 (by setting HW bp in the above exception handler,\r | |
151 | // we will not set Dr0 to 0 in GO/STEP handler because the break cause is not IMAGE_LOAD/_UNLOAD.\r | |
152 | //\r | |
18b144ea | 153 | AsmWriteDr0 (Dr0);\r |
154 | }\r | |
93c0bdec | 155 | if (!IsDrxEnabled (1, NewDr7) && (AsmReadDr1 () == (UINTN) ImageContext->PdbPointer)) {\r |
18b144ea | 156 | AsmWriteDr1 (Dr1);\r |
157 | }\r | |
93c0bdec | 158 | if (!IsDrxEnabled (2, NewDr7) && (AsmReadDr2 () == (UINTN) ImageContext)) {\r |
18b144ea | 159 | AsmWriteDr2 (Dr2);\r |
160 | }\r | |
93c0bdec | 161 | if (!IsDrxEnabled (3, NewDr7) && (AsmReadDr3 () == IO_PORT_BREAKPOINT_ADDRESS)) {\r |
18b144ea | 162 | AsmWriteDr3 (Dr3);\r |
163 | }\r | |
164 | if (AsmReadCr4 () == (Cr4 | BIT3)) {\r | |
165 | AsmWriteCr4 (Cr4);\r | |
166 | }\r | |
167 | if (NewDr7 == 0x20000480) {\r | |
168 | AsmWriteDr7 (Dr7);\r | |
169 | }\r | |
170 | //\r | |
b422b62c | 171 | // Restore original IDT entry for INT1 if it was hooked.\r |
172 | //\r | |
173 | if (IdtEntryHooked) {\r | |
174 | RestoreIdtEntry1 (&IdtDescriptor, &OriginalIdtEntry);\r | |
175 | }\r | |
176 | //\r | |
18b144ea | 177 | // Restore the interrupt state\r |
178 | //\r | |
179 | SetInterruptState (InterruptState);\r | |
180 | }\r | |
181 | \r | |
93c0bdec | 182 | /**\r |
183 | Performs additional actions after a PE/COFF image has been loaded and relocated.\r | |
184 | \r | |
185 | @param ImageContext Pointer to the image context structure that describes the\r | |
186 | PE/COFF image that has already been loaded and relocated.\r | |
187 | \r | |
188 | **/\r | |
189 | VOID\r | |
190 | EFIAPI\r | |
191 | PeCoffLoaderRelocateImageExtraAction (\r | |
192 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
193 | )\r | |
194 | {\r | |
195 | PeCoffLoaderExtraActionCommon (ImageContext, IMAGE_LOAD_SIGNATURE);\r | |
196 | }\r | |
197 | \r | |
18b144ea | 198 | /**\r |
199 | Performs additional actions just before a PE/COFF image is unloaded. Any resources\r | |
200 | that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.\r | |
201 | \r | |
18b144ea | 202 | @param ImageContext Pointer to the image context structure that describes the\r |
203 | PE/COFF image that is being unloaded.\r | |
204 | \r | |
205 | **/\r | |
206 | VOID\r | |
207 | EFIAPI\r | |
208 | PeCoffLoaderUnloadImageExtraAction (\r | |
209 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
210 | )\r | |
211 | {\r | |
93c0bdec | 212 | PeCoffLoaderExtraActionCommon (ImageContext, IMAGE_UNLOAD_SIGNATURE);\r |
18b144ea | 213 | }\r |