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.
333 Send empty packet to specify that particular command/functionality is not supported.
342 Reads the n-th register's value into an output buffer and sends it as a packet
343 @param SystemContext Register content at time of the exception
344 @param InBuffer This is the input buffer received from gdb server
348 IN EFI_SYSTEM_CONTEXT SystemContext
,
354 Reads the general registers into an output buffer and sends it as a packet
355 @param SystemContext Register content at time of the exception
359 ReadGeneralRegisters (
360 IN EFI_SYSTEM_CONTEXT SystemContext
365 Writes the new value of n-th register received into the input buffer to the n-th register
366 @param SystemContext Register content at time of the exception
367 @param InBuffer This is the input buffer received from gdb server
372 IN EFI_SYSTEM_CONTEXT SystemContext
,
378 Writes the new values received into the input buffer to the general registers
379 @param SystemContext Register content at time of the exception
380 @param InBuffer Pointer to the input buffer received from gdb server
385 WriteGeneralRegisters (
386 IN EFI_SYSTEM_CONTEXT SystemContext
,
392 Find the Length of the area to read and the start address. Finally, pass them to
393 another function, TransferFromMemToOutBufAndSend, that will read from that memory space and
396 @param *PacketData Pointer to Payload data for the packet
405 /** ‘M addr,length :XX...’
406 Find the Length of the area in bytes to write and the start address. Finally, pass them to
407 another function, TransferFromInBufToMem, that will write to that memory space the info in
410 @param PacketData Pointer to Payload data for the packet
420 Continue. addr is Address to resume. If addr is omitted, resume at current
423 @param SystemContext Register content at time of the exception
424 @param *PacketData Pointer to PacketData
430 IN EFI_SYSTEM_CONTEXT SystemContext
,
436 Single step. addr is the Address at which to resume. If addr is omitted, resume
439 @param SystemContext Register content at time of the exception
440 @param PacketData Pointer to Payload data for the packet
445 IN EFI_SYSTEM_CONTEXT SystemContext
,
450 Insert Single Step in the SystemContext
452 @param SystemContext Register content at time of the exception
456 IN EFI_SYSTEM_CONTEXT SystemContext
460 Remove Single Step in the SystemContext
462 @param SystemContext Register content at time of the exception
466 IN EFI_SYSTEM_CONTEXT SystemContext
471 ‘Z1, [addr], [length]’
472 ‘Z2, [addr], [length]’
473 ‘Z3, [addr], [length]’
474 ‘Z4, [addr], [length]’
476 Insert hardware breakpoint/watchpoint at address addr of size length
478 @param SystemContext Register content at time of the exception
479 @param *PacketData Pointer to the Payload data for the packet
485 IN EFI_SYSTEM_CONTEXT SystemContext
,
491 ‘z1, [addr], [length]’
492 ‘z2, [addr], [length]’
493 ‘z3, [addr], [length]’
494 ‘z4, [addr], [length]’
496 Remove hardware breakpoint/watchpoint at address addr of size length
498 @param SystemContext Register content at time of the exception
499 @param *PacketData Pointer to the Payload data for the packet
505 IN EFI_SYSTEM_CONTEXT SystemContext
,
511 Exception Handler for GDB. It will be called for all exceptions
512 registered via the gExceptionType[] array.
514 @param ExceptionType Exception that is being processed
515 @param SystemContext Register content at time of the exception
520 GdbExceptionHandler (
521 IN EFI_EXCEPTION_TYPE ExceptionType
,
522 IN OUT EFI_SYSTEM_CONTEXT SystemContext
527 Periodic callback for GDB. This function is used to catch a ctrl-c or other
528 break in type command from GDB.
530 @param SystemContext Register content at time of the call
535 GdbPeriodicCallBack (
536 IN OUT EFI_SYSTEM_CONTEXT SystemContext
541 Make two serial consoles: 1) StdIn and StdOut via GDB. 2) StdErr via GDB.
543 These console show up on the remote system running GDB
548 GdbInitializeSerialConsole (
554 Send a GDB Remote Serial Protocol Packet
556 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
557 the packet terminating character '#' and the two digit checksum.
559 If an ack '+' is not sent resend the packet, but timeout eventually so we don't end up
560 in an infinite loop. This is so if you unplug the debugger code just keeps running
562 @param PacketData Payload data for the packet
564 @retval Number of bytes of packet data sent.
574 Receive a GDB Remote Serial Protocol Packet
576 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
577 the packet terminating character '#' and the two digit checksum.
579 If host re-starts sending a packet without ending the previous packet, only the last valid packet is processed.
580 (In other words, if received packet is '$12345$12345$123456#checksum', only '$123456#checksum' will be processed.)
582 If an ack '+' is not sent resend the packet
584 @param PacketData Payload data for the packet
586 @retval Number of bytes of packet data received.
591 OUT CHAR8
*PacketData
,
592 IN UINTN PacketDataSize
597 Read data from a FileDescriptor. On success number of bytes read is returned. Zero indicates
598 the end of a file. On error -1 is returned. If count is zero, GdbRead returns zero.
600 @param FileDescriptor Device to talk to.
601 @param Buffer Buffer to hold Count bytes that were read
602 @param Count Number of bytes to transfer.
605 @retval {other} Number of bytes read.
610 IN INTN FileDescriptor
,
617 Write data to a FileDescriptor. On success number of bytes written is returned. Zero indicates
618 nothing was written. On error -1 is returned.
620 @param FileDescriptor Device to talk to.
621 @param Buffer Buffer to hold Count bytes that are to be written
622 @param Count Number of bytes to transfer.
625 @retval {other} Number of bytes written.
630 IN INTN FileDescriptor
,
631 OUT CONST VOID
*Buffer
,
636 FindPointerToRegister (
637 IN EFI_SYSTEM_CONTEXT SystemContext
,
643 IN EFI_SYSTEM_CONTEXT SystemContext
,
649 TransferFromInBufToMem (
656 TransferFromMemToOutBufAndSend (
663 IN EFI_SYSTEM_CONTEXT SystemContext
,
670 EFI_SYSTEM_CONTEXT SystemContext
674 ParseBreakpointPacket (
675 IN CHAR8
*PacketData
,
682 GetBreakpointDataAddress (
683 IN EFI_SYSTEM_CONTEXT SystemContext
,
684 IN UINTN BreakpointNumber
688 GetBreakpointDetected (
689 IN EFI_SYSTEM_CONTEXT SystemContext
694 IN EFI_SYSTEM_CONTEXT SystemContext
,
695 IN UINTN BreakpointNumber
704 FindNextFreeDebugRegister (
705 IN EFI_SYSTEM_CONTEXT SystemContext
,
710 EnableDebugRegister (
711 IN EFI_SYSTEM_CONTEXT SystemContext
,
719 FindMatchingDebugRegister (
720 IN EFI_SYSTEM_CONTEXT SystemContext
,
728 DisableDebugRegister (
729 IN EFI_SYSTEM_CONTEXT SystemContext
,
734 InitializeProcessor (
745 IN EFI_EXCEPTION_TYPE ExceptionType
,
746 IN OUT EFI_SYSTEM_CONTEXT SystemContext