summaryrefslogtreecommitdiff
path: root/EmbeddedPkg/Library/GdbDebugAgent/GdbDebugAgent.h
blob: dfdc016d9d6a45a73177da6d1df322dc5991f6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/** @file
  Private include file for GDB stub

  Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#ifndef __GCC_DEBUG_AGENT_INTERNAL__
#define __GCC_DEBUG_AGENT_INTERNAL__

#include <Uefi.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
#include <Library/GdbSerialLib.h>
#include <Library/PrintLib.h>
#include <Library/CacheMaintenanceLib.h>
#include <Library/DebugAgentTimerLib.h>
#include <Library/DebugAgentLib.h>

#include <IndustryStandard/PeImage.h>
#include <Protocol/DebugSupport.h>

extern CONST CHAR8 mHexToStr[];

// maximum size of input and output buffers
// This value came from the show remote command of the gdb we tested against
#define MAX_BUF_SIZE 2000

// maximum size of address buffer
#define MAX_ADDR_SIZE 32

// maximum size of register number buffer
#define MAX_REG_NUM_BUF_SIZE 32

// maximum size of length buffer
#define MAX_LENGTH_SIZE 32

// maximum size of T signal members
#define MAX_T_SIGNAL_SIZE 64

// the mask used to clear all the cache
#define TF_BIT 0x00000100


//
// GDB Signal definitions - generic names for interrupts
//
#define GDB_SIGINT      2  // Interrupt process via ctrl-c
#define GDB_SIGILL      4  // Illegal instruction
#define GDB_SIGTRAP     5  // Trace Trap (Breakpoint and SingleStep)
#define GDB_SIGEMT      7  // Emulator Trap
#define GDB_SIGFPE      8  // Floating point exception
#define GDB_SIGSEGV     11 // Setgment violation, page fault


//
// GDB File I/O Error values, zero means no error
// Includes all general GDB Unix like error values
//
#define GDB_EBADMEMADDRBUFSIZE   11  // the buffer that stores memory Address to be read from/written to is not the right size
#define GDB_EBADMEMLENGBUFSIZE   12  // the buffer that stores Length is not the right size
#define GDB_EBADMEMLENGTH        13  // Length, the given number of bytes to read or write, is not the right size
#define GDB_EBADMEMDATA          14  // one of the bytes or nibbles of the memory is leess than 0
#define GDB_EBADMEMDATASIZE      15  // the memory data, 'XX..', is too short or too long
#define GDB_EBADBUFSIZE          21  // the buffer created is not the correct size
#define GDB_EINVALIDARG          31  // argument is invalid
#define GDB_ENOSPACE             41  //
#define GDB_EINVALIDBRKPOINTTYPE 51  // the breakpoint type is not recognized
#define GDB_EINVALIDREGNUM       61  // given register number is not valid: either <0 or >=Number of Registers
#define GDB_EUNKNOWN             255 // unknown


//
// These devices are open by GDB so we can just read and write to them
//
#define GDB_STDIN   0x00
#define GDB_STDOUT  0x01
#define GDB_STDERR  0x02

//
//Define Register size for different architectures
//
#if defined (MDE_CPU_IA32)
#define REG_SIZE  32
#elif defined (MDE_CPU_X64)
#define REG_SIZE  64
#elif defined (MDE_CPU_ARM)
#define REG_SIZE  32
#endif


typedef struct {
  EFI_EXCEPTION_TYPE  Exception;
  UINT8               SignalNo;
} EFI_EXCEPTION_TYPE_ENTRY;


#if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64)

//
// Byte packed structure for DR6
// 32-bits on IA-32
// 64-bits on X64.  The upper 32-bits on X64 are reserved
//
typedef union {
  struct {
    UINT32  B0:1;           // Breakpoint condition detected
    UINT32  B1:1;           // Breakpoint condition detected
    UINT32  B2:1;           // Breakpoint condition detected
    UINT32  B3:1;           // Breakpoint condition detected
    UINT32  Reserved_1:9;   // Reserved
    UINT32  BD:1;           // Debug register access detected
    UINT32  BS:1;           // Single step
    UINT32  BT:1;           // Task switch
    UINT32  Reserved_2:16;  // Reserved
  } Bits;
  UINTN     UintN;
} IA32_DR6;

//
// Byte packed structure for DR7
// 32-bits on IA-32
// 64-bits on X64.  The upper 32-bits on X64 are reserved
//
typedef union {
  struct {
    UINT32  L0:1;           // Local breakpoint enable
    UINT32  G0:1;           // Global breakpoint enable
    UINT32  L1:1;           // Local breakpoint enable
    UINT32  G1:1;           // Global breakpoint enable
    UINT32  L2:1;           // Local breakpoint enable
    UINT32  G2:1;           // Global breakpoint enable
    UINT32  L3:1;           // Local breakpoint enable
    UINT32  G3:1;           // Global breakpoint enable
    UINT32  LE:1;           // Local exact breakpoint enable
    UINT32  GE:1;           // Global exact breakpoint enable
    UINT32  Reserved_1:3;   // Reserved
    UINT32  GD:1;           // Global detect enable
    UINT32  Reserved_2:2;   // Reserved
    UINT32  RW0:2;          // Read/Write field
    UINT32  LEN0:2;         // Length field
    UINT32  RW1:2;          // Read/Write field
    UINT32  LEN1:2;         // Length field
    UINT32  RW2:2;          // Read/Write field
    UINT32  LEN2:2;         // Length field
    UINT32  RW3:2;          // Read/Write field
    UINT32  LEN3:2;         // Length field
  } Bits;
  UINTN     UintN;
} IA32_DR7;

#endif /* if defined (MDE_CPU_IA32) || defined (MDE_CPU_X64) */

