PCI: add ICH7/8 ACPI/GPIO io resource quirks
[pv_ops_mirror.git] / arch / ppc / kernel / ppc-stub.c
blob5f9ee7bb67ec32b7bca5557bde3dd0119a9b54d2
1 /*
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 $
33 * Revision: 1.34 $
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 $
44 * NOTES: See Below $
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.
57 *************
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
76 * k kill
78 * ? What was the last sigval ? SNN (signal NN)
80 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
81 * baud rate
83 * All commands and responses are sent with a packet which includes a
84 * checksum. A packet consists of
86 * $<packet info>#<checksum>.
88 * where
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.
95 * Example:
97 * Host: Reply:
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
123 #define BUFMAX 2048
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];
131 static int kdebug;
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);
143 #if 0
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 */
149 #endif
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);"
157 "stmw 13,16(%0)"
158 : : "r" (buf));
159 /* XXX should save fp regs as well */
160 return 0;
162 void
163 kgdb_longjmp(long *buf, int val)
165 if (val == 0)
166 val = 1;
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);"
170 "mtlr 0; mr 3,%1"
171 : : "r" (buf), "r" (val));
173 /* Convert ch from a hex digit to an int */
174 static int
175 hex(unsigned char ch)
177 if (ch >= 'a' && ch <= 'f')
178 return ch-'a'+10;
179 if (ch >= '0' && ch <= '9')
180 return ch-'0';
181 if (ch >= 'A' && ch <= 'F')
182 return ch-'A'+10;
183 return -1;
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,
188 * return 0.
190 static unsigned char *
191 mem2hex(const char *mem, char *buf, int count)
193 unsigned char ch;
194 unsigned short tmp_s;
195 unsigned long tmp_l;
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;
207 mem += 2;
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;
215 mem += 4;
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];
225 } else {
226 while (count-- > 0) {
227 ch = *mem++;
228 *buf++ = hexchars[ch >> 4];
229 *buf++ = hexchars[ch & 0xf];
233 } else {
234 /* error condition */
236 debugger_fault_handler = NULL;
237 *buf = 0;
238 return buf;
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.
244 static char *
245 hex2mem(char *buf, char *mem, int count)
247 unsigned char ch;
248 int i;
249 char *orig_mem;
250 unsigned short tmp_s;
251 unsigned long tmp_l;
253 orig_mem = mem;
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;
270 mem += 2;
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;
283 mem += 4;
285 } else {
286 for (i=0; i<count; i++) {
287 ch = hex(*buf++) << 4;
288 ch |= hex(*buf++);
289 *mem++ = ch;
295 ** Flush the data cache, invalidate the instruction cache.
297 flush_icache_range((int)orig_mem, (int)orig_mem + count - 1);
299 } else {
300 /* error condition */
302 debugger_fault_handler = NULL;
303 return mem;
307 * While we find nice hex chars, build an int.
308 * Return number of chars processed.
310 static int
311 hexToInt(char **ptr, int *intValue)
313 int numChars = 0;
314 int hexValue;
316 *intValue = 0;
318 if (kgdb_setjmp((long*)fault_jmp_buf) == 0) {
319 debugger_fault_handler = kgdb_fault_handler;
320 while (**ptr) {
321 hexValue = hex(**ptr);
322 if (hexValue < 0)
323 break;
325 *intValue = (*intValue << 4) | hexValue;
326 numChars ++;
328 (*ptr)++;
330 } else {
331 /* error condition */
333 debugger_fault_handler = NULL;
335 return (numChars);
338 /* scan for the sequence $<data>#<checksum> */
339 static void
340 getpacket(char *buffer)
342 unsigned char checksum;
343 unsigned char xmitcsum;
344 int i;
345 int count;
346 unsigned char ch;
348 do {
349 /* wait around for the start character, ignore all other
350 * characters */
351 while ((ch = (getDebugChar() & 0x7f)) != '$') ;
353 checksum = 0;
354 xmitcsum = -1;
356 count = 0;
358 /* now, read until a # or end of buffer is found */
359 while (count < BUFMAX) {
360 ch = getDebugChar() & 0x7f;
361 if (ch == '#')
362 break;
363 checksum = checksum + ch;
364 buffer[count] = ch;
365 count = count + 1;
368 if (count >= BUFMAX)
369 continue;
371 buffer[count] = 0;
373 if (ch == '#') {
374 xmitcsum = hex(getDebugChar() & 0x7f) << 4;
375 xmitcsum |= hex(getDebugChar() & 0x7f);
376 if (checksum != xmitcsum)
377 putDebugChar('-'); /* failed checksum */
378 else {
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;
398 int count;
399 unsigned char ch, recv;
401 /* $<packet info>#<checksum>. */
402 do {
403 putDebugChar('$');
404 checksum = 0;
405 count = 0;
407 while ((ch = buffer[count])) {
408 putDebugChar(ch);
409 checksum += ch;
410 count += 1;
413 putDebugChar('#');
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)
430 #if 0
431 unsigned char c;
433 save_and_cli(flags);
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 */
448 #endif
449 debugger = kgdb;
450 debugger_bpt = kgdb_bpt;
451 debugger_sstep = kgdb_sstep;
452 debugger_iabr_match = kgdb_iabr_match;
453 debugger_dabr_match = kgdb_dabr_match;
455 initialized = 1;
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 */
517 ** 0x1000 PIT
518 ** 0x1010 FIT
519 ** 0x1020 watchdog
520 ** 0x1100 data TLB miss
521 ** 0x1200 instruction TLB miss
523 { 0x2002, SIGTRAP}, /* debug */
524 #else
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 */
538 #endif
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++)
548 if (ht->tt == tt)
549 return ht->signo;
551 return SIGHUP; /* default for things we don't know about */
554 #define PC_REGNUM 64
555 #define SP_REGNUM 1
558 * This function does all command processing for interfacing to gdb.
560 static int
561 handle_exception (struct pt_regs *regs)
563 int sigval;
564 int addr;
565 int length;
566 char *ptr;
567 unsigned int msr;
569 /* We don't handle user-mode breakpoints. */
570 if (user_mode(regs))
571 return 0;
573 if (debugger_fault_handler) {
574 debugger_fault_handler(regs);
575 panic("kgdb longjump failed!\n");
577 if (kgdb_active) {
578 printk(KERN_ERR "interrupt while in kgdb, returning\n");
579 return 0;
582 kgdb_active = 1;
583 kgdb_started = 1;
585 #ifdef KGDB_DEBUG
586 printk("kgdb: entering handle_exception; trap [0x%x]\n",
587 (unsigned int)regs->trap);
588 #endif
590 kgdb_interruptible(0);
591 lock_kernel();
592 msr = mfmsr();
593 mtmsr(msr & ~MSR_EE); /* disable interrupts */
595 if (regs->nip == (unsigned long)breakinst) {
596 /* Skip over breakpoint trap insn */
597 regs->nip += 4;
600 /* reply to host that an exception has occurred */
601 sigval = computeSignal(regs->trap);
602 ptr = remcomOutBuffer;
604 *ptr++ = 'T';
605 *ptr++ = hexchars[sigval >> 4];
606 *ptr++ = hexchars[sigval & 0xf];
607 *ptr++ = hexchars[PC_REGNUM >> 4];
608 *ptr++ = hexchars[PC_REGNUM & 0xf];
609 *ptr++ = ':';
610 ptr = mem2hex((char *)&regs->nip, ptr, 4);
611 *ptr++ = ';';
612 *ptr++ = hexchars[SP_REGNUM >> 4];
613 *ptr++ = hexchars[SP_REGNUM & 0xf];
614 *ptr++ = ':';
615 ptr = mem2hex(((char *)regs) + SP_REGNUM*4, ptr, 4);
616 *ptr++ = ';';
617 *ptr++ = 0;
619 putpacket(remcomOutBuffer);
620 if (kdebug)
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...
628 while (1) {
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;
638 break;
639 #if 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)
646 break;
648 ptr = remcomOutBuffer;
649 sprintf(ptr, "Text=%8.8x;Data=%8.8x;Bss=%8.8x",
650 &_start, &sdata, &__bss_start);
651 break;
653 #endif
654 case 'd':
655 /* toggle debug flag */
656 kdebug ^= 1;
657 break;
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:
662 * struct {
663 * u32 gpr[32];
664 * f64 fpr[32];
665 * u32 pc, ps, cnd, lr; (ps=msr)
666 * u32 cnt, xer, mq;
670 int i;
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 */
677 ptr[i] = '0';
679 ptr += 32*8*2;
680 /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
681 ptr = mem2hex((char *)&regs->nip, ptr, 4);
682 ptr = mem2hex((char *)&regs->msr, ptr, 4);
683 ptr = mem2hex((char *)&regs->ccr, ptr, 4);
684 ptr = mem2hex((char *)&regs->link, ptr, 4);
685 ptr = mem2hex((char *)&regs->ctr, ptr, 4);
686 ptr = mem2hex((char *)&regs->xer, ptr, 4);
688 break;
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);*/
704 ptr += 32*8*2;
706 /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
707 ptr = hex2mem(ptr, (char *)&regs->nip, 4);
708 ptr = hex2mem(ptr, (char *)&regs->msr, 4);
709 ptr = hex2mem(ptr, (char *)&regs->ccr, 4);
710 ptr = hex2mem(ptr, (char *)&regs->link, 4);
711 ptr = hex2mem(ptr, (char *)&regs->ctr, 4);
712 ptr = hex2mem(ptr, (char *)&regs->xer, 4);
714 strcpy(remcomOutBuffer,"OK");
716 break;
717 case 'H':
718 /* don't do anything, yet, just acknowledge */
719 hexToInt(&ptr, &addr);
720 strcpy(remcomOutBuffer,"OK");
721 break;
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,
731 length))
732 break;
733 strcpy(remcomOutBuffer, "E03");
734 } else
735 strcpy(remcomOutBuffer, "E01");
736 break;
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)
745 && *ptr++ == ':') {
746 if (hex2mem(ptr, (char *)addr, length))
747 strcpy(remcomOutBuffer, "OK");
748 else
749 strcpy(remcomOutBuffer, "E03");
750 flush_icache_range(addr, addr+length);
751 } else
752 strcpy(remcomOutBuffer, "E02");
753 break;
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))
762 regs->nip = 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();
769 mtmsr(msr);
771 kgdb_interruptible(1);
772 unlock_kernel();
773 kgdb_active = 0;
774 if (kdebug) {
775 printk("remcomInBuffer: %s\n", remcomInBuffer);
776 printk("remcomOutBuffer: %s\n", remcomOutBuffer);
778 return 1;
780 case 's':
781 kgdb_flush_cache_all();
782 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
783 mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | DBCR0_IC);
784 regs->msr |= MSR_DE;
785 #else
786 regs->msr |= MSR_SE;
787 #endif
788 unlock_kernel();
789 kgdb_active = 0;
790 if (kdebug) {
791 printk("remcomInBuffer: %s\n", remcomInBuffer);
792 printk("remcomOutBuffer: %s\n", remcomOutBuffer);
794 return 1;
796 case 'r': /* Reset (if user process..exit ???)*/
797 panic("kgdb reset.");
798 break;
799 } /* switch */
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);
806 } /* while(1) */
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
812 the debugger. */
814 void
815 breakpoint(void)
817 if (!initialized) {
818 printk("breakpoint() called b4 kgdb init\n");
819 return;
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)
832 char buffer[512];
834 if (!kgdb_started)
835 return 0;
837 count = (count <= (sizeof(buffer) / 2 - 2))
838 ? count : (sizeof(buffer) / 2 - 2);
840 buffer[0] = 'O';
841 mem2hex (s, &buffer[1], count);
842 putpacket(buffer);
844 return 1;
846 #endif
848 static void sysrq_handle_gdb(int key, struct pt_regs *pt_regs,
849 struct tty_struct *tty)
851 printk("Entering GDB stub\n");
852 breakpoint();
854 static struct sysrq_key_op sysrq_gdb_op = {
855 .handler = sysrq_handle_gdb,
856 .help_msg = "Gdb",
857 .action_msg = "GDB",
860 static int gdb_register_sysrq(void)
862 printk("Registering GDB sysrq handler\n");
863 register_sysrq_key('g', &sysrq_gdb_op);
864 return 0;
866 module_init(gdb_register_sysrq);