gdb-stub.c 45 KB

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