~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/cris/arch-v10/kernel/kgdb.c

Version: ~ [ linux-5.4-rc7 ] ~ [ linux-5.3.10 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.83 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.153 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.200 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.200 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.76 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp