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