typedef enum {
  InstructionExecution,   //Hardware breakpoint
  DataWrite,              //watch
  DataRead,               //rwatch
  DataReadWrite,          //awatch
  SoftwareBreakpoint,     //Software breakpoint
  NotSupported
} BREAK_TYPE;

//
// Array of exception types that need to be hooked by the debugger
//
extern EFI_EXCEPTION_TYPE_ENTRY gExceptionType[];

//
// If the periodic callback is called while we are processing an F packet we need
// to let the callback know to not read from the serail stream as it could steal
// characters from the F reponse packet
//
extern BOOLEAN gProcessingFPacket;


/**
 Return the number of entries in the gExceptionType[]

 @retval    UINTN, the number of entries in the gExceptionType[] array.
 **/
UINTN
MaxEfiException (
  VOID
  );


/**
 Check to see if the ISA is supported.
 ISA = Instruction Set Architecture

 @retval    TRUE if Isa is supported,
 FALSE otherwise.
 **/
BOOLEAN
CheckIsa (
  IN    EFI_INSTRUCTION_SET_ARCHITECTURE    Isa
  );


/**
 Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints

 @param  SystemContext        Register content at time of the exception
 @param  GdbExceptionType     GDB exception type
 **/

VOID
GdbSendTSignal (
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  IN  UINT8               GdbExceptionType
  );


/**
 Translates the EFI mapping to GDB mapping

 @param     EFIExceptionType        EFI Exception that is being processed
 @retval    UINTN that corresponds to EFIExceptionType's GDB exception type number
 **/
UINT8
ConvertEFItoGDBtype (
  IN  EFI_EXCEPTION_TYPE EFIExceptionType
  );


/**
 Empties the given buffer
 @param *Buf pointer to the first element in buffer to be emptied
 **/
VOID
EmptyBuffer (
  IN CHAR8  *Buf
  );


/**
 Converts an 8-bit Hex Char into a INTN.

 @param     Char  - the hex character to be converted into UINTN
 @retval    a INTN, from 0 to 15, that corressponds to Char
 -1 if Char is not a hex character
 **/
