dbg.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Serial debug monitor. Entered via the `dbg` command and driven from the host
  2. * with `make monitor` (picocom). ASCII commands, newline-terminated, hex args:
  3. *
  4. * r <addr> <len> read len bytes at addr -> hex dump
  5. * w <addr> <b0> [b1 ...] write bytes at addr
  6. * i <port> inb (read I/O port)
  7. * o <port> <val> outb (write I/O port)
  8. * h help
  9. * q quit
  10. *
  11. * Runs inline (blocks the terminal), so it owns the serial ring buffer and
  12. * doesn't conflict with the kexec auto-detect in the terminal idle loop.
  13. */
  14. #include "dbg.h"
  15. #include "paging.h"
  16. #include "serial.h"
  17. #define DBG_LINE_MAX 64
  18. #define DBG_NARGS 32
  19. #define DBG_READ_MAX 256
  20. static inline unsigned char dbg_inb(unsigned short port)
  21. {
  22. unsigned char v;
  23. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  24. return v;
  25. }
  26. static inline void dbg_outb(unsigned short port, unsigned char v)
  27. {
  28. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  29. }
  30. static void dbg_putbyte(unsigned char b)
  31. {
  32. static const char hex[] = "0123456789ABCDEF";
  33. serial_putc(SERIAL_COM1, hex[b >> 4]);
  34. serial_putc(SERIAL_COM1, hex[b & 0xF]);
  35. }
  36. /* Block until a byte arrives (echoing it so picocom works without local echo). */
  37. static void dbg_getc(unsigned char *c)
  38. {
  39. for (;;) {
  40. if (!serial_ring_wait(0xFFFFFFFFu, c))
  41. continue; /* timeout (effectively never) */
  42. if (*c == '\r')
  43. ; /* ignore bare CR */
  44. else if (*c == '\n')
  45. serial_puts(SERIAL_COM1, "\n");
  46. else
  47. serial_putc(SERIAL_COM1, (char)*c);
  48. return;
  49. }
  50. }
  51. static void dbg_read_line(char *buf, unsigned int max)
  52. {
  53. unsigned int i = 0;
  54. unsigned char c;
  55. for (;;) {
  56. dbg_getc(&c);
  57. if (c == '\n' || c == '\r')
  58. break;
  59. if (i + 1 < max)
  60. buf[i++] = (char)c;
  61. }
  62. buf[i] = '\0';
  63. }
  64. /* Parse a hex number at *s (optional 0x prefix); advance *s past it. */
  65. static int dbg_parse_hex(const char **s, unsigned int *out)
  66. {
  67. const char *p = *s;
  68. unsigned int v = 0;
  69. int digits = 0;
  70. if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
  71. p += 2;
  72. for (;; p++) {
  73. unsigned int d;
  74. char c = *p;
  75. if (c >= '0' && c <= '9') d = (unsigned int)(c - '0');
  76. else if (c >= 'a' && c <= 'f') d = (unsigned int)(c - 'a' + 10);
  77. else if (c >= 'A' && c <= 'F') d = (unsigned int)(c - 'A' + 10);
  78. else break;
  79. v = (v << 4) | d;
  80. digits++;
  81. }
  82. if (!digits)
  83. return 0;
  84. *out = v;
  85. *s = p;
  86. return 1;
  87. }
  88. /* Returns 1 to quit. */
  89. static int dbg_command(const char *line)
  90. {
  91. const char *p = line;
  92. unsigned int args[DBG_NARGS];
  93. unsigned int nargs = 0;
  94. char cmd;
  95. while (*p == ' ')
  96. p++;
  97. if (!*p)
  98. return 0;
  99. cmd = *p++;
  100. while (nargs < DBG_NARGS) {
  101. while (*p == ' ')
  102. p++;
  103. if (!*p)
  104. break;
  105. if (!dbg_parse_hex(&p, &args[nargs]))
  106. break;
  107. nargs++;
  108. }
  109. switch (cmd) {
  110. case 'r':
  111. if (nargs < 2) {
  112. serial_puts(SERIAL_COM1, "usage: r <addr> <len>\n");
  113. } else {
  114. unsigned int addr = args[0];
  115. unsigned int len = args[1];
  116. unsigned int i;
  117. if (len > DBG_READ_MAX)
  118. len = DBG_READ_MAX;
  119. serial_puthex(SERIAL_COM1, addr);
  120. serial_putc(SERIAL_COM1, ':');
  121. for (i = 0; i < len; i++) {
  122. volatile unsigned char *b;
  123. void *km;
  124. serial_putc(SERIAL_COM1, ' ');
  125. b = paging_debug_map(addr + i, &km);
  126. if (!b) {
  127. serial_puts(SERIAL_COM1, "??");
  128. continue;
  129. }
  130. dbg_putbyte(*b);
  131. if (km)
  132. kunmap(km);
  133. }
  134. serial_putc(SERIAL_COM1, '\n');
  135. }
  136. return 0;
  137. case 'w':
  138. if (nargs < 2) {
  139. serial_puts(SERIAL_COM1, "usage: w <addr> <b0> [b1 ...]\n");
  140. } else {
  141. unsigned int addr = args[0];
  142. unsigned int i;
  143. for (i = 1; i < nargs; i++) {
  144. void *km;
  145. volatile unsigned char *b =
  146. paging_debug_map(addr + i - 1, &km);
  147. if (!b)
  148. break;
  149. *b = (unsigned char)args[i];
  150. if (km)
  151. kunmap(km);
  152. }
  153. serial_puts(SERIAL_COM1, "ok\n");
  154. }
  155. return 0;
  156. case 'i':
  157. if (nargs < 1) {
  158. serial_puts(SERIAL_COM1, "usage: i <port>\n");
  159. } else {
  160. serial_puthex(SERIAL_COM1, args[0]);
  161. serial_puts(SERIAL_COM1, " = ");
  162. dbg_putbyte(dbg_inb((unsigned short)args[0]));
  163. serial_putc(SERIAL_COM1, '\n');
  164. }
  165. return 0;
  166. case 'o':
  167. if (nargs < 2) {
  168. serial_puts(SERIAL_COM1, "usage: o <port> <val>\n");
  169. } else {
  170. dbg_outb((unsigned short)args[0], (unsigned char)args[1]);
  171. serial_puts(SERIAL_COM1, "ok\n");
  172. }
  173. return 0;
  174. case 'h':
  175. serial_puts(SERIAL_COM1,
  176. "r <addr> <len> read memory (hex dump)\n"
  177. "w <addr> <b>... write bytes\n"
  178. "i <port> inb\n"
  179. "o <port> <val> outb\n"
  180. "h help\n"
  181. "q quit\n");
  182. return 0;
  183. case 'q':
  184. return 1;
  185. default:
  186. serial_puts(SERIAL_COM1, "unknown cmd (h for help)\n");
  187. return 0;
  188. }
  189. }
  190. void program_dbg_main(void)
  191. {
  192. char line[DBG_LINE_MAX];
  193. serial_puts(SERIAL_COM1, "\ndbg: ready (h for help)\n");
  194. for (;;) {
  195. serial_puts(SERIAL_COM1, "dbg> ");
  196. dbg_read_line(line, DBG_LINE_MAX);
  197. if (dbg_command(line))
  198. break;
  199. }
  200. serial_puts(SERIAL_COM1, "dbg: bye\n");
  201. }