]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/GdbStub/GdbStubInternal.h
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / GdbStub / GdbStubInternal.h
1 /** @file
2 Private include file for GDB stub
3
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #ifndef __GDB_STUB_INTERNAL__
11 #define __GDB_STUB_INTERNAL__
12
13 #include <Uefi.h>
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>
23
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>
30
31 extern CONST CHAR8 mHexToStr[];
32
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
36
37 // maximum size of address buffer
38 #define MAX_ADDR_SIZE 32
39
40 // maximum size of register number buffer
41 #define MAX_REG_NUM_BUF_SIZE 32
42
43 // maximum size of length buffer
44 #define MAX_LENGTH_SIZE 32
45
46 // maximum size of T signal members
47 #define MAX_T_SIGNAL_SIZE 64
48
49 // the mask used to clear all the cache
50 #define TF_BIT 0x00000100
51
52 //
53 // GDB Signal definitions - generic names for interrupts
54 //
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
60
61 //
62 // GDB File I/O Error values, zero means no error
63 // Includes all general GDB Unix like error values
64 //
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
76
77 //
78 // These devices are open by GDB so we can just read and write to them
79 //
80 #define GDB_STDIN 0x00
81 #define GDB_STDOUT 0x01
82 #define GDB_STDERR 0x02
83
84 //
85 // Define Register size for different architectures
86 //
87 #if defined (MDE_CPU_IA32)
88 #define REG_SIZE 32
89 #elif defined (MDE_CPU_X64)
90 #define REG_SIZE 64
91 #elif defined (MDE_CPU_ARM)
92 #define REG_SIZE 32
93 #endif
94
95 #define GDB_SERIAL_DEV_SIGNATURE SIGNATURE_32 ('g', 'd', 'b', 's')
96
97 typedef struct {
98 VENDOR_DEVICE_PATH VendorDevice;
99 UINT32 Index; // Support more than one
100 EFI_DEVICE_PATH_PROTOCOL End;
101 } GDB_SERIAL_DEVICE_PATH;
102
103 //
104 // Name: SERIAL_DEV
105 // Purpose: To provide device specific information
106 // Fields:
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
111 //
112 typedef struct {
113 UINTN Signature;
114 EFI_HANDLE Handle;
115 EFI_SERIAL_IO_PROTOCOL SerialIo;
116 EFI_SERIAL_IO_MODE SerialMode;
117 GDB_SERIAL_DEVICE_PATH DevicePath;
118 INTN InFileDescriptor;
119 INTN OutFileDescriptor;
120 } GDB_SERIAL_DEV;
121
122 #define GDB_SERIAL_DEV_FROM_THIS(a) CR (a, GDB_SERIAL_DEV, SerialIo, GDB_SERIAL_DEV_SIGNATURE)
123
124 typedef struct {
125 EFI_EXCEPTION_TYPE Exception;
126 UINT8 SignalNo;
127 } EFI_EXCEPTION_TYPE_ENTRY;
128
129 #if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)
130
131 //
132 // Byte packed structure for DR6
133 // 32-bits on IA-32
134 // 64-bits on X64. The upper 32-bits on X64 are reserved
135 //
136 typedef union {
137 struct {
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
147 } Bits;
148 UINTN UintN;
149 } IA32_DR6;
150
151 //
152 // Byte packed structure for DR7
153 // 32-bits on IA-32
154 // 64-bits on X64. The upper 32-bits on X64 are reserved
155 //
156 typedef union {
157 struct {
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
179 } Bits;
180 UINTN UintN;
181 } IA32_DR7;
182
183 #endif /* if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) */
184
185 typedef enum {
186 InstructionExecution, // Hardware breakpoint
187 DataWrite, // watch
188 DataRead, // rwatch
189 DataReadWrite, // awatch
190 SoftwareBreakpoint, // Software breakpoint
191 NotSupported
192 } BREAK_TYPE;
193
194 //
195 // Array of exception types that need to be hooked by the debugger
196 //
197 extern EFI_EXCEPTION_TYPE_ENTRY gExceptionType[];
198
199 //
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.
202 //
203 extern BOOLEAN gCtrlCBreakFlag;
204
205 //
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
209 //
210 extern BOOLEAN gProcessingFPacket;
211
212 // The offsets of registers SystemContext.
213 // The fields in the array are in the gdb ordering.
214 //
215 extern UINTN gRegisterOffsets[];
216
217 /**
218 Return the number of entries in the gExceptionType[]
219
220 @retval UINTN, the number of entries in the gExceptionType[] array.
221 **/
222 UINTN
223 MaxEfiException (
224 VOID
225 );
226
227 /**
228 Return the number of entries in the gRegisters[]
229
230 @retval UINTN, the number of entries (registers) in the gRegisters[] array.
231 **/
232 UINTN
233 MaxRegisterCount (
234 VOID
235 );
236
237 /**
238 Check to see if the ISA is supported.
239 ISA = Instruction Set Architecture
240
241 @retval TRUE if Isa is supported,
242 FALSE otherwise.
243 **/
244 BOOLEAN
245 CheckIsa (
246 IN EFI_INSTRUCTION_SET_ARCHITECTURE Isa
247 );
248
249 /**
250 Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints
251
252 @param SystemContext Register content at time of the exception
253 @param GdbExceptionType GDB exception type
254 **/
255
256 VOID
257 GdbSendTSignal (
258 IN EFI_SYSTEM_CONTEXT SystemContext,
259 IN UINT8 GdbExceptionType
260 );
261
262 /**
263 Translates the EFI mapping to GDB mapping
264
265 @param EFIExceptionType EFI Exception that is being processed
266 @retval UINTN that corresponds to EFIExceptionType's GDB exception type number
267 **/
268 UINT8
269 ConvertEFItoGDBtype (
270 IN EFI_EXCEPTION_TYPE EFIExceptionType
271 );
272
273 /**
274 Empties the given buffer
275 @param *Buf pointer to the first element in buffer to be emptied
276 **/
277 VOID
278 EmptyBuffer (
279 IN CHAR8 *Buf
280 );
281
282 /**
283 Converts an 8-bit Hex Char into a INTN.
284
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
288 **/
289 INTN
290 HexCharToInt (
291 IN CHAR8 Char
292 );
293
294 /** 'E NN'
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.
298
299 @param errno the error number that will be sent
300 **/
301 VOID
302 EFIAPI
303 SendError (
304 IN UINT8 ErrorNum
305 );
306
307 /**
308 Send 'OK' when the function is done executing successfully.
309 **/
310 VOID
311 EFIAPI
312 SendSuccess (
313 VOID
314 );
315
316 /**
317 Send empty packet to specify that particular command/functionality is not supported.
318 **/
319 VOID
320 EFIAPI
321 SendNotSupported (
322 VOID
323 );
324
325 /** ‘p n’
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
329 **/
330 VOID
331 ReadNthRegister (
332 IN EFI_SYSTEM_CONTEXT SystemContext,
333 IN CHAR8 *InBuffer
334 );
335
336 /** ‘g’
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
339 **/
340 VOID
341 EFIAPI
342 ReadGeneralRegisters (
343 IN EFI_SYSTEM_CONTEXT SystemContext
344 );
345
346 /** ‘P n...=r...’
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
350 **/
351 VOID
352 EFIAPI
353 WriteNthRegister (
354 IN EFI_SYSTEM_CONTEXT SystemContext,
355 IN CHAR8 *InBuffer
356 );
357
358 /** ‘G XX...’
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
362 **/
363
364 VOID
365 EFIAPI
366 WriteGeneralRegisters (
367 IN EFI_SYSTEM_CONTEXT SystemContext,
368 IN CHAR8 *InBuffer
369 );
370
371 /** ‘m addr,length ’
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
374 send it as a packet.
375
376 @param *PacketData Pointer to Payload data for the packet
377 **/
378 VOID
379 EFIAPI
380 ReadFromMemory (
381 IN CHAR8 *PacketData
382 );
383
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
387 the input buffer.
388
389 @param PacketData Pointer to Payload data for the packet
390 **/
391 VOID
392 EFIAPI
393 WriteToMemory (
394 IN CHAR8 *PacketData
395 );
396
397 /** ‘c [addr ]’
398 Continue. addr is Address to resume. If addr is omitted, resume at current
399 Address.
400
401 @param SystemContext Register content at time of the exception
402 @param *PacketData Pointer to PacketData
403 **/
404
405 VOID
406 EFIAPI
407 ContinueAtAddress (
408 IN EFI_SYSTEM_CONTEXT SystemContext,
409 IN CHAR8 *PacketData
410 );
411
412 /** ‘s [addr ]’
413 Single step. addr is the Address at which to resume. If addr is omitted, resume
414 at same Address.
415
416 @param SystemContext Register content at time of the exception
417 @param PacketData Pointer to Payload data for the packet
418 **/
419 VOID
420 EFIAPI
421 SingleStep (
422 IN EFI_SYSTEM_CONTEXT SystemContext,
423 IN CHAR8 *PacketData
424 );
425
426 /**
427 Insert Single Step in the SystemContext
428
429 @param SystemContext Register content at time of the exception
430 **/
431 VOID
432 AddSingleStep (
433 IN EFI_SYSTEM_CONTEXT SystemContext
434 );
435
436 /**
437 Remove Single Step in the SystemContext
438
439 @param SystemContext Register content at time of the exception
440 **/
441 VOID
442 RemoveSingleStep (
443 IN EFI_SYSTEM_CONTEXT SystemContext
444 );
445
446 /**
447 ‘Z1, [addr], [length]’
448 ‘Z2, [addr], [length]’
449 ‘Z3, [addr], [length]’
450 ‘Z4, [addr], [length]’
451
452 Insert hardware breakpoint/watchpoint at address addr of size length
453
454 @param SystemContext Register content at time of the exception
455 @param *PacketData Pointer to the Payload data for the packet
456
457 **/
458 VOID
459 EFIAPI
460 InsertBreakPoint (
461 IN EFI_SYSTEM_CONTEXT SystemContext,
462 IN CHAR8 *PacketData
463 );
464
465 /**
466 ‘z1, [addr], [length]’
467 ‘z2, [addr], [length]’
468 ‘z3, [addr], [length]’
469 ‘z4, [addr], [length]’
470
471 Remove hardware breakpoint/watchpoint at address addr of size length
472
473 @param SystemContext Register content at time of the exception
474 @param *PacketData Pointer to the Payload data for the packet
475
476 **/
477 VOID
478 EFIAPI
479 RemoveBreakPoint (
480 IN EFI_SYSTEM_CONTEXT SystemContext,
481 IN CHAR8 *PacketData
482 );
483
484 /**
485 Exception Handler for GDB. It will be called for all exceptions
486 registered via the gExceptionType[] array.
487
488 @param ExceptionType Exception that is being processed
489 @param SystemContext Register content at time of the exception
490
491 **/
492 VOID
493 EFIAPI
494 GdbExceptionHandler (
495 IN EFI_EXCEPTION_TYPE ExceptionType,
496 IN OUT EFI_SYSTEM_CONTEXT SystemContext
497 );
498
499 /**
500 Periodic callback for GDB. This function is used to catch a ctrl-c or other
501 break in type command from GDB.
502
503 @param SystemContext Register content at time of the call
504
505 **/
506 VOID
507 EFIAPI
508 GdbPeriodicCallBack (
509 IN OUT EFI_SYSTEM_CONTEXT SystemContext
510 );
511
512 /**
513 Make two serial consoles: 1) StdIn and StdOut via GDB. 2) StdErr via GDB.
514
515 These console show up on the remote system running GDB
516
517 **/
518
519 VOID
520 GdbInitializeSerialConsole (
521 VOID
522 );
523
524 /**
525 Send a GDB Remote Serial Protocol Packet
526
527 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
528 the packet terminating character '#' and the two digit checksum.
529
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
532
533 @param PacketData Payload data for the packet
534
535 @retval Number of bytes of packet data sent.
536
537 **/
538 UINTN
539 SendPacket (
540 IN CHAR8 *PacketData
541 );
542
543 /**
544 Receive a GDB Remote Serial Protocol Packet
545
546 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
547 the packet terminating character '#' and the two digit checksum.
548
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.)
551
552 If an ack '+' is not sent resend the packet
553
554 @param PacketData Payload data for the packet
555
556 @retval Number of bytes of packet data received.
557
558 **/
559 UINTN
560 ReceivePacket (
561 OUT CHAR8 *PacketData,
562 IN UINTN PacketDataSize
563 );
564
565 /**
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.
568
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.
572
573 @retval -1 Error
574 @retval {other} Number of bytes read.
575
576 **/
577 INTN
578 GdbRead (
579 IN INTN FileDescriptor,
580 OUT VOID *Buffer,
581 IN UINTN Count
582 );
583
584 /**
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.
587
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.
591
592 @retval -1 Error
593 @retval {other} Number of bytes written.
594
595 **/
596 INTN
597 GdbWrite (
598 IN INTN FileDescriptor,
599 OUT CONST VOID *Buffer,
600 IN UINTN Count
601 );
602
603 UINTN *
604 FindPointerToRegister (
605 IN EFI_SYSTEM_CONTEXT SystemContext,
606 IN UINTN RegNumber
607 );
608
609 CHAR8 *
610 BasicReadRegister (
611 IN EFI_SYSTEM_CONTEXT SystemContext,
612 IN UINTN RegNumber,
613 IN CHAR8 *OutBufPtr
614 );
615
616 VOID
617 TransferFromInBufToMem (
618 IN UINTN Length,
619 IN UINT8 *Address,
620 IN CHAR8 *NewData
621 );
622
623 VOID
624 TransferFromMemToOutBufAndSend (
625 IN UINTN Length,
626 IN UINT8 *Address
627 );
628
629 CHAR8 *
630 BasicWriteRegister (
631 IN EFI_SYSTEM_CONTEXT SystemContext,
632 IN UINTN RegNumber,
633 IN CHAR8 *InBufPtr
634 );
635
636 VOID
637 PrintReg (
638 EFI_SYSTEM_CONTEXT SystemContext
639 );
640
641 UINTN
642 ParseBreakpointPacket (
643 IN CHAR8 *PacketData,
644 OUT UINTN *Type,
645 OUT UINTN *Address,
646 OUT UINTN *Length
647 );
648
649 UINTN
650 GetBreakpointDataAddress (
651 IN EFI_SYSTEM_CONTEXT SystemContext,
652 IN UINTN BreakpointNumber
653 );
654
655 UINTN
656 GetBreakpointDetected (
657 IN EFI_SYSTEM_CONTEXT SystemContext
658 );
659
660 BREAK_TYPE
661 GetBreakpointType (
662 IN EFI_SYSTEM_CONTEXT SystemContext,
663 IN UINTN BreakpointNumber
664 );
665
666 UINTN
667 ConvertLengthData (
668 IN UINTN Length
669 );
670
671 EFI_STATUS
672 FindNextFreeDebugRegister (
673 IN EFI_SYSTEM_CONTEXT SystemContext,
674 OUT UINTN *Register
675 );
676
677 EFI_STATUS
678 EnableDebugRegister (
679 IN EFI_SYSTEM_CONTEXT SystemContext,
680 IN UINTN Register,
681 IN UINTN Address,
682 IN UINTN Length,
683 IN UINTN Type
684 );
685
686 EFI_STATUS
687 FindMatchingDebugRegister (
688 IN EFI_SYSTEM_CONTEXT SystemContext,
689 IN UINTN Address,
690 IN UINTN Length,
691 IN UINTN Type,
692 OUT UINTN *Register
693 );
694
695 EFI_STATUS
696 DisableDebugRegister (
697 IN EFI_SYSTEM_CONTEXT SystemContext,
698 IN UINTN Register
699 );
700
701 VOID
702 InitializeProcessor (
703 VOID
704 );
705
706 BOOLEAN
707 ValidateAddress (
708 IN VOID *Address
709 );
710
711 BOOLEAN
712 ValidateException (
713 IN EFI_EXCEPTION_TYPE ExceptionType,
714 IN OUT EFI_SYSTEM_CONTEXT SystemContext
715 );
716
717 #endif