]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SourceLevelDebugPkg/Library/DebugAgent/SecPeiDebugAgent/SecPeiDebugAgentLib.c
SourceLevelDebugPkg: Avoid NULL pointer reference.
[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 - 2015, 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 mMemoryDiscoveredNotifyList[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 = (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
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 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
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\r
380 DisableInterrupts ();\r
381\r
382 switch (InitFlag) {\r
383\r
384 case DEBUG_AGENT_INIT_PREMEM_SEC:\r
385\r
386 InitializeDebugIdt ();\r
387\r
388 MailboxLocation = (UINT64)(UINTN)&MailboxInStack;\r
389 Mailbox = &MailboxInStack;\r
390 ZeroMem ((VOID *) Mailbox, sizeof (DEBUG_AGENT_MAILBOX));\r
391 //\r
392 // Get and save debug port handle and set the length of memory block.\r
393 //\r
394 SetLocationSavedMailboxPointerInIdtEntry (&MailboxLocation);\r
395 //\r
396 // Force error message could be printed during the first shakehand between Target/HOST.\r
397 //\r
398 SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL, DEBUG_AGENT_ERROR);\r
399 //\r
400 // Save init arch type when debug agent initialized\r
401 //\r
402 SetDebugFlag (DEBUG_AGENT_FLAG_INIT_ARCH, DEBUG_ARCH_SYMBOL);\r
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
408\r
409 Phase2Context.InitFlag = InitFlag;\r
410 Phase2Context.Context = Context;\r
411 Phase2Context.Function = Function;\r
412 DebugPortInitialize ((VOID *) &Phase2Context, InitializeDebugAgentPhase2);\r
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
421 Mailbox = GetMailboxPointer ();\r
422 //\r
423 // Memory has been ready\r
424 //\r
425 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
426 if (IsHostAttached ()) {\r
427 //\r
428 // Trigger one software interrupt to inform HOST\r
429 //\r
430 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
431 }\r
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
440 //\r
441 // Fix up Debug Port handle address and mailbox address\r
442 //\r
443 DebugAgentContext = (DEBUG_AGENT_CONTEXT_POSTMEM_SEC *) Context;\r
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
468 if (EFI_ERROR (Status)) {\r
469 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate pages!\n"));\r
470 CpuDeadLoop ();\r
471 }\r
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
491 //\r
492 // Update IDT entry to save the location saved mailbox pointer\r
493 //\r
494 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
495 break;\r
496\r
497 case DEBUG_AGENT_INIT_PEI:\r
498 if (Context == NULL) {\r
499 DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
500 CpuDeadLoop ();\r
501 }\r
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
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
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
525 if (Mailbox == NULL) {\r
526 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to allocate memory!\n"));\r
527 CpuDeadLoop ();\r
528 } else {\r
529 MailboxLocation = (UINT64)(UINTN)Mailbox;\r
530 MailboxLocationPointer = BuildGuidDataHob (\r
531 &gEfiDebugAgentGuid,\r
532 &MailboxLocation,\r
533 sizeof (UINT64)\r
534 );\r
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
540 //\r
541 // Update IDT entry to save the location pointer saved mailbox pointer\r
542 //\r
543 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
544 }\r
545 //\r
546 // Save init arch type when debug agent initialized\r
547 //\r
548 SetDebugFlag (DEBUG_AGENT_FLAG_INIT_ARCH, DEBUG_ARCH_SYMBOL);\r
549 //\r
550 // Register for a callback once memory has been initialized.\r
551 // If memery has been ready, the callback funtion will be invoked immediately\r
552 //\r
553 Status = PeiServicesNotifyPpi (&mMemoryDiscoveredNotifyList[0]);\r
554 if (EFI_ERROR (Status)) {\r
555 DEBUG ((EFI_D_ERROR, "DebugAgent: Failed to register memory discovered callback function!\n"));\r
556 CpuDeadLoop ();\r
557 }\r
558 //\r
559 // Set HOB check flag if memory has not been ready yet\r
560 //\r
561 if (GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY) == 0) {\r
562 SetDebugFlag (DEBUG_AGENT_FLAG_CHECK_MAILBOX_IN_HOB, 1);\r
563 }\r
564\r
565 Phase2Context.InitFlag = InitFlag;\r
566 Phase2Context.Context = Context;\r
567 Phase2Context.Function = Function;\r
568 DebugPortInitialize ((VOID *) &Phase2Context, InitializeDebugAgentPhase2);\r
569\r
570 FindAndReportModuleImageInfo (4);\r
571\r
572 break;\r
573\r
574 case DEBUG_AGENT_INIT_THUNK_PEI_IA32TOX64:\r
575 if (Context == NULL) {\r
576 DEBUG ((EFI_D_ERROR, "DebugAgent: Input parameter Context cannot be NULL!\n"));\r
577 CpuDeadLoop ();\r
578 } else {\r
579 Ia32Idtr = (IA32_DESCRIPTOR *) Context;\r
580 Ia32IdtEntry = (IA32_IDT_ENTRY *)(Ia32Idtr->Base);\r
581 MailboxLocationPointer = (UINT64 *) (UINTN) (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetLow +\r
582 (Ia32IdtEntry[DEBUG_MAILBOX_VECTOR].Bits.OffsetHigh << 16));\r
583 Mailbox = (DEBUG_AGENT_MAILBOX *) (UINTN)(*MailboxLocationPointer);\r
584 //\r
585 // Mailbox should valid and setup before executing thunk code\r
586 //\r
587 VerifyMailboxChecksum (Mailbox);\r
588\r
589 DebugPortHandle = (UINT64) (UINTN)DebugPortInitialize ((VOID *)(UINTN)Mailbox->DebugPortHandle, NULL);\r
590 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, DebugPortHandle);\r
591 //\r
592 // Set up IDT entries\r
593 //\r
594 InitializeDebugIdt ();\r
595 //\r
596 // Update IDT entry to save location pointer saved the mailbox pointer\r
597 //\r
598 SetLocationSavedMailboxPointerInIdtEntry (MailboxLocationPointer);\r
599\r
600 FindAndReportModuleImageInfo (4);\r
601 }\r
602 break;\r
603\r
604 default:\r
605 //\r
606 // Only DEBUG_AGENT_INIT_PREMEM_SEC and DEBUG_AGENT_INIT_POSTMEM_SEC are allowed for this\r
607 // Debug Agent library instance.\r
608 //\r
609 DEBUG ((EFI_D_ERROR, "Debug Agent: The InitFlag value is not allowed!\n"));\r
610 CpuDeadLoop ();\r
611 break;\r
612 }\r
613\r
614 //\r
615 // Enable CPU interrupts so debug timer interrupts can be delivered\r
616 //\r
617 EnableInterrupts ();\r
618\r
619 //\r
620 // If Function is not NULL, invoke it always whatever debug agent was initialized sucesssfully or not.\r
621 //\r
622 if (Function != NULL) {\r
623 Function (Context);\r
624 }\r
625 //\r
626 // Set return status for DEBUG_AGENT_INIT_PEI\r
627 //\r
628 if (InitFlag == DEBUG_AGENT_INIT_PEI && Context != NULL) {\r
629 *(EFI_STATUS *)Context = EFI_SUCCESS;\r
630 }\r
631}\r
632\r
633/**\r
634 Caller provided function to be invoked at the end of DebugPortInitialize().\r
635\r
636 Refer to the descrption for DebugPortInitialize() for more details.\r
637\r
638 @param[in] Context The first input argument of DebugPortInitialize().\r
639 @param[in] DebugPortHandle Debug port handle created by Debug Communication Libary.\r
640\r
641**/\r
642VOID\r
643EFIAPI\r
644InitializeDebugAgentPhase2 (\r
645 IN VOID *Context,\r
646 IN DEBUG_PORT_HANDLE DebugPortHandle\r
647 )\r
648{\r
649 DEBUG_AGENT_PHASE2_CONTEXT *Phase2Context;\r
650 UINT64 *MailboxLocation;\r
651 DEBUG_AGENT_MAILBOX *Mailbox;\r
652 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
653 UINT16 BufferSize;\r
654 UINT64 NewDebugPortHandle;\r
655\r
656 Phase2Context = (DEBUG_AGENT_PHASE2_CONTEXT *) Context;\r
657 MailboxLocation = GetLocationSavedMailboxPointerInIdtEntry ();\r
658 Mailbox = (DEBUG_AGENT_MAILBOX *)(UINTN)(*MailboxLocation);\r
659 BufferSize = PcdGet16(PcdDebugPortHandleBufferSize);\r
660 if (Phase2Context->InitFlag == DEBUG_AGENT_INIT_PEI) {\r
661 NewDebugPortHandle = (UINT64)(UINTN)AllocateCopyPool (BufferSize, DebugPortHandle);\r
662 } else {\r
663 NewDebugPortHandle = (UINT64)(UINTN)DebugPortHandle;\r
664 }\r
665 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX, NewDebugPortHandle);\r
666\r
667 //\r
668 // Trigger one software interrupt to inform HOST\r
669 //\r
670 TriggerSoftInterrupt (SYSTEM_RESET_SIGNATURE);\r
671\r
672 if (Phase2Context->InitFlag == DEBUG_AGENT_INIT_PREMEM_SEC) {\r
673 //\r
674 // If Temporary RAM region is below 128 MB, then send message to\r
675 // host to disable low memory filtering.\r
676 //\r
677 SecCoreData = (EFI_SEC_PEI_HAND_OFF *)Phase2Context->Context;\r
678 if ((UINTN)SecCoreData->TemporaryRamBase < BASE_128MB && IsHostAttached ()) {\r
679 SetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY, 1);\r
680 TriggerSoftInterrupt (MEMORY_READY_SIGNATURE);\r
681 }\r
682 //\r
683 // Enable CPU interrupts so debug timer interrupts can be delivered\r
684 //\r
685 EnableInterrupts ();\r
686 //\r
687 // Call continuation function if it is not NULL.\r
688 //\r
689 Phase2Context->Function (Phase2Context->Context);\r
690 }\r
691}\r