]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugAgent.c
f4039f49952cc051e27d06f6849db5259783a460
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugAgent / DebugAgentCommon / DebugAgent.c
1 /** @file
2 Commond Debug Agent library implementition. It mainly includes
3 the first C function called by exception/interrupt handlers,
4 read/write debug packet to communication with HOST based on transfer
5 protocol.
6
7 Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php.
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include "DebugAgent.h"
19 #include "Ia32/DebugException.h"
20
21 CHAR8 mErrorMsgVersionAlert[] = "\rThe SourceLevelDebugPkg you are using requires a newer version of the Intel(R) UDK Debugger Tool.\r\n";
22 CHAR8 mErrorMsgSendInitPacket[] = "\rSend INIT break packet and try to connect the HOST (Intel(R) UDK Debugger Tool v1.3) ...\r\n";
23 CHAR8 mErrorMsgConnectOK[] = "HOST connection is successful!\r\n";
24 CHAR8 mErrorMsgConnectFail[] = "HOST connection is failed!\r\n";
25 CHAR8 mWarningMsgIngoreBreakpoint[] = "Ignore break point in SMM for SMI issued during DXE debugging!\r\n";
26
27 /**
28 Calculate CRC16 for target data.
29
30 @param[in] Data The target data.
31 @param[in] DataSize The target data size.
32 @param[in] Crc Initial CRC.
33
34 @return UINT16 The CRC16 value.
35
36 **/
37 UINT16
38 CalculateCrc16 (
39 IN UINT8 *Data,
40 IN UINTN DataSize,
41 IN UINT16 Crc
42 )
43 {
44 UINTN Index;
45 UINTN BitIndex;
46
47 for (Index = 0; Index < DataSize; Index++) {
48 Crc ^= Data[Index];
49 for (BitIndex = 0; BitIndex < 8; BitIndex++) {
50 if ((Crc & 0x8000) != 0) {
51 Crc <<= 1;
52 Crc ^= 0x1021;
53 } else {
54 Crc <<= 1;
55 }
56 }
57 }
58 return Crc;
59 }
60
61
62 /**
63 Read IDT entry to check if IDT entries are setup by Debug Agent.
64
65 @retval TRUE IDT entries were setup by Debug Agent.
66 @retval FALSE IDT entries were not setup by Debug Agent.
67
68 **/
69 BOOLEAN
70 IsDebugAgentInitialzed (
71 VOID
72 )
73 {
74 UINTN InterruptHandler;
75
76 InterruptHandler = (UINTN) GetExceptionHandlerInIdtEntry (0);
77 if (InterruptHandler >= 4 && *(UINT32 *)(InterruptHandler - 4) == AGENT_HANDLER_SIGNATURE) {
78 return TRUE;
79 } else {
80 return FALSE;
81 }
82 }
83
84 /**
85 Find and report module image info to HOST.
86
87 @param[in] AlignSize Image aligned size.
88
89 **/
90 VOID
91 FindAndReportModuleImageInfo (
92 IN UINTN AlignSize
93 )
94 {
95 UINTN Pe32Data;
96 EFI_IMAGE_DOS_HEADER *DosHdr;
97 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
98 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
99
100 //
101 // Find Image Base
102 //
103 Pe32Data = ((UINTN)mErrorMsgVersionAlert) & ~(AlignSize - 1);
104 while (Pe32Data != 0) {
105 DosHdr = (EFI_IMAGE_DOS_HEADER *) Pe32Data;
106 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
107 //
108 // DOS image header is present, so read the PE header after the DOS image header.
109 // Check if address overflow firstly.
110 //
111 if ((MAX_ADDRESS - (UINTN)DosHdr->e_lfanew) > Pe32Data) {
112 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data + (UINTN)(DosHdr->e_lfanew));
113 if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
114 //
115 // It's PE image.
116 //
117 break;
118 }
119 }
120 } else {
121 //
122 // DOS image header is not present, TE header is at the image base.
123 //
124 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
125 if ((Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) &&
126 ((Hdr.Te->Machine == IMAGE_FILE_MACHINE_I386) || Hdr.Te->Machine == IMAGE_FILE_MACHINE_X64)) {
127 //
128 // It's TE image, it TE header and Machine type match
129 //
130 break;
131 }
132 }
133
134 //
135 // Not found the image base, check the previous aligned address
136 //
137 Pe32Data -= AlignSize;
138 }
139
140 ImageContext.ImageAddress = Pe32Data;
141 ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress);
142 PeCoffLoaderRelocateImageExtraAction (&ImageContext);
143 }
144
145 /**
146 Trigger one software interrupt to debug agent to handle it.
147
148 @param[in] Signature Software interrupt signature.
149
150 **/
151 VOID
152 TriggerSoftInterrupt (
153 IN UINT32 Signature
154 )
155 {
156 UINTN Dr0;
157 UINTN Dr1;
158
159 //
160 // Save Debug Register State
161 //
162 Dr0 = AsmReadDr0 ();
163 Dr1 = AsmReadDr1 ();
164
165 //
166 // DR0 = Signature
167 //
168 AsmWriteDr0 (SOFT_INTERRUPT_SIGNATURE);
169 AsmWriteDr1 (Signature);
170
171 //
172 // Do INT3 to communicate with HOST side
173 //
174 CpuBreakpoint ();
175
176 //
177 // Restore Debug Register State only when Host didn't change it inside exception handler.
178 // Dr registers can only be changed by setting the HW breakpoint.
179 //
180 AsmWriteDr0 (Dr0);
181 AsmWriteDr1 (Dr1);
182
183 }
184
185 /**
186 Caculate Mailbox checksum and update the checksum field.
187
188 @param[in] Mailbox Debug Agent Mailbox pointer.
189
190 **/
191 VOID
192 UpdateMailboxChecksum (
193 IN DEBUG_AGENT_MAILBOX *Mailbox
194 )
195 {
196 Mailbox->CheckSum = CalculateCheckSum8 ((UINT8 *)Mailbox, sizeof (DEBUG_AGENT_MAILBOX) - 2);
197 }
198
199 /**
200 Verify Mailbox checksum.
201
202 If checksum error, print debug message and run init dead loop.
203
204 @param[in] Mailbox Debug Agent Mailbox pointer.
205
206 **/
207 VOID
208 VerifyMailboxChecksum (
209 IN DEBUG_AGENT_MAILBOX *Mailbox
210 )
211 {
212 UINT8 CheckSum;
213
214 CheckSum = CalculateCheckSum8 ((UINT8 *) Mailbox, sizeof (DEBUG_AGENT_MAILBOX) - 2);
215 //
216 // The checksum updating process may be disturbed by hardware SMI, we need to check CheckSum field
217 // and ToBeCheckSum field to validate the mail box.
218 //
219 if (CheckSum != Mailbox->CheckSum && CheckSum != Mailbox->ToBeCheckSum) {
220 DEBUG ((EFI_D_ERROR, "DebugAgent: Mailbox checksum error, stack or heap crashed!\n"));
221 DEBUG ((EFI_D_ERROR, "DebugAgent: CheckSum = %x, Mailbox->CheckSum = %x, Mailbox->ToBeCheckSum = %x\n", CheckSum, Mailbox->CheckSum, Mailbox->ToBeCheckSum));
222 CpuDeadLoop ();
223 }
224 }
225
226 /**
227 Update Mailbox content by index.
228
229 @param[in] Mailbox Debug Agent Mailbox pointer.
230 @param[in] Index Mailbox content index.
231 @param[in] Value Value to be set into Mailbox.
232
233 **/
234 VOID
235 UpdateMailboxContent (
236 IN DEBUG_AGENT_MAILBOX *Mailbox,
237 IN UINTN Index,
238 IN UINT64 Value
239 )
240 {
241 AcquireMpSpinLock (&mDebugMpContext.MailboxSpinLock);
242 switch (Index) {
243 case DEBUG_MAILBOX_DEBUG_FLAG_INDEX:
244 Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->DebugFlag.Uint64, sizeof(UINT64))
245 - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT64));
246 Mailbox->DebugFlag.Uint64 = Value;
247 break;
248 case DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX:
249 Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->DebugPortHandle, sizeof(UINTN))
250 - CalculateSum8 ((UINT8 *)&Value, sizeof(UINTN));
251 Mailbox->DebugPortHandle = (UINTN) Value;
252 break;
253 case DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX:
254 Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->ExceptionBufferPointer, sizeof(UINTN))
255 - CalculateSum8 ((UINT8 *)&Value, sizeof(UINTN));
256 Mailbox->ExceptionBufferPointer = (UINTN) Value;
257 break;
258 case DEBUG_MAILBOX_LAST_ACK:
259 Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->LastAck, sizeof(UINT8))
260 - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));
261 Mailbox->LastAck = (UINT8) Value;
262 break;
263 case DEBUG_MAILBOX_SEQUENCE_NO_INDEX:
264 Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->SequenceNo, sizeof(UINT8))
265 - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));
266 Mailbox->SequenceNo = (UINT8) Value;
267 break;
268 case DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX:
269 Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->HostSequenceNo, sizeof(UINT8))
270 - CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));
271 Mailbox->HostSequenceNo = (UINT8) Value;
272 break;
273 }
274 UpdateMailboxChecksum (Mailbox);
275 ReleaseMpSpinLock (&mDebugMpContext.MailboxSpinLock);
276 }
277 /**
278 Set debug flag in mailbox.
279
280 @param[in] FlagMask Debug flag mask value.
281 @param[in] FlagValue Debug flag value.
282
283 **/
284 VOID
285 SetDebugFlag (
286 IN UINT64 FlagMask,
287 IN UINT32 FlagValue
288 )
289 {
290 DEBUG_AGENT_MAILBOX *Mailbox;
291 UINT64 Data64;
292
293 Mailbox = GetMailboxPointer ();
294 Data64 = (Mailbox->DebugFlag.Uint64 & ~FlagMask) |
295 (LShiftU64 ((UINT64)FlagValue, LowBitSet64 (FlagMask)) & FlagMask);
296 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_FLAG_INDEX, Data64);
297 }
298
299 /**
300 Get debug flag in mailbox.
301
302 @param[in] FlagMask Debug flag mask value.
303
304 @return Debug flag value.
305
306 **/
307 UINT32
308 GetDebugFlag (
309 IN UINT64 FlagMask
310 )
311 {
312 DEBUG_AGENT_MAILBOX *Mailbox;
313 UINT32 DebugFlag;
314
315 Mailbox = GetMailboxPointer ();
316 DebugFlag = (UINT32) RShiftU64 (Mailbox->DebugFlag.Uint64 & FlagMask, LowBitSet64 (FlagMask));
317
318 return DebugFlag;
319 }
320
321 /**
322 Send a debug message packet to the debug port.
323
324 @param[in] Buffer The debug message.
325 @param[in] Length The length of debug message.
326
327 **/
328 VOID
329 SendDebugMsgPacket (
330 IN CHAR8 *Buffer,
331 IN UINTN Length
332 )
333 {
334 DEBUG_PACKET_HEADER DebugHeader;
335 DEBUG_PORT_HANDLE Handle;
336
337 Handle = GetDebugPortHandle();
338
339 DebugHeader.StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;
340 DebugHeader.Command = DEBUG_COMMAND_PRINT_MESSAGE;
341 DebugHeader.Length = sizeof (DEBUG_PACKET_HEADER) + (UINT8) Length;
342 DebugHeader.SequenceNo = 0xEE;
343 DebugHeader.Crc = 0;
344 DebugHeader.Crc = CalculateCrc16 (
345 (UINT8 *)Buffer, Length,
346 CalculateCrc16 ((UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER), 0)
347 );
348
349 DebugPortWriteBuffer (Handle, (UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER));
350 DebugPortWriteBuffer (Handle, (UINT8 *)Buffer, Length);
351 }
352
353 /**
354 Prints a debug message to the debug port if the specified error level is enabled.
355
356 If any bit in ErrorLevel is also set in Mainbox, then print the message specified
357 by Format and the associated variable argument list to the debug port.
358
359 @param[in] ErrorLevel The error level of the debug message.
360 @param[in] Format Format string for the debug message to print.
361 @param[in] ... Variable argument list whose contents are accessed
362 based on the format string specified by Format.
363
364 **/
365 VOID
366 EFIAPI
367 DebugAgentMsgPrint (
368 IN UINT8 ErrorLevel,
369 IN CHAR8 *Format,
370 ...
371 )
372 {
373 CHAR8 Buffer[DEBUG_DATA_MAXIMUM_REAL_DATA];
374 VA_LIST Marker;
375
376 //
377 // Check driver debug mask value and global mask
378 //
379 if ((ErrorLevel & GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL)) == 0) {
380 return;
381 }
382
383 //
384 // Convert the DEBUG() message to an ASCII String
385 //
386 VA_START (Marker, Format);
387 AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
388 VA_END (Marker);
389
390 SendDebugMsgPacket (Buffer, AsciiStrLen (Buffer));
391 }
392
393 /**
394 Prints a debug message to the debug output device if the specified error level is enabled.
395
396 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
397 GetDebugPrintErrorLevel (), then print the message specified by Format and the
398 associated variable argument list to the debug output device.
399
400 If Format is NULL, then ASSERT().
401
402 @param[in] ErrorLevel The error level of the debug message.
403 @param[in] IsSend Flag of debug message to declare that the data is being sent or being received.
404 @param[in] Data Variable argument list whose contents are accessed
405 @param[in] Length based on the format string specified by Format.
406
407 **/
408 VOID
409 EFIAPI
410 DebugAgentDataMsgPrint (
411 IN UINT8 ErrorLevel,
412 IN BOOLEAN IsSend,
413 IN UINT8 *Data,
414 IN UINT8 Length
415 )
416 {
417 CHAR8 Buffer[DEBUG_DATA_MAXIMUM_REAL_DATA];
418 CHAR8 *DestBuffer;
419 UINTN Index;
420
421 //
422 // Check driver debug mask value and global mask
423 //
424 if ((ErrorLevel & GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL)) == 0) {
425 return;
426 }
427
428 DestBuffer = Buffer;
429 if (IsSend) {
430 DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA, "Sent data [ ");
431 } else {
432 DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA, "Received data [ ");
433 }
434
435 Index = 0;
436 while (TRUE) {
437 if (DestBuffer - Buffer > DEBUG_DATA_MAXIMUM_REAL_DATA - 6) {
438 //
439 // If there was no enough space in buffer, send out the debug message,
440 // reserving 6 bytes is for the last data and end characters "]\n".
441 //
442 SendDebugMsgPacket (Buffer, DestBuffer - Buffer);
443 DestBuffer = Buffer;
444 }
445 DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA - (DestBuffer - Buffer), "%02x ", Data[Index]);
446 Index ++;
447 if (Index >= Length) {
448 //
449 // The last character of debug message has been foramtted in buffer
450 //
451 DestBuffer += AsciiSPrint(DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA - (DestBuffer - Buffer), "]\n");
452 SendDebugMsgPacket (Buffer, DestBuffer - Buffer);
453 break;
454 }
455 }
456 }
457
458 /**
459 Read remaing debug packet except for the start symbol
460
461 @param[in] Handle Pointer to Debug Port handle.
462 @param[in, out] DebugHeader Debug header buffer including start symbol.
463
464 @retval EFI_SUCCESS Read the symbol in BreakSymbol.
465 @retval EFI_CRC_ERROR CRC check fail.
466 @retval EFI_TIMEOUT Timeout occurs when reading debug packet.
467
468 **/
469 EFI_STATUS
470 ReadRemainingBreakPacket (
471 IN DEBUG_PORT_HANDLE Handle,
472 IN OUT DEBUG_PACKET_HEADER *DebugHeader
473 )
474 {
475 UINT16 Crc;
476 //
477 // Has received start symbol, try to read the rest part
478 //
479 if (DebugPortReadBuffer (Handle, &DebugHeader->Command, sizeof (DEBUG_PACKET_HEADER) - 1, READ_PACKET_TIMEOUT) == 0) {
480 //
481 // Timeout occur, exit
482 //
483 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Timeout in Debug Timer interrupt\n");
484 return EFI_TIMEOUT;
485 }
486
487 Crc = DebugHeader->Crc;
488 DebugHeader->Crc = 0;
489 if (CalculateCrc16 ((UINT8 *)DebugHeader, DebugHeader->Length, 0) != Crc) {
490 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Debug Timer CRC (%x) against (%x)\n", Crc, CalculateCrc16 ((UINT8 *) &DebugHeader, DebugHeader->Length, 0));
491 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *)DebugHeader, DebugHeader->Length);
492 return EFI_CRC_ERROR;
493 }
494
495 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, DebugHeader->SequenceNo);
496 return EFI_SUCCESS;
497 }
498
499 /**
500 Check if HOST is attached based on Mailbox.
501
502 @retval TRUE HOST is attached.
503 @retval FALSE HOST is not attached.
504
505 **/
506 BOOLEAN
507 IsHostAttached (
508 VOID
509 )
510 {
511 return (BOOLEAN) (GetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED) == 1);
512 }
513
514 /**
515 Set HOST connect flag in Mailbox.
516
517 @param[in] Attached Attach status.
518
519 **/
520 VOID
521 SetHostAttached (
522 IN BOOLEAN Attached
523 )
524 {
525 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Attach status is %d\n", Attached);
526 SetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED, (UINT32)Attached);
527 }
528
529 /**
530 Set debug setting of Debug Agent in Mailbox.
531
532 @param DebugSetting Pointer to Debug Setting defined by transfer protocol.
533
534 @retval RETURN_SUCCESS The setting is set successfully.
535 @retval RETURN_UNSUPPORTED The Key value is not supported.
536
537 **/
538 RETURN_STATUS
539 SetDebugSetting (
540 IN DEBUG_DATA_SET_DEBUG_SETTING *DebugSetting
541 )
542 {
543 RETURN_STATUS Status;
544
545 Status = RETURN_SUCCESS;
546 switch (DebugSetting->Key) {
547 case DEBUG_AGENT_SETTING_SMM_ENTRY_BREAK:
548 SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI, DebugSetting->Value);
549 break;
550 case DEBUG_AGENT_SETTING_PRINT_ERROR_LEVEL:
551 SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL, DebugSetting->Value);
552 break;
553 case DEBUG_AGENT_SETTING_BOOT_SCRIPT_ENTRY_BREAK:
554 SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT, DebugSetting->Value);
555 break;
556 default:
557 Status = RETURN_UNSUPPORTED;
558 }
559 return Status;
560 }
561
562 /**
563 Exectue GO command.
564
565 @param[in] CpuContext Pointer to saved CPU context.
566
567 **/
568 VOID
569 CommandGo (
570 IN DEBUG_CPU_CONTEXT *CpuContext
571 )
572 {
573 IA32_EFLAGS32 *Eflags;
574
575 Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags;
576 Eflags->Bits.TF = 0;
577 Eflags->Bits.RF = 1;
578 }
579
580 /**
581 Exectue Stepping command.
582
583 @param[in] CpuContext Pointer to saved CPU context.
584
585 **/
586 VOID
587 CommandStepping (
588 IN DEBUG_CPU_CONTEXT *CpuContext
589 )
590 {
591 IA32_EFLAGS32 *Eflags;
592
593 Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags;
594 Eflags->Bits.TF = 1;
595 Eflags->Bits.RF = 1;
596 }
597
598 /**
599 Set debug register for hardware breakpoint.
600
601 @param[in] CpuContext Pointer to saved CPU context.
602 @param[in] SetHwBreakpoint Hardware breakpoint to be set.
603
604 **/
605 VOID
606 SetDebugRegister (
607 IN DEBUG_CPU_CONTEXT *CpuContext,
608 IN DEBUG_DATA_SET_HW_BREAKPOINT *SetHwBreakpoint
609 )
610 {
611 UINT8 RegisterIndex;
612 UINTN Dr7Value;
613
614 RegisterIndex = SetHwBreakpoint->Type.Index;
615
616 //
617 // Set debug address
618 //
619 * ((UINTN *) &CpuContext->Dr0 + RegisterIndex) = (UINTN) SetHwBreakpoint->Address;
620
621 Dr7Value = CpuContext->Dr7;
622
623 //
624 // Enable Gx, Lx
625 //
626 Dr7Value |= 0x3 << (RegisterIndex * 2);
627 //
628 // Set RWx and Lenx
629 //
630 Dr7Value &= ~(0xf << (16 + RegisterIndex * 4));
631 Dr7Value |= (UINTN) ((SetHwBreakpoint->Type.Length << 2) | SetHwBreakpoint->Type.Access) << (16 + RegisterIndex * 4);
632 //
633 // Enable GE, LE
634 //
635 Dr7Value |= 0x300;
636
637 CpuContext->Dr7 = Dr7Value;
638 }
639
640 /**
641 Clear debug register for hardware breakpoint.
642
643 @param[in] CpuContext Pointer to saved CPU context.
644 @param[in] ClearHwBreakpoint Hardware breakpoint to be cleared.
645
646 **/
647 VOID
648 ClearDebugRegister (
649 IN DEBUG_CPU_CONTEXT *CpuContext,
650 IN DEBUG_DATA_CLEAR_HW_BREAKPOINT *ClearHwBreakpoint
651 )
652 {
653 if ((ClearHwBreakpoint->IndexMask & BIT0) != 0) {
654 CpuContext->Dr0 = 0;
655 CpuContext->Dr7 &= ~(0x3 << 0);
656 }
657 if ((ClearHwBreakpoint->IndexMask & BIT1) != 0) {
658 CpuContext->Dr1 = 0;
659 CpuContext->Dr7 &= ~(0x3 << 2);
660 }
661 if ((ClearHwBreakpoint->IndexMask & BIT2) != 0) {
662 CpuContext->Dr2 = 0;
663 CpuContext->Dr7 &= ~(0x3 << 4);
664 }
665 if ((ClearHwBreakpoint->IndexMask & BIT3) != 0) {
666 CpuContext->Dr3 = 0;
667 CpuContext->Dr7 &= ~(0x3 << 6);
668 }
669 }
670
671
672 /**
673 Return the offset of FP / MMX / XMM registers in the FPU saved state by register index.
674
675 @param[in] Index Register index.
676 @param[out] Width Register width returned.
677
678 @return Offset in the FPU Save State.
679
680 **/
681 UINT16
682 ArchReadFxStatOffset (
683 IN UINT8 Index,
684 OUT UINT8 *Width
685 )
686 {
687 if (Index < SOFT_DEBUGGER_REGISTER_ST0) {
688 switch (Index) {
689 case SOFT_DEBUGGER_REGISTER_FP_FCW:
690 *Width = (UINT8) sizeof (UINT16);
691 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Fcw);
692
693 case SOFT_DEBUGGER_REGISTER_FP_FSW:
694 *Width = (UINT8) sizeof (UINT16);
695 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Fsw);
696
697 case SOFT_DEBUGGER_REGISTER_FP_FTW:
698 *Width = (UINT8) sizeof (UINT16);
699 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Ftw);
700
701 case SOFT_DEBUGGER_REGISTER_FP_OPCODE:
702 *Width = (UINT8) sizeof (UINT16);
703 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Opcode);
704
705 case SOFT_DEBUGGER_REGISTER_FP_EIP:
706 *Width = (UINT8) sizeof (UINT32);
707 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Eip);
708
709 case SOFT_DEBUGGER_REGISTER_FP_CS:
710 *Width = (UINT8) sizeof (UINT16);
711 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Cs);
712
713 case SOFT_DEBUGGER_REGISTER_FP_DATAOFFSET:
714 *Width = (UINT8) sizeof (UINT32);
715 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, DataOffset);
716
717 case SOFT_DEBUGGER_REGISTER_FP_DS:
718 *Width = (UINT8) sizeof (UINT16);
719 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Ds);
720
721 case SOFT_DEBUGGER_REGISTER_FP_MXCSR:
722 *Width = (UINT8) sizeof (UINT32);
723 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Mxcsr);
724
725 case SOFT_DEBUGGER_REGISTER_FP_MXCSR_MASK:
726 *Width = (UINT8) sizeof (UINT32);
727 return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Mxcsr_Mask);
728 }
729 }
730
731 if (Index <= SOFT_DEBUGGER_REGISTER_ST7) {
732 *Width = 10;
733 } else if (Index <= SOFT_DEBUGGER_REGISTER_XMM15) {
734 *Width = 16;
735 } else {
736 //
737 // MMX register
738 //
739 *Width = 8;
740 Index -= SOFT_DEBUGGER_REGISTER_MM0 - SOFT_DEBUGGER_REGISTER_ST0;
741 }
742
743 return OFFSET_OF (DEBUG_DATA_FX_SAVE_STATE, St0Mm0) + (Index - SOFT_DEBUGGER_REGISTER_ST0) * 16;
744 }
745
746 /**
747 Return the pointer of the register value in the CPU saved context.
748
749 @param[in] CpuContext Pointer to saved CPU context.
750 @param[in] Index Register index value.
751 @param[out] Width Data width to read.
752
753 @return The pointer in the CPU saved context.
754
755 **/
756 UINT8 *
757 ArchReadRegisterBuffer (
758 IN DEBUG_CPU_CONTEXT *CpuContext,
759 IN UINT8 Index,
760 OUT UINT8 *Width
761 )
762 {
763 UINT8 *Buffer;
764
765 if (Index < SOFT_DEBUGGER_REGISTER_FP_BASE) {
766 Buffer = (UINT8 *) CpuContext + OFFSET_OF (DEBUG_CPU_CONTEXT, Dr0) + Index * sizeof (UINTN);
767 *Width = (UINT8) sizeof (UINTN);
768 } else {
769 //
770 // FPU/MMX/XMM registers
771 //
772 Buffer = (UINT8 *) CpuContext + OFFSET_OF (DEBUG_CPU_CONTEXT, FxSaveState) + ArchReadFxStatOffset (Index, Width);
773 }
774
775 return Buffer;
776 }
777
778 /**
779 Send the packet without data to HOST.
780
781 @param[in] CommandType Type of Command.
782 @param[in] SequenceNo Sequence number.
783
784 **/
785 VOID
786 SendPacketWithoutData (
787 IN UINT8 CommandType,
788 IN UINT8 SequenceNo
789 )
790 {
791 DEBUG_PACKET_HEADER DebugHeader;
792 DEBUG_PORT_HANDLE Handle;
793
794 Handle = GetDebugPortHandle();
795
796 DebugHeader.StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;
797 DebugHeader.Command = CommandType;
798 DebugHeader.Length = sizeof (DEBUG_PACKET_HEADER);
799 DebugHeader.SequenceNo = SequenceNo;
800 DebugHeader.Crc = 0;
801 DebugHeader.Crc = CalculateCrc16 ((UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER), 0);
802
803 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, TRUE, (UINT8 *) &DebugHeader, DebugHeader.Length);
804 DebugPortWriteBuffer (Handle, (UINT8 *) &DebugHeader, DebugHeader.Length);
805 }
806
807 /**
808 Send acknowledge packet to HOST.
809
810 @param[in] AckCommand Type of Acknowledge packet.
811
812 **/
813 VOID
814 SendAckPacket (
815 IN UINT8 AckCommand
816 )
817 {
818 UINT8 SequenceNo;
819 DEBUG_AGENT_MAILBOX *Mailbox;
820
821 if (AckCommand != DEBUG_COMMAND_OK) {
822 //
823 // This is not ACK OK packet
824 //
825 DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Send ACK(%d)\n", AckCommand);
826 }
827 Mailbox = GetMailboxPointer();
828 SequenceNo = Mailbox->HostSequenceNo;
829 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "SendAckPacket: SequenceNo = %x\n", SequenceNo);
830 SendPacketWithoutData (AckCommand, SequenceNo);
831 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_LAST_ACK, AckCommand);
832 }
833
834 /**
835 Receive valid packet from HOST.
836
837 @param[out] InputPacket Buffer to receive packet.
838 @param[out] BreakReceived TRUE means break-in symbol received.
839 FALSE means break-in symbol not received.
840 @param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return
841 TRUE: Compatible packet received.
842 FALSE: Incompatible packet received.
843 @param[in] Timeout Time out value to wait for acknowlege from HOST.
844 The unit is microsecond.
845 @param[in] SkipStartSymbol TRUE: Skip time out when reading start symbol.
846 FALSE: Does not Skip time out when reading start symbol.
847
848 @retval RETURN_SUCCESS A valid package was reveived in InputPacket.
849 @retval RETURN_TIMEOUT Timeout occurs.
850
851 **/
852 RETURN_STATUS
853 ReceivePacket (
854 OUT UINT8 *InputPacket,
855 OUT BOOLEAN *BreakReceived,
856 OUT BOOLEAN *IncompatibilityFlag, OPTIONAL
857 IN UINTN Timeout,
858 IN BOOLEAN SkipStartSymbol
859 )
860 {
861 DEBUG_PACKET_HEADER *DebugHeader;
862 UINTN Received;
863 DEBUG_PORT_HANDLE Handle;
864 UINT16 Crc;
865 UINTN TimeoutForStartSymbol;
866
867 Handle = GetDebugPortHandle();
868 if (SkipStartSymbol) {
869 TimeoutForStartSymbol = 0;
870 } else {
871 TimeoutForStartSymbol = Timeout;
872 }
873
874 DebugHeader = (DEBUG_PACKET_HEADER *) InputPacket;
875 while (TRUE) {
876 //
877 // Find the valid start symbol
878 //
879 Received = DebugPortReadBuffer (Handle, &DebugHeader->StartSymbol, sizeof (DebugHeader->StartSymbol), TimeoutForStartSymbol);
880 if (Received < sizeof (DebugHeader->StartSymbol)) {
881 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "DebugPortReadBuffer(StartSymbol) timeout\n");
882 return RETURN_TIMEOUT;
883 }
884
885 if (DebugHeader->StartSymbol != DEBUG_STARTING_SYMBOL_NORMAL) {
886 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Invalid start symbol received [%02x]\n", DebugHeader->StartSymbol);
887 continue;
888 }
889
890 //
891 // Read Package header till field Length
892 //
893 Received = DebugPortReadBuffer (
894 Handle,
895 &DebugHeader->Command,
896 OFFSET_OF (DEBUG_PACKET_HEADER, Length) + sizeof (DebugHeader->Length) - sizeof (DebugHeader->StartSymbol),
897 Timeout
898 );
899 if (Received == 0) {
900 DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "DebugPortReadBuffer(Command) timeout\n");
901 return RETURN_TIMEOUT;
902 }
903 if (DebugHeader->Length < sizeof (DEBUG_PACKET_HEADER)) {
904 if (IncompatibilityFlag != NULL) {
905 //
906 // This is one old version debug packet format, set Incompatibility flag
907 //
908 *IncompatibilityFlag = TRUE;
909 } else {
910 //
911 // Skip the bad small packet
912 //
913 continue;
914 }
915 } else {
916 //
917 // Read the payload data include the CRC field
918 //
919 Received = DebugPortReadBuffer (Handle, &DebugHeader->SequenceNo, (UINT8) (DebugHeader->Length - OFFSET_OF (DEBUG_PACKET_HEADER, SequenceNo)), Timeout);
920 if (Received == 0) {
921 DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "DebugPortReadBuffer(SequenceNo) timeout\n");
922 return RETURN_TIMEOUT;
923 }
924 //
925 // Calculate the CRC of Debug Packet
926 //
927 Crc = DebugHeader->Crc;
928 DebugHeader->Crc = 0;
929 if (Crc == CalculateCrc16 ((UINT8 *) DebugHeader, DebugHeader->Length, 0)) {
930 break;
931 }
932 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "CRC Error (received CRC is %x)\n", Crc);
933 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *) DebugHeader, DebugHeader->Length);
934 }
935 }
936
937 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *) DebugHeader, DebugHeader->Length);
938
939 return RETURN_SUCCESS;
940 }
941
942 /**
943 Receive acknowledge packet OK from HOST in specified time.
944
945 @param[in] Command The command type issued by TARGET.
946 @param[in] Timeout Time out value to wait for acknowlege from HOST.
947 The unit is microsecond.
948 @param[out] BreakReceived If BreakReceived is not NULL,
949 TRUE is retured if break-in symbol received.
950 FALSE is retured if break-in symbol not received.
951 @param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return
952 TRUE: Compatible packet received.
953 FALSE: Incompatible packet received.
954
955 @retval RETRUEN_SUCCESS Succeed to receive acknowlege packet from HOST,
956 the type of acknowlege packet saved in Ack.
957 @retval RETURN_TIMEOUT Specified timeout value was up.
958
959 **/
960 RETURN_STATUS
961 SendCommandAndWaitForAckOK (
962 IN UINT8 Command,
963 IN UINTN Timeout,
964 OUT BOOLEAN *BreakReceived, OPTIONAL
965 OUT BOOLEAN *IncompatibilityFlag OPTIONAL
966 )
967 {
968 RETURN_STATUS Status;
969 UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT];
970 DEBUG_PACKET_HEADER *DebugHeader;
971 UINT8 SequenceNo;
972 UINT8 HostSequenceNo;
973 UINT8 RetryCount;
974
975 RetryCount = 3;
976 DebugHeader = (DEBUG_PACKET_HEADER *) InputPacketBuffer;
977 Status = RETURN_TIMEOUT;
978 while (RetryCount > 0) {
979 SequenceNo = GetMailboxPointer()->SequenceNo;
980 HostSequenceNo = GetMailboxPointer()->HostSequenceNo;
981 SendPacketWithoutData (Command, SequenceNo);
982 Status = ReceivePacket ((UINT8 *) DebugHeader, BreakReceived, IncompatibilityFlag, Timeout, FALSE);
983 if (Status == RETURN_TIMEOUT) {
984 if (Command == DEBUG_COMMAND_INIT_BREAK) {
985 RetryCount--;
986 } else {
987 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Timeout when waiting for ACK packet.\n");
988 }
989 continue;
990 }
991 ASSERT_EFI_ERROR (Status);
992 //
993 // Status == RETURN_SUCCESS
994 //
995 if (DebugHeader->Command == DEBUG_COMMAND_OK && DebugHeader->SequenceNo == SequenceNo) {
996 //
997 // Received Ack OK
998 //
999 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_SEQUENCE_NO_INDEX, ++SequenceNo);
1000 return Status;
1001 }
1002 if (DebugHeader->Command == DEBUG_COMMAND_GO && (DebugHeader->SequenceNo == HostSequenceNo || Command == DEBUG_COMMAND_INIT_BREAK)) {
1003 //
1004 // Received Old GO
1005 //
1006 if (Command == DEBUG_COMMAND_INIT_BREAK) {
1007 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Receive GO() in last boot\n");
1008 }
1009 SendPacketWithoutData (DEBUG_COMMAND_OK, DebugHeader->SequenceNo);
1010 }
1011 }
1012
1013 ASSERT (Command == DEBUG_COMMAND_INIT_BREAK);
1014 return Status;
1015 }
1016
1017 /**
1018 Get current break cause.
1019
1020 @param[in] Vector Vector value of exception or interrupt.
1021 @param[in] CpuContext Pointer to save CPU context.
1022
1023 @return The type of break cause defined by XXXX
1024
1025 **/
1026 UINT8
1027 GetBreakCause (
1028 IN UINTN Vector,
1029 IN DEBUG_CPU_CONTEXT *CpuContext
1030 )
1031 {
1032 UINT8 Cause;
1033
1034 Cause = DEBUG_DATA_BREAK_CAUSE_UNKNOWN;
1035
1036 switch (Vector) {
1037 case DEBUG_INT1_VECTOR:
1038 case DEBUG_INT3_VECTOR:
1039
1040 if (Vector == DEBUG_INT1_VECTOR) {
1041 //
1042 // INT 1
1043 //
1044 if ((CpuContext->Dr6 & BIT14) != 0) {
1045 Cause = DEBUG_DATA_BREAK_CAUSE_STEPPING;
1046 //
1047 // If it's single step, no need to check DR0, to ensure single step work in PeCoffExtraActionLib
1048 // (right after triggering a breakpoint to report image load/unload).
1049 //
1050 return Cause;
1051
1052 } else {
1053 Cause = DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT;
1054 }
1055 } else {
1056 //
1057 // INT 3
1058 //
1059 Cause = DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT;
1060 }
1061
1062 switch (CpuContext->Dr0) {
1063 case IMAGE_LOAD_SIGNATURE:
1064 case IMAGE_UNLOAD_SIGNATURE:
1065
1066 if (CpuContext->Dr3 == IO_PORT_BREAKPOINT_ADDRESS) {
1067
1068 Cause = (UINT8) ((CpuContext->Dr0 == IMAGE_LOAD_SIGNATURE) ?
1069 DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD : DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD);
1070 }
1071 break;
1072
1073 case SOFT_INTERRUPT_SIGNATURE:
1074
1075 if (CpuContext->Dr1 == MEMORY_READY_SIGNATURE) {
1076 Cause = DEBUG_DATA_BREAK_CAUSE_MEMORY_READY;
1077 CpuContext->Dr0 = 0;
1078 } else if (CpuContext->Dr1 == SYSTEM_RESET_SIGNATURE) {
1079 Cause = DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET;
1080 CpuContext->Dr0 = 0;
1081 }
1082 break;
1083
1084 default:
1085 break;
1086
1087 }
1088
1089 break;
1090
1091 case DEBUG_TIMER_VECTOR:
1092 Cause = DEBUG_DATA_BREAK_CAUSE_USER_HALT;
1093 break;
1094
1095 default:
1096 if (Vector < 20) {
1097 if (GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) == 1) {
1098 //
1099 // If stepping command is executing
1100 //
1101 Cause = DEBUG_DATA_BREAK_CAUSE_STEPPING;
1102 } else {
1103 Cause = DEBUG_DATA_BREAK_CAUSE_EXCEPTION;
1104 }
1105 }
1106 break;
1107 }
1108
1109 return Cause;
1110 }
1111
1112 /**
1113 Send packet with response data to HOST.
1114
1115 @param[in] Data Pointer to response data buffer.
1116 @param[in] DataSize Size of response data in byte.
1117
1118 @retval RETURN_SUCCESS Response data was sent successfully.
1119
1120 **/
1121 RETURN_STATUS
1122 SendDataResponsePacket (
1123 IN UINT8 *Data,
1124 IN UINT16 DataSize
1125 )
1126 {
1127 RETURN_STATUS Status;
1128 DEBUG_PACKET_HEADER *DebugHeader;
1129 BOOLEAN LastPacket;
1130 DEBUG_PACKET_HEADER *AckDebugHeader;
1131 UINT8 DebugPacket[DEBUG_DATA_UPPER_LIMIT];
1132 UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT];
1133 DEBUG_PORT_HANDLE Handle;
1134 UINT8 SequenceNo;
1135
1136 Handle = GetDebugPortHandle();
1137
1138 DebugHeader = (DEBUG_PACKET_HEADER *) &DebugPacket;
1139 DebugHeader->StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;
1140
1141 while (TRUE) {
1142 SequenceNo = GetMailboxPointer()->HostSequenceNo;
1143 if (DataSize <= DEBUG_DATA_MAXIMUM_REAL_DATA) {
1144 LastPacket = TRUE;
1145 DebugHeader->Command = DEBUG_COMMAND_OK;
1146 DebugHeader->Length = (UINT8) (DataSize + sizeof (DEBUG_PACKET_HEADER));
1147 DebugHeader->SequenceNo = SequenceNo;
1148 DebugHeader->Crc = 0;
1149 CopyMem (DebugHeader + 1, Data, DataSize);
1150
1151 } else {
1152 LastPacket = FALSE;
1153 DebugHeader->Command = DEBUG_COMMAND_IN_PROGRESS;
1154 DebugHeader->Length = DEBUG_DATA_MAXIMUM_REAL_DATA + sizeof (DEBUG_PACKET_HEADER);
1155 DebugHeader->SequenceNo = SequenceNo;
1156 DebugHeader->Crc = 0;
1157 CopyMem (DebugHeader + 1, Data, DEBUG_DATA_MAXIMUM_REAL_DATA);
1158 }
1159
1160 //
1161 // Calculate and fill the checksum
1162 //
1163 DebugHeader->Crc = CalculateCrc16 ((UINT8 *) DebugHeader, DebugHeader->Length, 0);
1164
1165 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, TRUE, (UINT8 *) DebugHeader, DebugHeader->Length);
1166
1167 DebugPortWriteBuffer (Handle, (UINT8 *) DebugHeader, DebugHeader->Length);
1168
1169 while (TRUE) {
1170 Status = ReceivePacket (InputPacketBuffer, NULL, NULL, READ_PACKET_TIMEOUT, FALSE);
1171 if (Status == RETURN_TIMEOUT) {
1172 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Timeout in SendDataResponsePacket()\n");
1173 break;
1174 }
1175 AckDebugHeader = (DEBUG_PACKET_HEADER *) InputPacketBuffer;
1176 SequenceNo = AckDebugHeader->SequenceNo;
1177 if (AckDebugHeader->Command == DEBUG_COMMAND_OK &&
1178 SequenceNo == DebugHeader->SequenceNo &&
1179 LastPacket) {
1180 //
1181 // If this is the last packet, return RETURN_SUCCESS.
1182 //
1183 return RETURN_SUCCESS;
1184 }
1185 if ((SequenceNo == (UINT8) (DebugHeader->SequenceNo + 1)) && (AckDebugHeader->Command == DEBUG_COMMAND_CONTINUE)) {
1186 //
1187 // Send the rest packet
1188 //
1189 Data += DEBUG_DATA_MAXIMUM_REAL_DATA;
1190 DataSize -= DEBUG_DATA_MAXIMUM_REAL_DATA;
1191 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, (UINT8) SequenceNo);
1192 break;
1193 }
1194 if (SequenceNo >= DebugHeader->SequenceNo) {
1195 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Received one old or new command(SequenceNo is %x, last SequenceNo is %x)\n", SequenceNo, DebugHeader->SequenceNo);
1196 break;
1197 }
1198 }
1199 }
1200 }
1201
1202 /**
1203 Send break cause packet to HOST.
1204
1205 @param[in] Vector Vector value of exception or interrutp.
1206 @param[in] CpuContext Pointer to save CPU context.
1207
1208 @retval RETURN_SUCCESS Response data was sent successfully.
1209 @retval RETURN_DEVICE_ERROR Cannot receive DEBUG_COMMAND_OK from HOST.
1210
1211 **/
1212 RETURN_STATUS
1213 SendBreakCausePacket (
1214 IN UINTN Vector,
1215 IN DEBUG_CPU_CONTEXT *CpuContext
1216 )
1217 {
1218 DEBUG_DATA_RESPONSE_BREAK_CAUSE DebugDataBreakCause;
1219
1220 DebugDataBreakCause.StopAddress = CpuContext->Eip;
1221 DebugDataBreakCause.Cause = GetBreakCause (Vector, CpuContext);
1222
1223 return SendDataResponsePacket ((UINT8 *) &DebugDataBreakCause, (UINT16) sizeof (DEBUG_DATA_RESPONSE_BREAK_CAUSE));
1224 }
1225
1226 /**
1227 Try to attach the HOST.
1228
1229 Send init break packet to HOST:
1230 If no acknowlege received in specified Timeout, return RETURN_TIMEOUT.
1231 If received acknowlege, check the revision of HOST.
1232 Set Attach Flag if attach successfully.
1233
1234 @param[in] BreakCause Break cause of this break event.
1235 @param[in] Timeout Time out value to wait for acknowlege from HOST.
1236 The unit is microsecond.
1237 @param[out] BreakReceived If BreakReceived is not NULL,
1238 TRUE is retured if break-in symbol received.
1239 FALSE is retured if break-in symbol not received.
1240 **/
1241 RETURN_STATUS
1242 AttachHost (
1243 IN UINT8 BreakCause,
1244 IN UINTN Timeout,
1245 OUT BOOLEAN *BreakReceived
1246 )
1247 {
1248 RETURN_STATUS Status;
1249 DEBUG_PORT_HANDLE Handle;
1250 BOOLEAN IncompatibilityFlag;
1251
1252 IncompatibilityFlag = FALSE;
1253 Handle = GetDebugPortHandle();
1254
1255 //
1256 // Send init break and wait ack in Timeout
1257 //
1258 DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgSendInitPacket, AsciiStrLen (mErrorMsgSendInitPacket));
1259 if (BreakCause == DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET) {
1260 Status = SendCommandAndWaitForAckOK (DEBUG_COMMAND_INIT_BREAK, Timeout, BreakReceived, &IncompatibilityFlag);
1261 } else {
1262 Status = SendCommandAndWaitForAckOK (DEBUG_COMMAND_ATTACH_BREAK, Timeout, BreakReceived, &IncompatibilityFlag);
1263 }
1264 if (IncompatibilityFlag) {
1265 //
1266 // If the incompatible Debug Packet received, the HOST should be running transfer protocol before DEBUG_AGENT_REVISION.
1267 // It could be UDK Debugger for Windows v1.1/v1.2 or for Linux v0.8/v1.2.
1268 //
1269 DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgVersionAlert, AsciiStrLen (mErrorMsgVersionAlert));
1270 CpuDeadLoop ();
1271 }
1272
1273 if (RETURN_ERROR (Status)) {
1274 DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgConnectFail, AsciiStrLen (mErrorMsgConnectFail));
1275 } else {
1276 DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgConnectOK, AsciiStrLen (mErrorMsgConnectOK));
1277 //
1278 // Set Attach flag
1279 //
1280 SetHostAttached (TRUE);
1281 }
1282 return Status;
1283 }
1284
1285 /**
1286 Send Break point packet to HOST.
1287
1288 Only the first breaking processor could sent BREAK_POINT packet.
1289
1290 @param[in] BreakCause Break cause of this break event.
1291 @param[in] ProcessorIndex Processor index value.
1292 @param[out] BreakReceived If BreakReceived is not NULL,
1293 TRUE is retured if break-in symbol received.
1294 FALSE is retured if break-in symbol not received.
1295
1296 **/
1297 VOID
1298 SendBreakPacketToHost (
1299 IN UINT8 BreakCause,
1300 IN UINT32 ProcessorIndex,
1301 OUT BOOLEAN *BreakReceived
1302 )
1303 {
1304 UINT8 InputCharacter;
1305 DEBUG_PORT_HANDLE Handle;
1306
1307 Handle = GetDebugPortHandle();
1308
1309 if (IsHostAttached ()) {
1310 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Send Break Packet to HOST.\n", ProcessorIndex);
1311 SendCommandAndWaitForAckOK (DEBUG_COMMAND_BREAK_POINT, READ_PACKET_TIMEOUT, BreakReceived, NULL);
1312 } else {
1313 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Try to attach HOST.\n", ProcessorIndex);
1314 //
1315 // If HOST is not attached, try to attach it firstly.
1316 //
1317 //
1318 // Poll Attach symbols from HOST and ack OK
1319 //
1320 do {
1321 DebugPortReadBuffer (Handle, &InputCharacter, 1, 0);
1322 } while (InputCharacter != DEBUG_STARTING_SYMBOL_ATTACH);
1323 SendAckPacket (DEBUG_COMMAND_OK);
1324
1325 //
1326 // Try to attach HOST
1327 //
1328 while (AttachHost (BreakCause, 0, NULL) != RETURN_SUCCESS);
1329
1330 }
1331 }
1332
1333 /**
1334 The main function to process communication with HOST.
1335
1336 It received the command packet from HOST, and sent response data packet to HOST.
1337
1338 @param[in] Vector Vector value of exception or interrutp.
1339 @param[in, out] CpuContext Pointer to saved CPU context.
1340 @param[in] BreakReceived TRUE means break-in symbol received.
1341 FALSE means break-in symbol not received.
1342
1343 **/
1344 VOID
1345 CommandCommunication (
1346 IN UINTN Vector,
1347 IN OUT DEBUG_CPU_CONTEXT *CpuContext,
1348 IN BOOLEAN BreakReceived
1349 )
1350 {
1351 RETURN_STATUS Status;
1352 UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT];
1353 DEBUG_PACKET_HEADER *DebugHeader;
1354 UINT8 Width;
1355 UINT8 Data8;
1356 UINT32 Data32;
1357 UINT64 Data64;
1358 DEBUG_DATA_READ_MEMORY *MemoryRead;
1359 DEBUG_DATA_WRITE_MEMORY *MemoryWrite;
1360 DEBUG_DATA_READ_IO *IoRead;
1361 DEBUG_DATA_WRITE_IO *IoWrite;
1362 DEBUG_DATA_READ_REGISTER *RegisterRead;
1363 DEBUG_DATA_WRITE_REGISTER *RegisterWrite;
1364 UINT8 *RegisterBuffer;
1365 DEBUG_DATA_READ_MSR *MsrRegisterRead;
1366 DEBUG_DATA_WRITE_MSR *MsrRegisterWrite;
1367 DEBUG_DATA_CPUID *Cpuid;
1368 DEBUG_DATA_RESPONSE_CPUID CpuidResponse;
1369 DEBUG_DATA_SEARCH_SIGNATURE *SearchSignature;
1370 DEBUG_DATA_RESPONSE_GET_EXCEPTION Exception;
1371 DEBUG_DATA_RESPONSE_GET_REVISION DebugAgentRevision;
1372 DEBUG_DATA_SET_VIEWPOINT *SetViewPoint;
1373 BOOLEAN HaltDeferred;
1374 UINT32 ProcessorIndex;
1375 DEBUG_PORT_HANDLE Handle;
1376 DEBUG_AGENT_EXCEPTION_BUFFER AgentExceptionBuffer;
1377 UINT32 IssuedViewPoint;
1378 DEBUG_AGENT_MAILBOX *Mailbox;
1379
1380 ProcessorIndex = 0;
1381 IssuedViewPoint = 0;
1382 HaltDeferred = BreakReceived;
1383
1384 if (MultiProcessorDebugSupport()) {
1385 ProcessorIndex = GetProcessorIndex ();
1386 SetCpuStopFlagByIndex (ProcessorIndex, TRUE);
1387 if (mDebugMpContext.ViewPointIndex == ProcessorIndex) {
1388 //
1389 // Only the current view processor could set AgentInProgress Flag.
1390 //
1391 IssuedViewPoint = ProcessorIndex;
1392 }
1393 }
1394
1395 if (IssuedViewPoint == ProcessorIndex) {
1396 //
1397 // Set AgentInProgress Flag.
1398 //
1399 SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 1);
1400 }
1401
1402 Handle = GetDebugPortHandle();
1403
1404 while (TRUE) {
1405
1406 if (MultiProcessorDebugSupport()) {
1407 //
1408 // Check if the current processor is HOST view point
1409 //
1410 if (mDebugMpContext.ViewPointIndex != ProcessorIndex) {
1411 if (mDebugMpContext.RunCommandSet) {
1412 //
1413 // If HOST view point sets RUN flag, run GO command to leave
1414 //
1415 SetCpuStopFlagByIndex (ProcessorIndex, FALSE);
1416 CommandGo (CpuContext);
1417 break;
1418 } else {
1419 //
1420 // Run into loop again
1421 //
1422 CpuPause ();
1423 continue;
1424 }
1425 }
1426 }
1427
1428 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1429
1430 DebugHeader =(DEBUG_PACKET_HEADER *) InputPacketBuffer;
1431
1432 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "TARGET: Try to get command from HOST...\n");
1433 Status = ReceivePacket ((UINT8 *)DebugHeader, &BreakReceived, NULL, READ_PACKET_TIMEOUT, TRUE);
1434 if (Status != RETURN_SUCCESS || (DebugHeader->Command & DEBUG_COMMAND_RESPONSE) != 0) {
1435 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Get command[%x] sequenceno[%x] returned status is [%x] \n", DebugHeader->Command, DebugHeader->SequenceNo, Status);
1436 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Get command failed or it's response packet not expected! \n");
1437 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1438 continue;
1439 }
1440
1441 Mailbox = GetMailboxPointer ();
1442 if (DebugHeader->SequenceNo == Mailbox->HostSequenceNo) {
1443 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Receive one old command[%x] agaist command[%x]\n", DebugHeader->SequenceNo, Mailbox->HostSequenceNo);
1444 SendAckPacket (Mailbox->LastAck);
1445 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1446 continue;
1447 } else if (DebugHeader->SequenceNo == (UINT8) (Mailbox->HostSequenceNo + 1)) {
1448 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, (UINT8) DebugHeader->SequenceNo);
1449 } else {
1450 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Receive one invalid comamnd[%x] agaist command[%x]\n", DebugHeader->SequenceNo, Mailbox->HostSequenceNo);
1451 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1452 continue;
1453 }
1454
1455 //
1456 // Save CPU content before executing HOST commond
1457 //
1458 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX, (UINT64)(UINTN) &AgentExceptionBuffer.JumpBuffer);
1459 if (SetJump (&AgentExceptionBuffer.JumpBuffer) != 0) {
1460 //
1461 // If HOST command failed, continue to wait for HOST's next command
1462 // If needed, agent could send exception info to HOST.
1463 //
1464 SendAckPacket (DEBUG_COMMAND_ABORT);
1465 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1466 continue;
1467 }
1468
1469 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Processor[%x]:Received one command(%x)\n", mDebugMpContext.ViewPointIndex, DebugHeader->Command);
1470
1471 switch (DebugHeader->Command) {
1472
1473 case DEBUG_COMMAND_HALT:
1474 SendAckPacket (DEBUG_COMMAND_HALT_DEFERRED);
1475 HaltDeferred = TRUE;
1476 BreakReceived = FALSE;
1477 Status = RETURN_SUCCESS;
1478 break;
1479
1480 case DEBUG_COMMAND_RESET:
1481 SendAckPacket (DEBUG_COMMAND_OK);
1482 SendAckPacket (DEBUG_COMMAND_OK);
1483 SendAckPacket (DEBUG_COMMAND_OK);
1484 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1485
1486 ResetCold ();
1487 //
1488 // Assume system resets in 2 seconds, otherwise send TIMEOUT packet.
1489 // PCD can be used if 2 seconds isn't long enough for some platforms.
1490 //
1491 MicroSecondDelay (2000000);
1492 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, Mailbox->HostSequenceNo + 1);
1493 SendAckPacket (DEBUG_COMMAND_TIMEOUT);
1494 SendAckPacket (DEBUG_COMMAND_TIMEOUT);
1495 SendAckPacket (DEBUG_COMMAND_TIMEOUT);
1496 break;
1497
1498 case DEBUG_COMMAND_GO:
1499 CommandGo (CpuContext);
1500 //
1501 // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
1502 // If HOST changed Dr0 before GO, we will not change Dr0 here
1503 //
1504 Data8 = GetBreakCause (Vector, CpuContext);
1505 if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {
1506 CpuContext->Dr0 = 0;
1507 }
1508 //
1509 // Clear Stepping Flag
1510 //
1511 SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 0);
1512
1513 if (!HaltDeferred) {
1514 //
1515 // If no HALT command received when being in-active mode
1516 //
1517 if (MultiProcessorDebugSupport()) {
1518 Data32 = FindNextPendingBreakCpu ();
1519 if (Data32 != -1) {
1520 //
1521 // If there are still others processors being in break state,
1522 // send OK packet to HOST to finish this go command
1523 //
1524 SendAckPacket (DEBUG_COMMAND_OK);
1525 CpuPause ();
1526 //
1527 // Set current view to the next breaking processor
1528 //
1529 mDebugMpContext.ViewPointIndex = Data32;
1530 mDebugMpContext.BreakAtCpuIndex = mDebugMpContext.ViewPointIndex;
1531 SetCpuBreakFlagByIndex (mDebugMpContext.ViewPointIndex, FALSE);
1532 //
1533 // Send break packet to HOST to let HOST break again
1534 //
1535 SendBreakPacketToHost (DEBUG_DATA_BREAK_CAUSE_UNKNOWN, mDebugMpContext.BreakAtCpuIndex, &BreakReceived);
1536 //
1537 // Continue to run into loop to read command packet from HOST
1538 //
1539 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1540 break;
1541 }
1542
1543 //
1544 // If no else processor break, set stop bitmask,
1545 // and set Running flag for all processors.
1546 //
1547 SetCpuStopFlagByIndex (ProcessorIndex, FALSE);
1548 SetCpuRunningFlag (TRUE);
1549 CpuPause ();
1550 //
1551 // Wait for all processors are in running state
1552 //
1553 while (TRUE) {
1554 if (IsAllCpuRunning ()) {
1555 break;
1556 }
1557 }
1558 //
1559 // Set BSP to be current view point.
1560 //
1561 SetDebugViewPoint (mDebugMpContext.BspIndex);
1562 CpuPause ();
1563 //
1564 // Clear breaking processor index and running flag
1565 //
1566 mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);
1567 SetCpuRunningFlag (FALSE);
1568 }
1569
1570 //
1571 // Send OK packet to HOST to finish this go command
1572 //
1573 SendAckPacket (DEBUG_COMMAND_OK);
1574
1575 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1576
1577 if (!IsHostAttached()) {
1578 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_SEQUENCE_NO_INDEX, 0);
1579 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, 0);
1580 }
1581 return;
1582
1583 } else {
1584 //
1585 // If reveived HALT command, need to defer the GO command
1586 //
1587 SendAckPacket (DEBUG_COMMAND_HALT_PROCESSED);
1588 HaltDeferred = FALSE;
1589
1590 Vector = DEBUG_TIMER_VECTOR;
1591 }
1592 break;
1593
1594 case DEBUG_COMMAND_BREAK_CAUSE:
1595
1596 if (MultiProcessorDebugSupport() && ProcessorIndex != mDebugMpContext.BreakAtCpuIndex) {
1597 Status = SendBreakCausePacket (DEBUG_TIMER_VECTOR, CpuContext);
1598
1599 } else {
1600 Status = SendBreakCausePacket (Vector, CpuContext);
1601 }
1602
1603 break;
1604
1605 case DEBUG_COMMAND_SET_HW_BREAKPOINT:
1606 SetDebugRegister (CpuContext, (DEBUG_DATA_SET_HW_BREAKPOINT *) (DebugHeader + 1));
1607 SendAckPacket (DEBUG_COMMAND_OK);
1608 break;
1609
1610 case DEBUG_COMMAND_CLEAR_HW_BREAKPOINT:
1611 ClearDebugRegister (CpuContext, (DEBUG_DATA_CLEAR_HW_BREAKPOINT *) (DebugHeader + 1));
1612 SendAckPacket (DEBUG_COMMAND_OK);
1613 break;
1614
1615 case DEBUG_COMMAND_SINGLE_STEPPING:
1616 CommandStepping (CpuContext);
1617 //
1618 // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
1619 // If HOST changed Dr0 before GO, we will not change Dr0 here
1620 //
1621 Data8 = GetBreakCause (Vector, CpuContext);
1622 if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {
1623 CpuContext->Dr0 = 0;
1624 }
1625
1626 mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);
1627 //
1628 // Set Stepping Flag
1629 //
1630 SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 1);
1631 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1632 //
1633 // Executing stepping command directly without sending ACK packet,
1634 // ACK packet will be sent after stepping done.
1635 //
1636 return;
1637
1638 case DEBUG_COMMAND_SET_SW_BREAKPOINT:
1639 Data64 = (UINTN) (((DEBUG_DATA_SET_SW_BREAKPOINT *) (DebugHeader + 1))->Address);
1640 Data8 = *(UINT8 *) (UINTN) Data64;
1641 *(UINT8 *) (UINTN) Data64 = DEBUG_SW_BREAKPOINT_SYMBOL;
1642 Status = SendDataResponsePacket ((UINT8 *) &Data8, (UINT16) sizeof (UINT8));
1643 break;
1644
1645 case DEBUG_COMMAND_READ_MEMORY:
1646 MemoryRead = (DEBUG_DATA_READ_MEMORY *) (DebugHeader + 1);
1647 Status = SendDataResponsePacket ((UINT8 *) (UINTN) MemoryRead->Address, (UINT16) (MemoryRead->Count * MemoryRead->Width));
1648 break;
1649
1650 case DEBUG_COMMAND_WRITE_MEMORY:
1651 MemoryWrite = (DEBUG_DATA_WRITE_MEMORY *) (DebugHeader + 1);
1652 CopyMem ((VOID *) (UINTN) MemoryWrite->Address, &MemoryWrite->Data, MemoryWrite->Count * MemoryWrite->Width);
1653 SendAckPacket (DEBUG_COMMAND_OK);
1654 break;
1655
1656 case DEBUG_COMMAND_READ_IO:
1657 IoRead = (DEBUG_DATA_READ_IO *) (DebugHeader + 1);
1658 switch (IoRead->Width) {
1659 case 1:
1660 Data64 = IoRead8 ((UINTN) IoRead->Port);
1661 break;
1662 case 2:
1663 Data64 = IoRead16 ((UINTN) IoRead->Port);
1664 break;
1665 case 4:
1666 Data64 = IoRead32 ((UINTN) IoRead->Port);
1667 break;
1668 case 8:
1669 Data64 = IoRead64 ((UINTN) IoRead->Port);
1670 break;
1671 default:
1672 Data64 = (UINT64) -1;
1673 }
1674 Status = SendDataResponsePacket ((UINT8 *) &Data64, IoRead->Width);
1675 break;
1676
1677 case DEBUG_COMMAND_WRITE_IO:
1678 IoWrite = (DEBUG_DATA_WRITE_IO *) (DebugHeader + 1);
1679 switch (IoWrite->Width) {
1680 case 1:
1681 Data64 = IoWrite8 ((UINTN) IoWrite->Port, *(UINT8 *) &IoWrite->Data);
1682 break;
1683 case 2:
1684 Data64 = IoWrite16 ((UINTN) IoWrite->Port, *(UINT16 *) &IoWrite->Data);
1685 break;
1686 case 4:
1687 Data64 = IoWrite32 ((UINTN) IoWrite->Port, *(UINT32 *) &IoWrite->Data);
1688 break;
1689 case 8:
1690 Data64 = IoWrite64 ((UINTN) IoWrite->Port, *(UINT64 *) &IoWrite->Data);
1691 break;
1692 default:
1693 Data64 = (UINT64) -1;
1694 }
1695 SendAckPacket (DEBUG_COMMAND_OK);
1696 break;
1697
1698 case DEBUG_COMMAND_READ_ALL_REGISTERS:
1699 Status = SendDataResponsePacket ((UINT8 *) CpuContext, sizeof (*CpuContext));
1700 break;
1701
1702 case DEBUG_COMMAND_READ_REGISTER:
1703 RegisterRead = (DEBUG_DATA_READ_REGISTER *) (DebugHeader + 1);
1704
1705 if (RegisterRead->Index <= SOFT_DEBUGGER_REGISTER_MAX) {
1706 RegisterBuffer = ArchReadRegisterBuffer (CpuContext, RegisterRead->Index, &Width);
1707 Status = SendDataResponsePacket (RegisterBuffer, Width);
1708 } else {
1709 Status = RETURN_UNSUPPORTED;
1710 }
1711 break;
1712
1713 case DEBUG_COMMAND_WRITE_REGISTER:
1714 RegisterWrite = (DEBUG_DATA_WRITE_REGISTER *) (DebugHeader + 1);
1715 if (RegisterWrite->Index <= SOFT_DEBUGGER_REGISTER_MAX) {
1716 RegisterBuffer = ArchReadRegisterBuffer (CpuContext, RegisterWrite->Index, &Width);
1717 ASSERT (Width == RegisterWrite->Length);
1718 CopyMem (RegisterBuffer, RegisterWrite->Data, Width);
1719 SendAckPacket (DEBUG_COMMAND_OK);
1720 } else {
1721 Status = RETURN_UNSUPPORTED;
1722 }
1723 break;
1724
1725 case DEBUG_COMMAND_ARCH_MODE:
1726 Data8 = DEBUG_ARCH_SYMBOL;
1727 Status = SendDataResponsePacket ((UINT8 *) &Data8, (UINT16) sizeof (UINT8));
1728 break;
1729
1730 case DEBUG_COMMAND_READ_MSR:
1731 MsrRegisterRead = (DEBUG_DATA_READ_MSR *) (DebugHeader + 1);
1732 Data64 = AsmReadMsr64 (MsrRegisterRead->Index);
1733 Status = SendDataResponsePacket ((UINT8 *) &Data64, (UINT16) sizeof (UINT64));
1734 break;
1735
1736 case DEBUG_COMMAND_WRITE_MSR:
1737 MsrRegisterWrite = (DEBUG_DATA_WRITE_MSR *) (DebugHeader + 1);
1738 AsmWriteMsr64 (MsrRegisterWrite->Index, MsrRegisterWrite->Value);
1739 SendAckPacket (DEBUG_COMMAND_OK);
1740 break;
1741
1742 case DEBUG_COMMAND_SET_DEBUG_SETTING:
1743 Status = SetDebugSetting ((DEBUG_DATA_SET_DEBUG_SETTING *)(DebugHeader + 1));
1744 if (Status == RETURN_SUCCESS) {
1745 SendAckPacket (DEBUG_COMMAND_OK);
1746 }
1747 break;
1748
1749 case DEBUG_COMMAND_GET_REVISION:
1750 DebugAgentRevision.Revision = DEBUG_AGENT_REVISION;
1751 DebugAgentRevision.Capabilities = DEBUG_AGENT_CAPABILITIES;
1752 Status = SendDataResponsePacket ((UINT8 *) &DebugAgentRevision, (UINT16) sizeof (DEBUG_DATA_RESPONSE_GET_REVISION));
1753 break;
1754
1755 case DEBUG_COMMAND_GET_EXCEPTION:
1756 Exception.ExceptionNum = (UINT8) Vector;
1757 Exception.ExceptionData = (UINT32) CpuContext->ExceptionData;
1758 Status = SendDataResponsePacket ((UINT8 *) &Exception, (UINT16) sizeof (DEBUG_DATA_RESPONSE_GET_EXCEPTION));
1759 break;
1760
1761 case DEBUG_COMMAND_SET_VIEWPOINT:
1762 SetViewPoint = (DEBUG_DATA_SET_VIEWPOINT *) (DebugHeader + 1);
1763 if (MultiProcessorDebugSupport()) {
1764 if (IsCpuStopped (SetViewPoint->ViewPoint)) {
1765 SetDebugViewPoint (SetViewPoint->ViewPoint);
1766 SendAckPacket (DEBUG_COMMAND_OK);
1767 } else {
1768 //
1769 // If CPU is not halted
1770 //
1771 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
1772 }
1773 } else if (SetViewPoint->ViewPoint == 0) {
1774 SendAckPacket (DEBUG_COMMAND_OK);
1775
1776 } else {
1777 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
1778 }
1779
1780 break;
1781
1782 case DEBUG_COMMAND_GET_VIEWPOINT:
1783 Data32 = mDebugMpContext.ViewPointIndex;
1784 SendDataResponsePacket((UINT8 *) &Data32, (UINT16) sizeof (UINT32));
1785 break;
1786
1787 case DEBUG_COMMAND_MEMORY_READY:
1788 Data8 = (UINT8) GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY);
1789 SendDataResponsePacket (&Data8, (UINT16) sizeof (UINT8));
1790 break;
1791
1792 case DEBUG_COMMAND_DETACH:
1793 SetHostAttached (FALSE);
1794 SendAckPacket (DEBUG_COMMAND_OK);
1795 break;
1796
1797 case DEBUG_COMMAND_CPUID:
1798 Cpuid = (DEBUG_DATA_CPUID *) (DebugHeader + 1);
1799 AsmCpuidEx (
1800 Cpuid->Eax, Cpuid->Ecx,
1801 &CpuidResponse.Eax, &CpuidResponse.Ebx,
1802 &CpuidResponse.Ecx, &CpuidResponse.Edx
1803 );
1804 SendDataResponsePacket ((UINT8 *) &CpuidResponse, (UINT16) sizeof (CpuidResponse));
1805 break;
1806
1807 case DEBUG_COMMAND_SEARCH_SIGNATURE:
1808 SearchSignature = (DEBUG_DATA_SEARCH_SIGNATURE *) (DebugHeader + 1);
1809 if ((SearchSignature->Alignment != 0) &&
1810 (SearchSignature->Alignment == GetPowerOfTwo32 (SearchSignature->Alignment))
1811 ) {
1812 if (SearchSignature->Positive) {
1813 for (
1814 Data64 = ALIGN_VALUE ((UINTN) SearchSignature->Start, SearchSignature->Alignment);
1815 Data64 <= SearchSignature->Start + SearchSignature->Count - SearchSignature->DataLength;
1816 Data64 += SearchSignature->Alignment
1817 ) {
1818 if (CompareMem ((VOID *) (UINTN) Data64, &SearchSignature->Data, SearchSignature->DataLength) == 0) {
1819 break;
1820 }
1821 }
1822 if (Data64 > SearchSignature->Start + SearchSignature->Count - SearchSignature->DataLength) {
1823 Data64 = (UINT64) -1;
1824 }
1825 } else {
1826 for (
1827 Data64 = ALIGN_VALUE ((UINTN) SearchSignature->Start - SearchSignature->Alignment, SearchSignature->Alignment);
1828 Data64 >= SearchSignature->Start - SearchSignature->Count;
1829 Data64 -= SearchSignature->Alignment
1830 ) {
1831 if (CompareMem ((VOID *) (UINTN) Data64, &SearchSignature->Data, SearchSignature->DataLength) == 0) {
1832 break;
1833 }
1834 }
1835 if (Data64 < SearchSignature->Start - SearchSignature->Count) {
1836 Data64 = (UINT64) -1;
1837 }
1838 }
1839 SendDataResponsePacket ((UINT8 *) &Data64, (UINT16) sizeof (Data64));
1840 } else {
1841 Status = RETURN_UNSUPPORTED;
1842 }
1843 break;
1844
1845 default:
1846 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
1847 break;
1848 }
1849
1850 if (Status == RETURN_UNSUPPORTED) {
1851 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
1852 } else if (Status != RETURN_SUCCESS) {
1853 SendAckPacket (DEBUG_COMMAND_ABORT);
1854 }
1855
1856 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1857 CpuPause ();
1858 }
1859 }
1860
1861 /**
1862 C function called in interrupt handler.
1863
1864 @param[in] Vector Vector value of exception or interrutp.
1865 @param[in] CpuContext Pointer to save CPU context.
1866
1867 **/
1868 VOID
1869 EFIAPI
1870 InterruptProcess (
1871 IN UINT32 Vector,
1872 IN DEBUG_CPU_CONTEXT *CpuContext
1873 )
1874 {
1875 UINT8 InputCharacter;
1876 UINT8 BreakCause;
1877 UINTN SavedEip;
1878 BOOLEAN BreakReceived;
1879 UINT32 ProcessorIndex;
1880 UINT32 CurrentDebugTimerInitCount;
1881 DEBUG_PORT_HANDLE Handle;
1882 UINT8 Data8;
1883 UINT8 *Al;
1884 UINT32 IssuedViewPoint;
1885 DEBUG_AGENT_EXCEPTION_BUFFER *ExceptionBuffer;
1886
1887 ProcessorIndex = 0;
1888 IssuedViewPoint = 0;
1889 BreakReceived = FALSE;
1890
1891 if (mSkipBreakpoint) {
1892 //
1893 // If Skip Breakpoint flag is set, means communication is disturbed by hardware SMI, we need to ignore the break points in SMM
1894 //
1895 if ((Vector == DEBUG_INT1_VECTOR) || (Vector == DEBUG_INT3_VECTOR)) {
1896 DebugPortWriteBuffer (GetDebugPortHandle(), (UINT8 *) mWarningMsgIngoreBreakpoint, AsciiStrLen (mWarningMsgIngoreBreakpoint));
1897 return;
1898 }
1899 }
1900
1901 if (MultiProcessorDebugSupport()) {
1902 ProcessorIndex = GetProcessorIndex ();
1903 //
1904 // If this processor has alreay halted before, need to check it later
1905 //
1906 if (IsCpuStopped (ProcessorIndex)) {
1907 IssuedViewPoint = ProcessorIndex;
1908 }
1909 }
1910
1911 if (IssuedViewPoint == ProcessorIndex && GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) != 1) {
1912 //
1913 // Check if this exception is issued by Debug Agent itself
1914 // If yes, fill the debug agent exception buffer and LongJump() back to
1915 // the saved CPU content in CommandCommunication()
1916 //
1917 if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {
1918 DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Debug agent meet one Exception, ExceptionNum is %d.\n", Vector);
1919 ExceptionBuffer = (DEBUG_AGENT_EXCEPTION_BUFFER *) (UINTN) GetMailboxPointer()->ExceptionBufferPointer;
1920 ExceptionBuffer->ExceptionContent.ExceptionNum = (UINT8) Vector;
1921 ExceptionBuffer->ExceptionContent.ExceptionData = (UINT32) CpuContext->ExceptionData;
1922 LongJump ((BASE_LIBRARY_JUMP_BUFFER *)(UINTN)(ExceptionBuffer), 1);
1923 }
1924 }
1925
1926 if (MultiProcessorDebugSupport()) {
1927 //
1928 // If RUN commmand is executing, wait for it done.
1929 //
1930 while (mDebugMpContext.RunCommandSet) {
1931 CpuPause ();
1932 }
1933 }
1934
1935 Handle = GetDebugPortHandle();
1936 BreakCause = GetBreakCause (Vector, CpuContext);
1937 switch (Vector) {
1938 case DEBUG_INT1_VECTOR:
1939 case DEBUG_INT3_VECTOR:
1940 switch (BreakCause) {
1941 case DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET:
1942 if (AttachHost (BreakCause, READ_PACKET_TIMEOUT, &BreakReceived) != RETURN_SUCCESS) {
1943 //
1944 // Try to connect HOST, return if fails
1945 //
1946 break;
1947 }
1948 CommandCommunication (Vector, CpuContext, BreakReceived);
1949 break;
1950
1951 case DEBUG_DATA_BREAK_CAUSE_STEPPING:
1952 //
1953 // Stepping is finished, send Ack package.
1954 //
1955 if (MultiProcessorDebugSupport()) {
1956 mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;
1957 }
1958 SendAckPacket (DEBUG_COMMAND_OK);
1959 CommandCommunication (Vector, CpuContext, BreakReceived);
1960 break;
1961
1962 case DEBUG_DATA_BREAK_CAUSE_MEMORY_READY:
1963 //
1964 // Memory is ready
1965 //
1966 SendCommandAndWaitForAckOK (DEBUG_COMMAND_MEMORY_READY, READ_PACKET_TIMEOUT, &BreakReceived, NULL);
1967 CommandCommunication (Vector, CpuContext, BreakReceived);
1968 break;
1969
1970 case DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD:
1971 case DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD:
1972 //
1973 // Set AL to DEBUG_AGENT_IMAGE_CONTINUE
1974 //
1975 Al = ArchReadRegisterBuffer (CpuContext, SOFT_DEBUGGER_REGISTER_AX, &Data8);
1976 *Al = DEBUG_AGENT_IMAGE_CONTINUE;
1977
1978 if (!IsHostAttached ()) {
1979 //
1980 // If HOST is not connected for image load/unload, return
1981 //
1982 break;
1983 }
1984 //
1985 // Continue to run the following common code
1986 //
1987
1988 case DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT:
1989 case DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT:
1990 default:
1991 //
1992 // Send Break packet to HOST
1993 //
1994 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1995 //
1996 // Only the first breaking processor could send BREAK_POINT to HOST
1997 //
1998 if (IsFirstBreakProcessor (ProcessorIndex)) {
1999 SendBreakPacketToHost (BreakCause, ProcessorIndex, &BreakReceived);
2000 }
2001 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2002
2003 if (Vector == DEBUG_INT3_VECTOR) {
2004 //
2005 // go back address located "0xCC"
2006 //
2007 CpuContext->Eip--;
2008 SavedEip = CpuContext->Eip;
2009 CommandCommunication (Vector, CpuContext, BreakReceived);
2010 if ((SavedEip == CpuContext->Eip) &&
2011 (*(UINT8 *) (UINTN) CpuContext->Eip == DEBUG_SW_BREAKPOINT_SYMBOL)) {
2012 //
2013 // If this is not a software breakpoint set by HOST,
2014 // restore EIP
2015 //
2016 CpuContext->Eip++;
2017 }
2018 } else {
2019 CommandCommunication (Vector, CpuContext, BreakReceived);
2020 }
2021 break;
2022 }
2023
2024 break;
2025
2026 case DEBUG_TIMER_VECTOR:
2027
2028 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2029
2030 if (MultiProcessorDebugSupport()) {
2031 if (IsBsp (ProcessorIndex)) {
2032 //
2033 // If current processor is BSP, check Apic timer's init count if changed,
2034 // it may be re-written when switching BSP.
2035 // If it changed, re-initialize debug timer
2036 //
2037 CurrentDebugTimerInitCount = GetApicTimerInitCount ();
2038 if (mDebugMpContext.DebugTimerInitCount != CurrentDebugTimerInitCount) {
2039 InitializeDebugTimer ();
2040 }
2041 }
2042
2043 if (!IsBsp (ProcessorIndex) || mDebugMpContext.IpiSentByAp) {
2044 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2045 //
2046 // If current processor is not BSP or this is one IPI sent by AP
2047 //
2048 if (mDebugMpContext.BreakAtCpuIndex != (UINT32) (-1)) {
2049 CommandCommunication (Vector, CpuContext, FALSE);
2050 }
2051
2052 //
2053 // Clear EOI before exiting interrupt process routine.
2054 //
2055 SendApicEoi ();
2056 break;
2057 }
2058 }
2059
2060 //
2061 // Only BSP could run here
2062 //
2063 while (TRUE) {
2064 //
2065 // If there is data in debug port, will check whether it is break(attach/break-in) symbol,
2066 // If yes, go into communication mode with HOST.
2067 // If no, exit interrupt process.
2068 //
2069 if (DebugReadBreakSymbol (Handle, &InputCharacter) == EFI_NOT_FOUND) {
2070 break;
2071 }
2072
2073 if ((!IsHostAttached () && (InputCharacter == DEBUG_STARTING_SYMBOL_ATTACH)) ||
2074 (IsHostAttached () && (InputCharacter == DEBUG_COMMAND_HALT)) ||
2075 (IsHostAttached () && (InputCharacter == DEBUG_COMMAND_GO))
2076 ) {
2077 DebugAgentMsgPrint (DEBUG_AGENT_VERBOSE, "Received data [%02x]\n", InputCharacter);
2078 //
2079 // Ack OK for break-in symbol
2080 //
2081 SendAckPacket (DEBUG_COMMAND_OK);
2082
2083 //
2084 // If receive GO command in Debug Timer, means HOST may lost ACK packet before.
2085 //
2086 if (InputCharacter == DEBUG_COMMAND_GO) {
2087 break;
2088 }
2089
2090 if (!IsHostAttached ()) {
2091 //
2092 // Try to attach HOST, if no ack received after 200ms, return
2093 //
2094 if (AttachHost (BreakCause, READ_PACKET_TIMEOUT, &BreakReceived) != RETURN_SUCCESS) {
2095 break;
2096 }
2097 }
2098
2099 if (MultiProcessorDebugSupport()) {
2100 if(FindNextPendingBreakCpu () != -1) {
2101 SetCpuBreakFlagByIndex (ProcessorIndex, TRUE);
2102 } else {
2103 HaltOtherProcessors (ProcessorIndex);
2104 }
2105 }
2106 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2107 CommandCommunication (Vector, CpuContext, BreakReceived);
2108 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2109 break;
2110 }
2111 }
2112
2113 //
2114 // Clear EOI before exiting interrupt process routine.
2115 //
2116 SendApicEoi ();
2117
2118 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2119
2120 break;
2121
2122 default:
2123 if (Vector <= DEBUG_EXCEPT_SIMD) {
2124 if (BreakCause == DEBUG_DATA_BREAK_CAUSE_STEPPING) {
2125 //
2126 // Stepping is finished, send Ack package.
2127 //
2128 if (MultiProcessorDebugSupport()) {
2129 mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;
2130 }
2131 SendAckPacket (DEBUG_COMMAND_OK);
2132 } else {
2133 //
2134 // Exception occurs, send Break packet to HOST
2135 //
2136 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2137 //
2138 // Only the first breaking processor could send BREAK_POINT to HOST
2139 //
2140 if (IsFirstBreakProcessor (ProcessorIndex)) {
2141 SendBreakPacketToHost (BreakCause, ProcessorIndex, &BreakReceived);
2142 }
2143 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2144 }
2145
2146 CommandCommunication (Vector, CpuContext, BreakReceived);
2147 }
2148 break;
2149 }
2150
2151 if (MultiProcessorDebugSupport()) {
2152 //
2153 // Clear flag and wait for all processors run here
2154 //
2155 SetIpiSentByApFlag (FALSE);
2156 while (mDebugMpContext.RunCommandSet) {
2157 CpuPause ();
2158 }
2159
2160 //
2161 // Only current (view) processor could clean up AgentInProgress flag.
2162 //
2163 if (mDebugMpContext.ViewPointIndex == ProcessorIndex) {
2164 IssuedViewPoint = mDebugMpContext.ViewPointIndex;
2165 }
2166 }
2167
2168 if (IssuedViewPoint == ProcessorIndex && GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) != 1) {
2169 //
2170 // If the command is not stepping, clean up AgentInProgress flag
2171 //
2172 SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 0);
2173 }
2174
2175 return;
2176 }
2177