PageRenderTime 74ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/mn10300/kernel/gdb-stub.c

https://github.com/mstsirkin/kvm
C | 1924 lines | 1319 code | 255 blank | 350 comment | 276 complexity | 6606eb7ccaa32165b3c2de87a0b705e1 MD5 | raw file
  1. /* MN10300 GDB stub
  2. *
  3. * Originally written by Glenn Engel, Lake Stevens Instrument Division
  4. *
  5. * Contributed by HP Systems
  6. *
  7. * Modified for SPARC by Stu Grossman, Cygnus Support.
  8. *
  9. * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
  10. * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
  11. *
  12. * Copyright (C) 1995 Andreas Busse
  13. *
  14. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  15. * Modified for Linux/mn10300 by David Howells <dhowells@redhat.com>
  16. */
  17. /*
  18. * To enable debugger support, two things need to happen. One, a
  19. * call to set_debug_traps() is necessary in order to allow any breakpoints
  20. * or error conditions to be properly intercepted and reported to gdb.
  21. * Two, a breakpoint needs to be generated to begin communication. This
  22. * is most easily accomplished by a call to breakpoint(). Breakpoint()
  23. * simulates a breakpoint by executing a BREAK instruction.
  24. *
  25. *
  26. * The following gdb commands are supported:
  27. *
  28. * command function Return value
  29. *
  30. * g return the value of the CPU registers hex data or ENN
  31. * G set the value of the CPU registers OK or ENN
  32. *
  33. * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
  34. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
  35. *
  36. * c Resume at current address SNN ( signal NN)
  37. * cAA..AA Continue at address AA..AA SNN
  38. *
  39. * s Step one instruction SNN
  40. * sAA..AA Step one instruction from AA..AA SNN
  41. *
  42. * k kill
  43. *
  44. * ? What was the last sigval ? SNN (signal NN)
  45. *
  46. * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
  47. * baud rate
  48. *
  49. * All commands and responses are sent with a packet which includes a
  50. * checksum. A packet consists of
  51. *
  52. * $<packet info>#<checksum>.
  53. *
  54. * where
  55. * <packet info> :: <characters representing the command or response>
  56. * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
  57. *
  58. * When a packet is received, it is first acknowledged with either '+' or '-'.
  59. * '+' indicates a successful transfer. '-' indicates a failed transfer.
  60. *
  61. * Example:
  62. *
  63. * Host: Reply:
  64. * $m0,10#2a +$00010203040506070809101112131415#42
  65. *
  66. *
  67. * ==============
  68. * MORE EXAMPLES:
  69. * ==============
  70. *
  71. * For reference -- the following are the steps that one
  72. * company took (RidgeRun Inc) to get remote gdb debugging
  73. * going. In this scenario the host machine was a PC and the
  74. * target platform was a Galileo EVB64120A MIPS evaluation
  75. * board.
  76. *
  77. * Step 1:
  78. * First download gdb-5.0.tar.gz from the internet.
  79. * and then build/install the package.
  80. *
  81. * Example:
  82. * $ tar zxf gdb-5.0.tar.gz
  83. * $ cd gdb-5.0
  84. * $ ./configure --target=am33_2.0-linux-gnu
  85. * $ make
  86. * $ install
  87. * am33_2.0-linux-gnu-gdb
  88. *
  89. * Step 2:
  90. * Configure linux for remote debugging and build it.
  91. *
  92. * Example:
  93. * $ cd ~/linux
  94. * $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
  95. * $ make dep; make vmlinux
  96. *
  97. * Step 3:
  98. * Download the kernel to the remote target and start
  99. * the kernel running. It will promptly halt and wait
  100. * for the host gdb session to connect. It does this
  101. * since the "Kernel Hacking" option has defined
  102. * CONFIG_REMOTE_DEBUG which in turn enables your calls
  103. * to:
  104. * set_debug_traps();
  105. * breakpoint();
  106. *
  107. * Step 4:
  108. * Start the gdb session on the host.
  109. *
  110. * Example:
  111. * $ am33_2.0-linux-gnu-gdb vmlinux
  112. * (gdb) set remotebaud 115200
  113. * (gdb) target remote /dev/ttyS1
  114. * ...at this point you are connected to
  115. * the remote target and can use gdb
  116. * in the normal fasion. Setting
  117. * breakpoints, single stepping,
  118. * printing variables, etc.
  119. *
  120. */
  121. #include <linux/string.h>
  122. #include <linux/kernel.h>
  123. #include <linux/signal.h>
  124. #include <linux/sched.h>
  125. #include <linux/mm.h>
  126. #include <linux/console.h>
  127. #include <linux/init.h>
  128. #include <linux/bug.h>
  129. #include <asm/pgtable.h>
  130. #include <asm/system.h>
  131. #include <asm/gdb-stub.h>
  132. #include <asm/exceptions.h>
  133. #include <asm/debugger.h>
  134. #include <asm/serial-regs.h>
  135. #include <asm/busctl-regs.h>
  136. #include <unit/leds.h>
  137. #include <unit/serial.h>
  138. /* define to use F7F7 rather than FF which is subverted by JTAG debugger */
  139. #undef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  140. /*
  141. * BUFMAX defines the maximum number of characters in inbound/outbound buffers
  142. * at least NUMREGBYTES*2 are needed for register packets
  143. */
  144. #define BUFMAX 2048
  145. static const char gdbstub_banner[] =
  146. "Linux/MN10300 GDB Stub (c) RedHat 2007\n";
  147. u8 gdbstub_rx_buffer[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
  148. u32 gdbstub_rx_inp;
  149. u32 gdbstub_rx_outp;
  150. u8 gdbstub_busy;
  151. u8 gdbstub_rx_overflow;
  152. u8 gdbstub_rx_unget;
  153. static u8 gdbstub_flush_caches;
  154. static char input_buffer[BUFMAX];
  155. static char output_buffer[BUFMAX];
  156. static char trans_buffer[BUFMAX];
  157. struct gdbstub_bkpt {
  158. u8 *addr; /* address of breakpoint */
  159. u8 len; /* size of breakpoint */
  160. u8 origbytes[7]; /* original bytes */
  161. };
  162. static struct gdbstub_bkpt gdbstub_bkpts[256];
  163. /*
  164. * local prototypes
  165. */
  166. static void getpacket(char *buffer);
  167. static int putpacket(char *buffer);
  168. static int computeSignal(enum exception_code excep);
  169. static int hex(unsigned char ch);
  170. static int hexToInt(char **ptr, int *intValue);
  171. static unsigned char *mem2hex(const void *mem, char *buf, int count,
  172. int may_fault);
  173. static const char *hex2mem(const char *buf, void *_mem, int count,
  174. int may_fault);
  175. /*
  176. * Convert ch from a hex digit to an int
  177. */
  178. static int hex(unsigned char ch)
  179. {
  180. if (ch >= 'a' && ch <= 'f')
  181. return ch - 'a' + 10;
  182. if (ch >= '0' && ch <= '9')
  183. return ch - '0';
  184. if (ch >= 'A' && ch <= 'F')
  185. return ch - 'A' + 10;
  186. return -1;
  187. }
  188. #ifdef CONFIG_GDBSTUB_DEBUGGING
  189. void debug_to_serial(const char *p, int n)
  190. {
  191. __debug_to_serial(p, n);
  192. /* gdbstub_console_write(NULL, p, n); */
  193. }
  194. void gdbstub_printk(const char *fmt, ...)
  195. {
  196. va_list args;
  197. int len;
  198. /* Emit the output into the temporary buffer */
  199. va_start(args, fmt);
  200. len = vsnprintf(trans_buffer, sizeof(trans_buffer), fmt, args);
  201. va_end(args);
  202. debug_to_serial(trans_buffer, len);
  203. }
  204. #endif
  205. static inline char *gdbstub_strcpy(char *dst, const char *src)
  206. {
  207. int loop = 0;
  208. while ((dst[loop] = src[loop]))
  209. loop++;
  210. return dst;
  211. }
  212. /*
  213. * scan for the sequence $<data>#<checksum>
  214. */
  215. static void getpacket(char *buffer)
  216. {
  217. unsigned char checksum;
  218. unsigned char xmitcsum;
  219. unsigned char ch;
  220. int count, i, ret, error;
  221. for (;;) {
  222. /*
  223. * wait around for the start character,
  224. * ignore all other characters
  225. */
  226. do {
  227. gdbstub_io_rx_char(&ch, 0);
  228. } while (ch != '$');
  229. checksum = 0;
  230. xmitcsum = -1;
  231. count = 0;
  232. error = 0;
  233. /*
  234. * now, read until a # or end of buffer is found
  235. */
  236. while (count < BUFMAX) {
  237. ret = gdbstub_io_rx_char(&ch, 0);
  238. if (ret < 0)
  239. error = ret;
  240. if (ch == '#')
  241. break;
  242. checksum += ch;
  243. buffer[count] = ch;
  244. count++;
  245. }
  246. if (error == -EIO) {
  247. gdbstub_proto("### GDB Rx Error - Skipping packet"
  248. " ###\n");
  249. gdbstub_proto("### GDB Tx NAK\n");
  250. gdbstub_io_tx_char('-');
  251. continue;
  252. }
  253. if (count >= BUFMAX || error)
  254. continue;
  255. buffer[count] = 0;
  256. /* read the checksum */
  257. ret = gdbstub_io_rx_char(&ch, 0);
  258. if (ret < 0)
  259. error = ret;
  260. xmitcsum = hex(ch) << 4;
  261. ret = gdbstub_io_rx_char(&ch, 0);
  262. if (ret < 0)
  263. error = ret;
  264. xmitcsum |= hex(ch);
  265. if (error) {
  266. if (error == -EIO)
  267. gdbstub_io("### GDB Rx Error -"
  268. " Skipping packet\n");
  269. gdbstub_io("### GDB Tx NAK\n");
  270. gdbstub_io_tx_char('-');
  271. continue;
  272. }
  273. /* check the checksum */
  274. if (checksum != xmitcsum) {
  275. gdbstub_io("### GDB Tx NAK\n");
  276. gdbstub_io_tx_char('-'); /* failed checksum */
  277. continue;
  278. }
  279. gdbstub_proto("### GDB Rx '$%s#%02x' ###\n", buffer, checksum);
  280. gdbstub_io("### GDB Tx ACK\n");
  281. gdbstub_io_tx_char('+'); /* successful transfer */
  282. /*
  283. * if a sequence char is present,
  284. * reply the sequence ID
  285. */
  286. if (buffer[2] == ':') {
  287. gdbstub_io_tx_char(buffer[0]);
  288. gdbstub_io_tx_char(buffer[1]);
  289. /*
  290. * remove sequence chars from buffer
  291. */
  292. count = 0;
  293. while (buffer[count])
  294. count++;
  295. for (i = 3; i <= count; i++)
  296. buffer[i - 3] = buffer[i];
  297. }
  298. break;
  299. }
  300. }
  301. /*
  302. * send the packet in buffer.
  303. * - return 0 if successfully ACK'd
  304. * - return 1 if abandoned due to new incoming packet
  305. */
  306. static int putpacket(char *buffer)
  307. {
  308. unsigned char checksum;
  309. unsigned char ch;
  310. int count;
  311. /*
  312. * $<packet info>#<checksum>.
  313. */
  314. gdbstub_proto("### GDB Tx $'%s'#?? ###\n", buffer);
  315. do {
  316. gdbstub_io_tx_char('$');
  317. checksum = 0;
  318. count = 0;
  319. while ((ch = buffer[count]) != 0) {
  320. gdbstub_io_tx_char(ch);
  321. checksum += ch;
  322. count += 1;
  323. }
  324. gdbstub_io_tx_char('#');
  325. gdbstub_io_tx_char(hex_asc_hi(checksum));
  326. gdbstub_io_tx_char(hex_asc_lo(checksum));
  327. } while (gdbstub_io_rx_char(&ch, 0),
  328. ch == '-' && (gdbstub_io("### GDB Rx NAK\n"), 0),
  329. ch != '-' && ch != '+' &&
  330. (gdbstub_io("### GDB Rx ??? %02x\n", ch), 0),
  331. ch != '+' && ch != '$');
  332. if (ch == '+') {
  333. gdbstub_io("### GDB Rx ACK\n");
  334. return 0;
  335. }
  336. gdbstub_io("### GDB Tx Abandoned\n");
  337. gdbstub_rx_unget = ch;
  338. return 1;
  339. }
  340. /*
  341. * While we find nice hex chars, build an int.
  342. * Return number of chars processed.
  343. */
  344. static int hexToInt(char **ptr, int *intValue)
  345. {
  346. int numChars = 0;
  347. int hexValue;
  348. *intValue = 0;
  349. while (**ptr) {
  350. hexValue = hex(**ptr);
  351. if (hexValue < 0)
  352. break;
  353. *intValue = (*intValue << 4) | hexValue;
  354. numChars++;
  355. (*ptr)++;
  356. }
  357. return (numChars);
  358. }
  359. #ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
  360. /*
  361. * We single-step by setting breakpoints. When an exception
  362. * is handled, we need to restore the instructions hoisted
  363. * when the breakpoints were set.
  364. *
  365. * This is where we save the original instructions.
  366. */
  367. static struct gdb_bp_save {
  368. u8 *addr;
  369. u8 opcode[2];
  370. } step_bp[2];
  371. static const unsigned char gdbstub_insn_sizes[256] =
  372. {
  373. /* 1 2 3 4 5 6 7 8 9 a b c d e f */
  374. 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, 1, 3, 3, 3, /* 0 */
  375. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 1 */
  376. 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, /* 2 */
  377. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, /* 3 */
  378. 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, /* 4 */
  379. 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, /* 5 */
  380. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6 */
  381. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 7 */
  382. 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 8 */
  383. 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* 9 */
  384. 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* a */
  385. 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, /* b */
  386. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, /* c */
  387. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* d */
  388. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* e */
  389. 0, 2, 2, 2, 2, 2, 2, 4, 0, 3, 0, 4, 0, 6, 7, 1 /* f */
  390. };
  391. static int __gdbstub_mark_bp(u8 *addr, int ix)
  392. {
  393. /* vmalloc area */
  394. if (((u8 *) VMALLOC_START <= addr) && (addr < (u8 *) VMALLOC_END))
  395. goto okay;
  396. /* SRAM, SDRAM */
  397. if (((u8 *) 0x80000000UL <= addr) && (addr < (u8 *) 0xa0000000UL))
  398. goto okay;
  399. return 0;
  400. okay:
  401. if (gdbstub_read_byte(addr + 0, &step_bp[ix].opcode[0]) < 0 ||
  402. gdbstub_read_byte(addr + 1, &step_bp[ix].opcode[1]) < 0)
  403. return 0;
  404. step_bp[ix].addr = addr;
  405. return 1;
  406. }
  407. static inline void __gdbstub_restore_bp(void)
  408. {
  409. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  410. if (step_bp[0].addr) {
  411. gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
  412. gdbstub_write_byte(step_bp[0].opcode[1], step_bp[0].addr + 1);
  413. }
  414. if (step_bp[1].addr) {
  415. gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
  416. gdbstub_write_byte(step_bp[1].opcode[1], step_bp[1].addr + 1);
  417. }
  418. #else
  419. if (step_bp[0].addr)
  420. gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
  421. if (step_bp[1].addr)
  422. gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
  423. #endif
  424. gdbstub_flush_caches = 1;
  425. step_bp[0].addr = NULL;
  426. step_bp[0].opcode[0] = 0;
  427. step_bp[0].opcode[1] = 0;
  428. step_bp[1].addr = NULL;
  429. step_bp[1].opcode[0] = 0;
  430. step_bp[1].opcode[1] = 0;
  431. }
  432. /*
  433. * emulate single stepping by means of breakpoint instructions
  434. */
  435. static int gdbstub_single_step(struct pt_regs *regs)
  436. {
  437. unsigned size;
  438. uint32_t x;
  439. uint8_t cur, *pc, *sp;
  440. step_bp[0].addr = NULL;
  441. step_bp[0].opcode[0] = 0;
  442. step_bp[0].opcode[1] = 0;
  443. step_bp[1].addr = NULL;
  444. step_bp[1].opcode[0] = 0;
  445. step_bp[1].opcode[1] = 0;
  446. x = 0;
  447. pc = (u8 *) regs->pc;
  448. sp = (u8 *) (regs + 1);
  449. if (gdbstub_read_byte(pc, &cur) < 0)
  450. return -EFAULT;
  451. gdbstub_bkpt("Single Step from %p { %02x }\n", pc, cur);
  452. gdbstub_flush_caches = 1;
  453. size = gdbstub_insn_sizes[cur];
  454. if (size > 0) {
  455. if (!__gdbstub_mark_bp(pc + size, 0))
  456. goto fault;
  457. } else {
  458. switch (cur) {
  459. /* Bxx (d8,PC) */
  460. case 0xc0 ... 0xca:
  461. if (gdbstub_read_byte(pc + 1, (u8 *) &x) < 0)
  462. goto fault;
  463. if (!__gdbstub_mark_bp(pc + 2, 0))
  464. goto fault;
  465. if ((x < 0 || x > 2) &&
  466. !__gdbstub_mark_bp(pc + (s8) x, 1))
  467. goto fault;
  468. break;
  469. /* LXX (d8,PC) */
  470. case 0xd0 ... 0xda:
  471. if (!__gdbstub_mark_bp(pc + 1, 0))
  472. goto fault;
  473. if (regs->pc != regs->lar &&
  474. !__gdbstub_mark_bp((u8 *) regs->lar, 1))
  475. goto fault;
  476. break;
  477. /* SETLB - loads the next for bytes into the LIR
  478. * register */
  479. case 0xdb:
  480. if (!__gdbstub_mark_bp(pc + 1, 0))
  481. goto fault;
  482. break;
  483. /* JMP (d16,PC) or CALL (d16,PC) */
  484. case 0xcc:
  485. case 0xcd:
  486. if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
  487. gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0)
  488. goto fault;
  489. if (!__gdbstub_mark_bp(pc + (s16) x, 0))
  490. goto fault;
  491. break;
  492. /* JMP (d32,PC) or CALL (d32,PC) */
  493. case 0xdc:
  494. case 0xdd:
  495. if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
  496. gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0 ||
  497. gdbstub_read_byte(pc + 3, ((u8 *) &x) + 2) < 0 ||
  498. gdbstub_read_byte(pc + 4, ((u8 *) &x) + 3) < 0)
  499. goto fault;
  500. if (!__gdbstub_mark_bp(pc + (s32) x, 0))
  501. goto fault;
  502. break;
  503. /* RETF */
  504. case 0xde:
  505. if (!__gdbstub_mark_bp((u8 *) regs->mdr, 0))
  506. goto fault;
  507. break;
  508. /* RET */
  509. case 0xdf:
  510. if (gdbstub_read_byte(pc + 2, (u8 *) &x) < 0)
  511. goto fault;
  512. sp += (s8)x;
  513. if (gdbstub_read_byte(sp + 0, ((u8 *) &x) + 0) < 0 ||
  514. gdbstub_read_byte(sp + 1, ((u8 *) &x) + 1) < 0 ||
  515. gdbstub_read_byte(sp + 2, ((u8 *) &x) + 2) < 0 ||
  516. gdbstub_read_byte(sp + 3, ((u8 *) &x) + 3) < 0)
  517. goto fault;
  518. if (!__gdbstub_mark_bp((u8 *) x, 0))
  519. goto fault;
  520. break;
  521. case 0xf0:
  522. if (gdbstub_read_byte(pc + 1, &cur) < 0)
  523. goto fault;
  524. if (cur >= 0xf0 && cur <= 0xf7) {
  525. /* JMP (An) / CALLS (An) */
  526. switch (cur & 3) {
  527. case 0: x = regs->a0; break;
  528. case 1: x = regs->a1; break;
  529. case 2: x = regs->a2; break;
  530. case 3: x = regs->a3; break;
  531. }
  532. if (!__gdbstub_mark_bp((u8 *) x, 0))
  533. goto fault;
  534. } else if (cur == 0xfc) {
  535. /* RETS */
  536. if (gdbstub_read_byte(
  537. sp + 0, ((u8 *) &x) + 0) < 0 ||
  538. gdbstub_read_byte(
  539. sp + 1, ((u8 *) &x) + 1) < 0 ||
  540. gdbstub_read_byte(
  541. sp + 2, ((u8 *) &x) + 2) < 0 ||
  542. gdbstub_read_byte(
  543. sp + 3, ((u8 *) &x) + 3) < 0)
  544. goto fault;
  545. if (!__gdbstub_mark_bp((u8 *) x, 0))
  546. goto fault;
  547. } else if (cur == 0xfd) {
  548. /* RTI */
  549. if (gdbstub_read_byte(
  550. sp + 4, ((u8 *) &x) + 0) < 0 ||
  551. gdbstub_read_byte(
  552. sp + 5, ((u8 *) &x) + 1) < 0 ||
  553. gdbstub_read_byte(
  554. sp + 6, ((u8 *) &x) + 2) < 0 ||
  555. gdbstub_read_byte(
  556. sp + 7, ((u8 *) &x) + 3) < 0)
  557. goto fault;
  558. if (!__gdbstub_mark_bp((u8 *) x, 0))
  559. goto fault;
  560. } else {
  561. if (!__gdbstub_mark_bp(pc + 2, 0))
  562. goto fault;
  563. }
  564. break;
  565. /* potential 3-byte conditional branches */
  566. case 0xf8:
  567. if (gdbstub_read_byte(pc + 1, &cur) < 0)
  568. goto fault;
  569. if (!__gdbstub_mark_bp(pc + 3, 0))
  570. goto fault;
  571. if (cur >= 0xe8 && cur <= 0xeb) {
  572. if (gdbstub_read_byte(
  573. pc + 2, ((u8 *) &x) + 0) < 0)
  574. goto fault;
  575. if ((x < 0 || x > 3) &&
  576. !__gdbstub_mark_bp(pc + (s8) x, 1))
  577. goto fault;
  578. }
  579. break;
  580. case 0xfa:
  581. if (gdbstub_read_byte(pc + 1, &cur) < 0)
  582. goto fault;
  583. if (cur == 0xff) {
  584. /* CALLS (d16,PC) */
  585. if (gdbstub_read_byte(
  586. pc + 2, ((u8 *) &x) + 0) < 0 ||
  587. gdbstub_read_byte(
  588. pc + 3, ((u8 *) &x) + 1) < 0)
  589. goto fault;
  590. if (!__gdbstub_mark_bp(pc + (s16) x, 0))
  591. goto fault;
  592. } else {
  593. if (!__gdbstub_mark_bp(pc + 4, 0))
  594. goto fault;
  595. }
  596. break;
  597. case 0xfc:
  598. if (gdbstub_read_byte(pc + 1, &cur) < 0)
  599. goto fault;
  600. if (cur == 0xff) {
  601. /* CALLS (d32,PC) */
  602. if (gdbstub_read_byte(
  603. pc + 2, ((u8 *) &x) + 0) < 0 ||
  604. gdbstub_read_byte(
  605. pc + 3, ((u8 *) &x) + 1) < 0 ||
  606. gdbstub_read_byte(
  607. pc + 4, ((u8 *) &x) + 2) < 0 ||
  608. gdbstub_read_byte(
  609. pc + 5, ((u8 *) &x) + 3) < 0)
  610. goto fault;
  611. if (!__gdbstub_mark_bp(
  612. pc + (s32) x, 0))
  613. goto fault;
  614. } else {
  615. if (!__gdbstub_mark_bp(
  616. pc + 6, 0))
  617. goto fault;
  618. }
  619. break;
  620. }
  621. }
  622. gdbstub_bkpt("Step: %02x at %p; %02x at %p\n",
  623. step_bp[0].opcode[0], step_bp[0].addr,
  624. step_bp[1].opcode[0], step_bp[1].addr);
  625. if (step_bp[0].addr) {
  626. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  627. if (gdbstub_write_byte(0xF7, step_bp[0].addr + 0) < 0 ||
  628. gdbstub_write_byte(0xF7, step_bp[0].addr + 1) < 0)
  629. goto fault;
  630. #else
  631. if (gdbstub_write_byte(0xFF, step_bp[0].addr + 0) < 0)
  632. goto fault;
  633. #endif
  634. }
  635. if (step_bp[1].addr) {
  636. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  637. if (gdbstub_write_byte(0xF7, step_bp[1].addr + 0) < 0 ||
  638. gdbstub_write_byte(0xF7, step_bp[1].addr + 1) < 0)
  639. goto fault;
  640. #else
  641. if (gdbstub_write_byte(0xFF, step_bp[1].addr + 0) < 0)
  642. goto fault;
  643. #endif
  644. }
  645. return 0;
  646. fault:
  647. /* uh-oh - silly address alert, try and restore things */
  648. __gdbstub_restore_bp();
  649. return -EFAULT;
  650. }
  651. #endif /* CONFIG_GDBSTUB_ALLOW_SINGLE_STEP */
  652. #ifdef CONFIG_GDBSTUB_CONSOLE
  653. void gdbstub_console_write(struct console *con, const char *p, unsigned n)
  654. {
  655. static const char gdbstub_cr[] = { 0x0d };
  656. char outbuf[26];
  657. int qty;
  658. u8 busy;
  659. busy = gdbstub_busy;
  660. gdbstub_busy = 1;
  661. outbuf[0] = 'O';
  662. while (n > 0) {
  663. qty = 1;
  664. while (n > 0 && qty < 20) {
  665. mem2hex(p, outbuf + qty, 2, 0);
  666. qty += 2;
  667. if (*p == 0x0a) {
  668. mem2hex(gdbstub_cr, outbuf + qty, 2, 0);
  669. qty += 2;
  670. }
  671. p++;
  672. n--;
  673. }
  674. outbuf[qty] = 0;
  675. putpacket(outbuf);
  676. }
  677. gdbstub_busy = busy;
  678. }
  679. static kdev_t gdbstub_console_dev(struct console *con)
  680. {
  681. return MKDEV(1, 3); /* /dev/null */
  682. }
  683. static struct console gdbstub_console = {
  684. .name = "gdb",
  685. .write = gdbstub_console_write,
  686. .device = gdbstub_console_dev,
  687. .flags = CON_PRINTBUFFER,
  688. .index = -1,
  689. };
  690. #endif
  691. /*
  692. * Convert the memory pointed to by mem into hex, placing result in buf.
  693. * - if successful, return a pointer to the last char put in buf (NUL)
  694. * - in case of mem fault, return NULL
  695. * may_fault is non-zero if we are reading from arbitrary memory, but is
  696. * currently not used.
  697. */
  698. static
  699. unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
  700. {
  701. const u8 *mem = _mem;
  702. u8 ch[4];
  703. if ((u32) mem & 1 && count >= 1) {
  704. if (gdbstub_read_byte(mem, ch) != 0)
  705. return 0;
  706. buf = pack_hex_byte(buf, ch[0]);
  707. mem++;
  708. count--;
  709. }
  710. if ((u32) mem & 3 && count >= 2) {
  711. if (gdbstub_read_word(mem, ch) != 0)
  712. return 0;
  713. buf = pack_hex_byte(buf, ch[0]);
  714. buf = pack_hex_byte(buf, ch[1]);
  715. mem += 2;
  716. count -= 2;
  717. }
  718. while (count >= 4) {
  719. if (gdbstub_read_dword(mem, ch) != 0)
  720. return 0;
  721. buf = pack_hex_byte(buf, ch[0]);
  722. buf = pack_hex_byte(buf, ch[1]);
  723. buf = pack_hex_byte(buf, ch[2]);
  724. buf = pack_hex_byte(buf, ch[3]);
  725. mem += 4;
  726. count -= 4;
  727. }
  728. if (count >= 2) {
  729. if (gdbstub_read_word(mem, ch) != 0)
  730. return 0;
  731. buf = pack_hex_byte(buf, ch[0]);
  732. buf = pack_hex_byte(buf, ch[1]);
  733. mem += 2;
  734. count -= 2;
  735. }
  736. if (count >= 1) {
  737. if (gdbstub_read_byte(mem, ch) != 0)
  738. return 0;
  739. buf = pack_hex_byte(buf, ch[0]);
  740. }
  741. *buf = 0;
  742. return buf;
  743. }
  744. /*
  745. * convert the hex array pointed to by buf into binary to be placed in mem
  746. * return a pointer to the character AFTER the last byte written
  747. * may_fault is non-zero if we are reading from arbitrary memory, but is
  748. * currently not used.
  749. */
  750. static
  751. const char *hex2mem(const char *buf, void *_mem, int count, int may_fault)
  752. {
  753. u8 *mem = _mem;
  754. union {
  755. u32 val;
  756. u8 b[4];
  757. } ch;
  758. if ((u32) mem & 1 && count >= 1) {
  759. ch.b[0] = hex(*buf++) << 4;
  760. ch.b[0] |= hex(*buf++);
  761. if (gdbstub_write_byte(ch.val, mem) != 0)
  762. return 0;
  763. mem++;
  764. count--;
  765. }
  766. if ((u32) mem & 3 && count >= 2) {
  767. ch.b[0] = hex(*buf++) << 4;
  768. ch.b[0] |= hex(*buf++);
  769. ch.b[1] = hex(*buf++) << 4;
  770. ch.b[1] |= hex(*buf++);
  771. if (gdbstub_write_word(ch.val, mem) != 0)
  772. return 0;
  773. mem += 2;
  774. count -= 2;
  775. }
  776. while (count >= 4) {
  777. ch.b[0] = hex(*buf++) << 4;
  778. ch.b[0] |= hex(*buf++);
  779. ch.b[1] = hex(*buf++) << 4;
  780. ch.b[1] |= hex(*buf++);
  781. ch.b[2] = hex(*buf++) << 4;
  782. ch.b[2] |= hex(*buf++);
  783. ch.b[3] = hex(*buf++) << 4;
  784. ch.b[3] |= hex(*buf++);
  785. if (gdbstub_write_dword(ch.val, mem) != 0)
  786. return 0;
  787. mem += 4;
  788. count -= 4;
  789. }
  790. if (count >= 2) {
  791. ch.b[0] = hex(*buf++) << 4;
  792. ch.b[0] |= hex(*buf++);
  793. ch.b[1] = hex(*buf++) << 4;
  794. ch.b[1] |= hex(*buf++);
  795. if (gdbstub_write_word(ch.val, mem) != 0)
  796. return 0;
  797. mem += 2;
  798. count -= 2;
  799. }
  800. if (count >= 1) {
  801. ch.b[0] = hex(*buf++) << 4;
  802. ch.b[0] |= hex(*buf++);
  803. if (gdbstub_write_byte(ch.val, mem) != 0)
  804. return 0;
  805. }
  806. return buf;
  807. }
  808. /*
  809. * This table contains the mapping between MN10300 exception codes, and
  810. * signals, which are primarily what GDB understands. It also indicates
  811. * which hardware traps we need to commandeer when initializing the stub.
  812. */
  813. static const struct excep_to_sig_map {
  814. enum exception_code excep; /* MN10300 exception code */
  815. unsigned char signo; /* Signal that we map this into */
  816. } excep_to_sig_map[] = {
  817. { EXCEP_ITLBMISS, SIGSEGV },
  818. { EXCEP_DTLBMISS, SIGSEGV },
  819. { EXCEP_TRAP, SIGTRAP },
  820. { EXCEP_ISTEP, SIGTRAP },
  821. { EXCEP_IBREAK, SIGTRAP },
  822. { EXCEP_OBREAK, SIGTRAP },
  823. { EXCEP_UNIMPINS, SIGILL },
  824. { EXCEP_UNIMPEXINS, SIGILL },
  825. { EXCEP_MEMERR, SIGSEGV },
  826. { EXCEP_MISALIGN, SIGSEGV },
  827. { EXCEP_BUSERROR, SIGBUS },
  828. { EXCEP_ILLINSACC, SIGSEGV },
  829. { EXCEP_ILLDATACC, SIGSEGV },
  830. { EXCEP_IOINSACC, SIGSEGV },
  831. { EXCEP_PRIVINSACC, SIGSEGV },
  832. { EXCEP_PRIVDATACC, SIGSEGV },
  833. { EXCEP_FPU_DISABLED, SIGFPE },
  834. { EXCEP_FPU_UNIMPINS, SIGFPE },
  835. { EXCEP_FPU_OPERATION, SIGFPE },
  836. { EXCEP_WDT, SIGALRM },
  837. { EXCEP_NMI, SIGQUIT },
  838. { EXCEP_IRQ_LEVEL0, SIGINT },
  839. { EXCEP_IRQ_LEVEL1, SIGINT },
  840. { EXCEP_IRQ_LEVEL2, SIGINT },
  841. { EXCEP_IRQ_LEVEL3, SIGINT },
  842. { EXCEP_IRQ_LEVEL4, SIGINT },
  843. { EXCEP_IRQ_LEVEL5, SIGINT },
  844. { EXCEP_IRQ_LEVEL6, SIGINT },
  845. { 0, 0}
  846. };
  847. /*
  848. * convert the MN10300 exception code into a UNIX signal number
  849. */
  850. static int computeSignal(enum exception_code excep)
  851. {
  852. const struct excep_to_sig_map *map;
  853. for (map = excep_to_sig_map; map->signo; map++)
  854. if (map->excep == excep)
  855. return map->signo;
  856. return SIGHUP; /* default for things we don't know about */
  857. }
  858. static u32 gdbstub_fpcr, gdbstub_fpufs_array[32];
  859. /*
  860. *
  861. */
  862. static void gdbstub_store_fpu(void)
  863. {
  864. #ifdef CONFIG_FPU
  865. asm volatile(
  866. "or %2,epsw\n"
  867. #ifdef CONFIG_MN10300_PROC_MN103E010
  868. "nop\n"
  869. "nop\n"
  870. #endif
  871. "mov %1, a1\n"
  872. "fmov fs0, (a1+)\n"
  873. "fmov fs1, (a1+)\n"
  874. "fmov fs2, (a1+)\n"
  875. "fmov fs3, (a1+)\n"
  876. "fmov fs4, (a1+)\n"
  877. "fmov fs5, (a1+)\n"
  878. "fmov fs6, (a1+)\n"
  879. "fmov fs7, (a1+)\n"
  880. "fmov fs8, (a1+)\n"
  881. "fmov fs9, (a1+)\n"
  882. "fmov fs10, (a1+)\n"
  883. "fmov fs11, (a1+)\n"
  884. "fmov fs12, (a1+)\n"
  885. "fmov fs13, (a1+)\n"
  886. "fmov fs14, (a1+)\n"
  887. "fmov fs15, (a1+)\n"
  888. "fmov fs16, (a1+)\n"
  889. "fmov fs17, (a1+)\n"
  890. "fmov fs18, (a1+)\n"
  891. "fmov fs19, (a1+)\n"
  892. "fmov fs20, (a1+)\n"
  893. "fmov fs21, (a1+)\n"
  894. "fmov fs22, (a1+)\n"
  895. "fmov fs23, (a1+)\n"
  896. "fmov fs24, (a1+)\n"
  897. "fmov fs25, (a1+)\n"
  898. "fmov fs26, (a1+)\n"
  899. "fmov fs27, (a1+)\n"
  900. "fmov fs28, (a1+)\n"
  901. "fmov fs29, (a1+)\n"
  902. "fmov fs30, (a1+)\n"
  903. "fmov fs31, (a1+)\n"
  904. "fmov fpcr, %0\n"
  905. : "=d"(gdbstub_fpcr)
  906. : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE)
  907. : "a1"
  908. );
  909. #endif
  910. }
  911. /*
  912. *
  913. */
  914. static void gdbstub_load_fpu(void)
  915. {
  916. #ifdef CONFIG_FPU
  917. asm volatile(
  918. "or %1,epsw\n"
  919. #ifdef CONFIG_MN10300_PROC_MN103E010
  920. "nop\n"
  921. "nop\n"
  922. #endif
  923. "mov %0, a1\n"
  924. "fmov (a1+), fs0\n"
  925. "fmov (a1+), fs1\n"
  926. "fmov (a1+), fs2\n"
  927. "fmov (a1+), fs3\n"
  928. "fmov (a1+), fs4\n"
  929. "fmov (a1+), fs5\n"
  930. "fmov (a1+), fs6\n"
  931. "fmov (a1+), fs7\n"
  932. "fmov (a1+), fs8\n"
  933. "fmov (a1+), fs9\n"
  934. "fmov (a1+), fs10\n"
  935. "fmov (a1+), fs11\n"
  936. "fmov (a1+), fs12\n"
  937. "fmov (a1+), fs13\n"
  938. "fmov (a1+), fs14\n"
  939. "fmov (a1+), fs15\n"
  940. "fmov (a1+), fs16\n"
  941. "fmov (a1+), fs17\n"
  942. "fmov (a1+), fs18\n"
  943. "fmov (a1+), fs19\n"
  944. "fmov (a1+), fs20\n"
  945. "fmov (a1+), fs21\n"
  946. "fmov (a1+), fs22\n"
  947. "fmov (a1+), fs23\n"
  948. "fmov (a1+), fs24\n"
  949. "fmov (a1+), fs25\n"
  950. "fmov (a1+), fs26\n"
  951. "fmov (a1+), fs27\n"
  952. "fmov (a1+), fs28\n"
  953. "fmov (a1+), fs29\n"
  954. "fmov (a1+), fs30\n"
  955. "fmov (a1+), fs31\n"
  956. "fmov %2, fpcr\n"
  957. :
  958. : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE), "d"(gdbstub_fpcr)
  959. : "a1"
  960. );
  961. #endif
  962. }
  963. /*
  964. * set a software breakpoint
  965. */
  966. int gdbstub_set_breakpoint(u8 *addr, int len)
  967. {
  968. int bkpt, loop, xloop;
  969. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  970. len = (len + 1) & ~1;
  971. #endif
  972. gdbstub_bkpt("setbkpt(%p,%d)\n", addr, len);
  973. for (bkpt = 255; bkpt >= 0; bkpt--)
  974. if (!gdbstub_bkpts[bkpt].addr)
  975. break;
  976. if (bkpt < 0)
  977. return -ENOSPC;
  978. for (loop = 0; loop < len; loop++)
  979. if (gdbstub_read_byte(&addr[loop],
  980. &gdbstub_bkpts[bkpt].origbytes[loop]
  981. ) < 0)
  982. return -EFAULT;
  983. gdbstub_flush_caches = 1;
  984. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  985. for (loop = 0; loop < len; loop++)
  986. if (gdbstub_write_byte(0xF7, &addr[loop]) < 0)
  987. goto restore;
  988. #else
  989. for (loop = 0; loop < len; loop++)
  990. if (gdbstub_write_byte(0xFF, &addr[loop]) < 0)
  991. goto restore;
  992. #endif
  993. gdbstub_bkpts[bkpt].addr = addr;
  994. gdbstub_bkpts[bkpt].len = len;
  995. gdbstub_bkpt("Set BKPT[%02x]: %p-%p {%02x%02x%02x%02x%02x%02x%02x}\n",
  996. bkpt,
  997. gdbstub_bkpts[bkpt].addr,
  998. gdbstub_bkpts[bkpt].addr + gdbstub_bkpts[bkpt].len - 1,
  999. gdbstub_bkpts[bkpt].origbytes[0],
  1000. gdbstub_bkpts[bkpt].origbytes[1],
  1001. gdbstub_bkpts[bkpt].origbytes[2],
  1002. gdbstub_bkpts[bkpt].origbytes[3],
  1003. gdbstub_bkpts[bkpt].origbytes[4],
  1004. gdbstub_bkpts[bkpt].origbytes[5],
  1005. gdbstub_bkpts[bkpt].origbytes[6]
  1006. );
  1007. return 0;
  1008. restore:
  1009. for (xloop = 0; xloop < loop; xloop++)
  1010. gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[xloop],
  1011. addr + xloop);
  1012. return -EFAULT;
  1013. }
  1014. /*
  1015. * clear a software breakpoint
  1016. */
  1017. int gdbstub_clear_breakpoint(u8 *addr, int len)
  1018. {
  1019. int bkpt, loop;
  1020. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  1021. len = (len + 1) & ~1;
  1022. #endif
  1023. gdbstub_bkpt("clearbkpt(%p,%d)\n", addr, len);
  1024. for (bkpt = 255; bkpt >= 0; bkpt--)
  1025. if (gdbstub_bkpts[bkpt].addr == addr &&
  1026. gdbstub_bkpts[bkpt].len == len)
  1027. break;
  1028. if (bkpt < 0)
  1029. return -ENOENT;
  1030. gdbstub_bkpts[bkpt].addr = NULL;
  1031. gdbstub_flush_caches = 1;
  1032. for (loop = 0; loop < len; loop++)
  1033. if (gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[loop],
  1034. addr + loop) < 0)
  1035. return -EFAULT;
  1036. return 0;
  1037. }
  1038. /*
  1039. * This function does all command processing for interfacing to gdb
  1040. * - returns 0 if the exception should be skipped, -ERROR otherwise.
  1041. */
  1042. static int gdbstub(struct pt_regs *regs, enum exception_code excep)
  1043. {
  1044. unsigned long *stack;
  1045. unsigned long epsw, mdr;
  1046. uint32_t zero, ssp;
  1047. uint8_t broke;
  1048. char *ptr;
  1049. int sigval;
  1050. int addr;
  1051. int length;
  1052. int loop;
  1053. if (excep == EXCEP_FPU_DISABLED)
  1054. return -ENOTSUPP;
  1055. gdbstub_flush_caches = 0;
  1056. mn10300_set_gdbleds(1);
  1057. asm volatile("mov mdr,%0" : "=d"(mdr));
  1058. local_save_flags(epsw);
  1059. arch_local_change_intr_mask_level(
  1060. NUM2EPSW_IM(CONFIG_DEBUGGER_IRQ_LEVEL + 1));
  1061. gdbstub_store_fpu();
  1062. #ifdef CONFIG_GDBSTUB_IMMEDIATE
  1063. /* skip the initial pause loop */
  1064. if (regs->pc == (unsigned long) __gdbstub_pause)
  1065. regs->pc = (unsigned long) start_kernel;
  1066. #endif
  1067. /* if we were single stepping, restore the opcodes hoisted for the
  1068. * breakpoint[s] */
  1069. broke = 0;
  1070. #ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
  1071. if ((step_bp[0].addr && step_bp[0].addr == (u8 *) regs->pc) ||
  1072. (step_bp[1].addr && step_bp[1].addr == (u8 *) regs->pc))
  1073. broke = 1;
  1074. __gdbstub_restore_bp();
  1075. #endif
  1076. if (gdbstub_rx_unget) {
  1077. sigval = SIGINT;
  1078. if (gdbstub_rx_unget != 3)
  1079. goto packet_waiting;
  1080. gdbstub_rx_unget = 0;
  1081. }
  1082. stack = (unsigned long *) regs->sp;
  1083. sigval = broke ? SIGTRAP : computeSignal(excep);
  1084. /* send information about a BUG() */
  1085. if (!user_mode(regs) && excep == EXCEP_SYSCALL15) {
  1086. const struct bug_entry *bug;
  1087. bug = find_bug(regs->pc);
  1088. if (bug)
  1089. goto found_bug;
  1090. length = snprintf(trans_buffer, sizeof(trans_buffer),
  1091. "BUG() at address %lx\n", regs->pc);
  1092. goto send_bug_pkt;
  1093. found_bug:
  1094. length = snprintf(trans_buffer, sizeof(trans_buffer),
  1095. "BUG() at address %lx (%s:%d)\n",
  1096. regs->pc, bug->file, bug->line);
  1097. send_bug_pkt:
  1098. ptr = output_buffer;
  1099. *ptr++ = 'O';
  1100. ptr = mem2hex(trans_buffer, ptr, length, 0);
  1101. *ptr = 0;
  1102. putpacket(output_buffer);
  1103. regs->pc -= 2;
  1104. sigval = SIGABRT;
  1105. } else if (regs->pc == (unsigned long) __gdbstub_bug_trap) {
  1106. regs->pc = regs->mdr;
  1107. sigval = SIGABRT;
  1108. }
  1109. /*
  1110. * send a message to the debugger's user saying what happened if it may
  1111. * not be clear cut (we can't map exceptions onto signals properly)
  1112. */
  1113. if (sigval != SIGINT && sigval != SIGTRAP && sigval != SIGILL) {
  1114. static const char title[] = "Excep ", tbcberr[] = "BCBERR ";
  1115. static const char crlf[] = "\r\n";
  1116. char hx;
  1117. u32 bcberr = BCBERR;
  1118. ptr = output_buffer;
  1119. *ptr++ = 'O';
  1120. ptr = mem2hex(title, ptr, sizeof(title) - 1, 0);
  1121. hx = hex_asc_hi(excep >> 8);
  1122. ptr = pack_hex_byte(ptr, hx);
  1123. hx = hex_asc_lo(excep >> 8);
  1124. ptr = pack_hex_byte(ptr, hx);
  1125. hx = hex_asc_hi(excep);
  1126. ptr = pack_hex_byte(ptr, hx);
  1127. hx = hex_asc_lo(excep);
  1128. ptr = pack_hex_byte(ptr, hx);
  1129. ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
  1130. *ptr = 0;
  1131. putpacket(output_buffer); /* send it off... */
  1132. /* BCBERR */
  1133. ptr = output_buffer;
  1134. *ptr++ = 'O';
  1135. ptr = mem2hex(tbcberr, ptr, sizeof(tbcberr) - 1, 0);
  1136. hx = hex_asc_hi(bcberr >> 24);
  1137. ptr = pack_hex_byte(ptr, hx);
  1138. hx = hex_asc_lo(bcberr >> 24);
  1139. ptr = pack_hex_byte(ptr, hx);
  1140. hx = hex_asc_hi(bcberr >> 16);
  1141. ptr = pack_hex_byte(ptr, hx);
  1142. hx = hex_asc_lo(bcberr >> 16);
  1143. ptr = pack_hex_byte(ptr, hx);
  1144. hx = hex_asc_hi(bcberr >> 8);
  1145. ptr = pack_hex_byte(ptr, hx);
  1146. hx = hex_asc_lo(bcberr >> 8);
  1147. ptr = pack_hex_byte(ptr, hx);
  1148. hx = hex_asc_hi(bcberr);
  1149. ptr = pack_hex_byte(ptr, hx);
  1150. hx = hex_asc_lo(bcberr);
  1151. ptr = pack_hex_byte(ptr, hx);
  1152. ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
  1153. *ptr = 0;
  1154. putpacket(output_buffer); /* send it off... */
  1155. }
  1156. /*
  1157. * tell the debugger that an exception has occurred
  1158. */
  1159. ptr = output_buffer;
  1160. /*
  1161. * Send trap type (converted to signal)
  1162. */
  1163. *ptr++ = 'T';
  1164. ptr = pack_hex_byte(ptr, sigval);
  1165. /*
  1166. * Send Error PC
  1167. */
  1168. ptr = pack_hex_byte(ptr, GDB_REGID_PC);
  1169. *ptr++ = ':';
  1170. ptr = mem2hex(&regs->pc, ptr, 4, 0);
  1171. *ptr++ = ';';
  1172. /*
  1173. * Send frame pointer
  1174. */
  1175. ptr = pack_hex_byte(ptr, GDB_REGID_FP);
  1176. *ptr++ = ':';
  1177. ptr = mem2hex(&regs->a3, ptr, 4, 0);
  1178. *ptr++ = ';';
  1179. /*
  1180. * Send stack pointer
  1181. */
  1182. ssp = (unsigned long) (regs + 1);
  1183. ptr = pack_hex_byte(ptr, GDB_REGID_SP);
  1184. *ptr++ = ':';
  1185. ptr = mem2hex(&ssp, ptr, 4, 0);
  1186. *ptr++ = ';';
  1187. *ptr++ = 0;
  1188. putpacket(output_buffer); /* send it off... */
  1189. packet_waiting:
  1190. /*
  1191. * Wait for input from remote GDB
  1192. */
  1193. while (1) {
  1194. output_buffer[0] = 0;
  1195. getpacket(input_buffer);
  1196. switch (input_buffer[0]) {
  1197. /* request repeat of last signal number */
  1198. case '?':
  1199. output_buffer[0] = 'S';
  1200. output_buffer[1] = hex_asc_hi(sigval);
  1201. output_buffer[2] = hex_asc_lo(sigval);
  1202. output_buffer[3] = 0;
  1203. break;
  1204. case 'd':
  1205. /* toggle debug flag */
  1206. break;
  1207. /*
  1208. * Return the value of the CPU registers
  1209. */
  1210. case 'g':
  1211. zero = 0;
  1212. ssp = (u32) (regs + 1);
  1213. ptr = output_buffer;
  1214. ptr = mem2hex(&regs->d0, ptr, 4, 0);
  1215. ptr = mem2hex(&regs->d1, ptr, 4, 0);
  1216. ptr = mem2hex(&regs->d2, ptr, 4, 0);
  1217. ptr = mem2hex(&regs->d3, ptr, 4, 0);
  1218. ptr = mem2hex(&regs->a0, ptr, 4, 0);
  1219. ptr = mem2hex(&regs->a1, ptr, 4, 0);
  1220. ptr = mem2hex(&regs->a2, ptr, 4, 0);
  1221. ptr = mem2hex(&regs->a3, ptr, 4, 0);
  1222. ptr = mem2hex(&ssp, ptr, 4, 0); /* 8 */
  1223. ptr = mem2hex(&regs->pc, ptr, 4, 0);
  1224. ptr = mem2hex(&regs->mdr, ptr, 4, 0);
  1225. ptr = mem2hex(&regs->epsw, ptr, 4, 0);
  1226. ptr = mem2hex(&regs->lir, ptr, 4, 0);
  1227. ptr = mem2hex(&regs->lar, ptr, 4, 0);
  1228. ptr = mem2hex(&regs->mdrq, ptr, 4, 0);
  1229. ptr = mem2hex(&regs->e0, ptr, 4, 0); /* 15 */
  1230. ptr = mem2hex(&regs->e1, ptr, 4, 0);
  1231. ptr = mem2hex(&regs->e2, ptr, 4, 0);
  1232. ptr = mem2hex(&regs->e3, ptr, 4, 0);
  1233. ptr = mem2hex(&regs->e4, ptr, 4, 0);
  1234. ptr = mem2hex(&regs->e5, ptr, 4, 0);
  1235. ptr = mem2hex(&regs->e6, ptr, 4, 0);
  1236. ptr = mem2hex(&regs->e7, ptr, 4, 0);
  1237. ptr = mem2hex(&ssp, ptr, 4, 0);
  1238. ptr = mem2hex(&regs, ptr, 4, 0);
  1239. ptr = mem2hex(&regs->sp, ptr, 4, 0);
  1240. ptr = mem2hex(&regs->mcrh, ptr, 4, 0); /* 26 */
  1241. ptr = mem2hex(&regs->mcrl, ptr, 4, 0);
  1242. ptr = mem2hex(&regs->mcvf, ptr, 4, 0);
  1243. ptr = mem2hex(&gdbstub_fpcr, ptr, 4, 0); /* 29 - FPCR */
  1244. ptr = mem2hex(&zero, ptr, 4, 0);
  1245. ptr = mem2hex(&zero, ptr, 4, 0);
  1246. for (loop = 0; loop < 32; loop++)
  1247. ptr = mem2hex(&gdbstub_fpufs_array[loop],
  1248. ptr, 4, 0); /* 32 - FS0-31 */
  1249. break;
  1250. /*
  1251. * set the value of the CPU registers - return OK
  1252. */
  1253. case 'G':
  1254. {
  1255. const char *ptr;
  1256. ptr = &input_buffer[1];
  1257. ptr = hex2mem(ptr, &regs->d0, 4, 0);
  1258. ptr = hex2mem(ptr, &regs->d1, 4, 0);
  1259. ptr = hex2mem(ptr, &regs->d2, 4, 0);
  1260. ptr = hex2mem(ptr, &regs->d3, 4, 0);
  1261. ptr = hex2mem(ptr, &regs->a0, 4, 0);
  1262. ptr = hex2mem(ptr, &regs->a1, 4, 0);
  1263. ptr = hex2mem(ptr, &regs->a2, 4, 0);
  1264. ptr = hex2mem(ptr, &regs->a3, 4, 0);
  1265. ptr = hex2mem(ptr, &ssp, 4, 0); /* 8 */
  1266. ptr = hex2mem(ptr, &regs->pc, 4, 0);
  1267. ptr = hex2mem(ptr, &regs->mdr, 4, 0);
  1268. ptr = hex2mem(ptr, &regs->epsw, 4, 0);
  1269. ptr = hex2mem(ptr, &regs->lir, 4, 0);
  1270. ptr = hex2mem(ptr, &regs->lar, 4, 0);
  1271. ptr = hex2mem(ptr, &regs->mdrq, 4, 0);
  1272. ptr = hex2mem(ptr, &regs->e0, 4, 0); /* 15 */
  1273. ptr = hex2mem(ptr, &regs->e1, 4, 0);
  1274. ptr = hex2mem(ptr, &regs->e2, 4, 0);
  1275. ptr = hex2mem(ptr, &regs->e3, 4, 0);
  1276. ptr = hex2mem(ptr, &regs->e4, 4, 0);
  1277. ptr = hex2mem(ptr, &regs->e5, 4, 0);
  1278. ptr = hex2mem(ptr, &regs->e6, 4, 0);
  1279. ptr = hex2mem(ptr, &regs->e7, 4, 0);
  1280. ptr = hex2mem(ptr, &ssp, 4, 0);
  1281. ptr = hex2mem(ptr, &zero, 4, 0);
  1282. ptr = hex2mem(ptr, &regs->sp, 4, 0);
  1283. ptr = hex2mem(ptr, &regs->mcrh, 4, 0); /* 26 */
  1284. ptr = hex2mem(ptr, &regs->mcrl, 4, 0);
  1285. ptr = hex2mem(ptr, &regs->mcvf, 4, 0);
  1286. ptr = hex2mem(ptr, &zero, 4, 0); /* 29 - FPCR */
  1287. ptr = hex2mem(ptr, &zero, 4, 0);
  1288. ptr = hex2mem(ptr, &zero, 4, 0);
  1289. for (loop = 0; loop < 32; loop++) /* 32 - FS0-31 */
  1290. ptr = hex2mem(ptr, &zero, 4, 0);
  1291. #if 0
  1292. /*
  1293. * See if the stack pointer has moved. If so, then copy
  1294. * the saved locals and ins to the new location.
  1295. */
  1296. unsigned long *newsp = (unsigned long *) registers[SP];
  1297. if (sp != newsp)
  1298. sp = memcpy(newsp, sp, 16 * 4);
  1299. #endif
  1300. gdbstub_strcpy(output_buffer, "OK");
  1301. }
  1302. break;
  1303. /*
  1304. * mAA..AA,LLLL Read LLLL bytes at address AA..AA
  1305. */
  1306. case 'm':
  1307. ptr = &input_buffer[1];
  1308. if (hexToInt(&ptr, &addr) &&
  1309. *ptr++ == ',' &&
  1310. hexToInt(&ptr, &length)
  1311. ) {
  1312. if (mem2hex((char *) addr, output_buffer,
  1313. length, 1))
  1314. break;
  1315. gdbstub_strcpy(output_buffer, "E03");
  1316. } else {
  1317. gdbstub_strcpy(output_buffer, "E01");
  1318. }
  1319. break;
  1320. /*
  1321. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA
  1322. * return OK
  1323. */
  1324. case 'M':
  1325. ptr = &input_buffer[1];
  1326. if (hexToInt(&ptr, &addr) &&
  1327. *ptr++ == ',' &&
  1328. hexToInt(&ptr, &length) &&
  1329. *ptr++ == ':'
  1330. ) {
  1331. if (hex2mem(ptr, (char *) addr, length, 1))
  1332. gdbstub_strcpy(output_buffer, "OK");
  1333. else
  1334. gdbstub_strcpy(output_buffer, "E03");
  1335. gdbstub_flush_caches = 1;
  1336. } else {
  1337. gdbstub_strcpy(output_buffer, "E02");
  1338. }
  1339. break;
  1340. /*
  1341. * cAA..AA Continue at address AA..AA(optional)
  1342. */
  1343. case 'c':
  1344. /* try to read optional parameter, pc unchanged if no
  1345. * parm */
  1346. ptr = &input_buffer[1];
  1347. if (hexToInt(&ptr, &addr))
  1348. regs->pc = addr;
  1349. goto done;
  1350. /*
  1351. * kill the program
  1352. */
  1353. case 'k' :
  1354. goto done; /* just continue */
  1355. /*
  1356. * Reset the whole machine (FIXME: system dependent)
  1357. */
  1358. case 'r':
  1359. break;
  1360. /*
  1361. * Step to next instruction
  1362. */
  1363. case 's':
  1364. /* Using the T flag doesn't seem to perform single
  1365. * stepping (it seems to wind up being caught by the
  1366. * JTAG unit), so we have to use breakpoints and
  1367. * continue instead.
  1368. */
  1369. #ifdef CONFIG_GDBSTUB_ALLOW_SINGLE_STEP
  1370. if (gdbstub_single_step(regs) < 0)
  1371. /* ignore any fault error for now */
  1372. gdbstub_printk("unable to set single-step"
  1373. " bp\n");
  1374. goto done;
  1375. #else
  1376. gdbstub_strcpy(output_buffer, "E01");
  1377. break;
  1378. #endif
  1379. /*
  1380. * Set baud rate (bBB)
  1381. */
  1382. case 'b':
  1383. do {
  1384. int baudrate;
  1385. ptr = &input_buffer[1];
  1386. if (!hexToInt(&ptr, &baudrate)) {
  1387. gdbstub_strcpy(output_buffer, "B01");
  1388. break;
  1389. }
  1390. if (baudrate) {
  1391. /* ACK before changing speed */
  1392. putpacket("OK");
  1393. gdbstub_io_set_baud(baudrate);
  1394. }
  1395. } while (0);
  1396. break;
  1397. /*
  1398. * Set breakpoint
  1399. */
  1400. case 'Z':
  1401. ptr = &input_buffer[1];
  1402. if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
  1403. !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
  1404. !hexToInt(&ptr, &length)
  1405. ) {
  1406. gdbstub_strcpy(output_buffer, "E01");
  1407. break;
  1408. }
  1409. /* only support software breakpoints */
  1410. gdbstub_strcpy(output_buffer, "E03");
  1411. if (loop != 0 ||
  1412. length < 1 ||
  1413. length > 7 ||
  1414. (unsigned long) addr < 4096)
  1415. break;
  1416. if (gdbstub_set_breakpoint((u8 *) addr, length) < 0)
  1417. break;
  1418. gdbstub_strcpy(output_buffer, "OK");
  1419. break;
  1420. /*
  1421. * Clear breakpoint
  1422. */
  1423. case 'z':
  1424. ptr = &input_buffer[1];
  1425. if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
  1426. !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
  1427. !hexToInt(&ptr, &length)
  1428. ) {
  1429. gdbstub_strcpy(output_buffer, "E01");
  1430. break;
  1431. }
  1432. /* only support software breakpoints */
  1433. gdbstub_strcpy(output_buffer, "E03");
  1434. if (loop != 0 ||
  1435. length < 1 ||
  1436. length > 7 ||
  1437. (unsigned long) addr < 4096)
  1438. break;
  1439. if (gdbstub_clear_breakpoint((u8 *) addr, length) < 0)
  1440. break;
  1441. gdbstub_strcpy(output_buffer, "OK");
  1442. break;
  1443. default:
  1444. gdbstub_proto("### GDB Unsupported Cmd '%s'\n",
  1445. input_buffer);
  1446. break;
  1447. }
  1448. /* reply to the request */
  1449. putpacket(output_buffer);
  1450. }
  1451. done:
  1452. /*
  1453. * Need to flush the instruction cache here, as we may
  1454. * have deposited a breakpoint, and the icache probably
  1455. * has no way of knowing that a data ref to some location
  1456. * may have changed something that is in the instruction
  1457. * cache.
  1458. * NB: We flush both caches, just to be sure...
  1459. */
  1460. if (gdbstub_flush_caches)
  1461. debugger_local_cache_flushinv();
  1462. gdbstub_load_fpu();
  1463. mn10300_set_gdbleds(0);
  1464. if (excep == EXCEP_NMI)
  1465. NMICR = NMICR_NMIF;
  1466. touch_softlockup_watchdog();
  1467. local_irq_restore(epsw);
  1468. return 0;
  1469. }
  1470. /*
  1471. * Determine if we hit a debugger special breakpoint that needs skipping over
  1472. * automatically.
  1473. */
  1474. int at_debugger_breakpoint(struct pt_regs *regs)
  1475. {
  1476. return 0;
  1477. }
  1478. /*
  1479. * handle event interception
  1480. */
  1481. asmlinkage int debugger_intercept(enum exception_code excep,
  1482. int signo, int si_code, struct pt_regs *regs)
  1483. {
  1484. static u8 notfirst = 1;
  1485. int ret;
  1486. if (gdbstub_busy)
  1487. gdbstub_printk("--> gdbstub reentered itself\n");
  1488. gdbstub_busy = 1;
  1489. if (notfirst) {
  1490. unsigned long mdr;
  1491. asm("mov mdr,%0" : "=d"(mdr));
  1492. gdbstub_entry(
  1493. "--> debugger_intercept(%p,%04x) [MDR=%lx PC=%lx]\n",
  1494. regs, excep, mdr, regs->pc);
  1495. gdbstub_entry(
  1496. "PC: %08lx EPSW: %08lx SSP: %08lx mode: %s\n",
  1497. regs->pc, regs->epsw, (unsigned long) &ret,
  1498. user_mode(regs) ? "User" : "Super");
  1499. gdbstub_entry(
  1500. "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
  1501. regs->d0, regs->d1, regs->d2, regs->d3);
  1502. gdbstub_entry(
  1503. "a0: %08lx a1: %08lx a2: %08lx a3: %08lx\n",
  1504. regs->a0, regs->a1, regs->a2, regs->a3);
  1505. gdbstub_entry(
  1506. "e0: %08lx e1: %08lx e2: %08lx e3: %08lx\n",
  1507. regs->e0, regs->e1, regs->e2, regs->e3);
  1508. gdbstub_entry(
  1509. "e4: %08lx e5: %08lx e6: %08lx e7: %08lx\n",
  1510. regs->e4, regs->e5, regs->e6, regs->e7);
  1511. gdbstub_entry(
  1512. "lar: %08lx lir: %08lx mdr: %08lx usp: %08lx\n",
  1513. regs->lar, regs->lir, regs->mdr, regs->sp);
  1514. gdbstub_entry(
  1515. "cvf: %08lx crl: %08lx crh: %08lx drq: %08lx\n",
  1516. regs->mcvf, regs->mcrl, regs->mcrh, regs->mdrq);
  1517. gdbstub_entry(
  1518. "threadinfo=%p task=%p)\n",
  1519. current_thread_info(), current);
  1520. } else {
  1521. notfirst = 1;
  1522. }
  1523. ret = gdbstub(regs, excep);
  1524. gdbstub_entry("<-- debugger_intercept()\n");
  1525. gdbstub_busy = 0;
  1526. return ret;
  1527. }
  1528. /*
  1529. * handle the GDB stub itself causing an exception
  1530. */
  1531. asmlinkage void gdbstub_exception(struct pt_regs *regs,
  1532. enum exception_code excep)
  1533. {
  1534. unsigned long mdr;
  1535. asm("mov mdr,%0" : "=d"(mdr));
  1536. gdbstub_entry("--> gdbstub exception({%p},%04x) [MDR=%lx]\n",
  1537. regs, excep, mdr);
  1538. while ((unsigned long) regs == 0xffffffff) {}
  1539. /* handle guarded memory accesses where we know it might fault */
  1540. if (regs->pc == (unsigned) gdbstub_read_byte_guard) {
  1541. regs->pc = (unsigned) gdbstub_read_byte_cont;
  1542. goto fault;
  1543. }
  1544. if (regs->pc == (unsigned) gdbstub_read_word_guard) {
  1545. regs->pc = (unsigned) gdbstub_read_word_cont;
  1546. goto fault;
  1547. }
  1548. if (regs->pc == (unsigned) gdbstub_read_dword_guard) {
  1549. regs->pc = (unsigned) gdbstub_read_dword_cont;
  1550. goto fault;
  1551. }
  1552. if (regs->pc == (unsigned) gdbstub_write_byte_guard) {
  1553. regs->pc = (unsigned) gdbstub_write_byte_cont;
  1554. goto fault;
  1555. }
  1556. if (regs->pc == (unsigned) gdbstub_write_word_guard) {
  1557. regs->pc = (unsigned) gdbstub_write_word_cont;
  1558. goto fault;
  1559. }
  1560. if (regs->pc == (unsigned) gdbstub_write_dword_guard) {
  1561. regs->pc = (unsigned) gdbstub_write_dword_cont;
  1562. goto fault;
  1563. }
  1564. gdbstub_printk("\n### GDB stub caused an exception ###\n");
  1565. /* something went horribly wrong */
  1566. console_verbose();
  1567. show_registers(regs);
  1568. panic("GDB Stub caused an unexpected exception - can't continue\n");
  1569. /* we caught an attempt by the stub to access silly memory */
  1570. fault:
  1571. gdbstub_entry("<-- gdbstub exception() = EFAULT\n");
  1572. regs->d0 = -EFAULT;
  1573. return;
  1574. }
  1575. /*
  1576. * send an exit message to GDB
  1577. */
  1578. void gdbstub_exit(int status)
  1579. {
  1580. unsigned char checksum;
  1581. unsigned char ch;
  1582. int count;
  1583. gdbstub_busy = 1;
  1584. output_buffer[0] = 'W';
  1585. output_buffer[1] = hex_asc_hi(status);
  1586. output_buffer[2] = hex_asc_lo(status);
  1587. output_buffer[3] = 0;
  1588. gdbstub_io_tx_char('$');
  1589. checksum = 0;
  1590. count = 0;
  1591. while ((ch = output_buffer[count]) != 0) {
  1592. gdbstub_io_tx_char(ch);
  1593. checksum += ch;
  1594. count += 1;
  1595. }
  1596. gdbstub_io_tx_char('#');
  1597. gdbstub_io_tx_char(hex_asc_hi(checksum));
  1598. gdbstub_io_tx_char(hex_asc_lo(checksum));
  1599. /* make sure the output is flushed, or else RedBoot might clobber it */
  1600. gdbstub_io_tx_flush();
  1601. gdbstub_busy = 0;
  1602. }
  1603. /*
  1604. * initialise the GDB stub
  1605. */
  1606. asmlinkage void __init gdbstub_init(void)
  1607. {
  1608. #ifdef CONFIG_GDBSTUB_IMMEDIATE
  1609. unsigned char ch;
  1610. int ret;
  1611. #endif
  1612. gdbstub_busy = 1;
  1613. printk(KERN_INFO "%s", gdbstub_banner);
  1614. gdbstub_io_init();
  1615. gdbstub_entry("--> gdbstub_init\n");
  1616. /* try to talk to GDB (or anyone insane enough to want to type GDB
  1617. * protocol by hand) */
  1618. gdbstub_io("### GDB Tx ACK\n");
  1619. gdbstub_io_tx_char('+'); /* 'hello world' */
  1620. #ifdef CONFIG_GDBSTUB_IMMEDIATE
  1621. gdbstub_printk("GDB Stub waiting for packet\n");
  1622. /* in case GDB is started before us, ACK any packets that are already
  1623. * sitting there (presumably "$?#xx")
  1624. */
  1625. do { gdbstub_io_rx_char(&ch, 0); } while (ch != '$');
  1626. do { gdbstub_io_rx_char(&ch, 0); } while (ch != '#');
  1627. /* eat first csum byte */
  1628. do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
  1629. /* eat second csum byte */
  1630. do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
  1631. gdbstub_io("### GDB Tx NAK\n");
  1632. gdbstub_io_tx_char('-'); /* NAK it */
  1633. #else
  1634. printk("GDB Stub ready\n");
  1635. #endif
  1636. gdbstub_busy = 0;
  1637. gdbstub_entry("<-- gdbstub_init\n");
  1638. }
  1639. /*
  1640. * register the console at a more appropriate time
  1641. */
  1642. #ifdef CONFIG_GDBSTUB_CONSOLE
  1643. static int __init gdbstub_postinit(void)
  1644. {
  1645. printk(KERN_NOTICE "registering console\n");
  1646. register_console(&gdbstub_console);
  1647. return 0;
  1648. }
  1649. __initcall(gdbstub_postinit);
  1650. #endif
  1651. /*
  1652. * handle character reception on GDB serial port
  1653. * - jump into the GDB stub if BREAK is detected on the serial line
  1654. */
  1655. asmlinkage void gdbstub_rx_irq(struct pt_regs *regs, enum exception_code excep)
  1656. {
  1657. char ch;
  1658. int ret;
  1659. gdbstub_entry("--> gdbstub_rx_irq\n");
  1660. do {
  1661. ret = gdbstub_io_rx_char(&ch, 1);
  1662. if (ret != -EIO && ret != -EAGAIN) {
  1663. if (ret != -EINTR)
  1664. gdbstub_rx_unget = ch;
  1665. gdbstub(regs, excep);
  1666. }
  1667. } while (ret != -EAGAIN);
  1668. gdbstub_entry("<-- gdbstub_rx_irq\n");
  1669. }