INTN
HexCharToInt (
  IN  CHAR8 Char
  );


/** 'E NN'
 Send an error with the given error number after converting to hex.
 The error number is put into the buffer in hex. '255' is the biggest errno we can send.
 ex: 162 will be sent as A2.

 @param   errno    the error number that will be sent
 **/
VOID
EFIAPI
SendError (
  IN  UINT8     ErrorNum
  );


/**
 Send 'OK' when the function is done executing successfully.
 **/
VOID
SendSuccess (
  VOID
  );


/**
 Send empty packet to specify that particular command/functionality is not supported.
 **/
VOID
SendNotSupported (
  VOID
  );

/** ‘p n’
 Reads the n-th register's value into an output buffer and sends it as a packet
 @param     SystemContext       Register content at time of the exception
 @param     InBuffer            This is the input buffer received from gdb server
 **/
VOID
ReadNthRegister (
  IN    EFI_SYSTEM_CONTEXT  SystemContext,
  IN    CHAR8               *InBuffer
  );


/** ‘g’
 Reads the general registers into an output buffer  and sends it as a packet
 @param     SystemContext           Register content at time of the exception
 **/
VOID
ReadGeneralRegisters (
  IN    EFI_SYSTEM_CONTEXT  SystemContext
  );


/** ‘P n...=r...’
 Writes the new value of n-th register received into the input buffer to the n-th register
 @param     SystemContext       Register content at time of the exception
 @param     InBuffer            This is the input buffer received from gdb server
 **/
VOID
WriteNthRegister (
  IN    EFI_SYSTEM_CONTEXT  SystemContext,
  IN    CHAR8               *InBuffer
  );


/** ‘G XX...’
 Writes the new values received into the input buffer to the general registers
 @param     SystemContext               Register content at time of the exception
 @param     InBuffer                    Pointer to the input buffer received from gdb server
 **/

VOID
WriteGeneralRegisters (
  IN    EFI_SYSTEM_CONTEXT  SystemContext,
  IN    CHAR8               *InBuffer
  );


/** ‘m addr,length ’
 Find the Length of the area to read and the start addres. Finally, pass them to
 another function, TransferFromMemToOutBufAndSend, that will read from that memory space and
 send it as a packet.

 @param  *PacketData  Pointer to Payload data for the packet
 **/
VOID
ReadFromMemory (
  IN  CHAR8  *PacketData
  );


/** ‘M addr,length :XX...’
 Find the Length of the area in bytes to write and the start addres. Finally, pass them to
 another function, TransferFromInBufToMem, that will write to that memory space the info in
 the input buffer.

 @param   PacketData     Pointer to Payload data for the packet
 **/
VOID
WriteToMemory (
  IN CHAR8 *PacketData
  );


/** ‘c [addr ]’
 Continue. addr is Address to resume. If addr is omitted, resume at current
 Address.

 @param SystemContext Register content at time of the exception
 @param *PacketData   Pointer to PacketData
 **/

VOID
ContinueAtAddress (
  IN  EFI_SYSTEM_CONTEXT   SystemContext,
  IN  CHAR8                *PacketData
  );


/** ‘s [addr ]’
 Single step. addr is the Address at which to resume. If addr is omitted, resume
 at same Address.

 @param SystemContext   Register content at time of the exception
 @param PacketData      Pointer to Payload data for the packet
 **/
VOID
SingleStep (
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  IN  CHAR8               *PacketData
  );

/**
 Insert Single Step in the SystemContext

 @param SystemContext   Register content at time of the exception
 **/
VOID
AddSingleStep (
  IN  EFI_SYSTEM_CONTEXT  SystemContext
  );

/**
 Remove Single Step in the SystemContext

 @param SystemContext   Register content at time of the exception
 **/
VOID
RemoveSingleStep (
  IN  EFI_SYSTEM_CONTEXT  SystemContext
  );


