Path: blob/master/arch/cris/arch-v10/kernel/kgdb.c
15125 views
/*!**************************************************************************1*!2*! FILE NAME : kgdb.c3*!4*! DESCRIPTION: Implementation of the gdb stub with respect to ETRAX 100.5*! It is a mix of arch/m68k/kernel/kgdb.c and cris_stub.c.6*!7*!---------------------------------------------------------------------------8*! HISTORY9*!10*! DATE NAME CHANGES11*! ---- ---- -------12*! Apr 26 1999 Hendrik Ruijter Initial version.13*! May 6 1999 Hendrik Ruijter Removed call to strlen in libc and removed14*! struct assignment as it generates calls to15*! memcpy in libc.16*! Jun 17 1999 Hendrik Ruijter Added gdb 4.18 support. 'X', 'qC' and 'qL'.17*! Jul 21 1999 Bjorn Wesen eLinux port18*!19*!---------------------------------------------------------------------------20*!21*! (C) Copyright 1999, Axis Communications AB, LUND, SWEDEN22*!23*!**************************************************************************/24/* @(#) cris_stub.c 1.3 06/17/99 */2526/*27* kgdb usage notes:28* -----------------29*30* If you select CONFIG_ETRAX_KGDB in the configuration, the kernel will be31* built with different gcc flags: "-g" is added to get debug infos, and32* "-fomit-frame-pointer" is omitted to make debugging easier. Since the33* resulting kernel will be quite big (approx. > 7 MB), it will be stripped34* before compresion. Such a kernel will behave just as usually, except if35* given a "debug=<device>" command line option. (Only serial devices are36* allowed for <device>, i.e. no printers or the like; possible values are37* machine depedend and are the same as for the usual debug device, the one38* for logging kernel messages.) If that option is given and the device can be39* initialized, the kernel will connect to the remote gdb in trap_init(). The40* serial parameters are fixed to 8N1 and 115200 bps, for easyness of41* implementation.42*43* To start a debugging session, start that gdb with the debugging kernel44* image (the one with the symbols, vmlinux.debug) named on the command line.45* This file will be used by gdb to get symbol and debugging infos about the46* kernel. Next, select remote debug mode by47* target remote <device>48* where <device> is the name of the serial device over which the debugged49* machine is connected. Maybe you have to adjust the baud rate by50* set remotebaud <rate>51* or also other parameters with stty:52* shell stty ... </dev/...53* If the kernel to debug has already booted, it waited for gdb and now54* connects, and you'll see a breakpoint being reported. If the kernel isn't55* running yet, start it now. The order of gdb and the kernel doesn't matter.56* Another thing worth knowing about in the getting-started phase is how to57* debug the remote protocol itself. This is activated with58* set remotedebug 159* gdb will then print out each packet sent or received. You'll also get some60* messages about the gdb stub on the console of the debugged machine.61*62* If all that works, you can use lots of the usual debugging techniques on63* the kernel, e.g. inspecting and changing variables/memory, setting64* breakpoints, single stepping and so on. It's also possible to interrupt the65* debugged kernel by pressing C-c in gdb. Have fun! :-)66*67* The gdb stub is entered (and thus the remote gdb gets control) in the68* following situations:69*70* - If breakpoint() is called. This is just after kgdb initialization, or if71* a breakpoint() call has been put somewhere into the kernel source.72* (Breakpoints can of course also be set the usual way in gdb.)73* In eLinux, we call breakpoint() in init/main.c after IRQ initialization.74*75* - If there is a kernel exception, i.e. bad_super_trap() or die_if_kernel()76* are entered. All the CPU exceptions are mapped to (more or less..., see77* the hard_trap_info array below) appropriate signal, which are reported78* to gdb. die_if_kernel() is usually called after some kind of access79* error and thus is reported as SIGSEGV.80*81* - When panic() is called. This is reported as SIGABRT.82*83* - If C-c is received over the serial line, which is treated as84* SIGINT.85*86* Of course, all these signals are just faked for gdb, since there is no87* signal concept as such for the kernel. It also isn't possible --obviously--88* to set signal handlers from inside gdb, or restart the kernel with a89* signal.90*91* Current limitations:92*93* - While the kernel is stopped, interrupts are disabled for safety reasons94* (i.e., variables not changing magically or the like). But this also95* means that the clock isn't running anymore, and that interrupts from the96* hardware may get lost/not be served in time. This can cause some device97* errors...98*99* - When single-stepping, only one instruction of the current thread is100* executed, but interrupts are allowed for that time and will be serviced101* if pending. Be prepared for that.102*103* - All debugging happens in kernel virtual address space. There's no way to104* access physical memory not mapped in kernel space, or to access user105* space. A way to work around this is using get_user_long & Co. in gdb106* expressions, but only for the current process.107*108* - Interrupting the kernel only works if interrupts are currently allowed,109* and the interrupt of the serial line isn't blocked by some other means110* (IPL too high, disabled, ...)111*112* - The gdb stub is currently not reentrant, i.e. errors that happen therein113* (e.g. accessing invalid memory) may not be caught correctly. This could114* be removed in future by introducing a stack of struct registers.115*116*/117118/*119* To enable debugger support, two things need to happen. One, a120* call to kgdb_init() is necessary in order to allow any breakpoints121* or error conditions to be properly intercepted and reported to gdb.122* Two, a breakpoint needs to be generated to begin communication. This123* is most easily accomplished by a call to breakpoint().124*125* The following gdb commands are supported:126*127* command function Return value128*129* g return the value of the CPU registers hex data or ENN130* G set the value of the CPU registers OK or ENN131*132* mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN133* MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN134*135* c Resume at current address SNN ( signal NN)136* cAA..AA Continue at address AA..AA SNN137*138* s Step one instruction SNN139* sAA..AA Step one instruction from AA..AA SNN140*141* k kill142*143* ? What was the last sigval ? SNN (signal NN)144*145* bBB..BB Set baud rate to BB..BB OK or BNN, then sets146* baud rate147*148* All commands and responses are sent with a packet which includes a149* checksum. A packet consists of150*151* $<packet info>#<checksum>.152*153* where154* <packet info> :: <characters representing the command or response>155* <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>156*157* When a packet is received, it is first acknowledged with either '+' or '-'.158* '+' indicates a successful transfer. '-' indicates a failed transfer.159*160* Example:161*162* Host: Reply:163* $m0,10#2a +$00010203040506070809101112131415#42164*165*/166167168#include <linux/string.h>169#include <linux/signal.h>170#include <linux/kernel.h>171#include <linux/delay.h>172#include <linux/linkage.h>173#include <linux/reboot.h>174175#include <asm/setup.h>176#include <asm/ptrace.h>177178#include <arch/svinto.h>179#include <asm/irq.h>180181static int kgdb_started = 0;182183/********************************* Register image ****************************/184/* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's185Reference", p. 1-1, with the additional register definitions of the186ETRAX 100LX in cris-opc.h.187There are 16 general 32-bit registers, R0-R15, where R14 is the stack188pointer, SP, and R15 is the program counter, PC.189There are 16 special registers, P0-P15, where three of the unimplemented190registers, P0, P4 and P8, are reserved as zero-registers. A read from191any of these registers returns zero and a write has no effect. */192193typedef194struct register_image195{196/* Offset */197unsigned int r0; /* 0x00 */198unsigned int r1; /* 0x04 */199unsigned int r2; /* 0x08 */200unsigned int r3; /* 0x0C */201unsigned int r4; /* 0x10 */202unsigned int r5; /* 0x14 */203unsigned int r6; /* 0x18 */204unsigned int r7; /* 0x1C */205unsigned int r8; /* 0x20 Frame pointer */206unsigned int r9; /* 0x24 */207unsigned int r10; /* 0x28 */208unsigned int r11; /* 0x2C */209unsigned int r12; /* 0x30 */210unsigned int r13; /* 0x34 */211unsigned int sp; /* 0x38 Stack pointer */212unsigned int pc; /* 0x3C Program counter */213214unsigned char p0; /* 0x40 8-bit zero-register */215unsigned char vr; /* 0x41 Version register */216217unsigned short p4; /* 0x42 16-bit zero-register */218unsigned short ccr; /* 0x44 Condition code register */219220unsigned int mof; /* 0x46 Multiply overflow register */221222unsigned int p8; /* 0x4A 32-bit zero-register */223unsigned int ibr; /* 0x4E Interrupt base register */224unsigned int irp; /* 0x52 Interrupt return pointer */225unsigned int srp; /* 0x56 Subroutine return pointer */226unsigned int bar; /* 0x5A Breakpoint address register */227unsigned int dccr; /* 0x5E Double condition code register */228unsigned int brp; /* 0x62 Breakpoint return pointer (pc in caller) */229unsigned int usp; /* 0x66 User mode stack pointer */230} registers;231232/************** Prototypes for local library functions ***********************/233234/* Copy of strcpy from libc. */235static char *gdb_cris_strcpy (char *s1, const char *s2);236237/* Copy of strlen from libc. */238static int gdb_cris_strlen (const char *s);239240/* Copy of memchr from libc. */241static void *gdb_cris_memchr (const void *s, int c, int n);242243/* Copy of strtol from libc. Does only support base 16. */244static int gdb_cris_strtol (const char *s, char **endptr, int base);245246/********************** Prototypes for local functions. **********************/247/* Copy the content of a register image into another. The size n is248the size of the register image. Due to struct assignment generation of249memcpy in libc. */250static void copy_registers (registers *dptr, registers *sptr, int n);251252/* Copy the stored registers from the stack. Put the register contents253of thread thread_id in the struct reg. */254static void copy_registers_from_stack (int thread_id, registers *reg);255256/* Copy the registers to the stack. Put the register contents of thread257thread_id from struct reg to the stack. */258static void copy_registers_to_stack (int thread_id, registers *reg);259260/* Write a value to a specified register regno in the register image261of the current thread. */262static int write_register (int regno, char *val);263264/* Write a value to a specified register in the stack of a thread other265than the current thread. */266static write_stack_register (int thread_id, int regno, char *valptr);267268/* Read a value from a specified register in the register image. Returns the269status of the read operation. The register value is returned in valptr. */270static int read_register (char regno, unsigned int *valptr);271272/* Serial port, reads one character. ETRAX 100 specific. from debugport.c */273int getDebugChar (void);274275/* Serial port, writes one character. ETRAX 100 specific. from debugport.c */276void putDebugChar (int val);277278void enableDebugIRQ (void);279280/* Returns the integer equivalent of a hexadecimal character. */281static int hex (char ch);282283/* Convert the memory, pointed to by mem into hexadecimal representation.284Put the result in buf, and return a pointer to the last character285in buf (null). */286static char *mem2hex (char *buf, unsigned char *mem, int count);287288/* Convert the array, in hexadecimal representation, pointed to by buf into289binary representation. Put the result in mem, and return a pointer to290the character after the last byte written. */291static unsigned char *hex2mem (unsigned char *mem, char *buf, int count);292293/* Put the content of the array, in binary representation, pointed to by buf294into memory pointed to by mem, and return a pointer to295the character after the last byte written. */296static unsigned char *bin2mem (unsigned char *mem, unsigned char *buf, int count);297298/* Await the sequence $<data>#<checksum> and store <data> in the array buffer299returned. */300static void getpacket (char *buffer);301302/* Send $<data>#<checksum> from the <data> in the array buffer. */303static void putpacket (char *buffer);304305/* Build and send a response packet in order to inform the host the306stub is stopped. */307static void stub_is_stopped (int sigval);308309/* All expected commands are sent from remote.c. Send a response according310to the description in remote.c. */311static void handle_exception (int sigval);312313/* Performs a complete re-start from scratch. ETRAX specific. */314static void kill_restart (void);315316/******************** Prototypes for global functions. ***********************/317318/* The string str is prepended with the GDB printout token and sent. */319void putDebugString (const unsigned char *str, int length); /* used by etrax100ser.c */320321/* The hook for both static (compiled) and dynamic breakpoints set by GDB.322ETRAX 100 specific. */323void handle_breakpoint (void); /* used by irq.c */324325/* The hook for an interrupt generated by GDB. ETRAX 100 specific. */326void handle_interrupt (void); /* used by irq.c */327328/* A static breakpoint to be used at startup. */329void breakpoint (void); /* called by init/main.c */330331/* From osys_int.c, executing_task contains the number of the current332executing task in osys. Does not know of object-oriented threads. */333extern unsigned char executing_task;334335/* The number of characters used for a 64 bit thread identifier. */336#define HEXCHARS_IN_THREAD_ID 16337338/* Avoid warning as the internal_stack is not used in the C-code. */339#define USEDVAR(name) { if (name) { ; } }340#define USEDFUN(name) { void (*pf)(void) = (void *)name; USEDVAR(pf) }341342/********************************** Packet I/O ******************************/343/* BUFMAX defines the maximum number of characters in344inbound/outbound buffers */345#define BUFMAX 512346347/* Run-length encoding maximum length. Send 64 at most. */348#define RUNLENMAX 64349350/* The inbound/outbound buffers used in packet I/O */351static char remcomInBuffer[BUFMAX];352static char remcomOutBuffer[BUFMAX];353354/* Error and warning messages. */355enum error_type356{357SUCCESS, E01, E02, E03, E04, E05, E06, E07358};359static char *error_message[] =360{361"",362"E01 Set current or general thread - H[c,g] - internal error.",363"E02 Change register content - P - cannot change read-only register.",364"E03 Thread is not alive.", /* T, not used. */365"E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",366"E05 Change register content - P - the register is not implemented..",367"E06 Change memory content - M - internal error.",368"E07 Change register content - P - the register is not stored on the stack"369};370/********************************* Register image ****************************/371/* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's372Reference", p. 1-1, with the additional register definitions of the373ETRAX 100LX in cris-opc.h.374There are 16 general 32-bit registers, R0-R15, where R14 is the stack375pointer, SP, and R15 is the program counter, PC.376There are 16 special registers, P0-P15, where three of the unimplemented377registers, P0, P4 and P8, are reserved as zero-registers. A read from378any of these registers returns zero and a write has no effect. */379enum register_name380{381R0, R1, R2, R3,382R4, R5, R6, R7,383R8, R9, R10, R11,384R12, R13, SP, PC,385P0, VR, P2, P3,386P4, CCR, P6, MOF,387P8, IBR, IRP, SRP,388BAR, DCCR, BRP, USP389};390391/* The register sizes of the registers in register_name. An unimplemented register392is designated by size 0 in this array. */393static int register_size[] =394{3954, 4, 4, 4,3964, 4, 4, 4,3974, 4, 4, 4,3984, 4, 4, 4,3991, 1, 0, 0,4002, 2, 0, 4,4014, 4, 4, 4,4024, 4, 4, 4403};404405/* Contains the register image of the executing thread in the assembler406part of the code in order to avoid horrible addressing modes. */407static registers reg;408409/* FIXME: Should this be used? Delete otherwise. */410/* Contains the assumed consistency state of the register image. Uses the411enum error_type for state information. */412static int consistency_status = SUCCESS;413414/********************************** Handle exceptions ************************/415/* The variable reg contains the register image associated with the416current_thread_c variable. It is a complete register image created at417entry. The reg_g contains a register image of a task where the general418registers are taken from the stack and all special registers are taken419from the executing task. It is associated with current_thread_g and used420in order to provide access mainly for 'g', 'G' and 'P'.421*/422423/* Need two task id pointers in order to handle Hct and Hgt commands. */424static int current_thread_c = 0;425static int current_thread_g = 0;426427/* Need two register images in order to handle Hct and Hgt commands. The428variable reg_g is in addition to reg above. */429static registers reg_g;430431/********************************** Breakpoint *******************************/432/* Use an internal stack in the breakpoint and interrupt response routines */433#define INTERNAL_STACK_SIZE 1024434static char internal_stack[INTERNAL_STACK_SIZE];435436/* Due to the breakpoint return pointer, a state variable is needed to keep437track of whether it is a static (compiled) or dynamic (gdb-invoked)438breakpoint to be handled. A static breakpoint uses the content of register439BRP as it is whereas a dynamic breakpoint requires subtraction with 2440in order to execute the instruction. The first breakpoint is static. */441static unsigned char is_dyn_brkp = 0;442443/********************************* String library ****************************/444/* Single-step over library functions creates trap loops. */445446/* Copy char s2[] to s1[]. */447static char*448gdb_cris_strcpy (char *s1, const char *s2)449{450char *s = s1;451452for (s = s1; (*s++ = *s2++) != '\0'; )453;454return (s1);455}456457/* Find length of s[]. */458static int459gdb_cris_strlen (const char *s)460{461const char *sc;462463for (sc = s; *sc != '\0'; sc++)464;465return (sc - s);466}467468/* Find first occurrence of c in s[n]. */469static void*470gdb_cris_memchr (const void *s, int c, int n)471{472const unsigned char uc = c;473const unsigned char *su;474475for (su = s; 0 < n; ++su, --n)476if (*su == uc)477return ((void *)su);478return (NULL);479}480/******************************* Standard library ****************************/481/* Single-step over library functions creates trap loops. */482/* Convert string to long. */483static int484gdb_cris_strtol (const char *s, char **endptr, int base)485{486char *s1;487char *sd;488int x = 0;489490for (s1 = (char*)s; (sd = gdb_cris_memchr(hex_asc, *s1, base)) != NULL; ++s1)491x = x * base + (sd - hex_asc);492493if (endptr)494{495/* Unconverted suffix is stored in endptr unless endptr is NULL. */496*endptr = s1;497}498499return x;500}501502/********************************* Register image ****************************/503/* Copy the content of a register image into another. The size n is504the size of the register image. Due to struct assignment generation of505memcpy in libc. */506static void507copy_registers (registers *dptr, registers *sptr, int n)508{509unsigned char *dreg;510unsigned char *sreg;511512for (dreg = (unsigned char*)dptr, sreg = (unsigned char*)sptr; n > 0; n--)513*dreg++ = *sreg++;514}515516#ifdef PROCESS_SUPPORT517/* Copy the stored registers from the stack. Put the register contents518of thread thread_id in the struct reg. */519static void520copy_registers_from_stack (int thread_id, registers *regptr)521{522int j;523stack_registers *s = (stack_registers *)stack_list[thread_id];524unsigned int *d = (unsigned int *)regptr;525526for (j = 13; j >= 0; j--)527*d++ = s->r[j];528regptr->sp = (unsigned int)stack_list[thread_id];529regptr->pc = s->pc;530regptr->dccr = s->dccr;531regptr->srp = s->srp;532}533534/* Copy the registers to the stack. Put the register contents of thread535thread_id from struct reg to the stack. */536static void537copy_registers_to_stack (int thread_id, registers *regptr)538{539int i;540stack_registers *d = (stack_registers *)stack_list[thread_id];541unsigned int *s = (unsigned int *)regptr;542543for (i = 0; i < 14; i++) {544d->r[i] = *s++;545}546d->pc = regptr->pc;547d->dccr = regptr->dccr;548d->srp = regptr->srp;549}550#endif551552/* Write a value to a specified register in the register image of the current553thread. Returns status code SUCCESS, E02 or E05. */554static int555write_register (int regno, char *val)556{557int status = SUCCESS;558registers *current_reg = ®559560if (regno >= R0 && regno <= PC) {561/* 32-bit register with simple offset. */562hex2mem ((unsigned char *)current_reg + regno * sizeof(unsigned int),563val, sizeof(unsigned int));564}565else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {566/* Do not support read-only registers. */567status = E02;568}569else if (regno == CCR) {570/* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented,571and P7 (MOF) is 32 bits in ETRAX 100LX. */572hex2mem ((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),573val, sizeof(unsigned short));574}575else if (regno >= MOF && regno <= USP) {576/* 32 bit register with complex offset. (P8 has been taken care of.) */577hex2mem ((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),578val, sizeof(unsigned int));579}580else {581/* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */582status = E05;583}584return status;585}586587#ifdef PROCESS_SUPPORT588/* Write a value to a specified register in the stack of a thread other589than the current thread. Returns status code SUCCESS or E07. */590static int591write_stack_register (int thread_id, int regno, char *valptr)592{593int status = SUCCESS;594stack_registers *d = (stack_registers *)stack_list[thread_id];595unsigned int val;596597hex2mem ((unsigned char *)&val, valptr, sizeof(unsigned int));598if (regno >= R0 && regno < SP) {599d->r[regno] = val;600}601else if (regno == SP) {602stack_list[thread_id] = val;603}604else if (regno == PC) {605d->pc = val;606}607else if (regno == SRP) {608d->srp = val;609}610else if (regno == DCCR) {611d->dccr = val;612}613else {614/* Do not support registers in the current thread. */615status = E07;616}617return status;618}619#endif620621/* Read a value from a specified register in the register image. Returns the622value in the register or -1 for non-implemented registers.623Should check consistency_status after a call which may be E05 after changes624in the implementation. */625static int626read_register (char regno, unsigned int *valptr)627{628registers *current_reg = ®629630if (regno >= R0 && regno <= PC) {631/* 32-bit register with simple offset. */632*valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));633return SUCCESS;634}635else if (regno == P0 || regno == VR) {636/* 8 bit register with complex offset. */637*valptr = (unsigned int)(*(unsigned char *)638((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));639return SUCCESS;640}641else if (regno == P4 || regno == CCR) {642/* 16 bit register with complex offset. */643*valptr = (unsigned int)(*(unsigned short *)644((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));645return SUCCESS;646}647else if (regno >= MOF && regno <= USP) {648/* 32 bit register with complex offset. */649*valptr = *(unsigned int *)((char *)&(current_reg->p8)650+ (regno-P8) * sizeof(unsigned int));651return SUCCESS;652}653else {654/* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */655consistency_status = E05;656return E05;657}658}659660/********************************** Packet I/O ******************************/661/* Returns the integer equivalent of a hexadecimal character. */662static int663hex (char ch)664{665if ((ch >= 'a') && (ch <= 'f'))666return (ch - 'a' + 10);667if ((ch >= '0') && (ch <= '9'))668return (ch - '0');669if ((ch >= 'A') && (ch <= 'F'))670return (ch - 'A' + 10);671return (-1);672}673674/* Convert the memory, pointed to by mem into hexadecimal representation.675Put the result in buf, and return a pointer to the last character676in buf (null). */677678static int do_printk = 0;679680static char *681mem2hex(char *buf, unsigned char *mem, int count)682{683int i;684int ch;685686if (mem == NULL) {687/* Bogus read from m0. FIXME: What constitutes a valid address? */688for (i = 0; i < count; i++) {689*buf++ = '0';690*buf++ = '0';691}692} else {693/* Valid mem address. */694for (i = 0; i < count; i++) {695ch = *mem++;696buf = pack_hex_byte(buf, ch);697}698}699700/* Terminate properly. */701*buf = '\0';702return (buf);703}704705/* Convert the array, in hexadecimal representation, pointed to by buf into706binary representation. Put the result in mem, and return a pointer to707the character after the last byte written. */708static unsigned char*709hex2mem (unsigned char *mem, char *buf, int count)710{711int i;712unsigned char ch;713for (i = 0; i < count; i++) {714ch = hex (*buf++) << 4;715ch = ch + hex (*buf++);716*mem++ = ch;717}718return (mem);719}720721/* Put the content of the array, in binary representation, pointed to by buf722into memory pointed to by mem, and return a pointer to the character after723the last byte written.724Gdb will escape $, #, and the escape char (0x7d). */725static unsigned char*726bin2mem (unsigned char *mem, unsigned char *buf, int count)727{728int i;729unsigned char *next;730for (i = 0; i < count; i++) {731/* Check for any escaped characters. Be paranoid and732only unescape chars that should be escaped. */733if (*buf == 0x7d) {734next = buf + 1;735if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */736{737buf++;738*buf += 0x20;739}740}741*mem++ = *buf++;742}743return (mem);744}745746/* Await the sequence $<data>#<checksum> and store <data> in the array buffer747returned. */748static void749getpacket (char *buffer)750{751unsigned char checksum;752unsigned char xmitcsum;753int i;754int count;755char ch;756do {757while ((ch = getDebugChar ()) != '$')758/* Wait for the start character $ and ignore all other characters */;759checksum = 0;760xmitcsum = -1;761count = 0;762/* Read until a # or the end of the buffer is reached */763while (count < BUFMAX) {764ch = getDebugChar ();765if (ch == '#')766break;767checksum = checksum + ch;768buffer[count] = ch;769count = count + 1;770}771buffer[count] = '\0';772773if (ch == '#') {774xmitcsum = hex (getDebugChar ()) << 4;775xmitcsum += hex (getDebugChar ());776if (checksum != xmitcsum) {777/* Wrong checksum */778putDebugChar ('-');779}780else {781/* Correct checksum */782putDebugChar ('+');783/* If sequence characters are received, reply with them */784if (buffer[2] == ':') {785putDebugChar (buffer[0]);786putDebugChar (buffer[1]);787/* Remove the sequence characters from the buffer */788count = gdb_cris_strlen (buffer);789for (i = 3; i <= count; i++)790buffer[i - 3] = buffer[i];791}792}793}794} while (checksum != xmitcsum);795}796797/* Send $<data>#<checksum> from the <data> in the array buffer. */798799static void800putpacket(char *buffer)801{802int checksum;803int runlen;804int encode;805806do {807char *src = buffer;808putDebugChar ('$');809checksum = 0;810while (*src) {811/* Do run length encoding */812putDebugChar (*src);813checksum += *src;814runlen = 0;815while (runlen < RUNLENMAX && *src == src[runlen]) {816runlen++;817}818if (runlen > 3) {819/* Got a useful amount */820putDebugChar ('*');821checksum += '*';822encode = runlen + ' ' - 4;823putDebugChar (encode);824checksum += encode;825src += runlen;826}827else {828src++;829}830}831putDebugChar('#');832putDebugChar(hex_asc_hi(checksum));833putDebugChar(hex_asc_lo(checksum));834} while(kgdb_started && (getDebugChar() != '+'));835}836837/* The string str is prepended with the GDB printout token and sent. Required838in traditional implementations. */839void840putDebugString (const unsigned char *str, int length)841{842remcomOutBuffer[0] = 'O';843mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);844putpacket(remcomOutBuffer);845}846847/********************************** Handle exceptions ************************/848/* Build and send a response packet in order to inform the host the849stub is stopped. TAAn...:r...;n...:r...;n...:r...;850AA = signal number851n... = register number (hex)852r... = register contents853n... = `thread'854r... = thread process ID. This is a hex integer.855n... = other string not starting with valid hex digit.856gdb should ignore this n,r pair and go on to the next.857This way we can extend the protocol. */858static void859stub_is_stopped(int sigval)860{861char *ptr = remcomOutBuffer;862int regno;863864unsigned int reg_cont;865int status;866867/* Send trap type (converted to signal) */868869*ptr++ = 'T';870ptr = pack_hex_byte(ptr, sigval);871872/* Send register contents. We probably only need to send the873* PC, frame pointer and stack pointer here. Other registers will be874* explicitly asked for. But for now, send all.875*/876877for (regno = R0; regno <= USP; regno++) {878/* Store n...:r...; for the registers in the buffer. */879880status = read_register (regno, ®_cont);881882if (status == SUCCESS) {883ptr = pack_hex_byte(ptr, regno);884*ptr++ = ':';885886ptr = mem2hex(ptr, (unsigned char *)®_cont,887register_size[regno]);888*ptr++ = ';';889}890891}892893#ifdef PROCESS_SUPPORT894/* Store the registers of the executing thread. Assume that both step,895continue, and register content requests are with respect to this896thread. The executing task is from the operating system scheduler. */897898current_thread_c = executing_task;899current_thread_g = executing_task;900901/* A struct assignment translates into a libc memcpy call. Avoid902all libc functions in order to prevent recursive break points. */903copy_registers (®_g, ®, sizeof(registers));904905/* Store thread:r...; with the executing task TID. */906gdb_cris_strcpy (&remcomOutBuffer[pos], "thread:");907pos += gdb_cris_strlen ("thread:");908remcomOutBuffer[pos++] = hex_asc_hi(executing_task);909remcomOutBuffer[pos++] = hex_asc_lo(executing_task);910gdb_cris_strcpy (&remcomOutBuffer[pos], ";");911#endif912913/* null-terminate and send it off */914915*ptr = 0;916917putpacket (remcomOutBuffer);918}919920/* All expected commands are sent from remote.c. Send a response according921to the description in remote.c. */922static void923handle_exception (int sigval)924{925/* Avoid warning of not used. */926927USEDFUN(handle_exception);928USEDVAR(internal_stack[0]);929930/* Send response. */931932stub_is_stopped (sigval);933934for (;;) {935remcomOutBuffer[0] = '\0';936getpacket (remcomInBuffer);937switch (remcomInBuffer[0]) {938case 'g':939/* Read registers: g940Success: Each byte of register data is described by two hex digits.941Registers are in the internal order for GDB, and the bytes942in a register are in the same order the machine uses.943Failure: void. */944945{946#ifdef PROCESS_SUPPORT947/* Use the special register content in the executing thread. */948copy_registers (®_g, ®, sizeof(registers));949/* Replace the content available on the stack. */950if (current_thread_g != executing_task) {951copy_registers_from_stack (current_thread_g, ®_g);952}953mem2hex ((unsigned char *)remcomOutBuffer, (unsigned char *)®_g, sizeof(registers));954#else955mem2hex(remcomOutBuffer, (char *)®, sizeof(registers));956#endif957}958break;959960case 'G':961/* Write registers. GXX..XX962Each byte of register data is described by two hex digits.963Success: OK964Failure: void. */965#ifdef PROCESS_SUPPORT966hex2mem ((unsigned char *)®_g, &remcomInBuffer[1], sizeof(registers));967if (current_thread_g == executing_task) {968copy_registers (®, ®_g, sizeof(registers));969}970else {971copy_registers_to_stack(current_thread_g, ®_g);972}973#else974hex2mem((char *)®, &remcomInBuffer[1], sizeof(registers));975#endif976gdb_cris_strcpy (remcomOutBuffer, "OK");977break;978979case 'P':980/* Write register. Pn...=r...981Write register n..., hex value without 0x, with value r...,982which contains a hex value without 0x and two hex digits983for each byte in the register (target byte order). P1f=11223344 means984set register 31 to 44332211.985Success: OK986Failure: E02, E05 */987{988char *suffix;989int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);990int status;991#ifdef PROCESS_SUPPORT992if (current_thread_g != executing_task)993status = write_stack_register (current_thread_g, regno, suffix+1);994else995#endif996status = write_register (regno, suffix+1);997998switch (status) {999case E02:1000/* Do not support read-only registers. */1001gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);1002break;1003case E05:1004/* Do not support non-existing registers. */1005gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);1006break;1007case E07:1008/* Do not support non-existing registers on the stack. */1009gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);1010break;1011default:1012/* Valid register number. */1013gdb_cris_strcpy (remcomOutBuffer, "OK");1014break;1015}1016}1017break;10181019case 'm':1020/* Read from memory. mAA..AA,LLLL1021AA..AA is the address and LLLL is the length.1022Success: XX..XX is the memory content. Can be fewer bytes than1023requested if only part of the data may be read. m6000120a,6c means1024retrieve 108 byte from base address 6000120a.1025Failure: void. */1026{1027char *suffix;1028unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],1029&suffix, 16); int length = gdb_cris_strtol(suffix+1, 0, 16);10301031mem2hex(remcomOutBuffer, addr, length);1032}1033break;10341035case 'X':1036/* Write to memory. XAA..AA,LLLL:XX..XX1037AA..AA is the start address, LLLL is the number of bytes, and1038XX..XX is the binary data.1039Success: OK1040Failure: void. */1041case 'M':1042/* Write to memory. MAA..AA,LLLL:XX..XX1043AA..AA is the start address, LLLL is the number of bytes, and1044XX..XX is the hexadecimal data.1045Success: OK1046Failure: void. */1047{1048char *lenptr;1049char *dataptr;1050unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],1051&lenptr, 16);1052int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);1053if (*lenptr == ',' && *dataptr == ':') {1054if (remcomInBuffer[0] == 'M') {1055hex2mem(addr, dataptr + 1, length);1056}1057else /* X */ {1058bin2mem(addr, dataptr + 1, length);1059}1060gdb_cris_strcpy (remcomOutBuffer, "OK");1061}1062else {1063gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);1064}1065}1066break;10671068case 'c':1069/* Continue execution. cAA..AA1070AA..AA is the address where execution is resumed. If AA..AA is1071omitted, resume at the present address.1072Success: return to the executing thread.1073Failure: will never know. */1074if (remcomInBuffer[1] != '\0') {1075reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);1076}1077enableDebugIRQ();1078return;10791080case 's':1081/* Step. sAA..AA1082AA..AA is the address where execution is resumed. If AA..AA is1083omitted, resume at the present address. Success: return to the1084executing thread. Failure: will never know.10851086Should never be invoked. The single-step is implemented on1087the host side. If ever invoked, it is an internal error E04. */1088gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);1089putpacket (remcomOutBuffer);1090return;10911092case '?':1093/* The last signal which caused a stop. ?1094Success: SAA, where AA is the signal number.1095Failure: void. */1096remcomOutBuffer[0] = 'S';1097remcomOutBuffer[1] = hex_asc_hi(sigval);1098remcomOutBuffer[2] = hex_asc_lo(sigval);1099remcomOutBuffer[3] = 0;1100break;11011102case 'D':1103/* Detach from host. D1104Success: OK, and return to the executing thread.1105Failure: will never know */1106putpacket ("OK");1107return;11081109case 'k':1110case 'r':1111/* kill request or reset request.1112Success: restart of target.1113Failure: will never know. */1114kill_restart ();1115break;11161117case 'C':1118case 'S':1119case '!':1120case 'R':1121case 'd':1122/* Continue with signal sig. Csig;AA..AA1123Step with signal sig. Ssig;AA..AA1124Use the extended remote protocol. !1125Restart the target system. R01126Toggle debug flag. d1127Search backwards. tAA:PP,MM1128Not supported: E04 */1129gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);1130break;1131#ifdef PROCESS_SUPPORT11321133case 'T':1134/* Thread alive. TXX1135Is thread XX alive?1136Success: OK, thread XX is alive.1137Failure: E03, thread XX is dead. */1138{1139int thread_id = (int)gdb_cris_strtol (&remcomInBuffer[1], 0, 16);1140/* Cannot tell whether it is alive or not. */1141if (thread_id >= 0 && thread_id < number_of_tasks)1142gdb_cris_strcpy (remcomOutBuffer, "OK");1143}1144break;11451146case 'H':1147/* Set thread for subsequent operations: Hct1148c = 'c' for thread used in step and continue;1149t can be -1 for all threads.1150c = 'g' for thread used in other operations.1151t = 0 means pick any thread.1152Success: OK1153Failure: E01 */1154{1155int thread_id = gdb_cris_strtol (&remcomInBuffer[2], 0, 16);1156if (remcomInBuffer[1] == 'c') {1157/* c = 'c' for thread used in step and continue */1158/* Do not change current_thread_c here. It would create a mess in1159the scheduler. */1160gdb_cris_strcpy (remcomOutBuffer, "OK");1161}1162else if (remcomInBuffer[1] == 'g') {1163/* c = 'g' for thread used in other operations.1164t = 0 means pick any thread. Impossible since the scheduler does1165not allow that. */1166if (thread_id >= 0 && thread_id < number_of_tasks) {1167current_thread_g = thread_id;1168gdb_cris_strcpy (remcomOutBuffer, "OK");1169}1170else {1171/* Not expected - send an error message. */1172gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);1173}1174}1175else {1176/* Not expected - send an error message. */1177gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);1178}1179}1180break;11811182case 'q':1183case 'Q':1184/* Query of general interest. qXXXX1185Set general value XXXX. QXXXX=yyyy */1186{1187int pos;1188int nextpos;1189int thread_id;11901191switch (remcomInBuffer[1]) {1192case 'C':1193/* Identify the remote current thread. */1194gdb_cris_strcpy (&remcomOutBuffer[0], "QC");1195remcomOutBuffer[2] = hex_asc_hi(current_thread_c);1196remcomOutBuffer[3] = hex_asc_lo(current_thread_c);1197remcomOutBuffer[4] = '\0';1198break;1199case 'L':1200gdb_cris_strcpy (&remcomOutBuffer[0], "QM");1201/* Reply with number of threads. */1202if (os_is_started()) {1203remcomOutBuffer[2] = hex_asc_hi(number_of_tasks);1204remcomOutBuffer[3] = hex_asc_lo(number_of_tasks);1205}1206else {1207remcomOutBuffer[2] = hex_asc_hi(0);1208remcomOutBuffer[3] = hex_asc_lo(1);1209}1210/* Done with the reply. */1211remcomOutBuffer[4] = hex_asc_lo(1);1212pos = 5;1213/* Expects the argument thread id. */1214for (; pos < (5 + HEXCHARS_IN_THREAD_ID); pos++)1215remcomOutBuffer[pos] = remcomInBuffer[pos];1216/* Reply with the thread identifiers. */1217if (os_is_started()) {1218/* Store the thread identifiers of all tasks. */1219for (thread_id = 0; thread_id < number_of_tasks; thread_id++) {1220nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;1221for (; pos < nextpos; pos ++)1222remcomOutBuffer[pos] = hex_asc_lo(0);1223remcomOutBuffer[pos++] = hex_asc_lo(thread_id);1224}1225}1226else {1227/* Store the thread identifier of the boot task. */1228nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;1229for (; pos < nextpos; pos ++)1230remcomOutBuffer[pos] = hex_asc_lo(0);1231remcomOutBuffer[pos++] = hex_asc_lo(current_thread_c);1232}1233remcomOutBuffer[pos] = '\0';1234break;1235default:1236/* Not supported: "" */1237/* Request information about section offsets: qOffsets. */1238remcomOutBuffer[0] = 0;1239break;1240}1241}1242break;1243#endif /* PROCESS_SUPPORT */12441245default:1246/* The stub should ignore other request and send an empty1247response ($#<checksum>). This way we can extend the protocol and GDB1248can tell whether the stub it is talking to uses the old or the new. */1249remcomOutBuffer[0] = 0;1250break;1251}1252putpacket(remcomOutBuffer);1253}1254}12551256/* Performs a complete re-start from scratch. */1257static void1258kill_restart ()1259{1260machine_restart("");1261}12621263/********************************** Breakpoint *******************************/1264/* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.1265An internal stack is used by the stub. The register image of the caller is1266stored in the structure register_image.1267Interactive communication with the host is handled by handle_exception and1268finally the register image is restored. */12691270void kgdb_handle_breakpoint(void);12711272asm ("1273.global kgdb_handle_breakpoint1274kgdb_handle_breakpoint:1275;;1276;; Response to the break-instruction1277;;1278;; Create a register image of the caller1279;;1280move $dccr,[reg+0x5E] ; Save the flags in DCCR before disable interrupts1281di ; Disable interrupts1282move.d $r0,[reg] ; Save R01283move.d $r1,[reg+0x04] ; Save R11284move.d $r2,[reg+0x08] ; Save R21285move.d $r3,[reg+0x0C] ; Save R31286move.d $r4,[reg+0x10] ; Save R41287move.d $r5,[reg+0x14] ; Save R51288move.d $r6,[reg+0x18] ; Save R61289move.d $r7,[reg+0x1C] ; Save R71290move.d $r8,[reg+0x20] ; Save R81291move.d $r9,[reg+0x24] ; Save R91292move.d $r10,[reg+0x28] ; Save R101293move.d $r11,[reg+0x2C] ; Save R111294move.d $r12,[reg+0x30] ; Save R121295move.d $r13,[reg+0x34] ; Save R131296move.d $sp,[reg+0x38] ; Save SP (R14)1297;; Due to the old assembler-versions BRP might not be recognized1298.word 0xE670 ; move brp,$r01299subq 2,$r0 ; Set to address of previous instruction.1300move.d $r0,[reg+0x3c] ; Save the address in PC (R15)1301clear.b [reg+0x40] ; Clear P01302move $vr,[reg+0x41] ; Save special register P11303clear.w [reg+0x42] ; Clear P41304move $ccr,[reg+0x44] ; Save special register CCR1305move $mof,[reg+0x46] ; P71306clear.d [reg+0x4A] ; Clear P81307move $ibr,[reg+0x4E] ; P9,1308move $irp,[reg+0x52] ; P10,1309move $srp,[reg+0x56] ; P11,1310move $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR1311; P13, register DCCR already saved1312;; Due to the old assembler-versions BRP might not be recognized1313.word 0xE670 ; move brp,r01314;; Static (compiled) breakpoints must return to the next instruction in order1315;; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction1316;; in order to execute it when execution is continued.1317test.b [is_dyn_brkp] ; Is this a dynamic breakpoint?1318beq is_static ; No, a static breakpoint1319nop1320subq 2,$r0 ; rerun the instruction the break replaced1321is_static:1322moveq 1,$r11323move.b $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint1324move.d $r0,[reg+0x62] ; Save the return address in BRP1325move $usp,[reg+0x66] ; USP1326;;1327;; Handle the communication1328;;1329move.d internal_stack+1020,$sp ; Use the internal stack which grows upward1330moveq 5,$r10 ; SIGTRAP1331jsr handle_exception ; Interactive routine1332;;1333;; Return to the caller1334;;1335move.d [reg],$r0 ; Restore R01336move.d [reg+0x04],$r1 ; Restore R11337move.d [reg+0x08],$r2 ; Restore R21338move.d [reg+0x0C],$r3 ; Restore R31339move.d [reg+0x10],$r4 ; Restore R41340move.d [reg+0x14],$r5 ; Restore R51341move.d [reg+0x18],$r6 ; Restore R61342move.d [reg+0x1C],$r7 ; Restore R71343move.d [reg+0x20],$r8 ; Restore R81344move.d [reg+0x24],$r9 ; Restore R91345move.d [reg+0x28],$r10 ; Restore R101346move.d [reg+0x2C],$r11 ; Restore R111347move.d [reg+0x30],$r12 ; Restore R121348move.d [reg+0x34],$r13 ; Restore R131349;;1350;; FIXME: Which registers should be restored?1351;;1352move.d [reg+0x38],$sp ; Restore SP (R14)1353move [reg+0x56],$srp ; Restore the subroutine return pointer.1354move [reg+0x5E],$dccr ; Restore DCCR1355move [reg+0x66],$usp ; Restore USP1356jump [reg+0x62] ; A jump to the content in register BRP works.1357nop ;1358");13591360/* The hook for an interrupt generated by GDB. An internal stack is used1361by the stub. The register image of the caller is stored in the structure1362register_image. Interactive communication with the host is handled by1363handle_exception and finally the register image is restored. Due to the1364old assembler which does not recognise the break instruction and the1365breakpoint return pointer hex-code is used. */13661367void kgdb_handle_serial(void);13681369asm ("1370.global kgdb_handle_serial1371kgdb_handle_serial:1372;;1373;; Response to a serial interrupt1374;;13751376move $dccr,[reg+0x5E] ; Save the flags in DCCR1377di ; Disable interrupts1378move.d $r0,[reg] ; Save R01379move.d $r1,[reg+0x04] ; Save R11380move.d $r2,[reg+0x08] ; Save R21381move.d $r3,[reg+0x0C] ; Save R31382move.d $r4,[reg+0x10] ; Save R41383move.d $r5,[reg+0x14] ; Save R51384move.d $r6,[reg+0x18] ; Save R61385move.d $r7,[reg+0x1C] ; Save R71386move.d $r8,[reg+0x20] ; Save R81387move.d $r9,[reg+0x24] ; Save R91388move.d $r10,[reg+0x28] ; Save R101389move.d $r11,[reg+0x2C] ; Save R111390move.d $r12,[reg+0x30] ; Save R121391move.d $r13,[reg+0x34] ; Save R131392move.d $sp,[reg+0x38] ; Save SP (R14)1393move $irp,[reg+0x3c] ; Save the address in PC (R15)1394clear.b [reg+0x40] ; Clear P01395move $vr,[reg+0x41] ; Save special register P1,1396clear.w [reg+0x42] ; Clear P41397move $ccr,[reg+0x44] ; Save special register CCR1398move $mof,[reg+0x46] ; P71399clear.d [reg+0x4A] ; Clear P81400move $ibr,[reg+0x4E] ; P9,1401move $irp,[reg+0x52] ; P10,1402move $srp,[reg+0x56] ; P11,1403move $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR1404; P13, register DCCR already saved1405;; Due to the old assembler-versions BRP might not be recognized1406.word 0xE670 ; move brp,r01407move.d $r0,[reg+0x62] ; Save the return address in BRP1408move $usp,[reg+0x66] ; USP14091410;; get the serial character (from debugport.c) and check if it is a ctrl-c14111412jsr getDebugChar1413cmp.b 3, $r101414bne goback1415nop14161417move.d [reg+0x5E], $r10 ; Get DCCR1418btstq 8, $r10 ; Test the U-flag.1419bmi goback1420nop14211422;;1423;; Handle the communication1424;;1425move.d internal_stack+1020,$sp ; Use the internal stack1426moveq 2,$r10 ; SIGINT1427jsr handle_exception ; Interactive routine14281429goback:1430;;1431;; Return to the caller1432;;1433move.d [reg],$r0 ; Restore R01434move.d [reg+0x04],$r1 ; Restore R11435move.d [reg+0x08],$r2 ; Restore R21436move.d [reg+0x0C],$r3 ; Restore R31437move.d [reg+0x10],$r4 ; Restore R41438move.d [reg+0x14],$r5 ; Restore R51439move.d [reg+0x18],$r6 ; Restore R61440move.d [reg+0x1C],$r7 ; Restore R71441move.d [reg+0x20],$r8 ; Restore R81442move.d [reg+0x24],$r9 ; Restore R91443move.d [reg+0x28],$r10 ; Restore R101444move.d [reg+0x2C],$r11 ; Restore R111445move.d [reg+0x30],$r12 ; Restore R121446move.d [reg+0x34],$r13 ; Restore R131447;;1448;; FIXME: Which registers should be restored?1449;;1450move.d [reg+0x38],$sp ; Restore SP (R14)1451move [reg+0x56],$srp ; Restore the subroutine return pointer.1452move [reg+0x5E],$dccr ; Restore DCCR1453move [reg+0x66],$usp ; Restore USP1454reti ; Return from the interrupt routine1455nop1456");14571458/* Use this static breakpoint in the start-up only. */14591460void1461breakpoint(void)1462{1463kgdb_started = 1;1464is_dyn_brkp = 0; /* This is a static, not a dynamic breakpoint. */1465__asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */1466}14671468/* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */14691470void1471kgdb_init(void)1472{1473/* could initialize debug port as well but it's done in head.S already... */14741475/* breakpoint handler is now set in irq.c */1476set_int_vector(8, kgdb_handle_serial);14771478enableDebugIRQ();1479}14801481/****************************** End of file **********************************/148214831484