]> git.proxmox.com Git - mirror_edk2.git/blob - SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/DebugAgent.c
Updated connecting HOST version information from 1.3 to 1.3.1.
[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.1) ...\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 Copy memory from source to destination with specified width.
1114
1115 @param[out] Dest A pointer to the destination buffer of the memory copy.
1116 @param[in] Src A pointer to the source buffer of the memory copy.
1117 @param[in] Count The number of data with specified width to copy from source to destination.
1118 @param[in] Width Data width in byte.
1119
1120 **/
1121 VOID
1122 CopyMemByWidth (
1123 OUT UINT8 *Dest,
1124 IN UINT8 *Src,
1125 IN UINT16 Count,
1126 IN UINT8 Width
1127 )
1128 {
1129 UINT8 *Destination;
1130 UINT8 *Source;
1131 INT8 Step;
1132
1133 if (Src > Dest) {
1134 Destination = Dest;
1135 Source = Src;
1136 Step = Width;
1137 } else {
1138 //
1139 // Copy memory from tail to avoid memory overlap
1140 //
1141 Destination = Dest + (Count - 1) * Width;
1142 Source = Src + (Count - 1) * Width;
1143 Step = -Width;
1144 }
1145
1146 while (Count-- != 0) {
1147 switch (Width) {
1148 case 1:
1149 *(UINT8 *) Destination = MmioRead8 ((UINTN) Source);
1150 break;
1151 case 2:
1152 *(UINT16 *) Destination = MmioRead16 ((UINTN) Source);
1153 break;
1154 case 4:
1155 *(UINT32 *) Destination = MmioRead32 ((UINTN) Source);
1156 break;
1157 case 8:
1158 *(UINT64 *) Destination = MmioRead64 ((UINTN) Source);
1159 break;
1160 default:
1161 ASSERT (FALSE);
1162 }
1163 Source += Step;
1164 Destination += Step;
1165 }
1166 }
1167
1168 /**
1169 Read memory with speicifed width and send packet with response data to HOST.
1170
1171 @param[in] Data Pointer to response data buffer.
1172 @param[in] Count The number of data with specified Width.
1173 @param[in] Width Data width in byte.
1174
1175 @retval RETURN_SUCCESS Response data was sent successfully.
1176
1177 **/
1178 RETURN_STATUS
1179 ReadMemoryAndSendResponsePacket (
1180 IN UINT8 *Data,
1181 IN UINT16 Count,
1182 IN UINT8 Width
1183 )
1184 {
1185 RETURN_STATUS Status;
1186 DEBUG_PACKET_HEADER *DebugHeader;
1187 BOOLEAN LastPacket;
1188 DEBUG_PACKET_HEADER *AckDebugHeader;
1189 UINT8 DebugPacket[DEBUG_DATA_UPPER_LIMIT + sizeof (UINT64) - 1];
1190 UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT];
1191 DEBUG_PORT_HANDLE Handle;
1192 UINT8 SequenceNo;
1193 UINTN RemainingDataSize;
1194 UINTN CurrentDataSize;
1195
1196 Handle = GetDebugPortHandle();
1197
1198 //
1199 // Data is appended end of Debug Packet header, make sure data address
1200 // in Debug Packet 8-byte alignment always
1201 //
1202 DebugHeader = (DEBUG_PACKET_HEADER *) (ALIGN_VALUE ((UINTN)&DebugPacket + sizeof (DEBUG_PACKET_HEADER), sizeof (UINT64))
1203 - sizeof (DEBUG_PACKET_HEADER));
1204 DebugHeader->StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;
1205
1206 RemainingDataSize = Count * Width;
1207 while (TRUE) {
1208 SequenceNo = GetMailboxPointer()->HostSequenceNo;
1209 if (RemainingDataSize <= DEBUG_DATA_MAXIMUM_REAL_DATA) {
1210 //
1211 // If the remaining data is less one real packet size, this is the last data packet
1212 //
1213 CurrentDataSize = RemainingDataSize;
1214 LastPacket = TRUE;
1215 DebugHeader->Command = DEBUG_COMMAND_OK;
1216 } else {
1217 //
1218 // Data is too larger to be sent in one packet, calculate the actual data size could
1219 // be sent in one Maximum data packet
1220 //
1221 CurrentDataSize = (DEBUG_DATA_MAXIMUM_REAL_DATA / Width) * Width;
1222 LastPacket = FALSE;
1223 DebugHeader->Command = DEBUG_COMMAND_IN_PROGRESS;
1224 }
1225 //
1226 // Construct the rest Debug header
1227 //
1228 DebugHeader->Length = (UINT8)(CurrentDataSize + sizeof (DEBUG_PACKET_HEADER));
1229 DebugHeader->SequenceNo = SequenceNo;
1230 DebugHeader->Crc = 0;
1231 CopyMemByWidth ((UINT8 *)(DebugHeader + 1), Data, (UINT16) CurrentDataSize / Width, Width);
1232 //
1233 // Calculate and fill the checksum, DebugHeader->Crc should be 0 before invoking CalculateCrc16 ()
1234 //
1235 DebugHeader->Crc = CalculateCrc16 ((UINT8 *) DebugHeader, DebugHeader->Length, 0);
1236
1237 DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, TRUE, (UINT8 *) DebugHeader, DebugHeader->Length);
1238
1239 DebugPortWriteBuffer (Handle, (UINT8 *) DebugHeader, DebugHeader->Length);
1240
1241 while (TRUE) {
1242 Status = ReceivePacket (InputPacketBuffer, NULL, NULL, READ_PACKET_TIMEOUT, FALSE);
1243 if (Status == RETURN_TIMEOUT) {
1244 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Timeout in SendDataResponsePacket()\n");
1245 break;
1246 }
1247 AckDebugHeader = (DEBUG_PACKET_HEADER *) InputPacketBuffer;
1248 SequenceNo = AckDebugHeader->SequenceNo;
1249 if (AckDebugHeader->Command == DEBUG_COMMAND_OK &&
1250 SequenceNo == DebugHeader->SequenceNo &&
1251 LastPacket) {
1252 //
1253 // If this is the last packet, return RETURN_SUCCESS.
1254 //
1255 return RETURN_SUCCESS;
1256 }
1257 if ((SequenceNo == (UINT8) (DebugHeader->SequenceNo + 1)) && (AckDebugHeader->Command == DEBUG_COMMAND_CONTINUE)) {
1258 //
1259 // Calculate the rest data size
1260 //
1261 Data += CurrentDataSize;
1262 RemainingDataSize -= CurrentDataSize;
1263 UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, (UINT8) SequenceNo);
1264 break;
1265 }
1266 if (SequenceNo >= DebugHeader->SequenceNo) {
1267 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Received one old or new command(SequenceNo is %x, last SequenceNo is %x)\n", SequenceNo, DebugHeader->SequenceNo);
1268 break;
1269 }
1270 }
1271 }
1272 }
1273
1274 /**
1275 Send packet with response data to HOST.
1276
1277 @param[in] Data Pointer to response data buffer.
1278 @param[in] DataSize Size of response data in byte.
1279
1280 @retval RETURN_SUCCESS Response data was sent successfully.
1281
1282 **/
1283 RETURN_STATUS
1284 SendDataResponsePacket (
1285 IN UINT8 *Data,
1286 IN UINT16 DataSize
1287 )
1288 {
1289 return ReadMemoryAndSendResponsePacket (Data, DataSize, 1);
1290 }
1291
1292 /**
1293 Send break cause packet to HOST.
1294
1295 @param[in] Vector Vector value of exception or interrutp.
1296 @param[in] CpuContext Pointer to save CPU context.
1297
1298 @retval RETURN_SUCCESS Response data was sent successfully.
1299 @retval RETURN_DEVICE_ERROR Cannot receive DEBUG_COMMAND_OK from HOST.
1300
1301 **/
1302 RETURN_STATUS
1303 SendBreakCausePacket (
1304 IN UINTN Vector,
1305 IN DEBUG_CPU_CONTEXT *CpuContext
1306 )
1307 {
1308 DEBUG_DATA_RESPONSE_BREAK_CAUSE DebugDataBreakCause;
1309
1310 DebugDataBreakCause.StopAddress = CpuContext->Eip;
1311 DebugDataBreakCause.Cause = GetBreakCause (Vector, CpuContext);
1312
1313 return SendDataResponsePacket ((UINT8 *) &DebugDataBreakCause, (UINT16) sizeof (DEBUG_DATA_RESPONSE_BREAK_CAUSE));
1314 }
1315
1316 /**
1317 Try to attach the HOST.
1318
1319 Send init break packet to HOST:
1320 If no acknowlege received in specified Timeout, return RETURN_TIMEOUT.
1321 If received acknowlege, check the revision of HOST.
1322 Set Attach Flag if attach successfully.
1323
1324 @param[in] BreakCause Break cause of this break event.
1325 @param[in] Timeout Time out value to wait for acknowlege from HOST.
1326 The unit is microsecond.
1327 @param[out] BreakReceived If BreakReceived is not NULL,
1328 TRUE is retured if break-in symbol received.
1329 FALSE is retured if break-in symbol not received.
1330 **/
1331 RETURN_STATUS
1332 AttachHost (
1333 IN UINT8 BreakCause,
1334 IN UINTN Timeout,
1335 OUT BOOLEAN *BreakReceived
1336 )
1337 {
1338 RETURN_STATUS Status;
1339 DEBUG_PORT_HANDLE Handle;
1340 BOOLEAN IncompatibilityFlag;
1341
1342 IncompatibilityFlag = FALSE;
1343 Handle = GetDebugPortHandle();
1344
1345 //
1346 // Send init break and wait ack in Timeout
1347 //
1348 DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgSendInitPacket, AsciiStrLen (mErrorMsgSendInitPacket));
1349 if (BreakCause == DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET) {
1350 Status = SendCommandAndWaitForAckOK (DEBUG_COMMAND_INIT_BREAK, Timeout, BreakReceived, &IncompatibilityFlag);
1351 } else {
1352 Status = SendCommandAndWaitForAckOK (DEBUG_COMMAND_ATTACH_BREAK, Timeout, BreakReceived, &IncompatibilityFlag);
1353 }
1354 if (IncompatibilityFlag) {
1355 //
1356 // If the incompatible Debug Packet received, the HOST should be running transfer protocol before DEBUG_AGENT_REVISION.
1357 // It could be UDK Debugger for Windows v1.1/v1.2 or for Linux v0.8/v1.2.
1358 //
1359 DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgVersionAlert, AsciiStrLen (mErrorMsgVersionAlert));
1360 CpuDeadLoop ();
1361 }
1362
1363 if (RETURN_ERROR (Status)) {
1364 DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgConnectFail, AsciiStrLen (mErrorMsgConnectFail));
1365 } else {
1366 DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgConnectOK, AsciiStrLen (mErrorMsgConnectOK));
1367 //
1368 // Set Attach flag
1369 //
1370 SetHostAttached (TRUE);
1371 }
1372 return Status;
1373 }
1374
1375 /**
1376 Send Break point packet to HOST.
1377
1378 Only the first breaking processor could sent BREAK_POINT packet.
1379
1380 @param[in] BreakCause Break cause of this break event.
1381 @param[in] ProcessorIndex Processor index value.
1382 @param[out] BreakReceived If BreakReceived is not NULL,
1383 TRUE is retured if break-in symbol received.
1384 FALSE is retured if break-in symbol not received.
1385
1386 **/
1387 VOID
1388 SendBreakPacketToHost (
1389 IN UINT8 BreakCause,
1390 IN UINT32 ProcessorIndex,
1391 OUT BOOLEAN *BreakReceived
1392 )
1393 {
1394 UINT8 InputCharacter;
1395 DEBUG_PORT_HANDLE Handle;
1396
1397 Handle = GetDebugPortHandle();
1398
1399 if (IsHostAttached ()) {
1400 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Send Break Packet to HOST.\n", ProcessorIndex);
1401 SendCommandAndWaitForAckOK (DEBUG_COMMAND_BREAK_POINT, READ_PACKET_TIMEOUT, BreakReceived, NULL);
1402 } else {
1403 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Try to attach HOST.\n", ProcessorIndex);
1404 //
1405 // If HOST is not attached, try to attach it firstly.
1406 //
1407 //
1408 // Poll Attach symbols from HOST and ack OK
1409 //
1410 do {
1411 DebugPortReadBuffer (Handle, &InputCharacter, 1, 0);
1412 } while (InputCharacter != DEBUG_STARTING_SYMBOL_ATTACH);
1413 SendAckPacket (DEBUG_COMMAND_OK);
1414
1415 //
1416 // Try to attach HOST
1417 //
1418 while (AttachHost (BreakCause, 0, NULL) != RETURN_SUCCESS);
1419
1420 }
1421 }
1422
1423 /**
1424 The main function to process communication with HOST.
1425
1426 It received the command packet from HOST, and sent response data packet to HOST.
1427
1428 @param[in] Vector Vector value of exception or interrutp.
1429 @param[in, out] CpuContext Pointer to saved CPU context.
1430 @param[in] BreakReceived TRUE means break-in symbol received.
1431 FALSE means break-in symbol not received.
1432
1433 **/
1434 VOID
1435 CommandCommunication (
1436 IN UINTN Vector,
1437 IN OUT DEBUG_CPU_CONTEXT *CpuContext,
1438 IN BOOLEAN BreakReceived
1439 )
1440 {
1441 RETURN_STATUS Status;
1442 UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT + sizeof (UINT64) - 1];
1443 DEBUG_PACKET_HEADER *DebugHeader;
1444 UINT8 Width;
1445 UINT8 Data8;
1446 UINT32 Data32;
1447 UINT64 Data64;
1448 DEBUG_DATA_READ_MEMORY *MemoryRead;
1449 DEBUG_DATA_WRITE_MEMORY *MemoryWrite;
1450 DEBUG_DATA_READ_IO *IoRead;
1451 DEBUG_DATA_WRITE_IO *IoWrite;
1452 DEBUG_DATA_READ_REGISTER *RegisterRead;
1453 DEBUG_DATA_WRITE_REGISTER *RegisterWrite;
1454 UINT8 *RegisterBuffer;
1455 DEBUG_DATA_READ_MSR *MsrRegisterRead;
1456 DEBUG_DATA_WRITE_MSR *MsrRegisterWrite;
1457 DEBUG_DATA_CPUID *Cpuid;
1458 DEBUG_DATA_RESPONSE_CPUID CpuidResponse;
1459 DEBUG_DATA_SEARCH_SIGNATURE *SearchSignature;
1460 DEBUG_DATA_RESPONSE_GET_EXCEPTION Exception;
1461 DEBUG_DATA_RESPONSE_GET_REVISION DebugAgentRevision;
1462 DEBUG_DATA_SET_VIEWPOINT *SetViewPoint;
1463 BOOLEAN HaltDeferred;
1464 UINT32 ProcessorIndex;
1465 DEBUG_PORT_HANDLE Handle;
1466 DEBUG_AGENT_EXCEPTION_BUFFER AgentExceptionBuffer;
1467 UINT32 IssuedViewPoint;
1468 DEBUG_AGENT_MAILBOX *Mailbox;
1469 UINT8 *AlignedDataPtr;
1470
1471 ProcessorIndex = 0;
1472 IssuedViewPoint = 0;
1473 HaltDeferred = BreakReceived;
1474
1475 if (MultiProcessorDebugSupport()) {
1476 ProcessorIndex = GetProcessorIndex ();
1477 SetCpuStopFlagByIndex (ProcessorIndex, TRUE);
1478 if (mDebugMpContext.ViewPointIndex == ProcessorIndex) {
1479 //
1480 // Only the current view processor could set AgentInProgress Flag.
1481 //
1482 IssuedViewPoint = ProcessorIndex;
1483 }
1484 }
1485
1486 if (IssuedViewPoint == ProcessorIndex) {
1487 //
1488 // Set AgentInProgress Flag.
1489 //
1490 SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 1);
1491 }
1492
1493 Handle = GetDebugPortHandle();
1494
1495 while (TRUE) {
1496
1497 if (MultiProcessorDebugSupport()) {
1498 //
1499 // Check if the current processor is HOST view point
1500 //
1501 if (mDebugMpContext.ViewPointIndex != ProcessorIndex) {
1502 if (mDebugMpContext.RunCommandSet) {
1503 //
1504 // If HOST view point sets RUN flag, run GO command to leave
1505 //
1506 SetCpuStopFlagByIndex (ProcessorIndex, FALSE);
1507 CommandGo (CpuContext);
1508 break;
1509 } else {
1510 //
1511 // Run into loop again
1512 //
1513 CpuPause ();
1514 continue;
1515 }
1516 }
1517 }
1518
1519 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1520
1521 DebugHeader =(DEBUG_PACKET_HEADER *) InputPacketBuffer;
1522
1523 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "TARGET: Try to get command from HOST...\n");
1524 Status = ReceivePacket ((UINT8 *)DebugHeader, &BreakReceived, NULL, READ_PACKET_TIMEOUT, TRUE);
1525 if (Status != RETURN_SUCCESS || (DebugHeader->Command & DEBUG_COMMAND_RESPONSE) != 0) {
1526 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Get command[%x] sequenceno[%x] returned status is [%x] \n", DebugHeader->Command, DebugHeader->SequenceNo, Status);
1527 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Get command failed or it's response packet not expected! \n");
1528 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1529 continue;
1530 }
1531
1532 Mailbox = GetMailboxPointer ();
1533 if (DebugHeader->SequenceNo == Mailbox->HostSequenceNo) {
1534 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Receive one old command[%x] agaist command[%x]\n", DebugHeader->SequenceNo, Mailbox->HostSequenceNo);
1535 SendAckPacket (Mailbox->LastAck);
1536 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1537 continue;
1538 } else if (DebugHeader->SequenceNo == (UINT8) (Mailbox->HostSequenceNo + 1)) {
1539 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, (UINT8) DebugHeader->SequenceNo);
1540 } else {
1541 DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Receive one invalid comamnd[%x] agaist command[%x]\n", DebugHeader->SequenceNo, Mailbox->HostSequenceNo);
1542 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1543 continue;
1544 }
1545
1546 //
1547 // Save CPU content before executing HOST commond
1548 //
1549 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX, (UINT64)(UINTN) &AgentExceptionBuffer.JumpBuffer);
1550 if (SetJump (&AgentExceptionBuffer.JumpBuffer) != 0) {
1551 //
1552 // If HOST command failed, continue to wait for HOST's next command
1553 // If needed, agent could send exception info to HOST.
1554 //
1555 SendAckPacket (DEBUG_COMMAND_ABORT);
1556 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1557 continue;
1558 }
1559
1560 DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Processor[%x]:Received one command(%x)\n", mDebugMpContext.ViewPointIndex, DebugHeader->Command);
1561
1562 switch (DebugHeader->Command) {
1563
1564 case DEBUG_COMMAND_HALT:
1565 SendAckPacket (DEBUG_COMMAND_HALT_DEFERRED);
1566 HaltDeferred = TRUE;
1567 BreakReceived = FALSE;
1568 Status = RETURN_SUCCESS;
1569 break;
1570
1571 case DEBUG_COMMAND_RESET:
1572 SendAckPacket (DEBUG_COMMAND_OK);
1573 SendAckPacket (DEBUG_COMMAND_OK);
1574 SendAckPacket (DEBUG_COMMAND_OK);
1575 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1576
1577 ResetCold ();
1578 //
1579 // Assume system resets in 2 seconds, otherwise send TIMEOUT packet.
1580 // PCD can be used if 2 seconds isn't long enough for some platforms.
1581 //
1582 MicroSecondDelay (2000000);
1583 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, Mailbox->HostSequenceNo + 1);
1584 SendAckPacket (DEBUG_COMMAND_TIMEOUT);
1585 SendAckPacket (DEBUG_COMMAND_TIMEOUT);
1586 SendAckPacket (DEBUG_COMMAND_TIMEOUT);
1587 break;
1588
1589 case DEBUG_COMMAND_GO:
1590 CommandGo (CpuContext);
1591 //
1592 // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
1593 // If HOST changed Dr0 before GO, we will not change Dr0 here
1594 //
1595 Data8 = GetBreakCause (Vector, CpuContext);
1596 if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {
1597 CpuContext->Dr0 = 0;
1598 }
1599 //
1600 // Clear Stepping Flag
1601 //
1602 SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 0);
1603
1604 if (!HaltDeferred) {
1605 //
1606 // If no HALT command received when being in-active mode
1607 //
1608 if (MultiProcessorDebugSupport()) {
1609 Data32 = FindNextPendingBreakCpu ();
1610 if (Data32 != -1) {
1611 //
1612 // If there are still others processors being in break state,
1613 // send OK packet to HOST to finish this go command
1614 //
1615 SendAckPacket (DEBUG_COMMAND_OK);
1616 CpuPause ();
1617 //
1618 // Set current view to the next breaking processor
1619 //
1620 mDebugMpContext.ViewPointIndex = Data32;
1621 mDebugMpContext.BreakAtCpuIndex = mDebugMpContext.ViewPointIndex;
1622 SetCpuBreakFlagByIndex (mDebugMpContext.ViewPointIndex, FALSE);
1623 //
1624 // Send break packet to HOST to let HOST break again
1625 //
1626 SendBreakPacketToHost (DEBUG_DATA_BREAK_CAUSE_UNKNOWN, mDebugMpContext.BreakAtCpuIndex, &BreakReceived);
1627 //
1628 // Continue to run into loop to read command packet from HOST
1629 //
1630 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1631 break;
1632 }
1633
1634 //
1635 // If no else processor break, set stop bitmask,
1636 // and set Running flag for all processors.
1637 //
1638 SetCpuStopFlagByIndex (ProcessorIndex, FALSE);
1639 SetCpuRunningFlag (TRUE);
1640 CpuPause ();
1641 //
1642 // Wait for all processors are in running state
1643 //
1644 while (TRUE) {
1645 if (IsAllCpuRunning ()) {
1646 break;
1647 }
1648 }
1649 //
1650 // Set BSP to be current view point.
1651 //
1652 SetDebugViewPoint (mDebugMpContext.BspIndex);
1653 CpuPause ();
1654 //
1655 // Clear breaking processor index and running flag
1656 //
1657 mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);
1658 SetCpuRunningFlag (FALSE);
1659 }
1660
1661 //
1662 // Send OK packet to HOST to finish this go command
1663 //
1664 SendAckPacket (DEBUG_COMMAND_OK);
1665
1666 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1667
1668 if (!IsHostAttached()) {
1669 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_SEQUENCE_NO_INDEX, 0);
1670 UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, 0);
1671 }
1672 return;
1673
1674 } else {
1675 //
1676 // If reveived HALT command, need to defer the GO command
1677 //
1678 SendAckPacket (DEBUG_COMMAND_HALT_PROCESSED);
1679 HaltDeferred = FALSE;
1680
1681 Vector = DEBUG_TIMER_VECTOR;
1682 }
1683 break;
1684
1685 case DEBUG_COMMAND_BREAK_CAUSE:
1686
1687 if (MultiProcessorDebugSupport() && ProcessorIndex != mDebugMpContext.BreakAtCpuIndex) {
1688 Status = SendBreakCausePacket (DEBUG_TIMER_VECTOR, CpuContext);
1689
1690 } else {
1691 Status = SendBreakCausePacket (Vector, CpuContext);
1692 }
1693
1694 break;
1695
1696 case DEBUG_COMMAND_SET_HW_BREAKPOINT:
1697 SetDebugRegister (CpuContext, (DEBUG_DATA_SET_HW_BREAKPOINT *) (DebugHeader + 1));
1698 SendAckPacket (DEBUG_COMMAND_OK);
1699 break;
1700
1701 case DEBUG_COMMAND_CLEAR_HW_BREAKPOINT:
1702 ClearDebugRegister (CpuContext, (DEBUG_DATA_CLEAR_HW_BREAKPOINT *) (DebugHeader + 1));
1703 SendAckPacket (DEBUG_COMMAND_OK);
1704 break;
1705
1706 case DEBUG_COMMAND_SINGLE_STEPPING:
1707 CommandStepping (CpuContext);
1708 //
1709 // Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
1710 // If HOST changed Dr0 before GO, we will not change Dr0 here
1711 //
1712 Data8 = GetBreakCause (Vector, CpuContext);
1713 if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {
1714 CpuContext->Dr0 = 0;
1715 }
1716
1717 mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);
1718 //
1719 // Set Stepping Flag
1720 //
1721 SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 1);
1722 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1723 //
1724 // Executing stepping command directly without sending ACK packet,
1725 // ACK packet will be sent after stepping done.
1726 //
1727 return;
1728
1729 case DEBUG_COMMAND_SET_SW_BREAKPOINT:
1730 Data64 = (UINTN) (((DEBUG_DATA_SET_SW_BREAKPOINT *) (DebugHeader + 1))->Address);
1731 Data8 = *(UINT8 *) (UINTN) Data64;
1732 *(UINT8 *) (UINTN) Data64 = DEBUG_SW_BREAKPOINT_SYMBOL;
1733 Status = SendDataResponsePacket ((UINT8 *) &Data8, (UINT16) sizeof (UINT8));
1734 break;
1735
1736 case DEBUG_COMMAND_READ_MEMORY:
1737 MemoryRead = (DEBUG_DATA_READ_MEMORY *) (DebugHeader + 1);
1738 Status = ReadMemoryAndSendResponsePacket ((UINT8 *) (UINTN) MemoryRead->Address, MemoryRead->Count, MemoryRead->Width);
1739 break;
1740
1741 case DEBUG_COMMAND_WRITE_MEMORY:
1742 MemoryWrite = (DEBUG_DATA_WRITE_MEMORY *) (DebugHeader + 1);
1743 //
1744 // Copy data into one memory with 8-byte alignment address
1745 //
1746 AlignedDataPtr = ALIGN_POINTER ((UINT8 *) &MemoryWrite->Data, sizeof (UINT64));
1747 if (AlignedDataPtr != (UINT8 *) &MemoryWrite->Data) {
1748 CopyMem (AlignedDataPtr, (UINT8 *) &MemoryWrite->Data, MemoryWrite->Count * MemoryWrite->Width);
1749 }
1750 CopyMemByWidth ((UINT8 *) (UINTN) MemoryWrite->Address, AlignedDataPtr, MemoryWrite->Count, MemoryWrite->Width);
1751 SendAckPacket (DEBUG_COMMAND_OK);
1752 break;
1753
1754 case DEBUG_COMMAND_READ_IO:
1755 IoRead = (DEBUG_DATA_READ_IO *) (DebugHeader + 1);
1756 switch (IoRead->Width) {
1757 case 1:
1758 Data64 = IoRead8 ((UINTN) IoRead->Port);
1759 break;
1760 case 2:
1761 Data64 = IoRead16 ((UINTN) IoRead->Port);
1762 break;
1763 case 4:
1764 Data64 = IoRead32 ((UINTN) IoRead->Port);
1765 break;
1766 case 8:
1767 Data64 = IoRead64 ((UINTN) IoRead->Port);
1768 break;
1769 default:
1770 Data64 = (UINT64) -1;
1771 }
1772 Status = SendDataResponsePacket ((UINT8 *) &Data64, IoRead->Width);
1773 break;
1774
1775 case DEBUG_COMMAND_WRITE_IO:
1776 IoWrite = (DEBUG_DATA_WRITE_IO *) (DebugHeader + 1);
1777 switch (IoWrite->Width) {
1778 case 1:
1779 Data64 = IoWrite8 ((UINTN) IoWrite->Port, *(UINT8 *) &IoWrite->Data);
1780 break;
1781 case 2:
1782 Data64 = IoWrite16 ((UINTN) IoWrite->Port, *(UINT16 *) &IoWrite->Data);
1783 break;
1784 case 4:
1785 Data64 = IoWrite32 ((UINTN) IoWrite->Port, *(UINT32 *) &IoWrite->Data);
1786 break;
1787 case 8:
1788 Data64 = IoWrite64 ((UINTN) IoWrite->Port, *(UINT64 *) &IoWrite->Data);
1789 break;
1790 default:
1791 Data64 = (UINT64) -1;
1792 }
1793 SendAckPacket (DEBUG_COMMAND_OK);
1794 break;
1795
1796 case DEBUG_COMMAND_READ_ALL_REGISTERS:
1797 Status = SendDataResponsePacket ((UINT8 *) CpuContext, sizeof (*CpuContext));
1798 break;
1799
1800 case DEBUG_COMMAND_READ_REGISTER:
1801 RegisterRead = (DEBUG_DATA_READ_REGISTER *) (DebugHeader + 1);
1802
1803 if (RegisterRead->Index <= SOFT_DEBUGGER_REGISTER_MAX) {
1804 RegisterBuffer = ArchReadRegisterBuffer (CpuContext, RegisterRead->Index, &Width);
1805 Status = SendDataResponsePacket (RegisterBuffer, Width);
1806 } else {
1807 Status = RETURN_UNSUPPORTED;
1808 }
1809 break;
1810
1811 case DEBUG_COMMAND_WRITE_REGISTER:
1812 RegisterWrite = (DEBUG_DATA_WRITE_REGISTER *) (DebugHeader + 1);
1813 if (RegisterWrite->Index <= SOFT_DEBUGGER_REGISTER_MAX) {
1814 RegisterBuffer = ArchReadRegisterBuffer (CpuContext, RegisterWrite->Index, &Width);
1815 ASSERT (Width == RegisterWrite->Length);
1816 CopyMem (RegisterBuffer, RegisterWrite->Data, Width);
1817 SendAckPacket (DEBUG_COMMAND_OK);
1818 } else {
1819 Status = RETURN_UNSUPPORTED;
1820 }
1821 break;
1822
1823 case DEBUG_COMMAND_ARCH_MODE:
1824 Data8 = DEBUG_ARCH_SYMBOL;
1825 Status = SendDataResponsePacket ((UINT8 *) &Data8, (UINT16) sizeof (UINT8));
1826 break;
1827
1828 case DEBUG_COMMAND_READ_MSR:
1829 MsrRegisterRead = (DEBUG_DATA_READ_MSR *) (DebugHeader + 1);
1830 Data64 = AsmReadMsr64 (MsrRegisterRead->Index);
1831 Status = SendDataResponsePacket ((UINT8 *) &Data64, (UINT16) sizeof (UINT64));
1832 break;
1833
1834 case DEBUG_COMMAND_WRITE_MSR:
1835 MsrRegisterWrite = (DEBUG_DATA_WRITE_MSR *) (DebugHeader + 1);
1836 AsmWriteMsr64 (MsrRegisterWrite->Index, MsrRegisterWrite->Value);
1837 SendAckPacket (DEBUG_COMMAND_OK);
1838 break;
1839
1840 case DEBUG_COMMAND_SET_DEBUG_SETTING:
1841 Status = SetDebugSetting ((DEBUG_DATA_SET_DEBUG_SETTING *)(DebugHeader + 1));
1842 if (Status == RETURN_SUCCESS) {
1843 SendAckPacket (DEBUG_COMMAND_OK);
1844 }
1845 break;
1846
1847 case DEBUG_COMMAND_GET_REVISION:
1848 DebugAgentRevision.Revision = DEBUG_AGENT_REVISION;
1849 DebugAgentRevision.Capabilities = DEBUG_AGENT_CAPABILITIES;
1850 Status = SendDataResponsePacket ((UINT8 *) &DebugAgentRevision, (UINT16) sizeof (DEBUG_DATA_RESPONSE_GET_REVISION));
1851 break;
1852
1853 case DEBUG_COMMAND_GET_EXCEPTION:
1854 Exception.ExceptionNum = (UINT8) Vector;
1855 Exception.ExceptionData = (UINT32) CpuContext->ExceptionData;
1856 Status = SendDataResponsePacket ((UINT8 *) &Exception, (UINT16) sizeof (DEBUG_DATA_RESPONSE_GET_EXCEPTION));
1857 break;
1858
1859 case DEBUG_COMMAND_SET_VIEWPOINT:
1860 SetViewPoint = (DEBUG_DATA_SET_VIEWPOINT *) (DebugHeader + 1);
1861 if (MultiProcessorDebugSupport()) {
1862 if (IsCpuStopped (SetViewPoint->ViewPoint)) {
1863 SetDebugViewPoint (SetViewPoint->ViewPoint);
1864 SendAckPacket (DEBUG_COMMAND_OK);
1865 } else {
1866 //
1867 // If CPU is not halted
1868 //
1869 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
1870 }
1871 } else if (SetViewPoint->ViewPoint == 0) {
1872 SendAckPacket (DEBUG_COMMAND_OK);
1873
1874 } else {
1875 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
1876 }
1877
1878 break;
1879
1880 case DEBUG_COMMAND_GET_VIEWPOINT:
1881 Data32 = mDebugMpContext.ViewPointIndex;
1882 SendDataResponsePacket((UINT8 *) &Data32, (UINT16) sizeof (UINT32));
1883 break;
1884
1885 case DEBUG_COMMAND_MEMORY_READY:
1886 Data8 = (UINT8) GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY);
1887 SendDataResponsePacket (&Data8, (UINT16) sizeof (UINT8));
1888 break;
1889
1890 case DEBUG_COMMAND_DETACH:
1891 SetHostAttached (FALSE);
1892 SendAckPacket (DEBUG_COMMAND_OK);
1893 break;
1894
1895 case DEBUG_COMMAND_CPUID:
1896 Cpuid = (DEBUG_DATA_CPUID *) (DebugHeader + 1);
1897 AsmCpuidEx (
1898 Cpuid->Eax, Cpuid->Ecx,
1899 &CpuidResponse.Eax, &CpuidResponse.Ebx,
1900 &CpuidResponse.Ecx, &CpuidResponse.Edx
1901 );
1902 SendDataResponsePacket ((UINT8 *) &CpuidResponse, (UINT16) sizeof (CpuidResponse));
1903 break;
1904
1905 case DEBUG_COMMAND_SEARCH_SIGNATURE:
1906 SearchSignature = (DEBUG_DATA_SEARCH_SIGNATURE *) (DebugHeader + 1);
1907 if ((SearchSignature->Alignment != 0) &&
1908 (SearchSignature->Alignment == GetPowerOfTwo32 (SearchSignature->Alignment))
1909 ) {
1910 if (SearchSignature->Positive) {
1911 for (
1912 Data64 = ALIGN_VALUE ((UINTN) SearchSignature->Start, SearchSignature->Alignment);
1913 Data64 <= SearchSignature->Start + SearchSignature->Count - SearchSignature->DataLength;
1914 Data64 += SearchSignature->Alignment
1915 ) {
1916 if (CompareMem ((VOID *) (UINTN) Data64, &SearchSignature->Data, SearchSignature->DataLength) == 0) {
1917 break;
1918 }
1919 }
1920 if (Data64 > SearchSignature->Start + SearchSignature->Count - SearchSignature->DataLength) {
1921 Data64 = (UINT64) -1;
1922 }
1923 } else {
1924 for (
1925 Data64 = ALIGN_VALUE ((UINTN) SearchSignature->Start - SearchSignature->Alignment, SearchSignature->Alignment);
1926 Data64 >= SearchSignature->Start - SearchSignature->Count;
1927 Data64 -= SearchSignature->Alignment
1928 ) {
1929 if (CompareMem ((VOID *) (UINTN) Data64, &SearchSignature->Data, SearchSignature->DataLength) == 0) {
1930 break;
1931 }
1932 }
1933 if (Data64 < SearchSignature->Start - SearchSignature->Count) {
1934 Data64 = (UINT64) -1;
1935 }
1936 }
1937 SendDataResponsePacket ((UINT8 *) &Data64, (UINT16) sizeof (Data64));
1938 } else {
1939 Status = RETURN_UNSUPPORTED;
1940 }
1941 break;
1942
1943 default:
1944 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
1945 break;
1946 }
1947
1948 if (Status == RETURN_UNSUPPORTED) {
1949 SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
1950 } else if (Status != RETURN_SUCCESS) {
1951 SendAckPacket (DEBUG_COMMAND_ABORT);
1952 }
1953
1954 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
1955 CpuPause ();
1956 }
1957 }
1958
1959 /**
1960 C function called in interrupt handler.
1961
1962 @param[in] Vector Vector value of exception or interrutp.
1963 @param[in] CpuContext Pointer to save CPU context.
1964
1965 **/
1966 VOID
1967 EFIAPI
1968 InterruptProcess (
1969 IN UINT32 Vector,
1970 IN DEBUG_CPU_CONTEXT *CpuContext
1971 )
1972 {
1973 UINT8 InputCharacter;
1974 UINT8 BreakCause;
1975 UINTN SavedEip;
1976 BOOLEAN BreakReceived;
1977 UINT32 ProcessorIndex;
1978 UINT32 CurrentDebugTimerInitCount;
1979 DEBUG_PORT_HANDLE Handle;
1980 UINT8 Data8;
1981 UINT8 *Al;
1982 UINT32 IssuedViewPoint;
1983 DEBUG_AGENT_EXCEPTION_BUFFER *ExceptionBuffer;
1984
1985 ProcessorIndex = 0;
1986 IssuedViewPoint = 0;
1987 BreakReceived = FALSE;
1988
1989 if (mSkipBreakpoint) {
1990 //
1991 // If Skip Breakpoint flag is set, means communication is disturbed by hardware SMI, we need to ignore the break points in SMM
1992 //
1993 if ((Vector == DEBUG_INT1_VECTOR) || (Vector == DEBUG_INT3_VECTOR)) {
1994 DebugPortWriteBuffer (GetDebugPortHandle(), (UINT8 *) mWarningMsgIngoreBreakpoint, AsciiStrLen (mWarningMsgIngoreBreakpoint));
1995 return;
1996 }
1997 }
1998
1999 if (MultiProcessorDebugSupport()) {
2000 ProcessorIndex = GetProcessorIndex ();
2001 //
2002 // If this processor has alreay halted before, need to check it later
2003 //
2004 if (IsCpuStopped (ProcessorIndex)) {
2005 IssuedViewPoint = ProcessorIndex;
2006 }
2007 }
2008
2009 if (IssuedViewPoint == ProcessorIndex && GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) != 1) {
2010 //
2011 // Check if this exception is issued by Debug Agent itself
2012 // If yes, fill the debug agent exception buffer and LongJump() back to
2013 // the saved CPU content in CommandCommunication()
2014 //
2015 if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {
2016 DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Debug agent meet one Exception, ExceptionNum is %d, EIP = 0x%x.\n", Vector, (UINTN)CpuContext->Eip);
2017 ExceptionBuffer = (DEBUG_AGENT_EXCEPTION_BUFFER *) (UINTN) GetMailboxPointer()->ExceptionBufferPointer;
2018 ExceptionBuffer->ExceptionContent.ExceptionNum = (UINT8) Vector;
2019 ExceptionBuffer->ExceptionContent.ExceptionData = (UINT32) CpuContext->ExceptionData;
2020 LongJump ((BASE_LIBRARY_JUMP_BUFFER *)(UINTN)(ExceptionBuffer), 1);
2021 }
2022 }
2023
2024 if (MultiProcessorDebugSupport()) {
2025 //
2026 // If RUN commmand is executing, wait for it done.
2027 //
2028 while (mDebugMpContext.RunCommandSet) {
2029 CpuPause ();
2030 }
2031 }
2032
2033 Handle = GetDebugPortHandle();
2034 BreakCause = GetBreakCause (Vector, CpuContext);
2035 switch (Vector) {
2036 case DEBUG_INT1_VECTOR:
2037 case DEBUG_INT3_VECTOR:
2038 switch (BreakCause) {
2039 case DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET:
2040 if (AttachHost (BreakCause, READ_PACKET_TIMEOUT, &BreakReceived) != RETURN_SUCCESS) {
2041 //
2042 // Try to connect HOST, return if fails
2043 //
2044 break;
2045 }
2046 CommandCommunication (Vector, CpuContext, BreakReceived);
2047 break;
2048
2049 case DEBUG_DATA_BREAK_CAUSE_STEPPING:
2050 //
2051 // Stepping is finished, send Ack package.
2052 //
2053 if (MultiProcessorDebugSupport()) {
2054 mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;
2055 }
2056 SendAckPacket (DEBUG_COMMAND_OK);
2057 CommandCommunication (Vector, CpuContext, BreakReceived);
2058 break;
2059
2060 case DEBUG_DATA_BREAK_CAUSE_MEMORY_READY:
2061 //
2062 // Memory is ready
2063 //
2064 SendCommandAndWaitForAckOK (DEBUG_COMMAND_MEMORY_READY, READ_PACKET_TIMEOUT, &BreakReceived, NULL);
2065 CommandCommunication (Vector, CpuContext, BreakReceived);
2066 break;
2067
2068 case DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD:
2069 case DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD:
2070 //
2071 // Set AL to DEBUG_AGENT_IMAGE_CONTINUE
2072 //
2073 Al = ArchReadRegisterBuffer (CpuContext, SOFT_DEBUGGER_REGISTER_AX, &Data8);
2074 *Al = DEBUG_AGENT_IMAGE_CONTINUE;
2075
2076 if (!IsHostAttached ()) {
2077 //
2078 // If HOST is not connected for image load/unload, return
2079 //
2080 break;
2081 }
2082 //
2083 // Continue to run the following common code
2084 //
2085
2086 case DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT:
2087 case DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT:
2088 default:
2089 //
2090 // Send Break packet to HOST
2091 //
2092 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2093 //
2094 // Only the first breaking processor could send BREAK_POINT to HOST
2095 //
2096 if (IsFirstBreakProcessor (ProcessorIndex)) {
2097 SendBreakPacketToHost (BreakCause, ProcessorIndex, &BreakReceived);
2098 }
2099 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2100
2101 if (Vector == DEBUG_INT3_VECTOR) {
2102 //
2103 // go back address located "0xCC"
2104 //
2105 CpuContext->Eip--;
2106 SavedEip = CpuContext->Eip;
2107 CommandCommunication (Vector, CpuContext, BreakReceived);
2108 if ((SavedEip == CpuContext->Eip) &&
2109 (*(UINT8 *) (UINTN) CpuContext->Eip == DEBUG_SW_BREAKPOINT_SYMBOL)) {
2110 //
2111 // If this is not a software breakpoint set by HOST,
2112 // restore EIP
2113 //
2114 CpuContext->Eip++;
2115 }
2116 } else {
2117 CommandCommunication (Vector, CpuContext, BreakReceived);
2118 }
2119 break;
2120 }
2121
2122 break;
2123
2124 case DEBUG_TIMER_VECTOR:
2125
2126 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2127
2128 if (MultiProcessorDebugSupport()) {
2129 if (IsBsp (ProcessorIndex)) {
2130 //
2131 // If current processor is BSP, check Apic timer's init count if changed,
2132 // it may be re-written when switching BSP.
2133 // If it changed, re-initialize debug timer
2134 //
2135 CurrentDebugTimerInitCount = GetApicTimerInitCount ();
2136 if (mDebugMpContext.DebugTimerInitCount != CurrentDebugTimerInitCount) {
2137 InitializeDebugTimer ();
2138 }
2139 }
2140
2141 if (!IsBsp (ProcessorIndex) || mDebugMpContext.IpiSentByAp) {
2142 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2143 //
2144 // If current processor is not BSP or this is one IPI sent by AP
2145 //
2146 if (mDebugMpContext.BreakAtCpuIndex != (UINT32) (-1)) {
2147 CommandCommunication (Vector, CpuContext, FALSE);
2148 }
2149
2150 //
2151 // Clear EOI before exiting interrupt process routine.
2152 //
2153 SendApicEoi ();
2154 break;
2155 }
2156 }
2157
2158 //
2159 // Only BSP could run here
2160 //
2161 while (TRUE) {
2162 //
2163 // If there is data in debug port, will check whether it is break(attach/break-in) symbol,
2164 // If yes, go into communication mode with HOST.
2165 // If no, exit interrupt process.
2166 //
2167 if (DebugReadBreakSymbol (Handle, &InputCharacter) == EFI_NOT_FOUND) {
2168 break;
2169 }
2170
2171 if ((!IsHostAttached () && (InputCharacter == DEBUG_STARTING_SYMBOL_ATTACH)) ||
2172 (IsHostAttached () && (InputCharacter == DEBUG_COMMAND_HALT)) ||
2173 (IsHostAttached () && (InputCharacter == DEBUG_COMMAND_GO))
2174 ) {
2175 DebugAgentMsgPrint (DEBUG_AGENT_VERBOSE, "Received data [%02x]\n", InputCharacter);
2176 //
2177 // Ack OK for break-in symbol
2178 //
2179 SendAckPacket (DEBUG_COMMAND_OK);
2180
2181 //
2182 // If receive GO command in Debug Timer, means HOST may lost ACK packet before.
2183 //
2184 if (InputCharacter == DEBUG_COMMAND_GO) {
2185 break;
2186 }
2187
2188 if (!IsHostAttached ()) {
2189 //
2190 // Try to attach HOST, if no ack received after 200ms, return
2191 //
2192 if (AttachHost (BreakCause, READ_PACKET_TIMEOUT, &BreakReceived) != RETURN_SUCCESS) {
2193 break;
2194 }
2195 }
2196
2197 if (MultiProcessorDebugSupport()) {
2198 if(FindNextPendingBreakCpu () != -1) {
2199 SetCpuBreakFlagByIndex (ProcessorIndex, TRUE);
2200 } else {
2201 HaltOtherProcessors (ProcessorIndex);
2202 }
2203 }
2204 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2205 CommandCommunication (Vector, CpuContext, BreakReceived);
2206 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2207 break;
2208 }
2209 }
2210
2211 //
2212 // Clear EOI before exiting interrupt process routine.
2213 //
2214 SendApicEoi ();
2215
2216 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2217
2218 break;
2219
2220 default:
2221 if (Vector <= DEBUG_EXCEPT_SIMD) {
2222 if (BreakCause == DEBUG_DATA_BREAK_CAUSE_STEPPING) {
2223 //
2224 // Stepping is finished, send Ack package.
2225 //
2226 if (MultiProcessorDebugSupport()) {
2227 mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;
2228 }
2229 SendAckPacket (DEBUG_COMMAND_OK);
2230 } else {
2231 //
2232 // Exception occurs, send Break packet to HOST
2233 //
2234 AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2235 //
2236 // Only the first breaking processor could send BREAK_POINT to HOST
2237 //
2238 if (IsFirstBreakProcessor (ProcessorIndex)) {
2239 SendBreakPacketToHost (BreakCause, ProcessorIndex, &BreakReceived);
2240 }
2241 ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
2242 }
2243
2244 CommandCommunication (Vector, CpuContext, BreakReceived);
2245 }
2246 break;
2247 }
2248
2249 if (MultiProcessorDebugSupport()) {
2250 //
2251 // Clear flag and wait for all processors run here
2252 //
2253 SetIpiSentByApFlag (FALSE);
2254 while (mDebugMpContext.RunCommandSet) {
2255 CpuPause ();
2256 }
2257
2258 //
2259 // Only current (view) processor could clean up AgentInProgress flag.
2260 //
2261 if (mDebugMpContext.ViewPointIndex == ProcessorIndex) {
2262 IssuedViewPoint = mDebugMpContext.ViewPointIndex;
2263 }
2264 }
2265
2266 if (IssuedViewPoint == ProcessorIndex && GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) != 1) {
2267 //
2268 // If the command is not stepping, clean up AgentInProgress flag
2269 //
2270 SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 0);
2271 }
2272
2273 return;
2274 }
2275