]> git.proxmox.com Git - mirror_edk2.git/blame - SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebugAgentLib.c
SourceLevelDebugPkg: Fix typos in comments
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / SecPeiDebugAgent / SecPeiDebugAgentLib.c
CommitLineData
18b144ea 1/** @file\r
2 SEC Core Debug Agent Library instance implementition.\r
3\r
08021523 4 Copyright (c) 2010 - 2015, 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
15#include "SecPeiDebugAgentLib.h"\r
16\r
4fe43eb3 17GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mSkipBreakpoint = FALSE;\r
b422b62c 18\r
8cc26df4 19\r
4fe43eb3 20GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_VECTOR_HANDOFF_INFO_PPI mVectorHandoffInfoPpi = {\r
8cc26df4
JF
21 &mVectorHandoffInfoDebugAgent[0]\r
22};\r
23\r
24//\r
25// Ppis to be installed\r
26//\r
4fe43eb3 27GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_PPI_DESCRIPTOR mVectorHandoffInfoPpiList[] = {\r
8cc26df4
JF
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
4fe43eb3 35GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_NOTIFY_DESCRIPTOR mMemoryDiscoveredNotifyList[1] = {\r
b422b62c 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
18b144ea 57\r
93c0bdec 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
b422b62c 74 EFI_STATUS Status;\r
75 DEBUG_PACKET_HEADER DebugHeader;\r
76 UINT8 *Data8;\r
77\r
93c0bdec 78 *BreakSymbol = 0;\r
79 //\r
b422b62c 80 // If Debug Port buffer has data, read it till it was break symbol or Debug Port buffer empty.\r
93c0bdec 81 //\r
b422b62c 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
08021523 96 DebugAgentReadBuffer (Handle, Data8, 1, 0);\r
b422b62c 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
93c0bdec 100 return EFI_SUCCESS;\r
4fe43eb3 101 }\r
b422b62c 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
93c0bdec 112 }\r
113 }\r
4fe43eb3 114\r
93c0bdec 115 return EFI_NOT_FOUND;\r
116}\r
117\r
18b144ea 118/**\r
b422b62c 119 Get the pointer to location saved Mailbox pointer from IDT entry.\r
18b144ea 120\r
121**/\r
122VOID *\r
b422b62c 123GetLocationSavedMailboxPointerInIdtEntry (\r
18b144ea 124 VOID\r
125 )\r
126{\r
b422b62c 127 UINTN *MailboxLocation;\r
18b144ea 128\r
b422b62c 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
18b144ea 135}\r
136\r
137/**\r
138 Set the pointer of Mailbox into IDT entry before memory is ready.\r
139\r
b422b62c 140 @param[in] MailboxLocation Pointer to location saved Mailbox pointer.\r
18b144ea 141\r
142**/\r
143VOID\r
b422b62c 144SetLocationSavedMailboxPointerInIdtEntry (\r
145 IN VOID *MailboxLocation\r
18b144ea 146 )\r
147{\r
b422b62c 148 SetExceptionHandlerInIdtEntry (DEBUG_MAILBOX_VECTOR, MailboxLocation);\r
18b144ea 149}\r
150\r
151/**\r
b422b62c 152 Get the location of Mailbox pointer from the GUIDed HOB.\r
18b144ea 153\r
b422b62c 154 @return Pointer to the location saved Mailbox pointer.\r
18b144ea 155\r
156**/\r
b422b62c 157UINT64 *\r
158GetMailboxLocationFromHob (\r
18b144ea 159 VOID\r
160 )\r
161{\r
b422b62c 162 EFI_HOB_GUID_TYPE *GuidHob;\r
18b144ea 163\r
b422b62c 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
18b144ea 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
b422b62c 182 UINT64 DebugPortHandle;\r
183 UINT64 *MailboxLocationInIdt;\r
184 UINT64 *MailboxLocationInHob;\r
185 DEBUG_AGENT_MAILBOX *Mailbox;\r
4fe43eb3 186\r
b422b62c 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
0a488765 193 // Cannot used GetDebugFlag() to get Debug Flag to avoid GetMailboxPointer() nested\r
b422b62c 194 //\r
0a488765 195 if (Mailbox->DebugFlag.Bits.CheckMailboxInHob != 1 ||\r
196 Mailbox->DebugFlag.Bits.InitArch != DEBUG_ARCH_SYMBOL) {\r
b422b62c 197 //\r
0a488765 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
4fe43eb3 201 //\r
b422b62c 202 return Mailbox;\r
203 }\r
204\r
205 MailboxLocationInHob = GetMailboxLocationFromHob ();\r
206 //\r
0a488765 207 // Compare mailbox in IDT enry with mailbox in HOB,\r
208 // need to fix mailbox location if HOB moved by PEI CORE\r
b422b62c 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 = (UINT64)((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
18b144ea 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
4fe43eb3 242\r
b422b62c 243 DebugAgentMailbox = GetMailboxPointer ();\r
18b144ea 244\r
245 return (DEBUG_PORT_HANDLE) (UINTN)(DebugAgentMailbox->DebugPortHandle);\r
246}\r
247\r
248/**\r
b422b62c 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
18b144ea 254\r
b422b62c 255 @retval EFI_SUCCESS If the function completed successfully.\r
18b144ea 256\r
257**/\r
b422b62c 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
18b144ea 264 )\r
265{\r
dca18202 266 EFI_STATUS Status;\r
b422b62c 267 DEBUG_AGENT_MAILBOX *Mailbox;\r
268 BOOLEAN InterruptStatus;\r
4fe43eb3 269 EFI_PHYSICAL_ADDRESS Address;\r
dca18202 270 DEBUG_AGENT_MAILBOX *NewMailbox;\r
271 UINT64 *MailboxLocationInHob;\r
272\r
18b144ea 273 //\r
b422b62c 274 // Save and disable original interrupt status\r
18b144ea 275 //\r
b422b62c 276 InterruptStatus = SaveAndDisableInterrupts ();\r
dca18202 277\r
18b144ea 278 //\r
dca18202 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
0a488765 284 &Address\r
dca18202 285 );\r
286 ASSERT_EFI_ERROR (Status);\r
0a488765 287 NewMailbox = (DEBUG_AGENT_MAILBOX *) (UINTN) Address;\r
dca18202 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
18b144ea 292 //\r
b422b62c 293 Mailbox = GetMailboxPointer ();\r
dca18202 294 CopyMem (NewMailbox, Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
295 CopyMem (NewMailbox + 1, (VOID *)(UINTN)Mailbox->DebugPortHandle, PcdGet16(PcdDebugPortHandleBufferSize));\r
18b144ea 296 //\r
dca18202 297 // Update Mailbox Location pointer in GUIDed HOB and IDT entry with new one\r
18b144ea 298 //\r
dca18202 299 MailboxLocationInHob = GetMailboxLocationFromHob ();\r
8cbad981 300 ASSERT (MailboxLocationInHob != NULL);\r
dca18202 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
b422b62c 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
18b144ea 318\r
319 //\r
b422b62c 320 // Restore interrupt state.\r
18b144ea 321 //\r
b422b62c 322 SetInterruptState (InterruptStatus);\r
4fe43eb3 323\r
b422b62c 324 return EFI_SUCCESS;\r
18b144ea 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
74cdb367 346 If the parameter Function is not NULL, Debug Agent Library instance will invoke it by\r
18b144ea 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
4d0da5fb 367 DEBUG_AGENT_MAILBOX *NewMailbox;\r
18b144ea 368 DEBUG_AGENT_MAILBOX MailboxInStack;\r
369 DEBUG_AGENT_PHASE2_CONTEXT Phase2Context;\r
370 DEBUG_AGENT_CONTEXT_POSTMEM_SEC *DebugAgentContext;\r
b422b62c 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
4fe43eb3 377 EFI_PHYSICAL_ADDRESS Address;\r
08021523 378 UINT32 DebugTimerFrequency;\r
2638c111 379 BOOLEAN CpuInterruptState;\r
4fe43eb3 380\r
2638c111
JF
381 //\r
382 // Disable interrupts and save current interrupt state\r
383 //\r
384 CpuInterruptState = SaveAndDisableInterrupts();\r
18b144ea 385\r
e2104834 386 switch (InitFlag) {\r
387\r
388 case DEBUG_AGENT_INIT_PREMEM_SEC:\r
389\r
390 InitializeDebugIdt ();\r
391\r
b422b62c 392 MailboxLocation = (UINT64)(UINTN)&MailboxInStack;\r
e2104834 393 Mailbox = &MailboxInStack;\r
394 ZeroMem ((VOID *) Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
e2104834 395 //\r
396 // Get and save debug port handle and set the length of memory block.\r
397 //\r
b422b62c 398 SetLocationSavedMailboxPointerInIdtEntry (&MailboxLocation);\r
0a488765 399 //\r
400 // Force error message could be printed during the first shakehand between Target/HOST.\r
401 //\r
b422b62c 402 SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL, DEBUG_AGENT_ERROR);\r
0a488765 403 //\r
404 // Save init arch type when debug agent initialized\r
405 //\r
b6d1508f 406 SetDebugFlag (DEBUG_AGENT_FLAG_INIT_ARCH, DEBUG_ARCH_SYMBOL);\r
08021523
JF
407 //\r
408 // Initialize Debug Timer hardware and save its frequency\r
409 //\r
86d13652 410 InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
08021523 411 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
e2104834 412\r
5ab7f883 413 Phase2Context.InitFlag = InitFlag;\r
e2104834 414 Phase2Context.Context = Context;\r
415 Phase2Context.Function = Function;\r
416 DebugPortInitialize ((VOID *) &Phase2Context, InitializeDebugAgentPhase2);\r
e2104834 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
b422b62c 425 Mailbox = GetMailboxPointer ();\r
18b144ea 426 //\r
427 // Memory has been ready\r
428 //\r
b422b62c 429 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
430 if (IsHostAttached ()) {\r
18b144ea 431 //\r
432 // Trigger one software interrupt to inform HOST\r
433 //\r
434 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
435 }\r
8cc26df4
JF
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
b422b62c 444 //\r
445 // Fix up Debug Port handle address and mailbox address\r
446 //\r
18b144ea 447 DebugAgentContext = (DEBUG_AGENT_CONTEXT_POSTMEM_SEC *) Context;\r
4d0da5fb
LG
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
8cc26df4
JF
472 if (EFI_ERROR (Status)) {\r
473 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate pages!\n"));\r
474 CpuDeadLoop ();\r
475 }\r
4d0da5fb
LG
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
b422b62c 495 //\r
496 // Update IDT entry to save the location saved mailbox pointer\r
497 //\r
498 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
b422b62c 499 break;\r
500\r
501 case DEBUG_AGENT_INIT_PEI:\r
5ab7f883
JF
502 if (Context == NULL) {\r
503 DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
504 CpuDeadLoop ();\r
505 }\r
b422b62c 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
8cc26df4
JF
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
b422b62c 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
8977a7b7
JF
529 if (Mailbox == NULL) {\r
530 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate memory!\n"));\r
531 CpuDeadLoop ();\r
14f3f882
JF
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
86d13652 542 InitializeDebugTimer (&DebugTimerFrequency, TRUE);\r
14f3f882
JF
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
8977a7b7 548 }\r
b422b62c 549 //\r
0a488765 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
b422b62c 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 (&mMemoryDiscoveredNotifyList[0]);\r
8cc26df4
JF
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
b422b62c 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
18b144ea 568\r
5ab7f883 569 Phase2Context.InitFlag = InitFlag;\r
b422b62c 570 Phase2Context.Context = Context;\r
571 Phase2Context.Function = Function;\r
572 DebugPortInitialize ((VOID *) &Phase2Context, InitializeDebugAgentPhase2);\r
18b144ea 573\r
b422b62c 574 FindAndReportModuleImageInfo (4);\r
18b144ea 575\r
b422b62c 576 break;\r
18b144ea 577\r
b422b62c 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
fe0c434e 586 (UINT32) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
b422b62c 587 Mailbox = (DEBUG_AGENT_MAILBOX *) (UINTN)(*MailboxLocationPointer);\r
0a488765 588 //\r
589 // Mailbox should valid and setup before executing thunk code\r
590 //\r
b422b62c 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
e2104834 606 break;\r
18b144ea 607\r
e2104834 608 default:\r
18b144ea 609 //\r
4fe43eb3 610 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this\r
e2104834 611 // Debug Agent library instance.\r
18b144ea 612 //\r
e2104834 613 DEBUG ((EFI_D_ERROR, "Debug Agent: The InitFlag value is not allowed!\n"));\r
614 CpuDeadLoop ();\r
615 break;\r
e2104834 616 }\r
18b144ea 617\r
2638c111
JF
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
3b87e388 628 SaveAndSetDebugTimerInterrupt (TRUE);\r
2638c111
JF
629 //\r
630 // Enable CPU interrupts so debug timer interrupts can be delivered\r
631 //\r
632 EnableInterrupts ();\r
3b87e388 633 }\r
e2104834 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
18b144ea 639 }\r
5ab7f883
JF
640 //\r
641 // Set return status for DEBUG_AGENT_INIT_PEI\r
642 //\r
a1ac5791 643 if (InitFlag == DEBUG_AGENT_INIT_PEI && Context != NULL) {\r
5ab7f883
JF
644 *(EFI_STATUS *)Context = EFI_SUCCESS;\r
645 }\r
18b144ea 646}\r
647\r
648/**\r
649 Caller provided function to be invoked at the end of DebugPortInitialize().\r
650\r
74cdb367 651 Refer to the description for DebugPortInitialize() for more details.\r
18b144ea 652\r
653 @param[in] Context The first input argument of DebugPortInitialize().\r
74cdb367 654 @param[in] DebugPortHandle Debug port handle created by Debug Communication Library.\r
18b144ea 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
b422b62c 665 UINT64 *MailboxLocation;\r
18b144ea 666 DEBUG_AGENT_MAILBOX *Mailbox;\r
667 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
b422b62c 668 UINT16 BufferSize;\r
669 UINT64 NewDebugPortHandle;\r
18b144ea 670\r
b422b62c 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
7dd62c4e 675 if (Phase2Context->InitFlag == DEBUG_AGENT_INIT_PEI && BufferSize != 0) {\r
b422b62c 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
18b144ea 681\r
682 //\r
683 // Trigger one software interrupt to inform HOST\r
684 //\r
685 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE);\r
686\r
5ab7f883
JF
687 if (Phase2Context->InitFlag == DEBUG_AGENT_INIT_PREMEM_SEC) {\r
688 //\r
4fe43eb3 689 // If Temporary RAM region is below 128 MB, then send message to\r
5ab7f883
JF
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
0bb5d7a5
BJ
698 // Enable Debug Timer interrupt\r
699 //\r
700 SaveAndSetDebugTimerInterrupt (TRUE);\r
701 //\r
5ab7f883
JF
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
18b144ea 708 Phase2Context->Function (Phase2Context->Context);\r
709 }\r
710}\r