/**
  ‘Z1, [addr], [length]’
  ‘Z2, [addr], [length]’
  ‘Z3, [addr], [length]’
  ‘Z4, [addr], [length]’

  Insert hardware breakpoint/watchpoint at address addr of size length

  @param SystemContext  Register content at time of the exception
  @param *PacketData    Pointer to the Payload data for the packet

**/
VOID
EFIAPI
InsertBreakPoint(
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  IN  CHAR8               *PacketData
  );


/**
  ‘z1, [addr], [length]’
  ‘z2, [addr], [length]’
  ‘z3, [addr], [length]’
  ‘z4, [addr], [length]’

  Remove hardware breakpoint/watchpoint at address addr of size length

  @param SystemContext  Register content at time of the exception
  @param *PacketData    Pointer to the Payload data for the packet

**/
VOID
EFIAPI
RemoveBreakPoint(
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  IN  CHAR8               *PacketData
  );


/**
 Exception Hanldler for GDB. It will be called for all exceptions
 registered via the gExceptionType[] array.

 @param ExceptionType   Exception that is being processed
 @param SystemContext   Register content at time of the exception

 **/
VOID
EFIAPI
GdbExceptionHandler (
  IN     EFI_EXCEPTION_TYPE  ExceptionType,
  IN OUT EFI_SYSTEM_CONTEXT  SystemContext
  );


/**
 Periodic callback for GDB. This function is used to catch a ctrl-c or other
 break in type command from GDB.

 @param SystemContext           Register content at time of the call

 **/
VOID
EFIAPI
GdbPeriodicCallBack (
  IN OUT EFI_SYSTEM_CONTEXT  SystemContext
  );


/**
  Make two serail consoles: 1) StdIn and StdOut via GDB. 2) StdErr via GDB.

  These console show up on the remote system running GDB

**/

VOID
GdbInitializeSerialConsole (
  VOID
  );


/**
  Send a GDB Remote Serial Protocol Packet

  $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
  the packet teminating character '#' and the two digit checksum.

  If an ack '+' is not sent resend the packet, but timeout eventually so we don't end up
  in an infinit loop. This is so if you unplug the debugger code just keeps running

  @param PacketData   Payload data for the packet

  @retval             Number of bytes of packet data sent.

**/
UINTN
SendPacket (
  IN  CHAR8 *PacketData
  );


/**
 Receive a GDB Remote Serial Protocol Packet

 $PacketData#checksum PacketData is passed in and this function adds the packet prefix '$',
 the packet teminating character '#' and the two digit checksum.

 If host re-starts sending a packet without ending the previous packet, only the last valid packet is proccessed.
 (In other words, if received packet is '$12345$12345$123456#checksum', only '$123456#checksum' will be processed.)

 If an ack '+' is not sent resend the packet

 @param PacketData   Payload data for the packet

 @retval             Number of bytes of packet data received.

 **/
UINTN
ReceivePacket (
 OUT  CHAR8 *PacketData,
 IN   UINTN PacketDataSize
 );


/**
  Read data from a FileDescriptor. On success number of bytes read is returned. Zero indicates
  the end of a file. On error -1 is returned. If count is zero, GdbRead returns zero.

  @param  FileDescriptor   Device to talk to.
  @param  Buffer           Buffer to hold Count bytes that were read
  @param  Count            Number of bytes to transfer.

  @retval -1               Error
  @retval {other}          Number of bytes read.

**/
INTN
GdbRead (
  IN  INTN    FileDescriptor,
  OUT VOID    *Buffer,
  IN  UINTN   Count
  );


/**
  Write data to a FileDescriptor. On success number of bytes written is returned. Zero indicates
  nothing was written. On error -1 is returned.

  @param  FileDescriptor   Device to talk to.
  @param  Buffer           Buffer to hold Count bytes that are to be written
  @param  Count            Number of bytes to transfer.

  @retval -1               Error
  @retval {other}          Number of bytes written.

**/
INTN
GdbWrite (
  IN  INTN          FileDescriptor,
  OUT CONST VOID    *Buffer,
  IN  UINTN         Count
  );

