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