gdb-stub.c 45 KB

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