📄 ppc-stub.c
字号:
/* $Id: ppc-stub.c,v 1.6 1999/08/12 22:18:11 cort Exp $ * ppc-stub.c: KGDB support for the Linux kernel. * * adapted from arch/sparc/kernel/sparc-stub.c for the PowerPC * some stuff borrowed from Paul Mackerras' xmon * Copyright (C) 1998 Michael AK Tesch (tesch@cs.wisc.edu) * * Modifications to run under Linux * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) * * This file originally came from the gdb sources, and the * copyright notices have been retained below. *//**************************************************************************** THIS SOFTWARE IS NOT COPYRIGHTED HP offers the following for use in the public domain. HP makes no warranty with regard to the software or its performance and the user accepts the software "AS IS" with all faults. HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.****************************************************************************//**************************************************************************** * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $ * * Module name: remcom.c $ * Revision: 1.34 $ * Date: 91/03/09 12:29:49 $ * Contributor: Lake Stevens Instrument Division$ * * Description: low level support for gdb debugger. $ * * Considerations: only works on target hardware $ * * Written by: Glenn Engel $ * ModuleState: Experimental $ * * NOTES: See Below $ * * Modified for SPARC by Stu Grossman, Cygnus Support. * * This code has been extensively tested on the Fujitsu SPARClite demo board. * * To enable debugger support, two things need to happen. One, a * call to set_debug_traps() is necessary in order to allow any breakpoints * or error conditions to be properly intercepted and reported to gdb. * Two, a breakpoint needs to be generated to begin communication. This * is most easily accomplished by a call to breakpoint(). Breakpoint() * simulates a breakpoint by executing a trap #1. * ************* * * The following gdb commands are supported: * * command function Return value * * g return the value of the CPU registers hex data or ENN * G set the value of the CPU registers OK or ENN * qOffsets Get section offsets. Reply is Text=xxx;Data=yyy;Bss=zzz * * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN * * c Resume at current address SNN ( signal NN) * cAA..AA Continue at address AA..AA SNN * * s Step one instruction SNN * sAA..AA Step one instruction from AA..AA SNN * * k kill * * ? What was the last sigval ? SNN (signal NN) * * bBB..BB Set baud rate to BB..BB OK or BNN, then sets * baud rate * * All commands and responses are sent with a packet which includes a * checksum. A packet consists of * * $<packet info>#<checksum>. * * where * <packet info> :: <characters representing the command or response> * <checksum> :: <two hex digits computed as modulo 256 sum of <packetinfo>> * * When a packet is received, it is first acknowledged with either '+' or '-'. * '+' indicates a successful transfer. '-' indicates a failed transfer. * * Example: * * Host: Reply: * $m0,10#2a +$00010203040506070809101112131415#42 * ****************************************************************************/#include <linux/config.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/smp.h>#include <linux/smp_lock.h>#include <asm/system.h>#include <asm/signal.h>#include <asm/kgdb.h>#include <asm/pgtable.h>#include <asm/ptrace.h>void breakinst(void);/* * BUFMAX defines the maximum number of characters in inbound/outbound buffers * at least NUMREGBYTES*2 are needed for register packets */#define BUFMAX 2048static char remcomInBuffer[BUFMAX];static char remcomOutBuffer[BUFMAX];static int initialized;static int kgdb_active;static int kgdb_started;static u_int fault_jmp_buf[100];static int kdebug;static const char hexchars[]="0123456789abcdef";/* Place where we save old trap entries for restoration - sparc*//* struct tt_entry kgdb_savettable[256]; *//* typedef void (*trapfunc_t)(void); */#if 0/* Install an exception handler for kgdb */static void exceptionHandler(int tnum, unsigned int *tfunc){ /* We are dorking with a live trap table, all irqs off */}#endifintkgdb_setjmp(long *buf){ asm ("mflr 0; stw 0,0(%0);" "stw 1,4(%0); stw 2,8(%0);" "mfcr 0; stw 0,12(%0);" "stmw 13,16(%0)" : : "r" (buf)); /* XXX should save fp regs as well */ return 0;}voidkgdb_longjmp(long *buf, int val){ if (val == 0) val = 1; asm ("lmw 13,16(%0);" "lwz 0,12(%0); mtcrf 0x38,0;" "lwz 0,0(%0); lwz 1,4(%0); lwz 2,8(%0);" "mtlr 0; mr 3,%1" : : "r" (buf), "r" (val));}/* Convert ch from a hex digit to an int */static inthex(unsigned char ch){ if (ch >= 'a' && ch <= 'f') return ch-'a'+10; if (ch >= '0' && ch <= '9') return ch-'0'; if (ch >= 'A' && ch <= 'F') return ch-'A'+10; return -1;}/* Convert the memory pointed to by mem into hex, placing result in buf. * Return a pointer to the last char put in buf (null), in case of mem fault, * return 0. */static unsigned char *mem2hex(char *mem, char *buf, int count){ unsigned char ch; if (kgdb_setjmp((long*)fault_jmp_buf) == 0) { debugger_fault_handler = kgdb_fault_handler; while (count-- > 0) { ch = *mem++; *buf++ = hexchars[ch >> 4]; *buf++ = hexchars[ch & 0xf]; } } else { /* error condition */ } debugger_fault_handler = 0; *buf = 0; return buf;}/* convert the hex array pointed to by buf into binary to be placed in mem * return a pointer to the character AFTER the last byte written.*/static char *hex2mem(char *buf, char *mem, int count){ int i; unsigned char ch; if (kgdb_setjmp((long*)fault_jmp_buf) == 0) { debugger_fault_handler = kgdb_fault_handler; for (i=0; i<count; i++) { ch = hex(*buf++) << 4; ch |= hex(*buf++); *mem++ = ch; } flush_icache_range((int)mem, (int)mem+count); } else { /* error condition */ } debugger_fault_handler = 0; return mem;}/* * While we find nice hex chars, build an int. * Return number of chars processed. */static inthexToInt(char **ptr, int *intValue){ int numChars = 0; int hexValue; *intValue = 0; if (kgdb_setjmp((long*)fault_jmp_buf) == 0) { debugger_fault_handler = kgdb_fault_handler; while (**ptr) { hexValue = hex(**ptr); if (hexValue < 0) break; *intValue = (*intValue << 4) | hexValue; numChars ++; (*ptr)++; } } else { /* error condition */ } debugger_fault_handler = 0; return (numChars);}/* scan for the sequence $<data>#<checksum> */static voidgetpacket(char *buffer){ unsigned char checksum; unsigned char xmitcsum; int i; int count; unsigned char ch; do { /* wait around for the start character, ignore all other * characters */ while ((ch = (getDebugChar() & 0x7f)) != '$') ; checksum = 0; xmitcsum = -1; count = 0; /* now, read until a # or end of buffer is found */ while (count < BUFMAX) { ch = getDebugChar() & 0x7f; if (ch == '#') break; checksum = checksum + ch; buffer[count] = ch; count = count + 1; } if (count >= BUFMAX) continue; buffer[count] = 0; if (ch == '#') { xmitcsum = hex(getDebugChar() & 0x7f) << 4; xmitcsum |= hex(getDebugChar() & 0x7f); if (checksum != xmitcsum) putDebugChar('-'); /* failed checksum */ else { putDebugChar('+'); /* successful transfer */ /* if a sequence char is present, reply the ID */ if (buffer[2] == ':') { putDebugChar(buffer[0]); putDebugChar(buffer[1]); /* remove sequence chars from buffer */ count = strlen(buffer); for (i=3; i <= count; i++) buffer[i-3] = buffer[i]; } } } } while (checksum != xmitcsum);}/* send the packet in buffer. */static void putpacket(unsigned char *buffer){ unsigned char checksum; int count; unsigned char ch, recv; /* $<packet info>#<checksum>. */ do { putDebugChar('$'); checksum = 0; count = 0; while ((ch = buffer[count])) { putDebugChar(ch); checksum += ch; count += 1; } putDebugChar('#'); putDebugChar(hexchars[checksum >> 4]); putDebugChar(hexchars[checksum & 0xf]); recv = getDebugChar(); } while ((recv & 0x7f) != '+');}static void kgdb_flush_cache_all(void){ flush_instruction_cache();}static inline int get_msr(void){ int msr; asm volatile("mfmsr %0" : "=r" (msr):); return msr;}static inline void set_msr(int msr){ asm volatile("mtmsr %0" : : "r" (msr));}/* Set up exception handlers for tracing and breakpoints * [could be called kgdb_init()] */void set_debug_traps(void){#if 0 unsigned char c; save_and_cli(flags); /* In case GDB is started before us, ack any packets (presumably * "$?#xx") sitting there. * * I've found this code causes more problems than it solves, * so that's why it's commented out. GDB seems to work fine * now starting either before or after the kernel -bwb */ while((c = getDebugChar()) != '$'); while((c = getDebugChar()) != '#'); c = getDebugChar(); /* eat first csum byte */ c = getDebugChar(); /* eat second csum byte */ putDebugChar('+'); /* ack it */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -