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#include <linux/string.h>
124#include <linux/kernel.h>
125#include <linux/signal.h>
126#include <linux/sched.h>
127#include <linux/mm.h>
128#include <linux/console.h>
129#include <linux/init.h>
130#include <linux/bug.h>
131
132#include <asm/pgtable.h>
133#include <asm/system.h>
134#include <asm/gdb-stub.h>
135#include <asm/exceptions.h>
136#include <asm/debugger.h>
137#include <asm/serial-regs.h>
138#include <asm/busctl-regs.h>
139#include <unit/leds.h>
140#include <unit/serial.h>
141
142
143#undef GDBSTUB_USE_F7F7_AS_BREAKPOINT
144
145
146
147
148
149#define BUFMAX 2048
150
151static const char gdbstub_banner[] =
152 "Linux/MN10300 GDB Stub (c) RedHat 2007\n";
153
154u8 gdbstub_rx_buffer[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
155u32 gdbstub_rx_inp;
156u32 gdbstub_rx_outp;
157u8 gdbstub_busy;
158u8 gdbstub_rx_overflow;
159u8 gdbstub_rx_unget;
160
161static u8 gdbstub_flush_caches;
162static char input_buffer[BUFMAX];
163static char output_buffer[BUFMAX];
164static char trans_buffer[BUFMAX];
165
166struct gdbstub_bkpt {
167 u8 *addr;
168 u8 len;
169 u8 origbytes[7];
170};
171
172static struct gdbstub_bkpt gdbstub_bkpts[256];
173
174
175
176
177static void getpacket(char *buffer);
178static int putpacket(char *buffer);
179static int computeSignal(enum exception_code excep);
180static int hex(unsigned char ch);
181static int hexToInt(char **ptr, int *intValue);
182static unsigned char *mem2hex(const void *mem, char *buf, int count,
183 int may_fault);
184static const char *hex2mem(const char *buf, void *_mem, int count,
185 int may_fault);
186
187
188
189
190static int hex(unsigned char ch)
191{
192 if (ch >= 'a' && ch <= 'f')
193 return ch - 'a' + 10;
194 if (ch >= '0' && ch <= '9')
195 return ch - '0';
196 if (ch >= 'A' && ch <= 'F')
197 return ch - 'A' + 10;
198 return -1;
199}
200
201#ifdef CONFIG_GDBSTUB_DEBUGGING
202
203void debug_to_serial(const char *p, int n)
204{
205 __debug_to_serial(p, n);
206
207}
208
209void gdbstub_printk(const char *fmt, ...)
210{
211 va_list args;
212 int len;
213
214
215 va_start(args, fmt);
216 len = vsnprintf(trans_buffer, sizeof(trans_buffer), fmt, args);
217 va_end(args);
218 debug_to_serial(trans_buffer, len);
219}
220
221#endif
222
223static inline char *gdbstub_strcpy(char *dst, const char *src)
224{
225 int loop = 0;
226 while ((dst[loop] = src[loop]))
227 loop++;
228 return dst;
229}
230
231
232
233
234static void getpacket(char *buffer)
235{
236 unsigned char checksum;
237 unsigned char xmitcsum;
238 unsigned char ch;
239 int count, i, ret, error;
240
241 for (;;) {
242
243
244
245
246 do {
247 gdbstub_io_rx_char(&ch, 0);
248 } while (ch != '$');
249
250 checksum = 0;
251 xmitcsum = -1;
252 count = 0;
253 error = 0;
254
255
256
257
258 while (count < BUFMAX) {
259 ret = gdbstub_io_rx_char(&ch, 0);
260 if (ret < 0)
261 error = ret;
262
263 if (ch == '#')
264 break;
265 checksum += ch;
266 buffer[count] = ch;
267 count++;
268 }
269
270 if (error == -EIO) {
271 gdbstub_proto("### GDB Rx Error - Skipping packet"
272 " ###\n");
273 gdbstub_proto("### GDB Tx NAK\n");
274 gdbstub_io_tx_char('-');
275 continue;
276 }
277
278 if (count >= BUFMAX || error)
279 continue;
280
281 buffer[count] = 0;
282
283
284 ret = gdbstub_io_rx_char(&ch, 0);
285 if (ret < 0)
286 error = ret;
287 xmitcsum = hex(ch) << 4;
288
289 ret = gdbstub_io_rx_char(&ch, 0);
290 if (ret < 0)
291 error = ret;
292 xmitcsum |= hex(ch);
293
294 if (error) {
295 if (error == -EIO)
296 gdbstub_io("### GDB Rx Error -"
297 " Skipping packet\n");
298 gdbstub_io("### GDB Tx NAK\n");
299 gdbstub_io_tx_char('-');
300 continue;
301 }
302
303
304 if (checksum != xmitcsum) {
305 gdbstub_io("### GDB Tx NAK\n");
306 gdbstub_io_tx_char('-');
307 continue;
308 }
309
310 gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
311 gdbstub_io("### GDB Tx ACK\n");
312 gdbstub_io_tx_char('+');
313
314
315
316
317
318 if (buffer[2] == ':') {
319 gdbstub_io_tx_char(buffer[0]);
320 gdbstub_io_tx_char(buffer[1]);
321
322
323
324
325 count = 0;
326 while (buffer[count])
327 count++;
328 for (i = 3; i <= count; i++)
329 buffer[i - 3] = buffer[i];
330 }
331
332 break;
333 }
334}
335
336
337
338
339
340
341static int putpacket(char *buffer)
342{
343 unsigned char checksum;
344 unsigned char ch;
345 int count;
346
347
348
349
350 gdbstub_proto("### GDB Tx $'%s'#?? ###\n", buffer);
351
352 do {
353 gdbstub_io_tx_char('$');
354 checksum = 0;
355 count = 0;
356
357 while ((ch = buffer[count]) != 0) {
358 gdbstub_io_tx_char(ch);
359 checksum += ch;
360 count += 1;
361 }
362
363 gdbstub_io_tx_char('#');
364 gdbstub_io_tx_char(hex_asc_hi(checksum));
365 gdbstub_io_tx_char(hex_asc_lo(checksum));
366
367 } while (gdbstub_io_rx_char(&ch, 0),
368 ch == '-' && (gdbstub_io("### GDB Rx NAK\n"), 0),
369 ch != '-' && ch != '+' &&
370 (gdbstub_io("### GDB Rx ??? %02x\n", ch), 0),
371 ch != '+' && ch != '$');
372
373 if (ch == '+') {
374 gdbstub_io("### GDB Rx ACK\n");
375 return 0;
376 }
377
378 gdbstub_io("### GDB Tx Abandoned\n");
379 gdbstub_rx_unget = ch;
380 return 1;
381}
382
383
384
385
386
387static int hexToInt(char **ptr, int *intValue)
388{
389 int numChars = 0;
390 int hexValue;
391
392 *intValue = 0;
393
394 while (**ptr) {
395 hexValue = hex(**ptr);
396 if (hexValue < 0)
397 break;
398
399 *intValue = (*intValue << 4) | hexValue;
400 numChars++;
401
402 (*ptr)++;
403 }
404
405 return (numChars);
406}
407
408#ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
409
410
411
412
413
414
415
416static struct gdb_bp_save {
417 u8 *addr;
418 u8 opcode[2];
419} step_bp[2];
420
421static const unsigned char gdbstub_insn_sizes[256] =
422{
423
424 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3,
425 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
426 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3,
427 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1,
428 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2,
429 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
430 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
431 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
432 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2,
433 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2,
434 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2,
435 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2,
436 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2,
437 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
438 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
439 0, 2, 2, 2, 2, 2, 2, 4, 0, 3, 0, 4, 0, 6, 7, 1
440};
441
442static int __gdbstub_mark_bp(u8 *addr, int ix)
443{
444
445 if (((u8 *) VMALLOC_START <= addr) && (addr < (u8 *) VMALLOC_END))
446 goto okay;
447
448 if (((u8 *) 0x80000000UL <= addr) && (addr < (u8 *) 0xa0000000UL))
449 goto okay;
450 return 0;
451
452okay:
453 if (gdbstub_read_byte(addr + 0, &step_bp[ix].opcode[0]) < 0 ||
454 gdbstub_read_byte(addr + 1, &step_bp[ix].opcode[1]) < 0)
455 return 0;
456
457 step_bp[ix].addr = addr;
458 return 1;
459}
460
461static inline void __gdbstub_restore_bp(void)
462{
463#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
464 if (step_bp[0].addr) {
465 gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
466 gdbstub_write_byte(step_bp[0].opcode[1], step_bp[0].addr + 1);
467 }
468 if (step_bp[1].addr) {
469 gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
470 gdbstub_write_byte(step_bp[1].opcode[1], step_bp[1].addr + 1);
471 }
472#else
473 if (step_bp[0].addr)
474 gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
475 if (step_bp[1].addr)
476 gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
477#endif
478
479 gdbstub_flush_caches = 1;
480
481 step_bp[0].addr = NULL;
482 step_bp[0].opcode[0] = 0;
483 step_bp[0].opcode[1] = 0;
484 step_bp[1].addr = NULL;
485 step_bp[1].opcode[0] = 0;
486 step_bp[1].opcode[1] = 0;
487}
488
489
490
491
492static int gdbstub_single_step(struct pt_regs *regs)
493{
494 unsigned size;
495 uint32_t x;
496 uint8_t cur, *pc, *sp;
497
498 step_bp[0].addr = NULL;
499 step_bp[0].opcode[0] = 0;
500 step_bp[0].opcode[1] = 0;
501 step_bp[1].addr = NULL;
502 step_bp[1].opcode[0] = 0;
503 step_bp[1].opcode[1] = 0;
504 x = 0;
505
506 pc = (u8 *) regs->pc;
507 sp = (u8 *) (regs + 1);
508 if (gdbstub_read_byte(pc, &cur) < 0)
509 return -EFAULT;
510
511 gdbstub_bkpt("Single Step from %p { %02x }\n", pc, cur);
512
513 gdbstub_flush_caches = 1;
514
515 size = gdbstub_insn_sizes[cur];
516 if (size > 0) {
517 if (!__gdbstub_mark_bp(pc + size, 0))
518 goto fault;
519 } else {
520 switch (cur) {
521
522 case 0xc0 ... 0xca:
523 if (gdbstub_read_byte(pc + 1, (u8 *) &x) < 0)
524 goto fault;
525 if (!__gdbstub_mark_bp(pc + 2, 0))
526 goto fault;
527 if ((x < 0 || x > 2) &&
528 !__gdbstub_mark_bp(pc + (s8) x, 1))
529 goto fault;
530 break;
531
532
533 case 0xd0 ... 0xda:
534 if (!__gdbstub_mark_bp(pc + 1, 0))
535 goto fault;
536 if (regs->pc != regs->lar &&
537 !__gdbstub_mark_bp((u8 *) regs->lar, 1))
538 goto fault;
539 break;
540
541
542
543 case 0xdb:
544 if (!__gdbstub_mark_bp(pc + 1, 0))
545 goto fault;
546 break;
547
548
549 case 0xcc:
550 case 0xcd:
551 if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
552 gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0)
553 goto fault;
554 if (!__gdbstub_mark_bp(pc + (s16) x, 0))
555 goto fault;
556 break;
557
558
559 case 0xdc:
560 case 0xdd:
561 if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
562 gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0 ||
563 gdbstub_read_byte(pc + 3, ((u8 *) &x) + 2) < 0 ||
564 gdbstub_read_byte(pc + 4, ((u8 *) &x) + 3) < 0)
565 goto fault;
566 if (!__gdbstub_mark_bp(pc + (s32) x, 0))
567 goto fault;
568 break;
569
570
571 case 0xde:
572 if (!__gdbstub_mark_bp((u8 *) regs->mdr, 0))
573 goto fault;
574 break;
575
576
577 case 0xdf:
578 if (gdbstub_read_byte(pc + 2, (u8 *) &x) < 0)
579 goto fault;
580 sp += (s8)x;
581 if (gdbstub_read_byte(sp + 0, ((u8 *) &x) + 0) < 0 ||
582 gdbstub_read_byte(sp + 1, ((u8 *) &x) + 1) < 0 ||
583 gdbstub_read_byte(sp + 2, ((u8 *) &x) + 2) < 0 ||
584 gdbstub_read_byte(sp + 3, ((u8 *) &x) + 3) < 0)
585 goto fault;
586 if (!__gdbstub_mark_bp((u8 *) x, 0))
587 goto fault;
588 break;
589
590 case 0xf0:
591 if (gdbstub_read_byte(pc + 1, &cur) < 0)
592 goto fault;
593
594 if (cur >= 0xf0 && cur <= 0xf7) {
595
596 switch (cur & 3) {
597 case 0: x = regs->a0; break;
598 case 1: x = regs->a1; break;
599 case 2: x = regs->a2; break;
600 case 3: x = regs->a3; break;
601 }
602 if (!__gdbstub_mark_bp((u8 *) x, 0))
603 goto fault;
604 } else if (cur == 0xfc) {
605
606 if (gdbstub_read_byte(
607 sp + 0, ((u8 *) &x) + 0) < 0 ||
608 gdbstub_read_byte(
609 sp + 1, ((u8 *) &x) + 1) < 0 ||
610 gdbstub_read_byte(
611 sp + 2, ((u8 *) &x) + 2) < 0 ||
612 gdbstub_read_byte(
613 sp + 3, ((u8 *) &x) + 3) < 0)
614 goto fault;
615 if (!__gdbstub_mark_bp((u8 *) x, 0))
616 goto fault;
617 } else if (cur == 0xfd) {
618
619 if (gdbstub_read_byte(
620 sp + 4, ((u8 *) &x) + 0) < 0 ||
621 gdbstub_read_byte(
622 sp + 5, ((u8 *) &x) + 1) < 0 ||
623 gdbstub_read_byte(
624 sp + 6, ((u8 *) &x) + 2) < 0 ||
625 gdbstub_read_byte(
626 sp + 7, ((u8 *) &x) + 3) < 0)
627 goto fault;
628 if (!__gdbstub_mark_bp((u8 *) x, 0))
629 goto fault;
630 } else {
631 if (!__gdbstub_mark_bp(pc + 2, 0))
632 goto fault;
633 }
634
635 break;
636
637
638 case 0xf8:
639 if (gdbstub_read_byte(pc + 1, &cur) < 0)
640 goto fault;
641 if (!__gdbstub_mark_bp(pc + 3, 0))
642 goto fault;
643
644 if (cur >= 0xe8 && cur <= 0xeb) {
645 if (gdbstub_read_byte(
646 pc + 2, ((u8 *) &x) + 0) < 0)
647 goto fault;
648 if ((x < 0 || x > 3) &&
649 !__gdbstub_mark_bp(pc + (s8) x, 1))
650 goto fault;
651 }
652 break;
653
654 case 0xfa:
655 if (gdbstub_read_byte(pc + 1, &cur) < 0)
656 goto fault;
657
658 if (cur == 0xff) {
659
660 if (gdbstub_read_byte(
661 pc + 2, ((u8 *) &x) + 0) < 0 ||
662 gdbstub_read_byte(
663 pc + 3, ((u8 *) &x) + 1) < 0)
664 goto fault;
665 if (!__gdbstub_mark_bp(pc + (s16) x, 0))
666 goto fault;
667 } else {
668 if (!__gdbstub_mark_bp(pc + 4, 0))
669 goto fault;
670 }
671 break;
672
673 case 0xfc:
674 if (gdbstub_read_byte(pc + 1, &cur) < 0)
675 goto fault;
676 if (cur == 0xff) {
677
678 if (gdbstub_read_byte(
679 pc + 2, ((u8 *) &x) + 0) < 0 ||
680 gdbstub_read_byte(
681 pc + 3, ((u8 *) &x) + 1) < 0 ||
682 gdbstub_read_byte(
683 pc + 4, ((u8 *) &x) + 2) < 0 ||
684 gdbstub_read_byte(
685 pc + 5, ((u8 *) &x) + 3) < 0)
686 goto fault;
687 if (!__gdbstub_mark_bp(
688 pc + (s32) x, 0))
689 goto fault;
690 } else {
691 if (!__gdbstub_mark_bp(
692 pc + 6, 0))
693 goto fault;
694 }
695 break;
696
697 }
698 }
699
700 gdbstub_bkpt("Step: %02x at %p; %02x at %p\n",
701 step_bp[0].opcode[0], step_bp[0].addr,
702 step_bp[1].opcode[0], step_bp[1].addr);
703
704 if (step_bp[0].addr) {
705#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
706 if (gdbstub_write_byte(0xF7, step_bp[0].addr + 0) < 0 ||
707 gdbstub_write_byte(0xF7, step_bp[0].addr + 1) < 0)
708 goto fault;
709#else
710 if (gdbstub_write_byte(0xFF, step_bp[0].addr + 0) < 0)
711 goto fault;
712#endif
713 }
714
715 if (step_bp[1].addr) {
716#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
717 if (gdbstub_write_byte(0xF7, step_bp[1].addr + 0) < 0 ||
718 gdbstub_write_byte(0xF7, step_bp[1].addr + 1) < 0)
719 goto fault;
720#else
721 if (gdbstub_write_byte(0xFF, step_bp[1].addr + 0) < 0)
722 goto fault;
723#endif
724 }
725
726 return 0;
727
728 fault:
729
730 __gdbstub_restore_bp();
731 return -EFAULT;
732}
733#endif
734
735#ifdef CONFIG_GDBSTUB_CONSOLE
736
737void gdbstub_console_write(struct console *con, const char *p, unsigned n)
738{
739 static const char gdbstub_cr[] = { 0x0d };
740 char outbuf[26];
741 int qty;
742 u8 busy;
743
744 busy = gdbstub_busy;
745 gdbstub_busy = 1;
746
747 outbuf[0] = 'O';
748
749 while (n > 0) {
750 qty = 1;
751
752 while (n > 0 && qty < 20) {
753 mem2hex(p, outbuf + qty, 2, 0);
754 qty += 2;
755 if (*p == 0x0a) {
756 mem2hex(gdbstub_cr, outbuf + qty, 2, 0);
757 qty += 2;
758 }
759 p++;
760 n--;
761 }
762
763 outbuf[qty] = 0;
764 putpacket(outbuf);
765 }
766
767 gdbstub_busy = busy;
768}
769
770static kdev_t gdbstub_console_dev(struct console *con)
771{
772 return MKDEV(1, 3);
773}
774
775static struct console gdbstub_console = {
776 .name = "gdb",
777 .write = gdbstub_console_write,
778 .device = gdbstub_console_dev,
779 .flags = CON_PRINTBUFFER,
780 .index = -1,
781};
782
783#endif
784
785
786
787
788
789
790
791
792static
793unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
794{
795 const u8 *mem = _mem;
796 u8 ch[4];
797
798 if ((u32) mem & 1 && count >= 1) {
799 if (gdbstub_read_byte(mem, ch) != 0)
800 return 0;
801 buf = pack_hex_byte(buf, ch[0]);
802 mem++;
803 count--;
804 }
805
806 if ((u32) mem & 3 && count >= 2) {
807 if (gdbstub_read_word(mem, ch) != 0)
808 return 0;
809 buf = pack_hex_byte(buf, ch[0]);
810 buf = pack_hex_byte(buf, ch[1]);
811 mem += 2;
812 count -= 2;
813 }
814
815 while (count >= 4) {
816 if (gdbstub_read_dword(mem, ch) != 0)
817 return 0;
818 buf = pack_hex_byte(buf, ch[0]);
819 buf = pack_hex_byte(buf, ch[1]);
820 buf = pack_hex_byte(buf, ch[2]);
821 buf = pack_hex_byte(buf, ch[3]);
822 mem += 4;
823 count -= 4;
824 }
825
826 if (count >= 2) {
827 if (gdbstub_read_word(mem, ch) != 0)
828 return 0;
829 buf = pack_hex_byte(buf, ch[0]);
830 buf = pack_hex_byte(buf, ch[1]);
831 mem += 2;
832 count -= 2;
833 }
834
835 if (count >= 1) {
836 if (gdbstub_read_byte(mem, ch) != 0)
837 return 0;
838 buf = pack_hex_byte(buf, ch[0]);
839 }
840
841 *buf = 0;
842 return buf;
843}
844
845
846
847
848
849
850
851static
852const char *hex2mem(const char *buf, void *_mem, int count, int may_fault)
853{
854 u8 *mem = _mem;
855 union {
856 u32 val;
857 u8 b[4];
858 } ch;
859
860 if ((u32) mem & 1 && count >= 1) {
861 ch.b[0] = hex(*buf++) << 4;
862 ch.b[0] |= hex(*buf++);
863 if (gdbstub_write_byte(ch.val, mem) != 0)
864 return 0;
865 mem++;
866 count--;
867 }
868
869 if ((u32) mem & 3 && count >= 2) {
870 ch.b[0] = hex(*buf++) << 4;
871 ch.b[0] |= hex(*buf++);
872 ch.b[1] = hex(*buf++) << 4;
873 ch.b[1] |= hex(*buf++);
874 if (gdbstub_write_word(ch.val, mem) != 0)
875 return 0;
876 mem += 2;
877 count -= 2;
878 }
879
880 while (count >= 4) {
881 ch.b[0] = hex(*buf++) << 4;
882 ch.b[0] |= hex(*buf++);
883 ch.b[1] = hex(*buf++) << 4;
884 ch.b[1] |= hex(*buf++);
885 ch.b[2] = hex(*buf++) << 4;
886 ch.b[2] |= hex(*buf++);
887 ch.b[3] = hex(*buf++) << 4;
888 ch.b[3] |= hex(*buf++);
889 if (gdbstub_write_dword(ch.val, mem) != 0)
890 return 0;
891 mem += 4;
892 count -= 4;
893 }
894
895 if (count >= 2) {
896 ch.b[0] = hex(*buf++) << 4;
897 ch.b[0] |= hex(*buf++);
898 ch.b[1] = hex(*buf++) << 4;
899 ch.b[1] |= hex(*buf++);
900 if (gdbstub_write_word(ch.val, mem) != 0)
901 return 0;
902 mem += 2;
903 count -= 2;
904 }
905
906 if (count >= 1) {
907 ch.b[0] = hex(*buf++) << 4;
908 ch.b[0] |= hex(*buf++);
909 if (gdbstub_write_byte(ch.val, mem) != 0)
910 return 0;
911 }
912
913 return buf;
914}
915
916
917
918
919
920
921static const struct excep_to_sig_map {
922 enum exception_code excep;
923 unsigned char signo;
924} excep_to_sig_map[] = {
925 { EXCEP_ITLBMISS, SIGSEGV },
926 { EXCEP_DTLBMISS, SIGSEGV },
927 { EXCEP_TRAP, SIGTRAP },
928 { EXCEP_ISTEP, SIGTRAP },
929 { EXCEP_IBREAK, SIGTRAP },
930 { EXCEP_OBREAK, SIGTRAP },
931 { EXCEP_UNIMPINS, SIGILL },
932 { EXCEP_UNIMPEXINS, SIGILL },
933 { EXCEP_MEMERR, SIGSEGV },
934 { EXCEP_MISALIGN, SIGSEGV },
935 { EXCEP_BUSERROR, SIGBUS },
936 { EXCEP_ILLINSACC, SIGSEGV },
937 { EXCEP_ILLDATACC, SIGSEGV },
938 { EXCEP_IOINSACC, SIGSEGV },
939 { EXCEP_PRIVINSACC, SIGSEGV },
940 { EXCEP_PRIVDATACC, SIGSEGV },
941 { EXCEP_FPU_DISABLED, SIGFPE },
942 { EXCEP_FPU_UNIMPINS, SIGFPE },
943 { EXCEP_FPU_OPERATION, SIGFPE },
944 { EXCEP_WDT, SIGALRM },
945 { EXCEP_NMI, SIGQUIT },
946 { EXCEP_IRQ_LEVEL0, SIGINT },
947 { EXCEP_IRQ_LEVEL1, SIGINT },
948 { EXCEP_IRQ_LEVEL2, SIGINT },
949 { EXCEP_IRQ_LEVEL3, SIGINT },
950 { EXCEP_IRQ_LEVEL4, SIGINT },
951 { EXCEP_IRQ_LEVEL5, SIGINT },
952 { EXCEP_IRQ_LEVEL6, SIGINT },
953 { 0, 0}
954};
955
956
957
958
959static int computeSignal(enum exception_code excep)
960{
961 const struct excep_to_sig_map *map;
962
963 for (map = excep_to_sig_map; map->signo; map++)
964 if (map->excep == excep)
965 return map->signo;
966
967 return SIGHUP;
968}
969
970static u32 gdbstub_fpcr, gdbstub_fpufs_array[32];
971
972
973
974
975static void gdbstub_store_fpu(void)
976{
977#ifdef CONFIG_FPU
978
979 asm volatile(
980 "or %2,epsw\n"
981#ifdef CONFIG_MN10300_PROC_MN103E010
982 "nop\n"
983 "nop\n"
984#endif
985 "mov %1, a1\n"
986 "fmov fs0, (a1+)\n"
987 "fmov fs1, (a1+)\n"
988 "fmov fs2, (a1+)\n"
989 "fmov fs3, (a1+)\n"
990 "fmov fs4, (a1+)\n"
991 "fmov fs5, (a1+)\n"
992 "fmov fs6, (a1+)\n"
993 "fmov fs7, (a1+)\n"
994 "fmov fs8, (a1+)\n"
995 "fmov fs9, (a1+)\n"
996 "fmov fs10, (a1+)\n"
997 "fmov fs11, (a1+)\n"
998 "fmov fs12, (a1+)\n"
999 "fmov fs13, (a1+)\n"
1000 "fmov fs14, (a1+)\n"
1001 "fmov fs15, (a1+)\n"
1002 "fmov fs16, (a1+)\n"
1003 "fmov fs17, (a1+)\n"
1004 "fmov fs18, (a1+)\n"
1005 "fmov fs19, (a1+)\n"
1006 "fmov fs20, (a1+)\n"
1007 "fmov fs21, (a1+)\n"
1008 "fmov fs22, (a1+)\n"
1009 "fmov fs23, (a1+)\n"
1010 "fmov fs24, (a1+)\n"
1011 "fmov fs25, (a1+)\n"
1012 "fmov fs26, (a1+)\n"
1013 "fmov fs27, (a1+)\n"
1014 "fmov fs28, (a1+)\n"
1015 "fmov fs29, (a1+)\n"
1016 "fmov fs30, (a1+)\n"
1017 "fmov fs31, (a1+)\n"
1018 "fmov fpcr, %0\n"
1019 : "=d"(gdbstub_fpcr)
1020 : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE)
1021 : "a1"
1022 );
1023#endif
1024}
1025
1026
1027
1028
1029static void gdbstub_load_fpu(void)
1030{
1031#ifdef CONFIG_FPU
1032
1033 asm volatile(
1034 "or %1,epsw\n"
1035#ifdef CONFIG_MN10300_PROC_MN103E010
1036 "nop\n"
1037 "nop\n"
1038#endif
1039 "mov %0, a1\n"
1040 "fmov (a1+), fs0\n"
1041 "fmov (a1+), fs1\n"
1042 "fmov (a1+), fs2\n"
1043 "fmov (a1+), fs3\n"
1044 "fmov (a1+), fs4\n"
1045 "fmov (a1+), fs5\n"
1046 "fmov (a1+), fs6\n"
1047 "fmov (a1+), fs7\n"
1048 "fmov (a1+), fs8\n"
1049 "fmov (a1+), fs9\n"
1050 "fmov (a1+), fs10\n"
1051 "fmov (a1+), fs11\n"
1052 "fmov (a1+), fs12\n"
1053 "fmov (a1+), fs13\n"
1054 "fmov (a1+), fs14\n"
1055 "fmov (a1+), fs15\n"
1056 "fmov (a1+), fs16\n"
1057 "fmov (a1+), fs17\n"
1058 "fmov (a1+), fs18\n"
1059 "fmov (a1+), fs19\n"
1060 "fmov (a1+), fs20\n"
1061 "fmov (a1+), fs21\n"
1062 "fmov (a1+), fs22\n"
1063 "fmov (a1+), fs23\n"
1064 "fmov (a1+), fs24\n"
1065 "fmov (a1+), fs25\n"
1066 "fmov (a1+), fs26\n"
1067 "fmov (a1+), fs27\n"
1068 "fmov (a1+), fs28\n"
1069 "fmov (a1+), fs29\n"
1070 "fmov (a1+), fs30\n"
1071 "fmov (a1+), fs31\n"
1072 "fmov %2, fpcr\n"
1073 :
1074 : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE), "d"(gdbstub_fpcr)
1075 : "a1"
1076 );
1077#endif
1078}
1079
1080
1081
1082
1083int gdbstub_set_breakpoint(u8 *addr, int len)
1084{
1085 int bkpt, loop, xloop;
1086
1087#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1088 len = (len + 1) & ~1;
1089#endif
1090
1091 gdbstub_bkpt("setbkpt(%p,%d)\n", addr, len);
1092
1093 for (bkpt = 255; bkpt >= 0; bkpt--)
1094 if (!gdbstub_bkpts[bkpt].addr)
1095 break;
1096 if (bkpt < 0)
1097 return -ENOSPC;
1098
1099 for (loop = 0; loop < len; loop++)
1100 if (gdbstub_read_byte(&addr[loop],
1101 &gdbstub_bkpts[bkpt].origbytes[loop]
1102 ) < 0)
1103 return -EFAULT;
1104
1105 gdbstub_flush_caches = 1;
1106
1107#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1108 for (loop = 0; loop < len; loop++)
1109 if (gdbstub_write_byte(0xF7, &addr[loop]) < 0)
1110 goto restore;
1111#else
1112 for (loop = 0; loop < len; loop++)
1113 if (gdbstub_write_byte(0xFF, &addr[loop]) < 0)
1114 goto restore;
1115#endif
1116
1117 gdbstub_bkpts[bkpt].addr = addr;
1118 gdbstub_bkpts[bkpt].len = len;
1119
1120 gdbstub_bkpt("Set BKPT[%02x]: %p-%p {%02x%02x%02x%02x%02x%02x%02x}\n",
1121 bkpt,
1122 gdbstub_bkpts[bkpt].addr,
1123 gdbstub_bkpts[bkpt].addr + gdbstub_bkpts[bkpt].len - 1,
1124 gdbstub_bkpts[bkpt].origbytes[0],
1125 gdbstub_bkpts[bkpt].origbytes[1],
1126 gdbstub_bkpts[bkpt].origbytes[2],
1127 gdbstub_bkpts[bkpt].origbytes[3],
1128 gdbstub_bkpts[bkpt].origbytes[4],
1129 gdbstub_bkpts[bkpt].origbytes[5],
1130 gdbstub_bkpts[bkpt].origbytes[6]
1131 );
1132
1133 return 0;
1134
1135restore:
1136 for (xloop = 0; xloop < loop; xloop++)
1137 gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[xloop],
1138 addr + xloop);
1139 return -EFAULT;
1140}
1141
1142
1143
1144
1145int gdbstub_clear_breakpoint(u8 *addr, int len)
1146{
1147 int bkpt, loop;
1148
1149#ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1150 len = (len + 1) & ~1;
1151#endif
1152
1153 gdbstub_bkpt("clearbkpt(%p,%d)\n", addr, len);
1154
1155 for (bkpt = 255; bkpt >= 0; bkpt--)
1156 if (gdbstub_bkpts[bkpt].addr == addr &&
1157 gdbstub_bkpts[bkpt].len == len)
1158 break;
1159 if (bkpt < 0)
1160 return -ENOENT;
1161
1162 gdbstub_bkpts[bkpt].addr = NULL;
1163
1164 gdbstub_flush_caches = 1;
1165
1166 for (loop = 0; loop < len; loop++)
1167 if (gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[loop],
1168 addr + loop) < 0)
1169 return -EFAULT;
1170
1171 return 0;
1172}
1173
1174
1175
1176
1177
1178static int gdbstub(struct pt_regs *regs, enum exception_code excep)
1179{
1180 unsigned long *stack;
1181 unsigned long epsw, mdr;
1182 uint32_t zero, ssp;
1183 uint8_t broke;
1184 char *ptr;
1185 int sigval;
1186 int addr;
1187 int length;
1188 int loop;
1189
1190 if (excep == EXCEP_FPU_DISABLED)
1191 return -ENOTSUPP;
1192
1193 gdbstub_flush_caches = 0;
1194
1195 mn10300_set_gdbleds(1);
1196
1197 asm volatile("mov mdr,%0" : "=d"(mdr));
1198 local_save_flags(epsw);
1199 arch_local_change_intr_mask_level(
1200 NUM2EPSW_IM(CONFIG_DEBUGGER_IRQ_LEVEL + 1));
1201
1202 gdbstub_store_fpu();
1203
1204#ifdef CONFIG_GDBSTUB_IMMEDIATE
1205
1206 if (regs->pc == (unsigned long) __gdbstub_pause)
1207 regs->pc = (unsigned long) start_kernel;
1208#endif
1209
1210
1211
1212 broke = 0;
1213#ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
1214 if ((step_bp[0].addr && step_bp[0].addr == (u8 *) regs->pc) ||
1215 (step_bp[1].addr && step_bp[1].addr == (u8 *) regs->pc))
1216 broke = 1;
1217
1218 __gdbstub_restore_bp();
1219#endif
1220
1221 if (gdbstub_rx_unget) {
1222 sigval = SIGINT;
1223 if (gdbstub_rx_unget != 3)
1224 goto packet_waiting;
1225 gdbstub_rx_unget = 0;
1226 }
1227
1228 stack = (unsigned long *) regs->sp;
1229 sigval = broke ? SIGTRAP : computeSignal(excep);
1230
1231
1232 if (!user_mode(regs) && excep == EXCEP_SYSCALL15) {
1233 const struct bug_entry *bug;
1234
1235 bug = find_bug(regs->pc);
1236 if (bug)
1237 goto found_bug;
1238 length = snprintf(trans_buffer, sizeof(trans_buffer),
1239 "BUG() at address %lx\n", regs->pc);
1240 goto send_bug_pkt;
1241
1242 found_bug:
1243 length = snprintf(trans_buffer, sizeof(trans_buffer),
1244 "BUG() at address %lx (%s:%d)\n",
1245 regs->pc, bug->file, bug->line);
1246
1247 send_bug_pkt:
1248 ptr = output_buffer;
1249 *ptr++ = 'O';
1250 ptr = mem2hex(trans_buffer, ptr, length, 0);
1251 *ptr = 0;
1252 putpacket(output_buffer);
1253
1254 regs->pc -= 2;
1255 sigval = SIGABRT;
1256 } else if (regs->pc == (unsigned long) __gdbstub_bug_trap) {
1257 regs->pc = regs->mdr;
1258 sigval = SIGABRT;
1259 }
1260
1261
1262
1263
1264
1265 if (sigval != SIGINT && sigval != SIGTRAP && sigval != SIGILL) {
1266 static const char title[] = "Excep ", tbcberr[] = "BCBERR ";
1267 static const char crlf[] = "\r\n";
1268 char hx;
1269 u32 bcberr = BCBERR;
1270
1271 ptr = output_buffer;
1272 *ptr++ = 'O';
1273 ptr = mem2hex(title, ptr, sizeof(title) - 1, 0);
1274
1275 hx = hex_asc_hi(excep >> 8);
1276 ptr = pack_hex_byte(ptr, hx);
1277 hx = hex_asc_lo(excep >> 8);
1278 ptr = pack_hex_byte(ptr, hx);
1279 hx = hex_asc_hi(excep);
1280 ptr = pack_hex_byte(ptr, hx);
1281 hx = hex_asc_lo(excep);
1282 ptr = pack_hex_byte(ptr, hx);
1283
1284 ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
1285 *ptr = 0;
1286 putpacket(output_buffer);
1287
1288
1289 ptr = output_buffer;
1290 *ptr++ = 'O';
1291 ptr = mem2hex(tbcberr, ptr, sizeof(tbcberr) - 1, 0);
1292
1293 hx = hex_asc_hi(bcberr >> 24);
1294 ptr = pack_hex_byte(ptr, hx);
1295 hx = hex_asc_lo(bcberr >> 24);
1296 ptr = pack_hex_byte(ptr, hx);
1297 hx = hex_asc_hi(bcberr >> 16);
1298 ptr = pack_hex_byte(ptr, hx);
1299 hx = hex_asc_lo(bcberr >> 16);
1300 ptr = pack_hex_byte(ptr, hx);
1301 hx = hex_asc_hi(bcberr >> 8);
1302 ptr = pack_hex_byte(ptr, hx);
1303 hx = hex_asc_lo(bcberr >> 8);
1304 ptr = pack_hex_byte(ptr, hx);
1305 hx = hex_asc_hi(bcberr);
1306 ptr = pack_hex_byte(ptr, hx);
1307 hx = hex_asc_lo(bcberr);
1308 ptr = pack_hex_byte(ptr, hx);
1309
1310 ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
1311 *ptr = 0;
1312 putpacket(output_buffer);
1313 }
1314
1315
1316
1317
1318 ptr = output_buffer;
1319
1320
1321
1322
1323 *ptr++ = 'T';
1324 ptr = pack_hex_byte(ptr, sigval);
1325
1326
1327
1328
1329 ptr = pack_hex_byte(ptr, GDB_REGID_PC);
1330 *ptr++ = ':';
1331 ptr = mem2hex(®s->pc, ptr, 4, 0);
1332 *ptr++ = ';';
1333
1334
1335
1336
1337 ptr = pack_hex_byte(ptr, GDB_REGID_FP);
1338 *ptr++ = ':';
1339 ptr = mem2hex(®s->a3, ptr, 4, 0);
1340 *ptr++ = ';';
1341
1342
1343
1344
1345 ssp = (unsigned long) (regs + 1);
1346 ptr = pack_hex_byte(ptr, GDB_REGID_SP);
1347 *ptr++ = ':';
1348 ptr = mem2hex(&ssp, ptr, 4, 0);
1349 *ptr++ = ';';
1350
1351 *ptr++ = 0;
1352 putpacket(output_buffer);
1353
1354packet_waiting:
1355
1356
1357
1358 while (1) {
1359 output_buffer[0] = 0;
1360 getpacket(input_buffer);
1361
1362 switch (input_buffer[0]) {
1363
1364 case '?':
1365 output_buffer[0] = 'S';
1366 output_buffer[1] = hex_asc_hi(sigval);
1367 output_buffer[2] = hex_asc_lo(sigval);
1368 output_buffer[3] = 0;
1369 break;
1370
1371 case 'd':
1372
1373 break;
1374
1375
1376
1377
1378 case 'g':
1379 zero = 0;
1380 ssp = (u32) (regs + 1);
1381 ptr = output_buffer;
1382 ptr = mem2hex(®s->d0, ptr, 4, 0);
1383 ptr = mem2hex(®s->d1, ptr, 4, 0);
1384 ptr = mem2hex(®s->d2, ptr, 4, 0);
1385 ptr = mem2hex(®s->d3, ptr, 4, 0);
1386 ptr = mem2hex(®s->a0, ptr, 4, 0);
1387 ptr = mem2hex(®s->a1, ptr, 4, 0);
1388 ptr = mem2hex(®s->a2, ptr, 4, 0);
1389 ptr = mem2hex(®s->a3, ptr, 4, 0);
1390
1391 ptr = mem2hex(&ssp, ptr, 4, 0);
1392 ptr = mem2hex(®s->pc, ptr, 4, 0);
1393 ptr = mem2hex(®s->mdr, ptr, 4, 0);
1394 ptr = mem2hex(®s->epsw, ptr, 4, 0);
1395 ptr = mem2hex(®s->lir, ptr, 4, 0);
1396 ptr = mem2hex(®s->lar, ptr, 4, 0);
1397 ptr = mem2hex(®s->mdrq, ptr, 4, 0);
1398
1399 ptr = mem2hex(®s->e0, ptr, 4, 0);
1400 ptr = mem2hex(®s->e1, ptr, 4, 0);
1401 ptr = mem2hex(®s->e2, ptr, 4, 0);
1402 ptr = mem2hex(®s->e3, ptr, 4, 0);
1403 ptr = mem2hex(®s->e4, ptr, 4, 0);
1404 ptr = mem2hex(®s->e5, ptr, 4, 0);
1405 ptr = mem2hex(®s->e6, ptr, 4, 0);
1406 ptr = mem2hex(®s->e7, ptr, 4, 0);
1407
1408 ptr = mem2hex(&ssp, ptr, 4, 0);
1409 ptr = mem2hex(®s, ptr, 4, 0);
1410 ptr = mem2hex(®s->sp, ptr, 4, 0);
1411 ptr = mem2hex(®s->mcrh, ptr, 4, 0);
1412 ptr = mem2hex(®s->mcrl, ptr, 4, 0);
1413 ptr = mem2hex(®s->mcvf, ptr, 4, 0);
1414
1415 ptr = mem2hex(&gdbstub_fpcr, ptr, 4, 0);
1416 ptr = mem2hex(&zero, ptr, 4, 0);
1417 ptr = mem2hex(&zero, ptr, 4, 0);
1418 for (loop = 0; loop < 32; loop++)
1419 ptr = mem2hex(&gdbstub_fpufs_array[loop],
1420 ptr, 4, 0);
1421
1422 break;
1423
1424
1425
1426
1427 case 'G':
1428 {
1429 const char *ptr;
1430
1431 ptr = &input_buffer[1];
1432 ptr = hex2mem(ptr, ®s->d0, 4, 0);
1433 ptr = hex2mem(ptr, ®s->d1, 4, 0);
1434 ptr = hex2mem(ptr, ®s->d2, 4, 0);
1435 ptr = hex2mem(ptr, ®s->d3, 4, 0);
1436 ptr = hex2mem(ptr, ®s->a0, 4, 0);
1437 ptr = hex2mem(ptr, ®s->a1, 4, 0);
1438 ptr = hex2mem(ptr, ®s->a2, 4, 0);
1439 ptr = hex2mem(ptr, ®s->a3, 4, 0);
1440
1441 ptr = hex2mem(ptr, &ssp, 4, 0);
1442 ptr = hex2mem(ptr, ®s->pc, 4, 0);
1443 ptr = hex2mem(ptr, ®s->mdr, 4, 0);
1444 ptr = hex2mem(ptr, ®s->epsw, 4, 0);
1445 ptr = hex2mem(ptr, ®s->lir, 4, 0);
1446 ptr = hex2mem(ptr, ®s->lar, 4, 0);
1447 ptr = hex2mem(ptr, ®s->mdrq, 4, 0);
1448
1449 ptr = hex2mem(ptr, ®s->e0, 4, 0);
1450 ptr = hex2mem(ptr, ®s->e1, 4, 0);
1451 ptr = hex2mem(ptr, ®s->e2, 4, 0);
1452 ptr = hex2mem(ptr, ®s->e3, 4, 0);
1453 ptr = hex2mem(ptr, ®s->e4, 4, 0);
1454 ptr = hex2mem(ptr, ®s->e5, 4, 0);
1455 ptr = hex2mem(ptr, ®s->e6, 4, 0);
1456 ptr = hex2mem(ptr, ®s->e7, 4, 0);
1457
1458 ptr = hex2mem(ptr, &ssp, 4, 0);
1459 ptr = hex2mem(ptr, &zero, 4, 0);
1460 ptr = hex2mem(ptr, ®s->sp, 4, 0);
1461 ptr = hex2mem(ptr, ®s->mcrh, 4, 0);
1462 ptr = hex2mem(ptr, ®s->mcrl, 4, 0);
1463 ptr = hex2mem(ptr, ®s->mcvf, 4, 0);
1464
1465 ptr = hex2mem(ptr, &zero, 4, 0);
1466 ptr = hex2mem(ptr, &zero, 4, 0);
1467 ptr = hex2mem(ptr, &zero, 4, 0);
1468 for (loop = 0; loop < 32; loop++)
1469 ptr = hex2mem(ptr, &zero, 4, 0);
1470
1471#if 0
1472
1473
1474
1475
1476 unsigned long *newsp = (unsigned long *) registers[SP];
1477 if (sp != newsp)
1478 sp = memcpy(newsp, sp, 16 * 4);
1479#endif
1480
1481 gdbstub_strcpy(output_buffer, "OK");
1482 }
1483 break;
1484
1485
1486
1487
1488 case 'm':
1489 ptr = &input_buffer[1];
1490
1491 if (hexToInt(&ptr, &addr) &&
1492 *ptr++ == ',' &&
1493 hexToInt(&ptr, &length)
1494 ) {
1495 if (mem2hex((char *) addr, output_buffer,
1496 length, 1))
1497 break;
1498 gdbstub_strcpy(output_buffer, "E03");
1499 } else {
1500 gdbstub_strcpy(output_buffer, "E01");
1501 }
1502 break;
1503
1504
1505
1506
1507
1508 case 'M':
1509 ptr = &input_buffer[1];
1510
1511 if (hexToInt(&ptr, &addr) &&
1512 *ptr++ == ',' &&
1513 hexToInt(&ptr, &length) &&
1514 *ptr++ == ':'
1515 ) {
1516 if (hex2mem(ptr, (char *) addr, length, 1))
1517 gdbstub_strcpy(output_buffer, "OK");
1518 else
1519 gdbstub_strcpy(output_buffer, "E03");
1520
1521 gdbstub_flush_caches = 1;
1522 } else {
1523 gdbstub_strcpy(output_buffer, "E02");
1524 }
1525 break;
1526
1527
1528
1529
1530 case 'c':
1531
1532
1533
1534 ptr = &input_buffer[1];
1535 if (hexToInt(&ptr, &addr))
1536 regs->pc = addr;
1537 goto done;
1538
1539
1540
1541
1542 case 'k' :
1543 goto done;
1544
1545
1546
1547
1548 case 'r':
1549 break;
1550
1551
1552
1553
1554 case 's':
1555
1556
1557
1558
1559
1560#ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
1561 if (gdbstub_single_step(regs) < 0)
1562
1563 gdbstub_printk("unable to set single-step"
1564 " bp\n");
1565 goto done;
1566#else
1567 gdbstub_strcpy(output_buffer, "E01");
1568 break;
1569#endif
1570
1571
1572
1573
1574 case 'b':
1575 do {
1576 int baudrate;
1577
1578 ptr = &input_buffer[1];
1579 if (!hexToInt(&ptr, &baudrate)) {
1580 gdbstub_strcpy(output_buffer, "B01");
1581 break;
1582 }
1583
1584 if (baudrate) {
1585
1586 putpacket("OK");
1587 gdbstub_io_set_baud(baudrate);
1588 }
1589 } while (0);
1590 break;
1591
1592
1593
1594
1595 case 'Z':
1596 ptr = &input_buffer[1];
1597
1598 if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
1599 !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
1600 !hexToInt(&ptr, &length)
1601 ) {
1602 gdbstub_strcpy(output_buffer, "E01");
1603 break;
1604 }
1605
1606
1607 gdbstub_strcpy(output_buffer, "E03");
1608 if (loop != 0 ||
1609 length < 1 ||
1610 length > 7 ||
1611 (unsigned long) addr < 4096)
1612 break;
1613
1614 if (gdbstub_set_breakpoint((u8 *) addr, length) < 0)
1615 break;
1616
1617 gdbstub_strcpy(output_buffer, "OK");
1618 break;
1619
1620
1621
1622
1623 case 'z':
1624 ptr = &input_buffer[1];
1625
1626 if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
1627 !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
1628 !hexToInt(&ptr, &length)
1629 ) {
1630 gdbstub_strcpy(output_buffer, "E01");
1631 break;
1632 }
1633
1634
1635 gdbstub_strcpy(output_buffer, "E03");
1636 if (loop != 0 ||
1637 length < 1 ||
1638 length > 7 ||
1639 (unsigned long) addr < 4096)
1640 break;
1641
1642 if (gdbstub_clear_breakpoint((u8 *) addr, length) < 0)
1643 break;
1644
1645 gdbstub_strcpy(output_buffer, "OK");
1646 break;
1647
1648 default:
1649 gdbstub_proto("### GDB Unsupported Cmd '%s'\n",
1650 input_buffer);
1651 break;
1652 }
1653
1654
1655 putpacket(output_buffer);
1656 }
1657
1658done:
1659
1660
1661
1662
1663
1664
1665
1666
1667 if (gdbstub_flush_caches)
1668 debugger_local_cache_flushinv();
1669
1670 gdbstub_load_fpu();
1671 mn10300_set_gdbleds(0);
1672 if (excep == EXCEP_NMI)
1673 NMICR = NMICR_NMIF;
1674
1675 touch_softlockup_watchdog();
1676
1677 local_irq_restore(epsw);
1678 return 0;
1679}
1680
1681
1682
1683
1684
1685int at_debugger_breakpoint(struct pt_regs *regs)
1686{
1687 return 0;
1688}
1689
1690
1691
1692
1693asmlinkage int debugger_intercept(enum exception_code excep,
1694 int signo, int si_code, struct pt_regs *regs)
1695{
1696 static u8 notfirst = 1;
1697 int ret;
1698
1699 if (gdbstub_busy)
1700 gdbstub_printk("--> gdbstub reentered itself\n");
1701 gdbstub_busy = 1;
1702
1703 if (notfirst) {
1704 unsigned long mdr;
1705 asm("mov mdr,%0" : "=d"(mdr));
1706
1707 gdbstub_entry(
1708 "--> debugger_intercept(%p,%04x) [MDR=%lx PC=%lx]\n",
1709 regs, excep, mdr, regs->pc);
1710
1711 gdbstub_entry(
1712 "PC: %08lx EPSW: %08lx SSP: %08lx mode: %s\n",
1713 regs->pc, regs->epsw, (unsigned long) &ret,
1714 user_mode(regs) ? "User" : "Super");
1715 gdbstub_entry(
1716 "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
1717 regs->d0, regs->d1, regs->d2, regs->d3);
1718 gdbstub_entry(
1719 "a0: %08lx a1: %08lx a2: %08lx a3: %08lx\n",
1720 regs->a0, regs->a1, regs->a2, regs->a3);
1721 gdbstub_entry(
1722 "e0: %08lx e1: %08lx e2: %08lx e3: %08lx\n",
1723 regs->e0, regs->e1, regs->e2, regs->e3);
1724 gdbstub_entry(
1725 "e4: %08lx e5: %08lx e6: %08lx e7: %08lx\n",
1726 regs->e4, regs->e5, regs->e6, regs->e7);
1727 gdbstub_entry(
1728 "lar: %08lx lir: %08lx mdr: %08lx usp: %08lx\n",
1729 regs->lar, regs->lir, regs->mdr, regs->sp);
1730 gdbstub_entry(
1731 "cvf: %08lx crl: %08lx crh: %08lx drq: %08lx\n",
1732 regs->mcvf, regs->mcrl, regs->mcrh, regs->mdrq);
1733 gdbstub_entry(
1734 "threadinfo=%p task=%p)\n",
1735 current_thread_info(), current);
1736 } else {
1737 notfirst = 1;
1738 }
1739
1740 ret = gdbstub(regs, excep);
1741
1742 gdbstub_entry("<-- debugger_intercept()\n");
1743 gdbstub_busy = 0;
1744 return ret;
1745}
1746
1747
1748
1749
1750asmlinkage void gdbstub_exception(struct pt_regs *regs,
1751 enum exception_code excep)
1752{
1753 unsigned long mdr;
1754
1755 asm("mov mdr,%0" : "=d"(mdr));
1756 gdbstub_entry("--> gdbstub exception({%p},%04x) [MDR=%lx]\n",
1757 regs, excep, mdr);
1758
1759 while ((unsigned long) regs == 0xffffffff) {}
1760
1761
1762 if (regs->pc == (unsigned) gdbstub_read_byte_guard) {
1763 regs->pc = (unsigned) gdbstub_read_byte_cont;
1764 goto fault;
1765 }
1766
1767 if (regs->pc == (unsigned) gdbstub_read_word_guard) {
1768 regs->pc = (unsigned) gdbstub_read_word_cont;
1769 goto fault;
1770 }
1771
1772 if (regs->pc == (unsigned) gdbstub_read_dword_guard) {
1773 regs->pc = (unsigned) gdbstub_read_dword_cont;
1774 goto fault;
1775 }
1776
1777 if (regs->pc == (unsigned) gdbstub_write_byte_guard) {
1778 regs->pc = (unsigned) gdbstub_write_byte_cont;
1779 goto fault;
1780 }
1781
1782 if (regs->pc == (unsigned) gdbstub_write_word_guard) {
1783 regs->pc = (unsigned) gdbstub_write_word_cont;
1784 goto fault;
1785 }
1786
1787 if (regs->pc == (unsigned) gdbstub_write_dword_guard) {
1788 regs->pc = (unsigned) gdbstub_write_dword_cont;
1789 goto fault;
1790 }
1791
1792 gdbstub_printk("\n### GDB stub caused an exception ###\n");
1793
1794
1795 console_verbose();
1796 show_registers(regs);
1797
1798 panic("GDB Stub caused an unexpected exception - can't continue\n");
1799
1800
1801fault:
1802 gdbstub_entry("<-- gdbstub exception() = EFAULT\n");
1803 regs->d0 = -EFAULT;
1804 return;
1805}
1806
1807
1808
1809
1810void gdbstub_exit(int status)
1811{
1812 unsigned char checksum;
1813 unsigned char ch;
1814 int count;
1815
1816 gdbstub_busy = 1;
1817 output_buffer[0] = 'W';
1818 output_buffer[1] = hex_asc_hi(status);
1819 output_buffer[2] = hex_asc_lo(status);
1820 output_buffer[3] = 0;
1821
1822 gdbstub_io_tx_char('$');
1823 checksum = 0;
1824 count = 0;
1825
1826 while ((ch = output_buffer[count]) != 0) {
1827 gdbstub_io_tx_char(ch);
1828 checksum += ch;
1829 count += 1;
1830 }
1831
1832 gdbstub_io_tx_char('#');
1833 gdbstub_io_tx_char(hex_asc_hi(checksum));
1834 gdbstub_io_tx_char(hex_asc_lo(checksum));
1835
1836
1837 gdbstub_io_tx_flush();
1838
1839 gdbstub_busy = 0;
1840}
1841
1842
1843
1844
1845asmlinkage void __init gdbstub_init(void)
1846{
1847#ifdef CONFIG_GDBSTUB_IMMEDIATE
1848 unsigned char ch;
1849 int ret;
1850#endif
1851
1852 gdbstub_busy = 1;
1853
1854 printk(KERN_INFO "%s", gdbstub_banner);
1855
1856 gdbstub_io_init();
1857
1858 gdbstub_entry("--> gdbstub_init\n");
1859
1860
1861
1862 gdbstub_io("### GDB Tx ACK\n");
1863 gdbstub_io_tx_char('+');
1864
1865#ifdef CONFIG_GDBSTUB_IMMEDIATE
1866 gdbstub_printk("GDB Stub waiting for packet\n");
1867
1868
1869
1870
1871 do { gdbstub_io_rx_char(&ch, 0); } while (ch != '$');
1872 do { gdbstub_io_rx_char(&ch, 0); } while (ch != '#');
1873
1874 do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
1875
1876 do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
1877
1878 gdbstub_io("### GDB Tx NAK\n");
1879 gdbstub_io_tx_char('-');
1880
1881#else
1882 printk("GDB Stub ready\n");
1883#endif
1884
1885 gdbstub_busy = 0;
1886 gdbstub_entry("<-- gdbstub_init\n");
1887}
1888
1889
1890
1891
1892#ifdef CONFIG_GDBSTUB_CONSOLE
1893static int __init gdbstub_postinit(void)
1894{
1895 printk(KERN_NOTICE "registering console\n");
1896 register_console(&gdbstub_console);
1897 return 0;
1898}
1899
1900__initcall(gdbstub_postinit);
1901#endif
1902
1903
1904
1905
1906
1907asmlinkage void gdbstub_rx_irq(struct pt_regs *regs, enum exception_code excep)
1908{
1909 char ch;
1910 int ret;
1911
1912 gdbstub_entry("--> gdbstub_rx_irq\n");
1913
1914 do {
1915 ret = gdbstub_io_rx_char(&ch, 1);
1916 if (ret != -EIO && ret != -EAGAIN) {
1917 if (ret != -EINTR)
1918 gdbstub_rx_unget = ch;
1919 gdbstub(regs, excep);
1920 }
1921 } while (ret != -EAGAIN);
1922
1923 gdbstub_entry("<-- gdbstub_rx_irq\n");
1924}
1925