2 Private include file for GDB stub
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #ifndef __GDB_STUB_INTERNAL__
11 #define __GDB_STUB_INTERNAL__
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/UefiLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/GdbSerialLib.h>
22 #include <Library/PrintLib.h>
24 #include <Protocol/DebugSupport.h>
25 #include <Protocol/SerialIo.h>
26 #include <Protocol/LoadedImage.h>
27 #include <Protocol/LoadedImage.h>
28 #include <Guid/DebugImageInfoTable.h>
29 #include <IndustryStandard/PeImage.h>
31 extern CONST CHAR8 mHexToStr
[];
33 // maximum size of input and output buffers
34 // This value came from the show remote command of the gdb we tested against
35 #define MAX_BUF_SIZE 2000
37 // maximum size of address buffer
38 #define MAX_ADDR_SIZE 32
40 // maximum size of register number buffer
41 #define MAX_REG_NUM_BUF_SIZE 32
43 // maximum size of length buffer
44 #define MAX_LENGTH_SIZE 32
46 // maximum size of T signal members
47 #define MAX_T_SIGNAL_SIZE 64
49 // the mask used to clear all the cache
50 #define TF_BIT 0x00000100
54 // GDB Signal definitions - generic names for interrupts
56 #define GDB_SIGILL 4 // Illegal instruction
57 #define GDB_SIGTRAP 5 // Trace Trap (Breakpoint and SingleStep)
58 #define GDB_SIGEMT 7 // Emulator Trap
59 #define GDB_SIGFPE 8 // Floating point exception
60 #define GDB_SIGSEGV 11 // Segment violation, page fault
64 // GDB File I/O Error values, zero means no error
65 // Includes all general GDB Unix like error values
67 #define GDB_EBADMEMADDRBUFSIZE 11 // the buffer that stores memory Address to be read from/written to is not the right size
68 #define GDB_EBADMEMLENGBUFSIZE 12 // the buffer that stores Length is not the right size
69 #define GDB_EBADMEMLENGTH 13 // Length, the given number of bytes to read or write, is not the right size
70 #define GDB_EBADMEMDATA 14 // one of the bytes or nibbles of the memory is less than 0
71 #define GDB_EBADMEMDATASIZE 15 // the memory data, 'XX..', is too short or too long
72 #define GDB_EBADBUFSIZE 21 // the buffer created is not the correct size
73 #define GDB_EINVALIDARG 31 // argument is invalid
74 #define GDB_ENOSPACE 41 //
75 #define GDB_EINVALIDBRKPOINTTYPE 51 // the breakpoint type is not recognized
76 #define GDB_EINVALIDREGNUM 61 // given register number is not valid: either <0 or >=Number of Registers
77 #define GDB_EUNKNOWN 255 // unknown
81 // These devices are open by GDB so we can just read and write to them
83 #define GDB_STDIN 0x00
84 #define GDB_STDOUT 0x01
85 #define GDB_STDERR 0x02
88 //Define Register size for different architectures
90 #if defined (MDE_CPU_IA32)
92 #elif defined (MDE_CPU_X64)
94 #elif defined (MDE_CPU_ARM)
98 #define GDB_SERIAL_DEV_SIGNATURE SIGNATURE_32 ('g', 'd', 'b', 's')
101 VENDOR_DEVICE_PATH VendorDevice
;
102 UINT32 Index
; // Support more than one
103 EFI_DEVICE_PATH_PROTOCOL End
;
104 } GDB_SERIAL_DEVICE_PATH
;
108 // Purpose: To provide device specific information
110 // Signature UINTN: The identity of the serial device
111 // SerialIo SERIAL_IO_PROTOCOL: Serial I/O protocol interface
112 // SerialMode SERIAL_IO_MODE:
113 // DevicePath EFI_DEVICE_PATH_PROTOCOL *: Device path of the serial device
118 EFI_SERIAL_IO_PROTOCOL SerialIo
;
119 EFI_SERIAL_IO_MODE SerialMode
;
120 GDB_SERIAL_DEVICE_PATH DevicePath
;
121 INTN InFileDescriptor
;
122 INTN OutFileDescriptor
;
126 #define GDB_SERIAL_DEV_FROM_THIS(a) CR (a, GDB_SERIAL_DEV, SerialIo, GDB_SERIAL_DEV_SIGNATURE)
130 EFI_EXCEPTION_TYPE Exception
;
132 } EFI_EXCEPTION_TYPE_ENTRY
;
135 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
138 // Byte packed structure for DR6
140 // 64-bits on X64. The upper 32-bits on X64 are reserved
144 UINT32 B0
:1; // Breakpoint condition detected
145 UINT32 B1
:1; // Breakpoint condition detected
146 UINT32 B2
:1; // Breakpoint condition detected
147 UINT32 B3
:1; // Breakpoint condition detected
148 UINT32 Reserved_1
:9; // Reserved
149 UINT32 BD
:1; // Debug register access detected
150 UINT32 BS
:1; // Single step
151 UINT32 BT
:1; // Task switch
152 UINT32 Reserved_2
:16; // Reserved
158 // Byte packed structure for DR7
160 // 64-bits on X64. The upper 32-bits on X64 are reserved
164 UINT32 L0
:1; // Local breakpoint enable
165 UINT32 G0
:1; // Global breakpoint enable
166 UINT32 L1
:1; // Local breakpoint enable
167 UINT32 G1
:1; // Global breakpoint enable
168 UINT32 L2
:1; // Local breakpoint enable
169 UINT32 G2
:1; // Global breakpoint enable
170 UINT32 L3
:1; // Local breakpoint enable
171 UINT32 G3
:1; // Global breakpoint enable
172 UINT32 LE
:1; // Local exact breakpoint enable
173 UINT32 GE
:1; // Global exact breakpoint enable
174 UINT32 Reserved_1
:3; // Reserved
175 UINT32 GD
:1; // Global detect enable
176 UINT32 Reserved_2
:2; // Reserved
177 UINT32 RW0
:2; // Read/Write field
178 UINT32 LEN0
:2; // Length field
179 UINT32 RW1
:2; // Read/Write field
180 UINT32 LEN1
:2; // Length field
181 UINT32 RW2
:2; // Read/Write field
182 UINT32 LEN2
:2; // Length field
183 UINT32 RW3
:2; // Read/Write field
184 UINT32 LEN3
:2; // Length field
189 #endif /* if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) */
192 InstructionExecution
, //Hardware breakpoint
195 DataReadWrite
, //awatch
196 SoftwareBreakpoint
, //Software breakpoint
201 // Array of exception types that need to be hooked by the debugger
203 extern EFI_EXCEPTION_TYPE_ENTRY gExceptionType
[];
206 // Set TRUE if F Reply package signals a ctrl-c. We can not process the Ctrl-c
207 // here we need to wait for the periodic callback to do this.
209 extern BOOLEAN gCtrlCBreakFlag
;
212 // If the periodic callback is called while we are processing an F packet we need
213 // to let the callback know to not read from the serial stream as it could steal
214 // characters from the F response packet
216 extern BOOLEAN gProcessingFPacket
;
219 // The offsets of registers SystemContext.
220 // The fields in the array are in the gdb ordering.
222 extern UINTN gRegisterOffsets
[];
225 Return the number of entries in the gExceptionType[]
227 @retval UINTN, the number of entries in the gExceptionType[] array.
236 Return the number of entries in the gRegisters[]
238 @retval UINTN, the number of entries (registers) in the gRegisters[] array.
247 Check to see if the ISA is supported.
248 ISA = Instruction Set Architecture
250 @retval TRUE if Isa is supported,
255 IN EFI_INSTRUCTION_SET_ARCHITECTURE Isa
260 Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints
262 @param SystemContext Register content at time of the exception
263 @param GdbExceptionType GDB exception type
268 IN EFI_SYSTEM_CONTEXT SystemContext
,
269 IN UINT8 GdbExceptionType
274 Translates the EFI mapping to GDB mapping
276 @param EFIExceptionType EFI Exception that is being processed
277 @retval UINTN that corresponds to EFIExceptionType's GDB exception type number
280 ConvertEFItoGDBtype (
281 IN EFI_EXCEPTION_TYPE EFIExceptionType
286 Empties the given buffer
287 @param *Buf pointer to the first element in buffer to be emptied
296 Converts an 8-bit Hex Char into a INTN.
298 @param Char - the hex character to be converted into UINTN
299 @retval a INTN, from 0 to 15, that corresponds to Char
300 -1 if Char is not a hex character
309 Send an error with the given error number after converting to hex.
310 The error number is put into the buffer in hex. '255' is the biggest errno we can send.
311 ex: 162 will be sent as A2.
313 @param errno the error number that will be sent
323 Send 'OK' when the function is done executing successfully.
332 Send empty packet to specify that particular command/functionality is not supported.
340 Reads the n-th register's value into an output buffer and sends it as a packet
341 @param SystemContext Register content at time of the exception
342 @param InBuffer This is the input buffer received from gdb server
346 IN EFI_SYSTEM_CONTEXT SystemContext
,
352 Reads the general registers into an output buffer and sends it as a packet
353 @param SystemContext Register content at time of the exception
356 ReadGeneralRegisters (
357 IN EFI_SYSTEM_CONTEXT SystemContext
361 /** ‘P n...=r...’
362 Writes the new value of n-th register received into the input buffer to the n-th register
363 @param SystemContext Register content at time of the exception
364 @param InBuffer This is the input buffer received from gdb server
368 IN EFI_SYSTEM_CONTEXT SystemContext
,
374 Writes the new values received into the input buffer to the general registers
375 @param SystemContext Register content at time of the exception
376 @param InBuffer Pointer to the input buffer received from gdb server
380 WriteGeneralRegisters (
381 IN EFI_SYSTEM_CONTEXT SystemContext
,
386 /** ‘m addr,length ’
387 Find the Length of the area to read and the start address. Finally, pass them to
388 another function, TransferFromMemToOutBufAndSend, that will read from that memory space and
391 @param *PacketData Pointer to Payload data for the packet
399 /** ‘M addr,length :XX...’
400 Find the Length of the area in bytes to write and the start address. Finally, pass them to
401 another function, TransferFromInBufToMem, that will write to that memory space the info in
404 @param PacketData Pointer to Payload data for the packet
413 Continue. addr is Address to resume. If addr is omitted, resume at current
416 @param SystemContext Register content at time of the exception
417 @param *PacketData Pointer to PacketData
422 IN EFI_SYSTEM_CONTEXT SystemContext
,
428 Single step. addr is the Address at which to resume. If addr is omitted, resume
431 @param SystemContext Register content at time of the exception
432 @param PacketData Pointer to Payload data for the packet
436 IN EFI_SYSTEM_CONTEXT SystemContext
,
441 Insert Single Step in the SystemContext
443 @param SystemContext Register content at time of the exception
447 IN EFI_SYSTEM_CONTEXT SystemContext
451 Remove Single Step in the SystemContext
453 @param SystemContext Register content at time of the exception
457 IN EFI_SYSTEM_CONTEXT SystemContext
462 ‘Z1, [addr], [length]’
463 ‘Z2, [addr], [length]’
464 ‘Z3, [addr], [length]’
465 ‘Z4, [addr], [length]’
467 Insert hardware breakpoint/watchpoint at address addr of size length
469 @param SystemContext Register content at time of the exception
470 @param *PacketData Pointer to the Payload data for the packet
476 IN EFI_SYSTEM_CONTEXT SystemContext
,
482 ‘z1, [addr], [length]’
483 ‘z2, [addr], [length]’
484 ‘z3, [addr], [length]’
485 ‘z4, [addr], [length]’
487 Remove hardware breakpoint/watchpoint at address addr of size length
489 @param SystemContext Register content at time of the exception
490 @param *PacketData Pointer to the Payload data for the packet
496 IN EFI_SYSTEM_CONTEXT SystemContext
,
502 Exception Handler for GDB. It will be called for all exceptions
503 registered via the gExceptionType[] array.
505 @param ExceptionType Exception that is being processed
506 @param SystemContext Register content at time of the exception
511 GdbExceptionHandler (
512 IN EFI_EXCEPTION_TYPE ExceptionType
,
513 IN OUT EFI_SYSTEM_CONTEXT SystemContext
518 Periodic callback for GDB. This function is used to catch a ctrl-c or other
519 break in type command from GDB.
521 @param SystemContext Register content at time of the call
526 GdbPeriodicCallBack (
527 IN OUT EFI_SYSTEM_CONTEXT SystemContext
532 Make two serial consoles: 1) StdIn and StdOut via GDB. 2) StdErr via GDB.
534 These console show up on the remote system running GDB
539 GdbInitializeSerialConsole (
545 Send a GDB Remote Serial Protocol Packet
547 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
548 the packet terminating character '#' and the two digit checksum.
550 If an ack '+' is not sent resend the packet, but timeout eventually so we don't end up
551 in an infinite loop. This is so if you unplug the debugger code just keeps running
553 @param PacketData Payload data for the packet
555 @retval Number of bytes of packet data sent.
565 Receive a GDB Remote Serial Protocol Packet
567 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
568 the packet terminating character '#' and the two digit checksum.
570 If host re-starts sending a packet without ending the previous packet, only the last valid packet is processed.
571 (In other words, if received packet is '$12345$12345$123456#checksum', only '$123456#checksum' will be processed.)
573 If an ack '+' is not sent resend the packet
575 @param PacketData Payload data for the packet
577 @retval Number of bytes of packet data received.
582 OUT CHAR8
*PacketData
,
583 IN UINTN PacketDataSize
588 Read data from a FileDescriptor. On success number of bytes read is returned. Zero indicates
589 the end of a file. On error -1 is returned. If count is zero, GdbRead returns zero.
591 @param FileDescriptor Device to talk to.
592 @param Buffer Buffer to hold Count bytes that were read
593 @param Count Number of bytes to transfer.
596 @retval {other} Number of bytes read.
601 IN INTN FileDescriptor
,
608 Write data to a FileDescriptor. On success number of bytes written is returned. Zero indicates
609 nothing was written. On error -1 is returned.
611 @param FileDescriptor Device to talk to.
612 @param Buffer Buffer to hold Count bytes that are to be written
613 @param Count Number of bytes to transfer.
616 @retval {other} Number of bytes written.
621 IN INTN FileDescriptor
,
622 OUT CONST VOID
*Buffer
,
627 FindPointerToRegister (
628 IN EFI_SYSTEM_CONTEXT SystemContext
,
634 IN EFI_SYSTEM_CONTEXT SystemContext
,
640 TransferFromInBufToMem (
647 TransferFromMemToOutBufAndSend (
654 IN EFI_SYSTEM_CONTEXT SystemContext
,
661 EFI_SYSTEM_CONTEXT SystemContext
665 ParseBreakpointPacket (
666 IN CHAR8
*PacketData
,
673 GetBreakpointDataAddress (
674 IN EFI_SYSTEM_CONTEXT SystemContext
,
675 IN UINTN BreakpointNumber
679 GetBreakpointDetected (
680 IN EFI_SYSTEM_CONTEXT SystemContext
685 IN EFI_SYSTEM_CONTEXT SystemContext
,
686 IN UINTN BreakpointNumber
695 FindNextFreeDebugRegister (
696 IN EFI_SYSTEM_CONTEXT SystemContext
,
701 EnableDebugRegister (
702 IN EFI_SYSTEM_CONTEXT SystemContext
,
710 FindMatchingDebugRegister (
711 IN EFI_SYSTEM_CONTEXT SystemContext
,
719 DisableDebugRegister (
720 IN EFI_SYSTEM_CONTEXT SystemContext
,
725 InitializeProcessor (
736 IN EFI_EXCEPTION_TYPE ExceptionType
,
737 IN OUT EFI_SYSTEM_CONTEXT SystemContext