gdb-stub.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /*
  2. * arch/mips/kernel/gdb-stub.c
  3. *
  4. * Originally written by Glenn Engel, Lake Stevens Instrument Division
  5. *
  6. * Contributed by HP Systems
  7. *
  8. * Modified for SPARC by Stu Grossman, Cygnus Support.
  9. *
  10. * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse
  11. * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de>
  12. *
  13. * Copyright (C) 1995 Andreas Busse
  14. *
  15. * Copyright (C) 2003 MontaVista Software Inc.
  16. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  17. */
  18. /*
  19. * To enable debugger support, two things need to happen. One, a
  20. * call to set_debug_traps() is necessary in order to allow any breakpoints
  21. * or error conditions to be properly intercepted and reported to gdb.
  22. * Two, a breakpoint needs to be generated to begin communication. This
  23. * is most easily accomplished by a call to breakpoint(). Breakpoint()
  24. * simulates a breakpoint by executing a BREAK instruction.
  25. *
  26. *
  27. * The following gdb commands are supported:
  28. *
  29. * command function Return value
  30. *
  31. * g return the value of the CPU registers hex data or ENN
  32. * G set the value of the CPU registers OK or ENN
  33. *
  34. * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
  35. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
  36. *
  37. * c Resume at current address SNN ( signal NN)
  38. * cAA..AA Continue at address AA..AA SNN
  39. *
  40. * s Step one instruction SNN
  41. * sAA..AA Step one instruction from AA..AA SNN
  42. *
  43. * k kill
  44. *
  45. * ? What was the last sigval ? SNN (signal NN)
  46. *
  47. * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
  48. * baud rate
  49. *
  50. * All commands and responses are sent with a packet which includes a
  51. * checksum. A packet consists of
  52. *
  53. * $<packet info>#<checksum>.
  54. *
  55. * where
  56. * <packet info> :: <characters representing the command or response>
  57. * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
  58. *
  59. * When a packet is received, it is first acknowledged with either '+' or '-'.
  60. * '+' indicates a successful transfer. '-' indicates a failed transfer.
  61. *
  62. * Example:
  63. *
  64. * Host: Reply:
  65. * $m0,10#2a +$00010203040506070809101112131415#42
  66. *
  67. *
  68. * ==============
  69. * MORE EXAMPLES:
  70. * ==============
  71. *
  72. * For reference -- the following are the steps that one
  73. * company took (RidgeRun Inc) to get remote gdb debugging
  74. * going. In this scenario the host machine was a PC and the
  75. * target platform was a Galileo EVB64120A MIPS evaluation
  76. * board.
  77. *
  78. * Step 1:
  79. * First download gdb-5.0.tar.gz from the internet.
  80. * and then build/install the package.
  81. *
  82. * Example:
  83. * $ tar zxf gdb-5.0.tar.gz
  84. * $ cd gdb-5.0
  85. * $ ./configure --target=mips-linux-elf
  86. * $ make
  87. * $ install
  88. * $ which mips-linux-elf-gdb
  89. * /usr/local/bin/mips-linux-elf-gdb
  90. *
  91. * Step 2:
  92. * Configure linux for remote debugging and build it.
  93. *
  94. * Example:
  95. * $ cd ~/linux
  96. * $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging>
  97. * $ make
  98. *
  99. * Step 3:
  100. * Download the kernel to the remote target and start
  101. * the kernel running. It will promptly halt and wait
  102. * for the host gdb session to connect. It does this
  103. * since the "Kernel Hacking" option has defined
  104. * CONFIG_KGDB which in turn enables your calls
  105. * to:
  106. * set_debug_traps();
  107. * breakpoint();
  108. *
  109. * Step 4:
  110. * Start the gdb session on the host.
  111. *
  112. * Example:
  113. * $ mips-linux-elf-gdb vmlinux
  114. * (gdb) set remotebaud 115200
  115. * (gdb) target remote /dev/ttyS1
  116. * ...at this point you are connected to
  117. * the remote target and can use gdb
  118. * in the normal fasion. Setting
  119. * breakpoints, single stepping,
  120. * printing variables, etc.
  121. */
  122. #include <linux/config.h>
  123. #include <linux/string.h>
  124. #include <linux/kernel.h>
  125. #include <linux/signal.h>
  126. #include <linux/sched.h>
  127. #include <linux/mm.h>
  128. #include <linux/console.h>
  129. #include <linux/init.h>
  130. #include <linux/smp.h>
  131. #include <linux/spinlock.h>
  132. #include <linux/slab.h>
  133. #include <linux/reboot.h>
  134. #include <asm/asm.h>
  135. #include <asm/cacheflush.h>
  136. #include <asm/mipsregs.h>
  137. #include <asm/pgtable.h>
  138. #include <asm/system.h>
  139. #include <asm/gdb-stub.h>
  140. #include <asm/inst.h>
  141. /*
  142. * external low-level support routines
  143. */
  144. extern int putDebugChar(char c); /* write a single character */
  145. extern char getDebugChar(void); /* read and return a single char */
  146. extern void trap_low(void);
  147. /*
  148. * breakpoint and test functions
  149. */
  150. extern void breakpoint(void);
  151. extern void breakinst(void);
  152. extern void async_breakpoint(void);
  153. extern void async_breakinst(void);
  154. extern void adel(void);
  155. /*
  156. * local prototypes
  157. */
  158. static void getpacket(char *buffer);
  159. static void putpacket(char *buffer);
  160. static int computeSignal(int tt);
  161. static int hex(unsigned char ch);
  162. static int hexToInt(char **ptr, int *intValue);
  163. static int hexToLong(char **ptr, long *longValue);
  164. static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault);
  165. void handle_exception(struct gdb_regs *regs);
  166. int kgdb_enabled;
  167. /*
  168. * spin locks for smp case
  169. */
  170. static spinlock_t kgdb_lock = SPIN_LOCK_UNLOCKED;
  171. static spinlock_t kgdb_cpulock[NR_CPUS] = { [0 ... NR_CPUS-1] = SPIN_LOCK_UNLOCKED};
  172. /*
  173. * BUFMAX defines the maximum number of characters in inbound/outbound buffers
  174. * at least NUMREGBYTES*2 are needed for register packets
  175. */
  176. #define BUFMAX 2048
  177. static char input_buffer[BUFMAX];
  178. static char output_buffer[BUFMAX];
  179. static int initialized; /* !0 means we've been initialized */
  180. static int kgdb_started;
  181. static const char hexchars[]="0123456789abcdef";
  182. /* Used to prevent crashes in memory access. Note that they'll crash anyway if
  183. we haven't set up fault handlers yet... */
  184. int kgdb_read_byte(unsigned char *address, unsigned char *dest);
  185. int kgdb_write_byte(unsigned char val, unsigned char *dest);
  186. /*
  187. * Convert ch from a hex digit to an int
  188. */
  189. static int hex(unsigned char ch)
  190. {
  191. if (ch >= 'a' && ch <= 'f')
  192. return ch-'a'+10;
  193. if (ch >= '0' && ch <= '9')
  194. return ch-'0';
  195. if (ch >= 'A' && ch <= 'F')
  196. return ch-'A'+10;
  197. return -1;
  198. }
  199. /*
  200. * scan for the sequence $<data>#<checksum>
  201. */
  202. static void getpacket(char *buffer)
  203. {
  204. unsigned char checksum;
  205. unsigned char xmitcsum;
  206. int i;
  207. int count;
  208. unsigned char ch;
  209. do {
  210. /*
  211. * wait around for the start character,
  212. * ignore all other characters
  213. */
  214. while ((ch = (getDebugChar() & 0x7f)) != '$') ;
  215. checksum = 0;
  216. xmitcsum = -1;
  217. count = 0;
  218. /*
  219. * now, read until a # or end of buffer is found
  220. */
  221. while (count < BUFMAX) {
  222. ch = getDebugChar();
  223. if (ch == '#')
  224. break;
  225. checksum = checksum + ch;
  226. buffer[count] = ch;
  227. count = count + 1;
  228. }
  229. if (count >= BUFMAX)
  230. continue;
  231. buffer[count] = 0;
  232. if (ch == '#') {
  233. xmitcsum = hex(getDebugChar() & 0x7f) << 4;
  234. xmitcsum |= hex(getDebugChar() & 0x7f);
  235. if (checksum != xmitcsum)
  236. putDebugChar('-'); /* failed checksum */
  237. else {
  238. putDebugChar('+'); /* successful transfer */
  239. /*
  240. * if a sequence char is present,
  241. * reply the sequence ID
  242. */
  243. if (buffer[2] == ':') {
  244. putDebugChar(buffer[0]);
  245. putDebugChar(buffer[1]);
  246. /*
  247. * remove sequence chars from buffer
  248. */
  249. count = strlen(buffer);
  250. for (i=3; i <= count; i++)
  251. buffer[i-3] = buffer[i];
  252. }
  253. }
  254. }
  255. }
  256. while (checksum != xmitcsum);
  257. }
  258. /*
  259. * send the packet in buffer.
  260. */
  261. static void putpacket(char *buffer)
  262. {
  263. unsigned char checksum;
  264. int count;
  265. unsigned char ch;
  266. /*
  267. * $<packet info>#<checksum>.
  268. */
  269. do {
  270. putDebugChar('$');
  271. checksum = 0;
  272. count = 0;
  273. while ((ch = buffer[count]) != 0) {
  274. if (!(putDebugChar(ch)))
  275. return;
  276. checksum += ch;
  277. count += 1;
  278. }
  279. putDebugChar('#');
  280. putDebugChar(hexchars[checksum >> 4]);
  281. putDebugChar(hexchars[checksum & 0xf]);
  282. }
  283. while ((getDebugChar() & 0x7f) != '+');
  284. }
  285. /*
  286. * Convert the memory pointed to by mem into hex, placing result in buf.
  287. * Return a pointer to the last char put in buf (null), in case of mem fault,
  288. * return 0.
  289. * may_fault is non-zero if we are reading from arbitrary memory, but is currently
  290. * not used.
  291. */
  292. static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault)
  293. {
  294. unsigned char ch;
  295. while (count-- > 0) {
  296. if (kgdb_read_byte(mem++, &ch) != 0)
  297. return 0;
  298. *buf++ = hexchars[ch >> 4];
  299. *buf++ = hexchars[ch & 0xf];
  300. }
  301. *buf = 0;
  302. return buf;
  303. }
  304. /*
  305. * convert the hex array pointed to by buf into binary to be placed in mem
  306. * return a pointer to the character AFTER the last byte written
  307. * may_fault is non-zero if we are reading from arbitrary memory, but is currently
  308. * not used.
  309. */
  310. static char *hex2mem(char *buf, char *mem, int count, int binary, int may_fault)
  311. {
  312. int i;
  313. unsigned char ch;
  314. for (i=0; i<count; i++)
  315. {
  316. if (binary) {
  317. ch = *buf++;
  318. if (ch == 0x7d)
  319. ch = 0x20 ^ *buf++;
  320. }
  321. else {
  322. ch = hex(*buf++) << 4;
  323. ch |= hex(*buf++);
  324. }
  325. if (kgdb_write_byte(ch, mem++) != 0)
  326. return 0;
  327. }
  328. return mem;
  329. }
  330. /*
  331. * This table contains the mapping between SPARC hardware trap types, and
  332. * signals, which are primarily what GDB understands. It also indicates
  333. * which hardware traps we need to commandeer when initializing the stub.
  334. */
  335. static struct hard_trap_info {
  336. unsigned char tt; /* Trap type code for MIPS R3xxx and R4xxx */
  337. unsigned char signo; /* Signal that we map this trap into */
  338. } hard_trap_info[] = {
  339. { 6, SIGBUS }, /* instruction bus error */
  340. { 7, SIGBUS }, /* data bus error */
  341. { 9, SIGTRAP }, /* break */
  342. { 10, SIGILL }, /* reserved instruction */
  343. /* { 11, SIGILL }, */ /* CPU unusable */
  344. { 12, SIGFPE }, /* overflow */
  345. { 13, SIGTRAP }, /* trap */
  346. { 14, SIGSEGV }, /* virtual instruction cache coherency */
  347. { 15, SIGFPE }, /* floating point exception */
  348. { 23, SIGSEGV }, /* watch */
  349. { 31, SIGSEGV }, /* virtual data cache coherency */
  350. { 0, 0} /* Must be last */
  351. };
  352. /* Save the normal trap handlers for user-mode traps. */
  353. void *saved_vectors[32];
  354. /*
  355. * Set up exception handlers for tracing and breakpoints
  356. */
  357. void set_debug_traps(void)
  358. {
  359. struct hard_trap_info *ht;
  360. unsigned long flags;
  361. unsigned char c;
  362. local_irq_save(flags);
  363. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  364. saved_vectors[ht->tt] = set_except_vector(ht->tt, trap_low);
  365. putDebugChar('+'); /* 'hello world' */
  366. /*
  367. * In case GDB is started before us, ack any packets
  368. * (presumably "$?#xx") sitting there.
  369. */
  370. while((c = getDebugChar()) != '$');
  371. while((c = getDebugChar()) != '#');
  372. c = getDebugChar(); /* eat first csum byte */
  373. c = getDebugChar(); /* eat second csum byte */
  374. putDebugChar('+'); /* ack it */
  375. initialized = 1;
  376. local_irq_restore(flags);
  377. }
  378. void restore_debug_traps(void)
  379. {
  380. struct hard_trap_info *ht;
  381. unsigned long flags;
  382. local_irq_save(flags);
  383. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  384. set_except_vector(ht->tt, saved_vectors[ht->tt]);
  385. local_irq_restore(flags);
  386. }
  387. /*
  388. * Convert the MIPS hardware trap type code to a Unix signal number.
  389. */
  390. static int computeSignal(int tt)
  391. {
  392. struct hard_trap_info *ht;
  393. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  394. if (ht->tt == tt)
  395. return ht->signo;
  396. return SIGHUP; /* default for things we don't know about */
  397. }
  398. /*
  399. * While we find nice hex chars, build an int.
  400. * Return number of chars processed.
  401. */
  402. static int hexToInt(char **ptr, int *intValue)
  403. {
  404. int numChars = 0;
  405. int hexValue;
  406. *intValue = 0;
  407. while (**ptr) {
  408. hexValue = hex(**ptr);
  409. if (hexValue < 0)
  410. break;
  411. *intValue = (*intValue << 4) | hexValue;
  412. numChars ++;
  413. (*ptr)++;
  414. }
  415. return (numChars);
  416. }
  417. static int hexToLong(char **ptr, long *longValue)
  418. {
  419. int numChars = 0;
  420. int hexValue;
  421. *longValue = 0;
  422. while (**ptr) {
  423. hexValue = hex(**ptr);
  424. if (hexValue < 0)
  425. break;
  426. *longValue = (*longValue << 4) | hexValue;
  427. numChars ++;
  428. (*ptr)++;
  429. }
  430. return numChars;
  431. }
  432. #if 0
  433. /*
  434. * Print registers (on target console)
  435. * Used only to debug the stub...
  436. */
  437. void show_gdbregs(struct gdb_regs * regs)
  438. {
  439. /*
  440. * Saved main processor registers
  441. */
  442. printk("$0 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  443. regs->reg0, regs->reg1, regs->reg2, regs->reg3,
  444. regs->reg4, regs->reg5, regs->reg6, regs->reg7);
  445. printk("$8 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  446. regs->reg8, regs->reg9, regs->reg10, regs->reg11,
  447. regs->reg12, regs->reg13, regs->reg14, regs->reg15);
  448. printk("$16: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  449. regs->reg16, regs->reg17, regs->reg18, regs->reg19,
  450. regs->reg20, regs->reg21, regs->reg22, regs->reg23);
  451. printk("$24: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  452. regs->reg24, regs->reg25, regs->reg26, regs->reg27,
  453. regs->reg28, regs->reg29, regs->reg30, regs->reg31);
  454. /*
  455. * Saved cp0 registers
  456. */
  457. printk("epc : %08lx\nStatus: %08lx\nCause : %08lx\n",
  458. regs->cp0_epc, regs->cp0_status, regs->cp0_cause);
  459. }
  460. #endif /* dead code */
  461. /*
  462. * We single-step by setting breakpoints. When an exception
  463. * is handled, we need to restore the instructions hoisted
  464. * when the breakpoints were set.
  465. *
  466. * This is where we save the original instructions.
  467. */
  468. static struct gdb_bp_save {
  469. unsigned long addr;
  470. unsigned int val;
  471. } step_bp[2];
  472. #define BP 0x0000000d /* break opcode */
  473. /*
  474. * Set breakpoint instructions for single stepping.
  475. */
  476. static void single_step(struct gdb_regs *regs)
  477. {
  478. union mips_instruction insn;
  479. unsigned long targ;
  480. int is_branch, is_cond, i;
  481. targ = regs->cp0_epc;
  482. insn.word = *(unsigned int *)targ;
  483. is_branch = is_cond = 0;
  484. switch (insn.i_format.opcode) {
  485. /*
  486. * jr and jalr are in r_format format.
  487. */
  488. case spec_op:
  489. switch (insn.r_format.func) {
  490. case jalr_op:
  491. case jr_op:
  492. targ = *(&regs->reg0 + insn.r_format.rs);
  493. is_branch = 1;
  494. break;
  495. }
  496. break;
  497. /*
  498. * This group contains:
  499. * bltz_op, bgez_op, bltzl_op, bgezl_op,
  500. * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
  501. */
  502. case bcond_op:
  503. is_branch = is_cond = 1;
  504. targ += 4 + (insn.i_format.simmediate << 2);
  505. break;
  506. /*
  507. * These are unconditional and in j_format.
  508. */
  509. case jal_op:
  510. case j_op:
  511. is_branch = 1;
  512. targ += 4;
  513. targ >>= 28;
  514. targ <<= 28;
  515. targ |= (insn.j_format.target << 2);
  516. break;
  517. /*
  518. * These are conditional.
  519. */
  520. case beq_op:
  521. case beql_op:
  522. case bne_op:
  523. case bnel_op:
  524. case blez_op:
  525. case blezl_op:
  526. case bgtz_op:
  527. case bgtzl_op:
  528. case cop0_op:
  529. case cop1_op:
  530. case cop2_op:
  531. case cop1x_op:
  532. is_branch = is_cond = 1;
  533. targ += 4 + (insn.i_format.simmediate << 2);
  534. break;
  535. }
  536. if (is_branch) {
  537. i = 0;
  538. if (is_cond && targ != (regs->cp0_epc + 8)) {
  539. step_bp[i].addr = regs->cp0_epc + 8;
  540. step_bp[i++].val = *(unsigned *)(regs->cp0_epc + 8);
  541. *(unsigned *)(regs->cp0_epc + 8) = BP;
  542. }
  543. step_bp[i].addr = targ;
  544. step_bp[i].val = *(unsigned *)targ;
  545. *(unsigned *)targ = BP;
  546. } else {
  547. step_bp[0].addr = regs->cp0_epc + 4;
  548. step_bp[0].val = *(unsigned *)(regs->cp0_epc + 4);
  549. *(unsigned *)(regs->cp0_epc + 4) = BP;
  550. }
  551. }
  552. /*
  553. * If asynchronously interrupted by gdb, then we need to set a breakpoint
  554. * at the interrupted instruction so that we wind up stopped with a
  555. * reasonable stack frame.
  556. */
  557. static struct gdb_bp_save async_bp;
  558. /*
  559. * Swap the interrupted EPC with our asynchronous breakpoint routine.
  560. * This is safer than stuffing the breakpoint in-place, since no cache
  561. * flushes (or resulting smp_call_functions) are required. The
  562. * assumption is that only one CPU will be handling asynchronous bp's,
  563. * and only one can be active at a time.
  564. */
  565. extern spinlock_t smp_call_lock;
  566. void set_async_breakpoint(unsigned long *epc)
  567. {
  568. /* skip breaking into userland */
  569. if ((*epc & 0x80000000) == 0)
  570. return;
  571. /* avoid deadlock if someone is make IPC */
  572. if (spin_is_locked(&smp_call_lock))
  573. return;
  574. async_bp.addr = *epc;
  575. *epc = (unsigned long)async_breakpoint;
  576. }
  577. void kgdb_wait(void *arg)
  578. {
  579. unsigned flags;
  580. int cpu = smp_processor_id();
  581. local_irq_save(flags);
  582. spin_lock(&kgdb_cpulock[cpu]);
  583. spin_unlock(&kgdb_cpulock[cpu]);
  584. local_irq_restore(flags);
  585. }
  586. /*
  587. * This function does all command processing for interfacing to gdb. It
  588. * returns 1 if you should skip the instruction at the trap address, 0
  589. * otherwise.
  590. */
  591. void handle_exception (struct gdb_regs *regs)
  592. {
  593. int trap; /* Trap type */
  594. int sigval;
  595. long addr;
  596. int length;
  597. char *ptr;
  598. unsigned long *stack;
  599. int i;
  600. int bflag = 0;
  601. kgdb_started = 1;
  602. /*
  603. * acquire the big kgdb spinlock
  604. */
  605. if (!spin_trylock(&kgdb_lock)) {
  606. /*
  607. * some other CPU has the lock, we should go back to
  608. * receive the gdb_wait IPC
  609. */
  610. return;
  611. }
  612. /*
  613. * If we're in async_breakpoint(), restore the real EPC from
  614. * the breakpoint.
  615. */
  616. if (regs->cp0_epc == (unsigned long)async_breakinst) {
  617. regs->cp0_epc = async_bp.addr;
  618. async_bp.addr = 0;
  619. }
  620. /*
  621. * acquire the CPU spinlocks
  622. */
  623. for (i = num_online_cpus()-1; i >= 0; i--)
  624. if (spin_trylock(&kgdb_cpulock[i]) == 0)
  625. panic("kgdb: couldn't get cpulock %d\n", i);
  626. /*
  627. * force other cpus to enter kgdb
  628. */
  629. smp_call_function(kgdb_wait, NULL, 0, 0);
  630. /*
  631. * If we're in breakpoint() increment the PC
  632. */
  633. trap = (regs->cp0_cause & 0x7c) >> 2;
  634. if (trap == 9 && regs->cp0_epc == (unsigned long)breakinst)
  635. regs->cp0_epc += 4;
  636. /*
  637. * If we were single_stepping, restore the opcodes hoisted
  638. * for the breakpoint[s].
  639. */
  640. if (step_bp[0].addr) {
  641. *(unsigned *)step_bp[0].addr = step_bp[0].val;
  642. step_bp[0].addr = 0;
  643. if (step_bp[1].addr) {
  644. *(unsigned *)step_bp[1].addr = step_bp[1].val;
  645. step_bp[1].addr = 0;
  646. }
  647. }
  648. stack = (long *)regs->reg29; /* stack ptr */
  649. sigval = computeSignal(trap);
  650. /*
  651. * reply to host that an exception has occurred
  652. */
  653. ptr = output_buffer;
  654. /*
  655. * Send trap type (converted to signal)
  656. */
  657. *ptr++ = 'T';
  658. *ptr++ = hexchars[sigval >> 4];
  659. *ptr++ = hexchars[sigval & 0xf];
  660. /*
  661. * Send Error PC
  662. */
  663. *ptr++ = hexchars[REG_EPC >> 4];
  664. *ptr++ = hexchars[REG_EPC & 0xf];
  665. *ptr++ = ':';
  666. ptr = mem2hex((char *)&regs->cp0_epc, ptr, sizeof(long), 0);
  667. *ptr++ = ';';
  668. /*
  669. * Send frame pointer
  670. */
  671. *ptr++ = hexchars[REG_FP >> 4];
  672. *ptr++ = hexchars[REG_FP & 0xf];
  673. *ptr++ = ':';
  674. ptr = mem2hex((char *)&regs->reg30, ptr, sizeof(long), 0);
  675. *ptr++ = ';';
  676. /*
  677. * Send stack pointer
  678. */
  679. *ptr++ = hexchars[REG_SP >> 4];
  680. *ptr++ = hexchars[REG_SP & 0xf];
  681. *ptr++ = ':';
  682. ptr = mem2hex((char *)&regs->reg29, ptr, sizeof(long), 0);
  683. *ptr++ = ';';
  684. *ptr++ = 0;
  685. putpacket(output_buffer); /* send it off... */
  686. /*
  687. * Wait for input from remote GDB
  688. */
  689. while (1) {
  690. output_buffer[0] = 0;
  691. getpacket(input_buffer);
  692. switch (input_buffer[0])
  693. {
  694. case '?':
  695. output_buffer[0] = 'S';
  696. output_buffer[1] = hexchars[sigval >> 4];
  697. output_buffer[2] = hexchars[sigval & 0xf];
  698. output_buffer[3] = 0;
  699. break;
  700. /*
  701. * Detach debugger; let CPU run
  702. */
  703. case 'D':
  704. putpacket(output_buffer);
  705. goto finish_kgdb;
  706. break;
  707. case 'd':
  708. /* toggle debug flag */
  709. break;
  710. /*
  711. * Return the value of the CPU registers
  712. */
  713. case 'g':
  714. ptr = output_buffer;
  715. ptr = mem2hex((char *)&regs->reg0, ptr, 32*sizeof(long), 0); /* r0...r31 */
  716. ptr = mem2hex((char *)&regs->cp0_status, ptr, 6*sizeof(long), 0); /* cp0 */
  717. ptr = mem2hex((char *)&regs->fpr0, ptr, 32*sizeof(long), 0); /* f0...31 */
  718. ptr = mem2hex((char *)&regs->cp1_fsr, ptr, 2*sizeof(long), 0); /* cp1 */
  719. ptr = mem2hex((char *)&regs->frame_ptr, ptr, 2*sizeof(long), 0); /* frp */
  720. ptr = mem2hex((char *)&regs->cp0_index, ptr, 16*sizeof(long), 0); /* cp0 */
  721. break;
  722. /*
  723. * set the value of the CPU registers - return OK
  724. */
  725. case 'G':
  726. {
  727. ptr = &input_buffer[1];
  728. hex2mem(ptr, (char *)&regs->reg0, 32*sizeof(long), 0, 0);
  729. ptr += 32*(2*sizeof(long));
  730. hex2mem(ptr, (char *)&regs->cp0_status, 6*sizeof(long), 0, 0);
  731. ptr += 6*(2*sizeof(long));
  732. hex2mem(ptr, (char *)&regs->fpr0, 32*sizeof(long), 0, 0);
  733. ptr += 32*(2*sizeof(long));
  734. hex2mem(ptr, (char *)&regs->cp1_fsr, 2*sizeof(long), 0, 0);
  735. ptr += 2*(2*sizeof(long));
  736. hex2mem(ptr, (char *)&regs->frame_ptr, 2*sizeof(long), 0, 0);
  737. ptr += 2*(2*sizeof(long));
  738. hex2mem(ptr, (char *)&regs->cp0_index, 16*sizeof(long), 0, 0);
  739. strcpy(output_buffer,"OK");
  740. }
  741. break;
  742. /*
  743. * mAA..AA,LLLL Read LLLL bytes at address AA..AA
  744. */
  745. case 'm':
  746. ptr = &input_buffer[1];
  747. if (hexToLong(&ptr, &addr)
  748. && *ptr++ == ','
  749. && hexToInt(&ptr, &length)) {
  750. if (mem2hex((char *)addr, output_buffer, length, 1))
  751. break;
  752. strcpy (output_buffer, "E03");
  753. } else
  754. strcpy(output_buffer,"E01");
  755. break;
  756. /*
  757. * XAA..AA,LLLL: Write LLLL escaped binary bytes at address AA.AA
  758. */
  759. case 'X':
  760. bflag = 1;
  761. /* fall through */
  762. /*
  763. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK
  764. */
  765. case 'M':
  766. ptr = &input_buffer[1];
  767. if (hexToLong(&ptr, &addr)
  768. && *ptr++ == ','
  769. && hexToInt(&ptr, &length)
  770. && *ptr++ == ':') {
  771. if (hex2mem(ptr, (char *)addr, length, bflag, 1))
  772. strcpy(output_buffer, "OK");
  773. else
  774. strcpy(output_buffer, "E03");
  775. }
  776. else
  777. strcpy(output_buffer, "E02");
  778. break;
  779. /*
  780. * cAA..AA Continue at address AA..AA(optional)
  781. */
  782. case 'c':
  783. /* try to read optional parameter, pc unchanged if no parm */
  784. ptr = &input_buffer[1];
  785. if (hexToLong(&ptr, &addr))
  786. regs->cp0_epc = addr;
  787. goto exit_kgdb_exception;
  788. break;
  789. /*
  790. * kill the program; let us try to restart the machine
  791. * Reset the whole machine.
  792. */
  793. case 'k':
  794. case 'r':
  795. machine_restart("kgdb restarts machine");
  796. break;
  797. /*
  798. * Step to next instruction
  799. */
  800. case 's':
  801. /*
  802. * There is no single step insn in the MIPS ISA, so we
  803. * use breakpoints and continue, instead.
  804. */
  805. single_step(regs);
  806. goto exit_kgdb_exception;
  807. /* NOTREACHED */
  808. break;
  809. /*
  810. * Set baud rate (bBB)
  811. * FIXME: Needs to be written
  812. */
  813. case 'b':
  814. {
  815. #if 0
  816. int baudrate;
  817. extern void set_timer_3();
  818. ptr = &input_buffer[1];
  819. if (!hexToInt(&ptr, &baudrate))
  820. {
  821. strcpy(output_buffer,"B01");
  822. break;
  823. }
  824. /* Convert baud rate to uart clock divider */
  825. switch (baudrate)
  826. {
  827. case 38400:
  828. baudrate = 16;
  829. break;
  830. case 19200:
  831. baudrate = 33;
  832. break;
  833. case 9600:
  834. baudrate = 65;
  835. break;
  836. default:
  837. baudrate = 0;
  838. strcpy(output_buffer,"B02");
  839. goto x1;
  840. }
  841. if (baudrate) {
  842. putpacket("OK"); /* Ack before changing speed */
  843. set_timer_3(baudrate); /* Set it */
  844. }
  845. #endif
  846. }
  847. break;
  848. } /* switch */
  849. /*
  850. * reply to the request
  851. */
  852. putpacket(output_buffer);
  853. } /* while */
  854. return;
  855. finish_kgdb:
  856. restore_debug_traps();
  857. exit_kgdb_exception:
  858. /* release locks so other CPUs can go */
  859. for (i = num_online_cpus()-1; i >= 0; i--)
  860. spin_unlock(&kgdb_cpulock[i]);
  861. spin_unlock(&kgdb_lock);
  862. __flush_cache_all();
  863. return;
  864. }
  865. /*
  866. * This function will generate a breakpoint exception. It is used at the
  867. * beginning of a program to sync up with a debugger and can be used
  868. * otherwise as a quick means to stop program execution and "break" into
  869. * the debugger.
  870. */
  871. void breakpoint(void)
  872. {
  873. if (!initialized)
  874. return;
  875. __asm__ __volatile__(
  876. ".globl breakinst\n\t"
  877. ".set\tnoreorder\n\t"
  878. "nop\n"
  879. "breakinst:\tbreak\n\t"
  880. "nop\n\t"
  881. ".set\treorder"
  882. );
  883. }
  884. /* Nothing but the break; don't pollute any registers */
  885. void async_breakpoint(void)
  886. {
  887. __asm__ __volatile__(
  888. ".globl async_breakinst\n\t"
  889. ".set\tnoreorder\n\t"
  890. "nop\n"
  891. "async_breakinst:\tbreak\n\t"
  892. "nop\n\t"
  893. ".set\treorder"
  894. );
  895. }
  896. void adel(void)
  897. {
  898. __asm__ __volatile__(
  899. ".globl\tadel\n\t"
  900. "lui\t$8,0x8000\n\t"
  901. "lw\t$9,1($8)\n\t"
  902. );
  903. }
  904. /*
  905. * malloc is needed by gdb client in "call func()", even a private one
  906. * will make gdb happy
  907. */
  908. static void *malloc(size_t size)
  909. {
  910. return kmalloc(size, GFP_ATOMIC);
  911. }
  912. static void free(void *where)
  913. {
  914. kfree(where);
  915. }
  916. #ifdef CONFIG_GDB_CONSOLE
  917. void gdb_putsn(const char *str, int l)
  918. {
  919. char outbuf[18];
  920. if (!kgdb_started)
  921. return;
  922. outbuf[0]='O';
  923. while(l) {
  924. int i = (l>8)?8:l;
  925. mem2hex((char *)str, &outbuf[1], i, 0);
  926. outbuf[(i*2)+1]=0;
  927. putpacket(outbuf);
  928. str += i;
  929. l -= i;
  930. }
  931. }
  932. static void gdb_console_write(struct console *con, const char *s, unsigned n)
  933. {
  934. gdb_putsn(s, n);
  935. }
  936. static struct console gdb_console = {
  937. .name = "gdb",
  938. .write = gdb_console_write,
  939. .flags = CON_PRINTBUFFER,
  940. .index = -1
  941. };
  942. static int __init register_gdb_console(void)
  943. {
  944. register_console(&gdb_console);
  945. return 0;
  946. }
  947. console_initcall(register_gdb_console);
  948. #endif