linux/arch/cris/arch-v10/kernel/kgdb.c
<<
>>
Prefs
   1/*!**************************************************************************
   2*!
   3*! FILE NAME  : kgdb.c
   4*!
   5*! DESCRIPTION: Implementation of the gdb stub with respect to ETRAX 100.
   6*!              It is a mix of arch/m68k/kernel/kgdb.c and cris_stub.c.
   7*!
   8*!---------------------------------------------------------------------------
   9*! HISTORY
  10*!
  11*! DATE         NAME            CHANGES
  12*! ----         ----            -------
  13*! Apr 26 1999  Hendrik Ruijter Initial version.
  14*! May  6 1999  Hendrik Ruijter Removed call to strlen in libc and removed
  15*!                              struct assignment as it generates calls to
  16*!                              memcpy in libc.
  17*! Jun 17 1999  Hendrik Ruijter Added gdb 4.18 support. 'X', 'qC' and 'qL'.
  18*! Jul 21 1999  Bjorn Wesen     eLinux port
  19*!
  20*!---------------------------------------------------------------------------
  21*!
  22*! (C) Copyright 1999, Axis Communications AB, LUND, SWEDEN
  23*!
  24*!**************************************************************************/
  25/* @(#) cris_stub.c 1.3 06/17/99 */
  26
  27/*
  28 *  kgdb usage notes:
  29 *  -----------------
  30 *
  31 * If you select CONFIG_ETRAX_KGDB in the configuration, the kernel will be 
  32 * built with different gcc flags: "-g" is added to get debug infos, and
  33 * "-fomit-frame-pointer" is omitted to make debugging easier. Since the
  34 * resulting kernel will be quite big (approx. > 7 MB), it will be stripped
  35 * before compresion. Such a kernel will behave just as usually, except if
  36 * given a "debug=<device>" command line option. (Only serial devices are
  37 * allowed for <device>, i.e. no printers or the like; possible values are
  38 * machine depedend and are the same as for the usual debug device, the one
  39 * for logging kernel messages.) If that option is given and the device can be
  40 * initialized, the kernel will connect to the remote gdb in trap_init(). The
  41 * serial parameters are fixed to 8N1 and 115200 bps, for easyness of
  42 * implementation.
  43 *
  44 * To start a debugging session, start that gdb with the debugging kernel
  45 * image (the one with the symbols, vmlinux.debug) named on the command line.
  46 * This file will be used by gdb to get symbol and debugging infos about the
  47 * kernel. Next, select remote debug mode by
  48 *    target remote <device>
  49 * where <device> is the name of the serial device over which the debugged
  50 * machine is connected. Maybe you have to adjust the baud rate by
  51 *    set remotebaud <rate>
  52 * or also other parameters with stty:
  53 *    shell stty ... </dev/...
  54 * If the kernel to debug has already booted, it waited for gdb and now
  55 * connects, and you'll see a breakpoint being reported. If the kernel isn't
  56 * running yet, start it now. The order of gdb and the kernel doesn't matter.
  57 * Another thing worth knowing about in the getting-started phase is how to
  58 * debug the remote protocol itself. This is activated with
  59 *    set remotedebug 1
  60 * gdb will then print out each packet sent or received. You'll also get some
  61 * messages about the gdb stub on the console of the debugged machine.
  62 *
  63 * If all that works, you can use lots of the usual debugging techniques on
  64 * the kernel, e.g. inspecting and changing variables/memory, setting
  65 * breakpoints, single stepping and so on. It's also possible to interrupt the
  66 * debugged kernel by pressing C-c in gdb. Have fun! :-)
  67 *
  68 * The gdb stub is entered (and thus the remote gdb gets control) in the
  69 * following situations:
  70 *
  71 *  - If breakpoint() is called. This is just after kgdb initialization, or if
  72 *    a breakpoint() call has been put somewhere into the kernel source.
  73 *    (Breakpoints can of course also be set the usual way in gdb.)
  74 *    In eLinux, we call breakpoint() in init/main.c after IRQ initialization.
  75 *
  76 *  - If there is a kernel exception, i.e. bad_super_trap() or die_if_kernel()
  77 *    are entered. All the CPU exceptions are mapped to (more or less..., see
  78 *    the hard_trap_info array below) appropriate signal, which are reported
  79 *    to gdb. die_if_kernel() is usually called after some kind of access
  80 *    error and thus is reported as SIGSEGV.
  81 *
  82 *  - When panic() is called. This is reported as SIGABRT.
  83 *
  84 *  - If C-c is received over the serial line, which is treated as
  85 *    SIGINT.
  86 *
  87 * Of course, all these signals are just faked for gdb, since there is no
  88 * signal concept as such for the kernel. It also isn't possible --obviously--
  89 * to set signal handlers from inside gdb, or restart the kernel with a
  90 * signal.
  91 *
  92 * Current limitations:
  93 *
  94 *  - While the kernel is stopped, interrupts are disabled for safety reasons
  95 *    (i.e., variables not changing magically or the like). But this also
  96 *    means that the clock isn't running anymore, and that interrupts from the
  97 *    hardware may get lost/not be served in time. This can cause some device
  98 *    errors...
  99 *
 100 *  - When single-stepping, only one instruction of the current thread is
 101 *    executed, but interrupts are allowed for that time and will be serviced
 102 *    if pending. Be prepared for that.
 103 *
 104 *  - All debugging happens in kernel virtual address space. There's no way to
 105 *    access physical memory not mapped in kernel space, or to access user
 106 *    space. A way to work around this is using get_user_long & Co. in gdb
 107 *    expressions, but only for the current process.
 108 *
 109 *  - Interrupting the kernel only works if interrupts are currently allowed,
 110 *    and the interrupt of the serial line isn't blocked by some other means
 111 *    (IPL too high, disabled, ...)
 112 *
 113 *  - The gdb stub is currently not reentrant, i.e. errors that happen therein
 114 *    (e.g. accessing invalid memory) may not be caught correctly. This could
 115 *    be removed in future by introducing a stack of struct registers.
 116 *
 117 */
 118
 119/*
 120 *  To enable debugger support, two things need to happen.  One, a
 121 *  call to kgdb_init() is necessary in order to allow any breakpoints
 122 *  or error conditions to be properly intercepted and reported to gdb.
 123 *  Two, a breakpoint needs to be generated to begin communication.  This
 124 *  is most easily accomplished by a call to breakpoint(). 
 125 *
 126 *    The following gdb commands are supported:
 127 *
 128 * command          function                               Return value
 129 *
 130 *    g             return the value of the CPU registers  hex data or ENN
 131 *    G             set the value of the CPU registers     OK or ENN
 132 *
 133 *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
 134 *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
 135 *
 136 *    c             Resume at current address              SNN   ( signal NN)
 137 *    cAA..AA       Continue at address AA..AA             SNN
 138 *
 139 *    s             Step one instruction                   SNN
 140 *    sAA..AA       Step one instruction from AA..AA       SNN
 141 *
 142 *    k             kill
 143 *
 144 *    ?             What was the last sigval ?             SNN   (signal NN)
 145 *
 146 *    bBB..BB       Set baud rate to BB..BB                OK or BNN, then sets
 147 *                                                         baud rate
 148 *
 149 * All commands and responses are sent with a packet which includes a
 150 * checksum.  A packet consists of
 151 *
 152 * $<packet info>#<checksum>.
 153 *
 154 * where
 155 * <packet info> :: <characters representing the command or response>
 156 * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
 157 *
 158 * When a packet is received, it is first acknowledged with either '+' or '-'.
 159 * '+' indicates a successful transfer.  '-' indicates a failed transfer.
 160 *
 161 * Example:
 162 *
 163 * Host:                  Reply:
 164 * $m0,10#2a               +$00010203040506070809101112131415#42
 165 *
 166 */
 167
 168
 169#include <linux/string.h>
 170#include <linux/signal.h>
 171#include <linux/kernel.h>
 172#include <linux/delay.h>
 173#include <linux/linkage.h>
 174#include <linux/reboot.h>
 175
 176#include <asm/setup.h>
 177#include <asm/ptrace.h>
 178
 179#include <arch/svinto.h>
 180#include <asm/irq.h>
 181
 182static int kgdb_started = 0;
 183
 184/********************************* Register image ****************************/
 185/* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
 186   Reference", p. 1-1, with the additional register definitions of the
 187   ETRAX 100LX in cris-opc.h.
 188   There are 16 general 32-bit registers, R0-R15, where R14 is the stack
 189   pointer, SP, and R15 is the program counter, PC.
 190   There are 16 special registers, P0-P15, where three of the unimplemented
 191   registers, P0, P4 and P8, are reserved as zero-registers. A read from
 192   any of these registers returns zero and a write has no effect. */
 193
 194typedef
 195struct register_image
 196{
 197        /* Offset */
 198        unsigned int     r0;   /* 0x00 */
 199        unsigned int     r1;   /* 0x04 */
 200        unsigned int     r2;   /* 0x08 */
 201        unsigned int     r3;   /* 0x0C */
 202        unsigned int     r4;   /* 0x10 */
 203        unsigned int     r5;   /* 0x14 */
 204        unsigned int     r6;   /* 0x18 */
 205        unsigned int     r7;   /* 0x1C */
 206        unsigned int     r8;   /* 0x20 Frame pointer */
 207        unsigned int     r9;   /* 0x24 */
 208        unsigned int    r10;   /* 0x28 */
 209        unsigned int    r11;   /* 0x2C */
 210        unsigned int    r12;   /* 0x30 */
 211        unsigned int    r13;   /* 0x34 */
 212        unsigned int     sp;   /* 0x38 Stack pointer */
 213        unsigned int     pc;   /* 0x3C Program counter */
 214
 215        unsigned char    p0;   /* 0x40 8-bit zero-register */
 216        unsigned char    vr;   /* 0x41 Version register */
 217
 218        unsigned short   p4;   /* 0x42 16-bit zero-register */
 219        unsigned short  ccr;   /* 0x44 Condition code register */
 220        
 221        unsigned int    mof;   /* 0x46 Multiply overflow register */
 222        
 223        unsigned int     p8;   /* 0x4A 32-bit zero-register */
 224        unsigned int    ibr;   /* 0x4E Interrupt base register */
 225        unsigned int    irp;   /* 0x52 Interrupt return pointer */
 226        unsigned int    srp;   /* 0x56 Subroutine return pointer */
 227        unsigned int    bar;   /* 0x5A Breakpoint address register */
 228        unsigned int   dccr;   /* 0x5E Double condition code register */
 229        unsigned int    brp;   /* 0x62 Breakpoint return pointer (pc in caller) */
 230        unsigned int    usp;   /* 0x66 User mode stack pointer */
 231} registers;
 232
 233/* Serial port, reads one character. ETRAX 100 specific. from debugport.c */
 234int getDebugChar (void);
 235
 236/* Serial port, writes one character. ETRAX 100 specific. from debugport.c */
 237void putDebugChar (int val);
 238
 239void enableDebugIRQ (void);
 240
 241/******************** Prototypes for global functions. ***********************/
 242
 243/* The string str is prepended with the GDB printout token and sent. */
 244void putDebugString (const unsigned char *str, int length); /* used by etrax100ser.c */
 245
 246/* The hook for both static (compiled) and dynamic breakpoints set by GDB.
 247   ETRAX 100 specific. */
 248void handle_breakpoint (void);                          /* used by irq.c */
 249
 250/* The hook for an interrupt generated by GDB. ETRAX 100 specific. */
 251void handle_interrupt (void);                           /* used by irq.c */
 252
 253/* A static breakpoint to be used at startup. */
 254void breakpoint (void);                                 /* called by init/main.c */
 255
 256/* From osys_int.c, executing_task contains the number of the current
 257   executing task in osys. Does not know of object-oriented threads. */
 258extern unsigned char executing_task;
 259
 260/* The number of characters used for a 64 bit thread identifier. */
 261#define HEXCHARS_IN_THREAD_ID 16
 262
 263/********************************** Packet I/O ******************************/
 264/* BUFMAX defines the maximum number of characters in
 265   inbound/outbound buffers */
 266#define BUFMAX 512
 267
 268/* Run-length encoding maximum length. Send 64 at most. */
 269#define RUNLENMAX 64
 270
 271/* The inbound/outbound buffers used in packet I/O */
 272static char remcomInBuffer[BUFMAX];
 273static char remcomOutBuffer[BUFMAX];
 274
 275/* Error and warning messages. */
 276enum error_type
 277{
 278        SUCCESS, E01, E02, E03, E04, E05, E06, E07, E08
 279};
 280static char *error_message[] =
 281{
 282        "",
 283        "E01 Set current or general thread - H[c,g] - internal error.",
 284        "E02 Change register content - P - cannot change read-only register.",
 285        "E03 Thread is not alive.", /* T, not used. */
 286        "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
 287        "E05 Change register content - P - the register is not implemented..",
 288        "E06 Change memory content - M - internal error.",
 289        "E07 Change register content - P - the register is not stored on the stack",
 290        "E08 Invalid parameter"
 291};
 292/********************************* Register image ****************************/
 293/* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
 294   Reference", p. 1-1, with the additional register definitions of the
 295   ETRAX 100LX in cris-opc.h.
 296   There are 16 general 32-bit registers, R0-R15, where R14 is the stack
 297   pointer, SP, and R15 is the program counter, PC.
 298   There are 16 special registers, P0-P15, where three of the unimplemented
 299   registers, P0, P4 and P8, are reserved as zero-registers. A read from
 300   any of these registers returns zero and a write has no effect. */
 301enum register_name
 302{
 303        R0,  R1,   R2,  R3,
 304        R4,  R5,   R6,  R7,
 305        R8,  R9,   R10, R11,
 306        R12, R13,  SP,  PC,
 307        P0,  VR,   P2,  P3,
 308        P4,  CCR,  P6,  MOF,
 309        P8,  IBR,  IRP, SRP,
 310        BAR, DCCR, BRP, USP
 311};
 312
 313/* The register sizes of the registers in register_name. An unimplemented register
 314   is designated by size 0 in this array. */
 315static int register_size[] =
 316{
 317        4, 4, 4, 4,
 318        4, 4, 4, 4,
 319        4, 4, 4, 4,
 320        4, 4, 4, 4,
 321        1, 1, 0, 0,
 322        2, 2, 0, 4,
 323        4, 4, 4, 4,
 324        4, 4, 4, 4
 325};
 326
 327/* Contains the register image of the executing thread in the assembler
 328   part of the code in order to avoid horrible addressing modes. */
 329registers cris_reg;
 330
 331/* FIXME: Should this be used? Delete otherwise. */
 332/* Contains the assumed consistency state of the register image. Uses the
 333   enum error_type for state information. */
 334static int consistency_status = SUCCESS;
 335
 336/********************************** Handle exceptions ************************/
 337/* The variable cris_reg contains the register image associated with the
 338   current_thread_c variable. It is a complete register image created at
 339   entry. The reg_g contains a register image of a task where the general
 340   registers are taken from the stack and all special registers are taken
 341   from the executing task. It is associated with current_thread_g and used
 342   in order to provide access mainly for 'g', 'G' and 'P'.
 343*/
 344
 345/********************************** Breakpoint *******************************/
 346/* Use an internal stack in the breakpoint and interrupt response routines */
 347#define INTERNAL_STACK_SIZE 1024
 348char internal_stack[INTERNAL_STACK_SIZE];
 349
 350/* Due to the breakpoint return pointer, a state variable is needed to keep
 351   track of whether it is a static (compiled) or dynamic (gdb-invoked)
 352   breakpoint to be handled. A static breakpoint uses the content of register
 353   BRP as it is whereas a dynamic breakpoint requires subtraction with 2
 354   in order to execute the instruction. The first breakpoint is static. */
 355static unsigned char __used is_dyn_brkp;
 356
 357/********************************* String library ****************************/
 358/* Single-step over library functions creates trap loops. */
 359
 360/* Copy char s2[] to s1[]. */
 361static char*
 362gdb_cris_strcpy (char *s1, const char *s2)
 363{
 364        char *s = s1;
 365        
 366        for (s = s1; (*s++ = *s2++) != '\0'; )
 367                ;
 368        return (s1);
 369}
 370
 371/* Find length of s[]. */
 372static int
 373gdb_cris_strlen (const char *s)
 374{
 375        const char *sc;
 376        
 377        for (sc = s; *sc != '\0'; sc++)
 378                ;
 379        return (sc - s);
 380}
 381
 382/* Find first occurrence of c in s[n]. */
 383static void*
 384gdb_cris_memchr (const void *s, int c, int n)
 385{
 386        const unsigned char uc = c;
 387        const unsigned char *su;
 388        
 389        for (su = s; 0 < n; ++su, --n)
 390                if (*su == uc)
 391                        return ((void *)su);
 392        return (NULL);
 393}
 394/******************************* Standard library ****************************/
 395/* Single-step over library functions creates trap loops. */
 396/* Convert string to long. */
 397static int
 398gdb_cris_strtol (const char *s, char **endptr, int base)
 399{
 400        char *s1;
 401        char *sd;
 402        int x = 0;
 403        
 404        for (s1 = (char*)s; (sd = gdb_cris_memchr(hex_asc, *s1, base)) != NULL; ++s1)
 405                x = x * base + (sd - hex_asc);
 406        
 407        if (endptr)
 408        {
 409                /* Unconverted suffix is stored in endptr unless endptr is NULL. */
 410                *endptr = s1;
 411        }
 412        
 413        return x;
 414}
 415
 416/********************************** Packet I/O ******************************/
 417
 418/* Convert the memory, pointed to by mem into hexadecimal representation.
 419   Put the result in buf, and return a pointer to the last character
 420   in buf (null). */
 421
 422static char *
 423mem2hex(char *buf, unsigned char *mem, int count)
 424{
 425        int i;
 426        int ch;
 427        
 428        if (mem == NULL) {
 429                /* Bogus read from m0. FIXME: What constitutes a valid address? */
 430                for (i = 0; i < count; i++) {
 431                        *buf++ = '0';
 432                        *buf++ = '0';
 433                }
 434        } else {
 435                /* Valid mem address. */
 436                for (i = 0; i < count; i++) {
 437                        ch = *mem++;
 438                        buf = hex_byte_pack(buf, ch);
 439                }
 440        }
 441        
 442        /* Terminate properly. */
 443        *buf = '\0';
 444        return (buf);
 445}
 446
 447/* Put the content of the array, in binary representation, pointed to by buf
 448   into memory pointed to by mem, and return a pointer to the character after
 449   the last byte written.
 450   Gdb will escape $, #, and the escape char (0x7d). */
 451static unsigned char*
 452bin2mem (unsigned char *mem, unsigned char *buf, int count)
 453{
 454        int i;
 455        unsigned char *next;
 456        for (i = 0; i < count; i++) {
 457                /* Check for any escaped characters. Be paranoid and
 458                   only unescape chars that should be escaped. */
 459                if (*buf == 0x7d) {
 460                        next = buf + 1;
 461                        if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */
 462                                {
 463                                        buf++;
 464                                        *buf += 0x20;
 465                                }
 466                }
 467                *mem++ = *buf++;
 468        }
 469        return (mem);
 470}
 471
 472/* Await the sequence $<data>#<checksum> and store <data> in the array buffer
 473   returned. */
 474static void
 475getpacket (char *buffer)
 476{
 477        unsigned char checksum;
 478        unsigned char xmitcsum;
 479        int i;
 480        int count;
 481        char ch;
 482        do {
 483                while ((ch = getDebugChar ()) != '$')
 484                        /* Wait for the start character $ and ignore all other characters */;
 485                checksum = 0;
 486                xmitcsum = -1;
 487                count = 0;
 488                /* Read until a # or the end of the buffer is reached */
 489                while (count < BUFMAX - 1) {
 490                        ch = getDebugChar ();
 491                        if (ch == '#')
 492                                break;
 493                        checksum = checksum + ch;
 494                        buffer[count] = ch;
 495                        count = count + 1;
 496                }
 497                buffer[count] = '\0';
 498                
 499                if (ch == '#') {
 500                        xmitcsum = hex_to_bin(getDebugChar()) << 4;
 501                        xmitcsum += hex_to_bin(getDebugChar());
 502                        if (checksum != xmitcsum) {
 503                                /* Wrong checksum */
 504                                putDebugChar ('-');
 505                        }
 506                        else {
 507                                /* Correct checksum */
 508                                putDebugChar ('+');
 509                                /* If sequence characters are received, reply with them */
 510                                if (buffer[2] == ':') {
 511                                        putDebugChar (buffer[0]);
 512                                        putDebugChar (buffer[1]);
 513                                        /* Remove the sequence characters from the buffer */
 514                                        count = gdb_cris_strlen (buffer);
 515                                        for (i = 3; i <= count; i++)
 516                                                buffer[i - 3] = buffer[i];
 517                                }
 518                        }
 519                }
 520        } while (checksum != xmitcsum);
 521}
 522
 523/* Send $<data>#<checksum> from the <data> in the array buffer. */
 524
 525static void
 526putpacket(char *buffer)
 527{
 528        int checksum;
 529        int runlen;
 530        int encode;
 531        
 532        do {
 533                char *src = buffer;
 534                putDebugChar ('$');
 535                checksum = 0;
 536                while (*src) {
 537                        /* Do run length encoding */
 538                        putDebugChar (*src);
 539                        checksum += *src;
 540                        runlen = 0;
 541                        while (runlen < RUNLENMAX && *src == src[runlen]) {
 542                                runlen++;
 543                        }
 544                        if (runlen > 3) {
 545                                /* Got a useful amount */
 546                                putDebugChar ('*');
 547                                checksum += '*';
 548                                encode = runlen + ' ' - 4;
 549                                putDebugChar (encode);
 550                                checksum += encode;
 551                                src += runlen;
 552                        }
 553                        else {
 554                                src++;
 555                        }
 556                }
 557                putDebugChar('#');
 558                putDebugChar(hex_asc_hi(checksum));
 559                putDebugChar(hex_asc_lo(checksum));
 560        } while(kgdb_started && (getDebugChar() != '+'));
 561}
 562
 563/* The string str is prepended with the GDB printout token and sent. Required
 564   in traditional implementations. */
 565void
 566putDebugString (const unsigned char *str, int length)
 567{
 568        remcomOutBuffer[0] = 'O';
 569        mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);
 570        putpacket(remcomOutBuffer);
 571}
 572
 573/********************************* Register image ****************************/
 574/* Write a value to a specified register in the register image of the current
 575   thread. Returns status code SUCCESS, E02, E05 or E08. */
 576static int
 577write_register (int regno, char *val)
 578{
 579        int status = SUCCESS;
 580        registers *current_reg = &cris_reg;
 581
 582        if (regno >= R0 && regno <= PC) {
 583                /* 32-bit register with simple offset. */
 584                if (hex2bin((unsigned char *)current_reg + regno * sizeof(unsigned int),
 585                            val, sizeof(unsigned int)))
 586                        status = E08;
 587        }
 588        else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {
 589                /* Do not support read-only registers. */
 590                status = E02;
 591        }
 592        else if (regno == CCR) {
 593                /* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented, 
 594                   and P7 (MOF) is 32 bits in ETRAX 100LX. */
 595                if (hex2bin((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),
 596                            val, sizeof(unsigned short)))
 597                        status = E08;
 598        }
 599        else if (regno >= MOF && regno <= USP) {
 600                /* 32 bit register with complex offset.  (P8 has been taken care of.) */
 601                if (hex2bin((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),
 602                            val, sizeof(unsigned int)))
 603                        status = E08;
 604        } 
 605        else {
 606                /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
 607                status = E05;
 608        }
 609        return status;
 610}
 611
 612/* Read a value from a specified register in the register image. Returns the
 613   value in the register or -1 for non-implemented registers.
 614   Should check consistency_status after a call which may be E05 after changes
 615   in the implementation. */
 616static int
 617read_register (char regno, unsigned int *valptr)
 618{
 619        registers *current_reg = &cris_reg;
 620
 621        if (regno >= R0 && regno <= PC) {
 622                /* 32-bit register with simple offset. */
 623                *valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));
 624                return SUCCESS;
 625        }
 626        else if (regno == P0 || regno == VR) {
 627                /* 8 bit register with complex offset. */
 628                *valptr = (unsigned int)(*(unsigned char *)
 629                                         ((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));
 630                return SUCCESS;
 631        }
 632        else if (regno == P4 || regno == CCR) {
 633                /* 16 bit register with complex offset. */
 634                *valptr = (unsigned int)(*(unsigned short *)
 635                                         ((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));
 636                return SUCCESS;
 637        }
 638        else if (regno >= MOF && regno <= USP) {
 639                /* 32 bit register with complex offset. */
 640                *valptr = *(unsigned int *)((char *)&(current_reg->p8)
 641                                            + (regno-P8) * sizeof(unsigned int));
 642                return SUCCESS;
 643        }
 644        else {
 645                /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
 646                consistency_status = E05;
 647                return E05;
 648        }
 649}
 650
 651/********************************** Handle exceptions ************************/
 652/* Build and send a response packet in order to inform the host the
 653   stub is stopped. TAAn...:r...;n...:r...;n...:r...;
 654                    AA = signal number
 655                    n... = register number (hex)
 656                    r... = register contents
 657                    n... = `thread'
 658                    r... = thread process ID.  This is a hex integer.
 659                    n... = other string not starting with valid hex digit.
 660                    gdb should ignore this n,r pair and go on to the next.
 661                    This way we can extend the protocol. */
 662static void
 663stub_is_stopped(int sigval)
 664{
 665        char *ptr = remcomOutBuffer;
 666        int regno;
 667
 668        unsigned int reg_cont;
 669        int status;
 670        
 671        /* Send trap type (converted to signal) */
 672
 673        *ptr++ = 'T';
 674        ptr = hex_byte_pack(ptr, sigval);
 675
 676        /* Send register contents. We probably only need to send the
 677         * PC, frame pointer and stack pointer here. Other registers will be
 678         * explicitly asked for. But for now, send all.
 679         */
 680        
 681        for (regno = R0; regno <= USP; regno++) {
 682                /* Store n...:r...; for the registers in the buffer. */
 683
 684                status = read_register (regno, &reg_cont);
 685                
 686                if (status == SUCCESS) {
 687                        ptr = hex_byte_pack(ptr, regno);
 688                        *ptr++ = ':';
 689
 690                        ptr = mem2hex(ptr, (unsigned char *)&reg_cont,
 691                                      register_size[regno]);
 692                        *ptr++ = ';';
 693                }
 694                
 695        }
 696
 697        /* null-terminate and send it off */
 698
 699        *ptr = 0;
 700
 701        putpacket (remcomOutBuffer);
 702}
 703
 704/* Performs a complete re-start from scratch. */
 705static void
 706kill_restart (void)
 707{
 708        machine_restart("");
 709}
 710
 711/* All expected commands are sent from remote.c. Send a response according
 712   to the description in remote.c. */
 713void
 714handle_exception (int sigval)
 715{
 716        /* Send response. */
 717
 718        stub_is_stopped (sigval);
 719
 720        for (;;) {
 721                remcomOutBuffer[0] = '\0';
 722                getpacket (remcomInBuffer);
 723                switch (remcomInBuffer[0]) {
 724                        case 'g':
 725                                /* Read registers: g
 726                                   Success: Each byte of register data is described by two hex digits.
 727                                   Registers are in the internal order for GDB, and the bytes
 728                                   in a register  are in the same order the machine uses.
 729                                   Failure: void. */
 730                                
 731                                mem2hex(remcomOutBuffer, (char *)&cris_reg, sizeof(registers));
 732                                break;
 733                                
 734                        case 'G':
 735                                /* Write registers. GXX..XX
 736                                   Each byte of register data  is described by two hex digits.
 737                                   Success: OK
 738                                   Failure: E08. */
 739                                if (hex2bin((char *)&cris_reg, &remcomInBuffer[1], sizeof(registers)))
 740                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E08]);
 741                                else
 742                                        gdb_cris_strcpy (remcomOutBuffer, "OK");
 743                                break;
 744                                
 745                        case 'P':
 746                                /* Write register. Pn...=r...
 747                                   Write register n..., hex value without 0x, with value r...,
 748                                   which contains a hex value without 0x and two hex digits
 749                                   for each byte in the register (target byte order). P1f=11223344 means
 750                                   set register 31 to 44332211.
 751                                   Success: OK
 752                                   Failure: E02, E05, E08 */
 753                                {
 754                                        char *suffix;
 755                                        int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);
 756                                        int status;
 757                                        status = write_register (regno, suffix+1);
 758
 759                                        switch (status) {
 760                                                case E02:
 761                                                        /* Do not support read-only registers. */
 762                                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);
 763                                                        break;
 764                                                case E05:
 765                                                        /* Do not support non-existing registers. */
 766                                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);
 767                                                        break;
 768                                                case E07:
 769                                                        /* Do not support non-existing registers on the stack. */
 770                                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);
 771                                                        break;
 772                                                case E08:
 773                                                        /* Invalid parameter. */
 774                                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E08]);
 775                                                        break;
 776                                                default:
 777                                                        /* Valid register number. */
 778                                                        gdb_cris_strcpy (remcomOutBuffer, "OK");
 779                                                        break;
 780                                        }
 781                                }
 782                                break;
 783                                
 784                        case 'm':
 785                                /* Read from memory. mAA..AA,LLLL
 786                                   AA..AA is the address and LLLL is the length.
 787                                   Success: XX..XX is the memory content.  Can be fewer bytes than
 788                                   requested if only part of the data may be read. m6000120a,6c means
 789                                   retrieve 108 byte from base address 6000120a.
 790                                   Failure: void. */
 791                                {
 792                                        char *suffix;
 793                                        unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
 794                                                                                               &suffix, 16);                                        int length = gdb_cris_strtol(suffix+1, 0, 16);
 795                                        
 796                                        mem2hex(remcomOutBuffer, addr, length);
 797                                }
 798                                break;
 799                                
 800                        case 'X':
 801                                /* Write to memory. XAA..AA,LLLL:XX..XX
 802                                   AA..AA is the start address,  LLLL is the number of bytes, and
 803                                   XX..XX is the binary data.
 804                                   Success: OK
 805                                   Failure: void. */
 806                        case 'M':
 807                                /* Write to memory. MAA..AA,LLLL:XX..XX
 808                                   AA..AA is the start address,  LLLL is the number of bytes, and
 809                                   XX..XX is the hexadecimal data.
 810                                   Success: OK
 811                                   Failure: E08. */
 812                                {
 813                                        char *lenptr;
 814                                        char *dataptr;
 815                                        unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
 816                                                                                      &lenptr, 16);
 817                                        int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);
 818                                        if (*lenptr == ',' && *dataptr == ':') {
 819                                                if (remcomInBuffer[0] == 'M') {
 820                                                        if (hex2bin(addr, dataptr + 1, length))
 821                                                                gdb_cris_strcpy (remcomOutBuffer, error_message[E08]);
 822                                                        else
 823                                                                gdb_cris_strcpy (remcomOutBuffer, "OK");
 824                                                } else /* X */ {
 825                                                        bin2mem(addr, dataptr + 1, length);
 826                                                        gdb_cris_strcpy (remcomOutBuffer, "OK");
 827                                                }
 828                                        } else {
 829                                                gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);
 830                                        }
 831                                }
 832                                break;
 833                                
 834                        case 'c':
 835                                /* Continue execution. cAA..AA
 836                                   AA..AA is the address where execution is resumed. If AA..AA is
 837                                   omitted, resume at the present address.
 838                                   Success: return to the executing thread.
 839                                   Failure: will never know. */
 840                                if (remcomInBuffer[1] != '\0') {
 841                                        cris_reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
 842                                }
 843                                enableDebugIRQ();
 844                                return;
 845                                
 846                        case 's':
 847                                /* Step. sAA..AA
 848                                   AA..AA is the address where execution is resumed. If AA..AA is
 849                                   omitted, resume at the present address. Success: return to the
 850                                   executing thread. Failure: will never know.
 851                                   
 852                                   Should never be invoked. The single-step is implemented on
 853                                   the host side. If ever invoked, it is an internal error E04. */
 854                                gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
 855                                putpacket (remcomOutBuffer);
 856                                return;
 857                                
 858                        case '?':
 859                                /* The last signal which caused a stop. ?
 860                                   Success: SAA, where AA is the signal number.
 861                                   Failure: void. */
 862                                remcomOutBuffer[0] = 'S';
 863                                remcomOutBuffer[1] = hex_asc_hi(sigval);
 864                                remcomOutBuffer[2] = hex_asc_lo(sigval);
 865                                remcomOutBuffer[3] = 0;
 866                                break;
 867                                
 868                        case 'D':
 869                                /* Detach from host. D
 870                                   Success: OK, and return to the executing thread.
 871                                   Failure: will never know */
 872                                putpacket ("OK");
 873                                return;
 874                                
 875                        case 'k':
 876                        case 'r':
 877                                /* kill request or reset request.
 878                                   Success: restart of target.
 879                                   Failure: will never know. */
 880                                kill_restart ();
 881                                break;
 882                                
 883                        case 'C':
 884                        case 'S':
 885                        case '!':
 886                        case 'R':
 887                        case 'd':
 888                                /* Continue with signal sig. Csig;AA..AA
 889                                   Step with signal sig. Ssig;AA..AA
 890                                   Use the extended remote protocol. !
 891                                   Restart the target system. R0
 892                                   Toggle debug flag. d
 893                                   Search backwards. tAA:PP,MM
 894                                   Not supported: E04 */
 895                                gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
 896                                break;
 897                                
 898                        default:
 899                                /* The stub should ignore other request and send an empty
 900                                   response ($#<checksum>). This way we can extend the protocol and GDB
 901                                   can tell whether the stub it is talking to uses the old or the new. */
 902                                remcomOutBuffer[0] = 0;
 903                                break;
 904                }
 905                putpacket(remcomOutBuffer);
 906        }
 907}
 908
 909/********************************** Breakpoint *******************************/
 910/* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.
 911   An internal stack is used by the stub. The register image of the caller is
 912   stored in the structure register_image.
 913   Interactive communication with the host is handled by handle_exception and
 914   finally the register image is restored. */
 915
 916void kgdb_handle_breakpoint(void);
 917
 918asm ("\n"
 919"  .global kgdb_handle_breakpoint\n"
 920"kgdb_handle_breakpoint:\n"
 921";;\n"
 922";; Response to the break-instruction\n"
 923";;\n"
 924";; Create a register image of the caller\n"
 925";;\n"
 926"  move     $dccr,[cris_reg+0x5E] ; Save the flags in DCCR before disable interrupts\n"
 927"  di                        ; Disable interrupts\n"
 928"  move.d   $r0,[cris_reg]        ; Save R0\n"
 929"  move.d   $r1,[cris_reg+0x04]   ; Save R1\n"
 930"  move.d   $r2,[cris_reg+0x08]   ; Save R2\n"
 931"  move.d   $r3,[cris_reg+0x0C]   ; Save R3\n"
 932"  move.d   $r4,[cris_reg+0x10]   ; Save R4\n"
 933"  move.d   $r5,[cris_reg+0x14]   ; Save R5\n"
 934"  move.d   $r6,[cris_reg+0x18]   ; Save R6\n"
 935"  move.d   $r7,[cris_reg+0x1C]   ; Save R7\n"
 936"  move.d   $r8,[cris_reg+0x20]   ; Save R8\n"
 937"  move.d   $r9,[cris_reg+0x24]   ; Save R9\n"
 938"  move.d   $r10,[cris_reg+0x28]  ; Save R10\n"
 939"  move.d   $r11,[cris_reg+0x2C]  ; Save R11\n"
 940"  move.d   $r12,[cris_reg+0x30]  ; Save R12\n"
 941"  move.d   $r13,[cris_reg+0x34]  ; Save R13\n"
 942"  move.d   $sp,[cris_reg+0x38]   ; Save SP (R14)\n"
 943";; Due to the old assembler-versions BRP might not be recognized\n"
 944"  .word 0xE670              ; move brp,$r0\n"
 945"  subq     2,$r0             ; Set to address of previous instruction.\n"
 946"  move.d   $r0,[cris_reg+0x3c]   ; Save the address in PC (R15)\n"
 947"  clear.b  [cris_reg+0x40]      ; Clear P0\n"
 948"  move     $vr,[cris_reg+0x41]   ; Save special register P1\n"
 949"  clear.w  [cris_reg+0x42]      ; Clear P4\n"
 950"  move     $ccr,[cris_reg+0x44]  ; Save special register CCR\n"
 951"  move     $mof,[cris_reg+0x46]  ; P7\n"
 952"  clear.d  [cris_reg+0x4A]      ; Clear P8\n"
 953"  move     $ibr,[cris_reg+0x4E]  ; P9,\n"
 954"  move     $irp,[cris_reg+0x52]  ; P10,\n"
 955"  move     $srp,[cris_reg+0x56]  ; P11,\n"
 956"  move     $bar,[cris_reg+0x5A]  ; P12,\n"
 957"                            ; P13, register DCCR already saved\n"
 958";; Due to the old assembler-versions BRP might not be recognized\n"
 959"  .word 0xE670              ; move brp,r0\n"
 960";; Static (compiled) breakpoints must return to the next instruction in order\n"
 961";; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction\n"
 962";; in order to execute it when execution is continued.\n"
 963"  test.b   [is_dyn_brkp]    ; Is this a dynamic breakpoint?\n"
 964"  beq      is_static         ; No, a static breakpoint\n"
 965"  nop\n"
 966"  subq     2,$r0              ; rerun the instruction the break replaced\n"
 967"is_static:\n"
 968"  moveq    1,$r1\n"
 969"  move.b   $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint\n"
 970"  move.d   $r0,[cris_reg+0x62]    ; Save the return address in BRP\n"
 971"  move     $usp,[cris_reg+0x66]   ; USP\n"
 972";;\n"
 973";; Handle the communication\n"
 974";;\n"
 975"  move.d   internal_stack+1020,$sp ; Use the internal stack which grows upward\n"
 976"  moveq    5,$r10                   ; SIGTRAP\n"
 977"  jsr      handle_exception       ; Interactive routine\n"
 978";;\n"
 979";; Return to the caller\n"
 980";;\n"
 981"   move.d  [cris_reg],$r0         ; Restore R0\n"
 982"   move.d  [cris_reg+0x04],$r1    ; Restore R1\n"
 983"   move.d  [cris_reg+0x08],$r2    ; Restore R2\n"
 984"   move.d  [cris_reg+0x0C],$r3    ; Restore R3\n"
 985"   move.d  [cris_reg+0x10],$r4    ; Restore R4\n"
 986"   move.d  [cris_reg+0x14],$r5    ; Restore R5\n"
 987"   move.d  [cris_reg+0x18],$r6    ; Restore R6\n"
 988"   move.d  [cris_reg+0x1C],$r7    ; Restore R7\n"
 989"   move.d  [cris_reg+0x20],$r8    ; Restore R8\n"
 990"   move.d  [cris_reg+0x24],$r9    ; Restore R9\n"
 991"   move.d  [cris_reg+0x28],$r10   ; Restore R10\n"
 992"   move.d  [cris_reg+0x2C],$r11   ; Restore R11\n"
 993"   move.d  [cris_reg+0x30],$r12   ; Restore R12\n"
 994"   move.d  [cris_reg+0x34],$r13   ; Restore R13\n"
 995";;\n"
 996";; FIXME: Which registers should be restored?\n"
 997";;\n"
 998"   move.d  [cris_reg+0x38],$sp    ; Restore SP (R14)\n"
 999"   move    [cris_reg+0x56],$srp   ; Restore the subroutine return pointer.\n"
1000"   move    [cris_reg+0x5E],$dccr  ; Restore DCCR\n"
1001"   move    [cris_reg+0x66],$usp   ; Restore USP\n"
1002"   jump    [cris_reg+0x62]       ; A jump to the content in register BRP works.\n"
1003"   nop                       ;\n"
1004"\n");
1005
1006/* The hook for an interrupt generated by GDB. An internal stack is used
1007   by the stub. The register image of the caller is stored in the structure
1008   register_image. Interactive communication with the host is handled by
1009   handle_exception and finally the register image is restored. Due to the
1010   old assembler which does not recognise the break instruction and the
1011   breakpoint return pointer hex-code is used. */
1012
1013void kgdb_handle_serial(void);
1014
1015asm ("\n"
1016"  .global kgdb_handle_serial\n"
1017"kgdb_handle_serial:\n"
1018";;\n"
1019";; Response to a serial interrupt\n"
1020";;\n"
1021"\n"
1022"  move     $dccr,[cris_reg+0x5E] ; Save the flags in DCCR\n"
1023"  di                        ; Disable interrupts\n"
1024"  move.d   $r0,[cris_reg]        ; Save R0\n"
1025"  move.d   $r1,[cris_reg+0x04]   ; Save R1\n"
1026"  move.d   $r2,[cris_reg+0x08]   ; Save R2\n"
1027"  move.d   $r3,[cris_reg+0x0C]   ; Save R3\n"
1028"  move.d   $r4,[cris_reg+0x10]   ; Save R4\n"
1029"  move.d   $r5,[cris_reg+0x14]   ; Save R5\n"
1030"  move.d   $r6,[cris_reg+0x18]   ; Save R6\n"
1031"  move.d   $r7,[cris_reg+0x1C]   ; Save R7\n"
1032"  move.d   $r8,[cris_reg+0x20]   ; Save R8\n"
1033"  move.d   $r9,[cris_reg+0x24]   ; Save R9\n"
1034"  move.d   $r10,[cris_reg+0x28]  ; Save R10\n"
1035"  move.d   $r11,[cris_reg+0x2C]  ; Save R11\n"
1036"  move.d   $r12,[cris_reg+0x30]  ; Save R12\n"
1037"  move.d   $r13,[cris_reg+0x34]  ; Save R13\n"
1038"  move.d   $sp,[cris_reg+0x38]   ; Save SP (R14)\n"
1039"  move     $irp,[cris_reg+0x3c]  ; Save the address in PC (R15)\n"
1040"  clear.b  [cris_reg+0x40]      ; Clear P0\n"
1041"  move     $vr,[cris_reg+0x41]   ; Save special register P1,\n"
1042"  clear.w  [cris_reg+0x42]      ; Clear P4\n"
1043"  move     $ccr,[cris_reg+0x44]  ; Save special register CCR\n"
1044"  move     $mof,[cris_reg+0x46]  ; P7\n"
1045"  clear.d  [cris_reg+0x4A]      ; Clear P8\n"
1046"  move     $ibr,[cris_reg+0x4E]  ; P9,\n"
1047"  move     $irp,[cris_reg+0x52]  ; P10,\n"
1048"  move     $srp,[cris_reg+0x56]  ; P11,\n"
1049"  move     $bar,[cris_reg+0x5A]  ; P12,\n"
1050"                            ; P13, register DCCR already saved\n"
1051";; Due to the old assembler-versions BRP might not be recognized\n"
1052"  .word 0xE670              ; move brp,r0\n"
1053"  move.d   $r0,[cris_reg+0x62]   ; Save the return address in BRP\n"
1054"  move     $usp,[cris_reg+0x66]  ; USP\n"
1055"\n"
1056";; get the serial character (from debugport.c) and check if it is a ctrl-c\n"
1057"\n"
1058"  jsr getDebugChar\n"
1059"  cmp.b 3, $r10\n"
1060"  bne goback\n"
1061"  nop\n"
1062"\n"
1063"  move.d  [cris_reg+0x5E], $r10                ; Get DCCR\n"
1064"  btstq           8, $r10                      ; Test the U-flag.\n"
1065"  bmi     goback\n"
1066"  nop\n"
1067"\n"
1068";;\n"
1069";; Handle the communication\n"
1070";;\n"
1071"  move.d   internal_stack+1020,$sp ; Use the internal stack\n"
1072"  moveq    2,$r10                   ; SIGINT\n"
1073"  jsr      handle_exception       ; Interactive routine\n"
1074"\n"
1075"goback:\n"
1076";;\n"
1077";; Return to the caller\n"
1078";;\n"
1079"   move.d  [cris_reg],$r0         ; Restore R0\n"
1080"   move.d  [cris_reg+0x04],$r1    ; Restore R1\n"
1081"   move.d  [cris_reg+0x08],$r2    ; Restore R2\n"
1082"   move.d  [cris_reg+0x0C],$r3    ; Restore R3\n"
1083"   move.d  [cris_reg+0x10],$r4    ; Restore R4\n"
1084"   move.d  [cris_reg+0x14],$r5    ; Restore R5\n"
1085"   move.d  [cris_reg+0x18],$r6    ; Restore R6\n"
1086"   move.d  [cris_reg+0x1C],$r7    ; Restore R7\n"
1087"   move.d  [cris_reg+0x20],$r8    ; Restore R8\n"
1088"   move.d  [cris_reg+0x24],$r9    ; Restore R9\n"
1089"   move.d  [cris_reg+0x28],$r10   ; Restore R10\n"
1090"   move.d  [cris_reg+0x2C],$r11   ; Restore R11\n"
1091"   move.d  [cris_reg+0x30],$r12   ; Restore R12\n"
1092"   move.d  [cris_reg+0x34],$r13   ; Restore R13\n"
1093";;\n"
1094";; FIXME: Which registers should be restored?\n"
1095";;\n"
1096"   move.d  [cris_reg+0x38],$sp    ; Restore SP (R14)\n"
1097"   move    [cris_reg+0x56],$srp   ; Restore the subroutine return pointer.\n"
1098"   move    [cris_reg+0x5E],$dccr  ; Restore DCCR\n"
1099"   move    [cris_reg+0x66],$usp   ; Restore USP\n"
1100"   reti                      ; Return from the interrupt routine\n"
1101"   nop\n"
1102"\n");
1103
1104/* Use this static breakpoint in the start-up only. */
1105
1106void
1107breakpoint(void)
1108{
1109        kgdb_started = 1;
1110        is_dyn_brkp = 0;     /* This is a static, not a dynamic breakpoint. */
1111        __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */
1112}
1113
1114/* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */
1115
1116void
1117kgdb_init(void)
1118{
1119        /* could initialize debug port as well but it's done in head.S already... */
1120
1121        /* breakpoint handler is now set in irq.c */
1122        set_int_vector(8, kgdb_handle_serial);
1123        
1124        enableDebugIRQ();
1125}
1126
1127/****************************** End of file **********************************/
1128