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
20 *!---------------------------------------------------------------------------
22 *! (C) Copyright 1999, Axis Communications AB, LUND, SWEDEN
24 *!**************************************************************************/
25 /* @(#) cris_stub.c 1.3 06/17/99 */
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
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
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.
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! :-)
68 * The gdb stub is entered (and thus the remote gdb gets control) in the
69 * following situations:
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.
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.
82 * - When panic() is called. This is reported as SIGABRT.
84 * - If C-c is received over the serial line, which is treated as
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
92 * Current limitations:
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
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.
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.
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, ...)
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.
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().
126 * The following gdb commands are supported:
128 * command function Return value
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
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
136 * c Resume at current address SNN ( signal NN)
137 * cAA..AA Continue at address AA..AA SNN
139 * s Step one instruction SNN
140 * sAA..AA Step one instruction from AA..AA SNN
144 * ? What was the last sigval ? SNN (signal NN)
146 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
149 * All commands and responses are sent with a packet which includes a
150 * checksum. A packet consists of
152 * $<packet info>#<checksum>.
155 * <packet info> :: <characters representing the command or response>
156 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
158 * When a packet is received, it is first acknowledged with either '+' or '-'.
159 * '+' indicates a successful transfer. '-' indicates a failed transfer.
164 * $m0,10#2a +$00010203040506070809101112131415#42
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>
176 #include <asm/setup.h>
177 #include <asm/ptrace.h>
179 #include <arch/svinto.h>
182 static int kgdb_started = 0;
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. */
195 struct register_image
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 */
215 unsigned char p0; /* 0x40 8-bit zero-register */
216 unsigned char vr; /* 0x41 Version register */
218 unsigned short p4; /* 0x42 16-bit zero-register */
219 unsigned short ccr; /* 0x44 Condition code register */
221 unsigned int mof; /* 0x46 Multiply overflow register */
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 */
233 /* Serial port, reads one character. ETRAX 100 specific. from debugport.c */
234 int getDebugChar (void);
236 /* Serial port, writes one character. ETRAX 100 specific. from debugport.c */
237 void putDebugChar (int val);
239 void enableDebugIRQ (void);
241 /******************** Prototypes for global functions. ***********************/
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 */
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 */
250 /* The hook for an interrupt generated by GDB. ETRAX 100 specific. */
251 void handle_interrupt (void); /* used by irq.c */
253 /* A static breakpoint to be used at startup. */
254 void breakpoint (void); /* called by init/main.c */
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;
260 /* The number of characters used for a 64 bit thread identifier. */
261 #define HEXCHARS_IN_THREAD_ID 16
263 /* Avoid warning as the internal_stack is not used in the C-code. */
264 #define USEDVAR(name) { if (name) { ; } }
265 #define USEDFUN(name) { void (*pf)(void) = (void *)name; USEDVAR(pf) }
267 /********************************** Packet I/O ******************************/
268 /* BUFMAX defines the maximum number of characters in
269 inbound/outbound buffers */
272 /* Run-length encoding maximum length. Send 64 at most. */
275 /* The inbound/outbound buffers used in packet I/O */
276 static char remcomInBuffer[BUFMAX];
277 static char remcomOutBuffer[BUFMAX];
279 /* Error and warning messages. */
282 SUCCESS, E01, E02, E03, E04, E05, E06, E07
284 static char *error_message[] =
287 "E01 Set current or general thread - H[c,g] - internal error.",
288 "E02 Change register content - P - cannot change read-only register.",
289 "E03 Thread is not alive.", /* T, not used. */
290 "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
291 "E05 Change register content - P - the register is not implemented..",
292 "E06 Change memory content - M - internal error.",
293 "E07 Change register content - P - the register is not stored on the stack"
295 /********************************* Register image ****************************/
296 /* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
297 Reference", p. 1-1, with the additional register definitions of the
298 ETRAX 100LX in cris-opc.h.
299 There are 16 general 32-bit registers, R0-R15, where R14 is the stack
300 pointer, SP, and R15 is the program counter, PC.
301 There are 16 special registers, P0-P15, where three of the unimplemented
302 registers, P0, P4 and P8, are reserved as zero-registers. A read from
303 any of these registers returns zero and a write has no effect. */
316 /* The register sizes of the registers in register_name. An unimplemented register
317 is designated by size 0 in this array. */
318 static int register_size[] =
330 /* Contains the register image of the executing thread in the assembler
331 part of the code in order to avoid horrible addressing modes. */
334 /* FIXME: Should this be used? Delete otherwise. */
335 /* Contains the assumed consistency state of the register image. Uses the
336 enum error_type for state information. */
337 static int consistency_status = SUCCESS;
339 /********************************** Handle exceptions ************************/
340 /* The variable cris_reg contains the register image associated with the
341 current_thread_c variable. It is a complete register image created at
342 entry. The reg_g contains a register image of a task where the general
343 registers are taken from the stack and all special registers are taken
344 from the executing task. It is associated with current_thread_g and used
345 in order to provide access mainly for 'g', 'G' and 'P'.
348 #ifdef PROCESS_SUPPORT
349 /* Need two task id pointers in order to handle Hct and Hgt commands. */
350 static int current_thread_c = 0;
351 static int current_thread_g = 0;
353 /* Need two register images in order to handle Hct and Hgt commands. The
354 variable reg_g is in addition to cris_reg above. */
355 static registers reg_g;
356 #endif /* PROCESS_SUPPORT */
358 /********************************** Breakpoint *******************************/
359 /* Use an internal stack in the breakpoint and interrupt response routines */
360 #define INTERNAL_STACK_SIZE 1024
361 char internal_stack[INTERNAL_STACK_SIZE];
363 /* Due to the breakpoint return pointer, a state variable is needed to keep
364 track of whether it is a static (compiled) or dynamic (gdb-invoked)
365 breakpoint to be handled. A static breakpoint uses the content of register
366 BRP as it is whereas a dynamic breakpoint requires subtraction with 2
367 in order to execute the instruction. The first breakpoint is static. */
368 static unsigned char is_dyn_brkp = 0;
370 /********************************* String library ****************************/
371 /* Single-step over library functions creates trap loops. */
373 /* Copy char s2[] to s1[]. */
375 gdb_cris_strcpy (char *s1, const char *s2)
379 for (s = s1; (*s++ = *s2++) != '\0'; )
384 /* Find length of s[]. */
386 gdb_cris_strlen (const char *s)
390 for (sc = s; *sc != '\0'; sc++)
395 /* Find first occurrence of c in s[n]. */
397 gdb_cris_memchr (const void *s, int c, int n)
399 const unsigned char uc = c;
400 const unsigned char *su;
402 for (su = s; 0 < n; ++su, --n)
407 /******************************* Standard library ****************************/
408 /* Single-step over library functions creates trap loops. */
409 /* Convert string to long. */
411 gdb_cris_strtol (const char *s, char **endptr, int base)
417 for (s1 = (char*)s; (sd = gdb_cris_memchr(hex_asc, *s1, base)) != NULL; ++s1)
418 x = x * base + (sd - hex_asc);
422 /* Unconverted suffix is stored in endptr unless endptr is NULL. */
429 /********************************** Packet I/O ******************************/
430 /* Returns the integer equivalent of a hexadecimal character. */
434 if ((ch >= 'a') && (ch <= 'f'))
435 return (ch - 'a' + 10);
436 if ((ch >= '0') && (ch <= '9'))
438 if ((ch >= 'A') && (ch <= 'F'))
439 return (ch - 'A' + 10);
443 /* Convert the memory, pointed to by mem into hexadecimal representation.
444 Put the result in buf, and return a pointer to the last character
448 mem2hex(char *buf, unsigned char *mem, int count)
454 /* Bogus read from m0. FIXME: What constitutes a valid address? */
455 for (i = 0; i < count; i++) {
460 /* Valid mem address. */
461 for (i = 0; i < count; i++) {
463 buf = hex_byte_pack(buf, ch);
467 /* Terminate properly. */
472 /* Convert the array, in hexadecimal representation, pointed to by buf into
473 binary representation. Put the result in mem, and return a pointer to
474 the character after the last byte written. */
475 static unsigned char*
476 hex2mem (unsigned char *mem, char *buf, int count)
480 for (i = 0; i < count; i++) {
481 ch = hex (*buf++) << 4;
482 ch = ch + hex (*buf++);
488 /* Put the content of the array, in binary representation, pointed to by buf
489 into memory pointed to by mem, and return a pointer to the character after
490 the last byte written.
491 Gdb will escape $, #, and the escape char (0x7d). */
492 static unsigned char*
493 bin2mem (unsigned char *mem, unsigned char *buf, int count)
497 for (i = 0; i < count; i++) {
498 /* Check for any escaped characters. Be paranoid and
499 only unescape chars that should be escaped. */
502 if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */
513 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
516 getpacket (char *buffer)
518 unsigned char checksum;
519 unsigned char xmitcsum;
524 while ((ch = getDebugChar ()) != '$')
525 /* Wait for the start character $ and ignore all other characters */;
529 /* Read until a # or the end of the buffer is reached */
530 while (count < BUFMAX) {
531 ch = getDebugChar ();
534 checksum = checksum + ch;
538 buffer[count] = '\0';
541 xmitcsum = hex (getDebugChar ()) << 4;
542 xmitcsum += hex (getDebugChar ());
543 if (checksum != xmitcsum) {
548 /* Correct checksum */
550 /* If sequence characters are received, reply with them */
551 if (buffer[2] == ':') {
552 putDebugChar (buffer[0]);
553 putDebugChar (buffer[1]);
554 /* Remove the sequence characters from the buffer */
555 count = gdb_cris_strlen (buffer);
556 for (i = 3; i <= count; i++)
557 buffer[i - 3] = buffer[i];
561 } while (checksum != xmitcsum);
564 /* Send $<data>#<checksum> from the <data> in the array buffer. */
567 putpacket(char *buffer)
578 /* Do run length encoding */
582 while (runlen < RUNLENMAX && *src == src[runlen]) {
586 /* Got a useful amount */
589 encode = runlen + ' ' - 4;
590 putDebugChar (encode);
599 putDebugChar(hex_asc_hi(checksum));
600 putDebugChar(hex_asc_lo(checksum));
601 } while(kgdb_started && (getDebugChar() != '+'));
604 /* The string str is prepended with the GDB printout token and sent. Required
605 in traditional implementations. */
607 putDebugString (const unsigned char *str, int length)
609 remcomOutBuffer[0] = 'O';
610 mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);
611 putpacket(remcomOutBuffer);
614 /********************************* Register image ****************************/
615 #ifdef PROCESS_SUPPORT
616 /* Copy the content of a register image into another. The size n is
617 the size of the register image. Due to struct assignment generation of
620 copy_registers (registers *dptr, registers *sptr, int n)
625 for (dreg = (unsigned char*)dptr, sreg = (unsigned char*)sptr; n > 0; n--)
629 /* Copy the stored registers from the stack. Put the register contents
630 of thread thread_id in the struct reg. */
632 copy_registers_from_stack (int thread_id, registers *regptr)
635 stack_registers *s = (stack_registers *)stack_list[thread_id];
636 unsigned int *d = (unsigned int *)regptr;
638 for (j = 13; j >= 0; j--)
640 regptr->sp = (unsigned int)stack_list[thread_id];
642 regptr->dccr = s->dccr;
643 regptr->srp = s->srp;
646 /* Copy the registers to the stack. Put the register contents of thread
647 thread_id from struct reg to the stack. */
649 copy_registers_to_stack (int thread_id, registers *regptr)
652 stack_registers *d = (stack_registers *)stack_list[thread_id];
653 unsigned int *s = (unsigned int *)regptr;
655 for (i = 0; i < 14; i++) {
659 d->dccr = regptr->dccr;
660 d->srp = regptr->srp;
664 /* Write a value to a specified register in the register image of the current
665 thread. Returns status code SUCCESS, E02 or E05. */
667 write_register (int regno, char *val)
669 int status = SUCCESS;
670 registers *current_reg = &cris_reg;
672 if (regno >= R0 && regno <= PC) {
673 /* 32-bit register with simple offset. */
674 hex2mem ((unsigned char *)current_reg + regno * sizeof(unsigned int),
675 val, sizeof(unsigned int));
677 else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {
678 /* Do not support read-only registers. */
681 else if (regno == CCR) {
682 /* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented,
683 and P7 (MOF) is 32 bits in ETRAX 100LX. */
684 hex2mem ((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),
685 val, sizeof(unsigned short));
687 else if (regno >= MOF && regno <= USP) {
688 /* 32 bit register with complex offset. (P8 has been taken care of.) */
689 hex2mem ((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),
690 val, sizeof(unsigned int));
693 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
699 #ifdef PROCESS_SUPPORT
700 /* Write a value to a specified register in the stack of a thread other
701 than the current thread. Returns status code SUCCESS or E07. */
703 write_stack_register (int thread_id, int regno, char *valptr)
705 int status = SUCCESS;
706 stack_registers *d = (stack_registers *)stack_list[thread_id];
709 hex2mem ((unsigned char *)&val, valptr, sizeof(unsigned int));
710 if (regno >= R0 && regno < SP) {
713 else if (regno == SP) {
714 stack_list[thread_id] = val;
716 else if (regno == PC) {
719 else if (regno == SRP) {
722 else if (regno == DCCR) {
726 /* Do not support registers in the current thread. */
733 /* Read a value from a specified register in the register image. Returns the
734 value in the register or -1 for non-implemented registers.
735 Should check consistency_status after a call which may be E05 after changes
736 in the implementation. */
738 read_register (char regno, unsigned int *valptr)
740 registers *current_reg = &cris_reg;
742 if (regno >= R0 && regno <= PC) {
743 /* 32-bit register with simple offset. */
744 *valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));
747 else if (regno == P0 || regno == VR) {
748 /* 8 bit register with complex offset. */
749 *valptr = (unsigned int)(*(unsigned char *)
750 ((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));
753 else if (regno == P4 || regno == CCR) {
754 /* 16 bit register with complex offset. */
755 *valptr = (unsigned int)(*(unsigned short *)
756 ((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));
759 else if (regno >= MOF && regno <= USP) {
760 /* 32 bit register with complex offset. */
761 *valptr = *(unsigned int *)((char *)&(current_reg->p8)
762 + (regno-P8) * sizeof(unsigned int));
766 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
767 consistency_status = E05;
772 /********************************** Handle exceptions ************************/
773 /* Build and send a response packet in order to inform the host the
774 stub is stopped. TAAn...:r...;n...:r...;n...:r...;
776 n... = register number (hex)
777 r... = register contents
779 r... = thread process ID. This is a hex integer.
780 n... = other string not starting with valid hex digit.
781 gdb should ignore this n,r pair and go on to the next.
782 This way we can extend the protocol. */
784 stub_is_stopped(int sigval)
786 char *ptr = remcomOutBuffer;
789 unsigned int reg_cont;
792 /* Send trap type (converted to signal) */
795 ptr = hex_byte_pack(ptr, sigval);
797 /* Send register contents. We probably only need to send the
798 * PC, frame pointer and stack pointer here. Other registers will be
799 * explicitly asked for. But for now, send all.
802 for (regno = R0; regno <= USP; regno++) {
803 /* Store n...:r...; for the registers in the buffer. */
805 status = read_register (regno, ®_cont);
807 if (status == SUCCESS) {
808 ptr = hex_byte_pack(ptr, regno);
811 ptr = mem2hex(ptr, (unsigned char *)®_cont,
812 register_size[regno]);
818 #ifdef PROCESS_SUPPORT
819 /* Store the registers of the executing thread. Assume that both step,
820 continue, and register content requests are with respect to this
821 thread. The executing task is from the operating system scheduler. */
823 current_thread_c = executing_task;
824 current_thread_g = executing_task;
826 /* A struct assignment translates into a libc memcpy call. Avoid
827 all libc functions in order to prevent recursive break points. */
828 copy_registers (®_g, &cris_reg, sizeof(registers));
830 /* Store thread:r...; with the executing task TID. */
831 gdb_cris_strcpy (&remcomOutBuffer[pos], "thread:");
832 pos += gdb_cris_strlen ("thread:");
833 remcomOutBuffer[pos++] = hex_asc_hi(executing_task);
834 remcomOutBuffer[pos++] = hex_asc_lo(executing_task);
835 gdb_cris_strcpy (&remcomOutBuffer[pos], ";");
838 /* null-terminate and send it off */
842 putpacket (remcomOutBuffer);
845 /* Performs a complete re-start from scratch. */
852 /* All expected commands are sent from remote.c. Send a response according
853 to the description in remote.c. */
855 handle_exception (int sigval)
857 /* Avoid warning of not used. */
859 USEDFUN(handle_exception);
860 USEDVAR(internal_stack[0]);
864 stub_is_stopped (sigval);
867 remcomOutBuffer[0] = '\0';
868 getpacket (remcomInBuffer);
869 switch (remcomInBuffer[0]) {
872 Success: Each byte of register data is described by two hex digits.
873 Registers are in the internal order for GDB, and the bytes
874 in a register are in the same order the machine uses.
878 #ifdef PROCESS_SUPPORT
879 /* Use the special register content in the executing thread. */
880 copy_registers (®_g, &cris_reg, sizeof(registers));
881 /* Replace the content available on the stack. */
882 if (current_thread_g != executing_task) {
883 copy_registers_from_stack (current_thread_g, ®_g);
885 mem2hex ((unsigned char *)remcomOutBuffer, (unsigned char *)®_g, sizeof(registers));
887 mem2hex(remcomOutBuffer, (char *)&cris_reg, sizeof(registers));
893 /* Write registers. GXX..XX
894 Each byte of register data is described by two hex digits.
897 #ifdef PROCESS_SUPPORT
898 hex2mem ((unsigned char *)®_g, &remcomInBuffer[1], sizeof(registers));
899 if (current_thread_g == executing_task) {
900 copy_registers (&cris_reg, ®_g, sizeof(registers));
903 copy_registers_to_stack(current_thread_g, ®_g);
906 hex2mem((char *)&cris_reg, &remcomInBuffer[1], sizeof(registers));
908 gdb_cris_strcpy (remcomOutBuffer, "OK");
912 /* Write register. Pn...=r...
913 Write register n..., hex value without 0x, with value r...,
914 which contains a hex value without 0x and two hex digits
915 for each byte in the register (target byte order). P1f=11223344 means
916 set register 31 to 44332211.
921 int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);
923 #ifdef PROCESS_SUPPORT
924 if (current_thread_g != executing_task)
925 status = write_stack_register (current_thread_g, regno, suffix+1);
928 status = write_register (regno, suffix+1);
932 /* Do not support read-only registers. */
933 gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);
936 /* Do not support non-existing registers. */
937 gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);
940 /* Do not support non-existing registers on the stack. */
941 gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);
944 /* Valid register number. */
945 gdb_cris_strcpy (remcomOutBuffer, "OK");
952 /* Read from memory. mAA..AA,LLLL
953 AA..AA is the address and LLLL is the length.
954 Success: XX..XX is the memory content. Can be fewer bytes than
955 requested if only part of the data may be read. m6000120a,6c means
956 retrieve 108 byte from base address 6000120a.
960 unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
961 &suffix, 16); int length = gdb_cris_strtol(suffix+1, 0, 16);
963 mem2hex(remcomOutBuffer, addr, length);
968 /* Write to memory. XAA..AA,LLLL:XX..XX
969 AA..AA is the start address, LLLL is the number of bytes, and
970 XX..XX is the binary data.
974 /* Write to memory. MAA..AA,LLLL:XX..XX
975 AA..AA is the start address, LLLL is the number of bytes, and
976 XX..XX is the hexadecimal data.
982 unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
984 int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);
985 if (*lenptr == ',' && *dataptr == ':') {
986 if (remcomInBuffer[0] == 'M') {
987 hex2mem(addr, dataptr + 1, length);
990 bin2mem(addr, dataptr + 1, length);
992 gdb_cris_strcpy (remcomOutBuffer, "OK");
995 gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);
1001 /* Continue execution. cAA..AA
1002 AA..AA is the address where execution is resumed. If AA..AA is
1003 omitted, resume at the present address.
1004 Success: return to the executing thread.
1005 Failure: will never know. */
1006 if (remcomInBuffer[1] != '\0') {
1007 cris_reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
1014 AA..AA is the address where execution is resumed. If AA..AA is
1015 omitted, resume at the present address. Success: return to the
1016 executing thread. Failure: will never know.
1018 Should never be invoked. The single-step is implemented on
1019 the host side. If ever invoked, it is an internal error E04. */
1020 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1021 putpacket (remcomOutBuffer);
1025 /* The last signal which caused a stop. ?
1026 Success: SAA, where AA is the signal number.
1028 remcomOutBuffer[0] = 'S';
1029 remcomOutBuffer[1] = hex_asc_hi(sigval);
1030 remcomOutBuffer[2] = hex_asc_lo(sigval);
1031 remcomOutBuffer[3] = 0;
1035 /* Detach from host. D
1036 Success: OK, and return to the executing thread.
1037 Failure: will never know */
1043 /* kill request or reset request.
1044 Success: restart of target.
1045 Failure: will never know. */
1054 /* Continue with signal sig. Csig;AA..AA
1055 Step with signal sig. Ssig;AA..AA
1056 Use the extended remote protocol. !
1057 Restart the target system. R0
1058 Toggle debug flag. d
1059 Search backwards. tAA:PP,MM
1060 Not supported: E04 */
1061 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1063 #ifdef PROCESS_SUPPORT
1066 /* Thread alive. TXX
1068 Success: OK, thread XX is alive.
1069 Failure: E03, thread XX is dead. */
1071 int thread_id = (int)gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
1072 /* Cannot tell whether it is alive or not. */
1073 if (thread_id >= 0 && thread_id < number_of_tasks)
1074 gdb_cris_strcpy (remcomOutBuffer, "OK");
1079 /* Set thread for subsequent operations: Hct
1080 c = 'c' for thread used in step and continue;
1081 t can be -1 for all threads.
1082 c = 'g' for thread used in other operations.
1083 t = 0 means pick any thread.
1087 int thread_id = gdb_cris_strtol (&remcomInBuffer[2], 0, 16);
1088 if (remcomInBuffer[1] == 'c') {
1089 /* c = 'c' for thread used in step and continue */
1090 /* Do not change current_thread_c here. It would create a mess in
1092 gdb_cris_strcpy (remcomOutBuffer, "OK");
1094 else if (remcomInBuffer[1] == 'g') {
1095 /* c = 'g' for thread used in other operations.
1096 t = 0 means pick any thread. Impossible since the scheduler does
1098 if (thread_id >= 0 && thread_id < number_of_tasks) {
1099 current_thread_g = thread_id;
1100 gdb_cris_strcpy (remcomOutBuffer, "OK");
1103 /* Not expected - send an error message. */
1104 gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1108 /* Not expected - send an error message. */
1109 gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1116 /* Query of general interest. qXXXX
1117 Set general value XXXX. QXXXX=yyyy */
1123 switch (remcomInBuffer[1]) {
1125 /* Identify the remote current thread. */
1126 gdb_cris_strcpy (&remcomOutBuffer[0], "QC");
1127 remcomOutBuffer[2] = hex_asc_hi(current_thread_c);
1128 remcomOutBuffer[3] = hex_asc_lo(current_thread_c);
1129 remcomOutBuffer[4] = '\0';
1132 gdb_cris_strcpy (&remcomOutBuffer[0], "QM");
1133 /* Reply with number of threads. */
1134 if (os_is_started()) {
1135 remcomOutBuffer[2] = hex_asc_hi(number_of_tasks);
1136 remcomOutBuffer[3] = hex_asc_lo(number_of_tasks);
1139 remcomOutBuffer[2] = hex_asc_hi(0);
1140 remcomOutBuffer[3] = hex_asc_lo(1);
1142 /* Done with the reply. */
1143 remcomOutBuffer[4] = hex_asc_lo(1);
1145 /* Expects the argument thread id. */
1146 for (; pos < (5 + HEXCHARS_IN_THREAD_ID); pos++)
1147 remcomOutBuffer[pos] = remcomInBuffer[pos];
1148 /* Reply with the thread identifiers. */
1149 if (os_is_started()) {
1150 /* Store the thread identifiers of all tasks. */
1151 for (thread_id = 0; thread_id < number_of_tasks; thread_id++) {
1152 nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1153 for (; pos < nextpos; pos ++)
1154 remcomOutBuffer[pos] = hex_asc_lo(0);
1155 remcomOutBuffer[pos++] = hex_asc_lo(thread_id);
1159 /* Store the thread identifier of the boot task. */
1160 nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1161 for (; pos < nextpos; pos ++)
1162 remcomOutBuffer[pos] = hex_asc_lo(0);
1163 remcomOutBuffer[pos++] = hex_asc_lo(current_thread_c);
1165 remcomOutBuffer[pos] = '\0';
1168 /* Not supported: "" */
1169 /* Request information about section offsets: qOffsets. */
1170 remcomOutBuffer[0] = 0;
1175 #endif /* PROCESS_SUPPORT */
1178 /* The stub should ignore other request and send an empty
1179 response ($#<checksum>). This way we can extend the protocol and GDB
1180 can tell whether the stub it is talking to uses the old or the new. */
1181 remcomOutBuffer[0] = 0;
1184 putpacket(remcomOutBuffer);
1188 /********************************** Breakpoint *******************************/
1189 /* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.
1190 An internal stack is used by the stub. The register image of the caller is
1191 stored in the structure register_image.
1192 Interactive communication with the host is handled by handle_exception and
1193 finally the register image is restored. */
1195 void kgdb_handle_breakpoint(void);
1198 " .global kgdb_handle_breakpoint\n"
1199 "kgdb_handle_breakpoint:\n"
1201 ";; Response to the break-instruction\n"
1203 ";; Create a register image of the caller\n"
1205 " move $dccr,[cris_reg+0x5E] ; Save the flags in DCCR before disable interrupts\n"
1206 " di ; Disable interrupts\n"
1207 " move.d $r0,[cris_reg] ; Save R0\n"
1208 " move.d $r1,[cris_reg+0x04] ; Save R1\n"
1209 " move.d $r2,[cris_reg+0x08] ; Save R2\n"
1210 " move.d $r3,[cris_reg+0x0C] ; Save R3\n"
1211 " move.d $r4,[cris_reg+0x10] ; Save R4\n"
1212 " move.d $r5,[cris_reg+0x14] ; Save R5\n"
1213 " move.d $r6,[cris_reg+0x18] ; Save R6\n"
1214 " move.d $r7,[cris_reg+0x1C] ; Save R7\n"
1215 " move.d $r8,[cris_reg+0x20] ; Save R8\n"
1216 " move.d $r9,[cris_reg+0x24] ; Save R9\n"
1217 " move.d $r10,[cris_reg+0x28] ; Save R10\n"
1218 " move.d $r11,[cris_reg+0x2C] ; Save R11\n"
1219 " move.d $r12,[cris_reg+0x30] ; Save R12\n"
1220 " move.d $r13,[cris_reg+0x34] ; Save R13\n"
1221 " move.d $sp,[cris_reg+0x38] ; Save SP (R14)\n"
1222 ";; Due to the old assembler-versions BRP might not be recognized\n"
1223 " .word 0xE670 ; move brp,$r0\n"
1224 " subq 2,$r0 ; Set to address of previous instruction.\n"
1225 " move.d $r0,[cris_reg+0x3c] ; Save the address in PC (R15)\n"
1226 " clear.b [cris_reg+0x40] ; Clear P0\n"
1227 " move $vr,[cris_reg+0x41] ; Save special register P1\n"
1228 " clear.w [cris_reg+0x42] ; Clear P4\n"
1229 " move $ccr,[cris_reg+0x44] ; Save special register CCR\n"
1230 " move $mof,[cris_reg+0x46] ; P7\n"
1231 " clear.d [cris_reg+0x4A] ; Clear P8\n"
1232 " move $ibr,[cris_reg+0x4E] ; P9,\n"
1233 " move $irp,[cris_reg+0x52] ; P10,\n"
1234 " move $srp,[cris_reg+0x56] ; P11,\n"
1235 " move $dtp0,[cris_reg+0x5A] ; P12, register BAR, assembler might not know BAR\n"
1236 " ; P13, register DCCR already saved\n"
1237 ";; Due to the old assembler-versions BRP might not be recognized\n"
1238 " .word 0xE670 ; move brp,r0\n"
1239 ";; Static (compiled) breakpoints must return to the next instruction in order\n"
1240 ";; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction\n"
1241 ";; in order to execute it when execution is continued.\n"
1242 " test.b [is_dyn_brkp] ; Is this a dynamic breakpoint?\n"
1243 " beq is_static ; No, a static breakpoint\n"
1245 " subq 2,$r0 ; rerun the instruction the break replaced\n"
1248 " move.b $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint\n"
1249 " move.d $r0,[cris_reg+0x62] ; Save the return address in BRP\n"
1250 " move $usp,[cris_reg+0x66] ; USP\n"
1252 ";; Handle the communication\n"
1254 " move.d internal_stack+1020,$sp ; Use the internal stack which grows upward\n"
1255 " moveq 5,$r10 ; SIGTRAP\n"
1256 " jsr handle_exception ; Interactive routine\n"
1258 ";; Return to the caller\n"
1260 " move.d [cris_reg],$r0 ; Restore R0\n"
1261 " move.d [cris_reg+0x04],$r1 ; Restore R1\n"
1262 " move.d [cris_reg+0x08],$r2 ; Restore R2\n"
1263 " move.d [cris_reg+0x0C],$r3 ; Restore R3\n"
1264 " move.d [cris_reg+0x10],$r4 ; Restore R4\n"
1265 " move.d [cris_reg+0x14],$r5 ; Restore R5\n"
1266 " move.d [cris_reg+0x18],$r6 ; Restore R6\n"
1267 " move.d [cris_reg+0x1C],$r7 ; Restore R7\n"
1268 " move.d [cris_reg+0x20],$r8 ; Restore R8\n"
1269 " move.d [cris_reg+0x24],$r9 ; Restore R9\n"
1270 " move.d [cris_reg+0x28],$r10 ; Restore R10\n"
1271 " move.d [cris_reg+0x2C],$r11 ; Restore R11\n"
1272 " move.d [cris_reg+0x30],$r12 ; Restore R12\n"
1273 " move.d [cris_reg+0x34],$r13 ; Restore R13\n"
1275 ";; FIXME: Which registers should be restored?\n"
1277 " move.d [cris_reg+0x38],$sp ; Restore SP (R14)\n"
1278 " move [cris_reg+0x56],$srp ; Restore the subroutine return pointer.\n"
1279 " move [cris_reg+0x5E],$dccr ; Restore DCCR\n"
1280 " move [cris_reg+0x66],$usp ; Restore USP\n"
1281 " jump [cris_reg+0x62] ; A jump to the content in register BRP works.\n"
1285 /* The hook for an interrupt generated by GDB. An internal stack is used
1286 by the stub. The register image of the caller is stored in the structure
1287 register_image. Interactive communication with the host is handled by
1288 handle_exception and finally the register image is restored. Due to the
1289 old assembler which does not recognise the break instruction and the
1290 breakpoint return pointer hex-code is used. */
1292 void kgdb_handle_serial(void);
1295 " .global kgdb_handle_serial\n"
1296 "kgdb_handle_serial:\n"
1298 ";; Response to a serial interrupt\n"
1301 " move $dccr,[cris_reg+0x5E] ; Save the flags in DCCR\n"
1302 " di ; Disable interrupts\n"
1303 " move.d $r0,[cris_reg] ; Save R0\n"
1304 " move.d $r1,[cris_reg+0x04] ; Save R1\n"
1305 " move.d $r2,[cris_reg+0x08] ; Save R2\n"
1306 " move.d $r3,[cris_reg+0x0C] ; Save R3\n"
1307 " move.d $r4,[cris_reg+0x10] ; Save R4\n"
1308 " move.d $r5,[cris_reg+0x14] ; Save R5\n"
1309 " move.d $r6,[cris_reg+0x18] ; Save R6\n"
1310 " move.d $r7,[cris_reg+0x1C] ; Save R7\n"
1311 " move.d $r8,[cris_reg+0x20] ; Save R8\n"
1312 " move.d $r9,[cris_reg+0x24] ; Save R9\n"
1313 " move.d $r10,[cris_reg+0x28] ; Save R10\n"
1314 " move.d $r11,[cris_reg+0x2C] ; Save R11\n"
1315 " move.d $r12,[cris_reg+0x30] ; Save R12\n"
1316 " move.d $r13,[cris_reg+0x34] ; Save R13\n"
1317 " move.d $sp,[cris_reg+0x38] ; Save SP (R14)\n"
1318 " move $irp,[cris_reg+0x3c] ; Save the address in PC (R15)\n"
1319 " clear.b [cris_reg+0x40] ; Clear P0\n"
1320 " move $vr,[cris_reg+0x41] ; Save special register P1,\n"
1321 " clear.w [cris_reg+0x42] ; Clear P4\n"
1322 " move $ccr,[cris_reg+0x44] ; Save special register CCR\n"
1323 " move $mof,[cris_reg+0x46] ; P7\n"
1324 " clear.d [cris_reg+0x4A] ; Clear P8\n"
1325 " move $ibr,[cris_reg+0x4E] ; P9,\n"
1326 " move $irp,[cris_reg+0x52] ; P10,\n"
1327 " move $srp,[cris_reg+0x56] ; P11,\n"
1328 " move $dtp0,[cris_reg+0x5A] ; P12, register BAR, assembler might not know BAR\n"
1329 " ; P13, register DCCR already saved\n"
1330 ";; Due to the old assembler-versions BRP might not be recognized\n"
1331 " .word 0xE670 ; move brp,r0\n"
1332 " move.d $r0,[cris_reg+0x62] ; Save the return address in BRP\n"
1333 " move $usp,[cris_reg+0x66] ; USP\n"
1335 ";; get the serial character (from debugport.c) and check if it is a ctrl-c\n"
1337 " jsr getDebugChar\n"
1342 " move.d [cris_reg+0x5E], $r10 ; Get DCCR\n"
1343 " btstq 8, $r10 ; Test the U-flag.\n"
1348 ";; Handle the communication\n"
1350 " move.d internal_stack+1020,$sp ; Use the internal stack\n"
1351 " moveq 2,$r10 ; SIGINT\n"
1352 " jsr handle_exception ; Interactive routine\n"
1356 ";; Return to the caller\n"
1358 " move.d [cris_reg],$r0 ; Restore R0\n"
1359 " move.d [cris_reg+0x04],$r1 ; Restore R1\n"
1360 " move.d [cris_reg+0x08],$r2 ; Restore R2\n"
1361 " move.d [cris_reg+0x0C],$r3 ; Restore R3\n"
1362 " move.d [cris_reg+0x10],$r4 ; Restore R4\n"
1363 " move.d [cris_reg+0x14],$r5 ; Restore R5\n"
1364 " move.d [cris_reg+0x18],$r6 ; Restore R6\n"
1365 " move.d [cris_reg+0x1C],$r7 ; Restore R7\n"
1366 " move.d [cris_reg+0x20],$r8 ; Restore R8\n"
1367 " move.d [cris_reg+0x24],$r9 ; Restore R9\n"
1368 " move.d [cris_reg+0x28],$r10 ; Restore R10\n"
1369 " move.d [cris_reg+0x2C],$r11 ; Restore R11\n"
1370 " move.d [cris_reg+0x30],$r12 ; Restore R12\n"
1371 " move.d [cris_reg+0x34],$r13 ; Restore R13\n"
1373 ";; FIXME: Which registers should be restored?\n"
1375 " move.d [cris_reg+0x38],$sp ; Restore SP (R14)\n"
1376 " move [cris_reg+0x56],$srp ; Restore the subroutine return pointer.\n"
1377 " move [cris_reg+0x5E],$dccr ; Restore DCCR\n"
1378 " move [cris_reg+0x66],$usp ; Restore USP\n"
1379 " reti ; Return from the interrupt routine\n"
1383 /* Use this static breakpoint in the start-up only. */
1389 is_dyn_brkp = 0; /* This is a static, not a dynamic breakpoint. */
1390 __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */
1393 /* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */
1398 /* could initialize debug port as well but it's done in head.S already... */
1400 /* breakpoint handler is now set in irq.c */
1401 set_int_vector(8, kgdb_handle_serial);
1406 /****************************** End of file **********************************/