a3e698849da9a25aa5fe72f892a51f09afaf7bb0
[linux-flexiantxendom0-3.2.10.git] / arch / mips / kernel / gdb-stub.c
1 /*
2  *  arch/mips/kernel/gdb-stub.c
3  *
4  *  Originally written by Glenn Engel, Lake Stevens Instrument Division
5  *
6  *  Contributed by HP Systems
7  *
8  *  Modified for SPARC by Stu Grossman, Cygnus Support.
9  *
10  *  Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
11  *  Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
12  *
13  *  Copyright (C) 1995 Andreas Busse
14  */
15
16 /*
17  *  To enable debugger support, two things need to happen.  One, a
18  *  call to set_debug_traps() is necessary in order to allow any breakpoints
19  *  or error conditions to be properly intercepted and reported to gdb.
20  *  Two, a breakpoint needs to be generated to begin communication.  This
21  *  is most easily accomplished by a call to breakpoint().  Breakpoint()
22  *  simulates a breakpoint by executing a BREAK instruction.
23  *
24  *
25  *    The following gdb commands are supported:
26  *
27  * command          function                               Return value
28  *
29  *    g             return the value of the CPU registers  hex data or ENN
30  *    G             set the value of the CPU registers     OK or ENN
31  *
32  *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
33  *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
34  *
35  *    c             Resume at current address              SNN   ( signal NN)
36  *    cAA..AA       Continue at address AA..AA             SNN
37  *
38  *    s             Step one instruction                   SNN
39  *    sAA..AA       Step one instruction from AA..AA       SNN
40  *
41  *    k             kill
42  *
43  *    ?             What was the last sigval ?             SNN   (signal NN)
44  *
45  *    bBB..BB       Set baud rate to BB..BB                OK or BNN, then sets
46  *                                                         baud rate
47  *
48  * All commands and responses are sent with a packet which includes a
49  * checksum.  A packet consists of
50  *
51  * $<packet info>#<checksum>.
52  *
53  * where
54  * <packet info> :: <characters representing the command or response>
55  * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
56  *
57  * When a packet is received, it is first acknowledged with either '+' or '-'.
58  * '+' indicates a successful transfer.  '-' indicates a failed transfer.
59  *
60  * Example:
61  *
62  * Host:                  Reply:
63  * $m0,10#2a               +$00010203040506070809101112131415#42
64  *
65  *
66  *  ==============
67  *  MORE EXAMPLES:
68  *  ==============
69  *
70  *  For reference -- the following are the steps that one
71  *  company took (RidgeRun Inc) to get remote gdb debugging
72  *  going. In this scenario the host machine was a PC and the
73  *  target platform was a Galileo EVB64120A MIPS evaluation
74  *  board.
75  *
76  *  Step 1:
77  *  First download gdb-5.0.tar.gz from the internet.
78  *  and then build/install the package.
79  *
80  *  Example:
81  *    $ tar zxf gdb-5.0.tar.gz
82  *    $ cd gdb-5.0
83  *    $ ./configure --target=mips-linux-elf
84  *    $ make
85  *    $ install
86  *    $ which mips-linux-elf-gdb
87  *    /usr/local/bin/mips-linux-elf-gdb
88  *
89  *  Step 2:
90  *  Configure linux for remote debugging and build it.
91  *
92  *  Example:
93  *    $ cd ~/linux
94  *    $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
95  *    $ make dep; make vmlinux
96  *
97  *  Step 3:
98  *  Download the kernel to the remote target and start
99  *  the kernel running. It will promptly halt and wait
100  *  for the host gdb session to connect. It does this
101  *  since the "Kernel Hacking" option has defined
102  *  CONFIG_KGDB which in turn enables your calls
103  *  to:
104  *     set_debug_traps();
105  *     breakpoint();
106  *
107  *  Step 4:
108  *  Start the gdb session on the host.
109  *
110  *  Example:
111  *    $ mips-linux-elf-gdb vmlinux
112  *    (gdb) set remotebaud 115200
113  *    (gdb) target remote /dev/ttyS1
114  *    ...at this point you are connected to
115  *       the remote target and can use gdb
116  *       in the normal fasion. Setting
117  *       breakpoints, single stepping,
118  *       printing variables, etc.
119  */
120 #include <linux/config.h>
121 #include <linux/string.h>
122 #include <linux/kernel.h>
123 #include <linux/signal.h>
124 #include <linux/sched.h>
125 #include <linux/mm.h>
126 #include <linux/console.h>
127 #include <linux/init.h>
128 #include <linux/slab.h>
129 #include <linux/reboot.h>
130
131 #include <asm/asm.h>
132 #include <asm/mipsregs.h>
133 #include <asm/pgtable.h>
134 #include <asm/system.h>
135 #include <asm/gdb-stub.h>
136 #include <asm/inst.h>
137
138 /*
139  * external low-level support routines
140  */
141
142 extern int putDebugChar(char c);    /* write a single character      */
143 extern char getDebugChar(void);     /* read and return a single char */
144 extern void trap_low(void);
145
146 /*
147  * breakpoint and test functions
148  */
149 extern void breakpoint(void);
150 extern void breakinst(void);
151 extern void adel(void);
152
153 /*
154  * local prototypes
155  */
156
157 static void getpacket(char *buffer);
158 static void putpacket(char *buffer);
159 static int computeSignal(int tt);
160 static int hex(unsigned char ch);
161 static int hexToInt(char **ptr, int *intValue);
162 static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault);
163 void handle_exception(struct gdb_regs *regs);
164
165 /*
166  * BUFMAX defines the maximum number of characters in inbound/outbound buffers
167  * at least NUMREGBYTES*2 are needed for register packets
168  */
169 #define BUFMAX 2048
170
171 static char input_buffer[BUFMAX];
172 static char output_buffer[BUFMAX];
173 static int initialized; /* !0 means we've been initialized */
174 static const char hexchars[]="0123456789abcdef";
175
176 /* Used to prevent crashes in memory access.  Note that they'll crash anyway if
177    we haven't set up fault handlers yet... */
178 int kgdb_read_byte(unsigned char *address, unsigned char *dest);
179 int kgdb_write_byte(unsigned char val, unsigned char *dest);
180
181 /*
182  * Convert ch from a hex digit to an int
183  */
184 static int hex(unsigned char ch)
185 {
186         if (ch >= 'a' && ch <= 'f')
187                 return ch-'a'+10;
188         if (ch >= '0' && ch <= '9')
189                 return ch-'0';
190         if (ch >= 'A' && ch <= 'F')
191                 return ch-'A'+10;
192         return -1;
193 }
194
195 /*
196  * scan for the sequence $<data>#<checksum>
197  */
198 static void getpacket(char *buffer)
199 {
200         unsigned char checksum;
201         unsigned char xmitcsum;
202         int i;
203         int count;
204         unsigned char ch;
205
206         do {
207                 /*
208                  * wait around for the start character,
209                  * ignore all other characters
210                  */
211                 while ((ch = (getDebugChar() & 0x7f)) != '$') ;
212
213                 checksum = 0;
214                 xmitcsum = -1;
215                 count = 0;
216
217                 /*
218                  * now, read until a # or end of buffer is found
219                  */
220                 while (count < BUFMAX) {
221                         ch = getDebugChar() & 0x7f;
222                         if (ch == '#')
223                                 break;
224                         checksum = checksum + ch;
225                         buffer[count] = ch;
226                         count = count + 1;
227                 }
228
229                 if (count >= BUFMAX)
230                         continue;
231
232                 buffer[count] = 0;
233
234                 if (ch == '#') {
235                         xmitcsum = hex(getDebugChar() & 0x7f) << 4;
236                         xmitcsum |= hex(getDebugChar() & 0x7f);
237
238                         if (checksum != xmitcsum)
239                                 putDebugChar('-');      /* failed checksum */
240                         else {
241                                 putDebugChar('+'); /* successful transfer */
242
243                                 /*
244                                  * if a sequence char is present,
245                                  * reply the sequence ID
246                                  */
247                                 if (buffer[2] == ':') {
248                                         putDebugChar(buffer[0]);
249                                         putDebugChar(buffer[1]);
250
251                                         /*
252                                          * remove sequence chars from buffer
253                                          */
254                                         count = strlen(buffer);
255                                         for (i=3; i <= count; i++)
256                                                 buffer[i-3] = buffer[i];
257                                 }
258                         }
259                 }
260         }
261         while (checksum != xmitcsum);
262 }
263
264 /*
265  * send the packet in buffer.
266  */
267 static void putpacket(char *buffer)
268 {
269         unsigned char checksum;
270         int count;
271         unsigned char ch;
272
273         /*
274          * $<packet info>#<checksum>.
275          */
276
277         do {
278                 putDebugChar('$');
279                 checksum = 0;
280                 count = 0;
281
282                 while ((ch = buffer[count]) != 0) {
283                         if (!(putDebugChar(ch)))
284                                 return;
285                         checksum += ch;
286                         count += 1;
287                 }
288
289                 putDebugChar('#');
290                 putDebugChar(hexchars[checksum >> 4]);
291                 putDebugChar(hexchars[checksum & 0xf]);
292
293         }
294         while ((getDebugChar() & 0x7f) != '+');
295 }
296
297
298 /*
299  * Convert the memory pointed to by mem into hex, placing result in buf.
300  * Return a pointer to the last char put in buf (null), in case of mem fault,
301  * return 0.
302  * may_fault is non-zero if we are reading from arbitrary memory, but is currently
303  * not used.
304  */
305 static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault)
306 {
307         unsigned char ch;
308
309         while (count-- > 0) {
310                 if (kgdb_read_byte(mem++, &ch) != 0)
311                         return 0;
312                 *buf++ = hexchars[ch >> 4];
313                 *buf++ = hexchars[ch & 0xf];
314         }
315
316         *buf = 0;
317
318         return buf;
319 }
320
321 /*
322  * convert the hex array pointed to by buf into binary to be placed in mem
323  * return a pointer to the character AFTER the last byte written
324  * may_fault is non-zero if we are reading from arbitrary memory, but is currently
325  * not used.
326  */
327 static char *hex2mem(char *buf, char *mem, int count, int may_fault)
328 {
329         int i;
330         unsigned char ch;
331
332         for (i=0; i<count; i++)
333         {
334                 ch = hex(*buf++) << 4;
335                 ch |= hex(*buf++);
336                 if (kgdb_write_byte(ch, mem++) != 0)
337                         return 0;
338         }
339
340         return mem;
341 }
342
343 /*
344  * This table contains the mapping between SPARC hardware trap types, and
345  * signals, which are primarily what GDB understands.  It also indicates
346  * which hardware traps we need to commandeer when initializing the stub.
347  */
348 static struct hard_trap_info {
349         unsigned char tt;               /* Trap type code for MIPS R3xxx and R4xxx */
350         unsigned char signo;            /* Signal that we map this trap into */
351 } hard_trap_info[] = {
352         { 6, SIGBUS },                  /* instruction bus error */
353         { 7, SIGBUS },                  /* data bus error */
354         { 9, SIGTRAP },                 /* break */
355         { 10, SIGILL },                 /* reserved instruction */
356 /*      { 11, SIGILL },         */      /* CPU unusable */
357         { 12, SIGFPE },                 /* overflow */
358         { 13, SIGTRAP },                /* trap */
359         { 14, SIGSEGV },                /* virtual instruction cache coherency */
360         { 15, SIGFPE },                 /* floating point exception */
361         { 23, SIGSEGV },                /* watch */
362         { 31, SIGSEGV },                /* virtual data cache coherency */
363         { 0, 0}                         /* Must be last */
364 };
365
366 /* Save the normal trap handlers for user-mode traps. */
367 void *saved_vectors[32];
368
369 /*
370  * Set up exception handlers for tracing and breakpoints
371  */
372 void set_debug_traps(void)
373 {
374         struct hard_trap_info *ht;
375         unsigned long flags;
376         unsigned char c;
377
378         local_irq_save(flags);
379         for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
380                 saved_vectors[ht->tt] = set_except_vector(ht->tt, trap_low);
381
382         putDebugChar('+'); /* 'hello world' */
383         /*
384          * In case GDB is started before us, ack any packets
385          * (presumably "$?#xx") sitting there.
386          */
387         while((c = getDebugChar()) != '$');
388         while((c = getDebugChar()) != '#');
389         c = getDebugChar(); /* eat first csum byte */
390         c = getDebugChar(); /* eat second csum byte */
391         putDebugChar('+'); /* ack it */
392
393         initialized = 1;
394         local_irq_restore(flags);
395 }
396
397 /*
398  * Convert the MIPS hardware trap type code to a Unix signal number.
399  */
400 static int computeSignal(int tt)
401 {
402         struct hard_trap_info *ht;
403
404         for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
405                 if (ht->tt == tt)
406                         return ht->signo;
407
408         return SIGHUP;          /* default for things we don't know about */
409 }
410
411 /*
412  * While we find nice hex chars, build an int.
413  * Return number of chars processed.
414  */
415 static int hexToInt(char **ptr, int *intValue)
416 {
417         int numChars = 0;
418         int hexValue;
419
420         *intValue = 0;
421
422         while (**ptr) {
423                 hexValue = hex(**ptr);
424                 if (hexValue < 0)
425                         break;
426
427                 *intValue = (*intValue << 4) | hexValue;
428                 numChars ++;
429
430                 (*ptr)++;
431         }
432
433         return (numChars);
434 }
435
436
437 #if 0
438 /*
439  * Print registers (on target console)
440  * Used only to debug the stub...
441  */
442 void show_gdbregs(struct gdb_regs * regs)
443 {
444         /*
445          * Saved main processor registers
446          */
447         printk("$0 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
448                regs->reg0, regs->reg1, regs->reg2, regs->reg3,
449                regs->reg4, regs->reg5, regs->reg6, regs->reg7);
450         printk("$8 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
451                regs->reg8, regs->reg9, regs->reg10, regs->reg11,
452                regs->reg12, regs->reg13, regs->reg14, regs->reg15);
453         printk("$16: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
454                regs->reg16, regs->reg17, regs->reg18, regs->reg19,
455                regs->reg20, regs->reg21, regs->reg22, regs->reg23);
456         printk("$24: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
457                regs->reg24, regs->reg25, regs->reg26, regs->reg27,
458                regs->reg28, regs->reg29, regs->reg30, regs->reg31);
459
460         /*
461          * Saved cp0 registers
462          */
463         printk("epc  : %08lx\nStatus: %08lx\nCause : %08lx\n",
464                regs->cp0_epc, regs->cp0_status, regs->cp0_cause);
465 }
466 #endif /* dead code */
467
468 /*
469  * We single-step by setting breakpoints. When an exception
470  * is handled, we need to restore the instructions hoisted
471  * when the breakpoints were set.
472  *
473  * This is where we save the original instructions.
474  */
475 static struct gdb_bp_save {
476         unsigned int addr;
477         unsigned int val;
478 } step_bp[2];
479
480 #define BP 0x0000000d  /* break opcode */
481
482 /*
483  * Set breakpoint instructions for single stepping.
484  */
485 static void single_step(struct gdb_regs *regs)
486 {
487         union mips_instruction insn;
488         unsigned int targ;
489         int is_branch, is_cond, i;
490
491         targ = regs->cp0_epc;
492         insn.word = *(unsigned int *)targ;
493         is_branch = is_cond = 0;
494
495         switch (insn.i_format.opcode) {
496         /*
497          * jr and jalr are in r_format format.
498          */
499         case spec_op:
500                 switch (insn.r_format.func) {
501                 case jalr_op:
502                 case jr_op:
503                         targ = *(&regs->reg0 + insn.r_format.rs);
504                         is_branch = 1;
505                         break;
506                 }
507                 break;
508
509         /*
510          * This group contains:
511          * bltz_op, bgez_op, bltzl_op, bgezl_op,
512          * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
513          */
514         case bcond_op:
515                 is_branch = is_cond = 1;
516                 targ += 4 + (insn.i_format.simmediate << 2);
517                 break;
518
519         /*
520          * These are unconditional and in j_format.
521          */
522         case jal_op:
523         case j_op:
524                 is_branch = 1;
525                 targ += 4;
526                 targ >>= 28;
527                 targ <<= 28;
528                 targ |= (insn.j_format.target << 2);
529                 break;
530
531         /*
532          * These are conditional.
533          */
534         case beq_op:
535         case beql_op:
536         case bne_op:
537         case bnel_op:
538         case blez_op:
539         case blezl_op:
540         case bgtz_op:
541         case bgtzl_op:
542         case cop0_op:
543         case cop1_op:
544         case cop2_op:
545         case cop1x_op:
546                 is_branch = is_cond = 1;
547                 targ += 4 + (insn.i_format.simmediate << 2);
548                 break;
549         }
550
551         if (is_branch) {
552                 i = 0;
553                 if (is_cond && targ != (regs->cp0_epc + 8)) {
554                         step_bp[i].addr = regs->cp0_epc + 8;
555                         step_bp[i++].val = *(unsigned *)(regs->cp0_epc + 8);
556                         *(unsigned *)(regs->cp0_epc + 8) = BP;
557                 }
558                 step_bp[i].addr = targ;
559                 step_bp[i].val  = *(unsigned *)targ;
560                 *(unsigned *)targ = BP;
561         } else {
562                 step_bp[0].addr = regs->cp0_epc + 4;
563                 step_bp[0].val  = *(unsigned *)(regs->cp0_epc + 4);
564                 *(unsigned *)(regs->cp0_epc + 4) = BP;
565         }
566 }
567
568 /*
569  *  If asynchronously interrupted by gdb, then we need to set a breakpoint
570  *  at the interrupted instruction so that we wind up stopped with a
571  *  reasonable stack frame.
572  */
573 static struct gdb_bp_save async_bp;
574
575 void set_async_breakpoint(unsigned int epc)
576 {
577         async_bp.addr = epc;
578         async_bp.val  = *(unsigned *)epc;
579         *(unsigned *)epc = BP;
580         __flush_cache_all();
581 }
582
583
584 /*
585  * This function does all command processing for interfacing to gdb.  It
586  * returns 1 if you should skip the instruction at the trap address, 0
587  * otherwise.
588  */
589 void handle_exception (struct gdb_regs *regs)
590 {
591         int trap;                       /* Trap type */
592         int sigval;
593         int addr;
594         int length;
595         char *ptr;
596         unsigned long *stack;
597
598         /*
599          * If we're in breakpoint() increment the PC
600          */
601         trap = (regs->cp0_cause & 0x7c) >> 2;
602         if (trap == 9 && regs->cp0_epc == (unsigned long)breakinst)
603                 regs->cp0_epc += 4;
604
605         /*
606          * If we were single_stepping, restore the opcodes hoisted
607          * for the breakpoint[s].
608          */
609         if (step_bp[0].addr) {
610                 *(unsigned *)step_bp[0].addr = step_bp[0].val;
611                 step_bp[0].addr = 0;
612
613                 if (step_bp[1].addr) {
614                         *(unsigned *)step_bp[1].addr = step_bp[1].val;
615                         step_bp[1].addr = 0;
616                 }
617         }
618
619         /*
620          * If we were interrupted asynchronously by gdb, then a
621          * breakpoint was set at the EPC of the interrupt so
622          * that we'd wind up here with an interesting stack frame.
623          */
624         if (async_bp.addr) {
625                 *(unsigned *)async_bp.addr = async_bp.val;
626                 async_bp.addr = 0;
627         }
628
629         stack = (long *)regs->reg29;                    /* stack ptr */
630         sigval = computeSignal(trap);
631
632         /*
633          * reply to host that an exception has occurred
634          */
635         ptr = output_buffer;
636
637         /*
638          * Send trap type (converted to signal)
639          */
640         *ptr++ = 'T';
641         *ptr++ = hexchars[sigval >> 4];
642         *ptr++ = hexchars[sigval & 0xf];
643
644         /*
645          * Send Error PC
646          */
647         *ptr++ = hexchars[REG_EPC >> 4];
648         *ptr++ = hexchars[REG_EPC & 0xf];
649         *ptr++ = ':';
650         ptr = mem2hex((char *)&regs->cp0_epc, ptr, 4, 0);
651         *ptr++ = ';';
652
653         /*
654          * Send frame pointer
655          */
656         *ptr++ = hexchars[REG_FP >> 4];
657         *ptr++ = hexchars[REG_FP & 0xf];
658         *ptr++ = ':';
659         ptr = mem2hex((char *)&regs->reg30, ptr, 4, 0);
660         *ptr++ = ';';
661
662         /*
663          * Send stack pointer
664          */
665         *ptr++ = hexchars[REG_SP >> 4];
666         *ptr++ = hexchars[REG_SP & 0xf];
667         *ptr++ = ':';
668         ptr = mem2hex((char *)&regs->reg29, ptr, 4, 0);
669         *ptr++ = ';';
670
671         *ptr++ = 0;
672         putpacket(output_buffer);       /* send it off... */
673
674         /*
675          * Wait for input from remote GDB
676          */
677         while (1) {
678                 output_buffer[0] = 0;
679                 getpacket(input_buffer);
680
681                 switch (input_buffer[0])
682                 {
683                 case '?':
684                         output_buffer[0] = 'S';
685                         output_buffer[1] = hexchars[sigval >> 4];
686                         output_buffer[2] = hexchars[sigval & 0xf];
687                         output_buffer[3] = 0;
688                         break;
689
690                 case 'D':
691                         /* detach; let CPU run */
692                         putpacket(output_buffer);
693                         return;
694
695                 case 'd':
696                         /* toggle debug flag */
697                         break;
698
699                 /*
700                  * Return the value of the CPU registers
701                  */
702                 case 'g':
703                         ptr = output_buffer;
704                         ptr = mem2hex((char *)&regs->reg0, ptr, 32*4, 0); /* r0...r31 */
705                         ptr = mem2hex((char *)&regs->cp0_status, ptr, 6*4, 0); /* cp0 */
706                         ptr = mem2hex((char *)&regs->fpr0, ptr, 32*4, 0); /* f0...31 */
707                         ptr = mem2hex((char *)&regs->cp1_fsr, ptr, 2*4, 0); /* cp1 */
708                         ptr = mem2hex((char *)&regs->frame_ptr, ptr, 2*4, 0); /* frp */
709                         ptr = mem2hex((char *)&regs->cp0_index, ptr, 16*4, 0); /* cp0 */
710                         break;
711
712                 /*
713                  * set the value of the CPU registers - return OK
714                  */
715                 case 'G':
716                 {
717                         ptr = &input_buffer[1];
718                         hex2mem(ptr, (char *)&regs->reg0, 32*4, 0);
719                         ptr += 32*8;
720                         hex2mem(ptr, (char *)&regs->cp0_status, 6*4, 0);
721                         ptr += 6*8;
722                         hex2mem(ptr, (char *)&regs->fpr0, 32*4, 0);
723                         ptr += 32*8;
724                         hex2mem(ptr, (char *)&regs->cp1_fsr, 2*4, 0);
725                         ptr += 2*8;
726                         hex2mem(ptr, (char *)&regs->frame_ptr, 2*4, 0);
727                         ptr += 2*8;
728                         hex2mem(ptr, (char *)&regs->cp0_index, 16*4, 0);
729                         strcpy(output_buffer,"OK");
730                  }
731                 break;
732
733                 /*
734                  * mAA..AA,LLLL  Read LLLL bytes at address AA..AA
735                  */
736                 case 'm':
737                         ptr = &input_buffer[1];
738
739                         if (hexToInt(&ptr, &addr)
740                                 && *ptr++ == ','
741                                 && hexToInt(&ptr, &length)) {
742                                 if (mem2hex((char *)addr, output_buffer, length, 1))
743                                         break;
744                                 strcpy (output_buffer, "E03");
745                         } else
746                                 strcpy(output_buffer,"E01");
747                         break;
748
749                 /*
750                  * MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK
751                  */
752                 case 'M':
753                         ptr = &input_buffer[1];
754
755                         if (hexToInt(&ptr, &addr)
756                                 && *ptr++ == ','
757                                 && hexToInt(&ptr, &length)
758                                 && *ptr++ == ':') {
759                                 if (hex2mem(ptr, (char *)addr, length, 1))
760                                         strcpy(output_buffer, "OK");
761                                 else
762                                         strcpy(output_buffer, "E03");
763                         }
764                         else
765                                 strcpy(output_buffer, "E02");
766                         break;
767
768                 /*
769                  * cAA..AA    Continue at address AA..AA(optional)
770                  */
771                 case 'c':
772                         /* try to read optional parameter, pc unchanged if no parm */
773
774                         ptr = &input_buffer[1];
775                         if (hexToInt(&ptr, &addr))
776                                 regs->cp0_epc = addr;
777
778                         /*
779                          * Need to flush the instruction cache here, as we may
780                          * have deposited a breakpoint, and the icache probably
781                          * has no way of knowing that a data ref to some location
782                          * may have changed something that is in the instruction
783                          * cache.
784                          * NB: We flush both caches, just to be sure...
785                          */
786
787                         __flush_cache_all();
788                         return;
789                         /* NOTREACHED */
790                         break;
791
792
793                 /*
794                  * kill the program; let us try to restart the machine
795                  * Reset the whole machine.
796                  */
797                 case 'k':
798                 case 'r':
799                         machine_restart("kgdb restarts machine");
800                         break;
801
802                 /*
803                  * Step to next instruction
804                  */
805                 case 's':
806                         /*
807                          * There is no single step insn in the MIPS ISA, so we
808                          * use breakpoints and continue, instead.
809                          */
810                         single_step(regs);
811                         __flush_cache_all();
812                         return;
813                         /* NOTREACHED */
814
815                 /*
816                  * Set baud rate (bBB)
817                  * FIXME: Needs to be written
818                  */
819                 case 'b':
820                 {
821 #if 0
822                         int baudrate;
823                         extern void set_timer_3();
824
825                         ptr = &input_buffer[1];
826                         if (!hexToInt(&ptr, &baudrate))
827                         {
828                                 strcpy(output_buffer,"B01");
829                                 break;
830                         }
831
832                         /* Convert baud rate to uart clock divider */
833
834                         switch (baudrate)
835                         {
836                                 case 38400:
837                                         baudrate = 16;
838                                         break;
839                                 case 19200:
840                                         baudrate = 33;
841                                         break;
842                                 case 9600:
843                                         baudrate = 65;
844                                         break;
845                                 default:
846                                         baudrate = 0;
847                                         strcpy(output_buffer,"B02");
848                                         goto x1;
849                         }
850
851                         if (baudrate) {
852                                 putpacket("OK");        /* Ack before changing speed */
853                                 set_timer_3(baudrate); /* Set it */
854                         }
855 #endif
856                 }
857                 break;
858
859                 }                       /* switch */
860
861                 /*
862                  * reply to the request
863                  */
864
865                 putpacket(output_buffer);
866
867         } /* while */
868 }
869
870 /*
871  * This function will generate a breakpoint exception.  It is used at the
872  * beginning of a program to sync up with a debugger and can be used
873  * otherwise as a quick means to stop program execution and "break" into
874  * the debugger.
875  */
876 void breakpoint(void)
877 {
878         if (!initialized)
879                 return;
880
881         __asm__ __volatile__(
882                         ".globl breakinst\n\t" 
883                         ".set\tnoreorder\n\t"
884                         "nop\n\t"
885                         "breakinst:\tbreak\n\t"
886                         "nop\n\t"
887                         ".set\treorder"
888                         );
889 }
890
891 void adel(void)
892 {
893         __asm__ __volatile__(
894                         ".globl\tadel\n\t"
895                         "la\t$8,0x80000001\n\t"
896                         "lw\t$9,0($8)\n\t"
897                         );
898 }
899
900 /*
901  * malloc is needed by gdb client in "call func()", even a private one
902  * will make gdb happy
903  */
904 static void *malloc(size_t size)
905 {
906         return kmalloc(size, GFP_ATOMIC);
907 }
908
909 static void free(void *where)
910 {
911         kfree(where);
912 }
913
914 #ifdef CONFIG_GDB_CONSOLE
915
916 void gdb_putsn(const char *str, int l)
917 {
918         char outbuf[18];
919
920         outbuf[0]='O';
921
922         while(l) {
923                 int i = (l>8)?8:l;
924                 mem2hex((char *)str, &outbuf[1], i, 0);
925                 outbuf[(i*2)+1]=0;
926                 putpacket(outbuf);
927                 str += i;
928                 l -= i;
929         }
930 }
931
932 static void gdb_console_write(struct console *con, const char *s, unsigned n)
933 {
934         gdb_putsn(s, n);
935 }
936
937 static struct console gdb_console = {
938         .name   = "gdb",
939         .write  = gdb_console_write,
940         .flags  = CON_PRINTBUFFER,
941         .index  = -1
942 };
943
944 __init void register_gdb_console(void)
945 {
946         register_console(&gdb_console);
947 }
948
949 #endif