forked from murachue/gdbstub-ed64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdbstub.c
904 lines (782 loc) · 19.7 KB
/
gdbstub.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
#include <stdint.h>
#include "gdbstub.h"
/* cache */
/*
* note: N64 specs:
* I-Cache: 32bytes/line, 16KiB
* D-Cache: 16bytes/line, 8KiB
*/
#define do_cache(op, cache, _linesize, _start, _len) \
do { \
if((_len) != 0) { \
const uintptr_t xlinesize = (_linesize); \
const uintptr_t xstart = (uintptr_t)(_start); \
const uintptr_t xlen = (_len); \
const uintptr_t xend = (xstart + (xlen - 1)) | (xlinesize - 1); \
uintptr_t xline; \
for(xline = xstart; xline <= xend; xline += xlinesize) { /* TODO specifying end of virtual memory cause inf-loop!! */ \
__asm("cache %0, %1" : : "i"(((op)<<2)|(cache)), "m"(*(uint8_t*)xline)); \
} \
} \
} while(0)
void dwbinvalall() {
do_cache(0, 1, 16, (intptr_t)(int32_t)0x80000000, 8 * 1024); /* 0=IndexWBInvd */
}
void dwbinval(const void *ptr, uintptr_t len) {
/* if len is over d-cache size, assume whole is cached, flush all d-cache. */
if(8192 <= len) {
dwbinvalall();
return;
}
do_cache(5, 1, 16, ptr, len); /* 5=HitWBInvd */
}
void dinval(const void *ptr, uintptr_t len) {
/* if len is over d-cache size, assume whole is cached, flush all d-cache. */
if(8192 <= len) {
dwbinvalall(); /* note: there is no DIndexInvd (think it... too dangerous!!), do WBInvd instead (that is safe and intended). */
return;
}
do_cache(4, 1, 16, ptr, len); /* 4=HitInvd */
}
void iinvalall() {
do_cache(0, 0, 32, (intptr_t)(int32_t)0x80000000, 16 * 1024); /* 0=Index(WB)Invd */
}
void iinval(const void *ptr, uintptr_t len) {
/* if len is over i-cache size, assume whole is cached, flush all i-cache. */
if(16384 <= len) {
iinvalall();
return;
}
do_cache(4, 0, 32, ptr, len); /* 4=HitInvd */
}
/* N64/ED64v3 hardware regs that only used by this gdbstub */
struct {
uint32_t dramaddr;
uint32_t cartaddr;
uint32_t dram2cart; /* read (to PI) */
uint32_t cart2dram; /* write (from PI) */
uint32_t status;
uint32_t dom1lat; /* latency */
uint32_t dom1pwd; /* pulse width */
uint32_t dom1pgs; /* page size */
uint32_t dom1rls; /* release */
uint32_t dom2lat; /* latency */
uint32_t dom2pwd; /* pulse width */
uint32_t dom2pgs; /* page size */
uint32_t dom2rls; /* release */
} static volatile * const __attribute((unused)) PI = (void*)P32(0xA4600000);
enum PI_STATUS_READ {
PI_STATUS_DMABUSY = 1 << 0,
PI_STATUS_IOBUSY = 1 << 1,
PI_STATUS_ERROR = 1 << 2,
};
enum PI_STATUS_WRITE {
PI_STATUS_RESET = 1 << 0,
PI_STATUS_CLEARINTR = 1 << 1,
};
struct {
uint32_t cfg;
uint32_t status;
uint32_t dmalen;
uint32_t dmaaddr;
uint32_t msg;
uint32_t dmacfg;
uint32_t spi;
uint32_t spicfg;
uint32_t key;
uint32_t savcfg;
uint32_t sec;
uint32_t ver;
} static volatile * const ED64 = (void*)P32(0xA8040000);
enum ED64_CFG {
ED64_CFG_SDRAM_ON = 1 << 0,
ED64_CFG_SWAP = 1 << 1,
ED64_CFG_WR_MOD = 1 << 2,
ED64_CFG_WR_ADDR_MASK = 1 << 3,
};
enum ED64_STATUS {
ED64_STATUS_DMABUSY = 1 << 0,
ED64_STATUS_DMATIMEOUT = 1 << 1,
ED64_STATUS_TXE = 1 << 2,
ED64_STATUS_RXF = 1 << 3,
ED64_STATUS_SPI = 1 << 4,
};
enum ED64_DMACFG {
ED64_DMACFG_SD_TO_RAM = 1,
ED64_DMACFG_RAM_TO_SD = 2,
ED64_DMACFG_FIFO_TO_RAM = 3,
ED64_DMACFG_RAM_TO_FIFO = 4,
};
/* gdbstub */
enum {
STUBERR_UNKNOWN = 1,
STUBERR_GRTOOSHORT,
STUBERR_SRINV1,
STUBERR_SRINV2,
STUBERR_GMADDRSHORT,
STUBERR_GMADDRINV,
STUBERR_GMLENINV,
STUBERR_GMSEGV,
STUBERR_SMADDRSHORT,
STUBERR_SMADDRINV,
STUBERR_SMLENSHORT,
STUBERR_SMLENINV,
STUBERR_SMBYTESINV,
STUBERR_SMSEGV,
STUBERR_CADDRNOTIMPL,
STUBERR_STPCSEGV,
STUBERR_STJRSEGV,
STUBERR_STFNSEGV,
STUBERR_STTNSEGV,
};
/* 0~ GPR0-31(yes, include zero),[32]PS(status),LO,HI,BadVAddr,Cause,PC,[38]FPR0-31,[70]fpcs,fpir,[72]..(dsp?),[90]end */
/*static const uint64_t *regs = (void*)((32+6+32+2) * -8);*/
static uint8_t __attribute((aligned(8))) cartrxbuf[2048];
static uint8_t __attribute((aligned(8))) carttxbuf[2048];
static uint32_t origintcodes[4][2];
static struct {
intptr_t addr; /* -1 = unset */
uint32_t value;
} origbpcodes[2];
static void ed_enableregs(int en) {
ED64->cfg;
ED64->key = en ? 0x1234 : 0; /* Enable/Disable ED64 regs */
}
static uint32_t ed_waitdma(void) {
uint32_t last;
for(;;) {
ED64->cfg;
last = ED64->status;
if(!(last & ED64_STATUS_DMABUSY)) {
break;
}
}
return last;
}
static void dram2cart(const void *dramptr, uint32_t cartaddr, uint32_t len) {
PI->status = PI_STATUS_RESET | PI_STATUS_CLEARINTR;
PI->dramaddr = (uint32_t)(uintptr_t)dramptr & 0x00FFffff;
PI->cartaddr = 0x10000000 + cartaddr;
PI->dram2cart = len - 1; /* 64bits unit */
while(PI->status & PI_STATUS_DMABUSY) /*nothing*/;
}
static uint32_t cart2fifo(uint32_t cartaddr, uint32_t len) {
for(;;) {
ED64->cfg;
if(!(ED64->status & ED64_STATUS_TXE)) { break; }
}
ED64->cfg;
ED64->dmalen = len / 512 - 1;
ED64->cfg;
ED64->dmaaddr = cartaddr / 2048;
ED64->cfg;
ED64->dmacfg = ED64_DMACFG_RAM_TO_FIFO;
return ed_waitdma();
}
static uint32_t dram2fifo(const void *buf, uint32_t len) {
const uint32_t cartaddr = 0x04000000 - ((len + 2047) & -2048);
dwbinval(buf, len); /* TODO inval? only wb cause false write again but faster re-r/w. use case? */
dram2cart(buf, cartaddr, len);
return cart2fifo(cartaddr, len);
}
static uint32_t fifo2cart(uint32_t cartaddr, uint32_t len) {
ED64->cfg;
ED64->dmalen = len / 512 - 1;
ED64->cfg;
ED64->dmaaddr = cartaddr / 2048;
ED64->cfg;
ED64->dmacfg = ED64_DMACFG_FIFO_TO_RAM;
return ed_waitdma();
}
static void cart2dram(void *dramptr, uint32_t cartaddr, uint32_t len) {
PI->status = PI_STATUS_RESET | PI_STATUS_CLEARINTR;
PI->dramaddr = (uint32_t)(uintptr_t)dramptr & 0x00FFffff;
PI->cartaddr = 0x10000000 + cartaddr;
PI->cart2dram = len - 1;
while(PI->status & PI_STATUS_DMABUSY) /*nothing*/;
}
static uint32_t fifo2dram(void* buf, uintptr_t maxlen) {
/* note: I want to fifo2cart many time and then one cart2dram, */
/* but ED64's len=512/addr=2048(!) align does not allow it... */
const uint32_t cartaddr = 0x04000000 - 2048;
uintptr_t len = 0;
/* not dwbinval, discard to-be-write (that is not necessary data... overwritten soon.) */
dinval(buf, maxlen);
while(len < maxlen) {
if(0 < len) {
ED64->cfg;
if(ED64->status & ED64_STATUS_RXF) { /* no more data (RXF#) */
break;
}
}
fifo2cart(cartaddr, 512);
if(ED64->status & ED64_STATUS_DMATIMEOUT) { /* timeout */
break;
}
cart2dram(buf, cartaddr, 512);
buf = (char*)buf + 512;
len += 512;
}
return len;
}
static void install_handler(void *pfn) {
uint32_t i, j;
uint32_t stubintcode[2];
stubintcode[0] = 0x08000000 | (((uint32_t)(uintptr_t)pfn >> 2) & 0x03FFffff); /* j *pfn */
stubintcode[1] = 0; /* branch-delay-slot: nop */
for(i = 0; i < 2; i++) {
for(j = 0; j < 4; j++) {
*(uint32_t*)P32(0x80000000 + j * 0x80 + i * 4) = stubintcode[i];
}
}
dwbinval((void*)P32(0x80000000), 0x200);
iinval((void*)P32(0x80000000), 0x200);
}
static void install_switch(void) {
extern void stub_switch(void);
install_handler(stub_switch);
}
static void install_recover(void) {
extern void stub_recover(void);
install_handler(stub_recover);
}
void stub_install(void) {
uint32_t i, j;
extern void stub_installtlb(void);
stub_installtlb();
for(i = 0; i < sizeof(origbpcodes)/sizeof(*origbpcodes); i++) {
origbpcodes[i].addr = -1;
}
for(i = 0; i < 2; i++) {
for(j = 0; j < 4; j++) {
origintcodes[j][i] = *(uint32_t*)P32(0x80000000 + j * 0x80 + i * 4);
}
}
install_switch();
}
void stub_uninstall(void) {
uint32_t i, j;
for(i = 0; i < 2; i++) {
for(j = 0; j < 4; j++) {
*(uint32_t*)P32(0x80000000 + j * 0x80 + i * 4) = origintcodes[j][i];
}
}
dwbinval((void*)P32(0x80000000), 0x200);
iinval((void*)P32(0x80000000), 0x200);
}
/* sample program entry */
void stub(void) {
/* make TLB initialized; TODO detect TLB initialized and skip... possible?? */
extern void stub_tlbunmapall(void);
stub_tlbunmapall();
stub_install();
__asm("syscall"); /* invoke stub by triggering (safe&unique) exception */
stub_uninstall();
}
static int32_t hex2int(uint8_t c) {
if('0' <= c && c <= '9') { return c - '0'; }
if('A' <= c && c <= 'F') { return c - 'A' + 10; }
if('a' <= c && c <= 'f') { return c - 'a' + 10; }
return -1;
}
uint32_t stub_recovered;
static intptr_t int2hex(uint8_t *buf, uintptr_t bufsize, intptr_t index, uintptr_t value, uintptr_t nibbles) {
uintptr_t lpos;
static const char i2h[16] = "0123456789ABCDEF";
for(lpos = nibbles; 0 < lpos; lpos--) {
if(bufsize <= index) return -1; /* overflow! */
buf[index] = i2h[(value >> (4 * (lpos - 1))) & 15];
index++;
}
return index;
}
static intptr_t tohex(uint8_t *buf, uintptr_t bufsize, intptr_t index, const void *src_, uintptr_t srclen) {
uintptr_t i = 0;
stub_recovered = 0;
if((((uintptr_t)buf & 3) == 0) && ((srclen & 3) == 0)) {
/* word access */
const uint32_t *src = src_;
for(; i < srclen / 4; i++) {
uint32_t s = src[i];
if(stub_recovered) {
return -1;
}
if((index = int2hex(buf, bufsize, index, s, 8)) < 0) {
return -1; /* overflow! */
}
}
} else if((((uintptr_t)buf & 1) == 0) && ((srclen & 1) == 0)) {
/* halfword access */
const uint16_t *src = src_;
for(; i < srclen / 2; i++) {
uint16_t s = src[i];
if(stub_recovered) {
return -1;
}
if((index = int2hex(buf, bufsize, index, s, 4)) < 0) {
return -1; /* overflow! */
}
}
} else {
/* byte access */
const uint8_t *src = src_;
for(; i < srclen; i++) {
uint8_t s = src[i];
if(stub_recovered) {
return -1;
}
if((index = int2hex(buf, bufsize, index, s, 2)) < 0) {
return -1; /* overflow! */
}
}
}
return index;
}
static void sendraw(uint8_t *buf, uintptr_t bufsize, uintptr_t len) {
/* zerofill tail */
for(; len < bufsize; len++) {
buf[len] = 0;
}
/* go!! */
dram2fifo(buf, bufsize);
}
static int sendpkt(uint8_t *buf, intptr_t bufsize) {
intptr_t si, ei;
if(bufsize < 4) {
/* no minimum space is available. illegal function call!! */
return -1;
}
for(si = 0; si < bufsize - 3; si++) {
if(buf[si] == '$') {
break;
}
}
if(si == bufsize - 3) {
/* no start marker found */
return -2;
}
si++;
/* find end marker */
for(ei = si; ei < bufsize - 2; ei++) {
if(buf[ei] == '#') {
break;
}
}
if(ei == bufsize - 2) {
/* no end marker found */
return -3;
}
/* calculate and fill checksum */
{
uintptr_t sum = 0;
{
uintptr_t i;
for(i = si; i < ei; i++) {
sum += buf[i];
}
}
{
uint8_t sumb[1] = {sum};
if(tohex(buf, bufsize, ei + 1, sumb, 1) < 0) {
/* should not be happened! */
return -4;
}
}
}
ei += 1 + 2;
/* go!! */
{
uintptr_t ssize = (ei + (512 - 1)) & -512;
sendraw(buf, ssize, ei);
}
return 0;
}
static void die(uint16_t color) {
#if 0
extern uint16_t vram[];
for(uintptr_t i = 0; i < 320*240; i++) {
vram[i] = color;
}
#endif
dwbinvalall();
for(;;) /*nothing*/ ;
}
/* g -> <regbytes> */
static uint8_t cmd_getregs(uint8_t *buf, uintptr_t starti, uintptr_t endi) {
carttxbuf[0] = '+';
carttxbuf[1] = '$';
{
intptr_t ei = tohex(carttxbuf, sizeof(carttxbuf), 2, (void*)P32(REGS_START), REGS_END);
if(ei < 0) {
return STUBERR_GRTOOSHORT;
}
carttxbuf[ei] = '#';
}
if(sendpkt(carttxbuf, sizeof(carttxbuf))) die(0x07C0);
return 0;
}
/* G<regbytes> -> OK|Exx */
static uint8_t cmd_setregs(uint8_t *buf, uintptr_t starti, uintptr_t endi) {
uint8_t *p = (void*)P32(REGS_START);
uintptr_t len = (endi - starti - 1) / 2;
uintptr_t i;
if(REGS_END < len) {
len = REGS_END;
}
for(i = starti + 1; i < starti + 1 + len * 2; i++) {
uintptr_t b = 0;
int32_t nib;
nib = hex2int(buf[i++]);
if(nib < 0) {
return STUBERR_SRINV1;
}
b = nib << 4;
nib = hex2int(buf[i]);
if(nib < 0) {
return STUBERR_SRINV2;
}
b |= nib;
*p++ = b;
}
carttxbuf[0] = '+';
carttxbuf[1] = '$';
carttxbuf[2] = 'O';
carttxbuf[3] = 'K';
carttxbuf[4] = '#';
if(sendpkt(carttxbuf, sizeof(carttxbuf))) die(0x07C0);
return 0;
}
/* m<ADDR>,<LEN> -> <membytes>|Exx */
static uint8_t cmd_getmemory(uint8_t *buf, uintptr_t starti, uintptr_t endi) {
uintptr_t i = starti + 1;
uintptr_t addr = 0, len = 0;
for(; buf[i] != ','; i++) {
if(endi <= i) {
return STUBERR_GMADDRSHORT;
}
{
int32_t nib = hex2int(buf[i]);
if(nib < 0) {
return STUBERR_GMADDRINV;
}
addr = (addr << 4) | nib;
}
}
i++; /* skip ',' */
for(; buf[i] != '#'; i++) {
int32_t nib = hex2int(buf[i]);
if(nib < 0) {
return STUBERR_GMLENINV;
}
len = (len << 4) | nib;
}
if(sizeof(carttxbuf) - 5 < len / 2) {
len = (sizeof(carttxbuf) - 5) / 2;
}
stub_recovered = 0;
carttxbuf[0] = '+';
carttxbuf[1] = '$';
if(tohex(carttxbuf, sizeof(carttxbuf), 2, (void*)addr, len) < 0) {
return STUBERR_GMSEGV;
}
carttxbuf[2 + len * 2] = '#';
if(stub_recovered) {
return 6;
}
if(sendpkt(carttxbuf, sizeof(carttxbuf))) die(0x07C0);
return 0;
}
/* M<ADDR>,<LEN>:<BYTES> -> OK|Exx */
static uint8_t cmd_setmemory(uint8_t *buf, uintptr_t starti, uintptr_t endi) {
uintptr_t i = starti + 1;
uintptr_t addr = 0, len = 0;
for(; buf[i] != ','; i++) {
if(endi <= i) {
return STUBERR_SMADDRSHORT;
}
{
int32_t nib = hex2int(buf[i]);
if(nib < 0) {
return STUBERR_SMADDRINV;
}
addr = (addr << 4) | nib;
}
}
i++; /* skip ',' */
for(; buf[i] != ':'; i++) {
if(endi <= i) {
return STUBERR_SMLENSHORT;
}
{
int32_t nib = hex2int(buf[i]);
if(nib < 0) {
return STUBERR_SMLENINV;
}
len = (len << 4) | nib;
}
}
i++; /* skip ':' */
{
/* remember addr,len at entry for flushing cache */
const uintptr_t start_addr = addr, tobe_len = len;
uintptr_t width;
uint32_t by, phase;
if(((addr & 3) == 0) && ((len & 3) == 0)) {
width = 4;
} else if(((addr & 1) == 0) && ((len & 1) == 0)) {
width = 2;
} else {
width = 1;
}
stub_recovered = 0;
for(by = 0, phase = 0; len; i++) {
int32_t nib;
if(buf[i] == '#') {
break;
}
nib = hex2int(buf[i]);
if(nib < 0) {
return STUBERR_SMBYTESINV;
}
by = (by << 4) | nib;
switch(width) {
case 1:
phase = (phase + 1) % 2;
if(phase == 0) {
*(uint8_t*)addr = by;
}
break;
case 2:
phase = (phase + 1) % 4;
if(phase == 0) {
*(uint16_t*)addr = by;
}
break;
case 4:
phase = (phase + 1) % 8;
if(phase == 0) {
*(uint32_t*)addr = by;
}
break;
}
if(phase == 0) {
if(stub_recovered) {
return STUBERR_SMSEGV;
}
addr += width;
len -= width;
}
}
/* to recognize new insn, ex. "break" */
dwbinval((void*)start_addr, tobe_len);
iinval((void*)start_addr, tobe_len);
carttxbuf[0] = '+';
carttxbuf[1] = '$';
carttxbuf[2] = 'O';
carttxbuf[3] = 'K';
carttxbuf[4] = '#';
if(sendpkt(carttxbuf, sizeof(carttxbuf))) die(0x07C0);
return 0;
}
}
static void bpset(uint32_t index, uintptr_t addr, uint32_t value) {
origbpcodes[index].addr = addr;
origbpcodes[index].value = *(uint32_t*)addr;
*(uint32_t*)addr = value;
dwbinval((void*)addr, 4);
iinval((void*)addr, 4);
}
/* s -> (stop_reply) */
/* ref: Linux 2.6.25:arch/mips/kernel/gdb-stub.c */
static uint8_t cmd_step(uint8_t *buf, uintptr_t starti, uintptr_t endi) {
intptr_t pc;
uint32_t insn;
intptr_t tnext, fnext;
stub_recovered = 0;
pc = *(int64_t*)(P32(REGS_START) + REGS_PC);
insn = *(uint32_t*)pc;
if(stub_recovered) {
return STUBERR_STPCSEGV;
}
tnext = pc + 4;
fnext = -1;
if((insn & 0xF8000000) == 0x08000000) { /* J/JAL */
tnext = (tnext & 0xFFFFffffF0000000) | ((insn & 0x03FFffff) << 2);
} else if(0
|| ((insn & 0xFC0C0000) == 0x04000000) /* B{LT/GE}Z[AL][L] */
|| ((insn & 0xF0000000) == 0x10000000) /* B{EQ/NE/LE/GT} */
|| ((insn & 0xF3E00000) == 0x41000000) /* BCz{T/F}[L] */
|| ((insn & 0xF0000000) == 0x50000000) /* B{EQ/NE/LE/GT}L */
) {
fnext = tnext + 4; /* next of branch-delay */
tnext = tnext + ((intptr_t)(int16_t)insn << 2);
} else if((insn & 0xFC00003E) == 0x00000008) { /* JR/JALR */
uint32_t reg = (insn >> 21) & 0x1F;
tnext = *(int64_t*)(P32(REGS_START) + REGS_GPR + (reg * 8));
if(stub_recovered) {
return STUBERR_STJRSEGV;
}
}
if(fnext != -1) {
bpset(1, fnext, 0x0000000D);
if(stub_recovered) {
return STUBERR_STFNSEGV;
}
}
bpset(0, tnext, 0x0000000D);
if(stub_recovered) {
return STUBERR_STTNSEGV;
}
return 0;
}
/* interrupt entry */
void stub_entry(void) {
uint32_t i;
install_recover();
/* bprestore */
for(i = 0; i < sizeof(origbpcodes)/sizeof(*origbpcodes); i++) {
if(origbpcodes[i].addr != -1) {
uint32_t *addr = (uint32_t*)origbpcodes[i].addr;
*addr = origbpcodes[i].value;
dwbinval(addr, 4);
iinval(addr, 4);
origbpcodes[i].addr = -1;
}
}
/* move enableregs out of infloop for debugging ed64 with gdb. */
ed_enableregs(1);
{
carttxbuf[0] = '$';
carttxbuf[1] = 'S';
carttxbuf[2] = '0';
carttxbuf[3] = '5';
carttxbuf[4] = '#';
if(sendpkt(carttxbuf, sizeof(carttxbuf))) die(0xFFC0);
}
for(;;) {
uintptr_t len;
uintptr_t i;
/*ed_enableregs(1);*/
/* wait for arriving data */
for(;;) {
ED64->cfg;
if(!(ED64->status & ED64_STATUS_RXF)) {
break;
}
}
len = fifo2dram(cartrxbuf, sizeof(cartrxbuf));
/*ed_enableregs(0);*/
for(i = 0; i < len; i++) {
uintptr_t starti, endi;
uint32_t realsum;
uint8_t error;
if(cartrxbuf[i] != '$') {
/* not start of GDB remote packet */
/* TODO ignoreing '+' is ok, but '-' is not!! */
continue;
}
/* found packet head */
i++; if(len <= i) goto pkterr;
starti = i;
realsum = 0;
for(;;) {
if(cartrxbuf[i] == '#') {
break;
}
realsum += cartrxbuf[i];
i++; if(len <= i) goto pkterr;
}
endi = i;
/* skip '#' */
i++; if(len <= i) goto pkterr;
{
int32_t expectsum;
/* read expect cksum */
expectsum = hex2int(cartrxbuf[i]) << 4;
i++; if(len <= i) goto pkterr;
expectsum |= hex2int(cartrxbuf[i]);
/* error if checksum mismatch (inclding malformed expect cksum) */
if((realsum & 0xFF) != expectsum) {
goto pkterr;
}
}
/* here, we can assume whole command fits in rxbuf. */
/* process command */
error = 0;
switch(cartrxbuf[starti]) {
case 'g': /* register target2host */
error = cmd_getregs(cartrxbuf, starti, endi);
break;
case 'G': /* register host2target */
error = cmd_setregs(cartrxbuf, starti, endi);
break;
case 'm': /* memory target2host */
error = cmd_getmemory(cartrxbuf, starti, endi);
break;
case 'M': /* memory host2target */
error = cmd_setmemory(cartrxbuf, starti, endi);
break;
case '?': /* last signal */
{
carttxbuf[0] = '+';
carttxbuf[1] = '$';
carttxbuf[2] = 'S';
carttxbuf[3] = '0';
carttxbuf[4] = '5';
carttxbuf[5] = '#';
if(sendpkt(carttxbuf, sizeof(carttxbuf))) die(0xFFC0);
}
break;
case 's': /* step */
{
error = cmd_step(cartrxbuf, starti, endi);
if(error == 0) {
carttxbuf[0] = '+';
sendraw(carttxbuf, 512, 1);
goto bye;
}
}
break;
case 'c': /* continue */
/* c[ADDR] */
{
if(cartrxbuf[starti + 1] != '#') {
/* argument not supported */
error = STUBERR_CADDRNOTIMPL;
break;
}
/* nothing to do */
if(error == 0) {
carttxbuf[0] = '+';
sendraw(carttxbuf, 512, 1);
goto bye;
}
}
break;
default:
/* unknown command... */
carttxbuf[0] = '+';
carttxbuf[1] = '$';
carttxbuf[2] = '#';
if(sendpkt(carttxbuf, sizeof(carttxbuf))) die(0xFFC0);
}
if(error != 0) {
carttxbuf[0] = '+';
carttxbuf[1] = '$';
carttxbuf[2] = 'E';
if(tohex(carttxbuf, sizeof(carttxbuf), 3, &error, 1) < 0) die(0xFC00);
carttxbuf[5] = '#';
if(sendpkt(carttxbuf, sizeof(carttxbuf))) die(0xFFC0);
}
continue;
pkterr:
/* return NAK */
carttxbuf[0] = '-'; /* NAK */
sendraw(carttxbuf, 512, 1);
continue; /* if not invalid sum, should be break. if it is, should be continue. */
}
}
bye:
ed_enableregs(0);
install_switch();
}