1 /*!**************************************************************************
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.
8 *!---------------------------------------------------------------------------
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
17 *! Jun 17 1999 Hendrik Ruijter Added gdb 4.18 support. 'X', 'qC' and 'qL'.
18 *! Jul 21 1999 Bjorn Wesen eLinux port
21 *! Revision 1.8 2003/04/09 08:31:15 pkj
22 *! Typo correction (taken from Linux 2.5).
24 *! Revision 1.7 2002/07/12 09:14:56 bjornw
27 *! Revision 1.6 2001/10/09 13:10:03 matsfg
28 *! Added $ on registers and removed some underscores
30 *! Revision 1.5 2001/04/17 13:58:39 orjanf
31 *! * Renamed CONFIG_KGDB to CONFIG_ETRAX_KGDB.
33 *! Revision 1.4 2001/02/23 13:45:19 bjornw
36 *! Revision 1.3 2001/01/31 18:08:23 orjanf
37 *! Removed kgdb_handle_breakpoint from being the break 8 handler.
39 *! Revision 1.2 2001/01/12 14:22:25 orjanf
40 *! Updated kernel debugging support to work with ETRAX 100LX.
42 *! Revision 1.1 2000/07/10 16:25:21 bjornw
45 *! Revision 1.1.1.1 1999/12/03 14:57:31 bjornw
46 *! * Initial version of arch/cris, the latest CRIS architecture with an MMU.
47 *! Mostly copied from arch/etrax100 with appropriate renames of files.
48 *! The mm/ subdir is copied from arch/i386.
49 *! This does not compile yet at all.
52 *! Revision 1.4 1999/07/22 17:25:25 bjornw
53 *! Dont wait for + in putpacket if we havent hit the initial breakpoint yet. Added a kgdb_init function which sets up the break and irq vectors.
55 *! Revision 1.3 1999/07/21 19:51:18 bjornw
56 *! Check if the interrupting char is a ctrl-C, ignore otherwise.
58 *! Revision 1.2 1999/07/21 18:09:39 bjornw
59 *! Ported to eLinux architecture, and added some kgdb documentation.
62 *!---------------------------------------------------------------------------
64 *! $Id: kgdb.c,v 1.8 2003/04/09 08:31:15 pkj Exp $
66 *! (C) Copyright 1999, Axis Communications AB, LUND, SWEDEN
68 *!**************************************************************************/
69 /* @(#) cris_stub.c 1.3 06/17/99 */
75 * If you select CONFIG_ETRAX_KGDB in the configuration, the kernel will be
76 * built with different gcc flags: "-g" is added to get debug infos, and
77 * "-fomit-frame-pointer" is omitted to make debugging easier. Since the
78 * resulting kernel will be quite big (approx. > 7 MB), it will be stripped
79 * before compresion. Such a kernel will behave just as usually, except if
80 * given a "debug=<device>" command line option. (Only serial devices are
81 * allowed for <device>, i.e. no printers or the like; possible values are
82 * machine depedend and are the same as for the usual debug device, the one
83 * for logging kernel messages.) If that option is given and the device can be
84 * initialized, the kernel will connect to the remote gdb in trap_init(). The
85 * serial parameters are fixed to 8N1 and 115200 bps, for easyness of
88 * To start a debugging session, start that gdb with the debugging kernel
89 * image (the one with the symbols, vmlinux.debug) named on the command line.
90 * This file will be used by gdb to get symbol and debugging infos about the
91 * kernel. Next, select remote debug mode by
92 * target remote <device>
93 * where <device> is the name of the serial device over which the debugged
94 * machine is connected. Maybe you have to adjust the baud rate by
95 * set remotebaud <rate>
96 * or also other parameters with stty:
97 * shell stty ... </dev/...
98 * If the kernel to debug has already booted, it waited for gdb and now
99 * connects, and you'll see a breakpoint being reported. If the kernel isn't
100 * running yet, start it now. The order of gdb and the kernel doesn't matter.
101 * Another thing worth knowing about in the getting-started phase is how to
102 * debug the remote protocol itself. This is activated with
104 * gdb will then print out each packet sent or received. You'll also get some
105 * messages about the gdb stub on the console of the debugged machine.
107 * If all that works, you can use lots of the usual debugging techniques on
108 * the kernel, e.g. inspecting and changing variables/memory, setting
109 * breakpoints, single stepping and so on. It's also possible to interrupt the
110 * debugged kernel by pressing C-c in gdb. Have fun! :-)
112 * The gdb stub is entered (and thus the remote gdb gets control) in the
113 * following situations:
115 * - If breakpoint() is called. This is just after kgdb initialization, or if
116 * a breakpoint() call has been put somewhere into the kernel source.
117 * (Breakpoints can of course also be set the usual way in gdb.)
118 * In eLinux, we call breakpoint() in init/main.c after IRQ initialization.
120 * - If there is a kernel exception, i.e. bad_super_trap() or die_if_kernel()
121 * are entered. All the CPU exceptions are mapped to (more or less..., see
122 * the hard_trap_info array below) appropriate signal, which are reported
123 * to gdb. die_if_kernel() is usually called after some kind of access
124 * error and thus is reported as SIGSEGV.
126 * - When panic() is called. This is reported as SIGABRT.
128 * - If C-c is received over the serial line, which is treated as
131 * Of course, all these signals are just faked for gdb, since there is no
132 * signal concept as such for the kernel. It also isn't possible --obviously--
133 * to set signal handlers from inside gdb, or restart the kernel with a
136 * Current limitations:
138 * - While the kernel is stopped, interrupts are disabled for safety reasons
139 * (i.e., variables not changing magically or the like). But this also
140 * means that the clock isn't running anymore, and that interrupts from the
141 * hardware may get lost/not be served in time. This can cause some device
144 * - When single-stepping, only one instruction of the current thread is
145 * executed, but interrupts are allowed for that time and will be serviced
146 * if pending. Be prepared for that.
148 * - All debugging happens in kernel virtual address space. There's no way to
149 * access physical memory not mapped in kernel space, or to access user
150 * space. A way to work around this is using get_user_long & Co. in gdb
151 * expressions, but only for the current process.
153 * - Interrupting the kernel only works if interrupts are currently allowed,
154 * and the interrupt of the serial line isn't blocked by some other means
155 * (IPL too high, disabled, ...)
157 * - The gdb stub is currently not reentrant, i.e. errors that happen therein
158 * (e.g. accessing invalid memory) may not be caught correctly. This could
159 * be removed in future by introducing a stack of struct registers.
164 * To enable debugger support, two things need to happen. One, a
165 * call to kgdb_init() is necessary in order to allow any breakpoints
166 * or error conditions to be properly intercepted and reported to gdb.
167 * Two, a breakpoint needs to be generated to begin communication. This
168 * is most easily accomplished by a call to breakpoint().
170 * The following gdb commands are supported:
172 * command function Return value
174 * g return the value of the CPU registers hex data or ENN
175 * G set the value of the CPU registers OK or ENN
177 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
178 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
180 * c Resume at current address SNN ( signal NN)
181 * cAA..AA Continue at address AA..AA SNN
183 * s Step one instruction SNN
184 * sAA..AA Step one instruction from AA..AA SNN
188 * ? What was the last sigval ? SNN (signal NN)
190 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
193 * All commands and responses are sent with a packet which includes a
194 * checksum. A packet consists of
196 * $<packet info>#<checksum>.
199 * <packet info> :: <characters representing the command or response>
200 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
202 * When a packet is received, it is first acknowledged with either '+' or '-'.
203 * '+' indicates a successful transfer. '-' indicates a failed transfer.
208 * $m0,10#2a +$00010203040506070809101112131415#42
213 #include <linux/string.h>
214 #include <linux/signal.h>
215 #include <linux/kernel.h>
216 #include <linux/delay.h>
217 #include <linux/linkage.h>
219 #include <asm/setup.h>
220 #include <asm/ptrace.h>
222 #include <asm/svinto.h>
225 static int kgdb_started = 0;
227 /********************************* Register image ****************************/
228 /* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
229 Reference", p. 1-1, with the additional register definitions of the
230 ETRAX 100LX in cris-opc.h.
231 There are 16 general 32-bit registers, R0-R15, where R14 is the stack
232 pointer, SP, and R15 is the program counter, PC.
233 There are 16 special registers, P0-P15, where three of the unimplemented
234 registers, P0, P4 and P8, are reserved as zero-registers. A read from
235 any of these registers returns zero and a write has no effect. */
238 struct register_image
241 unsigned int r0; /* 0x00 */
242 unsigned int r1; /* 0x04 */
243 unsigned int r2; /* 0x08 */
244 unsigned int r3; /* 0x0C */
245 unsigned int r4; /* 0x10 */
246 unsigned int r5; /* 0x14 */
247 unsigned int r6; /* 0x18 */
248 unsigned int r7; /* 0x1C */
249 unsigned int r8; /* 0x20 Frame pointer */
250 unsigned int r9; /* 0x24 */
251 unsigned int r10; /* 0x28 */
252 unsigned int r11; /* 0x2C */
253 unsigned int r12; /* 0x30 */
254 unsigned int r13; /* 0x34 */
255 unsigned int sp; /* 0x38 Stack pointer */
256 unsigned int pc; /* 0x3C Program counter */
258 unsigned char p0; /* 0x40 8-bit zero-register */
259 unsigned char vr; /* 0x41 Version register */
261 unsigned short p4; /* 0x42 16-bit zero-register */
262 unsigned short ccr; /* 0x44 Condition code register */
264 unsigned int mof; /* 0x46 Multiply overflow register */
266 unsigned int p8; /* 0x4A 32-bit zero-register */
267 unsigned int ibr; /* 0x4E Interrupt base register */
268 unsigned int irp; /* 0x52 Interrupt return pointer */
269 unsigned int srp; /* 0x56 Subroutine return pointer */
270 unsigned int bar; /* 0x5A Breakpoint address register */
271 unsigned int dccr; /* 0x5E Double condition code register */
272 unsigned int brp; /* 0x62 Breakpoint return pointer (pc in caller) */
273 unsigned int usp; /* 0x66 User mode stack pointer */
276 /************** Prototypes for local library functions ***********************/
278 /* Copy of strcpy from libc. */
279 static char *gdb_cris_strcpy (char *s1, const char *s2);
281 /* Copy of strlen from libc. */
282 static int gdb_cris_strlen (const char *s);
284 /* Copy of memchr from libc. */
285 static void *gdb_cris_memchr (const void *s, int c, int n);
287 /* Copy of strtol from libc. Does only support base 16. */
288 static int gdb_cris_strtol (const char *s, char **endptr, int base);
290 /********************** Prototypes for local functions. **********************/
291 /* Copy the content of a register image into another. The size n is
292 the size of the register image. Due to struct assignment generation of
294 static void copy_registers (registers *dptr, registers *sptr, int n);
296 /* Copy the stored registers from the stack. Put the register contents
297 of thread thread_id in the struct reg. */
298 static void copy_registers_from_stack (int thread_id, registers *reg);
300 /* Copy the registers to the stack. Put the register contents of thread
301 thread_id from struct reg to the stack. */
302 static void copy_registers_to_stack (int thread_id, registers *reg);
304 /* Write a value to a specified register regno in the register image
305 of the current thread. */
306 static int write_register (int regno, char *val);
308 /* Write a value to a specified register in the stack of a thread other
309 than the current thread. */
310 static write_stack_register (int thread_id, int regno, char *valptr);
312 /* Read a value from a specified register in the register image. Returns the
313 status of the read operation. The register value is returned in valptr. */
314 static int read_register (char regno, unsigned int *valptr);
316 /* Serial port, reads one character. ETRAX 100 specific. from debugport.c */
317 int getDebugChar (void);
319 /* Serial port, writes one character. ETRAX 100 specific. from debugport.c */
320 void putDebugChar (int val);
322 void enableDebugIRQ (void);
324 /* Returns the character equivalent of a nibble, bit 7, 6, 5, and 4 of a byte,
325 represented by int x. */
326 static char highhex (int x);
328 /* Returns the character equivalent of a nibble, bit 3, 2, 1, and 0 of a byte,
329 represented by int x. */
330 static char lowhex (int x);
332 /* Returns the integer equivalent of a hexadecimal character. */
333 static int hex (char ch);
335 /* Convert the memory, pointed to by mem into hexadecimal representation.
336 Put the result in buf, and return a pointer to the last character
338 static char *mem2hex (char *buf, unsigned char *mem, int count);
340 /* Convert the array, in hexadecimal representation, pointed to by buf into
341 binary representation. Put the result in mem, and return a pointer to
342 the character after the last byte written. */
343 static unsigned char *hex2mem (unsigned char *mem, char *buf, int count);
345 /* Put the content of the array, in binary representation, pointed to by buf
346 into memory pointed to by mem, and return a pointer to
347 the character after the last byte written. */
348 static unsigned char *bin2mem (unsigned char *mem, unsigned char *buf, int count);
350 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
352 static void getpacket (char *buffer);
354 /* Send $<data>#<checksum> from the <data> in the array buffer. */
355 static void putpacket (char *buffer);
357 /* Build and send a response packet in order to inform the host the
359 static void stub_is_stopped (int sigval);
361 /* All expected commands are sent from remote.c. Send a response according
362 to the description in remote.c. */
363 static void handle_exception (int sigval);
365 /* Performs a complete re-start from scratch. ETRAX specific. */
366 static void kill_restart (void);
368 /******************** Prototypes for global functions. ***********************/
370 /* The string str is prepended with the GDB printout token and sent. */
371 void putDebugString (const unsigned char *str, int length); /* used by etrax100ser.c */
373 /* The hook for both static (compiled) and dynamic breakpoints set by GDB.
374 ETRAX 100 specific. */
375 void handle_breakpoint (void); /* used by irq.c */
377 /* The hook for an interrupt generated by GDB. ETRAX 100 specific. */
378 void handle_interrupt (void); /* used by irq.c */
380 /* A static breakpoint to be used at startup. */
381 void breakpoint (void); /* called by init/main.c */
383 /* From osys_int.c, executing_task contains the number of the current
384 executing task in osys. Does not know of object-oriented threads. */
385 extern unsigned char executing_task;
387 /* The number of characters used for a 64 bit thread identifier. */
388 #define HEXCHARS_IN_THREAD_ID 16
390 /* Avoid warning as the internal_stack is not used in the C-code. */
391 #define USEDVAR(name) { if (name) { ; } }
392 #define USEDFUN(name) { void (*pf)(void) = (void *)name; USEDVAR(pf) }
394 /********************************** Packet I/O ******************************/
395 /* BUFMAX defines the maximum number of characters in
396 inbound/outbound buffers */
399 /* Run-length encoding maximum length. Send 64 at most. */
402 /* Definition of all valid hexadecimal characters */
403 static const char hexchars[] = "0123456789abcdef";
405 /* The inbound/outbound buffers used in packet I/O */
406 static char remcomInBuffer[BUFMAX];
407 static char remcomOutBuffer[BUFMAX];
409 /* Error and warning messages. */
412 SUCCESS, E01, E02, E03, E04, E05, E06, E07
414 static char *error_message[] =
417 "E01 Set current or general thread - H[c,g] - internal error.",
418 "E02 Change register content - P - cannot change read-only register.",
419 "E03 Thread is not alive.", /* T, not used. */
420 "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
421 "E05 Change register content - P - the register is not implemented..",
422 "E06 Change memory content - M - internal error.",
423 "E07 Change register content - P - the register is not stored on the stack"
425 /********************************* Register image ****************************/
426 /* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
427 Reference", p. 1-1, with the additional register definitions of the
428 ETRAX 100LX in cris-opc.h.
429 There are 16 general 32-bit registers, R0-R15, where R14 is the stack
430 pointer, SP, and R15 is the program counter, PC.
431 There are 16 special registers, P0-P15, where three of the unimplemented
432 registers, P0, P4 and P8, are reserved as zero-registers. A read from
433 any of these registers returns zero and a write has no effect. */
446 /* The register sizes of the registers in register_name. An unimplemented register
447 is designated by size 0 in this array. */
448 static int register_size[] =
460 /* Contains the register image of the executing thread in the assembler
461 part of the code in order to avoid horrible addressing modes. */
462 static registers reg;
464 /* FIXME: Should this be used? Delete otherwise. */
465 /* Contains the assumed consistency state of the register image. Uses the
466 enum error_type for state information. */
467 static int consistency_status = SUCCESS;
469 /********************************** Handle exceptions ************************/
470 /* The variable reg contains the register image associated with the
471 current_thread_c variable. It is a complete register image created at
472 entry. The reg_g contains a register image of a task where the general
473 registers are taken from the stack and all special registers are taken
474 from the executing task. It is associated with current_thread_g and used
475 in order to provide access mainly for 'g', 'G' and 'P'.
478 /* Need two task id pointers in order to handle Hct and Hgt commands. */
479 static int current_thread_c = 0;
480 static int current_thread_g = 0;
482 /* Need two register images in order to handle Hct and Hgt commands. The
483 variable reg_g is in addition to reg above. */
484 static registers reg_g;
486 /********************************** Breakpoint *******************************/
487 /* Use an internal stack in the breakpoint and interrupt response routines */
488 #define INTERNAL_STACK_SIZE 1024
489 static char internal_stack[INTERNAL_STACK_SIZE];
491 /* Due to the breakpoint return pointer, a state variable is needed to keep
492 track of whether it is a static (compiled) or dynamic (gdb-invoked)
493 breakpoint to be handled. A static breakpoint uses the content of register
494 BRP as it is whereas a dynamic breakpoint requires subtraction with 2
495 in order to execute the instruction. The first breakpoint is static. */
496 static unsigned char is_dyn_brkp = 0;
498 /********************************* String library ****************************/
499 /* Single-step over library functions creates trap loops. */
501 /* Copy char s2[] to s1[]. */
503 gdb_cris_strcpy (char *s1, const char *s2)
507 for (s = s1; (*s++ = *s2++) != '\0'; )
512 /* Find length of s[]. */
514 gdb_cris_strlen (const char *s)
518 for (sc = s; *sc != '\0'; sc++)
523 /* Find first occurrence of c in s[n]. */
525 gdb_cris_memchr (const void *s, int c, int n)
527 const unsigned char uc = c;
528 const unsigned char *su;
530 for (su = s; 0 < n; ++su, --n)
535 /******************************* Standard library ****************************/
536 /* Single-step over library functions creates trap loops. */
537 /* Convert string to long. */
539 gdb_cris_strtol (const char *s, char **endptr, int base)
545 for (s1 = (char*)s; (sd = gdb_cris_memchr(hexchars, *s1, base)) != NULL; ++s1)
546 x = x * base + (sd - hexchars);
550 /* Unconverted suffix is stored in endptr unless endptr is NULL. */
563 /********************************* Register image ****************************/
564 /* Copy the content of a register image into another. The size n is
565 the size of the register image. Due to struct assignment generation of
568 copy_registers (registers *dptr, registers *sptr, int n)
573 for (dreg = (unsigned char*)dptr, sreg = (unsigned char*)sptr; n > 0; n--)
577 #ifdef PROCESS_SUPPORT
578 /* Copy the stored registers from the stack. Put the register contents
579 of thread thread_id in the struct reg. */
581 copy_registers_from_stack (int thread_id, registers *regptr)
584 stack_registers *s = (stack_registers *)stack_list[thread_id];
585 unsigned int *d = (unsigned int *)regptr;
587 for (j = 13; j >= 0; j--)
589 regptr->sp = (unsigned int)stack_list[thread_id];
591 regptr->dccr = s->dccr;
592 regptr->srp = s->srp;
595 /* Copy the registers to the stack. Put the register contents of thread
596 thread_id from struct reg to the stack. */
598 copy_registers_to_stack (int thread_id, registers *regptr)
601 stack_registers *d = (stack_registers *)stack_list[thread_id];
602 unsigned int *s = (unsigned int *)regptr;
604 for (i = 0; i < 14; i++) {
608 d->dccr = regptr->dccr;
609 d->srp = regptr->srp;
613 /* Write a value to a specified register in the register image of the current
614 thread. Returns status code SUCCESS, E02 or E05. */
616 write_register (int regno, char *val)
618 int status = SUCCESS;
619 registers *current_reg = ®
621 if (regno >= R0 && regno <= PC) {
622 /* 32-bit register with simple offset. */
623 hex2mem ((unsigned char *)current_reg + regno * sizeof(unsigned int),
624 val, sizeof(unsigned int));
626 else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {
627 /* Do not support read-only registers. */
630 else if (regno == CCR) {
631 /* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented,
632 and P7 (MOF) is 32 bits in ETRAX 100LX. */
633 hex2mem ((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),
634 val, sizeof(unsigned short));
636 else if (regno >= MOF && regno <= USP) {
637 /* 32 bit register with complex offset. (P8 has been taken care of.) */
638 hex2mem ((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),
639 val, sizeof(unsigned int));
642 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
648 #ifdef PROCESS_SUPPORT
649 /* Write a value to a specified register in the stack of a thread other
650 than the current thread. Returns status code SUCCESS or E07. */
652 write_stack_register (int thread_id, int regno, char *valptr)
654 int status = SUCCESS;
655 stack_registers *d = (stack_registers *)stack_list[thread_id];
658 hex2mem ((unsigned char *)&val, valptr, sizeof(unsigned int));
659 if (regno >= R0 && regno < SP) {
662 else if (regno == SP) {
663 stack_list[thread_id] = val;
665 else if (regno == PC) {
668 else if (regno == SRP) {
671 else if (regno == DCCR) {
675 /* Do not support registers in the current thread. */
682 /* Read a value from a specified register in the register image. Returns the
683 value in the register or -1 for non-implemented registers.
684 Should check consistency_status after a call which may be E05 after changes
685 in the implementation. */
687 read_register (char regno, unsigned int *valptr)
689 registers *current_reg = ®
691 if (regno >= R0 && regno <= PC) {
692 /* 32-bit register with simple offset. */
693 *valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));
696 else if (regno == P0 || regno == VR) {
697 /* 8 bit register with complex offset. */
698 *valptr = (unsigned int)(*(unsigned char *)
699 ((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));
702 else if (regno == P4 || regno == CCR) {
703 /* 16 bit register with complex offset. */
704 *valptr = (unsigned int)(*(unsigned short *)
705 ((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));
708 else if (regno >= MOF && regno <= USP) {
709 /* 32 bit register with complex offset. */
710 *valptr = *(unsigned int *)((char *)&(current_reg->p8)
711 + (regno-P8) * sizeof(unsigned int));
715 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
716 consistency_status = E05;
721 /********************************** Packet I/O ******************************/
722 /* Returns the character equivalent of a nibble, bit 7, 6, 5, and 4 of a byte,
723 represented by int x. */
727 return hexchars[(x >> 4) & 0xf];
730 /* Returns the character equivalent of a nibble, bit 3, 2, 1, and 0 of a byte,
731 represented by int x. */
735 return hexchars[x & 0xf];
738 /* Returns the integer equivalent of a hexadecimal character. */
742 if ((ch >= 'a') && (ch <= 'f'))
743 return (ch - 'a' + 10);
744 if ((ch >= '0') && (ch <= '9'))
746 if ((ch >= 'A') && (ch <= 'F'))
747 return (ch - 'A' + 10);
751 /* Convert the memory, pointed to by mem into hexadecimal representation.
752 Put the result in buf, and return a pointer to the last character
755 static int do_printk = 0;
758 mem2hex(char *buf, unsigned char *mem, int count)
764 /* Bogus read from m0. FIXME: What constitutes a valid address? */
765 for (i = 0; i < count; i++) {
770 /* Valid mem address. */
771 for (i = 0; i < count; i++) {
773 *buf++ = highhex (ch);
774 *buf++ = lowhex (ch);
778 /* Terminate properly. */
783 /* Convert the array, in hexadecimal representation, pointed to by buf into
784 binary representation. Put the result in mem, and return a pointer to
785 the character after the last byte written. */
786 static unsigned char*
787 hex2mem (unsigned char *mem, char *buf, int count)
791 for (i = 0; i < count; i++) {
792 ch = hex (*buf++) << 4;
793 ch = ch + hex (*buf++);
799 /* Put the content of the array, in binary representation, pointed to by buf
800 into memory pointed to by mem, and return a pointer to the character after
801 the last byte written.
802 Gdb will escape $, #, and the escape char (0x7d). */
803 static unsigned char*
804 bin2mem (unsigned char *mem, unsigned char *buf, int count)
808 for (i = 0; i < count; i++) {
809 /* Check for any escaped characters. Be paranoid and
810 only unescape chars that should be escaped. */
813 if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */
824 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
827 getpacket (char *buffer)
829 unsigned char checksum;
830 unsigned char xmitcsum;
835 while ((ch = getDebugChar ()) != '$')
836 /* Wait for the start character $ and ignore all other characters */;
840 /* Read until a # or the end of the buffer is reached */
841 while (count < BUFMAX) {
842 ch = getDebugChar ();
845 checksum = checksum + ch;
849 buffer[count] = '\0';
852 xmitcsum = hex (getDebugChar ()) << 4;
853 xmitcsum += hex (getDebugChar ());
854 if (checksum != xmitcsum) {
859 /* Correct checksum */
861 /* If sequence characters are received, reply with them */
862 if (buffer[2] == ':') {
863 putDebugChar (buffer[0]);
864 putDebugChar (buffer[1]);
865 /* Remove the sequence characters from the buffer */
866 count = gdb_cris_strlen (buffer);
867 for (i = 3; i <= count; i++)
868 buffer[i - 3] = buffer[i];
872 } while (checksum != xmitcsum);
875 /* Send $<data>#<checksum> from the <data> in the array buffer. */
878 putpacket(char *buffer)
889 /* Do run length encoding */
893 while (runlen < RUNLENMAX && *src == src[runlen]) {
897 /* Got a useful amount */
900 encode = runlen + ' ' - 4;
901 putDebugChar (encode);
910 putDebugChar (highhex (checksum));
911 putDebugChar (lowhex (checksum));
912 } while(kgdb_started && (getDebugChar() != '+'));
915 /* The string str is prepended with the GDB printout token and sent. Required
916 in traditional implementations. */
918 putDebugString (const unsigned char *str, int length)
920 remcomOutBuffer[0] = 'O';
921 mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);
922 putpacket(remcomOutBuffer);
925 /********************************** Handle exceptions ************************/
926 /* Build and send a response packet in order to inform the host the
927 stub is stopped. TAAn...:r...;n...:r...;n...:r...;
929 n... = register number (hex)
930 r... = register contents
932 r... = thread process ID. This is a hex integer.
933 n... = other string not starting with valid hex digit.
934 gdb should ignore this n,r pair and go on to the next.
935 This way we can extend the protocol. */
937 stub_is_stopped(int sigval)
939 char *ptr = remcomOutBuffer;
942 unsigned int reg_cont;
945 /* Send trap type (converted to signal) */
948 *ptr++ = highhex (sigval);
949 *ptr++ = lowhex (sigval);
951 /* Send register contents. We probably only need to send the
952 * PC, frame pointer and stack pointer here. Other registers will be
953 * explicitely asked for. But for now, send all.
956 for (regno = R0; regno <= USP; regno++) {
957 /* Store n...:r...; for the registers in the buffer. */
959 status = read_register (regno, ®_cont);
961 if (status == SUCCESS) {
963 *ptr++ = highhex (regno);
964 *ptr++ = lowhex (regno);
967 ptr = mem2hex(ptr, (unsigned char *)®_cont,
968 register_size[regno]);
974 #ifdef PROCESS_SUPPORT
975 /* Store the registers of the executing thread. Assume that both step,
976 continue, and register content requests are with respect to this
977 thread. The executing task is from the operating system scheduler. */
979 current_thread_c = executing_task;
980 current_thread_g = executing_task;
982 /* A struct assignment translates into a libc memcpy call. Avoid
983 all libc functions in order to prevent recursive break points. */
984 copy_registers (®_g, ®, sizeof(registers));
986 /* Store thread:r...; with the executing task TID. */
987 gdb_cris_strcpy (&remcomOutBuffer[pos], "thread:");
988 pos += gdb_cris_strlen ("thread:");
989 remcomOutBuffer[pos++] = highhex (executing_task);
990 remcomOutBuffer[pos++] = lowhex (executing_task);
991 gdb_cris_strcpy (&remcomOutBuffer[pos], ";");
994 /* null-terminate and send it off */
998 putpacket (remcomOutBuffer);
1001 /* All expected commands are sent from remote.c. Send a response according
1002 to the description in remote.c. */
1004 handle_exception (int sigval)
1006 /* Avoid warning of not used. */
1008 USEDFUN(handle_exception);
1009 USEDVAR(internal_stack[0]);
1011 /* Send response. */
1013 stub_is_stopped (sigval);
1016 remcomOutBuffer[0] = '\0';
1017 getpacket (remcomInBuffer);
1018 switch (remcomInBuffer[0]) {
1020 /* Read registers: g
1021 Success: Each byte of register data is described by two hex digits.
1022 Registers are in the internal order for GDB, and the bytes
1023 in a register are in the same order the machine uses.
1027 #ifdef PROCESS_SUPPORT
1028 /* Use the special register content in the executing thread. */
1029 copy_registers (®_g, ®, sizeof(registers));
1030 /* Replace the content available on the stack. */
1031 if (current_thread_g != executing_task) {
1032 copy_registers_from_stack (current_thread_g, ®_g);
1034 mem2hex ((unsigned char *)remcomOutBuffer, (unsigned char *)®_g, sizeof(registers));
1036 mem2hex(remcomOutBuffer, (char *)®, sizeof(registers));
1042 /* Write registers. GXX..XX
1043 Each byte of register data is described by two hex digits.
1046 #ifdef PROCESS_SUPPORT
1047 hex2mem ((unsigned char *)®_g, &remcomInBuffer[1], sizeof(registers));
1048 if (current_thread_g == executing_task) {
1049 copy_registers (®, ®_g, sizeof(registers));
1052 copy_registers_to_stack(current_thread_g, ®_g);
1055 hex2mem((char *)®, &remcomInBuffer[1], sizeof(registers));
1057 gdb_cris_strcpy (remcomOutBuffer, "OK");
1061 /* Write register. Pn...=r...
1062 Write register n..., hex value without 0x, with value r...,
1063 which contains a hex value without 0x and two hex digits
1064 for each byte in the register (target byte order). P1f=11223344 means
1065 set register 31 to 44332211.
1067 Failure: E02, E05 */
1070 int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);
1072 #ifdef PROCESS_SUPPORT
1073 if (current_thread_g != executing_task)
1074 status = write_stack_register (current_thread_g, regno, suffix+1);
1077 status = write_register (regno, suffix+1);
1081 /* Do not support read-only registers. */
1082 gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);
1085 /* Do not support non-existing registers. */
1086 gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);
1089 /* Do not support non-existing registers on the stack. */
1090 gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);
1093 /* Valid register number. */
1094 gdb_cris_strcpy (remcomOutBuffer, "OK");
1101 /* Read from memory. mAA..AA,LLLL
1102 AA..AA is the address and LLLL is the length.
1103 Success: XX..XX is the memory content. Can be fewer bytes than
1104 requested if only part of the data may be read. m6000120a,6c means
1105 retrieve 108 byte from base address 6000120a.
1109 unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
1110 &suffix, 16); int length = gdb_cris_strtol(suffix+1, 0, 16);
1112 mem2hex(remcomOutBuffer, addr, length);
1117 /* Write to memory. XAA..AA,LLLL:XX..XX
1118 AA..AA is the start address, LLLL is the number of bytes, and
1119 XX..XX is the binary data.
1123 /* Write to memory. MAA..AA,LLLL:XX..XX
1124 AA..AA is the start address, LLLL is the number of bytes, and
1125 XX..XX is the hexadecimal data.
1131 unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
1133 int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);
1134 if (*lenptr == ',' && *dataptr == ':') {
1135 if (remcomInBuffer[0] == 'M') {
1136 hex2mem(addr, dataptr + 1, length);
1139 bin2mem(addr, dataptr + 1, length);
1141 gdb_cris_strcpy (remcomOutBuffer, "OK");
1144 gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);
1150 /* Continue execution. cAA..AA
1151 AA..AA is the address where execution is resumed. If AA..AA is
1152 omitted, resume at the present address.
1153 Success: return to the executing thread.
1154 Failure: will never know. */
1155 if (remcomInBuffer[1] != '\0') {
1156 reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
1163 AA..AA is the address where execution is resumed. If AA..AA is
1164 omitted, resume at the present address. Success: return to the
1165 executing thread. Failure: will never know.
1167 Should never be invoked. The single-step is implemented on
1168 the host side. If ever invoked, it is an internal error E04. */
1169 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1170 putpacket (remcomOutBuffer);
1174 /* The last signal which caused a stop. ?
1175 Success: SAA, where AA is the signal number.
1177 remcomOutBuffer[0] = 'S';
1178 remcomOutBuffer[1] = highhex (sigval);
1179 remcomOutBuffer[2] = lowhex (sigval);
1180 remcomOutBuffer[3] = 0;
1184 /* Detach from host. D
1185 Success: OK, and return to the executing thread.
1186 Failure: will never know */
1192 /* kill request or reset request.
1193 Success: restart of target.
1194 Failure: will never know. */
1203 /* Continue with signal sig. Csig;AA..AA
1204 Step with signal sig. Ssig;AA..AA
1205 Use the extended remote protocol. !
1206 Restart the target system. R0
1207 Toggle debug flag. d
1208 Search backwards. tAA:PP,MM
1209 Not supported: E04 */
1210 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1212 #ifdef PROCESS_SUPPORT
1215 /* Thread alive. TXX
1217 Success: OK, thread XX is alive.
1218 Failure: E03, thread XX is dead. */
1220 int thread_id = (int)gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
1221 /* Cannot tell whether it is alive or not. */
1222 if (thread_id >= 0 && thread_id < number_of_tasks)
1223 gdb_cris_strcpy (remcomOutBuffer, "OK");
1228 /* Set thread for subsequent operations: Hct
1229 c = 'c' for thread used in step and continue;
1230 t can be -1 for all threads.
1231 c = 'g' for thread used in other operations.
1232 t = 0 means pick any thread.
1236 int thread_id = gdb_cris_strtol (&remcomInBuffer[2], 0, 16);
1237 if (remcomInBuffer[1] == 'c') {
1238 /* c = 'c' for thread used in step and continue */
1239 /* Do not change current_thread_c here. It would create a mess in
1241 gdb_cris_strcpy (remcomOutBuffer, "OK");
1243 else if (remcomInBuffer[1] == 'g') {
1244 /* c = 'g' for thread used in other operations.
1245 t = 0 means pick any thread. Impossible since the scheduler does
1247 if (thread_id >= 0 && thread_id < number_of_tasks) {
1248 current_thread_g = thread_id;
1249 gdb_cris_strcpy (remcomOutBuffer, "OK");
1252 /* Not expected - send an error message. */
1253 gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1257 /* Not expected - send an error message. */
1258 gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1265 /* Query of general interest. qXXXX
1266 Set general value XXXX. QXXXX=yyyy */
1272 switch (remcomInBuffer[1]) {
1274 /* Identify the remote current thread. */
1275 gdb_cris_strcpy (&remcomOutBuffer[0], "QC");
1276 remcomOutBuffer[2] = highhex (current_thread_c);
1277 remcomOutBuffer[3] = lowhex (current_thread_c);
1278 remcomOutBuffer[4] = '\0';
1281 gdb_cris_strcpy (&remcomOutBuffer[0], "QM");
1282 /* Reply with number of threads. */
1283 if (os_is_started()) {
1284 remcomOutBuffer[2] = highhex (number_of_tasks);
1285 remcomOutBuffer[3] = lowhex (number_of_tasks);
1288 remcomOutBuffer[2] = highhex (0);
1289 remcomOutBuffer[3] = lowhex (1);
1291 /* Done with the reply. */
1292 remcomOutBuffer[4] = lowhex (1);
1294 /* Expects the argument thread id. */
1295 for (; pos < (5 + HEXCHARS_IN_THREAD_ID); pos++)
1296 remcomOutBuffer[pos] = remcomInBuffer[pos];
1297 /* Reply with the thread identifiers. */
1298 if (os_is_started()) {
1299 /* Store the thread identifiers of all tasks. */
1300 for (thread_id = 0; thread_id < number_of_tasks; thread_id++) {
1301 nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1302 for (; pos < nextpos; pos ++)
1303 remcomOutBuffer[pos] = lowhex (0);
1304 remcomOutBuffer[pos++] = lowhex (thread_id);
1308 /* Store the thread identifier of the boot task. */
1309 nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1310 for (; pos < nextpos; pos ++)
1311 remcomOutBuffer[pos] = lowhex (0);
1312 remcomOutBuffer[pos++] = lowhex (current_thread_c);
1314 remcomOutBuffer[pos] = '\0';
1317 /* Not supported: "" */
1318 /* Request information about section offsets: qOffsets. */
1319 remcomOutBuffer[0] = 0;
1324 #endif /* PROCESS_SUPPORT */
1327 /* The stub should ignore other request and send an empty
1328 response ($#<checksum>). This way we can extend the protocol and GDB
1329 can tell whether the stub it is talking to uses the old or the new. */
1330 remcomOutBuffer[0] = 0;
1333 putpacket(remcomOutBuffer);
1337 /* The jump is to the address 0x00000002. Performs a complete re-start
1342 __asm__ volatile ("jump 2");
1345 /********************************** Breakpoint *******************************/
1346 /* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.
1347 An internal stack is used by the stub. The register image of the caller is
1348 stored in the structure register_image.
1349 Interactive communication with the host is handled by handle_exception and
1350 finally the register image is restored. */
1352 void kgdb_handle_breakpoint(void);
1355 .global kgdb_handle_breakpoint
1356 kgdb_handle_breakpoint:
1358 ;; Response to the break-instruction
1360 ;; Create a register image of the caller
1362 move $dccr,[reg+0x5E] ; Save the flags in DCCR before disable interrupts
1363 di ; Disable interrupts
1364 move.d $r0,[reg] ; Save R0
1365 move.d $r1,[reg+0x04] ; Save R1
1366 move.d $r2,[reg+0x08] ; Save R2
1367 move.d $r3,[reg+0x0C] ; Save R3
1368 move.d $r4,[reg+0x10] ; Save R4
1369 move.d $r5,[reg+0x14] ; Save R5
1370 move.d $r6,[reg+0x18] ; Save R6
1371 move.d $r7,[reg+0x1C] ; Save R7
1372 move.d $r8,[reg+0x20] ; Save R8
1373 move.d $r9,[reg+0x24] ; Save R9
1374 move.d $r10,[reg+0x28] ; Save R10
1375 move.d $r11,[reg+0x2C] ; Save R11
1376 move.d $r12,[reg+0x30] ; Save R12
1377 move.d $r13,[reg+0x34] ; Save R13
1378 move.d $sp,[reg+0x38] ; Save SP (R14)
1379 ;; Due to the old assembler-versions BRP might not be recognized
1380 .word 0xE670 ; move brp,$r0
1381 subq 2,$r0 ; Set to address of previous instruction.
1382 move.d $r0,[reg+0x3c] ; Save the address in PC (R15)
1383 clear.b [reg+0x40] ; Clear P0
1384 move $vr,[reg+0x41] ; Save special register P1
1385 clear.w [reg+0x42] ; Clear P4
1386 move $ccr,[reg+0x44] ; Save special register CCR
1387 move $mof,[reg+0x46] ; P7
1388 clear.d [reg+0x4A] ; Clear P8
1389 move $ibr,[reg+0x4E] ; P9,
1390 move $irp,[reg+0x52] ; P10,
1391 move $srp,[reg+0x56] ; P11,
1392 move $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR
1393 ; P13, register DCCR already saved
1394 ;; Due to the old assembler-versions BRP might not be recognized
1395 .word 0xE670 ; move brp,r0
1396 ;; Static (compiled) breakpoints must return to the next instruction in order
1397 ;; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction
1398 ;; in order to execute it when execution is continued.
1399 test.b [is_dyn_brkp] ; Is this a dynamic breakpoint?
1400 beq is_static ; No, a static breakpoint
1402 subq 2,$r0 ; rerun the instruction the break replaced
1405 move.b $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint
1406 move.d $r0,[reg+0x62] ; Save the return address in BRP
1407 move $usp,[reg+0x66] ; USP
1409 ;; Handle the communication
1411 move.d internal_stack+1020,$sp ; Use the internal stack which grows upward
1412 moveq 5,$r10 ; SIGTRAP
1413 jsr handle_exception ; Interactive routine
1415 ;; Return to the caller
1417 move.d [reg],$r0 ; Restore R0
1418 move.d [reg+0x04],$r1 ; Restore R1
1419 move.d [reg+0x08],$r2 ; Restore R2
1420 move.d [reg+0x0C],$r3 ; Restore R3
1421 move.d [reg+0x10],$r4 ; Restore R4
1422 move.d [reg+0x14],$r5 ; Restore R5
1423 move.d [reg+0x18],$r6 ; Restore R6
1424 move.d [reg+0x1C],$r7 ; Restore R7
1425 move.d [reg+0x20],$r8 ; Restore R8
1426 move.d [reg+0x24],$r9 ; Restore R9
1427 move.d [reg+0x28],$r10 ; Restore R10
1428 move.d [reg+0x2C],$r11 ; Restore R11
1429 move.d [reg+0x30],$r12 ; Restore R12
1430 move.d [reg+0x34],$r13 ; Restore R13
1432 ;; FIXME: Which registers should be restored?
1434 move.d [reg+0x38],$sp ; Restore SP (R14)
1435 move [reg+0x56],$srp ; Restore the subroutine return pointer.
1436 move [reg+0x5E],$dccr ; Restore DCCR
1437 move [reg+0x66],$usp ; Restore USP
1438 jump [reg+0x62] ; A jump to the content in register BRP works.
1442 /* The hook for an interrupt generated by GDB. An internal stack is used
1443 by the stub. The register image of the caller is stored in the structure
1444 register_image. Interactive communication with the host is handled by
1445 handle_exception and finally the register image is restored. Due to the
1446 old assembler which does not recognise the break instruction and the
1447 breakpoint return pointer hex-code is used. */
1449 void kgdb_handle_serial(void);
1452 .global kgdb_handle_serial
1455 ;; Response to a serial interrupt
1458 move $dccr,[reg+0x5E] ; Save the flags in DCCR
1459 di ; Disable interrupts
1460 move.d $r0,[reg] ; Save R0
1461 move.d $r1,[reg+0x04] ; Save R1
1462 move.d $r2,[reg+0x08] ; Save R2
1463 move.d $r3,[reg+0x0C] ; Save R3
1464 move.d $r4,[reg+0x10] ; Save R4
1465 move.d $r5,[reg+0x14] ; Save R5
1466 move.d $r6,[reg+0x18] ; Save R6
1467 move.d $r7,[reg+0x1C] ; Save R7
1468 move.d $r8,[reg+0x20] ; Save R8
1469 move.d $r9,[reg+0x24] ; Save R9
1470 move.d $r10,[reg+0x28] ; Save R10
1471 move.d $r11,[reg+0x2C] ; Save R11
1472 move.d $r12,[reg+0x30] ; Save R12
1473 move.d $r13,[reg+0x34] ; Save R13
1474 move.d $sp,[reg+0x38] ; Save SP (R14)
1475 move $irp,[reg+0x3c] ; Save the address in PC (R15)
1476 clear.b [reg+0x40] ; Clear P0
1477 move $vr,[reg+0x41] ; Save special register P1,
1478 clear.w [reg+0x42] ; Clear P4
1479 move $ccr,[reg+0x44] ; Save special register CCR
1480 move $mof,[reg+0x46] ; P7
1481 clear.d [reg+0x4A] ; Clear P8
1482 move $ibr,[reg+0x4E] ; P9,
1483 move $irp,[reg+0x52] ; P10,
1484 move $srp,[reg+0x56] ; P11,
1485 move $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR
1486 ; P13, register DCCR already saved
1487 ;; Due to the old assembler-versions BRP might not be recognized
1488 .word 0xE670 ; move brp,r0
1489 move.d $r0,[reg+0x62] ; Save the return address in BRP
1490 move $usp,[reg+0x66] ; USP
1492 ;; get the serial character (from debugport.c) and check if it is a ctrl-c
1500 ;; Handle the communication
1502 move.d internal_stack+1020,$sp ; Use the internal stack
1503 moveq 2,$r10 ; SIGINT
1504 jsr handle_exception ; Interactive routine
1508 ;; Return to the caller
1510 move.d [reg],$r0 ; Restore R0
1511 move.d [reg+0x04],$r1 ; Restore R1
1512 move.d [reg+0x08],$r2 ; Restore R2
1513 move.d [reg+0x0C],$r3 ; Restore R3
1514 move.d [reg+0x10],$r4 ; Restore R4
1515 move.d [reg+0x14],$r5 ; Restore R5
1516 move.d [reg+0x18],$r6 ; Restore R6
1517 move.d [reg+0x1C],$r7 ; Restore R7
1518 move.d [reg+0x20],$r8 ; Restore R8
1519 move.d [reg+0x24],$r9 ; Restore R9
1520 move.d [reg+0x28],$r10 ; Restore R10
1521 move.d [reg+0x2C],$r11 ; Restore R11
1522 move.d [reg+0x30],$r12 ; Restore R12
1523 move.d [reg+0x34],$r13 ; Restore R13
1525 ;; FIXME: Which registers should be restored?
1527 move.d [reg+0x38],$sp ; Restore SP (R14)
1528 move [reg+0x56],$srp ; Restore the subroutine return pointer.
1529 move [reg+0x5E],$dccr ; Restore DCCR
1530 move [reg+0x66],$usp ; Restore USP
1531 reti ; Return from the interrupt routine
1535 /* Use this static breakpoint in the start-up only. */
1541 is_dyn_brkp = 0; /* This is a static, not a dynamic breakpoint. */
1542 __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */
1545 /* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */
1550 /* could initialize debug port as well but it's done in head.S already... */
1552 /* breakpoint handler is now set in irq.c */
1553 set_int_vector(8, kgdb_handle_serial, 0);
1558 /****************************** End of file **********************************/