]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/DebugSupportDxe/Ia32/PlDebugSupport.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / DebugSupportDxe / Ia32 / PlDebugSupport.c
1 /** @file
2 IA32/x64 generic functions to support Debug Support protocol.
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "DebugSupport.h"
10
11 //
12 // This the global main table to keep track of the interrupts
13 //
14 IDT_ENTRY *IdtEntryTable = NULL;
15
16 /**
17 Read IDT Gate Descriptor from IDT Table.
18
19 @param Vector Specifies vector number.
20 @param IdtGateDescriptor Pointer to IDT Gate Descriptor read from IDT Table.
21
22 **/
23 VOID
24 ReadIdtGateDescriptor (
25 IN EFI_EXCEPTION_TYPE Vector,
26 OUT IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor
27 )
28 {
29 IA32_DESCRIPTOR IdtrValue;
30 IA32_IDT_GATE_DESCRIPTOR *IdtTable;
31
32 AsmReadIdtr (&IdtrValue);
33 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtrValue.Base;
34
35 CopyMem ((VOID *)IdtGateDescriptor, (VOID *)&(IdtTable)[Vector], sizeof (IA32_IDT_GATE_DESCRIPTOR));
36 }
37
38 /**
39 Write IDT Gate Descriptor into IDT Table.
40
41 @param Vector Specifies vector number.
42 @param IdtGateDescriptor Pointer to IDT Gate Descriptor written into IDT Table.
43
44 **/
45 VOID
46 WriteIdtGateDescriptor (
47 EFI_EXCEPTION_TYPE Vector,
48 IA32_IDT_GATE_DESCRIPTOR *IdtGateDescriptor
49 )
50 {
51 IA32_DESCRIPTOR IdtrValue;
52 IA32_IDT_GATE_DESCRIPTOR *IdtTable;
53
54 AsmReadIdtr (&IdtrValue);
55 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtrValue.Base;
56
57 CopyMem ((VOID *)&(IdtTable)[Vector], (VOID *)IdtGateDescriptor, sizeof (IA32_IDT_GATE_DESCRIPTOR));
58 }
59
60 /**
61 Creates a nes entry stub. Then saves the current IDT entry and replaces it
62 with an interrupt gate for the new entry point. The IdtEntryTable is updated
63 with the new registered function.
64
65 This code executes in boot services context. The stub entry executes in interrupt
66 context.
67
68 @param ExceptionType Specifies which vector to hook.
69 @param NewCallback A pointer to the new function to be registered.
70
71 **/
72 VOID
73 HookEntry (
74 IN EFI_EXCEPTION_TYPE ExceptionType,
75 IN CALLBACK_FUNC NewCallback
76 )
77 {
78 BOOLEAN OldIntFlagState;
79
80 CreateEntryStub (ExceptionType, (VOID **)&IdtEntryTable[ExceptionType].StubEntry);
81
82 //
83 // Disables CPU interrupts and returns the previous interrupt state
84 //
85 OldIntFlagState = SaveAndDisableInterrupts ();
86
87 //
88 // gets IDT Gate descriptor by index
89 //
90 ReadIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
91 //
92 // stores orignal interrupt handle
93 //
94 IdtEntryTable[ExceptionType].OrigVector = (DEBUG_PROC)GetInterruptHandleFromIdt (&(IdtEntryTable[ExceptionType].OrigDesc));
95
96 //
97 // encodes new IDT Gate descriptor by stub entry
98 //
99 Vect2Desc (&IdtEntryTable[ExceptionType].NewDesc, IdtEntryTable[ExceptionType].StubEntry);
100 //
101 // stores NewCallback
102 //
103 IdtEntryTable[ExceptionType].RegisteredCallback = NewCallback;
104
105 //
106 // writes back new IDT Gate descriptor
107 //
108 WriteIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].NewDesc));
109
110 //
111 // restore interrupt state
112 //
113 SetInterruptState (OldIntFlagState);
114
115 return;
116 }
117
118 /**
119 Undoes HookEntry. This code executes in boot services context.
120
121 @param ExceptionType Specifies which entry to unhook
122
123 **/
124 VOID
125 UnhookEntry (
126 IN EFI_EXCEPTION_TYPE ExceptionType
127 )
128 {
129 BOOLEAN OldIntFlagState;
130
131 //
132 // Disables CPU interrupts and returns the previous interrupt state
133 //
134 OldIntFlagState = SaveAndDisableInterrupts ();
135
136 //
137 // restore the default IDT Date Descriptor
138 //
139 WriteIdtGateDescriptor (ExceptionType, &(IdtEntryTable[ExceptionType].OrigDesc));
140
141 //
142 // restore interrupt state
143 //
144 SetInterruptState (OldIntFlagState);
145
146 return;
147 }
148
149 /**
150 Returns the maximum value that may be used for the ProcessorIndex parameter in
151 RegisterPeriodicCallback() and RegisterExceptionCallback().
152
153 Hard coded to support only 1 processor for now.
154
155 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
156 @param MaxProcessorIndex Pointer to a caller-allocated UINTN in which the maximum supported
157 processor index is returned. Always 0 returned.
158
159 @retval EFI_SUCCESS Always returned with **MaxProcessorIndex set to 0.
160
161 **/
162 EFI_STATUS
163 EFIAPI
164 GetMaximumProcessorIndex (
165 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
166 OUT UINTN *MaxProcessorIndex
167 )
168 {
169 *MaxProcessorIndex = 0;
170 return EFI_SUCCESS;
171 }
172
173 /**
174 Registers a function to be called back periodically in interrupt context.
175
176 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
177 @param ProcessorIndex Specifies which processor the callback function applies to.
178 @param PeriodicCallback A pointer to a function of type PERIODIC_CALLBACK that is the main
179 periodic entry point of the debug agent.
180
181 @retval EFI_SUCCESS The function completed successfully.
182 @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback
183 function was previously registered.
184 @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback
185 function.
186 **/
187 EFI_STATUS
188 EFIAPI
189 RegisterPeriodicCallback (
190 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
191 IN UINTN ProcessorIndex,
192 IN EFI_PERIODIC_CALLBACK PeriodicCallback
193 )
194 {
195 return ManageIdtEntryTable (PeriodicCallback, SYSTEM_TIMER_VECTOR);
196 }
197
198 /**
199 Registers a function to be called when a given processor exception occurs.
200
201 This code executes in boot services context.
202
203 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
204 @param ProcessorIndex Specifies which processor the callback function applies to.
205 @param ExceptionCallback A pointer to a function of type EXCEPTION_CALLBACK that is called
206 when the processor exception specified by ExceptionType occurs.
207 @param ExceptionType Specifies which processor exception to hook.
208
209 @retval EFI_SUCCESS The function completed successfully.
210 @retval EFI_ALREADY_STARTED Non-NULL PeriodicCallback parameter when a callback
211 function was previously registered.
212 @retval EFI_OUT_OF_RESOURCES System has insufficient memory resources to register new callback
213 function.
214 **/
215 EFI_STATUS
216 EFIAPI
217 RegisterExceptionCallback (
218 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
219 IN UINTN ProcessorIndex,
220 IN EFI_EXCEPTION_CALLBACK ExceptionCallback,
221 IN EFI_EXCEPTION_TYPE ExceptionType
222 )
223 {
224 return ManageIdtEntryTable (ExceptionCallback, ExceptionType);
225 }
226
227 /**
228 Invalidates processor instruction cache for a memory range. Subsequent execution in this range
229 causes a fresh memory fetch to retrieve code to be executed.
230
231 @param This A pointer to the EFI_DEBUG_SUPPORT_PROTOCOL instance.
232 @param ProcessorIndex Specifies which processor's instruction cache is to be invalidated.
233 @param Start Specifies the physical base of the memory range to be invalidated.
234 @param Length Specifies the minimum number of bytes in the processor's instruction
235 cache to invalidate.
236
237 @retval EFI_SUCCESS Always returned.
238
239 **/
240 EFI_STATUS
241 EFIAPI
242 InvalidateInstructionCache (
243 IN EFI_DEBUG_SUPPORT_PROTOCOL *This,
244 IN UINTN ProcessorIndex,
245 IN VOID *Start,
246 IN UINT64 Length
247 )
248 {
249 AsmWbinvd ();
250 return EFI_SUCCESS;
251 }
252
253 /**
254 Common piece of code that invokes the registered handlers.
255
256 This code executes in exception context so no efi calls are allowed.
257 This code is called from assembly file.
258
259 @param ExceptionType Exception type
260 @param ContextRecord System context
261
262 **/
263 VOID
264 InterruptDistrubutionHub (
265 EFI_EXCEPTION_TYPE ExceptionType,
266 EFI_SYSTEM_CONTEXT_IA32 *ContextRecord
267 )
268 {
269 if (IdtEntryTable[ExceptionType].RegisteredCallback != NULL) {
270 if (ExceptionType != SYSTEM_TIMER_VECTOR) {
271 IdtEntryTable[ExceptionType].RegisteredCallback (ExceptionType, ContextRecord);
272 } else {
273 OrigVector = IdtEntryTable[ExceptionType].OrigVector;
274 IdtEntryTable[ExceptionType].RegisteredCallback (ContextRecord);
275 }
276 }
277 }
278
279 /**
280 This is the callback that is written to the Loaded Image protocol instance
281 on the image handle. It uninstalls all registered handlers and frees all entry
282 stub memory.
283
284 @param ImageHandle The firmware allocated handle for the EFI image.
285
286 @retval EFI_SUCCESS Always.
287
288 **/
289 EFI_STATUS
290 EFIAPI
291 PlUnloadDebugSupportDriver (
292 IN EFI_HANDLE ImageHandle
293 )
294 {
295 EFI_EXCEPTION_TYPE ExceptionType;
296
297 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
298 ManageIdtEntryTable (NULL, ExceptionType);
299 //
300 // Free space for each Interrupt Stub precedure.
301 //
302 if (IdtEntryTable[ExceptionType].StubEntry != NULL) {
303 FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);
304 }
305 }
306
307 FreePool (IdtEntryTable);
308
309 return EFI_SUCCESS;
310 }
311
312 /**
313 Initializes driver's handler registration database.
314
315 This code executes in boot services context.
316 Must be public because it's referenced from DebugSupport.c
317
318 @retval EFI_UNSUPPORTED If IA32/x64 processor does not support FXSTOR/FXRSTOR instructions,
319 the context save will fail, so these processors are not supported.
320 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory.
321 @retval EFI_SUCCESS Initializes successfully.
322
323 **/
324 EFI_STATUS
325 PlInitializeDebugSupportDriver (
326 VOID
327 )
328 {
329 EFI_EXCEPTION_TYPE ExceptionType;
330
331 //
332 // Check whether FxStor instructions are supported.
333 //
334 if (!FxStorSupport ()) {
335 return EFI_UNSUPPORTED;
336 }
337
338 IdtEntryTable = AllocateZeroPool (sizeof (IDT_ENTRY) * NUM_IDT_ENTRIES);
339 if (IdtEntryTable == NULL) {
340 return EFI_OUT_OF_RESOURCES;
341 }
342
343 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
344 IdtEntryTable[ExceptionType].StubEntry = (DEBUG_PROC)(UINTN)AllocatePool (StubSize);
345 if (IdtEntryTable[ExceptionType].StubEntry == NULL) {
346 goto ErrorCleanup;
347 }
348
349 //
350 // Copy Interrupt stub code.
351 //
352 CopyMem ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry, InterruptEntryStub, StubSize);
353 }
354
355 return EFI_SUCCESS;
356
357 ErrorCleanup:
358
359 for (ExceptionType = 0; ExceptionType < NUM_IDT_ENTRIES; ExceptionType++) {
360 if (IdtEntryTable[ExceptionType].StubEntry != NULL) {
361 FreePool ((VOID *)(UINTN)IdtEntryTable[ExceptionType].StubEntry);
362 }
363 }
364
365 FreePool (IdtEntryTable);
366
367 return EFI_OUT_OF_RESOURCES;
368 }