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