gdb-stub.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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/string.h>
  123. #include <linux/kernel.h>
  124. #include <linux/signal.h>
  125. #include <linux/sched.h>
  126. #include <linux/mm.h>
  127. #include <linux/console.h>
  128. #include <linux/init.h>
  129. #include <linux/smp.h>
  130. #include <linux/spinlock.h>
  131. #include <linux/slab.h>
  132. #include <linux/reboot.h>
  133. #include <asm/asm.h>
  134. #include <asm/cacheflush.h>
  135. #include <asm/mipsregs.h>
  136. #include <asm/pgtable.h>
  137. #include <asm/system.h>
  138. #include <asm/gdb-stub.h>
  139. #include <asm/inst.h>
  140. #include <asm/smp.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 DEFINE_SPINLOCK(kgdb_lock);
  171. static raw_spinlock_t kgdb_cpulock[NR_CPUS] = {
  172. [0 ... NR_CPUS-1] = __RAW_SPIN_LOCK_UNLOCKED,
  173. };
  174. /*
  175. * BUFMAX defines the maximum number of characters in inbound/outbound buffers
  176. * at least NUMREGBYTES*2 are needed for register packets
  177. */
  178. #define BUFMAX 2048
  179. static char input_buffer[BUFMAX];
  180. static char output_buffer[BUFMAX];
  181. static int initialized; /* !0 means we've been initialized */
  182. static int kgdb_started;
  183. static const char hexchars[]="0123456789abcdef";
  184. /* Used to prevent crashes in memory access. Note that they'll crash anyway if
  185. we haven't set up fault handlers yet... */
  186. int kgdb_read_byte(unsigned char *address, unsigned char *dest);
  187. int kgdb_write_byte(unsigned char val, unsigned char *dest);
  188. /*
  189. * Convert ch from a hex digit to an int
  190. */
  191. static int hex(unsigned char ch)
  192. {
  193. if (ch >= 'a' && ch <= 'f')
  194. return ch-'a'+10;
  195. if (ch >= '0' && ch <= '9')
  196. return ch-'0';
  197. if (ch >= 'A' && ch <= 'F')
  198. return ch-'A'+10;
  199. return -1;
  200. }
  201. /*
  202. * scan for the sequence $<data>#<checksum>
  203. */
  204. static void getpacket(char *buffer)
  205. {
  206. unsigned char checksum;
  207. unsigned char xmitcsum;
  208. int i;
  209. int count;
  210. unsigned char ch;
  211. do {
  212. /*
  213. * wait around for the start character,
  214. * ignore all other characters
  215. */
  216. while ((ch = (getDebugChar() & 0x7f)) != '$') ;
  217. checksum = 0;
  218. xmitcsum = -1;
  219. count = 0;
  220. /*
  221. * now, read until a # or end of buffer is found
  222. */
  223. while (count < BUFMAX) {
  224. ch = getDebugChar();
  225. if (ch == '#')
  226. break;
  227. checksum = checksum + ch;
  228. buffer[count] = ch;
  229. count = count + 1;
  230. }
  231. if (count >= BUFMAX)
  232. continue;
  233. buffer[count] = 0;
  234. if (ch == '#') {
  235. xmitcsum = hex(getDebugChar() & 0x7f) << 4;
  236. xmitcsum |= hex(getDebugChar() & 0x7f);
  237. if (checksum != xmitcsum)
  238. putDebugChar('-'); /* failed checksum */
  239. else {
  240. putDebugChar('+'); /* successful transfer */
  241. /*
  242. * if a sequence char is present,
  243. * reply the sequence ID
  244. */
  245. if (buffer[2] == ':') {
  246. putDebugChar(buffer[0]);
  247. putDebugChar(buffer[1]);
  248. /*
  249. * remove sequence chars from buffer
  250. */
  251. count = strlen(buffer);
  252. for (i=3; i <= count; i++)
  253. buffer[i-3] = buffer[i];
  254. }
  255. }
  256. }
  257. }
  258. while (checksum != xmitcsum);
  259. }
  260. /*
  261. * send the packet in buffer.
  262. */
  263. static void putpacket(char *buffer)
  264. {
  265. unsigned char checksum;
  266. int count;
  267. unsigned char ch;
  268. /*
  269. * $<packet info>#<checksum>.
  270. */
  271. do {
  272. putDebugChar('$');
  273. checksum = 0;
  274. count = 0;
  275. while ((ch = buffer[count]) != 0) {
  276. if (!(putDebugChar(ch)))
  277. return;
  278. checksum += ch;
  279. count += 1;
  280. }
  281. putDebugChar('#');
  282. putDebugChar(hexchars[checksum >> 4]);
  283. putDebugChar(hexchars[checksum & 0xf]);
  284. }
  285. while ((getDebugChar() & 0x7f) != '+');
  286. }
  287. /*
  288. * Convert the memory pointed to by mem into hex, placing result in buf.
  289. * Return a pointer to the last char put in buf (null), in case of mem fault,
  290. * return 0.
  291. * may_fault is non-zero if we are reading from arbitrary memory, but is currently
  292. * not used.
  293. */
  294. static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault)
  295. {
  296. unsigned char ch;
  297. while (count-- > 0) {
  298. if (kgdb_read_byte(mem++, &ch) != 0)
  299. return 0;
  300. *buf++ = hexchars[ch >> 4];
  301. *buf++ = hexchars[ch & 0xf];
  302. }
  303. *buf = 0;
  304. return buf;
  305. }
  306. /*
  307. * convert the hex array pointed to by buf into binary to be placed in mem
  308. * return a pointer to the character AFTER the last byte written
  309. * may_fault is non-zero if we are reading from arbitrary memory, but is currently
  310. * not used.
  311. */
  312. static char *hex2mem(char *buf, char *mem, int count, int binary, int may_fault)
  313. {
  314. int i;
  315. unsigned char ch;
  316. for (i=0; i<count; i++)
  317. {
  318. if (binary) {
  319. ch = *buf++;
  320. if (ch == 0x7d)
  321. ch = 0x20 ^ *buf++;
  322. }
  323. else {
  324. ch = hex(*buf++) << 4;
  325. ch |= hex(*buf++);
  326. }
  327. if (kgdb_write_byte(ch, mem++) != 0)
  328. return 0;
  329. }
  330. return mem;
  331. }
  332. /*
  333. * This table contains the mapping between SPARC hardware trap types, and
  334. * signals, which are primarily what GDB understands. It also indicates
  335. * which hardware traps we need to commandeer when initializing the stub.
  336. */
  337. static struct hard_trap_info {
  338. unsigned char tt; /* Trap type code for MIPS R3xxx and R4xxx */
  339. unsigned char signo; /* Signal that we map this trap into */
  340. } hard_trap_info[] = {
  341. { 6, SIGBUS }, /* instruction bus error */
  342. { 7, SIGBUS }, /* data bus error */
  343. { 9, SIGTRAP }, /* break */
  344. { 10, SIGILL }, /* reserved instruction */
  345. /* { 11, SIGILL }, */ /* CPU unusable */
  346. { 12, SIGFPE }, /* overflow */
  347. { 13, SIGTRAP }, /* trap */
  348. { 14, SIGSEGV }, /* virtual instruction cache coherency */
  349. { 15, SIGFPE }, /* floating point exception */
  350. { 23, SIGSEGV }, /* watch */
  351. { 31, SIGSEGV }, /* virtual data cache coherency */
  352. { 0, 0} /* Must be last */
  353. };
  354. /* Save the normal trap handlers for user-mode traps. */
  355. void *saved_vectors[32];
  356. /*
  357. * Set up exception handlers for tracing and breakpoints
  358. */
  359. void set_debug_traps(void)
  360. {
  361. struct hard_trap_info *ht;
  362. unsigned long flags;
  363. unsigned char c;
  364. local_irq_save(flags);
  365. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  366. saved_vectors[ht->tt] = set_except_vector(ht->tt, trap_low);
  367. putDebugChar('+'); /* 'hello world' */
  368. /*
  369. * In case GDB is started before us, ack any packets
  370. * (presumably "$?#xx") sitting there.
  371. */
  372. while((c = getDebugChar()) != '$');
  373. while((c = getDebugChar()) != '#');
  374. c = getDebugChar(); /* eat first csum byte */
  375. c = getDebugChar(); /* eat second csum byte */
  376. putDebugChar('+'); /* ack it */
  377. initialized = 1;
  378. local_irq_restore(flags);
  379. }
  380. void restore_debug_traps(void)
  381. {
  382. struct hard_trap_info *ht;
  383. unsigned long flags;
  384. local_irq_save(flags);
  385. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  386. set_except_vector(ht->tt, saved_vectors[ht->tt]);
  387. local_irq_restore(flags);
  388. }
  389. /*
  390. * Convert the MIPS hardware trap type code to a Unix signal number.
  391. */
  392. static int computeSignal(int tt)
  393. {
  394. struct hard_trap_info *ht;
  395. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  396. if (ht->tt == tt)
  397. return ht->signo;
  398. return SIGHUP; /* default for things we don't know about */
  399. }
  400. /*
  401. * While we find nice hex chars, build an int.
  402. * Return number of chars processed.
  403. */
  404. static int hexToInt(char **ptr, int *intValue)
  405. {
  406. int numChars = 0;
  407. int hexValue;
  408. *intValue = 0;
  409. while (**ptr) {
  410. hexValue = hex(**ptr);
  411. if (hexValue < 0)
  412. break;
  413. *intValue = (*intValue << 4) | hexValue;
  414. numChars ++;
  415. (*ptr)++;
  416. }
  417. return (numChars);
  418. }
  419. static int hexToLong(char **ptr, long *longValue)
  420. {
  421. int numChars = 0;
  422. int hexValue;
  423. *longValue = 0;
  424. while (**ptr) {
  425. hexValue = hex(**ptr);
  426. if (hexValue < 0)
  427. break;
  428. *longValue = (*longValue << 4) | hexValue;
  429. numChars ++;
  430. (*ptr)++;
  431. }
  432. return numChars;
  433. }
  434. #if 0
  435. /*
  436. * Print registers (on target console)
  437. * Used only to debug the stub...
  438. */
  439. void show_gdbregs(struct gdb_regs * regs)
  440. {
  441. /*
  442. * Saved main processor registers
  443. */
  444. printk("$0 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  445. regs->reg0, regs->reg1, regs->reg2, regs->reg3,
  446. regs->reg4, regs->reg5, regs->reg6, regs->reg7);
  447. printk("$8 : %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  448. regs->reg8, regs->reg9, regs->reg10, regs->reg11,
  449. regs->reg12, regs->reg13, regs->reg14, regs->reg15);
  450. printk("$16: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  451. regs->reg16, regs->reg17, regs->reg18, regs->reg19,
  452. regs->reg20, regs->reg21, regs->reg22, regs->reg23);
  453. printk("$24: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
  454. regs->reg24, regs->reg25, regs->reg26, regs->reg27,
  455. regs->reg28, regs->reg29, regs->reg30, regs->reg31);
  456. /*
  457. * Saved cp0 registers
  458. */
  459. printk("epc : %08lx\nStatus: %08lx\nCause : %08lx\n",
  460. regs->cp0_epc, regs->cp0_status, regs->cp0_cause);
  461. }
  462. #endif /* dead code */
  463. /*
  464. * We single-step by setting breakpoints. When an exception
  465. * is handled, we need to restore the instructions hoisted
  466. * when the breakpoints were set.
  467. *
  468. * This is where we save the original instructions.
  469. */
  470. static struct gdb_bp_save {
  471. unsigned long addr;
  472. unsigned int val;
  473. } step_bp[2];
  474. #define BP 0x0000000d /* break opcode */
  475. /*
  476. * Set breakpoint instructions for single stepping.
  477. */
  478. static void single_step(struct gdb_regs *regs)
  479. {
  480. union mips_instruction insn;
  481. unsigned long targ;
  482. int is_branch, is_cond, i;
  483. targ = regs->cp0_epc;
  484. insn.word = *(unsigned int *)targ;
  485. is_branch = is_cond = 0;
  486. switch (insn.i_format.opcode) {
  487. /*
  488. * jr and jalr are in r_format format.
  489. */
  490. case spec_op:
  491. switch (insn.r_format.func) {
  492. case jalr_op:
  493. case jr_op:
  494. targ = *(&regs->reg0 + insn.r_format.rs);
  495. is_branch = 1;
  496. break;
  497. }
  498. break;
  499. /*
  500. * This group contains:
  501. * bltz_op, bgez_op, bltzl_op, bgezl_op,
  502. * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
  503. */
  504. case bcond_op:
  505. is_branch = is_cond = 1;
  506. targ += 4 + (insn.i_format.simmediate << 2);
  507. break;
  508. /*
  509. * These are unconditional and in j_format.
  510. */
  511. case jal_op:
  512. case j_op:
  513. is_branch = 1;
  514. targ += 4;
  515. targ >>= 28;
  516. targ <<= 28;
  517. targ |= (insn.j_format.target << 2);
  518. break;
  519. /*
  520. * These are conditional.
  521. */
  522. case beq_op:
  523. case beql_op:
  524. case bne_op:
  525. case bnel_op:
  526. case blez_op:
  527. case blezl_op:
  528. case bgtz_op:
  529. case bgtzl_op:
  530. case cop0_op:
  531. case cop1_op:
  532. case cop2_op:
  533. case cop1x_op:
  534. is_branch = is_cond = 1;
  535. targ += 4 + (insn.i_format.simmediate << 2);
  536. break;
  537. }
  538. if (is_branch) {
  539. i = 0;
  540. if (is_cond && targ != (regs->cp0_epc + 8)) {
  541. step_bp[i].addr = regs->cp0_epc + 8;
  542. step_bp[i++].val = *(unsigned *)(regs->cp0_epc + 8);
  543. *(unsigned *)(regs->cp0_epc + 8) = BP;
  544. }
  545. step_bp[i].addr = targ;
  546. step_bp[i].val = *(unsigned *)targ;
  547. *(unsigned *)targ = BP;
  548. } else {
  549. step_bp[0].addr = regs->cp0_epc + 4;
  550. step_bp[0].val = *(unsigned *)(regs->cp0_epc + 4);
  551. *(unsigned *)(regs->cp0_epc + 4) = BP;
  552. }
  553. }
  554. /*
  555. * If asynchronously interrupted by gdb, then we need to set a breakpoint
  556. * at the interrupted instruction so that we wind up stopped with a
  557. * reasonable stack frame.
  558. */
  559. static struct gdb_bp_save async_bp;
  560. /*
  561. * Swap the interrupted EPC with our asynchronous breakpoint routine.
  562. * This is safer than stuffing the breakpoint in-place, since no cache
  563. * flushes (or resulting smp_call_functions) are required. The
  564. * assumption is that only one CPU will be handling asynchronous bp's,
  565. * and only one can be active at a time.
  566. */
  567. extern spinlock_t smp_call_lock;
  568. void set_async_breakpoint(unsigned long *epc)
  569. {
  570. /* skip breaking into userland */
  571. if ((*epc & 0x80000000) == 0)
  572. return;
  573. #ifdef CONFIG_SMP
  574. /* avoid deadlock if someone is make IPC */
  575. if (spin_is_locked(&smp_call_lock))
  576. return;
  577. #endif
  578. async_bp.addr = *epc;
  579. *epc = (unsigned long)async_breakpoint;
  580. }
  581. static void kgdb_wait(void *arg)
  582. {
  583. unsigned flags;
  584. int cpu = smp_processor_id();
  585. local_irq_save(flags);
  586. __raw_spin_lock(&kgdb_cpulock[cpu]);
  587. __raw_spin_unlock(&kgdb_cpulock[cpu]);
  588. local_irq_restore(flags);
  589. }
  590. /*
  591. * GDB stub needs to call kgdb_wait on all processor with interrupts
  592. * disabled, so it uses it's own special variant.
  593. */
  594. static int kgdb_smp_call_kgdb_wait(void)
  595. {
  596. #ifdef CONFIG_SMP
  597. struct call_data_struct data;
  598. int i, cpus = num_online_cpus() - 1;
  599. int cpu = smp_processor_id();
  600. /*
  601. * Can die spectacularly if this CPU isn't yet marked online
  602. */
  603. BUG_ON(!cpu_online(cpu));
  604. if (!cpus)
  605. return 0;
  606. if (spin_is_locked(&smp_call_lock)) {
  607. /*
  608. * Some other processor is trying to make us do something
  609. * but we're not going to respond... give up
  610. */
  611. return -1;
  612. }
  613. /*
  614. * We will continue here, accepting the fact that
  615. * the kernel may deadlock if another CPU attempts
  616. * to call smp_call_function now...
  617. */
  618. data.func = kgdb_wait;
  619. data.info = NULL;
  620. atomic_set(&data.started, 0);
  621. data.wait = 0;
  622. spin_lock(&smp_call_lock);
  623. call_data = &data;
  624. mb();
  625. /* Send a message to all other CPUs and wait for them to respond */
  626. for (i = 0; i < NR_CPUS; i++)
  627. if (cpu_online(i) && i != cpu)
  628. core_send_ipi(i, SMP_CALL_FUNCTION);
  629. /* Wait for response */
  630. /* FIXME: lock-up detection, backtrace on lock-up */
  631. while (atomic_read(&data.started) != cpus)
  632. barrier();
  633. call_data = NULL;
  634. spin_unlock(&smp_call_lock);
  635. #endif
  636. return 0;
  637. }
  638. /*
  639. * This function does all command processing for interfacing to gdb. It
  640. * returns 1 if you should skip the instruction at the trap address, 0
  641. * otherwise.
  642. */
  643. void handle_exception (struct gdb_regs *regs)
  644. {
  645. int trap; /* Trap type */
  646. int sigval;
  647. long addr;
  648. int length;
  649. char *ptr;
  650. unsigned long *stack;
  651. int i;
  652. int bflag = 0;
  653. kgdb_started = 1;
  654. /*
  655. * acquire the big kgdb spinlock
  656. */
  657. if (!spin_trylock(&kgdb_lock)) {
  658. /*
  659. * some other CPU has the lock, we should go back to
  660. * receive the gdb_wait IPC
  661. */
  662. return;
  663. }
  664. /*
  665. * If we're in async_breakpoint(), restore the real EPC from
  666. * the breakpoint.
  667. */
  668. if (regs->cp0_epc == (unsigned long)async_breakinst) {
  669. regs->cp0_epc = async_bp.addr;
  670. async_bp.addr = 0;
  671. }
  672. /*
  673. * acquire the CPU spinlocks
  674. */
  675. for (i = num_online_cpus()-1; i >= 0; i--)
  676. if (__raw_spin_trylock(&kgdb_cpulock[i]) == 0)
  677. panic("kgdb: couldn't get cpulock %d\n", i);
  678. /*
  679. * force other cpus to enter kgdb
  680. */
  681. kgdb_smp_call_kgdb_wait();
  682. /*
  683. * If we're in breakpoint() increment the PC
  684. */
  685. trap = (regs->cp0_cause & 0x7c) >> 2;
  686. if (trap == 9 && regs->cp0_epc == (unsigned long)breakinst)
  687. regs->cp0_epc += 4;
  688. /*
  689. * If we were single_stepping, restore the opcodes hoisted
  690. * for the breakpoint[s].
  691. */
  692. if (step_bp[0].addr) {
  693. *(unsigned *)step_bp[0].addr = step_bp[0].val;
  694. step_bp[0].addr = 0;
  695. if (step_bp[1].addr) {
  696. *(unsigned *)step_bp[1].addr = step_bp[1].val;
  697. step_bp[1].addr = 0;
  698. }
  699. }
  700. stack = (long *)regs->reg29; /* stack ptr */
  701. sigval = computeSignal(trap);
  702. /*
  703. * reply to host that an exception has occurred
  704. */
  705. ptr = output_buffer;
  706. /*
  707. * Send trap type (converted to signal)
  708. */
  709. *ptr++ = 'T';
  710. *ptr++ = hexchars[sigval >> 4];
  711. *ptr++ = hexchars[sigval & 0xf];
  712. /*
  713. * Send Error PC
  714. */
  715. *ptr++ = hexchars[REG_EPC >> 4];
  716. *ptr++ = hexchars[REG_EPC & 0xf];
  717. *ptr++ = ':';
  718. ptr = mem2hex((char *)&regs->cp0_epc, ptr, sizeof(long), 0);
  719. *ptr++ = ';';
  720. /*
  721. * Send frame pointer
  722. */
  723. *ptr++ = hexchars[REG_FP >> 4];
  724. *ptr++ = hexchars[REG_FP & 0xf];
  725. *ptr++ = ':';
  726. ptr = mem2hex((char *)&regs->reg30, ptr, sizeof(long), 0);
  727. *ptr++ = ';';
  728. /*
  729. * Send stack pointer
  730. */
  731. *ptr++ = hexchars[REG_SP >> 4];
  732. *ptr++ = hexchars[REG_SP & 0xf];
  733. *ptr++ = ':';
  734. ptr = mem2hex((char *)&regs->reg29, ptr, sizeof(long), 0);
  735. *ptr++ = ';';
  736. *ptr++ = 0;
  737. putpacket(output_buffer); /* send it off... */
  738. /*
  739. * Wait for input from remote GDB
  740. */
  741. while (1) {
  742. output_buffer[0] = 0;
  743. getpacket(input_buffer);
  744. switch (input_buffer[0])
  745. {
  746. case '?':
  747. output_buffer[0] = 'S';
  748. output_buffer[1] = hexchars[sigval >> 4];
  749. output_buffer[2] = hexchars[sigval & 0xf];
  750. output_buffer[3] = 0;
  751. break;
  752. /*
  753. * Detach debugger; let CPU run
  754. */
  755. case 'D':
  756. putpacket(output_buffer);
  757. goto finish_kgdb;
  758. break;
  759. case 'd':
  760. /* toggle debug flag */
  761. break;
  762. /*
  763. * Return the value of the CPU registers
  764. */
  765. case 'g':
  766. ptr = output_buffer;
  767. ptr = mem2hex((char *)&regs->reg0, ptr, 32*sizeof(long), 0); /* r0...r31 */
  768. ptr = mem2hex((char *)&regs->cp0_status, ptr, 6*sizeof(long), 0); /* cp0 */
  769. ptr = mem2hex((char *)&regs->fpr0, ptr, 32*sizeof(long), 0); /* f0...31 */
  770. ptr = mem2hex((char *)&regs->cp1_fsr, ptr, 2*sizeof(long), 0); /* cp1 */
  771. ptr = mem2hex((char *)&regs->frame_ptr, ptr, 2*sizeof(long), 0); /* frp */
  772. ptr = mem2hex((char *)&regs->cp0_index, ptr, 16*sizeof(long), 0); /* cp0 */
  773. break;
  774. /*
  775. * set the value of the CPU registers - return OK
  776. */
  777. case 'G':
  778. {
  779. ptr = &input_buffer[1];
  780. hex2mem(ptr, (char *)&regs->reg0, 32*sizeof(long), 0, 0);
  781. ptr += 32*(2*sizeof(long));
  782. hex2mem(ptr, (char *)&regs->cp0_status, 6*sizeof(long), 0, 0);
  783. ptr += 6*(2*sizeof(long));
  784. hex2mem(ptr, (char *)&regs->fpr0, 32*sizeof(long), 0, 0);
  785. ptr += 32*(2*sizeof(long));
  786. hex2mem(ptr, (char *)&regs->cp1_fsr, 2*sizeof(long), 0, 0);
  787. ptr += 2*(2*sizeof(long));
  788. hex2mem(ptr, (char *)&regs->frame_ptr, 2*sizeof(long), 0, 0);
  789. ptr += 2*(2*sizeof(long));
  790. hex2mem(ptr, (char *)&regs->cp0_index, 16*sizeof(long), 0, 0);
  791. strcpy(output_buffer,"OK");
  792. }
  793. break;
  794. /*
  795. * mAA..AA,LLLL Read LLLL bytes at address AA..AA
  796. */
  797. case 'm':
  798. ptr = &input_buffer[1];
  799. if (hexToLong(&ptr, &addr)
  800. && *ptr++ == ','
  801. && hexToInt(&ptr, &length)) {
  802. if (mem2hex((char *)addr, output_buffer, length, 1))
  803. break;
  804. strcpy (output_buffer, "E03");
  805. } else
  806. strcpy(output_buffer,"E01");
  807. break;
  808. /*
  809. * XAA..AA,LLLL: Write LLLL escaped binary bytes at address AA.AA
  810. */
  811. case 'X':
  812. bflag = 1;
  813. /* fall through */
  814. /*
  815. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK
  816. */
  817. case 'M':
  818. ptr = &input_buffer[1];
  819. if (hexToLong(&ptr, &addr)
  820. && *ptr++ == ','
  821. && hexToInt(&ptr, &length)
  822. && *ptr++ == ':') {
  823. if (hex2mem(ptr, (char *)addr, length, bflag, 1))
  824. strcpy(output_buffer, "OK");
  825. else
  826. strcpy(output_buffer, "E03");
  827. }
  828. else
  829. strcpy(output_buffer, "E02");
  830. break;
  831. /*
  832. * cAA..AA Continue at address AA..AA(optional)
  833. */
  834. case 'c':
  835. /* try to read optional parameter, pc unchanged if no parm */
  836. ptr = &input_buffer[1];
  837. if (hexToLong(&ptr, &addr))
  838. regs->cp0_epc = addr;
  839. goto exit_kgdb_exception;
  840. break;
  841. /*
  842. * kill the program; let us try to restart the machine
  843. * Reset the whole machine.
  844. */
  845. case 'k':
  846. case 'r':
  847. machine_restart("kgdb restarts machine");
  848. break;
  849. /*
  850. * Step to next instruction
  851. */
  852. case 's':
  853. /*
  854. * There is no single step insn in the MIPS ISA, so we
  855. * use breakpoints and continue, instead.
  856. */
  857. single_step(regs);
  858. goto exit_kgdb_exception;
  859. /* NOTREACHED */
  860. break;
  861. /*
  862. * Set baud rate (bBB)
  863. * FIXME: Needs to be written
  864. */
  865. case 'b':
  866. {
  867. #if 0
  868. int baudrate;
  869. extern void set_timer_3();
  870. ptr = &input_buffer[1];
  871. if (!hexToInt(&ptr, &baudrate))
  872. {
  873. strcpy(output_buffer,"B01");
  874. break;
  875. }
  876. /* Convert baud rate to uart clock divider */
  877. switch (baudrate)
  878. {
  879. case 38400:
  880. baudrate = 16;
  881. break;
  882. case 19200:
  883. baudrate = 33;
  884. break;
  885. case 9600:
  886. baudrate = 65;
  887. break;
  888. default:
  889. baudrate = 0;
  890. strcpy(output_buffer,"B02");
  891. goto x1;
  892. }
  893. if (baudrate) {
  894. putpacket("OK"); /* Ack before changing speed */
  895. set_timer_3(baudrate); /* Set it */
  896. }
  897. #endif
  898. }
  899. break;
  900. } /* switch */
  901. /*
  902. * reply to the request
  903. */
  904. putpacket(output_buffer);
  905. } /* while */
  906. return;
  907. finish_kgdb:
  908. restore_debug_traps();
  909. exit_kgdb_exception:
  910. /* release locks so other CPUs can go */
  911. for (i = num_online_cpus()-1; i >= 0; i--)
  912. __raw_spin_unlock(&kgdb_cpulock[i]);
  913. spin_unlock(&kgdb_lock);
  914. __flush_cache_all();
  915. return;
  916. }
  917. /*
  918. * This function will generate a breakpoint exception. It is used at the
  919. * beginning of a program to sync up with a debugger and can be used
  920. * otherwise as a quick means to stop program execution and "break" into
  921. * the debugger.
  922. */
  923. void breakpoint(void)
  924. {
  925. if (!initialized)
  926. return;
  927. __asm__ __volatile__(
  928. ".globl breakinst\n\t"
  929. ".set\tnoreorder\n\t"
  930. "nop\n"
  931. "breakinst:\tbreak\n\t"
  932. "nop\n\t"
  933. ".set\treorder"
  934. );
  935. }
  936. /* Nothing but the break; don't pollute any registers */
  937. void async_breakpoint(void)
  938. {
  939. __asm__ __volatile__(
  940. ".globl async_breakinst\n\t"
  941. ".set\tnoreorder\n\t"
  942. "nop\n"
  943. "async_breakinst:\tbreak\n\t"
  944. "nop\n\t"
  945. ".set\treorder"
  946. );
  947. }
  948. void adel(void)
  949. {
  950. __asm__ __volatile__(
  951. ".globl\tadel\n\t"
  952. "lui\t$8,0x8000\n\t"
  953. "lw\t$9,1($8)\n\t"
  954. );
  955. }
  956. /*
  957. * malloc is needed by gdb client in "call func()", even a private one
  958. * will make gdb happy
  959. */
  960. static void * __attribute_used__ malloc(size_t size)
  961. {
  962. return kmalloc(size, GFP_ATOMIC);
  963. }
  964. static void __attribute_used__ free (void *where)
  965. {
  966. kfree(where);
  967. }
  968. #ifdef CONFIG_GDB_CONSOLE
  969. void gdb_putsn(const char *str, int l)
  970. {
  971. char outbuf[18];
  972. if (!kgdb_started)
  973. return;
  974. outbuf[0]='O';
  975. while(l) {
  976. int i = (l>8)?8:l;
  977. mem2hex((char *)str, &outbuf[1], i, 0);
  978. outbuf[(i*2)+1]=0;
  979. putpacket(outbuf);
  980. str += i;
  981. l -= i;
  982. }
  983. }
  984. static void gdb_console_write(struct console *con, const char *s, unsigned n)
  985. {
  986. gdb_putsn(s, n);
  987. }
  988. static struct console gdb_console = {
  989. .name = "gdb",
  990. .write = gdb_console_write,
  991. .flags = CON_PRINTBUFFER,
  992. .index = -1
  993. };
  994. static int __init register_gdb_console(void)
  995. {
  996. register_console(&gdb_console);
  997. return 0;
  998. }
  999. console_initcall(register_gdb_console);
  1000. #endif