Merge branch 'core/softirq' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[linux-2.6] / arch / mn10300 / kernel / gdb-stub.c
1 /* MN10300 GDB stub
2  *
3  * Originally written by Glenn Engel, Lake Stevens Instrument Division
4  *
5  * Contributed by HP Systems
6  *
7  * Modified for SPARC by Stu Grossman, Cygnus Support.
8  *
9  * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
10  * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
11  *
12  * Copyright (C) 1995 Andreas Busse
13  *
14  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
15  * Modified for Linux/mn10300 by David Howells <dhowells@redhat.com>
16  */
17
18 /*
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.
25  *
26  *
27  *    The following gdb commands are supported:
28  *
29  * command          function                               Return value
30  *
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
33  *
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
36  *
37  *    c             Resume at current address              SNN   ( signal NN)
38  *    cAA..AA       Continue at address AA..AA             SNN
39  *
40  *    s             Step one instruction                   SNN
41  *    sAA..AA       Step one instruction from AA..AA       SNN
42  *
43  *    k             kill
44  *
45  *    ?             What was the last sigval ?             SNN   (signal NN)
46  *
47  *    bBB..BB       Set baud rate to BB..BB                OK or BNN, then sets
48  *                                                         baud rate
49  *
50  * All commands and responses are sent with a packet which includes a
51  * checksum.  A packet consists of
52  *
53  * $<packet info>#<checksum>.
54  *
55  * where
56  * <packet info> :: <characters representing the command or response>
57  * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
58  *
59  * When a packet is received, it is first acknowledged with either '+' or '-'.
60  * '+' indicates a successful transfer.  '-' indicates a failed transfer.
61  *
62  * Example:
63  *
64  * Host:                  Reply:
65  * $m0,10#2a               +$00010203040506070809101112131415#42
66  *
67  *
68  *  ==============
69  *  MORE EXAMPLES:
70  *  ==============
71  *
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.
77  *
78  *  Step 1:
79  *  First download gdb-5.0.tar.gz from the internet.
80  *  and then build/install the package.
81  *
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
89  *
90  *  Step 2:
91  *  Configure linux for remote debugging and build it.
92  *
93  *  Example:
94  *    $ cd ~/linux
95  *    $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
96  *    $ make dep; make vmlinux
97  *
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();
107  *
108  *  Step 4:
109  *  Start the gdb session on the host.
110  *
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.
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/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>
141
142 /* define to use F7F7 rather than FF which is subverted by JTAG debugger */
143 #undef GDBSTUB_USE_F7F7_AS_BREAKPOINT
144
145 /*
146  * BUFMAX defines the maximum number of characters in inbound/outbound buffers
147  * at least NUMREGBYTES*2 are needed for register packets
148  */
149 #define BUFMAX 2048
150
151 static const char gdbstub_banner[] =
152         "Linux/MN10300 GDB Stub (c) RedHat 2007\n";
153
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;
160
161 static u8       gdbstub_flush_caches;
162 static char     input_buffer[BUFMAX];
163 static char     output_buffer[BUFMAX];
164 static char     trans_buffer[BUFMAX];
165
166 static const char hexchars[] = "0123456789abcdef";
167
168 struct gdbstub_bkpt {
169         u8      *addr;          /* address of breakpoint */
170         u8      len;            /* size of breakpoint */
171         u8      origbytes[7];   /* original bytes */
172 };
173
174 static struct gdbstub_bkpt gdbstub_bkpts[256];
175
176 /*
177  * local prototypes
178  */
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,
185                               int may_fault);
186 static const char *hex2mem(const char *buf, void *_mem, int count,
187                            int may_fault);
188
189 /*
190  * Convert ch from a hex digit to an int
191  */
192 static int hex(unsigned char ch)
193 {
194         if (ch >= 'a' && ch <= 'f')
195                 return ch - 'a' + 10;
196         if (ch >= '0' && ch <= '9')
197                 return ch - '0';
198         if (ch >= 'A' && ch <= 'F')
199                 return ch - 'A' + 10;
200         return -1;
201 }
202
203 #ifdef CONFIG_GDBSTUB_DEBUGGING
204
205 void debug_to_serial(const char *p, int n)
206 {
207         __debug_to_serial(p, n);
208         /* gdbstub_console_write(NULL, p, n); */
209 }
210
211 void gdbstub_printk(const char *fmt, ...)
212 {
213         va_list args;
214         int len;
215
216         /* Emit the output into the temporary buffer */
217         va_start(args, fmt);
218         len = vsnprintf(trans_buffer, sizeof(trans_buffer), fmt, args);
219         va_end(args);
220         debug_to_serial(trans_buffer, len);
221 }
222
223 #endif
224
225 static inline char *gdbstub_strcpy(char *dst, const char *src)
226 {
227         int loop = 0;
228         while ((dst[loop] = src[loop]))
229                loop++;
230         return dst;
231 }
232
233 /*
234  * scan for the sequence $<data>#<checksum>
235  */
236 static void getpacket(char *buffer)
237 {
238         unsigned char checksum;
239         unsigned char xmitcsum;
240         unsigned char ch;
241         int count, i, ret, error;
242
243         for (;;) {
244                 /*
245                  * wait around for the start character,
246                  * ignore all other characters
247                  */
248                 do {
249                         gdbstub_io_rx_char(&ch, 0);
250                 } while (ch != '$');
251
252                 checksum = 0;
253                 xmitcsum = -1;
254                 count = 0;
255                 error = 0;
256
257                 /*
258                  * now, read until a # or end of buffer is found
259                  */
260                 while (count < BUFMAX) {
261                         ret = gdbstub_io_rx_char(&ch, 0);
262                         if (ret < 0)
263                                 error = ret;
264
265                         if (ch == '#')
266                                 break;
267                         checksum += ch;
268                         buffer[count] = ch;
269                         count++;
270                 }
271
272                 if (error == -EIO) {
273                         gdbstub_proto("### GDB Rx Error - Skipping packet"
274                                       " ###\n");
275                         gdbstub_proto("### GDB Tx NAK\n");
276                         gdbstub_io_tx_char('-');
277                         continue;
278                 }
279
280                 if (count >= BUFMAX || error)
281                         continue;
282
283                 buffer[count] = 0;
284
285                 /* read the checksum */
286                 ret = gdbstub_io_rx_char(&ch, 0);
287                 if (ret < 0)
288                         error = ret;
289                 xmitcsum = hex(ch) << 4;
290
291                 ret = gdbstub_io_rx_char(&ch, 0);
292                 if (ret < 0)
293                         error = ret;
294                 xmitcsum |= hex(ch);
295
296                 if (error) {
297                         if (error == -EIO)
298                                 gdbstub_io("### GDB Rx Error -"
299                                            " Skipping packet\n");
300                         gdbstub_io("### GDB Tx NAK\n");
301                         gdbstub_io_tx_char('-');
302                         continue;
303                 }
304
305                 /* check the checksum */
306                 if (checksum != xmitcsum) {
307                         gdbstub_io("### GDB Tx NAK\n");
308                         gdbstub_io_tx_char('-');        /* failed checksum */
309                         continue;
310                 }
311
312                 gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
313                 gdbstub_io("### GDB Tx ACK\n");
314                 gdbstub_io_tx_char('+'); /* successful transfer */
315
316                 /*
317                  * if a sequence char is present,
318                  * reply the sequence ID
319                  */
320                 if (buffer[2] == ':') {
321                         gdbstub_io_tx_char(buffer[0]);
322                         gdbstub_io_tx_char(buffer[1]);
323
324                         /*
325                          * remove sequence chars from buffer
326                          */
327                         count = 0;
328                         while (buffer[count])
329                                 count++;
330                         for (i = 3; i <= count; i++)
331                                 buffer[i - 3] = buffer[i];
332                 }
333
334                 break;
335         }
336 }
337
338 /*
339  * send the packet in buffer.
340  * - return 0 if successfully ACK'd
341  * - return 1 if abandoned due to new incoming packet
342  */
343 static int putpacket(char *buffer)
344 {
345         unsigned char checksum;
346         unsigned char ch;
347         int count;
348
349         /*
350          * $<packet info>#<checksum>.
351          */
352         gdbstub_proto("### GDB Tx $'%s'#?? ###\n", buffer);
353
354         do {
355                 gdbstub_io_tx_char('$');
356                 checksum = 0;
357                 count = 0;
358
359                 while ((ch = buffer[count]) != 0) {
360                         gdbstub_io_tx_char(ch);
361                         checksum += ch;
362                         count += 1;
363                 }
364
365                 gdbstub_io_tx_char('#');
366                 gdbstub_io_tx_char(hexchars[checksum >> 4]);
367                 gdbstub_io_tx_char(hexchars[checksum & 0xf]);
368
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 != '$');
374
375         if (ch == '+') {
376                 gdbstub_io("### GDB Rx ACK\n");
377                 return 0;
378         }
379
380         gdbstub_io("### GDB Tx Abandoned\n");
381         gdbstub_rx_unget = ch;
382         return 1;
383 }
384
385 /*
386  * While we find nice hex chars, build an int.
387  * Return number of chars processed.
388  */
389 static int hexToInt(char **ptr, int *intValue)
390 {
391         int numChars = 0;
392         int hexValue;
393
394         *intValue = 0;
395
396         while (**ptr) {
397                 hexValue = hex(**ptr);
398                 if (hexValue < 0)
399                         break;
400
401                 *intValue = (*intValue << 4) | hexValue;
402                 numChars++;
403
404                 (*ptr)++;
405         }
406
407         return (numChars);
408 }
409
410 /*
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.
414  *
415  * This is where we save the original instructions.
416  */
417 static struct gdb_bp_save {
418         u8      *addr;
419         u8      opcode[2];
420 } step_bp[2];
421
422 static const unsigned char gdbstub_insn_sizes[256] =
423 {
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 */
441 };
442
443 static int __gdbstub_mark_bp(u8 *addr, int ix)
444 {
445         if (addr < (u8 *) 0x70000000UL)
446                 return 0;
447         /* 70000000-7fffffff: vmalloc area */
448         if (addr < (u8 *) 0x80000000UL)
449                 goto okay;
450         if (addr < (u8 *) 0x8c000000UL)
451                 return 0;
452         /* 8c000000-93ffffff: SRAM, SDRAM */
453         if (addr < (u8 *) 0x94000000UL)
454                 goto okay;
455         return 0;
456
457 okay:
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)
460                 return 0;
461
462         step_bp[ix].addr = addr;
463         return 1;
464 }
465
466 static inline void __gdbstub_restore_bp(void)
467 {
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);
472         }
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);
476         }
477 #else
478         if (step_bp[0].addr)
479                 gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
480         if (step_bp[1].addr)
481                 gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
482 #endif
483
484         gdbstub_flush_caches = 1;
485
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;
492 }
493
494 /*
495  * emulate single stepping by means of breakpoint instructions
496  */
497 static int gdbstub_single_step(struct pt_regs *regs)
498 {
499         unsigned size;
500         uint32_t x;
501         uint8_t cur, *pc, *sp;
502
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;
509         x = 0;
510
511         pc = (u8 *) regs->pc;
512         sp = (u8 *) (regs + 1);
513         if (gdbstub_read_byte(pc, &cur) < 0)
514                 return -EFAULT;
515
516         gdbstub_bkpt("Single Step from %p { %02x }\n", pc, cur);
517
518         gdbstub_flush_caches = 1;
519
520         size = gdbstub_insn_sizes[cur];
521         if (size > 0) {
522                 if (!__gdbstub_mark_bp(pc + size, 0))
523                         goto fault;
524         } else {
525                 switch (cur) {
526                         /* Bxx (d8,PC) */
527                 case 0xc0:
528                 case 0xc1:
529                 case 0xc2:
530                 case 0xc3:
531                 case 0xc4:
532                 case 0xc5:
533                 case 0xc6:
534                 case 0xc7:
535                 case 0xc8:
536                 case 0xc9:
537                 case 0xca:
538                         if (gdbstub_read_byte(pc + 1, (u8 *) &x) < 0)
539                                 goto fault;
540                         if (!__gdbstub_mark_bp(pc + 2, 0))
541                                 goto fault;
542                         if ((x < 0 || x > 2) &&
543                             !__gdbstub_mark_bp(pc + (s8) x, 1))
544                                 goto fault;
545                         break;
546
547                         /* LXX (d8,PC) */
548                 case 0xd0:
549                 case 0xd1:
550                 case 0xd2:
551                 case 0xd3:
552                 case 0xd4:
553                 case 0xd5:
554                 case 0xd6:
555                 case 0xd7:
556                 case 0xd8:
557                 case 0xd9:
558                 case 0xda:
559                         if (!__gdbstub_mark_bp(pc + 1, 0))
560                                 goto fault;
561                         if (regs->pc != regs->lar &&
562                             !__gdbstub_mark_bp((u8 *) regs->lar, 1))
563                                 goto fault;
564                         break;
565
566                         /* SETLB - loads the next for bytes into the LIR
567                          * register */
568                 case 0xdb:
569                         if (!__gdbstub_mark_bp(pc + 1, 0))
570                                 goto fault;
571                         break;
572
573                         /* JMP (d16,PC) or CALL (d16,PC) */
574                 case 0xcc:
575                 case 0xcd:
576                         if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
577                             gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0)
578                                 goto fault;
579                         if (!__gdbstub_mark_bp(pc + (s16) x, 0))
580                                 goto fault;
581                         break;
582
583                         /* JMP (d32,PC) or CALL (d32,PC) */
584                 case 0xdc:
585                 case 0xdd:
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)
590                                 goto fault;
591                         if (!__gdbstub_mark_bp(pc + (s32) x, 0))
592                                 goto fault;
593                         break;
594
595                         /* RETF */
596                 case 0xde:
597                         if (!__gdbstub_mark_bp((u8 *) regs->mdr, 0))
598                                 goto fault;
599                         break;
600
601                         /* RET */
602                 case 0xdf:
603                         if (gdbstub_read_byte(pc + 2, (u8 *) &x) < 0)
604                                 goto fault;
605                         sp += (s8)x;
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)
610                                 goto fault;
611                         if (!__gdbstub_mark_bp((u8 *) x, 0))
612                                 goto fault;
613                         break;
614
615                 case 0xf0:
616                         if (gdbstub_read_byte(pc + 1, &cur) < 0)
617                                 goto fault;
618
619                         if (cur >= 0xf0 && cur <= 0xf7) {
620                                 /* JMP (An) / CALLS (An) */
621                                 switch (cur & 3) {
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;
626                                 }
627                                 if (!__gdbstub_mark_bp((u8 *) x, 0))
628                                         goto fault;
629                         } else if (cur == 0xfc) {
630                                 /* RETS */
631                                 if (gdbstub_read_byte(
632                                             sp + 0, ((u8 *) &x) + 0) < 0 ||
633                                     gdbstub_read_byte(
634                                             sp + 1, ((u8 *) &x) + 1) < 0 ||
635                                     gdbstub_read_byte(
636                                             sp + 2, ((u8 *) &x) + 2) < 0 ||
637                                     gdbstub_read_byte(
638                                             sp + 3, ((u8 *) &x) + 3) < 0)
639                                         goto fault;
640                                 if (!__gdbstub_mark_bp((u8 *) x, 0))
641                                         goto fault;
642                         } else if (cur == 0xfd) {
643                                 /* RTI */
644                                 if (gdbstub_read_byte(
645                                             sp + 4, ((u8 *) &x) + 0) < 0 ||
646                                     gdbstub_read_byte(
647                                             sp + 5, ((u8 *) &x) + 1) < 0 ||
648                                     gdbstub_read_byte(
649                                             sp + 6, ((u8 *) &x) + 2) < 0 ||
650                                     gdbstub_read_byte(
651                                             sp + 7, ((u8 *) &x) + 3) < 0)
652                                         goto fault;
653                                 if (!__gdbstub_mark_bp((u8 *) x, 0))
654                                         goto fault;
655                         } else {
656                                 if (!__gdbstub_mark_bp(pc + 2, 0))
657                                         goto fault;
658                         }
659
660                         break;
661
662                         /* potential 3-byte conditional branches */
663                 case 0xf8:
664                         if (gdbstub_read_byte(pc + 1, &cur) < 0)
665                                 goto fault;
666                         if (!__gdbstub_mark_bp(pc + 3, 0))
667                                 goto fault;
668
669                         if (cur >= 0xe8 && cur <= 0xeb) {
670                                 if (gdbstub_read_byte(
671                                             pc + 2, ((u8 *) &x) + 0) < 0)
672                                         goto fault;
673                                 if ((x < 0 || x > 3) &&
674                                     !__gdbstub_mark_bp(pc + (s8) x, 1))
675                                         goto fault;
676                         }
677                         break;
678
679                 case 0xfa:
680                         if (gdbstub_read_byte(pc + 1, &cur) < 0)
681                                 goto fault;
682
683                         if (cur == 0xff) {
684                                 /* CALLS (d16,PC) */
685                                 if (gdbstub_read_byte(
686                                             pc + 2, ((u8 *) &x) + 0) < 0 ||
687                                     gdbstub_read_byte(
688                                             pc + 3, ((u8 *) &x) + 1) < 0)
689                                         goto fault;
690                                 if (!__gdbstub_mark_bp(pc + (s16) x, 0))
691                                         goto fault;
692                         } else {
693                                 if (!__gdbstub_mark_bp(pc + 4, 0))
694                                         goto fault;
695                         }
696                         break;
697
698                 case 0xfc:
699                         if (gdbstub_read_byte(pc + 1, &cur) < 0)
700                                 goto fault;
701                         if (cur == 0xff) {
702                                 /* CALLS (d32,PC) */
703                                 if (gdbstub_read_byte(
704                                             pc + 2, ((u8 *) &x) + 0) < 0 ||
705                                     gdbstub_read_byte(
706                                             pc + 3, ((u8 *) &x) + 1) < 0 ||
707                                     gdbstub_read_byte(
708                                             pc + 4, ((u8 *) &x) + 2) < 0 ||
709                                     gdbstub_read_byte(
710                                             pc + 5, ((u8 *) &x) + 3) < 0)
711                                         goto fault;
712                                 if (!__gdbstub_mark_bp(
713                                             pc + (s32) x, 0))
714                                         goto fault;
715                         } else {
716                                 if (!__gdbstub_mark_bp(
717                                             pc + 6, 0))
718                                         goto fault;
719                         }
720                         break;
721
722                 }
723         }
724
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);
728
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)
733                         goto fault;
734 #else
735                 if (gdbstub_write_byte(0xFF, step_bp[0].addr + 0) < 0)
736                         goto fault;
737 #endif
738         }
739
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)
744                         goto fault;
745 #else
746                 if (gdbstub_write_byte(0xFF, step_bp[1].addr + 0) < 0)
747                         goto fault;
748 #endif
749         }
750
751         return 0;
752
753  fault:
754         /* uh-oh - silly address alert, try and restore things */
755         __gdbstub_restore_bp();
756         return -EFAULT;
757 }
758
759 #ifdef CONFIG_GDBSTUB_CONSOLE
760
761 void gdbstub_console_write(struct console *con, const char *p, unsigned n)
762 {
763         static const char gdbstub_cr[] = { 0x0d };
764         char outbuf[26];
765         int qty;
766         u8 busy;
767
768         busy = gdbstub_busy;
769         gdbstub_busy = 1;
770
771         outbuf[0] = 'O';
772
773         while (n > 0) {
774                 qty = 1;
775
776                 while (n > 0 && qty < 20) {
777                         mem2hex(p, outbuf + qty, 2, 0);
778                         qty += 2;
779                         if (*p == 0x0a) {
780                                 mem2hex(gdbstub_cr, outbuf + qty, 2, 0);
781                                 qty += 2;
782                         }
783                         p++;
784                         n--;
785                 }
786
787                 outbuf[qty] = 0;
788                 putpacket(outbuf);
789         }
790
791         gdbstub_busy = busy;
792 }
793
794 static kdev_t gdbstub_console_dev(struct console *con)
795 {
796         return MKDEV(1, 3); /* /dev/null */
797 }
798
799 static struct console gdbstub_console = {
800         .name   = "gdb",
801         .write  = gdbstub_console_write,
802         .device = gdbstub_console_dev,
803         .flags  = CON_PRINTBUFFER,
804         .index  = -1,
805 };
806
807 #endif
808
809 /*
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.
815  */
816 static
817 unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
818 {
819         const u8 *mem = _mem;
820         u8 ch[4];
821
822         if ((u32) mem & 1 && count >= 1) {
823                 if (gdbstub_read_byte(mem, ch) != 0)
824                         return 0;
825                 *buf++ = hexchars[ch[0] >> 4];
826                 *buf++ = hexchars[ch[0] & 0xf];
827                 mem++;
828                 count--;
829         }
830
831         if ((u32) mem & 3 && count >= 2) {
832                 if (gdbstub_read_word(mem, ch) != 0)
833                         return 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];
838                 mem += 2;
839                 count -= 2;
840         }
841
842         while (count >= 4) {
843                 if (gdbstub_read_dword(mem, ch) != 0)
844                         return 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];
853                 mem += 4;
854                 count -= 4;
855         }
856
857         if (count >= 2) {
858                 if (gdbstub_read_word(mem, ch) != 0)
859                         return 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];
864                 mem += 2;
865                 count -= 2;
866         }
867
868         if (count >= 1) {
869                 if (gdbstub_read_byte(mem, ch) != 0)
870                         return 0;
871                 *buf++ = hexchars[ch[0] >> 4];
872                 *buf++ = hexchars[ch[0] & 0xf];
873         }
874
875         *buf = 0;
876         return buf;
877 }
878
879 /*
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.
884  */
885 static
886 const char *hex2mem(const char *buf, void *_mem, int count, int may_fault)
887 {
888         u8 *mem = _mem;
889         union {
890                 u32 val;
891                 u8 b[4];
892         } ch;
893
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)
898                         return 0;
899                 mem++;
900                 count--;
901         }
902
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)
909                         return 0;
910                 mem += 2;
911                 count -= 2;
912         }
913
914         while (count >= 4) {
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)
924                         return 0;
925                 mem += 4;
926                 count -= 4;
927         }
928
929         if (count >= 2) {
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)
935                         return 0;
936                 mem += 2;
937                 count -= 2;
938         }
939
940         if (count >= 1) {
941                 ch.b[0]  = hex(*buf++) << 4;
942                 ch.b[0] |= hex(*buf++);
943                 if (gdbstub_write_byte(ch.val, mem) != 0)
944                         return 0;
945         }
946
947         return buf;
948 }
949
950 /*
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.
954  */
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          },
987         { 0, 0}
988 };
989
990 /*
991  * convert the MN10300 exception code into a UNIX signal number
992  */
993 static int computeSignal(enum exception_code excep)
994 {
995         const struct excep_to_sig_map *map;
996
997         for (map = excep_to_sig_map; map->signo; map++)
998                 if (map->excep == excep)
999                         return map->signo;
1000
1001         return SIGHUP; /* default for things we don't know about */
1002 }
1003
1004 static u32 gdbstub_fpcr, gdbstub_fpufs_array[32];
1005
1006 /*
1007  *
1008  */
1009 static void gdbstub_store_fpu(void)
1010 {
1011 #ifdef CONFIG_FPU
1012
1013         asm volatile(
1014                 "or %2,epsw\n"
1015 #ifdef CONFIG_MN10300_PROC_MN103E010
1016                 "nop\n"
1017                 "nop\n"
1018 #endif
1019                 "mov %1, a1\n"
1020                 "fmov fs0,  (a1+)\n"
1021                 "fmov fs1,  (a1+)\n"
1022                 "fmov fs2,  (a1+)\n"
1023                 "fmov fs3,  (a1+)\n"
1024                 "fmov fs4,  (a1+)\n"
1025                 "fmov fs5,  (a1+)\n"
1026                 "fmov fs6,  (a1+)\n"
1027                 "fmov fs7,  (a1+)\n"
1028                 "fmov fs8,  (a1+)\n"
1029                 "fmov fs9,  (a1+)\n"
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"
1052                 "fmov fpcr, %0\n"
1053                 : "=d"(gdbstub_fpcr)
1054                 : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE)
1055                 : "a1"
1056                 );
1057 #endif
1058 }
1059
1060 /*
1061  *
1062  */
1063 static void gdbstub_load_fpu(void)
1064 {
1065 #ifdef CONFIG_FPU
1066
1067         asm volatile(
1068                 "or %1,epsw\n"
1069 #ifdef CONFIG_MN10300_PROC_MN103E010
1070                 "nop\n"
1071                 "nop\n"
1072 #endif
1073                 "mov %0, a1\n"
1074                 "fmov (a1+), fs0\n"
1075                 "fmov (a1+), fs1\n"
1076                 "fmov (a1+), fs2\n"
1077                 "fmov (a1+), fs3\n"
1078                 "fmov (a1+), fs4\n"
1079                 "fmov (a1+), fs5\n"
1080                 "fmov (a1+), fs6\n"
1081                 "fmov (a1+), fs7\n"
1082                 "fmov (a1+), fs8\n"
1083                 "fmov (a1+), fs9\n"
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"
1106                 "fmov %2, fpcr\n"
1107                 :
1108                 : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE), "d"(gdbstub_fpcr)
1109                 : "a1"
1110         );
1111 #endif
1112 }
1113
1114 /*
1115  * set a software breakpoint
1116  */
1117 int gdbstub_set_breakpoint(u8 *addr, int len)
1118 {
1119         int bkpt, loop, xloop;
1120
1121 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1122         len = (len + 1) & ~1;
1123 #endif
1124
1125         gdbstub_bkpt("setbkpt(%p,%d)\n", addr, len);
1126
1127         for (bkpt = 255; bkpt >= 0; bkpt--)
1128                 if (!gdbstub_bkpts[bkpt].addr)
1129                         break;
1130         if (bkpt < 0)
1131                 return -ENOSPC;
1132
1133         for (loop = 0; loop < len; loop++)
1134                 if (gdbstub_read_byte(&addr[loop],
1135                                       &gdbstub_bkpts[bkpt].origbytes[loop]
1136                                       ) < 0)
1137                         return -EFAULT;
1138
1139         gdbstub_flush_caches = 1;
1140
1141 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1142         for (loop = 0; loop < len; loop++)
1143                 if (gdbstub_write_byte(0xF7, &addr[loop]) < 0)
1144                         goto restore;
1145 #else
1146         for (loop = 0; loop < len; loop++)
1147                 if (gdbstub_write_byte(0xFF, &addr[loop]) < 0)
1148                         goto restore;
1149 #endif
1150
1151         gdbstub_bkpts[bkpt].addr = addr;
1152         gdbstub_bkpts[bkpt].len = len;
1153
1154         gdbstub_bkpt("Set BKPT[%02x]: %p-%p {%02x%02x%02x%02x%02x%02x%02x}\n",
1155                      bkpt,
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]
1165                      );
1166
1167         return 0;
1168
1169 restore:
1170         for (xloop = 0; xloop < loop; xloop++)
1171                 gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[xloop],
1172                                    addr + xloop);
1173         return -EFAULT;
1174 }
1175
1176 /*
1177  * clear a software breakpoint
1178  */
1179 int gdbstub_clear_breakpoint(u8 *addr, int len)
1180 {
1181         int bkpt, loop;
1182
1183 #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
1184         len = (len + 1) & ~1;
1185 #endif
1186
1187         gdbstub_bkpt("clearbkpt(%p,%d)\n", addr, len);
1188
1189         for (bkpt = 255; bkpt >= 0; bkpt--)
1190                 if (gdbstub_bkpts[bkpt].addr == addr &&
1191                     gdbstub_bkpts[bkpt].len == len)
1192                         break;
1193         if (bkpt < 0)
1194                 return -ENOENT;
1195
1196         gdbstub_bkpts[bkpt].addr = NULL;
1197
1198         gdbstub_flush_caches = 1;
1199
1200         for (loop = 0; loop < len; loop++)
1201                 if (gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[loop],
1202                                        addr + loop) < 0)
1203                         return -EFAULT;
1204
1205         return 0;
1206 }
1207
1208 /*
1209  * This function does all command processing for interfacing to gdb
1210  * - returns 1 if the exception should be skipped, 0 otherwise.
1211  */
1212 static int gdbstub(struct pt_regs *regs, enum exception_code excep)
1213 {
1214         unsigned long *stack;
1215         unsigned long epsw, mdr;
1216         uint32_t zero, ssp;
1217         uint8_t broke;
1218         char *ptr;
1219         int sigval;
1220         int addr;
1221         int length;
1222         int loop;
1223
1224         if (excep == EXCEP_FPU_DISABLED)
1225                 return 0;
1226
1227         gdbstub_flush_caches = 0;
1228
1229         mn10300_set_gdbleds(1);
1230
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));
1235
1236         gdbstub_store_fpu();
1237
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;
1242 #endif
1243
1244         /* if we were single stepping, restore the opcodes hoisted for the
1245          * breakpoint[s] */
1246         broke = 0;
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))
1249                 broke = 1;
1250
1251         __gdbstub_restore_bp();
1252
1253         if (gdbstub_rx_unget) {
1254                 sigval = SIGINT;
1255                 if (gdbstub_rx_unget != 3)
1256                         goto packet_waiting;
1257                 gdbstub_rx_unget = 0;
1258         }
1259
1260         stack = (unsigned long *) regs->sp;
1261         sigval = broke ? SIGTRAP : computeSignal(excep);
1262
1263         /* send information about a BUG() */
1264         if (!user_mode(regs) && excep == EXCEP_SYSCALL15) {
1265                 const struct bug_entry *bug;
1266
1267                 bug = find_bug(regs->pc);
1268                 if (bug)
1269                         goto found_bug;
1270                 length = snprintf(trans_buffer, sizeof(trans_buffer),
1271                                   "BUG() at address %lx\n", regs->pc);
1272                 goto send_bug_pkt;
1273
1274         found_bug:
1275                 length = snprintf(trans_buffer, sizeof(trans_buffer),
1276                                   "BUG() at address %lx (%s:%d)\n",
1277                                   regs->pc, bug->file, bug->line);
1278
1279         send_bug_pkt:
1280                 ptr = output_buffer;
1281                 *ptr++ = 'O';
1282                 ptr = mem2hex(trans_buffer, ptr, length, 0);
1283                 *ptr = 0;
1284                 putpacket(output_buffer);
1285
1286                 regs->pc -= 2;
1287                 sigval = SIGABRT;
1288         } else if (regs->pc == (unsigned long) __gdbstub_bug_trap) {
1289                 regs->pc = regs->mdr;
1290                 sigval = SIGABRT;
1291         }
1292
1293         /*
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)
1296          */
1297         if (sigval != SIGINT && sigval != SIGTRAP && sigval != SIGILL) {
1298                 static const char title[] = "Excep ", tbcberr[] = "BCBERR ";
1299                 static const char crlf[] = "\r\n";
1300                 char hx;
1301                 u32 bcberr = BCBERR;
1302
1303                 ptr = output_buffer;
1304                 *ptr++ = 'O';
1305                 ptr = mem2hex(title, ptr, sizeof(title) - 1, 0);
1306
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];
1315
1316                 ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
1317                 *ptr = 0;
1318                 putpacket(output_buffer);       /* send it off... */
1319
1320                 /* BCBERR */
1321                 ptr = output_buffer;
1322                 *ptr++ = 'O';
1323                 ptr = mem2hex(tbcberr, ptr, sizeof(tbcberr) - 1, 0);
1324
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];
1341
1342                 ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
1343                 *ptr = 0;
1344                 putpacket(output_buffer);       /* send it off... */
1345         }
1346
1347         /*
1348          * tell the debugger that an exception has occurred
1349          */
1350         ptr = output_buffer;
1351
1352         /*
1353          * Send trap type (converted to signal)
1354          */
1355         *ptr++ = 'T';
1356         *ptr++ = hexchars[sigval >> 4];
1357         *ptr++ = hexchars[sigval & 0xf];
1358
1359         /*
1360          * Send Error PC
1361          */
1362         *ptr++ = hexchars[GDB_REGID_PC >> 4];
1363         *ptr++ = hexchars[GDB_REGID_PC & 0xf];
1364         *ptr++ = ':';
1365         ptr = mem2hex(&regs->pc, ptr, 4, 0);
1366         *ptr++ = ';';
1367
1368         /*
1369          * Send frame pointer
1370          */
1371         *ptr++ = hexchars[GDB_REGID_FP >> 4];
1372         *ptr++ = hexchars[GDB_REGID_FP & 0xf];
1373         *ptr++ = ':';
1374         ptr = mem2hex(&regs->a3, ptr, 4, 0);
1375         *ptr++ = ';';
1376
1377         /*
1378          * Send stack pointer
1379          */
1380         ssp = (unsigned long) (regs + 1);
1381         *ptr++ = hexchars[GDB_REGID_SP >> 4];
1382         *ptr++ = hexchars[GDB_REGID_SP & 0xf];
1383         *ptr++ = ':';
1384         ptr = mem2hex(&ssp, ptr, 4, 0);
1385         *ptr++ = ';';
1386
1387         *ptr++ = 0;
1388         putpacket(output_buffer);       /* send it off... */
1389
1390 packet_waiting:
1391         /*
1392          * Wait for input from remote GDB
1393          */
1394         while (1) {
1395                 output_buffer[0] = 0;
1396                 getpacket(input_buffer);
1397
1398                 switch (input_buffer[0]) {
1399                         /* request repeat of last signal number */
1400                 case '?':
1401                         output_buffer[0] = 'S';
1402                         output_buffer[1] = hexchars[sigval >> 4];
1403                         output_buffer[2] = hexchars[sigval & 0xf];
1404                         output_buffer[3] = 0;
1405                         break;
1406
1407                 case 'd':
1408                         /* toggle debug flag */
1409                         break;
1410
1411                         /*
1412                          * Return the value of the CPU registers
1413                          */
1414                 case 'g':
1415                         zero = 0;
1416                         ssp = (u32) (regs + 1);
1417                         ptr = output_buffer;
1418                         ptr = mem2hex(&regs->d0, ptr, 4, 0);
1419                         ptr = mem2hex(&regs->d1, ptr, 4, 0);
1420                         ptr = mem2hex(&regs->d2, ptr, 4, 0);
1421                         ptr = mem2hex(&regs->d3, ptr, 4, 0);
1422                         ptr = mem2hex(&regs->a0, ptr, 4, 0);
1423                         ptr = mem2hex(&regs->a1, ptr, 4, 0);
1424                         ptr = mem2hex(&regs->a2, ptr, 4, 0);
1425                         ptr = mem2hex(&regs->a3, ptr, 4, 0);
1426
1427                         ptr = mem2hex(&ssp, ptr, 4, 0);         /* 8 */
1428                         ptr = mem2hex(&regs->pc, ptr, 4, 0);
1429                         ptr = mem2hex(&regs->mdr, ptr, 4, 0);
1430                         ptr = mem2hex(&regs->epsw, ptr, 4, 0);
1431                         ptr = mem2hex(&regs->lir, ptr, 4, 0);
1432                         ptr = mem2hex(&regs->lar, ptr, 4, 0);
1433                         ptr = mem2hex(&regs->mdrq, ptr, 4, 0);
1434
1435                         ptr = mem2hex(&regs->e0, ptr, 4, 0);    /* 15 */
1436                         ptr = mem2hex(&regs->e1, ptr, 4, 0);
1437                         ptr = mem2hex(&regs->e2, ptr, 4, 0);
1438                         ptr = mem2hex(&regs->e3, ptr, 4, 0);
1439                         ptr = mem2hex(&regs->e4, ptr, 4, 0);
1440                         ptr = mem2hex(&regs->e5, ptr, 4, 0);
1441                         ptr = mem2hex(&regs->e6, ptr, 4, 0);
1442                         ptr = mem2hex(&regs->e7, ptr, 4, 0);
1443
1444                         ptr = mem2hex(&ssp, ptr, 4, 0);
1445                         ptr = mem2hex(&regs, ptr, 4, 0);
1446                         ptr = mem2hex(&regs->sp, ptr, 4, 0);
1447                         ptr = mem2hex(&regs->mcrh, ptr, 4, 0);  /* 26 */
1448                         ptr = mem2hex(&regs->mcrl, ptr, 4, 0);
1449                         ptr = mem2hex(&regs->mcvf, ptr, 4, 0);
1450
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 */
1457
1458                         break;
1459
1460                         /*
1461                          * set the value of the CPU registers - return OK
1462                          */
1463                 case 'G':
1464                 {
1465                         const char *ptr;
1466
1467                         ptr = &input_buffer[1];
1468                         ptr = hex2mem(ptr, &regs->d0, 4, 0);
1469                         ptr = hex2mem(ptr, &regs->d1, 4, 0);
1470                         ptr = hex2mem(ptr, &regs->d2, 4, 0);
1471                         ptr = hex2mem(ptr, &regs->d3, 4, 0);
1472                         ptr = hex2mem(ptr, &regs->a0, 4, 0);
1473                         ptr = hex2mem(ptr, &regs->a1, 4, 0);
1474                         ptr = hex2mem(ptr, &regs->a2, 4, 0);
1475                         ptr = hex2mem(ptr, &regs->a3, 4, 0);
1476
1477                         ptr = hex2mem(ptr, &ssp, 4, 0);         /* 8 */
1478                         ptr = hex2mem(ptr, &regs->pc, 4, 0);
1479                         ptr = hex2mem(ptr, &regs->mdr, 4, 0);
1480                         ptr = hex2mem(ptr, &regs->epsw, 4, 0);
1481                         ptr = hex2mem(ptr, &regs->lir, 4, 0);
1482                         ptr = hex2mem(ptr, &regs->lar, 4, 0);
1483                         ptr = hex2mem(ptr, &regs->mdrq, 4, 0);
1484
1485                         ptr = hex2mem(ptr, &regs->e0, 4, 0);    /* 15 */
1486                         ptr = hex2mem(ptr, &regs->e1, 4, 0);
1487                         ptr = hex2mem(ptr, &regs->e2, 4, 0);
1488                         ptr = hex2mem(ptr, &regs->e3, 4, 0);
1489                         ptr = hex2mem(ptr, &regs->e4, 4, 0);
1490                         ptr = hex2mem(ptr, &regs->e5, 4, 0);
1491                         ptr = hex2mem(ptr, &regs->e6, 4, 0);
1492                         ptr = hex2mem(ptr, &regs->e7, 4, 0);
1493
1494                         ptr = hex2mem(ptr, &ssp, 4, 0);
1495                         ptr = hex2mem(ptr, &zero, 4, 0);
1496                         ptr = hex2mem(ptr, &regs->sp, 4, 0);
1497                         ptr = hex2mem(ptr, &regs->mcrh, 4, 0);  /* 26 */
1498                         ptr = hex2mem(ptr, &regs->mcrl, 4, 0);
1499                         ptr = hex2mem(ptr, &regs->mcvf, 4, 0);
1500
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);
1506
1507 #if 0
1508                         /*
1509                          * See if the stack pointer has moved. If so, then copy
1510                          * the saved locals and ins to the new location.
1511                          */
1512                         unsigned long *newsp = (unsigned long *) registers[SP];
1513                         if (sp != newsp)
1514                                 sp = memcpy(newsp, sp, 16 * 4);
1515 #endif
1516
1517                         gdbstub_strcpy(output_buffer, "OK");
1518                 }
1519                 break;
1520
1521                 /*
1522                  * mAA..AA,LLLL  Read LLLL bytes at address AA..AA
1523                  */
1524                 case 'm':
1525                         ptr = &input_buffer[1];
1526
1527                         if (hexToInt(&ptr, &addr) &&
1528                             *ptr++ == ',' &&
1529                             hexToInt(&ptr, &length)
1530                             ) {
1531                                 if (mem2hex((char *) addr, output_buffer,
1532                                             length, 1))
1533                                         break;
1534                                 gdbstub_strcpy(output_buffer, "E03");
1535                         } else {
1536                                 gdbstub_strcpy(output_buffer, "E01");
1537                         }
1538                         break;
1539
1540                         /*
1541                          * MAA..AA,LLLL: Write LLLL bytes at address AA.AA
1542                          * return OK
1543                          */
1544                 case 'M':
1545                         ptr = &input_buffer[1];
1546
1547                         if (hexToInt(&ptr, &addr) &&
1548                             *ptr++ == ',' &&
1549                             hexToInt(&ptr, &length) &&
1550                             *ptr++ == ':'
1551                             ) {
1552                                 if (hex2mem(ptr, (char *) addr, length, 1))
1553                                         gdbstub_strcpy(output_buffer, "OK");
1554                                 else
1555                                         gdbstub_strcpy(output_buffer, "E03");
1556
1557                                 gdbstub_flush_caches = 1;
1558                         } else {
1559                                 gdbstub_strcpy(output_buffer, "E02");
1560                         }
1561                         break;
1562
1563                         /*
1564                          * cAA..AA    Continue at address AA..AA(optional)
1565                          */
1566                 case 'c':
1567                         /* try to read optional parameter, pc unchanged if no
1568                          * parm */
1569
1570                         ptr = &input_buffer[1];
1571                         if (hexToInt(&ptr, &addr))
1572                                 regs->pc = addr;
1573                         goto done;
1574
1575                         /*
1576                          * kill the program
1577                          */
1578                 case 'k' :
1579                         goto done;      /* just continue */
1580
1581                         /*
1582                          * Reset the whole machine (FIXME: system dependent)
1583                          */
1584                 case 'r':
1585                         break;
1586
1587                         /*
1588                          * Step to next instruction
1589                          */
1590                 case 's':
1591                         /*
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
1595                          * continue instead.
1596                          */
1597                         if (gdbstub_single_step(regs) < 0)
1598                                 /* ignore any fault error for now */
1599                                 gdbstub_printk("unable to set single-step"
1600                                                " bp\n");
1601                         goto done;
1602
1603                         /*
1604                          * Set baud rate (bBB)
1605                          */
1606                 case 'b':
1607                         do {
1608                                 int baudrate;
1609
1610                                 ptr = &input_buffer[1];
1611                                 if (!hexToInt(&ptr, &baudrate)) {
1612                                         gdbstub_strcpy(output_buffer, "B01");
1613                                         break;
1614                                 }
1615
1616                                 if (baudrate) {
1617                                         /* ACK before changing speed */
1618                                         putpacket("OK");
1619                                         gdbstub_io_set_baud(baudrate);
1620                                 }
1621                         } while (0);
1622                         break;
1623
1624                         /*
1625                          * Set breakpoint
1626                          */
1627                 case 'Z':
1628                         ptr = &input_buffer[1];
1629
1630                         if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
1631                             !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
1632                             !hexToInt(&ptr, &length)
1633                             ) {
1634                                 gdbstub_strcpy(output_buffer, "E01");
1635                                 break;
1636                         }
1637
1638                         /* only support software breakpoints */
1639                         gdbstub_strcpy(output_buffer, "E03");
1640                         if (loop != 0 ||
1641                             length < 1 ||
1642                             length > 7 ||
1643                             (unsigned long) addr < 4096)
1644                                 break;
1645
1646                         if (gdbstub_set_breakpoint((u8 *) addr, length) < 0)
1647                                 break;
1648
1649                         gdbstub_strcpy(output_buffer, "OK");
1650                         break;
1651
1652                         /*
1653                          * Clear breakpoint
1654                          */
1655                 case 'z':
1656                         ptr = &input_buffer[1];
1657
1658                         if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
1659                             !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
1660                             !hexToInt(&ptr, &length)
1661                             ) {
1662                                 gdbstub_strcpy(output_buffer, "E01");
1663                                 break;
1664                         }
1665
1666                         /* only support software breakpoints */
1667                         gdbstub_strcpy(output_buffer, "E03");
1668                         if (loop != 0 ||
1669                             length < 1 ||
1670                             length > 7 ||
1671                             (unsigned long) addr < 4096)
1672                                 break;
1673
1674                         if (gdbstub_clear_breakpoint((u8 *) addr, length) < 0)
1675                                 break;
1676
1677                         gdbstub_strcpy(output_buffer, "OK");
1678                         break;
1679
1680                 default:
1681                         gdbstub_proto("### GDB Unsupported Cmd '%s'\n",
1682                                       input_buffer);
1683                         break;
1684                 }
1685
1686                 /* reply to the request */
1687                 putpacket(output_buffer);
1688         }
1689
1690 done:
1691         /*
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
1696          * cache.
1697          * NB: We flush both caches, just to be sure...
1698          */
1699         if (gdbstub_flush_caches)
1700                 gdbstub_purge_cache();
1701
1702         gdbstub_load_fpu();
1703         mn10300_set_gdbleds(0);
1704         if (excep == EXCEP_NMI)
1705                 NMICR = NMICR_NMIF;
1706
1707         touch_softlockup_watchdog();
1708
1709         local_irq_restore(epsw);
1710         return 1;
1711 }
1712
1713 /*
1714  * handle event interception
1715  */
1716 asmlinkage int gdbstub_intercept(struct pt_regs *regs,
1717                                  enum exception_code excep)
1718 {
1719         static u8 notfirst = 1;
1720         int ret;
1721
1722         if (gdbstub_busy)
1723                 gdbstub_printk("--> gdbstub reentered itself\n");
1724         gdbstub_busy = 1;
1725
1726         if (notfirst) {
1727                 unsigned long mdr;
1728                 asm("mov mdr,%0" : "=d"(mdr));
1729
1730                 gdbstub_entry(
1731                         "--> gdbstub_intercept(%p,%04x) [MDR=%lx PC=%lx]\n",
1732                         regs, excep, mdr, regs->pc);
1733
1734                 gdbstub_entry(
1735                         "PC:  %08lx EPSW:  %08lx  SSP: %08lx mode: %s\n",
1736                         regs->pc, regs->epsw, (unsigned long) &ret,
1737                         user_mode(regs) ? "User" : "Super");
1738                 gdbstub_entry(
1739                         "d0:  %08lx   d1:  %08lx   d2: %08lx   d3: %08lx\n",
1740                         regs->d0, regs->d1, regs->d2, regs->d3);
1741                 gdbstub_entry(
1742                         "a0:  %08lx   a1:  %08lx   a2: %08lx   a3: %08lx\n",
1743                         regs->a0, regs->a1, regs->a2, regs->a3);
1744                 gdbstub_entry(
1745                         "e0:  %08lx   e1:  %08lx   e2: %08lx   e3: %08lx\n",
1746                         regs->e0, regs->e1, regs->e2, regs->e3);
1747                 gdbstub_entry(
1748                         "e4:  %08lx   e5:  %08lx   e6: %08lx   e7: %08lx\n",
1749                         regs->e4, regs->e5, regs->e6, regs->e7);
1750                 gdbstub_entry(
1751                         "lar: %08lx   lir: %08lx  mdr: %08lx  usp: %08lx\n",
1752                         regs->lar, regs->lir, regs->mdr, regs->sp);
1753                 gdbstub_entry(
1754                         "cvf: %08lx   crl: %08lx  crh: %08lx  drq: %08lx\n",
1755                         regs->mcvf, regs->mcrl, regs->mcrh, regs->mdrq);
1756                 gdbstub_entry(
1757                         "threadinfo=%p task=%p)\n",
1758                         current_thread_info(), current);
1759         } else {
1760                 notfirst = 1;
1761         }
1762
1763         ret = gdbstub(regs, excep);
1764
1765         gdbstub_entry("<-- gdbstub_intercept()\n");
1766         gdbstub_busy = 0;
1767         return ret;
1768 }
1769
1770 /*
1771  * handle the GDB stub itself causing an exception
1772  */
1773 asmlinkage void gdbstub_exception(struct pt_regs *regs,
1774                                   enum exception_code excep)
1775 {
1776         unsigned long mdr;
1777
1778         asm("mov mdr,%0" : "=d"(mdr));
1779         gdbstub_entry("--> gdbstub exception({%p},%04x) [MDR=%lx]\n",
1780                       regs, excep, mdr);
1781
1782         while ((unsigned long) regs == 0xffffffff) {}
1783
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;
1787                 goto fault;
1788         }
1789
1790         if (regs->pc == (unsigned) gdbstub_read_word_guard) {
1791                 regs->pc = (unsigned) gdbstub_read_word_cont;
1792                 goto fault;
1793         }
1794
1795         if (regs->pc == (unsigned) gdbstub_read_dword_guard) {
1796                 regs->pc = (unsigned) gdbstub_read_dword_cont;
1797                 goto fault;
1798         }
1799
1800         if (regs->pc == (unsigned) gdbstub_write_byte_guard) {
1801                 regs->pc = (unsigned) gdbstub_write_byte_cont;
1802                 goto fault;
1803         }
1804
1805         if (regs->pc == (unsigned) gdbstub_write_word_guard) {
1806                 regs->pc = (unsigned) gdbstub_write_word_cont;
1807                 goto fault;
1808         }
1809
1810         if (regs->pc == (unsigned) gdbstub_write_dword_guard) {
1811                 regs->pc = (unsigned) gdbstub_write_dword_cont;
1812                 goto fault;
1813         }
1814
1815         gdbstub_printk("\n### GDB stub caused an exception ###\n");
1816
1817         /* something went horribly wrong */
1818         console_verbose();
1819         show_registers(regs);
1820
1821         panic("GDB Stub caused an unexpected exception - can't continue\n");
1822
1823         /* we caught an attempt by the stub to access silly memory */
1824 fault:
1825         gdbstub_entry("<-- gdbstub exception() = EFAULT\n");
1826         regs->d0 = -EFAULT;
1827         return;
1828 }
1829
1830 /*
1831  * send an exit message to GDB
1832  */
1833 void gdbstub_exit(int status)
1834 {
1835         unsigned char checksum;
1836         unsigned char ch;
1837         int count;
1838
1839         gdbstub_busy = 1;
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;
1844
1845         gdbstub_io_tx_char('$');
1846         checksum = 0;
1847         count = 0;
1848
1849         while ((ch = output_buffer[count]) != 0) {
1850                 gdbstub_io_tx_char(ch);
1851                 checksum += ch;
1852                 count += 1;
1853         }
1854
1855         gdbstub_io_tx_char('#');
1856         gdbstub_io_tx_char(hexchars[checksum >> 4]);
1857         gdbstub_io_tx_char(hexchars[checksum & 0xf]);
1858
1859         /* make sure the output is flushed, or else RedBoot might clobber it */
1860         gdbstub_io_tx_flush();
1861
1862         gdbstub_busy = 0;
1863 }
1864
1865 /*
1866  * initialise the GDB stub
1867  */
1868 asmlinkage void __init gdbstub_init(void)
1869 {
1870 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1871         unsigned char ch;
1872         int ret;
1873 #endif
1874
1875         gdbstub_busy = 1;
1876
1877         printk(KERN_INFO "%s", gdbstub_banner);
1878
1879         gdbstub_io_init();
1880
1881         gdbstub_entry("--> gdbstub_init\n");
1882
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' */
1887
1888 #ifdef CONFIG_GDBSTUB_IMMEDIATE
1889         gdbstub_printk("GDB Stub waiting for packet\n");
1890
1891         /* in case GDB is started before us, ACK any packets that are already
1892          * sitting there (presumably "$?#xx")
1893          */
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);
1900
1901         gdbstub_io("### GDB Tx NAK\n");
1902         gdbstub_io_tx_char('-'); /* NAK it */
1903
1904 #else
1905         printk("GDB Stub ready\n");
1906 #endif
1907
1908         gdbstub_busy = 0;
1909         gdbstub_entry("<-- gdbstub_init\n");
1910 }
1911
1912 /*
1913  * register the console at a more appropriate time
1914  */
1915 #ifdef CONFIG_GDBSTUB_CONSOLE
1916 static int __init gdbstub_postinit(void)
1917 {
1918         printk(KERN_NOTICE "registering console\n");
1919         register_console(&gdbstub_console);
1920         return 0;
1921 }
1922
1923 __initcall(gdbstub_postinit);
1924 #endif
1925
1926 /*
1927  * handle character reception on GDB serial port
1928  * - jump into the GDB stub if BREAK is detected on the serial line
1929  */
1930 asmlinkage void gdbstub_rx_irq(struct pt_regs *regs, enum exception_code excep)
1931 {
1932         char ch;
1933         int ret;
1934
1935         gdbstub_entry("--> gdbstub_rx_irq\n");
1936
1937         do {
1938                 ret = gdbstub_io_rx_char(&ch, 1);
1939                 if (ret != -EIO && ret != -EAGAIN) {
1940                         if (ret != -EINTR)
1941                                 gdbstub_rx_unget = ch;
1942                         gdbstub(regs, excep);
1943                 }
1944         } while (ret != -EAGAIN);
1945
1946         gdbstub_entry("<-- gdbstub_rx_irq\n");
1947 }