]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/cris/arch-v10/kernel/kgdb.c
Merge tag 'remoteproc-3.11-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git...
[karo-tx-linux.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 /********************************** Breakpoint *******************************/
345 /* Use an internal stack in the breakpoint and interrupt response routines */
346 #define INTERNAL_STACK_SIZE 1024
347 char internal_stack[INTERNAL_STACK_SIZE];
348
349 /* Due to the breakpoint return pointer, a state variable is needed to keep
350    track of whether it is a static (compiled) or dynamic (gdb-invoked)
351    breakpoint to be handled. A static breakpoint uses the content of register
352    BRP as it is whereas a dynamic breakpoint requires subtraction with 2
353    in order to execute the instruction. The first breakpoint is static. */
354 static unsigned char is_dyn_brkp = 0;
355
356 /********************************* String library ****************************/
357 /* Single-step over library functions creates trap loops. */
358
359 /* Copy char s2[] to s1[]. */
360 static char*
361 gdb_cris_strcpy (char *s1, const char *s2)
362 {
363         char *s = s1;
364         
365         for (s = s1; (*s++ = *s2++) != '\0'; )
366                 ;
367         return (s1);
368 }
369
370 /* Find length of s[]. */
371 static int
372 gdb_cris_strlen (const char *s)
373 {
374         const char *sc;
375         
376         for (sc = s; *sc != '\0'; sc++)
377                 ;
378         return (sc - s);
379 }
380
381 /* Find first occurrence of c in s[n]. */
382 static void*
383 gdb_cris_memchr (const void *s, int c, int n)
384 {
385         const unsigned char uc = c;
386         const unsigned char *su;
387         
388         for (su = s; 0 < n; ++su, --n)
389                 if (*su == uc)
390                         return ((void *)su);
391         return (NULL);
392 }
393 /******************************* Standard library ****************************/
394 /* Single-step over library functions creates trap loops. */
395 /* Convert string to long. */
396 static int
397 gdb_cris_strtol (const char *s, char **endptr, int base)
398 {
399         char *s1;
400         char *sd;
401         int x = 0;
402         
403         for (s1 = (char*)s; (sd = gdb_cris_memchr(hex_asc, *s1, base)) != NULL; ++s1)
404                 x = x * base + (sd - hex_asc);
405         
406         if (endptr)
407         {
408                 /* Unconverted suffix is stored in endptr unless endptr is NULL. */
409                 *endptr = s1;
410         }
411         
412         return x;
413 }
414
415 /********************************** Packet I/O ******************************/
416 /* Returns the integer equivalent of a hexadecimal character. */
417 static int
418 hex (char ch)
419 {
420         if ((ch >= 'a') && (ch <= 'f'))
421                 return (ch - 'a' + 10);
422         if ((ch >= '0') && (ch <= '9'))
423                 return (ch - '0');
424         if ((ch >= 'A') && (ch <= 'F'))
425                 return (ch - 'A' + 10);
426         return (-1);
427 }
428
429 /* Convert the memory, pointed to by mem into hexadecimal representation.
430    Put the result in buf, and return a pointer to the last character
431    in buf (null). */
432
433 static char *
434 mem2hex(char *buf, unsigned char *mem, int count)
435 {
436         int i;
437         int ch;
438         
439         if (mem == NULL) {
440                 /* Bogus read from m0. FIXME: What constitutes a valid address? */
441                 for (i = 0; i < count; i++) {
442                         *buf++ = '0';
443                         *buf++ = '0';
444                 }
445         } else {
446                 /* Valid mem address. */
447                 for (i = 0; i < count; i++) {
448                         ch = *mem++;
449                         buf = hex_byte_pack(buf, ch);
450                 }
451         }
452         
453         /* Terminate properly. */
454         *buf = '\0';
455         return (buf);
456 }
457
458 /* Convert the array, in hexadecimal representation, pointed to by buf into
459    binary representation. Put the result in mem, and return a pointer to
460    the character after the last byte written. */
461 static unsigned char*
462 hex2mem (unsigned char *mem, char *buf, int count)
463 {
464         int i;
465         unsigned char ch;
466         for (i = 0; i < count; i++) {
467                 ch = hex (*buf++) << 4;
468                 ch = ch + hex (*buf++);
469                 *mem++ = ch;
470         }
471         return (mem);
472 }
473
474 /* Put the content of the array, in binary representation, pointed to by buf
475    into memory pointed to by mem, and return a pointer to the character after
476    the last byte written.
477    Gdb will escape $, #, and the escape char (0x7d). */
478 static unsigned char*
479 bin2mem (unsigned char *mem, unsigned char *buf, int count)
480 {
481         int i;
482         unsigned char *next;
483         for (i = 0; i < count; i++) {
484                 /* Check for any escaped characters. Be paranoid and
485                    only unescape chars that should be escaped. */
486                 if (*buf == 0x7d) {
487                         next = buf + 1;
488                         if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */
489                                 {
490                                         buf++;
491                                         *buf += 0x20;
492                                 }
493                 }
494                 *mem++ = *buf++;
495         }
496         return (mem);
497 }
498
499 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
500    returned. */
501 static void
502 getpacket (char *buffer)
503 {
504         unsigned char checksum;
505         unsigned char xmitcsum;
506         int i;
507         int count;
508         char ch;
509         do {
510                 while ((ch = getDebugChar ()) != '$')
511                         /* Wait for the start character $ and ignore all other characters */;
512                 checksum = 0;
513                 xmitcsum = -1;
514                 count = 0;
515                 /* Read until a # or the end of the buffer is reached */
516                 while (count < BUFMAX - 1) {
517                         ch = getDebugChar ();
518                         if (ch == '#')
519                                 break;
520                         checksum = checksum + ch;
521                         buffer[count] = ch;
522                         count = count + 1;
523                 }
524                 buffer[count] = '\0';
525                 
526                 if (ch == '#') {
527                         xmitcsum = hex (getDebugChar ()) << 4;
528                         xmitcsum += hex (getDebugChar ());
529                         if (checksum != xmitcsum) {
530                                 /* Wrong checksum */
531                                 putDebugChar ('-');
532                         }
533                         else {
534                                 /* Correct checksum */
535                                 putDebugChar ('+');
536                                 /* If sequence characters are received, reply with them */
537                                 if (buffer[2] == ':') {
538                                         putDebugChar (buffer[0]);
539                                         putDebugChar (buffer[1]);
540                                         /* Remove the sequence characters from the buffer */
541                                         count = gdb_cris_strlen (buffer);
542                                         for (i = 3; i <= count; i++)
543                                                 buffer[i - 3] = buffer[i];
544                                 }
545                         }
546                 }
547         } while (checksum != xmitcsum);
548 }
549
550 /* Send $<data>#<checksum> from the <data> in the array buffer. */
551
552 static void
553 putpacket(char *buffer)
554 {
555         int checksum;
556         int runlen;
557         int encode;
558         
559         do {
560                 char *src = buffer;
561                 putDebugChar ('$');
562                 checksum = 0;
563                 while (*src) {
564                         /* Do run length encoding */
565                         putDebugChar (*src);
566                         checksum += *src;
567                         runlen = 0;
568                         while (runlen < RUNLENMAX && *src == src[runlen]) {
569                                 runlen++;
570                         }
571                         if (runlen > 3) {
572                                 /* Got a useful amount */
573                                 putDebugChar ('*');
574                                 checksum += '*';
575                                 encode = runlen + ' ' - 4;
576                                 putDebugChar (encode);
577                                 checksum += encode;
578                                 src += runlen;
579                         }
580                         else {
581                                 src++;
582                         }
583                 }
584                 putDebugChar('#');
585                 putDebugChar(hex_asc_hi(checksum));
586                 putDebugChar(hex_asc_lo(checksum));
587         } while(kgdb_started && (getDebugChar() != '+'));
588 }
589
590 /* The string str is prepended with the GDB printout token and sent. Required
591    in traditional implementations. */
592 void
593 putDebugString (const unsigned char *str, int length)
594 {
595         remcomOutBuffer[0] = 'O';
596         mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);
597         putpacket(remcomOutBuffer);
598 }
599
600 /********************************* Register image ****************************/
601 /* Write a value to a specified register in the register image of the current
602    thread. Returns status code SUCCESS, E02 or E05. */
603 static int
604 write_register (int regno, char *val)
605 {
606         int status = SUCCESS;
607         registers *current_reg = &cris_reg;
608
609         if (regno >= R0 && regno <= PC) {
610                 /* 32-bit register with simple offset. */
611                 hex2mem ((unsigned char *)current_reg + regno * sizeof(unsigned int),
612                          val, sizeof(unsigned int));
613         }
614         else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {
615                 /* Do not support read-only registers. */
616                 status = E02;
617         }
618         else if (regno == CCR) {
619                 /* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented, 
620                    and P7 (MOF) is 32 bits in ETRAX 100LX. */
621                 hex2mem ((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),
622                          val, sizeof(unsigned short));
623         }
624         else if (regno >= MOF && regno <= USP) {
625                 /* 32 bit register with complex offset.  (P8 has been taken care of.) */
626                 hex2mem ((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),
627                          val, sizeof(unsigned int));
628         } 
629         else {
630                 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
631                 status = E05;
632         }
633         return status;
634 }
635
636 /* Read a value from a specified register in the register image. Returns the
637    value in the register or -1 for non-implemented registers.
638    Should check consistency_status after a call which may be E05 after changes
639    in the implementation. */
640 static int
641 read_register (char regno, unsigned int *valptr)
642 {
643         registers *current_reg = &cris_reg;
644
645         if (regno >= R0 && regno <= PC) {
646                 /* 32-bit register with simple offset. */
647                 *valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));
648                 return SUCCESS;
649         }
650         else if (regno == P0 || regno == VR) {
651                 /* 8 bit register with complex offset. */
652                 *valptr = (unsigned int)(*(unsigned char *)
653                                          ((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));
654                 return SUCCESS;
655         }
656         else if (regno == P4 || regno == CCR) {
657                 /* 16 bit register with complex offset. */
658                 *valptr = (unsigned int)(*(unsigned short *)
659                                          ((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));
660                 return SUCCESS;
661         }
662         else if (regno >= MOF && regno <= USP) {
663                 /* 32 bit register with complex offset. */
664                 *valptr = *(unsigned int *)((char *)&(current_reg->p8)
665                                             + (regno-P8) * sizeof(unsigned int));
666                 return SUCCESS;
667         }
668         else {
669                 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
670                 consistency_status = E05;
671                 return E05;
672         }
673 }
674
675 /********************************** Handle exceptions ************************/
676 /* Build and send a response packet in order to inform the host the
677    stub is stopped. TAAn...:r...;n...:r...;n...:r...;
678                     AA = signal number
679                     n... = register number (hex)
680                     r... = register contents
681                     n... = `thread'
682                     r... = thread process ID.  This is a hex integer.
683                     n... = other string not starting with valid hex digit.
684                     gdb should ignore this n,r pair and go on to the next.
685                     This way we can extend the protocol. */
686 static void
687 stub_is_stopped(int sigval)
688 {
689         char *ptr = remcomOutBuffer;
690         int regno;
691
692         unsigned int reg_cont;
693         int status;
694         
695         /* Send trap type (converted to signal) */
696
697         *ptr++ = 'T';
698         ptr = hex_byte_pack(ptr, sigval);
699
700         /* Send register contents. We probably only need to send the
701          * PC, frame pointer and stack pointer here. Other registers will be
702          * explicitly asked for. But for now, send all.
703          */
704         
705         for (regno = R0; regno <= USP; regno++) {
706                 /* Store n...:r...; for the registers in the buffer. */
707
708                 status = read_register (regno, &reg_cont);
709                 
710                 if (status == SUCCESS) {
711                         ptr = hex_byte_pack(ptr, regno);
712                         *ptr++ = ':';
713
714                         ptr = mem2hex(ptr, (unsigned char *)&reg_cont,
715                                       register_size[regno]);
716                         *ptr++ = ';';
717                 }
718                 
719         }
720
721         /* null-terminate and send it off */
722
723         *ptr = 0;
724
725         putpacket (remcomOutBuffer);
726 }
727
728 /* Performs a complete re-start from scratch. */
729 static void
730 kill_restart (void)
731 {
732         machine_restart("");
733 }
734
735 /* All expected commands are sent from remote.c. Send a response according
736    to the description in remote.c. */
737 void
738 handle_exception (int sigval)
739 {
740         /* Send response. */
741
742         stub_is_stopped (sigval);
743
744         for (;;) {
745                 remcomOutBuffer[0] = '\0';
746                 getpacket (remcomInBuffer);
747                 switch (remcomInBuffer[0]) {
748                         case 'g':
749                                 /* Read registers: g
750                                    Success: Each byte of register data is described by two hex digits.
751                                    Registers are in the internal order for GDB, and the bytes
752                                    in a register  are in the same order the machine uses.
753                                    Failure: void. */
754                                 
755                                 mem2hex(remcomOutBuffer, (char *)&cris_reg, sizeof(registers));
756                                 break;
757                                 
758                         case 'G':
759                                 /* Write registers. GXX..XX
760                                    Each byte of register data  is described by two hex digits.
761                                    Success: OK
762                                    Failure: void. */
763                                 hex2mem((char *)&cris_reg, &remcomInBuffer[1], sizeof(registers));
764                                 gdb_cris_strcpy (remcomOutBuffer, "OK");
765                                 break;
766                                 
767                         case 'P':
768                                 /* Write register. Pn...=r...
769                                    Write register n..., hex value without 0x, with value r...,
770                                    which contains a hex value without 0x and two hex digits
771                                    for each byte in the register (target byte order). P1f=11223344 means
772                                    set register 31 to 44332211.
773                                    Success: OK
774                                    Failure: E02, E05 */
775                                 {
776                                         char *suffix;
777                                         int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);
778                                         int status;
779                                         status = write_register (regno, suffix+1);
780
781                                         switch (status) {
782                                                 case E02:
783                                                         /* Do not support read-only registers. */
784                                                         gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);
785                                                         break;
786                                                 case E05:
787                                                         /* Do not support non-existing registers. */
788                                                         gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);
789                                                         break;
790                                                 case E07:
791                                                         /* Do not support non-existing registers on the stack. */
792                                                         gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);
793                                                         break;
794                                                 default:
795                                                         /* Valid register number. */
796                                                         gdb_cris_strcpy (remcomOutBuffer, "OK");
797                                                         break;
798                                         }
799                                 }
800                                 break;
801                                 
802                         case 'm':
803                                 /* Read from memory. mAA..AA,LLLL
804                                    AA..AA is the address and LLLL is the length.
805                                    Success: XX..XX is the memory content.  Can be fewer bytes than
806                                    requested if only part of the data may be read. m6000120a,6c means
807                                    retrieve 108 byte from base address 6000120a.
808                                    Failure: void. */
809                                 {
810                                         char *suffix;
811                                         unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
812                                                                                                &suffix, 16);                                        int length = gdb_cris_strtol(suffix+1, 0, 16);
813                                         
814                                         mem2hex(remcomOutBuffer, addr, length);
815                                 }
816                                 break;
817                                 
818                         case 'X':
819                                 /* Write to memory. XAA..AA,LLLL:XX..XX
820                                    AA..AA is the start address,  LLLL is the number of bytes, and
821                                    XX..XX is the binary data.
822                                    Success: OK
823                                    Failure: void. */
824                         case 'M':
825                                 /* Write to memory. MAA..AA,LLLL:XX..XX
826                                    AA..AA is the start address,  LLLL is the number of bytes, and
827                                    XX..XX is the hexadecimal data.
828                                    Success: OK
829                                    Failure: void. */
830                                 {
831                                         char *lenptr;
832                                         char *dataptr;
833                                         unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
834                                                                                       &lenptr, 16);
835                                         int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);
836                                         if (*lenptr == ',' && *dataptr == ':') {
837                                                 if (remcomInBuffer[0] == 'M') {
838                                                         hex2mem(addr, dataptr + 1, length);
839                                                 }
840                                                 else /* X */ {
841                                                         bin2mem(addr, dataptr + 1, length);
842                                                 }
843                                                 gdb_cris_strcpy (remcomOutBuffer, "OK");
844                                         }
845                                         else {
846                                                 gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);
847                                         }
848                                 }
849                                 break;
850                                 
851                         case 'c':
852                                 /* Continue execution. cAA..AA
853                                    AA..AA is the address where execution is resumed. If AA..AA is
854                                    omitted, resume at the present address.
855                                    Success: return to the executing thread.
856                                    Failure: will never know. */
857                                 if (remcomInBuffer[1] != '\0') {
858                                         cris_reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
859                                 }
860                                 enableDebugIRQ();
861                                 return;
862                                 
863                         case 's':
864                                 /* Step. sAA..AA
865                                    AA..AA is the address where execution is resumed. If AA..AA is
866                                    omitted, resume at the present address. Success: return to the
867                                    executing thread. Failure: will never know.
868                                    
869                                    Should never be invoked. The single-step is implemented on
870                                    the host side. If ever invoked, it is an internal error E04. */
871                                 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
872                                 putpacket (remcomOutBuffer);
873                                 return;
874                                 
875                         case '?':
876                                 /* The last signal which caused a stop. ?
877                                    Success: SAA, where AA is the signal number.
878                                    Failure: void. */
879                                 remcomOutBuffer[0] = 'S';
880                                 remcomOutBuffer[1] = hex_asc_hi(sigval);
881                                 remcomOutBuffer[2] = hex_asc_lo(sigval);
882                                 remcomOutBuffer[3] = 0;
883                                 break;
884                                 
885                         case 'D':
886                                 /* Detach from host. D
887                                    Success: OK, and return to the executing thread.
888                                    Failure: will never know */
889                                 putpacket ("OK");
890                                 return;
891                                 
892                         case 'k':
893                         case 'r':
894                                 /* kill request or reset request.
895                                    Success: restart of target.
896                                    Failure: will never know. */
897                                 kill_restart ();
898                                 break;
899                                 
900                         case 'C':
901                         case 'S':
902                         case '!':
903                         case 'R':
904                         case 'd':
905                                 /* Continue with signal sig. Csig;AA..AA
906                                    Step with signal sig. Ssig;AA..AA
907                                    Use the extended remote protocol. !
908                                    Restart the target system. R0
909                                    Toggle debug flag. d
910                                    Search backwards. tAA:PP,MM
911                                    Not supported: E04 */
912                                 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
913                                 break;
914                                 
915                         default:
916                                 /* The stub should ignore other request and send an empty
917                                    response ($#<checksum>). This way we can extend the protocol and GDB
918                                    can tell whether the stub it is talking to uses the old or the new. */
919                                 remcomOutBuffer[0] = 0;
920                                 break;
921                 }
922                 putpacket(remcomOutBuffer);
923         }
924 }
925
926 /********************************** Breakpoint *******************************/
927 /* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.
928    An internal stack is used by the stub. The register image of the caller is
929    stored in the structure register_image.
930    Interactive communication with the host is handled by handle_exception and
931    finally the register image is restored. */
932
933 void kgdb_handle_breakpoint(void);
934
935 asm ("\n"
936 "  .global kgdb_handle_breakpoint\n"
937 "kgdb_handle_breakpoint:\n"
938 ";;\n"
939 ";; Response to the break-instruction\n"
940 ";;\n"
941 ";; Create a register image of the caller\n"
942 ";;\n"
943 "  move     $dccr,[cris_reg+0x5E] ; Save the flags in DCCR before disable interrupts\n"
944 "  di                        ; Disable interrupts\n"
945 "  move.d   $r0,[cris_reg]        ; Save R0\n"
946 "  move.d   $r1,[cris_reg+0x04]   ; Save R1\n"
947 "  move.d   $r2,[cris_reg+0x08]   ; Save R2\n"
948 "  move.d   $r3,[cris_reg+0x0C]   ; Save R3\n"
949 "  move.d   $r4,[cris_reg+0x10]   ; Save R4\n"
950 "  move.d   $r5,[cris_reg+0x14]   ; Save R5\n"
951 "  move.d   $r6,[cris_reg+0x18]   ; Save R6\n"
952 "  move.d   $r7,[cris_reg+0x1C]   ; Save R7\n"
953 "  move.d   $r8,[cris_reg+0x20]   ; Save R8\n"
954 "  move.d   $r9,[cris_reg+0x24]   ; Save R9\n"
955 "  move.d   $r10,[cris_reg+0x28]  ; Save R10\n"
956 "  move.d   $r11,[cris_reg+0x2C]  ; Save R11\n"
957 "  move.d   $r12,[cris_reg+0x30]  ; Save R12\n"
958 "  move.d   $r13,[cris_reg+0x34]  ; Save R13\n"
959 "  move.d   $sp,[cris_reg+0x38]   ; Save SP (R14)\n"
960 ";; Due to the old assembler-versions BRP might not be recognized\n"
961 "  .word 0xE670              ; move brp,$r0\n"
962 "  subq     2,$r0             ; Set to address of previous instruction.\n"
963 "  move.d   $r0,[cris_reg+0x3c]   ; Save the address in PC (R15)\n"
964 "  clear.b  [cris_reg+0x40]      ; Clear P0\n"
965 "  move     $vr,[cris_reg+0x41]   ; Save special register P1\n"
966 "  clear.w  [cris_reg+0x42]      ; Clear P4\n"
967 "  move     $ccr,[cris_reg+0x44]  ; Save special register CCR\n"
968 "  move     $mof,[cris_reg+0x46]  ; P7\n"
969 "  clear.d  [cris_reg+0x4A]      ; Clear P8\n"
970 "  move     $ibr,[cris_reg+0x4E]  ; P9,\n"
971 "  move     $irp,[cris_reg+0x52]  ; P10,\n"
972 "  move     $srp,[cris_reg+0x56]  ; P11,\n"
973 "  move     $dtp0,[cris_reg+0x5A] ; P12, register BAR, assembler might not know BAR\n"
974 "                            ; P13, register DCCR already saved\n"
975 ";; Due to the old assembler-versions BRP might not be recognized\n"
976 "  .word 0xE670              ; move brp,r0\n"
977 ";; Static (compiled) breakpoints must return to the next instruction in order\n"
978 ";; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction\n"
979 ";; in order to execute it when execution is continued.\n"
980 "  test.b   [is_dyn_brkp]    ; Is this a dynamic breakpoint?\n"
981 "  beq      is_static         ; No, a static breakpoint\n"
982 "  nop\n"
983 "  subq     2,$r0              ; rerun the instruction the break replaced\n"
984 "is_static:\n"
985 "  moveq    1,$r1\n"
986 "  move.b   $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint\n"
987 "  move.d   $r0,[cris_reg+0x62]    ; Save the return address in BRP\n"
988 "  move     $usp,[cris_reg+0x66]   ; USP\n"
989 ";;\n"
990 ";; Handle the communication\n"
991 ";;\n"
992 "  move.d   internal_stack+1020,$sp ; Use the internal stack which grows upward\n"
993 "  moveq    5,$r10                   ; SIGTRAP\n"
994 "  jsr      handle_exception       ; Interactive routine\n"
995 ";;\n"
996 ";; Return to the caller\n"
997 ";;\n"
998 "   move.d  [cris_reg],$r0         ; Restore R0\n"
999 "   move.d  [cris_reg+0x04],$r1    ; Restore R1\n"
1000 "   move.d  [cris_reg+0x08],$r2    ; Restore R2\n"
1001 "   move.d  [cris_reg+0x0C],$r3    ; Restore R3\n"
1002 "   move.d  [cris_reg+0x10],$r4    ; Restore R4\n"
1003 "   move.d  [cris_reg+0x14],$r5    ; Restore R5\n"
1004 "   move.d  [cris_reg+0x18],$r6    ; Restore R6\n"
1005 "   move.d  [cris_reg+0x1C],$r7    ; Restore R7\n"
1006 "   move.d  [cris_reg+0x20],$r8    ; Restore R8\n"
1007 "   move.d  [cris_reg+0x24],$r9    ; Restore R9\n"
1008 "   move.d  [cris_reg+0x28],$r10   ; Restore R10\n"
1009 "   move.d  [cris_reg+0x2C],$r11   ; Restore R11\n"
1010 "   move.d  [cris_reg+0x30],$r12   ; Restore R12\n"
1011 "   move.d  [cris_reg+0x34],$r13   ; Restore R13\n"
1012 ";;\n"
1013 ";; FIXME: Which registers should be restored?\n"
1014 ";;\n"
1015 "   move.d  [cris_reg+0x38],$sp    ; Restore SP (R14)\n"
1016 "   move    [cris_reg+0x56],$srp   ; Restore the subroutine return pointer.\n"
1017 "   move    [cris_reg+0x5E],$dccr  ; Restore DCCR\n"
1018 "   move    [cris_reg+0x66],$usp   ; Restore USP\n"
1019 "   jump    [cris_reg+0x62]       ; A jump to the content in register BRP works.\n"
1020 "   nop                       ;\n"
1021 "\n");
1022
1023 /* The hook for an interrupt generated by GDB. An internal stack is used
1024    by the stub. The register image of the caller is stored in the structure
1025    register_image. Interactive communication with the host is handled by
1026    handle_exception and finally the register image is restored. Due to the
1027    old assembler which does not recognise the break instruction and the
1028    breakpoint return pointer hex-code is used. */
1029
1030 void kgdb_handle_serial(void);
1031
1032 asm ("\n"
1033 "  .global kgdb_handle_serial\n"
1034 "kgdb_handle_serial:\n"
1035 ";;\n"
1036 ";; Response to a serial interrupt\n"
1037 ";;\n"
1038 "\n"
1039 "  move     $dccr,[cris_reg+0x5E] ; Save the flags in DCCR\n"
1040 "  di                        ; Disable interrupts\n"
1041 "  move.d   $r0,[cris_reg]        ; Save R0\n"
1042 "  move.d   $r1,[cris_reg+0x04]   ; Save R1\n"
1043 "  move.d   $r2,[cris_reg+0x08]   ; Save R2\n"
1044 "  move.d   $r3,[cris_reg+0x0C]   ; Save R3\n"
1045 "  move.d   $r4,[cris_reg+0x10]   ; Save R4\n"
1046 "  move.d   $r5,[cris_reg+0x14]   ; Save R5\n"
1047 "  move.d   $r6,[cris_reg+0x18]   ; Save R6\n"
1048 "  move.d   $r7,[cris_reg+0x1C]   ; Save R7\n"
1049 "  move.d   $r8,[cris_reg+0x20]   ; Save R8\n"
1050 "  move.d   $r9,[cris_reg+0x24]   ; Save R9\n"
1051 "  move.d   $r10,[cris_reg+0x28]  ; Save R10\n"
1052 "  move.d   $r11,[cris_reg+0x2C]  ; Save R11\n"
1053 "  move.d   $r12,[cris_reg+0x30]  ; Save R12\n"
1054 "  move.d   $r13,[cris_reg+0x34]  ; Save R13\n"
1055 "  move.d   $sp,[cris_reg+0x38]   ; Save SP (R14)\n"
1056 "  move     $irp,[cris_reg+0x3c]  ; Save the address in PC (R15)\n"
1057 "  clear.b  [cris_reg+0x40]      ; Clear P0\n"
1058 "  move     $vr,[cris_reg+0x41]   ; Save special register P1,\n"
1059 "  clear.w  [cris_reg+0x42]      ; Clear P4\n"
1060 "  move     $ccr,[cris_reg+0x44]  ; Save special register CCR\n"
1061 "  move     $mof,[cris_reg+0x46]  ; P7\n"
1062 "  clear.d  [cris_reg+0x4A]      ; Clear P8\n"
1063 "  move     $ibr,[cris_reg+0x4E]  ; P9,\n"
1064 "  move     $irp,[cris_reg+0x52]  ; P10,\n"
1065 "  move     $srp,[cris_reg+0x56]  ; P11,\n"
1066 "  move     $dtp0,[cris_reg+0x5A] ; P12, register BAR, assembler might not know BAR\n"
1067 "                            ; P13, register DCCR already saved\n"
1068 ";; Due to the old assembler-versions BRP might not be recognized\n"
1069 "  .word 0xE670              ; move brp,r0\n"
1070 "  move.d   $r0,[cris_reg+0x62]   ; Save the return address in BRP\n"
1071 "  move     $usp,[cris_reg+0x66]  ; USP\n"
1072 "\n"
1073 ";; get the serial character (from debugport.c) and check if it is a ctrl-c\n"
1074 "\n"
1075 "  jsr getDebugChar\n"
1076 "  cmp.b 3, $r10\n"
1077 "  bne goback\n"
1078 "  nop\n"
1079 "\n"
1080 "  move.d  [cris_reg+0x5E], $r10                ; Get DCCR\n"
1081 "  btstq           8, $r10                      ; Test the U-flag.\n"
1082 "  bmi     goback\n"
1083 "  nop\n"
1084 "\n"
1085 ";;\n"
1086 ";; Handle the communication\n"
1087 ";;\n"
1088 "  move.d   internal_stack+1020,$sp ; Use the internal stack\n"
1089 "  moveq    2,$r10                   ; SIGINT\n"
1090 "  jsr      handle_exception       ; Interactive routine\n"
1091 "\n"
1092 "goback:\n"
1093 ";;\n"
1094 ";; Return to the caller\n"
1095 ";;\n"
1096 "   move.d  [cris_reg],$r0         ; Restore R0\n"
1097 "   move.d  [cris_reg+0x04],$r1    ; Restore R1\n"
1098 "   move.d  [cris_reg+0x08],$r2    ; Restore R2\n"
1099 "   move.d  [cris_reg+0x0C],$r3    ; Restore R3\n"
1100 "   move.d  [cris_reg+0x10],$r4    ; Restore R4\n"
1101 "   move.d  [cris_reg+0x14],$r5    ; Restore R5\n"
1102 "   move.d  [cris_reg+0x18],$r6    ; Restore R6\n"
1103 "   move.d  [cris_reg+0x1C],$r7    ; Restore R7\n"
1104 "   move.d  [cris_reg+0x20],$r8    ; Restore R8\n"
1105 "   move.d  [cris_reg+0x24],$r9    ; Restore R9\n"
1106 "   move.d  [cris_reg+0x28],$r10   ; Restore R10\n"
1107 "   move.d  [cris_reg+0x2C],$r11   ; Restore R11\n"
1108 "   move.d  [cris_reg+0x30],$r12   ; Restore R12\n"
1109 "   move.d  [cris_reg+0x34],$r13   ; Restore R13\n"
1110 ";;\n"
1111 ";; FIXME: Which registers should be restored?\n"
1112 ";;\n"
1113 "   move.d  [cris_reg+0x38],$sp    ; Restore SP (R14)\n"
1114 "   move    [cris_reg+0x56],$srp   ; Restore the subroutine return pointer.\n"
1115 "   move    [cris_reg+0x5E],$dccr  ; Restore DCCR\n"
1116 "   move    [cris_reg+0x66],$usp   ; Restore USP\n"
1117 "   reti                      ; Return from the interrupt routine\n"
1118 "   nop\n"
1119 "\n");
1120
1121 /* Use this static breakpoint in the start-up only. */
1122
1123 void
1124 breakpoint(void)
1125 {
1126         kgdb_started = 1;
1127         is_dyn_brkp = 0;     /* This is a static, not a dynamic breakpoint. */
1128         __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */
1129 }
1130
1131 /* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */
1132
1133 void
1134 kgdb_init(void)
1135 {
1136         /* could initialize debug port as well but it's done in head.S already... */
1137
1138         /* breakpoint handler is now set in irq.c */
1139         set_int_vector(8, kgdb_handle_serial);
1140         
1141         enableDebugIRQ();
1142 }
1143
1144 /****************************** End of file **********************************/