gdb-stub.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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 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. * This function does all command processing for interfacing to gdb. It
  592. * returns 1 if you should skip the instruction at the trap address, 0
  593. * otherwise.
  594. */
  595. void handle_exception (struct gdb_regs *regs)
  596. {
  597. int trap; /* Trap type */
  598. int sigval;
  599. long addr;
  600. int length;
  601. char *ptr;
  602. unsigned long *stack;
  603. int i;
  604. int bflag = 0;
  605. kgdb_started = 1;
  606. /*
  607. * acquire the big kgdb spinlock
  608. */
  609. if (!spin_trylock(&kgdb_lock)) {
  610. /*
  611. * some other CPU has the lock, we should go back to
  612. * receive the gdb_wait IPC
  613. */
  614. return;
  615. }
  616. /*
  617. * If we're in async_breakpoint(), restore the real EPC from
  618. * the breakpoint.
  619. */
  620. if (regs->cp0_epc == (unsigned long)async_breakinst) {
  621. regs->cp0_epc = async_bp.addr;
  622. async_bp.addr = 0;
  623. }
  624. /*
  625. * acquire the CPU spinlocks
  626. */
  627. for (i = num_online_cpus()-1; i >= 0; i--)
  628. if (__raw_spin_trylock(&kgdb_cpulock[i]) == 0)
  629. panic("kgdb: couldn't get cpulock %d\n", i);
  630. /*
  631. * force other cpus to enter kgdb
  632. */
  633. smp_call_function(kgdb_wait, NULL, 0, 0);
  634. /*
  635. * If we're in breakpoint() increment the PC
  636. */
  637. trap = (regs->cp0_cause & 0x7c) >> 2;
  638. if (trap == 9 && regs->cp0_epc == (unsigned long)breakinst)
  639. regs->cp0_epc += 4;
  640. /*
  641. * If we were single_stepping, restore the opcodes hoisted
  642. * for the breakpoint[s].
  643. */
  644. if (step_bp[0].addr) {
  645. *(unsigned *)step_bp[0].addr = step_bp[0].val;
  646. step_bp[0].addr = 0;
  647. if (step_bp[1].addr) {
  648. *(unsigned *)step_bp[1].addr = step_bp[1].val;
  649. step_bp[1].addr = 0;
  650. }
  651. }
  652. stack = (long *)regs->reg29; /* stack ptr */
  653. sigval = computeSignal(trap);
  654. /*
  655. * reply to host that an exception has occurred
  656. */
  657. ptr = output_buffer;
  658. /*
  659. * Send trap type (converted to signal)
  660. */
  661. *ptr++ = 'T';
  662. *ptr++ = hexchars[sigval >> 4];
  663. *ptr++ = hexchars[sigval & 0xf];
  664. /*
  665. * Send Error PC
  666. */
  667. *ptr++ = hexchars[REG_EPC >> 4];
  668. *ptr++ = hexchars[REG_EPC & 0xf];
  669. *ptr++ = ':';
  670. ptr = mem2hex((char *)&regs->cp0_epc, ptr, sizeof(long), 0);
  671. *ptr++ = ';';
  672. /*
  673. * Send frame pointer
  674. */
  675. *ptr++ = hexchars[REG_FP >> 4];
  676. *ptr++ = hexchars[REG_FP & 0xf];
  677. *ptr++ = ':';
  678. ptr = mem2hex((char *)&regs->reg30, ptr, sizeof(long), 0);
  679. *ptr++ = ';';
  680. /*
  681. * Send stack pointer
  682. */
  683. *ptr++ = hexchars[REG_SP >> 4];
  684. *ptr++ = hexchars[REG_SP & 0xf];
  685. *ptr++ = ':';
  686. ptr = mem2hex((char *)&regs->reg29, ptr, sizeof(long), 0);
  687. *ptr++ = ';';
  688. *ptr++ = 0;
  689. putpacket(output_buffer); /* send it off... */
  690. /*
  691. * Wait for input from remote GDB
  692. */
  693. while (1) {
  694. output_buffer[0] = 0;
  695. getpacket(input_buffer);
  696. switch (input_buffer[0])
  697. {
  698. case '?':
  699. output_buffer[0] = 'S';
  700. output_buffer[1] = hexchars[sigval >> 4];
  701. output_buffer[2] = hexchars[sigval & 0xf];
  702. output_buffer[3] = 0;
  703. break;
  704. /*
  705. * Detach debugger; let CPU run
  706. */
  707. case 'D':
  708. putpacket(output_buffer);
  709. goto finish_kgdb;
  710. break;
  711. case 'd':
  712. /* toggle debug flag */
  713. break;
  714. /*
  715. * Return the value of the CPU registers
  716. */
  717. case 'g':
  718. ptr = output_buffer;
  719. ptr = mem2hex((char *)&regs->reg0, ptr, 32*sizeof(long), 0); /* r0...r31 */
  720. ptr = mem2hex((char *)&regs->cp0_status, ptr, 6*sizeof(long), 0); /* cp0 */
  721. ptr = mem2hex((char *)&regs->fpr0, ptr, 32*sizeof(long), 0); /* f0...31 */
  722. ptr = mem2hex((char *)&regs->cp1_fsr, ptr, 2*sizeof(long), 0); /* cp1 */
  723. ptr = mem2hex((char *)&regs->frame_ptr, ptr, 2*sizeof(long), 0); /* frp */
  724. ptr = mem2hex((char *)&regs->cp0_index, ptr, 16*sizeof(long), 0); /* cp0 */
  725. break;
  726. /*
  727. * set the value of the CPU registers - return OK
  728. */
  729. case 'G':
  730. {
  731. ptr = &input_buffer[1];
  732. hex2mem(ptr, (char *)&regs->reg0, 32*sizeof(long), 0, 0);
  733. ptr += 32*(2*sizeof(long));
  734. hex2mem(ptr, (char *)&regs->cp0_status, 6*sizeof(long), 0, 0);
  735. ptr += 6*(2*sizeof(long));
  736. hex2mem(ptr, (char *)&regs->fpr0, 32*sizeof(long), 0, 0);
  737. ptr += 32*(2*sizeof(long));
  738. hex2mem(ptr, (char *)&regs->cp1_fsr, 2*sizeof(long), 0, 0);
  739. ptr += 2*(2*sizeof(long));
  740. hex2mem(ptr, (char *)&regs->frame_ptr, 2*sizeof(long), 0, 0);
  741. ptr += 2*(2*sizeof(long));
  742. hex2mem(ptr, (char *)&regs->cp0_index, 16*sizeof(long), 0, 0);
  743. strcpy(output_buffer,"OK");
  744. }
  745. break;
  746. /*
  747. * mAA..AA,LLLL Read LLLL bytes at address AA..AA
  748. */
  749. case 'm':
  750. ptr = &input_buffer[1];
  751. if (hexToLong(&ptr, &addr)
  752. && *ptr++ == ','
  753. && hexToInt(&ptr, &length)) {
  754. if (mem2hex((char *)addr, output_buffer, length, 1))
  755. break;
  756. strcpy (output_buffer, "E03");
  757. } else
  758. strcpy(output_buffer,"E01");
  759. break;
  760. /*
  761. * XAA..AA,LLLL: Write LLLL escaped binary bytes at address AA.AA
  762. */
  763. case 'X':
  764. bflag = 1;
  765. /* fall through */
  766. /*
  767. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK
  768. */
  769. case 'M':
  770. ptr = &input_buffer[1];
  771. if (hexToLong(&ptr, &addr)
  772. && *ptr++ == ','
  773. && hexToInt(&ptr, &length)
  774. && *ptr++ == ':') {
  775. if (hex2mem(ptr, (char *)addr, length, bflag, 1))
  776. strcpy(output_buffer, "OK");
  777. else
  778. strcpy(output_buffer, "E03");
  779. }
  780. else
  781. strcpy(output_buffer, "E02");
  782. break;
  783. /*
  784. * cAA..AA Continue at address AA..AA(optional)
  785. */
  786. case 'c':
  787. /* try to read optional parameter, pc unchanged if no parm */
  788. ptr = &input_buffer[1];
  789. if (hexToLong(&ptr, &addr))
  790. regs->cp0_epc = addr;
  791. goto exit_kgdb_exception;
  792. break;
  793. /*
  794. * kill the program; let us try to restart the machine
  795. * Reset the whole machine.
  796. */
  797. case 'k':
  798. case 'r':
  799. machine_restart("kgdb restarts machine");
  800. break;
  801. /*
  802. * Step to next instruction
  803. */
  804. case 's':
  805. /*
  806. * There is no single step insn in the MIPS ISA, so we
  807. * use breakpoints and continue, instead.
  808. */
  809. single_step(regs);
  810. goto exit_kgdb_exception;
  811. /* NOTREACHED */
  812. break;
  813. /*
  814. * Set baud rate (bBB)
  815. * FIXME: Needs to be written
  816. */
  817. case 'b':
  818. {
  819. #if 0
  820. int baudrate;
  821. extern void set_timer_3();
  822. ptr = &input_buffer[1];
  823. if (!hexToInt(&ptr, &baudrate))
  824. {
  825. strcpy(output_buffer,"B01");
  826. break;
  827. }
  828. /* Convert baud rate to uart clock divider */
  829. switch (baudrate)
  830. {
  831. case 38400:
  832. baudrate = 16;
  833. break;
  834. case 19200:
  835. baudrate = 33;
  836. break;
  837. case 9600:
  838. baudrate = 65;
  839. break;
  840. default:
  841. baudrate = 0;
  842. strcpy(output_buffer,"B02");
  843. goto x1;
  844. }
  845. if (baudrate) {
  846. putpacket("OK"); /* Ack before changing speed */
  847. set_timer_3(baudrate); /* Set it */
  848. }
  849. #endif
  850. }
  851. break;
  852. } /* switch */
  853. /*
  854. * reply to the request
  855. */
  856. putpacket(output_buffer);
  857. } /* while */
  858. return;
  859. finish_kgdb:
  860. restore_debug_traps();
  861. exit_kgdb_exception:
  862. /* release locks so other CPUs can go */
  863. for (i = num_online_cpus()-1; i >= 0; i--)
  864. __raw_spin_unlock(&kgdb_cpulock[i]);
  865. spin_unlock(&kgdb_lock);
  866. __flush_cache_all();
  867. return;
  868. }
  869. /*
  870. * This function will generate a breakpoint exception. It is used at the
  871. * beginning of a program to sync up with a debugger and can be used
  872. * otherwise as a quick means to stop program execution and "break" into
  873. * the debugger.
  874. */
  875. void breakpoint(void)
  876. {
  877. if (!initialized)
  878. return;
  879. __asm__ __volatile__(
  880. ".globl breakinst\n\t"
  881. ".set\tnoreorder\n\t"
  882. "nop\n"
  883. "breakinst:\tbreak\n\t"
  884. "nop\n\t"
  885. ".set\treorder"
  886. );
  887. }
  888. /* Nothing but the break; don't pollute any registers */
  889. void async_breakpoint(void)
  890. {
  891. __asm__ __volatile__(
  892. ".globl async_breakinst\n\t"
  893. ".set\tnoreorder\n\t"
  894. "nop\n"
  895. "async_breakinst:\tbreak\n\t"
  896. "nop\n\t"
  897. ".set\treorder"
  898. );
  899. }
  900. void adel(void)
  901. {
  902. __asm__ __volatile__(
  903. ".globl\tadel\n\t"
  904. "lui\t$8,0x8000\n\t"
  905. "lw\t$9,1($8)\n\t"
  906. );
  907. }
  908. /*
  909. * malloc is needed by gdb client in "call func()", even a private one
  910. * will make gdb happy
  911. */
  912. static void * __attribute_used__ malloc(size_t size)
  913. {
  914. return kmalloc(size, GFP_ATOMIC);
  915. }
  916. static void __attribute_used__ free (void *where)
  917. {
  918. kfree(where);
  919. }
  920. #ifdef CONFIG_GDB_CONSOLE
  921. void gdb_putsn(const char *str, int l)
  922. {
  923. char outbuf[18];
  924. if (!kgdb_started)
  925. return;
  926. outbuf[0]='O';
  927. while(l) {
  928. int i = (l>8)?8:l;
  929. mem2hex((char *)str, &outbuf[1], i, 0);
  930. outbuf[(i*2)+1]=0;
  931. putpacket(outbuf);
  932. str += i;
  933. l -= i;
  934. }
  935. }
  936. static void gdb_console_write(struct console *con, const char *s, unsigned n)
  937. {
  938. gdb_putsn(s, n);
  939. }
  940. static struct console gdb_console = {
  941. .name = "gdb",
  942. .write = gdb_console_write,
  943. .flags = CON_PRINTBUFFER,
  944. .index = -1
  945. };
  946. static int __init register_gdb_console(void)
  947. {
  948. register_console(&gdb_console);
  949. return 0;
  950. }
  951. console_initcall(register_gdb_console);
  952. #endif