]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLib.c
This revision can only work with Intel(c) UDK Debugger Tool version 1.2 or greater...
[mirror_edk2.git] / SourceLevelDebugPkg / Library / PeCoffExtraActionLibDebug / PeCoffExtraActionLib.c
1 /** @file
2 PE/Coff Extra Action library instances.
3
4 Copyright (c) 2010 - 2012, 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 Common routine to report the PE/COFF image loading/relocating or unloading event.
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.
56 @param Signature IMAGE_LOAD_SIGNATURE or IMAGE_UNLOAD_SIGNATURE.
57
58 **/
59 VOID
60 PeCoffLoaderExtraActionCommon (
61 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,
62 IN UINTN Signature
63 )
64 {
65 BOOLEAN InterruptState;
66 UINTN Dr0;
67 UINTN Dr1;
68 UINTN Dr2;
69 UINTN Dr3;
70 UINTN Dr7;
71 UINTN Cr4;
72 UINTN NewDr7;
73 UINT8 LoadImageMethod;
74 UINT8 DebugAgentStatus;
75
76 ASSERT (ImageContext != NULL);
77
78 if (ImageContext->PdbPointer != NULL) {
79 DEBUG((EFI_D_ERROR, " PDB = %a\n", ImageContext->PdbPointer));
80 }
81
82 //
83 // Disable interrupts and save the current interrupt state
84 //
85 InterruptState = SaveAndDisableInterrupts ();
86
87 //
88 // Save Debug Register State
89 //
90 Dr0 = AsmReadDr0 ();
91 Dr1 = AsmReadDr1 ();
92 Dr2 = AsmReadDr2 ();
93 Dr3 = AsmReadDr3 ();
94 Dr7 = AsmReadDr7 ();
95 Cr4 = AsmReadCr4 ();
96
97 //
98 // DR0 = Signature
99 // DR1 = The address of the Null-terminated ASCII string for the PE/COFF image's PDB file name
100 // DR2 = The pointer to the ImageContext structure
101 // DR3 = IO_PORT_BREAKPOINT_ADDRESS
102 // DR7 = Disables all HW breakpoints except for DR3 I/O port access of length 1 byte
103 // CR4 = Make sure DE(BIT3) is set
104 //
105 AsmWriteDr7 (0);
106 AsmWriteDr0 (Signature);
107 AsmWriteDr1 ((UINTN) ImageContext->PdbPointer);
108 AsmWriteDr2 ((UINTN) ImageContext);
109 AsmWriteDr3 (IO_PORT_BREAKPOINT_ADDRESS);
110
111 LoadImageMethod = PcdGet8 (PcdDebugLoadImageMethod);
112 if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_IO_HW_BREAKPOINT) {
113 AsmWriteDr7 (0x20000480);
114 AsmWriteCr4 (Cr4 | BIT3);
115 //
116 // Do an IN from IO_PORT_BREAKPOINT_ADDRESS to generate a HW breakpoint until the port
117 // returns a read value other than DEBUG_AGENT_IMAGE_WAIT
118 //
119 do {
120 DebugAgentStatus = IoRead8 (IO_PORT_BREAKPOINT_ADDRESS);
121 } while (DebugAgentStatus == DEBUG_AGENT_IMAGE_WAIT);
122
123 } else if (LoadImageMethod == DEBUG_LOAD_IMAGE_METHOD_SOFT_INT3) {
124 //
125 // Generate a software break point.
126 //
127 CpuBreakpoint ();
128 }
129
130 //
131 // Restore Debug Register State only when Host didn't change it inside exception handler.
132 // E.g.: User halts the target and sets the HW breakpoint while target is
133 // in the above exception handler
134 //
135 NewDr7 = AsmReadDr7 ();
136 if (!IsDrxEnabled (0, NewDr7) && (AsmReadDr0 () == 0 || AsmReadDr0 () == Signature)) {
137 //
138 // If user changed Dr3 (by setting HW bp in the above exception handler,
139 // we will not set Dr0 to 0 in GO/STEP handler because the break cause is not IMAGE_LOAD/_UNLOAD.
140 //
141 AsmWriteDr0 (Dr0);
142 }
143 if (!IsDrxEnabled (1, NewDr7) && (AsmReadDr1 () == (UINTN) ImageContext->PdbPointer)) {
144 AsmWriteDr1 (Dr1);
145 }
146 if (!IsDrxEnabled (2, NewDr7) && (AsmReadDr2 () == (UINTN) ImageContext)) {
147 AsmWriteDr2 (Dr2);
148 }
149 if (!IsDrxEnabled (3, NewDr7) && (AsmReadDr3 () == IO_PORT_BREAKPOINT_ADDRESS)) {
150 AsmWriteDr3 (Dr3);
151 }
152 if (AsmReadCr4 () == (Cr4 | BIT3)) {
153 AsmWriteCr4 (Cr4);
154 }
155 if (NewDr7 == 0x20000480) {
156 AsmWriteDr7 (Dr7);
157 }
158 //
159 // Restore the interrupt state
160 //
161 SetInterruptState (InterruptState);
162 }
163
164 /**
165 Performs additional actions after a PE/COFF image has been loaded and relocated.
166
167 @param ImageContext Pointer to the image context structure that describes the
168 PE/COFF image that has already been loaded and relocated.
169
170 **/
171 VOID
172 EFIAPI
173 PeCoffLoaderRelocateImageExtraAction (
174 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
175 )
176 {
177 PeCoffLoaderExtraActionCommon (ImageContext, IMAGE_LOAD_SIGNATURE);
178 }
179
180 /**
181 Performs additional actions just before a PE/COFF image is unloaded. Any resources
182 that were allocated by PeCoffLoaderRelocateImageExtraAction() must be freed.
183
184 @param ImageContext Pointer to the image context structure that describes the
185 PE/COFF image that is being unloaded.
186
187 **/
188 VOID
189 EFIAPI
190 PeCoffLoaderUnloadImageExtraAction (
191 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
192 )
193 {
194 PeCoffLoaderExtraActionCommon (ImageContext, IMAGE_UNLOAD_SIGNATURE);
195 }