cstub.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* cstub.c - machine independent portion of remote GDB stub */
  2. /*
  3. * Copyright (C) 2006 Lubomir Kundrak
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <grub/misc.h>
  20. #include <grub/cpu/gdb.h>
  21. #include <grub/gdb.h>
  22. #include <grub/serial.h>
  23. #include <grub/backtrace.h>
  24. static const char hexchars[] = "0123456789abcdef";
  25. int grub_gdb_regs[GRUB_MACHINE_NR_REGS];
  26. #define GRUB_GDB_COMBUF_SIZE 400 /* At least sizeof(grub_gdb_regs)*2 are needed for
  27. register packets. */
  28. static char grub_gdb_inbuf[GRUB_GDB_COMBUF_SIZE + 1];
  29. static char grub_gdb_outbuf[GRUB_GDB_COMBUF_SIZE + 1];
  30. struct grub_serial_port *grub_gdb_port;
  31. static int
  32. hex (char ch)
  33. {
  34. if ((ch >= 'a') && (ch <= 'f'))
  35. return (ch - 'a' + 10);
  36. if ((ch >= '0') && (ch <= '9'))
  37. return (ch - '0');
  38. if ((ch >= 'A') && (ch <= 'F'))
  39. return (ch - 'A' + 10);
  40. return (-1);
  41. }
  42. /* Scan for the sequence $<data>#<checksum>. */
  43. static char *
  44. grub_gdb_getpacket (void)
  45. {
  46. char *buffer = &grub_gdb_inbuf[0];
  47. unsigned char checksum;
  48. unsigned char xmitcsum;
  49. int count;
  50. int ch;
  51. while (1)
  52. {
  53. /* Wait around for the start character, ignore all other
  54. characters. */
  55. while ((ch = grub_serial_port_fetch (grub_gdb_port)) != '$');
  56. retry:
  57. checksum = 0;
  58. xmitcsum = -1;
  59. count = 0;
  60. /* Now read until a # or end of buffer is found. */
  61. while (count < GRUB_GDB_COMBUF_SIZE)
  62. {
  63. do
  64. ch = grub_serial_port_fetch (grub_gdb_port);
  65. while (ch < 0);
  66. if (ch == '$')
  67. goto retry;
  68. if (ch == '#')
  69. break;
  70. checksum += ch;
  71. buffer[count] = ch;
  72. count = count + 1;
  73. }
  74. buffer[count] = 0;
  75. if (ch == '#')
  76. {
  77. do
  78. ch = grub_serial_port_fetch (grub_gdb_port);
  79. while (ch < 0);
  80. xmitcsum = hex (ch) << 4;
  81. do
  82. ch = grub_serial_port_fetch (grub_gdb_port);
  83. while (ch < 0);
  84. xmitcsum += hex (ch);
  85. if (checksum != xmitcsum)
  86. grub_serial_port_put (grub_gdb_port, '-'); /* Failed checksum. */
  87. else
  88. {
  89. grub_serial_port_put (grub_gdb_port, '+'); /* Successful transfer. */
  90. /* If a sequence char is present, reply the sequence ID. */
  91. if (buffer[2] == ':')
  92. {
  93. grub_serial_port_put (grub_gdb_port, buffer[0]);
  94. grub_serial_port_put (grub_gdb_port, buffer[1]);
  95. return &buffer[3];
  96. }
  97. return &buffer[0];
  98. }
  99. }
  100. }
  101. }
  102. /* Send the packet in buffer. */
  103. static void
  104. grub_gdb_putpacket (char *buffer)
  105. {
  106. grub_uint8_t checksum;
  107. /* $<packet info>#<checksum>. */
  108. do
  109. {
  110. char *ptr;
  111. grub_serial_port_put (grub_gdb_port, '$');
  112. checksum = 0;
  113. for (ptr = buffer; *ptr; ptr++)
  114. {
  115. grub_serial_port_put (grub_gdb_port, *ptr);
  116. checksum += *ptr;
  117. }
  118. grub_serial_port_put (grub_gdb_port, '#');
  119. grub_serial_port_put (grub_gdb_port, hexchars[checksum >> 4]);
  120. grub_serial_port_put (grub_gdb_port, hexchars[checksum & 0xf]);
  121. }
  122. while (grub_serial_port_fetch (grub_gdb_port) != '+');
  123. }
  124. /* Convert the memory pointed to by mem into hex, placing result in buf.
  125. Return a pointer to the last char put in buf (NULL). */
  126. static char *
  127. grub_gdb_mem2hex (char *mem, char *buf, grub_size_t count)
  128. {
  129. grub_size_t i;
  130. unsigned char ch;
  131. for (i = 0; i < count; i++)
  132. {
  133. ch = *mem++;
  134. *buf++ = hexchars[ch >> 4];
  135. *buf++ = hexchars[ch % 16];
  136. }
  137. *buf = 0;
  138. return (buf);
  139. }
  140. /* Convert the hex array pointed to by buf into binary to be placed in mem.
  141. Return a pointer to the character after the last byte written. */
  142. static char *
  143. grub_gdb_hex2mem (char *buf, char *mem, int count)
  144. {
  145. int i;
  146. unsigned char ch;
  147. for (i = 0; i < count; i++)
  148. {
  149. ch = hex (*buf++) << 4;
  150. ch = ch + hex (*buf++);
  151. *mem++ = ch;
  152. }
  153. return (mem);
  154. }
  155. /* Convert hex characters to int and return the number of characters
  156. processed. */
  157. static int
  158. grub_gdb_hex2int (char **ptr, grub_uint64_t *int_value)
  159. {
  160. int num_chars = 0;
  161. int hex_value;
  162. *int_value = 0;
  163. while (**ptr)
  164. {
  165. hex_value = hex (**ptr);
  166. if (hex_value >= 0)
  167. {
  168. *int_value = (*int_value << 4) | hex_value;
  169. num_chars++;
  170. }
  171. else
  172. break;
  173. (*ptr)++;
  174. }
  175. return (num_chars);
  176. }
  177. /* This function does all command procesing for interfacing to gdb. */
  178. void __attribute__ ((regparm(3)))
  179. grub_gdb_trap (int trap_no)
  180. {
  181. unsigned int sig_no;
  182. int stepping;
  183. grub_uint64_t addr;
  184. grub_uint64_t length;
  185. char *ptr;
  186. if (!grub_gdb_port)
  187. {
  188. grub_printf ("Unhandled exception 0x%x at ", trap_no);
  189. grub_backtrace_print_address ((void *) grub_gdb_regs[PC]);
  190. grub_printf ("\n");
  191. grub_backtrace_pointer ((void *) grub_gdb_regs[EBP]);
  192. grub_fatal ("Unhandled exception");
  193. }
  194. sig_no = grub_gdb_trap2sig (trap_no);
  195. ptr = grub_gdb_outbuf;
  196. /* Reply to host that an exception has occurred. */
  197. *ptr++ = 'T'; /* Notify gdb with signo, PC, FP and SP. */
  198. *ptr++ = hexchars[sig_no >> 4];
  199. *ptr++ = hexchars[sig_no & 0xf];
  200. /* Stack pointer. */
  201. *ptr++ = hexchars[SP];
  202. *ptr++ = ':';
  203. ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[ESP], ptr, 4);
  204. *ptr++ = ';';
  205. /* Frame pointer. */
  206. *ptr++ = hexchars[FP];
  207. *ptr++ = ':';
  208. ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[EBP], ptr, 4);
  209. *ptr++ = ';';
  210. /* Program counter. */
  211. *ptr++ = hexchars[PC];
  212. *ptr++ = ':';
  213. ptr = grub_gdb_mem2hex ((char *) &grub_gdb_regs[PC], ptr, 4);
  214. *ptr++ = ';';
  215. *ptr = '\0';
  216. grub_gdb_putpacket (grub_gdb_outbuf);
  217. stepping = 0;
  218. while (1)
  219. {
  220. grub_gdb_outbuf[0] = 0;
  221. ptr = grub_gdb_getpacket ();
  222. switch (*ptr++)
  223. {
  224. case '?':
  225. grub_gdb_outbuf[0] = 'S';
  226. grub_gdb_outbuf[1] = hexchars[sig_no >> 4];
  227. grub_gdb_outbuf[2] = hexchars[sig_no & 0xf];
  228. grub_gdb_outbuf[3] = 0;
  229. break;
  230. /* Return values of the CPU registers. */
  231. case 'g':
  232. grub_gdb_mem2hex ((char *) grub_gdb_regs, grub_gdb_outbuf,
  233. sizeof (grub_gdb_regs));
  234. break;
  235. /* Set values of the CPU registers -- return OK. */
  236. case 'G':
  237. grub_gdb_hex2mem (ptr, (char *) grub_gdb_regs,
  238. sizeof (grub_gdb_regs));
  239. grub_strcpy (grub_gdb_outbuf, "OK");
  240. break;
  241. /* Set the value of a single CPU register -- return OK. */
  242. case 'P':
  243. {
  244. grub_uint64_t regno;
  245. if (grub_gdb_hex2int (&ptr, &regno) && *ptr++ == '=')
  246. if (regno < GRUB_MACHINE_NR_REGS)
  247. {
  248. grub_gdb_hex2mem (ptr, (char *) &grub_gdb_regs[regno], 4);
  249. grub_strcpy (grub_gdb_outbuf, "OK");
  250. break;
  251. }
  252. /* FIXME: GDB requires setting orig_eax. I don't know what's
  253. this register is about. For now just simulate setting any
  254. registers. */
  255. grub_strcpy (grub_gdb_outbuf, /*"E01"*/ "OK");
  256. break;
  257. }
  258. /* mAA..AA,LLLL: Read LLLL bytes at address AA..AA. */
  259. case 'm':
  260. /* Try to read %x,%x. Set ptr = 0 if successful. */
  261. if (grub_gdb_hex2int (&ptr, &addr))
  262. if (*(ptr++) == ',')
  263. if (grub_gdb_hex2int (&ptr, &length))
  264. {
  265. ptr = 0;
  266. grub_gdb_mem2hex ((char *) (grub_addr_t) addr,
  267. grub_gdb_outbuf, length);
  268. }
  269. if (ptr)
  270. grub_strcpy (grub_gdb_outbuf, "E01");
  271. break;
  272. /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA -- return OK. */
  273. case 'M':
  274. /* Try to read %x,%x. Set ptr = 0 if successful. */
  275. if (grub_gdb_hex2int (&ptr, &addr))
  276. if (*(ptr++) == ',')
  277. if (grub_gdb_hex2int (&ptr, &length))
  278. if (*(ptr++) == ':')
  279. {
  280. grub_gdb_hex2mem (ptr, (char *) (grub_addr_t) addr, length);
  281. grub_strcpy (grub_gdb_outbuf, "OK");
  282. ptr = 0;
  283. }
  284. if (ptr)
  285. {
  286. grub_strcpy (grub_gdb_outbuf, "E02");
  287. }
  288. break;
  289. /* sAA..AA: Step one instruction from AA..AA(optional). */
  290. case 's':
  291. stepping = 1;
  292. /* FALLTHROUGH */
  293. /* cAA..AA: Continue at address AA..AA(optional). */
  294. case 'c':
  295. /* try to read optional parameter, pc unchanged if no parm */
  296. if (grub_gdb_hex2int (&ptr, &addr))
  297. grub_gdb_regs[PC] = addr;
  298. /* Clear the trace bit. */
  299. grub_gdb_regs[PS] &= 0xfffffeff;
  300. /* Set the trace bit if we're stepping. */
  301. if (stepping)
  302. grub_gdb_regs[PS] |= 0x100;
  303. return;
  304. /* Kill the program. */
  305. case 'k':
  306. /* Do nothing. */
  307. return;
  308. }
  309. /* Reply to the request. */
  310. grub_gdb_putpacket (grub_gdb_outbuf);
  311. }
  312. }