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