kgdb.c 49 KB

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