Home | History | Annotate | Line # | Download | only in amd64
      1 /*	$NetBSD: db_interface.c,v 1.45 2026/11/10 03:43:25 thorpej Exp $	*/
      2 
      3 /*
      4  * Mach Operating System
      5  * Copyright (c) 1991,1990 Carnegie Mellon University
      6  * All Rights Reserved.
      7  *
      8  * Permission to use, copy, modify and distribute this software and its
      9  * documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  *
     28  *	db_interface.c,v 2.4 1991/02/05 17:11:13 mrt (CMU)
     29  */
     30 
     31 /*
     32  * Interface to new debugger.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.45 2026/11/10 03:43:25 thorpej Exp $");
     37 
     38 #include "opt_ddb.h"
     39 #include "opt_multiprocessor.h"
     40 
     41 #include "lapic.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/proc.h>
     45 #include <sys/reboot.h>
     46 #include <sys/systm.h>
     47 #include <sys/atomic.h>
     48 #include <sys/cpu.h>
     49 
     50 #include <dev/cons.h>
     51 
     52 #include <machine/cpufunc.h>
     53 #include <machine/db_machdep.h>
     54 #include <machine/cpuvar.h>
     55 #if NIOAPIC > 0
     56 #include <machine/i82093var.h>
     57 #endif
     58 #if NLAPIC > 0
     59 #include <machine/i82489reg.h>
     60 #include <machine/i82489var.h>
     61 #endif
     62 
     63 #include <ddb/db_sym.h>
     64 #include <ddb/db_command.h>
     65 #include <ddb/db_extern.h>
     66 #include <ddb/db_access.h>
     67 #include <ddb/db_active.h>
     68 #include <ddb/db_output.h>
     69 #include <ddb/ddbvar.h>
     70 
     71 extern const char *const trap_type[];
     72 extern int trap_types;
     73 
     74 #ifdef MULTIPROCESSOR
     75 /* ddb_regs defined as a macro */
     76 db_regs_t *ddb_regp = NULL;
     77 #else
     78 db_regs_t ddb_regs;
     79 #endif
     80 
     81 void db_mach_cpu (db_expr_t, bool, db_expr_t, const char *);
     82 
     83 const struct db_command db_machine_command_table[] = {
     84 #ifdef MULTIPROCESSOR
     85 	{ DDB_ADD_CMD("cpu",	db_mach_cpu,	0,
     86 	  "switch to another cpu", "cpu-no", NULL) },
     87 #endif
     88 	{ DDB_END_CMD },
     89 };
     90 
     91 void kdbprinttrap(int, int);
     92 #ifdef MULTIPROCESSOR
     93 extern void ddb_ipi(struct trapframe);
     94 static void ddb_suspend(struct trapframe *);
     95 #ifndef XENPV
     96 int ddb_vec;
     97 #endif /* XENPV */
     98 static bool ddb_mp_online;
     99 #endif
    100 
    101 #define NOCPU	-1
    102 
    103 int ddb_cpu = NOCPU;
    104 
    105 typedef void (vector)(void);
    106 extern vector Xintr_ddbipi, Xintr_x2apic_ddbipi;
    107 
    108 void
    109 db_machine_init(void)
    110 {
    111 
    112 #ifdef MULTIPROCESSOR
    113 #ifndef XENPV
    114 	struct idt_vec *iv = &(cpu_info_primary.ci_idtvec);
    115 	vector *handler = &Xintr_ddbipi;
    116 	idt_descriptor_t *idt = iv->iv_idt;
    117 #if NLAPIC > 0
    118 	if (lapic_is_x2apic())
    119 		handler = &Xintr_x2apic_ddbipi;
    120 #endif
    121 	ddb_vec = idt_vec_alloc(iv, 0xf0, 0xff);
    122 	KASSERT(ddb_vec > 0);
    123 	set_idtgate(&idt[ddb_vec], handler, 1, SDT_SYS386IGT, SEL_KPL,
    124 	    GSEL(GCODE_SEL, SEL_KPL));
    125 #else
    126 	/* Initialised as part of xen_ipi_init() */
    127 #endif /* XENPV */
    128 #endif
    129 }
    130 
    131 #ifdef MULTIPROCESSOR
    132 
    133 __cpu_simple_lock_t db_lock;
    134 
    135 static int
    136 db_suspend_others(void)
    137 {
    138 	int cpu_me = cpu_number();
    139 	int win;
    140 
    141 #ifndef XENPV
    142 	if (ddb_vec == 0)
    143 		return 1;
    144 #endif /* XENPV */
    145 
    146 	__cpu_simple_lock(&db_lock);
    147 	if (ddb_cpu == NOCPU)
    148 		ddb_cpu = cpu_me;
    149 	win = (ddb_cpu == cpu_me);
    150 	__cpu_simple_unlock(&db_lock);
    151 	if (win) {
    152 #ifdef XENPV
    153 		xen_broadcast_ipi(XEN_IPI_DDB);
    154 #else
    155 #if NLAPIC > 0
    156 		if (ddb_vec != 0)
    157 			x86_ipi(ddb_vec, LAPIC_DEST_ALLEXCL,
    158 			    LAPIC_DLMODE_FIXED);
    159 #endif
    160 #endif /* XENPV */
    161 	}
    162 	ddb_mp_online = x86_mp_online;
    163 	x86_mp_online = false;
    164 	return win;
    165 }
    166 
    167 static void
    168 db_resume_others(void)
    169 {
    170 	CPU_INFO_ITERATOR cii;
    171 	struct cpu_info *ci;
    172 
    173 	x86_mp_online = ddb_mp_online;
    174 	__cpu_simple_lock(&db_lock);
    175 	ddb_cpu = NOCPU;
    176 	__cpu_simple_unlock(&db_lock);
    177 
    178 	for (CPU_INFO_FOREACH(cii, ci)) {
    179 		if (ci->ci_flags & CPUF_PAUSE)
    180 			atomic_and_32(&ci->ci_flags, ~CPUF_PAUSE);
    181 	}
    182 }
    183 
    184 #endif
    185 
    186 /*
    187  * Print trap reason.
    188  */
    189 void
    190 kdbprinttrap(int type, int code)
    191 {
    192 	db_printf("kernel: ");
    193 	if (type >= trap_types || type < 0)
    194 		db_printf("type %d", type);
    195 	else
    196 		db_printf("%s", trap_type[type]);
    197 	db_printf(" trap, code=%x\n", code);
    198 }
    199 
    200 /*
    201  *  kdb_trap - field a TRACE or BPT trap
    202  */
    203 int
    204 kdb_trap(int type, int code, db_regs_t *regs)
    205 {
    206 	int s;
    207 #ifdef MULTIPROCESSOR
    208 	db_regs_t dbreg;
    209 #endif
    210 
    211 	switch (type) {
    212 	case T_NMI:	/* NMI */
    213 		printf("NMI ... going to debugger\n");
    214 		/*FALLTHROUGH*/
    215 	case T_BPTFLT:	/* breakpoint */
    216 	case T_TRCTRAP:	/* single_step */
    217 	case -1:	/* keyboard interrupt */
    218 		break;
    219 	default:
    220 		if (!db_onpanic && db_recover == 0)
    221 			return (0);
    222 
    223 		kdbprinttrap(type, code);
    224 		if (db_recover != 0) {
    225 			db_error("Faulted in DDB; continuing...\n");
    226 			/*NOTREACHED*/
    227 		}
    228 	}
    229 
    230 #ifdef MULTIPROCESSOR
    231 	if (!db_suspend_others()) {
    232 		ddb_suspend(regs);
    233 	} else {
    234 	curcpu()->ci_ddb_regs = &dbreg;
    235 	ddb_regp = &dbreg;
    236 #endif
    237 
    238 	ddb_regs = *regs;
    239 
    240 	ddb_regs.tf_cs &= 0xffff;
    241 	ddb_regs.tf_ds &= 0xffff;
    242 	ddb_regs.tf_es &= 0xffff;
    243 	ddb_regs.tf_fs &= 0xffff;
    244 	ddb_regs.tf_gs &= 0xffff;
    245 	ddb_regs.tf_ss &= 0xffff;
    246 
    247 	s = splhigh();
    248 	db_active++;
    249 	cnpollc(true);
    250 	db_trap(type, code);
    251 	cnpollc(false);
    252 	db_active--;
    253 	splx(s);
    254 #ifdef MULTIPROCESSOR
    255 	db_resume_others();
    256 	}
    257 	/* Restore dbreg because ddb_regp can be changed by db_mach_cpu */
    258 	ddb_regp = &dbreg;
    259 #endif
    260 
    261 	*regs = ddb_regs;
    262 #ifdef MULTIPROCESSOR
    263 	ddb_regp = NULL;
    264 #endif
    265 
    266 	return (1);
    267 }
    268 
    269 void
    270 cpu_Debugger(void)
    271 {
    272 	breakpoint();
    273 }
    274 
    275 #ifdef MULTIPROCESSOR
    276 
    277 /*
    278  * Called when we receive a debugger IPI (inter-processor interrupt).
    279  * As with trap() in trap.c, this function is called from an assembly
    280  * language IDT gate entry routine which prepares a suitable stack frame,
    281  * and restores this frame after the exception has been processed. Note
    282  * that the effect is as if the arguments were passed call by reference.
    283  */
    284 
    285 void
    286 ddb_ipi(struct trapframe frame)
    287 {
    288 
    289 	ddb_suspend(&frame);
    290 }
    291 
    292 static void
    293 ddb_suspend(struct trapframe *frame)
    294 {
    295 	volatile struct cpu_info *ci = curcpu();
    296 	db_regs_t regs;
    297 
    298 	regs = *frame;
    299 
    300 	ci->ci_ddb_regs = &regs;
    301 
    302 	atomic_or_32(&ci->ci_flags, CPUF_PAUSE);
    303 
    304 	while (ci->ci_flags & CPUF_PAUSE)
    305 		x86_pause();
    306 	ci->ci_ddb_regs = 0;
    307 	tlbflushg();
    308 }
    309 
    310 
    311 extern void cpu_debug_dump(void); /* XXX */
    312 
    313 /*
    314  * XXX sync with sys/arch/i386/i386/db_interface.c
    315  * XXX sync with usr.sbin/crash/arch/x86.c
    316  */
    317 void
    318 db_mach_cpu(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif)
    319 {
    320 	struct cpu_info *ci;
    321 	if (!have_addr) {
    322 		cpu_debug_dump();
    323 		return;
    324 	}
    325 
    326 	if (addr < 0) {
    327 		db_printf("%ld: CPU out of range\n", addr);
    328 		return;
    329 	}
    330 	ci = cpu_lookup(addr);
    331 	if (ci == NULL) {
    332 		db_printf("CPU %ld not configured\n", addr);
    333 		return;
    334 	}
    335 	if (ci != curcpu()) {
    336 		if (!(ci->ci_flags & CPUF_PAUSE)) {
    337 			db_printf("CPU %ld not paused\n", addr);
    338 			return;
    339 		}
    340 	}
    341 	if (ci->ci_ddb_regs == 0) {
    342 		db_printf("CPU %ld has no saved regs\n", addr);
    343 		return;
    344 	}
    345 	db_printf("using CPU %ld", addr);
    346 	ddb_regp = ci->ci_ddb_regs;
    347 }
    348 
    349 #endif
    350