kgdb.c 52 KB

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