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*! $Log: kgdb.c,v $
  21*! Revision 1.5  2004/10/07 13:59:08  starvik
  22*! Corrected call to set_int_vector
  23*!
  24*! Revision 1.4  2003/04/09 05:20:44  starvik
  25*! Merge of Linux 2.5.67
  26*!
  27*! Revision 1.3  2003/01/21 19:11:08  starvik
  28*! Modified include path for new dir layout
  29*!
  30*! Revision 1.2  2002/11/19 14:35:24  starvik
  31*! Changes from linux 2.4
  32*! Changed struct initializer syntax to the currently prefered notation
  33*!
  34*! Revision 1.1  2001/12/17 13:59:27  bjornw
  35*! Initial revision
  36*!
  37*! Revision 1.6  2001/10/09 13:10:03  matsfg
  38*! Added $ on registers and removed some underscores
  39*!
  40*! Revision 1.5  2001/04/17 13:58:39  orjanf
  41*! * Renamed CONFIG_KGDB to CONFIG_ETRAX_KGDB.
  42*!
  43*! Revision 1.4  2001/02/23 13:45:19  bjornw
  44*! config.h check
  45*!
  46*! Revision 1.3  2001/01/31 18:08:23  orjanf
  47*! Removed kgdb_handle_breakpoint from being the break 8 handler.
  48*!
  49*! Revision 1.2  2001/01/12 14:22:25  orjanf
  50*! Updated kernel debugging support to work with ETRAX 100LX.
  51*!
  52*! Revision 1.1  2000/07/10 16:25:21  bjornw
  53*! Initial revision
  54*!
  55*! Revision 1.1.1.1  1999/12/03 14:57:31  bjornw
  56*! * Initial version of arch/cris, the latest CRIS architecture with an MMU.
  57*!   Mostly copied from arch/etrax100 with appropriate renames of files.
  58*!   The mm/ subdir is copied from arch/i386.
  59*!   This does not compile yet at all.
  60*!
  61*!
  62*! Revision 1.4  1999/07/22 17:25:25  bjornw
  63*! Dont wait for + in putpacket if we havent hit the initial breakpoint yet. Added a kgdb_init function which sets up the break and irq vectors.
  64*!
  65*! Revision 1.3  1999/07/21 19:51:18  bjornw
  66*! Check if the interrupting char is a ctrl-C, ignore otherwise.
  67*!
  68*! Revision 1.2  1999/07/21 18:09:39  bjornw
  69*! Ported to eLinux architecture, and added some kgdb documentation.
  70*!
  71*!
  72*!---------------------------------------------------------------------------
  73*!
  74*! $Id: kgdb.c,v 1.5 2004/10/07 13:59:08 starvik Exp $
  75*!
  76*! (C) Copyright 1999, Axis Communications AB, LUND, SWEDEN
  77*!
  78*!**************************************************************************/
  79/* @(#) cris_stub.c 1.3 06/17/99 */
  80
  81/*
  82 *  kgdb usage notes:
  83 *  -----------------
  84 *
  85 * If you select CONFIG_ETRAX_KGDB in the configuration, the kernel will be 
  86 * built with different gcc flags: "-g" is added to get debug infos, and
  87 * "-fomit-frame-pointer" is omitted to make debugging easier. Since the
  88 * resulting kernel will be quite big (approx. > 7 MB), it will be stripped
  89 * before compresion. Such a kernel will behave just as usually, except if
  90 * given a "debug=<device>" command line option. (Only serial devices are
  91 * allowed for <device>, i.e. no printers or the like; possible values are
  92 * machine depedend and are the same as for the usual debug device, the one
  93 * for logging kernel messages.) If that option is given and the device can be
  94 * initialized, the kernel will connect to the remote gdb in trap_init(). The
  95 * serial parameters are fixed to 8N1 and 115200 bps, for easyness of
  96 * implementation.
  97 *
  98 * To start a debugging session, start that gdb with the debugging kernel
  99 * image (the one with the symbols, vmlinux.debug) named on the command line.
 100 * This file will be used by gdb to get symbol and debugging infos about the
 101 * kernel. Next, select remote debug mode by
 102 *    target remote <device>
 103 * where <device> is the name of the serial device over which the debugged
 104 * machine is connected. Maybe you have to adjust the baud rate by
 105 *    set remotebaud <rate>
 106 * or also other parameters with stty:
 107 *    shell stty ... </dev/...
 108 * If the kernel to debug has already booted, it waited for gdb and now
 109 * connects, and you'll see a breakpoint being reported. If the kernel isn't
 110 * running yet, start it now. The order of gdb and the kernel doesn't matter.
 111 * Another thing worth knowing about in the getting-started phase is how to
 112 * debug the remote protocol itself. This is activated with
 113 *    set remotedebug 1
 114 * gdb will then print out each packet sent or received. You'll also get some
 115 * messages about the gdb stub on the console of the debugged machine.
 116 *
 117 * If all that works, you can use lots of the usual debugging techniques on
 118 * the kernel, e.g. inspecting and changing variables/memory, setting
 119 * breakpoints, single stepping and so on. It's also possible to interrupt the
 120 * debugged kernel by pressing C-c in gdb. Have fun! :-)
 121 *
 122 * The gdb stub is entered (and thus the remote gdb gets control) in the
 123 * following situations:
 124 *
 125 *  - If breakpoint() is called. This is just after kgdb initialization, or if
 126 *    a breakpoint() call has been put somewhere into the kernel source.
 127 *    (Breakpoints can of course also be set the usual way in gdb.)
 128 *    In eLinux, we call breakpoint() in init/main.c after IRQ initialization.
 129 *
 130 *  - If there is a kernel exception, i.e. bad_super_trap() or die_if_kernel()
 131 *    are entered. All the CPU exceptions are mapped to (more or less..., see
 132 *    the hard_trap_info array below) appropriate signal, which are reported
 133 *    to gdb. die_if_kernel() is usually called after some kind of access
 134 *    error and thus is reported as SIGSEGV.
 135 *
 136 *  - When panic() is called. This is reported as SIGABRT.
 137 *
 138 *  - If C-c is received over the serial line, which is treated as
 139 *    SIGINT.
 140 *
 141 * Of course, all these signals are just faked for gdb, since there is no
 142 * signal concept as such for the kernel. It also isn't possible --obviously--
 143 * to set signal handlers from inside gdb, or restart the kernel with a
 144 * signal.
 145 *
 146 * Current limitations:
 147 *
 148 *  - While the kernel is stopped, interrupts are disabled for safety reasons
 149 *    (i.e., variables not changing magically or the like). But this also
 150 *    means that the clock isn't running anymore, and that interrupts from the
 151 *    hardware may get lost/not be served in time. This can cause some device
 152 *    errors...
 153 *
 154 *  - When single-stepping, only one instruction of the current thread is
 155 *    executed, but interrupts are allowed for that time and will be serviced
 156 *    if pending. Be prepared for that.
 157 *
 158 *  - All debugging happens in kernel virtual address space. There's no way to
 159 *    access physical memory not mapped in kernel space, or to access user
 160 *    space. A way to work around this is using get_user_long & Co. in gdb
 161 *    expressions, but only for the current process.
 162 *
 163 *  - Interrupting the kernel only works if interrupts are currently allowed,
 164 *    and the interrupt of the serial line isn't blocked by some other means
 165 *    (IPL too high, disabled, ...)
 166 *
 167 *  - The gdb stub is currently not reentrant, i.e. errors that happen therein
 168 *    (e.g. accessing invalid memory) may not be caught correctly. This could
 169 *    be removed in future by introducing a stack of struct registers.
 170 *
 171 */
 172
 173/*
 174 *  To enable debugger support, two things need to happen.  One, a
 175 *  call to kgdb_init() is necessary in order to allow any breakpoints
 176 *  or error conditions to be properly intercepted and reported to gdb.
 177 *  Two, a breakpoint needs to be generated to begin communication.  This
 178 *  is most easily accomplished by a call to breakpoint(). 
 179 *
 180 *    The following gdb commands are supported:
 181 *
 182 * command          function                               Return value
 183 *
 184 *    g             return the value of the CPU registers  hex data or ENN
 185 *    G             set the value of the CPU registers     OK or ENN
 186 *
 187 *    mAA..AA,LLLL  Read LLLL bytes at address AA..AA      hex data or ENN
 188 *    MAA..AA,LLLL: Write LLLL bytes at address AA.AA      OK or ENN
 189 *
 190 *    c             Resume at current address              SNN   ( signal NN)
 191 *    cAA..AA       Continue at address AA..AA             SNN
 192 *
 193 *    s             Step one instruction                   SNN
 194 *    sAA..AA       Step one instruction from AA..AA       SNN
 195 *
 196 *    k             kill
 197 *
 198 *    ?             What was the last sigval ?             SNN   (signal NN)
 199 *
 200 *    bBB..BB       Set baud rate to BB..BB                OK or BNN, then sets
 201 *                                                         baud rate
 202 *
 203 * All commands and responses are sent with a packet which includes a
 204 * checksum.  A packet consists of
 205 *
 206 * $<packet info>#<checksum>.
 207 *
 208 * where
 209 * <packet info> :: <characters representing the command or response>
 210 * <checksum>    :: < two hex digits computed as modulo 256 sum of <packetinfo>>
 211 *
 212 * When a packet is received, it is first acknowledged with either '+' or '-'.
 213 * '+' indicates a successful transfer.  '-' indicates a failed transfer.
 214 *
 215 * Example:
 216 *
 217 * Host:                  Reply:
 218 * $m0,10#2a               +$00010203040506070809101112131415#42
 219 *
 220 */
 221
 222
 223#include <linux/string.h>
 224#include <linux/signal.h>
 225#include <linux/kernel.h>
 226#include <linux/delay.h>
 227#include <linux/linkage.h>
 228
 229#include <asm/setup.h>
 230#include <asm/ptrace.h>
 231
 232#include <asm/arch/svinto.h>
 233#include <asm/irq.h>
 234
 235static int kgdb_started = 0;
 236
 237/********************************* Register image ****************************/
 238/* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
 239   Reference", p. 1-1, with the additional register definitions of the
 240   ETRAX 100LX in cris-opc.h.
 241   There are 16 general 32-bit registers, R0-R15, where R14 is the stack
 242   pointer, SP, and R15 is the program counter, PC.
 243   There are 16 special registers, P0-P15, where three of the unimplemented
 244   registers, P0, P4 and P8, are reserved as zero-registers. A read from
 245   any of these registers returns zero and a write has no effect. */
 246
 247typedef
 248struct register_image
 249{
 250        /* Offset */
 251        unsigned int     r0;   /* 0x00 */
 252        unsigned int     r1;   /* 0x04 */
 253        unsigned int     r2;   /* 0x08 */
 254        unsigned int     r3;   /* 0x0C */
 255        unsigned int     r4;   /* 0x10 */
 256        unsigned int     r5;   /* 0x14 */
 257        unsigned int     r6;   /* 0x18 */
 258        unsigned int     r7;   /* 0x1C */
 259        unsigned int     r8;   /* 0x20 Frame pointer */
 260        unsigned int     r9;   /* 0x24 */
 261        unsigned int    r10;   /* 0x28 */
 262        unsigned int    r11;   /* 0x2C */
 263        unsigned int    r12;   /* 0x30 */
 264        unsigned int    r13;   /* 0x34 */
 265        unsigned int     sp;   /* 0x38 Stack pointer */
 266        unsigned int     pc;   /* 0x3C Program counter */
 267
 268        unsigned char    p0;   /* 0x40 8-bit zero-register */
 269        unsigned char    vr;   /* 0x41 Version register */
 270
 271        unsigned short   p4;   /* 0x42 16-bit zero-register */
 272        unsigned short  ccr;   /* 0x44 Condition code register */
 273        
 274        unsigned int    mof;   /* 0x46 Multiply overflow register */
 275        
 276        unsigned int     p8;   /* 0x4A 32-bit zero-register */
 277        unsigned int    ibr;   /* 0x4E Interrupt base register */
 278        unsigned int    irp;   /* 0x52 Interrupt return pointer */
 279        unsigned int    srp;   /* 0x56 Subroutine return pointer */
 280        unsigned int    bar;   /* 0x5A Breakpoint address register */
 281        unsigned int   dccr;   /* 0x5E Double condition code register */
 282        unsigned int    brp;   /* 0x62 Breakpoint return pointer (pc in caller) */
 283        unsigned int    usp;   /* 0x66 User mode stack pointer */
 284} registers;
 285
 286/************** Prototypes for local library functions ***********************/
 287
 288/* Copy of strcpy from libc. */
 289static char *gdb_cris_strcpy (char *s1, const char *s2);
 290
 291/* Copy of strlen from libc. */
 292static int gdb_cris_strlen (const char *s);
 293
 294/* Copy of memchr from libc. */
 295static void *gdb_cris_memchr (const void *s, int c, int n);
 296
 297/* Copy of strtol from libc. Does only support base 16. */
 298static int gdb_cris_strtol (const char *s, char **endptr, int base);
 299
 300/********************** Prototypes for local functions. **********************/
 301/* Copy the content of a register image into another. The size n is
 302   the size of the register image. Due to struct assignment generation of
 303   memcpy in libc. */
 304static void copy_registers (registers *dptr, registers *sptr, int n);
 305
 306/* Copy the stored registers from the stack. Put the register contents
 307   of thread thread_id in the struct reg. */
 308static void copy_registers_from_stack (int thread_id, registers *reg);
 309
 310/* Copy the registers to the stack. Put the register contents of thread
 311   thread_id from struct reg to the stack. */
 312static void copy_registers_to_stack (int thread_id, registers *reg);
 313
 314/* Write a value to a specified register regno in the register image
 315   of the current thread. */
 316static int write_register (int regno, char *val);
 317
 318/* Write a value to a specified register in the stack of a thread other
 319   than the current thread. */
 320static write_stack_register (int thread_id, int regno, char *valptr);
 321
 322/* Read a value from a specified register in the register image. Returns the
 323   status of the read operation. The register value is returned in valptr. */
 324static int read_register (char regno, unsigned int *valptr);
 325
 326/* Serial port, reads one character. ETRAX 100 specific. from debugport.c */
 327int getDebugChar (void);
 328
 329/* Serial port, writes one character. ETRAX 100 specific. from debugport.c */
 330void putDebugChar (int val);
 331
 332void enableDebugIRQ (void);
 333
 334/* Returns the character equivalent of a nibble, bit 7, 6, 5, and 4 of a byte,
 335   represented by int x. */
 336static char highhex (int x);
 337
 338/* Returns the character equivalent of a nibble, bit 3, 2, 1, and 0 of a byte,
 339   represented by int x. */
 340static char lowhex (int x);
 341
 342/* Returns the integer equivalent of a hexadecimal character. */
 343static int hex (char ch);
 344
 345/* Convert the memory, pointed to by mem into hexadecimal representation.
 346   Put the result in buf, and return a pointer to the last character
 347   in buf (null). */
 348static char *mem2hex (char *buf, unsigned char *mem, int count);
 349
 350/* Convert the array, in hexadecimal representation, pointed to by buf into
 351   binary representation. Put the result in mem, and return a pointer to
 352   the character after the last byte written. */
 353static unsigned char *hex2mem (unsigned char *mem, char *buf, int count);
 354
 355/* Put the content of the array, in binary representation, pointed to by buf
 356   into memory pointed to by mem, and return a pointer to
 357   the character after the last byte written. */
 358static unsigned char *bin2mem (unsigned char *mem, unsigned char *buf, int count);
 359
 360/* Await the sequence $<data>#<checksum> and store <data> in the array buffer
 361   returned. */
 362static void getpacket (char *buffer);
 363
 364/* Send $<data>#<checksum> from the <data> in the array buffer. */
 365static void putpacket (char *buffer);
 366
 367/* Build and send a response packet in order to inform the host the
 368   stub is stopped. */
 369static void stub_is_stopped (int sigval);
 370
 371/* All expected commands are sent from remote.c. Send a response according
 372   to the description in remote.c. */
 373static void handle_exception (int sigval);
 374
 375/* Performs a complete re-start from scratch. ETRAX specific. */
 376static void kill_restart (void);
 377
 378/******************** Prototypes for global functions. ***********************/
 379
 380/* The string str is prepended with the GDB printout token and sent. */
 381void putDebugString (const unsigned char *str, int length); /* used by etrax100ser.c */
 382
 383/* The hook for both static (compiled) and dynamic breakpoints set by GDB.
 384   ETRAX 100 specific. */
 385void handle_breakpoint (void);                          /* used by irq.c */
 386
 387/* The hook for an interrupt generated by GDB. ETRAX 100 specific. */
 388void handle_interrupt (void);                           /* used by irq.c */
 389
 390/* A static breakpoint to be used at startup. */
 391void breakpoint (void);                                 /* called by init/main.c */
 392
 393/* From osys_int.c, executing_task contains the number of the current
 394   executing task in osys. Does not know of object-oriented threads. */
 395extern unsigned char executing_task;
 396
 397/* The number of characters used for a 64 bit thread identifier. */
 398#define HEXCHARS_IN_THREAD_ID 16
 399
 400/* Avoid warning as the internal_stack is not used in the C-code. */
 401#define USEDVAR(name)    { if (name) { ; } }
 402#define USEDFUN(name) { void (*pf)(void) = (void *)name; USEDVAR(pf) }
 403
 404/********************************** Packet I/O ******************************/
 405/* BUFMAX defines the maximum number of characters in
 406   inbound/outbound buffers */
 407#define BUFMAX 512
 408
 409/* Run-length encoding maximum length. Send 64 at most. */
 410#define RUNLENMAX 64
 411
 412/* Definition of all valid hexadecimal characters */
 413static const char hexchars[] = "0123456789abcdef";
 414
 415/* The inbound/outbound buffers used in packet I/O */
 416static char remcomInBuffer[BUFMAX];
 417static char remcomOutBuffer[BUFMAX];
 418
 419/* Error and warning messages. */
 420enum error_type
 421{
 422        SUCCESS, E01, E02, E03, E04, E05, E06, E07
 423};
 424static char *error_message[] =
 425{
 426        "",
 427        "E01 Set current or general thread - H[c,g] - internal error.",
 428        "E02 Change register content - P - cannot change read-only register.",
 429        "E03 Thread is not alive.", /* T, not used. */
 430        "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
 431        "E05 Change register content - P - the register is not implemented..",
 432        "E06 Change memory content - M - internal error.",
 433        "E07 Change register content - P - the register is not stored on the stack"
 434};
 435/********************************* Register image ****************************/
 436/* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
 437   Reference", p. 1-1, with the additional register definitions of the
 438   ETRAX 100LX in cris-opc.h.
 439   There are 16 general 32-bit registers, R0-R15, where R14 is the stack
 440   pointer, SP, and R15 is the program counter, PC.
 441   There are 16 special registers, P0-P15, where three of the unimplemented
 442   registers, P0, P4 and P8, are reserved as zero-registers. A read from
 443   any of these registers returns zero and a write has no effect. */
 444enum register_name
 445{
 446        R0,  R1,   R2,  R3,
 447        R4,  R5,   R6,  R7,
 448        R8,  R9,   R10, R11,
 449        R12, R13,  SP,  PC,
 450        P0,  VR,   P2,  P3,
 451        P4,  CCR,  P6,  MOF,
 452        P8,  IBR,  IRP, SRP,
 453        BAR, DCCR, BRP, USP
 454};
 455
 456/* The register sizes of the registers in register_name. An unimplemented register
 457   is designated by size 0 in this array. */
 458static int register_size[] =
 459{
 460        4, 4, 4, 4,
 461        4, 4, 4, 4,
 462        4, 4, 4, 4,
 463        4, 4, 4, 4,
 464        1, 1, 0, 0,
 465        2, 2, 0, 4,
 466        4, 4, 4, 4,
 467        4, 4, 4, 4
 468};
 469
 470/* Contains the register image of the executing thread in the assembler
 471   part of the code in order to avoid horrible addressing modes. */
 472static registers reg;
 473
 474/* FIXME: Should this be used? Delete otherwise. */
 475/* Contains the assumed consistency state of the register image. Uses the
 476   enum error_type for state information. */
 477static int consistency_status = SUCCESS;
 478
 479/********************************** Handle exceptions ************************/
 480/* The variable reg contains the register image associated with the
 481   current_thread_c variable. It is a complete register image created at
 482   entry. The reg_g contains a register image of a task where the general
 483   registers are taken from the stack and all special registers are taken
 484   from the executing task. It is associated with current_thread_g and used
 485   in order to provide access mainly for 'g', 'G' and 'P'.
 486*/
 487
 488/* Need two task id pointers in order to handle Hct and Hgt commands. */
 489static int current_thread_c = 0;
 490static int current_thread_g = 0;
 491
 492/* Need two register images in order to handle Hct and Hgt commands. The
 493   variable reg_g is in addition to reg above. */
 494static registers reg_g;
 495
 496/********************************** Breakpoint *******************************/
 497/* Use an internal stack in the breakpoint and interrupt response routines */
 498#define INTERNAL_STACK_SIZE 1024
 499static char internal_stack[INTERNAL_STACK_SIZE];
 500
 501/* Due to the breakpoint return pointer, a state variable is needed to keep
 502   track of whether it is a static (compiled) or dynamic (gdb-invoked)
 503   breakpoint to be handled. A static breakpoint uses the content of register
 504   BRP as it is whereas a dynamic breakpoint requires subtraction with 2
 505   in order to execute the instruction. The first breakpoint is static. */
 506static unsigned char is_dyn_brkp = 0;
 507
 508/********************************* String library ****************************/
 509/* Single-step over library functions creates trap loops. */
 510
 511/* Copy char s2[] to s1[]. */
 512static char*
 513gdb_cris_strcpy (char *s1, const char *s2)
 514{
 515        char *s = s1;
 516        
 517        for (s = s1; (*s++ = *s2++) != '\0'; )
 518                ;
 519        return (s1);
 520}
 521
 522/* Find length of s[]. */
 523static int
 524gdb_cris_strlen (const char *s)
 525{
 526        const char *sc;
 527        
 528        for (sc = s; *sc != '\0'; sc++)
 529                ;
 530        return (sc - s);
 531}
 532
 533/* Find first occurrence of c in s[n]. */
 534static void*
 535gdb_cris_memchr (const void *s, int c, int n)
 536{
 537        const unsigned char uc = c;
 538        const unsigned char *su;
 539        
 540        for (su = s; 0 < n; ++su, --n)
 541                if (*su == uc)
 542                        return ((void *)su);
 543        return (NULL);
 544}
 545/******************************* Standard library ****************************/
 546/* Single-step over library functions creates trap loops. */
 547/* Convert string to long. */
 548static int
 549gdb_cris_strtol (const char *s, char **endptr, int base)
 550{
 551        char *s1;
 552        char *sd;
 553        int x = 0;
 554        
 555        for (s1 = (char*)s; (sd = gdb_cris_memchr(hexchars, *s1, base)) != NULL; ++s1)
 556                x = x * base + (sd - hexchars);
 557        
 558        if (endptr)
 559        {
 560                /* Unconverted suffix is stored in endptr unless endptr is NULL. */
 561                *endptr = s1;
 562        }
 563        
 564        return x;
 565}
 566
 567int
 568double_this(int x)
 569{
 570        return 2 * x;
 571}
 572
 573/********************************* Register image ****************************/
 574/* Copy the content of a register image into another. The size n is
 575   the size of the register image. Due to struct assignment generation of
 576   memcpy in libc. */
 577static void
 578copy_registers (registers *dptr, registers *sptr, int n)
 579{
 580        unsigned char *dreg;
 581        unsigned char *sreg;
 582        
 583        for (dreg = (unsigned char*)dptr, sreg = (unsigned char*)sptr; n > 0; n--)
 584                *dreg++ = *sreg++;
 585}
 586
 587#ifdef PROCESS_SUPPORT
 588/* Copy the stored registers from the stack. Put the register contents
 589   of thread thread_id in the struct reg. */
 590static void
 591copy_registers_from_stack (int thread_id, registers *regptr)
 592{
 593        int j;
 594        stack_registers *s = (stack_registers *)stack_list[thread_id];
 595        unsigned int *d = (unsigned int *)regptr;
 596        
 597        for (j = 13; j >= 0; j--)
 598                *d++ = s->r[j];
 599        regptr->sp = (unsigned int)stack_list[thread_id];
 600        regptr->pc = s->pc;
 601        regptr->dccr = s->dccr;
 602        regptr->srp = s->srp;
 603}
 604
 605/* Copy the registers to the stack. Put the register contents of thread
 606   thread_id from struct reg to the stack. */
 607static void
 608copy_registers_to_stack (int thread_id, registers *regptr)
 609{
 610        int i;
 611        stack_registers *d = (stack_registers *)stack_list[thread_id];
 612        unsigned int *s = (unsigned int *)regptr;
 613        
 614        for (i = 0; i < 14; i++) {
 615                d->r[i] = *s++;
 616        }
 617        d->pc = regptr->pc;
 618        d->dccr = regptr->dccr;
 619        d->srp = regptr->srp;
 620}
 621#endif
 622
 623/* Write a value to a specified register in the register image of the current
 624   thread. Returns status code SUCCESS, E02 or E05. */
 625static int
 626write_register (int regno, char *val)
 627{
 628        int status = SUCCESS;
 629        registers *current_reg = &reg;
 630
 631        if (regno >= R0 && regno <= PC) {
 632                /* 32-bit register with simple offset. */
 633                hex2mem ((unsigned char *)current_reg + regno * sizeof(unsigned int),
 634                         val, sizeof(unsigned int));
 635        }
 636        else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {
 637                /* Do not support read-only registers. */
 638                status = E02;
 639        }
 640        else if (regno == CCR) {
 641                /* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented, 
 642                   and P7 (MOF) is 32 bits in ETRAX 100LX. */
 643                hex2mem ((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),
 644                         val, sizeof(unsigned short));
 645        }
 646        else if (regno >= MOF && regno <= USP) {
 647                /* 32 bit register with complex offset.  (P8 has been taken care of.) */
 648                hex2mem ((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),
 649                         val, sizeof(unsigned int));
 650        } 
 651        else {
 652                /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
 653                status = E05;
 654        }
 655        return status;
 656}
 657
 658#ifdef PROCESS_SUPPORT
 659/* Write a value to a specified register in the stack of a thread other
 660   than the current thread. Returns status code SUCCESS or E07. */
 661static int
 662write_stack_register (int thread_id, int regno, char *valptr)
 663{
 664        int status = SUCCESS;
 665        stack_registers *d = (stack_registers *)stack_list[thread_id];
 666        unsigned int val;
 667        
 668        hex2mem ((unsigned char *)&val, valptr, sizeof(unsigned int));
 669        if (regno >= R0 && regno < SP) {
 670                d->r[regno] = val;
 671        }
 672        else if (regno == SP) {
 673                stack_list[thread_id] = val;
 674        }
 675        else if (regno == PC) {
 676                d->pc = val;
 677        }
 678        else if (regno == SRP) {
 679                d->srp = val;
 680        }
 681        else if (regno == DCCR) {
 682                d->dccr = val;
 683        }
 684        else {
 685                /* Do not support registers in the current thread. */
 686                status = E07;
 687        }
 688        return status;
 689}
 690#endif
 691
 692/* Read a value from a specified register in the register image. Returns the
 693   value in the register or -1 for non-implemented registers.
 694   Should check consistency_status after a call which may be E05 after changes
 695   in the implementation. */
 696static int
 697read_register (char regno, unsigned int *valptr)
 698{
 699        registers *current_reg = &reg;
 700
 701        if (regno >= R0 && regno <= PC) {
 702                /* 32-bit register with simple offset. */
 703                *valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));
 704                return SUCCESS;
 705        }
 706        else if (regno == P0 || regno == VR) {
 707                /* 8 bit register with complex offset. */
 708                *valptr = (unsigned int)(*(unsigned char *)
 709                                         ((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));
 710                return SUCCESS;
 711        }
 712        else if (regno == P4 || regno == CCR) {
 713                /* 16 bit register with complex offset. */
 714                *valptr = (unsigned int)(*(unsigned short *)
 715                                         ((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));
 716                return SUCCESS;
 717        }
 718        else if (regno >= MOF && regno <= USP) {
 719                /* 32 bit register with complex offset. */
 720                *valptr = *(unsigned int *)((char *)&(current_reg->p8)
 721                                            + (regno-P8) * sizeof(unsigned int));
 722                return SUCCESS;
 723        }
 724        else {
 725                /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
 726                consistency_status = E05;
 727                return E05;
 728        }
 729}
 730
 731/********************************** Packet I/O ******************************/
 732/* Returns the character equivalent of a nibble, bit 7, 6, 5, and 4 of a byte,
 733   represented by int x. */
 734static inline char
 735highhex(int x)
 736{
 737        return hexchars[(x >> 4) & 0xf];
 738}
 739
 740/* Returns the character equivalent of a nibble, bit 3, 2, 1, and 0 of a byte,
 741   represented by int x. */
 742static inline char
 743lowhex(int x)
 744{
 745        return hexchars[x & 0xf];
 746}
 747
 748/* Returns the integer equivalent of a hexadecimal character. */
 749static int
 750hex (char ch)
 751{
 752        if ((ch >= 'a') && (ch <= 'f'))
 753                return (ch - 'a' + 10);
 754        if ((ch >= '0') && (ch <= '9'))
 755                return (ch - '0');
 756        if ((ch >= 'A') && (ch <= 'F'))
 757                return (ch - 'A' + 10);
 758        return (-1);
 759}
 760
 761/* Convert the memory, pointed to by mem into hexadecimal representation.
 762   Put the result in buf, and return a pointer to the last character
 763   in buf (null). */
 764
 765static int do_printk = 0;
 766
 767static char *
 768mem2hex(char *buf, unsigned char *mem, int count)
 769{
 770        int i;
 771        int ch;
 772        
 773        if (mem == NULL) {
 774                /* Bogus read from m0. FIXME: What constitutes a valid address? */
 775                for (i = 0; i < count; i++) {
 776                        *buf++ = '0';
 777                        *buf++ = '0';
 778                }
 779        } else {
 780                /* Valid mem address. */
 781                for (i = 0; i < count; i++) {
 782                        ch = *mem++;
 783                        *buf++ = highhex (ch);
 784                        *buf++ = lowhex (ch);
 785                }
 786        }
 787        
 788        /* Terminate properly. */
 789        *buf = '\0';
 790        return (buf);
 791}
 792
 793/* Convert the array, in hexadecimal representation, pointed to by buf into
 794   binary representation. Put the result in mem, and return a pointer to
 795   the character after the last byte written. */
 796static unsigned char*
 797hex2mem (unsigned char *mem, char *buf, int count)
 798{
 799        int i;
 800        unsigned char ch;
 801        for (i = 0; i < count; i++) {
 802                ch = hex (*buf++) << 4;
 803                ch = ch + hex (*buf++);
 804                *mem++ = ch;
 805        }
 806        return (mem);
 807}
 808
 809/* Put the content of the array, in binary representation, pointed to by buf
 810   into memory pointed to by mem, and return a pointer to the character after
 811   the last byte written.
 812   Gdb will escape $, #, and the escape char (0x7d). */
 813static unsigned char*
 814bin2mem (unsigned char *mem, unsigned char *buf, int count)
 815{
 816        int i;
 817        unsigned char *next;
 818        for (i = 0; i < count; i++) {
 819                /* Check for any escaped characters. Be paranoid and
 820                   only unescape chars that should be escaped. */
 821                if (*buf == 0x7d) {
 822                        next = buf + 1;
 823                        if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */
 824                                {
 825                                        buf++;
 826                                        *buf += 0x20;
 827                                }
 828                }
 829                *mem++ = *buf++;
 830        }
 831        return (mem);
 832}
 833
 834/* Await the sequence $<data>#<checksum> and store <data> in the array buffer
 835   returned. */
 836static void
 837getpacket (char *buffer)
 838{
 839        unsigned char checksum;
 840        unsigned char xmitcsum;
 841        int i;
 842        int count;
 843        char ch;
 844        do {
 845                while ((ch = getDebugChar ()) != '$')
 846                        /* Wait for the start character $ and ignore all other characters */;
 847                checksum = 0;
 848                xmitcsum = -1;
 849                count = 0;
 850                /* Read until a # or the end of the buffer is reached */
 851                while (count < BUFMAX) {
 852                        ch = getDebugChar ();
 853                        if (ch == '#')
 854                                break;
 855                        checksum = checksum + ch;
 856                        buffer[count] = ch;
 857                        count = count + 1;
 858                }
 859                buffer[count] = '\0';
 860                
 861                if (ch == '#') {
 862                        xmitcsum = hex (getDebugChar ()) << 4;
 863                        xmitcsum += hex (getDebugChar ());
 864                        if (checksum != xmitcsum) {
 865                                /* Wrong checksum */
 866                                putDebugChar ('-');
 867                        }
 868                        else {
 869                                /* Correct checksum */
 870                                putDebugChar ('+');
 871                                /* If sequence characters are received, reply with them */
 872                                if (buffer[2] == ':') {
 873                                        putDebugChar (buffer[0]);
 874                                        putDebugChar (buffer[1]);
 875                                        /* Remove the sequence characters from the buffer */
 876                                        count = gdb_cris_strlen (buffer);
 877                                        for (i = 3; i <= count; i++)
 878                                                buffer[i - 3] = buffer[i];
 879                                }
 880                        }
 881                }
 882        } while (checksum != xmitcsum);
 883}
 884
 885/* Send $<data>#<checksum> from the <data> in the array buffer. */
 886
 887static void
 888putpacket(char *buffer)
 889{
 890        int checksum;
 891        int runlen;
 892        int encode;
 893        
 894        do {
 895                char *src = buffer;
 896                putDebugChar ('$');
 897                checksum = 0;
 898                while (*src) {
 899                        /* Do run length encoding */
 900                        putDebugChar (*src);
 901                        checksum += *src;
 902                        runlen = 0;
 903                        while (runlen < RUNLENMAX && *src == src[runlen]) {
 904                                runlen++;
 905                        }
 906                        if (runlen > 3) {
 907                                /* Got a useful amount */
 908                                putDebugChar ('*');
 909                                checksum += '*';
 910                                encode = runlen + ' ' - 4;
 911                                putDebugChar (encode);
 912                                checksum += encode;
 913                                src += runlen;
 914                        }
 915                        else {
 916                                src++;
 917                        }
 918                }
 919                putDebugChar ('#');
 920                putDebugChar (highhex (checksum));
 921                putDebugChar (lowhex (checksum));
 922        } while(kgdb_started && (getDebugChar() != '+'));
 923}
 924
 925/* The string str is prepended with the GDB printout token and sent. Required
 926   in traditional implementations. */
 927void
 928putDebugString (const unsigned char *str, int length)
 929{
 930        remcomOutBuffer[0] = 'O';
 931        mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);
 932        putpacket(remcomOutBuffer);
 933}
 934
 935/********************************** Handle exceptions ************************/
 936/* Build and send a response packet in order to inform the host the
 937   stub is stopped. TAAn...:r...;n...:r...;n...:r...;
 938                    AA = signal number
 939                    n... = register number (hex)
 940                    r... = register contents
 941                    n... = `thread'
 942                    r... = thread process ID.  This is a hex integer.
 943                    n... = other string not starting with valid hex digit.
 944                    gdb should ignore this n,r pair and go on to the next.
 945                    This way we can extend the protocol. */
 946static void
 947stub_is_stopped(int sigval)
 948{
 949        char *ptr = remcomOutBuffer;
 950        int regno;
 951
 952        unsigned int reg_cont;
 953        int status;
 954        
 955        /* Send trap type (converted to signal) */
 956
 957        *ptr++ = 'T';   
 958        *ptr++ = highhex (sigval);
 959        *ptr++ = lowhex (sigval);
 960
 961        /* Send register contents. We probably only need to send the
 962         * PC, frame pointer and stack pointer here. Other registers will be
 963         * explicitely asked for. But for now, send all. 
 964         */
 965        
 966        for (regno = R0; regno <= USP; regno++) {
 967                /* Store n...:r...; for the registers in the buffer. */
 968
 969                status = read_register (regno, &reg_cont);
 970                
 971                if (status == SUCCESS) {
 972                        
 973                        *ptr++ = highhex (regno);
 974                        *ptr++ = lowhex (regno);
 975                        *ptr++ = ':';
 976
 977                        ptr = mem2hex(ptr, (unsigned char *)&reg_cont,
 978                                      register_size[regno]);
 979                        *ptr++ = ';';
 980                }
 981                
 982        }
 983
 984#ifdef PROCESS_SUPPORT
 985        /* Store the registers of the executing thread. Assume that both step,
 986           continue, and register content requests are with respect to this
 987           thread. The executing task is from the operating system scheduler. */
 988
 989        current_thread_c = executing_task;
 990        current_thread_g = executing_task;
 991
 992        /* A struct assignment translates into a libc memcpy call. Avoid
 993           all libc functions in order to prevent recursive break points. */
 994        copy_registers (&reg_g, &reg, sizeof(registers));
 995
 996        /* Store thread:r...; with the executing task TID. */
 997        gdb_cris_strcpy (&remcomOutBuffer[pos], "thread:");
 998        pos += gdb_cris_strlen ("thread:");
 999        remcomOutBuffer[pos++] = highhex (executing_task);
1000        remcomOutBuffer[pos++] = lowhex (executing_task);
1001        gdb_cris_strcpy (&remcomOutBuffer[pos], ";");
1002#endif
1003
1004        /* null-terminate and send it off */
1005
1006        *ptr = 0;
1007
1008        putpacket (remcomOutBuffer);
1009}
1010
1011/* All expected commands are sent from remote.c. Send a response according
1012   to the description in remote.c. */
1013static void
1014handle_exception (int sigval)
1015{
1016        /* Avoid warning of not used. */
1017
1018        USEDFUN(handle_exception);
1019        USEDVAR(internal_stack[0]);
1020
1021        /* Send response. */
1022
1023        stub_is_stopped (sigval);
1024
1025        for (;;) {
1026                remcomOutBuffer[0] = '\0';
1027                getpacket (remcomInBuffer);
1028                switch (remcomInBuffer[0]) {
1029                        case 'g':
1030                                /* Read registers: g
1031                                   Success: Each byte of register data is described by two hex digits.
1032                                   Registers are in the internal order for GDB, and the bytes
1033                                   in a register  are in the same order the machine uses.
1034                                   Failure: void. */
1035                                
1036                                {
1037#ifdef PROCESS_SUPPORT
1038                                        /* Use the special register content in the executing thread. */
1039                                        copy_registers (&reg_g, &reg, sizeof(registers));
1040                                        /* Replace the content available on the stack. */
1041                                        if (current_thread_g != executing_task) {
1042                                                copy_registers_from_stack (current_thread_g, &reg_g);
1043                                        }
1044                                        mem2hex ((unsigned char *)remcomOutBuffer, (unsigned char *)&reg_g, sizeof(registers));
1045#else
1046                                        mem2hex(remcomOutBuffer, (char *)&reg, sizeof(registers));
1047#endif
1048                                }
1049                                break;
1050                                
1051                        case 'G':
1052                                /* Write registers. GXX..XX
1053                                   Each byte of register data  is described by two hex digits.
1054                                   Success: OK
1055                                   Failure: void. */
1056#ifdef PROCESS_SUPPORT
1057                                hex2mem ((unsigned char *)&reg_g, &remcomInBuffer[1], sizeof(registers));
1058                                if (current_thread_g == executing_task) {
1059                                        copy_registers (&reg, &reg_g, sizeof(registers));
1060                                }
1061                                else {
1062                                        copy_registers_to_stack(current_thread_g, &reg_g);
1063                                }
1064#else
1065                                hex2mem((char *)&reg, &remcomInBuffer[1], sizeof(registers));
1066#endif
1067                                gdb_cris_strcpy (remcomOutBuffer, "OK");
1068                                break;
1069                                
1070                        case 'P':
1071                                /* Write register. Pn...=r...
1072                                   Write register n..., hex value without 0x, with value r...,
1073                                   which contains a hex value without 0x and two hex digits
1074                                   for each byte in the register (target byte order). P1f=11223344 means
1075                                   set register 31 to 44332211.
1076                                   Success: OK
1077                                   Failure: E02, E05 */
1078                                {
1079                                        char *suffix;
1080                                        int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);
1081                                        int status;
1082#ifdef PROCESS_SUPPORT
1083                                        if (current_thread_g != executing_task)
1084                                                status = write_stack_register (current_thread_g, regno, suffix+1);
1085                                        else
1086#endif
1087                                                status = write_register (regno, suffix+1);
1088
1089                                        switch (status) {
1090                                                case E02:
1091                                                        /* Do not support read-only registers. */
1092                                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);
1093                                                        break;
1094                                                case E05:
1095                                                        /* Do not support non-existing registers. */
1096                                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);
1097                                                        break;
1098                                                case E07:
1099                                                        /* Do not support non-existing registers on the stack. */
1100                                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);
1101                                                        break;
1102                                                default:
1103                                                        /* Valid register number. */
1104                                                        gdb_cris_strcpy (remcomOutBuffer, "OK");
1105                                                        break;
1106                                        }
1107                                }
1108                                break;
1109                                
1110                        case 'm':
1111                                /* Read from memory. mAA..AA,LLLL
1112                                   AA..AA is the address and LLLL is the length.
1113                                   Success: XX..XX is the memory content.  Can be fewer bytes than
1114                                   requested if only part of the data may be read. m6000120a,6c means
1115                                   retrieve 108 byte from base address 6000120a.
1116                                   Failure: void. */
1117                                {
1118                                        char *suffix;
1119                                        unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
1120                                                                                               &suffix, 16);                                        int length = gdb_cris_strtol(suffix+1, 0, 16);
1121                                        
1122                                        mem2hex(remcomOutBuffer, addr, length);
1123                                }
1124                                break;
1125                                
1126                        case 'X':
1127                                /* Write to memory. XAA..AA,LLLL:XX..XX
1128                                   AA..AA is the start address,  LLLL is the number of bytes, and
1129                                   XX..XX is the binary data.
1130                                   Success: OK
1131                                   Failure: void. */
1132                        case 'M':
1133                                /* Write to memory. MAA..AA,LLLL:XX..XX
1134                                   AA..AA is the start address,  LLLL is the number of bytes, and
1135                                   XX..XX is the hexadecimal data.
1136                                   Success: OK
1137                                   Failure: void. */
1138                                {
1139                                        char *lenptr;
1140                                        char *dataptr;
1141                                        unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
1142                                                                                      &lenptr, 16);
1143                                        int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);
1144                                        if (*lenptr == ',' && *dataptr == ':') {
1145                                                if (remcomInBuffer[0] == 'M') {
1146                                                        hex2mem(addr, dataptr + 1, length);
1147                                                }
1148                                                else /* X */ {
1149                                                        bin2mem(addr, dataptr + 1, length);
1150                                                }
1151                                                gdb_cris_strcpy (remcomOutBuffer, "OK");
1152                                        }
1153                                        else {
1154                                                gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);
1155                                        }
1156                                }
1157                                break;
1158                                
1159                        case 'c':
1160                                /* Continue execution. cAA..AA
1161                                   AA..AA is the address where execution is resumed. If AA..AA is
1162                                   omitted, resume at the present address.
1163                                   Success: return to the executing thread.
1164                                   Failure: will never know. */
1165                                if (remcomInBuffer[1] != '\0') {
1166                                        reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
1167                                }
1168                                enableDebugIRQ();
1169                                return;
1170                                
1171                        case 's':
1172                                /* Step. sAA..AA
1173                                   AA..AA is the address where execution is resumed. If AA..AA is
1174                                   omitted, resume at the present address. Success: return to the
1175                                   executing thread. Failure: will never know.
1176                                   
1177                                   Should never be invoked. The single-step is implemented on
1178                                   the host side. If ever invoked, it is an internal error E04. */
1179                                gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1180                                putpacket (remcomOutBuffer);
1181                                return;
1182                                
1183                        case '?':
1184                                /* The last signal which caused a stop. ?
1185                                   Success: SAA, where AA is the signal number.
1186                                   Failure: void. */
1187                                remcomOutBuffer[0] = 'S';
1188                                remcomOutBuffer[1] = highhex (sigval);
1189                                remcomOutBuffer[2] = lowhex (sigval);
1190                                remcomOutBuffer[3] = 0;
1191                                break;
1192                                
1193                        case 'D':
1194                                /* Detach from host. D
1195                                   Success: OK, and return to the executing thread.
1196                                   Failure: will never know */
1197                                putpacket ("OK");
1198                                return;
1199                                
1200                        case 'k':
1201                        case 'r':
1202                                /* kill request or reset request.
1203                                   Success: restart of target.
1204                                   Failure: will never know. */
1205                                kill_restart ();
1206                                break;
1207                                
1208                        case 'C':
1209                        case 'S':
1210                        case '!':
1211                        case 'R':
1212                        case 'd':
1213                                /* Continue with signal sig. Csig;AA..AA
1214                                   Step with signal sig. Ssig;AA..AA
1215                                   Use the extended remote protocol. !
1216                                   Restart the target system. R0
1217                                   Toggle debug flag. d
1218                                   Search backwards. tAA:PP,MM
1219                                   Not supported: E04 */
1220                                gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1221                                break;
1222#ifdef PROCESS_SUPPORT
1223
1224                        case 'T':
1225                                /* Thread alive. TXX
1226                                   Is thread XX alive?
1227                                   Success: OK, thread XX is alive.
1228                                   Failure: E03, thread XX is dead. */
1229                                {
1230                                        int thread_id = (int)gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
1231                                        /* Cannot tell whether it is alive or not. */
1232                                        if (thread_id >= 0 && thread_id < number_of_tasks)
1233                                                gdb_cris_strcpy (remcomOutBuffer, "OK");
1234                                }
1235                                break;
1236                                                                
1237                        case 'H':
1238                                /* Set thread for subsequent operations: Hct
1239                                   c = 'c' for thread used in step and continue;
1240                                   t can be -1 for all threads.
1241                                   c = 'g' for thread used in other  operations.
1242                                   t = 0 means pick any thread.
1243                                   Success: OK
1244                                   Failure: E01 */
1245                                {
1246                                        int thread_id = gdb_cris_strtol (&remcomInBuffer[2], 0, 16);
1247                                        if (remcomInBuffer[1] == 'c') {
1248                                                /* c = 'c' for thread used in step and continue */
1249                                                /* Do not change current_thread_c here. It would create a mess in
1250                                                   the scheduler. */
1251                                                gdb_cris_strcpy (remcomOutBuffer, "OK");
1252                                        }
1253                                        else if (remcomInBuffer[1] == 'g') {
1254                                                /* c = 'g' for thread used in other  operations.
1255                                                   t = 0 means pick any thread. Impossible since the scheduler does
1256                                                   not allow that. */
1257                                                if (thread_id >= 0 && thread_id < number_of_tasks) {
1258                                                        current_thread_g = thread_id;
1259                                                        gdb_cris_strcpy (remcomOutBuffer, "OK");
1260                                                }
1261                                                else {
1262                                                        /* Not expected - send an error message. */
1263                                                        gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1264                                                }
1265                                        }
1266                                        else {
1267                                                /* Not expected - send an error message. */
1268                                                gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1269                                        }
1270                                }
1271                                break;
1272                                
1273                        case 'q':
1274                        case 'Q':
1275                                /* Query of general interest. qXXXX
1276                                   Set general value XXXX. QXXXX=yyyy */
1277                                {
1278                                        int pos;
1279                                        int nextpos;
1280                                        int thread_id;
1281                                        
1282                                        switch (remcomInBuffer[1]) {
1283                                                case 'C':
1284                                                        /* Identify the remote current thread. */
1285                                                        gdb_cris_strcpy (&remcomOutBuffer[0], "QC");
1286                                                        remcomOutBuffer[2] = highhex (current_thread_c);
1287                                                        remcomOutBuffer[3] = lowhex (current_thread_c);
1288                                                        remcomOutBuffer[4] = '\0';
1289                                                        break;
1290                                                case 'L':
1291                                                        gdb_cris_strcpy (&remcomOutBuffer[0], "QM");
1292                                                        /* Reply with number of threads. */
1293                                                        if (os_is_started()) {
1294                                                                remcomOutBuffer[2] = highhex (number_of_tasks);
1295                                                                remcomOutBuffer[3] = lowhex (number_of_tasks);
1296                                                        }
1297                                                        else {
1298                                                                remcomOutBuffer[2] = highhex (0);
1299                                                                remcomOutBuffer[3] = lowhex (1);
1300                                                        }
1301                                                        /* Done with the reply. */
1302                                                        remcomOutBuffer[4] = lowhex (1);
1303                                                        pos = 5;
1304                                                        /* Expects the argument thread id. */
1305                                                        for (; pos < (5 + HEXCHARS_IN_THREAD_ID); pos++)
1306                                                                remcomOutBuffer[pos] = remcomInBuffer[pos];
1307                                                        /* Reply with the thread identifiers. */
1308                                                        if (os_is_started()) {
1309                                                                /* Store the thread identifiers of all tasks. */
1310                                                                for (thread_id = 0; thread_id < number_of_tasks; thread_id++) {
1311                                                                        nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1312                                                                        for (; pos < nextpos; pos ++)
1313                                                                                remcomOutBuffer[pos] = lowhex (0);
1314                                                                        remcomOutBuffer[pos++] = lowhex (thread_id);
1315                                                                }
1316                                                        }
1317                                                        else {
1318                                                                /* Store the thread identifier of the boot task. */
1319                                                                nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1320                                                                for (; pos < nextpos; pos ++)
1321                                                                        remcomOutBuffer[pos] = lowhex (0);
1322                                                                remcomOutBuffer[pos++] = lowhex (current_thread_c);
1323                                                        }
1324                                                        remcomOutBuffer[pos] = '\0';
1325                                                        break;
1326                                                default:
1327                                                        /* Not supported: "" */
1328                                                        /* Request information about section offsets: qOffsets. */
1329                                                        remcomOutBuffer[0] = 0;
1330                                                        break;
1331                                        }
1332                                }
1333                                break;
1334#endif /* PROCESS_SUPPORT */
1335                                
1336                        default:
1337                                /* The stub should ignore other request and send an empty
1338                                   response ($#<checksum>). This way we can extend the protocol and GDB
1339                                   can tell whether the stub it is talking to uses the old or the new. */
1340                                remcomOutBuffer[0] = 0;
1341                                break;
1342                }
1343                putpacket(remcomOutBuffer);
1344        }
1345}
1346
1347/* The jump is to the address 0x00000002. Performs a complete re-start
1348   from scratch. */
1349static void
1350kill_restart ()
1351{
1352        __asm__ volatile ("jump 2");
1353}
1354
1355/********************************** Breakpoint *******************************/
1356/* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.
1357   An internal stack is used by the stub. The register image of the caller is
1358   stored in the structure register_image.
1359   Interactive communication with the host is handled by handle_exception and
1360   finally the register image is restored. */
1361
1362void kgdb_handle_breakpoint(void);
1363
1364asm ("
1365  .global kgdb_handle_breakpoint
1366kgdb_handle_breakpoint:
1367;;
1368;; Response to the break-instruction
1369;;
1370;; Create a register image of the caller
1371;;
1372  move     $dccr,[reg+0x5E] ; Save the flags in DCCR before disable interrupts
1373  di                        ; Disable interrupts
1374  move.d   $r0,[reg]        ; Save R0
1375  move.d   $r1,[reg+0x04]   ; Save R1
1376  move.d   $r2,[reg+0x08]   ; Save R2
1377  move.d   $r3,[reg+0x0C]   ; Save R3
1378  move.d   $r4,[reg+0x10]   ; Save R4
1379  move.d   $r5,[reg+0x14]   ; Save R5
1380  move.d   $r6,[reg+0x18]   ; Save R6
1381  move.d   $r7,[reg+0x1C]   ; Save R7
1382  move.d   $r8,[reg+0x20]   ; Save R8
1383  move.d   $r9,[reg+0x24]   ; Save R9
1384  move.d   $r10,[reg+0x28]  ; Save R10
1385  move.d   $r11,[reg+0x2C]  ; Save R11
1386  move.d   $r12,[reg+0x30]  ; Save R12
1387  move.d   $r13,[reg+0x34]  ; Save R13
1388  move.d   $sp,[reg+0x38]   ; Save SP (R14)
1389;; Due to the old assembler-versions BRP might not be recognized
1390  .word 0xE670              ; move brp,$r0
1391  subq     2,$r0             ; Set to address of previous instruction.
1392  move.d   $r0,[reg+0x3c]   ; Save the address in PC (R15)
1393  clear.b  [reg+0x40]      ; Clear P0
1394  move     $vr,[reg+0x41]   ; Save special register P1
1395  clear.w  [reg+0x42]      ; Clear P4
1396  move     $ccr,[reg+0x44]  ; Save special register CCR
1397  move     $mof,[reg+0x46]  ; P7
1398  clear.d  [reg+0x4A]      ; Clear P8
1399  move     $ibr,[reg+0x4E]  ; P9,
1400  move     $irp,[reg+0x52]  ; P10,
1401  move     $srp,[reg+0x56]  ; P11,
1402  move     $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR
1403                            ; P13, register DCCR already saved
1404;; Due to the old assembler-versions BRP might not be recognized
1405  .word 0xE670              ; move brp,r0
1406;; Static (compiled) breakpoints must return to the next instruction in order
1407;; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction
1408;; in order to execute it when execution is continued.
1409  test.b   [is_dyn_brkp]    ; Is this a dynamic breakpoint?
1410  beq      is_static         ; No, a static breakpoint
1411  nop
1412  subq     2,$r0              ; rerun the instruction the break replaced
1413is_static:
1414  moveq    1,$r1
1415  move.b   $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint
1416  move.d   $r0,[reg+0x62]    ; Save the return address in BRP
1417  move     $usp,[reg+0x66]   ; USP
1418;;
1419;; Handle the communication
1420;;
1421  move.d   internal_stack+1020,$sp ; Use the internal stack which grows upward
1422  moveq    5,$r10                   ; SIGTRAP
1423  jsr      handle_exception       ; Interactive routine
1424;;
1425;; Return to the caller
1426;;
1427   move.d  [reg],$r0         ; Restore R0
1428   move.d  [reg+0x04],$r1    ; Restore R1
1429   move.d  [reg+0x08],$r2    ; Restore R2
1430   move.d  [reg+0x0C],$r3    ; Restore R3
1431   move.d  [reg+0x10],$r4    ; Restore R4
1432   move.d  [reg+0x14],$r5    ; Restore R5
1433   move.d  [reg+0x18],$r6    ; Restore R6
1434   move.d  [reg+0x1C],$r7    ; Restore R7
1435   move.d  [reg+0x20],$r8    ; Restore R8
1436   move.d  [reg+0x24],$r9    ; Restore R9
1437   move.d  [reg+0x28],$r10   ; Restore R10
1438   move.d  [reg+0x2C],$r11   ; Restore R11
1439   move.d  [reg+0x30],$r12   ; Restore R12
1440   move.d  [reg+0x34],$r13   ; Restore R13
1441;;
1442;; FIXME: Which registers should be restored?
1443;;
1444   move.d  [reg+0x38],$sp    ; Restore SP (R14)
1445   move    [reg+0x56],$srp   ; Restore the subroutine return pointer.
1446   move    [reg+0x5E],$dccr  ; Restore DCCR
1447   move    [reg+0x66],$usp   ; Restore USP
1448   jump    [reg+0x62]       ; A jump to the content in register BRP works.
1449   nop                       ;
1450");
1451
1452/* The hook for an interrupt generated by GDB. An internal stack is used
1453   by the stub. The register image of the caller is stored in the structure
1454   register_image. Interactive communication with the host is handled by
1455   handle_exception and finally the register image is restored. Due to the
1456   old assembler which does not recognise the break instruction and the
1457   breakpoint return pointer hex-code is used. */
1458
1459void kgdb_handle_serial(void);
1460
1461asm ("
1462  .global kgdb_handle_serial
1463kgdb_handle_serial:
1464;;
1465;; Response to a serial interrupt
1466;;
1467
1468  move     $dccr,[reg+0x5E] ; Save the flags in DCCR
1469  di                        ; Disable interrupts
1470  move.d   $r0,[reg]        ; Save R0
1471  move.d   $r1,[reg+0x04]   ; Save R1
1472  move.d   $r2,[reg+0x08]   ; Save R2
1473  move.d   $r3,[reg+0x0C]   ; Save R3
1474  move.d   $r4,[reg+0x10]   ; Save R4
1475  move.d   $r5,[reg+0x14]   ; Save R5
1476  move.d   $r6,[reg+0x18]   ; Save R6
1477  move.d   $r7,[reg+0x1C]   ; Save R7
1478  move.d   $r8,[reg+0x20]   ; Save R8
1479  move.d   $r9,[reg+0x24]   ; Save R9
1480  move.d   $r10,[reg+0x28]  ; Save R10
1481  move.d   $r11,[reg+0x2C]  ; Save R11
1482  move.d   $r12,[reg+0x30]  ; Save R12
1483  move.d   $r13,[reg+0x34]  ; Save R13
1484  move.d   $sp,[reg+0x38]   ; Save SP (R14)
1485  move     $irp,[reg+0x3c]  ; Save the address in PC (R15)
1486  clear.b  [reg+0x40]      ; Clear P0
1487  move     $vr,[reg+0x41]   ; Save special register P1,
1488  clear.w  [reg+0x42]      ; Clear P4
1489  move     $ccr,[reg+0x44]  ; Save special register CCR
1490  move     $mof,[reg+0x46]  ; P7
1491  clear.d  [reg+0x4A]      ; Clear P8
1492  move     $ibr,[reg+0x4E]  ; P9,
1493  move     $irp,[reg+0x52]  ; P10,
1494  move     $srp,[reg+0x56]  ; P11,
1495  move     $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR
1496                            ; P13, register DCCR already saved
1497;; Due to the old assembler-versions BRP might not be recognized
1498  .word 0xE670              ; move brp,r0
1499  move.d   $r0,[reg+0x62]   ; Save the return address in BRP
1500  move     $usp,[reg+0x66]  ; USP
1501
1502;; get the serial character (from debugport.c) and check if it is a ctrl-c
1503
1504  jsr getDebugChar
1505  cmp.b 3, $r10
1506  bne goback
1507  nop
1508
1509;;
1510;; Handle the communication
1511;;
1512  move.d   internal_stack+1020,$sp ; Use the internal stack
1513  moveq    2,$r10                   ; SIGINT
1514  jsr      handle_exception       ; Interactive routine
1515
1516goback:
1517;;
1518;; Return to the caller
1519;;
1520   move.d  [reg],$r0         ; Restore R0
1521   move.d  [reg+0x04],$r1    ; Restore R1
1522   move.d  [reg+0x08],$r2    ; Restore R2
1523   move.d  [reg+0x0C],$r3    ; Restore R3
1524   move.d  [reg+0x10],$r4    ; Restore R4
1525   move.d  [reg+0x14],$r5    ; Restore R5
1526   move.d  [reg+0x18],$r6    ; Restore R6
1527   move.d  [reg+0x1C],$r7    ; Restore R7
1528   move.d  [reg+0x20],$r8    ; Restore R8
1529   move.d  [reg+0x24],$r9    ; Restore R9
1530   move.d  [reg+0x28],$r10   ; Restore R10
1531   move.d  [reg+0x2C],$r11   ; Restore R11
1532   move.d  [reg+0x30],$r12   ; Restore R12
1533   move.d  [reg+0x34],$r13   ; Restore R13
1534;;
1535;; FIXME: Which registers should be restored?
1536;;
1537   move.d  [reg+0x38],$sp    ; Restore SP (R14)
1538   move    [reg+0x56],$srp   ; Restore the subroutine return pointer.
1539   move    [reg+0x5E],$dccr  ; Restore DCCR
1540   move    [reg+0x66],$usp   ; Restore USP
1541   reti                      ; Return from the interrupt routine
1542   nop
1543");
1544
1545/* Use this static breakpoint in the start-up only. */
1546
1547void
1548breakpoint(void)
1549{
1550        kgdb_started = 1;
1551        is_dyn_brkp = 0;     /* This is a static, not a dynamic breakpoint. */
1552        __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */
1553}
1554
1555/* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */
1556
1557void
1558kgdb_init(void)
1559{
1560        /* could initialize debug port as well but it's done in head.S already... */
1561
1562        /* breakpoint handler is now set in irq.c */
1563        set_int_vector(8, kgdb_handle_serial);
1564        
1565        enableDebugIRQ();
1566}
1567
1568/****************************** End of file **********************************/
1569
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.