1/* 2 * arch/mips/kernel/gdb-stub.c 3 * 4 * Originally written by Glenn Engel, Lake Stevens Instrument Division 5 * 6 * Contributed by HP Systems 7 * 8 * Modified for SPARC by Stu Grossman, Cygnus Support. 9 * 10 * Modified for Linux/MIPS (and MIPS in general) by Andreas Busse 11 * Send complaints, suggestions etc. to <andy@waldorf-gmbh.de> 12 * 13 * Copyright (C) 1995 Andreas Busse 14 * 15 * Copyright (C) 2003 MontaVista Software Inc. 16 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net 17 */ 18 19/* 20 * To enable debugger support, two things need to happen. One, a 21 * call to set_debug_traps() is necessary in order to allow any breakpoints 22 * or error conditions to be properly intercepted and reported to gdb. 23 * Two, a breakpoint needs to be generated to begin communication. This 24 * is most easily accomplished by a call to breakpoint(). Breakpoint() 25 * simulates a breakpoint by executing a BREAK instruction. 26 * 27 * 28 * The following gdb commands are supported: 29 * 30 * command function Return value 31 * 32 * g return the value of the CPU registers hex data or ENN 33 * G set the value of the CPU registers OK or ENN 34 * 35 * mAA..AA,LLLL Read LLLL bytes at address AA..AA hex data or ENN 36 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA OK or ENN 37 * 38 * c Resume at current address SNN ( signal NN) 39 * cAA..AA Continue at address AA..AA SNN 40 * 41 * s Step one instruction SNN 42 * sAA..AA Step one instruction from AA..AA SNN 43 * 44 * k kill 45 * 46 * ? What was the last sigval ? SNN (signal NN) 47 * 48 * bBB..BB Set baud rate to BB..BB OK or BNN, then sets 49 * baud rate 50 * 51 * All commands and responses are sent with a packet which includes a 52 * checksum. A packet consists of 53 * 54 * $<packet info>#<checksum>. 55 * 56 * where 57 * <packet info> :: <characters representing the command or response> 58 * <checksum> :: < two hex digits computed as modulo 256 sum of <packetinfo>> 59 * 60 * When a packet is received, it is first acknowledged with either '+' or '-'. 61 * '+' indicates a successful transfer. '-' indicates a failed transfer. 62 * 63 * Example: 64 * 65 * Host: Reply: 66 * $m0,10#2a +$00010203040506070809101112131415#42 67 * 68 * 69 * ============== 70 * MORE EXAMPLES: 71 * ============== 72 * 73 * For reference -- the following are the steps that one 74 * company took (RidgeRun Inc) to get remote gdb debugging 75 * going. In this scenario the host machine was a PC and the 76 * target platform was a Galileo EVB64120A MIPS evaluation 77 * board. 78 * 79 * Step 1: 80 * First download gdb-5.0.tar.gz from the internet. 81 * and then build/install the package. 82 * 83 * Example: 84 * $ tar zxf gdb-5.0.tar.gz 85 * $ cd gdb-5.0 86 * $ ./configure --target=mips-linux-elf 87 * $ make 88 * $ install 89 * $ which mips-linux-elf-gdb 90 * /usr/local/bin/mips-linux-elf-gdb 91 * 92 * Step 2: 93 * Configure linux for remote debugging and build it. 94 * 95 * Example: 96 * $ cd ~/linux 97 * $ make menuconfig <go to "Kernel Hacking" and turn on remote debugging> 98 * $ make 99 * 100 * Step 3: 101 * Download the kernel to the remote target and start 102 * the kernel running. It will promptly halt and wait 103 * for the host gdb session to connect. It does this 104 * since the "Kernel Hacking" option has defined 105 * CONFIG_KGDB which in turn enables your calls 106 * to: 107 * set_debug_traps(); 108 * breakpoint(); 109 * 110 * Step 4: 111 * Start the gdb session on the host. 112 * 113 * Example: 114 * $ mips-linux-elf-gdb vmlinux 115 * (gdb) set remotebaud 115200 116 * (gdb) target remote /dev/ttyS1 117 * ...at this point you are connected to 118 * the remote target and can use gdb 119 * in the normal fasion. Setting 120 * breakpoints, single stepping, 121 * printing variables, etc. 122 */ 123#include <linux/string.h> 124#include <linux/kernel.h> 125#include <linux/signal.h> 126#include <linux/sched.h> 127#include <linux/mm.h> 128#include <linux/console.h> 129#include <linux/init.h> 130#include <linux/smp.h> 131#include <linux/spinlock.h> 132#include <linux/slab.h> 133#include <linux/reboot.h> 134 135#include <asm/asm.h> 136#include <asm/cacheflush.h> 137#include <asm/mipsregs.h> 138#include <asm/pgtable.h> 139#include <asm/system.h> 140#include <asm/gdb-stub.h> 141#include <asm/inst.h> 142#include <asm/smp.h> 143 144/* 145 * external low-level support routines 146 */ 147 148extern int putDebugChar(char c); /* write a single character */ 149extern char getDebugChar(void); /* read and return a single char */ 150extern void trap_low(void); 151 152/* 153 * breakpoint and test functions 154 */ 155extern void breakpoint(void); 156extern void breakinst(void); 157extern void async_breakpoint(void); 158extern void async_breakinst(void); 159extern void adel(void); 160 161/* 162 * local prototypes 163 */ 164 165static void getpacket(char *buffer); 166static void putpacket(char *buffer); 167static int computeSignal(int tt); 168static int hex(unsigned char ch); 169static int hexToInt(char **ptr, int *intValue); 170static int hexToLong(char **ptr, long *longValue); 171static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault); 172void handle_exception(struct gdb_regs *regs); 173 174int kgdb_enabled; 175 176/* 177 * spin locks for smp case 178 */ 179static DEFINE_SPINLOCK(kgdb_lock); 180static raw_spinlock_t kgdb_cpulock[NR_CPUS] = { 181 [0 ... NR_CPUS-1] = __RAW_SPIN_LOCK_UNLOCKED, 182}; 183 184/* 185 * BUFMAX defines the maximum number of characters in inbound/outbound buffers 186 * at least NUMREGBYTES*2 are needed for register packets 187 */ 188#define BUFMAX 2048 189 190static char input_buffer[BUFMAX]; 191static char output_buffer[BUFMAX]; 192static int initialized; /* !0 means we've been initialized */ 193static int kgdb_started; 194static const char hexchars[]="0123456789abcdef"; 195 196/* Used to prevent crashes in memory access. Note that they'll crash anyway if 197 we haven't set up fault handlers yet... */ 198int kgdb_read_byte(unsigned char *address, unsigned char *dest); 199int kgdb_write_byte(unsigned char val, unsigned char *dest); 200 201/* 202 * Convert ch from a hex digit to an int 203 */ 204static int hex(unsigned char ch) 205{ 206 if (ch >= 'a' && ch <= 'f') 207 return ch-'a'+10; 208 if (ch >= '0' && ch <= '9') 209 return ch-'0'; 210 if (ch >= 'A' && ch <= 'F') 211 return ch-'A'+10; 212 return -1; 213} 214 215/* 216 * scan for the sequence $<data>#<checksum> 217 */ 218static void getpacket(char *buffer) 219{ 220 unsigned char checksum; 221 unsigned char xmitcsum; 222 int i; 223 int count; 224 unsigned char ch; 225 226 do { 227 /* 228 * wait around for the start character, 229 * ignore all other characters 230 */ 231 while ((ch = (getDebugChar() & 0x7f)) != '$') ; 232 233 checksum = 0; 234 xmitcsum = -1; 235 count = 0; 236 237 /* 238 * now, read until a # or end of buffer is found 239 */ 240 while (count < BUFMAX) { 241 ch = getDebugChar(); 242 if (ch == '#') 243 break; 244 checksum = checksum + ch; 245 buffer[count] = ch; 246 count = count + 1; 247 } 248 249 if (count >= BUFMAX) 250 continue; 251 252 buffer[count] = 0; 253 254 if (ch == '#') { 255 xmitcsum = hex(getDebugChar() & 0x7f) << 4; 256 xmitcsum |= hex(getDebugChar() & 0x7f); 257 258 if (checksum != xmitcsum) 259 putDebugChar('-'); /* failed checksum */ 260 else { 261 putDebugChar('+'); /* successful transfer */ 262 263 /* 264 * if a sequence char is present, 265 * reply the sequence ID 266 */ 267 if (buffer[2] == ':') { 268 putDebugChar(buffer[0]); 269 putDebugChar(buffer[1]); 270 271 /* 272 * remove sequence chars from buffer 273 */ 274 count = strlen(buffer); 275 for (i=3; i <= count; i++) 276 buffer[i-3] = buffer[i]; 277 } 278 } 279 } 280 } 281 while (checksum != xmitcsum); 282} 283 284/* 285 * send the packet in buffer. 286 */ 287static void putpacket(char *buffer) 288{ 289 unsigned char checksum; 290 int count; 291 unsigned char ch; 292 293 /* 294 * $<packet info>#<checksum>. 295 */ 296 297 do { 298 putDebugChar('$'); 299 checksum = 0; 300 count = 0; 301 302 while ((ch = buffer[count]) != 0) { 303 if (!(putDebugChar(ch))) 304 return; 305 checksum += ch; 306 count += 1; 307 } 308 309 putDebugChar('#'); 310 putDebugChar(hexchars[checksum >> 4]); 311 putDebugChar(hexchars[checksum & 0xf]); 312 313 } 314 while ((getDebugChar() & 0x7f) != '+'); 315} 316 317 318/* 319 * Convert the memory pointed to by mem into hex, placing result in buf. 320 * Return a pointer to the last char put in buf (null), in case of mem fault, 321 * return 0. 322 * may_fault is non-zero if we are reading from arbitrary memory, but is currently 323 * not used. 324 */ 325static unsigned char *mem2hex(char *mem, char *buf, int count, int may_fault) 326{ 327 unsigned char ch; 328 329 while (count-- > 0) { 330 if (kgdb_read_byte(mem++, &ch) != 0) 331 return 0; 332 *buf++ = hexchars[ch >> 4]; 333 *buf++ = hexchars[ch & 0xf]; 334 } 335 336 *buf = 0; 337 338 return buf; 339} 340 341/* 342 * convert the hex array pointed to by buf into binary to be placed in mem 343 * return a pointer to the character AFTER the last byte written 344 * may_fault is non-zero if we are reading from arbitrary memory, but is currently 345 * not used. 346 */ 347static char *hex2mem(char *buf, char *mem, int count, int binary, int may_fault) 348{ 349 int i; 350 unsigned char ch; 351 352 for (i=0; i<count; i++) 353 { 354 if (binary) { 355 ch = *buf++; 356 if (ch == 0x7d) 357 ch = 0x20 ^ *buf++; 358 } 359 else { 360 ch = hex(*buf++) << 4; 361 ch |= hex(*buf++); 362 } 363 if (kgdb_write_byte(ch, mem++) != 0) 364 return 0; 365 } 366 367 return mem; 368} 369 370/* 371 * This table contains the mapping between SPARC hardware trap types, and 372 * signals, which are primarily what GDB understands. It also indicates 373 * which hardware traps we need to commandeer when initializing the stub. 374 */ 375static struct hard_trap_info { 376 unsigned char tt; /* Trap type code for MIPS R3xxx and R4xxx */ 377 unsigned char signo; /* Signal that we map this trap into */ 378} hard_trap_info[] = { 379 { 6, SIGBUS }, /* instruction bus error */ 380/* { 7, SIGBUS }, */ /* data bus error */ 381 { 9, SIGTRAP }, /* break */ 382 { 10, SIGILL }, /* reserved instruction */ 383/* { 11, SIGILL }, */ /* CPU unusable */ 384 { 12, SIGFPE }, /* overflow */ 385 { 13, SIGTRAP }, /* trap */ 386 { 14, SIGSEGV }, /* virtual instruction cache coherency */ 387 { 15, SIGFPE }, /* floating point exception */ 388 { 23, SIGSEGV }, /* watch */ 389 { 31, SIGSEGV }, /* virtual data cache coherency */ 390 { 0, 0} /* Must be last */ 391}; 392 393/* Save the normal trap handlers for user-mode traps. */ 394void *saved_vectors[32]; 395 396/* 397 * Set up exception handlers for tracing and breakpoints 398 */ 399void set_debug_traps(void) 400{ 401 struct hard_trap_info *ht; 402 unsigned long flags; 403 unsigned char c; 404 405 local_irq_save(flags); 406 for (ht = hard_trap_info; ht->tt && ht->signo; ht++) 407 saved_vectors[ht->tt] = set_except_vector(ht->tt, trap_low); 408 409 putDebugChar('+'); /* 'hello world' */ 410 /* 411 * In case GDB is started before us, ack any packets 412 * (presumably "$?#xx") sitting there. 413 */ 414 while((c = getDebugChar()) != '$'); 415 while((c = getDebugChar()) != '#'); 416 c = getDebugChar(); /* eat first csum byte */ 417 c = getDebugChar(); /* eat second csum byte */ 418 putDebugChar('+'); /* ack it */ 419 420 initialized = 1; 421 local_irq_restore(flags); 422} 423 424void restore_debug_traps(void) 425{ 426 struct hard_trap_info *ht; 427 unsigned long flags; 428 429 local_irq_save(flags); 430 for (ht = hard_trap_info; ht->tt && ht->signo; ht++) 431 set_except_vector(ht->tt, saved_vectors[ht->tt]); 432 local_irq_restore(flags); 433} 434 435/* 436 * Convert the MIPS hardware trap type code to a Unix signal number. 437 */ 438static int computeSignal(int tt) 439{ 440 struct hard_trap_info *ht; 441 442 for (ht = hard_trap_info; ht->tt && ht->signo; ht++) 443 if (ht->tt == tt) 444 return ht->signo; 445 446 return SIGHUP; /* default for things we don't know about */ 447} 448 449/* 450 * While we find nice hex chars, build an int. 451 * Return number of chars processed. 452 */ 453static int hexToInt(char **ptr, int *intValue) 454{ 455 int numChars = 0; 456 int hexValue; 457 458 *intValue = 0; 459 460 while (**ptr) { 461 hexValue = hex(**ptr); 462 if (hexValue < 0) 463 break; 464 465 *intValue = (*intValue << 4) | hexValue; 466 numChars ++; 467 468 (*ptr)++; 469 } 470 471 return (numChars); 472} 473 474static int hexToLong(char **ptr, long *longValue) 475{ 476 int numChars = 0; 477 int hexValue; 478 479 *longValue = 0; 480 481 while (**ptr) { 482 hexValue = hex(**ptr); 483 if (hexValue < 0) 484 break; 485 486 *longValue = (*longValue << 4) | hexValue; 487 numChars ++; 488 489 (*ptr)++; 490 } 491 492 return numChars; 493} 494 495 496 497/* 498 * We single-step by setting breakpoints. When an exception 499 * is handled, we need to restore the instructions hoisted 500 * when the breakpoints were set. 501 * 502 * This is where we save the original instructions. 503 */ 504static struct gdb_bp_save { 505 unsigned long addr; 506 unsigned int val; 507} step_bp[2]; 508 509#define BP 0x0000000d /* break opcode */ 510 511/* 512 * Set breakpoint instructions for single stepping. 513 */ 514static void single_step(struct gdb_regs *regs) 515{ 516 union mips_instruction insn; 517 unsigned long targ; 518 int is_branch, is_cond, i; 519 520 targ = regs->cp0_epc; 521 insn.word = *(unsigned int *)targ; 522 is_branch = is_cond = 0; 523 524 switch (insn.i_format.opcode) { 525 /* 526 * jr and jalr are in r_format format. 527 */ 528 case spec_op: 529 switch (insn.r_format.func) { 530 case jalr_op: 531 case jr_op: 532 targ = *(®s->reg0 + insn.r_format.rs); 533 is_branch = 1; 534 break; 535 } 536 break; 537 538 /* 539 * This group contains: 540 * bltz_op, bgez_op, bltzl_op, bgezl_op, 541 * bltzal_op, bgezal_op, bltzall_op, bgezall_op. 542 */ 543 case bcond_op: 544 is_branch = is_cond = 1; 545 targ += 4 + (insn.i_format.simmediate << 2); 546 break; 547 548 /* 549 * These are unconditional and in j_format. 550 */ 551 case jal_op: 552 case j_op: 553 is_branch = 1; 554 targ += 4; 555 targ >>= 28; 556 targ <<= 28; 557 targ |= (insn.j_format.target << 2); 558 break; 559 560 /* 561 * These are conditional. 562 */ 563 case beq_op: 564 case beql_op: 565 case bne_op: 566 case bnel_op: 567 case blez_op: 568 case blezl_op: 569 case bgtz_op: 570 case bgtzl_op: 571 case cop0_op: 572 case cop1_op: 573 case cop2_op: 574 case cop1x_op: 575 is_branch = is_cond = 1; 576 targ += 4 + (insn.i_format.simmediate << 2); 577 break; 578 } 579 580 if (is_branch) { 581 i = 0; 582 if (is_cond && targ != (regs->cp0_epc + 8)) { 583 step_bp[i].addr = regs->cp0_epc + 8; 584 step_bp[i++].val = *(unsigned *)(regs->cp0_epc + 8); 585 *(unsigned *)(regs->cp0_epc + 8) = BP; 586 } 587 step_bp[i].addr = targ; 588 step_bp[i].val = *(unsigned *)targ; 589 *(unsigned *)targ = BP; 590 } else { 591 step_bp[0].addr = regs->cp0_epc + 4; 592 step_bp[0].val = *(unsigned *)(regs->cp0_epc + 4); 593 *(unsigned *)(regs->cp0_epc + 4) = BP; 594 } 595} 596 597/* 598 * If asynchronously interrupted by gdb, then we need to set a breakpoint 599 * at the interrupted instruction so that we wind up stopped with a 600 * reasonable stack frame. 601 */ 602static struct gdb_bp_save async_bp; 603 604/* 605 * Swap the interrupted EPC with our asynchronous breakpoint routine. 606 * This is safer than stuffing the breakpoint in-place, since no cache 607 * flushes (or resulting smp_call_functions) are required. The 608 * assumption is that only one CPU will be handling asynchronous bp's, 609 * and only one can be active at a time. 610 */ 611extern spinlock_t smp_call_lock; 612 613void set_async_breakpoint(unsigned long *epc) 614{ 615 /* skip breaking into userland */ 616 if ((*epc & 0x80000000) == 0) 617 return; 618 619#ifdef CONFIG_SMP 620 /* avoid deadlock if someone is make IPC */ 621 if (spin_is_locked(&smp_call_lock)) 622 return; 623#endif 624 625 async_bp.addr = *epc; 626 *epc = (unsigned long)async_breakpoint; 627} 628 629static void kgdb_wait(void *arg) 630{ 631 unsigned flags; 632 int cpu = smp_processor_id(); 633 634 local_irq_save(flags); 635 636 __raw_spin_lock(&kgdb_cpulock[cpu]); 637 __raw_spin_unlock(&kgdb_cpulock[cpu]); 638 639 local_irq_restore(flags); 640} 641 642/* 643 * GDB stub needs to call kgdb_wait on all processor with interrupts 644 * disabled, so it uses it's own special variant. 645 */ 646static int kgdb_smp_call_kgdb_wait(void) 647{ 648#ifdef CONFIG_SMP 649 struct call_data_struct data; 650 int i, cpus = num_online_cpus() - 1; 651 int cpu = smp_processor_id(); 652 653 /* 654 * Can die spectacularly if this CPU isn't yet marked online 655 */ 656 BUG_ON(!cpu_online(cpu)); 657 658 if (!cpus) 659 return 0; 660 661 if (spin_is_locked(&smp_call_lock)) { 662 /* 663 * Some other processor is trying to make us do something 664 * but we're not going to respond... give up 665 */ 666 return -1; 667 } 668 669 /* 670 * We will continue here, accepting the fact that 671 * the kernel may deadlock if another CPU attempts 672 * to call smp_call_function now... 673 */ 674 675 data.func = kgdb_wait; 676 data.info = NULL; 677 atomic_set(&data.started, 0); 678 data.wait = 0; 679 680 spin_lock(&smp_call_lock); 681 call_data = &data; 682 mb(); 683 684 /* Send a message to all other CPUs and wait for them to respond */ 685 for (i = 0; i < NR_CPUS; i++) 686 if (cpu_online(i) && i != cpu) 687 core_send_ipi(i, SMP_CALL_FUNCTION); 688 689 /* Wait for response */ 690 while (atomic_read(&data.started) != cpus) 691 barrier(); 692 693 call_data = NULL; 694 spin_unlock(&smp_call_lock); 695#endif 696 697 return 0; 698} 699 700/* 701 * This function does all command processing for interfacing to gdb. It 702 * returns 1 if you should skip the instruction at the trap address, 0 703 * otherwise. 704 */ 705void handle_exception (struct gdb_regs *regs) 706{ 707 int trap; /* Trap type */ 708 int sigval; 709 long addr; 710 int length; 711 char *ptr; 712 unsigned long *stack; 713 int i; 714 int bflag = 0; 715 716 kgdb_started = 1; 717 718 /* 719 * acquire the big kgdb spinlock 720 */ 721 if (!spin_trylock(&kgdb_lock)) { 722 /* 723 * some other CPU has the lock, we should go back to 724 * receive the gdb_wait IPC 725 */ 726 return; 727 } 728 729 /* 730 * If we're in async_breakpoint(), restore the real EPC from 731 * the breakpoint. 732 */ 733 if (regs->cp0_epc == (unsigned long)async_breakinst) { 734 regs->cp0_epc = async_bp.addr; 735 async_bp.addr = 0; 736 } 737 738 /* 739 * acquire the CPU spinlocks 740 */ 741 for (i = num_online_cpus()-1; i >= 0; i--) 742 if (__raw_spin_trylock(&kgdb_cpulock[i]) == 0) 743 panic("kgdb: couldn't get cpulock %d\n", i); 744 745 /* 746 * force other cpus to enter kgdb 747 */ 748 kgdb_smp_call_kgdb_wait(); 749 750 /* 751 * If we're in breakpoint() increment the PC 752 */ 753 trap = (regs->cp0_cause & 0x7c) >> 2; 754 if (trap == 9 && regs->cp0_epc == (unsigned long)breakinst) 755 regs->cp0_epc += 4; 756 757 /* 758 * If we were single_stepping, restore the opcodes hoisted 759 * for the breakpoint[s]. 760 */ 761 if (step_bp[0].addr) { 762 *(unsigned *)step_bp[0].addr = step_bp[0].val; 763 step_bp[0].addr = 0; 764 765 if (step_bp[1].addr) { 766 *(unsigned *)step_bp[1].addr = step_bp[1].val; 767 step_bp[1].addr = 0; 768 } 769 } 770 771 stack = (long *)regs->reg29; /* stack ptr */ 772 sigval = computeSignal(trap); 773 774 /* 775 * reply to host that an exception has occurred 776 */ 777 ptr = output_buffer; 778 779 /* 780 * Send trap type (converted to signal) 781 */ 782 *ptr++ = 'T'; 783 *ptr++ = hexchars[sigval >> 4]; 784 *ptr++ = hexchars[sigval & 0xf]; 785 786 /* 787 * Send Error PC 788 */ 789 *ptr++ = hexchars[REG_EPC >> 4]; 790 *ptr++ = hexchars[REG_EPC & 0xf]; 791 *ptr++ = ':'; 792 ptr = mem2hex((char *)®s->cp0_epc, ptr, sizeof(long), 0); 793 *ptr++ = ';'; 794 795 /* 796 * Send frame pointer 797 */ 798 *ptr++ = hexchars[REG_FP >> 4]; 799 *ptr++ = hexchars[REG_FP & 0xf]; 800 *ptr++ = ':'; 801 ptr = mem2hex((char *)®s->reg30, ptr, sizeof(long), 0); 802 *ptr++ = ';'; 803 804 /* 805 * Send stack pointer 806 */ 807 *ptr++ = hexchars[REG_SP >> 4]; 808 *ptr++ = hexchars[REG_SP & 0xf]; 809 *ptr++ = ':'; 810 ptr = mem2hex((char *)®s->reg29, ptr, sizeof(long), 0); 811 *ptr++ = ';'; 812 813 *ptr++ = 0; 814 putpacket(output_buffer); /* send it off... */ 815 816 /* 817 * Wait for input from remote GDB 818 */ 819 while (1) { 820 output_buffer[0] = 0; 821 getpacket(input_buffer); 822 823 switch (input_buffer[0]) 824 { 825 case '?': 826 output_buffer[0] = 'S'; 827 output_buffer[1] = hexchars[sigval >> 4]; 828 output_buffer[2] = hexchars[sigval & 0xf]; 829 output_buffer[3] = 0; 830 break; 831 832 /* 833 * Detach debugger; let CPU run 834 */ 835 case 'D': 836 putpacket(output_buffer); 837 goto finish_kgdb; 838 break; 839 840 case 'd': 841 /* toggle debug flag */ 842 break; 843 844 /* 845 * Return the value of the CPU registers 846 */ 847 case 'g': 848 ptr = output_buffer; 849 ptr = mem2hex((char *)®s->reg0, ptr, 32*sizeof(long), 0); /* r0...r31 */ 850 ptr = mem2hex((char *)®s->cp0_status, ptr, 6*sizeof(long), 0); /* cp0 */ 851 ptr = mem2hex((char *)®s->fpr0, ptr, 32*sizeof(long), 0); /* f0...31 */ 852 ptr = mem2hex((char *)®s->cp1_fsr, ptr, 2*sizeof(long), 0); /* cp1 */ 853 ptr = mem2hex((char *)®s->frame_ptr, ptr, 2*sizeof(long), 0); /* frp */ 854 ptr = mem2hex((char *)®s->cp0_index, ptr, 16*sizeof(long), 0); /* cp0 */ 855 break; 856 857 /* 858 * set the value of the CPU registers - return OK 859 */ 860 case 'G': 861 { 862 ptr = &input_buffer[1]; 863 hex2mem(ptr, (char *)®s->reg0, 32*sizeof(long), 0, 0); 864 ptr += 32*(2*sizeof(long)); 865 hex2mem(ptr, (char *)®s->cp0_status, 6*sizeof(long), 0, 0); 866 ptr += 6*(2*sizeof(long)); 867 hex2mem(ptr, (char *)®s->fpr0, 32*sizeof(long), 0, 0); 868 ptr += 32*(2*sizeof(long)); 869 hex2mem(ptr, (char *)®s->cp1_fsr, 2*sizeof(long), 0, 0); 870 ptr += 2*(2*sizeof(long)); 871 hex2mem(ptr, (char *)®s->frame_ptr, 2*sizeof(long), 0, 0); 872 ptr += 2*(2*sizeof(long)); 873 hex2mem(ptr, (char *)®s->cp0_index, 16*sizeof(long), 0, 0); 874 strcpy(output_buffer,"OK"); 875 } 876 break; 877 878 /* 879 * mAA..AA,LLLL Read LLLL bytes at address AA..AA 880 */ 881 case 'm': 882 ptr = &input_buffer[1]; 883 884 if (hexToLong(&ptr, &addr) 885 && *ptr++ == ',' 886 && hexToInt(&ptr, &length)) { 887 if (mem2hex((char *)addr, output_buffer, length, 1)) 888 break; 889 strcpy (output_buffer, "E03"); 890 } else 891 strcpy(output_buffer,"E01"); 892 break; 893 894 /* 895 * XAA..AA,LLLL: Write LLLL escaped binary bytes at address AA.AA 896 */ 897 case 'X': 898 bflag = 1; 899 /* fall through */ 900 901 /* 902 * MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK 903 */ 904 case 'M': 905 ptr = &input_buffer[1]; 906 907 if (hexToLong(&ptr, &addr) 908 && *ptr++ == ',' 909 && hexToInt(&ptr, &length) 910 && *ptr++ == ':') { 911 if (hex2mem(ptr, (char *)addr, length, bflag, 1)) 912 strcpy(output_buffer, "OK"); 913 else 914 strcpy(output_buffer, "E03"); 915 } 916 else 917 strcpy(output_buffer, "E02"); 918 break; 919 920 /* 921 * cAA..AA Continue at address AA..AA(optional) 922 */ 923 case 'c': 924 /* try to read optional parameter, pc unchanged if no parm */ 925 926 ptr = &input_buffer[1]; 927 if (hexToLong(&ptr, &addr)) 928 regs->cp0_epc = addr; 929 930 goto exit_kgdb_exception; 931 break; 932 933 /* 934 * kill the program; let us try to restart the machine 935 * Reset the whole machine. 936 */ 937 case 'k': 938 case 'r': 939 machine_restart("kgdb restarts machine"); 940 break; 941 942 /* 943 * Step to next instruction 944 */ 945 case 's': 946 /* 947 * There is no single step insn in the MIPS ISA, so we 948 * use breakpoints and continue, instead. 949 */ 950 single_step(regs); 951 goto exit_kgdb_exception; 952 /* NOTREACHED */ 953 break; 954 955 case 'b': 956 { 957 } 958 break; 959 960 } /* switch */ 961 962 /* 963 * reply to the request 964 */ 965 966 putpacket(output_buffer); 967 968 } /* while */ 969 970 return; 971 972finish_kgdb: 973 restore_debug_traps(); 974 975exit_kgdb_exception: 976 /* release locks so other CPUs can go */ 977 for (i = num_online_cpus()-1; i >= 0; i--) 978 __raw_spin_unlock(&kgdb_cpulock[i]); 979 spin_unlock(&kgdb_lock); 980 981 __flush_cache_all(); 982 return; 983} 984 985/* 986 * This function will generate a breakpoint exception. It is used at the 987 * beginning of a program to sync up with a debugger and can be used 988 * otherwise as a quick means to stop program execution and "break" into 989 * the debugger. 990 */ 991void breakpoint(void) 992{ 993 if (!initialized) 994 return; 995 996 __asm__ __volatile__( 997 ".globl breakinst\n\t" 998 ".set\tnoreorder\n\t" 999 "nop\n" 1000 "breakinst:\tbreak\n\t" 1001 "nop\n\t" 1002 ".set\treorder" 1003 ); 1004} 1005 1006/* Nothing but the break; don't pollute any registers */ 1007void async_breakpoint(void) 1008{ 1009 __asm__ __volatile__( 1010 ".globl async_breakinst\n\t" 1011 ".set\tnoreorder\n\t" 1012 "nop\n" 1013 "async_breakinst:\tbreak\n\t" 1014 "nop\n\t" 1015 ".set\treorder" 1016 ); 1017} 1018 1019void adel(void) 1020{ 1021 __asm__ __volatile__( 1022 ".globl\tadel\n\t" 1023 "lui\t$8,0x8000\n\t" 1024 "lw\t$9,1($8)\n\t" 1025 ); 1026} 1027 1028/* 1029 * malloc is needed by gdb client in "call func()", even a private one 1030 * will make gdb happy 1031 */ 1032static void * __attribute_used__ malloc(size_t size) 1033{ 1034 return kmalloc(size, GFP_ATOMIC); 1035} 1036 1037static void __attribute_used__ free (void *where) 1038{ 1039 kfree(where); 1040} 1041 1042#ifdef CONFIG_GDB_CONSOLE 1043 1044void gdb_putsn(const char *str, int l) 1045{ 1046 char outbuf[18]; 1047 1048 if (!kgdb_started) 1049 return; 1050 1051 outbuf[0]='O'; 1052 1053 while(l) { 1054 int i = (l>8)?8:l; 1055 mem2hex((char *)str, &outbuf[1], i, 0); 1056 outbuf[(i*2)+1]=0; 1057 putpacket(outbuf); 1058 str += i; 1059 l -= i; 1060 } 1061} 1062 1063static void gdb_console_write(struct console *con, const char *s, unsigned n) 1064{ 1065 gdb_putsn(s, n); 1066} 1067 1068static struct console gdb_console = { 1069 .name = "gdb", 1070 .write = gdb_console_write, 1071 .flags = CON_PRINTBUFFER, 1072 .index = -1 1073}; 1074 1075static int __init register_gdb_console(void) 1076{ 1077 register_console(&gdb_console); 1078 1079 return 0; 1080} 1081 1082console_initcall(register_gdb_console); 1083 1084#endif 1085