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
53 // GDB Signal definitions - generic names for interrupts
55 #define GDB_SIGILL 4 // Illegal instruction
56 #define GDB_SIGTRAP 5 // Trace Trap (Breakpoint and SingleStep)
57 #define GDB_SIGEMT 7 // Emulator Trap
58 #define GDB_SIGFPE 8 // Floating point exception
59 #define GDB_SIGSEGV 11 // Segment violation, page fault
62 // GDB File I/O Error values, zero means no error
63 // Includes all general GDB Unix like error values
65 #define GDB_EBADMEMADDRBUFSIZE 11 // the buffer that stores memory Address to be read from/written to is not the right size
66 #define GDB_EBADMEMLENGBUFSIZE 12 // the buffer that stores Length is not the right size
67 #define GDB_EBADMEMLENGTH 13 // Length, the given number of bytes to read or write, is not the right size
68 #define GDB_EBADMEMDATA 14 // one of the bytes or nibbles of the memory is less than 0
69 #define GDB_EBADMEMDATASIZE 15 // the memory data, 'XX..', is too short or too long
70 #define GDB_EBADBUFSIZE 21 // the buffer created is not the correct size
71 #define GDB_EINVALIDARG 31 // argument is invalid
72 #define GDB_ENOSPACE 41 //
73 #define GDB_EINVALIDBRKPOINTTYPE 51 // the breakpoint type is not recognized
74 #define GDB_EINVALIDREGNUM 61 // given register number is not valid: either <0 or >=Number of Registers
75 #define GDB_EUNKNOWN 255 // unknown
78 // These devices are open by GDB so we can just read and write to them
80 #define GDB_STDIN 0x00
81 #define GDB_STDOUT 0x01
82 #define GDB_STDERR 0x02
85 // Define Register size for different architectures
87 #if defined (MDE_CPU_IA32)
89 #elif defined (MDE_CPU_X64)
91 #elif defined (MDE_CPU_ARM)
95 #define GDB_SERIAL_DEV_SIGNATURE SIGNATURE_32 ('g', 'd', 'b', 's')
98 VENDOR_DEVICE_PATH VendorDevice
;
99 UINT32 Index
; // Support more than one
100 EFI_DEVICE_PATH_PROTOCOL End
;
101 } GDB_SERIAL_DEVICE_PATH
;
105 // Purpose: To provide device specific information
107 // Signature UINTN: The identity of the serial device
108 // SerialIo SERIAL_IO_PROTOCOL: Serial I/O protocol interface
109 // SerialMode SERIAL_IO_MODE:
110 // DevicePath EFI_DEVICE_PATH_PROTOCOL *: Device path of the serial device
115 EFI_SERIAL_IO_PROTOCOL SerialIo
;
116 EFI_SERIAL_IO_MODE SerialMode
;
117 GDB_SERIAL_DEVICE_PATH DevicePath
;
118 INTN InFileDescriptor
;
119 INTN OutFileDescriptor
;
122 #define GDB_SERIAL_DEV_FROM_THIS(a) CR (a, GDB_SERIAL_DEV, SerialIo, GDB_SERIAL_DEV_SIGNATURE)
125 EFI_EXCEPTION_TYPE Exception
;
127 } EFI_EXCEPTION_TYPE_ENTRY
;
129 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
132 // Byte packed structure for DR6
134 // 64-bits on X64. The upper 32-bits on X64 are reserved
138 UINT32 B0
: 1; // Breakpoint condition detected
139 UINT32 B1
: 1; // Breakpoint condition detected
140 UINT32 B2
: 1; // Breakpoint condition detected
141 UINT32 B3
: 1; // Breakpoint condition detected
142 UINT32 Reserved_1
: 9; // Reserved
143 UINT32 BD
: 1; // Debug register access detected
144 UINT32 BS
: 1; // Single step
145 UINT32 BT
: 1; // Task switch
146 UINT32 Reserved_2
: 16; // Reserved
152 // Byte packed structure for DR7
154 // 64-bits on X64. The upper 32-bits on X64 are reserved
158 UINT32 L0
: 1; // Local breakpoint enable
159 UINT32 G0
: 1; // Global breakpoint enable
160 UINT32 L1
: 1; // Local breakpoint enable
161 UINT32 G1
: 1; // Global breakpoint enable
162 UINT32 L2
: 1; // Local breakpoint enable
163 UINT32 G2
: 1; // Global breakpoint enable
164 UINT32 L3
: 1; // Local breakpoint enable
165 UINT32 G3
: 1; // Global breakpoint enable
166 UINT32 LE
: 1; // Local exact breakpoint enable
167 UINT32 GE
: 1; // Global exact breakpoint enable
168 UINT32 Reserved_1
: 3; // Reserved
169 UINT32 GD
: 1; // Global detect enable
170 UINT32 Reserved_2
: 2; // Reserved
171 UINT32 RW0
: 2; // Read/Write field
172 UINT32 LEN0
: 2; // Length field
173 UINT32 RW1
: 2; // Read/Write field
174 UINT32 LEN1
: 2; // Length field
175 UINT32 RW2
: 2; // Read/Write field
176 UINT32 LEN2
: 2; // Length field
177 UINT32 RW3
: 2; // Read/Write field
178 UINT32 LEN3
: 2; // Length field
183 #endif /* if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) */
186 InstructionExecution
, // Hardware breakpoint
189 DataReadWrite
, // awatch
190 SoftwareBreakpoint
, // Software breakpoint
195 // Array of exception types that need to be hooked by the debugger
197 extern EFI_EXCEPTION_TYPE_ENTRY gExceptionType
[];
200 // Set TRUE if F Reply package signals a ctrl-c. We can not process the Ctrl-c
201 // here we need to wait for the periodic callback to do this.
203 extern BOOLEAN gCtrlCBreakFlag
;
206 // If the periodic callback is called while we are processing an F packet we need
207 // to let the callback know to not read from the serial stream as it could steal
208 // characters from the F response packet
210 extern BOOLEAN gProcessingFPacket
;
212 // The offsets of registers SystemContext.
213 // The fields in the array are in the gdb ordering.
215 extern UINTN gRegisterOffsets
[];
218 Return the number of entries in the gExceptionType[]
220 @retval UINTN, the number of entries in the gExceptionType[] array.
228 Return the number of entries in the gRegisters[]
230 @retval UINTN, the number of entries (registers) in the gRegisters[] array.
238 Check to see if the ISA is supported.
239 ISA = Instruction Set Architecture
241 @retval TRUE if Isa is supported,
246 IN EFI_INSTRUCTION_SET_ARCHITECTURE Isa
250 Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints
252 @param SystemContext Register content at time of the exception
253 @param GdbExceptionType GDB exception type
258 IN EFI_SYSTEM_CONTEXT SystemContext
,
259 IN UINT8 GdbExceptionType
263 Translates the EFI mapping to GDB mapping
265 @param EFIExceptionType EFI Exception that is being processed
266 @retval UINTN that corresponds to EFIExceptionType's GDB exception type number
269 ConvertEFItoGDBtype (
270 IN EFI_EXCEPTION_TYPE EFIExceptionType
274 Empties the given buffer
275 @param *Buf pointer to the first element in buffer to be emptied
283 Converts an 8-bit Hex Char into a INTN.
285 @param Char - the hex character to be converted into UINTN
286 @retval a INTN, from 0 to 15, that corresponds to Char
287 -1 if Char is not a hex character
295 Send an error with the given error number after converting to hex.
296 The error number is put into the buffer in hex. '255' is the biggest errno we can send.
297 ex: 162 will be sent as A2.
299 @param errno the error number that will be sent
308 Send 'OK' when the function is done executing successfully.
317 Send empty packet to specify that particular command/functionality is not supported.
326 Reads the n-th register's value into an output buffer and sends it as a packet
327 @param SystemContext Register content at time of the exception
328 @param InBuffer This is the input buffer received from gdb server
332 IN EFI_SYSTEM_CONTEXT SystemContext
,
337 Reads the general registers into an output buffer and sends it as a packet
338 @param SystemContext Register content at time of the exception
342 ReadGeneralRegisters (
343 IN EFI_SYSTEM_CONTEXT SystemContext
347 Writes the new value of n-th register received into the input buffer to the n-th register
348 @param SystemContext Register content at time of the exception
349 @param InBuffer This is the input buffer received from gdb server
354 IN EFI_SYSTEM_CONTEXT SystemContext
,
359 Writes the new values received into the input buffer to the general registers
360 @param SystemContext Register content at time of the exception
361 @param InBuffer Pointer to the input buffer received from gdb server
366 WriteGeneralRegisters (
367 IN EFI_SYSTEM_CONTEXT SystemContext
,
372 Find the Length of the area to read and the start address. Finally, pass them to
373 another function, TransferFromMemToOutBufAndSend, that will read from that memory space and
376 @param *PacketData Pointer to Payload data for the packet
384 /** ‘M addr,length :XX...’
385 Find the Length of the area in bytes to write and the start address. Finally, pass them to
386 another function, TransferFromInBufToMem, that will write to that memory space the info in
389 @param PacketData Pointer to Payload data for the packet
398 Continue. addr is Address to resume. If addr is omitted, resume at current
401 @param SystemContext Register content at time of the exception
402 @param *PacketData Pointer to PacketData
408 IN EFI_SYSTEM_CONTEXT SystemContext
,
413 Single step. addr is the Address at which to resume. If addr is omitted, resume
416 @param SystemContext Register content at time of the exception
417 @param PacketData Pointer to Payload data for the packet
422 IN EFI_SYSTEM_CONTEXT SystemContext
,
427 Insert Single Step in the SystemContext
429 @param SystemContext Register content at time of the exception
433 IN EFI_SYSTEM_CONTEXT SystemContext
437 Remove Single Step in the SystemContext
439 @param SystemContext Register content at time of the exception
443 IN EFI_SYSTEM_CONTEXT SystemContext
447 ‘Z1, [addr], [length]’
448 ‘Z2, [addr], [length]’
449 ‘Z3, [addr], [length]’
450 ‘Z4, [addr], [length]’
452 Insert hardware breakpoint/watchpoint at address addr of size length
454 @param SystemContext Register content at time of the exception
455 @param *PacketData Pointer to the Payload data for the packet
461 IN EFI_SYSTEM_CONTEXT SystemContext
,
466 ‘z1, [addr], [length]’
467 ‘z2, [addr], [length]’
468 ‘z3, [addr], [length]’
469 ‘z4, [addr], [length]’
471 Remove hardware breakpoint/watchpoint at address addr of size length
473 @param SystemContext Register content at time of the exception
474 @param *PacketData Pointer to the Payload data for the packet
480 IN EFI_SYSTEM_CONTEXT SystemContext
,
485 Exception Handler for GDB. It will be called for all exceptions
486 registered via the gExceptionType[] array.
488 @param ExceptionType Exception that is being processed
489 @param SystemContext Register content at time of the exception
494 GdbExceptionHandler (
495 IN EFI_EXCEPTION_TYPE ExceptionType
,
496 IN OUT EFI_SYSTEM_CONTEXT SystemContext
500 Periodic callback for GDB. This function is used to catch a ctrl-c or other
501 break in type command from GDB.
503 @param SystemContext Register content at time of the call
508 GdbPeriodicCallBack (
509 IN OUT EFI_SYSTEM_CONTEXT SystemContext
513 Make two serial consoles: 1) StdIn and StdOut via GDB. 2) StdErr via GDB.
515 These console show up on the remote system running GDB
520 GdbInitializeSerialConsole (
525 Send a GDB Remote Serial Protocol Packet
527 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
528 the packet terminating character '#' and the two digit checksum.
530 If an ack '+' is not sent resend the packet, but timeout eventually so we don't end up
531 in an infinite loop. This is so if you unplug the debugger code just keeps running
533 @param PacketData Payload data for the packet
535 @retval Number of bytes of packet data sent.
544 Receive a GDB Remote Serial Protocol Packet
546 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
547 the packet terminating character '#' and the two digit checksum.
549 If host re-starts sending a packet without ending the previous packet, only the last valid packet is processed.
550 (In other words, if received packet is '$12345$12345$123456#checksum', only '$123456#checksum' will be processed.)
552 If an ack '+' is not sent resend the packet
554 @param PacketData Payload data for the packet
556 @retval Number of bytes of packet data received.
561 OUT CHAR8
*PacketData
,
562 IN UINTN PacketDataSize
566 Read data from a FileDescriptor. On success number of bytes read is returned. Zero indicates
567 the end of a file. On error -1 is returned. If count is zero, GdbRead returns zero.
569 @param FileDescriptor Device to talk to.
570 @param Buffer Buffer to hold Count bytes that were read
571 @param Count Number of bytes to transfer.
574 @retval {other} Number of bytes read.
579 IN INTN FileDescriptor
,
585 Write data to a FileDescriptor. On success number of bytes written is returned. Zero indicates
586 nothing was written. On error -1 is returned.
588 @param FileDescriptor Device to talk to.
589 @param Buffer Buffer to hold Count bytes that are to be written
590 @param Count Number of bytes to transfer.
593 @retval {other} Number of bytes written.
598 IN INTN FileDescriptor
,
599 OUT CONST VOID
*Buffer
,
604 FindPointerToRegister (
605 IN EFI_SYSTEM_CONTEXT SystemContext
,
611 IN EFI_SYSTEM_CONTEXT SystemContext
,
617 TransferFromInBufToMem (
624 TransferFromMemToOutBufAndSend (
631 IN EFI_SYSTEM_CONTEXT SystemContext
,
638 EFI_SYSTEM_CONTEXT SystemContext
642 ParseBreakpointPacket (
643 IN CHAR8
*PacketData
,
650 GetBreakpointDataAddress (
651 IN EFI_SYSTEM_CONTEXT SystemContext
,
652 IN UINTN BreakpointNumber
656 GetBreakpointDetected (
657 IN EFI_SYSTEM_CONTEXT SystemContext
662 IN EFI_SYSTEM_CONTEXT SystemContext
,
663 IN UINTN BreakpointNumber
672 FindNextFreeDebugRegister (
673 IN EFI_SYSTEM_CONTEXT SystemContext
,
678 EnableDebugRegister (
679 IN EFI_SYSTEM_CONTEXT SystemContext
,
687 FindMatchingDebugRegister (
688 IN EFI_SYSTEM_CONTEXT SystemContext
,
696 DisableDebugRegister (
697 IN EFI_SYSTEM_CONTEXT SystemContext
,
702 InitializeProcessor (
713 IN EFI_EXCEPTION_TYPE ExceptionType
,
714 IN OUT EFI_SYSTEM_CONTEXT SystemContext