GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / cris / arch-v10 / kernel / kgdb.c
blob2b17d0c186ea317862322fd61451bc8c6dcbc53a
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
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
20 *!---------------------------------------------------------------------------
22 *! (C) Copyright 1999, Axis Communications AB, LUND, SWEDEN
24 *!**************************************************************************/
25 /* @(#) cris_stub.c 1.3 06/17/99 */
29 * To enable debugger support, two things need to happen. One, a
30 * call to kgdb_init() is necessary in order to allow any breakpoints
31 * or error conditions to be properly intercepted and reported to gdb.
32 * Two, a breakpoint needs to be generated to begin communication. This
33 * is most easily accomplished by a call to breakpoint().
35 * The following gdb commands are supported:
37 * command function Return value
39 * g return the value of the CPU registers hex data or ENN
40 * G set the value of the CPU registers OK or ENN
42 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
43 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
45 * c Resume at current address SNN ( signal NN)
46 * cAA..AA Continue at address AA..AA SNN
48 * s Step one instruction SNN
49 * sAA..AA Step one instruction from AA..AA SNN
51 * k kill
53 * ? What was the last sigval ? SNN (signal NN)
55 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
56 * baud rate
58 * All commands and responses are sent with a packet which includes a
59 * checksum. A packet consists of
61 * $<packet info>#<checksum>.
63 * where
64 * <packet info> :: <characters representing the command or response>
65 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
67 * When a packet is received, it is first acknowledged with either '+' or '-'.
68 * '+' indicates a successful transfer. '-' indicates a failed transfer.
70 * Example:
72 * Host: Reply:
73 * $m0,10#2a +$00010203040506070809101112131415#42
78 #include <linux/string.h>
79 #include <linux/signal.h>
80 #include <linux/kernel.h>
81 #include <linux/delay.h>
82 #include <linux/linkage.h>
83 #include <linux/reboot.h>
85 #include <asm/setup.h>
86 #include <asm/ptrace.h>
88 #include <arch/svinto.h>
89 #include <asm/irq.h>
91 static int kgdb_started = 0;
93 /********************************* Register image ****************************/
94 /* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
95 Reference", p. 1-1, with the additional register definitions of the
96 ETRAX 100LX in cris-opc.h.
97 There are 16 general 32-bit registers, R0-R15, where R14 is the stack
98 pointer, SP, and R15 is the program counter, PC.
99 There are 16 special registers, P0-P15, where three of the unimplemented
100 registers, P0, P4 and P8, are reserved as zero-registers. A read from
101 any of these registers returns zero and a write has no effect. */
103 typedef
104 struct register_image
106 /* Offset */
107 unsigned int r0; /* 0x00 */
108 unsigned int r1; /* 0x04 */
109 unsigned int r2; /* 0x08 */
110 unsigned int r3; /* 0x0C */
111 unsigned int r4; /* 0x10 */
112 unsigned int r5; /* 0x14 */
113 unsigned int r6; /* 0x18 */
114 unsigned int r7; /* 0x1C */
115 unsigned int r8; /* 0x20 Frame pointer */
116 unsigned int r9; /* 0x24 */
117 unsigned int r10; /* 0x28 */
118 unsigned int r11; /* 0x2C */
119 unsigned int r12; /* 0x30 */
120 unsigned int r13; /* 0x34 */
121 unsigned int sp; /* 0x38 Stack pointer */
122 unsigned int pc; /* 0x3C Program counter */
124 unsigned char p0; /* 0x40 8-bit zero-register */
125 unsigned char vr; /* 0x41 Version register */
127 unsigned short p4; /* 0x42 16-bit zero-register */
128 unsigned short ccr; /* 0x44 Condition code register */
130 unsigned int mof; /* 0x46 Multiply overflow register */
132 unsigned int p8; /* 0x4A 32-bit zero-register */
133 unsigned int ibr; /* 0x4E Interrupt base register */
134 unsigned int irp; /* 0x52 Interrupt return pointer */
135 unsigned int srp; /* 0x56 Subroutine return pointer */
136 unsigned int bar; /* 0x5A Breakpoint address register */
137 unsigned int dccr; /* 0x5E Double condition code register */
138 unsigned int brp; /* 0x62 Breakpoint return pointer (pc in caller) */
139 unsigned int usp; /* 0x66 User mode stack pointer */
140 } registers;
142 /************** Prototypes for local library functions ***********************/
144 /* Copy of strcpy from libc. */
145 static char *gdb_cris_strcpy (char *s1, const char *s2);
147 /* Copy of strlen from libc. */
148 static int gdb_cris_strlen (const char *s);
150 /* Copy of memchr from libc. */
151 static void *gdb_cris_memchr (const void *s, int c, int n);
153 /* Copy of strtol from libc. Does only support base 16. */
154 static int gdb_cris_strtol (const char *s, char **endptr, int base);
156 /********************** Prototypes for local functions. **********************/
157 /* Copy the content of a register image into another. The size n is
158 the size of the register image. Due to struct assignment generation of
159 memcpy in libc. */
160 static void copy_registers (registers *dptr, registers *sptr, int n);
162 /* Copy the stored registers from the stack. Put the register contents
163 of thread thread_id in the struct reg. */
164 static void copy_registers_from_stack (int thread_id, registers *reg);
166 /* Copy the registers to the stack. Put the register contents of thread
167 thread_id from struct reg to the stack. */
168 static void copy_registers_to_stack (int thread_id, registers *reg);
170 /* Write a value to a specified register regno in the register image
171 of the current thread. */
172 static int write_register (int regno, char *val);
174 /* Write a value to a specified register in the stack of a thread other
175 than the current thread. */
176 static write_stack_register (int thread_id, int regno, char *valptr);
178 /* Read a value from a specified register in the register image. Returns the
179 status of the read operation. The register value is returned in valptr. */
180 static int read_register (char regno, unsigned int *valptr);
182 /* Serial port, reads one character. ETRAX 100 specific. from debugport.c */
183 int getDebugChar (void);
185 /* Serial port, writes one character. ETRAX 100 specific. from debugport.c */
186 void putDebugChar (int val);
188 void enableDebugIRQ (void);
190 /* Returns the integer equivalent of a hexadecimal character. */
191 static int hex (char ch);
193 /* Convert the memory, pointed to by mem into hexadecimal representation.
194 Put the result in buf, and return a pointer to the last character
195 in buf (null). */
196 static char *mem2hex (char *buf, unsigned char *mem, int count);
198 /* Convert the array, in hexadecimal representation, pointed to by buf into
199 binary representation. Put the result in mem, and return a pointer to
200 the character after the last byte written. */
201 static unsigned char *hex2mem (unsigned char *mem, char *buf, int count);
203 /* Put the content of the array, in binary representation, pointed to by buf
204 into memory pointed to by mem, and return a pointer to
205 the character after the last byte written. */
206 static unsigned char *bin2mem (unsigned char *mem, unsigned char *buf, int count);
208 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
209 returned. */
210 static void getpacket (char *buffer);
212 /* Send $<data>#<checksum> from the <data> in the array buffer. */
213 static void putpacket (char *buffer);
215 /* Build and send a response packet in order to inform the host the
216 stub is stopped. */
217 static void stub_is_stopped (int sigval);
219 /* All expected commands are sent from remote.c. Send a response according
220 to the description in remote.c. */
221 static void handle_exception (int sigval);
223 /* Performs a complete re-start from scratch. ETRAX specific. */
224 static void kill_restart (void);
226 /******************** Prototypes for global functions. ***********************/
228 /* The string str is prepended with the GDB printout token and sent. */
229 void putDebugString (const unsigned char *str, int length); /* used by etrax100ser.c */
231 /* The hook for both static (compiled) and dynamic breakpoints set by GDB.
232 ETRAX 100 specific. */
233 void handle_breakpoint (void); /* used by irq.c */
235 /* The hook for an interrupt generated by GDB. ETRAX 100 specific. */
236 void handle_interrupt (void); /* used by irq.c */
238 /* A static breakpoint to be used at startup. */
239 void breakpoint (void); /* called by init/main.c */
241 /* From osys_int.c, executing_task contains the number of the current
242 executing task in osys. Does not know of object-oriented threads. */
243 extern unsigned char executing_task;
245 /* The number of characters used for a 64 bit thread identifier. */
246 #define HEXCHARS_IN_THREAD_ID 16
248 /* Avoid warning as the internal_stack is not used in the C-code. */
249 #define USEDVAR(name) { if (name) { ; } }
250 #define USEDFUN(name) { void (*pf)(void) = (void *)name; USEDVAR(pf) }
252 /********************************** Packet I/O ******************************/
253 /* BUFMAX defines the maximum number of characters in
254 inbound/outbound buffers */
255 #define BUFMAX 512
257 /* Run-length encoding maximum length. Send 64 at most. */
258 #define RUNLENMAX 64
260 /* The inbound/outbound buffers used in packet I/O */
261 static char remcomInBuffer[BUFMAX];
262 static char remcomOutBuffer[BUFMAX];
264 /* Error and warning messages. */
265 enum error_type
267 SUCCESS, E01, E02, E03, E04, E05, E06, E07
269 static char *error_message[] =
272 "E01 Set current or general thread - H[c,g] - internal error.",
273 "E02 Change register content - P - cannot change read-only register.",
274 "E03 Thread is not alive.", /* T, not used. */
275 "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
276 "E05 Change register content - P - the register is not implemented..",
277 "E06 Change memory content - M - internal error.",
278 "E07 Change register content - P - the register is not stored on the stack"
280 /********************************* Register image ****************************/
281 /* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
282 Reference", p. 1-1, with the additional register definitions of the
283 ETRAX 100LX in cris-opc.h.
284 There are 16 general 32-bit registers, R0-R15, where R14 is the stack
285 pointer, SP, and R15 is the program counter, PC.
286 There are 16 special registers, P0-P15, where three of the unimplemented
287 registers, P0, P4 and P8, are reserved as zero-registers. A read from
288 any of these registers returns zero and a write has no effect. */
289 enum register_name
291 R0, R1, R2, R3,
292 R4, R5, R6, R7,
293 R8, R9, R10, R11,
294 R12, R13, SP, PC,
295 P0, VR, P2, P3,
296 P4, CCR, P6, MOF,
297 P8, IBR, IRP, SRP,
298 BAR, DCCR, BRP, USP
301 /* The register sizes of the registers in register_name. An unimplemented register
302 is designated by size 0 in this array. */
303 static int register_size[] =
305 4, 4, 4, 4,
306 4, 4, 4, 4,
307 4, 4, 4, 4,
308 4, 4, 4, 4,
309 1, 1, 0, 0,
310 2, 2, 0, 4,
311 4, 4, 4, 4,
312 4, 4, 4, 4
315 /* Contains the register image of the executing thread in the assembler
316 part of the code in order to avoid horrible addressing modes. */
317 static registers reg;
319 /* Contains the assumed consistency state of the register image. Uses the
320 enum error_type for state information. */
321 static int consistency_status = SUCCESS;
323 /********************************** Handle exceptions ************************/
324 /* The variable reg contains the register image associated with the
325 current_thread_c variable. It is a complete register image created at
326 entry. The reg_g contains a register image of a task where the general
327 registers are taken from the stack and all special registers are taken
328 from the executing task. It is associated with current_thread_g and used
329 in order to provide access mainly for 'g', 'G' and 'P'.
332 /* Need two task id pointers in order to handle Hct and Hgt commands. */
333 static int current_thread_c = 0;
334 static int current_thread_g = 0;
336 /* Need two register images in order to handle Hct and Hgt commands. The
337 variable reg_g is in addition to reg above. */
338 static registers reg_g;
340 /********************************** Breakpoint *******************************/
341 /* Use an internal stack in the breakpoint and interrupt response routines */
342 #define INTERNAL_STACK_SIZE 1024
343 static char internal_stack[INTERNAL_STACK_SIZE];
345 /* Due to the breakpoint return pointer, a state variable is needed to keep
346 track of whether it is a static (compiled) or dynamic (gdb-invoked)
347 breakpoint to be handled. A static breakpoint uses the content of register
348 BRP as it is whereas a dynamic breakpoint requires subtraction with 2
349 in order to execute the instruction. The first breakpoint is static. */
350 static unsigned char is_dyn_brkp = 0;
352 /********************************* String library ****************************/
353 /* Single-step over library functions creates trap loops. */
355 /* Copy char s2[] to s1[]. */
356 static char*
357 gdb_cris_strcpy (char *s1, const char *s2)
359 char *s = s1;
361 for (s = s1; (*s++ = *s2++) != '\0'; )
363 return (s1);
366 /* Find length of s[]. */
367 static int
368 gdb_cris_strlen (const char *s)
370 const char *sc;
372 for (sc = s; *sc != '\0'; sc++)
374 return (sc - s);
377 /* Find first occurrence of c in s[n]. */
378 static void*
379 gdb_cris_memchr (const void *s, int c, int n)
381 const unsigned char uc = c;
382 const unsigned char *su;
384 for (su = s; 0 < n; ++su, --n)
385 if (*su == uc)
386 return ((void *)su);
387 return (NULL);
389 /******************************* Standard library ****************************/
390 /* Single-step over library functions creates trap loops. */
391 /* Convert string to long. */
392 static int
393 gdb_cris_strtol (const char *s, char **endptr, int base)
395 char *s1;
396 char *sd;
397 int x = 0;
399 for (s1 = (char*)s; (sd = gdb_cris_memchr(hex_asc, *s1, base)) != NULL; ++s1)
400 x = x * base + (sd - hex_asc);
402 if (endptr)
404 /* Unconverted suffix is stored in endptr unless endptr is NULL. */
405 *endptr = s1;
408 return x;
411 /********************************* Register image ****************************/
412 /* Copy the content of a register image into another. The size n is
413 the size of the register image. Due to struct assignment generation of
414 memcpy in libc. */
415 static void
416 copy_registers (registers *dptr, registers *sptr, int n)
418 unsigned char *dreg;
419 unsigned char *sreg;
421 for (dreg = (unsigned char*)dptr, sreg = (unsigned char*)sptr; n > 0; n--)
422 *dreg++ = *sreg++;
425 #ifdef PROCESS_SUPPORT
426 /* Copy the stored registers from the stack. Put the register contents
427 of thread thread_id in the struct reg. */
428 static void
429 copy_registers_from_stack (int thread_id, registers *regptr)
431 int j;
432 stack_registers *s = (stack_registers *)stack_list[thread_id];
433 unsigned int *d = (unsigned int *)regptr;
435 for (j = 13; j >= 0; j--)
436 *d++ = s->r[j];
437 regptr->sp = (unsigned int)stack_list[thread_id];
438 regptr->pc = s->pc;
439 regptr->dccr = s->dccr;
440 regptr->srp = s->srp;
443 /* Copy the registers to the stack. Put the register contents of thread
444 thread_id from struct reg to the stack. */
445 static void
446 copy_registers_to_stack (int thread_id, registers *regptr)
448 int i;
449 stack_registers *d = (stack_registers *)stack_list[thread_id];
450 unsigned int *s = (unsigned int *)regptr;
452 for (i = 0; i < 14; i++) {
453 d->r[i] = *s++;
455 d->pc = regptr->pc;
456 d->dccr = regptr->dccr;
457 d->srp = regptr->srp;
459 #endif
461 /* Write a value to a specified register in the register image of the current
462 thread. Returns status code SUCCESS, E02 or E05. */
463 static int
464 write_register (int regno, char *val)
466 int status = SUCCESS;
467 registers *current_reg = &reg;
469 if (regno >= R0 && regno <= PC) {
470 /* 32-bit register with simple offset. */
471 hex2mem ((unsigned char *)current_reg + regno * sizeof(unsigned int),
472 val, sizeof(unsigned int));
474 else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {
475 /* Do not support read-only registers. */
476 status = E02;
478 else if (regno == CCR) {
479 /* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented,
480 and P7 (MOF) is 32 bits in ETRAX 100LX. */
481 hex2mem ((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),
482 val, sizeof(unsigned short));
484 else if (regno >= MOF && regno <= USP) {
485 /* 32 bit register with complex offset. (P8 has been taken care of.) */
486 hex2mem ((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),
487 val, sizeof(unsigned int));
489 else {
490 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
491 status = E05;
493 return status;
496 #ifdef PROCESS_SUPPORT
497 /* Write a value to a specified register in the stack of a thread other
498 than the current thread. Returns status code SUCCESS or E07. */
499 static int
500 write_stack_register (int thread_id, int regno, char *valptr)
502 int status = SUCCESS;
503 stack_registers *d = (stack_registers *)stack_list[thread_id];
504 unsigned int val;
506 hex2mem ((unsigned char *)&val, valptr, sizeof(unsigned int));
507 if (regno >= R0 && regno < SP) {
508 d->r[regno] = val;
510 else if (regno == SP) {
511 stack_list[thread_id] = val;
513 else if (regno == PC) {
514 d->pc = val;
516 else if (regno == SRP) {
517 d->srp = val;
519 else if (regno == DCCR) {
520 d->dccr = val;
522 else {
523 /* Do not support registers in the current thread. */
524 status = E07;
526 return status;
528 #endif
530 /* Read a value from a specified register in the register image. Returns the
531 value in the register or -1 for non-implemented registers.
532 Should check consistency_status after a call which may be E05 after changes
533 in the implementation. */
534 static int
535 read_register (char regno, unsigned int *valptr)
537 registers *current_reg = &reg;
539 if (regno >= R0 && regno <= PC) {
540 /* 32-bit register with simple offset. */
541 *valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));
542 return SUCCESS;
544 else if (regno == P0 || regno == VR) {
545 /* 8 bit register with complex offset. */
546 *valptr = (unsigned int)(*(unsigned char *)
547 ((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));
548 return SUCCESS;
550 else if (regno == P4 || regno == CCR) {
551 /* 16 bit register with complex offset. */
552 *valptr = (unsigned int)(*(unsigned short *)
553 ((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));
554 return SUCCESS;
556 else if (regno >= MOF && regno <= USP) {
557 /* 32 bit register with complex offset. */
558 *valptr = *(unsigned int *)((char *)&(current_reg->p8)
559 + (regno-P8) * sizeof(unsigned int));
560 return SUCCESS;
562 else {
563 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
564 consistency_status = E05;
565 return E05;
569 /********************************** Packet I/O ******************************/
570 /* Returns the integer equivalent of a hexadecimal character. */
571 static int
572 hex (char ch)
574 if ((ch >= 'a') && (ch <= 'f'))
575 return (ch - 'a' + 10);
576 if ((ch >= '0') && (ch <= '9'))
577 return (ch - '0');
578 if ((ch >= 'A') && (ch <= 'F'))
579 return (ch - 'A' + 10);
580 return (-1);
583 /* Convert the memory, pointed to by mem into hexadecimal representation.
584 Put the result in buf, and return a pointer to the last character
585 in buf (null). */
587 static int do_printk = 0;
589 static char *
590 mem2hex(char *buf, unsigned char *mem, int count)
592 int i;
593 int ch;
595 if (mem == NULL) {
596 for (i = 0; i < count; i++) {
597 *buf++ = '0';
598 *buf++ = '0';
600 } else {
601 /* Valid mem address. */
602 for (i = 0; i < count; i++) {
603 ch = *mem++;
604 buf = pack_hex_byte(buf, ch);
608 /* Terminate properly. */
609 *buf = '\0';
610 return (buf);
613 /* Convert the array, in hexadecimal representation, pointed to by buf into
614 binary representation. Put the result in mem, and return a pointer to
615 the character after the last byte written. */
616 static unsigned char*
617 hex2mem (unsigned char *mem, char *buf, int count)
619 int i;
620 unsigned char ch;
621 for (i = 0; i < count; i++) {
622 ch = hex (*buf++) << 4;
623 ch = ch + hex (*buf++);
624 *mem++ = ch;
626 return (mem);
629 /* Put the content of the array, in binary representation, pointed to by buf
630 into memory pointed to by mem, and return a pointer to the character after
631 the last byte written.
632 Gdb will escape $, #, and the escape char (0x7d). */
633 static unsigned char*
634 bin2mem (unsigned char *mem, unsigned char *buf, int count)
636 int i;
637 unsigned char *next;
638 for (i = 0; i < count; i++) {
639 /* Check for any escaped characters. Be paranoid and
640 only unescape chars that should be escaped. */
641 if (*buf == 0x7d) {
642 next = buf + 1;
643 if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */
645 buf++;
646 *buf += 0x20;
649 *mem++ = *buf++;
651 return (mem);
654 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
655 returned. */
656 static void
657 getpacket (char *buffer)
659 unsigned char checksum;
660 unsigned char xmitcsum;
661 int i;
662 int count;
663 char ch;
664 do {
665 while ((ch = getDebugChar ()) != '$')
666 /* Wait for the start character $ and ignore all other characters */;
667 checksum = 0;
668 xmitcsum = -1;
669 count = 0;
670 /* Read until a # or the end of the buffer is reached */
671 while (count < BUFMAX) {
672 ch = getDebugChar ();
673 if (ch == '#')
674 break;
675 checksum = checksum + ch;
676 buffer[count] = ch;
677 count = count + 1;
679 buffer[count] = '\0';
681 if (ch == '#') {
682 xmitcsum = hex (getDebugChar ()) << 4;
683 xmitcsum += hex (getDebugChar ());
684 if (checksum != xmitcsum) {
685 /* Wrong checksum */
686 putDebugChar ('-');
688 else {
689 /* Correct checksum */
690 putDebugChar ('+');
691 /* If sequence characters are received, reply with them */
692 if (buffer[2] == ':') {
693 putDebugChar (buffer[0]);
694 putDebugChar (buffer[1]);
695 /* Remove the sequence characters from the buffer */
696 count = gdb_cris_strlen (buffer);
697 for (i = 3; i <= count; i++)
698 buffer[i - 3] = buffer[i];
702 } while (checksum != xmitcsum);
705 /* Send $<data>#<checksum> from the <data> in the array buffer. */
707 static void
708 putpacket(char *buffer)
710 int checksum;
711 int runlen;
712 int encode;
714 do {
715 char *src = buffer;
716 putDebugChar ('$');
717 checksum = 0;
718 while (*src) {
719 /* Do run length encoding */
720 putDebugChar (*src);
721 checksum += *src;
722 runlen = 0;
723 while (runlen < RUNLENMAX && *src == src[runlen]) {
724 runlen++;
726 if (runlen > 3) {
727 /* Got a useful amount */
728 putDebugChar ('*');
729 checksum += '*';
730 encode = runlen + ' ' - 4;
731 putDebugChar (encode);
732 checksum += encode;
733 src += runlen;
735 else {
736 src++;
739 putDebugChar('#');
740 putDebugChar(hex_asc_hi(checksum));
741 putDebugChar(hex_asc_lo(checksum));
742 } while(kgdb_started && (getDebugChar() != '+'));
745 /* The string str is prepended with the GDB printout token and sent. Required
746 in traditional implementations. */
747 void
748 putDebugString (const unsigned char *str, int length)
750 remcomOutBuffer[0] = 'O';
751 mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);
752 putpacket(remcomOutBuffer);
755 /********************************** Handle exceptions ************************/
756 /* Build and send a response packet in order to inform the host the
757 stub is stopped. TAAn...:r...;n...:r...;n...:r...;
758 AA = signal number
759 n... = register number (hex)
760 r... = register contents
761 n... = `thread'
762 r... = thread process ID. This is a hex integer.
763 n... = other string not starting with valid hex digit.
764 gdb should ignore this n,r pair and go on to the next.
765 This way we can extend the protocol. */
766 static void
767 stub_is_stopped(int sigval)
769 char *ptr = remcomOutBuffer;
770 int regno;
772 unsigned int reg_cont;
773 int status;
775 /* Send trap type (converted to signal) */
777 *ptr++ = 'T';
778 ptr = pack_hex_byte(ptr, sigval);
780 /* Send register contents. We probably only need to send the
781 * PC, frame pointer and stack pointer here. Other registers will be
782 * explicitly asked for. But for now, send all.
785 for (regno = R0; regno <= USP; regno++) {
786 /* Store n...:r...; for the registers in the buffer. */
788 status = read_register (regno, &reg_cont);
790 if (status == SUCCESS) {
791 ptr = pack_hex_byte(ptr, regno);
792 *ptr++ = ':';
794 ptr = mem2hex(ptr, (unsigned char *)&reg_cont,
795 register_size[regno]);
796 *ptr++ = ';';
801 #ifdef PROCESS_SUPPORT
802 /* Store the registers of the executing thread. Assume that both step,
803 continue, and register content requests are with respect to this
804 thread. The executing task is from the operating system scheduler. */
806 current_thread_c = executing_task;
807 current_thread_g = executing_task;
809 /* A struct assignment translates into a libc memcpy call. Avoid
810 all libc functions in order to prevent recursive break points. */
811 copy_registers (&reg_g, &reg, sizeof(registers));
813 /* Store thread:r...; with the executing task TID. */
814 gdb_cris_strcpy (&remcomOutBuffer[pos], "thread:");
815 pos += gdb_cris_strlen ("thread:");
816 remcomOutBuffer[pos++] = hex_asc_hi(executing_task);
817 remcomOutBuffer[pos++] = hex_asc_lo(executing_task);
818 gdb_cris_strcpy (&remcomOutBuffer[pos], ";");
819 #endif
821 /* null-terminate and send it off */
823 *ptr = 0;
825 putpacket (remcomOutBuffer);
828 /* All expected commands are sent from remote.c. Send a response according
829 to the description in remote.c. */
830 static void
831 handle_exception (int sigval)
833 /* Avoid warning of not used. */
835 USEDFUN(handle_exception);
836 USEDVAR(internal_stack[0]);
838 /* Send response. */
840 stub_is_stopped (sigval);
842 for (;;) {
843 remcomOutBuffer[0] = '\0';
844 getpacket (remcomInBuffer);
845 switch (remcomInBuffer[0]) {
846 case 'g':
847 /* Read registers: g
848 Success: Each byte of register data is described by two hex digits.
849 Registers are in the internal order for GDB, and the bytes
850 in a register are in the same order the machine uses.
851 Failure: void. */
854 #ifdef PROCESS_SUPPORT
855 /* Use the special register content in the executing thread. */
856 copy_registers (&reg_g, &reg, sizeof(registers));
857 /* Replace the content available on the stack. */
858 if (current_thread_g != executing_task) {
859 copy_registers_from_stack (current_thread_g, &reg_g);
861 mem2hex ((unsigned char *)remcomOutBuffer, (unsigned char *)&reg_g, sizeof(registers));
862 #else
863 mem2hex(remcomOutBuffer, (char *)&reg, sizeof(registers));
864 #endif
866 break;
868 case 'G':
869 /* Write registers. GXX..XX
870 Each byte of register data is described by two hex digits.
871 Success: OK
872 Failure: void. */
873 #ifdef PROCESS_SUPPORT
874 hex2mem ((unsigned char *)&reg_g, &remcomInBuffer[1], sizeof(registers));
875 if (current_thread_g == executing_task) {
876 copy_registers (&reg, &reg_g, sizeof(registers));
878 else {
879 copy_registers_to_stack(current_thread_g, &reg_g);
881 #else
882 hex2mem((char *)&reg, &remcomInBuffer[1], sizeof(registers));
883 #endif
884 gdb_cris_strcpy (remcomOutBuffer, "OK");
885 break;
887 case 'P':
888 /* Write register. Pn...=r...
889 Write register n..., hex value without 0x, with value r...,
890 which contains a hex value without 0x and two hex digits
891 for each byte in the register (target byte order). P1f=11223344 means
892 set register 31 to 44332211.
893 Success: OK
894 Failure: E02, E05 */
896 char *suffix;
897 int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);
898 int status;
899 #ifdef PROCESS_SUPPORT
900 if (current_thread_g != executing_task)
901 status = write_stack_register (current_thread_g, regno, suffix+1);
902 else
903 #endif
904 status = write_register (regno, suffix+1);
906 switch (status) {
907 case E02:
908 /* Do not support read-only registers. */
909 gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);
910 break;
911 case E05:
912 /* Do not support non-existing registers. */
913 gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);
914 break;
915 case E07:
916 /* Do not support non-existing registers on the stack. */
917 gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);
918 break;
919 default:
920 /* Valid register number. */
921 gdb_cris_strcpy (remcomOutBuffer, "OK");
922 break;
925 break;
927 case 'm':
928 /* Read from memory. mAA..AA,LLLL
929 AA..AA is the address and LLLL is the length.
930 Success: XX..XX is the memory content. Can be fewer bytes than
931 requested if only part of the data may be read. m6000120a,6c means
932 retrieve 108 byte from base address 6000120a.
933 Failure: void. */
935 char *suffix;
936 unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
937 &suffix, 16); int length = gdb_cris_strtol(suffix+1, 0, 16);
939 mem2hex(remcomOutBuffer, addr, length);
941 break;
943 case 'X':
944 /* Write to memory. XAA..AA,LLLL:XX..XX
945 AA..AA is the start address, LLLL is the number of bytes, and
946 XX..XX is the binary data.
947 Success: OK
948 Failure: void. */
949 case 'M':
950 /* Write to memory. MAA..AA,LLLL:XX..XX
951 AA..AA is the start address, LLLL is the number of bytes, and
952 XX..XX is the hexadecimal data.
953 Success: OK
954 Failure: void. */
956 char *lenptr;
957 char *dataptr;
958 unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
959 &lenptr, 16);
960 int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);
961 if (*lenptr == ',' && *dataptr == ':') {
962 if (remcomInBuffer[0] == 'M') {
963 hex2mem(addr, dataptr + 1, length);
965 else /* X */ {
966 bin2mem(addr, dataptr + 1, length);
968 gdb_cris_strcpy (remcomOutBuffer, "OK");
970 else {
971 gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);
974 break;
976 case 'c':
977 /* Continue execution. cAA..AA
978 AA..AA is the address where execution is resumed. If AA..AA is
979 omitted, resume at the present address.
980 Success: return to the executing thread.
981 Failure: will never know. */
982 if (remcomInBuffer[1] != '\0') {
983 reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
985 enableDebugIRQ();
986 return;
988 case 's':
989 /* Step. sAA..AA
990 AA..AA is the address where execution is resumed. If AA..AA is
991 omitted, resume at the present address. Success: return to the
992 executing thread. Failure: will never know.
994 Should never be invoked. The single-step is implemented on
995 the host side. If ever invoked, it is an internal error E04. */
996 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
997 putpacket (remcomOutBuffer);
998 return;
1000 case '?':
1001 /* The last signal which caused a stop. ?
1002 Success: SAA, where AA is the signal number.
1003 Failure: void. */
1004 remcomOutBuffer[0] = 'S';
1005 remcomOutBuffer[1] = hex_asc_hi(sigval);
1006 remcomOutBuffer[2] = hex_asc_lo(sigval);
1007 remcomOutBuffer[3] = 0;
1008 break;
1010 case 'D':
1011 /* Detach from host. D
1012 Success: OK, and return to the executing thread.
1013 Failure: will never know */
1014 putpacket ("OK");
1015 return;
1017 case 'k':
1018 case 'r':
1019 /* kill request or reset request.
1020 Success: restart of target.
1021 Failure: will never know. */
1022 kill_restart ();
1023 break;
1025 case 'C':
1026 case 'S':
1027 case '!':
1028 case 'R':
1029 case 'd':
1030 /* Continue with signal sig. Csig;AA..AA
1031 Step with signal sig. Ssig;AA..AA
1032 Use the extended remote protocol. !
1033 Restart the target system. R0
1034 Toggle debug flag. d
1035 Search backwards. tAA:PP,MM
1036 Not supported: E04 */
1037 gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
1038 break;
1039 #ifdef PROCESS_SUPPORT
1041 case 'T':
1042 /* Thread alive. TXX
1043 Is thread XX alive?
1044 Success: OK, thread XX is alive.
1045 Failure: E03, thread XX is dead. */
1047 int thread_id = (int)gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
1048 /* Cannot tell whether it is alive or not. */
1049 if (thread_id >= 0 && thread_id < number_of_tasks)
1050 gdb_cris_strcpy (remcomOutBuffer, "OK");
1052 break;
1054 case 'H':
1055 /* Set thread for subsequent operations: Hct
1056 c = 'c' for thread used in step and continue;
1057 t can be -1 for all threads.
1058 c = 'g' for thread used in other operations.
1059 t = 0 means pick any thread.
1060 Success: OK
1061 Failure: E01 */
1063 int thread_id = gdb_cris_strtol (&remcomInBuffer[2], 0, 16);
1064 if (remcomInBuffer[1] == 'c') {
1065 /* c = 'c' for thread used in step and continue */
1066 /* Do not change current_thread_c here. It would create a mess in
1067 the scheduler. */
1068 gdb_cris_strcpy (remcomOutBuffer, "OK");
1070 else if (remcomInBuffer[1] == 'g') {
1071 /* c = 'g' for thread used in other operations.
1072 t = 0 means pick any thread. Impossible since the scheduler does
1073 not allow that. */
1074 if (thread_id >= 0 && thread_id < number_of_tasks) {
1075 current_thread_g = thread_id;
1076 gdb_cris_strcpy (remcomOutBuffer, "OK");
1078 else {
1079 /* Not expected - send an error message. */
1080 gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1083 else {
1084 /* Not expected - send an error message. */
1085 gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
1088 break;
1090 case 'q':
1091 case 'Q':
1093 int pos;
1094 int nextpos;
1095 int thread_id;
1097 switch (remcomInBuffer[1]) {
1098 case 'C':
1099 /* Identify the remote current thread. */
1100 gdb_cris_strcpy (&remcomOutBuffer[0], "QC");
1101 remcomOutBuffer[2] = hex_asc_hi(current_thread_c);
1102 remcomOutBuffer[3] = hex_asc_lo(current_thread_c);
1103 remcomOutBuffer[4] = '\0';
1104 break;
1105 case 'L':
1106 gdb_cris_strcpy (&remcomOutBuffer[0], "QM");
1107 /* Reply with number of threads. */
1108 if (os_is_started()) {
1109 remcomOutBuffer[2] = hex_asc_hi(number_of_tasks);
1110 remcomOutBuffer[3] = hex_asc_lo(number_of_tasks);
1112 else {
1113 remcomOutBuffer[2] = hex_asc_hi(0);
1114 remcomOutBuffer[3] = hex_asc_lo(1);
1116 /* Done with the reply. */
1117 remcomOutBuffer[4] = hex_asc_lo(1);
1118 pos = 5;
1119 /* Expects the argument thread id. */
1120 for (; pos < (5 + HEXCHARS_IN_THREAD_ID); pos++)
1121 remcomOutBuffer[pos] = remcomInBuffer[pos];
1122 /* Reply with the thread identifiers. */
1123 if (os_is_started()) {
1124 /* Store the thread identifiers of all tasks. */
1125 for (thread_id = 0; thread_id < number_of_tasks; thread_id++) {
1126 nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1127 for (; pos < nextpos; pos ++)
1128 remcomOutBuffer[pos] = hex_asc_lo(0);
1129 remcomOutBuffer[pos++] = hex_asc_lo(thread_id);
1132 else {
1133 /* Store the thread identifier of the boot task. */
1134 nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
1135 for (; pos < nextpos; pos ++)
1136 remcomOutBuffer[pos] = hex_asc_lo(0);
1137 remcomOutBuffer[pos++] = hex_asc_lo(current_thread_c);
1139 remcomOutBuffer[pos] = '\0';
1140 break;
1141 default:
1142 /* Not supported: "" */
1143 /* Request information about section offsets: qOffsets. */
1144 remcomOutBuffer[0] = 0;
1145 break;
1148 break;
1149 #endif /* PROCESS_SUPPORT */
1151 default:
1152 /* The stub should ignore other request and send an empty
1153 response ($#<checksum>). This way we can extend the protocol and GDB
1154 can tell whether the stub it is talking to uses the old or the new. */
1155 remcomOutBuffer[0] = 0;
1156 break;
1158 putpacket(remcomOutBuffer);
1162 /* Performs a complete re-start from scratch. */
1163 static void
1164 kill_restart ()
1166 machine_restart("");
1169 /********************************** Breakpoint *******************************/
1170 /* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.
1171 An internal stack is used by the stub. The register image of the caller is
1172 stored in the structure register_image.
1173 Interactive communication with the host is handled by handle_exception and
1174 finally the register image is restored. */
1176 void kgdb_handle_breakpoint(void);
1178 asm ("
1179 .global kgdb_handle_breakpoint
1180 kgdb_handle_breakpoint:
1182 ;; Response to the break-instruction
1184 ;; Create a register image of the caller
1186 move $dccr,[reg+0x5E] ; Save the flags in DCCR before disable interrupts
1187 di ; Disable interrupts
1188 move.d $r0,[reg] ; Save R0
1189 move.d $r1,[reg+0x04] ; Save R1
1190 move.d $r2,[reg+0x08] ; Save R2
1191 move.d $r3,[reg+0x0C] ; Save R3
1192 move.d $r4,[reg+0x10] ; Save R4
1193 move.d $r5,[reg+0x14] ; Save R5
1194 move.d $r6,[reg+0x18] ; Save R6
1195 move.d $r7,[reg+0x1C] ; Save R7
1196 move.d $r8,[reg+0x20] ; Save R8
1197 move.d $r9,[reg+0x24] ; Save R9
1198 move.d $r10,[reg+0x28] ; Save R10
1199 move.d $r11,[reg+0x2C] ; Save R11
1200 move.d $r12,[reg+0x30] ; Save R12
1201 move.d $r13,[reg+0x34] ; Save R13
1202 move.d $sp,[reg+0x38] ; Save SP (R14)
1203 ;; Due to the old assembler-versions BRP might not be recognized
1204 .word 0xE670 ; move brp,$r0
1205 subq 2,$r0 ; Set to address of previous instruction.
1206 move.d $r0,[reg+0x3c] ; Save the address in PC (R15)
1207 clear.b [reg+0x40] ; Clear P0
1208 move $vr,[reg+0x41] ; Save special register P1
1209 clear.w [reg+0x42] ; Clear P4
1210 move $ccr,[reg+0x44] ; Save special register CCR
1211 move $mof,[reg+0x46] ; P7
1212 clear.d [reg+0x4A] ; Clear P8
1213 move $ibr,[reg+0x4E] ; P9,
1214 move $irp,[reg+0x52] ; P10,
1215 move $srp,[reg+0x56] ; P11,
1216 move $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR
1217 ; P13, register DCCR already saved
1218 ;; Due to the old assembler-versions BRP might not be recognized
1219 .word 0xE670 ; move brp,r0
1220 ;; Static (compiled) breakpoints must return to the next instruction in order
1221 ;; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction
1222 ;; in order to execute it when execution is continued.
1223 test.b [is_dyn_brkp] ; Is this a dynamic breakpoint?
1224 beq is_static ; No, a static breakpoint
1226 subq 2,$r0 ; rerun the instruction the break replaced
1227 is_static:
1228 moveq 1,$r1
1229 move.b $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint
1230 move.d $r0,[reg+0x62] ; Save the return address in BRP
1231 move $usp,[reg+0x66] ; USP
1233 ;; Handle the communication
1235 move.d internal_stack+1020,$sp ; Use the internal stack which grows upward
1236 moveq 5,$r10 ; SIGTRAP
1237 jsr handle_exception ; Interactive routine
1239 ;; Return to the caller
1241 move.d [reg],$r0 ; Restore R0
1242 move.d [reg+0x04],$r1 ; Restore R1
1243 move.d [reg+0x08],$r2 ; Restore R2
1244 move.d [reg+0x0C],$r3 ; Restore R3
1245 move.d [reg+0x10],$r4 ; Restore R4
1246 move.d [reg+0x14],$r5 ; Restore R5
1247 move.d [reg+0x18],$r6 ; Restore R6
1248 move.d [reg+0x1C],$r7 ; Restore R7
1249 move.d [reg+0x20],$r8 ; Restore R8
1250 move.d [reg+0x24],$r9 ; Restore R9
1251 move.d [reg+0x28],$r10 ; Restore R10
1252 move.d [reg+0x2C],$r11 ; Restore R11
1253 move.d [reg+0x30],$r12 ; Restore R12
1254 move.d [reg+0x34],$r13 ; Restore R13
1256 ;; FIXME: Which registers should be restored?
1258 move.d [reg+0x38],$sp ; Restore SP (R14)
1259 move [reg+0x56],$srp ; Restore the subroutine return pointer.
1260 move [reg+0x5E],$dccr ; Restore DCCR
1261 move [reg+0x66],$usp ; Restore USP
1262 jump [reg+0x62] ; A jump to the content in register BRP works.
1263 nop ;
1266 /* The hook for an interrupt generated by GDB. An internal stack is used
1267 by the stub. The register image of the caller is stored in the structure
1268 register_image. Interactive communication with the host is handled by
1269 handle_exception and finally the register image is restored. Due to the
1270 old assembler which does not recognise the break instruction and the
1271 breakpoint return pointer hex-code is used. */
1273 void kgdb_handle_serial(void);
1275 asm ("
1276 .global kgdb_handle_serial
1277 kgdb_handle_serial:
1279 ;; Response to a serial interrupt
1282 move $dccr,[reg+0x5E] ; Save the flags in DCCR
1283 di ; Disable interrupts
1284 move.d $r0,[reg] ; Save R0
1285 move.d $r1,[reg+0x04] ; Save R1
1286 move.d $r2,[reg+0x08] ; Save R2
1287 move.d $r3,[reg+0x0C] ; Save R3
1288 move.d $r4,[reg+0x10] ; Save R4
1289 move.d $r5,[reg+0x14] ; Save R5
1290 move.d $r6,[reg+0x18] ; Save R6
1291 move.d $r7,[reg+0x1C] ; Save R7
1292 move.d $r8,[reg+0x20] ; Save R8
1293 move.d $r9,[reg+0x24] ; Save R9
1294 move.d $r10,[reg+0x28] ; Save R10
1295 move.d $r11,[reg+0x2C] ; Save R11
1296 move.d $r12,[reg+0x30] ; Save R12
1297 move.d $r13,[reg+0x34] ; Save R13
1298 move.d $sp,[reg+0x38] ; Save SP (R14)
1299 move $irp,[reg+0x3c] ; Save the address in PC (R15)
1300 clear.b [reg+0x40] ; Clear P0
1301 move $vr,[reg+0x41] ; Save special register P1,
1302 clear.w [reg+0x42] ; Clear P4
1303 move $ccr,[reg+0x44] ; Save special register CCR
1304 move $mof,[reg+0x46] ; P7
1305 clear.d [reg+0x4A] ; Clear P8
1306 move $ibr,[reg+0x4E] ; P9,
1307 move $irp,[reg+0x52] ; P10,
1308 move $srp,[reg+0x56] ; P11,
1309 move $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR
1310 ; P13, register DCCR already saved
1311 ;; Due to the old assembler-versions BRP might not be recognized
1312 .word 0xE670 ; move brp,r0
1313 move.d $r0,[reg+0x62] ; Save the return address in BRP
1314 move $usp,[reg+0x66] ; USP
1316 ;; get the serial character (from debugport.c) and check if it is a ctrl-c
1318 jsr getDebugChar
1319 cmp.b 3, $r10
1320 bne goback
1323 move.d [reg+0x5E], $r10 ; Get DCCR
1324 btstq 8, $r10 ; Test the U-flag.
1325 bmi goback
1329 ;; Handle the communication
1331 move.d internal_stack+1020,$sp ; Use the internal stack
1332 moveq 2,$r10 ; SIGINT
1333 jsr handle_exception ; Interactive routine
1335 goback:
1337 ;; Return to the caller
1339 move.d [reg],$r0 ; Restore R0
1340 move.d [reg+0x04],$r1 ; Restore R1
1341 move.d [reg+0x08],$r2 ; Restore R2
1342 move.d [reg+0x0C],$r3 ; Restore R3
1343 move.d [reg+0x10],$r4 ; Restore R4
1344 move.d [reg+0x14],$r5 ; Restore R5
1345 move.d [reg+0x18],$r6 ; Restore R6
1346 move.d [reg+0x1C],$r7 ; Restore R7
1347 move.d [reg+0x20],$r8 ; Restore R8
1348 move.d [reg+0x24],$r9 ; Restore R9
1349 move.d [reg+0x28],$r10 ; Restore R10
1350 move.d [reg+0x2C],$r11 ; Restore R11
1351 move.d [reg+0x30],$r12 ; Restore R12
1352 move.d [reg+0x34],$r13 ; Restore R13
1354 ;; FIXME: Which registers should be restored?
1356 move.d [reg+0x38],$sp ; Restore SP (R14)
1357 move [reg+0x56],$srp ; Restore the subroutine return pointer.
1358 move [reg+0x5E],$dccr ; Restore DCCR
1359 move [reg+0x66],$usp ; Restore USP
1360 reti ; Return from the interrupt routine
1364 /* Use this static breakpoint in the start-up only. */
1366 void
1367 breakpoint(void)
1369 kgdb_started = 1;
1370 is_dyn_brkp = 0; /* This is a static, not a dynamic breakpoint. */
1371 __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */
1374 /* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */
1376 void
1377 kgdb_init(void)
1379 /* could initialize debug port as well but it's done in head.S already... */
1381 /* breakpoint handler is now set in irq.c */
1382 set_int_vector(8, kgdb_handle_serial);
1384 enableDebugIRQ();
1387 /****************************** End of file **********************************/