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