ppc-stub.c 22 KB

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