117 #include <linux/string.h>
118 #include <linux/kernel.h>
119 #include <linux/signal.h>
120 #include <linux/sched.h>
124 #include <linux/slab.h>
127 #include <asm/asm-offsets.h>
128 #include <asm/pgtable.h>
129 #include <asm/gdb-stub.h>
131 #define LEDS(x) do { } while(0)
133 #undef GDBSTUB_DEBUG_PROTOCOL
148 unsigned long tplr, tppr, tpxr, cxnr;
168 #define BREAK_INSN 0x801000c0
170 static const char gdbstub_banner[] =
"Linux/FR-V GDB Stub (c) RedHat 2003\n";
181 static
char input_buffer[
BUFMAX];
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"
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);
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')
235 static char buf[1024];
241 len =
vsnprintf(buf,
sizeof(buf), fmt, args);
246 static inline char *gdbstub_strcpy(
char *
dst,
const char *
src)
249 while ((dst[loop] = src[loop]))
256 asm volatile(
" dcef @(gr0,gr0),#1 \n"
257 " icei @(gr0,gr0),#1 \n"
267 static void gdbstub_recv_packet(
char *
buffer)
270 unsigned char xmitcsum;
305 if (count >=
BUFMAX || error)
314 xmitcsum = hex(ch) << 4;
330 if (checksum != xmitcsum) {
336 gdbstub_proto(
"### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
341 if (buffer[2] ==
':') {
347 while (buffer[count]) count++;
348 for (i=3; i <=
count; i++)
349 buffer[i - 3] = buffer[i];
362 static int gdbstub_send_packet(
char *buffer)
376 while ((ch = buffer[count]) != 0) {
387 #ifdef GDBSTUB_DEBUG_PROTOCOL
389 ch!=
'-' && ch!=
'+' && (
gdbstub_proto(
"### GDB Rx ??? %02x\n",ch),0),
399 gdbstub_rx_unget = ch;
407 static int hexToInt(
char **
ptr,
unsigned long *_value)
417 *_value = (*_value << 4) | ((
uint8_t) ch & 0xf);
430 static inline int gdbstub_addr_probe(
const void *
vaddr)
435 asm(
"lrad %1,%0,#1,#0,#0" :
"=r"(
paddr) :
"r"(vaddr));
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;
480 static inline int gdbstub_addr_map(
const void *vaddr)
488 if (gdbstub_addr_probe(vaddr))
491 pte = gdbstub_virt_to_pte((
unsigned long) vaddr);
501 static inline void gdbstub_addr_unmap(
void)
517 if (!gdbstub_addr_map(addr))
520 asm volatile(
" movgs gr0,brr \n"
523 :
"=r"(
res),
"=r"(brr)
526 gdbstub_addr_unmap();
534 if (!gdbstub_addr_map(addr))
537 asm volatile(
" movgs gr0,brr \n"
541 :
"r"(val),
"m"(*(
uint32_t *) addr));
542 gdbstub_addr_unmap();
551 if (!gdbstub_addr_map(addr))
554 asm volatile(
" movgs gr0,brr \n"
557 :
"=r"(
res),
"=r"(brr)
560 gdbstub_addr_unmap();
568 if (!gdbstub_addr_map(addr))
571 asm volatile(
" movgs gr0,brr \n"
575 :
"r"(val),
"m"(*(
uint16_t *) addr));
576 gdbstub_addr_unmap();
585 if (!gdbstub_addr_map(addr))
588 asm volatile(
" movgs gr0,brr \n"
591 :
"=r"(
res),
"=r"(brr)
594 gdbstub_addr_unmap();
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);
647 #ifdef CONFIG_GDB_CONSOLE
649 static struct console gdbstub_console = {
666 static unsigned char *mem2hex(
const void *_mem,
char *
buf,
int count,
int may_fault)
674 buf = hex_byte_pack(buf, ch[0]);
682 buf = hex_byte_pack(buf, ch[0]);
683 buf = hex_byte_pack(buf, ch[1]);
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]);
702 buf = hex_byte_pack(buf, ch[0]);
703 buf = hex_byte_pack(buf, ch[1]);
711 buf = hex_byte_pack(buf, ch[0]);
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++);
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++);
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++);
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++);
780 ch.b[0] = hex(*buf++) << 4;
781 ch.b[0] |= hex(*buf++);
796 static const struct brr_to_sig_map {
797 unsigned long brr_mask;
798 unsigned long tbr_tt;
800 } brr_to_sig_map[] = {
827 static inline int gdbstub_compute_signal(
unsigned long brr)
829 const struct brr_to_sig_map *
map;
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)
847 int bkpt, loop, xloop;
851 unsigned long mask0, mask1;
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++)
872 &gdbstub_bkpts[bkpt].originsns[loop]))
875 for (loop = 0; loop < len/4; loop++)
880 for (xloop = 0; xloop < loop; 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],
904 if (addr & 3 || len != 4)
911 asm volatile(
"movgs %0,ibar0" : :
"r"(
addr));
919 asm volatile(
"movgs %0,ibar1" : :
"r"(
addr));
927 asm volatile(
"movgs %0,ibar2" : :
"r"(
addr));
935 asm volatile(
"movgs %0,ibar3" : :
"r"(
addr));
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;
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));
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));
1015 unsigned long mask0, mask1;
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++)
1035 gdbstub_bkpts[bkpt].originsns[loop]))
1041 if (addr & 3 || len != 4)
1044 #define __get_ibar(X) ({ unsigned long x; asm volatile("movsg ibar"#X",%0" : "=r"(x)); x; })
1050 asm volatile(
"movgs gr0,ibar0");
1058 asm volatile(
"movgs gr0,ibar1");
1066 asm volatile(
"movgs gr0,ibar2");
1074 asm volatile(
"movgs gr0,ibar3");
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; })
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");
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");
1160 static void gdbstub_check_breakpoint(
void)
1165 for (bkpt = 255; bkpt >= 0; bkpt--)
1166 if (gdbstub_bkpts[bkpt].addr == addr)
1191 for (loop = 0; loop < NR_PT_REGS; loop++) {
1194 if (loop == NR_PT_REGS - 1 || loop % 5 == 4)
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));
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));
1349 static void gdbstub_handle_query(
void)
1351 if (
strcmp(input_buffer,
"qAttached") == 0) {
1357 if (
strcmp(input_buffer,
"qC") == 0) {
1363 if (
strcmp(input_buffer,
"qOffsets") == 0) {
1369 if (
strcmp(input_buffer,
"qSymbol::") == 0) {
1374 if (
strcmp(input_buffer,
"qSupported") == 0) {
1377 sizeof(input_buffer));
1403 #ifndef CONFIG_GDBSTUB_IMMEDIATE
1449 asm volatile(
"movgs %0,timerd" ::
"r"(10000000));
1464 gdbstub_check_breakpoint();
1476 if (gdbstub_rx_unget) {
1478 goto packet_waiting;
1490 static const char title[] =
"Break ";
1491 static const char crlf[] =
"\r\n";
1497 ptr = mem2hex(title, ptr,
sizeof(title) - 1,0);
1500 ptr = hex_byte_pack(ptr, hx);
1502 ptr = hex_byte_pack(ptr, hx);
1504 ptr = hex_byte_pack(ptr, hx);
1506 ptr = hex_byte_pack(ptr, hx);
1508 ptr = hex_byte_pack(ptr, hx);
1510 ptr = hex_byte_pack(ptr, hx);
1512 ptr = hex_byte_pack(ptr, hx);
1514 ptr = hex_byte_pack(ptr, hx);
1516 ptr = mem2hex(crlf, ptr,
sizeof(crlf) - 1, 0);
1528 ptr = hex_byte_pack(ptr, sigval);
1565 gdbstub_recv_packet(input_buffer);
1566 LEDS(0x5600 | input_buffer[0]);
1568 switch (input_buffer[0]) {
1609 ptr = mem2hex(&zero, ptr, 4, 0);
1611 for (loop = 1; loop <= 27; loop++)
1613 temp = (
unsigned long) __frame;
1614 ptr = mem2hex(&temp, ptr, 4, 0);
1621 ptr = mem2hex(&temp, ptr, 4, 0);
1624 for (loop = 32; loop <= 63; loop++)
1628 for (loop = 0; loop <= 63; loop++)
1636 ptr = mem2hex(&zero, ptr, 4, 0);
1637 ptr = mem2hex(&zero, ptr, 4, 0);
1638 ptr = mem2hex(&zero, 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);
1667 for (loop = 0; loop <= 7; loop++)
1672 for (loop = 0; loop <= 1; loop++)
1685 ptr = &input_buffer[1];
1688 ptr = hex2mem(ptr, &temp, 4);
1690 for (loop = 1; loop <= 27; loop++)
1693 ptr = hex2mem(ptr, &temp, 4);
1694 __frame = (
struct pt_regs *) temp;
1700 ptr = hex2mem(ptr, &temp, 4);
1703 for (loop = 32; loop <= 63; loop++)
1707 for (loop = 0; loop <= 63; loop++)
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));
1735 for (loop = 0; loop <= 7; loop++)
1740 for (loop = 0; loop <= 1; loop++)
1754 ptr = &input_buffer[1];
1756 if (hexToInt(&ptr, &addr) &&
1758 hexToInt(&ptr, &length)
1771 ptr = &input_buffer[1];
1773 if (hexToInt(&ptr, &addr) &&
1775 hexToInt(&ptr, &length) &&
1778 if (hex2mem(ptr, (
char *)addr, length)) {
1799 ptr = &input_buffer[1];
1801 if (!hexToInt(&ptr, &addr) ||
1803 !hexToInt(&ptr, &temp)
1877 ptr = &input_buffer[1];
1878 if (hexToInt(&ptr, &addr))
1904 if (
strcmp(input_buffer,
"vCont?") == 0) {
1908 goto unsupported_cmd;
1912 ptr = &input_buffer[1];
1913 if (!hexToInt(&ptr, &temp)) {
1920 gdbstub_send_packet(
"OK");
1927 ptr = &input_buffer[1];
1929 if (!hexToInt(&ptr,&temp) || *ptr++ !=
',' ||
1930 !hexToInt(&ptr,&addr) || *ptr++ !=
',' ||
1931 !hexToInt(&ptr,&length)
1955 ptr = &input_buffer[1];
1957 if (!hexToInt(&ptr,&temp) || *ptr++ !=
',' ||
1958 !hexToInt(&ptr,&addr) || *ptr++ !=
',' ||
1959 !hexToInt(&ptr,&length)
1987 gdbstub_handle_query();
1992 gdbstub_proto(
"### GDB Unsupported Cmd '%s'\n",input_buffer);
2029 #ifdef CONFIG_GDBSTUB_IMMEDIATE
2042 #ifdef CONFIG_GDBSTUB_IMMEDIATE
2065 ptr = mem2hex(gdbstub_banner, ptr,
sizeof(gdbstub_banner) - 1, 0);
2068 #if defined(CONFIG_GDB_CONSOLE) && defined(CONFIG_GDBSTUB_IMMEDIATE)
2078 #if defined (CONFIG_GDB_CONSOLE) && !defined(CONFIG_GDBSTUB_IMMEDIATE)
2079 static int __init gdbstub_postinit(
void)
2081 printk(
"registering console\n");
2133 static void free(
void *p)
2138 static uint32_t ___get_HSR0(
void) __maybe_unused;