1 /* $NetBSD: db_machdep.c,v 1.60 2022/10/26 23:38:09 riastradh Exp $ */
2
3 /*
4 * :set tabs=4
5 *
6 * Mach Operating System
7 * Copyright (c) 1991,1990 Carnegie Mellon University
8 * All Rights Reserved.
9 *
10 * Permission to use, copy, modify and distribute this software and its
11 * documentation is hereby granted, provided that both the copyright
12 * notice and this permission notice appear in all copies of the
13 * software, derivative works or modified versions, and any portions
14 * thereof, and that both notices appear in supporting documentation.
15 *
16 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
18 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 *
20 * Carnegie Mellon requests users of this software to return to
21 *
22 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
23 * School of Computer Science
24 * Carnegie Mellon University
25 * Pittsburgh PA 15213-3890
26 *
27 * any improvements or extensions that they make and grant Carnegie the
28 * rights to redistribute these changes.
29 *
30 * db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU)
31 *
32 * VAX enhancements by cmcmanis@mcmanis.com no rights reserved :-)
33 *
34 */
35
36 /*
37 * Interface to new debugger.
38 * Taken from i386 port and modified for vax.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: db_machdep.c,v 1.60 2022/10/26 23:38:09 riastradh Exp $");
43
44 #include "opt_multiprocessor.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h> /* just for boothowto --eichin */
48 #include <sys/cpu.h>
49 #include <sys/device.h>
50 #include <sys/intr.h>
51 #include <sys/proc.h>
52 #include <sys/reboot.h>
53
54 #include <dev/cons.h>
55
56 #include <machine/db_machdep.h>
57 #include <machine/trap.h>
58 #include <machine/frame.h>
59 #include <machine/rpb.h>
60 #include <vax/vax/gencons.h>
61
62 #include <ddb/db_active.h>
63 #include <ddb/db_sym.h>
64 #include <ddb/db_command.h>
65 #include <ddb/db_output.h>
66 #include <ddb/db_extern.h>
67 #include <ddb/db_access.h>
68 #include <ddb/db_interface.h>
69 #include <ddb/db_variables.h>
70
71 #include "ioconf.h"
72
73 db_regs_t ddb_regs;
74
75 void kdbprinttrap(int, int);
76
77 int db_active = 0;
78
79 static int splsave; /* IPL before entering debugger */
80
81 #ifdef MULTIPROCESSOR
82 static struct cpu_info *stopcpu;
83 /*
84 * Only the master CPU is allowed to enter DDB, but the correct frames
85 * must still be there. Keep the state-machine here.
86 */
87 static int
pause_cpus(void)88 pause_cpus(void)
89 {
90 volatile struct cpu_info *ci = curcpu();
91
92 if (stopcpu == NULL) {
93 stopcpu = curcpu();
94 cpu_send_ipi(IPI_DEST_ALL, IPI_DDB);
95 }
96 if ((ci->ci_flags & CI_MASTERCPU) == 0) {
97 ci->ci_flags |= CI_STOPPED;
98 while (ci->ci_flags & CI_STOPPED)
99 ;
100 return 1;
101 } else
102 return 0;
103 }
104
105 static void
resume_cpus(void)106 resume_cpus(void)
107 {
108 struct cpu_info *ci;
109 int i;
110
111 stopcpu = NULL;
112 for (i = 0; i < cpu_cd.cd_ndevs; i++) {
113 if ((ci = device_lookup_private(&cpu_cd, i)) == NULL)
114 continue;
115 ci->ci_flags &= ~CI_STOPPED;
116 }
117 }
118 #endif
119 /*
120 * VAX Call frame on the stack, this from
121 * "Computer Programming and Architecture, The VAX-11"
122 * Henry Levy & Richard Eckhouse Jr.
123 * ISBN 0-932376-07-X
124 */
125 typedef struct __vax_frame {
126 u_int vax_cond; /* condition handler */
127 u_int vax_psw:16; /* 16 bit processor status word */
128 u_int vax_regs:12; /* Register save mask. */
129 u_int vax_zero:1; /* Always zero */
130 u_int vax_calls:1; /* True if CALLS, false if CALLG */
131 u_int vax_spa:2; /* Stack pointer alignment */
132 u_int *vax_ap; /* argument pointer */
133 struct __vax_frame *vax_fp; /* frame pointer of previous frame */
134 u_int vax_pc; /* program counter */
135 u_int vax_args[1]; /* 0 or more arguments */
136 } VAX_CALLFRAME;
137
138 /*
139 * DDB is called by either <ESC> - D on keyboard, via a TRACE or
140 * BPT trap or from kernel, normally as a result of a panic.
141 * If it is the result of a panic, set the ddb register frame to
142 * contain the registers when panic was called. (easy to debug).
143 */
144 void
kdb_trap(struct trapframe * tf)145 kdb_trap(struct trapframe *tf)
146 {
147 int s;
148 #ifdef MULTIPROCESSOR
149 struct cpu_info *ci = curcpu();
150 #endif
151
152 switch (tf->tf_trap) {
153 case T_BPTFLT: /* breakpoint */
154 case T_TRCTRAP: /* single_step */
155 break;
156
157 /* XXX todo: should be migrated to use VAX_CALLFRAME at some point */
158 case T_KDBTRAP:
159 #ifndef MULTIPROCESSOR /* No fancy panic stack conversion here */
160 if (panicstr) {
161 struct callsframe *pf, *df;
162
163 df = (void *)tf->tf_fp; /* start of debug's calls */
164 pf = (void *)df->ca_fp; /* start of panic's calls */
165 memcpy(&ddb_regs.tf_r0, &pf->ca_argno, sizeof(int) * 12);
166 ddb_regs.tf_fp = pf->ca_fp;
167 ddb_regs.tf_pc = pf->ca_pc;
168 ddb_regs.tf_ap = pf->ca_ap;
169 ddb_regs.tf_sp = (unsigned)pf;
170 ddb_regs.tf_psl = tf->tf_psl & ~0x1fffe0;
171 ddb_regs.tf_psl |= pf->ca_maskpsw & 0xffe0;
172 ddb_regs.tf_psl |= (splsave << 16);
173 }
174 #endif
175 break;
176
177 default:
178 if ((boothowto & RB_KDB) == 0)
179 return;
180
181 kdbprinttrap(tf->tf_trap, tf->tf_code);
182 if (db_recover != 0) {
183 db_error("Faulted in DDB; continuing...\n");
184 /*NOTREACHED*/
185 }
186 }
187
188 #ifdef MULTIPROCESSOR
189 ci->ci_ddb_regs = tf;
190 if (pause_cpus())
191 return;
192 #endif
193 #ifndef MULTIPROCESSOR
194 if (!panicstr)
195 memcpy(&ddb_regs, tf, sizeof(struct trapframe));
196 #else
197 memcpy(&ddb_regs, stopcpu->ci_ddb_regs, sizeof(struct trapframe));
198 printf("stopped on CPU %d\n", stopcpu->ci_cpuid);
199 #endif
200
201 /* XXX Should switch to interrupt stack here, if needed. */
202
203 s = splhigh();
204 db_active++;
205 cnpollc(true);
206 db_trap(tf->tf_trap, tf->tf_code);
207 cnpollc(false);
208 db_active--;
209 splx(s);
210
211 #ifndef MULTIPROCESSOR
212 if (!panicstr)
213 memcpy(tf, &ddb_regs, sizeof(struct trapframe));
214 #else
215 memcpy(stopcpu->ci_ddb_regs, &ddb_regs, sizeof(struct trapframe));
216 #endif
217 tf->tf_sp = mfpr(PR_USP);
218 #ifdef MULTIPROCESSOR
219 rpb.wait = 0;
220 resume_cpus();
221 #endif
222 }
223
224 extern const char * const traptypes[];
225 extern int no_traps;
226
227 /*
228 * Print trap reason.
229 */
230 void
kdbprinttrap(int type,int code)231 kdbprinttrap(int type, int code)
232 {
233 db_printf("kernel: ");
234 if (type >= no_traps || type < 0)
235 db_printf("type %d", type);
236 else
237 db_printf("%s", traptypes[type]);
238 db_printf(" trap, code=%x\n", code);
239 }
240
241 /*
242 * Read bytes from kernel address space for debugger.
243 */
244 void
db_read_bytes(vaddr_t addr,size_t size,char * data)245 db_read_bytes(vaddr_t addr, size_t size, char *data)
246 {
247 memcpy(data, (void *)addr, size);
248 }
249
250 /*
251 * Write bytes to kernel address space for debugger.
252 */
253 void
db_write_bytes(vaddr_t addr,size_t size,const char * data)254 db_write_bytes(vaddr_t addr, size_t size, const char *data)
255 {
256 memcpy((void *)addr, data, size);
257 }
258
259 void
Debugger(void)260 Debugger(void)
261 {
262 splsave = splx(0xe); /* XXX WRONG (this can lower IPL) */
263 setsoftddb(); /* beg for debugger */
264 splx(splsave);
265 }
266
267 /*
268 * Machine register set.
269 */
270 const struct db_variable db_regs[] = {
271 {"r0", &ddb_regs.tf_r0, FCN_NULL},
272 {"r1", &ddb_regs.tf_r1, FCN_NULL},
273 {"r2", &ddb_regs.tf_r2, FCN_NULL},
274 {"r3", &ddb_regs.tf_r3, FCN_NULL},
275 {"r4", &ddb_regs.tf_r4, FCN_NULL},
276 {"r5", &ddb_regs.tf_r5, FCN_NULL},
277 {"r6", &ddb_regs.tf_r6, FCN_NULL},
278 {"r7", &ddb_regs.tf_r7, FCN_NULL},
279 {"r8", &ddb_regs.tf_r8, FCN_NULL},
280 {"r9", &ddb_regs.tf_r9, FCN_NULL},
281 {"r10", &ddb_regs.tf_r10, FCN_NULL},
282 {"r11", &ddb_regs.tf_r11, FCN_NULL},
283 {"ap", &ddb_regs.tf_ap, FCN_NULL},
284 {"fp", &ddb_regs.tf_fp, FCN_NULL},
285 {"sp", &ddb_regs.tf_sp, FCN_NULL},
286 {"pc", &ddb_regs.tf_pc, FCN_NULL},
287 {"psl", &ddb_regs.tf_psl, FCN_NULL},
288 };
289 const struct db_variable * const db_eregs = db_regs + __arraycount(db_regs);
290
291 #define IN_USERLAND(x) (((u_int)(x) & 0x80000000) == 0)
292
293 /*
294 * Dump a stack traceback. Takes two arguments:
295 * fp - CALL FRAME pointer
296 * stackbase - Lowest stack value
297 */
298 static void
db_dump_stack(VAX_CALLFRAME * fp,u_int stackbase,void (* pr)(const char *,...))299 db_dump_stack(VAX_CALLFRAME *fp, u_int stackbase,
300 void (*pr)(const char *, ...)) {
301 u_int nargs, arg_base, regs;
302 VAX_CALLFRAME *tmp_frame;
303 db_expr_t diff;
304 db_sym_t sym;
305 const char *symname;
306 extern int sret;
307 extern unsigned int etext;
308
309 (*pr)("Stack traceback : \n");
310 if (IN_USERLAND(fp)) {
311 (*pr)(" Process is executing in user space.\n");
312 return;
313 }
314
315 #if 0
316 while (((u_int)(fp->vax_fp) > stackbase - 0x100) &&
317 ((u_int)(fp->vax_fp) < (stackbase + USPACE))) {
318 #endif
319 while (!IN_USERLAND(fp->vax_fp)) {
320 u_int pc = fp->vax_pc;
321
322 /*
323 * Figure out the arguments by using a bit of subtlety.
324 * As the argument pointer may have been used as a temporary
325 * by the callee ... recreate what it would have pointed to
326 * as follows:
327 * The vax_regs value has a 12 bit bitmask of the registers
328 * that were saved on the stack.
329 * Store that in 'regs' and then for every bit that is
330 * on (indicates the register contents are on the stack)
331 * increment the argument base (arg_base) by one.
332 * When that is done, args[arg_base] points to the longword
333 * that identifies the number of arguments.
334 * arg_base+1 - arg_base+n are the argument pointers/contents.
335 */
336
337
338 /* If this was due to a trap/fault, pull the correct pc
339 * out of the trap frame. */
340 if (pc == (u_int) &sret && fp->vax_fp != 0) {
341 struct trapframe *tf;
342 /* Isolate the saved register bits, and count them */
343 regs = fp->vax_regs;
344 for (arg_base = 0; regs != 0; regs >>= 1) {
345 if (regs & 1)
346 arg_base++;
347 }
348 tf = (struct trapframe *) &fp->vax_args[arg_base + 2];
349 (*pr)("0x%lx: trap type=0x%lx code=0x%lx pc=0x%lx psl=0x%lx\n",
350 tf, tf->tf_trap, tf->tf_code,
351 tf->tf_pc, tf->tf_psl);
352 pc = tf->tf_pc;
353 }
354
355 diff = INT_MAX;
356 symname = NULL;
357 if (pc >= 0x80000000 && pc < (u_int) &etext) {
358 sym = db_search_symbol(pc, DB_STGY_ANY, &diff);
359 db_symbol_values(sym, &symname, 0);
360 }
361 if (symname != NULL)
362 (*pr)("0x%lx: %s+0x%lx(", fp, symname, diff);
363 else
364 (*pr)("0x%lx: %#x(", fp, pc);
365
366 /* First get the frame that called this function ... */
367 tmp_frame = fp->vax_fp;
368
369 /* Isolate the saved register bits, and count them */
370 regs = tmp_frame->vax_regs;
371 for (arg_base = 0; regs != 0; regs >>= 1) {
372 if (regs & 1)
373 arg_base++;
374 }
375
376 /* number of arguments is then pointed to by vax_args[arg_base] */
377 nargs = tmp_frame->vax_args[arg_base];
378 if (nargs) {
379 nargs--; /* reduce by one for formatting niceties */
380 arg_base++; /* skip past the actual number of arguments */
381 while (nargs--)
382 (*pr)("%#x,", tmp_frame->vax_args[arg_base++]);
383
384 /* now print out the last arg with closing brace and \n */
385 (*pr)("%#x)\n", tmp_frame->vax_args[arg_base]);
386 } else
387 (*pr)("void)\n");
388 /* move to the next frame */
389 fp = fp->vax_fp;
390 }
391 }
392
393 /*
394 * Implement the trace command which has the form:
395 *
396 * trace <-- Trace panic (same as before)
397 * trace 0x88888 <-- Trace frame whose address is 888888
398 * trace/t <-- Trace current process (0 if no current proc)
399 * trace/t 0tnn <-- Trace process nn (0t for decimal)
400 */
401 void
402 db_stack_trace_print(
403 db_expr_t addr, /* Address parameter */
404 bool have_addr, /* True if addr is valid */
405 db_expr_t count, /* Optional count */
406 const char *modif, /* pointer to flag modifier 't' */
407 void (*pr)(const char *, ...)) /* Print function */
408 {
409 struct lwp *l = curlwp;
410 struct proc *p = l->l_proc;
411 struct pcb *pcb;
412 int trace_proc;
413 pid_t curpid;
414 const char *s;
415
416 /* Check to see if we're tracing a process */
417 trace_proc = 0;
418 s = modif;
419 while (!trace_proc && *s) {
420 if (*s++ == 't')
421 trace_proc++; /* why yes we are */
422 }
423
424 /* Trace a panic */
425 if (panicstr) {
426 (*pr)("panic: %s\n", panicstr);
427 /* xxx ? where did we panic and whose stack are we using? */
428 #ifdef MULTIPROCESSOR
429 db_dump_stack((VAX_CALLFRAME *)(ddb_regs.tf_fp), ddb_regs.tf_ap, pr);
430 #else
431 db_dump_stack((VAX_CALLFRAME *)(ddb_regs.tf_sp), ddb_regs.tf_ap, pr);
432 #endif
433 return;
434 }
435
436 /*
437 * If user typed an address its either a PID, or a Frame
438 * if no address then either current proc or panic
439 */
440 if (have_addr) {
441 if (trace_proc) {
442 p = proc_find_raw((int)addr);
443 /* Try to be helpful by looking at it as if it were decimal */
444 if (p == NULL) {
445 u_int tpid = 0;
446 u_int foo = addr;
447
448 while (foo != 0) {
449 int digit = (foo >> 28) & 0xf;
450 if (digit > 9) {
451 (*pr)(" No such process.\n");
452 return;
453 }
454 tpid = tpid * 10 + digit;
455 foo = foo << 4;
456 }
457 p = proc_find_raw(tpid);
458 if (p == NULL) {
459 (*pr)(" No such process.\n");
460 return;
461 }
462 }
463 } else {
464 db_dump_stack((VAX_CALLFRAME *)addr, 0, pr);
465 return;
466 }
467 #if 0
468 } else {
469 if (!trace_proc) {
470 l = curlwp;
471 if (l == NULL) {
472 (*pr)("trace: no current process! (ignored)\n");
473 return;
474 }
475 } else {
476 if (! panicstr) {
477 (*pr)("Not a panic, no active process, ignored.\n");
478 return;
479 }
480 }
481 #endif
482 }
483 KASSERT(l != NULL);
484 pcb = lwp_getpcb(l);
485 curpid = p->p_pid;
486 (*pr)("Process %d.%d\n", curpid, l->l_lid);
487 (*pr)(" PCB contents:\n");
488 (*pr)(" KSP = 0x%x\n", (unsigned int)(pcb->KSP));
489 (*pr)(" ESP = 0x%x\n", (unsigned int)(pcb->ESP));
490 (*pr)(" SSP = 0x%x\n", (unsigned int)(pcb->SSP));
491 (*pr)(" USP = 0x%x\n", (unsigned int)(pcb->USP));
492 (*pr)(" R[00] = 0x%08x R[06] = 0x%08x\n",
493 (unsigned int)(pcb->R[0]), (unsigned int)(pcb->R[6]));
494 (*pr)(" R[01] = 0x%08x R[07] = 0x%08x\n",
495 (unsigned int)(pcb->R[1]), (unsigned int)(pcb->R[7]));
496 (*pr)(" R[02] = 0x%08x R[08] = 0x%08x\n",
497 (unsigned int)(pcb->R[2]), (unsigned int)(pcb->R[8]));
498 (*pr)(" R[03] = 0x%08x R[09] = 0x%08x\n",
499 (unsigned int)(pcb->R[3]), (unsigned int)(pcb->R[9]));
500 (*pr)(" R[04] = 0x%08x R[10] = 0x%08x\n",
501 (unsigned int)(pcb->R[4]), (unsigned int)(pcb->R[10]));
502 (*pr)(" R[05] = 0x%08x R[11] = 0x%08x\n",
503 (unsigned int)(pcb->R[5]), (unsigned int)(pcb->R[11]));
504 (*pr)(" AP = 0x%x\n", (unsigned int)(pcb->AP));
505 (*pr)(" FP = 0x%x\n", (unsigned int)(pcb->FP));
506 (*pr)(" PC = 0x%x\n", (unsigned int)(pcb->PC));
507 (*pr)(" PSL = 0x%x\n", (unsigned int)(pcb->PSL));
508 (*pr)(" Trap frame pointer: %o\n", l->l_md.md_utf);
509 db_dump_stack((VAX_CALLFRAME *)(pcb->FP), (u_int)pcb->KSP, pr);
510 return;
511 #if 0
512 while (((u_int)(cur_frame->vax_fp) > stackbase) &&
513 ((u_int)(cur_frame->vax_fp) < (stackbase + USPACE))) {
514 u_int nargs;
515 VAX_CALLFRAME *tmp_frame;
516
517 diff = INT_MAX;
518 symname = NULL;
519 sym = db_search_symbol(cur_frame->vax_pc, DB_STGY_ANY, &diff);
520 db_symbol_values(sym, &symname, 0);
521 (*pr)("%s+0x%lx(", symname, diff);
522
523 /*
524 * Figure out the arguments by using a bit of subterfuge
525 * since the argument pointer may have been used as a temporary
526 * by the callee ... recreate what it would have pointed to
527 * as follows:
528 * The vax_regs value has a 12 bit bitmask of the registers
529 * that were saved on the stack.
530 * Store that in 'regs' and then for every bit that is
531 * on (indicates the register contents are on the stack)
532 * increment the argument base (arg_base) by one.
533 * When that is done, args[arg_base] points to the longword
534 * that identifies the number of arguments.
535 * arg_base+1 - arg_base+n are the argument pointers/contents.
536 */
537
538 /* First get the frame that called this function ... */
539 tmp_frame = cur_frame->vax_fp;
540
541 /* Isolate the saved register bits, and count them */
542 regs = tmp_frame->vax_regs;
543 for (arg_base = 0; regs != 0; regs >>= 1) {
544 if (regs & 1)
545 arg_base++;
546 }
547
548 /* number of arguments is then pointed to by vax_args[arg_base] */
549 nargs = tmp_frame->vax_args[arg_base];
550 if (nargs) {
551 nargs--; /* reduce by one for formatting niceties */
552 arg_base++; /* skip past the actual number of arguments */
553 while (nargs--)
554 (*pr)("0x%x,", tmp_frame->vax_args[arg_base++]);
555
556 /* now print out the last arg with closing brace and \n */
557 (*pr)("0x%x)\n", tmp_frame->vax_args[++arg_base]);
558 } else
559 (*pr)("void)\n");
560 /* move to the next frame */
561 cur_frame = cur_frame->vax_fp;
562 }
563
564 /*
565 * DEAD CODE, previous panic tracing code.
566 */
567 if (! have_addr) {
568 printf("Trace default\n");
569 if (panicstr) {
570 cf = (int *)ddb_regs.sp;
571 } else {
572 printf("Don't know what to do without panic\n");
573 return;
574 }
575 stackbase = (ddb_regs.psl & PSL_IS ? istack : pcb);
576 }
577 #endif
578 }
579
580 static int ddbescape = 0;
581
582 int
583 kdbrint(int tkn)
584 {
585 if (ddbescape && ((tkn & 0x7f) == 'D')) {
586 setsoftddb();
587 ddbescape = 0;
588 return 1;
589 }
590
591 if ((ddbescape == 0) && ((tkn & 0x7f) == 27)) {
592 ddbescape = 1;
593 return 1;
594 }
595
596 if (ddbescape) {
597 ddbescape = 0;
598 return 2;
599 }
600
601 ddbescape = 0;
602 return 0;
603 }
604
605 #ifdef MULTIPROCESSOR
606
607 static void
608 db_mach_cpu(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
609 {
610 struct cpu_info *ci;
611
612 if ((addr < 0) || (addr >= cpu_cd.cd_ndevs))
613 return db_printf("%ld: CPU out of range\n", addr);
614 if ((ci = device_lookup_private(&cpu_cd, addr)) == NULL)
615 return db_printf("%ld: CPU not configured\n", addr);
616
617 if ((ci != curcpu()) && ((ci->ci_flags & CI_STOPPED) == 0))
618 return db_printf("CPU %ld not stopped???\n", addr);
619
620 memcpy(stopcpu->ci_ddb_regs, &ddb_regs, sizeof(struct trapframe));
621 stopcpu = ci;
622 memcpy(&ddb_regs, stopcpu->ci_ddb_regs, sizeof(struct trapframe));
623 db_printf("using CPU %ld", addr);
624 if (ci->ci_curlwp)
625 db_printf(" in proc %d.%d (%s)\n",
626 ci->ci_curlwp->l_proc->p_pid,
627 ci->ci_curlwp->l_lid,
628 ci->ci_curlwp->l_proc->p_comm);
629 }
630 #endif
631
632 const struct db_command db_machine_command_table[] = {
633 #ifdef MULTIPROCESSOR
634 { DDB_ADD_CMD("cpu", db_mach_cpu, 0,
635 "switch to another cpu", "cpu-no", NULL) },
636 #endif
637 { DDB_END_CMD },
638 };
639