gdb-stub.c 25 KB

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