gdb-stub.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  1. /* MN10300 GDB stub
  2. *
  3. * Originally written by Glenn Engel, Lake Stevens Instrument Division
  4. *
  5. * Contributed by HP Systems
  6. *
  7. * Modified for SPARC by Stu Grossman, Cygnus Support.
  8. *
  9. * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
  10. * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
  11. *
  12. * Copyright (C) 1995 Andreas Busse
  13. *
  14. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  15. * Modified for Linux/mn10300 by David Howells <dhowells@redhat.com>
  16. */
  17. /*
  18. * To enable debugger support, two things need to happen. One, a
  19. * call to set_debug_traps() is necessary in order to allow any breakpoints
  20. * or error conditions to be properly intercepted and reported to gdb.
  21. * Two, a breakpoint needs to be generated to begin communication. This
  22. * is most easily accomplished by a call to breakpoint(). Breakpoint()
  23. * simulates a breakpoint by executing a BREAK instruction.
  24. *
  25. *
  26. * The following gdb commands are supported:
  27. *
  28. * command function Return value
  29. *
  30. * g return the value of the CPU registers hex data or ENN
  31. * G set the value of the CPU registers OK or ENN
  32. *
  33. * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
  34. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
  35. *
  36. * c Resume at current address SNN ( signal NN)
  37. * cAA..AA Continue at address AA..AA SNN
  38. *
  39. * s Step one instruction SNN
  40. * sAA..AA Step one instruction from AA..AA SNN
  41. *
  42. * k kill
  43. *
  44. * ? What was the last sigval ? SNN (signal NN)
  45. *
  46. * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
  47. * baud rate
  48. *
  49. * All commands and responses are sent with a packet which includes a
  50. * checksum. A packet consists of
  51. *
  52. * $<packet info>#<checksum>.
  53. *
  54. * where
  55. * <packet info> :: <characters representing the command or response>
  56. * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
  57. *
  58. * When a packet is received, it is first acknowledged with either '+' or '-'.
  59. * '+' indicates a successful transfer. '-' indicates a failed transfer.
  60. *
  61. * Example:
  62. *
  63. * Host: Reply:
  64. * $m0,10#2a +$00010203040506070809101112131415#42
  65. *
  66. *
  67. * ==============
  68. * MORE EXAMPLES:
  69. * ==============
  70. *
  71. * For reference -- the following are the steps that one
  72. * company took (RidgeRun Inc) to get remote gdb debugging
  73. * going. In this scenario the host machine was a PC and the
  74. * target platform was a Galileo EVB64120A MIPS evaluation
  75. * board.
  76. *
  77. * Step 1:
  78. * First download gdb-5.0.tar.gz from the internet.
  79. * and then build/install the package.
  80. *
  81. * Example:
  82. * $ tar zxf gdb-5.0.tar.gz
  83. * $ cd gdb-5.0
  84. * $ ./configure --target=am33_2.0-linux-gnu
  85. * $ make
  86. * $ install
  87. * am33_2.0-linux-gnu-gdb
  88. *
  89. * Step 2:
  90. * Configure linux for remote debugging and build it.
  91. *
  92. * Example:
  93. * $ cd ~/linux
  94. * $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
  95. * $ make dep; make vmlinux
  96. *
  97. * Step 3:
  98. * Download the kernel to the remote target and start
  99. * the kernel running. It will promptly halt and wait
  100. * for the host gdb session to connect. It does this
  101. * since the "Kernel Hacking" option has defined
  102. * CONFIG_REMOTE_DEBUG which in turn enables your calls
  103. * to:
  104. * set_debug_traps();
  105. * breakpoint();
  106. *
  107. * Step 4:
  108. * Start the gdb session on the host.
  109. *
  110. * Example:
  111. * $ am33_2.0-linux-gnu-gdb vmlinux
  112. * (gdb) set remotebaud 115200
  113. * (gdb) target remote /dev/ttyS1
  114. * ...at this point you are connected to
  115. * the remote target and can use gdb
  116. * in the normal fasion. Setting
  117. * breakpoints, single stepping,
  118. * printing variables, etc.
  119. *
  120. */
  121. #include <linux/string.h>
  122. #include <linux/kernel.h>
  123. #include <linux/signal.h>
  124. #include <linux/sched.h>
  125. #include <linux/mm.h>
  126. #include <linux/console.h>
  127. #include <linux/init.h>
  128. #include <linux/bug.h>
  129. #include <asm/pgtable.h>
  130. #include <asm/system.h>
  131. #include <asm/gdb-stub.h>
  132. #include <asm/exceptions.h>
  133. #include <asm/cacheflush.h>
  134. #include <asm/serial-regs.h>
  135. #include <asm/busctl-regs.h>
  136. #include <asm/unit/leds.h>
  137. #include <asm/unit/serial.h>
  138. /* define to use F7F7 rather than FF which is subverted by JTAG debugger */
  139. #undef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  140. /*
  141. * BUFMAX defines the maximum number of characters in inbound/outbound buffers
  142. * at least NUMREGBYTES*2 are needed for register packets
  143. */
  144. #define BUFMAX 2048
  145. static const char gdbstub_banner[] =
  146. "Linux/MN10300 GDB Stub (c) RedHat 2007\n";
  147. u8 gdbstub_rx_buffer[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
  148. u32 gdbstub_rx_inp;
  149. u32 gdbstub_rx_outp;
  150. u8 gdbstub_busy;
  151. u8 gdbstub_rx_overflow;
  152. u8 gdbstub_rx_unget;
  153. static u8 gdbstub_flush_caches;
  154. static char input_buffer[BUFMAX];
  155. static char output_buffer[BUFMAX];
  156. static char trans_buffer[BUFMAX];
  157. 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. if (addr < (u8 *) 0x70000000UL)
  393. return 0;
  394. /* 70000000-7fffffff: vmalloc area */
  395. if (addr < (u8 *) 0x80000000UL)
  396. goto okay;
  397. if (addr < (u8 *) 0x8c000000UL)
  398. return 0;
  399. /* 8c000000-93ffffff: SRAM, SDRAM */
  400. if (addr < (u8 *) 0x94000000UL)
  401. goto okay;
  402. return 0;
  403. okay:
  404. if (gdbstub_read_byte(addr + 0, &step_bp[ix].opcode[0]) < 0 ||
  405. gdbstub_read_byte(addr + 1, &step_bp[ix].opcode[1]) < 0)
  406. return 0;
  407. step_bp[ix].addr = addr;
  408. return 1;
  409. }
  410. static inline void __gdbstub_restore_bp(void)
  411. {
  412. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  413. if (step_bp[0].addr) {
  414. gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
  415. gdbstub_write_byte(step_bp[0].opcode[1], step_bp[0].addr + 1);
  416. }
  417. if (step_bp[1].addr) {
  418. gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
  419. gdbstub_write_byte(step_bp[1].opcode[1], step_bp[1].addr + 1);
  420. }
  421. #else
  422. if (step_bp[0].addr)
  423. gdbstub_write_byte(step_bp[0].opcode[0], step_bp[0].addr + 0);
  424. if (step_bp[1].addr)
  425. gdbstub_write_byte(step_bp[1].opcode[0], step_bp[1].addr + 0);
  426. #endif
  427. gdbstub_flush_caches = 1;
  428. step_bp[0].addr = NULL;
  429. step_bp[0].opcode[0] = 0;
  430. step_bp[0].opcode[1] = 0;
  431. step_bp[1].addr = NULL;
  432. step_bp[1].opcode[0] = 0;
  433. step_bp[1].opcode[1] = 0;
  434. }
  435. /*
  436. * emulate single stepping by means of breakpoint instructions
  437. */
  438. static int gdbstub_single_step(struct pt_regs *regs)
  439. {
  440. unsigned size;
  441. uint32_t x;
  442. uint8_t cur, *pc, *sp;
  443. step_bp[0].addr = NULL;
  444. step_bp[0].opcode[0] = 0;
  445. step_bp[0].opcode[1] = 0;
  446. step_bp[1].addr = NULL;
  447. step_bp[1].opcode[0] = 0;
  448. step_bp[1].opcode[1] = 0;
  449. x = 0;
  450. pc = (u8 *) regs->pc;
  451. sp = (u8 *) (regs + 1);
  452. if (gdbstub_read_byte(pc, &cur) < 0)
  453. return -EFAULT;
  454. gdbstub_bkpt("Single Step from %p { %02x }\n", pc, cur);
  455. gdbstub_flush_caches = 1;
  456. size = gdbstub_insn_sizes[cur];
  457. if (size > 0) {
  458. if (!__gdbstub_mark_bp(pc + size, 0))
  459. goto fault;
  460. } else {
  461. switch (cur) {
  462. /* Bxx (d8,PC) */
  463. case 0xc0:
  464. case 0xc1:
  465. case 0xc2:
  466. case 0xc3:
  467. case 0xc4:
  468. case 0xc5:
  469. case 0xc6:
  470. case 0xc7:
  471. case 0xc8:
  472. case 0xc9:
  473. case 0xca:
  474. if (gdbstub_read_byte(pc + 1, (u8 *) &x) < 0)
  475. goto fault;
  476. if (!__gdbstub_mark_bp(pc + 2, 0))
  477. goto fault;
  478. if ((x < 0 || x > 2) &&
  479. !__gdbstub_mark_bp(pc + (s8) x, 1))
  480. goto fault;
  481. break;
  482. /* LXX (d8,PC) */
  483. case 0xd0:
  484. case 0xd1:
  485. case 0xd2:
  486. case 0xd3:
  487. case 0xd4:
  488. case 0xd5:
  489. case 0xd6:
  490. case 0xd7:
  491. case 0xd8:
  492. case 0xd9:
  493. case 0xda:
  494. if (!__gdbstub_mark_bp(pc + 1, 0))
  495. goto fault;
  496. if (regs->pc != regs->lar &&
  497. !__gdbstub_mark_bp((u8 *) regs->lar, 1))
  498. goto fault;
  499. break;
  500. /* SETLB - loads the next for bytes into the LIR
  501. * register */
  502. case 0xdb:
  503. if (!__gdbstub_mark_bp(pc + 1, 0))
  504. goto fault;
  505. break;
  506. /* JMP (d16,PC) or CALL (d16,PC) */
  507. case 0xcc:
  508. case 0xcd:
  509. if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
  510. gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0)
  511. goto fault;
  512. if (!__gdbstub_mark_bp(pc + (s16) x, 0))
  513. goto fault;
  514. break;
  515. /* JMP (d32,PC) or CALL (d32,PC) */
  516. case 0xdc:
  517. case 0xdd:
  518. if (gdbstub_read_byte(pc + 1, ((u8 *) &x) + 0) < 0 ||
  519. gdbstub_read_byte(pc + 2, ((u8 *) &x) + 1) < 0 ||
  520. gdbstub_read_byte(pc + 3, ((u8 *) &x) + 2) < 0 ||
  521. gdbstub_read_byte(pc + 4, ((u8 *) &x) + 3) < 0)
  522. goto fault;
  523. if (!__gdbstub_mark_bp(pc + (s32) x, 0))
  524. goto fault;
  525. break;
  526. /* RETF */
  527. case 0xde:
  528. if (!__gdbstub_mark_bp((u8 *) regs->mdr, 0))
  529. goto fault;
  530. break;
  531. /* RET */
  532. case 0xdf:
  533. if (gdbstub_read_byte(pc + 2, (u8 *) &x) < 0)
  534. goto fault;
  535. sp += (s8)x;
  536. if (gdbstub_read_byte(sp + 0, ((u8 *) &x) + 0) < 0 ||
  537. gdbstub_read_byte(sp + 1, ((u8 *) &x) + 1) < 0 ||
  538. gdbstub_read_byte(sp + 2, ((u8 *) &x) + 2) < 0 ||
  539. gdbstub_read_byte(sp + 3, ((u8 *) &x) + 3) < 0)
  540. goto fault;
  541. if (!__gdbstub_mark_bp((u8 *) x, 0))
  542. goto fault;
  543. break;
  544. case 0xf0:
  545. if (gdbstub_read_byte(pc + 1, &cur) < 0)
  546. goto fault;
  547. if (cur >= 0xf0 && cur <= 0xf7) {
  548. /* JMP (An) / CALLS (An) */
  549. switch (cur & 3) {
  550. case 0: x = regs->a0; break;
  551. case 1: x = regs->a1; break;
  552. case 2: x = regs->a2; break;
  553. case 3: x = regs->a3; break;
  554. }
  555. if (!__gdbstub_mark_bp((u8 *) x, 0))
  556. goto fault;
  557. } else if (cur == 0xfc) {
  558. /* RETS */
  559. if (gdbstub_read_byte(
  560. sp + 0, ((u8 *) &x) + 0) < 0 ||
  561. gdbstub_read_byte(
  562. sp + 1, ((u8 *) &x) + 1) < 0 ||
  563. gdbstub_read_byte(
  564. sp + 2, ((u8 *) &x) + 2) < 0 ||
  565. gdbstub_read_byte(
  566. sp + 3, ((u8 *) &x) + 3) < 0)
  567. goto fault;
  568. if (!__gdbstub_mark_bp((u8 *) x, 0))
  569. goto fault;
  570. } else if (cur == 0xfd) {
  571. /* RTI */
  572. if (gdbstub_read_byte(
  573. sp + 4, ((u8 *) &x) + 0) < 0 ||
  574. gdbstub_read_byte(
  575. sp + 5, ((u8 *) &x) + 1) < 0 ||
  576. gdbstub_read_byte(
  577. sp + 6, ((u8 *) &x) + 2) < 0 ||
  578. gdbstub_read_byte(
  579. sp + 7, ((u8 *) &x) + 3) < 0)
  580. goto fault;
  581. if (!__gdbstub_mark_bp((u8 *) x, 0))
  582. goto fault;
  583. } else {
  584. if (!__gdbstub_mark_bp(pc + 2, 0))
  585. goto fault;
  586. }
  587. break;
  588. /* potential 3-byte conditional branches */
  589. case 0xf8:
  590. if (gdbstub_read_byte(pc + 1, &cur) < 0)
  591. goto fault;
  592. if (!__gdbstub_mark_bp(pc + 3, 0))
  593. goto fault;
  594. if (cur >= 0xe8 && cur <= 0xeb) {
  595. if (gdbstub_read_byte(
  596. pc + 2, ((u8 *) &x) + 0) < 0)
  597. goto fault;
  598. if ((x < 0 || x > 3) &&
  599. !__gdbstub_mark_bp(pc + (s8) x, 1))
  600. goto fault;
  601. }
  602. break;
  603. case 0xfa:
  604. if (gdbstub_read_byte(pc + 1, &cur) < 0)
  605. goto fault;
  606. if (cur == 0xff) {
  607. /* CALLS (d16,PC) */
  608. if (gdbstub_read_byte(
  609. pc + 2, ((u8 *) &x) + 0) < 0 ||
  610. gdbstub_read_byte(
  611. pc + 3, ((u8 *) &x) + 1) < 0)
  612. goto fault;
  613. if (!__gdbstub_mark_bp(pc + (s16) x, 0))
  614. goto fault;
  615. } else {
  616. if (!__gdbstub_mark_bp(pc + 4, 0))
  617. goto fault;
  618. }
  619. break;
  620. case 0xfc:
  621. if (gdbstub_read_byte(pc + 1, &cur) < 0)
  622. goto fault;
  623. if (cur == 0xff) {
  624. /* CALLS (d32,PC) */
  625. if (gdbstub_read_byte(
  626. pc + 2, ((u8 *) &x) + 0) < 0 ||
  627. gdbstub_read_byte(
  628. pc + 3, ((u8 *) &x) + 1) < 0 ||
  629. gdbstub_read_byte(
  630. pc + 4, ((u8 *) &x) + 2) < 0 ||
  631. gdbstub_read_byte(
  632. pc + 5, ((u8 *) &x) + 3) < 0)
  633. goto fault;
  634. if (!__gdbstub_mark_bp(
  635. pc + (s32) x, 0))
  636. goto fault;
  637. } else {
  638. if (!__gdbstub_mark_bp(
  639. pc + 6, 0))
  640. goto fault;
  641. }
  642. break;
  643. }
  644. }
  645. gdbstub_bkpt("Step: %02x at %p; %02x at %p\n",
  646. step_bp[0].opcode[0], step_bp[0].addr,
  647. step_bp[1].opcode[0], step_bp[1].addr);
  648. if (step_bp[0].addr) {
  649. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  650. if (gdbstub_write_byte(0xF7, step_bp[0].addr + 0) < 0 ||
  651. gdbstub_write_byte(0xF7, step_bp[0].addr + 1) < 0)
  652. goto fault;
  653. #else
  654. if (gdbstub_write_byte(0xFF, step_bp[0].addr + 0) < 0)
  655. goto fault;
  656. #endif
  657. }
  658. if (step_bp[1].addr) {
  659. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  660. if (gdbstub_write_byte(0xF7, step_bp[1].addr + 0) < 0 ||
  661. gdbstub_write_byte(0xF7, step_bp[1].addr + 1) < 0)
  662. goto fault;
  663. #else
  664. if (gdbstub_write_byte(0xFF, step_bp[1].addr + 0) < 0)
  665. goto fault;
  666. #endif
  667. }
  668. return 0;
  669. fault:
  670. /* uh-oh - silly address alert, try and restore things */
  671. __gdbstub_restore_bp();
  672. return -EFAULT;
  673. }
  674. #ifdef CONFIG_GDBSTUB_CONSOLE
  675. void gdbstub_console_write(struct console *con, const char *p, unsigned n)
  676. {
  677. static const char gdbstub_cr[] = { 0x0d };
  678. char outbuf[26];
  679. int qty;
  680. u8 busy;
  681. busy = gdbstub_busy;
  682. gdbstub_busy = 1;
  683. outbuf[0] = 'O';
  684. while (n > 0) {
  685. qty = 1;
  686. while (n > 0 && qty < 20) {
  687. mem2hex(p, outbuf + qty, 2, 0);
  688. qty += 2;
  689. if (*p == 0x0a) {
  690. mem2hex(gdbstub_cr, outbuf + qty, 2, 0);
  691. qty += 2;
  692. }
  693. p++;
  694. n--;
  695. }
  696. outbuf[qty] = 0;
  697. putpacket(outbuf);
  698. }
  699. gdbstub_busy = busy;
  700. }
  701. static kdev_t gdbstub_console_dev(struct console *con)
  702. {
  703. return MKDEV(1, 3); /* /dev/null */
  704. }
  705. static struct console gdbstub_console = {
  706. .name = "gdb",
  707. .write = gdbstub_console_write,
  708. .device = gdbstub_console_dev,
  709. .flags = CON_PRINTBUFFER,
  710. .index = -1,
  711. };
  712. #endif
  713. /*
  714. * Convert the memory pointed to by mem into hex, placing result in buf.
  715. * - if successful, return a pointer to the last char put in buf (NUL)
  716. * - in case of mem fault, return NULL
  717. * may_fault is non-zero if we are reading from arbitrary memory, but is
  718. * currently not used.
  719. */
  720. static
  721. unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fault)
  722. {
  723. const u8 *mem = _mem;
  724. u8 ch[4];
  725. if ((u32) mem & 1 && count >= 1) {
  726. if (gdbstub_read_byte(mem, ch) != 0)
  727. return 0;
  728. buf = pack_hex_byte(buf, ch[0]);
  729. mem++;
  730. count--;
  731. }
  732. if ((u32) mem & 3 && count >= 2) {
  733. if (gdbstub_read_word(mem, ch) != 0)
  734. return 0;
  735. buf = pack_hex_byte(buf, ch[0]);
  736. buf = pack_hex_byte(buf, ch[1]);
  737. mem += 2;
  738. count -= 2;
  739. }
  740. while (count >= 4) {
  741. if (gdbstub_read_dword(mem, ch) != 0)
  742. return 0;
  743. buf = pack_hex_byte(buf, ch[0]);
  744. buf = pack_hex_byte(buf, ch[1]);
  745. buf = pack_hex_byte(buf, ch[2]);
  746. buf = pack_hex_byte(buf, ch[3]);
  747. mem += 4;
  748. count -= 4;
  749. }
  750. if (count >= 2) {
  751. if (gdbstub_read_word(mem, ch) != 0)
  752. return 0;
  753. buf = pack_hex_byte(buf, ch[0]);
  754. buf = pack_hex_byte(buf, ch[1]);
  755. mem += 2;
  756. count -= 2;
  757. }
  758. if (count >= 1) {
  759. if (gdbstub_read_byte(mem, ch) != 0)
  760. return 0;
  761. buf = pack_hex_byte(buf, ch[0]);
  762. }
  763. *buf = 0;
  764. return buf;
  765. }
  766. /*
  767. * convert the hex array pointed to by buf into binary to be placed in mem
  768. * return a pointer to the character AFTER the last byte written
  769. * may_fault is non-zero if we are reading from arbitrary memory, but is
  770. * currently not used.
  771. */
  772. static
  773. const char *hex2mem(const char *buf, void *_mem, int count, int may_fault)
  774. {
  775. u8 *mem = _mem;
  776. union {
  777. u32 val;
  778. u8 b[4];
  779. } ch;
  780. if ((u32) mem & 1 && count >= 1) {
  781. ch.b[0] = hex(*buf++) << 4;
  782. ch.b[0] |= hex(*buf++);
  783. if (gdbstub_write_byte(ch.val, mem) != 0)
  784. return 0;
  785. mem++;
  786. count--;
  787. }
  788. if ((u32) mem & 3 && 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. while (count >= 4) {
  799. ch.b[0] = hex(*buf++) << 4;
  800. ch.b[0] |= hex(*buf++);
  801. ch.b[1] = hex(*buf++) << 4;
  802. ch.b[1] |= hex(*buf++);
  803. ch.b[2] = hex(*buf++) << 4;
  804. ch.b[2] |= hex(*buf++);
  805. ch.b[3] = hex(*buf++) << 4;
  806. ch.b[3] |= hex(*buf++);
  807. if (gdbstub_write_dword(ch.val, mem) != 0)
  808. return 0;
  809. mem += 4;
  810. count -= 4;
  811. }
  812. if (count >= 2) {
  813. ch.b[0] = hex(*buf++) << 4;
  814. ch.b[0] |= hex(*buf++);
  815. ch.b[1] = hex(*buf++) << 4;
  816. ch.b[1] |= hex(*buf++);
  817. if (gdbstub_write_word(ch.val, mem) != 0)
  818. return 0;
  819. mem += 2;
  820. count -= 2;
  821. }
  822. if (count >= 1) {
  823. ch.b[0] = hex(*buf++) << 4;
  824. ch.b[0] |= hex(*buf++);
  825. if (gdbstub_write_byte(ch.val, mem) != 0)
  826. return 0;
  827. }
  828. return buf;
  829. }
  830. /*
  831. * This table contains the mapping between MN10300 exception codes, and
  832. * signals, which are primarily what GDB understands. It also indicates
  833. * which hardware traps we need to commandeer when initializing the stub.
  834. */
  835. static const struct excep_to_sig_map {
  836. enum exception_code excep; /* MN10300 exception code */
  837. unsigned char signo; /* Signal that we map this into */
  838. } excep_to_sig_map[] = {
  839. { EXCEP_ITLBMISS, SIGSEGV },
  840. { EXCEP_DTLBMISS, SIGSEGV },
  841. { EXCEP_TRAP, SIGTRAP },
  842. { EXCEP_ISTEP, SIGTRAP },
  843. { EXCEP_IBREAK, SIGTRAP },
  844. { EXCEP_OBREAK, SIGTRAP },
  845. { EXCEP_UNIMPINS, SIGILL },
  846. { EXCEP_UNIMPEXINS, SIGILL },
  847. { EXCEP_MEMERR, SIGSEGV },
  848. { EXCEP_MISALIGN, SIGSEGV },
  849. { EXCEP_BUSERROR, SIGBUS },
  850. { EXCEP_ILLINSACC, SIGSEGV },
  851. { EXCEP_ILLDATACC, SIGSEGV },
  852. { EXCEP_IOINSACC, SIGSEGV },
  853. { EXCEP_PRIVINSACC, SIGSEGV },
  854. { EXCEP_PRIVDATACC, SIGSEGV },
  855. { EXCEP_FPU_DISABLED, SIGFPE },
  856. { EXCEP_FPU_UNIMPINS, SIGFPE },
  857. { EXCEP_FPU_OPERATION, SIGFPE },
  858. { EXCEP_WDT, SIGALRM },
  859. { EXCEP_NMI, SIGQUIT },
  860. { EXCEP_IRQ_LEVEL0, SIGINT },
  861. { EXCEP_IRQ_LEVEL1, SIGINT },
  862. { EXCEP_IRQ_LEVEL2, SIGINT },
  863. { EXCEP_IRQ_LEVEL3, SIGINT },
  864. { EXCEP_IRQ_LEVEL4, SIGINT },
  865. { EXCEP_IRQ_LEVEL5, SIGINT },
  866. { EXCEP_IRQ_LEVEL6, SIGINT },
  867. { 0, 0}
  868. };
  869. /*
  870. * convert the MN10300 exception code into a UNIX signal number
  871. */
  872. static int computeSignal(enum exception_code excep)
  873. {
  874. const struct excep_to_sig_map *map;
  875. for (map = excep_to_sig_map; map->signo; map++)
  876. if (map->excep == excep)
  877. return map->signo;
  878. return SIGHUP; /* default for things we don't know about */
  879. }
  880. static u32 gdbstub_fpcr, gdbstub_fpufs_array[32];
  881. /*
  882. *
  883. */
  884. static void gdbstub_store_fpu(void)
  885. {
  886. #ifdef CONFIG_FPU
  887. asm volatile(
  888. "or %2,epsw\n"
  889. #ifdef CONFIG_MN10300_PROC_MN103E010
  890. "nop\n"
  891. "nop\n"
  892. #endif
  893. "mov %1, a1\n"
  894. "fmov fs0, (a1+)\n"
  895. "fmov fs1, (a1+)\n"
  896. "fmov fs2, (a1+)\n"
  897. "fmov fs3, (a1+)\n"
  898. "fmov fs4, (a1+)\n"
  899. "fmov fs5, (a1+)\n"
  900. "fmov fs6, (a1+)\n"
  901. "fmov fs7, (a1+)\n"
  902. "fmov fs8, (a1+)\n"
  903. "fmov fs9, (a1+)\n"
  904. "fmov fs10, (a1+)\n"
  905. "fmov fs11, (a1+)\n"
  906. "fmov fs12, (a1+)\n"
  907. "fmov fs13, (a1+)\n"
  908. "fmov fs14, (a1+)\n"
  909. "fmov fs15, (a1+)\n"
  910. "fmov fs16, (a1+)\n"
  911. "fmov fs17, (a1+)\n"
  912. "fmov fs18, (a1+)\n"
  913. "fmov fs19, (a1+)\n"
  914. "fmov fs20, (a1+)\n"
  915. "fmov fs21, (a1+)\n"
  916. "fmov fs22, (a1+)\n"
  917. "fmov fs23, (a1+)\n"
  918. "fmov fs24, (a1+)\n"
  919. "fmov fs25, (a1+)\n"
  920. "fmov fs26, (a1+)\n"
  921. "fmov fs27, (a1+)\n"
  922. "fmov fs28, (a1+)\n"
  923. "fmov fs29, (a1+)\n"
  924. "fmov fs30, (a1+)\n"
  925. "fmov fs31, (a1+)\n"
  926. "fmov fpcr, %0\n"
  927. : "=d"(gdbstub_fpcr)
  928. : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE)
  929. : "a1"
  930. );
  931. #endif
  932. }
  933. /*
  934. *
  935. */
  936. static void gdbstub_load_fpu(void)
  937. {
  938. #ifdef CONFIG_FPU
  939. asm volatile(
  940. "or %1,epsw\n"
  941. #ifdef CONFIG_MN10300_PROC_MN103E010
  942. "nop\n"
  943. "nop\n"
  944. #endif
  945. "mov %0, a1\n"
  946. "fmov (a1+), fs0\n"
  947. "fmov (a1+), fs1\n"
  948. "fmov (a1+), fs2\n"
  949. "fmov (a1+), fs3\n"
  950. "fmov (a1+), fs4\n"
  951. "fmov (a1+), fs5\n"
  952. "fmov (a1+), fs6\n"
  953. "fmov (a1+), fs7\n"
  954. "fmov (a1+), fs8\n"
  955. "fmov (a1+), fs9\n"
  956. "fmov (a1+), fs10\n"
  957. "fmov (a1+), fs11\n"
  958. "fmov (a1+), fs12\n"
  959. "fmov (a1+), fs13\n"
  960. "fmov (a1+), fs14\n"
  961. "fmov (a1+), fs15\n"
  962. "fmov (a1+), fs16\n"
  963. "fmov (a1+), fs17\n"
  964. "fmov (a1+), fs18\n"
  965. "fmov (a1+), fs19\n"
  966. "fmov (a1+), fs20\n"
  967. "fmov (a1+), fs21\n"
  968. "fmov (a1+), fs22\n"
  969. "fmov (a1+), fs23\n"
  970. "fmov (a1+), fs24\n"
  971. "fmov (a1+), fs25\n"
  972. "fmov (a1+), fs26\n"
  973. "fmov (a1+), fs27\n"
  974. "fmov (a1+), fs28\n"
  975. "fmov (a1+), fs29\n"
  976. "fmov (a1+), fs30\n"
  977. "fmov (a1+), fs31\n"
  978. "fmov %2, fpcr\n"
  979. :
  980. : "g" (&gdbstub_fpufs_array), "i"(EPSW_FE), "d"(gdbstub_fpcr)
  981. : "a1"
  982. );
  983. #endif
  984. }
  985. /*
  986. * set a software breakpoint
  987. */
  988. int gdbstub_set_breakpoint(u8 *addr, int len)
  989. {
  990. int bkpt, loop, xloop;
  991. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  992. len = (len + 1) & ~1;
  993. #endif
  994. gdbstub_bkpt("setbkpt(%p,%d)\n", addr, len);
  995. for (bkpt = 255; bkpt >= 0; bkpt--)
  996. if (!gdbstub_bkpts[bkpt].addr)
  997. break;
  998. if (bkpt < 0)
  999. return -ENOSPC;
  1000. for (loop = 0; loop < len; loop++)
  1001. if (gdbstub_read_byte(&addr[loop],
  1002. &gdbstub_bkpts[bkpt].origbytes[loop]
  1003. ) < 0)
  1004. return -EFAULT;
  1005. gdbstub_flush_caches = 1;
  1006. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  1007. for (loop = 0; loop < len; loop++)
  1008. if (gdbstub_write_byte(0xF7, &addr[loop]) < 0)
  1009. goto restore;
  1010. #else
  1011. for (loop = 0; loop < len; loop++)
  1012. if (gdbstub_write_byte(0xFF, &addr[loop]) < 0)
  1013. goto restore;
  1014. #endif
  1015. gdbstub_bkpts[bkpt].addr = addr;
  1016. gdbstub_bkpts[bkpt].len = len;
  1017. gdbstub_bkpt("Set BKPT[%02x]: %p-%p {%02x%02x%02x%02x%02x%02x%02x}\n",
  1018. bkpt,
  1019. gdbstub_bkpts[bkpt].addr,
  1020. gdbstub_bkpts[bkpt].addr + gdbstub_bkpts[bkpt].len - 1,
  1021. gdbstub_bkpts[bkpt].origbytes[0],
  1022. gdbstub_bkpts[bkpt].origbytes[1],
  1023. gdbstub_bkpts[bkpt].origbytes[2],
  1024. gdbstub_bkpts[bkpt].origbytes[3],
  1025. gdbstub_bkpts[bkpt].origbytes[4],
  1026. gdbstub_bkpts[bkpt].origbytes[5],
  1027. gdbstub_bkpts[bkpt].origbytes[6]
  1028. );
  1029. return 0;
  1030. restore:
  1031. for (xloop = 0; xloop < loop; xloop++)
  1032. gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[xloop],
  1033. addr + xloop);
  1034. return -EFAULT;
  1035. }
  1036. /*
  1037. * clear a software breakpoint
  1038. */
  1039. int gdbstub_clear_breakpoint(u8 *addr, int len)
  1040. {
  1041. int bkpt, loop;
  1042. #ifdef GDBSTUB_USE_F7F7_AS_BREAKPOINT
  1043. len = (len + 1) & ~1;
  1044. #endif
  1045. gdbstub_bkpt("clearbkpt(%p,%d)\n", addr, len);
  1046. for (bkpt = 255; bkpt >= 0; bkpt--)
  1047. if (gdbstub_bkpts[bkpt].addr == addr &&
  1048. gdbstub_bkpts[bkpt].len == len)
  1049. break;
  1050. if (bkpt < 0)
  1051. return -ENOENT;
  1052. gdbstub_bkpts[bkpt].addr = NULL;
  1053. gdbstub_flush_caches = 1;
  1054. for (loop = 0; loop < len; loop++)
  1055. if (gdbstub_write_byte(gdbstub_bkpts[bkpt].origbytes[loop],
  1056. addr + loop) < 0)
  1057. return -EFAULT;
  1058. return 0;
  1059. }
  1060. /*
  1061. * This function does all command processing for interfacing to gdb
  1062. * - returns 1 if the exception should be skipped, 0 otherwise.
  1063. */
  1064. static int gdbstub(struct pt_regs *regs, enum exception_code excep)
  1065. {
  1066. unsigned long *stack;
  1067. unsigned long epsw, mdr;
  1068. uint32_t zero, ssp;
  1069. uint8_t broke;
  1070. char *ptr;
  1071. int sigval;
  1072. int addr;
  1073. int length;
  1074. int loop;
  1075. if (excep == EXCEP_FPU_DISABLED)
  1076. return 0;
  1077. gdbstub_flush_caches = 0;
  1078. mn10300_set_gdbleds(1);
  1079. asm volatile("mov mdr,%0" : "=d"(mdr));
  1080. asm volatile("mov epsw,%0" : "=d"(epsw));
  1081. asm volatile("mov %0,epsw"
  1082. :: "d"((epsw & ~EPSW_IM) | EPSW_IE | EPSW_IM_1));
  1083. gdbstub_store_fpu();
  1084. #ifdef CONFIG_GDBSTUB_IMMEDIATE
  1085. /* skip the initial pause loop */
  1086. if (regs->pc == (unsigned long) __gdbstub_pause)
  1087. regs->pc = (unsigned long) start_kernel;
  1088. #endif
  1089. /* if we were single stepping, restore the opcodes hoisted for the
  1090. * breakpoint[s] */
  1091. broke = 0;
  1092. if ((step_bp[0].addr && step_bp[0].addr == (u8 *) regs->pc) ||
  1093. (step_bp[1].addr && step_bp[1].addr == (u8 *) regs->pc))
  1094. broke = 1;
  1095. __gdbstub_restore_bp();
  1096. if (gdbstub_rx_unget) {
  1097. sigval = SIGINT;
  1098. if (gdbstub_rx_unget != 3)
  1099. goto packet_waiting;
  1100. gdbstub_rx_unget = 0;
  1101. }
  1102. stack = (unsigned long *) regs->sp;
  1103. sigval = broke ? SIGTRAP : computeSignal(excep);
  1104. /* send information about a BUG() */
  1105. if (!user_mode(regs) && excep == EXCEP_SYSCALL15) {
  1106. const struct bug_entry *bug;
  1107. bug = find_bug(regs->pc);
  1108. if (bug)
  1109. goto found_bug;
  1110. length = snprintf(trans_buffer, sizeof(trans_buffer),
  1111. "BUG() at address %lx\n", regs->pc);
  1112. goto send_bug_pkt;
  1113. found_bug:
  1114. length = snprintf(trans_buffer, sizeof(trans_buffer),
  1115. "BUG() at address %lx (%s:%d)\n",
  1116. regs->pc, bug->file, bug->line);
  1117. send_bug_pkt:
  1118. ptr = output_buffer;
  1119. *ptr++ = 'O';
  1120. ptr = mem2hex(trans_buffer, ptr, length, 0);
  1121. *ptr = 0;
  1122. putpacket(output_buffer);
  1123. regs->pc -= 2;
  1124. sigval = SIGABRT;
  1125. } else if (regs->pc == (unsigned long) __gdbstub_bug_trap) {
  1126. regs->pc = regs->mdr;
  1127. sigval = SIGABRT;
  1128. }
  1129. /*
  1130. * send a message to the debugger's user saying what happened if it may
  1131. * not be clear cut (we can't map exceptions onto signals properly)
  1132. */
  1133. if (sigval != SIGINT && sigval != SIGTRAP && sigval != SIGILL) {
  1134. static const char title[] = "Excep ", tbcberr[] = "BCBERR ";
  1135. static const char crlf[] = "\r\n";
  1136. char hx;
  1137. u32 bcberr = BCBERR;
  1138. ptr = output_buffer;
  1139. *ptr++ = 'O';
  1140. ptr = mem2hex(title, ptr, sizeof(title) - 1, 0);
  1141. hx = hex_asc_hi(excep >> 8);
  1142. ptr = pack_hex_byte(ptr, hx);
  1143. hx = hex_asc_lo(excep >> 8);
  1144. ptr = pack_hex_byte(ptr, hx);
  1145. hx = hex_asc_hi(excep);
  1146. ptr = pack_hex_byte(ptr, hx);
  1147. hx = hex_asc_lo(excep);
  1148. ptr = pack_hex_byte(ptr, hx);
  1149. ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
  1150. *ptr = 0;
  1151. putpacket(output_buffer); /* send it off... */
  1152. /* BCBERR */
  1153. ptr = output_buffer;
  1154. *ptr++ = 'O';
  1155. ptr = mem2hex(tbcberr, ptr, sizeof(tbcberr) - 1, 0);
  1156. hx = hex_asc_hi(bcberr >> 24);
  1157. ptr = pack_hex_byte(ptr, hx);
  1158. hx = hex_asc_lo(bcberr >> 24);
  1159. ptr = pack_hex_byte(ptr, hx);
  1160. hx = hex_asc_hi(bcberr >> 16);
  1161. ptr = pack_hex_byte(ptr, hx);
  1162. hx = hex_asc_lo(bcberr >> 16);
  1163. ptr = pack_hex_byte(ptr, hx);
  1164. hx = hex_asc_hi(bcberr >> 8);
  1165. ptr = pack_hex_byte(ptr, hx);
  1166. hx = hex_asc_lo(bcberr >> 8);
  1167. ptr = pack_hex_byte(ptr, hx);
  1168. hx = hex_asc_hi(bcberr);
  1169. ptr = pack_hex_byte(ptr, hx);
  1170. hx = hex_asc_lo(bcberr);
  1171. ptr = pack_hex_byte(ptr, hx);
  1172. ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
  1173. *ptr = 0;
  1174. putpacket(output_buffer); /* send it off... */
  1175. }
  1176. /*
  1177. * tell the debugger that an exception has occurred
  1178. */
  1179. ptr = output_buffer;
  1180. /*
  1181. * Send trap type (converted to signal)
  1182. */
  1183. *ptr++ = 'T';
  1184. ptr = pack_hex_byte(ptr, sigval);
  1185. /*
  1186. * Send Error PC
  1187. */
  1188. ptr = pack_hex_byte(ptr, GDB_REGID_PC);
  1189. *ptr++ = ':';
  1190. ptr = mem2hex(&regs->pc, ptr, 4, 0);
  1191. *ptr++ = ';';
  1192. /*
  1193. * Send frame pointer
  1194. */
  1195. ptr = pack_hex_byte(ptr, GDB_REGID_FP);
  1196. *ptr++ = ':';
  1197. ptr = mem2hex(&regs->a3, ptr, 4, 0);
  1198. *ptr++ = ';';
  1199. /*
  1200. * Send stack pointer
  1201. */
  1202. ssp = (unsigned long) (regs + 1);
  1203. ptr = pack_hex_byte(ptr, GDB_REGID_SP);
  1204. *ptr++ = ':';
  1205. ptr = mem2hex(&ssp, ptr, 4, 0);
  1206. *ptr++ = ';';
  1207. *ptr++ = 0;
  1208. putpacket(output_buffer); /* send it off... */
  1209. packet_waiting:
  1210. /*
  1211. * Wait for input from remote GDB
  1212. */
  1213. while (1) {
  1214. output_buffer[0] = 0;
  1215. getpacket(input_buffer);
  1216. switch (input_buffer[0]) {
  1217. /* request repeat of last signal number */
  1218. case '?':
  1219. output_buffer[0] = 'S';
  1220. output_buffer[1] = hex_asc_hi(sigval);
  1221. output_buffer[2] = hex_asc_lo(sigval);
  1222. output_buffer[3] = 0;
  1223. break;
  1224. case 'd':
  1225. /* toggle debug flag */
  1226. break;
  1227. /*
  1228. * Return the value of the CPU registers
  1229. */
  1230. case 'g':
  1231. zero = 0;
  1232. ssp = (u32) (regs + 1);
  1233. ptr = output_buffer;
  1234. ptr = mem2hex(&regs->d0, ptr, 4, 0);
  1235. ptr = mem2hex(&regs->d1, ptr, 4, 0);
  1236. ptr = mem2hex(&regs->d2, ptr, 4, 0);
  1237. ptr = mem2hex(&regs->d3, ptr, 4, 0);
  1238. ptr = mem2hex(&regs->a0, ptr, 4, 0);
  1239. ptr = mem2hex(&regs->a1, ptr, 4, 0);
  1240. ptr = mem2hex(&regs->a2, ptr, 4, 0);
  1241. ptr = mem2hex(&regs->a3, ptr, 4, 0);
  1242. ptr = mem2hex(&ssp, ptr, 4, 0); /* 8 */
  1243. ptr = mem2hex(&regs->pc, ptr, 4, 0);
  1244. ptr = mem2hex(&regs->mdr, ptr, 4, 0);
  1245. ptr = mem2hex(&regs->epsw, ptr, 4, 0);
  1246. ptr = mem2hex(&regs->lir, ptr, 4, 0);
  1247. ptr = mem2hex(&regs->lar, ptr, 4, 0);
  1248. ptr = mem2hex(&regs->mdrq, ptr, 4, 0);
  1249. ptr = mem2hex(&regs->e0, ptr, 4, 0); /* 15 */
  1250. ptr = mem2hex(&regs->e1, ptr, 4, 0);
  1251. ptr = mem2hex(&regs->e2, ptr, 4, 0);
  1252. ptr = mem2hex(&regs->e3, ptr, 4, 0);
  1253. ptr = mem2hex(&regs->e4, ptr, 4, 0);
  1254. ptr = mem2hex(&regs->e5, ptr, 4, 0);
  1255. ptr = mem2hex(&regs->e6, ptr, 4, 0);
  1256. ptr = mem2hex(&regs->e7, ptr, 4, 0);
  1257. ptr = mem2hex(&ssp, ptr, 4, 0);
  1258. ptr = mem2hex(&regs, ptr, 4, 0);
  1259. ptr = mem2hex(&regs->sp, ptr, 4, 0);
  1260. ptr = mem2hex(&regs->mcrh, ptr, 4, 0); /* 26 */
  1261. ptr = mem2hex(&regs->mcrl, ptr, 4, 0);
  1262. ptr = mem2hex(&regs->mcvf, ptr, 4, 0);
  1263. ptr = mem2hex(&gdbstub_fpcr, ptr, 4, 0); /* 29 - FPCR */
  1264. ptr = mem2hex(&zero, ptr, 4, 0);
  1265. ptr = mem2hex(&zero, ptr, 4, 0);
  1266. for (loop = 0; loop < 32; loop++)
  1267. ptr = mem2hex(&gdbstub_fpufs_array[loop],
  1268. ptr, 4, 0); /* 32 - FS0-31 */
  1269. break;
  1270. /*
  1271. * set the value of the CPU registers - return OK
  1272. */
  1273. case 'G':
  1274. {
  1275. const char *ptr;
  1276. ptr = &input_buffer[1];
  1277. ptr = hex2mem(ptr, &regs->d0, 4, 0);
  1278. ptr = hex2mem(ptr, &regs->d1, 4, 0);
  1279. ptr = hex2mem(ptr, &regs->d2, 4, 0);
  1280. ptr = hex2mem(ptr, &regs->d3, 4, 0);
  1281. ptr = hex2mem(ptr, &regs->a0, 4, 0);
  1282. ptr = hex2mem(ptr, &regs->a1, 4, 0);
  1283. ptr = hex2mem(ptr, &regs->a2, 4, 0);
  1284. ptr = hex2mem(ptr, &regs->a3, 4, 0);
  1285. ptr = hex2mem(ptr, &ssp, 4, 0); /* 8 */
  1286. ptr = hex2mem(ptr, &regs->pc, 4, 0);
  1287. ptr = hex2mem(ptr, &regs->mdr, 4, 0);
  1288. ptr = hex2mem(ptr, &regs->epsw, 4, 0);
  1289. ptr = hex2mem(ptr, &regs->lir, 4, 0);
  1290. ptr = hex2mem(ptr, &regs->lar, 4, 0);
  1291. ptr = hex2mem(ptr, &regs->mdrq, 4, 0);
  1292. ptr = hex2mem(ptr, &regs->e0, 4, 0); /* 15 */
  1293. ptr = hex2mem(ptr, &regs->e1, 4, 0);
  1294. ptr = hex2mem(ptr, &regs->e2, 4, 0);
  1295. ptr = hex2mem(ptr, &regs->e3, 4, 0);
  1296. ptr = hex2mem(ptr, &regs->e4, 4, 0);
  1297. ptr = hex2mem(ptr, &regs->e5, 4, 0);
  1298. ptr = hex2mem(ptr, &regs->e6, 4, 0);
  1299. ptr = hex2mem(ptr, &regs->e7, 4, 0);
  1300. ptr = hex2mem(ptr, &ssp, 4, 0);
  1301. ptr = hex2mem(ptr, &zero, 4, 0);
  1302. ptr = hex2mem(ptr, &regs->sp, 4, 0);
  1303. ptr = hex2mem(ptr, &regs->mcrh, 4, 0); /* 26 */
  1304. ptr = hex2mem(ptr, &regs->mcrl, 4, 0);
  1305. ptr = hex2mem(ptr, &regs->mcvf, 4, 0);
  1306. ptr = hex2mem(ptr, &zero, 4, 0); /* 29 - FPCR */
  1307. ptr = hex2mem(ptr, &zero, 4, 0);
  1308. ptr = hex2mem(ptr, &zero, 4, 0);
  1309. for (loop = 0; loop < 32; loop++) /* 32 - FS0-31 */
  1310. ptr = hex2mem(ptr, &zero, 4, 0);
  1311. #if 0
  1312. /*
  1313. * See if the stack pointer has moved. If so, then copy
  1314. * the saved locals and ins to the new location.
  1315. */
  1316. unsigned long *newsp = (unsigned long *) registers[SP];
  1317. if (sp != newsp)
  1318. sp = memcpy(newsp, sp, 16 * 4);
  1319. #endif
  1320. gdbstub_strcpy(output_buffer, "OK");
  1321. }
  1322. break;
  1323. /*
  1324. * mAA..AA,LLLL Read LLLL bytes at address AA..AA
  1325. */
  1326. case 'm':
  1327. ptr = &input_buffer[1];
  1328. if (hexToInt(&ptr, &addr) &&
  1329. *ptr++ == ',' &&
  1330. hexToInt(&ptr, &length)
  1331. ) {
  1332. if (mem2hex((char *) addr, output_buffer,
  1333. length, 1))
  1334. break;
  1335. gdbstub_strcpy(output_buffer, "E03");
  1336. } else {
  1337. gdbstub_strcpy(output_buffer, "E01");
  1338. }
  1339. break;
  1340. /*
  1341. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA
  1342. * return OK
  1343. */
  1344. case 'M':
  1345. ptr = &input_buffer[1];
  1346. if (hexToInt(&ptr, &addr) &&
  1347. *ptr++ == ',' &&
  1348. hexToInt(&ptr, &length) &&
  1349. *ptr++ == ':'
  1350. ) {
  1351. if (hex2mem(ptr, (char *) addr, length, 1))
  1352. gdbstub_strcpy(output_buffer, "OK");
  1353. else
  1354. gdbstub_strcpy(output_buffer, "E03");
  1355. gdbstub_flush_caches = 1;
  1356. } else {
  1357. gdbstub_strcpy(output_buffer, "E02");
  1358. }
  1359. break;
  1360. /*
  1361. * cAA..AA Continue at address AA..AA(optional)
  1362. */
  1363. case 'c':
  1364. /* try to read optional parameter, pc unchanged if no
  1365. * parm */
  1366. ptr = &input_buffer[1];
  1367. if (hexToInt(&ptr, &addr))
  1368. regs->pc = addr;
  1369. goto done;
  1370. /*
  1371. * kill the program
  1372. */
  1373. case 'k' :
  1374. goto done; /* just continue */
  1375. /*
  1376. * Reset the whole machine (FIXME: system dependent)
  1377. */
  1378. case 'r':
  1379. break;
  1380. /*
  1381. * Step to next instruction
  1382. */
  1383. case 's':
  1384. /*
  1385. * using the T flag doesn't seem to perform single
  1386. * stepping (it seems to wind up being caught by the
  1387. * JTAG unit), so we have to use breakpoints and
  1388. * continue instead.
  1389. */
  1390. if (gdbstub_single_step(regs) < 0)
  1391. /* ignore any fault error for now */
  1392. gdbstub_printk("unable to set single-step"
  1393. " bp\n");
  1394. goto done;
  1395. /*
  1396. * Set baud rate (bBB)
  1397. */
  1398. case 'b':
  1399. do {
  1400. int baudrate;
  1401. ptr = &input_buffer[1];
  1402. if (!hexToInt(&ptr, &baudrate)) {
  1403. gdbstub_strcpy(output_buffer, "B01");
  1404. break;
  1405. }
  1406. if (baudrate) {
  1407. /* ACK before changing speed */
  1408. putpacket("OK");
  1409. gdbstub_io_set_baud(baudrate);
  1410. }
  1411. } while (0);
  1412. break;
  1413. /*
  1414. * Set breakpoint
  1415. */
  1416. case 'Z':
  1417. ptr = &input_buffer[1];
  1418. if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
  1419. !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
  1420. !hexToInt(&ptr, &length)
  1421. ) {
  1422. gdbstub_strcpy(output_buffer, "E01");
  1423. break;
  1424. }
  1425. /* only support software breakpoints */
  1426. gdbstub_strcpy(output_buffer, "E03");
  1427. if (loop != 0 ||
  1428. length < 1 ||
  1429. length > 7 ||
  1430. (unsigned long) addr < 4096)
  1431. break;
  1432. if (gdbstub_set_breakpoint((u8 *) addr, length) < 0)
  1433. break;
  1434. gdbstub_strcpy(output_buffer, "OK");
  1435. break;
  1436. /*
  1437. * Clear breakpoint
  1438. */
  1439. case 'z':
  1440. ptr = &input_buffer[1];
  1441. if (!hexToInt(&ptr, &loop) || *ptr++ != ',' ||
  1442. !hexToInt(&ptr, &addr) || *ptr++ != ',' ||
  1443. !hexToInt(&ptr, &length)
  1444. ) {
  1445. gdbstub_strcpy(output_buffer, "E01");
  1446. break;
  1447. }
  1448. /* only support software breakpoints */
  1449. gdbstub_strcpy(output_buffer, "E03");
  1450. if (loop != 0 ||
  1451. length < 1 ||
  1452. length > 7 ||
  1453. (unsigned long) addr < 4096)
  1454. break;
  1455. if (gdbstub_clear_breakpoint((u8 *) addr, length) < 0)
  1456. break;
  1457. gdbstub_strcpy(output_buffer, "OK");
  1458. break;
  1459. default:
  1460. gdbstub_proto("### GDB Unsupported Cmd '%s'\n",
  1461. input_buffer);
  1462. break;
  1463. }
  1464. /* reply to the request */
  1465. putpacket(output_buffer);
  1466. }
  1467. done:
  1468. /*
  1469. * Need to flush the instruction cache here, as we may
  1470. * have deposited a breakpoint, and the icache probably
  1471. * has no way of knowing that a data ref to some location
  1472. * may have changed something that is in the instruction
  1473. * cache.
  1474. * NB: We flush both caches, just to be sure...
  1475. */
  1476. if (gdbstub_flush_caches)
  1477. gdbstub_purge_cache();
  1478. gdbstub_load_fpu();
  1479. mn10300_set_gdbleds(0);
  1480. if (excep == EXCEP_NMI)
  1481. NMICR = NMICR_NMIF;
  1482. touch_softlockup_watchdog();
  1483. local_irq_restore(epsw);
  1484. return 1;
  1485. }
  1486. /*
  1487. * handle event interception
  1488. */
  1489. asmlinkage int gdbstub_intercept(struct pt_regs *regs,
  1490. enum exception_code excep)
  1491. {
  1492. static u8 notfirst = 1;
  1493. int ret;
  1494. if (gdbstub_busy)
  1495. gdbstub_printk("--> gdbstub reentered itself\n");
  1496. gdbstub_busy = 1;
  1497. if (notfirst) {
  1498. unsigned long mdr;
  1499. asm("mov mdr,%0" : "=d"(mdr));
  1500. gdbstub_entry(
  1501. "--> gdbstub_intercept(%p,%04x) [MDR=%lx PC=%lx]\n",
  1502. regs, excep, mdr, regs->pc);
  1503. gdbstub_entry(
  1504. "PC: %08lx EPSW: %08lx SSP: %08lx mode: %s\n",
  1505. regs->pc, regs->epsw, (unsigned long) &ret,
  1506. user_mode(regs) ? "User" : "Super");
  1507. gdbstub_entry(
  1508. "d0: %08lx d1: %08lx d2: %08lx d3: %08lx\n",
  1509. regs->d0, regs->d1, regs->d2, regs->d3);
  1510. gdbstub_entry(
  1511. "a0: %08lx a1: %08lx a2: %08lx a3: %08lx\n",
  1512. regs->a0, regs->a1, regs->a2, regs->a3);
  1513. gdbstub_entry(
  1514. "e0: %08lx e1: %08lx e2: %08lx e3: %08lx\n",
  1515. regs->e0, regs->e1, regs->e2, regs->e3);
  1516. gdbstub_entry(
  1517. "e4: %08lx e5: %08lx e6: %08lx e7: %08lx\n",
  1518. regs->e4, regs->e5, regs->e6, regs->e7);
  1519. gdbstub_entry(
  1520. "lar: %08lx lir: %08lx mdr: %08lx usp: %08lx\n",
  1521. regs->lar, regs->lir, regs->mdr, regs->sp);
  1522. gdbstub_entry(
  1523. "cvf: %08lx crl: %08lx crh: %08lx drq: %08lx\n",
  1524. regs->mcvf, regs->mcrl, regs->mcrh, regs->mdrq);
  1525. gdbstub_entry(
  1526. "threadinfo=%p task=%p)\n",
  1527. current_thread_info(), current);
  1528. } else {
  1529. notfirst = 1;
  1530. }
  1531. ret = gdbstub(regs, excep);
  1532. gdbstub_entry("<-- gdbstub_intercept()\n");
  1533. gdbstub_busy = 0;
  1534. return ret;
  1535. }
  1536. /*
  1537. * handle the GDB stub itself causing an exception
  1538. */
  1539. asmlinkage void gdbstub_exception(struct pt_regs *regs,
  1540. enum exception_code excep)
  1541. {
  1542. unsigned long mdr;
  1543. asm("mov mdr,%0" : "=d"(mdr));
  1544. gdbstub_entry("--> gdbstub exception({%p},%04x) [MDR=%lx]\n",
  1545. regs, excep, mdr);
  1546. while ((unsigned long) regs == 0xffffffff) {}
  1547. /* handle guarded memory accesses where we know it might fault */
  1548. if (regs->pc == (unsigned) gdbstub_read_byte_guard) {
  1549. regs->pc = (unsigned) gdbstub_read_byte_cont;
  1550. goto fault;
  1551. }
  1552. if (regs->pc == (unsigned) gdbstub_read_word_guard) {
  1553. regs->pc = (unsigned) gdbstub_read_word_cont;
  1554. goto fault;
  1555. }
  1556. if (regs->pc == (unsigned) gdbstub_read_dword_guard) {
  1557. regs->pc = (unsigned) gdbstub_read_dword_cont;
  1558. goto fault;
  1559. }
  1560. if (regs->pc == (unsigned) gdbstub_write_byte_guard) {
  1561. regs->pc = (unsigned) gdbstub_write_byte_cont;
  1562. goto fault;
  1563. }
  1564. if (regs->pc == (unsigned) gdbstub_write_word_guard) {
  1565. regs->pc = (unsigned) gdbstub_write_word_cont;
  1566. goto fault;
  1567. }
  1568. if (regs->pc == (unsigned) gdbstub_write_dword_guard) {
  1569. regs->pc = (unsigned) gdbstub_write_dword_cont;
  1570. goto fault;
  1571. }
  1572. gdbstub_printk("\n### GDB stub caused an exception ###\n");
  1573. /* something went horribly wrong */
  1574. console_verbose();
  1575. show_registers(regs);
  1576. panic("GDB Stub caused an unexpected exception - can't continue\n");
  1577. /* we caught an attempt by the stub to access silly memory */
  1578. fault:
  1579. gdbstub_entry("<-- gdbstub exception() = EFAULT\n");
  1580. regs->d0 = -EFAULT;
  1581. return;
  1582. }
  1583. /*
  1584. * send an exit message to GDB
  1585. */
  1586. void gdbstub_exit(int status)
  1587. {
  1588. unsigned char checksum;
  1589. unsigned char ch;
  1590. int count;
  1591. gdbstub_busy = 1;
  1592. output_buffer[0] = 'W';
  1593. output_buffer[1] = hex_asc_hi(status);
  1594. output_buffer[2] = hex_asc_lo(status);
  1595. output_buffer[3] = 0;
  1596. gdbstub_io_tx_char('$');
  1597. checksum = 0;
  1598. count = 0;
  1599. while ((ch = output_buffer[count]) != 0) {
  1600. gdbstub_io_tx_char(ch);
  1601. checksum += ch;
  1602. count += 1;
  1603. }
  1604. gdbstub_io_tx_char('#');
  1605. gdbstub_io_tx_char(hex_asc_hi(checksum));
  1606. gdbstub_io_tx_char(hex_asc_lo(checksum));
  1607. /* make sure the output is flushed, or else RedBoot might clobber it */
  1608. gdbstub_io_tx_flush();
  1609. gdbstub_busy = 0;
  1610. }
  1611. /*
  1612. * initialise the GDB stub
  1613. */
  1614. asmlinkage void __init gdbstub_init(void)
  1615. {
  1616. #ifdef CONFIG_GDBSTUB_IMMEDIATE
  1617. unsigned char ch;
  1618. int ret;
  1619. #endif
  1620. gdbstub_busy = 1;
  1621. printk(KERN_INFO "%s", gdbstub_banner);
  1622. gdbstub_io_init();
  1623. gdbstub_entry("--> gdbstub_init\n");
  1624. /* try to talk to GDB (or anyone insane enough to want to type GDB
  1625. * protocol by hand) */
  1626. gdbstub_io("### GDB Tx ACK\n");
  1627. gdbstub_io_tx_char('+'); /* 'hello world' */
  1628. #ifdef CONFIG_GDBSTUB_IMMEDIATE
  1629. gdbstub_printk("GDB Stub waiting for packet\n");
  1630. /* in case GDB is started before us, ACK any packets that are already
  1631. * sitting there (presumably "$?#xx")
  1632. */
  1633. do { gdbstub_io_rx_char(&ch, 0); } while (ch != '$');
  1634. do { gdbstub_io_rx_char(&ch, 0); } while (ch != '#');
  1635. /* eat first csum byte */
  1636. do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
  1637. /* eat second csum byte */
  1638. do { ret = gdbstub_io_rx_char(&ch, 0); } while (ret != 0);
  1639. gdbstub_io("### GDB Tx NAK\n");
  1640. gdbstub_io_tx_char('-'); /* NAK it */
  1641. #else
  1642. printk("GDB Stub ready\n");
  1643. #endif
  1644. gdbstub_busy = 0;
  1645. gdbstub_entry("<-- gdbstub_init\n");
  1646. }
  1647. /*
  1648. * register the console at a more appropriate time
  1649. */
  1650. #ifdef CONFIG_GDBSTUB_CONSOLE
  1651. static int __init gdbstub_postinit(void)
  1652. {
  1653. printk(KERN_NOTICE "registering console\n");
  1654. register_console(&gdbstub_console);
  1655. return 0;
  1656. }
  1657. __initcall(gdbstub_postinit);
  1658. #endif
  1659. /*
  1660. * handle character reception on GDB serial port
  1661. * - jump into the GDB stub if BREAK is detected on the serial line
  1662. */
  1663. asmlinkage void gdbstub_rx_irq(struct pt_regs *regs, enum exception_code excep)
  1664. {
  1665. char ch;
  1666. int ret;
  1667. gdbstub_entry("--> gdbstub_rx_irq\n");
  1668. do {
  1669. ret = gdbstub_io_rx_char(&ch, 1);
  1670. if (ret != -EIO && ret != -EAGAIN) {
  1671. if (ret != -EINTR)
  1672. gdbstub_rx_unget = ch;
  1673. gdbstub(regs, excep);
  1674. }
  1675. } while (ret != -EAGAIN);
  1676. gdbstub_entry("<-- gdbstub_rx_irq\n");
  1677. }