gdb-stub.c 44 KB

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