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/config.h>
103 #include <linux/kernel.h>
104 #include <linux/string.h>
105 #include <linux/mm.h>
106 #include <linux/smp.h>
107 #include <linux/smp_lock.h>
108 #include <linux/init.h>
109 #include <linux/sysrq.h>
111 #include <asm/cacheflush.h>
112 #include <asm/system.h>
113 #include <asm/signal.h>
114 #include <asm/kgdb.h>
115 #include <asm/pgtable.h>
116 #include <asm/ptrace.h>
118 void breakinst(void);
121 * BUFMAX defines the maximum number of characters in inbound/outbound buffers
122 * at least NUMREGBYTES*2 are needed for register packets
125 static char remcomInBuffer
[BUFMAX
];
126 static char remcomOutBuffer
[BUFMAX
];
128 static int initialized
;
129 static int kgdb_active
;
130 static int kgdb_started
;
131 static u_int fault_jmp_buf
[100];
135 static const char hexchars
[]="0123456789abcdef";
137 /* Place where we save old trap entries for restoration - sparc*/
138 /* struct tt_entry kgdb_savettable[256]; */
139 /* typedef void (*trapfunc_t)(void); */
141 static void kgdb_fault_handler(struct pt_regs
*regs
);
142 static int handle_exception (struct pt_regs
*regs
);
145 /* Install an exception handler for kgdb */
146 static void exceptionHandler(int tnum
, unsigned int *tfunc
)
148 /* We are dorking with a live trap table, all irqs off */
153 kgdb_setjmp(long *buf
)
155 asm ("mflr 0; stw 0,0(%0);"
156 "stw 1,4(%0); stw 2,8(%0);"
157 "mfcr 0; stw 0,12(%0);"
160 /* XXX should save fp regs as well */
164 kgdb_longjmp(long *buf
, int val
)
168 asm ("lmw 13,16(%0);"
169 "lwz 0,12(%0); mtcrf 0x38,0;"
170 "lwz 0,0(%0); lwz 1,4(%0); lwz 2,8(%0);"
172 : : "r" (buf
), "r" (val
));
174 /* Convert ch from a hex digit to an int */
176 hex(unsigned char ch
)
178 if (ch
>= 'a' && ch
<= 'f')
180 if (ch
>= '0' && ch
<= '9')
182 if (ch
>= 'A' && ch
<= 'F')
187 /* Convert the memory pointed to by mem into hex, placing result in buf.
188 * Return a pointer to the last char put in buf (null), in case of mem fault,
191 static unsigned char *
192 mem2hex(const char *mem
, char *buf
, int count
)
195 unsigned short tmp_s
;
198 if (kgdb_setjmp((long*)fault_jmp_buf
) == 0) {
199 debugger_fault_handler
= kgdb_fault_handler
;
201 /* Accessing 16 bit and 32 bit objects in a single
202 ** load instruction is required to avoid bad side
203 ** effects for some IO registers.
206 if ((count
== 2) && (((long)mem
& 1) == 0)) {
207 tmp_s
= *(unsigned short *)mem
;
209 *buf
++ = hexchars
[(tmp_s
>> 12) & 0xf];
210 *buf
++ = hexchars
[(tmp_s
>> 8) & 0xf];
211 *buf
++ = hexchars
[(tmp_s
>> 4) & 0xf];
212 *buf
++ = hexchars
[tmp_s
& 0xf];
214 } else if ((count
== 4) && (((long)mem
& 3) == 0)) {
215 tmp_l
= *(unsigned int *)mem
;
217 *buf
++ = hexchars
[(tmp_l
>> 28) & 0xf];
218 *buf
++ = hexchars
[(tmp_l
>> 24) & 0xf];
219 *buf
++ = hexchars
[(tmp_l
>> 20) & 0xf];
220 *buf
++ = hexchars
[(tmp_l
>> 16) & 0xf];
221 *buf
++ = hexchars
[(tmp_l
>> 12) & 0xf];
222 *buf
++ = hexchars
[(tmp_l
>> 8) & 0xf];
223 *buf
++ = hexchars
[(tmp_l
>> 4) & 0xf];
224 *buf
++ = hexchars
[tmp_l
& 0xf];
227 while (count
-- > 0) {
229 *buf
++ = hexchars
[ch
>> 4];
230 *buf
++ = hexchars
[ch
& 0xf];
235 /* error condition */
237 debugger_fault_handler
= NULL
;
242 /* convert the hex array pointed to by buf into binary to be placed in mem
243 * return a pointer to the character AFTER the last byte written.
246 hex2mem(char *buf
, char *mem
, int count
)
251 unsigned short tmp_s
;
256 if (kgdb_setjmp((long*)fault_jmp_buf
) == 0) {
257 debugger_fault_handler
= kgdb_fault_handler
;
259 /* Accessing 16 bit and 32 bit objects in a single
260 ** store instruction is required to avoid bad side
261 ** effects for some IO registers.
264 if ((count
== 2) && (((long)mem
& 1) == 0)) {
265 tmp_s
= hex(*buf
++) << 12;
266 tmp_s
|= hex(*buf
++) << 8;
267 tmp_s
|= hex(*buf
++) << 4;
268 tmp_s
|= hex(*buf
++);
270 *(unsigned short *)mem
= tmp_s
;
273 } else if ((count
== 4) && (((long)mem
& 3) == 0)) {
274 tmp_l
= hex(*buf
++) << 28;
275 tmp_l
|= hex(*buf
++) << 24;
276 tmp_l
|= hex(*buf
++) << 20;
277 tmp_l
|= hex(*buf
++) << 16;
278 tmp_l
|= hex(*buf
++) << 12;
279 tmp_l
|= hex(*buf
++) << 8;
280 tmp_l
|= hex(*buf
++) << 4;
281 tmp_l
|= hex(*buf
++);
283 *(unsigned long *)mem
= tmp_l
;
287 for (i
=0; i
<count
; i
++) {
288 ch
= hex(*buf
++) << 4;
296 ** Flush the data cache, invalidate the instruction cache.
298 flush_icache_range((int)orig_mem
, (int)orig_mem
+ count
- 1);
301 /* error condition */
303 debugger_fault_handler
= NULL
;
308 * While we find nice hex chars, build an int.
309 * Return number of chars processed.
312 hexToInt(char **ptr
, int *intValue
)
319 if (kgdb_setjmp((long*)fault_jmp_buf
) == 0) {
320 debugger_fault_handler
= kgdb_fault_handler
;
322 hexValue
= hex(**ptr
);
326 *intValue
= (*intValue
<< 4) | hexValue
;
332 /* error condition */
334 debugger_fault_handler
= NULL
;
339 /* scan for the sequence $<data>#<checksum> */
341 getpacket(char *buffer
)
343 unsigned char checksum
;
344 unsigned char xmitcsum
;
350 /* wait around for the start character, ignore all other
352 while ((ch
= (getDebugChar() & 0x7f)) != '$') ;
359 /* now, read until a # or end of buffer is found */
360 while (count
< BUFMAX
) {
361 ch
= getDebugChar() & 0x7f;
364 checksum
= checksum
+ ch
;
375 xmitcsum
= hex(getDebugChar() & 0x7f) << 4;
376 xmitcsum
|= hex(getDebugChar() & 0x7f);
377 if (checksum
!= xmitcsum
)
378 putDebugChar('-'); /* failed checksum */
380 putDebugChar('+'); /* successful transfer */
381 /* if a sequence char is present, reply the ID */
382 if (buffer
[2] == ':') {
383 putDebugChar(buffer
[0]);
384 putDebugChar(buffer
[1]);
385 /* remove sequence chars from buffer */
386 count
= strlen(buffer
);
387 for (i
=3; i
<= count
; i
++)
388 buffer
[i
-3] = buffer
[i
];
392 } while (checksum
!= xmitcsum
);
395 /* send the packet in buffer. */
396 static void putpacket(unsigned char *buffer
)
398 unsigned char checksum
;
400 unsigned char ch
, recv
;
402 /* $<packet info>#<checksum>. */
408 while ((ch
= buffer
[count
])) {
415 putDebugChar(hexchars
[checksum
>> 4]);
416 putDebugChar(hexchars
[checksum
& 0xf]);
417 recv
= getDebugChar();
418 } while ((recv
& 0x7f) != '+');
421 static void kgdb_flush_cache_all(void)
423 flush_instruction_cache();
426 /* Set up exception handlers for tracing and breakpoints
427 * [could be called kgdb_init()]
429 void set_debug_traps(void)
436 /* In case GDB is started before us, ack any packets (presumably
437 * "$?#xx") sitting there.
439 * I've found this code causes more problems than it solves,
440 * so that's why it's commented out. GDB seems to work fine
441 * now starting either before or after the kernel -bwb
444 while((c
= getDebugChar()) != '$');
445 while((c
= getDebugChar()) != '#');
446 c
= getDebugChar(); /* eat first csum byte */
447 c
= getDebugChar(); /* eat second csum byte */
448 putDebugChar('+'); /* ack it */
451 debugger_bpt
= kgdb_bpt
;
452 debugger_sstep
= kgdb_sstep
;
453 debugger_iabr_match
= kgdb_iabr_match
;
454 debugger_dabr_match
= kgdb_dabr_match
;
459 static void kgdb_fault_handler(struct pt_regs
*regs
)
461 kgdb_longjmp((long*)fault_jmp_buf
, 1);
464 int kgdb_bpt(struct pt_regs
*regs
)
466 return handle_exception(regs
);
469 int kgdb_sstep(struct pt_regs
*regs
)
471 return handle_exception(regs
);
474 void kgdb(struct pt_regs
*regs
)
476 handle_exception(regs
);
479 int kgdb_iabr_match(struct pt_regs
*regs
)
481 printk(KERN_ERR
"kgdb doesn't support iabr, what?!?\n");
482 return handle_exception(regs
);
485 int kgdb_dabr_match(struct pt_regs
*regs
)
487 printk(KERN_ERR
"kgdb doesn't support dabr, what?!?\n");
488 return handle_exception(regs
);
491 /* Convert the hardware trap type code to a unix signal number. */
493 * This table contains the mapping between PowerPC hardware trap types, and
494 * signals, which are primarily what GDB understands.
496 static struct hard_trap_info
498 unsigned int tt
; /* Trap type code for powerpc */
499 unsigned char signo
; /* Signal that we map this trap into */
500 } hard_trap_info
[] = {
501 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
502 { 0x100, SIGINT
}, /* critical input interrupt */
503 { 0x200, SIGSEGV
}, /* machine check */
504 { 0x300, SIGSEGV
}, /* data storage */
505 { 0x400, SIGBUS
}, /* instruction storage */
506 { 0x500, SIGINT
}, /* interrupt */
507 { 0x600, SIGBUS
}, /* alignment */
508 { 0x700, SIGILL
}, /* program */
509 { 0x800, SIGILL
}, /* reserved */
510 { 0x900, SIGILL
}, /* reserved */
511 { 0xa00, SIGILL
}, /* reserved */
512 { 0xb00, SIGILL
}, /* reserved */
513 { 0xc00, SIGCHLD
}, /* syscall */
514 { 0xd00, SIGILL
}, /* reserved */
515 { 0xe00, SIGILL
}, /* reserved */
516 { 0xf00, SIGILL
}, /* reserved */
521 ** 0x1100 data TLB miss
522 ** 0x1200 instruction TLB miss
524 { 0x2002, SIGTRAP
}, /* debug */
526 { 0x200, SIGSEGV
}, /* machine check */
527 { 0x300, SIGSEGV
}, /* address error (store) */
528 { 0x400, SIGBUS
}, /* instruction bus error */
529 { 0x500, SIGINT
}, /* interrupt */
530 { 0x600, SIGBUS
}, /* alingment */
531 { 0x700, SIGTRAP
}, /* breakpoint trap */
532 { 0x800, SIGFPE
}, /* fpu unavail */
533 { 0x900, SIGALRM
}, /* decrementer */
534 { 0xa00, SIGILL
}, /* reserved */
535 { 0xb00, SIGILL
}, /* reserved */
536 { 0xc00, SIGCHLD
}, /* syscall */
537 { 0xd00, SIGTRAP
}, /* single-step/watch */
538 { 0xe00, SIGFPE
}, /* fp assist */
540 { 0, 0} /* Must be last */
544 static int computeSignal(unsigned int tt
)
546 struct hard_trap_info
*ht
;
548 for (ht
= hard_trap_info
; ht
->tt
&& ht
->signo
; ht
++)
552 return SIGHUP
; /* default for things we don't know about */
559 * This function does all command processing for interfacing to gdb.
562 handle_exception (struct pt_regs
*regs
)
570 /* We don't handle user-mode breakpoints. */
574 if (debugger_fault_handler
) {
575 debugger_fault_handler(regs
);
576 panic("kgdb longjump failed!\n");
579 printk(KERN_ERR
"interrupt while in kgdb, returning\n");
587 printk("kgdb: entering handle_exception; trap [0x%x]\n",
588 (unsigned int)regs
->trap
);
591 kgdb_interruptible(0);
594 mtmsr(msr
& ~MSR_EE
); /* disable interrupts */
596 if (regs
->nip
== (unsigned long)breakinst
) {
597 /* Skip over breakpoint trap insn */
601 /* reply to host that an exception has occurred */
602 sigval
= computeSignal(regs
->trap
);
603 ptr
= remcomOutBuffer
;
606 *ptr
++ = hexchars
[sigval
>> 4];
607 *ptr
++ = hexchars
[sigval
& 0xf];
608 *ptr
++ = hexchars
[PC_REGNUM
>> 4];
609 *ptr
++ = hexchars
[PC_REGNUM
& 0xf];
611 ptr
= mem2hex((char *)®s
->nip
, ptr
, 4);
613 *ptr
++ = hexchars
[SP_REGNUM
>> 4];
614 *ptr
++ = hexchars
[SP_REGNUM
& 0xf];
616 ptr
= mem2hex(((char *)regs
) + SP_REGNUM
*4, ptr
, 4);
620 putpacket(remcomOutBuffer
);
622 printk("remcomOutBuffer: %s\n", remcomOutBuffer
);
624 /* XXX We may want to add some features dealing with poking the
625 * XXX page tables, ... (look at sparc-stub.c for more info)
626 * XXX also required hacking to the gdb sources directly...
630 remcomOutBuffer
[0] = 0;
632 getpacket(remcomInBuffer
);
633 switch (remcomInBuffer
[0]) {
634 case '?': /* report most recent signal */
635 remcomOutBuffer
[0] = 'S';
636 remcomOutBuffer
[1] = hexchars
[sigval
>> 4];
637 remcomOutBuffer
[2] = hexchars
[sigval
& 0xf];
638 remcomOutBuffer
[3] = 0;
641 case 'q': /* this screws up gdb for some reason...*/
643 extern long _start
, sdata
, __bss_start
;
645 ptr
= &remcomInBuffer
[1];
646 if (strncmp(ptr
, "Offsets", 7) != 0)
649 ptr
= remcomOutBuffer
;
650 sprintf(ptr
, "Text=%8.8x;Data=%8.8x;Bss=%8.8x",
651 &_start
, &sdata
, &__bss_start
);
656 /* toggle debug flag */
660 case 'g': /* return the value of the CPU registers.
661 * some of them are non-PowerPC names :(
662 * they are stored in gdb like:
666 * u32 pc, ps, cnd, lr; (ps=msr)
672 ptr
= remcomOutBuffer
;
673 /* General Purpose Regs */
674 ptr
= mem2hex((char *)regs
, ptr
, 32 * 4);
675 /* Floating Point Regs - FIXME */
676 /*ptr = mem2hex((char *), ptr, 32 * 8);*/
677 for(i
=0; i
<(32*8*2); i
++) { /* 2chars/byte */
681 /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
682 ptr
= mem2hex((char *)®s
->nip
, ptr
, 4);
683 ptr
= mem2hex((char *)®s
->msr
, ptr
, 4);
684 ptr
= mem2hex((char *)®s
->ccr
, ptr
, 4);
685 ptr
= mem2hex((char *)®s
->link
, ptr
, 4);
686 ptr
= mem2hex((char *)®s
->ctr
, ptr
, 4);
687 ptr
= mem2hex((char *)®s
->xer
, ptr
, 4);
691 case 'G': /* set the value of the CPU registers */
693 ptr
= &remcomInBuffer
[1];
696 * If the stack pointer has moved, you should pray.
697 * (cause only god can help you).
700 /* General Purpose Regs */
701 hex2mem(ptr
, (char *)regs
, 32 * 4);
703 /* Floating Point Regs - FIXME?? */
704 /*ptr = hex2mem(ptr, ??, 32 * 8);*/
707 /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
708 ptr
= hex2mem(ptr
, (char *)®s
->nip
, 4);
709 ptr
= hex2mem(ptr
, (char *)®s
->msr
, 4);
710 ptr
= hex2mem(ptr
, (char *)®s
->ccr
, 4);
711 ptr
= hex2mem(ptr
, (char *)®s
->link
, 4);
712 ptr
= hex2mem(ptr
, (char *)®s
->ctr
, 4);
713 ptr
= hex2mem(ptr
, (char *)®s
->xer
, 4);
715 strcpy(remcomOutBuffer
,"OK");
719 /* don't do anything, yet, just acknowledge */
720 hexToInt(&ptr
, &addr
);
721 strcpy(remcomOutBuffer
,"OK");
724 case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
725 /* Try to read %x,%x. */
727 ptr
= &remcomInBuffer
[1];
729 if (hexToInt(&ptr
, &addr
) && *ptr
++ == ','
730 && hexToInt(&ptr
, &length
)) {
731 if (mem2hex((char *)addr
, remcomOutBuffer
,
734 strcpy(remcomOutBuffer
, "E03");
736 strcpy(remcomOutBuffer
, "E01");
739 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
740 /* Try to read '%x,%x:'. */
742 ptr
= &remcomInBuffer
[1];
744 if (hexToInt(&ptr
, &addr
) && *ptr
++ == ','
745 && hexToInt(&ptr
, &length
)
747 if (hex2mem(ptr
, (char *)addr
, length
))
748 strcpy(remcomOutBuffer
, "OK");
750 strcpy(remcomOutBuffer
, "E03");
751 flush_icache_range(addr
, addr
+length
);
753 strcpy(remcomOutBuffer
, "E02");
757 case 'k': /* kill the program, actually just continue */
758 case 'c': /* cAA..AA Continue; address AA..AA optional */
759 /* try to read optional parameter, pc unchanged if no parm */
761 ptr
= &remcomInBuffer
[1];
762 if (hexToInt(&ptr
, &addr
))
765 /* Need to flush the instruction cache here, as we may have deposited a
766 * breakpoint, and the icache probably has no way of knowing that a data ref to
767 * some location may have changed something that is in the instruction cache.
769 kgdb_flush_cache_all();
772 kgdb_interruptible(1);
776 printk("remcomInBuffer: %s\n", remcomInBuffer
);
777 printk("remcomOutBuffer: %s\n", remcomOutBuffer
);
782 kgdb_flush_cache_all();
783 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
784 mtspr(SPRN_DBCR0
, mfspr(SPRN_DBCR0
) | DBCR0_IC
);
792 printk("remcomInBuffer: %s\n", remcomInBuffer
);
793 printk("remcomOutBuffer: %s\n", remcomOutBuffer
);
797 case 'r': /* Reset (if user process..exit ???)*/
798 panic("kgdb reset.");
801 if (remcomOutBuffer
[0] && kdebug
) {
802 printk("remcomInBuffer: %s\n", remcomInBuffer
);
803 printk("remcomOutBuffer: %s\n", remcomOutBuffer
);
805 /* reply to the request */
806 putpacket(remcomOutBuffer
);
810 /* This function will generate a breakpoint exception. It is used at the
811 beginning of a program to sync up with a debugger and can be used
812 otherwise as a quick means to stop program execution and "break" into
819 printk("breakpoint() called b4 kgdb init\n");
823 asm(" .globl breakinst \n\
824 breakinst: .long 0x7d821008");
827 #ifdef CONFIG_KGDB_CONSOLE
828 /* Output string in GDB O-packet format if GDB has connected. If nothing
829 output, returns 0 (caller must then handle output). */
831 kgdb_output_string (const char* s
, unsigned int count
)
838 count
= (count
<= (sizeof(buffer
) / 2 - 2))
839 ? count
: (sizeof(buffer
) / 2 - 2);
842 mem2hex (s
, &buffer
[1], count
);
849 static void sysrq_handle_gdb(int key
, struct pt_regs
*pt_regs
,
850 struct tty_struct
*tty
)
852 printk("Entering GDB stub\n");
855 static struct sysrq_key_op sysrq_gdb_op
= {
856 .handler
= sysrq_handle_gdb
,
861 static int gdb_register_sysrq(void)
863 printk("Registering GDB sysrq handler\n");
864 register_sysrq_key('g', &sysrq_gdb_op
);
867 module_init(gdb_register_sysrq
);