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