]> git.proxmox.com Git - mirror_edk2.git/blame - SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebugAgentLib.c
SourceLevelDebugPkg: Fix calculate the checksum bug and add NULL pointer check
[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
346 If the parameter Function is not NULL, Debug Agent Libary 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
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
4fe43eb3 379\r
18b144ea 380 DisableInterrupts ();\r
381\r
e2104834 382 switch (InitFlag) {\r
383\r
384 case DEBUG_AGENT_INIT_PREMEM_SEC:\r
385\r
386 InitializeDebugIdt ();\r
387\r
b422b62c 388 MailboxLocation = (UINT64)(UINTN)&MailboxInStack;\r
e2104834 389 Mailbox = &MailboxInStack;\r
390 ZeroMem ((VOID *) Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
e2104834 391 //\r
392 // Get and save debug port handle and set the length of memory block.\r
393 //\r
b422b62c 394 SetLocationSavedMailboxPointerInIdtEntry (&MailboxLocation);\r
0a488765 395 //\r
396 // Force error message could be printed during the first shakehand between Target/HOST.\r
397 //\r
b422b62c 398 SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL, DEBUG_AGENT_ERROR);\r
0a488765 399 //\r
400 // Save init arch type when debug agent initialized\r
401 //\r
b6d1508f 402 SetDebugFlag (DEBUG_AGENT_FLAG_INIT_ARCH, DEBUG_ARCH_SYMBOL);\r
08021523
JF
403 //\r
404 // Initialize Debug Timer hardware and save its frequency\r
405 //\r
406 InitializeDebugTimer (&DebugTimerFrequency);\r
407 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
e2104834 408\r
5ab7f883 409 Phase2Context.InitFlag = InitFlag;\r
e2104834 410 Phase2Context.Context = Context;\r
411 Phase2Context.Function = Function;\r
412 DebugPortInitialize ((VOID *) &Phase2Context, InitializeDebugAgentPhase2);\r
e2104834 413 //\r
414 // If reaches here, it means Debug Port initialization failed.\r
415 //\r
416 DEBUG ((EFI_D_ERROR, "Debug Agent: Debug port initialization failed.\n"));\r
417\r
418 break;\r
419\r
420 case DEBUG_AGENT_INIT_POSTMEM_SEC:\r
b422b62c 421 Mailbox = GetMailboxPointer ();\r
18b144ea 422 //\r
423 // Memory has been ready\r
424 //\r
b422b62c 425 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
426 if (IsHostAttached ()) {\r
18b144ea 427 //\r
428 // Trigger one software interrupt to inform HOST\r
429 //\r
430 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
431 }\r
8cc26df4
JF
432 //\r
433 // Install Vector Handoff Info PPI to persist vectors used by Debug Agent\r
434 //\r
435 Status = PeiServicesInstallPpi (&mVectorHandoffInfoPpiList[0]);\r
436 if (EFI_ERROR (Status)) {\r
437 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to install Vector Handoff Info PPI!\n"));\r
438 CpuDeadLoop ();\r
439 }\r
b422b62c 440 //\r
441 // Fix up Debug Port handle address and mailbox address\r
442 //\r
18b144ea 443 DebugAgentContext = (DEBUG_AGENT_CONTEXT_POSTMEM_SEC *) Context;\r
4d0da5fb
LG
444 if (DebugAgentContext != NULL) {\r
445 DebugPortHandle = (UINT64)(UINT32)(Mailbox->DebugPortHandle + DebugAgentContext->StackMigrateOffset);\r
446 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
447 Mailbox = (DEBUG_AGENT_MAILBOX *) ((UINTN) Mailbox + DebugAgentContext->StackMigrateOffset);\r
448 MailboxLocation = (UINT64)(UINTN)Mailbox;\r
449 //\r
450 // Build mailbox location in HOB and fix-up its address\r
451 //\r
452 MailboxLocationPointer = BuildGuidDataHob (\r
453 &gEfiDebugAgentGuid,\r
454 &MailboxLocation,\r
455 sizeof (UINT64)\r
456 );\r
457 MailboxLocationPointer = (UINT64 *) ((UINTN) MailboxLocationPointer + DebugAgentContext->HeapMigrateOffset);\r
458 } else {\r
459 //\r
460 // DebugAgentContext is NULL. Then, Mailbox can directly be copied into memory.\r
461 // Allocate ACPI NVS memory for new Mailbox and Debug Port Handle buffer\r
462 //\r
463 Status = PeiServicesAllocatePages (\r
464 EfiACPIMemoryNVS,\r
465 EFI_SIZE_TO_PAGES (sizeof(DEBUG_AGENT_MAILBOX) + PcdGet16(PcdDebugPortHandleBufferSize)),\r
466 &Address\r
467 );\r
8cc26df4
JF
468 if (EFI_ERROR (Status)) {\r
469 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate pages!\n"));\r
470 CpuDeadLoop ();\r
471 }\r
4d0da5fb
LG
472 NewMailbox = (DEBUG_AGENT_MAILBOX *) (UINTN) Address;\r
473 //\r
474 // Copy Mailbox and Debug Port Handle buffer to new location in ACPI NVS memory, because original Mailbox\r
475 // and Debug Port Handle buffer in the allocated pool that may be marked as free by DXE Core after DXE Core\r
476 // reallocates the HOB.\r
477 //\r
478 CopyMem (NewMailbox, Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
479 CopyMem (NewMailbox + 1, (VOID *)(UINTN)Mailbox->DebugPortHandle, PcdGet16(PcdDebugPortHandleBufferSize));\r
480 UpdateMailboxContent (NewMailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, (UINT64)(UINTN)(NewMailbox + 1));\r
481 MailboxLocation = (UINT64)(UINTN)NewMailbox;\r
482 //\r
483 // Build mailbox location in HOB\r
484 //\r
485 MailboxLocationPointer = BuildGuidDataHob (\r
486 &gEfiDebugAgentGuid,\r
487 &MailboxLocation,\r
488 sizeof (UINT64)\r
489 );\r
490 }\r
b422b62c 491 //\r
492 // Update IDT entry to save the location saved mailbox pointer\r
493 //\r
494 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
b422b62c 495 break;\r
496\r
497 case DEBUG_AGENT_INIT_PEI:\r
5ab7f883
JF
498 if (Context == NULL) {\r
499 DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
500 CpuDeadLoop ();\r
501 }\r
b422b62c 502 //\r
503 // Check if Debug Agent has initialized before\r
504 //\r
505 if (IsDebugAgentInitialzed()) {\r
506 DEBUG ((EFI_D_WARN, "Debug Agent: It has already initialized in SEC Core!\n"));\r
507 break;\r
508 }\r
509 //\r
8cc26df4
JF
510 // Install Vector Handoff Info PPI to persist vectors used by Debug Agent\r
511 //\r
512 Status = PeiServicesInstallPpi (&mVectorHandoffInfoPpiList[0]);\r
513 if (EFI_ERROR (Status)) {\r
514 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to install Vector Handoff Info PPI!\n"));\r
515 CpuDeadLoop ();\r
516 }\r
517 //\r
b422b62c 518 // Set up IDT entries\r
519 //\r
520 InitializeDebugIdt ();\r
521 //\r
522 // Build mailbox in HOB and setup Mailbox Set In Pei flag\r
523 //\r
524 Mailbox = AllocateZeroPool (sizeof (DEBUG_AGENT_MAILBOX));\r
8977a7b7
JF
525 if (Mailbox == NULL) {\r
526 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate memory!\n"));\r
527 CpuDeadLoop ();\r
528 }\r
b422b62c 529 MailboxLocation = (UINT64)(UINTN)Mailbox;\r
530 MailboxLocationPointer = BuildGuidDataHob (\r
531 &gEfiDebugAgentGuid,\r
532 &MailboxLocation,\r
533 sizeof (UINT64)\r
534 );\r
08021523
JF
535 //\r
536 // Initialize Debug Timer hardware and save its frequency\r
537 //\r
538 InitializeDebugTimer (&DebugTimerFrequency);\r
539 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_TIMER_FREQUENCY, DebugTimerFrequency);\r
b422b62c 540 //\r
541 // Update IDT entry to save the location pointer saved mailbox pointer\r
542 //\r
543 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
544 //\r
0a488765 545 // Save init arch type when debug agent initialized\r
546 //\r
547 SetDebugFlag (DEBUG_AGENT_FLAG_INIT_ARCH, DEBUG_ARCH_SYMBOL);\r
548 //\r
b422b62c 549 // Register for a callback once memory has been initialized.\r
550 // If memery has been ready, the callback funtion will be invoked immediately\r
551 //\r
552 Status = PeiServicesNotifyPpi (&mMemoryDiscoveredNotifyList[0]);\r
8cc26df4
JF
553 if (EFI_ERROR (Status)) {\r
554 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to register memory discovered callback function!\n"));\r
555 CpuDeadLoop ();\r
556 }\r
b422b62c 557 //\r
558 // Set HOB check flag if memory has not been ready yet\r
559 //\r
560 if (GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY) == 0) {\r
561 SetDebugFlag (DEBUG_AGENT_FLAG_CHECK_MAILBOX_IN_HOB, 1);\r
562 }\r
18b144ea 563\r
5ab7f883 564 Phase2Context.InitFlag = InitFlag;\r
b422b62c 565 Phase2Context.Context = Context;\r
566 Phase2Context.Function = Function;\r
567 DebugPortInitialize ((VOID *) &Phase2Context, InitializeDebugAgentPhase2);\r
18b144ea 568\r
b422b62c 569 FindAndReportModuleImageInfo (4);\r
18b144ea 570\r
b422b62c 571 break;\r
18b144ea 572\r
b422b62c 573 case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64:\r
574 if (Context == NULL) {\r
575 DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
576 CpuDeadLoop ();\r
577 } else {\r
578 Ia32Idtr = (IA32_DESCRIPTOR *) Context;\r
579 Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
580 MailboxLocationPointer = (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
581 (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
582 Mailbox = (DEBUG_AGENT_MAILBOX *) (UINTN)(*MailboxLocationPointer);\r
0a488765 583 //\r
584 // Mailbox should valid and setup before executing thunk code\r
585 //\r
b422b62c 586 VerifyMailboxChecksum (Mailbox);\r
587\r
588 DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((VOID *)(UINTN)Mailbox->DebugPortHandle, NULL);\r
589 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
590 //\r
591 // Set up IDT entries\r
592 //\r
593 InitializeDebugIdt ();\r
594 //\r
595 // Update IDT entry to save location pointer saved the mailbox pointer\r
596 //\r
597 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
598\r
599 FindAndReportModuleImageInfo (4);\r
600 }\r
e2104834 601 break;\r
18b144ea 602\r
e2104834 603 default:\r
18b144ea 604 //\r
4fe43eb3 605 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this\r
e2104834 606 // Debug Agent library instance.\r
18b144ea 607 //\r
e2104834 608 DEBUG ((EFI_D_ERROR, "Debug Agent: The InitFlag value is not allowed!\n"));\r
609 CpuDeadLoop ();\r
610 break;\r
e2104834 611 }\r
18b144ea 612\r
5ab7f883
JF
613 //\r
614 // Enable CPU interrupts so debug timer interrupts can be delivered\r
615 //\r
b422b62c 616 EnableInterrupts ();\r
617\r
e2104834 618 //\r
619 // If Function is not NULL, invoke it always whatever debug agent was initialized sucesssfully or not.\r
620 //\r
621 if (Function != NULL) {\r
622 Function (Context);\r
18b144ea 623 }\r
5ab7f883
JF
624 //\r
625 // Set return status for DEBUG_AGENT_INIT_PEI\r
626 //\r
a1ac5791 627 if (InitFlag == DEBUG_AGENT_INIT_PEI && Context != NULL) {\r
5ab7f883
JF
628 *(EFI_STATUS *)Context = EFI_SUCCESS;\r
629 }\r
18b144ea 630}\r
631\r
632/**\r
633 Caller provided function to be invoked at the end of DebugPortInitialize().\r
634\r
635 Refer to the descrption for DebugPortInitialize() for more details.\r
636\r
637 @param[in] Context The first input argument of DebugPortInitialize().\r
638 @param[in] DebugPortHandle Debug port handle created by Debug Communication Libary.\r
639\r
640**/\r
641VOID\r
642EFIAPI\r
643InitializeDebugAgentPhase2 (\r
644 IN VOID *Context,\r
645 IN DEBUG_PORT_HANDLE DebugPortHandle\r
646 )\r
647{\r
648 DEBUG_AGENT_PHASE2_CONTEXT *Phase2Context;\r
b422b62c 649 UINT64 *MailboxLocation;\r
18b144ea 650 DEBUG_AGENT_MAILBOX *Mailbox;\r
651 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
b422b62c 652 UINT16 BufferSize;\r
653 UINT64 NewDebugPortHandle;\r
18b144ea 654\r
b422b62c 655 Phase2Context = (DEBUG_AGENT_PHASE2_CONTEXT *) Context;\r
656 MailboxLocation = GetLocationSavedMailboxPointerInIdtEntry ();\r
657 Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
658 BufferSize = PcdGet16(PcdDebugPortHandleBufferSize);\r
5ab7f883 659 if (Phase2Context->InitFlag == DEBUG_AGENT_INIT_PEI) {\r
b422b62c 660 NewDebugPortHandle = (UINT64)(UINTN)AllocateCopyPool (BufferSize, DebugPortHandle);\r
661 } else {\r
662 NewDebugPortHandle = (UINT64)(UINTN)DebugPortHandle;\r
663 }\r
664 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, NewDebugPortHandle);\r
18b144ea 665\r
666 //\r
667 // Trigger one software interrupt to inform HOST\r
668 //\r
669 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE);\r
670\r
5ab7f883
JF
671 if (Phase2Context->InitFlag == DEBUG_AGENT_INIT_PREMEM_SEC) {\r
672 //\r
4fe43eb3 673 // If Temporary RAM region is below 128 MB, then send message to\r
5ab7f883
JF
674 // host to disable low memory filtering.\r
675 //\r
676 SecCoreData = (EFI_SEC_PEI_HAND_OFF *)Phase2Context->Context;\r
677 if ((UINTN)SecCoreData->TemporaryRamBase < BASE_128MB && IsHostAttached ()) {\r
678 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
679 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
680 }\r
681 //\r
682 // Enable CPU interrupts so debug timer interrupts can be delivered\r
683 //\r
684 EnableInterrupts ();\r
685 //\r
686 // Call continuation function if it is not NULL.\r
687 //\r
18b144ea 688 Phase2Context->Function (Phase2Context->Context);\r
689 }\r
690}\r