1 /*
2 * May be copied or modified under the terms of the GNU General Public
3 * License. See linux/COPYING for more information.
4 *
5 * Contains extracts from code by Glenn Engel, Jim Kingdon,
6 * David Grothe <dave@gcom.com>, Tigran Aivazian <tigran@sco.com>,
7 * Amit S. Kale <akale@veritas.com>, William Gatliff <bgat@open-widgets.com>,
8 * Ben Lee, Steve Chamberlain and Benoit Miller <fulg@iname.com>.
9 *
10 * This version by Henry Bell <henry.bell@st.com>
11 * Minor modifications by Jeremy Siegel <jsiegel@mvista.com>
12 *
13 * Contains low-level support for remote debug using GDB.
14 *
15 * To enable debugger support, two things need to happen. A call to
16 * set_debug_traps() is necessary in order to allow any breakpoints
17 * or error conditions to be properly intercepted and reported to gdb.
18 * A breakpoint also needs to be generated to begin communication. This
19 * is most easily accomplished by a call to breakpoint() which does
20 * a trapa if the initialisation phase has been successfully completed.
21 *
22 * In this case, set_debug_traps() is not used to "take over" exceptions;
23 * other kernel code is modified instead to enter the kgdb functions here
24 * when appropriate (see entry.S for breakpoint traps and NMI interrupts,
25 * see traps.c for kernel error exceptions).
26 *
27 * The following gdb commands are supported:
28 *
29 * Command Function Return value
30 *
31 * g return the value of the CPU registers hex data or ENN
32 * G set the value of the CPU registers OK or ENN
33 *
34 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN
35 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN
36 * XAA..AA,LLLL: Same, but data is binary (not hex) OK or ENN
37 *
38 * c Resume at current address SNN ( signal NN)
39 * cAA..AA Continue at address AA..AA SNN
40 * CNN; Resume at current address with signal SNN
41 * CNN;AA..AA Resume at address AA..AA with signal SNN
42 *
43 * s Step one instruction SNN
44 * sAA..AA Step one instruction from AA..AA SNN
45 * SNN; Step one instruction with signal SNN
46 * SNNAA..AA Step one instruction from AA..AA w/NN SNN
47 *
48 * k kill (Detach GDB)
49 *
50 * d Toggle debug flag
51 * D Detach GDB
52 *
53 * Hct Set thread t for operations, OK or ENN
54 * c = 'c' (step, cont), c = 'g' (other
55 * operations)
56 *
57 * qC Query current thread ID QCpid
58 * qfThreadInfo Get list of current threads (first) m<id>
59 * qsThreadInfo " " " " " (subsequent)
60 * qOffsets Get section offsets Text=x;Data=y;Bss=z
61 *
62 * TXX Find if thread XX is alive OK or ENN
63 * ? What was the last sigval ? SNN (signal NN)
64 * O Output to GDB console
65 *
66 * Remote communication protocol.
67 *
68 * A debug packet whose contents are <data> is encapsulated for
69 * transmission in the form:
70 *
71 * $ <data> # CSUM1 CSUM2
72 *
73 * <data> must be ASCII alphanumeric and cannot include characters
74 * '$' or '#'. If <data> starts with two characters followed by
75 * ':', then the existing stubs interpret this as a sequence number.
76 *
77 * CSUM1 and CSUM2 are ascii hex representation of an 8-bit
78 * checksum of <data>, the most significant nibble is sent first.
79 * the hex digits 0-9,a-f are used.
80 *
81 * Receiver responds with:
82 *
83 * + - if CSUM is correct and ready for next packet
84 * - - if CSUM is incorrect
85 *
86 * Responses can be run-length encoded to save space. A '*' means that
87 * the next character is an ASCII encoding giving a repeat count which
88 * stands for that many repititions of the character preceding the '*'.
89 * The encoding is n+29, yielding a printable character where n >=3
90 * (which is where RLE starts to win). Don't use an n > 126.
91 *
92 * So "0* " means the same as "0000".
93 */
94
95 #include <linux/string.h>
96 #include <linux/kernel.h>
97 #include <linux/sched.h>
98 #include <linux/smp.h>
99 #include <linux/spinlock.h>
100 #include <linux/delay.h>
101 #include <linux/linkage.h>
102 #include <linux/init.h>
103
104 #include <asm/system.h>
105 #include <asm/current.h>
106 #include <asm/signal.h>
107 #include <asm/pgtable.h>
108 #include <asm/ptrace.h>
109 #include <asm/kgdb.h>
110
111 #ifdef CONFIG_SH_KGDB_CONSOLE
112 #include <linux/console.h>
113 #endif
114
115 /* Function pointers for linkage */
116 kgdb_debug_hook_t *kgdb_debug_hook;
117 kgdb_bus_error_hook_t *kgdb_bus_err_hook;
118
119 int (*kgdb_getchar)(void);
120 void (*kgdb_putchar)(int);
121
put_debug_char(int c)122 static void put_debug_char(int c)
123 {
124 if (!kgdb_putchar)
125 return;
126 (*kgdb_putchar)(c);
127 }
get_debug_char(void)128 static int get_debug_char(void)
129 {
130 if (!kgdb_getchar)
131 return -1;
132 return (*kgdb_getchar)();
133 }
134
135 /* Num chars in in/out bound buffers, register packets need NUMREGBYTES * 2 */
136 #define BUFMAX 1024
137 #define NUMREGBYTES (MAXREG*4)
138 #define OUTBUFMAX (NUMREGBYTES*2+512)
139
140 enum regs {
141 R0 = 0, R1, R2, R3, R4, R5, R6, R7,
142 R8, R9, R10, R11, R12, R13, R14, R15,
143 PC, PR, GBR, VBR, MACH, MACL, SR,
144 /* */
145 MAXREG
146 };
147
148 static unsigned int registers[MAXREG];
149 struct kgdb_regs trap_registers;
150
151 char kgdb_in_gdb_mode;
152 char in_nmi; /* Set during NMI to prevent reentry */
153 int kgdb_nofault; /* Boolean to ignore bus errs (i.e. in GDB) */
154 int kgdb_enabled = 1; /* Default to enabled, cmdline can disable */
155 int kgdb_halt;
156
157 /* Exposed for user access */
158 struct task_struct *kgdb_current;
159 unsigned int kgdb_g_imask;
160 int kgdb_trapa_val;
161 int kgdb_excode;
162 struct pt_regs *kgdb_tregs;
163
164 /* Default values for SCI (can override via kernel args in setup.c) */
165 #ifndef CONFIG_KGDB_DEFPORT
166 #define CONFIG_KGDB_DEFPORT 1
167 #endif
168
169 #ifndef CONFIG_KGDB_DEFBAUD
170 #define CONFIG_KGDB_DEFBAUD 115200
171 #endif
172
173 #if defined(CONFIG_KGDB_DEFPARITY_E)
174 #define CONFIG_KGDB_DEFPARITY 'E'
175 #elif defined(CONFIG_KGDB_DEFPARITY_O)
176 #define CONFIG_KGDB_DEFPARITY 'O'
177 #else /* CONFIG_KGDB_DEFPARITY_N */
178 #define CONFIG_KGDB_DEFPARITY 'N'
179 #endif
180
181 #ifdef CONFIG_KGDB_DEFBITS_7
182 #define CONFIG_KGDB_DEFBITS '7'
183 #else /* CONFIG_KGDB_DEFBITS_8 */
184 #define CONFIG_KGDB_DEFBITS '8'
185 #endif
186
187 /* SCI/UART settings, used in kgdb_serial_setup() */
188 int (*kgdb_serial_setup)(void) = kgdb_sci_setup;
189 int kgdb_portnum = CONFIG_KGDB_DEFPORT;
190 int kgdb_baud = CONFIG_KGDB_DEFBAUD;
191 char kgdb_parity = CONFIG_KGDB_DEFPARITY;
192 char kgdb_bits = CONFIG_KGDB_DEFBITS;
193 int kgdb_cflag;
194
195 /* Jump buffer for setjmp/longjmp */
196 static jmp_buf rem_com_env;
197
198 /* TRA differs sh3/4 */
199 #if defined(__sh3__)
200 #define TRA 0xffffffd0
201 #elif defined(__SH4__)
202 #define TRA 0xff000020
203 #endif
204
205 /* Macros for single step instruction identification */
206 #define OPCODE_BT(op) (((op) & 0xff00) == 0x8900)
207 #define OPCODE_BF(op) (((op) & 0xff00) == 0x8b00)
208 #define OPCODE_BTF_DISP(op) (((op) & 0x80) ? (((op) | 0xffffff80) << 1) : \
209 (((op) & 0x7f ) << 1))
210 #define OPCODE_BFS(op) (((op) & 0xff00) == 0x8f00)
211 #define OPCODE_BTS(op) (((op) & 0xff00) == 0x8d00)
212 #define OPCODE_BRA(op) (((op) & 0xf000) == 0xa000)
213 #define OPCODE_BRA_DISP(op) (((op) & 0x800) ? (((op) | 0xfffff800) << 1) : \
214 (((op) & 0x7ff) << 1))
215 #define OPCODE_BRAF(op) (((op) & 0xf0ff) == 0x0023)
216 #define OPCODE_BRAF_REG(op) (((op) & 0x0f00) >> 8)
217 #define OPCODE_BSR(op) (((op) & 0xf000) == 0xb000)
218 #define OPCODE_BSR_DISP(op) (((op) & 0x800) ? (((op) | 0xfffff800) << 1) : \
219 (((op) & 0x7ff) << 1))
220 #define OPCODE_BSRF(op) (((op) & 0xf0ff) == 0x0003)
221 #define OPCODE_BSRF_REG(op) (((op) >> 8) & 0xf)
222 #define OPCODE_JMP(op) (((op) & 0xf0ff) == 0x402b)
223 #define OPCODE_JMP_REG(op) (((op) >> 8) & 0xf)
224 #define OPCODE_JSR(op) (((op) & 0xf0ff) == 0x400b)
225 #define OPCODE_JSR_REG(op) (((op) >> 8) & 0xf)
226 #define OPCODE_RTS(op) ((op) == 0xb)
227 #define OPCODE_RTE(op) ((op) == 0x2b)
228
229 #define SR_T_BIT_MASK 0x1
230 #define STEP_OPCODE 0xc320
231 #define BIOS_CALL_TRAP 0x3f
232
233 /* Exception codes as per SH-4 core manual */
234 #define ADDRESS_ERROR_LOAD_VEC 7
235 #define ADDRESS_ERROR_STORE_VEC 8
236 #define TRAP_VEC 11
237 #define INVALID_INSN_VEC 12
238 #define INVALID_SLOT_VEC 13
239 #define NMI_VEC 14
240 #define USER_BREAK_VEC 15
241 #define SERIAL_BREAK_VEC 58
242
243 /* Misc static */
244 static int stepped_address;
245 static short stepped_opcode;
246 static const char hexchars[] = "0123456789abcdef";
247 static char in_buffer[BUFMAX];
248 static char out_buffer[OUTBUFMAX];
249
250 static void kgdb_to_gdb(const char *s);
251
252 #ifdef CONFIG_KGDB_THREAD
253 static struct task_struct *trapped_thread;
254 static struct task_struct *current_thread;
255 typedef unsigned char threadref[8];
256 #define BUF_THREAD_ID_SIZE 16
257 #endif
258
259 /* Return addr as a real volatile address */
ctrl_inl(const unsigned long addr)260 static inline unsigned int ctrl_inl(const unsigned long addr)
261 {
262 return *(volatile unsigned long *) addr;
263 }
264
265 /* Correctly set *addr using volatile */
ctrl_outl(const unsigned int b,unsigned long addr)266 static inline void ctrl_outl(const unsigned int b, unsigned long addr)
267 {
268 *(volatile unsigned long *) addr = b;
269 }
270
271 /* Get high hex bits */
highhex(const int x)272 static char highhex(const int x)
273 {
274 return hexchars[(x >> 4) & 0xf];
275 }
276
277 /* Get low hex bits */
lowhex(const int x)278 static char lowhex(const int x)
279 {
280 return hexchars[x & 0xf];
281 }
282
283 /* Convert ch to hex */
hex(const char ch)284 static int hex(const char ch)
285 {
286 if ((ch >= 'a') && (ch <= 'f'))
287 return (ch - 'a' + 10);
288 if ((ch >= '0') && (ch <= '9'))
289 return (ch - '0');
290 if ((ch >= 'A') && (ch <= 'F'))
291 return (ch - 'A' + 10);
292 return (-1);
293 }
294
295 /* Convert the memory pointed to by mem into hex, placing result in buf.
296 Returns a pointer to the last char put in buf (null) */
mem_to_hex(const char * mem,char * buf,const int count)297 static char *mem_to_hex(const char *mem, char *buf, const int count)
298 {
299 int i;
300 int ch;
301 unsigned short s_val;
302 unsigned long l_val;
303
304 /* Check for 16 or 32 */
305 if (count == 2 && ((long) mem & 1) == 0) {
306 s_val = *(unsigned short *) mem;
307 mem = (char *) &s_val;
308 } else if (count == 4 && ((long) mem & 3) == 0) {
309 l_val = *(unsigned long *) mem;
310 mem = (char *) &l_val;
311 }
312 for (i = 0; i < count; i++) {
313 ch = *mem++;
314 *buf++ = highhex(ch);
315 *buf++ = lowhex(ch);
316 }
317 *buf = 0;
318 return (buf);
319 }
320
321 /* Convert the hex array pointed to by buf into binary, to be placed in mem.
322 Return a pointer to the character after the last byte written */
hex_to_mem(const char * buf,char * mem,const int count)323 static char *hex_to_mem(const char *buf, char *mem, const int count)
324 {
325 int i;
326 unsigned char ch;
327
328 for (i = 0; i < count; i++) {
329 ch = hex(*buf++) << 4;
330 ch = ch + hex(*buf++);
331 *mem++ = ch;
332 }
333 return (mem);
334 }
335
336 /* While finding valid hex chars, convert to an integer, then return it */
hex_to_int(char ** ptr,int * int_value)337 static int hex_to_int(char **ptr, int *int_value)
338 {
339 int num_chars = 0;
340 int hex_value;
341
342 *int_value = 0;
343
344 while (**ptr) {
345 hex_value = hex(**ptr);
346 if (hex_value >= 0) {
347 *int_value = (*int_value << 4) | hex_value;
348 num_chars++;
349 } else
350 break;
351 (*ptr)++;
352 }
353 return num_chars;
354 }
355
356 /* Copy the binary array pointed to by buf into mem. Fix $, #,
357 and 0x7d escaped with 0x7d. Return a pointer to the character
358 after the last byte written. */
ebin_to_mem(const char * buf,char * mem,int count)359 static char *ebin_to_mem(const char *buf, char *mem, int count)
360 {
361 for (; count > 0; count--, buf++) {
362 if (*buf == 0x7d)
363 *mem++ = *(++buf) ^ 0x20;
364 else
365 *mem++ = *buf;
366 }
367 return mem;
368 }
369
370 /* Pack a hex byte */
pack_hex_byte(char * pkt,int byte)371 static char *pack_hex_byte(char *pkt, int byte)
372 {
373 *pkt++ = hexchars[(byte >> 4) & 0xf];
374 *pkt++ = hexchars[(byte & 0xf)];
375 return pkt;
376 }
377
378 #ifdef CONFIG_KGDB_THREAD
379
380 /* Pack a thread ID */
pack_threadid(char * pkt,threadref * id)381 static char *pack_threadid(char *pkt, threadref * id)
382 {
383 char *limit;
384 unsigned char *altid;
385
386 altid = (unsigned char *) id;
387
388 limit = pkt + BUF_THREAD_ID_SIZE;
389 while (pkt < limit)
390 pkt = pack_hex_byte(pkt, *altid++);
391 return pkt;
392 }
393
394 /* Convert an integer into our threadref */
int_to_threadref(threadref * id,const int value)395 static void int_to_threadref(threadref * id, const int value)
396 {
397 unsigned char *scan = (unsigned char *) id;
398 int i = 4;
399
400 while (i--)
401 *scan++ = 0;
402
403 *scan++ = (value >> 24) & 0xff;
404 *scan++ = (value >> 16) & 0xff;
405 *scan++ = (value >> 8) & 0xff;
406 *scan++ = (value & 0xff);
407 }
408
409 /* Return a task structure ptr for a particular pid */
get_thread(int pid)410 static struct task_struct *get_thread(int pid)
411 {
412 struct task_struct *thread;
413
414 /* Use PID_MAX w/gdb for pid 0 */
415 if (pid == PID_MAX) pid = 0;
416
417 /* First check via PID */
418 thread = find_task_by_pid(pid);
419
420 if (thread)
421 return thread;
422
423 /* Start at the start */
424 thread = init_tasks[0];
425
426 /* Walk along the linked list of tasks */
427 do {
428 if (thread->pid == pid)
429 return thread;
430 thread = thread->next_task;
431 } while (thread != init_tasks[0]);
432
433 return NULL;
434 }
435
436 #endif /* CONFIG_KGDB_THREAD */
437
438 /* Scan for the start char '$', read the packet and check the checksum */
get_packet(char * buffer,int buflen)439 static void get_packet(char *buffer, int buflen)
440 {
441 unsigned char checksum;
442 unsigned char xmitcsum;
443 int i;
444 int count;
445 char ch;
446
447 do {
448 /* Ignore everything until the start character */
449 while ((ch = get_debug_char()) != '$');
450
451 checksum = 0;
452 xmitcsum = -1;
453 count = 0;
454
455 /* Now, read until a # or end of buffer is found */
456 while (count < (buflen - 1)) {
457 ch = get_debug_char();
458
459 if (ch == '#')
460 break;
461
462 checksum = checksum + ch;
463 buffer[count] = ch;
464 count = count + 1;
465 }
466
467 buffer[count] = 0;
468
469 /* Continue to read checksum following # */
470 if (ch == '#') {
471 xmitcsum = hex(get_debug_char()) << 4;
472 xmitcsum += hex(get_debug_char());
473
474 /* Checksum */
475 if (checksum != xmitcsum)
476 put_debug_char('-'); /* Failed checksum */
477 else {
478 /* Ack successful transfer */
479 put_debug_char('+');
480
481 /* If a sequence char is present, reply
482 the sequence ID */
483 if (buffer[2] == ':') {
484 put_debug_char(buffer[0]);
485 put_debug_char(buffer[1]);
486
487 /* Remove sequence chars from buffer */
488 count = strlen(buffer);
489 for (i = 3; i <= count; i++)
490 buffer[i - 3] = buffer[i];
491 }
492 }
493 }
494 }
495 while (checksum != xmitcsum); /* Keep trying while we fail */
496 }
497
498 /* Send the packet in the buffer with run-length encoding */
put_packet(char * buffer)499 static void put_packet(char *buffer)
500 {
501 int checksum;
502 char *src;
503 int runlen;
504 int encode;
505
506 do {
507 src = buffer;
508 put_debug_char('$');
509 checksum = 0;
510
511 /* Continue while we still have chars left */
512 while (*src) {
513 /* Check for runs up to 99 chars long */
514 for (runlen = 1; runlen < 99; runlen++) {
515 if (src[0] != src[runlen])
516 break;
517 }
518
519 if (runlen > 3) {
520 /* Got a useful amount, send encoding */
521 encode = runlen + ' ' - 4;
522 put_debug_char(*src); checksum += *src;
523 put_debug_char('*'); checksum += '*';
524 put_debug_char(encode); checksum += encode;
525 src += runlen;
526 } else {
527 /* Otherwise just send the current char */
528 put_debug_char(*src); checksum += *src;
529 src += 1;
530 }
531 }
532
533 /* '#' Separator, put high and low components of checksum */
534 put_debug_char('#');
535 put_debug_char(highhex(checksum));
536 put_debug_char(lowhex(checksum));
537 }
538 while ((get_debug_char()) != '+'); /* While no ack */
539 }
540
541 /* A bus error has occurred - perform a longjmp to return execution and
542 allow handling of the error */
kgdb_handle_bus_error(void)543 static void kgdb_handle_bus_error(void)
544 {
545 kgdb_longjmp(rem_com_env, 1);
546 }
547
548 /* Translate SH-3/4 exception numbers to unix-like signal values */
compute_signal(const int excep_code)549 static int compute_signal(const int excep_code)
550 {
551 int sigval;
552
553 switch (excep_code) {
554
555 case INVALID_INSN_VEC:
556 case INVALID_SLOT_VEC:
557 sigval = SIGILL;
558 break;
559 case ADDRESS_ERROR_LOAD_VEC:
560 case ADDRESS_ERROR_STORE_VEC:
561 sigval = SIGSEGV;
562 break;
563
564 case SERIAL_BREAK_VEC:
565 case NMI_VEC:
566 sigval = SIGINT;
567 break;
568
569 case USER_BREAK_VEC:
570 case TRAP_VEC:
571 sigval = SIGTRAP;
572 break;
573
574 default:
575 sigval = SIGBUS; /* "software generated" */
576 break;
577 }
578
579 return (sigval);
580 }
581
582 /* Make a local copy of the registers passed into the handler (bletch) */
kgdb_regs_to_gdb_regs(const struct kgdb_regs * regs,int * gdb_regs)583 static void kgdb_regs_to_gdb_regs(const struct kgdb_regs *regs,
584 int *gdb_regs)
585 {
586 gdb_regs[R0] = regs->regs[R0];
587 gdb_regs[R1] = regs->regs[R1];
588 gdb_regs[R2] = regs->regs[R2];
589 gdb_regs[R3] = regs->regs[R3];
590 gdb_regs[R4] = regs->regs[R4];
591 gdb_regs[R5] = regs->regs[R5];
592 gdb_regs[R6] = regs->regs[R6];
593 gdb_regs[R7] = regs->regs[R7];
594 gdb_regs[R8] = regs->regs[R8];
595 gdb_regs[R9] = regs->regs[R9];
596 gdb_regs[R10] = regs->regs[R10];
597 gdb_regs[R11] = regs->regs[R11];
598 gdb_regs[R12] = regs->regs[R12];
599 gdb_regs[R13] = regs->regs[R13];
600 gdb_regs[R14] = regs->regs[R14];
601 gdb_regs[R15] = regs->regs[R15];
602 gdb_regs[PC] = regs->pc;
603 gdb_regs[PR] = regs->pr;
604 gdb_regs[GBR] = regs->gbr;
605 gdb_regs[MACH] = regs->mach;
606 gdb_regs[MACL] = regs->macl;
607 gdb_regs[SR] = regs->sr;
608 gdb_regs[VBR] = regs->vbr;
609 }
610
611 /* Copy local gdb registers back to kgdb regs, for later copy to kernel */
gdb_regs_to_kgdb_regs(const int * gdb_regs,struct kgdb_regs * regs)612 static void gdb_regs_to_kgdb_regs(const int *gdb_regs,
613 struct kgdb_regs *regs)
614 {
615 regs->regs[R0] = gdb_regs[R0];
616 regs->regs[R1] = gdb_regs[R1];
617 regs->regs[R2] = gdb_regs[R2];
618 regs->regs[R3] = gdb_regs[R3];
619 regs->regs[R4] = gdb_regs[R4];
620 regs->regs[R5] = gdb_regs[R5];
621 regs->regs[R6] = gdb_regs[R6];
622 regs->regs[R7] = gdb_regs[R7];
623 regs->regs[R8] = gdb_regs[R8];
624 regs->regs[R9] = gdb_regs[R9];
625 regs->regs[R10] = gdb_regs[R10];
626 regs->regs[R11] = gdb_regs[R11];
627 regs->regs[R12] = gdb_regs[R12];
628 regs->regs[R13] = gdb_regs[R13];
629 regs->regs[R14] = gdb_regs[R14];
630 regs->regs[R15] = gdb_regs[R15];
631 regs->pc = gdb_regs[PC];
632 regs->pr = gdb_regs[PR];
633 regs->gbr = gdb_regs[GBR];
634 regs->mach = gdb_regs[MACH];
635 regs->macl = gdb_regs[MACL];
636 regs->sr = gdb_regs[SR];
637 regs->vbr = gdb_regs[VBR];
638 }
639
640 #ifdef CONFIG_KGDB_THREAD
641 /* Make a local copy of registers from the specified thread */
642 asmlinkage void ret_from_fork(void);
thread_regs_to_gdb_regs(const struct task_struct * thread,int * gdb_regs)643 static void thread_regs_to_gdb_regs(const struct task_struct *thread,
644 int *gdb_regs)
645 {
646 int regno;
647 int *tregs;
648
649 /* Initialize to zero */
650 for (regno = 0; regno < MAXREG; regno++)
651 gdb_regs[regno] = 0;
652
653 /* Just making sure... */
654 if (thread == NULL)
655 return;
656
657 /* A new fork has pt_regs on the stack from a fork() call */
658 if (thread->thread.pc == (unsigned long)ret_from_fork) {
659
660 int vbr_val;
661 struct pt_regs *kregs;
662 kregs = (struct pt_regs*)thread->thread.sp;
663
664 gdb_regs[R0] = kregs->regs[R0];
665 gdb_regs[R1] = kregs->regs[R1];
666 gdb_regs[R2] = kregs->regs[R2];
667 gdb_regs[R3] = kregs->regs[R3];
668 gdb_regs[R4] = kregs->regs[R4];
669 gdb_regs[R5] = kregs->regs[R5];
670 gdb_regs[R6] = kregs->regs[R6];
671 gdb_regs[R7] = kregs->regs[R7];
672 gdb_regs[R8] = kregs->regs[R8];
673 gdb_regs[R9] = kregs->regs[R9];
674 gdb_regs[R10] = kregs->regs[R10];
675 gdb_regs[R11] = kregs->regs[R11];
676 gdb_regs[R12] = kregs->regs[R12];
677 gdb_regs[R13] = kregs->regs[R13];
678 gdb_regs[R14] = kregs->regs[R14];
679 gdb_regs[R15] = kregs->regs[R15];
680 gdb_regs[PC] = kregs->pc;
681 gdb_regs[PR] = kregs->pr;
682 gdb_regs[GBR] = kregs->gbr;
683 gdb_regs[MACH] = kregs->mach;
684 gdb_regs[MACL] = kregs->macl;
685 gdb_regs[SR] = kregs->sr;
686
687 asm("stc vbr, %0":"=r"(vbr_val));
688 gdb_regs[VBR] = vbr_val;
689 return;
690 }
691
692 /* Otherwise, we have only some registers from switch_to() */
693 tregs = (int *)thread->thread.sp;
694 gdb_regs[R15] = (int)tregs;
695 gdb_regs[R14] = *tregs++;
696 gdb_regs[R13] = *tregs++;
697 gdb_regs[R12] = *tregs++;
698 gdb_regs[R11] = *tregs++;
699 gdb_regs[R10] = *tregs++;
700 gdb_regs[R9] = *tregs++;
701 gdb_regs[R8] = *tregs++;
702 gdb_regs[PR] = *tregs++;
703 gdb_regs[GBR] = *tregs++;
704 gdb_regs[PC] = thread->thread.pc;
705 }
706 #endif /* CONFIG_KGDB_THREAD */
707
708 /* Calculate the new address for after a step */
get_step_address(void)709 static short *get_step_address(void)
710 {
711 short op = *(short *) trap_registers.pc;
712 long addr;
713
714 /* BT */
715 if (OPCODE_BT(op)) {
716 if (trap_registers.sr & SR_T_BIT_MASK)
717 addr = trap_registers.pc + 4 + OPCODE_BTF_DISP(op);
718 else
719 addr = trap_registers.pc + 2;
720 }
721
722 /* BTS */
723 else if (OPCODE_BTS(op)) {
724 if (trap_registers.sr & SR_T_BIT_MASK)
725 addr = trap_registers.pc + 4 + OPCODE_BTF_DISP(op);
726 else
727 addr = trap_registers.pc + 4; /* Not in delay slot */
728 }
729
730 /* BF */
731 else if (OPCODE_BF(op)) {
732 if (!(trap_registers.sr & SR_T_BIT_MASK))
733 addr = trap_registers.pc + 4 + OPCODE_BTF_DISP(op);
734 else
735 addr = trap_registers.pc + 2;
736 }
737
738 /* BFS */
739 else if (OPCODE_BFS(op)) {
740 if (!(trap_registers.sr & SR_T_BIT_MASK))
741 addr = trap_registers.pc + 4 + OPCODE_BTF_DISP(op);
742 else
743 addr = trap_registers.pc + 4; /* Not in delay slot */
744 }
745
746 /* BRA */
747 else if (OPCODE_BRA(op))
748 addr = trap_registers.pc + 4 + OPCODE_BRA_DISP(op);
749
750 /* BRAF */
751 else if (OPCODE_BRAF(op))
752 addr = trap_registers.pc + 4
753 + trap_registers.regs[OPCODE_BRAF_REG(op)];
754
755 /* BSR */
756 else if (OPCODE_BSR(op))
757 addr = trap_registers.pc + 4 + OPCODE_BSR_DISP(op);
758
759 /* BSRF */
760 else if (OPCODE_BSRF(op))
761 addr = trap_registers.pc + 4
762 + trap_registers.regs[OPCODE_BSRF_REG(op)];
763
764 /* JMP */
765 else if (OPCODE_JMP(op))
766 addr = trap_registers.regs[OPCODE_JMP_REG(op)];
767
768 /* JSR */
769 else if (OPCODE_JSR(op))
770 addr = trap_registers.regs[OPCODE_JSR_REG(op)];
771
772 /* RTS */
773 else if (OPCODE_RTS(op))
774 addr = trap_registers.pr;
775
776 /* RTE */
777 else if (OPCODE_RTE(op))
778 addr = trap_registers.regs[15];
779
780 /* Other */
781 else
782 addr = trap_registers.pc + 2;
783
784 kgdb_flush_icache_range(addr, addr + 2);
785 return (short *) addr;
786 }
787
788 /* Set up a single-step. Replace the instruction immediately after the
789 current instruction (i.e. next in the expected flow of control) with a
790 trap instruction, so that returning will cause only a single instruction
791 to be executed. Note that this model is slightly broken for instructions
792 with delay slots (e.g. B[TF]S, BSR, BRA etc), where both the branch
793 and the instruction in the delay slot will be executed. */
do_single_step(void)794 static void do_single_step(void)
795 {
796 unsigned short *addr = 0;
797
798 /* Determine where the target instruction will send us to */
799 addr = get_step_address();
800 stepped_address = (int)addr;
801
802 /* Replace it */
803 stepped_opcode = *(short *)addr;
804 *addr = STEP_OPCODE;
805
806 /* Flush and return */
807 kgdb_flush_icache_range((long) addr, (long) addr + 2);
808 return;
809 }
810
811 /* Undo a single step */
undo_single_step(void)812 static void undo_single_step(void)
813 {
814 /* If we have stepped, put back the old instruction */
815 /* Use stepped_address in case we stopped elsewhere */
816 if (stepped_opcode != 0) {
817 *(short*)stepped_address = stepped_opcode;
818 kgdb_flush_icache_range(stepped_address, stepped_address + 2);
819 }
820 stepped_opcode = 0;
821 }
822
823 /* Send a signal message */
send_signal_msg(const int signum)824 static void send_signal_msg(const int signum)
825 {
826 #ifndef CONFIG_KGDB_THREAD
827 out_buffer[0] = 'S';
828 out_buffer[1] = highhex(signum);
829 out_buffer[2] = lowhex(signum);
830 out_buffer[3] = 0;
831 put_packet(out_buffer);
832 #else /* CONFIG_KGDB_THREAD */
833 int threadid;
834 threadref thref;
835 char *out = out_buffer;
836 const char *tstring = "thread";
837
838 *out++ = 'T';
839 *out++ = highhex(signum);
840 *out++ = lowhex(signum);
841
842 while (*tstring) {
843 *out++ = *tstring++;
844 }
845 *out++ = ':';
846
847 threadid = trapped_thread->pid;
848 if (threadid == 0) threadid = PID_MAX;
849 int_to_threadref(&thref, threadid);
850 pack_threadid(out, &thref);
851 out += BUF_THREAD_ID_SIZE;
852 *out++ = ';';
853
854 *out = 0;
855 put_packet(out_buffer);
856 #endif /* CONFIG_KGDB_THREAD */
857 }
858
859 /* Reply that all was well */
send_ok_msg(void)860 static void send_ok_msg(void)
861 {
862 strcpy(out_buffer, "OK");
863 put_packet(out_buffer);
864 }
865
866 /* Reply that an error occurred */
send_err_msg(void)867 static void send_err_msg(void)
868 {
869 strcpy(out_buffer, "E01");
870 put_packet(out_buffer);
871 }
872
873 /* Empty message indicates unrecognised command */
send_empty_msg(void)874 static void send_empty_msg(void)
875 {
876 put_packet("");
877 }
878
879 /* Read memory due to 'm' message */
read_mem_msg(void)880 static void read_mem_msg(void)
881 {
882 char *ptr;
883 int addr;
884 int length;
885
886 /* Jmp, disable bus error handler */
887 if (kgdb_setjmp(rem_com_env) == 0) {
888
889 kgdb_nofault = 1;
890
891 /* Walk through, have m<addr>,<length> */
892 ptr = &in_buffer[1];
893 if (hex_to_int(&ptr, &addr) && (*ptr++ == ','))
894 if (hex_to_int(&ptr, &length)) {
895 ptr = 0;
896 if (length * 2 > OUTBUFMAX)
897 length = OUTBUFMAX / 2;
898 mem_to_hex((char *) addr, out_buffer, length);
899 }
900 if (ptr)
901 send_err_msg();
902 else
903 put_packet(out_buffer);
904 } else
905 send_err_msg();
906
907 /* Restore bus error handler */
908 kgdb_nofault = 0;
909 }
910
911 /* Write memory due to 'M' or 'X' message */
write_mem_msg(int binary)912 static void write_mem_msg(int binary)
913 {
914 char *ptr;
915 int addr;
916 int length;
917
918 if (kgdb_setjmp(rem_com_env) == 0) {
919
920 kgdb_nofault = 1;
921
922 /* Walk through, have M<addr>,<length>:<data> */
923 ptr = &in_buffer[1];
924 if (hex_to_int(&ptr, &addr) && (*ptr++ == ','))
925 if (hex_to_int(&ptr, &length) && (*ptr++ == ':')) {
926 if (binary)
927 ebin_to_mem(ptr, (char*)addr, length);
928 else
929 hex_to_mem(ptr, (char*)addr, length);
930 kgdb_flush_icache_range(addr, addr + length);
931 ptr = 0;
932 send_ok_msg();
933 }
934 if (ptr)
935 send_err_msg();
936 } else
937 send_err_msg();
938
939 /* Restore bus error handler */
940 kgdb_nofault = 0;
941 }
942
943 /* Continue message */
continue_msg(void)944 static void continue_msg(void)
945 {
946 /* Try to read optional parameter, PC unchanged if none */
947 char *ptr = &in_buffer[1];
948 int addr;
949
950 if (hex_to_int(&ptr, &addr))
951 trap_registers.pc = addr;
952 }
953
954 /* Continue message with signal */
continue_with_sig_msg(void)955 static void continue_with_sig_msg(void)
956 {
957 int signal;
958 char *ptr = &in_buffer[1];
959 int addr;
960
961 /* Report limitation */
962 kgdb_to_gdb("Cannot force signal in kgdb, continuing anyway.\n");
963
964 /* Signal */
965 hex_to_int(&ptr, &signal);
966 if (*ptr == ';')
967 ptr++;
968
969 /* Optional address */
970 if (hex_to_int(&ptr, &addr))
971 trap_registers.pc = addr;
972 }
973
974 /* Step message */
step_msg(void)975 static void step_msg(void)
976 {
977 continue_msg();
978 do_single_step();
979 }
980
981 /* Step message with signal */
step_with_sig_msg(void)982 static void step_with_sig_msg(void)
983 {
984 continue_with_sig_msg();
985 do_single_step();
986 }
987
988 /* Send register contents */
send_regs_msg(void)989 static void send_regs_msg(void)
990 {
991 #ifdef CONFIG_KGDB_THREAD
992 if (!current_thread)
993 kgdb_regs_to_gdb_regs(&trap_registers, registers);
994 else
995 thread_regs_to_gdb_regs(current_thread, registers);
996 #else
997 kgdb_regs_to_gdb_regs(&trap_registers, registers);
998 #endif
999
1000 mem_to_hex((char *) registers, out_buffer, NUMREGBYTES);
1001 put_packet(out_buffer);
1002 }
1003
1004 /* Set register contents - currently can't set other thread's registers */
set_regs_msg(void)1005 static void set_regs_msg(void)
1006 {
1007 #ifdef CONFIG_KGDB_THREAD
1008 if (!current_thread) {
1009 #endif
1010 kgdb_regs_to_gdb_regs(&trap_registers, registers);
1011 hex_to_mem(&in_buffer[1], (char *) registers, NUMREGBYTES);
1012 gdb_regs_to_kgdb_regs(registers, &trap_registers);
1013 send_ok_msg();
1014 #ifdef CONFIG_KGDB_THREAD
1015 } else
1016 send_err_msg();
1017 #endif
1018 }
1019
1020
1021 #ifdef CONFIG_KGDB_THREAD
1022
1023 /* Set the status for a thread */
set_thread_msg(void)1024 void set_thread_msg(void)
1025 {
1026 int threadid;
1027 struct task_struct *thread = NULL;
1028 char *ptr;
1029
1030 switch (in_buffer[1]) {
1031
1032 /* To select which thread for gG etc messages, i.e. supported */
1033 case 'g':
1034
1035 ptr = &in_buffer[2];
1036 hex_to_int(&ptr, &threadid);
1037 thread = get_thread(threadid);
1038
1039 /* If we haven't found it */
1040 if (!thread) {
1041 send_err_msg();
1042 break;
1043 }
1044
1045 /* Set current_thread (or not) */
1046 if (thread == trapped_thread)
1047 current_thread = NULL;
1048 else
1049 current_thread = thread;
1050 send_ok_msg();
1051 break;
1052
1053 /* To select which thread for cCsS messages, i.e. unsupported */
1054 case 'c':
1055 send_ok_msg();
1056 break;
1057
1058 default:
1059 send_empty_msg();
1060 break;
1061 }
1062 }
1063
1064 /* Is a thread alive? */
thread_status_msg(void)1065 static void thread_status_msg(void)
1066 {
1067 char *ptr;
1068 int threadid;
1069 struct task_struct *thread = NULL;
1070
1071 ptr = &in_buffer[1];
1072 hex_to_int(&ptr, &threadid);
1073 thread = get_thread(threadid);
1074 if (thread)
1075 send_ok_msg();
1076 else
1077 send_err_msg();
1078 }
1079 /* Send the current thread ID */
thread_id_msg(void)1080 static void thread_id_msg(void)
1081 {
1082 int threadid;
1083 threadref thref;
1084
1085 out_buffer[0] = 'Q';
1086 out_buffer[1] = 'C';
1087
1088 if (current_thread)
1089 threadid = current_thread->pid;
1090 else if (trapped_thread)
1091 threadid = trapped_thread->pid;
1092 else /* Impossible, but just in case! */
1093 {
1094 send_err_msg();
1095 return;
1096 }
1097
1098 /* Translate pid 0 to PID_MAX for gdb */
1099 if (threadid == 0) threadid = PID_MAX;
1100
1101 int_to_threadref(&thref, threadid);
1102 pack_threadid(out_buffer + 2, &thref);
1103 out_buffer[2 + BUF_THREAD_ID_SIZE] = '\0';
1104 put_packet(out_buffer);
1105 }
1106
1107 /* Send thread info */
thread_info_msg(void)1108 static void thread_info_msg(void)
1109 {
1110 struct task_struct *thread = NULL;
1111 int threadid;
1112 char *pos;
1113 threadref thref;
1114
1115 /* Start with 'm' */
1116 out_buffer[0] = 'm';
1117 pos = &out_buffer[1];
1118
1119 /* For all possible thread IDs - this will overrun if > 44 threads! */
1120 /* Start at 1 and include PID_MAX (since GDB won't use pid 0...) */
1121 for (threadid = 1; threadid <= PID_MAX; threadid++) {
1122
1123 read_lock(&tasklist_lock);
1124 thread = get_thread(threadid);
1125 read_unlock(&tasklist_lock);
1126
1127 /* If it's a valid thread */
1128 if (thread) {
1129 int_to_threadref(&thref, threadid);
1130 pack_threadid(pos, &thref);
1131 pos += BUF_THREAD_ID_SIZE;
1132 *pos++ = ',';
1133 }
1134 }
1135 *--pos = 0; /* Lose final comma */
1136 put_packet(out_buffer);
1137
1138 }
1139
1140 /* Return printable info for gdb's 'info threads' command */
thread_extra_info_msg(void)1141 static void thread_extra_info_msg(void)
1142 {
1143 int threadid;
1144 struct task_struct *thread = NULL;
1145 char buffer[20], *ptr;
1146 int i;
1147
1148 /* Extract thread ID */
1149 ptr = &in_buffer[17];
1150 hex_to_int(&ptr, &threadid);
1151 thread = get_thread(threadid);
1152
1153 /* If we don't recognise it, say so */
1154 if (thread == NULL)
1155 strcpy(buffer, "(unknown)");
1156 else
1157 strcpy(buffer, thread->comm);
1158
1159 /* Construct packet */
1160 for (i = 0, ptr = out_buffer; buffer[i]; i++)
1161 ptr = pack_hex_byte(ptr, buffer[i]);
1162
1163 if (thread->thread.pc == (unsigned long)ret_from_fork) {
1164 strcpy(buffer, "<new fork>");
1165 for (i = 0; buffer[i]; i++)
1166 ptr = pack_hex_byte(ptr, buffer[i]);
1167 }
1168
1169 *ptr = '\0';
1170 put_packet(out_buffer);
1171 }
1172
1173 /* Handle all qFooBarBaz messages - have to use an if statement as
1174 opposed to a switch because q messages can have > 1 char id. */
query_msg(void)1175 static void query_msg(void)
1176 {
1177 const char *q_start = &in_buffer[1];
1178
1179 /* qC = return current thread ID */
1180 if (strncmp(q_start, "C", 1) == 0)
1181 thread_id_msg();
1182
1183 /* qfThreadInfo = query all threads (first) */
1184 else if (strncmp(q_start, "fThreadInfo", 11) == 0)
1185 thread_info_msg();
1186
1187 /* qsThreadInfo = query all threads (subsequent). We know we have sent
1188 them all after the qfThreadInfo message, so there are no to send */
1189 else if (strncmp(q_start, "sThreadInfo", 11) == 0)
1190 put_packet("l"); /* el = last */
1191
1192 /* qThreadExtraInfo = supply printable information per thread */
1193 else if (strncmp(q_start, "ThreadExtraInfo", 15) == 0)
1194 thread_extra_info_msg();
1195
1196 /* Unsupported - empty message as per spec */
1197 else
1198 send_empty_msg();
1199 }
1200 #endif /* CONFIG_KGDB_THREAD */
1201
1202 /* The command loop, read and act on requests */
kgdb_command_loop(const int excep_code,const int trapa_value)1203 static void kgdb_command_loop(const int excep_code, const int trapa_value)
1204 {
1205 int sigval;
1206
1207 if (excep_code == NMI_VEC) {
1208 #ifndef CONFIG_KGDB_NMI
1209 KGDB_PRINTK("Ignoring unexpected NMI?\n");
1210 return;
1211 #else /* CONFIG_KGDB_NMI */
1212 if (!kgdb_enabled) {
1213 kgdb_enabled = 1;
1214 kgdb_init();
1215 }
1216 #endif /* CONFIG_KGDB_NMI */
1217 }
1218
1219 /* Ignore if we're disabled */
1220 if (!kgdb_enabled)
1221 return;
1222
1223 #ifdef CONFIG_KGDB_THREAD
1224 /* Until GDB specifies a thread */
1225 current_thread = NULL;
1226 trapped_thread = current;
1227 #endif
1228
1229 /* Enter GDB mode (e.g. after detach) */
1230 if (!kgdb_in_gdb_mode) {
1231 /* Do serial setup, notify user, issue preemptive ack */
1232 (void)kgdb_serial_setup();
1233 KGDB_PRINTK("Waiting for GDB (on %s%d at %d baud)\n",
1234 (kgdb_porttype ? kgdb_porttype->name : ""),
1235 kgdb_portnum, kgdb_baud);
1236 kgdb_in_gdb_mode = 1;
1237 put_debug_char('+');
1238 }
1239
1240 /* Reply to host that an exception has occurred */
1241 sigval = compute_signal(excep_code);
1242 send_signal_msg(sigval);
1243
1244 /* TRAP_VEC exception indicates a software trap inserted in place of
1245 code by GDB so back up PC by one instruction, as this instruction
1246 will later be replaced by its original one. Do NOT do this for
1247 trap 0xff, since that indicates a compiled-in breakpoint which
1248 will not be replaced (and we would retake the trap forever) */
1249 if ((excep_code == TRAP_VEC) && (trapa_value != (0xff << 2))) {
1250 trap_registers.pc -= 2;
1251 }
1252
1253 /* Undo any stepping we may have done */
1254 undo_single_step();
1255
1256 while (1) {
1257
1258 out_buffer[0] = 0;
1259 get_packet(in_buffer, BUFMAX);
1260
1261 /* Examine first char of buffer to see what we need to do */
1262 switch (in_buffer[0]) {
1263
1264 case '?': /* Send which signal we've received */
1265 send_signal_msg(sigval);
1266 break;
1267
1268 case 'g': /* Return the values of the CPU registers */
1269 send_regs_msg();
1270 break;
1271
1272 case 'G': /* Set the value of the CPU registers */
1273 set_regs_msg();
1274 break;
1275
1276 case 'm': /* Read LLLL bytes address AA..AA */
1277 read_mem_msg();
1278 break;
1279
1280 case 'M': /* Write LLLL bytes address AA..AA, ret OK */
1281 write_mem_msg(0); /* 0 = data in hex */
1282 break;
1283
1284 case 'X': /* Write LLLL bytes esc bin address AA..AA */
1285 if (kgdb_bits == '8')
1286 write_mem_msg(1); /* 1 = data in binary */
1287 else
1288 send_empty_msg();
1289 break;
1290
1291 case 'C': /* Continue, signum included, we ignore it */
1292 continue_with_sig_msg();
1293 return;
1294
1295 case 'c': /* Continue at address AA..AA (optional) */
1296 continue_msg();
1297 return;
1298
1299 case 'S': /* Step, signum included, we ignore it */
1300 step_with_sig_msg();
1301 return;
1302
1303 case 's': /* Step one instruction from AA..AA */
1304 step_msg();
1305 return;
1306
1307 #ifdef CONFIG_KGDB_THREAD
1308
1309 case 'H': /* Task related */
1310 set_thread_msg();
1311 break;
1312
1313 case 'T': /* Query thread status */
1314 thread_status_msg();
1315 break;
1316
1317 case 'q': /* Handle query - currently thread-related */
1318 query_msg();
1319 break;
1320 #endif
1321
1322 case 'k': /* 'Kill the program' with a kernel ? */
1323 break;
1324
1325 case 'D': /* Detach from program, send reply OK */
1326 kgdb_in_gdb_mode = 0;
1327 send_ok_msg();
1328 get_debug_char();
1329 return;
1330
1331 default:
1332 send_empty_msg();
1333 break;
1334 }
1335 }
1336 }
1337
1338 /* There has been an exception, most likely a breakpoint. */
kgdb_handle_exception(struct pt_regs * regs)1339 void kgdb_handle_exception(struct pt_regs *regs)
1340 {
1341 int excep_code, vbr_val;
1342 int count;
1343 int trapa_value = ctrl_inl(TRA);
1344
1345 /* Copy kernel regs (from stack) */
1346 for (count = 0; count < 16; count++)
1347 trap_registers.regs[count] = regs->regs[count];
1348 trap_registers.pc = regs->pc;
1349 trap_registers.pr = regs->pr;
1350 trap_registers.sr = regs->sr;
1351 trap_registers.gbr = regs->gbr;
1352 trap_registers.mach = regs->mach;
1353 trap_registers.macl = regs->macl;
1354
1355 asm("stc vbr, %0":"=r"(vbr_val));
1356 trap_registers.vbr = vbr_val;
1357
1358 /* Get excode for command loop call, user access */
1359 asm("stc r2_bank, %0":"=r"(excep_code));
1360 kgdb_excode = excep_code;
1361
1362 /* Other interesting environment items for reference */
1363 asm("stc r6_bank, %0":"=r"(kgdb_g_imask));
1364 kgdb_current = current;
1365 kgdb_trapa_val = trapa_value;
1366 kgdb_tregs = regs;
1367
1368 /* Act on the exception */
1369 kgdb_command_loop(excep_code >> 5, trapa_value);
1370
1371 kgdb_current = NULL;
1372
1373 /* Copy back the (maybe modified) registers */
1374 for (count = 0; count < 16; count++)
1375 regs->regs[count] = trap_registers.regs[count];
1376 regs->pc = trap_registers.pc;
1377 regs->pr = trap_registers.pr;
1378 regs->sr = trap_registers.sr;
1379 regs->gbr = trap_registers.gbr;
1380 regs->mach = trap_registers.mach;
1381 regs->macl = trap_registers.macl;
1382
1383 vbr_val = trap_registers.vbr;
1384 asm("ldc %0, vbr": :"r"(vbr_val));
1385
1386 return;
1387 }
1388
1389 /* Trigger a breakpoint by function */
breakpoint(void)1390 void breakpoint(void)
1391 {
1392 if (!kgdb_enabled) {
1393 kgdb_enabled = 1;
1394 kgdb_init();
1395 }
1396 BREAKPOINT();
1397 }
1398
1399 /* Initialise the KGDB data structures and serial configuration */
kgdb_init(void)1400 int kgdb_init(void)
1401 {
1402 if (!kgdb_enabled)
1403 return 1;
1404
1405 in_nmi = 0;
1406 kgdb_nofault = 0;
1407 stepped_opcode = 0;
1408 kgdb_in_gdb_mode = 0;
1409
1410 /* Set up for serial comms */
1411 if (kgdb_serial_setup == NULL) {
1412 KGDB_PRINTK("no serial init function!!\n");
1413 return -1;
1414 }
1415
1416 if (kgdb_serial_setup() != 0) {
1417 KGDB_PRINTK("serial setup error\n");
1418 return -1;
1419 }
1420
1421 /* Init ptr to exception handler */
1422 kgdb_debug_hook = kgdb_handle_exception;
1423 kgdb_bus_err_hook = kgdb_handle_bus_error;
1424
1425 /* Enter kgdb now if requested, or just report init done */
1426 if (kgdb_halt) {
1427 kgdb_in_gdb_mode = 1;
1428 put_debug_char('+');
1429 breakpoint();
1430 }
1431 else
1432 {
1433 KGDB_PRINTK("stub is initialized.\n");
1434 }
1435
1436 return 0;
1437 }
1438
1439 /* Make function available for "user messages"; console will use it too. */
1440
1441 char gdbmsgbuf[BUFMAX];
1442 #define MAXOUT ((BUFMAX-2)/2)
1443
kgdb_msg_write(const char * s,unsigned count)1444 static void kgdb_msg_write(const char *s, unsigned count)
1445 {
1446 int i;
1447 int wcount;
1448 char *bufptr;
1449
1450 /* 'O'utput */
1451 gdbmsgbuf[0] = 'O';
1452
1453 /* Fill and send buffers... */
1454 while (count > 0) {
1455 bufptr = gdbmsgbuf + 1;
1456
1457 /* Calculate how many this time */
1458 wcount = (count > MAXOUT) ? MAXOUT : count;
1459
1460 /* Pack in hex chars */
1461 for (i = 0; i < wcount; i++)
1462 bufptr = pack_hex_byte(bufptr, s[i]);
1463 *bufptr = '\0';
1464
1465 /* Move up */
1466 s += wcount;
1467 count -= wcount;
1468
1469 /* Write packet */
1470 put_packet(gdbmsgbuf);
1471 }
1472 }
1473
kgdb_to_gdb(const char * s)1474 static void kgdb_to_gdb(const char *s)
1475 {
1476 kgdb_msg_write(s, strlen(s));
1477 }
1478
1479 #ifdef CONFIG_SH_KGDB_CONSOLE
kgdb_console_write(struct console * co,const char * s,unsigned count)1480 void kgdb_console_write(struct console *co, const char *s, unsigned count)
1481 {
1482 /* Bail if we're not talking to GDB */
1483 if (!kgdb_in_gdb_mode)
1484 return;
1485
1486 kgdb_msg_write(s, count);
1487 }
1488 #endif
1489