~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/mips/kernel/gdb-stub.c

Version: ~ [ linux-5.0-rc6 ] ~ [ linux-4.20.10 ] ~ [ linux-4.19.23 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.101 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.158 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.174 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.134 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp