1 /*
2 * Copyright (C) 2005 Jimi Xenidis <jimix@watson.ibm.com>, IBM Corporation
3 * Copyright (C) 2006 Isaku Yamahata <yamahata at valinux co jp>
4 * VA Linux Systems Japan. K.K.
5 *
6 * gdbstub arch neutral part
7 * Based on x86 cdb (xen/arch/x86/cdb.c) and ppc gdbstub(xen/common/gdbstub.c)
8 * But extensively modified.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25 * gdbstub: implements the architecture independant parts of the
26 * gdb remote protocol.
27 */
28
29 /* We try to avoid assuming much about what the rest of the system is
30 doing. In particular, dynamic memory allocation is out of the
31 question. */
32
33 /* Resuming after we've stopped used to work, but more through luck
34 than any actual intention. It doesn't at the moment. */
35
36 #include <xen/lib.h>
37 #include <xen/spinlock.h>
38 #include <xen/serial.h>
39 #include <xen/irq.h>
40 #include <xen/watchdog.h>
41 #include <asm/debugger.h>
42 #include <xen/init.h>
43 #include <xen/smp.h>
44 #include <xen/console.h>
45 #include <xen/errno.h>
46 #include <xen/delay.h>
47 #include <asm/byteorder.h>
48
49 /* Printk isn't particularly safe just after we've trapped to the
50 debugger. so avoid it. */
51 #define dbg_printk(...)
52 /*#define dbg_printk(...) printk(__VA_ARGS__)*/
53
54 #define GDB_RETRY_MAX 10
55
56 struct gdb_cpu_info
57 {
58 atomic_t paused;
59 atomic_t ack;
60 };
61
62 static struct gdb_cpu_info gdb_cpu[NR_CPUS];
63 static atomic_t gdb_smp_paused_count;
64
65 static void gdb_smp_pause(void);
66 static void gdb_smp_resume(void);
67
68 static char __initdata opt_gdb[30];
69 string_param("gdb", opt_gdb);
70
71 static void gdbstub_console_puts(const char *str);
72
73 /* value <-> char (de)serialzers */
74 static char
hex2char(unsigned long x)75 hex2char(unsigned long x)
76 {
77 const char array[] = "0123456789abcdef";
78 return array[x & 15];
79 }
80
81 static unsigned int
char2hex(unsigned char c)82 char2hex(unsigned char c)
83 {
84 if ( (c >= '0') && (c <= '9') )
85 return c - '0';
86 else if ( (c >= 'a') && (c <= 'f') )
87 return c - 'a' + 10;
88 else if ( (c >= 'A') && (c <= 'F') )
89 return c - 'A' + 10;
90 else
91 BUG();
92 return -1;
93 }
94
95 static unsigned char
str2hex(const char * str)96 str2hex(const char *str)
97 {
98 return (char2hex(str[0]) << 4) | char2hex(str[1]);
99 }
100
101 static unsigned long
str2ulong(const char * str,unsigned long bytes)102 str2ulong(const char *str, unsigned long bytes)
103 {
104 unsigned long x = 0;
105 unsigned long i = 0;
106
107 while ( *str && (i < (bytes * 2)) )
108 {
109 x <<= 4;
110 x += char2hex(*str);
111 ++str;
112 ++i;
113 }
114
115 return x;
116 }
117
118 static unsigned long
str_to_native_ulong(const char * str)119 str_to_native_ulong(const char *str)
120 {
121 unsigned long x = 0, i = 0;
122
123 while ( *str && (i < BYTES_PER_LONG) )
124 {
125 #ifdef __BIG_ENDIAN
126 x <<= 8;
127 x += str2hex(str);
128 #elif defined(__LITTLE_ENDIAN)
129 x += (unsigned long)str2hex(str) << (i*8);
130 #else
131 # error unknown endian
132 #endif
133 str += 2;
134 i++;
135 }
136
137 return x;
138 }
139
140 /* gdb io wrappers */
141 static signed long
gdb_io_write(const char * buf,unsigned long len,struct gdb_context * ctx)142 gdb_io_write(const char *buf, unsigned long len, struct gdb_context *ctx)
143 {
144 int i;
145 for ( i = 0; i < len; i++ )
146 serial_putc(ctx->serhnd, buf[i]);
147 return i;
148 }
149
150 static int
gdb_io_write_char(u8 data,struct gdb_context * ctx)151 gdb_io_write_char(u8 data, struct gdb_context *ctx)
152 {
153 return gdb_io_write((char*)&data, 1, ctx);
154 }
155
156 static unsigned char
gdb_io_read(struct gdb_context * ctx)157 gdb_io_read(struct gdb_context *ctx)
158 {
159 return serial_getc(ctx->serhnd);
160 }
161
162 /* Receive a command. Returns -1 on csum error, 0 otherwise. */
163 /* Does not acknowledge. */
164 static int
attempt_receive_packet(struct gdb_context * ctx)165 attempt_receive_packet(struct gdb_context *ctx)
166 {
167 u8 csum;
168 u8 received_csum;
169 u8 ch;
170
171 /* Skip over everything up to the first '$' */
172 while ( (ch = gdb_io_read(ctx)) != '$' )
173 continue;
174
175 csum = 0;
176 for ( ctx->in_bytes = 0;
177 ctx->in_bytes < sizeof(ctx->in_buf);
178 ctx->in_bytes++ )
179 {
180 ch = gdb_io_read(ctx);
181 if ( ch == '#' )
182 break;
183 ctx->in_buf[ctx->in_bytes] = ch;
184 csum += ch;
185 }
186
187 if ( ctx->in_bytes == sizeof(ctx->in_buf) )
188 {
189 dbg_printk("WARNING: GDB sent a stupidly big packet.\n");
190 return -1;
191 }
192
193 ctx->in_buf[ctx->in_bytes] = '\0';
194 received_csum = char2hex(gdb_io_read(ctx)) * 16 +
195 char2hex(gdb_io_read(ctx));
196
197 return (received_csum == csum) ? 0 : -1;
198 }
199
200 /* Receive a command, discarding up to ten packets with csum
201 * errors. Acknowledges all received packets. */
202 static int
receive_command(struct gdb_context * ctx)203 receive_command(struct gdb_context *ctx)
204 {
205 int r, count = 0;
206
207 count = 0;
208 do {
209 r = attempt_receive_packet(ctx);
210 gdb_io_write_char((r < 0) ? '-' : '+', ctx);
211 count++;
212 } while ( (r < 0) && (count < GDB_RETRY_MAX) );
213
214 return r;
215 }
216
217 /* routines to send reply packets */
218
219 static void
gdb_start_packet(struct gdb_context * ctx)220 gdb_start_packet(struct gdb_context *ctx)
221 {
222 ctx->out_buf[0] = '$';
223 ctx->out_offset = 1;
224 ctx->out_csum = 0;
225 }
226
227 static void
gdb_write_to_packet_char(u8 data,struct gdb_context * ctx)228 gdb_write_to_packet_char(u8 data, struct gdb_context *ctx)
229 {
230 ctx->out_csum += data;
231 ctx->out_buf[ctx->out_offset] = data;
232 ctx->out_offset++;
233 }
234
235 void
gdb_write_to_packet(const char * buf,int count,struct gdb_context * ctx)236 gdb_write_to_packet(const char *buf, int count, struct gdb_context *ctx)
237 {
238 int x;
239 for ( x = 0; x < count; x++ )
240 gdb_write_to_packet_char(buf[x], ctx);
241 }
242
243 void
gdb_write_to_packet_str(const char * buf,struct gdb_context * ctx)244 gdb_write_to_packet_str(const char *buf, struct gdb_context *ctx)
245 {
246 gdb_write_to_packet(buf, strlen(buf), ctx);
247 }
248
249 void
gdb_write_to_packet_hex(unsigned long x,int int_size,struct gdb_context * ctx)250 gdb_write_to_packet_hex(unsigned long x, int int_size, struct gdb_context *ctx)
251 {
252 char buf[sizeof(unsigned long) * 2 + 1];
253 int i, width = int_size * 2;
254
255 buf[sizeof(unsigned long) * 2] = 0;
256
257 switch ( int_size )
258 {
259 case sizeof(u8):
260 case sizeof(u16):
261 case sizeof(u32):
262 case sizeof(u64):
263 break;
264 default:
265 dbg_printk("WARNING: %s x: %#lx int_size: %d\n",
266 __func__, x, int_size);
267 break;
268 }
269
270 #ifdef __BIG_ENDIAN
271 i = sizeof(unsigned long) * 2
272 do {
273 buf[--i] = hex2char(x & 15);
274 x >>= 4;
275 } while ( x );
276
277 while ( (i + width) > (sizeof(unsigned long) * 2) )
278 buf[--i] = '0';
279
280 gdb_write_to_packet(&buf[i], width, ctx);
281 #elif defined(__LITTLE_ENDIAN)
282 i = 0;
283 while ( i < width )
284 {
285 buf[i++] = hex2char(x>>4);
286 buf[i++] = hex2char(x);
287 x >>= 8;
288 }
289 gdb_write_to_packet(buf, width, ctx);
290 #else
291 # error unknown endian
292 #endif
293 }
294
295 static int
gdb_check_ack(struct gdb_context * ctx)296 gdb_check_ack(struct gdb_context *ctx)
297 {
298 u8 c = gdb_io_read(ctx);
299
300 switch ( c )
301 {
302 case '+':
303 return 1;
304 case '-':
305 return 0;
306 default:
307 printk("Bad ack: %c\n", c);
308 return 0;
309 }
310 }
311
312 /* Return 0 if the reply was successfully received, !0 otherwise. */
313 void
gdb_send_packet(struct gdb_context * ctx)314 gdb_send_packet(struct gdb_context *ctx)
315 {
316 char buf[3];
317 int count;
318
319 snprintf(buf, sizeof(buf), "%.02x\n", ctx->out_csum);
320
321 gdb_write_to_packet_char('#', ctx);
322 gdb_write_to_packet(buf, 2, ctx);
323
324 count = 0;
325 do {
326 gdb_io_write(ctx->out_buf, ctx->out_offset, ctx);
327 } while ( !gdb_check_ack(ctx) && (count++ < GDB_RETRY_MAX) );
328
329 if ( count == GDB_RETRY_MAX )
330 dbg_printk("WARNING: %s reached max retry %d\n",
331 __func__, GDB_RETRY_MAX);
332 }
333
334 void
gdb_send_reply(const char * buf,struct gdb_context * ctx)335 gdb_send_reply(const char *buf, struct gdb_context *ctx)
336 {
337 gdb_start_packet(ctx);
338 gdb_write_to_packet_str(buf, ctx);
339 gdb_send_packet(ctx);
340 }
341
342 /* arch neutral command handlers */
343
344 static void
gdb_cmd_signum(struct gdb_context * ctx)345 gdb_cmd_signum(struct gdb_context *ctx)
346 {
347 gdb_write_to_packet_char('S', ctx);
348 gdb_write_to_packet_hex(ctx->signum, sizeof(ctx->signum), ctx);
349 gdb_send_packet(ctx);
350 }
351
352 static void
gdb_cmd_read_mem(unsigned long addr,unsigned long length,struct gdb_context * ctx)353 gdb_cmd_read_mem(unsigned long addr, unsigned long length,
354 struct gdb_context *ctx)
355 {
356 int x, r;
357 unsigned char val;
358
359 dbg_printk("Memory read starting at %lx, length %lx.\n", addr,
360 length);
361
362 for ( x = 0; x < length; x++ )
363 {
364 r = gdb_arch_copy_from_user(&val, (void *)(addr + x), 1);
365 if ( r != 0 )
366 {
367 dbg_printk("Error reading from %lx.\n", addr + x);
368 break;
369 }
370 gdb_write_to_packet_hex(val, sizeof(val), ctx);
371 }
372
373 if ( x == 0 )
374 gdb_write_to_packet_str("E05", ctx);
375
376 dbg_printk("Read done.\n");
377
378 gdb_send_packet(ctx);
379 }
380
381 static void
gdb_cmd_write_mem(unsigned long addr,unsigned long length,const char * buf,struct gdb_context * ctx)382 gdb_cmd_write_mem(unsigned long addr, unsigned long length,
383 const char *buf, struct gdb_context *ctx)
384 {
385 int x, r;
386 unsigned char val;
387
388 dbg_printk("Memory write starting at %lx, length %lx.\n", addr, length);
389
390 for ( x = 0; x < length; x++, addr++, buf += 2 )
391 {
392 val = str2ulong(buf, sizeof(val));
393 r = gdb_arch_copy_to_user((void*)addr, (void*)&val, 1);
394 if ( r != 0 )
395 {
396 dbg_printk("Error writing to %lx.\n", addr);
397 break;
398 }
399 }
400
401 if (x == length)
402 gdb_write_to_packet_str("OK", ctx);
403 else
404 gdb_write_to_packet_str("E11", ctx);
405
406 dbg_printk("Write done.\n");
407
408 gdb_send_packet(ctx);
409 }
410
411 static void
gdbstub_attach(struct gdb_context * ctx)412 gdbstub_attach(struct gdb_context *ctx)
413 {
414 if ( ctx->currently_attached )
415 return;
416 ctx->currently_attached = 1;
417 ctx->console_steal_id = console_steal(ctx->serhnd, gdbstub_console_puts);
418 }
419
420 static void
gdbstub_detach(struct gdb_context * ctx)421 gdbstub_detach(struct gdb_context *ctx)
422 {
423 if ( !ctx->currently_attached )
424 return;
425 ctx->currently_attached = 0;
426 console_giveback(ctx->console_steal_id);
427 }
428
429 /* command dispatcher */
430 static int
process_command(struct cpu_user_regs * regs,struct gdb_context * ctx)431 process_command(struct cpu_user_regs *regs, struct gdb_context *ctx)
432 {
433 const char *ptr;
434 unsigned long addr, length, val;
435 int resume = 0;
436 unsigned long type = GDB_CONTINUE;
437
438 /* XXX check ctx->in_bytes >= 2 or similar. */
439
440 gdb_start_packet(ctx);
441 switch ( ctx->in_buf[0] )
442 {
443 case '?': /* query signal number */
444 gdb_cmd_signum(ctx);
445 break;
446 case 'H': /* thread operations */
447 gdb_send_reply("OK", ctx);
448 break;
449 case 'g': /* Read registers */
450 gdb_arch_read_reg_array(regs, ctx);
451 break;
452 case 'G': /* Write registers */
453 gdb_arch_write_reg_array(regs, ctx->in_buf + 1, ctx);
454 break;
455 case 'm': /* Read memory */
456 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
457 if ( (ptr == (ctx->in_buf + 1)) || (ptr[0] != ',') )
458 {
459 gdb_send_reply("E03", ctx);
460 return 0;
461 }
462 length = simple_strtoul(ptr + 1, &ptr, 16);
463 if ( ptr[0] != 0 )
464 {
465 gdb_send_reply("E04", ctx);
466 return 0;
467 }
468 gdb_cmd_read_mem(addr, length, ctx);
469 break;
470 case 'M': /* Write memory */
471 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
472 if ( (ptr == (ctx->in_buf + 1)) || (ptr[0] != ',') )
473 {
474 gdb_send_reply("E03", ctx);
475 return 0;
476 }
477 length = simple_strtoul(ptr + 1, &ptr, 16);
478 if ( ptr[0] != ':')
479 {
480 gdb_send_reply("E04", ctx);
481 return 0;
482 }
483 gdb_cmd_write_mem(addr, length, ptr + 1, ctx);
484 break;
485 case 'p': /* read register */
486 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
487 if ( ptr == (ctx->in_buf + 1) )
488 {
489 gdb_send_reply("E03", ctx);
490 return 0;
491 }
492 if ( ptr[0] != 0 )
493 {
494 gdb_send_reply("E04", ctx);
495 return 0;
496 }
497 gdb_arch_read_reg(addr, regs, ctx);
498 break;
499 case 'P': /* write register */
500 addr = simple_strtoul(ctx->in_buf + 1, &ptr, 16);
501 if ( ptr == (ctx->in_buf + 1) )
502 {
503 gdb_send_reply("E03", ctx);
504 return 0;
505 }
506 if ( ptr[0] != '=' )
507 {
508 gdb_send_reply("E04", ctx);
509 return 0;
510 }
511 ptr++;
512 val = str_to_native_ulong(ptr);
513 gdb_arch_write_reg(addr, val, regs, ctx);
514 break;
515 case 'D':
516 case 'k':
517 gdbstub_detach(ctx);
518 gdb_send_reply("OK", ctx);
519 ctx->connected = 0;
520 resume = 1;
521 break;
522 case 's': /* Single step */
523 type = GDB_STEP;
524 case 'c': /* Resume at current address */
525 addr = ~((unsigned long)0);
526
527 if ( ctx->in_buf[1] )
528 addr = str2ulong(&ctx->in_buf[1], sizeof(unsigned long));
529 gdbstub_attach(ctx);
530 resume = 1;
531 gdb_arch_resume(regs, addr, type, ctx);
532 break;
533 default:
534 gdb_send_reply("", ctx);
535 break;
536 }
537 return resume;
538 }
539
540 static struct gdb_context
541 __gdb_ctx = {
542 .serhnd = -1,
543 .running = ATOMIC_INIT(1),
544 .signum = 1
545 };
546 static struct gdb_context *gdb_ctx = &__gdb_ctx;
547
548 static void
gdbstub_console_puts(const char * str)549 gdbstub_console_puts(const char *str)
550 {
551 const char *p;
552
553 gdb_start_packet(gdb_ctx);
554 gdb_write_to_packet_char('O', gdb_ctx);
555
556 for ( p = str; *p != '\0'; p++ )
557 {
558 gdb_write_to_packet_char(hex2char((*p>>4) & 0x0f), gdb_ctx );
559 gdb_write_to_packet_char(hex2char((*p) & 0x0f), gdb_ctx );
560 }
561
562 gdb_send_packet(gdb_ctx);
563 }
564
565 /* trap handler: main entry point */
566 int
__trap_to_gdb(struct cpu_user_regs * regs,unsigned long cookie)567 __trap_to_gdb(struct cpu_user_regs *regs, unsigned long cookie)
568 {
569 int rc = 0;
570 unsigned long flags;
571
572 if ( gdb_ctx->serhnd < 0 )
573 {
574 printk("Debugging connection not set up.\n");
575 return -EBUSY;
576 }
577
578 /* We rely on our caller to ensure we're only on one processor
579 * at a time... We should probably panic here, but given that
580 * we're a debugger we should probably be a little tolerant of
581 * things going wrong. */
582 /* We don't want to use a spin lock here, because we're doing
583 two distinct things:
584
585 1 -- we don't want to run on more than one processor at a time,
586 and
587 2 -- we want to do something sensible if we re-enter ourselves.
588
589 Spin locks are good for 1, but useless for 2. */
590 if ( !atomic_dec_and_test(&gdb_ctx->running) )
591 {
592 printk("WARNING WARNING WARNING: Avoiding recursive gdb.\n");
593 atomic_inc(&gdb_ctx->running);
594 return -EBUSY;
595 }
596
597 if ( !gdb_ctx->connected )
598 {
599 printk("GDB connection activated.\n");
600 gdb_arch_print_state(regs);
601 gdb_ctx->connected = 1;
602 }
603
604 gdb_smp_pause();
605
606 local_irq_save(flags);
607
608 watchdog_disable();
609 console_start_sync();
610
611 gdb_arch_enter(regs);
612 gdb_ctx->signum = gdb_arch_signal_num(regs, cookie);
613
614 /* If gdb is already attached, tell it we've stopped again. */
615 if ( gdb_ctx->currently_attached )
616 {
617 gdb_start_packet(gdb_ctx);
618 gdb_cmd_signum(gdb_ctx);
619 }
620
621 do {
622 if ( receive_command(gdb_ctx) < 0 )
623 {
624 dbg_printk("Error in GDB session...\n");
625 rc = -EIO;
626 break;
627 }
628 } while ( process_command(regs, gdb_ctx) == 0 );
629
630 gdb_smp_resume();
631
632 gdb_arch_exit(regs);
633 console_end_sync();
634 watchdog_enable();
635 atomic_inc(&gdb_ctx->running);
636
637 local_irq_restore(flags);
638
639 return rc;
640 }
641
initialise_gdb(void)642 static int __init initialise_gdb(void)
643 {
644 if ( *opt_gdb == '\0' )
645 return 0;
646
647 gdb_ctx->serhnd = serial_parse_handle(opt_gdb);
648 if ( gdb_ctx->serhnd == -1 )
649 {
650 printk("Bad gdb= option '%s'\n", opt_gdb);
651 return 0;
652 }
653
654 serial_start_sync(gdb_ctx->serhnd);
655
656 printk("GDB stub initialised.\n");
657
658 return 0;
659 }
660 presmp_initcall(initialise_gdb);
661
gdb_pause_this_cpu(void * unused)662 static void gdb_pause_this_cpu(void *unused)
663 {
664 unsigned long flags;
665
666 local_irq_save(flags);
667
668 atomic_set(&gdb_cpu[smp_processor_id()].ack, 1);
669 atomic_inc(&gdb_smp_paused_count);
670
671 while ( atomic_read(&gdb_cpu[smp_processor_id()].paused) )
672 mdelay(1);
673
674 atomic_dec(&gdb_smp_paused_count);
675 atomic_set(&gdb_cpu[smp_processor_id()].ack, 0);
676
677 /* Restore interrupts */
678 local_irq_restore(flags);
679 }
680
gdb_smp_pause(void)681 static void gdb_smp_pause(void)
682 {
683 int timeout = 100;
684 int cpu;
685
686 for_each_online_cpu(cpu)
687 {
688 atomic_set(&gdb_cpu[cpu].ack, 0);
689 atomic_set(&gdb_cpu[cpu].paused, 1);
690 }
691
692 atomic_set(&gdb_smp_paused_count, 0);
693
694 smp_call_function(gdb_pause_this_cpu, NULL, /* dont wait! */0);
695
696 /* Wait 100ms for all other CPUs to enter pause loop */
697 while ( (atomic_read(&gdb_smp_paused_count) < (num_online_cpus() - 1))
698 && (timeout-- > 0) )
699 mdelay(1);
700
701 if ( atomic_read(&gdb_smp_paused_count) < (num_online_cpus() - 1) )
702 {
703 printk("GDB: Not all CPUs have paused, missing CPUs ");
704 for_each_online_cpu(cpu)
705 {
706 if ( (cpu != smp_processor_id()) &&
707 !atomic_read(&gdb_cpu[cpu].ack) )
708 printk("%d ", cpu);
709 }
710 printk("\n");
711 }
712 }
713
gdb_smp_resume(void)714 static void gdb_smp_resume(void)
715 {
716 int cpu;
717 int timeout = 100;
718
719 for_each_online_cpu(cpu)
720 atomic_set(&gdb_cpu[cpu].paused, 0);
721
722 /* Make sure all CPUs resume */
723 while ( (atomic_read(&gdb_smp_paused_count) > 0)
724 && (timeout-- > 0) )
725 mdelay(1);
726
727 if ( atomic_read(&gdb_smp_paused_count) > 0 )
728 {
729 printk("GDB: Not all CPUs have resumed execution, missing CPUs ");
730 for_each_online_cpu(cpu)
731 {
732 if ( (cpu != smp_processor_id()) &&
733 atomic_read(&gdb_cpu[cpu].ack) )
734 printk("%d ", cpu);
735 }
736 printk("\n");
737 }
738 }
739
740 /*
741 * Local variables:
742 * mode: C
743 * c-file-style: "BSD"
744 * c-basic-offset: 4
745 * tab-width: 4
746 * End:
747 */
748