1 /* $NetBSD: kgdb_stub.c,v 1.14 2003/05/13 02:13:14 dbj Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratories. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94 45 */ 46 47 /* 48 * "Stub" to allow remote cpu to debug over a serial line using gdb. 49 */ 50 51 #include <sys/cdefs.h> 52 __KERNEL_RCSID(0, "$NetBSD: kgdb_stub.c,v 1.14 2003/05/13 02:13:14 dbj Exp $"); 53 54 #include "opt_kgdb.h" 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/kgdb.h> 59 60 #undef DEBUG_KGDB 61 62 #ifdef DEBUG_KGDB 63 #define DPRINTF(x) printf x 64 #else 65 #define DPRINTF(x) 66 #endif 67 68 /* XXX: Maybe these should be in the MD files? */ 69 #ifndef KGDB_DEV 70 #define KGDB_DEV NODEV 71 #endif 72 #ifndef KGDB_DEVRATE 73 #define KGDB_DEVRATE 19200 74 #endif 75 76 int kgdb_dev = KGDB_DEV; /* remote debugging device (NODEV if none) */ 77 int kgdb_rate = KGDB_DEVRATE; /* remote debugging baud rate */ 78 int kgdb_active = 0; /* remote debugging active if != 0 */ 79 int kgdb_debug_init = 0; /* != 0 waits for remote at system init */ 80 int kgdb_debug_panic = 0; /* != 0 waits for remote on panic */ 81 label_t *kgdb_recover = 0; 82 83 static void kgdb_copy __P((void *, void *, int)); 84 /* static void kgdb_zero __P((void *, int)); */ 85 static void kgdb_send __P((u_char *)); 86 static int kgdb_recv __P((u_char *, int)); 87 static int digit2i __P((u_char)); 88 static u_char i2digit __P((int)); 89 static void mem2hex __P((void *, void *, int)); 90 static u_char *hex2mem __P((void *, u_char *, int)); 91 static vaddr_t hex2i __P((u_char **)); 92 93 static int (*kgdb_getc) __P((void *)); 94 static void (*kgdb_putc) __P((void *, int)); 95 static void *kgdb_ioarg; 96 97 /* KGDB_BUFLEN must be at least (2*KGDB_NUMREGS*sizeof(kgdb_reg_t)+1) */ 98 static u_char buffer[KGDB_BUFLEN]; 99 static kgdb_reg_t gdb_regs[KGDB_NUMREGS]; 100 101 #define GETC() ((*kgdb_getc)(kgdb_ioarg)) 102 #define PUTC(c) ((*kgdb_putc)(kgdb_ioarg, c)) 103 104 /* 105 * db_trap_callback can be hooked by MD port code to handle special 106 * cases such as disabling hardware watchdogs while in kgdb. Name 107 * is shared with DDB. 108 */ 109 void (*db_trap_callback)(int); 110 111 /* 112 * This little routine exists simply so that bcopy() can be debugged. 113 */ 114 static void 115 kgdb_copy(vsrc, vdst, len) 116 void *vsrc, *vdst; 117 int len; 118 { 119 char *src = vsrc; 120 char *dst = vdst; 121 122 while (--len >= 0) 123 *dst++ = *src++; 124 } 125 126 #if 0 127 /* ditto for bzero */ 128 static void 129 kgdb_zero(vptr, len) 130 void *vptr; 131 int len; 132 { 133 char *ptr = vptr; 134 135 while (--len >= 0) 136 *ptr++ = (char) 0; 137 } 138 #endif 139 140 /* 141 * Convert a hex digit into an integer. 142 * This returns -1 if the argument passed is no 143 * valid hex digit. 144 */ 145 static int 146 digit2i(c) 147 u_char c; 148 { 149 if (c >= '0' && c <= '9') 150 return (c - '0'); 151 else if (c >= 'a' && c <= 'f') 152 return (c - 'a' + 10); 153 else if (c >= 'A' && c <= 'F') 154 155 return (c - 'A' + 10); 156 else 157 return (-1); 158 } 159 160 /* 161 * Convert the low 4 bits of an integer into 162 * an hex digit. 163 */ 164 static u_char 165 i2digit(n) 166 int n; 167 { 168 return ("0123456789abcdef"[n & 0x0f]); 169 } 170 171 /* 172 * Convert a byte array into an hex string. 173 */ 174 static void 175 mem2hex(vdst, vsrc, len) 176 void *vdst, *vsrc; 177 int len; 178 { 179 u_char *dst = vdst; 180 u_char *src = vsrc; 181 182 while (len--) { 183 *dst++ = i2digit(*src >> 4); 184 *dst++ = i2digit(*src++); 185 } 186 *dst = '\0'; 187 } 188 189 /* 190 * Convert an hex string into a byte array. 191 * This returns a pointer to the character following 192 * the last valid hex digit. If the string ends in 193 * the middle of a byte, NULL is returned. 194 */ 195 static u_char * 196 hex2mem(vdst, src, maxlen) 197 void *vdst; 198 u_char *src; 199 int maxlen; 200 { 201 u_char *dst = vdst; 202 int msb, lsb; 203 204 while (*src && maxlen--) { 205 msb = digit2i(*src++); 206 if (msb < 0) 207 return (src - 1); 208 lsb = digit2i(*src++); 209 if (lsb < 0) 210 return (NULL); 211 *dst++ = (msb << 4) | lsb; 212 } 213 return (src); 214 } 215 216 /* 217 * Convert an hex string into an integer. 218 * This returns a pointer to the character following 219 * the last valid hex digit. 220 */ 221 static vaddr_t 222 hex2i(srcp) 223 u_char **srcp; 224 { 225 char *src = *srcp; 226 vaddr_t r = 0; 227 int nibble; 228 229 while ((nibble = digit2i(*src)) >= 0) { 230 r *= 16; 231 r += nibble; 232 src++; 233 } 234 *srcp = src; 235 return (r); 236 } 237 238 /* 239 * Send a packet. 240 */ 241 static void 242 kgdb_send(bp) 243 u_char *bp; 244 { 245 u_char *p; 246 u_char csum, c; 247 248 DPRINTF(("kgdb_send: %s\n", bp)); 249 do { 250 p = bp; 251 PUTC(KGDB_START); 252 for (csum = 0; (c = *p); p++) { 253 PUTC(c); 254 csum += c; 255 } 256 PUTC(KGDB_END); 257 PUTC(i2digit(csum >> 4)); 258 PUTC(i2digit(csum)); 259 } while ((c = GETC() & 0x7f) == KGDB_BADP); 260 } 261 262 /* 263 * Receive a packet. 264 */ 265 static int 266 kgdb_recv(bp, maxlen) 267 u_char *bp; 268 int maxlen; 269 { 270 u_char *p; 271 int c, csum, tmpcsum; 272 int len; 273 274 DPRINTF(("kgdb_recv: ")); 275 do { 276 p = bp; 277 csum = len = 0; 278 while ((c = GETC()) != KGDB_START) 279 DPRINTF(("%c",c)); 280 DPRINTF(("%c Start ",c)); 281 282 while ((c = GETC()) != KGDB_END && len < maxlen) { 283 DPRINTF(("%c",c)); 284 c &= 0x7f; 285 csum += c; 286 *p++ = c; 287 len++; 288 } 289 csum &= 0xff; 290 *p = '\0'; 291 DPRINTF(("%c End ", c)); 292 293 if (len >= maxlen) { 294 DPRINTF(("Long- ")); 295 PUTC(KGDB_BADP); 296 continue; 297 } 298 tmpcsum = csum; 299 300 c = GETC(); 301 DPRINTF(("%c",c)); 302 csum -= digit2i(c) * 16; 303 c = GETC(); 304 DPRINTF(("%c",c)); 305 csum -= digit2i(c); 306 307 if (csum == 0) { 308 DPRINTF(("Good+ ")); 309 PUTC(KGDB_GOODP); 310 /* Sequence present? */ 311 if (bp[2] == ':') { 312 DPRINTF(("Seq %c%c ", bp[0], bp[1])); 313 PUTC(bp[0]); 314 PUTC(bp[1]); 315 len -= 3; 316 kgdb_copy(bp + 3, bp, len); 317 } 318 break; 319 } 320 DPRINTF((" Bad(wanted %x, off by %d)- ", tmpcsum, csum)); 321 PUTC(KGDB_BADP); 322 } while (1); 323 DPRINTF(("kgdb_recv: %s\n", bp)); 324 return (len); 325 } 326 327 /* 328 * This is called by the appropriate tty driver. 329 */ 330 void 331 kgdb_attach(getfn, putfn, ioarg) 332 int (*getfn) __P((void *)); 333 void (*putfn) __P((void *, int)); 334 void *ioarg; 335 { 336 kgdb_getc = getfn; 337 kgdb_putc = putfn; 338 kgdb_ioarg = ioarg; 339 } 340 341 /* 342 * This function does all command processing for interfacing to 343 * a remote gdb. Note that the error codes are ignored by gdb 344 * at present, but might eventually become meaningful. (XXX) 345 * It might makes sense to use POSIX errno values, because 346 * that is what the gdb/remote.c functions want to return. 347 */ 348 int 349 kgdb_trap(type, regs) 350 int type; 351 db_regs_t *regs; 352 { 353 label_t jmpbuf; 354 vaddr_t addr; 355 size_t len; 356 u_char *p; 357 358 if (kgdb_dev < 0 || kgdb_getc == NULL) { 359 /* not debugging */ 360 return (0); 361 } 362 363 db_clear_single_step(regs); 364 365 if (db_trap_callback) db_trap_callback(1); 366 367 /* Detect and recover from unexpected traps. */ 368 if (kgdb_recover != 0) { 369 printf("kgdb: caught trap 0x%x at %p\n", 370 type, (void*)PC_REGS(regs)); 371 kgdb_send("E0E"); /* 14==EFAULT */ 372 longjmp(kgdb_recover); 373 } 374 375 /* 376 * The first entry to this function is normally through 377 * a breakpoint trap in kgdb_connect(), in which case we 378 * must advance past the breakpoint because gdb will not. 379 * 380 * Machines vary as to where they leave the PC after a 381 * breakpoint trap. Those that leave the PC set to the 382 * address of the trap instruction (i.e. pc532) will not 383 * define FIXUP_PC_AFTER_BREAK(), and therefore will just 384 * advance the PC. On machines that leave the PC set to 385 * the instruction after the trap, FIXUP_PC_AFTER_BREAK 386 * will be defined to back-up the PC, so that after the 387 * "first-time" part of the if statement below has run, 388 * the PC will be the same as it was on entry. 389 * 390 * On the first entry here, we expect that gdb is not yet 391 * listening to us, so just enter the interaction loop. 392 * After the debugger is "active" (connected) it will be 393 * waiting for a "signaled" message from us. 394 */ 395 if (kgdb_active == 0) { 396 if (!IS_BREAKPOINT_TRAP(type, 0)) { 397 /* No debugger active -- let trap handle this. */ 398 if (db_trap_callback) db_trap_callback(0); 399 return (0); 400 } 401 /* Make the PC point at the breakpoint... */ 402 #ifdef FIXUP_PC_AFTER_BREAK 403 FIXUP_PC_AFTER_BREAK(regs); 404 #endif 405 /* ... and then advance past it. */ 406 #ifdef PC_ADVANCE 407 PC_ADVANCE(regs); 408 #else 409 PC_REGS(regs) += BKPT_SIZE; 410 #endif 411 kgdb_active = 1; 412 } else { 413 /* Tell remote host that an exception has occurred. */ 414 sprintf(buffer, "S%02x", kgdb_signal(type)); 415 kgdb_send(buffer); 416 } 417 418 /* Stick frame regs into our reg cache. */ 419 kgdb_getregs(regs, gdb_regs); 420 421 /* 422 * Interact with gdb until it lets us go. 423 * If we cause a trap, resume here. 424 */ 425 (void)setjmp((kgdb_recover = &jmpbuf)); 426 for (;;) { 427 kgdb_recv(buffer, sizeof(buffer)); 428 switch (buffer[0]) { 429 430 default: 431 /* Unknown command. */ 432 kgdb_send(""); 433 continue; 434 435 case KGDB_SIGNAL: 436 /* 437 * if this command came from a running gdb, 438 * answer it -- the other guy has no way of 439 * knowing if we're in or out of this loop 440 * when he issues a "remote-signal". 441 */ 442 sprintf(buffer, "S%02x", kgdb_signal(type)); 443 kgdb_send(buffer); 444 continue; 445 446 case KGDB_REG_R: 447 mem2hex(buffer, gdb_regs, sizeof(gdb_regs)); 448 kgdb_send(buffer); 449 continue; 450 451 case KGDB_REG_W: 452 p = hex2mem(gdb_regs, buffer + 1, sizeof(gdb_regs)); 453 if (p == NULL || *p != '\0') 454 kgdb_send("E01"); 455 else { 456 kgdb_setregs(regs, gdb_regs); 457 kgdb_send("OK"); 458 } 459 continue; 460 461 case KGDB_MEM_R: 462 p = buffer + 1; 463 addr = hex2i(&p); 464 if (*p++ != ',') { 465 kgdb_send("E02"); 466 continue; 467 } 468 len = hex2i(&p); 469 if (*p != '\0') { 470 kgdb_send("E03"); 471 continue; 472 } 473 if (len > sizeof(buffer) / 2) { 474 kgdb_send("E04"); 475 continue; 476 } 477 if (kgdb_acc(addr, len) == 0) { 478 kgdb_send("E05"); 479 continue; 480 } 481 db_read_bytes(addr, (size_t)len, 482 (char *)buffer + sizeof(buffer) / 2); 483 mem2hex(buffer, buffer + sizeof(buffer) / 2, len); 484 kgdb_send(buffer); 485 continue; 486 487 case KGDB_MEM_W: 488 p = buffer + 1; 489 addr = hex2i(&p); 490 if (*p++ != ',') { 491 kgdb_send("E06"); 492 continue; 493 } 494 len = hex2i(&p); 495 if (*p++ != ':') { 496 kgdb_send("E07"); 497 continue; 498 } 499 if (len > (sizeof(buffer) - (p - buffer))) { 500 kgdb_send("E08"); 501 continue; 502 } 503 p = hex2mem(buffer, p, sizeof(buffer)); 504 if (p == NULL) { 505 kgdb_send("E09"); 506 continue; 507 } 508 if (kgdb_acc(addr, len) == 0) { 509 kgdb_send("E0A"); 510 continue; 511 } 512 db_write_bytes(addr, (size_t)len, (char *)buffer); 513 kgdb_send("OK"); 514 continue; 515 516 case KGDB_DETACH: 517 case KGDB_KILL: 518 kgdb_active = 0; 519 printf("kgdb detached\n"); 520 db_clear_single_step(regs); 521 kgdb_send("OK"); 522 goto out; 523 524 case KGDB_CONT: 525 if (buffer[1]) { 526 p = buffer + 1; 527 addr = hex2i(&p); 528 if (*p) { 529 kgdb_send("E0B"); 530 continue; 531 } 532 PC_REGS(regs) = addr; 533 DPRINTF(("kgdb: continuing at %08lx\n", addr)); 534 535 } else { 536 DPRINTF(( 537 "kgdb: continuing at old address %08lx\n", 538 (vaddr_t)PC_REGS(regs))); 539 } 540 541 db_clear_single_step(regs); 542 goto out; 543 544 case KGDB_STEP: 545 if (buffer[1]) { 546 p = buffer + 1; 547 addr = hex2i(&p); 548 if (*p) { 549 kgdb_send("E0B"); 550 continue; 551 } 552 PC_REGS(regs) = addr; 553 } 554 db_set_single_step(regs); 555 goto out; 556 } 557 } 558 out: 559 if (db_trap_callback) db_trap_callback(0); 560 kgdb_recover = 0; 561 return (1); 562 } 563