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 */
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
53 * ? What was the last sigval ? SNN (signal NN)
55 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
58 * All commands and responses are sent with a packet which includes a
59 * checksum. A packet consists of
61 * $<packet info>#<checksum>.
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.
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>
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. */
104 struct register_image
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 */
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
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
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
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
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 */
257 /* Run-length encoding maximum length. Send 64 at most. */
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. */
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. */
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
[] =
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[]. */
357 gdb_cris_strcpy (char *s1
, const char *s2
)
361 for (s
= s1
; (*s
++ = *s2
++) != '\0'; )
366 /* Find length of s[]. */
368 gdb_cris_strlen (const char *s
)
372 for (sc
= s
; *sc
!= '\0'; sc
++)
377 /* Find first occurrence of c in s[n]. */
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
)
389 /******************************* Standard library ****************************/
390 /* Single-step over library functions creates trap loops. */
391 /* Convert string to long. */
393 gdb_cris_strtol (const char *s
, char **endptr
, int base
)
399 for (s1
= (char*)s
; (sd
= gdb_cris_memchr(hex_asc
, *s1
, base
)) != NULL
; ++s1
)
400 x
= x
* base
+ (sd
- hex_asc
);
404 /* Unconverted suffix is stored in endptr unless endptr is NULL. */
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
416 copy_registers (registers
*dptr
, registers
*sptr
, int n
)
421 for (dreg
= (unsigned char*)dptr
, sreg
= (unsigned char*)sptr
; n
> 0; n
--)
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. */
429 copy_registers_from_stack (int thread_id
, registers
*regptr
)
432 stack_registers
*s
= (stack_registers
*)stack_list
[thread_id
];
433 unsigned int *d
= (unsigned int *)regptr
;
435 for (j
= 13; j
>= 0; j
--)
437 regptr
->sp
= (unsigned int)stack_list
[thread_id
];
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. */
446 copy_registers_to_stack (int thread_id
, registers
*regptr
)
449 stack_registers
*d
= (stack_registers
*)stack_list
[thread_id
];
450 unsigned int *s
= (unsigned int *)regptr
;
452 for (i
= 0; i
< 14; i
++) {
456 d
->dccr
= regptr
->dccr
;
457 d
->srp
= regptr
->srp
;
461 /* Write a value to a specified register in the register image of the current
462 thread. Returns status code SUCCESS, E02 or E05. */
464 write_register (int regno
, char *val
)
466 int status
= SUCCESS
;
467 registers
*current_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. */
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));
490 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
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. */
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
];
506 hex2mem ((unsigned char *)&val
, valptr
, sizeof(unsigned int));
507 if (regno
>= R0
&& regno
< SP
) {
510 else if (regno
== SP
) {
511 stack_list
[thread_id
] = val
;
513 else if (regno
== PC
) {
516 else if (regno
== SRP
) {
519 else if (regno
== DCCR
) {
523 /* Do not support registers in the current thread. */
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. */
535 read_register (char regno
, unsigned int *valptr
)
537 registers
*current_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));
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)));
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)));
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));
563 /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
564 consistency_status
= E05
;
569 /********************************** Packet I/O ******************************/
570 /* Returns the integer equivalent of a hexadecimal character. */
574 if ((ch
>= 'a') && (ch
<= 'f'))
575 return (ch
- 'a' + 10);
576 if ((ch
>= '0') && (ch
<= '9'))
578 if ((ch
>= 'A') && (ch
<= 'F'))
579 return (ch
- 'A' + 10);
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
587 static int do_printk
= 0;
590 mem2hex(char *buf
, unsigned char *mem
, int count
)
596 for (i
= 0; i
< count
; i
++) {
601 /* Valid mem address. */
602 for (i
= 0; i
< count
; i
++) {
604 buf
= pack_hex_byte(buf
, ch
);
608 /* Terminate properly. */
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
)
621 for (i
= 0; i
< count
; i
++) {
622 ch
= hex (*buf
++) << 4;
623 ch
= ch
+ hex (*buf
++);
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
)
638 for (i
= 0; i
< count
; i
++) {
639 /* Check for any escaped characters. Be paranoid and
640 only unescape chars that should be escaped. */
643 if (*next
== 0x3 || *next
== 0x4 || *next
== 0x5D) /* #, $, ESC */
654 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
657 getpacket (char *buffer
)
659 unsigned char checksum
;
660 unsigned char xmitcsum
;
665 while ((ch
= getDebugChar ()) != '$')
666 /* Wait for the start character $ and ignore all other characters */;
670 /* Read until a # or the end of the buffer is reached */
671 while (count
< BUFMAX
) {
672 ch
= getDebugChar ();
675 checksum
= checksum
+ ch
;
679 buffer
[count
] = '\0';
682 xmitcsum
= hex (getDebugChar ()) << 4;
683 xmitcsum
+= hex (getDebugChar ());
684 if (checksum
!= xmitcsum
) {
689 /* Correct checksum */
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. */
708 putpacket(char *buffer
)
719 /* Do run length encoding */
723 while (runlen
< RUNLENMAX
&& *src
== src
[runlen
]) {
727 /* Got a useful amount */
730 encode
= runlen
+ ' ' - 4;
731 putDebugChar (encode
);
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. */
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...;
759 n... = register number (hex)
760 r... = register contents
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. */
767 stub_is_stopped(int sigval
)
769 char *ptr
= remcomOutBuffer
;
772 unsigned int reg_cont
;
775 /* Send trap type (converted to signal) */
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
, ®_cont
);
790 if (status
== SUCCESS
) {
791 ptr
= pack_hex_byte(ptr
, regno
);
794 ptr
= mem2hex(ptr
, (unsigned char *)®_cont
,
795 register_size
[regno
]);
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 (®_g
, ®
, 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
], ";");
821 /* null-terminate and send it off */
825 putpacket (remcomOutBuffer
);
828 /* All expected commands are sent from remote.c. Send a response according
829 to the description in remote.c. */
831 handle_exception (int sigval
)
833 /* Avoid warning of not used. */
835 USEDFUN(handle_exception
);
836 USEDVAR(internal_stack
[0]);
840 stub_is_stopped (sigval
);
843 remcomOutBuffer
[0] = '\0';
844 getpacket (remcomInBuffer
);
845 switch (remcomInBuffer
[0]) {
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.
854 #ifdef PROCESS_SUPPORT
855 /* Use the special register content in the executing thread. */
856 copy_registers (®_g
, ®
, 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
, ®_g
);
861 mem2hex ((unsigned char *)remcomOutBuffer
, (unsigned char *)®_g
, sizeof(registers
));
863 mem2hex(remcomOutBuffer
, (char *)®
, sizeof(registers
));
869 /* Write registers. GXX..XX
870 Each byte of register data is described by two hex digits.
873 #ifdef PROCESS_SUPPORT
874 hex2mem ((unsigned char *)®_g
, &remcomInBuffer
[1], sizeof(registers
));
875 if (current_thread_g
== executing_task
) {
876 copy_registers (®
, ®_g
, sizeof(registers
));
879 copy_registers_to_stack(current_thread_g
, ®_g
);
882 hex2mem((char *)®
, &remcomInBuffer
[1], sizeof(registers
));
884 gdb_cris_strcpy (remcomOutBuffer
, "OK");
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.
897 int regno
= gdb_cris_strtol (&remcomInBuffer
[1], &suffix
, 16);
899 #ifdef PROCESS_SUPPORT
900 if (current_thread_g
!= executing_task
)
901 status
= write_stack_register (current_thread_g
, regno
, suffix
+1);
904 status
= write_register (regno
, suffix
+1);
908 /* Do not support read-only registers. */
909 gdb_cris_strcpy (remcomOutBuffer
, error_message
[E02
]);
912 /* Do not support non-existing registers. */
913 gdb_cris_strcpy (remcomOutBuffer
, error_message
[E05
]);
916 /* Do not support non-existing registers on the stack. */
917 gdb_cris_strcpy (remcomOutBuffer
, error_message
[E07
]);
920 /* Valid register number. */
921 gdb_cris_strcpy (remcomOutBuffer
, "OK");
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.
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
);
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.
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.
958 unsigned char *addr
= (unsigned char *)gdb_cris_strtol(&remcomInBuffer
[1],
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
);
966 bin2mem(addr
, dataptr
+ 1, length
);
968 gdb_cris_strcpy (remcomOutBuffer
, "OK");
971 gdb_cris_strcpy (remcomOutBuffer
, error_message
[E06
]);
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);
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
);
1001 /* The last signal which caused a stop. ?
1002 Success: SAA, where AA is the signal number.
1004 remcomOutBuffer
[0] = 'S';
1005 remcomOutBuffer
[1] = hex_asc_hi(sigval
);
1006 remcomOutBuffer
[2] = hex_asc_lo(sigval
);
1007 remcomOutBuffer
[3] = 0;
1011 /* Detach from host. D
1012 Success: OK, and return to the executing thread.
1013 Failure: will never know */
1019 /* kill request or reset request.
1020 Success: restart of target.
1021 Failure: will never know. */
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
]);
1039 #ifdef PROCESS_SUPPORT
1042 /* Thread alive. TXX
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");
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.
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
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
1074 if (thread_id
>= 0 && thread_id
< number_of_tasks
) {
1075 current_thread_g
= thread_id
;
1076 gdb_cris_strcpy (remcomOutBuffer
, "OK");
1079 /* Not expected - send an error message. */
1080 gdb_cris_strcpy (remcomOutBuffer
, error_message
[E01
]);
1084 /* Not expected - send an error message. */
1085 gdb_cris_strcpy (remcomOutBuffer
, error_message
[E01
]);
1097 switch (remcomInBuffer
[1]) {
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';
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
);
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);
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
);
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';
1142 /* Not supported: "" */
1143 /* Request information about section offsets: qOffsets. */
1144 remcomOutBuffer
[0] = 0;
1149 #endif /* PROCESS_SUPPORT */
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;
1158 putpacket(remcomOutBuffer
);
1162 /* Performs a complete re-start from scratch. */
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);
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
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.
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);
1276 .global 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
1323 move.d [reg+0x5E], $r10 ; Get DCCR
1324 btstq 8, $r10 ; Test the U-flag.
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
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. */
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 */
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
);
1387 /****************************** End of file **********************************/