UINTN *
FindPointerToRegister (
  IN  EFI_SYSTEM_CONTEXT    SystemContext,
  IN  UINTN                 RegNumber
  );

CHAR8 *
BasicReadRegister (
  IN  EFI_SYSTEM_CONTEXT      SystemContext,
  IN  UINTN                   RegNumber,
  IN  CHAR8                   *OutBufPtr
  );

VOID
TransferFromInBufToMem (
  IN  UINTN   Length,
  IN  UINT8   *Address,
  IN  CHAR8   *NewData
  );

VOID
TransferFromMemToOutBufAndSend (
  IN  UINTN  Length,
  IN  UINT8  *Address
  );

CHAR8 *
BasicWriteRegister (
  IN  EFI_SYSTEM_CONTEXT    SystemContext,
  IN  UINTN                 RegNumber,
  IN  CHAR8                 *InBufPtr
  );

VOID
PrintReg (
  EFI_SYSTEM_CONTEXT SystemContext
  );

UINTN
ParseBreakpointPacket (
  IN  CHAR8 *PacketData,
  OUT UINTN *Type,
  OUT UINTN *Address,
  OUT UINTN *Length
  );

UINTN
GetBreakpointDataAddress (
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  IN  UINTN               BreakpointNumber
  );

UINTN
GetBreakpointDetected (
  IN  EFI_SYSTEM_CONTEXT  SystemContext
  );

BREAK_TYPE
GetBreakpointType (
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  IN  UINTN               BreakpointNumber
  );

UINTN
ConvertLengthData (
  IN  UINTN  Length
  );

EFI_STATUS
FindNextFreeDebugRegister (
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  OUT UINTN               *Register
  );

EFI_STATUS
EnableDebugRegister (
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  IN  UINTN               Register,
  IN  UINTN               Address,
  IN  UINTN               Length,
  IN  UINTN               Type
  );

EFI_STATUS
FindMatchingDebugRegister (
 IN  EFI_SYSTEM_CONTEXT  SystemContext,
 IN  UINTN               Address,
 IN  UINTN               Length,
 IN  UINTN               Type,
 OUT UINTN               *Register
 );

EFI_STATUS
DisableDebugRegister (
 IN  EFI_SYSTEM_CONTEXT  SystemContext,
 IN  UINTN               Register
 );

VOID
InitializeProcessor (
  VOID
  );

/**
 Send the T signal with the given exception type (in gdb order) and possibly with n:r pairs related to the watchpoints

 @param  SystemContext        Register content at time of the exception
 @param  GdbExceptionType     GDB exception type
 **/
VOID
ProcessorSendTSignal (
  IN  EFI_SYSTEM_CONTEXT  SystemContext,
  IN  UINT8               GdbExceptionType,
  IN  OUT CHAR8           *TSignalPtr,
  IN  UINTN               SizeOfBuffer
  );

/**
 Check to see if this exception is related to ctrl-c handling.

 @param ExceptionType     Exception that is being processed
 @param SystemContext     Register content at time of the exception

 @return  TRUE  This was a ctrl-c check that did not find a ctrl-c
 @return  FALSE This was not a ctrl-c check or some one hit ctrl-c
 **/
BOOLEAN
ProcessorControlC (
  IN  EFI_EXCEPTION_TYPE        ExceptionType,
  IN OUT EFI_SYSTEM_CONTEXT     SystemContext
  );


/**
  Initialize debug agent.

  This function is used to set up debug enviroment. It may enable interrupts.

  @param[in] InitFlag   Init flag is used to decide initialize process.
  @param[in] Context    Context needed according to InitFlag, it was optional.

**/
VOID
EFIAPI
DebugAgentHookExceptions (
  IN UINT32                InitFlag,
  IN VOID                  *Context  OPTIONAL
  );


#endif