Home | History | Annotate | Line # | Download | only in aarch64
      1 /* $NetBSD: db_machdep.c,v 1.47 2026/11/10 03:43:24 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: db_machdep.c,v 1.47 2026/11/10 03:43:24 thorpej Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_compat_netbsd32.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/atomic.h>
     42 #include <sys/cpu.h>
     43 #include <sys/lwp.h>
     44 #include <sys/intr.h>
     45 
     46 #include <uvm/uvm.h>
     47 
     48 #include <arm/cpufunc.h>
     49 
     50 #include <aarch64/db_machdep.h>
     51 #include <aarch64/armreg.h>
     52 #include <aarch64/locore.h>
     53 #include <aarch64/pmap.h>
     54 
     55 #include <arm/cpufunc.h>
     56 
     57 #include <ddb/db_access.h>
     58 #include <ddb/db_active.h>
     59 #include <ddb/db_command.h>
     60 #include <ddb/db_output.h>
     61 #include <ddb/db_proc.h>
     62 #include <ddb/db_variables.h>
     63 #include <ddb/db_run.h>
     64 #include <ddb/db_sym.h>
     65 #include <ddb/db_extern.h>
     66 #include <ddb/db_interface.h>
     67 #include <ddb/db_user.h>
     68 
     69 #include <dev/cons.h>
     70 
     71 void db_md_cpuinfo_cmd(db_expr_t, bool, db_expr_t, const char *);
     72 void db_md_frame_cmd(db_expr_t, bool, db_expr_t, const char *);
     73 void db_md_lwp_cmd(db_expr_t, bool, db_expr_t, const char *);
     74 void db_md_pte_cmd(db_expr_t, bool, db_expr_t, const char *);
     75 void db_md_reset_cmd(db_expr_t, bool, db_expr_t, const char *);
     76 void db_md_tlbi_cmd(db_expr_t, bool, db_expr_t, const char *);
     77 void db_md_ttbr_cmd(db_expr_t, bool, db_expr_t, const char *);
     78 void db_md_sysreg_cmd(db_expr_t, bool, db_expr_t, const char *);
     79 void db_md_break_cmd(db_expr_t, bool, db_expr_t, const char *);
     80 void db_md_watch_cmd(db_expr_t, bool, db_expr_t, const char *);
     81 #if defined(_KERNEL) && defined(MULTIPROCESSOR)
     82 void db_md_switch_cpu_cmd(db_expr_t, bool, db_expr_t, const char *);
     83 #endif
     84 #if defined(_KERNEL)
     85 static void db_md_meminfo_cmd(db_expr_t, bool, db_expr_t, const char *);
     86 #endif
     87 
     88 #ifdef _KERNEL
     89 #define MAX_BREAKPOINT	15
     90 #define MAX_WATCHPOINT	15
     91 /* The number varies depending on the CPU core (e.g. big.LITTLE) */
     92 static int max_breakpoint = MAX_BREAKPOINT;
     93 static int max_watchpoint = MAX_WATCHPOINT;
     94 
     95 struct breakpoint_info {
     96 	db_addr_t addr;
     97 };
     98 static struct breakpoint_info breakpoint_buf[MAX_BREAKPOINT + 1];
     99 
    100 struct watchpoint_info {
    101 	db_addr_t addr;
    102 	int size;
    103 	int accesstype;
    104 };
    105 static struct watchpoint_info watchpoint_buf[MAX_WATCHPOINT + 1];
    106 #endif
    107 
    108 const struct db_command db_machine_command_table[] = {
    109 #if defined(_KERNEL) && defined(MULTIPROCESSOR)
    110 	{
    111 		DDB_ADD_CMD(
    112 		    "cpu", db_md_switch_cpu_cmd, 0,
    113 		    "switch to a different cpu",
    114 		    NULL, NULL)
    115 	},
    116 #endif
    117 #if defined(_KERNEL)
    118 	{
    119 		DDB_ADD_CMD(
    120 		    "break", db_md_break_cmd, 0,
    121 		    "set or clear breakpoint",
    122 		    "[address|#]",
    123 		    "\taddress: breakpoint address to set\n"
    124 		    "\t#: breakpoint number to remove\n")
    125 	},
    126 	{
    127 		DDB_ADD_CMD(
    128 		    "cpuinfo", db_md_cpuinfo_cmd, 0,
    129 		    "Displays the current cpuinfo",
    130 		    NULL, NULL)
    131 	},
    132 	{
    133 		DDB_ADD_CMD(
    134 		    "frame", db_md_frame_cmd, 0,
    135 		    "Displays the contents of a trapframe",
    136 		    "address",
    137 		    "\taddress:\taddress of trapframe to display")
    138 	},
    139 	{
    140 		DDB_ADD_CMD(
    141 		    "lwp", db_md_lwp_cmd, 0,
    142 		    "Displays the lwp",
    143 		    "address",
    144 		    "\taddress:\taddress of lwp to display")
    145 	},
    146 	{
    147 		DDB_ADD_CMD(
    148 		    "pte", db_md_pte_cmd, 0,
    149 		    "Display information of pte",
    150 		    "address",
    151 		    "\taddress:\tvirtual address of page")
    152 	},
    153 	{
    154 		DDB_ADD_CMD(
    155 		    "reset", db_md_reset_cmd, 0,
    156 		    "Reset the system",
    157 		    NULL, NULL)
    158 	},
    159 	{
    160 		DDB_ADD_CMD(
    161 		    "sysreg", db_md_sysreg_cmd, 0,
    162 		    "Displays system registers",
    163 		    NULL, NULL)
    164 	},
    165 	{
    166 		DDB_ADD_CMD(
    167 		    "tlbi", db_md_tlbi_cmd, 0,
    168 		    "flush tlb",
    169 		    NULL, NULL)
    170 	},
    171 	{
    172 		DDB_ADD_CMD(
    173 		    "ttbr", db_md_ttbr_cmd, 0,
    174 		    "Dump or count TTBR table",
    175 		    "[/apc] address | pid",
    176 		    "\taddress:\taddress of pmap to display\n"
    177 		    "\tpid:\t\tpid of pmap to display")
    178 	},
    179 	{
    180 		DDB_ADD_CMD(
    181 		    "watch", db_md_watch_cmd, 0,
    182 		    "set or clear watchpoint",
    183 		    "[/rwbhlq] [address|#]",
    184 		    "\taddress: watchpoint address to set\n"
    185 		    "\t#: watchpoint number to remove\n"
    186 		    "\t/rw: read or write access\n"
    187 		    "\t/bhlq: size of access\n")
    188 	},
    189 	{
    190 		DDB_ADD_CMD(
    191 		    "meminfo", db_md_meminfo_cmd, 0,
    192 		    "Dump info about memory ranges",
    193 		    NULL, NULL)
    194 	},
    195 #endif
    196 	{
    197 		DDB_END_CMD
    198 	},
    199 };
    200 
    201 const struct db_variable db_regs[] = {
    202 	{ "x0",    (long *) &ddb_regs.tf_reg[0],  FCN_NULL, NULL },
    203 	{ "x1",    (long *) &ddb_regs.tf_reg[1],  FCN_NULL, NULL },
    204 	{ "x2",    (long *) &ddb_regs.tf_reg[2],  FCN_NULL, NULL },
    205 	{ "x3",    (long *) &ddb_regs.tf_reg[3],  FCN_NULL, NULL },
    206 	{ "x4",    (long *) &ddb_regs.tf_reg[4],  FCN_NULL, NULL },
    207 	{ "x5",    (long *) &ddb_regs.tf_reg[5],  FCN_NULL, NULL },
    208 	{ "x6",    (long *) &ddb_regs.tf_reg[6],  FCN_NULL, NULL },
    209 	{ "x7",    (long *) &ddb_regs.tf_reg[7],  FCN_NULL, NULL },
    210 	{ "x8",    (long *) &ddb_regs.tf_reg[8],  FCN_NULL, NULL },
    211 	{ "x9",    (long *) &ddb_regs.tf_reg[9],  FCN_NULL, NULL },
    212 	{ "x10",   (long *) &ddb_regs.tf_reg[10], FCN_NULL, NULL },
    213 	{ "x11",   (long *) &ddb_regs.tf_reg[11], FCN_NULL, NULL },
    214 	{ "x12",   (long *) &ddb_regs.tf_reg[12], FCN_NULL, NULL },
    215 	{ "x13",   (long *) &ddb_regs.tf_reg[13], FCN_NULL, NULL },
    216 	{ "x14",   (long *) &ddb_regs.tf_reg[14], FCN_NULL, NULL },
    217 	{ "x15",   (long *) &ddb_regs.tf_reg[15], FCN_NULL, NULL },
    218 	{ "x16",   (long *) &ddb_regs.tf_reg[16], FCN_NULL, NULL },
    219 	{ "x17",   (long *) &ddb_regs.tf_reg[17], FCN_NULL, NULL },
    220 	{ "x18",   (long *) &ddb_regs.tf_reg[18], FCN_NULL, NULL },
    221 	{ "x19",   (long *) &ddb_regs.tf_reg[19], FCN_NULL, NULL },
    222 	{ "x20",   (long *) &ddb_regs.tf_reg[20], FCN_NULL, NULL },
    223 	{ "x21",   (long *) &ddb_regs.tf_reg[21], FCN_NULL, NULL },
    224 	{ "x22",   (long *) &ddb_regs.tf_reg[22], FCN_NULL, NULL },
    225 	{ "x23",   (long *) &ddb_regs.tf_reg[23], FCN_NULL, NULL },
    226 	{ "x24",   (long *) &ddb_regs.tf_reg[24], FCN_NULL, NULL },
    227 	{ "x25",   (long *) &ddb_regs.tf_reg[25], FCN_NULL, NULL },
    228 	{ "x26",   (long *) &ddb_regs.tf_reg[26], FCN_NULL, NULL },
    229 	{ "x27",   (long *) &ddb_regs.tf_reg[27], FCN_NULL, NULL },
    230 	{ "x28",   (long *) &ddb_regs.tf_reg[28], FCN_NULL, NULL },
    231 	{ "x29",   (long *) &ddb_regs.tf_reg[29], FCN_NULL, NULL },
    232 	{ "x30",   (long *) &ddb_regs.tf_reg[30], FCN_NULL, NULL },
    233 	{ "sp",    (long *) &ddb_regs.tf_sp,      FCN_NULL, NULL },
    234 	{ "pc",    (long *) &ddb_regs.tf_pc,      FCN_NULL, NULL },
    235 	{ "spsr",  (long *) &ddb_regs.tf_spsr,    FCN_NULL, NULL }
    236 };
    237 
    238 const struct db_variable * const db_eregs = db_regs + __arraycount(db_regs);
    239 
    240 void
    241 dump_trapframe(struct trapframe *tf, void (*pr)(const char *, ...))
    242 {
    243 	struct trapframe tf_buf;
    244 
    245 	db_read_bytes((db_addr_t)tf, sizeof(tf_buf), (char *)&tf_buf);
    246 	tf = &tf_buf;
    247 
    248 #ifdef COMPAT_NETBSD32
    249 	if (tf->tf_spsr & SPSR_A32) {
    250 		(*pr)("    pc=%016"PRIxREGISTER",   spsr=%016"PRIxREGISTER
    251 		    " (AArch32)\n", tf->tf_pc, tf->tf_spsr);
    252 		(*pr)("   esr=%016"PRIxREGISTER",    far=%016"PRIxREGISTER"\n",
    253 		    tf->tf_esr, tf->tf_far);
    254 		(*pr)("    r0=%016"PRIxREGISTER",     r1=%016"PRIxREGISTER"\n",
    255 		    tf->tf_reg[0], tf->tf_reg[1]);
    256 		(*pr)("    r2=%016"PRIxREGISTER",     r3=%016"PRIxREGISTER"\n",
    257 		    tf->tf_reg[2], tf->tf_reg[3]);
    258 		(*pr)("    r4=%016"PRIxREGISTER",     r5=%016"PRIxREGISTER"\n",
    259 		    tf->tf_reg[4], tf->tf_reg[5]);
    260 		(*pr)("    r6=%016"PRIxREGISTER",     r7=%016"PRIxREGISTER"\n",
    261 		    tf->tf_reg[6], tf->tf_reg[7]);
    262 		(*pr)("    r8=%016"PRIxREGISTER",     r9=%016"PRIxREGISTER"\n",
    263 		    tf->tf_reg[8], tf->tf_reg[9]);
    264 		(*pr)("   r10=%016"PRIxREGISTER",    r11=%016"PRIxREGISTER"\n",
    265 		    tf->tf_reg[10], tf->tf_reg[11]);
    266 		(*pr)("   r12=%016"PRIxREGISTER", sp=r13=%016"PRIxREGISTER"\n",
    267 		    tf->tf_reg[12], tf->tf_reg[13]);
    268 		(*pr)("lr=r14=%016"PRIxREGISTER", pc=r15=%016"PRIxREGISTER"\n",
    269 		    tf->tf_reg[14], tf->tf_pc);
    270 		return;
    271 	}
    272 #endif
    273 	(*pr)("    pc=%016"PRIxREGISTER",   spsr=%016"PRIxREGISTER"\n",
    274 	    tf->tf_pc, tf->tf_spsr);
    275 	(*pr)("   esr=%016"PRIxREGISTER",    far=%016"PRIxREGISTER"\n",
    276 	    tf->tf_esr, tf->tf_far);
    277 	(*pr)("    x0=%016"PRIxREGISTER",     x1=%016"PRIxREGISTER"\n",
    278 	    tf->tf_reg[0], tf->tf_reg[1]);
    279 	(*pr)("    x2=%016"PRIxREGISTER",     x3=%016"PRIxREGISTER"\n",
    280 	    tf->tf_reg[2], tf->tf_reg[3]);
    281 	(*pr)("    x4=%016"PRIxREGISTER",     x5=%016"PRIxREGISTER"\n",
    282 	    tf->tf_reg[4], tf->tf_reg[5]);
    283 	(*pr)("    x6=%016"PRIxREGISTER",     x7=%016"PRIxREGISTER"\n",
    284 	    tf->tf_reg[6], tf->tf_reg[7]);
    285 	(*pr)("    x8=%016"PRIxREGISTER",     x9=%016"PRIxREGISTER"\n",
    286 	    tf->tf_reg[8], tf->tf_reg[9]);
    287 	(*pr)("   x10=%016"PRIxREGISTER",    x11=%016"PRIxREGISTER"\n",
    288 	    tf->tf_reg[10], tf->tf_reg[11]);
    289 	(*pr)("   x12=%016"PRIxREGISTER",    x13=%016"PRIxREGISTER"\n",
    290 	    tf->tf_reg[12], tf->tf_reg[13]);
    291 	(*pr)("   x14=%016"PRIxREGISTER",    x15=%016"PRIxREGISTER"\n",
    292 	    tf->tf_reg[14], tf->tf_reg[15]);
    293 	(*pr)("   x16=%016"PRIxREGISTER",    x17=%016"PRIxREGISTER"\n",
    294 	    tf->tf_reg[16], tf->tf_reg[17]);
    295 	(*pr)("   x18=%016"PRIxREGISTER",    x19=%016"PRIxREGISTER"\n",
    296 	    tf->tf_reg[18], tf->tf_reg[19]);
    297 	(*pr)("   x20=%016"PRIxREGISTER",    x21=%016"PRIxREGISTER"\n",
    298 	    tf->tf_reg[20], tf->tf_reg[21]);
    299 	(*pr)("   x22=%016"PRIxREGISTER",    x23=%016"PRIxREGISTER"\n",
    300 	    tf->tf_reg[22], tf->tf_reg[23]);
    301 	(*pr)("   x24=%016"PRIxREGISTER",    x25=%016"PRIxREGISTER"\n",
    302 	    tf->tf_reg[24], tf->tf_reg[25]);
    303 	(*pr)("   x26=%016"PRIxREGISTER",    x27=%016"PRIxREGISTER"\n",
    304 	    tf->tf_reg[26], tf->tf_reg[27]);
    305 	(*pr)("   x28=%016"PRIxREGISTER", fp=x29=%016"PRIxREGISTER"\n",
    306 	    tf->tf_reg[28], tf->tf_reg[29]);
    307 	(*pr)("lr=x30=%016"PRIxREGISTER",     sp=%016"PRIxREGISTER"\n",
    308 	    tf->tf_reg[30],  tf->tf_sp);
    309 }
    310 
    311 void
    312 dump_switchframe(struct trapframe *tf, void (*pr)(const char *, ...))
    313 {
    314 	struct trapframe tf_buf;
    315 
    316 	db_read_bytes((db_addr_t)tf, sizeof(tf_buf), (char *)&tf_buf);
    317 	tf = &tf_buf;
    318 
    319 	(*pr)("   x19=%016"PRIxREGISTER",    x20=%016"PRIxREGISTER"\n",
    320 	    tf->tf_reg[19], tf->tf_reg[20]);
    321 	(*pr)("   x21=%016"PRIxREGISTER",    x22=%016"PRIxREGISTER"\n",
    322 	    tf->tf_reg[21], tf->tf_reg[22]);
    323 	(*pr)("   x23=%016"PRIxREGISTER",    x24=%016"PRIxREGISTER"\n",
    324 	    tf->tf_reg[23], tf->tf_reg[24]);
    325 	(*pr)("   x25=%016"PRIxREGISTER",    x26=%016"PRIxREGISTER"\n",
    326 	    tf->tf_reg[25], tf->tf_reg[26]);
    327 	(*pr)("   x27=%016"PRIxREGISTER",    x28=%016"PRIxREGISTER"\n",
    328 	    tf->tf_reg[27], tf->tf_reg[28]);
    329 	(*pr)("fp=x29=%016"PRIxREGISTER", lr=x30=%016"PRIxREGISTER"\n",
    330 	    tf->tf_reg[29], tf->tf_reg[30]);
    331 }
    332 
    333 
    334 #if defined(_KERNEL)
    335 static void
    336 show_cpuinfo(struct cpu_info *ci)
    337 {
    338 	struct cpu_info cpuinfobuf;
    339 	u_int cpuidx;
    340 	int i;
    341 
    342 	db_read_bytes((db_addr_t)ci, sizeof(cpuinfobuf), (char *)&cpuinfobuf);
    343 
    344 	cpuidx = cpu_index(&cpuinfobuf);
    345 	db_printf("cpu_info=%p, cpu_name=%s\n", ci, cpuinfobuf.ci_cpuname);
    346 	db_printf("%p cpu[%u].ci_cpuid         = 0x%lx\n",
    347 	    &ci->ci_cpuid, cpuidx, cpuinfobuf.ci_cpuid);
    348 	db_printf("%p cpu[%u].ci_curlwp        = %p\n",
    349 	    &ci->ci_curlwp, cpuidx, cpuinfobuf.ci_curlwp);
    350 	db_printf("%p cpu[%u].ci_onproc        = %p\n",
    351 	    &ci->ci_onproc, cpuidx, cpuinfobuf.ci_onproc);
    352 	for (i = 0; i < SOFTINT_COUNT; i++) {
    353 		db_printf("%p cpu[%u].ci_softlwps[%d]   = %p\n",
    354 		    &ci->ci_softlwps[i], cpuidx, i, cpuinfobuf.ci_softlwps[i]);
    355 	}
    356 	db_printf("%p cpu[%u].ci_lastintr      = %" PRIu64 "\n",
    357 	    &ci->ci_lastintr, cpuidx, cpuinfobuf.ci_lastintr);
    358 	db_printf("%p cpu[%u].ci_want_resched  = %d\n",
    359 	    &ci->ci_want_resched, cpuidx, cpuinfobuf.ci_want_resched);
    360 	db_printf("%p cpu[%u].ci_cpl           = %d\n",
    361 	    &ci->ci_cpl, cpuidx, cpuinfobuf.ci_cpl);
    362 	db_printf("%p cpu[%u].ci_softints      = 0x%08x\n",
    363 	    &ci->ci_softints, cpuidx, cpuinfobuf.ci_softints);
    364 	db_printf("%p cpu[%u].ci_intr_depth    = %u\n",
    365 	    &ci->ci_intr_depth, cpuidx, cpuinfobuf.ci_intr_depth);
    366 	db_printf("%p cpu[%u].ci_biglock_count = %u\n",
    367 	    &ci->ci_biglock_count, cpuidx, cpuinfobuf.ci_biglock_count);
    368 }
    369 
    370 void
    371 db_md_cpuinfo_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    372     const char *modif)
    373 {
    374 #ifdef MULTIPROCESSOR
    375 	CPU_INFO_ITERATOR cii;
    376 	struct cpu_info *ci;
    377 	bool showall = false;
    378 
    379 	if (modif != NULL) {
    380 		for (; *modif != '\0'; modif++) {
    381 			switch (*modif) {
    382 			case 'a':
    383 				showall = true;
    384 				break;
    385 			}
    386 		}
    387 	}
    388 
    389 	if (showall) {
    390 		for (CPU_INFO_FOREACH(cii, ci)) {
    391 			show_cpuinfo(ci);
    392 		}
    393 	} else
    394 #endif /* MULTIPROCESSOR */
    395 		show_cpuinfo(curcpu());
    396 }
    397 
    398 void
    399 db_md_frame_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    400     const char *modif)
    401 {
    402 	struct trapframe *tf;
    403 
    404 	if (!have_addr) {
    405 		db_printf("frame address must be specified\n");
    406 		return;
    407 	}
    408 
    409 	tf = (struct trapframe *)addr;
    410 	dump_trapframe(tf, db_printf);
    411 }
    412 
    413 void
    414 db_md_lwp_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    415     const char *modif)
    416 {
    417 	lwp_t *l, lwp_buf;
    418 	struct pcb *pcb, pcb_buf;
    419 
    420 	if (!have_addr) {
    421 		db_printf("lwp address must be specified\n");
    422 		return;
    423 	}
    424 
    425 	db_read_bytes(addr, sizeof(lwp_buf), (char *)&lwp_buf);
    426 	l = &lwp_buf;
    427 
    428 #define SAFESTRPTR(p)	(((p) == NULL) ? "NULL" : (p))
    429 
    430 	db_printf("lwp=%p\n", (void *)addr);
    431 
    432 	db_printf("\tlwp_getpcb(l)     =%p\n", lwp_getpcb(l));
    433 
    434 	db_printf("\tl->l_md.md_onfault=%p\n", l->l_md.md_onfault);
    435 	db_printf("\tl->l_md.md_utf    =%p\n", l->l_md.md_utf);
    436 	dump_trapframe(l->l_md.md_utf, db_printf);
    437 
    438 	db_read_bytes((db_addr_t)l->l_addr, sizeof(pcb_buf), (char *)&pcb_buf);
    439 	pcb = &pcb_buf;
    440 
    441 	db_printf("\tl->l_addr.pcb_tf    =%p\n", pcb->pcb_tf);
    442 	if (pcb->pcb_tf != l->l_md.md_utf)
    443 		dump_switchframe(pcb->pcb_tf, db_printf);
    444 	db_printf("\tl->l_md.md_cpacr  =%016" PRIx64 "\n", l->l_md.md_cpacr);
    445 	db_printf("\tl->l_md.md_flags  =%08x\n", l->l_md.md_flags);
    446 
    447 	db_printf("\tl->l_cpu          =%p\n", l->l_cpu);
    448 	db_printf("\tl->l_proc         =%p\n", l->l_proc);
    449 	db_printf("\tl->l_private      =%p\n", l->l_private);
    450 	db_printf("\tl->l_name         =%s\n", SAFESTRPTR(l->l_name));
    451 	db_printf("\tl->l_wmesg        =%s\n", SAFESTRPTR(l->l_wmesg));
    452 }
    453 
    454 static void
    455 db_par_print(uint64_t par, vaddr_t va)
    456 {
    457 	paddr_t pa = (__SHIFTOUT(par, PAR_PA) << PAR_PA_SHIFT) +
    458 	    (va & __BITS(PAR_PA_SHIFT - 1, 0));
    459 
    460 	if (__SHIFTOUT(par, PAR_F) == 0) {
    461 		db_printf("%016" PRIx64
    462 		    ": ATTR=0x%02" __PRIxBITS
    463 		    ", NS=%" __PRIuBITS
    464 		    ", SH=%" __PRIuBITS
    465 		    ", PA=%016" PRIxPADDR
    466 		    " (no fault)\n",
    467 		    par,
    468 		    __SHIFTOUT(par, PAR_ATTR),
    469 		    __SHIFTOUT(par, PAR_NS),
    470 		    __SHIFTOUT(par, PAR_SH),
    471 		    pa);
    472 	} else {
    473 		db_printf("%016" PRIx64
    474 		    ", S=%" __PRIuBITS
    475 		    ", PTW=%" __PRIuBITS
    476 		    ", FST=%" __PRIuBITS
    477 		    " (fault)\n",
    478 		    par,
    479 		    __SHIFTOUT(par, PAR_S),
    480 		    __SHIFTOUT(par, PAR_PTW),
    481 		    __SHIFTOUT(par, PAR_FST));
    482 	}
    483 }
    484 
    485 void
    486 db_md_pte_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    487     const char *modif)
    488 {
    489 	uint64_t par;
    490 
    491 	if (!have_addr) {
    492 		db_printf("pte address must be specified\n");
    493 		return;
    494 	}
    495 
    496 	reg_s1e0r_write(addr);
    497 	isb();
    498 	par = reg_par_el1_read();
    499 	db_printf("Stage1 EL0 translation %016llx -> PAR_EL1 = ", addr);
    500 	db_par_print(par, addr);
    501 
    502 	reg_s1e1r_write(addr);
    503 	isb();
    504 	par = reg_par_el1_read();
    505 	db_printf("Stage1 EL1 translation %016llx -> PAR_EL1 = ", addr);
    506 	db_par_print(par, addr);
    507 
    508 	db_pteinfo(addr, db_printf);
    509 }
    510 
    511 void
    512 db_md_reset_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    513     const char *modif)
    514 {
    515 	if (cpu_reset_address == NULL) {
    516 		db_printf("cpu_reset_address is not set\n");
    517 		return;
    518 	}
    519 
    520 	cpu_reset_address();
    521 }
    522 
    523 void
    524 db_md_tlbi_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    525     const char *modif)
    526 {
    527 	aarch64_tlbi_all();
    528 }
    529 
    530 void
    531 db_md_ttbr_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    532     const char *modif)
    533 {
    534 	bool countmode = false, by_pid = true;
    535 
    536 	if (!have_addr) {
    537 		db_printf("usage: machine ttbr [/a] [/p] [/c] address|pid\n");
    538 		db_printf("\t/a == argument is an address of any pmap_t\n");
    539 		db_printf("\t/p == argument is a pid [default]\n");
    540 		db_printf("\t/c == count TLB entries\n");
    541 		return;
    542 	}
    543 
    544 	if (modif != NULL) {
    545 		for (; *modif != '\0'; modif++) {
    546 			switch (*modif) {
    547 			case 'c':
    548 				countmode = true;
    549 				break;
    550 			case 'a':
    551 				by_pid = false;
    552 				break;
    553 			case 'p':
    554 				by_pid = true;
    555 				break;
    556 			}
    557 		}
    558 	}
    559 
    560 	if (by_pid) {
    561 		proc_t *p = db_proc_find((pid_t)addr);
    562 		if (p == NULL) {
    563 			db_printf("bad address\n");
    564 			return;
    565 		}
    566 		addr = (db_addr_t)p->p_vmspace->vm_map.pmap;
    567 	}
    568 
    569 	db_ttbrdump(countmode, addr, db_printf);
    570 }
    571 
    572 void
    573 db_md_sysreg_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    574     const char *modif)
    575 {
    576 #define SHOW_ARMREG(x)	\
    577 	db_printf("%-16s = %016" PRIx64 "\n", #x, reg_ ## x ## _read())
    578 
    579 //	SHOW_ARMREG(cbar_el1);	/* Cortex */
    580 	SHOW_ARMREG(ccsidr_el1);
    581 	SHOW_ARMREG(clidr_el1);
    582 	SHOW_ARMREG(cntfrq_el0);
    583 	SHOW_ARMREG(cntkctl_el1);
    584 	SHOW_ARMREG(cntp_ctl_el0);
    585 	SHOW_ARMREG(cntp_cval_el0);
    586 	SHOW_ARMREG(cntp_tval_el0);
    587 	SHOW_ARMREG(cntpct_el0);
    588 //	SHOW_ARMREG(cntps_ctl_el1);	/* need secure state */
    589 //	SHOW_ARMREG(cntps_cval_el1);	/* need secure state */
    590 //	SHOW_ARMREG(cntps_tval_el1);	/* need secure state */
    591 	SHOW_ARMREG(cntv_ctl_el0);
    592 	SHOW_ARMREG(cntv_ctl_el0);
    593 	SHOW_ARMREG(cntv_cval_el0);
    594 	SHOW_ARMREG(cntv_tval_el0);
    595 	SHOW_ARMREG(cntv_tval_el0);
    596 	SHOW_ARMREG(cntvct_el0);
    597 	SHOW_ARMREG(cpacr_el1);
    598 	SHOW_ARMREG(csselr_el1);
    599 	SHOW_ARMREG(ctr_el0);
    600 	SHOW_ARMREG(currentel);
    601 	SHOW_ARMREG(daif);
    602 	SHOW_ARMREG(dczid_el0);
    603 	SHOW_ARMREG(elr_el1);
    604 	SHOW_ARMREG(esr_el1);
    605 	SHOW_ARMREG(far_el1);
    606 //	SHOW_ARMREG(fpcr);	/* FP trap */
    607 //	SHOW_ARMREG(fpsr);	/* FP trap */
    608 	SHOW_ARMREG(id_aa64afr0_el1);
    609 	SHOW_ARMREG(id_aa64afr1_el1);
    610 	SHOW_ARMREG(id_aa64dfr0_el1);
    611 	SHOW_ARMREG(id_aa64dfr1_el1);
    612 	SHOW_ARMREG(id_aa64isar0_el1);
    613 	SHOW_ARMREG(id_aa64isar1_el1);
    614 	SHOW_ARMREG(id_aa64mmfr0_el1);
    615 	SHOW_ARMREG(id_aa64mmfr1_el1);
    616 	SHOW_ARMREG(id_aa64pfr0_el1);
    617 	SHOW_ARMREG(id_aa64pfr1_el1);
    618 	SHOW_ARMREG(isr_el1);
    619 //	SHOW_ARMREG(l2ctlr_el1);	/* Cortex */
    620 	SHOW_ARMREG(mair_el1);
    621 	SHOW_ARMREG(mdscr_el1);
    622 	SHOW_ARMREG(midr_el1);
    623 	SHOW_ARMREG(mpidr_el1);
    624 	SHOW_ARMREG(mvfr0_el1);
    625 	SHOW_ARMREG(mvfr1_el1);
    626 	SHOW_ARMREG(mvfr2_el1);
    627 	SHOW_ARMREG(nzcv);
    628 	SHOW_ARMREG(par_el1);
    629 	SHOW_ARMREG(pmccfiltr_el0);
    630 	SHOW_ARMREG(pmccntr_el0);
    631 	SHOW_ARMREG(revidr_el1);
    632 //	SHOW_ARMREG(rmr_el1);		/* unknown reason trap */
    633 //	SHOW_ARMREG(rvbar_el1);
    634 	SHOW_ARMREG(sctlr_el1);
    635 	SHOW_ARMREG(spsel);
    636 	SHOW_ARMREG(spsr_el1);
    637 	SHOW_ARMREG(tcr_el1);
    638 	SHOW_ARMREG(tpidr_el0);
    639 	SHOW_ARMREG(tpidrro_el0);
    640 	SHOW_ARMREG(tpidr_el1);
    641 	SHOW_ARMREG(ttbr0_el1);
    642 	SHOW_ARMREG(ttbr1_el1);
    643 	SHOW_ARMREG(vbar_el1);
    644 }
    645 
    646 /*
    647  * hardware breakpoint/watchpoint command
    648  */
    649 static void
    650 aarch64_set_bcr_bvr(int n, uint64_t bcr, uint64_t bvr)
    651 {
    652 #define DBG_BCR_BVR_SET(regno, bcr, bvr)			\
    653 	do {							\
    654 		reg_dbgbcr ## regno ## _el1_write(bcr);		\
    655 		reg_dbgbvr ## regno ## _el1_write(bvr);		\
    656 	} while (0 /* CONSTCOND */)
    657 
    658 	switch (n) {
    659 	case 0:		DBG_BCR_BVR_SET(0,  bcr, bvr);	break;
    660 	case 1:		DBG_BCR_BVR_SET(1,  bcr, bvr);	break;
    661 	case 2:		DBG_BCR_BVR_SET(2,  bcr, bvr);	break;
    662 	case 3:		DBG_BCR_BVR_SET(3,  bcr, bvr);	break;
    663 	case 4:		DBG_BCR_BVR_SET(4,  bcr, bvr);	break;
    664 	case 5:		DBG_BCR_BVR_SET(5,  bcr, bvr);	break;
    665 	case 6:		DBG_BCR_BVR_SET(6,  bcr, bvr);	break;
    666 	case 7:		DBG_BCR_BVR_SET(7,  bcr, bvr);	break;
    667 	case 8:		DBG_BCR_BVR_SET(8,  bcr, bvr);	break;
    668 	case 9:		DBG_BCR_BVR_SET(9,  bcr, bvr);	break;
    669 	case 10:	DBG_BCR_BVR_SET(10, bcr, bvr);	break;
    670 	case 11:	DBG_BCR_BVR_SET(11, bcr, bvr);	break;
    671 	case 12:	DBG_BCR_BVR_SET(12, bcr, bvr);	break;
    672 	case 13:	DBG_BCR_BVR_SET(13, bcr, bvr);	break;
    673 	case 14:	DBG_BCR_BVR_SET(14, bcr, bvr);	break;
    674 	case 15:	DBG_BCR_BVR_SET(15, bcr, bvr);	break;
    675 	}
    676 }
    677 
    678 static void
    679 aarch64_set_wcr_wvr(int n, uint64_t wcr, uint64_t wvr)
    680 {
    681 #define DBG_WCR_WVR_SET(regno, wcr, wvr)			\
    682 	do {							\
    683 		reg_dbgwcr ## regno ## _el1_write(wcr);		\
    684 		reg_dbgwvr ## regno ## _el1_write(wvr);		\
    685 	} while (0 /* CONSTCOND */)
    686 
    687 	switch (n) {
    688 	case 0:		DBG_WCR_WVR_SET(0,  wcr, wvr);	break;
    689 	case 1:		DBG_WCR_WVR_SET(1,  wcr, wvr);	break;
    690 	case 2:		DBG_WCR_WVR_SET(2,  wcr, wvr);	break;
    691 	case 3:		DBG_WCR_WVR_SET(3,  wcr, wvr);	break;
    692 	case 4:		DBG_WCR_WVR_SET(4,  wcr, wvr);	break;
    693 	case 5:		DBG_WCR_WVR_SET(5,  wcr, wvr);	break;
    694 	case 6:		DBG_WCR_WVR_SET(6,  wcr, wvr);	break;
    695 	case 7:		DBG_WCR_WVR_SET(7,  wcr, wvr);	break;
    696 	case 8:		DBG_WCR_WVR_SET(8,  wcr, wvr);	break;
    697 	case 9:		DBG_WCR_WVR_SET(9,  wcr, wvr);	break;
    698 	case 10:	DBG_WCR_WVR_SET(10, wcr, wvr);	break;
    699 	case 11:	DBG_WCR_WVR_SET(11, wcr, wvr);	break;
    700 	case 12:	DBG_WCR_WVR_SET(12, wcr, wvr);	break;
    701 	case 13:	DBG_WCR_WVR_SET(13, wcr, wvr);	break;
    702 	case 14:	DBG_WCR_WVR_SET(14, wcr, wvr);	break;
    703 	case 15:	DBG_WCR_WVR_SET(15, wcr, wvr);	break;
    704 	}
    705 }
    706 
    707 void
    708 aarch64_breakpoint_set(int n, vaddr_t addr)
    709 {
    710 	uint64_t bcr, bvr;
    711 
    712 	if (addr == 0) {
    713 		bvr = 0;
    714 		bcr = 0;
    715 	} else {
    716 		bvr = addr & DBGBVR_MASK;
    717 		bcr =
    718 		    __SHIFTIN(0, DBGBCR_BT) |
    719 		    __SHIFTIN(0, DBGBCR_LBN) |
    720 		    __SHIFTIN(0, DBGBCR_SSC) |
    721 		    __SHIFTIN(0, DBGBCR_HMC) |
    722 		    __SHIFTIN(15, DBGBCR_BAS) |
    723 		    __SHIFTIN(3, DBGBCR_PMC) |
    724 		    __SHIFTIN(1, DBGBCR_E);
    725 	}
    726 
    727 	aarch64_set_bcr_bvr(n, bcr, bvr);
    728 }
    729 
    730 void
    731 aarch64_watchpoint_set(int n, vaddr_t addr, u_int size, u_int accesstype)
    732 {
    733 	uint64_t wvr, wcr;
    734 	uint32_t matchbytebit;
    735 
    736 	KASSERT(size <= 8);
    737 	if (size > 8)
    738 		size = 8;
    739 
    740 	/*
    741 	 * It is always watched in 8byte units, and
    742 	 * BAS is a bit field of byte offset in 8byte units.
    743 	 */
    744 	matchbytebit = 0xff >> (8 - size);
    745 	matchbytebit <<= (addr & 7);
    746 	addr &= ~7UL;
    747 
    748 	/* load, store, or both */
    749 	accesstype &= WATCHPOINT_ACCESS_MASK;
    750 	if (accesstype == 0)
    751 		accesstype = WATCHPOINT_ACCESS_LOADSTORE;
    752 
    753 	if (addr == 0) {
    754 		wvr = 0;
    755 		wcr = 0;
    756 	} else {
    757 		wvr = addr;
    758 		wcr =
    759 		    __SHIFTIN(0, DBGWCR_MASK) |		/* MASK: no mask */
    760 		    __SHIFTIN(0, DBGWCR_WT) |		/* WT: 0 */
    761 		    __SHIFTIN(0, DBGWCR_LBN) |		/* LBN: 0 */
    762 		    __SHIFTIN(0, DBGWCR_SSC) |		/* SSC: 00 */
    763 		    __SHIFTIN(0, DBGWCR_HMC) |		/* HMC: 0 */
    764 		    __SHIFTIN(matchbytebit, DBGWCR_BAS) | /* BAS: 0-8byte */
    765 		    __SHIFTIN(accesstype, DBGWCR_LSC) |	/* LSC: Load/Store */
    766 		    __SHIFTIN(3, DBGWCR_PAC) |		/* PAC: 11 */
    767 		    __SHIFTIN(1, DBGWCR_E);		/* Enable */
    768 	}
    769 
    770 	aarch64_set_wcr_wvr(n, wcr, wvr);
    771 }
    772 
    773 static int
    774 db_md_breakpoint_set(int n, vaddr_t addr)
    775 {
    776 	if (n >= __arraycount(breakpoint_buf))
    777 		return -1;
    778 
    779 	if ((addr & 3) != 0) {
    780 		db_printf("address must be 4bytes aligned\n");
    781 		return -1;
    782 	}
    783 
    784 	breakpoint_buf[n].addr = addr;
    785 	return 0;
    786 }
    787 
    788 static int
    789 db_md_watchpoint_set(int n, vaddr_t addr, u_int size, u_int accesstype)
    790 {
    791 	if (n >= __arraycount(watchpoint_buf))
    792 		return -1;
    793 
    794 	if (size != 0 && ((addr) & ~7UL) != ((addr + size - 1) & ~7UL)) {
    795 		db_printf(
    796 		    "address and size must fit within a block of 8bytes\n");
    797 		return -1;
    798 	}
    799 
    800 	watchpoint_buf[n].addr = addr;
    801 	watchpoint_buf[n].size = size;
    802 	watchpoint_buf[n].accesstype = accesstype;
    803 	return 0;
    804 }
    805 
    806 static void
    807 db_md_breakwatchpoints_clear(void)
    808 {
    809 	int i;
    810 
    811 	for (i = 0; i <= max_breakpoint; i++)
    812 		aarch64_breakpoint_set(i, 0);
    813 	for (i = 0; i <= max_watchpoint; i++)
    814 		aarch64_watchpoint_set(i, 0, 0, 0);
    815 }
    816 
    817 static void
    818 db_md_breakwatchpoints_reload(void)
    819 {
    820 	int i;
    821 
    822 	for (i = 0; i <= max_breakpoint; i++) {
    823 		aarch64_breakpoint_set(i,
    824 		    breakpoint_buf[i].addr);
    825 	}
    826 	for (i = 0; i <= max_watchpoint; i++) {
    827 		aarch64_watchpoint_set(i,
    828 		    watchpoint_buf[i].addr,
    829 		    watchpoint_buf[i].size,
    830 		    watchpoint_buf[i].accesstype);
    831 	}
    832 }
    833 
    834 void
    835 db_machdep_cpu_init(void)
    836 {
    837 	uint64_t dfr, mdscr;
    838 	int i, cpu_max_breakpoint, cpu_max_watchpoint;
    839 
    840 	dfr = reg_id_aa64dfr0_el1_read();
    841 	cpu_max_breakpoint = __SHIFTOUT(dfr, ID_AA64DFR0_EL1_BRPS);
    842 	cpu_max_watchpoint = __SHIFTOUT(dfr, ID_AA64DFR0_EL1_WRPS);
    843 
    844 	for (i = 0; i <= cpu_max_breakpoint; i++) {
    845 		/* clear all breakpoints */
    846 		aarch64_breakpoint_set(i, 0);
    847 	}
    848 	for (i = 0; i <= cpu_max_watchpoint; i++) {
    849 		/* clear all watchpoints */
    850 		aarch64_watchpoint_set(i, 0, 0, 0);
    851 	}
    852 
    853 	/* enable watchpoint and breakpoint */
    854 	mdscr = reg_mdscr_el1_read();
    855 	mdscr |= MDSCR_MDE | MDSCR_KDE;
    856 	reg_mdscr_el1_write(mdscr);
    857 	reg_oslar_el1_write(0);
    858 }
    859 
    860 void
    861 db_machdep_init(struct cpu_info * const ci)
    862 {
    863 	struct aarch64_sysctl_cpu_id * const id = &ci->ci_id;
    864 	const uint64_t dfr = id->ac_aa64dfr0;
    865 	const u_int cpu_max_breakpoint = __SHIFTOUT(dfr, ID_AA64DFR0_EL1_BRPS);
    866 	const u_int cpu_max_watchpoint = __SHIFTOUT(dfr, ID_AA64DFR0_EL1_WRPS);
    867 
    868 	/*
    869 	 * num of {watch,break}point may be different depending on the
    870 	 * core.
    871 	 */
    872 	if (max_breakpoint > cpu_max_breakpoint)
    873 		max_breakpoint = cpu_max_breakpoint;
    874 	if (max_watchpoint > cpu_max_watchpoint)
    875 		max_watchpoint = cpu_max_watchpoint;
    876 }
    877 
    878 
    879 static void
    880 show_breakpoints(void)
    881 {
    882 	uint64_t addr;
    883 	unsigned int i, nused;
    884 
    885 	for (nused = 0, i = 0; i <= max_breakpoint; i++) {
    886 		addr = breakpoint_buf[i].addr;
    887 		if (addr == 0) {
    888 			db_printf("%d: disabled\n", i);
    889 		} else {
    890 			db_printf("%d: breakpoint %016" PRIx64 " (", i,
    891 			    addr);
    892 			db_printsym(addr, DB_STGY_ANY, db_printf);
    893 			db_printf(")\n");
    894 			nused++;
    895 		}
    896 	}
    897 	db_printf("breakpoint used %d/%d\n", nused, max_breakpoint + 1);
    898 }
    899 
    900 static void
    901 show_watchpoints(void)
    902 {
    903 	uint64_t addr;
    904 	unsigned int i, nused;
    905 
    906 	for (nused = 0, i = 0; i <= max_watchpoint; i++) {
    907 		addr = watchpoint_buf[i].addr;
    908 		if (addr == 0) {
    909 			db_printf("%d: disabled\n", i);
    910 		} else {
    911 			db_printf("%d: watching %016" PRIx64 " (", i,
    912 			    addr);
    913 			db_printsym(addr, DB_STGY_ANY, db_printf);
    914 			db_printf("), %d bytes", watchpoint_buf[i].size);
    915 
    916 			switch (watchpoint_buf[i].accesstype) {
    917 			case WATCHPOINT_ACCESS_LOAD:
    918 				db_printf(", load");
    919 				break;
    920 			case WATCHPOINT_ACCESS_STORE:
    921 				db_printf(", store");
    922 				break;
    923 			case WATCHPOINT_ACCESS_LOADSTORE:
    924 				db_printf(", load/store");
    925 				break;
    926 			}
    927 			db_printf("\n");
    928 			nused++;
    929 		}
    930 	}
    931 	db_printf("watchpoint used %d/%d\n", nused, max_watchpoint + 1);
    932 }
    933 
    934 void
    935 db_md_break_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    936     const char *modif)
    937 {
    938 	int i, rc;
    939 	int added, cleared;
    940 
    941 	if (!have_addr) {
    942 		show_breakpoints();
    943 		return;
    944 	}
    945 
    946 	added = -1;
    947 	cleared = -1;
    948 	if (0 <= addr && addr <= max_breakpoint) {
    949 		i = addr;
    950 		if (breakpoint_buf[i].addr != 0) {
    951 			db_md_breakpoint_set(i, 0);
    952 			cleared = i;
    953 		}
    954 	} else {
    955 		for (i = 0; i <= max_breakpoint; i++) {
    956 			if (breakpoint_buf[i].addr == addr) {
    957 				db_md_breakpoint_set(i, 0);
    958 				cleared = i;
    959 			}
    960 		}
    961 		if (cleared == -1) {
    962 			for (i = 0; i <= max_breakpoint; i++) {
    963 				if (breakpoint_buf[i].addr == 0) {
    964 					rc = db_md_breakpoint_set(i, addr);
    965 					if (rc != 0)
    966 						return;
    967 					added = i;
    968 					break;
    969 				}
    970 			}
    971 			if (i > max_breakpoint) {
    972 				db_printf("no more available breakpoint\n");
    973 			}
    974 		}
    975 	}
    976 
    977 	if (added >= 0)
    978 		db_printf("add breakpoint %d as %016"DDB_EXPR_FMT"x\n",
    979 		    added, addr);
    980 	if (cleared >= 0)
    981 		db_printf("clear breakpoint %d\n", cleared);
    982 
    983 	show_breakpoints();
    984 }
    985 
    986 void
    987 db_md_watch_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
    988     const char *modif)
    989 {
    990 	int i, rc;
    991 	int added, cleared;
    992 	u_int accesstype, watchsize;
    993 
    994 	if (!have_addr) {
    995 		show_watchpoints();
    996 		return;
    997 	}
    998 
    999 	accesstype = watchsize = 0;
   1000 	if ((modif != NULL) && (*modif != '\0')) {
   1001 		int ch;
   1002 		for (; *modif != '\0'; modif++) {
   1003 			ch = *modif;
   1004 
   1005 			switch (ch) {
   1006 			case 'b':
   1007 				watchsize = 1;
   1008 				break;
   1009 			case 'h':
   1010 				watchsize = 2;
   1011 				break;
   1012 			case 'l':
   1013 				watchsize = 4;
   1014 				break;
   1015 			case 'q':
   1016 				watchsize = 8;
   1017 				break;
   1018 			case 'r':
   1019 				accesstype |= WATCHPOINT_ACCESS_LOAD;
   1020 				break;
   1021 			case 'w':
   1022 				accesstype |= WATCHPOINT_ACCESS_STORE;
   1023 				break;
   1024 			}
   1025 		}
   1026 	}
   1027 	if (watchsize == 0)
   1028 		watchsize = 4;	/* default: 4byte */
   1029 	if (accesstype == 0)
   1030 		accesstype = WATCHPOINT_ACCESS_LOADSTORE; /* default */
   1031 
   1032 	added = -1;
   1033 	cleared = -1;
   1034 	if (0 <= addr && addr <= max_watchpoint) {
   1035 		i = addr;
   1036 		if (watchpoint_buf[i].addr != 0) {
   1037 			db_md_watchpoint_set(i, 0, 0, 0);
   1038 			cleared = i;
   1039 		}
   1040 	} else {
   1041 		for (i = 0; i <= max_watchpoint; i++) {
   1042 			if (watchpoint_buf[i].addr == addr) {
   1043 				db_md_watchpoint_set(i, 0, 0, 0);
   1044 				cleared = i;
   1045 			}
   1046 		}
   1047 		if (cleared == -1) {
   1048 			for (i = 0; i <= max_watchpoint; i++) {
   1049 				if (watchpoint_buf[i].addr == 0) {
   1050 					rc = db_md_watchpoint_set(i, addr,
   1051 					    watchsize, accesstype);
   1052 					if (rc != 0)
   1053 						return;
   1054 					added = i;
   1055 					break;
   1056 				}
   1057 			}
   1058 			if (i > max_watchpoint) {
   1059 				db_printf("no more available watchpoint\n");
   1060 			}
   1061 		}
   1062 	}
   1063 
   1064 	if (added >= 0)
   1065 		db_printf("add watchpoint %d as %016"DDB_EXPR_FMT"x\n",
   1066 		    added, addr);
   1067 	if (cleared >= 0)
   1068 		db_printf("clear watchpoint %d\n", cleared);
   1069 
   1070 	show_watchpoints();
   1071 }
   1072 #endif
   1073 
   1074 #ifdef MULTIPROCESSOR
   1075 volatile struct cpu_info *db_trigger;
   1076 volatile struct cpu_info *db_onproc;
   1077 volatile struct cpu_info *db_newcpu;
   1078 volatile struct trapframe *db_readytoswitch[MAXCPUS];
   1079 
   1080 #ifdef _KERNEL
   1081 void
   1082 db_md_switch_cpu_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
   1083     const char *modif)
   1084 {
   1085 	struct cpu_info *new_ci = NULL;
   1086 	u_int cpuno = (u_int)addr;
   1087 	int i;
   1088 
   1089 	membar_consumer();
   1090 	if (!have_addr) {
   1091 		for (i = 0; i < ncpu; i++) {
   1092 			if (db_readytoswitch[i] != NULL) {
   1093 				db_printf("cpu%d: ready. tf=%p, pc=%016lx ", i,
   1094 				    db_readytoswitch[i],
   1095 				    db_readytoswitch[i]->tf_pc);
   1096 				db_printsym(db_readytoswitch[i]->tf_pc,
   1097 				    DB_STGY_ANY, db_printf);
   1098 				db_printf("\n");
   1099 			} else {
   1100 				db_printf("cpu%d: not responding\n", i);
   1101 			}
   1102 		}
   1103 		return;
   1104 	}
   1105 
   1106 	if (cpuno < ncpu)
   1107 		new_ci = cpu_lookup(cpuno);
   1108 	if (new_ci == NULL) {
   1109 		db_printf("cpu %u does not exist", cpuno);
   1110 		return;
   1111 	}
   1112 	if (db_readytoswitch[new_ci->ci_index] == 0) {
   1113 		db_printf("cpu %u is not responding", cpuno);
   1114 		return;
   1115 	}
   1116 
   1117 	if (new_ci == curcpu())
   1118 		return;
   1119 
   1120 	db_newcpu = new_ci;
   1121 	db_continue_cmd(0, false, 0, "");
   1122 }
   1123 
   1124 #endif /* _KERNEL */
   1125 #endif /* MULTIPROCESSOR */
   1126 
   1127 #ifdef DDB
   1128 int
   1129 kdb_trap(int type, struct trapframe *tf)
   1130 {
   1131 #ifdef MULTIPROCESSOR
   1132 	struct cpu_info * const ci = curcpu();
   1133 	bool static_brk = false;
   1134 #endif
   1135 	int s;
   1136 	bool restore_hw_watchpoints = true;
   1137 
   1138 	switch (type) {
   1139 	case DB_TRAP_WATCHPOINT:
   1140 	case DB_TRAP_BREAKPOINT:
   1141 		/*
   1142 		 * In the case of a hardware watchpoint or breakpoint,
   1143 		 * even if cpu return from ddb as is, it will be trapped again,
   1144 		 * so clear it all once.
   1145 		 *
   1146 		 * breakpoint and watchpoint will be restored at the end of
   1147 		 * next DB_TRAP_BKPT_INSN (ddb's STEP_INVISIBLE mode).
   1148 		 */
   1149 		db_md_breakwatchpoints_clear();
   1150 		restore_hw_watchpoints = false;
   1151 		break;
   1152 	case DB_TRAP_BKPT_INSN:
   1153 #ifdef MULTIPROCESSOR
   1154 		/* brk #0xffff in cpu_Debugger() ? */
   1155 		if (__SHIFTOUT(tf->tf_esr, ESR_ISS) == 0xffff)
   1156 			static_brk = true;
   1157 		/* FALLTHRU */
   1158 #endif
   1159 	case DB_TRAP_SW_STEP:
   1160 	case DB_TRAP_UNKNOWN:
   1161 	case -1:	/* from pic_ipi_ddb() */
   1162 		break;
   1163 	default:
   1164 		if (db_recover != 0) {
   1165 			db_error("Faulted in DDB: continuing...\n");
   1166 			/* NOTREACHED */
   1167 		}
   1168 		break;
   1169 	}
   1170 
   1171 #ifdef MULTIPROCESSOR
   1172 	if (ncpu > 1) {
   1173 		/*
   1174 		 * Try to take ownership of DDB.
   1175 		 * If we do, tell all other CPUs to enter DDB too.
   1176 		 */
   1177 		if (atomic_cas_ptr(&db_onproc, NULL, ci) == NULL) {
   1178 			intr_ipi_send(NULL, IPI_DDB);
   1179 			db_trigger = ci;
   1180 		} else {
   1181 			/*
   1182 			 * If multiple CPUs catch kdb_trap() that is not IPI_DDB
   1183 			 * derived at the same time, only the CPU that was able
   1184 			 * to get db_onproc first will execute db_trap.
   1185 			 * The CPU that could not get db_onproc will be set to
   1186 			 * type = -1 once, and kdb_trap will be called again
   1187 			 * with the correct type after kdb_trap returns.
   1188 			 */
   1189 			type = -1;
   1190 			restore_hw_watchpoints = true;
   1191 		}
   1192 	}
   1193 	db_readytoswitch[ci->ci_index] = tf;
   1194 #endif
   1195 
   1196 	for (;;) {
   1197 #ifdef MULTIPROCESSOR
   1198 		if (ncpu > 1) {
   1199 			/* waiting my turn, or exit */
   1200 			dsb(ishld);
   1201 			while (db_onproc != ci) {
   1202 				__asm __volatile ("wfe");
   1203 
   1204 				dsb(ishld);
   1205 				if (db_onproc == NULL)
   1206 					goto kdb_trap_done;
   1207 			}
   1208 			/* It's my turn! */
   1209 		}
   1210 #endif /* MULTIPROCESSOR */
   1211 
   1212 		/* Should switch to kdb`s own stack here. */
   1213 		ddb_regs = *tf;
   1214 
   1215 		s = splhigh();
   1216 		db_active++;
   1217 		cnpollc(true);
   1218 		db_trap(type, 0/*code*/);
   1219 		cnpollc(false);
   1220 		db_active--;
   1221 		splx(s);
   1222 
   1223 		*tf = ddb_regs;
   1224 
   1225 #ifdef MULTIPROCESSOR
   1226 		if (ncpu < 2)
   1227 			break;
   1228 
   1229 		if (db_newcpu == NULL && db_onproc != db_trigger) {
   1230 			/*
   1231 			 * If the "machine cpu" switches CPUs after entering
   1232 			 * ddb from a breakpoint or watchpoint, it will return
   1233 			 * control to the CPU that triggered the ddb in the
   1234 			 * first place in order to correctly reset the
   1235 			 * breakpoint or watchpoint.
   1236 			 * If db_trap() returns further from here,
   1237 			 * watchpoints and breakpoints will be reset.
   1238 			 * (db_run_mode = STEP_INVISIBLE)
   1239 			 */
   1240 			db_newcpu = db_trigger;
   1241 		}
   1242 
   1243 		if (db_newcpu != NULL) {
   1244 			/* XXX: override sys/ddb/db_run.c:db_run_mode */
   1245 			db_continue_cmd(0, false, 0, "");
   1246 
   1247 			/*
   1248 			 * When static BRK instruction (cpu_Debugger()),
   1249 			 * db_trap() advance the $PC to the next instruction of
   1250 			 * BRK. If db_trap() will be called twice by
   1251 			 * "machine cpu" command, the $PC will be advanced to
   1252 			 * the after the next instruction.
   1253 			 * To avoid this, change 'type' so that the second call
   1254 			 * to db_trap() will not change the PC.
   1255 			 */
   1256 			if (static_brk)
   1257 				type = -1;
   1258 
   1259 			db_onproc = db_newcpu;
   1260 			db_newcpu = NULL;
   1261 			dsb(ishst);
   1262 			/* waking up the CPU waiting for its turn to db_trap */
   1263 			sev();
   1264 
   1265 			continue;	/* go to waiting my turn */
   1266 		}
   1267 #endif /* MULTIPROCESSOR */
   1268 
   1269 		break;
   1270 	}
   1271 
   1272 #ifdef MULTIPROCESSOR
   1273 	if (ncpu > 1 && db_onproc == ci) {
   1274 		db_onproc = NULL;
   1275 		dsb(ishst);
   1276 		/* waking up the CPU waiting for its turn to exit */
   1277 		sev();
   1278 
   1279 		db_readytoswitch[cpu_index(ci)] = NULL;
   1280 		/* wait for all other CPUs are ready to exit */
   1281 		for (;;) {
   1282 			int i;
   1283 			dsb(ishld);
   1284 			for (i = 0; i < ncpu; i++) {
   1285 				if (db_readytoswitch[i] != NULL)
   1286 					break;
   1287 			}
   1288 			if (i == ncpu)
   1289 				break;
   1290 		}
   1291 		db_trigger = NULL;
   1292 		sev();
   1293 	} else {
   1294  kdb_trap_done:
   1295 		db_readytoswitch[cpu_index(ci)] = NULL;
   1296 		dsb(ishst);
   1297 		__asm __volatile ("wfe");
   1298 	}
   1299 #endif
   1300 	if (restore_hw_watchpoints)
   1301 		db_md_breakwatchpoints_reload();
   1302 
   1303 	return 1;
   1304 }
   1305 #endif
   1306 
   1307 #if defined(_KERNEL)
   1308 static void
   1309 db_md_meminfo_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
   1310     const char *modif)
   1311 {
   1312 	unsigned blk;
   1313 
   1314 	for (blk = 0; blk < bootconfig.dramblocks; blk++) {
   1315 		db_printf("blk[%u]: start %lx end %lx (pages %x)\n",
   1316 		    blk, bootconfig.dram[blk].address,
   1317 		    bootconfig.dram[blk].address +
   1318 		    (uint64_t)bootconfig.dram[blk].pages * PAGE_SIZE,
   1319 		    bootconfig.dram[blk].pages);
   1320 	}
   1321 }
   1322 #endif
   1323