cris/kgdb: Fix buffer overflow in getpacket()
[linux-2.6-microblaze.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 <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 /* Serial port, reads one character. ETRAX 100 specific. from debugport.c */
234 int getDebugChar (void);
235
236 /* Serial port, writes one character. ETRAX 100 specific. from debugport.c */
237 void putDebugChar (int val);
238
239 void enableDebugIRQ (void);
240
241 /******************** Prototypes for global functions. ***********************/
242
243 /* The string str is prepended with the GDB printout token and sent. */
244 void putDebugString (const unsigned char *str, int length); /* used by etrax100ser.c */
245
246 /* The hook for both static (compiled) and dynamic breakpoints set by GDB.
247    ETRAX 100 specific. */
248 void handle_breakpoint (void);                          /* used by irq.c */
249
250 /* The hook for an interrupt generated by GDB. ETRAX 100 specific. */
251 void handle_interrupt (void);                           /* used by irq.c */
252
253 /* A static breakpoint to be used at startup. */
254 void breakpoint (void);                                 /* called by init/main.c */
255
256 /* From osys_int.c, executing_task contains the number of the current
257    executing task in osys. Does not know of object-oriented threads. */
258 extern unsigned char executing_task;
259
260 /* The number of characters used for a 64 bit thread identifier. */
261 #define HEXCHARS_IN_THREAD_ID 16
262
263 /********************************** Packet I/O ******************************/
264 /* BUFMAX defines the maximum number of characters in
265    inbound/outbound buffers */
266 #define BUFMAX 512
267
268 /* Run-length encoding maximum length. Send 64 at most. */
269 #define RUNLENMAX 64
270
271 /* The inbound/outbound buffers used in packet I/O */
272 static char remcomInBuffer[BUFMAX];
273 static char remcomOutBuffer[BUFMAX];
274
275 /* Error and warning messages. */
276 enum error_type
277 {
278         SUCCESS, E01, E02, E03, E04, E05, E06, E07
279 };
280 static char *error_message[] =
281 {
282         "",
283         "E01 Set current or general thread - H[c,g] - internal error.",
284         "E02 Change register content - P - cannot change read-only register.",
285         "E03 Thread is not alive.", /* T, not used. */
286         "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
287         "E05 Change register content - P - the register is not implemented..",
288         "E06 Change memory content - M - internal error.",
289         "E07 Change register content - P - the register is not stored on the stack"
290 };
291 /********************************* Register image ****************************/
292 /* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
293    Reference", p. 1-1, with the additional register definitions of the
294    ETRAX 100LX in cris-opc.h.
295    There are 16 general 32-bit registers, R0-R15, where R14 is the stack
296    pointer, SP, and R15 is the program counter, PC.
297    There are 16 special registers, P0-P15, where three of the unimplemented
298    registers, P0, P4 and P8, are reserved as zero-registers. A read from
299    any of these registers returns zero and a write has no effect. */
300 enum register_name
301 {
302         R0,  R1,   R2,  R3,
303         R4,  R5,   R6,  R7,
304         R8,  R9,   R10, R11,
305         R12, R13,  SP,  PC,
306         P0,  VR,   P2,  P3,
307         P4,  CCR,  P6,  MOF,
308         P8,  IBR,  IRP, SRP,
309         BAR, DCCR, BRP, USP
310 };
311
312 /* The register sizes of the registers in register_name. An unimplemented register
313    is designated by size 0 in this array. */
314 static int register_size[] =
315 {
316         4, 4, 4, 4,
317         4, 4, 4, 4,
318         4, 4, 4, 4,
319         4, 4, 4, 4,
320         1, 1, 0, 0,
321         2, 2, 0, 4,
322         4, 4, 4, 4,
323         4, 4, 4, 4
324 };
325
326 /* Contains the register image of the executing thread in the assembler
327    part of the code in order to avoid horrible addressing modes. */
328 registers cris_reg;
329
330 /* FIXME: Should this be used? Delete otherwise. */
331 /* Contains the assumed consistency state of the register image. Uses the
332    enum error_type for state information. */
333 static int consistency_status = SUCCESS;
334
335 /********************************** Handle exceptions ************************/
336 /* The variable cris_reg contains the register image associated with the
337    current_thread_c variable. It is a complete register image created at
338    entry. The reg_g contains a register image of a task where the general
339    registers are taken from the stack and all special registers are taken
340    from the executing task. It is associated with current_thread_g and used
341    in order to provide access mainly for 'g', 'G' and 'P'.
342 */
343
344 #ifdef PROCESS_SUPPORT
345 /* Need two task id pointers in order to handle Hct and Hgt commands. */
346 static int current_thread_c = 0;
347 static int current_thread_g = 0;
348
349 /* Need two register images in order to handle Hct and Hgt commands. The
350    variable reg_g is in addition to cris_reg above. */
351 static registers reg_g;
352 #endif /* PROCESS_SUPPORT */
353
354 /********************************** Breakpoint *******************************/
355 /* Use an internal stack in the breakpoint and interrupt response routines */
356 #define INTERNAL_STACK_SIZE 1024
357 char internal_stack[INTERNAL_STACK_SIZE];
358
359 /* Due to the breakpoint return pointer, a state variable is needed to keep
360    track of whether it is a static (compiled) or dynamic (gdb-invoked)
361    breakpoint to be handled. A static breakpoint uses the content of register
362    BRP as it is whereas a dynamic breakpoint requires subtraction with 2
363    in order to execute the instruction. The first breakpoint is static. */
364 static unsigned char is_dyn_brkp = 0;
365
366 /********************************* String library ****************************/
367 /* Single-step over library functions creates trap loops. */
368
369 /* Copy char s2[] to s1[]. */
370 static char*
371 gdb_cris_strcpy (char *s1, const char *s2)
372 {
373         char *s = s1;
374         
375         for (s = s1; (*s++ = *s2++) != '\0'; )
376                 ;
377         return (s1);
378 }
379
380 /* Find length of s[]. */
381 static int
382 gdb_cris_strlen (const char *s)
383 {
384         const char *sc;
385         
386         for (sc = s; *sc != '\0'; sc++)
387                 ;
388         return (sc - s);
389 }
390
391 /* Find first occurrence of c in s[n]. */
392 static void*
393 gdb_cris_memchr (const void *s, int c, int n)
394 {
395         const unsigned char uc = c;
396         const unsigned char *su;
397         
398         for (su = s; 0 < n; ++su, --n)
399                 if (*su == uc)
400                         return ((void *)su);
401         return (NULL);
402 }
403 /******************************* Standard library ****************************/
404 /* Single-step over library functions creates trap loops. */
405 /* Convert string to long. */
406 static int
407 gdb_cris_strtol (const char *s, char **endptr, int base)
408 {
409         char *s1;
410         char *sd;
411         int x = 0;
412         
413         for (s1 = (char*)s; (sd = gdb_cris_memchr(hex_asc, *s1, base)) != NULL; ++s1)
414                 x = x * base + (sd - hex_asc);
415         
416         if (endptr)
417         {
418                 /* Unconverted suffix is stored in endptr unless endptr is NULL. */
419                 *endptr = s1;
420         }
421         
422         return x;
423 }
424
425 /********************************** Packet I/O ******************************/
426 /* Returns the integer equivalent of a hexadecimal character. */
427 static int
428 hex (char ch)
429 {
430         if ((ch >= 'a') && (ch <= 'f'))
431                 return (ch - 'a' + 10);
432         if ((ch >= '0') && (ch <= '9'))
433                 return (ch - '0');
434         if ((ch >= 'A') && (ch <= 'F'))
435                 return (ch - 'A' + 10);
436         return (-1);
437 }
438
439 /* Convert the memory, pointed to by mem into hexadecimal representation.
440    Put the result in buf, and return a pointer to the last character
441    in buf (null). */
442
443 static char *
444 mem2hex(char *buf, unsigned char *mem, int count)
445 {
446         int i;
447         int ch;
448         
449         if (mem == NULL) {
450                 /* Bogus read from m0. FIXME: What constitutes a valid address? */
451                 for (i = 0; i < count; i++) {
452                         *buf++ = '0';
453                         *buf++ = '0';
454                 }
455         } else {
456                 /* Valid mem address. */
457                 for (i = 0; i < count; i++) {
458                         ch = *mem++;
459                         buf = hex_byte_pack(buf, ch);
460                 }
461         }
462         
463         /* Terminate properly. */
464         *buf = '\0';
465         return (buf);
466 }
467
468 /* Convert the array, in hexadecimal representation, pointed to by buf into
469    binary representation. Put the result in mem, and return a pointer to
470    the character after the last byte written. */
471 static unsigned char*
472 hex2mem (unsigned char *mem, char *buf, int count)
473 {
474         int i;
475         unsigned char ch;
476         for (i = 0; i < count; i++) {
477                 ch = hex (*buf++) << 4;
478                 ch = ch + hex (*buf++);
479                 *mem++ = ch;
480         }
481         return (mem);
482 }
483
484 /* Put the content of the array, in binary representation, pointed to by buf
485    into memory pointed to by mem, and return a pointer to the character after
486    the last byte written.
487    Gdb will escape $, #, and the escape char (0x7d). */
488 static unsigned char*
489 bin2mem (unsigned char *mem, unsigned char *buf, int count)
490 {
491         int i;
492         unsigned char *next;
493         for (i = 0; i < count; i++) {
494                 /* Check for any escaped characters. Be paranoid and
495                    only unescape chars that should be escaped. */
496                 if (*buf == 0x7d) {
497                         next = buf + 1;
498                         if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */
499                                 {
500                                         buf++;
501                                         *buf += 0x20;
502                                 }
503                 }
504                 *mem++ = *buf++;
505         }
506         return (mem);
507 }
508
509 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
510    returned. */
511 static void
512 getpacket (char *buffer)
513 {
514         unsigned char checksum;
515         unsigned char xmitcsum;
516         int i;
517         int count;
518         char ch;
519         do {
520                 while ((ch = getDebugChar ()) != '$')
521                         /* Wait for the start character $ and ignore all other characters */;
522                 checksum = 0;
523                 xmitcsum = -1;
524                 count = 0;
525                 /* Read until a # or the end of the buffer is reached */
526                 while (count < BUFMAX - 1) {
527                         ch = getDebugChar ();
528                         if (ch == '#')
529                                 break;
530                         checksum = checksum + ch;
531                         buffer[count] = ch;
532                         count = count + 1;
533                 }
534                 buffer[count] = '\0';
535                 
536                 if (ch == '#') {
537                         xmitcsum = hex (getDebugChar ()) << 4;
538                         xmitcsum += hex (getDebugChar ());
539                         if (checksum != xmitcsum) {
540                                 /* Wrong checksum */
541                                 putDebugChar ('-');
542                         }
543                         else {
544                                 /* Correct checksum */
545                                 putDebugChar ('+');
546                                 /* If sequence characters are received, reply with them */
547                                 if (buffer[2] == ':') {
548                                         putDebugChar (buffer[0]);
549                                         putDebugChar (buffer[1]);
550                                         /* Remove the sequence characters from the buffer */
551                                         count = gdb_cris_strlen (buffer);
552                                         for (i = 3; i <= count; i++)
553                                                 buffer[i - 3] = buffer[i];
554                                 }
555                         }
556                 }
557         } while (checksum != xmitcsum);
558 }
559
560 /* Send $<data>#<checksum> from the <data> in the array buffer. */
561
562 static void
563 putpacket(char *buffer)
564 {
565         int checksum;
566         int runlen;
567         int encode;
568         
569         do {
570                 char *src = buffer;
571                 putDebugChar ('$');
572                 checksum = 0;
573                 while (*src) {
574                         /* Do run length encoding */
575                         putDebugChar (*src);
576                         checksum += *src;
577                         runlen = 0;
578                         while (runlen < RUNLENMAX && *src == src[runlen]) {
579                                 runlen++;
580                         }
581                         if (runlen > 3) {
582                                 /* Got a useful amount */
583                                 putDebugChar ('*');
584                                 checksum += '*';
585                                 encode = runlen + ' ' - 4;
586                                 putDebugChar (encode);
587                                 checksum += encode;
588                                 src += runlen;
589                         }
590                         else {
591                                 src++;
592                         }
593                 }
594                 putDebugChar('#');
595                 putDebugChar(hex_asc_hi(checksum));
596                 putDebugChar(hex_asc_lo(checksum));
597         } while(kgdb_started && (getDebugChar() != '+'));
598 }
599
600 /* The string str is prepended with the GDB printout token and sent. Required
601    in traditional implementations. */
602 void
603 putDebugString (const unsigned char *str, int length)
604 {
605         remcomOutBuffer[0] = 'O';
606         mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);
607         putpacket(remcomOutBuffer);
608 }
609
610 /********************************* Register image ****************************/
611 #ifdef PROCESS_SUPPORT
612 /* Copy the content of a register image into another. The size n is
613    the size of the register image. Due to struct assignment generation of
614    memcpy in libc. */
615 static void
616 copy_registers (registers *dptr, registers *sptr, int n)
617 {
618         unsigned char *dreg;
619         unsigned char *sreg;
620         
621         for (dreg = (unsigned char*)dptr, sreg = (unsigned char*)sptr; n > 0; n--)
622                 *dreg++ = *sreg++;
623 }
624
625 /* Copy the stored registers from the stack. Put the register contents
626    of thread thread_id in the struct reg. */
627 static void
628 copy_registers_from_stack (int thread_id, registers *regptr)
629 {
630         int j;
631         stack_registers *s = (stack_registers *)stack_list[thread_id];
632         unsigned int *d = (unsigned int *)regptr;
633         
634         for (j = 13; j >= 0; j--)
635                 *d++ = s->r[j];
636         regptr->sp = (unsigned int)stack_list[thread_id];
637         regptr->pc = s->pc;
638         regptr->dccr = s->dccr;
639         regptr->srp = s->srp;
640 }
641
642 /* Copy the registers to the stack. Put the register contents of thread
643    thread_id from struct reg to the stack. */
644 static void
645 copy_registers_to_stack (int thread_id, registers *regptr)
646 {
647         int i;
648         stack_registers *d = (stack_registers *)stack_list[thread_id];
649         unsigned int *s = (unsigned int *)regptr;
650         
651         for (i = 0; i < 14; i++) {
652                 d->r[i] = *s++;
653         }
654         d->pc = regptr->pc;
655         d->dccr = regptr->dccr;
656         d->srp = regptr->srp;
657 }
658 #endif
659
660 /* Write a value to a specified register in the register image of the current
661    thread. Returns status code SUCCESS, E02 or E05. */
662 static int
663 write_register (int regno, char *val)
664 {
665         int status = SUCCESS;
666         registers *current_reg = &cris_reg;
667
668         if (regno >= R0 && regno <= PC) {
669                 /* 32-bit register with simple offset. */
670                 hex2mem ((unsigned char *)current_reg + regno * sizeof(unsigned int),
671                          val, sizeof(unsigned int));
672         }
673         else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {
674                 /* Do not support read-only registers. */
675                 status = E02;
676         }
677         else if (regno == CCR) {
678                 /* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented, 
679                    and P7 (MOF) is 32 bits in ETRAX 100LX. */
680                 hex2mem ((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),
681                          val, sizeof(unsigned short));
682         }
683         else if (regno >= MOF && regno <= USP) {
684                 /* 32 bit register with complex offset.  (P8 has been taken care of.) */
685                 hex2mem ((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),
686                          val, sizeof(unsigned int));
687         } 
688         else {
689                 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
690                 status = E05;
691         }
692         return status;
693 }
694
695 #ifdef PROCESS_SUPPORT
696 /* Write a value to a specified register in the stack of a thread other
697    than the current thread. Returns status code SUCCESS or E07. */
698 static int
699 write_stack_register (int thread_id, int regno, char *valptr)
700 {
701         int status = SUCCESS;
702         stack_registers *d = (stack_registers *)stack_list[thread_id];
703         unsigned int val;
704         
705         hex2mem ((unsigned char *)&val, valptr, sizeof(unsigned int));
706         if (regno >= R0 && regno < SP) {
707                 d->r[regno] = val;
708         }
709         else if (regno == SP) {
710                 stack_list[thread_id] = val;
711         }
712         else if (regno == PC) {
713                 d->pc = val;
714         }
715         else if (regno == SRP) {
716                 d->srp = val;
717         }
718         else if (regno == DCCR) {
719                 d->dccr = val;
720         }
721         else {
722                 /* Do not support registers in the current thread. */
723                 status = E07;
724         }
725         return status;
726 }
727 #endif
728
729 /* Read a value from a specified register in the register image. Returns the
730    value in the register or -1 for non-implemented registers.
731    Should check consistency_status after a call which may be E05 after changes
732    in the implementation. */
733 static int
734 read_register (char regno, unsigned int *valptr)
735 {
736         registers *current_reg = &cris_reg;
737
738         if (regno >= R0 && regno <= PC) {
739                 /* 32-bit register with simple offset. */
740                 *valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));
741                 return SUCCESS;
742         }
743         else if (regno == P0 || regno == VR) {
744                 /* 8 bit register with complex offset. */
745                 *valptr = (unsigned int)(*(unsigned char *)
746                                          ((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));
747                 return SUCCESS;
748         }
749         else if (regno == P4 || regno == CCR) {
750                 /* 16 bit register with complex offset. */
751                 *valptr = (unsigned int)(*(unsigned short *)
752                                          ((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));
753                 return SUCCESS;
754         }
755         else if (regno >= MOF && regno <= USP) {
756                 /* 32 bit register with complex offset. */
757                 *valptr = *(unsigned int *)((char *)&(current_reg->p8)
758                                             + (regno-P8) * sizeof(unsigned int));
759                 return SUCCESS;
760         }
761         else {
762                 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
763                 consistency_status = E05;
764                 return E05;
765         }
766 }
767
768 /********************************** Handle exceptions ************************/
769 /* Build and send a response packet in order to inform the host the
770    stub is stopped. TAAn...:r...;n...:r...;n...:r...;
771                     AA = signal number
772                     n... = register number (hex)
773                     r... = register contents
774                     n... = `thread'
775                     r... = thread process ID.  This is a hex integer.
776                     n... = other string not starting with valid hex digit.
777                     gdb should ignore this n,r pair and go on to the next.
778                     This way we can extend the protocol. */
779 static void
780 stub_is_stopped(int sigval)
781 {
782         char *ptr = remcomOutBuffer;
783         int regno;
784
785         unsigned int reg_cont;
786         int status;
787         
788         /* Send trap type (converted to signal) */
789
790         *ptr++ = 'T';
791         ptr = hex_byte_pack(ptr, sigval);
792
793         /* Send register contents. We probably only need to send the
794          * PC, frame pointer and stack pointer here. Other registers will be
795          * explicitly asked for. But for now, send all.
796          */
797         
798         for (regno = R0; regno <= USP; regno++) {
799                 /* Store n...:r...; for the registers in the buffer. */
800
801                 status = read_register (regno, &reg_cont);
802                 
803                 if (status == SUCCESS) {
804                         ptr = hex_byte_pack(ptr, regno);
805                         *ptr++ = ':';
806
807                         ptr = mem2hex(ptr, (unsigned char *)&reg_cont,
808                                       register_size[regno]);
809                         *ptr++ = ';';
810                 }
811                 
812         }
813
814 #ifdef PROCESS_SUPPORT
815         /* Store the registers of the executing thread. Assume that both step,
816            continue, and register content requests are with respect to this
817            thread. The executing task is from the operating system scheduler. */
818
819         current_thread_c = executing_task;
820         current_thread_g = executing_task;
821
822         /* A struct assignment translates into a libc memcpy call. Avoid
823            all libc functions in order to prevent recursive break points. */
824         copy_registers (&reg_g, &cris_reg, sizeof(registers));
825
826         /* Store thread:r...; with the executing task TID. */
827         gdb_cris_strcpy (&remcomOutBuffer[pos], "thread:");
828         pos += gdb_cris_strlen ("thread:");
829         remcomOutBuffer[pos++] = hex_asc_hi(executing_task);
830         remcomOutBuffer[pos++] = hex_asc_lo(executing_task);
831         gdb_cris_strcpy (&remcomOutBuffer[pos], ";");
832 #endif
833
834         /* null-terminate and send it off */
835
836         *ptr = 0;
837
838         putpacket (remcomOutBuffer);
839 }
840
841 /* Performs a complete re-start from scratch. */
842 static void
843 kill_restart (void)
844 {
845         machine_restart("");
846 }
847
848 /* All expected commands are sent from remote.c. Send a response according
849    to the description in remote.c. */
850 void
851 handle_exception (int sigval)
852 {
853         /* Send response. */
854
855         stub_is_stopped (sigval);
856
857         for (;;) {
858                 remcomOutBuffer[0] = '\0';
859                 getpacket (remcomInBuffer);
860                 switch (remcomInBuffer[0]) {
861                         case 'g':
862                                 /* Read registers: g
863                                    Success: Each byte of register data is described by two hex digits.
864                                    Registers are in the internal order for GDB, and the bytes
865                                    in a register  are in the same order the machine uses.
866                                    Failure: void. */
867                                 
868                                 {
869 #ifdef PROCESS_SUPPORT
870                                         /* Use the special register content in the executing thread. */
871                                         copy_registers (&reg_g, &cris_reg, sizeof(registers));
872                                         /* Replace the content available on the stack. */
873                                         if (current_thread_g != executing_task) {
874                                                 copy_registers_from_stack (current_thread_g, &reg_g);
875                                         }
876                                         mem2hex ((unsigned char *)remcomOutBuffer, (unsigned char *)&reg_g, sizeof(registers));
877 #else
878                                         mem2hex(remcomOutBuffer, (char *)&cris_reg, sizeof(registers));
879 #endif
880                                 }
881                                 break;
882                                 
883                         case 'G':
884                                 /* Write registers. GXX..XX
885                                    Each byte of register data  is described by two hex digits.
886                                    Success: OK
887                                    Failure: void. */
888 #ifdef PROCESS_SUPPORT
889                                 hex2mem ((unsigned char *)&reg_g, &remcomInBuffer[1], sizeof(registers));
890                                 if (current_thread_g == executing_task) {
891                                         copy_registers (&cris_reg, &reg_g, sizeof(registers));
892                                 }
893                                 else {
894                                         copy_registers_to_stack(current_thread_g, &reg_g);
895                                 }
896 #else
897                                 hex2mem((char *)&cris_reg, &remcomInBuffer[1], sizeof(registers));
898 #endif
899                                 gdb_cris_strcpy (remcomOutBuffer, "OK");
900                                 break;
901                                 
902                         case 'P':
903                                 /* Write register. Pn...=r...
904                                    Write register n..., hex value without 0x, with value r...,
905                                    which contains a hex value without 0x and two hex digits
906                                    for each byte in the register (target byte order). P1f=11223344 means
907                                    set register 31 to 44332211.
908                                    Success: OK
909                                    Failure: E02, E05 */
910                                 {
911                                         char *suffix;
912                                         int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);
913                                         int status;
914 #ifdef PROCESS_SUPPORT
915                                         if (current_thread_g != executing_task)
916                                                 status = write_stack_register (current_thread_g, regno, suffix+1);
917                                         else
918 #endif
919                                                 status = write_register (regno, suffix+1);
920
921                                         switch (status) {
922                                                 case E02:
923                                                         /* Do not support read-only registers. */
924                                                         gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);
925                                                         break;
926                                                 case E05:
927                                                         /* Do not support non-existing registers. */
928                                                         gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);
929                                                         break;
930                                                 case E07:
931                                                         /* Do not support non-existing registers on the stack. */
932                                                         gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);
933                                                         break;
934                                                 default:
935                                                         /* Valid register number. */
936                                                         gdb_cris_strcpy (remcomOutBuffer, "OK");
937                                                         break;
938                                         }
939                                 }
940                                 break;
941                                 
942                         case 'm':
943                                 /* Read from memory. mAA..AA,LLLL
944                                    AA..AA is the address and LLLL is the length.
945                                    Success: XX..XX is the memory content.  Can be fewer bytes than
946                                    requested if only part of the data may be read. m6000120a,6c means
947                                    retrieve 108 byte from base address 6000120a.
948                                    Failure: void. */
949                                 {
950                                         char *suffix;
951                                         unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
952                                                                                                &suffix, 16);                                        int length = gdb_cris_strtol(suffix+1, 0, 16);
953                                         
954                                         mem2hex(remcomOutBuffer, addr, length);
955                                 }
956                                 break;
957                                 
958                         case 'X':
959                                 /* Write to memory. XAA..AA,LLLL:XX..XX
960                                    AA..AA is the start address,  LLLL is the number of bytes, and
961                                    XX..XX is the binary data.
962                                    Success: OK
963                                    Failure: void. */
964                         case 'M':
965                                 /* Write to memory. MAA..AA,LLLL:XX..XX
966                                    AA..AA is the start address,  LLLL is the number of bytes, and
967                                    XX..XX is the hexadecimal data.
968                                    Success: OK
969                                    Failure: void. */
970                                 {
971                                         char *lenptr;
972                                         char *dataptr;
973                                         unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
974                                                                                       &lenptr, 16);
975                                         int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);
976                                         if (*lenptr == ',' && *dataptr == ':') {
977                                                 if (remcomInBuffer[0] == 'M') {
978                                                         hex2mem(addr, dataptr + 1, length);
979                                                 }
980                                                 else /* X */ {
981                                                         bin2mem(addr, dataptr + 1, length);
982                                                 }
983                                                 gdb_cris_strcpy (remcomOutBuffer, "OK");
984                                         }
985                                         else {
986                                                 gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);
987                                         }
988                                 }
989                                 break;
990                                 
991                         case 'c':
992                                 /* Continue execution. cAA..AA
993                                    AA..AA is the address where execution is resumed. If AA..AA is
994                                    omitted, resume at the present address.
995                                    Success: return to the executing thread.
996                                    Failure: will never know. */
997                                 if (remcomInBuffer[1] != '\0') {
998                                         cris_reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
999                                 }
1000                                 enableDebugIRQ();
1001                                 return;
1002                                 
1003                         case 's':
1004                                 /* Step. sAA..AA
1005                                    AA..AA is the address where execution is resumed. If AA..AA is
1006                                    omitted, resume at the present address. Success: return to the
1007                                    executing thread. Failure: will never know.
1008                                    
1009                                    Should never be invoked. The single-step is implemented on
1010                                    the host side. If ever invoked, it is an internal error E04. */
1011                                 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1012                                 putpacket (remcomOutBuffer);
1013                                 return;
1014                                 
1015                         case '?':
1016                                 /* The last signal which caused a stop. ?
1017                                    Success: SAA, where AA is the signal number.
1018                                    Failure: void. */
1019                                 remcomOutBuffer[0] = 'S';
1020                                 remcomOutBuffer[1] = hex_asc_hi(sigval);
1021                                 remcomOutBuffer[2] = hex_asc_lo(sigval);
1022                                 remcomOutBuffer[3] = 0;
1023                                 break;
1024                                 
1025                         case 'D':
1026                                 /* Detach from host. D
1027                                    Success: OK, and return to the executing thread.
1028                                    Failure: will never know */
1029                                 putpacket ("OK");
1030                                 return;
1031                                 
1032                         case 'k':
1033                         case 'r':
1034                                 /* kill request or reset request.
1035                                    Success: restart of target.
1036                                    Failure: will never know. */
1037                                 kill_restart ();
1038                                 break;
1039                                 
1040                         case 'C':
1041                         case 'S':
1042                         case '!':
1043                         case 'R':
1044                         case 'd':
1045                                 /* Continue with signal sig. Csig;AA..AA
1046                                    Step with signal sig. Ssig;AA..AA
1047                                    Use the extended remote protocol. !
1048                                    Restart the target system. R0
1049                                    Toggle debug flag. d
1050                                    Search backwards. tAA:PP,MM
1051                                    Not supported: E04 */
1052                                 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1053                                 break;
1054 #ifdef PROCESS_SUPPORT
1055
1056                         case 'T':
1057                                 /* Thread alive. TXX
1058                                    Is thread XX alive?
1059                                    Success: OK, thread XX is alive.
1060                                    Failure: E03, thread XX is dead. */
1061                                 {
1062                                         int thread_id = (int)gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
1063                                         /* Cannot tell whether it is alive or not. */
1064                                         if (thread_id >= 0 && thread_id < number_of_tasks)
1065                                                 gdb_cris_strcpy (remcomOutBuffer, "OK");
1066                                 }
1067                                 break;
1068                                                                 
1069                         case 'H':
1070                                 /* Set thread for subsequent operations: Hct
1071                                    c = 'c' for thread used in step and continue;
1072                                    t can be -1 for all threads.
1073                                    c = 'g' for thread used in other  operations.
1074                                    t = 0 means pick any thread.
1075                                    Success: OK
1076                                    Failure: E01 */
1077                                 {
1078                                         int thread_id = gdb_cris_strtol (&remcomInBuffer[2], 0, 16);
1079                                         if (remcomInBuffer[1] == 'c') {
1080                                                 /* c = 'c' for thread used in step and continue */
1081                                                 /* Do not change current_thread_c here. It would create a mess in
1082                                                    the scheduler. */
1083                                                 gdb_cris_strcpy (remcomOutBuffer, "OK");
1084                                         }
1085                                         else if (remcomInBuffer[1] == 'g') {
1086                                                 /* c = 'g' for thread used in other  operations.
1087                                                    t = 0 means pick any thread. Impossible since the scheduler does
1088                                                    not allow that. */
1089                                                 if (thread_id >= 0 && thread_id < number_of_tasks) {
1090                                                         current_thread_g = thread_id;
1091                                                         gdb_cris_strcpy (remcomOutBuffer, "OK");
1092                                                 }
1093                                                 else {
1094                                                         /* Not expected - send an error message. */
1095                                                         gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1096                                                 }
1097                                         }
1098                                         else {
1099                                                 /* Not expected - send an error message. */
1100                                                 gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1101                                         }
1102                                 }
1103                                 break;
1104                                 
1105                         case 'q':
1106                         case 'Q':
1107                                 /* Query of general interest. qXXXX
1108                                    Set general value XXXX. QXXXX=yyyy */
1109                                 {
1110                                         int pos;
1111                                         int nextpos;
1112                                         int thread_id;
1113                                         
1114                                         switch (remcomInBuffer[1]) {
1115                                                 case 'C':
1116                                                         /* Identify the remote current thread. */
1117                                                         gdb_cris_strcpy (&remcomOutBuffer[0], "QC");
1118                                                         remcomOutBuffer[2] = hex_asc_hi(current_thread_c);
1119                                                         remcomOutBuffer[3] = hex_asc_lo(current_thread_c);
1120                                                         remcomOutBuffer[4] = '\0';
1121                                                         break;
1122                                                 case 'L':
1123                                                         gdb_cris_strcpy (&remcomOutBuffer[0], "QM");
1124                                                         /* Reply with number of threads. */
1125                                                         if (os_is_started()) {
1126                                                                 remcomOutBuffer[2] = hex_asc_hi(number_of_tasks);
1127                                                                 remcomOutBuffer[3] = hex_asc_lo(number_of_tasks);
1128                                                         }
1129                                                         else {
1130                                                                 remcomOutBuffer[2] = hex_asc_hi(0);
1131                                                                 remcomOutBuffer[3] = hex_asc_lo(1);
1132                                                         }
1133                                                         /* Done with the reply. */
1134                                                         remcomOutBuffer[4] = hex_asc_lo(1);
1135                                                         pos = 5;
1136                                                         /* Expects the argument thread id. */
1137                                                         for (; pos < (5 + HEXCHARS_IN_THREAD_ID); pos++)
1138                                                                 remcomOutBuffer[pos] = remcomInBuffer[pos];
1139                                                         /* Reply with the thread identifiers. */
1140                                                         if (os_is_started()) {
1141                                                                 /* Store the thread identifiers of all tasks. */
1142                                                                 for (thread_id = 0; thread_id < number_of_tasks; thread_id++) {
1143                                                                         nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1144                                                                         for (; pos < nextpos; pos ++)
1145                                                                                 remcomOutBuffer[pos] = hex_asc_lo(0);
1146                                                                         remcomOutBuffer[pos++] = hex_asc_lo(thread_id);
1147                                                                 }
1148                                                         }
1149                                                         else {
1150                                                                 /* Store the thread identifier of the boot task. */
1151                                                                 nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1152                                                                 for (; pos < nextpos; pos ++)
1153                                                                         remcomOutBuffer[pos] = hex_asc_lo(0);
1154                                                                 remcomOutBuffer[pos++] = hex_asc_lo(current_thread_c);
1155                                                         }
1156                                                         remcomOutBuffer[pos] = '\0';
1157                                                         break;
1158                                                 default:
1159                                                         /* Not supported: "" */
1160                                                         /* Request information about section offsets: qOffsets. */
1161                                                         remcomOutBuffer[0] = 0;
1162                                                         break;
1163                                         }
1164                                 }
1165                                 break;
1166 #endif /* PROCESS_SUPPORT */
1167                                 
1168                         default:
1169                                 /* The stub should ignore other request and send an empty
1170                                    response ($#<checksum>). This way we can extend the protocol and GDB
1171                                    can tell whether the stub it is talking to uses the old or the new. */
1172                                 remcomOutBuffer[0] = 0;
1173                                 break;
1174                 }
1175                 putpacket(remcomOutBuffer);
1176         }
1177 }
1178
1179 /********************************** Breakpoint *******************************/
1180 /* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.
1181    An internal stack is used by the stub. The register image of the caller is
1182    stored in the structure register_image.
1183    Interactive communication with the host is handled by handle_exception and
1184    finally the register image is restored. */
1185
1186 void kgdb_handle_breakpoint(void);
1187
1188 asm ("\n"
1189 "  .global kgdb_handle_breakpoint\n"
1190 "kgdb_handle_breakpoint:\n"
1191 ";;\n"
1192 ";; Response to the break-instruction\n"
1193 ";;\n"
1194 ";; Create a register image of the caller\n"
1195 ";;\n"
1196 "  move     $dccr,[cris_reg+0x5E] ; Save the flags in DCCR before disable interrupts\n"
1197 "  di                        ; Disable interrupts\n"
1198 "  move.d   $r0,[cris_reg]        ; Save R0\n"
1199 "  move.d   $r1,[cris_reg+0x04]   ; Save R1\n"
1200 "  move.d   $r2,[cris_reg+0x08]   ; Save R2\n"
1201 "  move.d   $r3,[cris_reg+0x0C]   ; Save R3\n"
1202 "  move.d   $r4,[cris_reg+0x10]   ; Save R4\n"
1203 "  move.d   $r5,[cris_reg+0x14]   ; Save R5\n"
1204 "  move.d   $r6,[cris_reg+0x18]   ; Save R6\n"
1205 "  move.d   $r7,[cris_reg+0x1C]   ; Save R7\n"
1206 "  move.d   $r8,[cris_reg+0x20]   ; Save R8\n"
1207 "  move.d   $r9,[cris_reg+0x24]   ; Save R9\n"
1208 "  move.d   $r10,[cris_reg+0x28]  ; Save R10\n"
1209 "  move.d   $r11,[cris_reg+0x2C]  ; Save R11\n"
1210 "  move.d   $r12,[cris_reg+0x30]  ; Save R12\n"
1211 "  move.d   $r13,[cris_reg+0x34]  ; Save R13\n"
1212 "  move.d   $sp,[cris_reg+0x38]   ; Save SP (R14)\n"
1213 ";; Due to the old assembler-versions BRP might not be recognized\n"
1214 "  .word 0xE670              ; move brp,$r0\n"
1215 "  subq     2,$r0             ; Set to address of previous instruction.\n"
1216 "  move.d   $r0,[cris_reg+0x3c]   ; Save the address in PC (R15)\n"
1217 "  clear.b  [cris_reg+0x40]      ; Clear P0\n"
1218 "  move     $vr,[cris_reg+0x41]   ; Save special register P1\n"
1219 "  clear.w  [cris_reg+0x42]      ; Clear P4\n"
1220 "  move     $ccr,[cris_reg+0x44]  ; Save special register CCR\n"
1221 "  move     $mof,[cris_reg+0x46]  ; P7\n"
1222 "  clear.d  [cris_reg+0x4A]      ; Clear P8\n"
1223 "  move     $ibr,[cris_reg+0x4E]  ; P9,\n"
1224 "  move     $irp,[cris_reg+0x52]  ; P10,\n"
1225 "  move     $srp,[cris_reg+0x56]  ; P11,\n"
1226 "  move     $dtp0,[cris_reg+0x5A] ; P12, register BAR, assembler might not know BAR\n"
1227 "                            ; P13, register DCCR already saved\n"
1228 ";; Due to the old assembler-versions BRP might not be recognized\n"
1229 "  .word 0xE670              ; move brp,r0\n"
1230 ";; Static (compiled) breakpoints must return to the next instruction in order\n"
1231 ";; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction\n"
1232 ";; in order to execute it when execution is continued.\n"
1233 "  test.b   [is_dyn_brkp]    ; Is this a dynamic breakpoint?\n"
1234 "  beq      is_static         ; No, a static breakpoint\n"
1235 "  nop\n"
1236 "  subq     2,$r0              ; rerun the instruction the break replaced\n"
1237 "is_static:\n"
1238 "  moveq    1,$r1\n"
1239 "  move.b   $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint\n"
1240 "  move.d   $r0,[cris_reg+0x62]    ; Save the return address in BRP\n"
1241 "  move     $usp,[cris_reg+0x66]   ; USP\n"
1242 ";;\n"
1243 ";; Handle the communication\n"
1244 ";;\n"
1245 "  move.d   internal_stack+1020,$sp ; Use the internal stack which grows upward\n"
1246 "  moveq    5,$r10                   ; SIGTRAP\n"
1247 "  jsr      handle_exception       ; Interactive routine\n"
1248 ";;\n"
1249 ";; Return to the caller\n"
1250 ";;\n"
1251 "   move.d  [cris_reg],$r0         ; Restore R0\n"
1252 "   move.d  [cris_reg+0x04],$r1    ; Restore R1\n"
1253 "   move.d  [cris_reg+0x08],$r2    ; Restore R2\n"
1254 "   move.d  [cris_reg+0x0C],$r3    ; Restore R3\n"
1255 "   move.d  [cris_reg+0x10],$r4    ; Restore R4\n"
1256 "   move.d  [cris_reg+0x14],$r5    ; Restore R5\n"
1257 "   move.d  [cris_reg+0x18],$r6    ; Restore R6\n"
1258 "   move.d  [cris_reg+0x1C],$r7    ; Restore R7\n"
1259 "   move.d  [cris_reg+0x20],$r8    ; Restore R8\n"
1260 "   move.d  [cris_reg+0x24],$r9    ; Restore R9\n"
1261 "   move.d  [cris_reg+0x28],$r10   ; Restore R10\n"
1262 "   move.d  [cris_reg+0x2C],$r11   ; Restore R11\n"
1263 "   move.d  [cris_reg+0x30],$r12   ; Restore R12\n"
1264 "   move.d  [cris_reg+0x34],$r13   ; Restore R13\n"
1265 ";;\n"
1266 ";; FIXME: Which registers should be restored?\n"
1267 ";;\n"
1268 "   move.d  [cris_reg+0x38],$sp    ; Restore SP (R14)\n"
1269 "   move    [cris_reg+0x56],$srp   ; Restore the subroutine return pointer.\n"
1270 "   move    [cris_reg+0x5E],$dccr  ; Restore DCCR\n"
1271 "   move    [cris_reg+0x66],$usp   ; Restore USP\n"
1272 "   jump    [cris_reg+0x62]       ; A jump to the content in register BRP works.\n"
1273 "   nop                       ;\n"
1274 "\n");
1275
1276 /* The hook for an interrupt generated by GDB. An internal stack is used
1277    by the stub. The register image of the caller is stored in the structure
1278    register_image. Interactive communication with the host is handled by
1279    handle_exception and finally the register image is restored. Due to the
1280    old assembler which does not recognise the break instruction and the
1281    breakpoint return pointer hex-code is used. */
1282
1283 void kgdb_handle_serial(void);
1284
1285 asm ("\n"
1286 "  .global kgdb_handle_serial\n"
1287 "kgdb_handle_serial:\n"
1288 ";;\n"
1289 ";; Response to a serial interrupt\n"
1290 ";;\n"
1291 "\n"
1292 "  move     $dccr,[cris_reg+0x5E] ; Save the flags in DCCR\n"
1293 "  di                        ; Disable interrupts\n"
1294 "  move.d   $r0,[cris_reg]        ; Save R0\n"
1295 "  move.d   $r1,[cris_reg+0x04]   ; Save R1\n"
1296 "  move.d   $r2,[cris_reg+0x08]   ; Save R2\n"
1297 "  move.d   $r3,[cris_reg+0x0C]   ; Save R3\n"
1298 "  move.d   $r4,[cris_reg+0x10]   ; Save R4\n"
1299 "  move.d   $r5,[cris_reg+0x14]   ; Save R5\n"
1300 "  move.d   $r6,[cris_reg+0x18]   ; Save R6\n"
1301 "  move.d   $r7,[cris_reg+0x1C]   ; Save R7\n"
1302 "  move.d   $r8,[cris_reg+0x20]   ; Save R8\n"
1303 "  move.d   $r9,[cris_reg+0x24]   ; Save R9\n"
1304 "  move.d   $r10,[cris_reg+0x28]  ; Save R10\n"
1305 "  move.d   $r11,[cris_reg+0x2C]  ; Save R11\n"
1306 "  move.d   $r12,[cris_reg+0x30]  ; Save R12\n"
1307 "  move.d   $r13,[cris_reg+0x34]  ; Save R13\n"
1308 "  move.d   $sp,[cris_reg+0x38]   ; Save SP (R14)\n"
1309 "  move     $irp,[cris_reg+0x3c]  ; Save the address in PC (R15)\n"
1310 "  clear.b  [cris_reg+0x40]      ; Clear P0\n"
1311 "  move     $vr,[cris_reg+0x41]   ; Save special register P1,\n"
1312 "  clear.w  [cris_reg+0x42]      ; Clear P4\n"
1313 "  move     $ccr,[cris_reg+0x44]  ; Save special register CCR\n"
1314 "  move     $mof,[cris_reg+0x46]  ; P7\n"
1315 "  clear.d  [cris_reg+0x4A]      ; Clear P8\n"
1316 "  move     $ibr,[cris_reg+0x4E]  ; P9,\n"
1317 "  move     $irp,[cris_reg+0x52]  ; P10,\n"
1318 "  move     $srp,[cris_reg+0x56]  ; P11,\n"
1319 "  move     $dtp0,[cris_reg+0x5A] ; P12, register BAR, assembler might not know BAR\n"
1320 "                            ; P13, register DCCR already saved\n"
1321 ";; Due to the old assembler-versions BRP might not be recognized\n"
1322 "  .word 0xE670              ; move brp,r0\n"
1323 "  move.d   $r0,[cris_reg+0x62]   ; Save the return address in BRP\n"
1324 "  move     $usp,[cris_reg+0x66]  ; USP\n"
1325 "\n"
1326 ";; get the serial character (from debugport.c) and check if it is a ctrl-c\n"
1327 "\n"
1328 "  jsr getDebugChar\n"
1329 "  cmp.b 3, $r10\n"
1330 "  bne goback\n"
1331 "  nop\n"
1332 "\n"
1333 "  move.d  [cris_reg+0x5E], $r10                ; Get DCCR\n"
1334 "  btstq           8, $r10                      ; Test the U-flag.\n"
1335 "  bmi     goback\n"
1336 "  nop\n"
1337 "\n"
1338 ";;\n"
1339 ";; Handle the communication\n"
1340 ";;\n"
1341 "  move.d   internal_stack+1020,$sp ; Use the internal stack\n"
1342 "  moveq    2,$r10                   ; SIGINT\n"
1343 "  jsr      handle_exception       ; Interactive routine\n"
1344 "\n"
1345 "goback:\n"
1346 ";;\n"
1347 ";; Return to the caller\n"
1348 ";;\n"
1349 "   move.d  [cris_reg],$r0         ; Restore R0\n"
1350 "   move.d  [cris_reg+0x04],$r1    ; Restore R1\n"
1351 "   move.d  [cris_reg+0x08],$r2    ; Restore R2\n"
1352 "   move.d  [cris_reg+0x0C],$r3    ; Restore R3\n"
1353 "   move.d  [cris_reg+0x10],$r4    ; Restore R4\n"
1354 "   move.d  [cris_reg+0x14],$r5    ; Restore R5\n"
1355 "   move.d  [cris_reg+0x18],$r6    ; Restore R6\n"
1356 "   move.d  [cris_reg+0x1C],$r7    ; Restore R7\n"
1357 "   move.d  [cris_reg+0x20],$r8    ; Restore R8\n"
1358 "   move.d  [cris_reg+0x24],$r9    ; Restore R9\n"
1359 "   move.d  [cris_reg+0x28],$r10   ; Restore R10\n"
1360 "   move.d  [cris_reg+0x2C],$r11   ; Restore R11\n"
1361 "   move.d  [cris_reg+0x30],$r12   ; Restore R12\n"
1362 "   move.d  [cris_reg+0x34],$r13   ; Restore R13\n"
1363 ";;\n"
1364 ";; FIXME: Which registers should be restored?\n"
1365 ";;\n"
1366 "   move.d  [cris_reg+0x38],$sp    ; Restore SP (R14)\n"
1367 "   move    [cris_reg+0x56],$srp   ; Restore the subroutine return pointer.\n"
1368 "   move    [cris_reg+0x5E],$dccr  ; Restore DCCR\n"
1369 "   move    [cris_reg+0x66],$usp   ; Restore USP\n"
1370 "   reti                      ; Return from the interrupt routine\n"
1371 "   nop\n"
1372 "\n");
1373
1374 /* Use this static breakpoint in the start-up only. */
1375
1376 void
1377 breakpoint(void)
1378 {
1379         kgdb_started = 1;
1380         is_dyn_brkp = 0;     /* This is a static, not a dynamic breakpoint. */
1381         __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */
1382 }
1383
1384 /* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */
1385
1386 void
1387 kgdb_init(void)
1388 {
1389         /* could initialize debug port as well but it's done in head.S already... */
1390
1391         /* breakpoint handler is now set in irq.c */
1392         set_int_vector(8, kgdb_handle_serial);
1393         
1394         enableDebugIRQ();
1395 }
1396
1397 /****************************** End of file **********************************/