Blackfin/ipipe: upgrade to I-pipe mainline
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / mn10300 / kernel / gdb-stub.c
blobb169d99d9f20e8fd4b5da5ea353b70f02819844b
1 /* MN10300 GDB stub
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
43 * k kill
45 * ? What was the last sigval ? SNN (signal NN)
47 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
48 * baud rate
50 * All commands and responses are sent with a packet which includes a
51 * checksum. A packet consists of
53 * $<packet info>#<checksum>.
55 * where
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.
62 * Example:
64 * Host: Reply:
65 * $m0,10#2a +$00010203040506070809101112131415#42
68 * ==============
69 * MORE EXAMPLES:
70 * ==============
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
76 * board.
78 * Step 1:
79 * First download gdb-5.0.tar.gz from the internet.
80 * and then build/install the package.
82 * Example:
83 * $ tar zxf gdb-5.0.tar.gz
84 * $ cd gdb-5.0
85 * $ ./configure --target=am33_2.0-linux-gnu
86 * $ make
87 * $ install
88 * am33_2.0-linux-gnu-gdb
90 * Step 2:
91 * Configure linux for remote debugging and build it.
93 * Example:
94 * $ cd ~/linux
95 * $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
96 * $ make dep; make vmlinux
98 * Step 3:
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
104 * to:
105 * set_debug_traps();
106 * breakpoint();
108 * Step 4:
109 * Start the gdb session on the host.
111 * Example:
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 <unit/leds.h>
140 #include <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
149 #define BUFMAX 2048
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)));
155 u32 gdbstub_rx_inp;
156 u32 gdbstub_rx_outp;
157 u8 gdbstub_busy;
158 u8 gdbstub_rx_overflow;
159 u8 gdbstub_rx_unget;
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 struct gdbstub_bkpt {
167 u8 *addr; /* address of breakpoint */
168 u8 len; /* size of breakpoint */
169 u8 origbytes[7]; /* original bytes */
172 static struct gdbstub_bkpt gdbstub_bkpts[256];
175 * local prototypes
177 static void getpacket(char *buffer);
178 static int putpacket(char *buffer);
179 static int computeSignal(enum exception_code excep);
180 static int hex(unsigned char ch);
181 static int hexToInt(char **ptr, int *intValue);
182 static unsigned char *mem2hex(const void *mem, char *buf, int count,
183 int may_fault);
184 static const char *hex2mem(const char *buf, void *_mem, int count,
185 int may_fault);
188 * Convert ch from a hex digit to an int
190 static int hex(unsigned char ch)
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;
201 #ifdef CONFIG_GDBSTUB_DEBUGGING
203 void debug_to_serial(const char *p, int n)
205 __debug_to_serial(p, n);
206 /* gdbstub_console_write(NULL, p, n); */
209 void gdbstub_printk(const char *fmt, ...)
211 va_list args;
212 int len;
214 /* Emit the output into the temporary buffer */
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);
221 #endif
223 static inline char *gdbstub_strcpy(char *dst, const char *src)
225 int loop = 0;
226 while ((dst[loop] = src[loop]))
227 loop++;
228 return dst;
232 * scan for the sequence $<data>#<checksum>
234 static void getpacket(char *buffer)
236 unsigned char checksum;
237 unsigned char xmitcsum;
238 unsigned char ch;
239 int count, i, ret, error;
241 for (;;) {
243 * wait around for the start character,
244 * ignore all other characters
246 do {
247 gdbstub_io_rx_char(&ch, 0);
248 } while (ch != '$');
250 checksum = 0;
251 xmitcsum = -1;
252 count = 0;
253 error = 0;
256 * now, read until a # or end of buffer is found
258 while (count < BUFMAX) {
259 ret = gdbstub_io_rx_char(&ch, 0);
260 if (ret < 0)
261 error = ret;
263 if (ch == '#')
264 break;
265 checksum += ch;
266 buffer[count] = ch;
267 count++;
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;
278 if (count >= BUFMAX || error)
279 continue;
281 buffer[count] = 0;
283 /* read the checksum */
284 ret = gdbstub_io_rx_char(&ch, 0);
285 if (ret < 0)
286 error = ret;
287 xmitcsum = hex(ch) << 4;
289 ret = gdbstub_io_rx_char(&ch, 0);
290 if (ret < 0)
291 error = ret;
292 xmitcsum |= hex(ch);
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;
303 /* check the checksum */
304 if (checksum != xmitcsum) {
305 gdbstub_io("### GDB Tx NAK\n");
306 gdbstub_io_tx_char('-'); /* failed checksum */
307 continue;
310 gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
311 gdbstub_io("### GDB Tx ACK\n");
312 gdbstub_io_tx_char('+'); /* successful transfer */
315 * if a sequence char is present,
316 * reply the sequence ID
318 if (buffer[2] == ':') {
319 gdbstub_io_tx_char(buffer[0]);
320 gdbstub_io_tx_char(buffer[1]);
323 * remove sequence chars from buffer
325 count = 0;
326 while (buffer[count])
327 count++;
328 for (i = 3; i <= count; i++)
329 buffer[i - 3] = buffer[i];
332 break;
337 * send the packet in buffer.
338 * - return 0 if successfully ACK'd
339 * - return 1 if abandoned due to new incoming packet
341 static int putpacket(char *buffer)
343 unsigned char checksum;
344 unsigned char ch;
345 int count;
348 * $<packet info>#<checksum>.
350 gdbstub_proto("### GDB Tx $'%s'#?? ###\n", buffer);
352 do {
353 gdbstub_io_tx_char('$');
354 checksum = 0;
355 count = 0;
357 while ((ch = buffer[count]) != 0) {
358 gdbstub_io_tx_char(ch);
359 checksum += ch;
360 count += 1;
363 gdbstub_io_tx_char('#');
364 gdbstub_io_tx_char(hex_asc_hi(checksum));
365 gdbstub_io_tx_char(hex_asc_lo(checksum));
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 != '$');
373 if (ch == '+') {
374 gdbstub_io("### GDB Rx ACK\n");
375 return 0;
378 gdbstub_io("### GDB Tx Abandoned\n");
379 gdbstub_rx_unget = ch;
380 return 1;
384 * While we find nice hex chars, build an int.
385 * Return number of chars processed.
387 static int hexToInt(char **ptr, int *intValue)
389 int numChars = 0;
390 int hexValue;
392 *intValue = 0;
394 while (**ptr) {
395 hexValue = hex(**ptr);
396 if (hexValue < 0)
397 break;
399 *intValue = (*intValue << 4) | hexValue;
400 numChars++;
402 (*ptr)++;
405 return (numChars);
409 * We single-step by setting breakpoints. When an exception
410 * is handled, we need to restore the instructions hoisted
411 * when the breakpoints were set.
413 * This is where we save the original instructions.
415 static struct gdb_bp_save {
416 u8 *addr;
417 u8 opcode[2];
418 } step_bp[2];
420 static const unsigned char gdbstub_insn_sizes[256] =
422 /* 1 2 3 4 5 6 7 8 9 a b c d e f */
423 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, /* 0 */
424 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 1 */
425 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, /* 2 */
426 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, /* 3 */
427 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, /* 4 */
428 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, /* 5 */
429 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */
430 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */
431 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 8 */
432 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 9 */
433 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* a */
434 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* b */
435 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, /* c */
436 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
437 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */
438 0, 2, 2, 2, 2, 2, 2, 4, 0, 3, 0, 4, 0, 6, 7, 1 /* f */
441 static int __gdbstub_mark_bp(u8 *addr, int ix)
443 /* vmalloc area */
444 if (((u8 *) VMALLOC_START <= addr) && (addr < (u8 *) VMALLOC_END))
445 goto okay;
446 /* SRAM, SDRAM */
447 if (((u8 *) 0x80000000UL <= addr) && (addr < (u8 *) 0xa0000000UL))
448 goto okay;
449 return 0;
451 okay:
452 if (gdbstub_read_byte(addr + 0, &step_bp[ix].opcode[0]) < 0 ||
453 gdbstub_read_byte(addr + 1, &step_bp[ix].opcode[1]) < 0)
454 return 0;
456 step_bp[ix].addr = addr;
457 return 1;
460 static inline void __gdbstub_restore_bp(void)
462 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
463 if (step_bp[0].addr) {
464 gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
465 gdbstub_write_byte(step_bp[0].opcode[1], step_bp[0].addr + 1);
467 if (step_bp[1].addr) {
468 gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
469 gdbstub_write_byte(step_bp[1].opcode[1], step_bp[1].addr + 1);
471 #else
472 if (step_bp[0].addr)
473 gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
474 if (step_bp[1].addr)
475 gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
476 #endif
478 gdbstub_flush_caches = 1;
480 step_bp[0].addr = NULL;
481 step_bp[0].opcode[0] = 0;
482 step_bp[0].opcode[1] = 0;
483 step_bp[1].addr = NULL;
484 step_bp[1].opcode[0] = 0;
485 step_bp[1].opcode[1] = 0;
489 * emulate single stepping by means of breakpoint instructions
491 static int gdbstub_single_step(struct pt_regs *regs)
493 unsigned size;
494 uint32_t x;
495 uint8_t cur, *pc, *sp;
497 step_bp[0].addr = NULL;
498 step_bp[0].opcode[0] = 0;
499 step_bp[0].opcode[1] = 0;
500 step_bp[1].addr = NULL;
501 step_bp[1].opcode[0] = 0;
502 step_bp[1].opcode[1] = 0;
503 x = 0;
505 pc = (u8 *) regs->pc;
506 sp = (u8 *) (regs + 1);
507 if (gdbstub_read_byte(pc, &cur) < 0)
508 return -EFAULT;
510 gdbstub_bkpt("Single Step from %p { %02x }\n", pc, cur);
512 gdbstub_flush_caches = 1;
514 size = gdbstub_insn_sizes[cur];
515 if (size > 0) {
516 if (!__gdbstub_mark_bp(pc + size, 0))
517 goto fault;
518 } else {
519 switch (cur) {
520 /* Bxx (d8,PC) */
521 case 0xc0 ... 0xca:
522 if (gdbstub_read_byte(pc + 1, (u8 *) &x) < 0)
523 goto fault;
524 if (!__gdbstub_mark_bp(pc + 2, 0))
525 goto fault;
526 if ((x < 0 || x > 2) &&
527 !__gdbstub_mark_bp(pc + (s8) x, 1))
528 goto fault;
529 break;
531 /* LXX (d8,PC) */
532 case 0xd0 ... 0xda:
533 if (!__gdbstub_mark_bp(pc + 1, 0))
534 goto fault;
535 if (regs->pc != regs->lar &&
536 !__gdbstub_mark_bp((u8 *) regs->lar, 1))
537 goto fault;
538 break;
540 /* SETLB - loads the next for bytes into the LIR
541 * register */
542 case 0xdb:
543 if (!__gdbstub_mark_bp(pc + 1, 0))
544 goto fault;
545 break;
547 /* JMP (d16,PC) or CALL (d16,PC) */
548 case 0xcc:
549 case 0xcd:
550 if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
551 gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0)
552 goto fault;
553 if (!__gdbstub_mark_bp(pc + (s16) x, 0))
554 goto fault;
555 break;
557 /* JMP (d32,PC) or CALL (d32,PC) */
558 case 0xdc:
559 case 0xdd:
560 if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
561 gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0 ||
562 gdbstub_read_byte(pc + 3, ((u8 *) &x) + 2) < 0 ||
563 gdbstub_read_byte(pc + 4, ((u8 *) &x) + 3) < 0)
564 goto fault;
565 if (!__gdbstub_mark_bp(pc + (s32) x, 0))
566 goto fault;
567 break;
569 /* RETF */
570 case 0xde:
571 if (!__gdbstub_mark_bp((u8 *) regs->mdr, 0))
572 goto fault;
573 break;
575 /* RET */
576 case 0xdf:
577 if (gdbstub_read_byte(pc + 2, (u8 *) &x) < 0)
578 goto fault;
579 sp += (s8)x;
580 if (gdbstub_read_byte(sp + 0, ((u8 *) &x) + 0) < 0 ||
581 gdbstub_read_byte(sp + 1, ((u8 *) &x) + 1) < 0 ||
582 gdbstub_read_byte(sp + 2, ((u8 *) &x) + 2) < 0 ||
583 gdbstub_read_byte(sp + 3, ((u8 *) &x) + 3) < 0)
584 goto fault;
585 if (!__gdbstub_mark_bp((u8 *) x, 0))
586 goto fault;
587 break;
589 case 0xf0:
590 if (gdbstub_read_byte(pc + 1, &cur) < 0)
591 goto fault;
593 if (cur >= 0xf0 && cur <= 0xf7) {
594 /* JMP (An) / CALLS (An) */
595 switch (cur & 3) {
596 case 0: x = regs->a0; break;
597 case 1: x = regs->a1; break;
598 case 2: x = regs->a2; break;
599 case 3: x = regs->a3; break;
601 if (!__gdbstub_mark_bp((u8 *) x, 0))
602 goto fault;
603 } else if (cur == 0xfc) {
604 /* RETS */
605 if (gdbstub_read_byte(
606 sp + 0, ((u8 *) &x) + 0) < 0 ||
607 gdbstub_read_byte(
608 sp + 1, ((u8 *) &x) + 1) < 0 ||
609 gdbstub_read_byte(
610 sp + 2, ((u8 *) &x) + 2) < 0 ||
611 gdbstub_read_byte(
612 sp + 3, ((u8 *) &x) + 3) < 0)
613 goto fault;
614 if (!__gdbstub_mark_bp((u8 *) x, 0))
615 goto fault;
616 } else if (cur == 0xfd) {
617 /* RTI */
618 if (gdbstub_read_byte(
619 sp + 4, ((u8 *) &x) + 0) < 0 ||
620 gdbstub_read_byte(
621 sp + 5, ((u8 *) &x) + 1) < 0 ||
622 gdbstub_read_byte(
623 sp + 6, ((u8 *) &x) + 2) < 0 ||
624 gdbstub_read_byte(
625 sp + 7, ((u8 *) &x) + 3) < 0)
626 goto fault;
627 if (!__gdbstub_mark_bp((u8 *) x, 0))
628 goto fault;
629 } else {
630 if (!__gdbstub_mark_bp(pc + 2, 0))
631 goto fault;
634 break;
636 /* potential 3-byte conditional branches */
637 case 0xf8:
638 if (gdbstub_read_byte(pc + 1, &cur) < 0)
639 goto fault;
640 if (!__gdbstub_mark_bp(pc + 3, 0))
641 goto fault;
643 if (cur >= 0xe8 && cur <= 0xeb) {
644 if (gdbstub_read_byte(
645 pc + 2, ((u8 *) &x) + 0) < 0)
646 goto fault;
647 if ((x < 0 || x > 3) &&
648 !__gdbstub_mark_bp(pc + (s8) x, 1))
649 goto fault;
651 break;
653 case 0xfa:
654 if (gdbstub_read_byte(pc + 1, &cur) < 0)
655 goto fault;
657 if (cur == 0xff) {
658 /* CALLS (d16,PC) */
659 if (gdbstub_read_byte(
660 pc + 2, ((u8 *) &x) + 0) < 0 ||
661 gdbstub_read_byte(
662 pc + 3, ((u8 *) &x) + 1) < 0)
663 goto fault;
664 if (!__gdbstub_mark_bp(pc + (s16) x, 0))
665 goto fault;
666 } else {
667 if (!__gdbstub_mark_bp(pc + 4, 0))
668 goto fault;
670 break;
672 case 0xfc:
673 if (gdbstub_read_byte(pc + 1, &cur) < 0)
674 goto fault;
675 if (cur == 0xff) {
676 /* CALLS (d32,PC) */
677 if (gdbstub_read_byte(
678 pc + 2, ((u8 *) &x) + 0) < 0 ||
679 gdbstub_read_byte(
680 pc + 3, ((u8 *) &x) + 1) < 0 ||
681 gdbstub_read_byte(
682 pc + 4, ((u8 *) &x) + 2) < 0 ||
683 gdbstub_read_byte(
684 pc + 5, ((u8 *) &x) + 3) < 0)
685 goto fault;
686 if (!__gdbstub_mark_bp(
687 pc + (s32) x, 0))
688 goto fault;
689 } else {
690 if (!__gdbstub_mark_bp(
691 pc + 6, 0))
692 goto fault;
694 break;
699 gdbstub_bkpt("Step: %02x at %p; %02x at %p\n",
700 step_bp[0].opcode[0], step_bp[0].addr,
701 step_bp[1].opcode[0], step_bp[1].addr);
703 if (step_bp[0].addr) {
704 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
705 if (gdbstub_write_byte(0xF7, step_bp[0].addr + 0) < 0 ||
706 gdbstub_write_byte(0xF7, step_bp[0].addr + 1) < 0)
707 goto fault;
708 #else
709 if (gdbstub_write_byte(0xFF, step_bp[0].addr + 0) < 0)
710 goto fault;
711 #endif
714 if (step_bp[1].addr) {
715 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
716 if (gdbstub_write_byte(0xF7, step_bp[1].addr + 0) < 0 ||
717 gdbstub_write_byte(0xF7, step_bp[1].addr + 1) < 0)
718 goto fault;
719 #else
720 if (gdbstub_write_byte(0xFF, step_bp[1].addr + 0) < 0)
721 goto fault;
722 #endif
725 return 0;
727 fault:
728 /* uh-oh - silly address alert, try and restore things */
729 __gdbstub_restore_bp();
730 return -EFAULT;
733 #ifdef CONFIG_GDBSTUB_CONSOLE
735 void gdbstub_console_write(struct console *con, const char *p, unsigned n)
737 static const char gdbstub_cr[] = { 0x0d };
738 char outbuf[26];
739 int qty;
740 u8 busy;
742 busy = gdbstub_busy;
743 gdbstub_busy = 1;
745 outbuf[0] = 'O';
747 while (n > 0) {
748 qty = 1;
750 while (n > 0 && qty < 20) {
751 mem2hex(p, outbuf + qty, 2, 0);
752 qty += 2;
753 if (*p == 0x0a) {
754 mem2hex(gdbstub_cr, outbuf + qty, 2, 0);
755 qty += 2;
757 p++;
758 n--;
761 outbuf[qty] = 0;
762 putpacket(outbuf);
765 gdbstub_busy = busy;
768 static kdev_t gdbstub_console_dev(struct console *con)
770 return MKDEV(1, 3); /* /dev/null */
773 static struct console gdbstub_console = {
774 .name = "gdb",
775 .write = gdbstub_console_write,
776 .device = gdbstub_console_dev,
777 .flags = CON_PRINTBUFFER,
778 .index = -1,
781 #endif
784 * Convert the memory pointed to by mem into hex, placing result in buf.
785 * - if successful, return a pointer to the last char put in buf (NUL)
786 * - in case of mem fault, return NULL
787 * may_fault is non-zero if we are reading from arbitrary memory, but is
788 * currently not used.
790 static
791 unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
793 const u8 *mem = _mem;
794 u8 ch[4];
796 if ((u32) mem & 1 && count >= 1) {
797 if (gdbstub_read_byte(mem, ch) != 0)
798 return 0;
799 buf = pack_hex_byte(buf, ch[0]);
800 mem++;
801 count--;
804 if ((u32) mem & 3 && count >= 2) {
805 if (gdbstub_read_word(mem, ch) != 0)
806 return 0;
807 buf = pack_hex_byte(buf, ch[0]);
808 buf = pack_hex_byte(buf, ch[1]);
809 mem += 2;
810 count -= 2;
813 while (count >= 4) {
814 if (gdbstub_read_dword(mem, ch) != 0)
815 return 0;
816 buf = pack_hex_byte(buf, ch[0]);
817 buf = pack_hex_byte(buf, ch[1]);
818 buf = pack_hex_byte(buf, ch[2]);
819 buf = pack_hex_byte(buf, ch[3]);
820 mem += 4;
821 count -= 4;
824 if (count >= 2) {
825 if (gdbstub_read_word(mem, ch) != 0)
826 return 0;
827 buf = pack_hex_byte(buf, ch[0]);
828 buf = pack_hex_byte(buf, ch[1]);
829 mem += 2;
830 count -= 2;
833 if (count >= 1) {
834 if (gdbstub_read_byte(mem, ch) != 0)
835 return 0;
836 buf = pack_hex_byte(buf, ch[0]);
839 *buf = 0;
840 return buf;
844 * convert the hex array pointed to by buf into binary to be placed in mem
845 * return a pointer to the character AFTER the last byte written
846 * may_fault is non-zero if we are reading from arbitrary memory, but is
847 * currently not used.
849 static
850 const char *hex2mem(const char *buf, void *_mem, int count, int may_fault)
852 u8 *mem = _mem;
853 union {
854 u32 val;
855 u8 b[4];
856 } ch;
858 if ((u32) mem & 1 && count >= 1) {
859 ch.b[0] = hex(*buf++) << 4;
860 ch.b[0] |= hex(*buf++);
861 if (gdbstub_write_byte(ch.val, mem) != 0)
862 return 0;
863 mem++;
864 count--;
867 if ((u32) mem & 3 && count >= 2) {
868 ch.b[0] = hex(*buf++) << 4;
869 ch.b[0] |= hex(*buf++);
870 ch.b[1] = hex(*buf++) << 4;
871 ch.b[1] |= hex(*buf++);
872 if (gdbstub_write_word(ch.val, mem) != 0)
873 return 0;
874 mem += 2;
875 count -= 2;
878 while (count >= 4) {
879 ch.b[0] = hex(*buf++) << 4;
880 ch.b[0] |= hex(*buf++);
881 ch.b[1] = hex(*buf++) << 4;
882 ch.b[1] |= hex(*buf++);
883 ch.b[2] = hex(*buf++) << 4;
884 ch.b[2] |= hex(*buf++);
885 ch.b[3] = hex(*buf++) << 4;
886 ch.b[3] |= hex(*buf++);
887 if (gdbstub_write_dword(ch.val, mem) != 0)
888 return 0;
889 mem += 4;
890 count -= 4;
893 if (count >= 2) {
894 ch.b[0] = hex(*buf++) << 4;
895 ch.b[0] |= hex(*buf++);
896 ch.b[1] = hex(*buf++) << 4;
897 ch.b[1] |= hex(*buf++);
898 if (gdbstub_write_word(ch.val, mem) != 0)
899 return 0;
900 mem += 2;
901 count -= 2;
904 if (count >= 1) {
905 ch.b[0] = hex(*buf++) << 4;
906 ch.b[0] |= hex(*buf++);
907 if (gdbstub_write_byte(ch.val, mem) != 0)
908 return 0;
911 return buf;
915 * This table contains the mapping between MN10300 exception codes, and
916 * signals, which are primarily what GDB understands. It also indicates
917 * which hardware traps we need to commandeer when initializing the stub.
919 static const struct excep_to_sig_map {
920 enum exception_code excep; /* MN10300 exception code */
921 unsigned char signo; /* Signal that we map this into */
922 } excep_to_sig_map[] = {
923 { EXCEP_ITLBMISS, SIGSEGV },
924 { EXCEP_DTLBMISS, SIGSEGV },
925 { EXCEP_TRAP, SIGTRAP },
926 { EXCEP_ISTEP, SIGTRAP },
927 { EXCEP_IBREAK, SIGTRAP },
928 { EXCEP_OBREAK, SIGTRAP },
929 { EXCEP_UNIMPINS, SIGILL },
930 { EXCEP_UNIMPEXINS, SIGILL },
931 { EXCEP_MEMERR, SIGSEGV },
932 { EXCEP_MISALIGN, SIGSEGV },
933 { EXCEP_BUSERROR, SIGBUS },
934 { EXCEP_ILLINSACC, SIGSEGV },
935 { EXCEP_ILLDATACC, SIGSEGV },
936 { EXCEP_IOINSACC, SIGSEGV },
937 { EXCEP_PRIVINSACC, SIGSEGV },
938 { EXCEP_PRIVDATACC, SIGSEGV },
939 { EXCEP_FPU_DISABLED, SIGFPE },
940 { EXCEP_FPU_UNIMPINS, SIGFPE },
941 { EXCEP_FPU_OPERATION, SIGFPE },
942 { EXCEP_WDT, SIGALRM },
943 { EXCEP_NMI, SIGQUIT },
944 { EXCEP_IRQ_LEVEL0, SIGINT },
945 { EXCEP_IRQ_LEVEL1, SIGINT },
946 { EXCEP_IRQ_LEVEL2, SIGINT },
947 { EXCEP_IRQ_LEVEL3, SIGINT },
948 { EXCEP_IRQ_LEVEL4, SIGINT },
949 { EXCEP_IRQ_LEVEL5, SIGINT },
950 { EXCEP_IRQ_LEVEL6, SIGINT },
951 { 0, 0}
955 * convert the MN10300 exception code into a UNIX signal number
957 static int computeSignal(enum exception_code excep)
959 const struct excep_to_sig_map *map;
961 for (map = excep_to_sig_map; map->signo; map++)
962 if (map->excep == excep)
963 return map->signo;
965 return SIGHUP; /* default for things we don't know about */
968 static u32 gdbstub_fpcr, gdbstub_fpufs_array[32];
973 static void gdbstub_store_fpu(void)
975 #ifdef CONFIG_FPU
977 asm volatile(
978 "or %2,epsw\n"
979 #ifdef CONFIG_MN10300_PROC_MN103E010
980 "nop\n"
981 "nop\n"
982 #endif
983 "mov %1, a1\n"
984 "fmov fs0, (a1+)\n"
985 "fmov fs1, (a1+)\n"
986 "fmov fs2, (a1+)\n"
987 "fmov fs3, (a1+)\n"
988 "fmov fs4, (a1+)\n"
989 "fmov fs5, (a1+)\n"
990 "fmov fs6, (a1+)\n"
991 "fmov fs7, (a1+)\n"
992 "fmov fs8, (a1+)\n"
993 "fmov fs9, (a1+)\n"
994 "fmov fs10, (a1+)\n"
995 "fmov fs11, (a1+)\n"
996 "fmov fs12, (a1+)\n"
997 "fmov fs13, (a1+)\n"
998 "fmov fs14, (a1+)\n"
999 "fmov fs15, (a1+)\n"
1000 "fmov fs16, (a1+)\n"
1001 "fmov fs17, (a1+)\n"
1002 "fmov fs18, (a1+)\n"
1003 "fmov fs19, (a1+)\n"
1004 "fmov fs20, (a1+)\n"
1005 "fmov fs21, (a1+)\n"
1006 "fmov fs22, (a1+)\n"
1007 "fmov fs23, (a1+)\n"
1008 "fmov fs24, (a1+)\n"
1009 "fmov fs25, (a1+)\n"
1010 "fmov fs26, (a1+)\n"
1011 "fmov fs27, (a1+)\n"
1012 "fmov fs28, (a1+)\n"
1013 "fmov fs29, (a1+)\n"
1014 "fmov fs30, (a1+)\n"
1015 "fmov fs31, (a1+)\n"
1016 "fmov fpcr, %0\n"
1017 : "=d"(gdbstub_fpcr)
1018 : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE)
1019 : "a1"
1021 #endif
1027 static void gdbstub_load_fpu(void)
1029 #ifdef CONFIG_FPU
1031 asm volatile(
1032 "or %1,epsw\n"
1033 #ifdef CONFIG_MN10300_PROC_MN103E010
1034 "nop\n"
1035 "nop\n"
1036 #endif
1037 "mov %0, a1\n"
1038 "fmov (a1+), fs0\n"
1039 "fmov (a1+), fs1\n"
1040 "fmov (a1+), fs2\n"
1041 "fmov (a1+), fs3\n"
1042 "fmov (a1+), fs4\n"
1043 "fmov (a1+), fs5\n"
1044 "fmov (a1+), fs6\n"
1045 "fmov (a1+), fs7\n"
1046 "fmov (a1+), fs8\n"
1047 "fmov (a1+), fs9\n"
1048 "fmov (a1+), fs10\n"
1049 "fmov (a1+), fs11\n"
1050 "fmov (a1+), fs12\n"
1051 "fmov (a1+), fs13\n"
1052 "fmov (a1+), fs14\n"
1053 "fmov (a1+), fs15\n"
1054 "fmov (a1+), fs16\n"
1055 "fmov (a1+), fs17\n"
1056 "fmov (a1+), fs18\n"
1057 "fmov (a1+), fs19\n"
1058 "fmov (a1+), fs20\n"
1059 "fmov (a1+), fs21\n"
1060 "fmov (a1+), fs22\n"
1061 "fmov (a1+), fs23\n"
1062 "fmov (a1+), fs24\n"
1063 "fmov (a1+), fs25\n"
1064 "fmov (a1+), fs26\n"
1065 "fmov (a1+), fs27\n"
1066 "fmov (a1+), fs28\n"
1067 "fmov (a1+), fs29\n"
1068 "fmov (a1+), fs30\n"
1069 "fmov (a1+), fs31\n"
1070 "fmov %2, fpcr\n"
1072 : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE), "d"(gdbstub_fpcr)
1073 : "a1"
1075 #endif
1079 * set a software breakpoint
1081 int gdbstub_set_breakpoint(u8 *addr, int len)
1083 int bkpt, loop, xloop;
1085 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1086 len = (len + 1) & ~1;
1087 #endif
1089 gdbstub_bkpt("setbkpt(%p,%d)\n", addr, len);
1091 for (bkpt = 255; bkpt >= 0; bkpt--)
1092 if (!gdbstub_bkpts[bkpt].addr)
1093 break;
1094 if (bkpt < 0)
1095 return -ENOSPC;
1097 for (loop = 0; loop < len; loop++)
1098 if (gdbstub_read_byte(&addr[loop],
1099 &gdbstub_bkpts[bkpt].origbytes[loop]
1100 ) < 0)
1101 return -EFAULT;
1103 gdbstub_flush_caches = 1;
1105 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1106 for (loop = 0; loop < len; loop++)
1107 if (gdbstub_write_byte(0xF7, &addr[loop]) < 0)
1108 goto restore;
1109 #else
1110 for (loop = 0; loop < len; loop++)
1111 if (gdbstub_write_byte(0xFF, &addr[loop]) < 0)
1112 goto restore;
1113 #endif
1115 gdbstub_bkpts[bkpt].addr = addr;
1116 gdbstub_bkpts[bkpt].len = len;
1118 gdbstub_bkpt("Set BKPT[%02x]: %p-%p {%02x%02x%02x%02x%02x%02x%02x}\n",
1119 bkpt,
1120 gdbstub_bkpts[bkpt].addr,
1121 gdbstub_bkpts[bkpt].addr + gdbstub_bkpts[bkpt].len - 1,
1122 gdbstub_bkpts[bkpt].origbytes[0],
1123 gdbstub_bkpts[bkpt].origbytes[1],
1124 gdbstub_bkpts[bkpt].origbytes[2],
1125 gdbstub_bkpts[bkpt].origbytes[3],
1126 gdbstub_bkpts[bkpt].origbytes[4],
1127 gdbstub_bkpts[bkpt].origbytes[5],
1128 gdbstub_bkpts[bkpt].origbytes[6]
1131 return 0;
1133 restore:
1134 for (xloop = 0; xloop < loop; xloop++)
1135 gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[xloop],
1136 addr + xloop);
1137 return -EFAULT;
1141 * clear a software breakpoint
1143 int gdbstub_clear_breakpoint(u8 *addr, int len)
1145 int bkpt, loop;
1147 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1148 len = (len + 1) & ~1;
1149 #endif
1151 gdbstub_bkpt("clearbkpt(%p,%d)\n", addr, len);
1153 for (bkpt = 255; bkpt >= 0; bkpt--)
1154 if (gdbstub_bkpts[bkpt].addr == addr &&
1155 gdbstub_bkpts[bkpt].len == len)
1156 break;
1157 if (bkpt < 0)
1158 return -ENOENT;
1160 gdbstub_bkpts[bkpt].addr = NULL;
1162 gdbstub_flush_caches = 1;
1164 for (loop = 0; loop < len; loop++)
1165 if (gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[loop],
1166 addr + loop) < 0)
1167 return -EFAULT;
1169 return 0;
1173 * This function does all command processing for interfacing to gdb
1174 * - returns 1 if the exception should be skipped, 0 otherwise.
1176 static int gdbstub(struct pt_regs *regs, enum exception_code excep)
1178 unsigned long *stack;
1179 unsigned long epsw, mdr;
1180 uint32_t zero, ssp;
1181 uint8_t broke;
1182 char *ptr;
1183 int sigval;
1184 int addr;
1185 int length;
1186 int loop;
1188 if (excep == EXCEP_FPU_DISABLED)
1189 return 0;
1191 gdbstub_flush_caches = 0;
1193 mn10300_set_gdbleds(1);
1195 asm volatile("mov mdr,%0" : "=d"(mdr));
1196 local_save_flags(epsw);
1197 arch_local_change_intr_mask_level(
1198 NUM2EPSW_IM(CONFIG_GDBSTUB_IRQ_LEVEL + 1));
1200 gdbstub_store_fpu();
1202 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1203 /* skip the initial pause loop */
1204 if (regs->pc == (unsigned long) __gdbstub_pause)
1205 regs->pc = (unsigned long) start_kernel;
1206 #endif
1208 /* if we were single stepping, restore the opcodes hoisted for the
1209 * breakpoint[s] */
1210 broke = 0;
1211 if ((step_bp[0].addr && step_bp[0].addr == (u8 *) regs->pc) ||
1212 (step_bp[1].addr && step_bp[1].addr == (u8 *) regs->pc))
1213 broke = 1;
1215 __gdbstub_restore_bp();
1217 if (gdbstub_rx_unget) {
1218 sigval = SIGINT;
1219 if (gdbstub_rx_unget != 3)
1220 goto packet_waiting;
1221 gdbstub_rx_unget = 0;
1224 stack = (unsigned long *) regs->sp;
1225 sigval = broke ? SIGTRAP : computeSignal(excep);
1227 /* send information about a BUG() */
1228 if (!user_mode(regs) && excep == EXCEP_SYSCALL15) {
1229 const struct bug_entry *bug;
1231 bug = find_bug(regs->pc);
1232 if (bug)
1233 goto found_bug;
1234 length = snprintf(trans_buffer, sizeof(trans_buffer),
1235 "BUG() at address %lx\n", regs->pc);
1236 goto send_bug_pkt;
1238 found_bug:
1239 length = snprintf(trans_buffer, sizeof(trans_buffer),
1240 "BUG() at address %lx (%s:%d)\n",
1241 regs->pc, bug->file, bug->line);
1243 send_bug_pkt:
1244 ptr = output_buffer;
1245 *ptr++ = 'O';
1246 ptr = mem2hex(trans_buffer, ptr, length, 0);
1247 *ptr = 0;
1248 putpacket(output_buffer);
1250 regs->pc -= 2;
1251 sigval = SIGABRT;
1252 } else if (regs->pc == (unsigned long) __gdbstub_bug_trap) {
1253 regs->pc = regs->mdr;
1254 sigval = SIGABRT;
1258 * send a message to the debugger's user saying what happened if it may
1259 * not be clear cut (we can't map exceptions onto signals properly)
1261 if (sigval != SIGINT && sigval != SIGTRAP && sigval != SIGILL) {
1262 static const char title[] = "Excep ", tbcberr[] = "BCBERR ";
1263 static const char crlf[] = "\r\n";
1264 char hx;
1265 u32 bcberr = BCBERR;
1267 ptr = output_buffer;
1268 *ptr++ = 'O';
1269 ptr = mem2hex(title, ptr, sizeof(title) - 1, 0);
1271 hx = hex_asc_hi(excep >> 8);
1272 ptr = pack_hex_byte(ptr, hx);
1273 hx = hex_asc_lo(excep >> 8);
1274 ptr = pack_hex_byte(ptr, hx);
1275 hx = hex_asc_hi(excep);
1276 ptr = pack_hex_byte(ptr, hx);
1277 hx = hex_asc_lo(excep);
1278 ptr = pack_hex_byte(ptr, hx);
1280 ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
1281 *ptr = 0;
1282 putpacket(output_buffer); /* send it off... */
1284 /* BCBERR */
1285 ptr = output_buffer;
1286 *ptr++ = 'O';
1287 ptr = mem2hex(tbcberr, ptr, sizeof(tbcberr) - 1, 0);
1289 hx = hex_asc_hi(bcberr >> 24);
1290 ptr = pack_hex_byte(ptr, hx);
1291 hx = hex_asc_lo(bcberr >> 24);
1292 ptr = pack_hex_byte(ptr, hx);
1293 hx = hex_asc_hi(bcberr >> 16);
1294 ptr = pack_hex_byte(ptr, hx);
1295 hx = hex_asc_lo(bcberr >> 16);
1296 ptr = pack_hex_byte(ptr, hx);
1297 hx = hex_asc_hi(bcberr >> 8);
1298 ptr = pack_hex_byte(ptr, hx);
1299 hx = hex_asc_lo(bcberr >> 8);
1300 ptr = pack_hex_byte(ptr, hx);
1301 hx = hex_asc_hi(bcberr);
1302 ptr = pack_hex_byte(ptr, hx);
1303 hx = hex_asc_lo(bcberr);
1304 ptr = pack_hex_byte(ptr, hx);
1306 ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
1307 *ptr = 0;
1308 putpacket(output_buffer); /* send it off... */
1312 * tell the debugger that an exception has occurred
1314 ptr = output_buffer;
1317 * Send trap type (converted to signal)
1319 *ptr++ = 'T';
1320 ptr = pack_hex_byte(ptr, sigval);
1323 * Send Error PC
1325 ptr = pack_hex_byte(ptr, GDB_REGID_PC);
1326 *ptr++ = ':';
1327 ptr = mem2hex(&regs->pc, ptr, 4, 0);
1328 *ptr++ = ';';
1331 * Send frame pointer
1333 ptr = pack_hex_byte(ptr, GDB_REGID_FP);
1334 *ptr++ = ':';
1335 ptr = mem2hex(&regs->a3, ptr, 4, 0);
1336 *ptr++ = ';';
1339 * Send stack pointer
1341 ssp = (unsigned long) (regs + 1);
1342 ptr = pack_hex_byte(ptr, GDB_REGID_SP);
1343 *ptr++ = ':';
1344 ptr = mem2hex(&ssp, ptr, 4, 0);
1345 *ptr++ = ';';
1347 *ptr++ = 0;
1348 putpacket(output_buffer); /* send it off... */
1350 packet_waiting:
1352 * Wait for input from remote GDB
1354 while (1) {
1355 output_buffer[0] = 0;
1356 getpacket(input_buffer);
1358 switch (input_buffer[0]) {
1359 /* request repeat of last signal number */
1360 case '?':
1361 output_buffer[0] = 'S';
1362 output_buffer[1] = hex_asc_hi(sigval);
1363 output_buffer[2] = hex_asc_lo(sigval);
1364 output_buffer[3] = 0;
1365 break;
1367 case 'd':
1368 /* toggle debug flag */
1369 break;
1372 * Return the value of the CPU registers
1374 case 'g':
1375 zero = 0;
1376 ssp = (u32) (regs + 1);
1377 ptr = output_buffer;
1378 ptr = mem2hex(&regs->d0, ptr, 4, 0);
1379 ptr = mem2hex(&regs->d1, ptr, 4, 0);
1380 ptr = mem2hex(&regs->d2, ptr, 4, 0);
1381 ptr = mem2hex(&regs->d3, ptr, 4, 0);
1382 ptr = mem2hex(&regs->a0, ptr, 4, 0);
1383 ptr = mem2hex(&regs->a1, ptr, 4, 0);
1384 ptr = mem2hex(&regs->a2, ptr, 4, 0);
1385 ptr = mem2hex(&regs->a3, ptr, 4, 0);
1387 ptr = mem2hex(&ssp, ptr, 4, 0); /* 8 */
1388 ptr = mem2hex(&regs->pc, ptr, 4, 0);
1389 ptr = mem2hex(&regs->mdr, ptr, 4, 0);
1390 ptr = mem2hex(&regs->epsw, ptr, 4, 0);
1391 ptr = mem2hex(&regs->lir, ptr, 4, 0);
1392 ptr = mem2hex(&regs->lar, ptr, 4, 0);
1393 ptr = mem2hex(&regs->mdrq, ptr, 4, 0);
1395 ptr = mem2hex(&regs->e0, ptr, 4, 0); /* 15 */
1396 ptr = mem2hex(&regs->e1, ptr, 4, 0);
1397 ptr = mem2hex(&regs->e2, ptr, 4, 0);
1398 ptr = mem2hex(&regs->e3, ptr, 4, 0);
1399 ptr = mem2hex(&regs->e4, ptr, 4, 0);
1400 ptr = mem2hex(&regs->e5, ptr, 4, 0);
1401 ptr = mem2hex(&regs->e6, ptr, 4, 0);
1402 ptr = mem2hex(&regs->e7, ptr, 4, 0);
1404 ptr = mem2hex(&ssp, ptr, 4, 0);
1405 ptr = mem2hex(&regs, ptr, 4, 0);
1406 ptr = mem2hex(&regs->sp, ptr, 4, 0);
1407 ptr = mem2hex(&regs->mcrh, ptr, 4, 0); /* 26 */
1408 ptr = mem2hex(&regs->mcrl, ptr, 4, 0);
1409 ptr = mem2hex(&regs->mcvf, ptr, 4, 0);
1411 ptr = mem2hex(&gdbstub_fpcr, ptr, 4, 0); /* 29 - FPCR */
1412 ptr = mem2hex(&zero, ptr, 4, 0);
1413 ptr = mem2hex(&zero, ptr, 4, 0);
1414 for (loop = 0; loop < 32; loop++)
1415 ptr = mem2hex(&gdbstub_fpufs_array[loop],
1416 ptr, 4, 0); /* 32 - FS0-31 */
1418 break;
1421 * set the value of the CPU registers - return OK
1423 case 'G':
1425 const char *ptr;
1427 ptr = &input_buffer[1];
1428 ptr = hex2mem(ptr, &regs->d0, 4, 0);
1429 ptr = hex2mem(ptr, &regs->d1, 4, 0);
1430 ptr = hex2mem(ptr, &regs->d2, 4, 0);
1431 ptr = hex2mem(ptr, &regs->d3, 4, 0);
1432 ptr = hex2mem(ptr, &regs->a0, 4, 0);
1433 ptr = hex2mem(ptr, &regs->a1, 4, 0);
1434 ptr = hex2mem(ptr, &regs->a2, 4, 0);
1435 ptr = hex2mem(ptr, &regs->a3, 4, 0);
1437 ptr = hex2mem(ptr, &ssp, 4, 0); /* 8 */
1438 ptr = hex2mem(ptr, &regs->pc, 4, 0);
1439 ptr = hex2mem(ptr, &regs->mdr, 4, 0);
1440 ptr = hex2mem(ptr, &regs->epsw, 4, 0);
1441 ptr = hex2mem(ptr, &regs->lir, 4, 0);
1442 ptr = hex2mem(ptr, &regs->lar, 4, 0);
1443 ptr = hex2mem(ptr, &regs->mdrq, 4, 0);
1445 ptr = hex2mem(ptr, &regs->e0, 4, 0); /* 15 */
1446 ptr = hex2mem(ptr, &regs->e1, 4, 0);
1447 ptr = hex2mem(ptr, &regs->e2, 4, 0);
1448 ptr = hex2mem(ptr, &regs->e3, 4, 0);
1449 ptr = hex2mem(ptr, &regs->e4, 4, 0);
1450 ptr = hex2mem(ptr, &regs->e5, 4, 0);
1451 ptr = hex2mem(ptr, &regs->e6, 4, 0);
1452 ptr = hex2mem(ptr, &regs->e7, 4, 0);
1454 ptr = hex2mem(ptr, &ssp, 4, 0);
1455 ptr = hex2mem(ptr, &zero, 4, 0);
1456 ptr = hex2mem(ptr, &regs->sp, 4, 0);
1457 ptr = hex2mem(ptr, &regs->mcrh, 4, 0); /* 26 */
1458 ptr = hex2mem(ptr, &regs->mcrl, 4, 0);
1459 ptr = hex2mem(ptr, &regs->mcvf, 4, 0);
1461 ptr = hex2mem(ptr, &zero, 4, 0); /* 29 - FPCR */
1462 ptr = hex2mem(ptr, &zero, 4, 0);
1463 ptr = hex2mem(ptr, &zero, 4, 0);
1464 for (loop = 0; loop < 32; loop++) /* 32 - FS0-31 */
1465 ptr = hex2mem(ptr, &zero, 4, 0);
1467 #if 0
1469 * See if the stack pointer has moved. If so, then copy
1470 * the saved locals and ins to the new location.
1472 unsigned long *newsp = (unsigned long *) registers[SP];
1473 if (sp != newsp)
1474 sp = memcpy(newsp, sp, 16 * 4);
1475 #endif
1477 gdbstub_strcpy(output_buffer, "OK");
1479 break;
1482 * mAA..AA,LLLL Read LLLL bytes at address AA..AA
1484 case 'm':
1485 ptr = &input_buffer[1];
1487 if (hexToInt(&ptr, &addr) &&
1488 *ptr++ == ',' &&
1489 hexToInt(&ptr, &length)
1491 if (mem2hex((char *) addr, output_buffer,
1492 length, 1))
1493 break;
1494 gdbstub_strcpy(output_buffer, "E03");
1495 } else {
1496 gdbstub_strcpy(output_buffer, "E01");
1498 break;
1501 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA
1502 * return OK
1504 case 'M':
1505 ptr = &input_buffer[1];
1507 if (hexToInt(&ptr, &addr) &&
1508 *ptr++ == ',' &&
1509 hexToInt(&ptr, &length) &&
1510 *ptr++ == ':'
1512 if (hex2mem(ptr, (char *) addr, length, 1))
1513 gdbstub_strcpy(output_buffer, "OK");
1514 else
1515 gdbstub_strcpy(output_buffer, "E03");
1517 gdbstub_flush_caches = 1;
1518 } else {
1519 gdbstub_strcpy(output_buffer, "E02");
1521 break;
1524 * cAA..AA Continue at address AA..AA(optional)
1526 case 'c':
1527 /* try to read optional parameter, pc unchanged if no
1528 * parm */
1530 ptr = &input_buffer[1];
1531 if (hexToInt(&ptr, &addr))
1532 regs->pc = addr;
1533 goto done;
1536 * kill the program
1538 case 'k' :
1539 goto done; /* just continue */
1542 * Reset the whole machine (FIXME: system dependent)
1544 case 'r':
1545 break;
1548 * Step to next instruction
1550 case 's':
1552 * using the T flag doesn't seem to perform single
1553 * stepping (it seems to wind up being caught by the
1554 * JTAG unit), so we have to use breakpoints and
1555 * continue instead.
1557 if (gdbstub_single_step(regs) < 0)
1558 /* ignore any fault error for now */
1559 gdbstub_printk("unable to set single-step"
1560 " bp\n");
1561 goto done;
1564 * Set baud rate (bBB)
1566 case 'b':
1567 do {
1568 int baudrate;
1570 ptr = &input_buffer[1];
1571 if (!hexToInt(&ptr, &baudrate)) {
1572 gdbstub_strcpy(output_buffer, "B01");
1573 break;
1576 if (baudrate) {
1577 /* ACK before changing speed */
1578 putpacket("OK");
1579 gdbstub_io_set_baud(baudrate);
1581 } while (0);
1582 break;
1585 * Set breakpoint
1587 case 'Z':
1588 ptr = &input_buffer[1];
1590 if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
1591 !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
1592 !hexToInt(&ptr, &length)
1594 gdbstub_strcpy(output_buffer, "E01");
1595 break;
1598 /* only support software breakpoints */
1599 gdbstub_strcpy(output_buffer, "E03");
1600 if (loop != 0 ||
1601 length < 1 ||
1602 length > 7 ||
1603 (unsigned long) addr < 4096)
1604 break;
1606 if (gdbstub_set_breakpoint((u8 *) addr, length) < 0)
1607 break;
1609 gdbstub_strcpy(output_buffer, "OK");
1610 break;
1613 * Clear breakpoint
1615 case 'z':
1616 ptr = &input_buffer[1];
1618 if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
1619 !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
1620 !hexToInt(&ptr, &length)
1622 gdbstub_strcpy(output_buffer, "E01");
1623 break;
1626 /* only support software breakpoints */
1627 gdbstub_strcpy(output_buffer, "E03");
1628 if (loop != 0 ||
1629 length < 1 ||
1630 length > 7 ||
1631 (unsigned long) addr < 4096)
1632 break;
1634 if (gdbstub_clear_breakpoint((u8 *) addr, length) < 0)
1635 break;
1637 gdbstub_strcpy(output_buffer, "OK");
1638 break;
1640 default:
1641 gdbstub_proto("### GDB Unsupported Cmd '%s'\n",
1642 input_buffer);
1643 break;
1646 /* reply to the request */
1647 putpacket(output_buffer);
1650 done:
1652 * Need to flush the instruction cache here, as we may
1653 * have deposited a breakpoint, and the icache probably
1654 * has no way of knowing that a data ref to some location
1655 * may have changed something that is in the instruction
1656 * cache.
1657 * NB: We flush both caches, just to be sure...
1659 if (gdbstub_flush_caches)
1660 gdbstub_purge_cache();
1662 gdbstub_load_fpu();
1663 mn10300_set_gdbleds(0);
1664 if (excep == EXCEP_NMI)
1665 NMICR = NMICR_NMIF;
1667 touch_softlockup_watchdog();
1669 local_irq_restore(epsw);
1670 return 1;
1674 * handle event interception
1676 asmlinkage int gdbstub_intercept(struct pt_regs *regs,
1677 enum exception_code excep)
1679 static u8 notfirst = 1;
1680 int ret;
1682 if (gdbstub_busy)
1683 gdbstub_printk("--> gdbstub reentered itself\n");
1684 gdbstub_busy = 1;
1686 if (notfirst) {
1687 unsigned long mdr;
1688 asm("mov mdr,%0" : "=d"(mdr));
1690 gdbstub_entry(
1691 "--> gdbstub_intercept(%p,%04x) [MDR=%lx PC=%lx]\n",
1692 regs, excep, mdr, regs->pc);
1694 gdbstub_entry(
1695 "PC: %08lx EPSW: %08lx SSP: %08lx mode: %s\n",
1696 regs->pc, regs->epsw, (unsigned long) &ret,
1697 user_mode(regs) ? "User" : "Super");
1698 gdbstub_entry(
1699 "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
1700 regs->d0, regs->d1, regs->d2, regs->d3);
1701 gdbstub_entry(
1702 "a0: %08lx a1: %08lx a2: %08lx a3: %08lx\n",
1703 regs->a0, regs->a1, regs->a2, regs->a3);
1704 gdbstub_entry(
1705 "e0: %08lx e1: %08lx e2: %08lx e3: %08lx\n",
1706 regs->e0, regs->e1, regs->e2, regs->e3);
1707 gdbstub_entry(
1708 "e4: %08lx e5: %08lx e6: %08lx e7: %08lx\n",
1709 regs->e4, regs->e5, regs->e6, regs->e7);
1710 gdbstub_entry(
1711 "lar: %08lx lir: %08lx mdr: %08lx usp: %08lx\n",
1712 regs->lar, regs->lir, regs->mdr, regs->sp);
1713 gdbstub_entry(
1714 "cvf: %08lx crl: %08lx crh: %08lx drq: %08lx\n",
1715 regs->mcvf, regs->mcrl, regs->mcrh, regs->mdrq);
1716 gdbstub_entry(
1717 "threadinfo=%p task=%p)\n",
1718 current_thread_info(), current);
1719 } else {
1720 notfirst = 1;
1723 ret = gdbstub(regs, excep);
1725 gdbstub_entry("<-- gdbstub_intercept()\n");
1726 gdbstub_busy = 0;
1727 return ret;
1731 * handle the GDB stub itself causing an exception
1733 asmlinkage void gdbstub_exception(struct pt_regs *regs,
1734 enum exception_code excep)
1736 unsigned long mdr;
1738 asm("mov mdr,%0" : "=d"(mdr));
1739 gdbstub_entry("--> gdbstub exception({%p},%04x) [MDR=%lx]\n",
1740 regs, excep, mdr);
1742 while ((unsigned long) regs == 0xffffffff) {}
1744 /* handle guarded memory accesses where we know it might fault */
1745 if (regs->pc == (unsigned) gdbstub_read_byte_guard) {
1746 regs->pc = (unsigned) gdbstub_read_byte_cont;
1747 goto fault;
1750 if (regs->pc == (unsigned) gdbstub_read_word_guard) {
1751 regs->pc = (unsigned) gdbstub_read_word_cont;
1752 goto fault;
1755 if (regs->pc == (unsigned) gdbstub_read_dword_guard) {
1756 regs->pc = (unsigned) gdbstub_read_dword_cont;
1757 goto fault;
1760 if (regs->pc == (unsigned) gdbstub_write_byte_guard) {
1761 regs->pc = (unsigned) gdbstub_write_byte_cont;
1762 goto fault;
1765 if (regs->pc == (unsigned) gdbstub_write_word_guard) {
1766 regs->pc = (unsigned) gdbstub_write_word_cont;
1767 goto fault;
1770 if (regs->pc == (unsigned) gdbstub_write_dword_guard) {
1771 regs->pc = (unsigned) gdbstub_write_dword_cont;
1772 goto fault;
1775 gdbstub_printk("\n### GDB stub caused an exception ###\n");
1777 /* something went horribly wrong */
1778 console_verbose();
1779 show_registers(regs);
1781 panic("GDB Stub caused an unexpected exception - can't continue\n");
1783 /* we caught an attempt by the stub to access silly memory */
1784 fault:
1785 gdbstub_entry("<-- gdbstub exception() = EFAULT\n");
1786 regs->d0 = -EFAULT;
1787 return;
1791 * send an exit message to GDB
1793 void gdbstub_exit(int status)
1795 unsigned char checksum;
1796 unsigned char ch;
1797 int count;
1799 gdbstub_busy = 1;
1800 output_buffer[0] = 'W';
1801 output_buffer[1] = hex_asc_hi(status);
1802 output_buffer[2] = hex_asc_lo(status);
1803 output_buffer[3] = 0;
1805 gdbstub_io_tx_char('$');
1806 checksum = 0;
1807 count = 0;
1809 while ((ch = output_buffer[count]) != 0) {
1810 gdbstub_io_tx_char(ch);
1811 checksum += ch;
1812 count += 1;
1815 gdbstub_io_tx_char('#');
1816 gdbstub_io_tx_char(hex_asc_hi(checksum));
1817 gdbstub_io_tx_char(hex_asc_lo(checksum));
1819 /* make sure the output is flushed, or else RedBoot might clobber it */
1820 gdbstub_io_tx_flush();
1822 gdbstub_busy = 0;
1826 * initialise the GDB stub
1828 asmlinkage void __init gdbstub_init(void)
1830 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1831 unsigned char ch;
1832 int ret;
1833 #endif
1835 gdbstub_busy = 1;
1837 printk(KERN_INFO "%s", gdbstub_banner);
1839 gdbstub_io_init();
1841 gdbstub_entry("--> gdbstub_init\n");
1843 /* try to talk to GDB (or anyone insane enough to want to type GDB
1844 * protocol by hand) */
1845 gdbstub_io("### GDB Tx ACK\n");
1846 gdbstub_io_tx_char('+'); /* 'hello world' */
1848 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1849 gdbstub_printk("GDB Stub waiting for packet\n");
1851 /* in case GDB is started before us, ACK any packets that are already
1852 * sitting there (presumably "$?#xx")
1854 do { gdbstub_io_rx_char(&ch, 0); } while (ch != '$');
1855 do { gdbstub_io_rx_char(&ch, 0); } while (ch != '#');
1856 /* eat first csum byte */
1857 do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
1858 /* eat second csum byte */
1859 do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
1861 gdbstub_io("### GDB Tx NAK\n");
1862 gdbstub_io_tx_char('-'); /* NAK it */
1864 #else
1865 printk("GDB Stub ready\n");
1866 #endif
1868 gdbstub_busy = 0;
1869 gdbstub_entry("<-- gdbstub_init\n");
1873 * register the console at a more appropriate time
1875 #ifdef CONFIG_GDBSTUB_CONSOLE
1876 static int __init gdbstub_postinit(void)
1878 printk(KERN_NOTICE "registering console\n");
1879 register_console(&gdbstub_console);
1880 return 0;
1883 __initcall(gdbstub_postinit);
1884 #endif
1887 * handle character reception on GDB serial port
1888 * - jump into the GDB stub if BREAK is detected on the serial line
1890 asmlinkage void gdbstub_rx_irq(struct pt_regs *regs, enum exception_code excep)
1892 char ch;
1893 int ret;
1895 gdbstub_entry("--> gdbstub_rx_irq\n");
1897 do {
1898 ret = gdbstub_io_rx_char(&ch, 1);
1899 if (ret != -EIO && ret != -EAGAIN) {
1900 if (ret != -EINTR)
1901 gdbstub_rx_unget = ch;
1902 gdbstub(regs, excep);
1904 } while (ret != -EAGAIN);
1906 gdbstub_entry("<-- gdbstub_rx_irq\n");