forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdb-stub.c
1923 lines (1668 loc) · 44.5 KB
/
gdb-stub.c
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* MN10300 GDB stub
*
* Originally written by Glenn Engel, Lake Stevens Instrument Division
*
* Contributed by HP Systems
*
* Modified for SPARC by Stu Grossman, Cygnus Support.
*
* Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
* Send complaints, suggestions etc. to <[email protected]>
*
* Copyright (C) 1995 Andreas Busse
*
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
* Modified for Linux/mn10300 by David Howells <[email protected]>
*/
/*
* To enable debugger support, two things need to happen. One, a
* call to set_debug_traps() is necessary in order to allow any breakpoints
* or error conditions to be properly intercepted and reported to gdb.
* Two, a breakpoint needs to be generated to begin communication. This
* is most easily accomplished by a call to breakpoint(). Breakpoint()
* simulates a breakpoint by executing a BREAK instruction.
*
*
* The following gdb commands are supported:
*
* command function Return value
*
* g return the value of the CPU registers hex data or ENN
* G set the value of the CPU registers OK or ENN
*
* mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
* MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
*
* c Resume at current address SNN ( signal NN)
* cAA..AA Continue at address AA..AA SNN
*
* s Step one instruction SNN
* sAA..AA Step one instruction from AA..AA SNN
*
* k kill
*
* ? What was the last sigval ? SNN (signal NN)
*
* bBB..BB Set baud rate to BB..BB OK or BNN, then sets
* baud rate
*
* All commands and responses are sent with a packet which includes a
* checksum. A packet consists of
*
* $<packet info>#<checksum>.
*
* where
* <packet info> :: <characters representing the command or response>
* <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
*
* When a packet is received, it is first acknowledged with either '+' or '-'.
* '+' indicates a successful transfer. '-' indicates a failed transfer.
*
* Example:
*
* Host: Reply:
* $m0,10#2a +$00010203040506070809101112131415#42
*
*
* ==============
* MORE EXAMPLES:
* ==============
*
* For reference -- the following are the steps that one
* company took (RidgeRun Inc) to get remote gdb debugging
* going. In this scenario the host machine was a PC and the
* target platform was a Galileo EVB64120A MIPS evaluation
* board.
*
* Step 1:
* First download gdb-5.0.tar.gz from the internet.
* and then build/install the package.
*
* Example:
* $ tar zxf gdb-5.0.tar.gz
* $ cd gdb-5.0
* $ ./configure --target=am33_2.0-linux-gnu
* $ make
* $ install
* am33_2.0-linux-gnu-gdb
*
* Step 2:
* Configure linux for remote debugging and build it.
*
* Example:
* $ cd ~/linux
* $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
* $ make dep; make vmlinux
*
* Step 3:
* Download the kernel to the remote target and start
* the kernel running. It will promptly halt and wait
* for the host gdb session to connect. It does this
* since the "Kernel Hacking" option has defined
* CONFIG_REMOTE_DEBUG which in turn enables your calls
* to:
* set_debug_traps();
* breakpoint();
*
* Step 4:
* Start the gdb session on the host.
*
* Example:
* $ am33_2.0-linux-gnu-gdb vmlinux
* (gdb) set remotebaud 115200
* (gdb) target remote /dev/ttyS1
* ...at this point you are connected to
* the remote target and can use gdb
* in the normal fasion. Setting
* breakpoints, single stepping,
* printing variables, etc.
*
*/
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/console.h>
#include <linux/init.h>
#include <linux/bug.h>
#include <asm/pgtable.h>
#include <asm/gdb-stub.h>
#include <asm/exceptions.h>
#include <asm/debugger.h>
#include <asm/serial-regs.h>
#include <asm/busctl-regs.h>
#include <unit/leds.h>
#include <unit/serial.h>
/* define to use F7F7 rather than FF which is subverted by JTAG debugger */
#undef GDBSTUB_USE_F7F7_AS_BREAKPOINT
/*
* BUFMAX defines the maximum number of characters in inbound/outbound buffers
* at least NUMREGBYTES*2 are needed for register packets
*/
#define BUFMAX 2048
static const char gdbstub_banner[] =
"Linux/MN10300 GDB Stub (c) RedHat 2007\n";
u8 gdbstub_rx_buffer[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
u32 gdbstub_rx_inp;
u32 gdbstub_rx_outp;
u8 gdbstub_busy;
u8 gdbstub_rx_overflow;
u8 gdbstub_rx_unget;
static u8 gdbstub_flush_caches;
static char input_buffer[BUFMAX];
static char output_buffer[BUFMAX];
static char trans_buffer[BUFMAX];
struct gdbstub_bkpt {
u8 *addr; /* address of breakpoint */
u8 len; /* size of breakpoint */
u8 origbytes[7]; /* original bytes */
};
static struct gdbstub_bkpt gdbstub_bkpts[256];
/*
* local prototypes
*/
static void getpacket(char *buffer);
static int putpacket(char *buffer);
static int computeSignal(enum exception_code excep);
static int hex(unsigned char ch);
static int hexToInt(char **ptr, int *intValue);
static unsigned char *mem2hex(const void *mem, char *buf, int count,
int may_fault);
static const char *hex2mem(const char *buf, void *_mem, int count,
int may_fault);
/*
* Convert ch from a hex digit to an int
*/
static int hex(unsigned char ch)
{
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
return -1;
}
#ifdef CONFIG_GDBSTUB_DEBUGGING
void debug_to_serial(const char *p, int n)
{
__debug_to_serial(p, n);
/* gdbstub_console_write(NULL, p, n); */
}
void gdbstub_printk(const char *fmt, ...)
{
va_list args;
int len;
/* Emit the output into the temporary buffer */
va_start(args, fmt);
len = vsnprintf(trans_buffer, sizeof(trans_buffer), fmt, args);
va_end(args);
debug_to_serial(trans_buffer, len);
}
#endif
static inline char *gdbstub_strcpy(char *dst, const char *src)
{
int loop = 0;
while ((dst[loop] = src[loop]))
loop++;
return dst;
}
/*
* scan for the sequence $<data>#<checksum>
*/
static void getpacket(char *buffer)
{
unsigned char checksum;
unsigned char xmitcsum;
unsigned char ch;
int count, i, ret, error;
for (;;) {
/*
* wait around for the start character,
* ignore all other characters
*/
do {
gdbstub_io_rx_char(&ch, 0);
} while (ch != '$');
checksum = 0;
xmitcsum = -1;
count = 0;
error = 0;
/*
* now, read until a # or end of buffer is found
*/
while (count < BUFMAX) {
ret = gdbstub_io_rx_char(&ch, 0);
if (ret < 0)
error = ret;
if (ch == '#')
break;
checksum += ch;
buffer[count] = ch;
count++;
}
if (error == -EIO) {
gdbstub_proto("### GDB Rx Error - Skipping packet"
" ###\n");
gdbstub_proto("### GDB Tx NAK\n");
gdbstub_io_tx_char('-');
continue;
}
if (count >= BUFMAX || error)
continue;
buffer[count] = 0;
/* read the checksum */
ret = gdbstub_io_rx_char(&ch, 0);
if (ret < 0)
error = ret;
xmitcsum = hex(ch) << 4;
ret = gdbstub_io_rx_char(&ch, 0);
if (ret < 0)
error = ret;
xmitcsum |= hex(ch);
if (error) {
if (error == -EIO)
gdbstub_io("### GDB Rx Error -"
" Skipping packet\n");
gdbstub_io("### GDB Tx NAK\n");
gdbstub_io_tx_char('-');
continue;
}
/* check the checksum */
if (checksum != xmitcsum) {
gdbstub_io("### GDB Tx NAK\n");
gdbstub_io_tx_char('-'); /* failed checksum */
continue;
}
gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
gdbstub_io("### GDB Tx ACK\n");
gdbstub_io_tx_char('+'); /* successful transfer */
/*
* if a sequence char is present,
* reply the sequence ID
*/
if (buffer[2] == ':') {
gdbstub_io_tx_char(buffer[0]);
gdbstub_io_tx_char(buffer[1]);
/*
* remove sequence chars from buffer
*/
count = 0;
while (buffer[count])
count++;
for (i = 3; i <= count; i++)
buffer[i - 3] = buffer[i];
}
break;
}
}
/*
* send the packet in buffer.
* - return 0 if successfully ACK'd
* - return 1 if abandoned due to new incoming packet
*/
static int putpacket(char *buffer)
{
unsigned char checksum;
unsigned char ch;
int count;
/*
* $<packet info>#<checksum>.
*/
gdbstub_proto("### GDB Tx $'%s'#?? ###\n", buffer);
do {
gdbstub_io_tx_char('$');
checksum = 0;
count = 0;
while ((ch = buffer[count]) != 0) {
gdbstub_io_tx_char(ch);
checksum += ch;
count += 1;
}
gdbstub_io_tx_char('#');
gdbstub_io_tx_char(hex_asc_hi(checksum));
gdbstub_io_tx_char(hex_asc_lo(checksum));
} while (gdbstub_io_rx_char(&ch, 0),
ch == '-' && (gdbstub_io("### GDB Rx NAK\n"), 0),
ch != '-' && ch != '+' &&
(gdbstub_io("### GDB Rx ??? %02x\n", ch), 0),
ch != '+' && ch != '$');
if (ch == '+') {
gdbstub_io("### GDB Rx ACK\n");
return 0;
}
gdbstub_io("### GDB Tx Abandoned\n");
gdbstub_rx_unget = ch;
return 1;
}
/*
* While we find nice hex chars, build an int.
* Return number of chars processed.
*/
static int hexToInt(char **ptr, int *intValue)
{
int numChars = 0;
int hexValue;
*intValue = 0;
while (**ptr) {
hexValue = hex(**ptr);
if (hexValue < 0)
break;
*intValue = (*intValue << 4) | hexValue;
numChars++;
(*ptr)++;
}
return (numChars);
}
#ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
/*
* We single-step by setting breakpoints. When an exception
* is handled, we need to restore the instructions hoisted
* when the breakpoints were set.
*
* This is where we save the original instructions.
*/
static struct gdb_bp_save {
u8 *addr;
u8 opcode[2];
} step_bp[2];
static const unsigned char gdbstub_insn_sizes[256] =
{
/* 1 2 3 4 5 6 7 8 9 a b c d e f */
1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, /* 0 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 1 */
2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, /* 2 */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, /* 3 */
1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, /* 4 */
1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, /* 5 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */
2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 8 */
2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 9 */
2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* a */
2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* b */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, /* c */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */
0, 2, 2, 2, 2, 2, 2, 4, 0, 3, 0, 4, 0, 6, 7, 1 /* f */
};
static int __gdbstub_mark_bp(u8 *addr, int ix)
{
/* vmalloc area */
if (((u8 *) VMALLOC_START <= addr) && (addr < (u8 *) VMALLOC_END))
goto okay;
/* SRAM, SDRAM */
if (((u8 *) 0x80000000UL <= addr) && (addr < (u8 *) 0xa0000000UL))
goto okay;
return 0;
okay:
if (gdbstub_read_byte(addr + 0, &step_bp[ix].opcode[0]) < 0 ||
gdbstub_read_byte(addr + 1, &step_bp[ix].opcode[1]) < 0)
return 0;
step_bp[ix].addr = addr;
return 1;
}
static inline void __gdbstub_restore_bp(void)
{
#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
if (step_bp[0].addr) {
gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
gdbstub_write_byte(step_bp[0].opcode[1], step_bp[0].addr + 1);
}
if (step_bp[1].addr) {
gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
gdbstub_write_byte(step_bp[1].opcode[1], step_bp[1].addr + 1);
}
#else
if (step_bp[0].addr)
gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
if (step_bp[1].addr)
gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
#endif
gdbstub_flush_caches = 1;
step_bp[0].addr = NULL;
step_bp[0].opcode[0] = 0;
step_bp[0].opcode[1] = 0;
step_bp[1].addr = NULL;
step_bp[1].opcode[0] = 0;
step_bp[1].opcode[1] = 0;
}
/*
* emulate single stepping by means of breakpoint instructions
*/
static int gdbstub_single_step(struct pt_regs *regs)
{
unsigned size;
uint32_t x;
uint8_t cur, *pc, *sp;
step_bp[0].addr = NULL;
step_bp[0].opcode[0] = 0;
step_bp[0].opcode[1] = 0;
step_bp[1].addr = NULL;
step_bp[1].opcode[0] = 0;
step_bp[1].opcode[1] = 0;
x = 0;
pc = (u8 *) regs->pc;
sp = (u8 *) (regs + 1);
if (gdbstub_read_byte(pc, &cur) < 0)
return -EFAULT;
gdbstub_bkpt("Single Step from %p { %02x }\n", pc, cur);
gdbstub_flush_caches = 1;
size = gdbstub_insn_sizes[cur];
if (size > 0) {
if (!__gdbstub_mark_bp(pc + size, 0))
goto fault;
} else {
switch (cur) {
/* Bxx (d8,PC) */
case 0xc0 ... 0xca:
if (gdbstub_read_byte(pc + 1, (u8 *) &x) < 0)
goto fault;
if (!__gdbstub_mark_bp(pc + 2, 0))
goto fault;
if ((x < 0 || x > 2) &&
!__gdbstub_mark_bp(pc + (s8) x, 1))
goto fault;
break;
/* LXX (d8,PC) */
case 0xd0 ... 0xda:
if (!__gdbstub_mark_bp(pc + 1, 0))
goto fault;
if (regs->pc != regs->lar &&
!__gdbstub_mark_bp((u8 *) regs->lar, 1))
goto fault;
break;
/* SETLB - loads the next for bytes into the LIR
* register */
case 0xdb:
if (!__gdbstub_mark_bp(pc + 1, 0))
goto fault;
break;
/* JMP (d16,PC) or CALL (d16,PC) */
case 0xcc:
case 0xcd:
if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0)
goto fault;
if (!__gdbstub_mark_bp(pc + (s16) x, 0))
goto fault;
break;
/* JMP (d32,PC) or CALL (d32,PC) */
case 0xdc:
case 0xdd:
if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0 ||
gdbstub_read_byte(pc + 3, ((u8 *) &x) + 2) < 0 ||
gdbstub_read_byte(pc + 4, ((u8 *) &x) + 3) < 0)
goto fault;
if (!__gdbstub_mark_bp(pc + (s32) x, 0))
goto fault;
break;
/* RETF */
case 0xde:
if (!__gdbstub_mark_bp((u8 *) regs->mdr, 0))
goto fault;
break;
/* RET */
case 0xdf:
if (gdbstub_read_byte(pc + 2, (u8 *) &x) < 0)
goto fault;
sp += (s8)x;
if (gdbstub_read_byte(sp + 0, ((u8 *) &x) + 0) < 0 ||
gdbstub_read_byte(sp + 1, ((u8 *) &x) + 1) < 0 ||
gdbstub_read_byte(sp + 2, ((u8 *) &x) + 2) < 0 ||
gdbstub_read_byte(sp + 3, ((u8 *) &x) + 3) < 0)
goto fault;
if (!__gdbstub_mark_bp((u8 *) x, 0))
goto fault;
break;
case 0xf0:
if (gdbstub_read_byte(pc + 1, &cur) < 0)
goto fault;
if (cur >= 0xf0 && cur <= 0xf7) {
/* JMP (An) / CALLS (An) */
switch (cur & 3) {
case 0: x = regs->a0; break;
case 1: x = regs->a1; break;
case 2: x = regs->a2; break;
case 3: x = regs->a3; break;
}
if (!__gdbstub_mark_bp((u8 *) x, 0))
goto fault;
} else if (cur == 0xfc) {
/* RETS */
if (gdbstub_read_byte(
sp + 0, ((u8 *) &x) + 0) < 0 ||
gdbstub_read_byte(
sp + 1, ((u8 *) &x) + 1) < 0 ||
gdbstub_read_byte(
sp + 2, ((u8 *) &x) + 2) < 0 ||
gdbstub_read_byte(
sp + 3, ((u8 *) &x) + 3) < 0)
goto fault;
if (!__gdbstub_mark_bp((u8 *) x, 0))
goto fault;
} else if (cur == 0xfd) {
/* RTI */
if (gdbstub_read_byte(
sp + 4, ((u8 *) &x) + 0) < 0 ||
gdbstub_read_byte(
sp + 5, ((u8 *) &x) + 1) < 0 ||
gdbstub_read_byte(
sp + 6, ((u8 *) &x) + 2) < 0 ||
gdbstub_read_byte(
sp + 7, ((u8 *) &x) + 3) < 0)
goto fault;
if (!__gdbstub_mark_bp((u8 *) x, 0))
goto fault;
} else {
if (!__gdbstub_mark_bp(pc + 2, 0))
goto fault;
}
break;
/* potential 3-byte conditional branches */
case 0xf8:
if (gdbstub_read_byte(pc + 1, &cur) < 0)
goto fault;
if (!__gdbstub_mark_bp(pc + 3, 0))
goto fault;
if (cur >= 0xe8 && cur <= 0xeb) {
if (gdbstub_read_byte(
pc + 2, ((u8 *) &x) + 0) < 0)
goto fault;
if ((x < 0 || x > 3) &&
!__gdbstub_mark_bp(pc + (s8) x, 1))
goto fault;
}
break;
case 0xfa:
if (gdbstub_read_byte(pc + 1, &cur) < 0)
goto fault;
if (cur == 0xff) {
/* CALLS (d16,PC) */
if (gdbstub_read_byte(
pc + 2, ((u8 *) &x) + 0) < 0 ||
gdbstub_read_byte(
pc + 3, ((u8 *) &x) + 1) < 0)
goto fault;
if (!__gdbstub_mark_bp(pc + (s16) x, 0))
goto fault;
} else {
if (!__gdbstub_mark_bp(pc + 4, 0))
goto fault;
}
break;
case 0xfc:
if (gdbstub_read_byte(pc + 1, &cur) < 0)
goto fault;
if (cur == 0xff) {
/* CALLS (d32,PC) */
if (gdbstub_read_byte(
pc + 2, ((u8 *) &x) + 0) < 0 ||
gdbstub_read_byte(
pc + 3, ((u8 *) &x) + 1) < 0 ||
gdbstub_read_byte(
pc + 4, ((u8 *) &x) + 2) < 0 ||
gdbstub_read_byte(
pc + 5, ((u8 *) &x) + 3) < 0)
goto fault;
if (!__gdbstub_mark_bp(
pc + (s32) x, 0))
goto fault;
} else {
if (!__gdbstub_mark_bp(
pc + 6, 0))
goto fault;
}
break;
}
}
gdbstub_bkpt("Step: %02x at %p; %02x at %p\n",
step_bp[0].opcode[0], step_bp[0].addr,
step_bp[1].opcode[0], step_bp[1].addr);
if (step_bp[0].addr) {
#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
if (gdbstub_write_byte(0xF7, step_bp[0].addr + 0) < 0 ||
gdbstub_write_byte(0xF7, step_bp[0].addr + 1) < 0)
goto fault;
#else
if (gdbstub_write_byte(0xFF, step_bp[0].addr + 0) < 0)
goto fault;
#endif
}
if (step_bp[1].addr) {
#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
if (gdbstub_write_byte(0xF7, step_bp[1].addr + 0) < 0 ||
gdbstub_write_byte(0xF7, step_bp[1].addr + 1) < 0)
goto fault;
#else
if (gdbstub_write_byte(0xFF, step_bp[1].addr + 0) < 0)
goto fault;
#endif
}
return 0;
fault:
/* uh-oh - silly address alert, try and restore things */
__gdbstub_restore_bp();
return -EFAULT;
}
#endif /* CONFIG_GDBSTUB_ALLOW_SINGLE_STEP */
#ifdef CONFIG_GDBSTUB_CONSOLE
void gdbstub_console_write(struct console *con, const char *p, unsigned n)
{
static const char gdbstub_cr[] = { 0x0d };
char outbuf[26];
int qty;
u8 busy;
busy = gdbstub_busy;
gdbstub_busy = 1;
outbuf[0] = 'O';
while (n > 0) {
qty = 1;
while (n > 0 && qty < 20) {
mem2hex(p, outbuf + qty, 2, 0);
qty += 2;
if (*p == 0x0a) {
mem2hex(gdbstub_cr, outbuf + qty, 2, 0);
qty += 2;
}
p++;
n--;
}
outbuf[qty] = 0;
putpacket(outbuf);
}
gdbstub_busy = busy;
}
static kdev_t gdbstub_console_dev(struct console *con)
{
return MKDEV(1, 3); /* /dev/null */
}
static struct console gdbstub_console = {
.name = "gdb",
.write = gdbstub_console_write,
.device = gdbstub_console_dev,
.flags = CON_PRINTBUFFER,
.index = -1,
};
#endif
/*
* Convert the memory pointed to by mem into hex, placing result in buf.
* - if successful, return a pointer to the last char put in buf (NUL)
* - in case of mem fault, return NULL
* may_fault is non-zero if we are reading from arbitrary memory, but is
* currently not used.
*/
static
unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
{
const u8 *mem = _mem;
u8 ch[4];
if ((u32) mem & 1 && count >= 1) {
if (gdbstub_read_byte(mem, ch) != 0)
return 0;
buf = hex_byte_pack(buf, ch[0]);
mem++;
count--;
}
if ((u32) mem & 3 && count >= 2) {
if (gdbstub_read_word(mem, ch) != 0)
return 0;
buf = hex_byte_pack(buf, ch[0]);
buf = hex_byte_pack(buf, ch[1]);
mem += 2;
count -= 2;
}
while (count >= 4) {
if (gdbstub_read_dword(mem, ch) != 0)
return 0;
buf = hex_byte_pack(buf, ch[0]);
buf = hex_byte_pack(buf, ch[1]);
buf = hex_byte_pack(buf, ch[2]);
buf = hex_byte_pack(buf, ch[3]);
mem += 4;
count -= 4;
}
if (count >= 2) {
if (gdbstub_read_word(mem, ch) != 0)
return 0;
buf = hex_byte_pack(buf, ch[0]);
buf = hex_byte_pack(buf, ch[1]);
mem += 2;
count -= 2;
}
if (count >= 1) {
if (gdbstub_read_byte(mem, ch) != 0)
return 0;
buf = hex_byte_pack(buf, ch[0]);
}
*buf = 0;
return buf;
}
/*
* convert the hex array pointed to by buf into binary to be placed in mem
* return a pointer to the character AFTER the last byte written
* may_fault is non-zero if we are reading from arbitrary memory, but is
* currently not used.
*/
static
const char *hex2mem(const char *buf, void *_mem, int count, int may_fault)
{
u8 *mem = _mem;
union {
u32 val;
u8 b[4];
} ch;
if ((u32) mem & 1 && count >= 1) {
ch.b[0] = hex(*buf++) << 4;
ch.b[0] |= hex(*buf++);
if (gdbstub_write_byte(ch.val, mem) != 0)
return 0;
mem++;
count--;
}
if ((u32) mem & 3 && count >= 2) {
ch.b[0] = hex(*buf++) << 4;
ch.b[0] |= hex(*buf++);
ch.b[1] = hex(*buf++) << 4;
ch.b[1] |= hex(*buf++);
if (gdbstub_write_word(ch.val, mem) != 0)
return 0;
mem += 2;
count -= 2;
}
while (count >= 4) {
ch.b[0] = hex(*buf++) << 4;
ch.b[0] |= hex(*buf++);
ch.b[1] = hex(*buf++) << 4;
ch.b[1] |= hex(*buf++);
ch.b[2] = hex(*buf++) << 4;
ch.b[2] |= hex(*buf++);
ch.b[3] = hex(*buf++) << 4;
ch.b[3] |= hex(*buf++);
if (gdbstub_write_dword(ch.val, mem) != 0)
return 0;
mem += 4;
count -= 4;
}
if (count >= 2) {
ch.b[0] = hex(*buf++) << 4;
ch.b[0] |= hex(*buf++);
ch.b[1] = hex(*buf++) << 4;
ch.b[1] |= hex(*buf++);
if (gdbstub_write_word(ch.val, mem) != 0)
return 0;
mem += 2;
count -= 2;
}
if (count >= 1) {
ch.b[0] = hex(*buf++) << 4;
ch.b[0] |= hex(*buf++);
if (gdbstub_write_byte(ch.val, mem) != 0)
return 0;
}
return buf;
}
/*
* This table contains the mapping between MN10300 exception codes, and
* signals, which are primarily what GDB understands. It also indicates
* which hardware traps we need to commandeer when initializing the stub.
*/
static const struct excep_to_sig_map {
enum exception_code excep; /* MN10300 exception code */
unsigned char signo; /* Signal that we map this into */
} excep_to_sig_map[] = {
{ EXCEP_ITLBMISS, SIGSEGV },
{ EXCEP_DTLBMISS, SIGSEGV },
{ EXCEP_TRAP, SIGTRAP },
{ EXCEP_ISTEP, SIGTRAP },
{ EXCEP_IBREAK, SIGTRAP },
{ EXCEP_OBREAK, SIGTRAP },
{ EXCEP_UNIMPINS, SIGILL },
{ EXCEP_UNIMPEXINS, SIGILL },
{ EXCEP_MEMERR, SIGSEGV },
{ EXCEP_MISALIGN, SIGSEGV },
{ EXCEP_BUSERROR, SIGBUS },
{ EXCEP_ILLINSACC, SIGSEGV },
{ EXCEP_ILLDATACC, SIGSEGV },
{ EXCEP_IOINSACC, SIGSEGV },
{ EXCEP_PRIVINSACC, SIGSEGV },
{ EXCEP_PRIVDATACC, SIGSEGV },
{ EXCEP_FPU_DISABLED, SIGFPE },
{ EXCEP_FPU_UNIMPINS, SIGFPE },
{ EXCEP_FPU_OPERATION, SIGFPE },
{ EXCEP_WDT, SIGALRM },
{ EXCEP_NMI, SIGQUIT },
{ EXCEP_IRQ_LEVEL0, SIGINT },
{ EXCEP_IRQ_LEVEL1, SIGINT },
{ EXCEP_IRQ_LEVEL2, SIGINT },
{ EXCEP_IRQ_LEVEL3, SIGINT },
{ EXCEP_IRQ_LEVEL4, SIGINT },
{ EXCEP_IRQ_LEVEL5, SIGINT },
{ EXCEP_IRQ_LEVEL6, SIGINT },
{ 0, 0}
};
/*
* convert the MN10300 exception code into a UNIX signal number
*/
static int computeSignal(enum exception_code excep)
{
const struct excep_to_sig_map *map;
for (map = excep_to_sig_map; map->signo; map++)
if (map->excep == excep)
return map->signo;
return SIGHUP; /* default for things we don't know about */
}
static u32 gdbstub_fpcr, gdbstub_fpufs_array[32];
/*
*
*/
static void gdbstub_store_fpu(void)
{
#ifdef CONFIG_FPU
asm volatile(
"or %2,epsw\n"
#ifdef CONFIG_MN10300_PROC_MN103E010
"nop\n"
"nop\n"
#endif
"mov %1, a1\n"
"fmov fs0, (a1+)\n"
"fmov fs1, (a1+)\n"
"fmov fs2, (a1+)\n"
"fmov fs3, (a1+)\n"
"fmov fs4, (a1+)\n"
"fmov fs5, (a1+)\n"
"fmov fs6, (a1+)\n"
"fmov fs7, (a1+)\n"
"fmov fs8, (a1+)\n"
"fmov fs9, (a1+)\n"
"fmov fs10, (a1+)\n"
"fmov fs11, (a1+)\n"
"fmov fs12, (a1+)\n"
"fmov fs13, (a1+)\n"
"fmov fs14, (a1+)\n"
"fmov fs15, (a1+)\n"