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