1 /* gdb-stub.c: FRV GDB stub
3 * Copyright (C) 2003,4 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 * - Derived from Linux/MIPS version, Copyright (C) 1995 Andreas Busse
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 * To enable debugger support, two things need to happen. One, a
15 * call to set_debug_traps() is necessary in order to allow any breakpoints
16 * or error conditions to be properly intercepted and reported to gdb.
17 * Two, a breakpoint needs to be generated to begin communication. This
18 * is most easily accomplished by a call to breakpoint(). Breakpoint()
19 * simulates a breakpoint by executing a BREAK instruction.
22 * The following gdb commands are supported:
24 * command function Return value
26 * g return the value of the CPU registers hex data or ENN
27 * G set the value of the CPU registers OK or ENN
29 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
30 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
32 * c Resume at current address SNN ( signal NN)
33 * cAA..AA Continue at address AA..AA SNN
35 * s Step one instruction SNN
36 * sAA..AA Step one instruction from AA..AA SNN
40 * ? What was the last sigval ? SNN (signal NN)
42 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
45 * All commands and responses are sent with a packet which includes a
46 * checksum. A packet consists of
48 * $<packet info>#<checksum>.
51 * <packet info> :: <characters representing the command or response>
52 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
54 * When a packet is received, it is first acknowledged with either '+' or '-'.
55 * '+' indicates a successful transfer. '-' indicates a failed transfer.
60 * $m0,10#2a +$00010203040506070809101112131415#42
67 * For reference -- the following are the steps that one
68 * company took (RidgeRun Inc) to get remote gdb debugging
69 * going. In this scenario the host machine was a PC and the
70 * target platform was a Galileo EVB64120A MIPS evaluation
74 * First download gdb-5.0.tar.gz from the internet.
75 * and then build/install the package.
78 * $ tar zxf gdb-5.0.tar.gz
80 * $ ./configure --target=frv-elf-gdb
85 * Configure linux for remote debugging and build it.
89 * $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
93 * Download the kernel to the remote target and start
94 * the kernel running. It will promptly halt and wait
95 * for the host gdb session to connect. It does this
96 * since the "Kernel Hacking" option has defined
97 * CONFIG_REMOTE_DEBUG which in turn enables your calls
103 * Start the gdb session on the host.
106 * $ frv-elf-gdb vmlinux
107 * (gdb) set remotebaud 115200
108 * (gdb) target remote /dev/ttyS1
109 * ...at this point you are connected to
110 * the remote target and can use gdb
111 * in the normal fasion. Setting
112 * breakpoints, single stepping,
113 * printing variables, etc.
117 #include <linux/string.h>
118 #include <linux/kernel.h>
119 #include <linux/signal.h>
120 #include <linux/sched.h>
121 #include <linux/mm.h>
122 #include <linux/console.h>
123 #include <linux/init.h>
124 #include <linux/slab.h>
125 #include <linux/nmi.h>
127 #include <asm/asm-offsets.h>
128 #include <asm/pgtable.h>
129 #include <asm/gdb-stub.h>
131 #define LEDS(x) do { /* *(u32*)0xe1200004 = ~(x); mb(); */ } while(0)
133 #undef GDBSTUB_DEBUG_PROTOCOL
135 extern void debug_to_serial(const char *p
, int n
);
136 extern void gdbstub_console_write(struct console
*co
, const char *p
, unsigned n
);
138 extern volatile uint32_t __break_error_detect
[3]; /* ESFR1, ESR15, EAR15 */
142 } __attribute__((aligned(8)));
146 unsigned long hsr0
, pcsr
, esr0
, ear0
, epcr0
;
148 unsigned long tplr
, tppr
, tpxr
, cxnr
;
152 struct __debug_amr iamr
[16];
153 struct __debug_amr damr
[16];
156 struct __debug_amr tlb
[64*2];
160 static struct __debug_mmu __debug_mmu
;
163 * BUFMAX defines the maximum number of characters in inbound/outbound buffers
164 * at least NUMREGBYTES*2 are needed for register packets
168 #define BREAK_INSN 0x801000c0 /* use "break" as bkpt */
170 static const char gdbstub_banner
[] = "Linux/FR-V GDB Stub (c) RedHat 2003\n";
172 volatile u8 gdbstub_rx_buffer
[PAGE_SIZE
] __attribute__((aligned(PAGE_SIZE
)));
173 volatile u32 gdbstub_rx_inp
= 0;
174 volatile u32 gdbstub_rx_outp
= 0;
175 volatile u8 gdbstub_rx_overflow
= 0;
176 u8 gdbstub_rx_unget
= 0;
178 /* set with GDB whilst running to permit step through exceptions */
179 extern volatile u32
__attribute__((section(".bss"))) gdbstub_trace_through_exceptions
;
181 static char input_buffer
[BUFMAX
];
182 static char output_buffer
[BUFMAX
];
184 static const char *regnames
[] = {
185 "PSR ", "ISR ", "CCR ", "CCCR",
186 "LR ", "LCR ", "PC ", "_stt",
187 "sys ", "GR8*", "GNE0", "GNE1",
189 "TBR ", "SP ", "FP ", "GR3 ",
190 "GR4 ", "GR5 ", "GR6 ", "GR7 ",
191 "GR8 ", "GR9 ", "GR10", "GR11",
192 "GR12", "GR13", "GR14", "GR15",
193 "GR16", "GR17", "GR18", "GR19",
194 "GR20", "GR21", "GR22", "GR23",
195 "GR24", "GR25", "GR26", "GR27",
196 "EFRM", "CURR", "GR30", "BFRM"
199 struct gdbstub_bkpt
{
200 unsigned long addr
; /* address of breakpoint */
201 unsigned len
; /* size of breakpoint */
202 uint32_t originsns
[7]; /* original instructions */
205 static struct gdbstub_bkpt gdbstub_bkpts
[256];
211 static void gdbstub_recv_packet(char *buffer
);
212 static int gdbstub_send_packet(char *buffer
);
213 static int gdbstub_compute_signal(unsigned long tbr
);
214 static int hex(unsigned char ch
);
215 static int hexToInt(char **ptr
, unsigned long *intValue
);
216 static unsigned char *mem2hex(const void *mem
, char *buf
, int count
, int may_fault
);
217 static char *hex2mem(const char *buf
, void *_mem
, int count
);
220 * Convert ch from a hex digit to an int
222 static int hex(unsigned char ch
)
224 if (ch
>= 'a' && ch
<= 'f')
226 if (ch
>= '0' && ch
<= '9')
228 if (ch
>= 'A' && ch
<= 'F')
233 void gdbstub_printk(const char *fmt
, ...)
235 static char buf
[1024];
239 /* Emit the output into the temporary buffer */
241 len
= vsnprintf(buf
, sizeof(buf
), fmt
, args
);
243 debug_to_serial(buf
, len
);
246 static inline char *gdbstub_strcpy(char *dst
, const char *src
)
249 while ((dst
[loop
] = src
[loop
]))
254 static void gdbstub_purge_cache(void)
256 asm volatile(" dcef @(gr0,gr0),#1 \n"
257 " icei @(gr0,gr0),#1 \n"
263 /*****************************************************************************/
265 * scan for the sequence $<data>#<checksum>
267 static void gdbstub_recv_packet(char *buffer
)
269 unsigned char checksum
;
270 unsigned char xmitcsum
;
272 int count
, i
, ret
, error
;
275 /* wait around for the start character, ignore all other characters */
277 gdbstub_rx_char(&ch
, 0);
285 /* now, read until a # or end of buffer is found */
286 while (count
< BUFMAX
) {
287 ret
= gdbstub_rx_char(&ch
, 0);
299 gdbstub_proto("### GDB Rx Error - Skipping packet ###\n");
300 gdbstub_proto("### GDB Tx NAK\n");
301 gdbstub_tx_char('-');
305 if (count
>= BUFMAX
|| error
)
310 /* read the checksum */
311 ret
= gdbstub_rx_char(&ch
, 0);
314 xmitcsum
= hex(ch
) << 4;
316 ret
= gdbstub_rx_char(&ch
, 0);
323 gdbstub_proto("### GDB Rx Error - Skipping packet\n");
324 gdbstub_proto("### GDB Tx NAK\n");
325 gdbstub_tx_char('-');
329 /* check the checksum */
330 if (checksum
!= xmitcsum
) {
331 gdbstub_proto("### GDB Tx NAK\n");
332 gdbstub_tx_char('-'); /* failed checksum */
336 gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer
, checksum
);
337 gdbstub_proto("### GDB Tx ACK\n");
338 gdbstub_tx_char('+'); /* successful transfer */
340 /* if a sequence char is present, reply the sequence ID */
341 if (buffer
[2] == ':') {
342 gdbstub_tx_char(buffer
[0]);
343 gdbstub_tx_char(buffer
[1]);
345 /* remove sequence chars from buffer */
347 while (buffer
[count
]) count
++;
348 for (i
=3; i
<= count
; i
++)
349 buffer
[i
- 3] = buffer
[i
];
354 } /* end gdbstub_recv_packet() */
356 /*****************************************************************************/
358 * send the packet in buffer.
359 * - return 0 if successfully ACK'd
360 * - return 1 if abandoned due to new incoming packet
362 static int gdbstub_send_packet(char *buffer
)
364 unsigned char checksum
;
368 /* $<packet info>#<checksum> */
369 gdbstub_proto("### GDB Tx '%s' ###\n", buffer
);
372 gdbstub_tx_char('$');
376 while ((ch
= buffer
[count
]) != 0) {
382 gdbstub_tx_char('#');
383 gdbstub_tx_char(hex_asc_hi(checksum
));
384 gdbstub_tx_char(hex_asc_lo(checksum
));
386 } while (gdbstub_rx_char(&ch
,0),
387 #ifdef GDBSTUB_DEBUG_PROTOCOL
388 ch
=='-' && (gdbstub_proto("### GDB Rx NAK\n"),0),
389 ch
!='-' && ch
!='+' && (gdbstub_proto("### GDB Rx ??? %02x\n",ch
),0),
394 gdbstub_proto("### GDB Rx ACK\n");
398 gdbstub_proto("### GDB Tx Abandoned\n");
399 gdbstub_rx_unget
= ch
;
401 } /* end gdbstub_send_packet() */
404 * While we find nice hex chars, build an int.
405 * Return number of chars processed.
407 static int hexToInt(char **ptr
, unsigned long *_value
)
417 *_value
= (*_value
<< 4) | ((uint8_t) ch
& 0xf);
426 /*****************************************************************************/
428 * probe an address to see whether it maps to anything
430 static inline int gdbstub_addr_probe(const void *vaddr
)
435 asm("lrad %1,%0,#1,#0,#0" : "=r"(paddr
) : "r"(vaddr
));
436 if (!(paddr
& xAMPRx_V
))
441 } /* end gdbstub_addr_probe() */
444 static unsigned long __saved_dampr
, __saved_damlr
;
446 static inline unsigned long gdbstub_virt_to_pte(unsigned long vaddr
)
452 unsigned long val
, dampr5
;
454 pgd
= (pgd_t
*) __get_DAMLR(3) + pgd_index(vaddr
);
455 pud
= pud_offset(pgd
, vaddr
);
456 pmd
= pmd_offset(pud
, vaddr
);
458 if (pmd_bad(*pmd
) || !pmd_present(*pmd
))
461 /* make sure dampr5 maps to the correct pmd */
462 dampr5
= __get_DAMPR(5);
464 __set_DAMPR(5, val
| xAMPRx_L
| xAMPRx_SS_16Kb
| xAMPRx_S
| xAMPRx_C
| xAMPRx_V
);
466 /* now its safe to access pmd */
467 pte
= (pte_t
*)__get_DAMLR(5) + __pte_index(vaddr
);
468 if (pte_present(*pte
))
473 /* restore original dampr5 */
474 __set_DAMPR(5, dampr5
);
480 static inline int gdbstub_addr_map(const void *vaddr
)
485 __saved_dampr
= __get_DAMPR(2);
486 __saved_damlr
= __get_DAMLR(2);
488 if (gdbstub_addr_probe(vaddr
))
491 pte
= gdbstub_virt_to_pte((unsigned long) vaddr
);
494 __set_DAMLR(2, (unsigned long) vaddr
& PAGE_MASK
);
501 static inline void gdbstub_addr_unmap(void)
504 __set_DAMPR(2, __saved_dampr
);
505 __set_DAMLR(2, __saved_damlr
);
510 * access potentially dodgy memory through a potentially dodgy pointer
512 static inline int gdbstub_read_dword(const void *addr
, uint32_t *_res
)
517 if (!gdbstub_addr_map(addr
))
520 asm volatile(" movgs gr0,brr \n"
523 : "=r"(res
), "=r"(brr
)
524 : "m"(*(uint32_t *) addr
));
526 gdbstub_addr_unmap();
530 static inline int gdbstub_write_dword(void *addr
, uint32_t val
)
534 if (!gdbstub_addr_map(addr
))
537 asm volatile(" movgs gr0,brr \n"
541 : "r"(val
), "m"(*(uint32_t *) addr
));
542 gdbstub_addr_unmap();
546 static inline int gdbstub_read_word(const void *addr
, uint16_t *_res
)
551 if (!gdbstub_addr_map(addr
))
554 asm volatile(" movgs gr0,brr \n"
557 : "=r"(res
), "=r"(brr
)
558 : "m"(*(uint16_t *) addr
));
560 gdbstub_addr_unmap();
564 static inline int gdbstub_write_word(void *addr
, uint16_t val
)
568 if (!gdbstub_addr_map(addr
))
571 asm volatile(" movgs gr0,brr \n"
575 : "r"(val
), "m"(*(uint16_t *) addr
));
576 gdbstub_addr_unmap();
580 static inline int gdbstub_read_byte(const void *addr
, uint8_t *_res
)
585 if (!gdbstub_addr_map(addr
))
588 asm volatile(" movgs gr0,brr \n"
591 : "=r"(res
), "=r"(brr
)
592 : "m"(*(uint8_t *) addr
));
594 gdbstub_addr_unmap();
598 static inline int gdbstub_write_byte(void *addr
, uint8_t val
)
602 if (!gdbstub_addr_map(addr
))
605 asm volatile(" movgs gr0,brr \n"
609 : "r"(val
), "m"(*(uint8_t *) addr
));
610 gdbstub_addr_unmap();
614 static void __gdbstub_console_write(struct console
*co
, const char *p
, unsigned n
)
624 while (n
> 0 && qty
< 20) {
625 mem2hex(p
, outbuf
+ qty
, 2, 0);
636 gdbstub_send_packet(outbuf
);
641 void debug_to_serial(const char *p
, int n
)
643 gdbstub_console_write(NULL
,p
,n
);
647 #ifdef CONFIG_GDB_CONSOLE
649 static struct console gdbstub_console
= {
651 .write
= gdbstub_console_write
, /* in break.S */
652 .flags
= CON_PRINTBUFFER
,
658 /*****************************************************************************/
660 * Convert the memory pointed to by mem into hex, placing result in buf.
661 * - if successful, return a pointer to the last char put in buf (NUL)
662 * - in case of mem fault, return NULL
663 * may_fault is non-zero if we are reading from arbitrary memory, but is currently
666 static unsigned char *mem2hex(const void *_mem
, char *buf
, int count
, int may_fault
)
668 const uint8_t *mem
= _mem
;
669 uint8_t ch
[4] __attribute__((aligned(4)));
671 if ((uint32_t)mem
&1 && count
>=1) {
672 if (!gdbstub_read_byte(mem
,ch
))
674 buf
= hex_byte_pack(buf
, ch
[0]);
679 if ((uint32_t)mem
&3 && count
>=2) {
680 if (!gdbstub_read_word(mem
,(uint16_t *)ch
))
682 buf
= hex_byte_pack(buf
, ch
[0]);
683 buf
= hex_byte_pack(buf
, ch
[1]);
689 if (!gdbstub_read_dword(mem
,(uint32_t *)ch
))
691 buf
= hex_byte_pack(buf
, ch
[0]);
692 buf
= hex_byte_pack(buf
, ch
[1]);
693 buf
= hex_byte_pack(buf
, ch
[2]);
694 buf
= hex_byte_pack(buf
, ch
[3]);
700 if (!gdbstub_read_word(mem
,(uint16_t *)ch
))
702 buf
= hex_byte_pack(buf
, ch
[0]);
703 buf
= hex_byte_pack(buf
, ch
[1]);
709 if (!gdbstub_read_byte(mem
,ch
))
711 buf
= hex_byte_pack(buf
, ch
[0]);
717 } /* end mem2hex() */
719 /*****************************************************************************/
721 * convert the hex array pointed to by buf into binary to be placed in mem
722 * return a pointer to the character AFTER the last byte of buffer consumed
724 static char *hex2mem(const char *buf
, void *_mem
, int count
)
733 if ((u32
)mem
&1 && count
>=1) {
734 ch
.b
[0] = hex(*buf
++) << 4;
735 ch
.b
[0] |= hex(*buf
++);
736 if (!gdbstub_write_byte(mem
,ch
.b
[0]))
742 if ((u32
)mem
&3 && count
>=2) {
743 ch
.b
[0] = hex(*buf
++) << 4;
744 ch
.b
[0] |= hex(*buf
++);
745 ch
.b
[1] = hex(*buf
++) << 4;
746 ch
.b
[1] |= hex(*buf
++);
747 if (!gdbstub_write_word(mem
,ch
.w
))
754 ch
.b
[0] = hex(*buf
++) << 4;
755 ch
.b
[0] |= hex(*buf
++);
756 ch
.b
[1] = hex(*buf
++) << 4;
757 ch
.b
[1] |= hex(*buf
++);
758 ch
.b
[2] = hex(*buf
++) << 4;
759 ch
.b
[2] |= hex(*buf
++);
760 ch
.b
[3] = hex(*buf
++) << 4;
761 ch
.b
[3] |= hex(*buf
++);
762 if (!gdbstub_write_dword(mem
,ch
.l
))
769 ch
.b
[0] = hex(*buf
++) << 4;
770 ch
.b
[0] |= hex(*buf
++);
771 ch
.b
[1] = hex(*buf
++) << 4;
772 ch
.b
[1] |= hex(*buf
++);
773 if (!gdbstub_write_word(mem
,ch
.w
))
780 ch
.b
[0] = hex(*buf
++) << 4;
781 ch
.b
[0] |= hex(*buf
++);
782 if (!gdbstub_write_byte(mem
,ch
.b
[0]))
787 } /* end hex2mem() */
789 /*****************************************************************************/
791 * This table contains the mapping between FRV TBR.TT exception codes,
792 * and signals, which are primarily what GDB understands. It also
793 * indicates which hardware traps we need to commandeer when
794 * initializing the stub.
796 static const struct brr_to_sig_map
{
797 unsigned long brr_mask
; /* BRR bitmask */
798 unsigned long tbr_tt
; /* TBR.TT code (in BRR.EBTT) */
799 unsigned int signo
; /* Signal that we map this into */
800 } brr_to_sig_map
[] = {
801 { BRR_EB
, TBR_TT_INSTR_ACC_ERROR
, SIGSEGV
},
802 { BRR_EB
, TBR_TT_ILLEGAL_INSTR
, SIGILL
},
803 { BRR_EB
, TBR_TT_PRIV_INSTR
, SIGILL
},
804 { BRR_EB
, TBR_TT_MP_EXCEPTION
, SIGFPE
},
805 { BRR_EB
, TBR_TT_DATA_ACC_ERROR
, SIGSEGV
},
806 { BRR_EB
, TBR_TT_DATA_STR_ERROR
, SIGSEGV
},
807 { BRR_EB
, TBR_TT_DIVISION_EXCEP
, SIGFPE
},
808 { BRR_EB
, TBR_TT_COMPOUND_EXCEP
, SIGSEGV
},
809 { BRR_EB
, TBR_TT_INTERRUPT_13
, SIGALRM
}, /* watchdog */
810 { BRR_EB
, TBR_TT_INTERRUPT_14
, SIGINT
}, /* GDB serial */
811 { BRR_EB
, TBR_TT_INTERRUPT_15
, SIGQUIT
}, /* NMI */
812 { BRR_CB
, 0, SIGUSR1
},
813 { BRR_TB
, 0, SIGUSR2
},
814 { BRR_DBNEx
, 0, SIGTRAP
},
815 { BRR_DBx
, 0, SIGTRAP
}, /* h/w watchpoint */
816 { BRR_IBx
, 0, SIGTRAP
}, /* h/w breakpoint */
817 { BRR_CBB
, 0, SIGTRAP
},
818 { BRR_SB
, 0, SIGTRAP
},
819 { BRR_ST
, 0, SIGTRAP
}, /* single step */
820 { 0, 0, SIGHUP
} /* default */
823 /*****************************************************************************/
825 * convert the FRV BRR register contents into a UNIX signal number
827 static inline int gdbstub_compute_signal(unsigned long brr
)
829 const struct brr_to_sig_map
*map
;
830 unsigned long tbr
= (brr
& BRR_EBTT
) >> 12;
832 for (map
= brr_to_sig_map
; map
->brr_mask
; map
++)
833 if (map
->brr_mask
& brr
)
834 if (!map
->tbr_tt
|| map
->tbr_tt
== tbr
)
838 } /* end gdbstub_compute_signal() */
840 /*****************************************************************************/
842 * set a software breakpoint or a hardware breakpoint or watchpoint
844 static int gdbstub_set_breakpoint(unsigned long type
, unsigned long addr
, unsigned long len
)
847 int bkpt
, loop
, xloop
;
851 unsigned long mask0
, mask1
;
856 //gdbstub_printk("setbkpt(%ld,%08lx,%ld)\n", type, addr, len);
859 /* set software breakpoint */
861 if (addr
& 3 || len
> 7*4)
864 for (bkpt
= 255; bkpt
>= 0; bkpt
--)
865 if (!gdbstub_bkpts
[bkpt
].addr
)
870 for (loop
= 0; loop
< len
/4; loop
++)
871 if (!gdbstub_read_dword(&((uint32_t *) addr
)[loop
],
872 &gdbstub_bkpts
[bkpt
].originsns
[loop
]))
875 for (loop
= 0; loop
< len
/4; loop
++)
876 if (!gdbstub_write_dword(&((uint32_t *) addr
)[loop
],
879 /* need to undo the changes if possible */
880 for (xloop
= 0; xloop
< loop
; xloop
++)
881 gdbstub_write_dword(&((uint32_t *) addr
)[xloop
],
882 gdbstub_bkpts
[bkpt
].originsns
[xloop
]);
886 gdbstub_bkpts
[bkpt
].addr
= addr
;
887 gdbstub_bkpts
[bkpt
].len
= len
;
890 gdbstub_printk("Set BKPT[%02x]: %08lx #%d {%04x, %04x} -> { %04x, %04x }\n",
892 gdbstub_bkpts
[bkpt
].addr
,
893 gdbstub_bkpts
[bkpt
].len
,
894 gdbstub_bkpts
[bkpt
].originsns
[0],
895 gdbstub_bkpts
[bkpt
].originsns
[1],
896 ((uint32_t *) addr
)[0],
897 ((uint32_t *) addr
)[1]
902 /* set hardware breakpoint */
904 if (addr
& 3 || len
!= 4)
907 if (!(__debug_regs
->dcr
& DCR_IBE0
)) {
908 //gdbstub_printk("set h/w break 0: %08lx\n", addr);
909 __debug_regs
->dcr
|= DCR_IBE0
;
910 __debug_regs
->ibar
[0] = addr
;
911 asm volatile("movgs %0,ibar0" : : "r"(addr
));
915 if (!(__debug_regs
->dcr
& DCR_IBE1
)) {
916 //gdbstub_printk("set h/w break 1: %08lx\n", addr);
917 __debug_regs
->dcr
|= DCR_IBE1
;
918 __debug_regs
->ibar
[1] = addr
;
919 asm volatile("movgs %0,ibar1" : : "r"(addr
));
923 if (!(__debug_regs
->dcr
& DCR_IBE2
)) {
924 //gdbstub_printk("set h/w break 2: %08lx\n", addr);
925 __debug_regs
->dcr
|= DCR_IBE2
;
926 __debug_regs
->ibar
[2] = addr
;
927 asm volatile("movgs %0,ibar2" : : "r"(addr
));
931 if (!(__debug_regs
->dcr
& DCR_IBE3
)) {
932 //gdbstub_printk("set h/w break 3: %08lx\n", addr);
933 __debug_regs
->dcr
|= DCR_IBE3
;
934 __debug_regs
->ibar
[3] = addr
;
935 asm volatile("movgs %0,ibar3" : : "r"(addr
));
941 /* set data read/write/access watchpoint */
945 if ((addr
& ~7) != ((addr
+ len
- 1) & ~7))
950 memset(dbmr
.bytes
, 0xff, sizeof(dbmr
.bytes
));
951 for (loop
= 0; loop
< len
; loop
++)
952 dbmr
.bytes
[tmp
+ loop
] = 0;
956 if (!(__debug_regs
->dcr
& (DCR_DRBE0
|DCR_DWBE0
))) {
957 //gdbstub_printk("set h/w watchpoint 0 type %ld: %08lx\n", type, addr);
958 tmp
= type
==2 ? DCR_DWBE0
: type
==3 ? DCR_DRBE0
: DCR_DRBE0
|DCR_DWBE0
;
960 __debug_regs
->dcr
|= tmp
;
961 __debug_regs
->dbar
[0] = addr
;
962 __debug_regs
->dbmr
[0][0] = dbmr
.mask0
;
963 __debug_regs
->dbmr
[0][1] = dbmr
.mask1
;
964 __debug_regs
->dbdr
[0][0] = 0;
965 __debug_regs
->dbdr
[0][1] = 0;
967 asm volatile(" movgs %0,dbar0 \n"
968 " movgs %1,dbmr00 \n"
969 " movgs %2,dbmr01 \n"
970 " movgs gr0,dbdr00 \n"
971 " movgs gr0,dbdr01 \n"
972 : : "r"(addr
), "r"(dbmr
.mask0
), "r"(dbmr
.mask1
));
976 if (!(__debug_regs
->dcr
& (DCR_DRBE1
|DCR_DWBE1
))) {
977 //gdbstub_printk("set h/w watchpoint 1 type %ld: %08lx\n", type, addr);
978 tmp
= type
==2 ? DCR_DWBE1
: type
==3 ? DCR_DRBE1
: DCR_DRBE1
|DCR_DWBE1
;
980 __debug_regs
->dcr
|= tmp
;
981 __debug_regs
->dbar
[1] = addr
;
982 __debug_regs
->dbmr
[1][0] = dbmr
.mask0
;
983 __debug_regs
->dbmr
[1][1] = dbmr
.mask1
;
984 __debug_regs
->dbdr
[1][0] = 0;
985 __debug_regs
->dbdr
[1][1] = 0;
987 asm volatile(" movgs %0,dbar1 \n"
988 " movgs %1,dbmr10 \n"
989 " movgs %2,dbmr11 \n"
990 " movgs gr0,dbdr10 \n"
991 " movgs gr0,dbdr11 \n"
992 : : "r"(addr
), "r"(dbmr
.mask0
), "r"(dbmr
.mask1
));
1002 } /* end gdbstub_set_breakpoint() */
1004 /*****************************************************************************/
1006 * clear a breakpoint or watchpoint
1008 int gdbstub_clear_breakpoint(unsigned long type
, unsigned long addr
, unsigned long len
)
1015 unsigned long mask0
, mask1
;
1020 //gdbstub_printk("clearbkpt(%ld,%08lx,%ld)\n", type, addr, len);
1023 /* clear software breakpoint */
1025 for (bkpt
= 255; bkpt
>= 0; bkpt
--)
1026 if (gdbstub_bkpts
[bkpt
].addr
== addr
&& gdbstub_bkpts
[bkpt
].len
== len
)
1031 gdbstub_bkpts
[bkpt
].addr
= 0;
1033 for (loop
= 0; loop
< len
/4; loop
++)
1034 if (!gdbstub_write_dword(&((uint32_t *) addr
)[loop
],
1035 gdbstub_bkpts
[bkpt
].originsns
[loop
]))
1039 /* clear hardware breakpoint */
1041 if (addr
& 3 || len
!= 4)
1044 #define __get_ibar(X) ({ unsigned long x; asm volatile("movsg ibar"#X",%0" : "=r"(x)); x; })
1046 if (__debug_regs
->dcr
& DCR_IBE0
&& __get_ibar(0) == addr
) {
1047 //gdbstub_printk("clear h/w break 0: %08lx\n", addr);
1048 __debug_regs
->dcr
&= ~DCR_IBE0
;
1049 __debug_regs
->ibar
[0] = 0;
1050 asm volatile("movgs gr0,ibar0");
1054 if (__debug_regs
->dcr
& DCR_IBE1
&& __get_ibar(1) == addr
) {
1055 //gdbstub_printk("clear h/w break 1: %08lx\n", addr);
1056 __debug_regs
->dcr
&= ~DCR_IBE1
;
1057 __debug_regs
->ibar
[1] = 0;
1058 asm volatile("movgs gr0,ibar1");
1062 if (__debug_regs
->dcr
& DCR_IBE2
&& __get_ibar(2) == addr
) {
1063 //gdbstub_printk("clear h/w break 2: %08lx\n", addr);
1064 __debug_regs
->dcr
&= ~DCR_IBE2
;
1065 __debug_regs
->ibar
[2] = 0;
1066 asm volatile("movgs gr0,ibar2");
1070 if (__debug_regs
->dcr
& DCR_IBE3
&& __get_ibar(3) == addr
) {
1071 //gdbstub_printk("clear h/w break 3: %08lx\n", addr);
1072 __debug_regs
->dcr
&= ~DCR_IBE3
;
1073 __debug_regs
->ibar
[3] = 0;
1074 asm volatile("movgs gr0,ibar3");
1080 /* clear data read/write/access watchpoint */
1084 if ((addr
& ~7) != ((addr
+ len
- 1) & ~7))
1089 memset(dbmr
.bytes
, 0xff, sizeof(dbmr
.bytes
));
1090 for (loop
= 0; loop
< len
; loop
++)
1091 dbmr
.bytes
[tmp
+ loop
] = 0;
1095 #define __get_dbar(X) ({ unsigned long x; asm volatile("movsg dbar"#X",%0" : "=r"(x)); x; })
1096 #define __get_dbmr0(X) ({ unsigned long x; asm volatile("movsg dbmr"#X"0,%0" : "=r"(x)); x; })
1097 #define __get_dbmr1(X) ({ unsigned long x; asm volatile("movsg dbmr"#X"1,%0" : "=r"(x)); x; })
1099 /* consider DBAR 0 */
1100 tmp
= type
==2 ? DCR_DWBE0
: type
==3 ? DCR_DRBE0
: DCR_DRBE0
|DCR_DWBE0
;
1102 if ((__debug_regs
->dcr
& (DCR_DRBE0
|DCR_DWBE0
)) != tmp
||
1103 __get_dbar(0) != addr
||
1104 __get_dbmr0(0) != dbmr
.mask0
||
1105 __get_dbmr1(0) != dbmr
.mask1
)
1108 //gdbstub_printk("clear h/w watchpoint 0 type %ld: %08lx\n", type, addr);
1109 __debug_regs
->dcr
&= ~(DCR_DRBE0
|DCR_DWBE0
);
1110 __debug_regs
->dbar
[0] = 0;
1111 __debug_regs
->dbmr
[0][0] = 0;
1112 __debug_regs
->dbmr
[0][1] = 0;
1113 __debug_regs
->dbdr
[0][0] = 0;
1114 __debug_regs
->dbdr
[0][1] = 0;
1116 asm volatile(" movgs gr0,dbar0 \n"
1117 " movgs gr0,dbmr00 \n"
1118 " movgs gr0,dbmr01 \n"
1119 " movgs gr0,dbdr00 \n"
1120 " movgs gr0,dbdr01 \n");
1124 /* consider DBAR 0 */
1125 tmp
= type
==2 ? DCR_DWBE1
: type
==3 ? DCR_DRBE1
: DCR_DRBE1
|DCR_DWBE1
;
1127 if ((__debug_regs
->dcr
& (DCR_DRBE1
|DCR_DWBE1
)) != tmp
||
1128 __get_dbar(1) != addr
||
1129 __get_dbmr0(1) != dbmr
.mask0
||
1130 __get_dbmr1(1) != dbmr
.mask1
)
1133 //gdbstub_printk("clear h/w watchpoint 1 type %ld: %08lx\n", type, addr);
1134 __debug_regs
->dcr
&= ~(DCR_DRBE1
|DCR_DWBE1
);
1135 __debug_regs
->dbar
[1] = 0;
1136 __debug_regs
->dbmr
[1][0] = 0;
1137 __debug_regs
->dbmr
[1][1] = 0;
1138 __debug_regs
->dbdr
[1][0] = 0;
1139 __debug_regs
->dbdr
[1][1] = 0;
1141 asm volatile(" movgs gr0,dbar1 \n"
1142 " movgs gr0,dbmr10 \n"
1143 " movgs gr0,dbmr11 \n"
1144 " movgs gr0,dbdr10 \n"
1145 " movgs gr0,dbdr11 \n");
1154 } /* end gdbstub_clear_breakpoint() */
1156 /*****************************************************************************/
1158 * check a for an internal software breakpoint, and wind the PC back if necessary
1160 static void gdbstub_check_breakpoint(void)
1162 unsigned long addr
= __debug_frame
->pc
- 4;
1165 for (bkpt
= 255; bkpt
>= 0; bkpt
--)
1166 if (gdbstub_bkpts
[bkpt
].addr
== addr
)
1169 __debug_frame
->pc
= addr
;
1171 //gdbstub_printk("alter pc [%d] %08lx\n", bkpt, __debug_frame->pc);
1173 } /* end gdbstub_check_breakpoint() */
1175 /*****************************************************************************/
1179 static void __maybe_unused
gdbstub_show_regs(void)
1184 gdbstub_printk("\n");
1186 gdbstub_printk("Frame: @%p [%s]\n",
1188 __debug_frame
->psr
& PSR_S
? "kernel" : "user");
1190 reg
= (unsigned long *) __debug_frame
;
1191 for (loop
= 0; loop
< NR_PT_REGS
; loop
++) {
1192 printk("%s %08lx", regnames
[loop
+ 0], reg
[loop
+ 0]);
1194 if (loop
== NR_PT_REGS
- 1 || loop
% 5 == 4)
1200 gdbstub_printk("Process %s (pid: %d)\n", current
->comm
, current
->pid
);
1201 } /* end gdbstub_show_regs() */
1203 /*****************************************************************************/
1205 * dump debugging regs
1207 static void __maybe_unused
gdbstub_dump_debugregs(void)
1209 gdbstub_printk("DCR %08lx ", __debug_status
.dcr
);
1210 gdbstub_printk("BRR %08lx\n", __debug_status
.brr
);
1212 gdbstub_printk("IBAR0 %08lx ", __get_ibar(0));
1213 gdbstub_printk("IBAR1 %08lx ", __get_ibar(1));
1214 gdbstub_printk("IBAR2 %08lx ", __get_ibar(2));
1215 gdbstub_printk("IBAR3 %08lx\n", __get_ibar(3));
1217 gdbstub_printk("DBAR0 %08lx ", __get_dbar(0));
1218 gdbstub_printk("DBMR00 %08lx ", __get_dbmr0(0));
1219 gdbstub_printk("DBMR01 %08lx\n", __get_dbmr1(0));
1221 gdbstub_printk("DBAR1 %08lx ", __get_dbar(1));
1222 gdbstub_printk("DBMR10 %08lx ", __get_dbmr0(1));
1223 gdbstub_printk("DBMR11 %08lx\n", __get_dbmr1(1));
1225 gdbstub_printk("\n");
1226 } /* end gdbstub_dump_debugregs() */
1228 /*****************************************************************************/
1230 * dump the MMU state into a structure so that it can be accessed with GDB
1232 void gdbstub_get_mmu_state(void)
1234 asm volatile("movsg hsr0,%0" : "=r"(__debug_mmu
.regs
.hsr0
));
1235 asm volatile("movsg pcsr,%0" : "=r"(__debug_mmu
.regs
.pcsr
));
1236 asm volatile("movsg esr0,%0" : "=r"(__debug_mmu
.regs
.esr0
));
1237 asm volatile("movsg ear0,%0" : "=r"(__debug_mmu
.regs
.ear0
));
1238 asm volatile("movsg epcr0,%0" : "=r"(__debug_mmu
.regs
.epcr0
));
1240 /* read the protection / SAT registers */
1241 __debug_mmu
.iamr
[0].L
= __get_IAMLR(0);
1242 __debug_mmu
.iamr
[0].P
= __get_IAMPR(0);
1243 __debug_mmu
.iamr
[1].L
= __get_IAMLR(1);
1244 __debug_mmu
.iamr
[1].P
= __get_IAMPR(1);
1245 __debug_mmu
.iamr
[2].L
= __get_IAMLR(2);
1246 __debug_mmu
.iamr
[2].P
= __get_IAMPR(2);
1247 __debug_mmu
.iamr
[3].L
= __get_IAMLR(3);
1248 __debug_mmu
.iamr
[3].P
= __get_IAMPR(3);
1249 __debug_mmu
.iamr
[4].L
= __get_IAMLR(4);
1250 __debug_mmu
.iamr
[4].P
= __get_IAMPR(4);
1251 __debug_mmu
.iamr
[5].L
= __get_IAMLR(5);
1252 __debug_mmu
.iamr
[5].P
= __get_IAMPR(5);
1253 __debug_mmu
.iamr
[6].L
= __get_IAMLR(6);
1254 __debug_mmu
.iamr
[6].P
= __get_IAMPR(6);
1255 __debug_mmu
.iamr
[7].L
= __get_IAMLR(7);
1256 __debug_mmu
.iamr
[7].P
= __get_IAMPR(7);
1257 __debug_mmu
.iamr
[8].L
= __get_IAMLR(8);
1258 __debug_mmu
.iamr
[8].P
= __get_IAMPR(8);
1259 __debug_mmu
.iamr
[9].L
= __get_IAMLR(9);
1260 __debug_mmu
.iamr
[9].P
= __get_IAMPR(9);
1261 __debug_mmu
.iamr
[10].L
= __get_IAMLR(10);
1262 __debug_mmu
.iamr
[10].P
= __get_IAMPR(10);
1263 __debug_mmu
.iamr
[11].L
= __get_IAMLR(11);
1264 __debug_mmu
.iamr
[11].P
= __get_IAMPR(11);
1265 __debug_mmu
.iamr
[12].L
= __get_IAMLR(12);
1266 __debug_mmu
.iamr
[12].P
= __get_IAMPR(12);
1267 __debug_mmu
.iamr
[13].L
= __get_IAMLR(13);
1268 __debug_mmu
.iamr
[13].P
= __get_IAMPR(13);
1269 __debug_mmu
.iamr
[14].L
= __get_IAMLR(14);
1270 __debug_mmu
.iamr
[14].P
= __get_IAMPR(14);
1271 __debug_mmu
.iamr
[15].L
= __get_IAMLR(15);
1272 __debug_mmu
.iamr
[15].P
= __get_IAMPR(15);
1274 __debug_mmu
.damr
[0].L
= __get_DAMLR(0);
1275 __debug_mmu
.damr
[0].P
= __get_DAMPR(0);
1276 __debug_mmu
.damr
[1].L
= __get_DAMLR(1);
1277 __debug_mmu
.damr
[1].P
= __get_DAMPR(1);
1278 __debug_mmu
.damr
[2].L
= __get_DAMLR(2);
1279 __debug_mmu
.damr
[2].P
= __get_DAMPR(2);
1280 __debug_mmu
.damr
[3].L
= __get_DAMLR(3);
1281 __debug_mmu
.damr
[3].P
= __get_DAMPR(3);
1282 __debug_mmu
.damr
[4].L
= __get_DAMLR(4);
1283 __debug_mmu
.damr
[4].P
= __get_DAMPR(4);
1284 __debug_mmu
.damr
[5].L
= __get_DAMLR(5);
1285 __debug_mmu
.damr
[5].P
= __get_DAMPR(5);
1286 __debug_mmu
.damr
[6].L
= __get_DAMLR(6);
1287 __debug_mmu
.damr
[6].P
= __get_DAMPR(6);
1288 __debug_mmu
.damr
[7].L
= __get_DAMLR(7);
1289 __debug_mmu
.damr
[7].P
= __get_DAMPR(7);
1290 __debug_mmu
.damr
[8].L
= __get_DAMLR(8);
1291 __debug_mmu
.damr
[8].P
= __get_DAMPR(8);
1292 __debug_mmu
.damr
[9].L
= __get_DAMLR(9);
1293 __debug_mmu
.damr
[9].P
= __get_DAMPR(9);
1294 __debug_mmu
.damr
[10].L
= __get_DAMLR(10);
1295 __debug_mmu
.damr
[10].P
= __get_DAMPR(10);
1296 __debug_mmu
.damr
[11].L
= __get_DAMLR(11);
1297 __debug_mmu
.damr
[11].P
= __get_DAMPR(11);
1298 __debug_mmu
.damr
[12].L
= __get_DAMLR(12);
1299 __debug_mmu
.damr
[12].P
= __get_DAMPR(12);
1300 __debug_mmu
.damr
[13].L
= __get_DAMLR(13);
1301 __debug_mmu
.damr
[13].P
= __get_DAMPR(13);
1302 __debug_mmu
.damr
[14].L
= __get_DAMLR(14);
1303 __debug_mmu
.damr
[14].P
= __get_DAMPR(14);
1304 __debug_mmu
.damr
[15].L
= __get_DAMLR(15);
1305 __debug_mmu
.damr
[15].P
= __get_DAMPR(15);
1309 /* read the DAT entries from the TLB */
1310 struct __debug_amr
*p
;
1313 asm volatile("movsg tplr,%0" : "=r"(__debug_mmu
.regs
.tplr
));
1314 asm volatile("movsg tppr,%0" : "=r"(__debug_mmu
.regs
.tppr
));
1315 asm volatile("movsg tpxr,%0" : "=r"(__debug_mmu
.regs
.tpxr
));
1316 asm volatile("movsg cxnr,%0" : "=r"(__debug_mmu
.regs
.cxnr
));
1318 p
= __debug_mmu
.tlb
;
1321 asm volatile("movgs %0,tpxr" :: "r"(0 << TPXR_WAY_SHIFT
));
1322 for (loop
= 0; loop
< 64; loop
++) {
1323 asm volatile("tlbpr %0,gr0,#1,#0" :: "r"(loop
<< PAGE_SHIFT
));
1324 asm volatile("movsg tplr,%0" : "=r"(p
->L
));
1325 asm volatile("movsg tppr,%0" : "=r"(p
->P
));
1330 asm volatile("movgs %0,tpxr" :: "r"(1 << TPXR_WAY_SHIFT
));
1331 for (loop
= 0; loop
< 64; loop
++) {
1332 asm volatile("tlbpr %0,gr0,#1,#0" :: "r"(loop
<< PAGE_SHIFT
));
1333 asm volatile("movsg tplr,%0" : "=r"(p
->L
));
1334 asm volatile("movsg tppr,%0" : "=r"(p
->P
));
1338 asm volatile("movgs %0,tplr" :: "r"(__debug_mmu
.regs
.tplr
));
1339 asm volatile("movgs %0,tppr" :: "r"(__debug_mmu
.regs
.tppr
));
1340 asm volatile("movgs %0,tpxr" :: "r"(__debug_mmu
.regs
.tpxr
));
1344 } /* end gdbstub_get_mmu_state() */
1347 * handle general query commands of the form 'qXXXXX'
1349 static void gdbstub_handle_query(void)
1351 if (strcmp(input_buffer
, "qAttached") == 0) {
1352 /* return current thread ID */
1353 sprintf(output_buffer
, "1");
1357 if (strcmp(input_buffer
, "qC") == 0) {
1358 /* return current thread ID */
1359 sprintf(output_buffer
, "QC 0");
1363 if (strcmp(input_buffer
, "qOffsets") == 0) {
1364 /* return relocation offset of text and data segments */
1365 sprintf(output_buffer
, "Text=0;Data=0;Bss=0");
1369 if (strcmp(input_buffer
, "qSymbol::") == 0) {
1370 sprintf(output_buffer
, "OK");
1374 if (strcmp(input_buffer
, "qSupported") == 0) {
1375 /* query of supported features */
1376 sprintf(output_buffer
, "PacketSize=%u;ReverseContinue-;ReverseStep-",
1377 sizeof(input_buffer
));
1381 gdbstub_strcpy(output_buffer
,"E01");
1384 /*****************************************************************************/
1386 * handle event interception and GDB remote protocol processing
1388 * PSR.ET==0, PSR.S==1 and the CPU is in debug mode
1389 * __debug_frame points to the saved registers
1390 * __frame points to the kernel mode exception frame, if it was in kernel
1391 * mode when the break happened
1393 void gdbstub(int sigval
)
1395 unsigned long addr
, length
, loop
, dbar
, temp
, temp2
, temp3
;
1398 int flush_cache
= 0;
1403 #ifndef CONFIG_GDBSTUB_IMMEDIATE
1404 /* return immediately if GDB immediate activation option not set */
1411 save_user_regs(&__debug_frame0
->uc
);
1414 gdbstub_printk("--> gdbstub() %08x %p %08x %08x\n",
1418 __debug_regs
->bpsr
);
1419 // gdbstub_show_regs();
1424 /* if we were interrupted by input on the serial gdbstub serial port,
1425 * restore the context prior to the interrupt so that we return to that
1428 temp
= (unsigned long) __entry_kerneltrap_table
;
1429 temp2
= (unsigned long) __entry_usertrap_table
;
1430 temp3
= __debug_frame
->pc
& ~15;
1432 if (temp3
== temp
+ TBR_TT_INTERRUPT_15
||
1433 temp3
== temp2
+ TBR_TT_INTERRUPT_15
1435 asm volatile("movsg pcsr,%0" : "=r"(__debug_frame
->pc
));
1436 __debug_frame
->psr
|= PSR_ET
;
1437 __debug_frame
->psr
&= ~PSR_S
;
1438 if (__debug_frame
->psr
& PSR_PS
)
1439 __debug_frame
->psr
|= PSR_S
;
1440 __debug_status
.brr
= (__debug_frame
->tbr
& TBR_TT
) << 12;
1441 __debug_status
.brr
|= BRR_EB
;
1445 /* handle the decrement timer going off (FR451 only) */
1446 if (temp3
== temp
+ TBR_TT_DECREMENT_TIMER
||
1447 temp3
== temp2
+ TBR_TT_DECREMENT_TIMER
1449 asm volatile("movgs %0,timerd" :: "r"(10000000));
1450 asm volatile("movsg pcsr,%0" : "=r"(__debug_frame
->pc
));
1451 __debug_frame
->psr
|= PSR_ET
;
1452 __debug_frame
->psr
&= ~PSR_S
;
1453 if (__debug_frame
->psr
& PSR_PS
)
1454 __debug_frame
->psr
|= PSR_S
;
1455 __debug_status
.brr
= (__debug_frame
->tbr
& TBR_TT
) << 12;
1456 __debug_status
.brr
|= BRR_EB
;
1462 /* after a BREAK insn, the PC lands on the far side of it */
1463 if (__debug_status
.brr
& BRR_SB
)
1464 gdbstub_check_breakpoint();
1468 /* handle attempts to write console data via GDB "O" commands */
1469 if (__debug_frame
->pc
== (unsigned long) gdbstub_console_write
+ 4) {
1470 __gdbstub_console_write((struct console
*) __debug_frame
->gr8
,
1471 (const char *) __debug_frame
->gr9
,
1472 (unsigned) __debug_frame
->gr10
);
1476 if (gdbstub_rx_unget
) {
1478 goto packet_waiting
;
1482 sigval
= gdbstub_compute_signal(__debug_status
.brr
);
1486 /* send a message to the debugger's user saying what happened if it may
1487 * not be clear cut (we can't map exceptions onto signals properly)
1489 if (sigval
!= SIGINT
&& sigval
!= SIGTRAP
&& sigval
!= SIGILL
) {
1490 static const char title
[] = "Break ";
1491 static const char crlf
[] = "\r\n";
1492 unsigned long brr
= __debug_status
.brr
;
1495 ptr
= output_buffer
;
1497 ptr
= mem2hex(title
, ptr
, sizeof(title
) - 1,0);
1499 hx
= hex_asc_hi(brr
>> 24);
1500 ptr
= hex_byte_pack(ptr
, hx
);
1501 hx
= hex_asc_lo(brr
>> 24);
1502 ptr
= hex_byte_pack(ptr
, hx
);
1503 hx
= hex_asc_hi(brr
>> 16);
1504 ptr
= hex_byte_pack(ptr
, hx
);
1505 hx
= hex_asc_lo(brr
>> 16);
1506 ptr
= hex_byte_pack(ptr
, hx
);
1507 hx
= hex_asc_hi(brr
>> 8);
1508 ptr
= hex_byte_pack(ptr
, hx
);
1509 hx
= hex_asc_lo(brr
>> 8);
1510 ptr
= hex_byte_pack(ptr
, hx
);
1511 hx
= hex_asc_hi(brr
);
1512 ptr
= hex_byte_pack(ptr
, hx
);
1513 hx
= hex_asc_lo(brr
);
1514 ptr
= hex_byte_pack(ptr
, hx
);
1516 ptr
= mem2hex(crlf
, ptr
, sizeof(crlf
) - 1, 0);
1518 gdbstub_send_packet(output_buffer
); /* send it off... */
1523 /* tell the debugger that an exception has occurred */
1524 ptr
= output_buffer
;
1526 /* Send trap type (converted to signal) */
1528 ptr
= hex_byte_pack(ptr
, sigval
);
1531 ptr
= hex_byte_pack(ptr
, GDB_REG_PC
);
1533 ptr
= mem2hex(&__debug_frame
->pc
, ptr
, 4, 0);
1537 * Send frame pointer
1539 ptr
= hex_byte_pack(ptr
, GDB_REG_FP
);
1541 ptr
= mem2hex(&__debug_frame
->fp
, ptr
, 4, 0);
1545 * Send stack pointer
1547 ptr
= hex_byte_pack(ptr
, GDB_REG_SP
);
1549 ptr
= mem2hex(&__debug_frame
->sp
, ptr
, 4, 0);
1553 gdbstub_send_packet(output_buffer
); /* send it off... */
1558 gdbstub_get_mmu_state();
1560 /* wait for input from remote GDB */
1562 output_buffer
[0] = 0;
1565 gdbstub_recv_packet(input_buffer
);
1566 LEDS(0x5600 | input_buffer
[0]);
1568 switch (input_buffer
[0]) {
1569 /* request repeat of last signal number */
1571 output_buffer
[0] = 'S';
1572 output_buffer
[1] = hex_asc_hi(sigval
);
1573 output_buffer
[2] = hex_asc_lo(sigval
);
1574 output_buffer
[3] = 0;
1578 /* toggle debug flag */
1581 /* return the value of the CPU registers
1582 * - GR0, GR1, GR2, GR3, GR4, GR5, GR6, GR7,
1583 * - GR8, GR9, GR10, GR11, GR12, GR13, GR14, GR15,
1584 * - GR16, GR17, GR18, GR19, GR20, GR21, GR22, GR23,
1585 * - GR24, GR25, GR26, GR27, GR28, GR29, GR30, GR31,
1586 * - GR32, GR33, GR34, GR35, GR36, GR37, GR38, GR39,
1587 * - GR40, GR41, GR42, GR43, GR44, GR45, GR46, GR47,
1588 * - GR48, GR49, GR50, GR51, GR52, GR53, GR54, GR55,
1589 * - GR56, GR57, GR58, GR59, GR60, GR61, GR62, GR63,
1590 * - FP0, FP1, FP2, FP3, FP4, FP5, FP6, FP7,
1591 * - FP8, FP9, FP10, FP11, FP12, FP13, FP14, FP15,
1592 * - FP16, FP17, FP18, FP19, FP20, FP21, FP22, FP23,
1593 * - FP24, FP25, FP26, FP27, FP28, FP29, FP30, FP31,
1594 * - FP32, FP33, FP34, FP35, FP36, FP37, FP38, FP39,
1595 * - FP40, FP41, FP42, FP43, FP44, FP45, FP46, FP47,
1596 * - FP48, FP49, FP50, FP51, FP52, FP53, FP54, FP55,
1597 * - FP56, FP57, FP58, FP59, FP60, FP61, FP62, FP63,
1598 * - PC, PSR, CCR, CCCR,
1599 * - _X132, _X133, _X134
1600 * - TBR, BRR, DBAR0, DBAR1, DBAR2, DBAR3,
1601 * - _X141, _X142, _X143, _X144,
1606 ptr
= output_buffer
;
1608 /* deal with GR0, GR1-GR27, GR28-GR31, GR32-GR63 */
1609 ptr
= mem2hex(&zero
, ptr
, 4, 0);
1611 for (loop
= 1; loop
<= 27; loop
++)
1612 ptr
= mem2hex(&__debug_user_context
->i
.gr
[loop
], ptr
, 4, 0);
1613 temp
= (unsigned long) __frame
;
1614 ptr
= mem2hex(&temp
, ptr
, 4, 0);
1615 ptr
= mem2hex(&__debug_user_context
->i
.gr
[29], ptr
, 4, 0);
1616 ptr
= mem2hex(&__debug_user_context
->i
.gr
[30], ptr
, 4, 0);
1618 ptr
= mem2hex(&__debug_user_context
->i
.gr
[31], ptr
, 4, 0);
1620 temp
= (unsigned long) __debug_frame
;
1621 ptr
= mem2hex(&temp
, ptr
, 4, 0);
1624 for (loop
= 32; loop
<= 63; loop
++)
1625 ptr
= mem2hex(&__debug_user_context
->i
.gr
[loop
], ptr
, 4, 0);
1627 /* deal with FR0-FR63 */
1628 for (loop
= 0; loop
<= 63; loop
++)
1629 ptr
= mem2hex(&__debug_user_context
->f
.fr
[loop
], ptr
, 4, 0);
1631 /* deal with special registers */
1632 ptr
= mem2hex(&__debug_frame
->pc
, ptr
, 4, 0);
1633 ptr
= mem2hex(&__debug_frame
->psr
, ptr
, 4, 0);
1634 ptr
= mem2hex(&__debug_frame
->ccr
, ptr
, 4, 0);
1635 ptr
= mem2hex(&__debug_frame
->cccr
, ptr
, 4, 0);
1636 ptr
= mem2hex(&zero
, ptr
, 4, 0);
1637 ptr
= mem2hex(&zero
, ptr
, 4, 0);
1638 ptr
= mem2hex(&zero
, ptr
, 4, 0);
1639 ptr
= mem2hex(&__debug_frame
->tbr
, ptr
, 4, 0);
1640 ptr
= mem2hex(&__debug_status
.brr
, ptr
, 4, 0);
1642 asm volatile("movsg dbar0,%0" : "=r"(dbar
));
1643 ptr
= mem2hex(&dbar
, ptr
, 4, 0);
1644 asm volatile("movsg dbar1,%0" : "=r"(dbar
));
1645 ptr
= mem2hex(&dbar
, ptr
, 4, 0);
1646 asm volatile("movsg dbar2,%0" : "=r"(dbar
));
1647 ptr
= mem2hex(&dbar
, ptr
, 4, 0);
1648 asm volatile("movsg dbar3,%0" : "=r"(dbar
));
1649 ptr
= mem2hex(&dbar
, ptr
, 4, 0);
1651 asm volatile("movsg scr0,%0" : "=r"(dbar
));
1652 ptr
= mem2hex(&dbar
, ptr
, 4, 0);
1653 asm volatile("movsg scr1,%0" : "=r"(dbar
));
1654 ptr
= mem2hex(&dbar
, ptr
, 4, 0);
1655 asm volatile("movsg scr2,%0" : "=r"(dbar
));
1656 ptr
= mem2hex(&dbar
, ptr
, 4, 0);
1657 asm volatile("movsg scr3,%0" : "=r"(dbar
));
1658 ptr
= mem2hex(&dbar
, ptr
, 4, 0);
1660 ptr
= mem2hex(&__debug_frame
->lr
, ptr
, 4, 0);
1661 ptr
= mem2hex(&__debug_frame
->lcr
, ptr
, 4, 0);
1663 ptr
= mem2hex(&__debug_frame
->iacc0
, ptr
, 8, 0);
1665 ptr
= mem2hex(&__debug_user_context
->f
.fsr
[0], ptr
, 4, 0);
1667 for (loop
= 0; loop
<= 7; loop
++)
1668 ptr
= mem2hex(&__debug_user_context
->f
.acc
[loop
], ptr
, 4, 0);
1670 ptr
= mem2hex(&__debug_user_context
->f
.accg
, ptr
, 8, 0);
1672 for (loop
= 0; loop
<= 1; loop
++)
1673 ptr
= mem2hex(&__debug_user_context
->f
.msr
[loop
], ptr
, 4, 0);
1675 ptr
= mem2hex(&__debug_frame
->gner0
, ptr
, 4, 0);
1676 ptr
= mem2hex(&__debug_frame
->gner1
, ptr
, 4, 0);
1678 ptr
= mem2hex(&__debug_user_context
->f
.fner
[0], ptr
, 4, 0);
1679 ptr
= mem2hex(&__debug_user_context
->f
.fner
[1], ptr
, 4, 0);
1683 /* set the values of the CPU registers */
1685 ptr
= &input_buffer
[1];
1687 /* deal with GR0, GR1-GR27, GR28-GR31, GR32-GR63 */
1688 ptr
= hex2mem(ptr
, &temp
, 4);
1690 for (loop
= 1; loop
<= 27; loop
++)
1691 ptr
= hex2mem(ptr
, &__debug_user_context
->i
.gr
[loop
], 4);
1693 ptr
= hex2mem(ptr
, &temp
, 4);
1694 __frame
= (struct pt_regs
*) temp
;
1695 ptr
= hex2mem(ptr
, &__debug_frame
->gr29
, 4);
1696 ptr
= hex2mem(ptr
, &__debug_frame
->gr30
, 4);
1698 ptr
= hex2mem(ptr
, &__debug_frame
->gr31
, 4);
1700 ptr
= hex2mem(ptr
, &temp
, 4);
1703 for (loop
= 32; loop
<= 63; loop
++)
1704 ptr
= hex2mem(ptr
, &__debug_user_context
->i
.gr
[loop
], 4);
1706 /* deal with FR0-FR63 */
1707 for (loop
= 0; loop
<= 63; loop
++)
1708 ptr
= mem2hex(&__debug_user_context
->f
.fr
[loop
], ptr
, 4, 0);
1710 /* deal with special registers */
1711 ptr
= hex2mem(ptr
, &__debug_frame
->pc
, 4);
1712 ptr
= hex2mem(ptr
, &__debug_frame
->psr
, 4);
1713 ptr
= hex2mem(ptr
, &__debug_frame
->ccr
, 4);
1714 ptr
= hex2mem(ptr
, &__debug_frame
->cccr
,4);
1716 for (loop
= 132; loop
<= 140; loop
++)
1717 ptr
= hex2mem(ptr
, &temp
, 4);
1719 ptr
= hex2mem(ptr
, &temp
, 4);
1720 asm volatile("movgs %0,scr0" :: "r"(temp
));
1721 ptr
= hex2mem(ptr
, &temp
, 4);
1722 asm volatile("movgs %0,scr1" :: "r"(temp
));
1723 ptr
= hex2mem(ptr
, &temp
, 4);
1724 asm volatile("movgs %0,scr2" :: "r"(temp
));
1725 ptr
= hex2mem(ptr
, &temp
, 4);
1726 asm volatile("movgs %0,scr3" :: "r"(temp
));
1728 ptr
= hex2mem(ptr
, &__debug_frame
->lr
, 4);
1729 ptr
= hex2mem(ptr
, &__debug_frame
->lcr
, 4);
1731 ptr
= hex2mem(ptr
, &__debug_frame
->iacc0
, 8);
1733 ptr
= hex2mem(ptr
, &__debug_user_context
->f
.fsr
[0], 4);
1735 for (loop
= 0; loop
<= 7; loop
++)
1736 ptr
= hex2mem(ptr
, &__debug_user_context
->f
.acc
[loop
], 4);
1738 ptr
= hex2mem(ptr
, &__debug_user_context
->f
.accg
, 8);
1740 for (loop
= 0; loop
<= 1; loop
++)
1741 ptr
= hex2mem(ptr
, &__debug_user_context
->f
.msr
[loop
], 4);
1743 ptr
= hex2mem(ptr
, &__debug_frame
->gner0
, 4);
1744 ptr
= hex2mem(ptr
, &__debug_frame
->gner1
, 4);
1746 ptr
= hex2mem(ptr
, &__debug_user_context
->f
.fner
[0], 4);
1747 ptr
= hex2mem(ptr
, &__debug_user_context
->f
.fner
[1], 4);
1749 gdbstub_strcpy(output_buffer
,"OK");
1752 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
1754 ptr
= &input_buffer
[1];
1756 if (hexToInt(&ptr
, &addr
) &&
1758 hexToInt(&ptr
, &length
)
1760 if (mem2hex((char *)addr
, output_buffer
, length
, 1))
1762 gdbstub_strcpy (output_buffer
, "E03");
1765 gdbstub_strcpy(output_buffer
,"E01");
1769 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
1771 ptr
= &input_buffer
[1];
1773 if (hexToInt(&ptr
, &addr
) &&
1775 hexToInt(&ptr
, &length
) &&
1778 if (hex2mem(ptr
, (char *)addr
, length
)) {
1779 gdbstub_strcpy(output_buffer
, "OK");
1782 gdbstub_strcpy(output_buffer
, "E03");
1786 gdbstub_strcpy(output_buffer
, "E02");
1791 /* pNN: Read value of reg N and return it */
1793 /* return no value, indicating that we don't support
1794 * this command and that gdb should use 'g' instead */
1797 /* PNN,=RRRRRRRR: Write value R to reg N return OK */
1799 ptr
= &input_buffer
[1];
1801 if (!hexToInt(&ptr
, &addr
) ||
1803 !hexToInt(&ptr
, &temp
)
1805 gdbstub_strcpy(output_buffer
, "E01");
1813 case GDB_REG_GR(1) ... GDB_REG_GR(63):
1814 __debug_user_context
->i
.gr
[addr
- GDB_REG_GR(0)] = temp
;
1816 case GDB_REG_FR(0) ... GDB_REG_FR(63):
1817 __debug_user_context
->f
.fr
[addr
- GDB_REG_FR(0)] = temp
;
1820 __debug_user_context
->i
.pc
= temp
;
1823 __debug_user_context
->i
.psr
= temp
;
1826 __debug_user_context
->i
.ccr
= temp
;
1829 __debug_user_context
->i
.cccr
= temp
;
1832 __debug_status
.brr
= temp
;
1835 __debug_user_context
->i
.lr
= temp
;
1838 __debug_user_context
->i
.lcr
= temp
;
1841 __debug_user_context
->f
.fsr
[0] = temp
;
1843 case GDB_REG_ACC(0) ... GDB_REG_ACC(7):
1844 __debug_user_context
->f
.acc
[addr
- GDB_REG_ACC(0)] = temp
;
1846 case GDB_REG_ACCG(0):
1847 *(uint32_t *) &__debug_user_context
->f
.accg
[0] = temp
;
1849 case GDB_REG_ACCG(4):
1850 *(uint32_t *) &__debug_user_context
->f
.accg
[4] = temp
;
1852 case GDB_REG_MSR(0) ... GDB_REG_MSR(1):
1853 __debug_user_context
->f
.msr
[addr
- GDB_REG_MSR(0)] = temp
;
1855 case GDB_REG_GNER(0) ... GDB_REG_GNER(1):
1856 __debug_user_context
->i
.gner
[addr
- GDB_REG_GNER(0)] = temp
;
1858 case GDB_REG_FNER(0) ... GDB_REG_FNER(1):
1859 __debug_user_context
->f
.fner
[addr
- GDB_REG_FNER(0)] = temp
;
1867 gdbstub_strcpy(output_buffer
, "OK");
1870 gdbstub_strcpy(output_buffer
, "E02");
1874 /* cAA..AA Continue at address AA..AA(optional) */
1876 /* try to read optional parameter, pc unchanged if no parm */
1877 ptr
= &input_buffer
[1];
1878 if (hexToInt(&ptr
, &addr
))
1879 __debug_frame
->pc
= addr
;
1882 /* kill the program */
1884 goto done
; /* just continue */
1888 gdbstub_strcpy(output_buffer
, "OK");
1891 /* reset the whole machine (FIXME: system dependent) */
1896 /* step to next instruction */
1898 __debug_regs
->dcr
|= DCR_SE
;
1899 __debug_status
.dcr
|= DCR_SE
;
1902 /* extended command */
1904 if (strcmp(input_buffer
, "vCont?") == 0) {
1905 output_buffer
[0] = 0;
1908 goto unsupported_cmd
;
1910 /* set baud rate (bBB) */
1912 ptr
= &input_buffer
[1];
1913 if (!hexToInt(&ptr
, &temp
)) {
1914 gdbstub_strcpy(output_buffer
,"B01");
1919 /* ack before changing speed */
1920 gdbstub_send_packet("OK");
1921 gdbstub_set_baud(temp
);
1925 /* set breakpoint */
1927 ptr
= &input_buffer
[1];
1929 if (!hexToInt(&ptr
,&temp
) || *ptr
++ != ',' ||
1930 !hexToInt(&ptr
,&addr
) || *ptr
++ != ',' ||
1931 !hexToInt(&ptr
,&length
)
1933 gdbstub_strcpy(output_buffer
,"E01");
1938 gdbstub_strcpy(output_buffer
,"E03");
1942 if (gdbstub_set_breakpoint(temp
, addr
, length
) < 0) {
1943 gdbstub_strcpy(output_buffer
,"E03");
1948 flush_cache
= 1; /* soft bkpt by modified memory */
1950 gdbstub_strcpy(output_buffer
,"OK");
1953 /* clear breakpoint */
1955 ptr
= &input_buffer
[1];
1957 if (!hexToInt(&ptr
,&temp
) || *ptr
++ != ',' ||
1958 !hexToInt(&ptr
,&addr
) || *ptr
++ != ',' ||
1959 !hexToInt(&ptr
,&length
)
1961 gdbstub_strcpy(output_buffer
,"E01");
1966 gdbstub_strcpy(output_buffer
,"E03");
1970 if (gdbstub_clear_breakpoint(temp
, addr
, length
) < 0) {
1971 gdbstub_strcpy(output_buffer
,"E03");
1976 flush_cache
= 1; /* soft bkpt by modified memory */
1978 gdbstub_strcpy(output_buffer
,"OK");
1981 /* Thread-setting packet */
1983 gdbstub_strcpy(output_buffer
, "OK");
1987 gdbstub_handle_query();
1992 gdbstub_proto("### GDB Unsupported Cmd '%s'\n",input_buffer
);
1993 gdbstub_strcpy(output_buffer
,"E01");
1997 /* reply to the request */
1999 gdbstub_send_packet(output_buffer
);
2003 restore_user_regs(&__debug_frame0
->uc
);
2005 //gdbstub_dump_debugregs();
2006 //gdbstub_printk("<-- gdbstub() %08x\n", __debug_frame->pc);
2008 /* need to flush the instruction cache before resuming, as we may have
2009 * deposited a breakpoint, and the icache probably has no way of
2010 * knowing that a data ref to some location may have changed something
2011 * that is in the instruction cache. NB: We flush both caches, just to
2015 /* note: flushing the icache will clobber EAR0 on the FR451 */
2017 gdbstub_purge_cache();
2021 } /* end gdbstub() */
2023 /*****************************************************************************/
2025 * initialise the GDB stub
2027 void __init
gdbstub_init(void)
2029 #ifdef CONFIG_GDBSTUB_IMMEDIATE
2034 gdbstub_printk("%s", gdbstub_banner
);
2038 /* try to talk to GDB (or anyone insane enough to want to type GDB protocol by hand) */
2039 gdbstub_proto("### GDB Tx ACK\n");
2040 gdbstub_tx_char('+'); /* 'hello world' */
2042 #ifdef CONFIG_GDBSTUB_IMMEDIATE
2043 gdbstub_printk("GDB Stub waiting for packet\n");
2046 * In case GDB is started before us, ack any packets
2047 * (presumably "$?#xx") sitting there.
2049 do { gdbstub_rx_char(&ch
, 0); } while (ch
!= '$');
2050 do { gdbstub_rx_char(&ch
, 0); } while (ch
!= '#');
2051 do { ret
= gdbstub_rx_char(&ch
, 0); } while (ret
!= 0); /* eat first csum byte */
2052 do { ret
= gdbstub_rx_char(&ch
, 0); } while (ret
!= 0); /* eat second csum byte */
2054 gdbstub_proto("### GDB Tx NAK\n");
2055 gdbstub_tx_char('-'); /* nak it */
2058 gdbstub_printk("GDB Stub set\n");
2063 ptr
= output_buffer
;
2065 ptr
= mem2hex(gdbstub_banner
, ptr
, sizeof(gdbstub_banner
) - 1, 0);
2066 gdbstub_send_packet(output_buffer
);
2068 #if defined(CONFIG_GDB_CONSOLE) && defined(CONFIG_GDBSTUB_IMMEDIATE)
2069 register_console(&gdbstub_console
);
2072 } /* end gdbstub_init() */
2074 /*****************************************************************************/
2076 * register the console at a more appropriate time
2078 #if defined (CONFIG_GDB_CONSOLE) && !defined(CONFIG_GDBSTUB_IMMEDIATE)
2079 static int __init
gdbstub_postinit(void)
2081 printk("registering console\n");
2082 register_console(&gdbstub_console
);
2084 } /* end gdbstub_postinit() */
2086 __initcall(gdbstub_postinit
);
2089 /*****************************************************************************/
2091 * send an exit message to GDB
2093 void gdbstub_exit(int status
)
2095 unsigned char checksum
;
2099 sprintf(output_buffer
,"W%02x",status
&0xff);
2101 gdbstub_tx_char('$');
2105 while ((ch
= output_buffer
[count
]) != 0) {
2106 gdbstub_tx_char(ch
);
2111 gdbstub_tx_char('#');
2112 gdbstub_tx_char(hex_asc_hi(checksum
));
2113 gdbstub_tx_char(hex_asc_lo(checksum
));
2115 /* make sure the output is flushed, or else RedBoot might clobber it */
2116 gdbstub_tx_char('-');
2119 } /* end gdbstub_exit() */
2121 /*****************************************************************************/
2123 * GDB wants to call malloc() and free() to allocate memory for calling kernel
2124 * functions directly from its command line
2126 static void *malloc(size_t size
) __maybe_unused
;
2127 static void *malloc(size_t size
)
2129 return kmalloc(size
, GFP_ATOMIC
);
2132 static void free(void *p
) __maybe_unused
;
2133 static void free(void *p
)
2138 static uint32_t ___get_HSR0(void) __maybe_unused
;
2139 static uint32_t ___get_HSR0(void)
2141 return __get_HSR(0);
2144 static uint32_t ___set_HSR0(uint32_t x
) __maybe_unused
;
2145 static uint32_t ___set_HSR0(uint32_t x
)
2148 return __get_HSR(0);