1 /* $NetBSD: db_interface.c,v 1.9 2026/11/10 03:43:26 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 Mellon 26 * the rights to redistribute these changes. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.9 2026/11/10 03:43:26 thorpej Exp $"); 31 32 #ifdef _KERNEL_OPT 33 #include "opt_multiprocessor.h" 34 #include "opt_kgdb.h" 35 #endif 36 37 #define __PMAP_PRIVATE 38 39 #include <sys/types.h> 40 #include <sys/param.h> 41 42 #include <sys/atomic.h> 43 #include <sys/cpu.h> 44 #include <sys/proc.h> 45 #include <sys/reboot.h> 46 #include <sys/systm.h> 47 48 #include <uvm/uvm_extern.h> 49 50 #include <dev/cons.h> 51 52 #include <machine/db_machdep.h> 53 54 #include <ddb/db_access.h> 55 #include <ddb/db_active.h> 56 #include <ddb/db_user.h> 57 #ifndef KGDB 58 #include <ddb/db_command.h> 59 #include <ddb/db_output.h> 60 #include <ddb/db_sym.h> 61 #include <ddb/db_extern.h> 62 #include <ddb/db_interface.h> 63 #include <ddb/db_lex.h> 64 #include <ddb/db_run.h> /* for db_continue_cmd() proto */ 65 #endif 66 67 #define NOCPU ~0 68 volatile u_int ddb_cpu = NOCPU; 69 70 #ifdef _KERNEL 71 db_regs_t ddb_regs; 72 73 #ifdef MULTIPROCESSOR 74 static void db_mach_cpu_cmd(db_expr_t, bool, db_expr_t, const char *); 75 #endif 76 static void db_mach_reset_cmd(db_expr_t, bool, db_expr_t, const char *); 77 78 void db_cpuinfo_cmd(db_expr_t, bool, db_expr_t, const char *); 79 void db_tlbdump_cmd(db_expr_t, bool, db_expr_t, const char *); 80 void db_kvtophys_cmd(db_expr_t, bool, db_expr_t, const char *); 81 82 paddr_t kvtophys(vaddr_t); 83 #endif 84 85 86 const struct db_command db_machine_command_table[] = { 87 #ifdef _KERNEL 88 #ifdef MULTIPROCESSOR 89 { DDB_ADD_CMD("cpu", db_mach_cpu_cmd, 0, 90 "switch to another cpu", "cpu#", NULL) }, 91 #endif 92 { DDB_ADD_CMD("cpuinfo", db_cpuinfo_cmd, 0, 93 "Displays the cpuinfo", 94 NULL, NULL) 95 }, 96 { DDB_ADD_CMD("kvtop", db_kvtophys_cmd, 0, 97 "Print the physical address for a given kernel virtual address", 98 "address", 99 " address:\tvirtual address to look up") }, 100 { DDB_ADD_CMD("reset", db_mach_reset_cmd, CS_NOREPEAT, 101 "Initiate hardware reset", 102 NULL, NULL) }, 103 { DDB_ADD_CMD("tlb", db_tlbdump_cmd, 0, 104 "Print out TLB entries. (only works with options DEBUG)", 105 NULL, NULL) }, 106 #endif 107 { DDB_END_CMD }, 108 }; 109 110 #ifdef _KERNEL 111 112 #ifndef KGDB 113 int 114 kdb_trap(int type, db_regs_t *regs) 115 { 116 switch (type) { 117 case CAUSE_BREAKPOINT: /* breakpoint */ 118 printf("kernel: breakpoint\n"); 119 break; 120 case -1: /* keyboard interrupt */ 121 printf("kernel: kdbint trap\n"); 122 break; 123 default: 124 printf("kernel: cause %d\n", type); 125 if (db_recover != 0) { 126 db_error("Faulted in DDB; continuing...\n"); 127 /*NOTREACHED*/ 128 } 129 break; 130 } 131 132 const int s = splhigh(); 133 struct cpu_info * const ci = curcpu(); 134 135 #if defined(MULTIPROCESSOR) 136 bool first_in_ddb = false; 137 const u_int cpu_me = cpu_index(ci); 138 const u_int old_ddb_cpu = atomic_cas_uint(&ddb_cpu, NOCPU, cpu_me); 139 if (old_ddb_cpu == NOCPU) { 140 first_in_ddb = true; 141 cpu_pause_others(); 142 } else { 143 if (old_ddb_cpu != cpu_me) { 144 KASSERT(cpu_is_paused(cpu_me)); 145 cpu_pause(); 146 splx(s); 147 return 1; 148 } 149 } 150 KASSERT(!cpu_is_paused(cpu_me)); 151 #endif 152 153 ddb_regs = *regs; 154 ci->ci_ddb_regs = &ddb_regs; 155 db_active++; 156 cnpollc(true); 157 db_trap(type, 0 /*code*/); 158 cnpollc(false); 159 db_active--; 160 ci->ci_ddb_regs = NULL; 161 *regs = ddb_regs; 162 163 #if defined(MULTIPROCESSOR) 164 if (atomic_cas_uint(&ddb_cpu, cpu_me, NOCPU) == cpu_me) { 165 cpu_resume_others(); 166 } else { 167 cpu_resume(ddb_cpu); 168 if (first_in_ddb) 169 cpu_pause(); 170 } 171 #endif 172 173 splx(s); 174 return 1; 175 } 176 #endif /* !KGDB */ 177 178 179 #ifndef KGDB 180 void 181 db_tlbdump_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 182 const char *modif) 183 { 184 } 185 186 void 187 db_kvtophys_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 188 const char *modif) 189 { 190 191 if (!have_addr) 192 return; 193 } 194 195 196 static void 197 show_cpuinfo(struct cpu_info *kci) 198 { 199 struct cpu_info cpuinfobuf; 200 cpuid_t cpuid; 201 int i; 202 203 db_read_bytes((db_addr_t)kci, sizeof(cpuinfobuf), (char *)&cpuinfobuf); 204 205 struct cpu_info *ci = &cpuinfobuf; 206 cpuid = ci->ci_cpuid; 207 db_printf("cpu_info=%p, cpu_name=%s\n", kci, ci->ci_cpuname); 208 db_printf("%p cpu[%lu].ci_cpuid = %lu\n", 209 &ci->ci_cpuid, cpuid, ci->ci_cpuid); 210 db_printf("%p cpu[%lu].ci_curlwp = %p\n", 211 &ci->ci_curlwp, cpuid, ci->ci_curlwp); 212 for (i = 0; i < SOFTINT_COUNT; i++) { 213 db_printf("%p cpu[%lu].ci_softlwps[%d] = %p\n", 214 &ci->ci_softlwps[i], cpuid, i, ci->ci_softlwps[i]); 215 } 216 db_printf("%p cpu[%lu].ci_want_resched = %d\n", 217 &ci->ci_want_resched, cpuid, ci->ci_want_resched); 218 db_printf("%p cpu[%lu].ci_cpl = %d\n", 219 &ci->ci_cpl, cpuid, ci->ci_cpl); 220 db_printf("%p cpu[%lu].ci_softints = 0x%08x\n", 221 &ci->ci_softints, cpuid, ci->ci_softints); 222 db_printf("%p cpu[%lu].ci_intr_depth = %u\n", 223 &ci->ci_intr_depth, cpuid, ci->ci_intr_depth); 224 db_printf("%p cpu[%lu].ci_lastintr = %" PRIu64 "\n", 225 &ci->ci_lastintr, cpuid, ci->ci_lastintr); 226 db_printf("%p cpu[%lu].ci_lastintr_scheduled = %" PRIu64 "\n", 227 &ci->ci_lastintr_scheduled, cpuid, ci->ci_lastintr_scheduled); 228 } 229 230 void 231 db_cpuinfo_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 232 const char *modif) 233 { 234 #ifdef MULTIPROCESSOR 235 CPU_INFO_ITERATOR cii; 236 struct cpu_info *ci; 237 bool showall = false; 238 239 if (modif != NULL) { 240 for (; *modif != '\0'; modif++) { 241 switch (*modif) { 242 case 'a': 243 showall = true; 244 break; 245 } 246 } 247 } 248 249 if (showall) { 250 for (CPU_INFO_FOREACH(cii, ci)) { 251 show_cpuinfo(ci); 252 } 253 } else 254 #endif /* MULTIPROCESSOR */ 255 show_cpuinfo(curcpu()); 256 } 257 258 259 static void 260 db_mach_reset_cmd(db_expr_t addr, bool have_addr, db_expr_t count, 261 const char *modif) 262 { 263 } 264 265 #endif /* !KGDB */ 266 267 #ifdef MULTIPROCESSOR 268 269 bool 270 ddb_running_on_this_cpu_p(void) 271 { 272 return ddb_cpu == cpu_index(curcpu()); 273 } 274 275 bool 276 ddb_running_on_any_cpu_p(void) 277 { 278 return ddb_cpu != NOCPU; 279 } 280 281 void 282 db_resume_others(void) 283 { 284 u_int cpu_me = cpu_index(curcpu()); 285 286 if (atomic_cas_uint(&ddb_cpu, cpu_me, NOCPU) == cpu_me) 287 cpu_resume_others(); 288 } 289 290 static void 291 db_mach_cpu_cmd(db_expr_t addr, bool have_addr, db_expr_t count, const char *modif) 292 { 293 CPU_INFO_ITERATOR cii; 294 struct cpu_info *ci; 295 296 if (!have_addr) { 297 cpu_debug_dump(); 298 return; 299 } 300 for (CPU_INFO_FOREACH(cii, ci)) { 301 if (cpu_index(ci) == addr) 302 break; 303 } 304 if (ci == NULL) { 305 db_printf("CPU %ld not configured\n", (long)addr); 306 return; 307 } 308 if (ci != curcpu()) { 309 if (!cpu_is_paused(cpu_index(ci))) { 310 db_printf("CPU %ld not paused\n", (long)addr); 311 return; 312 } 313 (void)atomic_cas_uint(&ddb_cpu, cpu_index(curcpu()), cpu_index(ci)); 314 db_continue_cmd(0, false, 0, ""); 315 } 316 } 317 #endif /* MULTIPROCESSOR */ 318 319 #endif /* _KERNEL */ 320