2 * ppc-stub.c: KGDB support for the Linux kernel.
4 * adapted from arch/sparc/kernel/sparc-stub.c for the PowerPC
5 * some stuff borrowed from Paul Mackerras' xmon
6 * Copyright (C) 1998 Michael AK Tesch (tesch@cs.wisc.edu)
8 * Modifications to run under Linux
9 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
11 * This file originally came from the gdb sources, and the
12 * copyright notices have been retained below.
15 /****************************************************************************
17 THIS SOFTWARE IS NOT COPYRIGHTED
19 HP offers the following for use in the public domain. HP makes no
20 warranty with regard to the software or its performance and the
21 user accepts the software "AS IS" with all faults.
23 HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
24 TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 ****************************************************************************/
29 /****************************************************************************
30 * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
32 * Module name: remcom.c $
34 * Date: 91/03/09 12:29:49 $
35 * Contributor: Lake Stevens Instrument Division$
37 * Description: low level support for gdb debugger. $
39 * Considerations: only works on target hardware $
41 * Written by: Glenn Engel $
42 * ModuleState: Experimental $
46 * Modified for SPARC by Stu Grossman, Cygnus Support.
48 * This code has been extensively tested on the Fujitsu SPARClite demo board.
50 * To enable debugger support, two things need to happen. One, a
51 * call to set_debug_traps() is necessary in order to allow any breakpoints
52 * or error conditions to be properly intercepted and reported to gdb.
53 * Two, a breakpoint needs to be generated to begin communication. This
54 * is most easily accomplished by a call to breakpoint(). Breakpoint()
55 * simulates a breakpoint by executing a trap #1.
59 * The following gdb commands are supported:
61 * command function Return value
63 * g return the value of the CPU registers hex data or ENN
64 * G set the value of the CPU registers OK or ENN
65 * qOffsets Get section offsets. Reply is Text=xxx;Data=yyy;Bss=zzz
67 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
68 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
70 * c Resume at current address SNN ( signal NN)
71 * cAA..AA Continue at address AA..AA SNN
73 * s Step one instruction SNN
74 * sAA..AA Step one instruction from AA..AA SNN
78 * ? What was the last sigval ? SNN (signal NN)
80 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
83 * All commands and responses are sent with a packet which includes a
84 * checksum. A packet consists of
86 * $<packet info>#<checksum>.
89 * <packet info> :: <characters representing the command or response>
90 * <checksum> :: <two hex digits computed as modulo 256 sum of <packetinfo>>
92 * When a packet is received, it is first acknowledged with either '+' or '-'.
93 * '+' indicates a successful transfer. '-' indicates a failed transfer.
98 * $m0,10#2a +$00010203040506070809101112131415#42
100 ****************************************************************************/
102 #include <linux/kernel.h>
103 #include <linux/string.h>
104 #include <linux/mm.h>
105 #include <linux/smp.h>
106 #include <linux/smp_lock.h>
107 #include <linux/init.h>
108 #include <linux/sysrq.h>
110 #include <asm/cacheflush.h>
111 #include <asm/system.h>
112 #include <asm/signal.h>
113 #include <asm/kgdb.h>
114 #include <asm/pgtable.h>
115 #include <asm/ptrace.h>
117 void breakinst(void);
120 * BUFMAX defines the maximum number of characters in inbound/outbound buffers
121 * at least NUMREGBYTES*2 are needed for register packets
124 static char remcomInBuffer[BUFMAX];
125 static char remcomOutBuffer[BUFMAX];
127 static int initialized;
128 static int kgdb_active;
129 static int kgdb_started;
130 static u_int fault_jmp_buf[100];
134 static const char hexchars[]="0123456789abcdef";
136 /* Place where we save old trap entries for restoration - sparc*/
137 /* struct tt_entry kgdb_savettable[256]; */
138 /* typedef void (*trapfunc_t)(void); */
140 static void kgdb_fault_handler(struct pt_regs *regs);
141 static int handle_exception (struct pt_regs *regs);
144 /* Install an exception handler for kgdb */
145 static void exceptionHandler(int tnum, unsigned int *tfunc)
147 /* We are dorking with a live trap table, all irqs off */
152 kgdb_setjmp(long *buf)
154 asm ("mflr 0; stw 0,0(%0);"
155 "stw 1,4(%0); stw 2,8(%0);"
156 "mfcr 0; stw 0,12(%0);"
159 /* XXX should save fp regs as well */
163 kgdb_longjmp(long *buf, int val)
167 asm ("lmw 13,16(%0);"
168 "lwz 0,12(%0); mtcrf 0x38,0;"
169 "lwz 0,0(%0); lwz 1,4(%0); lwz 2,8(%0);"
171 : : "r" (buf), "r" (val));
173 /* Convert ch from a hex digit to an int */
175 hex(unsigned char ch)
177 if (ch >= 'a' && ch <= 'f')
179 if (ch >= '0' && ch <= '9')
181 if (ch >= 'A' && ch <= 'F')
186 /* Convert the memory pointed to by mem into hex, placing result in buf.
187 * Return a pointer to the last char put in buf (null), in case of mem fault,
190 static unsigned char *
191 mem2hex(const char *mem, char *buf, int count)
194 unsigned short tmp_s;
197 if (kgdb_setjmp((long*)fault_jmp_buf) == 0) {
198 debugger_fault_handler = kgdb_fault_handler;
200 /* Accessing 16 bit and 32 bit objects in a single
201 ** load instruction is required to avoid bad side
202 ** effects for some IO registers.
205 if ((count == 2) && (((long)mem & 1) == 0)) {
206 tmp_s = *(unsigned short *)mem;
208 *buf++ = hexchars[(tmp_s >> 12) & 0xf];
209 *buf++ = hexchars[(tmp_s >> 8) & 0xf];
210 *buf++ = hexchars[(tmp_s >> 4) & 0xf];
211 *buf++ = hexchars[tmp_s & 0xf];
213 } else if ((count == 4) && (((long)mem & 3) == 0)) {
214 tmp_l = *(unsigned int *)mem;
216 *buf++ = hexchars[(tmp_l >> 28) & 0xf];
217 *buf++ = hexchars[(tmp_l >> 24) & 0xf];
218 *buf++ = hexchars[(tmp_l >> 20) & 0xf];
219 *buf++ = hexchars[(tmp_l >> 16) & 0xf];
220 *buf++ = hexchars[(tmp_l >> 12) & 0xf];
221 *buf++ = hexchars[(tmp_l >> 8) & 0xf];
222 *buf++ = hexchars[(tmp_l >> 4) & 0xf];
223 *buf++ = hexchars[tmp_l & 0xf];
226 while (count-- > 0) {
228 *buf++ = hexchars[ch >> 4];
229 *buf++ = hexchars[ch & 0xf];
234 /* error condition */
236 debugger_fault_handler = NULL;
241 /* convert the hex array pointed to by buf into binary to be placed in mem
242 * return a pointer to the character AFTER the last byte written.
245 hex2mem(char *buf, char *mem, int count)
250 unsigned short tmp_s;
255 if (kgdb_setjmp((long*)fault_jmp_buf) == 0) {
256 debugger_fault_handler = kgdb_fault_handler;
258 /* Accessing 16 bit and 32 bit objects in a single
259 ** store instruction is required to avoid bad side
260 ** effects for some IO registers.
263 if ((count == 2) && (((long)mem & 1) == 0)) {
264 tmp_s = hex(*buf++) << 12;
265 tmp_s |= hex(*buf++) << 8;
266 tmp_s |= hex(*buf++) << 4;
267 tmp_s |= hex(*buf++);
269 *(unsigned short *)mem = tmp_s;
272 } else if ((count == 4) && (((long)mem & 3) == 0)) {
273 tmp_l = hex(*buf++) << 28;
274 tmp_l |= hex(*buf++) << 24;
275 tmp_l |= hex(*buf++) << 20;
276 tmp_l |= hex(*buf++) << 16;
277 tmp_l |= hex(*buf++) << 12;
278 tmp_l |= hex(*buf++) << 8;
279 tmp_l |= hex(*buf++) << 4;
280 tmp_l |= hex(*buf++);
282 *(unsigned long *)mem = tmp_l;
286 for (i=0; i<count; i++) {
287 ch = hex(*buf++) << 4;
295 ** Flush the data cache, invalidate the instruction cache.
297 flush_icache_range((int)orig_mem, (int)orig_mem + count - 1);
300 /* error condition */
302 debugger_fault_handler = NULL;
307 * While we find nice hex chars, build an int.
308 * Return number of chars processed.
311 hexToInt(char **ptr, int *intValue)
318 if (kgdb_setjmp((long*)fault_jmp_buf) == 0) {
319 debugger_fault_handler = kgdb_fault_handler;
321 hexValue = hex(**ptr);
325 *intValue = (*intValue << 4) | hexValue;
331 /* error condition */
333 debugger_fault_handler = NULL;
338 /* scan for the sequence $<data>#<checksum> */
340 getpacket(char *buffer)
342 unsigned char checksum;
343 unsigned char xmitcsum;
349 /* wait around for the start character, ignore all other
351 while ((ch = (getDebugChar() & 0x7f)) != '$') ;
358 /* now, read until a # or end of buffer is found */
359 while (count < BUFMAX) {
360 ch = getDebugChar() & 0x7f;
363 checksum = checksum + ch;
374 xmitcsum = hex(getDebugChar() & 0x7f) << 4;
375 xmitcsum |= hex(getDebugChar() & 0x7f);
376 if (checksum != xmitcsum)
377 putDebugChar('-'); /* failed checksum */
379 putDebugChar('+'); /* successful transfer */
380 /* if a sequence char is present, reply the ID */
381 if (buffer[2] == ':') {
382 putDebugChar(buffer[0]);
383 putDebugChar(buffer[1]);
384 /* remove sequence chars from buffer */
385 count = strlen(buffer);
386 for (i=3; i <= count; i++)
387 buffer[i-3] = buffer[i];
391 } while (checksum != xmitcsum);
394 /* send the packet in buffer. */
395 static void putpacket(unsigned char *buffer)
397 unsigned char checksum;
399 unsigned char ch, recv;
401 /* $<packet info>#<checksum>. */
407 while ((ch = buffer[count])) {
414 putDebugChar(hexchars[checksum >> 4]);
415 putDebugChar(hexchars[checksum & 0xf]);
416 recv = getDebugChar();
417 } while ((recv & 0x7f) != '+');
420 static void kgdb_flush_cache_all(void)
422 flush_instruction_cache();
425 /* Set up exception handlers for tracing and breakpoints
426 * [could be called kgdb_init()]
428 void set_debug_traps(void)
435 /* In case GDB is started before us, ack any packets (presumably
436 * "$?#xx") sitting there.
438 * I've found this code causes more problems than it solves,
439 * so that's why it's commented out. GDB seems to work fine
440 * now starting either before or after the kernel -bwb
443 while((c = getDebugChar()) != '$');
444 while((c = getDebugChar()) != '#');
445 c = getDebugChar(); /* eat first csum byte */
446 c = getDebugChar(); /* eat second csum byte */
447 putDebugChar('+'); /* ack it */
450 debugger_bpt = kgdb_bpt;
451 debugger_sstep = kgdb_sstep;
452 debugger_iabr_match = kgdb_iabr_match;
453 debugger_dabr_match = kgdb_dabr_match;
458 static void kgdb_fault_handler(struct pt_regs *regs)
460 kgdb_longjmp((long*)fault_jmp_buf, 1);
463 int kgdb_bpt(struct pt_regs *regs)
465 return handle_exception(regs);
468 int kgdb_sstep(struct pt_regs *regs)
470 return handle_exception(regs);
473 void kgdb(struct pt_regs *regs)
475 handle_exception(regs);
478 int kgdb_iabr_match(struct pt_regs *regs)
480 printk(KERN_ERR "kgdb doesn't support iabr, what?!?\n");
481 return handle_exception(regs);
484 int kgdb_dabr_match(struct pt_regs *regs)
486 printk(KERN_ERR "kgdb doesn't support dabr, what?!?\n");
487 return handle_exception(regs);
490 /* Convert the hardware trap type code to a unix signal number. */
492 * This table contains the mapping between PowerPC hardware trap types, and
493 * signals, which are primarily what GDB understands.
495 static struct hard_trap_info
497 unsigned int tt; /* Trap type code for powerpc */
498 unsigned char signo; /* Signal that we map this trap into */
499 } hard_trap_info[] = {
500 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
501 { 0x100, SIGINT }, /* critical input interrupt */
502 { 0x200, SIGSEGV }, /* machine check */
503 { 0x300, SIGSEGV }, /* data storage */
504 { 0x400, SIGBUS }, /* instruction storage */
505 { 0x500, SIGINT }, /* interrupt */
506 { 0x600, SIGBUS }, /* alignment */
507 { 0x700, SIGILL }, /* program */
508 { 0x800, SIGILL }, /* reserved */
509 { 0x900, SIGILL }, /* reserved */
510 { 0xa00, SIGILL }, /* reserved */
511 { 0xb00, SIGILL }, /* reserved */
512 { 0xc00, SIGCHLD }, /* syscall */
513 { 0xd00, SIGILL }, /* reserved */
514 { 0xe00, SIGILL }, /* reserved */
515 { 0xf00, SIGILL }, /* reserved */
520 ** 0x1100 data TLB miss
521 ** 0x1200 instruction TLB miss
523 { 0x2002, SIGTRAP}, /* debug */
525 { 0x200, SIGSEGV }, /* machine check */
526 { 0x300, SIGSEGV }, /* address error (store) */
527 { 0x400, SIGBUS }, /* instruction bus error */
528 { 0x500, SIGINT }, /* interrupt */
529 { 0x600, SIGBUS }, /* alingment */
530 { 0x700, SIGTRAP }, /* breakpoint trap */
531 { 0x800, SIGFPE }, /* fpu unavail */
532 { 0x900, SIGALRM }, /* decrementer */
533 { 0xa00, SIGILL }, /* reserved */
534 { 0xb00, SIGILL }, /* reserved */
535 { 0xc00, SIGCHLD }, /* syscall */
536 { 0xd00, SIGTRAP }, /* single-step/watch */
537 { 0xe00, SIGFPE }, /* fp assist */
539 { 0, 0} /* Must be last */
543 static int computeSignal(unsigned int tt)
545 struct hard_trap_info *ht;
547 for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
551 return SIGHUP; /* default for things we don't know about */
558 * This function does all command processing for interfacing to gdb.
561 handle_exception (struct pt_regs *regs)
569 /* We don't handle user-mode breakpoints. */
573 if (debugger_fault_handler) {
574 debugger_fault_handler(regs);
575 panic("kgdb longjump failed!\n");
578 printk(KERN_ERR "interrupt while in kgdb, returning\n");
586 printk("kgdb: entering handle_exception; trap [0x%x]\n",
587 (unsigned int)regs->trap);
590 kgdb_interruptible(0);
593 mtmsr(msr & ~MSR_EE); /* disable interrupts */
595 if (regs->nip == (unsigned long)breakinst) {
596 /* Skip over breakpoint trap insn */
600 /* reply to host that an exception has occurred */
601 sigval = computeSignal(regs->trap);
602 ptr = remcomOutBuffer;
605 *ptr++ = hexchars[sigval >> 4];
606 *ptr++ = hexchars[sigval & 0xf];
607 *ptr++ = hexchars[PC_REGNUM >> 4];
608 *ptr++ = hexchars[PC_REGNUM & 0xf];
610 ptr = mem2hex((char *)®s->nip, ptr, 4);
612 *ptr++ = hexchars[SP_REGNUM >> 4];
613 *ptr++ = hexchars[SP_REGNUM & 0xf];
615 ptr = mem2hex(((char *)regs) + SP_REGNUM*4, ptr, 4);
619 putpacket(remcomOutBuffer);
621 printk("remcomOutBuffer: %s\n", remcomOutBuffer);
623 /* XXX We may want to add some features dealing with poking the
624 * XXX page tables, ... (look at sparc-stub.c for more info)
625 * XXX also required hacking to the gdb sources directly...
629 remcomOutBuffer[0] = 0;
631 getpacket(remcomInBuffer);
632 switch (remcomInBuffer[0]) {
633 case '?': /* report most recent signal */
634 remcomOutBuffer[0] = 'S';
635 remcomOutBuffer[1] = hexchars[sigval >> 4];
636 remcomOutBuffer[2] = hexchars[sigval & 0xf];
637 remcomOutBuffer[3] = 0;
640 case 'q': /* this screws up gdb for some reason...*/
642 extern long _start, sdata, __bss_start;
644 ptr = &remcomInBuffer[1];
645 if (strncmp(ptr, "Offsets", 7) != 0)
648 ptr = remcomOutBuffer;
649 sprintf(ptr, "Text=%8.8x;Data=%8.8x;Bss=%8.8x",
650 &_start, &sdata, &__bss_start);
655 /* toggle debug flag */
659 case 'g': /* return the value of the CPU registers.
660 * some of them are non-PowerPC names :(
661 * they are stored in gdb like:
665 * u32 pc, ps, cnd, lr; (ps=msr)
671 ptr = remcomOutBuffer;
672 /* General Purpose Regs */
673 ptr = mem2hex((char *)regs, ptr, 32 * 4);
674 /* Floating Point Regs - FIXME */
675 /*ptr = mem2hex((char *), ptr, 32 * 8);*/
676 for(i=0; i<(32*8*2); i++) { /* 2chars/byte */
680 /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
681 ptr = mem2hex((char *)®s->nip, ptr, 4);
682 ptr = mem2hex((char *)®s->msr, ptr, 4);
683 ptr = mem2hex((char *)®s->ccr, ptr, 4);
684 ptr = mem2hex((char *)®s->link, ptr, 4);
685 ptr = mem2hex((char *)®s->ctr, ptr, 4);
686 ptr = mem2hex((char *)®s->xer, ptr, 4);
690 case 'G': /* set the value of the CPU registers */
692 ptr = &remcomInBuffer[1];
695 * If the stack pointer has moved, you should pray.
696 * (cause only god can help you).
699 /* General Purpose Regs */
700 hex2mem(ptr, (char *)regs, 32 * 4);
702 /* Floating Point Regs - FIXME?? */
703 /*ptr = hex2mem(ptr, ??, 32 * 8);*/
706 /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
707 ptr = hex2mem(ptr, (char *)®s->nip, 4);
708 ptr = hex2mem(ptr, (char *)®s->msr, 4);
709 ptr = hex2mem(ptr, (char *)®s->ccr, 4);
710 ptr = hex2mem(ptr, (char *)®s->link, 4);
711 ptr = hex2mem(ptr, (char *)®s->ctr, 4);
712 ptr = hex2mem(ptr, (char *)®s->xer, 4);
714 strcpy(remcomOutBuffer,"OK");
718 /* don't do anything, yet, just acknowledge */
719 hexToInt(&ptr, &addr);
720 strcpy(remcomOutBuffer,"OK");
723 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
724 /* Try to read %x,%x. */
726 ptr = &remcomInBuffer[1];
728 if (hexToInt(&ptr, &addr) && *ptr++ == ','
729 && hexToInt(&ptr, &length)) {
730 if (mem2hex((char *)addr, remcomOutBuffer,
733 strcpy(remcomOutBuffer, "E03");
735 strcpy(remcomOutBuffer, "E01");
738 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
739 /* Try to read '%x,%x:'. */
741 ptr = &remcomInBuffer[1];
743 if (hexToInt(&ptr, &addr) && *ptr++ == ','
744 && hexToInt(&ptr, &length)
746 if (hex2mem(ptr, (char *)addr, length))
747 strcpy(remcomOutBuffer, "OK");
749 strcpy(remcomOutBuffer, "E03");
750 flush_icache_range(addr, addr+length);
752 strcpy(remcomOutBuffer, "E02");
756 case 'k': /* kill the program, actually just continue */
757 case 'c': /* cAA..AA Continue; address AA..AA optional */
758 /* try to read optional parameter, pc unchanged if no parm */
760 ptr = &remcomInBuffer[1];
761 if (hexToInt(&ptr, &addr))
764 /* Need to flush the instruction cache here, as we may have deposited a
765 * breakpoint, and the icache probably has no way of knowing that a data ref to
766 * some location may have changed something that is in the instruction cache.
768 kgdb_flush_cache_all();
771 kgdb_interruptible(1);
775 printk("remcomInBuffer: %s\n", remcomInBuffer);
776 printk("remcomOutBuffer: %s\n", remcomOutBuffer);
781 kgdb_flush_cache_all();
782 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
783 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | DBCR0_IC);
791 printk("remcomInBuffer: %s\n", remcomInBuffer);
792 printk("remcomOutBuffer: %s\n", remcomOutBuffer);
796 case 'r': /* Reset (if user process..exit ???)*/
797 panic("kgdb reset.");
800 if (remcomOutBuffer[0] && kdebug) {
801 printk("remcomInBuffer: %s\n", remcomInBuffer);
802 printk("remcomOutBuffer: %s\n", remcomOutBuffer);
804 /* reply to the request */
805 putpacket(remcomOutBuffer);
809 /* This function will generate a breakpoint exception. It is used at the
810 beginning of a program to sync up with a debugger and can be used
811 otherwise as a quick means to stop program execution and "break" into
818 printk("breakpoint() called b4 kgdb init\n");
822 asm(" .globl breakinst \n\
823 breakinst: .long 0x7d821008");
826 #ifdef CONFIG_KGDB_CONSOLE
827 /* Output string in GDB O-packet format if GDB has connected. If nothing
828 output, returns 0 (caller must then handle output). */
830 kgdb_output_string (const char* s, unsigned int count)
837 count = (count <= (sizeof(buffer) / 2 - 2))
838 ? count : (sizeof(buffer) / 2 - 2);
841 mem2hex (s, &buffer[1], count);
848 static void sysrq_handle_gdb(int key, struct pt_regs *pt_regs,
849 struct tty_struct *tty)
851 printk("Entering GDB stub\n");
854 static struct sysrq_key_op sysrq_gdb_op = {
855 .handler = sysrq_handle_gdb,
860 static int gdb_register_sysrq(void)
862 printk("Registering GDB sysrq handler\n");
863 register_sysrq_key('g', &sysrq_gdb_op);
866 module_init(gdb_register_sysrq);