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