2 * arch/cris/arch-v32/kernel/kgdb.c
4 * CRIS v32 version by Orjan Friberg, Axis Communications AB.
7 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
8 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
10 * Originally written by Glenn Engel, Lake Stevens Instrument Division
12 * Contributed by HP Systems
14 * Modified for SPARC by Stu Grossman, Cygnus Support.
16 * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
17 * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
19 * Copyright (C) 1995 Andreas Busse
25 * To enable debugger support, two things need to happen. One, a
26 * call to kgdb_init() is necessary in order to allow any breakpoints
27 * or error conditions to be properly intercepted and reported to gdb.
28 * Two, a breakpoint needs to be generated to begin communication. This
29 * is most easily accomplished by a call to breakpoint().
31 * The following gdb commands are supported:
33 * command function Return value
35 * g return the value of the CPU registers hex data or ENN
36 * G set the value of the CPU registers OK or ENN
38 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
39 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
41 * c Resume at current address SNN ( signal NN)
42 * cAA..AA Continue at address AA..AA SNN
44 * s Step one instruction SNN
45 * sAA..AA Step one instruction from AA..AA SNN
49 * ? What was the last sigval ? SNN (signal NN)
51 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
54 * All commands and responses are sent with a packet which includes a
55 * checksum. A packet consists of
57 * $<packet info>#<checksum>.
60 * <packet info> :: <characters representing the command or response>
61 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
63 * When a packet is received, it is first acknowledged with either '+' or '-'.
64 * '+' indicates a successful transfer. '-' indicates a failed transfer.
69 * $m0,10#2a +$00010203040506070809101112131415#42
74 #include <linux/string.h>
75 #include <linux/signal.h>
76 #include <linux/kernel.h>
77 #include <linux/delay.h>
78 #include <linux/linkage.h>
79 #include <linux/reboot.h>
81 #include <asm/setup.h>
82 #include <asm/ptrace.h>
85 #include <hwregs/reg_map.h>
86 #include <hwregs/reg_rdwr.h>
87 #include <hwregs/intr_vect_defs.h>
88 #include <hwregs/ser_defs.h>
91 extern void gdb_handle_exception(void);
92 /* From kgdb_asm.S. */
93 extern void kgdb_handle_exception(void);
95 static int kgdb_started
= 0;
97 /********************************* Register image ****************************/
100 struct register_image
103 unsigned int r0
; /* 0x00 */
104 unsigned int r1
; /* 0x04 */
105 unsigned int r2
; /* 0x08 */
106 unsigned int r3
; /* 0x0C */
107 unsigned int r4
; /* 0x10 */
108 unsigned int r5
; /* 0x14 */
109 unsigned int r6
; /* 0x18 */
110 unsigned int r7
; /* 0x1C */
111 unsigned int r8
; /* 0x20; Frame pointer (if any) */
112 unsigned int r9
; /* 0x24 */
113 unsigned int r10
; /* 0x28 */
114 unsigned int r11
; /* 0x2C */
115 unsigned int r12
; /* 0x30 */
116 unsigned int r13
; /* 0x34 */
117 unsigned int sp
; /* 0x38; R14, Stack pointer */
118 unsigned int acr
; /* 0x3C; R15, Address calculation register. */
120 unsigned char bz
; /* 0x40; P0, 8-bit zero register */
121 unsigned char vr
; /* 0x41; P1, Version register (8-bit) */
122 unsigned int pid
; /* 0x42; P2, Process ID */
123 unsigned char srs
; /* 0x46; P3, Support register select (8-bit) */
124 unsigned short wz
; /* 0x47; P4, 16-bit zero register */
125 unsigned int exs
; /* 0x49; P5, Exception status */
126 unsigned int eda
; /* 0x4D; P6, Exception data address */
127 unsigned int mof
; /* 0x51; P7, Multiply overflow register */
128 unsigned int dz
; /* 0x55; P8, 32-bit zero register */
129 unsigned int ebp
; /* 0x59; P9, Exception base pointer */
130 unsigned int erp
; /* 0x5D; P10, Exception return pointer. Contains the PC we are interested in. */
131 unsigned int srp
; /* 0x61; P11, Subroutine return pointer */
132 unsigned int nrp
; /* 0x65; P12, NMI return pointer */
133 unsigned int ccs
; /* 0x69; P13, Condition code stack */
134 unsigned int usp
; /* 0x6D; P14, User mode stack pointer */
135 unsigned int spc
; /* 0x71; P15, Single step PC */
136 unsigned int pc
; /* 0x75; Pseudo register (for the most part set to ERP). */
141 struct bp_register_image
143 /* Support register bank 0. */
161 /* Support register bank 1. */
179 /* Support register bank 2. */
197 /* Support register bank 3. */
198 unsigned int s0_3
; /* BP_CTRL */
199 unsigned int s1_3
; /* BP_I0_START */
200 unsigned int s2_3
; /* BP_I0_END */
201 unsigned int s3_3
; /* BP_D0_START */
202 unsigned int s4_3
; /* BP_D0_END */
203 unsigned int s5_3
; /* BP_D1_START */
204 unsigned int s6_3
; /* BP_D1_END */
205 unsigned int s7_3
; /* BP_D2_START */
206 unsigned int s8_3
; /* BP_D2_END */
207 unsigned int s9_3
; /* BP_D3_START */
208 unsigned int s10_3
; /* BP_D3_END */
209 unsigned int s11_3
; /* BP_D4_START */
210 unsigned int s12_3
; /* BP_D4_END */
211 unsigned int s13_3
; /* BP_D5_START */
212 unsigned int s14_3
; /* BP_D5_END */
213 unsigned int s15_3
; /* BP_RESERVED */
237 /* The register sizes of the registers in register_name. An unimplemented register
238 is designated by size 0 in this array. */
239 static int register_size
[] =
260 /* Contains the register image of the kernel.
261 (Global so that they can be reached from assembler code.) */
263 support_registers sreg
;
265 /************** Prototypes for local library functions ***********************/
267 /* Copy of strcpy from libc. */
268 static char *gdb_cris_strcpy(char *s1
, const char *s2
);
270 /* Copy of strlen from libc. */
271 static int gdb_cris_strlen(const char *s
);
273 /* Copy of memchr from libc. */
274 static void *gdb_cris_memchr(const void *s
, int c
, int n
);
276 /* Copy of strtol from libc. Does only support base 16. */
277 static int gdb_cris_strtol(const char *s
, char **endptr
, int base
);
279 /********************** Prototypes for local functions. **********************/
281 /* Write a value to a specified register regno in the register image
282 of the current thread. */
283 static int write_register(int regno
, char *val
);
285 /* Read a value from a specified register in the register image. Returns the
286 status of the read operation. The register value is returned in valptr. */
287 static int read_register(char regno
, unsigned int *valptr
);
289 /* Serial port, reads one character. ETRAX 100 specific. from debugport.c */
290 int getDebugChar(void);
292 #ifdef CONFIG_ETRAX_VCS_SIM
293 int getDebugChar(void)
299 /* Serial port, writes one character. ETRAX 100 specific. from debugport.c */
300 void putDebugChar(int val
);
302 #ifdef CONFIG_ETRAX_VCS_SIM
303 void putDebugChar(int val
)
305 socketwrite((char *)&val
, 1);
309 /* Returns the integer equivalent of a hexadecimal character. */
310 static int hex(char ch
);
312 /* Convert the memory, pointed to by mem into hexadecimal representation.
313 Put the result in buf, and return a pointer to the last character
315 static char *mem2hex(char *buf
, unsigned char *mem
, int count
);
317 /* Convert the array, in hexadecimal representation, pointed to by buf into
318 binary representation. Put the result in mem, and return a pointer to
319 the character after the last byte written. */
320 static unsigned char *hex2mem(unsigned char *mem
, char *buf
, int count
);
322 /* Put the content of the array, in binary representation, pointed to by buf
323 into memory pointed to by mem, and return a pointer to
324 the character after the last byte written. */
325 static unsigned char *bin2mem(unsigned char *mem
, unsigned char *buf
, int count
);
327 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
329 static void getpacket(char *buffer
);
331 /* Send $<data>#<checksum> from the <data> in the array buffer. */
332 static void putpacket(char *buffer
);
334 /* Build and send a response packet in order to inform the host the
336 static void stub_is_stopped(int sigval
);
338 /* All expected commands are sent from remote.c. Send a response according
339 to the description in remote.c. Not static since it needs to be reached
340 from assembler code. */
341 void handle_exception(int sigval
);
343 /* Performs a complete re-start from scratch. ETRAX specific. */
344 static void kill_restart(void);
346 /******************** Prototypes for global functions. ***********************/
348 /* The string str is prepended with the GDB printout token and sent. */
349 void putDebugString(const unsigned char *str
, int len
);
351 /* A static breakpoint to be used at startup. */
352 void breakpoint(void);
354 /* Avoid warning as the internal_stack is not used in the C-code. */
355 #define USEDVAR(name) { if (name) { ; } }
356 #define USEDFUN(name) { void (*pf)(void) = (void *)name; USEDVAR(pf) }
358 /********************************** Packet I/O ******************************/
359 /* BUFMAX defines the maximum number of characters in
360 inbound/outbound buffers */
363 /* Run-length encoding maximum length. Send 64 at most. */
366 /* The inbound/outbound buffers used in packet I/O */
367 static char input_buffer
[BUFMAX
];
368 static char output_buffer
[BUFMAX
];
370 /* Error and warning messages. */
373 SUCCESS
, E01
, E02
, E03
, E04
, E05
, E06
,
376 static char *error_message
[] =
379 "E01 Set current or general thread - H[c,g] - internal error.",
380 "E02 Change register content - P - cannot change read-only register.",
381 "E03 Thread is not alive.", /* T, not used. */
382 "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
383 "E05 Change register content - P - the register is not implemented..",
384 "E06 Change memory content - M - internal error.",
387 /********************************** Breakpoint *******************************/
388 #define INTERNAL_STACK_SIZE 1024
389 char internal_stack
[INTERNAL_STACK_SIZE
];
391 /* Due to the breakpoint return pointer, a state variable is needed to keep
392 track of whether it is a static (compiled) or dynamic (gdb-invoked)
393 breakpoint to be handled. A static breakpoint uses the content of register
394 ERP as it is whereas a dynamic breakpoint requires subtraction with 2
395 in order to execute the instruction. The first breakpoint is static; all
396 following are assumed to be dynamic. */
397 static int dynamic_bp
= 0;
399 /********************************* String library ****************************/
400 /* Single-step over library functions creates trap loops. */
402 /* Copy char s2[] to s1[]. */
404 gdb_cris_strcpy(char *s1
, const char *s2
)
408 for (s
= s1
; (*s
++ = *s2
++) != '\0'; )
413 /* Find length of s[]. */
415 gdb_cris_strlen(const char *s
)
419 for (sc
= s
; *sc
!= '\0'; sc
++)
424 /* Find first occurrence of c in s[n]. */
426 gdb_cris_memchr(const void *s
, int c
, int n
)
428 const unsigned char uc
= c
;
429 const unsigned char *su
;
431 for (su
= s
; 0 < n
; ++su
, --n
)
436 /******************************* Standard library ****************************/
437 /* Single-step over library functions creates trap loops. */
438 /* Convert string to long. */
440 gdb_cris_strtol(const char *s
, char **endptr
, int base
)
446 for (s1
= (char*)s
; (sd
= gdb_cris_memchr(hex_asc
, *s1
, base
)) != NULL
; ++s1
)
447 x
= x
* base
+ (sd
- hex_asc
);
450 /* Unconverted suffix is stored in endptr unless endptr is NULL. */
457 /********************************* Register image ****************************/
459 /* Write a value to a specified register in the register image of the current
460 thread. Returns status code SUCCESS, E02 or E05. */
462 write_register(int regno
, char *val
)
464 int status
= SUCCESS
;
466 if (regno
>= R0
&& regno
<= ACR
) {
467 /* Consecutive 32-bit registers. */
468 hex2mem((unsigned char *)®
.r0
+ (regno
- R0
) * sizeof(unsigned int),
469 val
, sizeof(unsigned int));
471 } else if (regno
== BZ
|| regno
== VR
|| regno
== WZ
|| regno
== DZ
) {
472 /* Read-only registers. */
475 } else if (regno
== PID
) {
476 /* 32-bit register. (Even though we already checked SRS and WZ, we cannot
477 combine this with the EXS - SPC write since SRS and WZ have different size.) */
478 hex2mem((unsigned char *)®
.pid
, val
, sizeof(unsigned int));
480 } else if (regno
== SRS
) {
481 /* 8-bit register. */
482 hex2mem((unsigned char *)®
.srs
, val
, sizeof(unsigned char));
484 } else if (regno
>= EXS
&& regno
<= SPC
) {
485 /* Consecutive 32-bit registers. */
486 hex2mem((unsigned char *)®
.exs
+ (regno
- EXS
) * sizeof(unsigned int),
487 val
, sizeof(unsigned int));
489 } else if (regno
== PC
) {
490 /* Pseudo-register. Treat as read-only. */
493 } else if (regno
>= S0
&& regno
<= S15
) {
494 /* 32-bit registers. */
495 hex2mem((unsigned char *)&sreg
.s0_0
+ (reg
.srs
* 16 * sizeof(unsigned int)) + (regno
- S0
) * sizeof(unsigned int), val
, sizeof(unsigned int));
497 /* Non-existing register. */
503 /* Read a value from a specified register in the register image. Returns the
504 value in the register or -1 for non-implemented registers. */
506 read_register(char regno
, unsigned int *valptr
)
508 int status
= SUCCESS
;
510 /* We read the zero registers from the register struct (instead of just returning 0)
513 if (regno
>= R0
&& regno
<= ACR
) {
514 /* Consecutive 32-bit registers. */
515 *valptr
= *(unsigned int *)((char *)®
.r0
+ (regno
- R0
) * sizeof(unsigned int));
517 } else if (regno
== BZ
|| regno
== VR
) {
518 /* Consecutive 8-bit registers. */
519 *valptr
= (unsigned int)(*(unsigned char *)
520 ((char *)®
.bz
+ (regno
- BZ
) * sizeof(char)));
522 } else if (regno
== PID
) {
523 /* 32-bit register. */
524 *valptr
= *(unsigned int *)((char *)®
.pid
);
526 } else if (regno
== SRS
) {
527 /* 8-bit register. */
528 *valptr
= (unsigned int)(*(unsigned char *)((char *)®
.srs
));
530 } else if (regno
== WZ
) {
531 /* 16-bit register. */
532 *valptr
= (unsigned int)(*(unsigned short *)(char *)®
.wz
);
534 } else if (regno
>= EXS
&& regno
<= PC
) {
535 /* Consecutive 32-bit registers. */
536 *valptr
= *(unsigned int *)((char *)®
.exs
+ (regno
- EXS
) * sizeof(unsigned int));
538 } else if (regno
>= S0
&& regno
<= S15
) {
539 /* Consecutive 32-bit registers, located elsewhere. */
540 *valptr
= *(unsigned int *)((char *)&sreg
.s0_0
+ (reg
.srs
* 16 * sizeof(unsigned int)) + (regno
- S0
) * sizeof(unsigned int));
543 /* Non-existing register. */
550 /********************************** Packet I/O ******************************/
551 /* Returns the integer equivalent of a hexadecimal character. */
555 if ((ch
>= 'a') && (ch
<= 'f'))
556 return (ch
- 'a' + 10);
557 if ((ch
>= '0') && (ch
<= '9'))
559 if ((ch
>= 'A') && (ch
<= 'F'))
560 return (ch
- 'A' + 10);
564 /* Convert the memory, pointed to by mem into hexadecimal representation.
565 Put the result in buf, and return a pointer to the last character
569 mem2hex(char *buf
, unsigned char *mem
, int count
)
575 /* Invalid address, caught by 'm' packet handler. */
576 for (i
= 0; i
< count
; i
++) {
581 /* Valid mem address. */
582 for (i
= 0; i
< count
; i
++) {
584 buf
= pack_hex_byte(buf
, ch
);
587 /* Terminate properly. */
592 /* Same as mem2hex, but puts it in network byte order. */
594 mem2hex_nbo(char *buf
, unsigned char *mem
, int count
)
600 for (i
= 0; i
< count
; i
++) {
602 buf
= pack_hex_byte(buf
, ch
);
605 /* Terminate properly. */
610 /* Convert the array, in hexadecimal representation, pointed to by buf into
611 binary representation. Put the result in mem, and return a pointer to
612 the character after the last byte written. */
613 static unsigned char*
614 hex2mem(unsigned char *mem
, char *buf
, int count
)
618 for (i
= 0; i
< count
; i
++) {
619 ch
= hex (*buf
++) << 4;
620 ch
= ch
+ hex (*buf
++);
626 /* Put the content of the array, in binary representation, pointed to by buf
627 into memory pointed to by mem, and return a pointer to the character after
628 the last byte written.
629 Gdb will escape $, #, and the escape char (0x7d). */
630 static unsigned char*
631 bin2mem(unsigned char *mem
, unsigned char *buf
, int count
)
635 for (i
= 0; i
< count
; i
++) {
636 /* Check for any escaped characters. Be paranoid and
637 only unescape chars that should be escaped. */
640 if (*next
== 0x3 || *next
== 0x4 || *next
== 0x5D) {
651 /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
654 getpacket(char *buffer
)
656 unsigned char checksum
;
657 unsigned char xmitcsum
;
663 while((ch
= getDebugChar ()) != '$')
664 /* Wait for the start character $ and ignore all other characters */;
668 /* Read until a # or the end of the buffer is reached */
669 while (count
< BUFMAX
) {
673 checksum
= checksum
+ ch
;
684 xmitcsum
= hex(getDebugChar()) << 4;
685 xmitcsum
+= hex(getDebugChar());
686 if (checksum
!= xmitcsum
) {
690 /* Correct checksum */
692 /* If sequence characters are received, reply with them */
693 if (buffer
[2] == ':') {
694 putDebugChar(buffer
[0]);
695 putDebugChar(buffer
[1]);
696 /* Remove the sequence characters from the buffer */
697 count
= gdb_cris_strlen(buffer
);
698 for (i
= 3; i
<= count
; i
++)
699 buffer
[i
- 3] = buffer
[i
];
703 } while (checksum
!= xmitcsum
);
706 /* Send $<data>#<checksum> from the <data> in the array buffer. */
709 putpacket(char *buffer
)
720 /* Do run length encoding */
724 while (runlen
< RUNLENMAX
&& *src
== src
[runlen
]) {
728 /* Got a useful amount */
731 encode
= runlen
+ ' ' - 4;
732 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 len
)
750 /* Move SPC forward if we are single-stepping. */
752 asm("move $spc, $r10");
753 asm("cmp.d spchere, $r10");
756 asm("move.d spccont, $r10");
757 asm("move $r10, $spc");
760 output_buffer
[0] = 'O';
761 mem2hex(&output_buffer
[1], (unsigned char *)str
, len
);
762 putpacket(output_buffer
);
767 /********************************** Handle exceptions ************************/
768 /* Build and send a response packet in order to inform the host the
769 stub is stopped. TAAn...:r...;n...:r...;n...:r...;
771 n... = register number (hex)
772 r... = register contents
774 r... = thread process ID. This is a hex integer.
775 n... = other string not starting with valid hex digit.
776 gdb should ignore this n,r pair and go on to the next.
777 This way we can extend the protocol. */
779 stub_is_stopped(int sigval
)
781 char *ptr
= output_buffer
;
782 unsigned int reg_cont
;
784 /* Send trap type (converted to signal) */
787 ptr
= pack_hex_byte(ptr
, sigval
);
789 if (((reg
.exs
& 0xff00) >> 8) == 0xc) {
791 /* Some kind of hardware watchpoint triggered. Find which one
792 and determine its type (read/write/access). */
793 int S
, bp
, trig_bits
= 0, rw_bits
= 0;
795 unsigned int *bp_d_regs
= &sreg
.s3_3
;
796 /* In a lot of cases, the stopped data address will simply be EDA.
797 In some cases, we adjust it to match the watched data range.
798 (We don't want to change the actual EDA though). */
799 unsigned int stopped_data_address
;
800 /* The S field of EXS. */
801 S
= (reg
.exs
& 0xffff0000) >> 16;
804 /* Instruction watchpoint. */
806 /* Data watchpoint. Find the one that triggered. */
807 for (bp
= 0; bp
< 6; bp
++) {
809 /* Dx_RD, Dx_WR in the S field of EXS for this BP. */
810 int bitpos_trig
= 1 + bp
* 2;
811 /* Dx_BPRD, Dx_BPWR in BP_CTRL for this BP. */
812 int bitpos_config
= 2 + bp
* 4;
814 /* Get read/write trig bits for this BP. */
815 trig_bits
= (S
& (3 << bitpos_trig
)) >> bitpos_trig
;
817 /* Read/write config bits for this BP. */
818 rw_bits
= (sreg
.s0_3
& (3 << bitpos_config
)) >> bitpos_config
;
820 /* Sanity check: the BP shouldn't trigger for accesses
821 that it isn't configured for. */
822 if ((rw_bits
== 0x1 && trig_bits
!= 0x1) ||
823 (rw_bits
== 0x2 && trig_bits
!= 0x2))
824 panic("Invalid r/w trigging for this BP");
826 /* Mark this BP as trigged for future reference. */
827 trig_mask
|= (1 << bp
);
829 if (reg
.eda
>= bp_d_regs
[bp
* 2] &&
830 reg
.eda
<= bp_d_regs
[bp
* 2 + 1]) {
831 /* EDA withing range for this BP; it must be the one
832 we're looking for. */
833 stopped_data_address
= reg
.eda
;
839 /* Found a trigged BP with EDA within its configured data range. */
840 } else if (trig_mask
) {
841 /* Something triggered, but EDA doesn't match any BP's range. */
842 for (bp
= 0; bp
< 6; bp
++) {
843 /* Dx_BPRD, Dx_BPWR in BP_CTRL for this BP. */
844 int bitpos_config
= 2 + bp
* 4;
846 /* Read/write config bits for this BP (needed later). */
847 rw_bits
= (sreg
.s0_3
& (3 << bitpos_config
)) >> bitpos_config
;
849 if (trig_mask
& (1 << bp
)) {
850 /* EDA within 31 bytes of the configured start address? */
851 if (reg
.eda
+ 31 >= bp_d_regs
[bp
* 2]) {
852 /* Changing the reported address to match
853 the start address of the first applicable BP. */
854 stopped_data_address
= bp_d_regs
[bp
* 2];
857 /* We continue since we might find another useful BP. */
858 printk("EDA doesn't match trigged BP's range");
866 /* Note that we report the type according to what the BP is configured
867 for (otherwise we'd never report an 'awatch'), not according to how
868 it trigged. We did check that the trigged bits match what the BP is
869 configured for though. */
870 if (rw_bits
== 0x1) {
872 strncpy(ptr
, "rwatch", 6);
874 } else if (rw_bits
== 0x2) {
876 strncpy(ptr
, "watch", 5);
878 } else if (rw_bits
== 0x3) {
880 strncpy(ptr
, "awatch", 6);
883 panic("Invalid r/w bits for this BP.");
887 /* Note that we don't read_register(EDA, ...) */
888 ptr
= mem2hex_nbo(ptr
, (unsigned char *)&stopped_data_address
, register_size
[EDA
]);
892 /* Only send PC, frame and stack pointer. */
893 read_register(PC
, ®_cont
);
894 ptr
= pack_hex_byte(ptr
, PC
);
896 ptr
= mem2hex(ptr
, (unsigned char *)®_cont
, register_size
[PC
]);
899 read_register(R8
, ®_cont
);
900 ptr
= pack_hex_byte(ptr
, R8
);
902 ptr
= mem2hex(ptr
, (unsigned char *)®_cont
, register_size
[R8
]);
905 read_register(SP
, ®_cont
);
906 ptr
= pack_hex_byte(ptr
, SP
);
908 ptr
= mem2hex(ptr
, (unsigned char *)®_cont
, register_size
[SP
]);
911 /* Send ERP as well; this will save us an entire register fetch in some cases. */
912 read_register(ERP
, ®_cont
);
913 ptr
= pack_hex_byte(ptr
, ERP
);
915 ptr
= mem2hex(ptr
, (unsigned char *)®_cont
, register_size
[ERP
]);
918 /* null-terminate and send it off */
920 putpacket(output_buffer
);
923 /* Returns the size of an instruction that has a delay slot. */
925 int insn_size(unsigned long pc
)
927 unsigned short opcode
= *(unsigned short *)pc
;
930 switch ((opcode
& 0x0f00) >> 8) {
941 /* Could be 4 or 6; check more bits. */
942 if ((opcode
& 0xff) == 0xff)
948 panic("Couldn't find size of opcode 0x%x at 0x%lx\n", opcode
, pc
);
954 void register_fixup(int sigval
)
956 /* Compensate for ACR push at the beginning of exception handler. */
962 /* Delay slot bit set. Report as stopped on proper instruction. */
964 /* Rely on SPC if set. */
967 /* Calculate the PC from the size of the instruction
968 that the delay slot we're in belongs to. */
969 reg
.pc
+= insn_size(reg
.erp
& ~1) - 1 ;
973 if ((reg
.exs
& 0x3) == 0x0) {
974 /* Bits 1 - 0 indicate the type of memory operation performed
975 by the interrupted instruction. 0 means no memory operation,
976 and EDA is undefined in that case. We zero it to avoid confusion. */
980 if (sigval
== SIGTRAP
) {
981 /* Break 8, single step or hardware breakpoint exception. */
983 /* Check IDX field of EXS. */
984 if (((reg
.exs
& 0xff00) >> 8) == 0x18) {
988 /* Static (compiled) breakpoints must return to the next instruction
989 in order to avoid infinite loops (default value of ERP). Dynamic
990 (gdb-invoked) must subtract the size of the break instruction from
991 the ERP so that the instruction that was originally in the break
992 instruction's place will be run when we return from the exception. */
994 /* Assuming that all breakpoints are dynamic from now on. */
998 /* Only if not in a delay slot. */
999 if (!(reg
.erp
& 0x1)) {
1005 } else if (((reg
.exs
& 0xff00) >> 8) == 0x3) {
1007 /* Don't fiddle with S1. */
1009 } else if (((reg
.exs
& 0xff00) >> 8) == 0xc) {
1011 /* Hardware watchpoint exception. */
1013 /* SPC has been updated so that we will get a single step exception
1014 when we return, but we don't want that. */
1017 /* Don't fiddle with S1. */
1020 } else if (sigval
== SIGINT
) {
1021 /* Nothing special. */
1025 static void insert_watchpoint(char type
, int addr
, int len
)
1027 /* Breakpoint/watchpoint types (GDB terminology):
1028 0 = memory breakpoint for instructions
1029 (not supported; done via memory write instead)
1030 1 = hardware breakpoint for instructions (supported)
1031 2 = write watchpoint (supported)
1032 3 = read watchpoint (supported)
1033 4 = access watchpoint (supported) */
1035 if (type
< '1' || type
> '4') {
1036 output_buffer
[0] = 0;
1040 /* Read watchpoints are set as access watchpoints, because of GDB's
1041 inability to deal with pure read watchpoints. */
1046 /* Hardware (instruction) breakpoint. */
1047 /* Bit 0 in BP_CTRL holds the configuration for I0. */
1048 if (sreg
.s0_3
& 0x1) {
1049 /* Already in use. */
1050 gdb_cris_strcpy(output_buffer
, error_message
[E04
]);
1055 sreg
.s2_3
= (addr
+ len
- 1);
1059 unsigned int *bp_d_regs
= &sreg
.s3_3
;
1061 /* The watchpoint allocation scheme is the simplest possible.
1062 For example, if a region is watched for read and
1063 a write watch is requested, a new watchpoint will
1064 be used. Also, if a watch for a region that is already
1065 covered by one or more existing watchpoints, a new
1066 watchpoint will be used. */
1068 /* First, find a free data watchpoint. */
1069 for (bp
= 0; bp
< 6; bp
++) {
1070 /* Each data watchpoint's control registers occupy 2 bits
1071 (hence the 3), starting at bit 2 for D0 (hence the 2)
1072 with 4 bits between for each watchpoint (yes, the 4). */
1073 if (!(sreg
.s0_3
& (0x3 << (2 + (bp
* 4))))) {
1079 /* We're out of watchpoints. */
1080 gdb_cris_strcpy(output_buffer
, error_message
[E04
]);
1084 /* Configure the control register first. */
1085 if (type
== '3' || type
== '4') {
1086 /* Trigger on read. */
1087 sreg
.s0_3
|= (1 << (2 + bp
* 4));
1089 if (type
== '2' || type
== '4') {
1090 /* Trigger on write. */
1091 sreg
.s0_3
|= (2 << (2 + bp
* 4));
1094 /* Ugly pointer arithmetics to configure the watched range. */
1095 bp_d_regs
[bp
* 2] = addr
;
1096 bp_d_regs
[bp
* 2 + 1] = (addr
+ len
- 1);
1099 /* Set the S1 flag to enable watchpoints. */
1100 reg
.ccs
|= (1 << (S_CCS_BITNR
+ CCS_SHIFT
));
1101 gdb_cris_strcpy(output_buffer
, "OK");
1104 static void remove_watchpoint(char type
, int addr
, int len
)
1106 /* Breakpoint/watchpoint types:
1107 0 = memory breakpoint for instructions
1108 (not supported; done via memory write instead)
1109 1 = hardware breakpoint for instructions (supported)
1110 2 = write watchpoint (supported)
1111 3 = read watchpoint (supported)
1112 4 = access watchpoint (supported) */
1113 if (type
< '1' || type
> '4') {
1114 output_buffer
[0] = 0;
1118 /* Read watchpoints are set as access watchpoints, because of GDB's
1119 inability to deal with pure read watchpoints. */
1124 /* Hardware breakpoint. */
1125 /* Bit 0 in BP_CTRL holds the configuration for I0. */
1126 if (!(sreg
.s0_3
& 0x1)) {
1128 gdb_cris_strcpy(output_buffer
, error_message
[E04
]);
1137 unsigned int *bp_d_regs
= &sreg
.s3_3
;
1138 /* Try to find a watchpoint that is configured for the
1139 specified range, then check that read/write also matches. */
1141 /* Ugly pointer arithmetic, since I cannot rely on a
1142 single switch (addr) as there may be several watchpoints with
1143 the same start address for example. */
1145 for (bp
= 0; bp
< 6; bp
++) {
1146 if (bp_d_regs
[bp
* 2] == addr
&&
1147 bp_d_regs
[bp
* 2 + 1] == (addr
+ len
- 1)) {
1148 /* Matching range. */
1149 int bitpos
= 2 + bp
* 4;
1152 /* Read/write bits for this BP. */
1153 rw_bits
= (sreg
.s0_3
& (0x3 << bitpos
)) >> bitpos
;
1155 if ((type
== '3' && rw_bits
== 0x1) ||
1156 (type
== '2' && rw_bits
== 0x2) ||
1157 (type
== '4' && rw_bits
== 0x3)) {
1158 /* Read/write matched. */
1165 /* No watchpoint matched. */
1166 gdb_cris_strcpy(output_buffer
, error_message
[E04
]);
1170 /* Found a matching watchpoint. Now, deconfigure it by
1171 both disabling read/write in bp_ctrl and zeroing its
1172 start/end addresses. */
1173 sreg
.s0_3
&= ~(3 << (2 + (bp
* 4)));
1174 bp_d_regs
[bp
* 2] = 0;
1175 bp_d_regs
[bp
* 2 + 1] = 0;
1178 /* Note that we don't clear the S1 flag here. It's done when continuing. */
1179 gdb_cris_strcpy(output_buffer
, "OK");
1184 /* All expected commands are sent from remote.c. Send a response according
1185 to the description in remote.c. */
1187 handle_exception(int sigval
)
1189 /* Avoid warning of not used. */
1191 USEDFUN(handle_exception
);
1192 USEDVAR(internal_stack
[0]);
1194 register_fixup(sigval
);
1196 /* Send response. */
1197 stub_is_stopped(sigval
);
1200 output_buffer
[0] = '\0';
1201 getpacket(input_buffer
);
1202 switch (input_buffer
[0]) {
1204 /* Read registers: g
1205 Success: Each byte of register data is described by two hex digits.
1206 Registers are in the internal order for GDB, and the bytes
1207 in a register are in the same order the machine uses.
1211 /* General and special registers. */
1212 buf
= mem2hex(output_buffer
, (char *)®
, sizeof(registers
));
1213 /* Support registers. */
1214 /* -1 because of the null termination that mem2hex adds. */
1216 (char *)&sreg
+ (reg
.srs
* 16 * sizeof(unsigned int)),
1217 16 * sizeof(unsigned int));
1221 /* Write registers. GXX..XX
1222 Each byte of register data is described by two hex digits.
1225 /* General and special registers. */
1226 hex2mem((char *)®
, &input_buffer
[1], sizeof(registers
));
1227 /* Support registers. */
1228 hex2mem((char *)&sreg
+ (reg
.srs
* 16 * sizeof(unsigned int)),
1229 &input_buffer
[1] + sizeof(registers
),
1230 16 * sizeof(unsigned int));
1231 gdb_cris_strcpy(output_buffer
, "OK");
1235 /* Write register. Pn...=r...
1236 Write register n..., hex value without 0x, with value r...,
1237 which contains a hex value without 0x and two hex digits
1238 for each byte in the register (target byte order). P1f=11223344 means
1239 set register 31 to 44332211.
1241 Failure: E02, E05 */
1244 int regno
= gdb_cris_strtol(&input_buffer
[1], &suffix
, 16);
1247 status
= write_register(regno
, suffix
+1);
1251 /* Do not support read-only registers. */
1252 gdb_cris_strcpy(output_buffer
, error_message
[E02
]);
1255 /* Do not support non-existing registers. */
1256 gdb_cris_strcpy(output_buffer
, error_message
[E05
]);
1259 /* Valid register number. */
1260 gdb_cris_strcpy(output_buffer
, "OK");
1267 /* Read from memory. mAA..AA,LLLL
1268 AA..AA is the address and LLLL is the length.
1269 Success: XX..XX is the memory content. Can be fewer bytes than
1270 requested if only part of the data may be read. m6000120a,6c means
1271 retrieve 108 byte from base address 6000120a.
1275 unsigned char *addr
= (unsigned char *)gdb_cris_strtol(&input_buffer
[1],
1277 int len
= gdb_cris_strtol(suffix
+1, 0, 16);
1279 /* Bogus read (i.e. outside the kernel's
1281 if (!((unsigned int)addr
>= 0xc0000000 &&
1282 (unsigned int)addr
< 0xd0000000))
1285 mem2hex(output_buffer
, addr
, len
);
1290 /* Write to memory. XAA..AA,LLLL:XX..XX
1291 AA..AA is the start address, LLLL is the number of bytes, and
1292 XX..XX is the binary data.
1296 /* Write to memory. MAA..AA,LLLL:XX..XX
1297 AA..AA is the start address, LLLL is the number of bytes, and
1298 XX..XX is the hexadecimal data.
1304 unsigned char *addr
= (unsigned char *)gdb_cris_strtol(&input_buffer
[1],
1306 int len
= gdb_cris_strtol(lenptr
+1, &dataptr
, 16);
1307 if (*lenptr
== ',' && *dataptr
== ':') {
1308 if (input_buffer
[0] == 'M') {
1309 hex2mem(addr
, dataptr
+ 1, len
);
1311 bin2mem(addr
, dataptr
+ 1, len
);
1313 gdb_cris_strcpy(output_buffer
, "OK");
1316 gdb_cris_strcpy(output_buffer
, error_message
[E06
]);
1322 /* Continue execution. cAA..AA
1323 AA..AA is the address where execution is resumed. If AA..AA is
1324 omitted, resume at the present address.
1325 Success: return to the executing thread.
1326 Failure: will never know. */
1328 if (input_buffer
[1] != '\0') {
1329 gdb_cris_strcpy(output_buffer
, error_message
[E04
]);
1333 /* Before continuing, make sure everything is set up correctly. */
1335 /* Set the SPC to some unlikely value. */
1337 /* Set the S1 flag to 0 unless some watchpoint is enabled (since setting
1338 S1 to 0 would also disable watchpoints). (Note that bits 26-31 in BP_CTRL
1339 are reserved, so don't check against those). */
1340 if ((sreg
.s0_3
& 0x3fff) == 0) {
1341 reg
.ccs
&= ~(1 << (S_CCS_BITNR
+ CCS_SHIFT
));
1348 AA..AA is the address where execution is resumed. If AA..AA is
1349 omitted, resume at the present address. Success: return to the
1350 executing thread. Failure: will never know. */
1352 if (input_buffer
[1] != '\0') {
1353 gdb_cris_strcpy(output_buffer
, error_message
[E04
]);
1357 /* Set the SPC to PC, which is where we'll return
1358 (deduced previously). */
1361 /* Set the S1 (first stacked, not current) flag, which will
1362 kick into action when we rfe. */
1363 reg
.ccs
|= (1 << (S_CCS_BITNR
+ CCS_SHIFT
));
1368 /* Insert breakpoint or watchpoint, Ztype,addr,length.
1369 Remote protocol says: A remote target shall return an empty string
1370 for an unrecognized breakpoint or watchpoint packet type. */
1374 int addr
= gdb_cris_strtol(&input_buffer
[3], &lenptr
, 16);
1375 int len
= gdb_cris_strtol(lenptr
+ 1, &dataptr
, 16);
1376 char type
= input_buffer
[1];
1378 insert_watchpoint(type
, addr
, len
);
1383 /* Remove breakpoint or watchpoint, Ztype,addr,length.
1384 Remote protocol says: A remote target shall return an empty string
1385 for an unrecognized breakpoint or watchpoint packet type. */
1389 int addr
= gdb_cris_strtol(&input_buffer
[3], &lenptr
, 16);
1390 int len
= gdb_cris_strtol(lenptr
+ 1, &dataptr
, 16);
1391 char type
= input_buffer
[1];
1393 remove_watchpoint(type
, addr
, len
);
1399 /* The last signal which caused a stop. ?
1400 Success: SAA, where AA is the signal number.
1402 output_buffer
[0] = 'S';
1403 output_buffer
[1] = hex_asc_hi(sigval
);
1404 output_buffer
[2] = hex_asc_lo(sigval
);
1405 output_buffer
[3] = 0;
1409 /* Detach from host. D
1410 Success: OK, and return to the executing thread.
1411 Failure: will never know */
1417 /* kill request or reset request.
1418 Success: restart of target.
1419 Failure: will never know. */
1428 /* Continue with signal sig. Csig;AA..AA
1429 Step with signal sig. Ssig;AA..AA
1430 Use the extended remote protocol. !
1431 Restart the target system. R0
1432 Toggle debug flag. d
1433 Search backwards. tAA:PP,MM
1434 Not supported: E04 */
1436 gdb_cris_strcpy(output_buffer
, error_message
[E04
]);
1440 /* The stub should ignore other request and send an empty
1441 response ($#<checksum>). This way we can extend the protocol and GDB
1442 can tell whether the stub it is talking to uses the old or the new. */
1443 output_buffer
[0] = 0;
1446 putpacket(output_buffer
);
1453 reg_intr_vect_rw_mask intr_mask
;
1454 reg_ser_rw_intr_mask ser_intr_mask
;
1456 /* Configure the kgdb serial port. */
1457 #if defined(CONFIG_ETRAX_KGDB_PORT0)
1458 /* Note: no shortcut registered (not handled by multiple_interrupt).
1460 set_exception_vector(SER0_INTR_VECT
, kgdb_handle_exception
);
1461 /* Enable the ser irq in the global config. */
1462 intr_mask
= REG_RD(intr_vect
, regi_irq
, rw_mask
);
1464 REG_WR(intr_vect
, regi_irq
, rw_mask
, intr_mask
);
1466 ser_intr_mask
= REG_RD(ser
, regi_ser0
, rw_intr_mask
);
1467 ser_intr_mask
.dav
= regk_ser_yes
;
1468 REG_WR(ser
, regi_ser0
, rw_intr_mask
, ser_intr_mask
);
1469 #elif defined(CONFIG_ETRAX_KGDB_PORT1)
1470 /* Note: no shortcut registered (not handled by multiple_interrupt).
1472 set_exception_vector(SER1_INTR_VECT
, kgdb_handle_exception
);
1473 /* Enable the ser irq in the global config. */
1474 intr_mask
= REG_RD(intr_vect
, regi_irq
, rw_mask
);
1476 REG_WR(intr_vect
, regi_irq
, rw_mask
, intr_mask
);
1478 ser_intr_mask
= REG_RD(ser
, regi_ser1
, rw_intr_mask
);
1479 ser_intr_mask
.dav
= regk_ser_yes
;
1480 REG_WR(ser
, regi_ser1
, rw_intr_mask
, ser_intr_mask
);
1481 #elif defined(CONFIG_ETRAX_KGDB_PORT2)
1482 /* Note: no shortcut registered (not handled by multiple_interrupt).
1484 set_exception_vector(SER2_INTR_VECT
, kgdb_handle_exception
);
1485 /* Enable the ser irq in the global config. */
1486 intr_mask
= REG_RD(intr_vect
, regi_irq
, rw_mask
);
1488 REG_WR(intr_vect
, regi_irq
, rw_mask
, intr_mask
);
1490 ser_intr_mask
= REG_RD(ser
, regi_ser2
, rw_intr_mask
);
1491 ser_intr_mask
.dav
= regk_ser_yes
;
1492 REG_WR(ser
, regi_ser2
, rw_intr_mask
, ser_intr_mask
);
1493 #elif defined(CONFIG_ETRAX_KGDB_PORT3)
1494 /* Note: no shortcut registered (not handled by multiple_interrupt).
1496 set_exception_vector(SER3_INTR_VECT
, kgdb_handle_exception
);
1497 /* Enable the ser irq in the global config. */
1498 intr_mask
= REG_RD(intr_vect
, regi_irq
, rw_mask
);
1500 REG_WR(intr_vect
, regi_irq
, rw_mask
, intr_mask
);
1502 ser_intr_mask
= REG_RD(ser
, regi_ser3
, rw_intr_mask
);
1503 ser_intr_mask
.dav
= regk_ser_yes
;
1504 REG_WR(ser
, regi_ser3
, rw_intr_mask
, ser_intr_mask
);
1508 /* Performs a complete re-start from scratch. */
1512 machine_restart("");
1515 /* Use this static breakpoint in the start-up only. */
1521 dynamic_bp
= 0; /* This is a static, not a dynamic breakpoint. */
1522 __asm__
volatile ("break 8"); /* Jump to kgdb_handle_breakpoint. */
1525 /****************************** End of file **********************************/