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