PageRenderTime 50ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/cris/arch-v10/kernel/kgdb.c

https://bitbucket.org/evzijst/gittest
C | 1568 lines | 876 code | 153 blank | 539 comment | 150 complexity | 596e54a88887cfd1f9fd57c53a7174b8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /*!**************************************************************************
  2. *!
  3. *! FILE NAME : kgdb.c
  4. *!
  5. *! DESCRIPTION: Implementation of the gdb stub with respect to ETRAX 100.
  6. *! It is a mix of arch/m68k/kernel/kgdb.c and cris_stub.c.
  7. *!
  8. *!---------------------------------------------------------------------------
  9. *! HISTORY
  10. *!
  11. *! DATE NAME CHANGES
  12. *! ---- ---- -------
  13. *! Apr 26 1999 Hendrik Ruijter Initial version.
  14. *! May 6 1999 Hendrik Ruijter Removed call to strlen in libc and removed
  15. *! struct assignment as it generates calls to
  16. *! memcpy in libc.
  17. *! Jun 17 1999 Hendrik Ruijter Added gdb 4.18 support. 'X', 'qC' and 'qL'.
  18. *! Jul 21 1999 Bjorn Wesen eLinux port
  19. *!
  20. *! $Log: kgdb.c,v $
  21. *! Revision 1.5 2004/10/07 13:59:08 starvik
  22. *! Corrected call to set_int_vector
  23. *!
  24. *! Revision 1.4 2003/04/09 05:20:44 starvik
  25. *! Merge of Linux 2.5.67
  26. *!
  27. *! Revision 1.3 2003/01/21 19:11:08 starvik
  28. *! Modified include path for new dir layout
  29. *!
  30. *! Revision 1.2 2002/11/19 14:35:24 starvik
  31. *! Changes from linux 2.4
  32. *! Changed struct initializer syntax to the currently prefered notation
  33. *!
  34. *! Revision 1.1 2001/12/17 13:59:27 bjornw
  35. *! Initial revision
  36. *!
  37. *! Revision 1.6 2001/10/09 13:10:03 matsfg
  38. *! Added $ on registers and removed some underscores
  39. *!
  40. *! Revision 1.5 2001/04/17 13:58:39 orjanf
  41. *! * Renamed CONFIG_KGDB to CONFIG_ETRAX_KGDB.
  42. *!
  43. *! Revision 1.4 2001/02/23 13:45:19 bjornw
  44. *! config.h check
  45. *!
  46. *! Revision 1.3 2001/01/31 18:08:23 orjanf
  47. *! Removed kgdb_handle_breakpoint from being the break 8 handler.
  48. *!
  49. *! Revision 1.2 2001/01/12 14:22:25 orjanf
  50. *! Updated kernel debugging support to work with ETRAX 100LX.
  51. *!
  52. *! Revision 1.1 2000/07/10 16:25:21 bjornw
  53. *! Initial revision
  54. *!
  55. *! Revision 1.1.1.1 1999/12/03 14:57:31 bjornw
  56. *! * Initial version of arch/cris, the latest CRIS architecture with an MMU.
  57. *! Mostly copied from arch/etrax100 with appropriate renames of files.
  58. *! The mm/ subdir is copied from arch/i386.
  59. *! This does not compile yet at all.
  60. *!
  61. *!
  62. *! Revision 1.4 1999/07/22 17:25:25 bjornw
  63. *! Dont wait for + in putpacket if we havent hit the initial breakpoint yet. Added a kgdb_init function which sets up the break and irq vectors.
  64. *!
  65. *! Revision 1.3 1999/07/21 19:51:18 bjornw
  66. *! Check if the interrupting char is a ctrl-C, ignore otherwise.
  67. *!
  68. *! Revision 1.2 1999/07/21 18:09:39 bjornw
  69. *! Ported to eLinux architecture, and added some kgdb documentation.
  70. *!
  71. *!
  72. *!---------------------------------------------------------------------------
  73. *!
  74. *! $Id: kgdb.c,v 1.5 2004/10/07 13:59:08 starvik Exp $
  75. *!
  76. *! (C) Copyright 1999, Axis Communications AB, LUND, SWEDEN
  77. *!
  78. *!**************************************************************************/
  79. /* @(#) cris_stub.c 1.3 06/17/99 */
  80. /*
  81. * kgdb usage notes:
  82. * -----------------
  83. *
  84. * If you select CONFIG_ETRAX_KGDB in the configuration, the kernel will be
  85. * built with different gcc flags: "-g" is added to get debug infos, and
  86. * "-fomit-frame-pointer" is omitted to make debugging easier. Since the
  87. * resulting kernel will be quite big (approx. > 7 MB), it will be stripped
  88. * before compresion. Such a kernel will behave just as usually, except if
  89. * given a "debug=<device>" command line option. (Only serial devices are
  90. * allowed for <device>, i.e. no printers or the like; possible values are
  91. * machine depedend and are the same as for the usual debug device, the one
  92. * for logging kernel messages.) If that option is given and the device can be
  93. * initialized, the kernel will connect to the remote gdb in trap_init(). The
  94. * serial parameters are fixed to 8N1 and 115200 bps, for easyness of
  95. * implementation.
  96. *
  97. * To start a debugging session, start that gdb with the debugging kernel
  98. * image (the one with the symbols, vmlinux.debug) named on the command line.
  99. * This file will be used by gdb to get symbol and debugging infos about the
  100. * kernel. Next, select remote debug mode by
  101. * target remote <device>
  102. * where <device> is the name of the serial device over which the debugged
  103. * machine is connected. Maybe you have to adjust the baud rate by
  104. * set remotebaud <rate>
  105. * or also other parameters with stty:
  106. * shell stty ... </dev/...
  107. * If the kernel to debug has already booted, it waited for gdb and now
  108. * connects, and you'll see a breakpoint being reported. If the kernel isn't
  109. * running yet, start it now. The order of gdb and the kernel doesn't matter.
  110. * Another thing worth knowing about in the getting-started phase is how to
  111. * debug the remote protocol itself. This is activated with
  112. * set remotedebug 1
  113. * gdb will then print out each packet sent or received. You'll also get some
  114. * messages about the gdb stub on the console of the debugged machine.
  115. *
  116. * If all that works, you can use lots of the usual debugging techniques on
  117. * the kernel, e.g. inspecting and changing variables/memory, setting
  118. * breakpoints, single stepping and so on. It's also possible to interrupt the
  119. * debugged kernel by pressing C-c in gdb. Have fun! :-)
  120. *
  121. * The gdb stub is entered (and thus the remote gdb gets control) in the
  122. * following situations:
  123. *
  124. * - If breakpoint() is called. This is just after kgdb initialization, or if
  125. * a breakpoint() call has been put somewhere into the kernel source.
  126. * (Breakpoints can of course also be set the usual way in gdb.)
  127. * In eLinux, we call breakpoint() in init/main.c after IRQ initialization.
  128. *
  129. * - If there is a kernel exception, i.e. bad_super_trap() or die_if_kernel()
  130. * are entered. All the CPU exceptions are mapped to (more or less..., see
  131. * the hard_trap_info array below) appropriate signal, which are reported
  132. * to gdb. die_if_kernel() is usually called after some kind of access
  133. * error and thus is reported as SIGSEGV.
  134. *
  135. * - When panic() is called. This is reported as SIGABRT.
  136. *
  137. * - If C-c is received over the serial line, which is treated as
  138. * SIGINT.
  139. *
  140. * Of course, all these signals are just faked for gdb, since there is no
  141. * signal concept as such for the kernel. It also isn't possible --obviously--
  142. * to set signal handlers from inside gdb, or restart the kernel with a
  143. * signal.
  144. *
  145. * Current limitations:
  146. *
  147. * - While the kernel is stopped, interrupts are disabled for safety reasons
  148. * (i.e., variables not changing magically or the like). But this also
  149. * means that the clock isn't running anymore, and that interrupts from the
  150. * hardware may get lost/not be served in time. This can cause some device
  151. * errors...
  152. *
  153. * - When single-stepping, only one instruction of the current thread is
  154. * executed, but interrupts are allowed for that time and will be serviced
  155. * if pending. Be prepared for that.
  156. *
  157. * - All debugging happens in kernel virtual address space. There's no way to
  158. * access physical memory not mapped in kernel space, or to access user
  159. * space. A way to work around this is using get_user_long & Co. in gdb
  160. * expressions, but only for the current process.
  161. *
  162. * - Interrupting the kernel only works if interrupts are currently allowed,
  163. * and the interrupt of the serial line isn't blocked by some other means
  164. * (IPL too high, disabled, ...)
  165. *
  166. * - The gdb stub is currently not reentrant, i.e. errors that happen therein
  167. * (e.g. accessing invalid memory) may not be caught correctly. This could
  168. * be removed in future by introducing a stack of struct registers.
  169. *
  170. */
  171. /*
  172. * To enable debugger support, two things need to happen. One, a
  173. * call to kgdb_init() is necessary in order to allow any breakpoints
  174. * or error conditions to be properly intercepted and reported to gdb.
  175. * Two, a breakpoint needs to be generated to begin communication. This
  176. * is most easily accomplished by a call to breakpoint().
  177. *
  178. * The following gdb commands are supported:
  179. *
  180. * command function Return value
  181. *
  182. * g return the value of the CPU registers hex data or ENN
  183. * G set the value of the CPU registers OK or ENN
  184. *
  185. * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
  186. * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
  187. *
  188. * c Resume at current address SNN ( signal NN)
  189. * cAA..AA Continue at address AA..AA SNN
  190. *
  191. * s Step one instruction SNN
  192. * sAA..AA Step one instruction from AA..AA SNN
  193. *
  194. * k kill
  195. *
  196. * ? What was the last sigval ? SNN (signal NN)
  197. *
  198. * bBB..BB Set baud rate to BB..BB OK or BNN, then sets
  199. * baud rate
  200. *
  201. * All commands and responses are sent with a packet which includes a
  202. * checksum. A packet consists of
  203. *
  204. * $<packet info>#<checksum>.
  205. *
  206. * where
  207. * <packet info> :: <characters representing the command or response>
  208. * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>>
  209. *
  210. * When a packet is received, it is first acknowledged with either '+' or '-'.
  211. * '+' indicates a successful transfer. '-' indicates a failed transfer.
  212. *
  213. * Example:
  214. *
  215. * Host: Reply:
  216. * $m0,10#2a +$00010203040506070809101112131415#42
  217. *
  218. */
  219. #include <linux/string.h>
  220. #include <linux/signal.h>
  221. #include <linux/kernel.h>
  222. #include <linux/delay.h>
  223. #include <linux/linkage.h>
  224. #include <asm/setup.h>
  225. #include <asm/ptrace.h>
  226. #include <asm/arch/svinto.h>
  227. #include <asm/irq.h>
  228. static int kgdb_started = 0;
  229. /********************************* Register image ****************************/
  230. /* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
  231. Reference", p. 1-1, with the additional register definitions of the
  232. ETRAX 100LX in cris-opc.h.
  233. There are 16 general 32-bit registers, R0-R15, where R14 is the stack
  234. pointer, SP, and R15 is the program counter, PC.
  235. There are 16 special registers, P0-P15, where three of the unimplemented
  236. registers, P0, P4 and P8, are reserved as zero-registers. A read from
  237. any of these registers returns zero and a write has no effect. */
  238. typedef
  239. struct register_image
  240. {
  241. /* Offset */
  242. unsigned int r0; /* 0x00 */
  243. unsigned int r1; /* 0x04 */
  244. unsigned int r2; /* 0x08 */
  245. unsigned int r3; /* 0x0C */
  246. unsigned int r4; /* 0x10 */
  247. unsigned int r5; /* 0x14 */
  248. unsigned int r6; /* 0x18 */
  249. unsigned int r7; /* 0x1C */
  250. unsigned int r8; /* 0x20 Frame pointer */
  251. unsigned int r9; /* 0x24 */
  252. unsigned int r10; /* 0x28 */
  253. unsigned int r11; /* 0x2C */
  254. unsigned int r12; /* 0x30 */
  255. unsigned int r13; /* 0x34 */
  256. unsigned int sp; /* 0x38 Stack pointer */
  257. unsigned int pc; /* 0x3C Program counter */
  258. unsigned char p0; /* 0x40 8-bit zero-register */
  259. unsigned char vr; /* 0x41 Version register */
  260. unsigned short p4; /* 0x42 16-bit zero-register */
  261. unsigned short ccr; /* 0x44 Condition code register */
  262. unsigned int mof; /* 0x46 Multiply overflow register */
  263. unsigned int p8; /* 0x4A 32-bit zero-register */
  264. unsigned int ibr; /* 0x4E Interrupt base register */
  265. unsigned int irp; /* 0x52 Interrupt return pointer */
  266. unsigned int srp; /* 0x56 Subroutine return pointer */
  267. unsigned int bar; /* 0x5A Breakpoint address register */
  268. unsigned int dccr; /* 0x5E Double condition code register */
  269. unsigned int brp; /* 0x62 Breakpoint return pointer (pc in caller) */
  270. unsigned int usp; /* 0x66 User mode stack pointer */
  271. } registers;
  272. /************** Prototypes for local library functions ***********************/
  273. /* Copy of strcpy from libc. */
  274. static char *gdb_cris_strcpy (char *s1, const char *s2);
  275. /* Copy of strlen from libc. */
  276. static int gdb_cris_strlen (const char *s);
  277. /* Copy of memchr from libc. */
  278. static void *gdb_cris_memchr (const void *s, int c, int n);
  279. /* Copy of strtol from libc. Does only support base 16. */
  280. static int gdb_cris_strtol (const char *s, char **endptr, int base);
  281. /********************** Prototypes for local functions. **********************/
  282. /* Copy the content of a register image into another. The size n is
  283. the size of the register image. Due to struct assignment generation of
  284. memcpy in libc. */
  285. static void copy_registers (registers *dptr, registers *sptr, int n);
  286. /* Copy the stored registers from the stack. Put the register contents
  287. of thread thread_id in the struct reg. */
  288. static void copy_registers_from_stack (int thread_id, registers *reg);
  289. /* Copy the registers to the stack. Put the register contents of thread
  290. thread_id from struct reg to the stack. */
  291. static void copy_registers_to_stack (int thread_id, registers *reg);
  292. /* Write a value to a specified register regno in the register image
  293. of the current thread. */
  294. static int write_register (int regno, char *val);
  295. /* Write a value to a specified register in the stack of a thread other
  296. than the current thread. */
  297. static write_stack_register (int thread_id, int regno, char *valptr);
  298. /* Read a value from a specified register in the register image. Returns the
  299. status of the read operation. The register value is returned in valptr. */
  300. static int read_register (char regno, unsigned int *valptr);
  301. /* Serial port, reads one character. ETRAX 100 specific. from debugport.c */
  302. int getDebugChar (void);
  303. /* Serial port, writes one character. ETRAX 100 specific. from debugport.c */
  304. void putDebugChar (int val);
  305. void enableDebugIRQ (void);
  306. /* Returns the character equivalent of a nibble, bit 7, 6, 5, and 4 of a byte,
  307. represented by int x. */
  308. static char highhex (int x);
  309. /* Returns the character equivalent of a nibble, bit 3, 2, 1, and 0 of a byte,
  310. represented by int x. */
  311. static char lowhex (int x);
  312. /* Returns the integer equivalent of a hexadecimal character. */
  313. static int hex (char ch);
  314. /* Convert the memory, pointed to by mem into hexadecimal representation.
  315. Put the result in buf, and return a pointer to the last character
  316. in buf (null). */
  317. static char *mem2hex (char *buf, unsigned char *mem, int count);
  318. /* Convert the array, in hexadecimal representation, pointed to by buf into
  319. binary representation. Put the result in mem, and return a pointer to
  320. the character after the last byte written. */
  321. static unsigned char *hex2mem (unsigned char *mem, char *buf, int count);
  322. /* Put the content of the array, in binary representation, pointed to by buf
  323. into memory pointed to by mem, and return a pointer to
  324. the character after the last byte written. */
  325. static unsigned char *bin2mem (unsigned char *mem, unsigned char *buf, int count);
  326. /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
  327. returned. */
  328. static void getpacket (char *buffer);
  329. /* Send $<data>#<checksum> from the <data> in the array buffer. */
  330. static void putpacket (char *buffer);
  331. /* Build and send a response packet in order to inform the host the
  332. stub is stopped. */
  333. static void stub_is_stopped (int sigval);
  334. /* All expected commands are sent from remote.c. Send a response according
  335. to the description in remote.c. */
  336. static void handle_exception (int sigval);
  337. /* Performs a complete re-start from scratch. ETRAX specific. */
  338. static void kill_restart (void);
  339. /******************** Prototypes for global functions. ***********************/
  340. /* The string str is prepended with the GDB printout token and sent. */
  341. void putDebugString (const unsigned char *str, int length); /* used by etrax100ser.c */
  342. /* The hook for both static (compiled) and dynamic breakpoints set by GDB.
  343. ETRAX 100 specific. */
  344. void handle_breakpoint (void); /* used by irq.c */
  345. /* The hook for an interrupt generated by GDB. ETRAX 100 specific. */
  346. void handle_interrupt (void); /* used by irq.c */
  347. /* A static breakpoint to be used at startup. */
  348. void breakpoint (void); /* called by init/main.c */
  349. /* From osys_int.c, executing_task contains the number of the current
  350. executing task in osys. Does not know of object-oriented threads. */
  351. extern unsigned char executing_task;
  352. /* The number of characters used for a 64 bit thread identifier. */
  353. #define HEXCHARS_IN_THREAD_ID 16
  354. /* Avoid warning as the internal_stack is not used in the C-code. */
  355. #define USEDVAR(name) { if (name) { ; } }
  356. #define USEDFUN(name) { void (*pf)(void) = (void *)name; USEDVAR(pf) }
  357. /********************************** Packet I/O ******************************/
  358. /* BUFMAX defines the maximum number of characters in
  359. inbound/outbound buffers */
  360. #define BUFMAX 512
  361. /* Run-length encoding maximum length. Send 64 at most. */
  362. #define RUNLENMAX 64
  363. /* Definition of all valid hexadecimal characters */
  364. static const char hexchars[] = "0123456789abcdef";
  365. /* The inbound/outbound buffers used in packet I/O */
  366. static char remcomInBuffer[BUFMAX];
  367. static char remcomOutBuffer[BUFMAX];
  368. /* Error and warning messages. */
  369. enum error_type
  370. {
  371. SUCCESS, E01, E02, E03, E04, E05, E06, E07
  372. };
  373. static char *error_message[] =
  374. {
  375. "",
  376. "E01 Set current or general thread - H[c,g] - internal error.",
  377. "E02 Change register content - P - cannot change read-only register.",
  378. "E03 Thread is not alive.", /* T, not used. */
  379. "E04 The command is not supported - [s,C,S,!,R,d,r] - internal error.",
  380. "E05 Change register content - P - the register is not implemented..",
  381. "E06 Change memory content - M - internal error.",
  382. "E07 Change register content - P - the register is not stored on the stack"
  383. };
  384. /********************************* Register image ****************************/
  385. /* Use the order of registers as defined in "AXIS ETRAX CRIS Programmer's
  386. Reference", p. 1-1, with the additional register definitions of the
  387. ETRAX 100LX in cris-opc.h.
  388. There are 16 general 32-bit registers, R0-R15, where R14 is the stack
  389. pointer, SP, and R15 is the program counter, PC.
  390. There are 16 special registers, P0-P15, where three of the unimplemented
  391. registers, P0, P4 and P8, are reserved as zero-registers. A read from
  392. any of these registers returns zero and a write has no effect. */
  393. enum register_name
  394. {
  395. R0, R1, R2, R3,
  396. R4, R5, R6, R7,
  397. R8, R9, R10, R11,
  398. R12, R13, SP, PC,
  399. P0, VR, P2, P3,
  400. P4, CCR, P6, MOF,
  401. P8, IBR, IRP, SRP,
  402. BAR, DCCR, BRP, USP
  403. };
  404. /* The register sizes of the registers in register_name. An unimplemented register
  405. is designated by size 0 in this array. */
  406. static int register_size[] =
  407. {
  408. 4, 4, 4, 4,
  409. 4, 4, 4, 4,
  410. 4, 4, 4, 4,
  411. 4, 4, 4, 4,
  412. 1, 1, 0, 0,
  413. 2, 2, 0, 4,
  414. 4, 4, 4, 4,
  415. 4, 4, 4, 4
  416. };
  417. /* Contains the register image of the executing thread in the assembler
  418. part of the code in order to avoid horrible addressing modes. */
  419. static registers reg;
  420. /* FIXME: Should this be used? Delete otherwise. */
  421. /* Contains the assumed consistency state of the register image. Uses the
  422. enum error_type for state information. */
  423. static int consistency_status = SUCCESS;
  424. /********************************** Handle exceptions ************************/
  425. /* The variable reg contains the register image associated with the
  426. current_thread_c variable. It is a complete register image created at
  427. entry. The reg_g contains a register image of a task where the general
  428. registers are taken from the stack and all special registers are taken
  429. from the executing task. It is associated with current_thread_g and used
  430. in order to provide access mainly for 'g', 'G' and 'P'.
  431. */
  432. /* Need two task id pointers in order to handle Hct and Hgt commands. */
  433. static int current_thread_c = 0;
  434. static int current_thread_g = 0;
  435. /* Need two register images in order to handle Hct and Hgt commands. The
  436. variable reg_g is in addition to reg above. */
  437. static registers reg_g;
  438. /********************************** Breakpoint *******************************/
  439. /* Use an internal stack in the breakpoint and interrupt response routines */
  440. #define INTERNAL_STACK_SIZE 1024
  441. static char internal_stack[INTERNAL_STACK_SIZE];
  442. /* Due to the breakpoint return pointer, a state variable is needed to keep
  443. track of whether it is a static (compiled) or dynamic (gdb-invoked)
  444. breakpoint to be handled. A static breakpoint uses the content of register
  445. BRP as it is whereas a dynamic breakpoint requires subtraction with 2
  446. in order to execute the instruction. The first breakpoint is static. */
  447. static unsigned char is_dyn_brkp = 0;
  448. /********************************* String library ****************************/
  449. /* Single-step over library functions creates trap loops. */
  450. /* Copy char s2[] to s1[]. */
  451. static char*
  452. gdb_cris_strcpy (char *s1, const char *s2)
  453. {
  454. char *s = s1;
  455. for (s = s1; (*s++ = *s2++) != '\0'; )
  456. ;
  457. return (s1);
  458. }
  459. /* Find length of s[]. */
  460. static int
  461. gdb_cris_strlen (const char *s)
  462. {
  463. const char *sc;
  464. for (sc = s; *sc != '\0'; sc++)
  465. ;
  466. return (sc - s);
  467. }
  468. /* Find first occurrence of c in s[n]. */
  469. static void*
  470. gdb_cris_memchr (const void *s, int c, int n)
  471. {
  472. const unsigned char uc = c;
  473. const unsigned char *su;
  474. for (su = s; 0 < n; ++su, --n)
  475. if (*su == uc)
  476. return ((void *)su);
  477. return (NULL);
  478. }
  479. /******************************* Standard library ****************************/
  480. /* Single-step over library functions creates trap loops. */
  481. /* Convert string to long. */
  482. static int
  483. gdb_cris_strtol (const char *s, char **endptr, int base)
  484. {
  485. char *s1;
  486. char *sd;
  487. int x = 0;
  488. for (s1 = (char*)s; (sd = gdb_cris_memchr(hexchars, *s1, base)) != NULL; ++s1)
  489. x = x * base + (sd - hexchars);
  490. if (endptr)
  491. {
  492. /* Unconverted suffix is stored in endptr unless endptr is NULL. */
  493. *endptr = s1;
  494. }
  495. return x;
  496. }
  497. int
  498. double_this(int x)
  499. {
  500. return 2 * x;
  501. }
  502. /********************************* Register image ****************************/
  503. /* Copy the content of a register image into another. The size n is
  504. the size of the register image. Due to struct assignment generation of
  505. memcpy in libc. */
  506. static void
  507. copy_registers (registers *dptr, registers *sptr, int n)
  508. {
  509. unsigned char *dreg;
  510. unsigned char *sreg;
  511. for (dreg = (unsigned char*)dptr, sreg = (unsigned char*)sptr; n > 0; n--)
  512. *dreg++ = *sreg++;
  513. }
  514. #ifdef PROCESS_SUPPORT
  515. /* Copy the stored registers from the stack. Put the register contents
  516. of thread thread_id in the struct reg. */
  517. static void
  518. copy_registers_from_stack (int thread_id, registers *regptr)
  519. {
  520. int j;
  521. stack_registers *s = (stack_registers *)stack_list[thread_id];
  522. unsigned int *d = (unsigned int *)regptr;
  523. for (j = 13; j >= 0; j--)
  524. *d++ = s->r[j];
  525. regptr->sp = (unsigned int)stack_list[thread_id];
  526. regptr->pc = s->pc;
  527. regptr->dccr = s->dccr;
  528. regptr->srp = s->srp;
  529. }
  530. /* Copy the registers to the stack. Put the register contents of thread
  531. thread_id from struct reg to the stack. */
  532. static void
  533. copy_registers_to_stack (int thread_id, registers *regptr)
  534. {
  535. int i;
  536. stack_registers *d = (stack_registers *)stack_list[thread_id];
  537. unsigned int *s = (unsigned int *)regptr;
  538. for (i = 0; i < 14; i++) {
  539. d->r[i] = *s++;
  540. }
  541. d->pc = regptr->pc;
  542. d->dccr = regptr->dccr;
  543. d->srp = regptr->srp;
  544. }
  545. #endif
  546. /* Write a value to a specified register in the register image of the current
  547. thread. Returns status code SUCCESS, E02 or E05. */
  548. static int
  549. write_register (int regno, char *val)
  550. {
  551. int status = SUCCESS;
  552. registers *current_reg = &reg;
  553. if (regno >= R0 && regno <= PC) {
  554. /* 32-bit register with simple offset. */
  555. hex2mem ((unsigned char *)current_reg + regno * sizeof(unsigned int),
  556. val, sizeof(unsigned int));
  557. }
  558. else if (regno == P0 || regno == VR || regno == P4 || regno == P8) {
  559. /* Do not support read-only registers. */
  560. status = E02;
  561. }
  562. else if (regno == CCR) {
  563. /* 16 bit register with complex offset. (P4 is read-only, P6 is not implemented,
  564. and P7 (MOF) is 32 bits in ETRAX 100LX. */
  565. hex2mem ((unsigned char *)&(current_reg->ccr) + (regno-CCR) * sizeof(unsigned short),
  566. val, sizeof(unsigned short));
  567. }
  568. else if (regno >= MOF && regno <= USP) {
  569. /* 32 bit register with complex offset. (P8 has been taken care of.) */
  570. hex2mem ((unsigned char *)&(current_reg->ibr) + (regno-IBR) * sizeof(unsigned int),
  571. val, sizeof(unsigned int));
  572. }
  573. else {
  574. /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
  575. status = E05;
  576. }
  577. return status;
  578. }
  579. #ifdef PROCESS_SUPPORT
  580. /* Write a value to a specified register in the stack of a thread other
  581. than the current thread. Returns status code SUCCESS or E07. */
  582. static int
  583. write_stack_register (int thread_id, int regno, char *valptr)
  584. {
  585. int status = SUCCESS;
  586. stack_registers *d = (stack_registers *)stack_list[thread_id];
  587. unsigned int val;
  588. hex2mem ((unsigned char *)&val, valptr, sizeof(unsigned int));
  589. if (regno >= R0 && regno < SP) {
  590. d->r[regno] = val;
  591. }
  592. else if (regno == SP) {
  593. stack_list[thread_id] = val;
  594. }
  595. else if (regno == PC) {
  596. d->pc = val;
  597. }
  598. else if (regno == SRP) {
  599. d->srp = val;
  600. }
  601. else if (regno == DCCR) {
  602. d->dccr = val;
  603. }
  604. else {
  605. /* Do not support registers in the current thread. */
  606. status = E07;
  607. }
  608. return status;
  609. }
  610. #endif
  611. /* Read a value from a specified register in the register image. Returns the
  612. value in the register or -1 for non-implemented registers.
  613. Should check consistency_status after a call which may be E05 after changes
  614. in the implementation. */
  615. static int
  616. read_register (char regno, unsigned int *valptr)
  617. {
  618. registers *current_reg = &reg;
  619. if (regno >= R0 && regno <= PC) {
  620. /* 32-bit register with simple offset. */
  621. *valptr = *(unsigned int *)((char *)current_reg + regno * sizeof(unsigned int));
  622. return SUCCESS;
  623. }
  624. else if (regno == P0 || regno == VR) {
  625. /* 8 bit register with complex offset. */
  626. *valptr = (unsigned int)(*(unsigned char *)
  627. ((char *)&(current_reg->p0) + (regno-P0) * sizeof(char)));
  628. return SUCCESS;
  629. }
  630. else if (regno == P4 || regno == CCR) {
  631. /* 16 bit register with complex offset. */
  632. *valptr = (unsigned int)(*(unsigned short *)
  633. ((char *)&(current_reg->p4) + (regno-P4) * sizeof(unsigned short)));
  634. return SUCCESS;
  635. }
  636. else if (regno >= MOF && regno <= USP) {
  637. /* 32 bit register with complex offset. */
  638. *valptr = *(unsigned int *)((char *)&(current_reg->p8)
  639. + (regno-P8) * sizeof(unsigned int));
  640. return SUCCESS;
  641. }
  642. else {
  643. /* Do not support nonexisting or unimplemented registers (P2, P3, and P6). */
  644. consistency_status = E05;
  645. return E05;
  646. }
  647. }
  648. /********************************** Packet I/O ******************************/
  649. /* Returns the character equivalent of a nibble, bit 7, 6, 5, and 4 of a byte,
  650. represented by int x. */
  651. static inline char
  652. highhex(int x)
  653. {
  654. return hexchars[(x >> 4) & 0xf];
  655. }
  656. /* Returns the character equivalent of a nibble, bit 3, 2, 1, and 0 of a byte,
  657. represented by int x. */
  658. static inline char
  659. lowhex(int x)
  660. {
  661. return hexchars[x & 0xf];
  662. }
  663. /* Returns the integer equivalent of a hexadecimal character. */
  664. static int
  665. hex (char ch)
  666. {
  667. if ((ch >= 'a') && (ch <= 'f'))
  668. return (ch - 'a' + 10);
  669. if ((ch >= '0') && (ch <= '9'))
  670. return (ch - '0');
  671. if ((ch >= 'A') && (ch <= 'F'))
  672. return (ch - 'A' + 10);
  673. return (-1);
  674. }
  675. /* Convert the memory, pointed to by mem into hexadecimal representation.
  676. Put the result in buf, and return a pointer to the last character
  677. in buf (null). */
  678. static int do_printk = 0;
  679. static char *
  680. mem2hex(char *buf, unsigned char *mem, int count)
  681. {
  682. int i;
  683. int ch;
  684. if (mem == NULL) {
  685. /* Bogus read from m0. FIXME: What constitutes a valid address? */
  686. for (i = 0; i < count; i++) {
  687. *buf++ = '0';
  688. *buf++ = '0';
  689. }
  690. } else {
  691. /* Valid mem address. */
  692. for (i = 0; i < count; i++) {
  693. ch = *mem++;
  694. *buf++ = highhex (ch);
  695. *buf++ = lowhex (ch);
  696. }
  697. }
  698. /* Terminate properly. */
  699. *buf = '\0';
  700. return (buf);
  701. }
  702. /* Convert the array, in hexadecimal representation, pointed to by buf into
  703. binary representation. Put the result in mem, and return a pointer to
  704. the character after the last byte written. */
  705. static unsigned char*
  706. hex2mem (unsigned char *mem, char *buf, int count)
  707. {
  708. int i;
  709. unsigned char ch;
  710. for (i = 0; i < count; i++) {
  711. ch = hex (*buf++) << 4;
  712. ch = ch + hex (*buf++);
  713. *mem++ = ch;
  714. }
  715. return (mem);
  716. }
  717. /* Put the content of the array, in binary representation, pointed to by buf
  718. into memory pointed to by mem, and return a pointer to the character after
  719. the last byte written.
  720. Gdb will escape $, #, and the escape char (0x7d). */
  721. static unsigned char*
  722. bin2mem (unsigned char *mem, unsigned char *buf, int count)
  723. {
  724. int i;
  725. unsigned char *next;
  726. for (i = 0; i < count; i++) {
  727. /* Check for any escaped characters. Be paranoid and
  728. only unescape chars that should be escaped. */
  729. if (*buf == 0x7d) {
  730. next = buf + 1;
  731. if (*next == 0x3 || *next == 0x4 || *next == 0x5D) /* #, $, ESC */
  732. {
  733. buf++;
  734. *buf += 0x20;
  735. }
  736. }
  737. *mem++ = *buf++;
  738. }
  739. return (mem);
  740. }
  741. /* Await the sequence $<data>#<checksum> and store <data> in the array buffer
  742. returned. */
  743. static void
  744. getpacket (char *buffer)
  745. {
  746. unsigned char checksum;
  747. unsigned char xmitcsum;
  748. int i;
  749. int count;
  750. char ch;
  751. do {
  752. while ((ch = getDebugChar ()) != '$')
  753. /* Wait for the start character $ and ignore all other characters */;
  754. checksum = 0;
  755. xmitcsum = -1;
  756. count = 0;
  757. /* Read until a # or the end of the buffer is reached */
  758. while (count < BUFMAX) {
  759. ch = getDebugChar ();
  760. if (ch == '#')
  761. break;
  762. checksum = checksum + ch;
  763. buffer[count] = ch;
  764. count = count + 1;
  765. }
  766. buffer[count] = '\0';
  767. if (ch == '#') {
  768. xmitcsum = hex (getDebugChar ()) << 4;
  769. xmitcsum += hex (getDebugChar ());
  770. if (checksum != xmitcsum) {
  771. /* Wrong checksum */
  772. putDebugChar ('-');
  773. }
  774. else {
  775. /* Correct checksum */
  776. putDebugChar ('+');
  777. /* If sequence characters are received, reply with them */
  778. if (buffer[2] == ':') {
  779. putDebugChar (buffer[0]);
  780. putDebugChar (buffer[1]);
  781. /* Remove the sequence characters from the buffer */
  782. count = gdb_cris_strlen (buffer);
  783. for (i = 3; i <= count; i++)
  784. buffer[i - 3] = buffer[i];
  785. }
  786. }
  787. }
  788. } while (checksum != xmitcsum);
  789. }
  790. /* Send $<data>#<checksum> from the <data> in the array buffer. */
  791. static void
  792. putpacket(char *buffer)
  793. {
  794. int checksum;
  795. int runlen;
  796. int encode;
  797. do {
  798. char *src = buffer;
  799. putDebugChar ('$');
  800. checksum = 0;
  801. while (*src) {
  802. /* Do run length encoding */
  803. putDebugChar (*src);
  804. checksum += *src;
  805. runlen = 0;
  806. while (runlen < RUNLENMAX && *src == src[runlen]) {
  807. runlen++;
  808. }
  809. if (runlen > 3) {
  810. /* Got a useful amount */
  811. putDebugChar ('*');
  812. checksum += '*';
  813. encode = runlen + ' ' - 4;
  814. putDebugChar (encode);
  815. checksum += encode;
  816. src += runlen;
  817. }
  818. else {
  819. src++;
  820. }
  821. }
  822. putDebugChar ('#');
  823. putDebugChar (highhex (checksum));
  824. putDebugChar (lowhex (checksum));
  825. } while(kgdb_started && (getDebugChar() != '+'));
  826. }
  827. /* The string str is prepended with the GDB printout token and sent. Required
  828. in traditional implementations. */
  829. void
  830. putDebugString (const unsigned char *str, int length)
  831. {
  832. remcomOutBuffer[0] = 'O';
  833. mem2hex(&remcomOutBuffer[1], (unsigned char *)str, length);
  834. putpacket(remcomOutBuffer);
  835. }
  836. /********************************** Handle exceptions ************************/
  837. /* Build and send a response packet in order to inform the host the
  838. stub is stopped. TAAn...:r...;n...:r...;n...:r...;
  839. AA = signal number
  840. n... = register number (hex)
  841. r... = register contents
  842. n... = `thread'
  843. r... = thread process ID. This is a hex integer.
  844. n... = other string not starting with valid hex digit.
  845. gdb should ignore this n,r pair and go on to the next.
  846. This way we can extend the protocol. */
  847. static void
  848. stub_is_stopped(int sigval)
  849. {
  850. char *ptr = remcomOutBuffer;
  851. int regno;
  852. unsigned int reg_cont;
  853. int status;
  854. /* Send trap type (converted to signal) */
  855. *ptr++ = 'T';
  856. *ptr++ = highhex (sigval);
  857. *ptr++ = lowhex (sigval);
  858. /* Send register contents. We probably only need to send the
  859. * PC, frame pointer and stack pointer here. Other registers will be
  860. * explicitely asked for. But for now, send all.
  861. */
  862. for (regno = R0; regno <= USP; regno++) {
  863. /* Store n...:r...; for the registers in the buffer. */
  864. status = read_register (regno, &reg_cont);
  865. if (status == SUCCESS) {
  866. *ptr++ = highhex (regno);
  867. *ptr++ = lowhex (regno);
  868. *ptr++ = ':';
  869. ptr = mem2hex(ptr, (unsigned char *)&reg_cont,
  870. register_size[regno]);
  871. *ptr++ = ';';
  872. }
  873. }
  874. #ifdef PROCESS_SUPPORT
  875. /* Store the registers of the executing thread. Assume that both step,
  876. continue, and register content requests are with respect to this
  877. thread. The executing task is from the operating system scheduler. */
  878. current_thread_c = executing_task;
  879. current_thread_g = executing_task;
  880. /* A struct assignment translates into a libc memcpy call. Avoid
  881. all libc functions in order to prevent recursive break points. */
  882. copy_registers (&reg_g, &reg, sizeof(registers));
  883. /* Store thread:r...; with the executing task TID. */
  884. gdb_cris_strcpy (&remcomOutBuffer[pos], "thread:");
  885. pos += gdb_cris_strlen ("thread:");
  886. remcomOutBuffer[pos++] = highhex (executing_task);
  887. remcomOutBuffer[pos++] = lowhex (executing_task);
  888. gdb_cris_strcpy (&remcomOutBuffer[pos], ";");
  889. #endif
  890. /* null-terminate and send it off */
  891. *ptr = 0;
  892. putpacket (remcomOutBuffer);
  893. }
  894. /* All expected commands are sent from remote.c. Send a response according
  895. to the description in remote.c. */
  896. static void
  897. handle_exception (int sigval)
  898. {
  899. /* Avoid warning of not used. */
  900. USEDFUN(handle_exception);
  901. USEDVAR(internal_stack[0]);
  902. /* Send response. */
  903. stub_is_stopped (sigval);
  904. for (;;) {
  905. remcomOutBuffer[0] = '\0';
  906. getpacket (remcomInBuffer);
  907. switch (remcomInBuffer[0]) {
  908. case 'g':
  909. /* Read registers: g
  910. Success: Each byte of register data is described by two hex digits.
  911. Registers are in the internal order for GDB, and the bytes
  912. in a register are in the same order the machine uses.
  913. Failure: void. */
  914. {
  915. #ifdef PROCESS_SUPPORT
  916. /* Use the special register content in the executing thread. */
  917. copy_registers (&reg_g, &reg, sizeof(registers));
  918. /* Replace the content available on the stack. */
  919. if (current_thread_g != executing_task) {
  920. copy_registers_from_stack (current_thread_g, &reg_g);
  921. }
  922. mem2hex ((unsigned char *)remcomOutBuffer, (unsigned char *)&reg_g, sizeof(registers));
  923. #else
  924. mem2hex(remcomOutBuffer, (char *)&reg, sizeof(registers));
  925. #endif
  926. }
  927. break;
  928. case 'G':
  929. /* Write registers. GXX..XX
  930. Each byte of register data is described by two hex digits.
  931. Success: OK
  932. Failure: void. */
  933. #ifdef PROCESS_SUPPORT
  934. hex2mem ((unsigned char *)&reg_g, &remcomInBuffer[1], sizeof(registers));
  935. if (current_thread_g == executing_task) {
  936. copy_registers (&reg, &reg_g, sizeof(registers));
  937. }
  938. else {
  939. copy_registers_to_stack(current_thread_g, &reg_g);
  940. }
  941. #else
  942. hex2mem((char *)&reg, &remcomInBuffer[1], sizeof(registers));
  943. #endif
  944. gdb_cris_strcpy (remcomOutBuffer, "OK");
  945. break;
  946. case 'P':
  947. /* Write register. Pn...=r...
  948. Write register n..., hex value without 0x, with value r...,
  949. which contains a hex value without 0x and two hex digits
  950. for each byte in the register (target byte order). P1f=11223344 means
  951. set register 31 to 44332211.
  952. Success: OK
  953. Failure: E02, E05 */
  954. {
  955. char *suffix;
  956. int regno = gdb_cris_strtol (&remcomInBuffer[1], &suffix, 16);
  957. int status;
  958. #ifdef PROCESS_SUPPORT
  959. if (current_thread_g != executing_task)
  960. status = write_stack_register (current_thread_g, regno, suffix+1);
  961. else
  962. #endif
  963. status = write_register (regno, suffix+1);
  964. switch (status) {
  965. case E02:
  966. /* Do not support read-only registers. */
  967. gdb_cris_strcpy (remcomOutBuffer, error_message[E02]);
  968. break;
  969. case E05:
  970. /* Do not support non-existing registers. */
  971. gdb_cris_strcpy (remcomOutBuffer, error_message[E05]);
  972. break;
  973. case E07:
  974. /* Do not support non-existing registers on the stack. */
  975. gdb_cris_strcpy (remcomOutBuffer, error_message[E07]);
  976. break;
  977. default:
  978. /* Valid register number. */
  979. gdb_cris_strcpy (remcomOutBuffer, "OK");
  980. break;
  981. }
  982. }
  983. break;
  984. case 'm':
  985. /* Read from memory. mAA..AA,LLLL
  986. AA..AA is the address and LLLL is the length.
  987. Success: XX..XX is the memory content. Can be fewer bytes than
  988. requested if only part of the data may be read. m6000120a,6c means
  989. retrieve 108 byte from base address 6000120a.
  990. Failure: void. */
  991. {
  992. char *suffix;
  993. unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
  994. &suffix, 16); int length = gdb_cris_strtol(suffix+1, 0, 16);
  995. mem2hex(remcomOutBuffer, addr, length);
  996. }
  997. break;
  998. case 'X':
  999. /* Write to memory. XAA..AA,LLLL:XX..XX
  1000. AA..AA is the start address, LLLL is the number of bytes, and
  1001. XX..XX is the binary data.
  1002. Success: OK
  1003. Failure: void. */
  1004. case 'M':
  1005. /* Write to memory. MAA..AA,LLLL:XX..XX
  1006. AA..AA is the start address, LLLL is the number of bytes, and
  1007. XX..XX is the hexadecimal data.
  1008. Success: OK
  1009. Failure: void. */
  1010. {
  1011. char *lenptr;
  1012. char *dataptr;
  1013. unsigned char *addr = (unsigned char *)gdb_cris_strtol(&remcomInBuffer[1],
  1014. &lenptr, 16);
  1015. int length = gdb_cris_strtol(lenptr+1, &dataptr, 16);
  1016. if (*lenptr == ',' && *dataptr == ':') {
  1017. if (remcomInBuffer[0] == 'M') {
  1018. hex2mem(addr, dataptr + 1, length);
  1019. }
  1020. else /* X */ {
  1021. bin2mem(addr, dataptr + 1, length);
  1022. }
  1023. gdb_cris_strcpy (remcomOutBuffer, "OK");
  1024. }
  1025. else {
  1026. gdb_cris_strcpy (remcomOutBuffer, error_message[E06]);
  1027. }
  1028. }
  1029. break;
  1030. case 'c':
  1031. /* Continue execution. cAA..AA
  1032. AA..AA is the address where execution is resumed. If AA..AA is
  1033. omitted, resume at the present address.
  1034. Success: return to the executing thread.
  1035. Failure: will never know. */
  1036. if (remcomInBuffer[1] != '\0') {
  1037. reg.pc = gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
  1038. }
  1039. enableDebugIRQ();
  1040. return;
  1041. case 's':
  1042. /* Step. sAA..AA
  1043. AA..AA is the address where execution is resumed. If AA..AA is
  1044. omitted, resume at the present address. Success: return to the
  1045. executing thread. Failure: will never know.
  1046. Should never be invoked. The single-step is implemented on
  1047. the host side. If ever invoked, it is an internal error E04. */
  1048. gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
  1049. putpacket (remcomOutBuffer);
  1050. return;
  1051. case '?':
  1052. /* The last signal which caused a stop. ?
  1053. Success: SAA, where AA is the signal number.
  1054. Failure: void. */
  1055. remcomOutBuffer[0] = 'S';
  1056. remcomOutBuffer[1] = highhex (sigval);
  1057. remcomOutBuffer[2] = lowhex (sigval);
  1058. remcomOutBuffer[3] = 0;
  1059. break;
  1060. case 'D':
  1061. /* Detach from host. D
  1062. Success: OK, and return to the executing thread.
  1063. Failure: will never know */
  1064. putpacket ("OK");
  1065. return;
  1066. case 'k':
  1067. case 'r':
  1068. /* kill request or reset request.
  1069. Success: restart of target.
  1070. Failure: will never know. */
  1071. kill_restart ();
  1072. break;
  1073. case 'C':
  1074. case 'S':
  1075. case '!':
  1076. case 'R':
  1077. case 'd':
  1078. /* Continue with signal sig. Csig;AA..AA
  1079. Step with signal sig. Ssig;AA..AA
  1080. Use the extended remote protocol. !
  1081. Restart the target system. R0
  1082. Toggle debug flag. d
  1083. Search backwards. tAA:PP,MM
  1084. Not supported: E04 */
  1085. gdb_cris_strcpy (remcomOutBuffer, error_message[E04]);
  1086. break;
  1087. #ifdef PROCESS_SUPPORT
  1088. case 'T':
  1089. /* Thread alive. TXX
  1090. Is thread XX alive?
  1091. Success: OK, thread XX is alive.
  1092. Failure: E03, thread XX is dead. */
  1093. {
  1094. int thread_id = (int)gdb_cris_strtol (&remcomInBuffer[1], 0, 16);
  1095. /* Cannot tell whether it is alive or not. */
  1096. if (thread_id >= 0 && thread_id < number_of_tasks)
  1097. gdb_cris_strcpy (remcomOutBuffer, "OK");
  1098. }
  1099. break;
  1100. case 'H':
  1101. /* Set thread for subsequent operations: Hct
  1102. c = 'c' for thread used in step and continue;
  1103. t can be -1 for all threads.
  1104. c = 'g' for thread used in other operations.
  1105. t = 0 means pick any thread.
  1106. Success: OK
  1107. Failure: E01 */
  1108. {
  1109. int thread_id = gdb_cris_strtol (&remcomInBuffer[2], 0, 16);
  1110. if (remcomInBuffer[1] == 'c') {
  1111. /* c = 'c' for thread used in step and continue */
  1112. /* Do not change current_thread_c here. It would create a mess in
  1113. the scheduler. */
  1114. gdb_cris_strcpy (remcomOutBuffer, "OK");
  1115. }
  1116. else if (remcomInBuffer[1] == 'g') {
  1117. /* c = 'g' for thread used in other operations.
  1118. t = 0 means pick any thread. Impossible since the scheduler does
  1119. not allow that. */
  1120. if (thread_id >= 0 && thread_id < number_of_tasks) {
  1121. current_thread_g = thread_id;
  1122. gdb_cris_strcpy (remcomOutBuffer, "OK");
  1123. }
  1124. else {
  1125. /* Not expected - send an error message. */
  1126. gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
  1127. }
  1128. }
  1129. else {
  1130. /* Not expected - send an error message. */
  1131. gdb_cris_strcpy (remcomOutBuffer, error_message[E01]);
  1132. }
  1133. }
  1134. break;
  1135. case 'q':
  1136. case 'Q':
  1137. /* Query of general interest. qXXXX
  1138. Set general value XXXX. QXXXX=yyyy */
  1139. {
  1140. int pos;
  1141. int nextpos;
  1142. int thread_id;
  1143. switch (remcomInBuffer[1]) {
  1144. case 'C':
  1145. /* Identify the remote current thread. */
  1146. gdb_cris_strcpy (&remcomOutBuffer[0], "QC");
  1147. remcomOutBuffer[2] = highhex (current_thread_c);
  1148. remcomOutBuffer[3] = lowhex (current_thread_c);
  1149. remcomOutBuffer[4] = '\0';
  1150. break;
  1151. case 'L':
  1152. gdb_cris_strcpy (&remcomOutBuffer[0], "QM");
  1153. /* Reply with number of threads. */
  1154. if (os_is_started()) {
  1155. remcomOutBuffer[2] = highhex (number_of_tasks);
  1156. remcomOutBuffer[3] = lowhex (number_of_tasks);
  1157. }
  1158. else {
  1159. remcomOutBuffer[2] = highhex (0);
  1160. remcomOutBuffer[3] = lowhex (1);
  1161. }
  1162. /* Done with the reply. */
  1163. remcomOutBuffer[4] = lowhex (1);
  1164. pos = 5;
  1165. /* Expects the argument thread id. */
  1166. for (; pos < (5 + HEXCHARS_IN_THREAD_ID); pos++)
  1167. remcomOutBuffer[pos] = remcomInBuffer[pos];
  1168. /* Reply with the thread identifiers. */
  1169. if (os_is_started()) {
  1170. /* Store the thread identifiers of all tasks. */
  1171. for (thread_id = 0; thread_id < number_of_tasks; thread_id++) {
  1172. nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
  1173. for (; pos < nextpos; pos ++)
  1174. remcomOutBuffer[pos] = lowhex (0);
  1175. remcomOutBuffer[pos++] = lowhex (thread_id);
  1176. }
  1177. }
  1178. else {
  1179. /* Store the thread identifier of the boot task. */
  1180. nextpos = pos + HEXCHARS_IN_THREAD_ID - 1;
  1181. for (; pos < nextpos; pos ++)
  1182. remcomOutBuffer[pos] = lowhex (0);
  1183. remcomOutBuffer[pos++] = lowhex (current_thread_c);
  1184. }
  1185. remcomOutBuffer[pos] = '\0';
  1186. break;
  1187. default:
  1188. /* Not supported: "" */
  1189. /* Request information about section offsets: qOffsets. */
  1190. remcomOutBuffer[0] = 0;
  1191. break;
  1192. }
  1193. }
  1194. break;
  1195. #endif /* PROCESS_SUPPORT */
  1196. default:
  1197. /* The stub should ignore other request and send an empty
  1198. response ($#<checksum>). This way we can extend the protocol and GDB
  1199. can tell whether the stub it is talking to uses the old or the new. */
  1200. remcomOutBuffer[0] = 0;
  1201. break;
  1202. }
  1203. putpacket(remcomOutBuffer);
  1204. }
  1205. }
  1206. /* The jump is to the address 0x00000002. Performs a complete re-start
  1207. from scratch. */
  1208. static void
  1209. kill_restart ()
  1210. {
  1211. __asm__ volatile ("jump 2");
  1212. }
  1213. /********************************** Breakpoint *******************************/
  1214. /* The hook for both a static (compiled) and a dynamic breakpoint set by GDB.
  1215. An internal stack is used by the stub. The register image of the caller is
  1216. stored in the structure register_image.
  1217. Interactive communication with the host is handled by handle_exception and
  1218. finally the register image is restored. */
  1219. void kgdb_handle_breakpoint(void);
  1220. asm ("
  1221. .global kgdb_handle_breakpoint
  1222. kgdb_handle_breakpoint:
  1223. ;;
  1224. ;; Response to the break-instruction
  1225. ;;
  1226. ;; Create a register image of the caller
  1227. ;;
  1228. move $dccr,[reg+0x5E] ; Save the flags in DCCR before disable interrupts
  1229. di ; Disable interrupts
  1230. move.d $r0,[reg] ; Save R0
  1231. move.d $r1,[reg+0x04] ; Save R1
  1232. move.d $r2,[reg+0x08] ; Save R2
  1233. move.d $r3,[reg+0x0C] ; Save R3
  1234. move.d $r4,[reg+0x10] ; Save R4
  1235. move.d $r5,[reg+0x14] ; Save R5
  1236. move.d $r6,[reg+0x18] ; Save R6
  1237. move.d $r7,[reg+0x1C] ; Save R7
  1238. move.d $r8,[reg+0x20] ; Save R8
  1239. move.d $r9,[reg+0x24] ; Save R9
  1240. move.d $r10,[reg+0x28] ; Save R10
  1241. move.d $r11,[reg+0x2C] ; Save R11
  1242. move.d $r12,[reg+0x30] ; Save R12
  1243. move.d $r13,[reg+0x34] ; Save R13
  1244. move.d $sp,[reg+0x38] ; Save SP (R14)
  1245. ;; Due to the old assembler-versions BRP might not be recognized
  1246. .word 0xE670 ; move brp,$r0
  1247. subq 2,$r0 ; Set to address of previous instruction.
  1248. move.d $r0,[reg+0x3c] ; Save the address in PC (R15)
  1249. clear.b [reg+0x40] ; Clear P0
  1250. move $vr,[reg+0x41] ; Save special register P1
  1251. clear.w [reg+0x42] ; Clear P4
  1252. move $ccr,[reg+0x44] ; Save special register CCR
  1253. move $mof,[reg+0x46] ; P7
  1254. clear.d [reg+0x4A] ; Clear P8
  1255. move $ibr,[reg+0x4E] ; P9,
  1256. move $irp,[reg+0x52] ; P10,
  1257. move $srp,[reg+0x56] ; P11,
  1258. move $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR
  1259. ; P13, register DCCR already saved
  1260. ;; Due to the old assembler-versions BRP might not be recognized
  1261. .word 0xE670 ; move brp,r0
  1262. ;; Static (compiled) breakpoints must return to the next instruction in order
  1263. ;; to avoid infinite loops. Dynamic (gdb-invoked) must restore the instruction
  1264. ;; in order to execute it when execution is continued.
  1265. test.b [is_dyn_brkp] ; Is this a dynamic breakpoint?
  1266. beq is_static ; No, a static breakpoint
  1267. nop
  1268. subq 2,$r0 ; rerun the instruction the break replaced
  1269. is_static:
  1270. moveq 1,$r1
  1271. move.b $r1,[is_dyn_brkp] ; Set the state variable to dynamic breakpoint
  1272. move.d $r0,[reg+0x62] ; Save the return address in BRP
  1273. move $usp,[reg+0x66] ; USP
  1274. ;;
  1275. ;; Handle the communication
  1276. ;;
  1277. move.d internal_stack+1020,$sp ; Use the internal stack which grows upward
  1278. moveq 5,$r10 ; SIGTRAP
  1279. jsr handle_exception ; Interactive routine
  1280. ;;
  1281. ;; Return to the caller
  1282. ;;
  1283. move.d [reg],$r0 ; Restore R0
  1284. move.d [reg+0x04],$r1 ; Restore R1
  1285. move.d [reg+0x08],$r2 ; Restore R2
  1286. move.d [reg+0x0C],$r3 ; Restore R3
  1287. move.d [reg+0x10],$r4 ; Restore R4
  1288. move.d [reg+0x14],$r5 ; Restore R5
  1289. move.d [reg+0x18],$r6 ; Restore R6
  1290. move.d [reg+0x1C],$r7 ; Restore R7
  1291. move.d [reg+0x20],$r8 ; Restore R8
  1292. move.d [reg+0x24],$r9 ; Restore R9
  1293. move.d [reg+0x28],$r10 ; Restore R10
  1294. move.d [reg+0x2C],$r11 ; Restore R11
  1295. move.d [reg+0x30],$r12 ; Restore R12
  1296. move.d [reg+0x34],$r13 ; Restore R13
  1297. ;;
  1298. ;; FIXME: Which registers should be restored?
  1299. ;;
  1300. move.d [reg+0x38],$sp ; Restore SP (R14)
  1301. move [reg+0x56],$srp ; Restore the subroutine return pointer.
  1302. move [reg+0x5E],$dccr ; Restore DCCR
  1303. move [reg+0x66],$usp ; Restore USP
  1304. jump [reg+0x62] ; A jump to the content in register BRP works.
  1305. nop ;
  1306. ");
  1307. /* The hook for an interrupt generated by GDB. An internal stack is used
  1308. by the stub. The register image of the caller is stored in the structure
  1309. register_image. Interactive communication with the host is handled by
  1310. handle_exception and finally the register image is restored. Due to the
  1311. old assembler which does not recognise the break instruction and the
  1312. breakpoint return pointer hex-code is used. */
  1313. void kgdb_handle_serial(void);
  1314. asm ("
  1315. .global kgdb_handle_serial
  1316. kgdb_handle_serial:
  1317. ;;
  1318. ;; Response to a serial interrupt
  1319. ;;
  1320. move $dccr,[reg+0x5E] ; Save the flags in DCCR
  1321. di ; Disable interrupts
  1322. move.d $r0,[reg] ; Save R0
  1323. move.d $r1,[reg+0x04] ; Save R1
  1324. move.d $r2,[reg+0x08] ; Save R2
  1325. move.d $r3,[reg+0x0C] ; Save R3
  1326. move.d $r4,[reg+0x10] ; Save R4
  1327. move.d $r5,[reg+0x14] ; Save R5
  1328. move.d $r6,[reg+0x18] ; Save R6
  1329. move.d $r7,[reg+0x1C] ; Save R7
  1330. move.d $r8,[reg+0x20] ; Save R8
  1331. move.d $r9,[reg+0x24] ; Save R9
  1332. move.d $r10,[reg+0x28] ; Save R10
  1333. move.d $r11,[reg+0x2C] ; Save R11
  1334. move.d $r12,[reg+0x30] ; Save R12
  1335. move.d $r13,[reg+0x34] ; Save R13
  1336. move.d $sp,[reg+0x38] ; Save SP (R14)
  1337. move $irp,[reg+0x3c] ; Save the address in PC (R15)
  1338. clear.b [reg+0x40] ; Clear P0
  1339. move $vr,[reg+0x41] ; Save special register P1,
  1340. clear.w [reg+0x42] ; Clear P4
  1341. move $ccr,[reg+0x44] ; Save special register CCR
  1342. move $mof,[reg+0x46] ; P7
  1343. clear.d [reg+0x4A] ; Clear P8
  1344. move $ibr,[reg+0x4E] ; P9,
  1345. move $irp,[reg+0x52] ; P10,
  1346. move $srp,[reg+0x56] ; P11,
  1347. move $dtp0,[reg+0x5A] ; P12, register BAR, assembler might not know BAR
  1348. ; P13, register DCCR already saved
  1349. ;; Due to the old assembler-versions BRP might not be recognized
  1350. .word 0xE670 ; move brp,r0
  1351. move.d $r0,[reg+0x62] ; Save the return address in BRP
  1352. move $usp,[reg+0x66] ; USP
  1353. ;; get the serial character (from debugport.c) and check if it is a ctrl-c
  1354. jsr getDebugChar
  1355. cmp.b 3, $r10
  1356. bne goback
  1357. nop
  1358. ;;
  1359. ;; Handle the communication
  1360. ;;
  1361. move.d internal_stack+1020,$sp ; Use the internal stack
  1362. moveq 2,$r10 ; SIGINT
  1363. jsr handle_exception ; Interactive routine
  1364. goback:
  1365. ;;
  1366. ;; Return to the caller
  1367. ;;
  1368. move.d [reg],$r0 ; Restore R0
  1369. move.d [reg+0x04],$r1 ; Restore R1
  1370. move.d [reg+0x08],$r2 ; Restore R2
  1371. move.d [reg+0x0C],$r3 ; Restore R3
  1372. move.d [reg+0x10],$r4 ; Restore R4
  1373. move.d [reg+0x14],$r5 ; Restore R5
  1374. move.d [reg+0x18],$r6 ; Restore R6
  1375. move.d [reg+0x1C],$r7 ; Restore R7
  1376. move.d [reg+0x20],$r8 ; Restore R8
  1377. move.d [reg+0x24],$r9 ; Restore R9
  1378. move.d [reg+0x28],$r10 ; Restore R10
  1379. move.d [reg+0x2C],$r11 ; Restore R11
  1380. move.d [reg+0x30],$r12 ; Restore R12
  1381. move.d [reg+0x34],$r13 ; Restore R13
  1382. ;;
  1383. ;; FIXME: Which registers should be restored?
  1384. ;;
  1385. move.d [reg+0x38],$sp ; Restore SP (R14)
  1386. move [reg+0x56],$srp ; Restore the subroutine return pointer.
  1387. move [reg+0x5E],$dccr ; Restore DCCR
  1388. move [reg+0x66],$usp ; Restore USP
  1389. reti ; Return from the interrupt routine
  1390. nop
  1391. ");
  1392. /* Use this static breakpoint in the start-up only. */
  1393. void
  1394. breakpoint(void)
  1395. {
  1396. kgdb_started = 1;
  1397. is_dyn_brkp = 0; /* This is a static, not a dynamic breakpoint. */
  1398. __asm__ volatile ("break 8"); /* Jump to handle_breakpoint. */
  1399. }
  1400. /* initialize kgdb. doesn't break into the debugger, but sets up irq and ports */
  1401. void
  1402. kgdb_init(void)
  1403. {
  1404. /* could initialize debug port as well but it's done in head.S already... */
  1405. /* breakpoint handler is now set in irq.c */
  1406. set_int_vector(8, kgdb_handle_serial);
  1407. enableDebugIRQ();
  1408. }
  1409. /****************************** End of file **********************************/