]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebugAgentLib.c
SignedCapsulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / SecPeiDebugAgent / SecPeiDebugAgentLib.c
... / ...
CommitLineData
1/** @file\r
2 SEC Core Debug Agent Library instance implementition.\r
3\r
4 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r
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
15#include "SecPeiDebugAgentLib.h"\r
16\r
17GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mSkipBreakpoint = FALSE;\r
18\r
19\r
20GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_VECTOR_HANDOFF_INFO_PPI mVectorHandoffInfoPpi = {\r
21 &mVectorHandoffInfoDebugAgent[0]\r
22};\r
23\r
24//\r
25// Ppis to be installed\r
26//\r
27GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mVectorHandoffInfoPpiList[] = {\r
28 {\r
29 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
30 &gEfiVectorHandoffInfoPpiGuid,\r
31 &mVectorHandoffInfoPpi\r
32 }\r
33};\r
34\r
35GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_NOTIFY_DESCRIPTOR mDebugAgentMemoryDiscoveredNotifyList[1] = {\r
36 {\r
37 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
38 &gEfiPeiMemoryDiscoveredPpiGuid,\r
39 DebugAgentCallbackMemoryDiscoveredPpi\r
40 }\r
41};\r
42\r
43/**\r
44 Check if debug agent support multi-processor.\r
45\r
46 @retval TRUE Multi-processor is supported.\r
47 @retval FALSE Multi-processor is not supported.\r
48\r
49**/\r
50BOOLEAN\r
51MultiProcessorDebugSupport (\r
52 VOID\r
53 )\r
54{\r
55 return FALSE;\r
56}\r
57\r
58/**\r
59 Read the Attach/Break-in symbols from the debug port.\r
60\r
61 @param[in] Handle Pointer to Debug Port handle.\r
62 @param[out] BreakSymbol Returned break symbol.\r
63\r
64 @retval EFI_SUCCESS Read the symbol in BreakSymbol.\r
65 @retval EFI_NOT_FOUND No read the break symbol.\r
66\r
67**/\r
68EFI_STATUS\r
69DebugReadBreakSymbol (\r
70 IN DEBUG_PORT_HANDLE Handle,\r
71 OUT UINT8 *BreakSymbol\r
72 )\r
73{\r
74 EFI_STATUS Status;\r
75 DEBUG_PACKET_HEADER DebugHeader;\r
76 UINT8 *Data8;\r
77\r
78 *BreakSymbol = 0;\r
79 //\r
80 // If Debug Port buffer has data, read it till it was break symbol or Debug Port buffer empty.\r
81 //\r
82 Data8 = (UINT8 *) &DebugHeader;\r
83 while (TRUE) {\r
84 //\r
85 // If start symbol is not received\r
86 //\r
87 if (!DebugPortPollBuffer (Handle)) {\r
88 //\r
89 // If no data in Debug Port, exit\r
90 //\r
91 break;\r
92 }\r
93 //\r
94 // Try to read the start symbol\r
95 //\r
96 DebugAgentReadBuffer (Handle, Data8, 1, 0);\r
97 if (*Data8 == DEBUG_STARTING_SYMBOL_ATTACH) {\r
98 *BreakSymbol = *Data8;\r
99 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Debug Timer attach symbol received %x", *BreakSymbol);\r
100 return EFI_SUCCESS;\r
101 }\r
102 if (*Data8 == DEBUG_STARTING_SYMBOL_NORMAL) {\r
103 Status = ReadRemainingBreakPacket (Handle, &DebugHeader);\r
104 if (Status == EFI_SUCCESS) {\r
105 *BreakSymbol = DebugHeader.Command;\r
106 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Debug Timer break symbol received %x", *BreakSymbol);\r
107 return EFI_SUCCESS;\r
108 }\r
109 if (Status == EFI_TIMEOUT) {\r
110 break;\r
111 }\r
112 }\r
113 }\r
114\r
115 return EFI_NOT_FOUND;\r
116}\r
117\r
118/**\r
119 Get the pointer to location saved Mailbox pointer from IDT entry.\r
120\r
121**/\r
122VOID *\r
123GetLocationSavedMailboxPointerInIdtEntry (\r
124 VOID\r
125 )\r
126{\r
127 UINTN *MailboxLocation;\r
128\r
129 MailboxLocation = (UINTN *) GetExceptionHandlerInIdtEntry (DEBUG_MAILBOX_VECTOR);\r
130 //\r
131 // *MailboxLocation is the pointer to Mailbox\r
132 //\r
133 VerifyMailboxChecksum ((DEBUG_AGENT_MAILBOX *) (*MailboxLocation));\r
134 return MailboxLocation;\r
135}\r
136\r
137/**\r
138 Set the pointer of Mailbox into IDT entry before memory is ready.\r
139\r
140 @param[in] MailboxLocation Pointer to location saved Mailbox pointer.\r
141\r
142**/\r
143VOID\r
144SetLocationSavedMailboxPointerInIdtEntry (\r
145 IN VOID *MailboxLocation\r
146 )\r
147{\r
148 SetExceptionHandlerInIdtEntry (DEBUG_MAILBOX_VECTOR, MailboxLocation);\r
149}\r
150\r
151/**\r
152 Get the location of Mailbox pointer from the GUIDed HOB.\r
153\r
154 @return Pointer to the location saved Mailbox pointer.\r
155\r
156**/\r
157UINT64 *\r
158GetMailboxLocationFromHob (\r
159 VOID\r
160 )\r
161{\r
162 EFI_HOB_GUID_TYPE *GuidHob;\r
163\r
164 GuidHob = GetFirstGuidHob (&gEfiDebugAgentGuid);\r
165 if (GuidHob == NULL) {\r
166 return NULL;\r
167 }\r
168 return (UINT64 *) (GET_GUID_HOB_DATA(GuidHob));\r
169}\r
170\r
171/**\r
172 Get Debug Agent Mailbox pointer.\r
173\r
174 @return Mailbox pointer.\r
175\r
176**/\r
177DEBUG_AGENT_MAILBOX *\r
178GetMailboxPointer (\r
179 VOID\r
180 )\r
181{\r
182 UINT64 DebugPortHandle;\r
183 UINT64 *MailboxLocationInIdt;\r
184 UINT64 *MailboxLocationInHob;\r
185 DEBUG_AGENT_MAILBOX *Mailbox;\r
186\r
187 //\r
188 // Get mailbox from IDT entry firstly\r
189 //\r
190 MailboxLocationInIdt = GetLocationSavedMailboxPointerInIdtEntry ();\r
191 Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocationInIdt);\r
192 //\r
193 // Cannot used GetDebugFlag() to get Debug Flag to avoid GetMailboxPointer() nested\r
194 //\r
195 if (Mailbox->DebugFlag.Bits.CheckMailboxInHob != 1 ||\r
196 Mailbox->DebugFlag.Bits.InitArch != DEBUG_ARCH_SYMBOL) {\r
197 //\r
198 // If mailbox was setup in SEC or the current CPU arch is different from the init arch\r
199 // Debug Agent initialized, return the mailbox from IDT entry directly.\r
200 // Otherwise, we need to check the mailbox location saved in GUIDed HOB further.\r
201 //\r
202 return Mailbox;\r
203 }\r
204\r
205 MailboxLocationInHob = GetMailboxLocationFromHob ();\r
206 //\r
207 // Compare mailbox in IDT enry with mailbox in HOB,\r
208 // need to fix mailbox location if HOB moved by PEI CORE\r
209 //\r
210 if (MailboxLocationInHob != MailboxLocationInIdt && MailboxLocationInHob != NULL) {\r
211 Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocationInHob);\r
212 //\r
213 // Fix up Debug Port handler and save new mailbox in IDT entry\r
214 //\r
215 Mailbox = (DEBUG_AGENT_MAILBOX *)((UINTN)Mailbox + ((UINTN)(MailboxLocationInHob) - (UINTN)MailboxLocationInIdt));\r
216 DebugPortHandle = (UINTN)Mailbox->DebugPortHandle + ((UINTN)(MailboxLocationInHob) - (UINTN)MailboxLocationInIdt);\r
217 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
218 *MailboxLocationInHob = (UINT64)(UINTN)Mailbox;\r
219 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationInHob);\r
220 //\r
221 // Clean CheckMailboxInHob flag\r
222 //\r
223 Mailbox->DebugFlag.Bits.CheckMailboxInHob = 0;\r
224 UpdateMailboxChecksum (Mailbox);\r
225 }\r
226\r
227 return Mailbox;\r
228}\r
229\r
230/**\r
231 Get debug port handle.\r
232\r
233 @return Debug port handle.\r
234\r
235**/\r
236DEBUG_PORT_HANDLE\r
237GetDebugPortHandle (\r
238 VOID\r
239 )\r
240{\r
241 DEBUG_AGENT_MAILBOX *DebugAgentMailbox;\r
242\r
243 DebugAgentMailbox = GetMailboxPointer ();\r
244\r
245 return (DEBUG_PORT_HANDLE) (UINTN)(DebugAgentMailbox->DebugPortHandle);\r
246}\r
247\r
248/**\r
249 Debug Agent provided notify callback function on Memory Discovered PPI.\r
250\r
251 @param[in] PeiServices Indirect reference to the PEI Services Table.\r
252 @param[in] NotifyDescriptor Address of the notification descriptor data structure.\r
253 @param[in] Ppi Address of the PPI that was installed.\r
254\r
255 @retval EFI_SUCCESS If the function completed successfully.\r
256\r
257**/\r
258EFI_STATUS\r
259EFIAPI\r
260DebugAgentCallbackMemoryDiscoveredPpi (\r
261 IN EFI_PEI_SERVICES **PeiServices,\r
262 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
263 IN VOID *Ppi\r
264 )\r
265{\r
266 EFI_STATUS Status;\r
267 DEBUG_AGENT_MAILBOX *Mailbox;\r
268 BOOLEAN InterruptStatus;\r
269 EFI_PHYSICAL_ADDRESS Address;\r
270 DEBUG_AGENT_MAILBOX *NewMailbox;\r
271 UINT64 *MailboxLocationInHob;\r
272\r
273 //\r
274 // Save and disable original interrupt status\r
275 //\r
276 InterruptStatus = SaveAndDisableInterrupts ();\r
277\r
278 //\r
279 // Allocate ACPI NVS memory for new Mailbox and Debug Port Handle buffer\r
280 //\r
281 Status = PeiServicesAllocatePages (\r
282 EfiACPIMemoryNVS,\r
283 EFI_SIZE_TO_PAGES (sizeof(DEBUG_AGENT_MAILBOX) + PcdGet16(PcdDebugPortHandleBufferSize)),\r
284 &Address\r
285 );\r
286 ASSERT_EFI_ERROR (Status);\r
287 NewMailbox = (DEBUG_AGENT_MAILBOX *) (UINTN) Address;\r
288 //\r
289 // Copy Mailbox and Debug Port Handle buffer to new location in ACPI NVS memory, because original Mailbox\r
290 // and Debug Port Handle buffer in the allocated pool that may be marked as free by DXE Core after DXE Core\r
291 // reallocates the HOB.\r
292 //\r
293 Mailbox = GetMailboxPointer ();\r
294 CopyMem (NewMailbox, Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
295 CopyMem (NewMailbox + 1, (VOID *)(UINTN)Mailbox->DebugPortHandle, PcdGet16(PcdDebugPortHandleBufferSize));\r
296 //\r
297 // Update Mailbox Location pointer in GUIDed HOB and IDT entry with new one\r
298 //\r
299 MailboxLocationInHob = GetMailboxLocationFromHob ();\r
300 ASSERT (MailboxLocationInHob != NULL);\r
301 *MailboxLocationInHob = (UINT64)(UINTN)NewMailbox;\r
302 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationInHob);\r
303 //\r
304 // Update Debug Port Handle in new Mailbox\r
305 //\r
306 UpdateMailboxContent (NewMailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, (UINT64)(UINTN)(NewMailbox + 1));\r
307 //\r
308 // Set physical memory ready flag\r
309 //\r
310 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
311\r
312 if (IsHostAttached ()) {\r
313 //\r
314 // Trigger one software interrupt to inform HOST\r
315 //\r
316 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
317 }\r
318\r
319 //\r
320 // Restore interrupt state.\r
321 //\r
322 SetInterruptState (InterruptStatus);\r
323\r
324 return EFI_SUCCESS;\r
325}\r
326\r
327/**\r
328 Initialize debug agent.\r
329\r
330 This function is used to set up debug environment for SEC and PEI phase.\r
331\r
332 If InitFlag is DEBUG_AGENT_INIT_PREMEM_SEC, it will overirde IDT table entries\r
333 and initialize debug port. It will enable interrupt to support break-in feature.\r
334 It will set up debug agent Mailbox in cache-as-ramfrom. It will be called before\r
335 physical memory is ready.\r
336 If InitFlag is DEBUG_AGENT_INIT_POSTMEM_SEC, debug agent will build one GUIDed\r
337 HOB to copy debug agent Mailbox. It will be called after physical memory is ready.\r
338\r
339 This function is used to set up debug environment to support source level debugging.\r
340 If certain Debug Agent Library instance has to save some private data in the stack,\r
341 this function must work on the mode that doesn't return to the caller, then\r
342 the caller needs to wrap up all rest of logic after InitializeDebugAgent() into one\r
343 function and pass it into InitializeDebugAgent(). InitializeDebugAgent() is\r
344 responsible to invoke the passing-in function at the end of InitializeDebugAgent().\r
345\r
346 If the parameter Function is not NULL, Debug Agent Library instance will invoke it by\r
347 passing in the Context to be its parameter.\r
348\r
349 If Function() is NULL, Debug Agent Library instance will return after setup debug\r
350 environment.\r
351\r
352 @param[in] InitFlag Init flag is used to decide the initialize process.\r
353 @param[in] Context Context needed according to InitFlag; it was optional.\r
354 @param[in] Function Continue function called by debug agent library; it was\r
355 optional.\r
356\r
357**/\r
358VOID\r
359EFIAPI\r
360InitializeDebugAgent (\r
361 IN UINT32 InitFlag,\r
362 IN VOID *Context, OPTIONAL\r
363 IN DEBUG_AGENT_CONTINUE Function OPTIONAL\r
364 )\r
365{\r
366 DEBUG_AGENT_MAILBOX *Mailbox;\r
367 DEBUG_AGENT_MAILBOX *NewMailbox;\r
368 DEBUG_AGENT_MAILBOX MailboxInStack;\r
369 DEBUG_AGENT_PHASE2_CONTEXT Phase2Context;\r
370 DEBUG_AGENT_CONTEXT_POSTMEM_SEC *DebugAgentContext;\r
371 EFI_STATUS Status;\r
372 IA32_DESCRIPTOR *Ia32Idtr;\r
373 IA32_IDT_ENTRY *Ia32IdtEntry;\r
374 UINT64 DebugPortHandle;\r
375 UINT64 MailboxLocation;\r
376 UINT64 *MailboxLocationPointer;\r
377 EFI_PHYSICAL_ADDRESS Address;\r
378 UINT32 DebugTimerFrequency;\r
379 BOOLEAN CpuInterruptState;\r
380\r
381 //\r
382 // Disable interrupts and save current interrupt state\r
383 //\r
384 CpuInterruptState = SaveAndDisableInterrupts();\r
385\r
386 switch (InitFlag) {\r
387\r
388 case DEBUG_AGENT_INIT_PREMEM_SEC:\r
389\r
390 InitializeDebugIdt ();\r
391\r
392 MailboxLocation = (UINT64)(UINTN)&MailboxInStack;\r
393 Mailbox = &MailboxInStack;\r
394 ZeroMem ((VOID *) Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
395 //\r
396 // Get and save debug port handle and set the length of memory block.\r
397 //\r
398 SetLocationSavedMailboxPointerInIdtEntry (&MailboxLocation);\r
399 //\r
400 // Force error message could be printed during the first shakehand between Target/HOST.\r
401 //\r
402 SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL, DEBUG_AGENT_ERROR);\r
403 //\r
404 // Save init arch type when debug agent initialized\r
405 //\r
406 SetDebugFlag (DEBUG_AGENT_FLAG_INIT_ARCH, DEBUG_ARCH_SYMBOL);\r
407 //\r
408 // Initialize Debug Timer hardware and save its frequency\r
409 //\r
410 InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
411 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
412\r
413 Phase2Context.InitFlag = InitFlag;\r
414 Phase2Context.Context = Context;\r
415 Phase2Context.Function = Function;\r
416 DebugPortInitialize ((VOID *) &Phase2Context, InitializeDebugAgentPhase2);\r
417 //\r
418 // If reaches here, it means Debug Port initialization failed.\r
419 //\r
420 DEBUG ((EFI_D_ERROR, "Debug Agent: Debug port initialization failed.\n"));\r
421\r
422 break;\r
423\r
424 case DEBUG_AGENT_INIT_POSTMEM_SEC:\r
425 Mailbox = GetMailboxPointer ();\r
426 //\r
427 // Memory has been ready\r
428 //\r
429 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
430 if (IsHostAttached ()) {\r
431 //\r
432 // Trigger one software interrupt to inform HOST\r
433 //\r
434 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
435 }\r
436 //\r
437 // Install Vector Handoff Info PPI to persist vectors used by Debug Agent\r
438 //\r
439 Status = PeiServicesInstallPpi (&mVectorHandoffInfoPpiList[0]);\r
440 if (EFI_ERROR (Status)) {\r
441 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to install Vector Handoff Info PPI!\n"));\r
442 CpuDeadLoop ();\r
443 }\r
444 //\r
445 // Fix up Debug Port handle address and mailbox address\r
446 //\r
447 DebugAgentContext = (DEBUG_AGENT_CONTEXT_POSTMEM_SEC *) Context;\r
448 if (DebugAgentContext != NULL) {\r
449 DebugPortHandle = (UINT64)(UINT32)(Mailbox->DebugPortHandle + DebugAgentContext->StackMigrateOffset);\r
450 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
451 Mailbox = (DEBUG_AGENT_MAILBOX *) ((UINTN) Mailbox + DebugAgentContext->StackMigrateOffset);\r
452 MailboxLocation = (UINT64)(UINTN)Mailbox;\r
453 //\r
454 // Build mailbox location in HOB and fix-up its address\r
455 //\r
456 MailboxLocationPointer = BuildGuidDataHob (\r
457 &gEfiDebugAgentGuid,\r
458 &MailboxLocation,\r
459 sizeof (UINT64)\r
460 );\r
461 MailboxLocationPointer = (UINT64 *) ((UINTN) MailboxLocationPointer + DebugAgentContext->HeapMigrateOffset);\r
462 } else {\r
463 //\r
464 // DebugAgentContext is NULL. Then, Mailbox can directly be copied into memory.\r
465 // Allocate ACPI NVS memory for new Mailbox and Debug Port Handle buffer\r
466 //\r
467 Status = PeiServicesAllocatePages (\r
468 EfiACPIMemoryNVS,\r
469 EFI_SIZE_TO_PAGES (sizeof(DEBUG_AGENT_MAILBOX) + PcdGet16(PcdDebugPortHandleBufferSize)),\r
470 &Address\r
471 );\r
472 if (EFI_ERROR (Status)) {\r
473 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate pages!\n"));\r
474 CpuDeadLoop ();\r
475 }\r
476 NewMailbox = (DEBUG_AGENT_MAILBOX *) (UINTN) Address;\r
477 //\r
478 // Copy Mailbox and Debug Port Handle buffer to new location in ACPI NVS memory, because original Mailbox\r
479 // and Debug Port Handle buffer in the allocated pool that may be marked as free by DXE Core after DXE Core\r
480 // reallocates the HOB.\r
481 //\r
482 CopyMem (NewMailbox, Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
483 CopyMem (NewMailbox + 1, (VOID *)(UINTN)Mailbox->DebugPortHandle, PcdGet16(PcdDebugPortHandleBufferSize));\r
484 UpdateMailboxContent (NewMailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, (UINT64)(UINTN)(NewMailbox + 1));\r
485 MailboxLocation = (UINT64)(UINTN)NewMailbox;\r
486 //\r
487 // Build mailbox location in HOB\r
488 //\r
489 MailboxLocationPointer = BuildGuidDataHob (\r
490 &gEfiDebugAgentGuid,\r
491 &MailboxLocation,\r
492 sizeof (UINT64)\r
493 );\r
494 }\r
495 //\r
496 // Update IDT entry to save the location saved mailbox pointer\r
497 //\r
498 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
499 break;\r
500\r
501 case DEBUG_AGENT_INIT_PEI:\r
502 if (Context == NULL) {\r
503 DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
504 CpuDeadLoop ();\r
505 }\r
506 //\r
507 // Check if Debug Agent has initialized before\r
508 //\r
509 if (IsDebugAgentInitialzed()) {\r
510 DEBUG ((EFI_D_WARN, "Debug Agent: It has already initialized in SEC Core!\n"));\r
511 break;\r
512 }\r
513 //\r
514 // Install Vector Handoff Info PPI to persist vectors used by Debug Agent\r
515 //\r
516 Status = PeiServicesInstallPpi (&mVectorHandoffInfoPpiList[0]);\r
517 if (EFI_ERROR (Status)) {\r
518 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to install Vector Handoff Info PPI!\n"));\r
519 CpuDeadLoop ();\r
520 }\r
521 //\r
522 // Set up IDT entries\r
523 //\r
524 InitializeDebugIdt ();\r
525 //\r
526 // Build mailbox in HOB and setup Mailbox Set In Pei flag\r
527 //\r
528 Mailbox = AllocateZeroPool (sizeof (DEBUG_AGENT_MAILBOX));\r
529 if (Mailbox == NULL) {\r
530 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate memory!\n"));\r
531 CpuDeadLoop ();\r
532 } else {\r
533 MailboxLocation = (UINT64)(UINTN)Mailbox;\r
534 MailboxLocationPointer = BuildGuidDataHob (\r
535 &gEfiDebugAgentGuid,\r
536 &MailboxLocation,\r
537 sizeof (UINT64)\r
538 );\r
539 //\r
540 // Initialize Debug Timer hardware and save its frequency\r
541 //\r
542 InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
543 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
544 //\r
545 // Update IDT entry to save the location pointer saved mailbox pointer\r
546 //\r
547 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
548 }\r
549 //\r
550 // Save init arch type when debug agent initialized\r
551 //\r
552 SetDebugFlag (DEBUG_AGENT_FLAG_INIT_ARCH, DEBUG_ARCH_SYMBOL);\r
553 //\r
554 // Register for a callback once memory has been initialized.\r
555 // If memery has been ready, the callback funtion will be invoked immediately\r
556 //\r
557 Status = PeiServicesNotifyPpi (&mDebugAgentMemoryDiscoveredNotifyList[0]);\r
558 if (EFI_ERROR (Status)) {\r
559 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to register memory discovered callback function!\n"));\r
560 CpuDeadLoop ();\r
561 }\r
562 //\r
563 // Set HOB check flag if memory has not been ready yet\r
564 //\r
565 if (GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY) == 0) {\r
566 SetDebugFlag (DEBUG_AGENT_FLAG_CHECK_MAILBOX_IN_HOB, 1);\r
567 }\r
568\r
569 Phase2Context.InitFlag = InitFlag;\r
570 Phase2Context.Context = Context;\r
571 Phase2Context.Function = Function;\r
572 DebugPortInitialize ((VOID *) &Phase2Context, InitializeDebugAgentPhase2);\r
573\r
574 FindAndReportModuleImageInfo (4);\r
575\r
576 break;\r
577\r
578 case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64:\r
579 if (Context == NULL) {\r
580 DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
581 CpuDeadLoop ();\r
582 } else {\r
583 Ia32Idtr = (IA32_DESCRIPTOR *) Context;\r
584 Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
585 MailboxLocationPointer = (UINT64 *) ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
586 ((UINTN) Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
587 Mailbox = (DEBUG_AGENT_MAILBOX *) (UINTN)(*MailboxLocationPointer);\r
588 //\r
589 // Mailbox should valid and setup before executing thunk code\r
590 //\r
591 VerifyMailboxChecksum (Mailbox);\r
592\r
593 DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((VOID *)(UINTN)Mailbox->DebugPortHandle, NULL);\r
594 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
595 //\r
596 // Set up IDT entries\r
597 //\r
598 InitializeDebugIdt ();\r
599 //\r
600 // Update IDT entry to save location pointer saved the mailbox pointer\r
601 //\r
602 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
603\r
604 FindAndReportModuleImageInfo (4);\r
605 }\r
606 break;\r
607\r
608 default:\r
609 //\r
610 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this\r
611 // Debug Agent library instance.\r
612 //\r
613 DEBUG ((EFI_D_ERROR, "Debug Agent: The InitFlag value is not allowed!\n"));\r
614 CpuDeadLoop ();\r
615 break;\r
616 }\r
617\r
618 if (InitFlag == DEBUG_AGENT_INIT_POSTMEM_SEC) {\r
619 //\r
620 // Restore CPU Interrupt state and keep debug timer interrupt state as is\r
621 // in DEBUG_AGENT_INIT_POSTMEM_SEC case\r
622 //\r
623 SetInterruptState (CpuInterruptState);\r
624 } else {\r
625 //\r
626 // Enable Debug Timer interrupt\r
627 //\r
628 SaveAndSetDebugTimerInterrupt (TRUE);\r
629 //\r
630 // Enable CPU interrupts so debug timer interrupts can be delivered\r
631 //\r
632 EnableInterrupts ();\r
633 }\r
634 //\r
635 // If Function is not NULL, invoke it always whatever debug agent was initialized sucesssfully or not.\r
636 //\r
637 if (Function != NULL) {\r
638 Function (Context);\r
639 }\r
640 //\r
641 // Set return status for DEBUG_AGENT_INIT_PEI\r
642 //\r
643 if (InitFlag == DEBUG_AGENT_INIT_PEI && Context != NULL) {\r
644 *(EFI_STATUS *)Context = EFI_SUCCESS;\r
645 }\r
646}\r
647\r
648/**\r
649 Caller provided function to be invoked at the end of DebugPortInitialize().\r
650\r
651 Refer to the description for DebugPortInitialize() for more details.\r
652\r
653 @param[in] Context The first input argument of DebugPortInitialize().\r
654 @param[in] DebugPortHandle Debug port handle created by Debug Communication Library.\r
655\r
656**/\r
657VOID\r
658EFIAPI\r
659InitializeDebugAgentPhase2 (\r
660 IN VOID *Context,\r
661 IN DEBUG_PORT_HANDLE DebugPortHandle\r
662 )\r
663{\r
664 DEBUG_AGENT_PHASE2_CONTEXT *Phase2Context;\r
665 UINT64 *MailboxLocation;\r
666 DEBUG_AGENT_MAILBOX *Mailbox;\r
667 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
668 UINT16 BufferSize;\r
669 UINT64 NewDebugPortHandle;\r
670\r
671 Phase2Context = (DEBUG_AGENT_PHASE2_CONTEXT *) Context;\r
672 MailboxLocation = GetLocationSavedMailboxPointerInIdtEntry ();\r
673 Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
674 BufferSize = PcdGet16(PcdDebugPortHandleBufferSize);\r
675 if (Phase2Context->InitFlag == DEBUG_AGENT_INIT_PEI && BufferSize != 0) {\r
676 NewDebugPortHandle = (UINT64)(UINTN)AllocateCopyPool (BufferSize, DebugPortHandle);\r
677 } else {\r
678 NewDebugPortHandle = (UINT64)(UINTN)DebugPortHandle;\r
679 }\r
680 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, NewDebugPortHandle);\r
681\r
682 //\r
683 // Trigger one software interrupt to inform HOST\r
684 //\r
685 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE);\r
686\r
687 if (Phase2Context->InitFlag == DEBUG_AGENT_INIT_PREMEM_SEC) {\r
688 //\r
689 // If Temporary RAM region is below 128 MB, then send message to\r
690 // host to disable low memory filtering.\r
691 //\r
692 SecCoreData = (EFI_SEC_PEI_HAND_OFF *)Phase2Context->Context;\r
693 if ((UINTN)SecCoreData->TemporaryRamBase < BASE_128MB && IsHostAttached ()) {\r
694 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
695 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
696 }\r
697 //\r
698 // Enable Debug Timer interrupt\r
699 //\r
700 SaveAndSetDebugTimerInterrupt (TRUE);\r
701 //\r
702 // Enable CPU interrupts so debug timer interrupts can be delivered\r
703 //\r
704 EnableInterrupts ();\r
705 //\r
706 // Call continuation function if it is not NULL.\r
707 //\r
708 Phase2Context->Function (Phase2Context->Context);\r
709 }\r
710}\r