3 * Originally written by Glenn Engel, Lake Stevens Instrument Division
5 * Contributed by HP Systems
7 * Modified for SPARC by Stu Grossman, Cygnus Support.
9 * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
10 * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
12 * Copyright (C) 1995 Andreas Busse
14 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
15 * Modified for Linux/mn10300 by David Howells <dhowells@redhat.com>
19 * To enable debugger support, two things need to happen. One, a
20 * call to set_debug_traps() is necessary in order to allow any breakpoints
21 * or error conditions to be properly intercepted and reported to gdb.
22 * Two, a breakpoint needs to be generated to begin communication. This
23 * is most easily accomplished by a call to breakpoint(). Breakpoint()
24 * simulates a breakpoint by executing a BREAK instruction.
27 * The following gdb commands are supported:
29 * command function Return value
31 * g return the value of the CPU registers hex data or ENN
32 * G set the value of the CPU registers OK or ENN
34 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
35 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
37 * c Resume at current address SNN ( signal NN)
38 * cAA..AA Continue at address AA..AA SNN
40 * s Step one instruction SNN
41 * sAA..AA Step one instruction from AA..AA SNN
45 * ? What was the last sigval ? SNN (signal NN)
47 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
50 * All commands and responses are sent with a packet which includes a
51 * checksum. A packet consists of
53 * $<packet info>#<checksum>.
56 * <packet info> :: <characters representing the command or response>
57 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
59 * When a packet is received, it is first acknowledged with either '+' or '-'.
60 * '+' indicates a successful transfer. '-' indicates a failed transfer.
65 * $m0,10#2a +$00010203040506070809101112131415#42
72 * For reference -- the following are the steps that one
73 * company took (RidgeRun Inc) to get remote gdb debugging
74 * going. In this scenario the host machine was a PC and the
75 * target platform was a Galileo EVB64120A MIPS evaluation
79 * First download gdb-5.0.tar.gz from the internet.
80 * and then build/install the package.
83 * $ tar zxf gdb-5.0.tar.gz
85 * $ ./configure --target=am33_2.0-linux-gnu
88 * am33_2.0-linux-gnu-gdb
91 * Configure linux for remote debugging and build it.
95 * $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
96 * $ make dep; make vmlinux
99 * Download the kernel to the remote target and start
100 * the kernel running. It will promptly halt and wait
101 * for the host gdb session to connect. It does this
102 * since the "Kernel Hacking" option has defined
103 * CONFIG_REMOTE_DEBUG which in turn enables your calls
109 * Start the gdb session on the host.
112 * $ am33_2.0-linux-gnu-gdb vmlinux
113 * (gdb) set remotebaud 115200
114 * (gdb) target remote /dev/ttyS1
115 * ...at this point you are connected to
116 * the remote target and can use gdb
117 * in the normal fasion. Setting
118 * breakpoints, single stepping,
119 * printing variables, etc.
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>
132 #include <asm/pgtable.h>
133 #include <asm/system.h>
134 #include <asm/gdb-stub.h>
135 #include <asm/exceptions.h>
136 #include <asm/cacheflush.h>
137 #include <asm/serial-regs.h>
138 #include <asm/busctl-regs.h>
139 #include <asm/unit/leds.h>
140 #include <asm/unit/serial.h>
142 /* define to use F7F7 rather than FF which is subverted by JTAG debugger */
143 #undef GDBSTUB_USE_F7F7_AS_BREAKPOINT
146 * BUFMAX defines the maximum number of characters in inbound/outbound buffers
147 * at least NUMREGBYTES*2 are needed for register packets
151 static const char gdbstub_banner
[] =
152 "Linux/MN10300 GDB Stub (c) RedHat 2007\n";
154 u8 gdbstub_rx_buffer
[PAGE_SIZE
] __attribute__((aligned(PAGE_SIZE
)));
158 u8 gdbstub_rx_overflow
;
161 static u8 gdbstub_flush_caches
;
162 static char input_buffer
[BUFMAX
];
163 static char output_buffer
[BUFMAX
];
164 static char trans_buffer
[BUFMAX
];
166 static const char hexchars
[] = "0123456789abcdef";
168 struct gdbstub_bkpt
{
169 u8
*addr
; /* address of breakpoint */
170 u8 len
; /* size of breakpoint */
171 u8 origbytes
[7]; /* original bytes */
174 static struct gdbstub_bkpt gdbstub_bkpts
[256];
179 static void getpacket(char *buffer
);
180 static int putpacket(char *buffer
);
181 static int computeSignal(enum exception_code excep
);
182 static int hex(unsigned char ch
);
183 static int hexToInt(char **ptr
, int *intValue
);
184 static unsigned char *mem2hex(const void *mem
, char *buf
, int count
,
186 static const char *hex2mem(const char *buf
, void *_mem
, int count
,
190 * Convert ch from a hex digit to an int
192 static int hex(unsigned char ch
)
194 if (ch
>= 'a' && ch
<= 'f')
195 return ch
- 'a' + 10;
196 if (ch
>= '0' && ch
<= '9')
198 if (ch
>= 'A' && ch
<= 'F')
199 return ch
- 'A' + 10;
203 #ifdef CONFIG_GDBSTUB_DEBUGGING
205 void debug_to_serial(const char *p
, int n
)
207 __debug_to_serial(p
, n
);
208 /* gdbstub_console_write(NULL, p, n); */
211 void gdbstub_printk(const char *fmt
, ...)
216 /* Emit the output into the temporary buffer */
218 len
= vsnprintf(trans_buffer
, sizeof(trans_buffer
), fmt
, args
);
220 debug_to_serial(trans_buffer
, len
);
225 static inline char *gdbstub_strcpy(char *dst
, const char *src
)
228 while ((dst
[loop
] = src
[loop
]))
234 * scan for the sequence $<data>#<checksum>
236 static void getpacket(char *buffer
)
238 unsigned char checksum
;
239 unsigned char xmitcsum
;
241 int count
, i
, ret
, error
;
245 * wait around for the start character,
246 * ignore all other characters
249 gdbstub_io_rx_char(&ch
, 0);
258 * now, read until a # or end of buffer is found
260 while (count
< BUFMAX
) {
261 ret
= gdbstub_io_rx_char(&ch
, 0);
273 gdbstub_proto("### GDB Rx Error - Skipping packet"
275 gdbstub_proto("### GDB Tx NAK\n");
276 gdbstub_io_tx_char('-');
280 if (count
>= BUFMAX
|| error
)
285 /* read the checksum */
286 ret
= gdbstub_io_rx_char(&ch
, 0);
289 xmitcsum
= hex(ch
) << 4;
291 ret
= gdbstub_io_rx_char(&ch
, 0);
298 gdbstub_io("### GDB Rx Error -"
299 " Skipping packet\n");
300 gdbstub_io("### GDB Tx NAK\n");
301 gdbstub_io_tx_char('-');
305 /* check the checksum */
306 if (checksum
!= xmitcsum
) {
307 gdbstub_io("### GDB Tx NAK\n");
308 gdbstub_io_tx_char('-'); /* failed checksum */
312 gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer
, checksum
);
313 gdbstub_io("### GDB Tx ACK\n");
314 gdbstub_io_tx_char('+'); /* successful transfer */
317 * if a sequence char is present,
318 * reply the sequence ID
320 if (buffer
[2] == ':') {
321 gdbstub_io_tx_char(buffer
[0]);
322 gdbstub_io_tx_char(buffer
[1]);
325 * remove sequence chars from buffer
328 while (buffer
[count
])
330 for (i
= 3; i
<= count
; i
++)
331 buffer
[i
- 3] = buffer
[i
];
339 * send the packet in buffer.
340 * - return 0 if successfully ACK'd
341 * - return 1 if abandoned due to new incoming packet
343 static int putpacket(char *buffer
)
345 unsigned char checksum
;
350 * $<packet info>#<checksum>.
352 gdbstub_proto("### GDB Tx $'%s'#?? ###\n", buffer
);
355 gdbstub_io_tx_char('$');
359 while ((ch
= buffer
[count
]) != 0) {
360 gdbstub_io_tx_char(ch
);
365 gdbstub_io_tx_char('#');
366 gdbstub_io_tx_char(hexchars
[checksum
>> 4]);
367 gdbstub_io_tx_char(hexchars
[checksum
& 0xf]);
369 } while (gdbstub_io_rx_char(&ch
, 0),
370 ch
== '-' && (gdbstub_io("### GDB Rx NAK\n"), 0),
371 ch
!= '-' && ch
!= '+' &&
372 (gdbstub_io("### GDB Rx ??? %02x\n", ch
), 0),
373 ch
!= '+' && ch
!= '$');
376 gdbstub_io("### GDB Rx ACK\n");
380 gdbstub_io("### GDB Tx Abandoned\n");
381 gdbstub_rx_unget
= ch
;
386 * While we find nice hex chars, build an int.
387 * Return number of chars processed.
389 static int hexToInt(char **ptr
, int *intValue
)
397 hexValue
= hex(**ptr
);
401 *intValue
= (*intValue
<< 4) | hexValue
;
411 * We single-step by setting breakpoints. When an exception
412 * is handled, we need to restore the instructions hoisted
413 * when the breakpoints were set.
415 * This is where we save the original instructions.
417 static struct gdb_bp_save
{
422 static const unsigned char gdbstub_insn_sizes
[256] =
424 /* 1 2 3 4 5 6 7 8 9 a b c d e f */
425 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, /* 0 */
426 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 1 */
427 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, /* 2 */
428 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, /* 3 */
429 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, /* 4 */
430 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, /* 5 */
431 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */
432 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */
433 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 8 */
434 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 9 */
435 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* a */
436 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* b */
437 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, /* c */
438 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
439 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */
440 0, 2, 2, 2, 2, 2, 2, 4, 0, 3, 0, 4, 0, 6, 7, 1 /* f */
443 static int __gdbstub_mark_bp(u8
*addr
, int ix
)
445 if (addr
< (u8
*) 0x70000000UL
)
447 /* 70000000-7fffffff: vmalloc area */
448 if (addr
< (u8
*) 0x80000000UL
)
450 if (addr
< (u8
*) 0x8c000000UL
)
452 /* 8c000000-93ffffff: SRAM, SDRAM */
453 if (addr
< (u8
*) 0x94000000UL
)
458 if (gdbstub_read_byte(addr
+ 0, &step_bp
[ix
].opcode
[0]) < 0 ||
459 gdbstub_read_byte(addr
+ 1, &step_bp
[ix
].opcode
[1]) < 0)
462 step_bp
[ix
].addr
= addr
;
466 static inline void __gdbstub_restore_bp(void)
468 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
469 if (step_bp
[0].addr
) {
470 gdbstub_write_byte(step_bp
[0].opcode
[0], step_bp
[0].addr
+ 0);
471 gdbstub_write_byte(step_bp
[0].opcode
[1], step_bp
[0].addr
+ 1);
473 if (step_bp
[1].addr
) {
474 gdbstub_write_byte(step_bp
[1].opcode
[0], step_bp
[1].addr
+ 0);
475 gdbstub_write_byte(step_bp
[1].opcode
[1], step_bp
[1].addr
+ 1);
479 gdbstub_write_byte(step_bp
[0].opcode
[0], step_bp
[0].addr
+ 0);
481 gdbstub_write_byte(step_bp
[1].opcode
[0], step_bp
[1].addr
+ 0);
484 gdbstub_flush_caches
= 1;
486 step_bp
[0].addr
= NULL
;
487 step_bp
[0].opcode
[0] = 0;
488 step_bp
[0].opcode
[1] = 0;
489 step_bp
[1].addr
= NULL
;
490 step_bp
[1].opcode
[0] = 0;
491 step_bp
[1].opcode
[1] = 0;
495 * emulate single stepping by means of breakpoint instructions
497 static int gdbstub_single_step(struct pt_regs
*regs
)
501 uint8_t cur
, *pc
, *sp
;
503 step_bp
[0].addr
= NULL
;
504 step_bp
[0].opcode
[0] = 0;
505 step_bp
[0].opcode
[1] = 0;
506 step_bp
[1].addr
= NULL
;
507 step_bp
[1].opcode
[0] = 0;
508 step_bp
[1].opcode
[1] = 0;
511 pc
= (u8
*) regs
->pc
;
512 sp
= (u8
*) (regs
+ 1);
513 if (gdbstub_read_byte(pc
, &cur
) < 0)
516 gdbstub_bkpt("Single Step from %p { %02x }\n", pc
, cur
);
518 gdbstub_flush_caches
= 1;
520 size
= gdbstub_insn_sizes
[cur
];
522 if (!__gdbstub_mark_bp(pc
+ size
, 0))
538 if (gdbstub_read_byte(pc
+ 1, (u8
*) &x
) < 0)
540 if (!__gdbstub_mark_bp(pc
+ 2, 0))
542 if ((x
< 0 || x
> 2) &&
543 !__gdbstub_mark_bp(pc
+ (s8
) x
, 1))
559 if (!__gdbstub_mark_bp(pc
+ 1, 0))
561 if (regs
->pc
!= regs
->lar
&&
562 !__gdbstub_mark_bp((u8
*) regs
->lar
, 1))
566 /* SETLB - loads the next for bytes into the LIR
569 if (!__gdbstub_mark_bp(pc
+ 1, 0))
573 /* JMP (d16,PC) or CALL (d16,PC) */
576 if (gdbstub_read_byte(pc
+ 1, ((u8
*) &x
) + 0) < 0 ||
577 gdbstub_read_byte(pc
+ 2, ((u8
*) &x
) + 1) < 0)
579 if (!__gdbstub_mark_bp(pc
+ (s16
) x
, 0))
583 /* JMP (d32,PC) or CALL (d32,PC) */
586 if (gdbstub_read_byte(pc
+ 1, ((u8
*) &x
) + 0) < 0 ||
587 gdbstub_read_byte(pc
+ 2, ((u8
*) &x
) + 1) < 0 ||
588 gdbstub_read_byte(pc
+ 3, ((u8
*) &x
) + 2) < 0 ||
589 gdbstub_read_byte(pc
+ 4, ((u8
*) &x
) + 3) < 0)
591 if (!__gdbstub_mark_bp(pc
+ (s32
) x
, 0))
597 if (!__gdbstub_mark_bp((u8
*) regs
->mdr
, 0))
603 if (gdbstub_read_byte(pc
+ 2, (u8
*) &x
) < 0)
606 if (gdbstub_read_byte(sp
+ 0, ((u8
*) &x
) + 0) < 0 ||
607 gdbstub_read_byte(sp
+ 1, ((u8
*) &x
) + 1) < 0 ||
608 gdbstub_read_byte(sp
+ 2, ((u8
*) &x
) + 2) < 0 ||
609 gdbstub_read_byte(sp
+ 3, ((u8
*) &x
) + 3) < 0)
611 if (!__gdbstub_mark_bp((u8
*) x
, 0))
616 if (gdbstub_read_byte(pc
+ 1, &cur
) < 0)
619 if (cur
>= 0xf0 && cur
<= 0xf7) {
620 /* JMP (An) / CALLS (An) */
622 case 0: x
= regs
->a0
; break;
623 case 1: x
= regs
->a1
; break;
624 case 2: x
= regs
->a2
; break;
625 case 3: x
= regs
->a3
; break;
627 if (!__gdbstub_mark_bp((u8
*) x
, 0))
629 } else if (cur
== 0xfc) {
631 if (gdbstub_read_byte(
632 sp
+ 0, ((u8
*) &x
) + 0) < 0 ||
634 sp
+ 1, ((u8
*) &x
) + 1) < 0 ||
636 sp
+ 2, ((u8
*) &x
) + 2) < 0 ||
638 sp
+ 3, ((u8
*) &x
) + 3) < 0)
640 if (!__gdbstub_mark_bp((u8
*) x
, 0))
642 } else if (cur
== 0xfd) {
644 if (gdbstub_read_byte(
645 sp
+ 4, ((u8
*) &x
) + 0) < 0 ||
647 sp
+ 5, ((u8
*) &x
) + 1) < 0 ||
649 sp
+ 6, ((u8
*) &x
) + 2) < 0 ||
651 sp
+ 7, ((u8
*) &x
) + 3) < 0)
653 if (!__gdbstub_mark_bp((u8
*) x
, 0))
656 if (!__gdbstub_mark_bp(pc
+ 2, 0))
662 /* potential 3-byte conditional branches */
664 if (gdbstub_read_byte(pc
+ 1, &cur
) < 0)
666 if (!__gdbstub_mark_bp(pc
+ 3, 0))
669 if (cur
>= 0xe8 && cur
<= 0xeb) {
670 if (gdbstub_read_byte(
671 pc
+ 2, ((u8
*) &x
) + 0) < 0)
673 if ((x
< 0 || x
> 3) &&
674 !__gdbstub_mark_bp(pc
+ (s8
) x
, 1))
680 if (gdbstub_read_byte(pc
+ 1, &cur
) < 0)
685 if (gdbstub_read_byte(
686 pc
+ 2, ((u8
*) &x
) + 0) < 0 ||
688 pc
+ 3, ((u8
*) &x
) + 1) < 0)
690 if (!__gdbstub_mark_bp(pc
+ (s16
) x
, 0))
693 if (!__gdbstub_mark_bp(pc
+ 4, 0))
699 if (gdbstub_read_byte(pc
+ 1, &cur
) < 0)
703 if (gdbstub_read_byte(
704 pc
+ 2, ((u8
*) &x
) + 0) < 0 ||
706 pc
+ 3, ((u8
*) &x
) + 1) < 0 ||
708 pc
+ 4, ((u8
*) &x
) + 2) < 0 ||
710 pc
+ 5, ((u8
*) &x
) + 3) < 0)
712 if (!__gdbstub_mark_bp(
716 if (!__gdbstub_mark_bp(
725 gdbstub_bkpt("Step: %02x at %p; %02x at %p\n",
726 step_bp
[0].opcode
[0], step_bp
[0].addr
,
727 step_bp
[1].opcode
[0], step_bp
[1].addr
);
729 if (step_bp
[0].addr
) {
730 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
731 if (gdbstub_write_byte(0xF7, step_bp
[0].addr
+ 0) < 0 ||
732 gdbstub_write_byte(0xF7, step_bp
[0].addr
+ 1) < 0)
735 if (gdbstub_write_byte(0xFF, step_bp
[0].addr
+ 0) < 0)
740 if (step_bp
[1].addr
) {
741 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
742 if (gdbstub_write_byte(0xF7, step_bp
[1].addr
+ 0) < 0 ||
743 gdbstub_write_byte(0xF7, step_bp
[1].addr
+ 1) < 0)
746 if (gdbstub_write_byte(0xFF, step_bp
[1].addr
+ 0) < 0)
754 /* uh-oh - silly address alert, try and restore things */
755 __gdbstub_restore_bp();
759 #ifdef CONFIG_GDBSTUB_CONSOLE
761 void gdbstub_console_write(struct console
*con
, const char *p
, unsigned n
)
763 static const char gdbstub_cr
[] = { 0x0d };
776 while (n
> 0 && qty
< 20) {
777 mem2hex(p
, outbuf
+ qty
, 2, 0);
780 mem2hex(gdbstub_cr
, outbuf
+ qty
, 2, 0);
794 static kdev_t
gdbstub_console_dev(struct console
*con
)
796 return MKDEV(1, 3); /* /dev/null */
799 static struct console gdbstub_console
= {
801 .write
= gdbstub_console_write
,
802 .device
= gdbstub_console_dev
,
803 .flags
= CON_PRINTBUFFER
,
810 * Convert the memory pointed to by mem into hex, placing result in buf.
811 * - if successful, return a pointer to the last char put in buf (NUL)
812 * - in case of mem fault, return NULL
813 * may_fault is non-zero if we are reading from arbitrary memory, but is
814 * currently not used.
817 unsigned char *mem2hex(const void *_mem
, char *buf
, int count
, int may_fault
)
819 const u8
*mem
= _mem
;
822 if ((u32
) mem
& 1 && count
>= 1) {
823 if (gdbstub_read_byte(mem
, ch
) != 0)
825 *buf
++ = hexchars
[ch
[0] >> 4];
826 *buf
++ = hexchars
[ch
[0] & 0xf];
831 if ((u32
) mem
& 3 && count
>= 2) {
832 if (gdbstub_read_word(mem
, ch
) != 0)
834 *buf
++ = hexchars
[ch
[0] >> 4];
835 *buf
++ = hexchars
[ch
[0] & 0xf];
836 *buf
++ = hexchars
[ch
[1] >> 4];
837 *buf
++ = hexchars
[ch
[1] & 0xf];
843 if (gdbstub_read_dword(mem
, ch
) != 0)
845 *buf
++ = hexchars
[ch
[0] >> 4];
846 *buf
++ = hexchars
[ch
[0] & 0xf];
847 *buf
++ = hexchars
[ch
[1] >> 4];
848 *buf
++ = hexchars
[ch
[1] & 0xf];
849 *buf
++ = hexchars
[ch
[2] >> 4];
850 *buf
++ = hexchars
[ch
[2] & 0xf];
851 *buf
++ = hexchars
[ch
[3] >> 4];
852 *buf
++ = hexchars
[ch
[3] & 0xf];
858 if (gdbstub_read_word(mem
, ch
) != 0)
860 *buf
++ = hexchars
[ch
[0] >> 4];
861 *buf
++ = hexchars
[ch
[0] & 0xf];
862 *buf
++ = hexchars
[ch
[1] >> 4];
863 *buf
++ = hexchars
[ch
[1] & 0xf];
869 if (gdbstub_read_byte(mem
, ch
) != 0)
871 *buf
++ = hexchars
[ch
[0] >> 4];
872 *buf
++ = hexchars
[ch
[0] & 0xf];
880 * convert the hex array pointed to by buf into binary to be placed in mem
881 * return a pointer to the character AFTER the last byte written
882 * may_fault is non-zero if we are reading from arbitrary memory, but is
883 * currently not used.
886 const char *hex2mem(const char *buf
, void *_mem
, int count
, int may_fault
)
894 if ((u32
) mem
& 1 && count
>= 1) {
895 ch
.b
[0] = hex(*buf
++) << 4;
896 ch
.b
[0] |= hex(*buf
++);
897 if (gdbstub_write_byte(ch
.val
, mem
) != 0)
903 if ((u32
) mem
& 3 && count
>= 2) {
904 ch
.b
[0] = hex(*buf
++) << 4;
905 ch
.b
[0] |= hex(*buf
++);
906 ch
.b
[1] = hex(*buf
++) << 4;
907 ch
.b
[1] |= hex(*buf
++);
908 if (gdbstub_write_word(ch
.val
, mem
) != 0)
915 ch
.b
[0] = hex(*buf
++) << 4;
916 ch
.b
[0] |= hex(*buf
++);
917 ch
.b
[1] = hex(*buf
++) << 4;
918 ch
.b
[1] |= hex(*buf
++);
919 ch
.b
[2] = hex(*buf
++) << 4;
920 ch
.b
[2] |= hex(*buf
++);
921 ch
.b
[3] = hex(*buf
++) << 4;
922 ch
.b
[3] |= hex(*buf
++);
923 if (gdbstub_write_dword(ch
.val
, mem
) != 0)
930 ch
.b
[0] = hex(*buf
++) << 4;
931 ch
.b
[0] |= hex(*buf
++);
932 ch
.b
[1] = hex(*buf
++) << 4;
933 ch
.b
[1] |= hex(*buf
++);
934 if (gdbstub_write_word(ch
.val
, mem
) != 0)
941 ch
.b
[0] = hex(*buf
++) << 4;
942 ch
.b
[0] |= hex(*buf
++);
943 if (gdbstub_write_byte(ch
.val
, mem
) != 0)
951 * This table contains the mapping between MN10300 exception codes, and
952 * signals, which are primarily what GDB understands. It also indicates
953 * which hardware traps we need to commandeer when initializing the stub.
955 static const struct excep_to_sig_map
{
956 enum exception_code excep
; /* MN10300 exception code */
957 unsigned char signo
; /* Signal that we map this into */
958 } excep_to_sig_map
[] = {
959 { EXCEP_ITLBMISS
, SIGSEGV
},
960 { EXCEP_DTLBMISS
, SIGSEGV
},
961 { EXCEP_TRAP
, SIGTRAP
},
962 { EXCEP_ISTEP
, SIGTRAP
},
963 { EXCEP_IBREAK
, SIGTRAP
},
964 { EXCEP_OBREAK
, SIGTRAP
},
965 { EXCEP_UNIMPINS
, SIGILL
},
966 { EXCEP_UNIMPEXINS
, SIGILL
},
967 { EXCEP_MEMERR
, SIGSEGV
},
968 { EXCEP_MISALIGN
, SIGSEGV
},
969 { EXCEP_BUSERROR
, SIGBUS
},
970 { EXCEP_ILLINSACC
, SIGSEGV
},
971 { EXCEP_ILLDATACC
, SIGSEGV
},
972 { EXCEP_IOINSACC
, SIGSEGV
},
973 { EXCEP_PRIVINSACC
, SIGSEGV
},
974 { EXCEP_PRIVDATACC
, SIGSEGV
},
975 { EXCEP_FPU_DISABLED
, SIGFPE
},
976 { EXCEP_FPU_UNIMPINS
, SIGFPE
},
977 { EXCEP_FPU_OPERATION
, SIGFPE
},
978 { EXCEP_WDT
, SIGALRM
},
979 { EXCEP_NMI
, SIGQUIT
},
980 { EXCEP_IRQ_LEVEL0
, SIGINT
},
981 { EXCEP_IRQ_LEVEL1
, SIGINT
},
982 { EXCEP_IRQ_LEVEL2
, SIGINT
},
983 { EXCEP_IRQ_LEVEL3
, SIGINT
},
984 { EXCEP_IRQ_LEVEL4
, SIGINT
},
985 { EXCEP_IRQ_LEVEL5
, SIGINT
},
986 { EXCEP_IRQ_LEVEL6
, SIGINT
},
991 * convert the MN10300 exception code into a UNIX signal number
993 static int computeSignal(enum exception_code excep
)
995 const struct excep_to_sig_map
*map
;
997 for (map
= excep_to_sig_map
; map
->signo
; map
++)
998 if (map
->excep
== excep
)
1001 return SIGHUP
; /* default for things we don't know about */
1004 static u32 gdbstub_fpcr
, gdbstub_fpufs_array
[32];
1009 static void gdbstub_store_fpu(void)
1015 #ifdef CONFIG_MN10300_PROC_MN103E010
1030 "fmov fs10, (a1+)\n"
1031 "fmov fs11, (a1+)\n"
1032 "fmov fs12, (a1+)\n"
1033 "fmov fs13, (a1+)\n"
1034 "fmov fs14, (a1+)\n"
1035 "fmov fs15, (a1+)\n"
1036 "fmov fs16, (a1+)\n"
1037 "fmov fs17, (a1+)\n"
1038 "fmov fs18, (a1+)\n"
1039 "fmov fs19, (a1+)\n"
1040 "fmov fs20, (a1+)\n"
1041 "fmov fs21, (a1+)\n"
1042 "fmov fs22, (a1+)\n"
1043 "fmov fs23, (a1+)\n"
1044 "fmov fs24, (a1+)\n"
1045 "fmov fs25, (a1+)\n"
1046 "fmov fs26, (a1+)\n"
1047 "fmov fs27, (a1+)\n"
1048 "fmov fs28, (a1+)\n"
1049 "fmov fs29, (a1+)\n"
1050 "fmov fs30, (a1+)\n"
1051 "fmov fs31, (a1+)\n"
1053 : "=d"(gdbstub_fpcr
)
1054 : "g" (&gdbstub_fpufs_array
), "i"(EPSW_FE
)
1063 static void gdbstub_load_fpu(void)
1069 #ifdef CONFIG_MN10300_PROC_MN103E010
1084 "fmov (a1+), fs10\n"
1085 "fmov (a1+), fs11\n"
1086 "fmov (a1+), fs12\n"
1087 "fmov (a1+), fs13\n"
1088 "fmov (a1+), fs14\n"
1089 "fmov (a1+), fs15\n"
1090 "fmov (a1+), fs16\n"
1091 "fmov (a1+), fs17\n"
1092 "fmov (a1+), fs18\n"
1093 "fmov (a1+), fs19\n"
1094 "fmov (a1+), fs20\n"
1095 "fmov (a1+), fs21\n"
1096 "fmov (a1+), fs22\n"
1097 "fmov (a1+), fs23\n"
1098 "fmov (a1+), fs24\n"
1099 "fmov (a1+), fs25\n"
1100 "fmov (a1+), fs26\n"
1101 "fmov (a1+), fs27\n"
1102 "fmov (a1+), fs28\n"
1103 "fmov (a1+), fs29\n"
1104 "fmov (a1+), fs30\n"
1105 "fmov (a1+), fs31\n"
1108 : "g" (&gdbstub_fpufs_array
), "i"(EPSW_FE
), "d"(gdbstub_fpcr
)
1115 * set a software breakpoint
1117 int gdbstub_set_breakpoint(u8
*addr
, int len
)
1119 int bkpt
, loop
, xloop
;
1121 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1122 len
= (len
+ 1) & ~1;
1125 gdbstub_bkpt("setbkpt(%p,%d)\n", addr
, len
);
1127 for (bkpt
= 255; bkpt
>= 0; bkpt
--)
1128 if (!gdbstub_bkpts
[bkpt
].addr
)
1133 for (loop
= 0; loop
< len
; loop
++)
1134 if (gdbstub_read_byte(&addr
[loop
],
1135 &gdbstub_bkpts
[bkpt
].origbytes
[loop
]
1139 gdbstub_flush_caches
= 1;
1141 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1142 for (loop
= 0; loop
< len
; loop
++)
1143 if (gdbstub_write_byte(0xF7, &addr
[loop
]) < 0)
1146 for (loop
= 0; loop
< len
; loop
++)
1147 if (gdbstub_write_byte(0xFF, &addr
[loop
]) < 0)
1151 gdbstub_bkpts
[bkpt
].addr
= addr
;
1152 gdbstub_bkpts
[bkpt
].len
= len
;
1154 gdbstub_bkpt("Set BKPT[%02x]: %p-%p {%02x%02x%02x%02x%02x%02x%02x}\n",
1156 gdbstub_bkpts
[bkpt
].addr
,
1157 gdbstub_bkpts
[bkpt
].addr
+ gdbstub_bkpts
[bkpt
].len
- 1,
1158 gdbstub_bkpts
[bkpt
].origbytes
[0],
1159 gdbstub_bkpts
[bkpt
].origbytes
[1],
1160 gdbstub_bkpts
[bkpt
].origbytes
[2],
1161 gdbstub_bkpts
[bkpt
].origbytes
[3],
1162 gdbstub_bkpts
[bkpt
].origbytes
[4],
1163 gdbstub_bkpts
[bkpt
].origbytes
[5],
1164 gdbstub_bkpts
[bkpt
].origbytes
[6]
1170 for (xloop
= 0; xloop
< loop
; xloop
++)
1171 gdbstub_write_byte(gdbstub_bkpts
[bkpt
].origbytes
[xloop
],
1177 * clear a software breakpoint
1179 int gdbstub_clear_breakpoint(u8
*addr
, int len
)
1183 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1184 len
= (len
+ 1) & ~1;
1187 gdbstub_bkpt("clearbkpt(%p,%d)\n", addr
, len
);
1189 for (bkpt
= 255; bkpt
>= 0; bkpt
--)
1190 if (gdbstub_bkpts
[bkpt
].addr
== addr
&&
1191 gdbstub_bkpts
[bkpt
].len
== len
)
1196 gdbstub_bkpts
[bkpt
].addr
= NULL
;
1198 gdbstub_flush_caches
= 1;
1200 for (loop
= 0; loop
< len
; loop
++)
1201 if (gdbstub_write_byte(gdbstub_bkpts
[bkpt
].origbytes
[loop
],
1209 * This function does all command processing for interfacing to gdb
1210 * - returns 1 if the exception should be skipped, 0 otherwise.
1212 static int gdbstub(struct pt_regs
*regs
, enum exception_code excep
)
1214 unsigned long *stack
;
1215 unsigned long epsw
, mdr
;
1224 if (excep
== EXCEP_FPU_DISABLED
)
1227 gdbstub_flush_caches
= 0;
1229 mn10300_set_gdbleds(1);
1231 asm volatile("mov mdr,%0" : "=d"(mdr
));
1232 asm volatile("mov epsw,%0" : "=d"(epsw
));
1233 asm volatile("mov %0,epsw"
1234 :: "d"((epsw
& ~EPSW_IM
) | EPSW_IE
| EPSW_IM_1
));
1236 gdbstub_store_fpu();
1238 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1239 /* skip the initial pause loop */
1240 if (regs
->pc
== (unsigned long) __gdbstub_pause
)
1241 regs
->pc
= (unsigned long) start_kernel
;
1244 /* if we were single stepping, restore the opcodes hoisted for the
1247 if ((step_bp
[0].addr
&& step_bp
[0].addr
== (u8
*) regs
->pc
) ||
1248 (step_bp
[1].addr
&& step_bp
[1].addr
== (u8
*) regs
->pc
))
1251 __gdbstub_restore_bp();
1253 if (gdbstub_rx_unget
) {
1255 if (gdbstub_rx_unget
!= 3)
1256 goto packet_waiting
;
1257 gdbstub_rx_unget
= 0;
1260 stack
= (unsigned long *) regs
->sp
;
1261 sigval
= broke
? SIGTRAP
: computeSignal(excep
);
1263 /* send information about a BUG() */
1264 if (!user_mode(regs
) && excep
== EXCEP_SYSCALL15
) {
1265 const struct bug_entry
*bug
;
1267 bug
= find_bug(regs
->pc
);
1270 length
= snprintf(trans_buffer
, sizeof(trans_buffer
),
1271 "BUG() at address %lx\n", regs
->pc
);
1275 length
= snprintf(trans_buffer
, sizeof(trans_buffer
),
1276 "BUG() at address %lx (%s:%d)\n",
1277 regs
->pc
, bug
->file
, bug
->line
);
1280 ptr
= output_buffer
;
1282 ptr
= mem2hex(trans_buffer
, ptr
, length
, 0);
1284 putpacket(output_buffer
);
1288 } else if (regs
->pc
== (unsigned long) __gdbstub_bug_trap
) {
1289 regs
->pc
= regs
->mdr
;
1294 * send a message to the debugger's user saying what happened if it may
1295 * not be clear cut (we can't map exceptions onto signals properly)
1297 if (sigval
!= SIGINT
&& sigval
!= SIGTRAP
&& sigval
!= SIGILL
) {
1298 static const char title
[] = "Excep ", tbcberr
[] = "BCBERR ";
1299 static const char crlf
[] = "\r\n";
1301 u32 bcberr
= BCBERR
;
1303 ptr
= output_buffer
;
1305 ptr
= mem2hex(title
, ptr
, sizeof(title
) - 1, 0);
1307 hx
= hexchars
[(excep
& 0xf000) >> 12];
1308 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1309 hx
= hexchars
[(excep
& 0x0f00) >> 8];
1310 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1311 hx
= hexchars
[(excep
& 0x00f0) >> 4];
1312 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1313 hx
= hexchars
[(excep
& 0x000f)];
1314 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1316 ptr
= mem2hex(crlf
, ptr
, sizeof(crlf
) - 1, 0);
1318 putpacket(output_buffer
); /* send it off... */
1321 ptr
= output_buffer
;
1323 ptr
= mem2hex(tbcberr
, ptr
, sizeof(tbcberr
) - 1, 0);
1325 hx
= hexchars
[(bcberr
& 0xf0000000) >> 28];
1326 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1327 hx
= hexchars
[(bcberr
& 0x0f000000) >> 24];
1328 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1329 hx
= hexchars
[(bcberr
& 0x00f00000) >> 20];
1330 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1331 hx
= hexchars
[(bcberr
& 0x000f0000) >> 16];
1332 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1333 hx
= hexchars
[(bcberr
& 0x0000f000) >> 12];
1334 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1335 hx
= hexchars
[(bcberr
& 0x00000f00) >> 8];
1336 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1337 hx
= hexchars
[(bcberr
& 0x000000f0) >> 4];
1338 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1339 hx
= hexchars
[(bcberr
& 0x0000000f)];
1340 *ptr
++ = hexchars
[hx
>> 4]; *ptr
++ = hexchars
[hx
& 0xf];
1342 ptr
= mem2hex(crlf
, ptr
, sizeof(crlf
) - 1, 0);
1344 putpacket(output_buffer
); /* send it off... */
1348 * tell the debugger that an exception has occurred
1350 ptr
= output_buffer
;
1353 * Send trap type (converted to signal)
1356 *ptr
++ = hexchars
[sigval
>> 4];
1357 *ptr
++ = hexchars
[sigval
& 0xf];
1362 *ptr
++ = hexchars
[GDB_REGID_PC
>> 4];
1363 *ptr
++ = hexchars
[GDB_REGID_PC
& 0xf];
1365 ptr
= mem2hex(®s
->pc
, ptr
, 4, 0);
1369 * Send frame pointer
1371 *ptr
++ = hexchars
[GDB_REGID_FP
>> 4];
1372 *ptr
++ = hexchars
[GDB_REGID_FP
& 0xf];
1374 ptr
= mem2hex(®s
->a3
, ptr
, 4, 0);
1378 * Send stack pointer
1380 ssp
= (unsigned long) (regs
+ 1);
1381 *ptr
++ = hexchars
[GDB_REGID_SP
>> 4];
1382 *ptr
++ = hexchars
[GDB_REGID_SP
& 0xf];
1384 ptr
= mem2hex(&ssp
, ptr
, 4, 0);
1388 putpacket(output_buffer
); /* send it off... */
1392 * Wait for input from remote GDB
1395 output_buffer
[0] = 0;
1396 getpacket(input_buffer
);
1398 switch (input_buffer
[0]) {
1399 /* request repeat of last signal number */
1401 output_buffer
[0] = 'S';
1402 output_buffer
[1] = hexchars
[sigval
>> 4];
1403 output_buffer
[2] = hexchars
[sigval
& 0xf];
1404 output_buffer
[3] = 0;
1408 /* toggle debug flag */
1412 * Return the value of the CPU registers
1416 ssp
= (u32
) (regs
+ 1);
1417 ptr
= output_buffer
;
1418 ptr
= mem2hex(®s
->d0
, ptr
, 4, 0);
1419 ptr
= mem2hex(®s
->d1
, ptr
, 4, 0);
1420 ptr
= mem2hex(®s
->d2
, ptr
, 4, 0);
1421 ptr
= mem2hex(®s
->d3
, ptr
, 4, 0);
1422 ptr
= mem2hex(®s
->a0
, ptr
, 4, 0);
1423 ptr
= mem2hex(®s
->a1
, ptr
, 4, 0);
1424 ptr
= mem2hex(®s
->a2
, ptr
, 4, 0);
1425 ptr
= mem2hex(®s
->a3
, ptr
, 4, 0);
1427 ptr
= mem2hex(&ssp
, ptr
, 4, 0); /* 8 */
1428 ptr
= mem2hex(®s
->pc
, ptr
, 4, 0);
1429 ptr
= mem2hex(®s
->mdr
, ptr
, 4, 0);
1430 ptr
= mem2hex(®s
->epsw
, ptr
, 4, 0);
1431 ptr
= mem2hex(®s
->lir
, ptr
, 4, 0);
1432 ptr
= mem2hex(®s
->lar
, ptr
, 4, 0);
1433 ptr
= mem2hex(®s
->mdrq
, ptr
, 4, 0);
1435 ptr
= mem2hex(®s
->e0
, ptr
, 4, 0); /* 15 */
1436 ptr
= mem2hex(®s
->e1
, ptr
, 4, 0);
1437 ptr
= mem2hex(®s
->e2
, ptr
, 4, 0);
1438 ptr
= mem2hex(®s
->e3
, ptr
, 4, 0);
1439 ptr
= mem2hex(®s
->e4
, ptr
, 4, 0);
1440 ptr
= mem2hex(®s
->e5
, ptr
, 4, 0);
1441 ptr
= mem2hex(®s
->e6
, ptr
, 4, 0);
1442 ptr
= mem2hex(®s
->e7
, ptr
, 4, 0);
1444 ptr
= mem2hex(&ssp
, ptr
, 4, 0);
1445 ptr
= mem2hex(®s
, ptr
, 4, 0);
1446 ptr
= mem2hex(®s
->sp
, ptr
, 4, 0);
1447 ptr
= mem2hex(®s
->mcrh
, ptr
, 4, 0); /* 26 */
1448 ptr
= mem2hex(®s
->mcrl
, ptr
, 4, 0);
1449 ptr
= mem2hex(®s
->mcvf
, ptr
, 4, 0);
1451 ptr
= mem2hex(&gdbstub_fpcr
, ptr
, 4, 0); /* 29 - FPCR */
1452 ptr
= mem2hex(&zero
, ptr
, 4, 0);
1453 ptr
= mem2hex(&zero
, ptr
, 4, 0);
1454 for (loop
= 0; loop
< 32; loop
++)
1455 ptr
= mem2hex(&gdbstub_fpufs_array
[loop
],
1456 ptr
, 4, 0); /* 32 - FS0-31 */
1461 * set the value of the CPU registers - return OK
1467 ptr
= &input_buffer
[1];
1468 ptr
= hex2mem(ptr
, ®s
->d0
, 4, 0);
1469 ptr
= hex2mem(ptr
, ®s
->d1
, 4, 0);
1470 ptr
= hex2mem(ptr
, ®s
->d2
, 4, 0);
1471 ptr
= hex2mem(ptr
, ®s
->d3
, 4, 0);
1472 ptr
= hex2mem(ptr
, ®s
->a0
, 4, 0);
1473 ptr
= hex2mem(ptr
, ®s
->a1
, 4, 0);
1474 ptr
= hex2mem(ptr
, ®s
->a2
, 4, 0);
1475 ptr
= hex2mem(ptr
, ®s
->a3
, 4, 0);
1477 ptr
= hex2mem(ptr
, &ssp
, 4, 0); /* 8 */
1478 ptr
= hex2mem(ptr
, ®s
->pc
, 4, 0);
1479 ptr
= hex2mem(ptr
, ®s
->mdr
, 4, 0);
1480 ptr
= hex2mem(ptr
, ®s
->epsw
, 4, 0);
1481 ptr
= hex2mem(ptr
, ®s
->lir
, 4, 0);
1482 ptr
= hex2mem(ptr
, ®s
->lar
, 4, 0);
1483 ptr
= hex2mem(ptr
, ®s
->mdrq
, 4, 0);
1485 ptr
= hex2mem(ptr
, ®s
->e0
, 4, 0); /* 15 */
1486 ptr
= hex2mem(ptr
, ®s
->e1
, 4, 0);
1487 ptr
= hex2mem(ptr
, ®s
->e2
, 4, 0);
1488 ptr
= hex2mem(ptr
, ®s
->e3
, 4, 0);
1489 ptr
= hex2mem(ptr
, ®s
->e4
, 4, 0);
1490 ptr
= hex2mem(ptr
, ®s
->e5
, 4, 0);
1491 ptr
= hex2mem(ptr
, ®s
->e6
, 4, 0);
1492 ptr
= hex2mem(ptr
, ®s
->e7
, 4, 0);
1494 ptr
= hex2mem(ptr
, &ssp
, 4, 0);
1495 ptr
= hex2mem(ptr
, &zero
, 4, 0);
1496 ptr
= hex2mem(ptr
, ®s
->sp
, 4, 0);
1497 ptr
= hex2mem(ptr
, ®s
->mcrh
, 4, 0); /* 26 */
1498 ptr
= hex2mem(ptr
, ®s
->mcrl
, 4, 0);
1499 ptr
= hex2mem(ptr
, ®s
->mcvf
, 4, 0);
1501 ptr
= hex2mem(ptr
, &zero
, 4, 0); /* 29 - FPCR */
1502 ptr
= hex2mem(ptr
, &zero
, 4, 0);
1503 ptr
= hex2mem(ptr
, &zero
, 4, 0);
1504 for (loop
= 0; loop
< 32; loop
++) /* 32 - FS0-31 */
1505 ptr
= hex2mem(ptr
, &zero
, 4, 0);
1509 * See if the stack pointer has moved. If so, then copy
1510 * the saved locals and ins to the new location.
1512 unsigned long *newsp
= (unsigned long *) registers
[SP
];
1514 sp
= memcpy(newsp
, sp
, 16 * 4);
1517 gdbstub_strcpy(output_buffer
, "OK");
1522 * mAA..AA,LLLL Read LLLL bytes at address AA..AA
1525 ptr
= &input_buffer
[1];
1527 if (hexToInt(&ptr
, &addr
) &&
1529 hexToInt(&ptr
, &length
)
1531 if (mem2hex((char *) addr
, output_buffer
,
1534 gdbstub_strcpy(output_buffer
, "E03");
1536 gdbstub_strcpy(output_buffer
, "E01");
1541 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA
1545 ptr
= &input_buffer
[1];
1547 if (hexToInt(&ptr
, &addr
) &&
1549 hexToInt(&ptr
, &length
) &&
1552 if (hex2mem(ptr
, (char *) addr
, length
, 1))
1553 gdbstub_strcpy(output_buffer
, "OK");
1555 gdbstub_strcpy(output_buffer
, "E03");
1557 gdbstub_flush_caches
= 1;
1559 gdbstub_strcpy(output_buffer
, "E02");
1564 * cAA..AA Continue at address AA..AA(optional)
1567 /* try to read optional parameter, pc unchanged if no
1570 ptr
= &input_buffer
[1];
1571 if (hexToInt(&ptr
, &addr
))
1579 goto done
; /* just continue */
1582 * Reset the whole machine (FIXME: system dependent)
1588 * Step to next instruction
1592 * using the T flag doesn't seem to perform single
1593 * stepping (it seems to wind up being caught by the
1594 * JTAG unit), so we have to use breakpoints and
1597 if (gdbstub_single_step(regs
) < 0)
1598 /* ignore any fault error for now */
1599 gdbstub_printk("unable to set single-step"
1604 * Set baud rate (bBB)
1610 ptr
= &input_buffer
[1];
1611 if (!hexToInt(&ptr
, &baudrate
)) {
1612 gdbstub_strcpy(output_buffer
, "B01");
1617 /* ACK before changing speed */
1619 gdbstub_io_set_baud(baudrate
);
1628 ptr
= &input_buffer
[1];
1630 if (!hexToInt(&ptr
, &loop
) || *ptr
++ != ',' ||
1631 !hexToInt(&ptr
, &addr
) || *ptr
++ != ',' ||
1632 !hexToInt(&ptr
, &length
)
1634 gdbstub_strcpy(output_buffer
, "E01");
1638 /* only support software breakpoints */
1639 gdbstub_strcpy(output_buffer
, "E03");
1643 (unsigned long) addr
< 4096)
1646 if (gdbstub_set_breakpoint((u8
*) addr
, length
) < 0)
1649 gdbstub_strcpy(output_buffer
, "OK");
1656 ptr
= &input_buffer
[1];
1658 if (!hexToInt(&ptr
, &loop
) || *ptr
++ != ',' ||
1659 !hexToInt(&ptr
, &addr
) || *ptr
++ != ',' ||
1660 !hexToInt(&ptr
, &length
)
1662 gdbstub_strcpy(output_buffer
, "E01");
1666 /* only support software breakpoints */
1667 gdbstub_strcpy(output_buffer
, "E03");
1671 (unsigned long) addr
< 4096)
1674 if (gdbstub_clear_breakpoint((u8
*) addr
, length
) < 0)
1677 gdbstub_strcpy(output_buffer
, "OK");
1681 gdbstub_proto("### GDB Unsupported Cmd '%s'\n",
1686 /* reply to the request */
1687 putpacket(output_buffer
);
1692 * Need to flush the instruction cache here, as we may
1693 * have deposited a breakpoint, and the icache probably
1694 * has no way of knowing that a data ref to some location
1695 * may have changed something that is in the instruction
1697 * NB: We flush both caches, just to be sure...
1699 if (gdbstub_flush_caches
)
1700 gdbstub_purge_cache();
1703 mn10300_set_gdbleds(0);
1704 if (excep
== EXCEP_NMI
)
1707 touch_softlockup_watchdog();
1709 local_irq_restore(epsw
);
1714 * handle event interception
1716 asmlinkage
int gdbstub_intercept(struct pt_regs
*regs
,
1717 enum exception_code excep
)
1719 static u8 notfirst
= 1;
1723 gdbstub_printk("--> gdbstub reentered itself\n");
1728 asm("mov mdr,%0" : "=d"(mdr
));
1731 "--> gdbstub_intercept(%p,%04x) [MDR=%lx PC=%lx]\n",
1732 regs
, excep
, mdr
, regs
->pc
);
1735 "PC: %08lx EPSW: %08lx SSP: %08lx mode: %s\n",
1736 regs
->pc
, regs
->epsw
, (unsigned long) &ret
,
1737 user_mode(regs
) ? "User" : "Super");
1739 "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
1740 regs
->d0
, regs
->d1
, regs
->d2
, regs
->d3
);
1742 "a0: %08lx a1: %08lx a2: %08lx a3: %08lx\n",
1743 regs
->a0
, regs
->a1
, regs
->a2
, regs
->a3
);
1745 "e0: %08lx e1: %08lx e2: %08lx e3: %08lx\n",
1746 regs
->e0
, regs
->e1
, regs
->e2
, regs
->e3
);
1748 "e4: %08lx e5: %08lx e6: %08lx e7: %08lx\n",
1749 regs
->e4
, regs
->e5
, regs
->e6
, regs
->e7
);
1751 "lar: %08lx lir: %08lx mdr: %08lx usp: %08lx\n",
1752 regs
->lar
, regs
->lir
, regs
->mdr
, regs
->sp
);
1754 "cvf: %08lx crl: %08lx crh: %08lx drq: %08lx\n",
1755 regs
->mcvf
, regs
->mcrl
, regs
->mcrh
, regs
->mdrq
);
1757 "threadinfo=%p task=%p)\n",
1758 current_thread_info(), current
);
1763 ret
= gdbstub(regs
, excep
);
1765 gdbstub_entry("<-- gdbstub_intercept()\n");
1771 * handle the GDB stub itself causing an exception
1773 asmlinkage
void gdbstub_exception(struct pt_regs
*regs
,
1774 enum exception_code excep
)
1778 asm("mov mdr,%0" : "=d"(mdr
));
1779 gdbstub_entry("--> gdbstub exception({%p},%04x) [MDR=%lx]\n",
1782 while ((unsigned long) regs
== 0xffffffff) {}
1784 /* handle guarded memory accesses where we know it might fault */
1785 if (regs
->pc
== (unsigned) gdbstub_read_byte_guard
) {
1786 regs
->pc
= (unsigned) gdbstub_read_byte_cont
;
1790 if (regs
->pc
== (unsigned) gdbstub_read_word_guard
) {
1791 regs
->pc
= (unsigned) gdbstub_read_word_cont
;
1795 if (regs
->pc
== (unsigned) gdbstub_read_dword_guard
) {
1796 regs
->pc
= (unsigned) gdbstub_read_dword_cont
;
1800 if (regs
->pc
== (unsigned) gdbstub_write_byte_guard
) {
1801 regs
->pc
= (unsigned) gdbstub_write_byte_cont
;
1805 if (regs
->pc
== (unsigned) gdbstub_write_word_guard
) {
1806 regs
->pc
= (unsigned) gdbstub_write_word_cont
;
1810 if (regs
->pc
== (unsigned) gdbstub_write_dword_guard
) {
1811 regs
->pc
= (unsigned) gdbstub_write_dword_cont
;
1815 gdbstub_printk("\n### GDB stub caused an exception ###\n");
1817 /* something went horribly wrong */
1819 show_registers(regs
);
1821 panic("GDB Stub caused an unexpected exception - can't continue\n");
1823 /* we caught an attempt by the stub to access silly memory */
1825 gdbstub_entry("<-- gdbstub exception() = EFAULT\n");
1831 * send an exit message to GDB
1833 void gdbstub_exit(int status
)
1835 unsigned char checksum
;
1840 output_buffer
[0] = 'W';
1841 output_buffer
[1] = hexchars
[(status
>> 4) & 0x0F];
1842 output_buffer
[2] = hexchars
[status
& 0x0F];
1843 output_buffer
[3] = 0;
1845 gdbstub_io_tx_char('$');
1849 while ((ch
= output_buffer
[count
]) != 0) {
1850 gdbstub_io_tx_char(ch
);
1855 gdbstub_io_tx_char('#');
1856 gdbstub_io_tx_char(hexchars
[checksum
>> 4]);
1857 gdbstub_io_tx_char(hexchars
[checksum
& 0xf]);
1859 /* make sure the output is flushed, or else RedBoot might clobber it */
1860 gdbstub_io_tx_flush();
1866 * initialise the GDB stub
1868 asmlinkage
void __init
gdbstub_init(void)
1870 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1877 printk(KERN_INFO
"%s", gdbstub_banner
);
1881 gdbstub_entry("--> gdbstub_init\n");
1883 /* try to talk to GDB (or anyone insane enough to want to type GDB
1884 * protocol by hand) */
1885 gdbstub_io("### GDB Tx ACK\n");
1886 gdbstub_io_tx_char('+'); /* 'hello world' */
1888 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1889 gdbstub_printk("GDB Stub waiting for packet\n");
1891 /* in case GDB is started before us, ACK any packets that are already
1892 * sitting there (presumably "$?#xx")
1894 do { gdbstub_io_rx_char(&ch
, 0); } while (ch
!= '$');
1895 do { gdbstub_io_rx_char(&ch
, 0); } while (ch
!= '#');
1896 /* eat first csum byte */
1897 do { ret
= gdbstub_io_rx_char(&ch
, 0); } while (ret
!= 0);
1898 /* eat second csum byte */
1899 do { ret
= gdbstub_io_rx_char(&ch
, 0); } while (ret
!= 0);
1901 gdbstub_io("### GDB Tx NAK\n");
1902 gdbstub_io_tx_char('-'); /* NAK it */
1905 printk("GDB Stub ready\n");
1909 gdbstub_entry("<-- gdbstub_init\n");
1913 * register the console at a more appropriate time
1915 #ifdef CONFIG_GDBSTUB_CONSOLE
1916 static int __init
gdbstub_postinit(void)
1918 printk(KERN_NOTICE
"registering console\n");
1919 register_console(&gdbstub_console
);
1923 __initcall(gdbstub_postinit
);
1927 * handle character reception on GDB serial port
1928 * - jump into the GDB stub if BREAK is detected on the serial line
1930 asmlinkage
void gdbstub_rx_irq(struct pt_regs
*regs
, enum exception_code excep
)
1935 gdbstub_entry("--> gdbstub_rx_irq\n");
1938 ret
= gdbstub_io_rx_char(&ch
, 1);
1939 if (ret
!= -EIO
&& ret
!= -EAGAIN
) {
1941 gdbstub_rx_unget
= ch
;
1942 gdbstub(regs
, excep
);
1944 } while (ret
!= -EAGAIN
);
1946 gdbstub_entry("<-- gdbstub_rx_irq\n");