ppc-stub.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. * ppc-stub.c: KGDB support for the Linux kernel.
  3. *
  4. * adapted from arch/sparc/kernel/sparc-stub.c for the PowerPC
  5. * some stuff borrowed from Paul Mackerras' xmon
  6. * Copyright (C) 1998 Michael AK Tesch (tesch@cs.wisc.edu)
  7. *
  8. * Modifications to run under Linux
  9. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  10. *
  11. * This file originally came from the gdb sources, and the
  12. * copyright notices have been retained below.
  13. */
  14. /****************************************************************************
  15. THIS SOFTWARE IS NOT COPYRIGHTED
  16. HP offers the following for use in the public domain. HP makes no
  17. warranty with regard to the software or its performance and the
  18. user accepts the software "AS IS" with all faults.
  19. HP DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD
  20. TO THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22. ****************************************************************************/
  23. /****************************************************************************
  24. * Header: remcom.c,v 1.34 91/03/09 12:29:49 glenne Exp $
  25. *
  26. * Module name: remcom.c $
  27. * Revision: 1.34 $
  28. * Date: 91/03/09 12:29:49 $
  29. * Contributor: Lake Stevens Instrument Division$
  30. *
  31. * Description: low level support for gdb debugger. $
  32. *
  33. * Considerations: only works on target hardware $
  34. *
  35. * Written by: Glenn Engel $
  36. * ModuleState: Experimental $
  37. *
  38. * NOTES: See Below $
  39. *
  40. * Modified for SPARC by Stu Grossman, Cygnus Support.
  41. *
  42. * This code has been extensively tested on the Fujitsu SPARClite demo board.
  43. *
  44. * To enable debugger support, two things need to happen. One, a
  45. * call to set_debug_traps() is necessary in order to allow any breakpoints
  46. * or error conditions to be properly intercepted and reported to gdb.
  47. * Two, a breakpoint needs to be generated to begin communication. This
  48. * is most easily accomplished by a call to breakpoint(). Breakpoint()
  49. * simulates a breakpoint by executing a trap #1.
  50. *
  51. *************
  52. *
  53. * The following gdb commands are supported:
  54. *
  55. * command function Return value
  56. *
  57. * g return the value of the CPU registers hex data or ENN
  58. * G set the value of the CPU registers OK or ENN
  59. * qOffsets Get section offsets. Reply is Text=xxx;Data=yyy;Bss=zzz
  60. *
  61. * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
  62. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
  63. *
  64. * c Resume at current address SNN ( signal NN)
  65. * cAA..AA Continue at address AA..AA SNN
  66. *
  67. * s Step one instruction SNN
  68. * sAA..AA Step one instruction from AA..AA SNN
  69. *
  70. * k kill
  71. *
  72. * ? What was the last sigval ? SNN (signal NN)
  73. *
  74. * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
  75. * baud rate
  76. *
  77. * All commands and responses are sent with a packet which includes a
  78. * checksum. A packet consists of
  79. *
  80. * $<packet info>#<checksum>.
  81. *
  82. * where
  83. * <packet info> :: <characters representing the command or response>
  84. * <checksum> :: <two hex digits computed as modulo 256 sum of <packetinfo>>
  85. *
  86. * When a packet is received, it is first acknowledged with either '+' or '-'.
  87. * '+' indicates a successful transfer. '-' indicates a failed transfer.
  88. *
  89. * Example:
  90. *
  91. * Host: Reply:
  92. * $m0,10#2a +$00010203040506070809101112131415#42
  93. *
  94. ****************************************************************************/
  95. #include <linux/config.h>
  96. #include <linux/kernel.h>
  97. #include <linux/string.h>
  98. #include <linux/mm.h>
  99. #include <linux/smp.h>
  100. #include <linux/smp_lock.h>
  101. #include <linux/init.h>
  102. #include <linux/sysrq.h>
  103. #include <asm/cacheflush.h>
  104. #include <asm/system.h>
  105. #include <asm/signal.h>
  106. #include <asm/kgdb.h>
  107. #include <asm/pgtable.h>
  108. #include <asm/ptrace.h>
  109. void breakinst(void);
  110. /*
  111. * BUFMAX defines the maximum number of characters in inbound/outbound buffers
  112. * at least NUMREGBYTES*2 are needed for register packets
  113. */
  114. #define BUFMAX 2048
  115. static char remcomInBuffer[BUFMAX];
  116. static char remcomOutBuffer[BUFMAX];
  117. static int initialized;
  118. static int kgdb_active;
  119. static int kgdb_started;
  120. static u_int fault_jmp_buf[100];
  121. static int kdebug;
  122. static const char hexchars[]="0123456789abcdef";
  123. /* Place where we save old trap entries for restoration - sparc*/
  124. /* struct tt_entry kgdb_savettable[256]; */
  125. /* typedef void (*trapfunc_t)(void); */
  126. static void kgdb_fault_handler(struct pt_regs *regs);
  127. static int handle_exception (struct pt_regs *regs);
  128. #if 0
  129. /* Install an exception handler for kgdb */
  130. static void exceptionHandler(int tnum, unsigned int *tfunc)
  131. {
  132. /* We are dorking with a live trap table, all irqs off */
  133. }
  134. #endif
  135. int
  136. kgdb_setjmp(long *buf)
  137. {
  138. asm ("mflr 0; stw 0,0(%0);"
  139. "stw 1,4(%0); stw 2,8(%0);"
  140. "mfcr 0; stw 0,12(%0);"
  141. "stmw 13,16(%0)"
  142. : : "r" (buf));
  143. /* XXX should save fp regs as well */
  144. return 0;
  145. }
  146. void
  147. kgdb_longjmp(long *buf, int val)
  148. {
  149. if (val == 0)
  150. val = 1;
  151. asm ("lmw 13,16(%0);"
  152. "lwz 0,12(%0); mtcrf 0x38,0;"
  153. "lwz 0,0(%0); lwz 1,4(%0); lwz 2,8(%0);"
  154. "mtlr 0; mr 3,%1"
  155. : : "r" (buf), "r" (val));
  156. }
  157. /* Convert ch from a hex digit to an int */
  158. static int
  159. hex(unsigned char ch)
  160. {
  161. if (ch >= 'a' && ch <= 'f')
  162. return ch-'a'+10;
  163. if (ch >= '0' && ch <= '9')
  164. return ch-'0';
  165. if (ch >= 'A' && ch <= 'F')
  166. return ch-'A'+10;
  167. return -1;
  168. }
  169. /* Convert the memory pointed to by mem into hex, placing result in buf.
  170. * Return a pointer to the last char put in buf (null), in case of mem fault,
  171. * return 0.
  172. */
  173. static unsigned char *
  174. mem2hex(const char *mem, char *buf, int count)
  175. {
  176. unsigned char ch;
  177. unsigned short tmp_s;
  178. unsigned long tmp_l;
  179. if (kgdb_setjmp((long*)fault_jmp_buf) == 0) {
  180. debugger_fault_handler = kgdb_fault_handler;
  181. /* Accessing 16 bit and 32 bit objects in a single
  182. ** load instruction is required to avoid bad side
  183. ** effects for some IO registers.
  184. */
  185. if ((count == 2) && (((long)mem & 1) == 0)) {
  186. tmp_s = *(unsigned short *)mem;
  187. mem += 2;
  188. *buf++ = hexchars[(tmp_s >> 12) & 0xf];
  189. *buf++ = hexchars[(tmp_s >> 8) & 0xf];
  190. *buf++ = hexchars[(tmp_s >> 4) & 0xf];
  191. *buf++ = hexchars[tmp_s & 0xf];
  192. } else if ((count == 4) && (((long)mem & 3) == 0)) {
  193. tmp_l = *(unsigned int *)mem;
  194. mem += 4;
  195. *buf++ = hexchars[(tmp_l >> 28) & 0xf];
  196. *buf++ = hexchars[(tmp_l >> 24) & 0xf];
  197. *buf++ = hexchars[(tmp_l >> 20) & 0xf];
  198. *buf++ = hexchars[(tmp_l >> 16) & 0xf];
  199. *buf++ = hexchars[(tmp_l >> 12) & 0xf];
  200. *buf++ = hexchars[(tmp_l >> 8) & 0xf];
  201. *buf++ = hexchars[(tmp_l >> 4) & 0xf];
  202. *buf++ = hexchars[tmp_l & 0xf];
  203. } else {
  204. while (count-- > 0) {
  205. ch = *mem++;
  206. *buf++ = hexchars[ch >> 4];
  207. *buf++ = hexchars[ch & 0xf];
  208. }
  209. }
  210. } else {
  211. /* error condition */
  212. }
  213. debugger_fault_handler = NULL;
  214. *buf = 0;
  215. return buf;
  216. }
  217. /* convert the hex array pointed to by buf into binary to be placed in mem
  218. * return a pointer to the character AFTER the last byte written.
  219. */
  220. static char *
  221. hex2mem(char *buf, char *mem, int count)
  222. {
  223. unsigned char ch;
  224. int i;
  225. char *orig_mem;
  226. unsigned short tmp_s;
  227. unsigned long tmp_l;
  228. orig_mem = mem;
  229. if (kgdb_setjmp((long*)fault_jmp_buf) == 0) {
  230. debugger_fault_handler = kgdb_fault_handler;
  231. /* Accessing 16 bit and 32 bit objects in a single
  232. ** store instruction is required to avoid bad side
  233. ** effects for some IO registers.
  234. */
  235. if ((count == 2) && (((long)mem & 1) == 0)) {
  236. tmp_s = hex(*buf++) << 12;
  237. tmp_s |= hex(*buf++) << 8;
  238. tmp_s |= hex(*buf++) << 4;
  239. tmp_s |= hex(*buf++);
  240. *(unsigned short *)mem = tmp_s;
  241. mem += 2;
  242. } else if ((count == 4) && (((long)mem & 3) == 0)) {
  243. tmp_l = hex(*buf++) << 28;
  244. tmp_l |= hex(*buf++) << 24;
  245. tmp_l |= hex(*buf++) << 20;
  246. tmp_l |= hex(*buf++) << 16;
  247. tmp_l |= hex(*buf++) << 12;
  248. tmp_l |= hex(*buf++) << 8;
  249. tmp_l |= hex(*buf++) << 4;
  250. tmp_l |= hex(*buf++);
  251. *(unsigned long *)mem = tmp_l;
  252. mem += 4;
  253. } else {
  254. for (i=0; i<count; i++) {
  255. ch = hex(*buf++) << 4;
  256. ch |= hex(*buf++);
  257. *mem++ = ch;
  258. }
  259. }
  260. /*
  261. ** Flush the data cache, invalidate the instruction cache.
  262. */
  263. flush_icache_range((int)orig_mem, (int)orig_mem + count - 1);
  264. } else {
  265. /* error condition */
  266. }
  267. debugger_fault_handler = NULL;
  268. return mem;
  269. }
  270. /*
  271. * While we find nice hex chars, build an int.
  272. * Return number of chars processed.
  273. */
  274. static int
  275. hexToInt(char **ptr, int *intValue)
  276. {
  277. int numChars = 0;
  278. int hexValue;
  279. *intValue = 0;
  280. if (kgdb_setjmp((long*)fault_jmp_buf) == 0) {
  281. debugger_fault_handler = kgdb_fault_handler;
  282. while (**ptr) {
  283. hexValue = hex(**ptr);
  284. if (hexValue < 0)
  285. break;
  286. *intValue = (*intValue << 4) | hexValue;
  287. numChars ++;
  288. (*ptr)++;
  289. }
  290. } else {
  291. /* error condition */
  292. }
  293. debugger_fault_handler = NULL;
  294. return (numChars);
  295. }
  296. /* scan for the sequence $<data>#<checksum> */
  297. static void
  298. getpacket(char *buffer)
  299. {
  300. unsigned char checksum;
  301. unsigned char xmitcsum;
  302. int i;
  303. int count;
  304. unsigned char ch;
  305. do {
  306. /* wait around for the start character, ignore all other
  307. * characters */
  308. while ((ch = (getDebugChar() & 0x7f)) != '$') ;
  309. checksum = 0;
  310. xmitcsum = -1;
  311. count = 0;
  312. /* now, read until a # or end of buffer is found */
  313. while (count < BUFMAX) {
  314. ch = getDebugChar() & 0x7f;
  315. if (ch == '#')
  316. break;
  317. checksum = checksum + ch;
  318. buffer[count] = ch;
  319. count = count + 1;
  320. }
  321. if (count >= BUFMAX)
  322. continue;
  323. buffer[count] = 0;
  324. if (ch == '#') {
  325. xmitcsum = hex(getDebugChar() & 0x7f) << 4;
  326. xmitcsum |= hex(getDebugChar() & 0x7f);
  327. if (checksum != xmitcsum)
  328. putDebugChar('-'); /* failed checksum */
  329. else {
  330. putDebugChar('+'); /* successful transfer */
  331. /* if a sequence char is present, reply the ID */
  332. if (buffer[2] == ':') {
  333. putDebugChar(buffer[0]);
  334. putDebugChar(buffer[1]);
  335. /* remove sequence chars from buffer */
  336. count = strlen(buffer);
  337. for (i=3; i <= count; i++)
  338. buffer[i-3] = buffer[i];
  339. }
  340. }
  341. }
  342. } while (checksum != xmitcsum);
  343. }
  344. /* send the packet in buffer. */
  345. static void putpacket(unsigned char *buffer)
  346. {
  347. unsigned char checksum;
  348. int count;
  349. unsigned char ch, recv;
  350. /* $<packet info>#<checksum>. */
  351. do {
  352. putDebugChar('$');
  353. checksum = 0;
  354. count = 0;
  355. while ((ch = buffer[count])) {
  356. putDebugChar(ch);
  357. checksum += ch;
  358. count += 1;
  359. }
  360. putDebugChar('#');
  361. putDebugChar(hexchars[checksum >> 4]);
  362. putDebugChar(hexchars[checksum & 0xf]);
  363. recv = getDebugChar();
  364. } while ((recv & 0x7f) != '+');
  365. }
  366. static void kgdb_flush_cache_all(void)
  367. {
  368. flush_instruction_cache();
  369. }
  370. /* Set up exception handlers for tracing and breakpoints
  371. * [could be called kgdb_init()]
  372. */
  373. void set_debug_traps(void)
  374. {
  375. #if 0
  376. unsigned char c;
  377. save_and_cli(flags);
  378. /* In case GDB is started before us, ack any packets (presumably
  379. * "$?#xx") sitting there.
  380. *
  381. * I've found this code causes more problems than it solves,
  382. * so that's why it's commented out. GDB seems to work fine
  383. * now starting either before or after the kernel -bwb
  384. */
  385. while((c = getDebugChar()) != '$');
  386. while((c = getDebugChar()) != '#');
  387. c = getDebugChar(); /* eat first csum byte */
  388. c = getDebugChar(); /* eat second csum byte */
  389. putDebugChar('+'); /* ack it */
  390. #endif
  391. debugger = kgdb;
  392. debugger_bpt = kgdb_bpt;
  393. debugger_sstep = kgdb_sstep;
  394. debugger_iabr_match = kgdb_iabr_match;
  395. debugger_dabr_match = kgdb_dabr_match;
  396. initialized = 1;
  397. }
  398. static void kgdb_fault_handler(struct pt_regs *regs)
  399. {
  400. kgdb_longjmp((long*)fault_jmp_buf, 1);
  401. }
  402. int kgdb_bpt(struct pt_regs *regs)
  403. {
  404. return handle_exception(regs);
  405. }
  406. int kgdb_sstep(struct pt_regs *regs)
  407. {
  408. return handle_exception(regs);
  409. }
  410. void kgdb(struct pt_regs *regs)
  411. {
  412. handle_exception(regs);
  413. }
  414. int kgdb_iabr_match(struct pt_regs *regs)
  415. {
  416. printk(KERN_ERR "kgdb doesn't support iabr, what?!?\n");
  417. return handle_exception(regs);
  418. }
  419. int kgdb_dabr_match(struct pt_regs *regs)
  420. {
  421. printk(KERN_ERR "kgdb doesn't support dabr, what?!?\n");
  422. return handle_exception(regs);
  423. }
  424. /* Convert the hardware trap type code to a unix signal number. */
  425. /*
  426. * This table contains the mapping between PowerPC hardware trap types, and
  427. * signals, which are primarily what GDB understands.
  428. */
  429. static struct hard_trap_info
  430. {
  431. unsigned int tt; /* Trap type code for powerpc */
  432. unsigned char signo; /* Signal that we map this trap into */
  433. } hard_trap_info[] = {
  434. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  435. { 0x100, SIGINT }, /* critical input interrupt */
  436. { 0x200, SIGSEGV }, /* machine check */
  437. { 0x300, SIGSEGV }, /* data storage */
  438. { 0x400, SIGBUS }, /* instruction storage */
  439. { 0x500, SIGINT }, /* interrupt */
  440. { 0x600, SIGBUS }, /* alignment */
  441. { 0x700, SIGILL }, /* program */
  442. { 0x800, SIGILL }, /* reserved */
  443. { 0x900, SIGILL }, /* reserved */
  444. { 0xa00, SIGILL }, /* reserved */
  445. { 0xb00, SIGILL }, /* reserved */
  446. { 0xc00, SIGCHLD }, /* syscall */
  447. { 0xd00, SIGILL }, /* reserved */
  448. { 0xe00, SIGILL }, /* reserved */
  449. { 0xf00, SIGILL }, /* reserved */
  450. /*
  451. ** 0x1000 PIT
  452. ** 0x1010 FIT
  453. ** 0x1020 watchdog
  454. ** 0x1100 data TLB miss
  455. ** 0x1200 instruction TLB miss
  456. */
  457. { 0x2002, SIGTRAP}, /* debug */
  458. #else
  459. { 0x200, SIGSEGV }, /* machine check */
  460. { 0x300, SIGSEGV }, /* address error (store) */
  461. { 0x400, SIGBUS }, /* instruction bus error */
  462. { 0x500, SIGINT }, /* interrupt */
  463. { 0x600, SIGBUS }, /* alingment */
  464. { 0x700, SIGTRAP }, /* breakpoint trap */
  465. { 0x800, SIGFPE }, /* fpu unavail */
  466. { 0x900, SIGALRM }, /* decrementer */
  467. { 0xa00, SIGILL }, /* reserved */
  468. { 0xb00, SIGILL }, /* reserved */
  469. { 0xc00, SIGCHLD }, /* syscall */
  470. { 0xd00, SIGTRAP }, /* single-step/watch */
  471. { 0xe00, SIGFPE }, /* fp assist */
  472. #endif
  473. { 0, 0} /* Must be last */
  474. };
  475. static int computeSignal(unsigned int tt)
  476. {
  477. struct hard_trap_info *ht;
  478. for (ht = hard_trap_info; ht->tt && ht->signo; ht++)
  479. if (ht->tt == tt)
  480. return ht->signo;
  481. return SIGHUP; /* default for things we don't know about */
  482. }
  483. #define PC_REGNUM 64
  484. #define SP_REGNUM 1
  485. /*
  486. * This function does all command processing for interfacing to gdb.
  487. */
  488. static int
  489. handle_exception (struct pt_regs *regs)
  490. {
  491. int sigval;
  492. int addr;
  493. int length;
  494. char *ptr;
  495. unsigned int msr;
  496. /* We don't handle user-mode breakpoints. */
  497. if (user_mode(regs))
  498. return 0;
  499. if (debugger_fault_handler) {
  500. debugger_fault_handler(regs);
  501. panic("kgdb longjump failed!\n");
  502. }
  503. if (kgdb_active) {
  504. printk(KERN_ERR "interrupt while in kgdb, returning\n");
  505. return 0;
  506. }
  507. kgdb_active = 1;
  508. kgdb_started = 1;
  509. #ifdef KGDB_DEBUG
  510. printk("kgdb: entering handle_exception; trap [0x%x]\n",
  511. (unsigned int)regs->trap);
  512. #endif
  513. kgdb_interruptible(0);
  514. lock_kernel();
  515. msr = mfmsr();
  516. mtmsr(msr & ~MSR_EE); /* disable interrupts */
  517. if (regs->nip == (unsigned long)breakinst) {
  518. /* Skip over breakpoint trap insn */
  519. regs->nip += 4;
  520. }
  521. /* reply to host that an exception has occurred */
  522. sigval = computeSignal(regs->trap);
  523. ptr = remcomOutBuffer;
  524. *ptr++ = 'T';
  525. *ptr++ = hexchars[sigval >> 4];
  526. *ptr++ = hexchars[sigval & 0xf];
  527. *ptr++ = hexchars[PC_REGNUM >> 4];
  528. *ptr++ = hexchars[PC_REGNUM & 0xf];
  529. *ptr++ = ':';
  530. ptr = mem2hex((char *)&regs->nip, ptr, 4);
  531. *ptr++ = ';';
  532. *ptr++ = hexchars[SP_REGNUM >> 4];
  533. *ptr++ = hexchars[SP_REGNUM & 0xf];
  534. *ptr++ = ':';
  535. ptr = mem2hex(((char *)regs) + SP_REGNUM*4, ptr, 4);
  536. *ptr++ = ';';
  537. *ptr++ = 0;
  538. putpacket(remcomOutBuffer);
  539. if (kdebug)
  540. printk("remcomOutBuffer: %s\n", remcomOutBuffer);
  541. /* XXX We may want to add some features dealing with poking the
  542. * XXX page tables, ... (look at sparc-stub.c for more info)
  543. * XXX also required hacking to the gdb sources directly...
  544. */
  545. while (1) {
  546. remcomOutBuffer[0] = 0;
  547. getpacket(remcomInBuffer);
  548. switch (remcomInBuffer[0]) {
  549. case '?': /* report most recent signal */
  550. remcomOutBuffer[0] = 'S';
  551. remcomOutBuffer[1] = hexchars[sigval >> 4];
  552. remcomOutBuffer[2] = hexchars[sigval & 0xf];
  553. remcomOutBuffer[3] = 0;
  554. break;
  555. #if 0
  556. case 'q': /* this screws up gdb for some reason...*/
  557. {
  558. extern long _start, sdata, __bss_start;
  559. ptr = &remcomInBuffer[1];
  560. if (strncmp(ptr, "Offsets", 7) != 0)
  561. break;
  562. ptr = remcomOutBuffer;
  563. sprintf(ptr, "Text=%8.8x;Data=%8.8x;Bss=%8.8x",
  564. &_start, &sdata, &__bss_start);
  565. break;
  566. }
  567. #endif
  568. case 'd':
  569. /* toggle debug flag */
  570. kdebug ^= 1;
  571. break;
  572. case 'g': /* return the value of the CPU registers.
  573. * some of them are non-PowerPC names :(
  574. * they are stored in gdb like:
  575. * struct {
  576. * u32 gpr[32];
  577. * f64 fpr[32];
  578. * u32 pc, ps, cnd, lr; (ps=msr)
  579. * u32 cnt, xer, mq;
  580. * }
  581. */
  582. {
  583. int i;
  584. ptr = remcomOutBuffer;
  585. /* General Purpose Regs */
  586. ptr = mem2hex((char *)regs, ptr, 32 * 4);
  587. /* Floating Point Regs - FIXME */
  588. /*ptr = mem2hex((char *), ptr, 32 * 8);*/
  589. for(i=0; i<(32*8*2); i++) { /* 2chars/byte */
  590. ptr[i] = '0';
  591. }
  592. ptr += 32*8*2;
  593. /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
  594. ptr = mem2hex((char *)&regs->nip, ptr, 4);
  595. ptr = mem2hex((char *)&regs->msr, ptr, 4);
  596. ptr = mem2hex((char *)&regs->ccr, ptr, 4);
  597. ptr = mem2hex((char *)&regs->link, ptr, 4);
  598. ptr = mem2hex((char *)&regs->ctr, ptr, 4);
  599. ptr = mem2hex((char *)&regs->xer, ptr, 4);
  600. }
  601. break;
  602. case 'G': /* set the value of the CPU registers */
  603. {
  604. ptr = &remcomInBuffer[1];
  605. /*
  606. * If the stack pointer has moved, you should pray.
  607. * (cause only god can help you).
  608. */
  609. /* General Purpose Regs */
  610. hex2mem(ptr, (char *)regs, 32 * 4);
  611. /* Floating Point Regs - FIXME?? */
  612. /*ptr = hex2mem(ptr, ??, 32 * 8);*/
  613. ptr += 32*8*2;
  614. /* pc, msr, cr, lr, ctr, xer, (mq is unused) */
  615. ptr = hex2mem(ptr, (char *)&regs->nip, 4);
  616. ptr = hex2mem(ptr, (char *)&regs->msr, 4);
  617. ptr = hex2mem(ptr, (char *)&regs->ccr, 4);
  618. ptr = hex2mem(ptr, (char *)&regs->link, 4);
  619. ptr = hex2mem(ptr, (char *)&regs->ctr, 4);
  620. ptr = hex2mem(ptr, (char *)&regs->xer, 4);
  621. strcpy(remcomOutBuffer,"OK");
  622. }
  623. break;
  624. case 'H':
  625. /* don't do anything, yet, just acknowledge */
  626. hexToInt(&ptr, &addr);
  627. strcpy(remcomOutBuffer,"OK");
  628. break;
  629. case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
  630. /* Try to read %x,%x. */
  631. ptr = &remcomInBuffer[1];
  632. if (hexToInt(&ptr, &addr) && *ptr++ == ','
  633. && hexToInt(&ptr, &length)) {
  634. if (mem2hex((char *)addr, remcomOutBuffer,
  635. length))
  636. break;
  637. strcpy(remcomOutBuffer, "E03");
  638. } else
  639. strcpy(remcomOutBuffer, "E01");
  640. break;
  641. case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
  642. /* Try to read '%x,%x:'. */
  643. ptr = &remcomInBuffer[1];
  644. if (hexToInt(&ptr, &addr) && *ptr++ == ','
  645. && hexToInt(&ptr, &length)
  646. && *ptr++ == ':') {
  647. if (hex2mem(ptr, (char *)addr, length))
  648. strcpy(remcomOutBuffer, "OK");
  649. else
  650. strcpy(remcomOutBuffer, "E03");
  651. flush_icache_range(addr, addr+length);
  652. } else
  653. strcpy(remcomOutBuffer, "E02");
  654. break;
  655. case 'k': /* kill the program, actually just continue */
  656. case 'c': /* cAA..AA Continue; address AA..AA optional */
  657. /* try to read optional parameter, pc unchanged if no parm */
  658. ptr = &remcomInBuffer[1];
  659. if (hexToInt(&ptr, &addr))
  660. regs->nip = addr;
  661. /* Need to flush the instruction cache here, as we may have deposited a
  662. * breakpoint, and the icache probably has no way of knowing that a data ref to
  663. * some location may have changed something that is in the instruction cache.
  664. */
  665. kgdb_flush_cache_all();
  666. mtmsr(msr);
  667. kgdb_interruptible(1);
  668. unlock_kernel();
  669. kgdb_active = 0;
  670. if (kdebug) {
  671. printk("remcomInBuffer: %s\n", remcomInBuffer);
  672. printk("remcomOutBuffer: %s\n", remcomOutBuffer);
  673. }
  674. return 1;
  675. case 's':
  676. kgdb_flush_cache_all();
  677. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  678. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | DBCR0_IC);
  679. regs->msr |= MSR_DE;
  680. #else
  681. regs->msr |= MSR_SE;
  682. #endif
  683. unlock_kernel();
  684. kgdb_active = 0;
  685. if (kdebug) {
  686. printk("remcomInBuffer: %s\n", remcomInBuffer);
  687. printk("remcomOutBuffer: %s\n", remcomOutBuffer);
  688. }
  689. return 1;
  690. case 'r': /* Reset (if user process..exit ???)*/
  691. panic("kgdb reset.");
  692. break;
  693. } /* switch */
  694. if (remcomOutBuffer[0] && kdebug) {
  695. printk("remcomInBuffer: %s\n", remcomInBuffer);
  696. printk("remcomOutBuffer: %s\n", remcomOutBuffer);
  697. }
  698. /* reply to the request */
  699. putpacket(remcomOutBuffer);
  700. } /* while(1) */
  701. }
  702. /* This function will generate a breakpoint exception. It is used at the
  703. beginning of a program to sync up with a debugger and can be used
  704. otherwise as a quick means to stop program execution and "break" into
  705. the debugger. */
  706. void
  707. breakpoint(void)
  708. {
  709. if (!initialized) {
  710. printk("breakpoint() called b4 kgdb init\n");
  711. return;
  712. }
  713. asm(" .globl breakinst \n\
  714. breakinst: .long 0x7d821008");
  715. }
  716. #ifdef CONFIG_KGDB_CONSOLE
  717. /* Output string in GDB O-packet format if GDB has connected. If nothing
  718. output, returns 0 (caller must then handle output). */
  719. int
  720. kgdb_output_string (const char* s, unsigned int count)
  721. {
  722. char buffer[512];
  723. if (!kgdb_started)
  724. return 0;
  725. count = (count <= (sizeof(buffer) / 2 - 2))
  726. ? count : (sizeof(buffer) / 2 - 2);
  727. buffer[0] = 'O';
  728. mem2hex (s, &buffer[1], count);
  729. putpacket(buffer);
  730. return 1;
  731. }
  732. #endif
  733. static void sysrq_handle_gdb(int key, struct pt_regs *pt_regs,
  734. struct tty_struct *tty)
  735. {
  736. printk("Entering GDB stub\n");
  737. breakpoint();
  738. }
  739. static struct sysrq_key_op sysrq_gdb_op = {
  740. .handler = sysrq_handle_gdb,
  741. .help_msg = "Gdb",
  742. .action_msg = "GDB",
  743. };
  744. static int gdb_register_sysrq(void)
  745. {
  746. printk("Registering GDB sysrq handler\n");
  747. register_sysrq_key('g', &sysrq_gdb_op);
  748. return 0;
  749. }
  750. module_init(gdb_register_sysrq);