Home | History | Annotate | Line # | Download | only in riscv
riscv_machdep.c revision 1.35
      1 /*	$NetBSD: riscv_machdep.c,v 1.35 2023/12/22 08:41:59 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014, 2019, 2022 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, and by Nick Hudson.
      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 "opt_ddb.h"
     33 #include "opt_modular.h"
     34 #include "opt_multiprocessor.h"
     35 #include "opt_riscv_debug.h"
     36 
     37 #include <sys/cdefs.h>
     38 __RCSID("$NetBSD: riscv_machdep.c,v 1.35 2023/12/22 08:41:59 skrll Exp $");
     39 
     40 #include <sys/param.h>
     41 
     42 #include <sys/asan.h>
     43 #include <sys/boot_flag.h>
     44 #include <sys/cpu.h>
     45 #include <sys/exec.h>
     46 #include <sys/kmem.h>
     47 #include <sys/ktrace.h>
     48 #include <sys/lwp.h>
     49 #include <sys/module.h>
     50 #include <sys/mount.h>
     51 #include <sys/msgbuf.h>
     52 #include <sys/optstr.h>
     53 #include <sys/proc.h>
     54 #include <sys/reboot.h>
     55 #include <sys/syscall.h>
     56 #include <sys/sysctl.h>
     57 #include <sys/systm.h>
     58 
     59 #include <dev/cons.h>
     60 #include <uvm/uvm_extern.h>
     61 
     62 #include <riscv/frame.h>
     63 #include <riscv/locore.h>
     64 #include <riscv/machdep.h>
     65 #include <riscv/pte.h>
     66 #include <riscv/sbi.h>
     67 
     68 #include <libfdt.h>
     69 #include <dev/fdt/fdtvar.h>
     70 #include <dev/fdt/fdt_boot.h>
     71 #include <dev/fdt/fdt_memory.h>
     72 #include <dev/fdt/fdt_private.h>
     73 
     74 int cpu_printfataltraps = 1;
     75 char machine[] = MACHINE;
     76 char machine_arch[] = MACHINE_ARCH;
     77 
     78 #ifdef VERBOSE_INIT_RISCV
     79 #define	VPRINTF(...)	printf(__VA_ARGS__)
     80 #else
     81 #define	VPRINTF(...)	__nothing
     82 #endif
     83 
     84 /* 64 should be enough, even for a ZFS UUID */
     85 #define	MAX_BOOT_DEV_STR	64
     86 
     87 char bootdevstr[MAX_BOOT_DEV_STR] = "";
     88 char *boot_args = NULL;
     89 
     90 paddr_t physical_start;
     91 paddr_t physical_end;
     92 
     93 static void
     94 earlyconsputc(dev_t dev, int c)
     95 {
     96 	uartputc(c);
     97 }
     98 
     99 static int
    100 earlyconsgetc(dev_t dev)
    101 {
    102 	return uartgetc();
    103 }
    104 
    105 static struct consdev earlycons = {
    106 	.cn_putc = earlyconsputc,
    107 	.cn_getc = earlyconsgetc,
    108 	.cn_pollc = nullcnpollc,
    109 };
    110 
    111 struct vm_map *phys_map;
    112 
    113 struct trapframe cpu_ddb_regs;
    114 const pcu_ops_t * const pcu_ops_md_defs[PCU_UNIT_COUNT] = {
    115 #ifdef FPE
    116 	[PCU_FPU] = &pcu_fpu_ops,
    117 #endif
    118 };
    119 
    120 /*
    121  * Used by PHYSTOV and VTOPHYS -- Will be set be BSS is zeroed so
    122  * keep it in data
    123  */
    124 unsigned long kern_vtopdiff __attribute__((__section__(".data")));
    125 
    126 
    127 /*
    128  * machine dependent system variables.
    129  */
    130 SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
    131 {
    132 	sysctl_createv(clog, 0, NULL, NULL,
    133 	    CTLFLAG_PERMANENT,
    134 	    CTLTYPE_NODE, "machdep", NULL,
    135 	    NULL, 0, NULL, 0,
    136 	    CTL_MACHDEP, CTL_EOL);
    137 }
    138 
    139 void
    140 delay(unsigned long us)
    141 {
    142 	const uint32_t cycles_per_us = curcpu()->ci_data.cpu_cc_freq / 1000000;
    143 	const uint64_t cycles = (uint64_t)us * cycles_per_us;
    144 	const uint64_t finish = csr_cycle_read() + cycles;
    145 
    146 	while (csr_cycle_read() < finish) {
    147 		/* spin, baby spin */
    148 	}
    149 }
    150 
    151 #ifdef MODULAR
    152 /*
    153  * Push any modules loaded by the boot loader.
    154  */
    155 void
    156 module_init_md(void)
    157 {
    158 }
    159 #endif /* MODULAR */
    160 
    161 /*
    162  * Set registers on exec.
    163  * Clear all registers except sp, pc.
    164  * sp is set to the stack pointer passed in.  pc is set to the entry
    165  * point given by the exec_package passed in.
    166  */
    167 void
    168 setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
    169 {
    170 	struct trapframe * const tf = l->l_md.md_utf;
    171 	struct proc * const p = l->l_proc;
    172 
    173 	memset(tf, 0, sizeof(*tf));
    174 	tf->tf_sp = (intptr_t)stack_align(stack);
    175 	tf->tf_pc = (intptr_t)pack->ep_entry & ~1;
    176 #ifdef _LP64
    177 	tf->tf_sr = (p->p_flag & PK_32) ? SR_USER32 : SR_USER64;
    178 #else
    179 	tf->tf_sr = SR_USER;
    180 #endif
    181 
    182 	// Set up arguments for ___start(cleanup, ps_strings)
    183 	tf->tf_a0 = 0;			// cleanup
    184 	tf->tf_a1 = p->p_psstrp;	// ps_strings
    185 
    186 	/*
    187 	 * Must have interrupts disabled for exception return.
    188 	 * Must be switching to user mode.
    189 	 * Must enable interrupts after sret.
    190 	 */
    191 	KASSERT(__SHIFTOUT(tf->tf_sr, SR_SIE) == 0);
    192 	KASSERT(__SHIFTOUT(tf->tf_sr, SR_SPP) == 0);
    193 	KASSERT(__SHIFTOUT(tf->tf_sr, SR_SPIE) != 0);
    194 }
    195 
    196 void
    197 md_child_return(struct lwp *l)
    198 {
    199 	struct trapframe * const tf = lwp_trapframe(l);
    200 
    201 	tf->tf_a0 = 0;
    202 	tf->tf_a1 = 1;
    203 #ifdef FPE
    204 	/* Disable FP as we can't be using it (yet). */
    205 	tf->tf_sr &= ~SR_FS;
    206 #endif
    207 
    208 	/*
    209 	 * Must have interrupts disabled for exception return.
    210 	 * Must be switching to user mode.
    211 	 * Must enable interrupts after sret.
    212 	 */
    213 
    214 	KASSERT(__SHIFTOUT(tf->tf_sr, SR_SIE) == 0);
    215 	KASSERT(__SHIFTOUT(tf->tf_sr, SR_SPP) == 0);
    216 	KASSERT(__SHIFTOUT(tf->tf_sr, SR_SPIE) != 0);
    217 
    218 	userret(l);
    219 }
    220 
    221 void
    222 cpu_spawn_return(struct lwp *l)
    223 {
    224 	userret(l);
    225 }
    226 
    227 /*
    228  * Start a new LWP
    229  */
    230 void
    231 startlwp(void *arg)
    232 {
    233 	ucontext_t * const uc = arg;
    234 	lwp_t * const l = curlwp;
    235 	int error __diagused;
    236 
    237 	error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
    238 	KASSERT(error == 0);
    239 
    240 	kmem_free(uc, sizeof(*uc));
    241 	userret(l);
    242 }
    243 
    244 // We've worked hard to make sure struct reg and __gregset_t are the same.
    245 // Ditto for struct fpreg and fregset_t.
    246 
    247 #ifdef _LP64
    248 CTASSERT(sizeof(struct reg) == sizeof(__gregset_t));
    249 #endif
    250 CTASSERT(sizeof(struct fpreg) == sizeof(__fregset_t));
    251 
    252 void
    253 cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
    254 {
    255 	const struct trapframe * const tf = l->l_md.md_utf;
    256 
    257 	/* Save register context. */
    258 	*(struct reg *)mcp->__gregs = tf->tf_regs;
    259 
    260 	*flags |= _UC_CPU | _UC_TLSBASE;
    261 
    262 	/* Save floating point register context, if any. */
    263 	KASSERT(l == curlwp);
    264 	if (fpu_valid_p(l)) {
    265 		/*
    266 		 * If this process is the current FP owner, dump its
    267 		 * context to the PCB first.
    268 		 */
    269 		fpu_save(l);
    270 
    271 		struct pcb * const pcb = lwp_getpcb(l);
    272 		*(struct fpreg *)mcp->__fregs = pcb->pcb_fpregs;
    273 		*flags |= _UC_FPU;
    274 	}
    275 }
    276 
    277 int
    278 cpu_mcontext_validate(struct lwp *l, const mcontext_t *mcp)
    279 {
    280 	/*
    281 	 * Verify that at least the PC and SP are user addresses.
    282 	 */
    283 	if ((intptr_t) mcp->__gregs[_REG_PC] < 0
    284 	    || (intptr_t) mcp->__gregs[_REG_SP] < 0
    285 	    || (mcp->__gregs[_REG_PC] & 1))
    286 		return EINVAL;
    287 
    288 	return 0;
    289 }
    290 
    291 int
    292 cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
    293 {
    294 	struct trapframe * const tf = l->l_md.md_utf;
    295 	struct proc * const p = l->l_proc;
    296 	const __greg_t * const gr = mcp->__gregs;
    297 	int error;
    298 
    299 	/* Restore register context, if any. */
    300 	if (flags & _UC_CPU) {
    301 		error = cpu_mcontext_validate(l, mcp);
    302 		if (error)
    303 			return error;
    304 
    305 		/*
    306 		 * Avoid updating TLS register here.
    307 		 */
    308 		const __greg_t saved_tp = tf->tf_reg[_REG_TP];
    309 		tf->tf_regs = *(const struct reg *)gr;
    310 		tf->tf_reg[_REG_TP] = saved_tp;
    311 	}
    312 
    313 	/* Restore the private thread context */
    314 	if (flags & _UC_TLSBASE) {
    315 		lwp_setprivate(l, (void *)(intptr_t)mcp->__gregs[_X_TP]);
    316 	}
    317 
    318 	/* Restore floating point register context, if any. */
    319 	if (flags & _UC_FPU) {
    320 		KASSERT(l == curlwp);
    321 		/* Tell PCU we are replacing the FPU contents. */
    322 		fpu_replace(l);
    323 
    324 		/*
    325 		 * The PCB FP regs struct includes the FP CSR, so use the
    326 		 * proper size of fpreg when copying.
    327 		 */
    328 		struct pcb * const pcb = lwp_getpcb(l);
    329 		pcb->pcb_fpregs = *(const struct fpreg *)mcp->__fregs;
    330 	}
    331 
    332 	mutex_enter(p->p_lock);
    333 	if (flags & _UC_SETSTACK)
    334 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    335 	if (flags & _UC_CLRSTACK)
    336 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    337 	mutex_exit(p->p_lock);
    338 
    339 	return 0;
    340 }
    341 
    342 void
    343 cpu_need_resched(struct cpu_info *ci, struct lwp *l, int flags)
    344 {
    345 	KASSERT(kpreempt_disabled());
    346 
    347 	if ((flags & RESCHED_KPREEMPT) != 0) {
    348 #ifdef __HAVE_PREEMPTION
    349 		if ((flags & RESCHED_REMOTE) != 0) {
    350 			cpu_send_ipi(ci, IPI_KPREEMPT);
    351 		} else {
    352 			softint_trigger(SOFTINT_KPREEMPT);
    353 		}
    354 #endif
    355 		return;
    356 	}
    357 	if ((flags & RESCHED_REMOTE) != 0) {
    358 #ifdef MULTIPROCESSOR
    359 		cpu_send_ipi(ci, IPI_AST);
    360 #endif
    361 	} else {
    362 		l->l_md.md_astpending = 1;	/* force call to ast() */
    363 	}
    364 }
    365 
    366 void
    367 cpu_signotify(struct lwp *l)
    368 {
    369 	KASSERT(kpreempt_disabled());
    370 #ifdef __HAVE_FAST_SOFTINTS
    371 	KASSERT(lwp_locked(l, NULL));
    372 #endif
    373 
    374 	if (l->l_cpu != curcpu()) {
    375 #ifdef MULTIPROCESSOR
    376 		cpu_send_ipi(l->l_cpu, IPI_AST);
    377 #endif
    378 	} else {
    379 		l->l_md.md_astpending = 1; 	/* force call to ast() */
    380 	}
    381 }
    382 
    383 void
    384 cpu_need_proftick(struct lwp *l)
    385 {
    386 	KASSERT(kpreempt_disabled());
    387 	KASSERT(l->l_cpu == curcpu());
    388 
    389 	l->l_pflag |= LP_OWEUPC;
    390 	l->l_md.md_astpending = 1;		/* force call to ast() */
    391 }
    392 
    393 
    394 /* Sync the discs, unmount the filesystems, and adjust the todr */
    395 static void
    396 bootsync(void)
    397 {
    398 	static bool bootsyncdone = false;
    399 
    400 	if (bootsyncdone)
    401 		return;
    402 
    403 	bootsyncdone = true;
    404 
    405 	/* Make sure we can still manage to do things */
    406 	if ((csr_sstatus_read() & SR_SIE) == 0) {
    407 		/*
    408 		 * If we get here then boot has been called without RB_NOSYNC
    409 		 * and interrupts were disabled. This means the boot() call
    410 		 * did not come from a user process e.g. shutdown, but must
    411 		 * have come from somewhere in the kernel.
    412 		 */
    413 		ENABLE_INTERRUPTS();
    414 		printf("Warning interrupts disabled during boot()\n");
    415 	}
    416 
    417 	vfs_shutdown();
    418 
    419 	resettodr();
    420 }
    421 
    422 
    423 void
    424 cpu_reboot(int howto, char *bootstr)
    425 {
    426 
    427 	/*
    428 	 * If RB_NOSYNC was not specified sync the discs.
    429 	 * Note: Unless cold is set to 1 here, syslogd will die during the
    430 	 * unmount.  It looks like syslogd is getting woken up only to find
    431 	 * that it cannot page part of the binary in as the filesystem has
    432 	 * been unmounted.
    433 	 */
    434 	if ((howto & RB_NOSYNC) == 0)
    435 		bootsync();
    436 
    437 #if 0
    438 	/* Disable interrupts. */
    439 	const int s = splhigh();
    440 
    441 	/* Do a dump if requested. */
    442 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
    443 		dumpsys();
    444 
    445 	splx(s);
    446 #endif
    447 
    448 	pmf_system_shutdown(boothowto);
    449 
    450 	/* Say NO to interrupts for good */
    451 	splhigh();
    452 
    453 	/* Run any shutdown hooks */
    454 	doshutdownhooks();
    455 
    456 	/* Make sure IRQ's are disabled */
    457 	DISABLE_INTERRUPTS();
    458 
    459 	if (howto & RB_HALT) {
    460 		printf("\n");
    461 		printf("The operating system has halted.\n");
    462 		printf("Please press any key to reboot.\n\n");
    463 		cnpollc(true);	/* for proper keyboard command handling */
    464 		if (cngetc() == 0) {
    465 			/* no console attached, so just hlt */
    466 			printf("No keyboard - cannot reboot after all.\n");
    467 			goto spin;
    468 		}
    469 		cnpollc(false);
    470 	}
    471 
    472 	printf("rebooting...\n");
    473 
    474 	sbi_system_reset(SBI_RESET_TYPE_COLDREBOOT, SBI_RESET_REASON_NONE);
    475 spin:
    476 	for (;;) {
    477 		asm volatile("wfi" ::: "memory");
    478 	}
    479 	/* NOTREACHED */
    480 }
    481 
    482 void
    483 cpu_dumpconf(void)
    484 {
    485 	// TBD!!
    486 }
    487 
    488 
    489 int
    490 cpu_lwp_setprivate(lwp_t *l, void *addr)
    491 {
    492 	struct trapframe * const tf = lwp_trapframe(l);
    493 
    494 	tf->tf_reg[_REG_TP] = (register_t)addr;
    495 
    496 	return 0;
    497 }
    498 
    499 
    500 void
    501 cpu_startup(void)
    502 {
    503 	vaddr_t minaddr, maxaddr;
    504 	char pbuf[10];	/* "999999 MB" -- But Sv39 is max 512GB */
    505 
    506 	/*
    507 	 * Good {morning,afternoon,evening,night}.
    508 	 */
    509 	printf("%s%s", copyright, version);
    510 	format_bytes(pbuf, sizeof(pbuf), ctob(physmem));
    511 	printf("total memory = %s\n", pbuf);
    512 
    513 	minaddr = 0;
    514 	/*
    515 	 * Allocate a submap for physio.
    516 	 */
    517 	phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
    518 	    VM_PHYS_SIZE, 0, FALSE, NULL);
    519 
    520 	format_bytes(pbuf, sizeof(pbuf), ptoa(uvm_availmem(false)));
    521 	printf("avail memory = %s\n", pbuf);
    522 
    523 #ifdef MULTIPROCESSOR
    524 	kcpuset_create(&cpus_halted, true);
    525 	KASSERT(cpus_halted != NULL);
    526 
    527 	kcpuset_create(&cpus_hatched, true);
    528 	KASSERT(cpus_hatched != NULL);
    529 
    530 	kcpuset_create(&cpus_paused, true);
    531 	KASSERT(cpus_paused != NULL);
    532 
    533 	kcpuset_create(&cpus_resumed, true);
    534 	KASSERT(cpus_resumed != NULL);
    535 
    536 	kcpuset_create(&cpus_running, true);
    537 	KASSERT(cpus_running != NULL);
    538 
    539 	kcpuset_set(cpus_hatched, cpu_index(curcpu()));
    540 	kcpuset_set(cpus_running, cpu_index(curcpu()));
    541 #endif
    542 
    543 	fdtbus_intr_init();
    544 
    545 	fdt_setup_rndseed();
    546 	fdt_setup_efirng();
    547 }
    548 
    549 static void
    550 riscv_add_memory(const struct fdt_memory *m, void *arg)
    551 {
    552 	paddr_t first = atop(m->start);
    553 	paddr_t last = atop(m->end);
    554 	int freelist = VM_FREELIST_DEFAULT;
    555 
    556 	VPRINTF("adding %#16" PRIxPADDR " - %#16" PRIxPADDR"  to freelist %d\n",
    557 	    m->start, m->end, freelist);
    558 
    559 	uvm_page_physload(first, last, first, last, freelist);
    560 	physmem += last - first;
    561 }
    562 
    563 
    564 static void
    565 cpu_kernel_vm_init(paddr_t memory_start, paddr_t memory_end)
    566 {
    567 	extern char __kernel_text[];
    568 	extern char _end[];
    569 
    570 	vaddr_t kernstart = trunc_page((vaddr_t)__kernel_text);
    571 	vaddr_t kernend = round_page((vaddr_t)_end);
    572 	paddr_t kernstart_phys = KERN_VTOPHYS(kernstart);
    573 	paddr_t kernend_phys = KERN_VTOPHYS(kernend);
    574 
    575 	VPRINTF("%s: kernel phys start %#" PRIxPADDR " end %#" PRIxPADDR "\n",
    576 	    __func__, kernstart_phys, kernend_phys);
    577 	fdt_memory_remove_range(kernstart_phys,
    578 	    kernend_phys - kernstart_phys);
    579 
    580 	/*
    581 	 * Don't give these pages to UVM.
    582 	 *
    583 	 * cpu_kernel_vm_init need to create proper tables then the following
    584 	 * will be true.
    585 	 *
    586 	 * Now we have APs started the pages used for stacks and L1PT can
    587 	 * be given to uvm
    588 	 */
    589 	extern char const __start__init_memory[];
    590 	extern char const __stop__init_memory[] __weak;
    591 	if (&__start__init_memory[0] != &__stop__init_memory[0]) {
    592 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
    593 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
    594 
    595 		VPRINTF("%s: init   phys start %#" PRIxPADDR
    596 		    " end %#" PRIxPADDR "\n", __func__, spa, epa);
    597 		fdt_memory_remove_range(spa, epa - spa);
    598 	}
    599 
    600 #ifdef _LP64
    601 	paddr_t pa = memory_start & ~XSEGOFSET;
    602 	pmap_direct_base = RISCV_DIRECTMAP_START;
    603 	extern pd_entry_t l2_pte[PAGE_SIZE / sizeof(pd_entry_t)];
    604 
    605 
    606 	const vsize_t vshift = XSEGSHIFT;
    607 	const vaddr_t pdetab_mask = PMAP_PDETABSIZE - 1;
    608 	const vsize_t inc = 1UL << vshift;
    609 
    610 	const vaddr_t sva = RISCV_DIRECTMAP_START + pa;
    611 	const vaddr_t eva = RISCV_DIRECTMAP_END;
    612 	const size_t sidx = (sva >> vshift) & pdetab_mask;
    613 	const size_t eidx = (eva >> vshift) & pdetab_mask;
    614 
    615 	/* Allocate gigapages covering all physical memory in the direct map. */
    616 	for (size_t i = sidx; i < eidx && pa < memory_end; i++, pa += inc) {
    617 		l2_pte[i] = PA_TO_PTE(pa) | PTE_KERN | PTE_HARDWIRED | PTE_RW;
    618 		VPRINTF("dm:   %p :  %#" PRIxPADDR "\n", &l2_pte[i], l2_pte[i]);
    619 	}
    620 #endif
    621 //	pt_dump(printf);
    622 }
    623 
    624 static void
    625 riscv_init_lwp0_uarea(void)
    626 {
    627 	extern char lwp0uspace[];
    628 
    629 	uvm_lwp_setuarea(&lwp0, (vaddr_t)lwp0uspace);
    630 	memset(&lwp0.l_md, 0, sizeof(lwp0.l_md));
    631 	memset(lwp_getpcb(&lwp0), 0, sizeof(struct pcb));
    632 
    633 	struct trapframe *tf = (struct trapframe *)(lwp0uspace + USPACE) - 1;
    634 	memset(tf, 0, sizeof(*tf));
    635 
    636 	lwp0.l_md.md_utf = lwp0.l_md.md_ktf = tf;
    637 }
    638 
    639 
    640 static void
    641 riscv_print_memory(const struct fdt_memory *m, void *arg)
    642 {
    643 
    644 	VPRINTF("FDT /memory @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
    645 	    m->start, m->end - m->start);
    646 }
    647 
    648 
    649 static void
    650 parse_mi_bootargs(char *args)
    651 {
    652 	int howto;
    653 	bool found, start, skipping;
    654 
    655 	if (args == NULL)
    656 		return;
    657 
    658 	start = true;
    659 	skipping = false;
    660 	for (char *cp = args; *cp; cp++) {
    661 		/* check for "words" starting with a "-" only */
    662 		if (start) {
    663 			if (*cp == '-') {
    664 				skipping = false;
    665 			} else {
    666 				skipping = true;
    667 			}
    668 			start = false;
    669 			continue;
    670 		}
    671 
    672 		if (*cp == ' ') {
    673 			start = true;
    674 			skipping = false;
    675 			continue;
    676 		}
    677 
    678 		if (skipping) {
    679 			continue;
    680 		}
    681 
    682 		/* Check valid boot flags */
    683 		howto = 0;
    684 		BOOT_FLAG(*cp, howto);
    685 		if (!howto)
    686 			printf("bootflag '%c' not recognised\n", *cp);
    687 		else
    688 			boothowto |= howto;
    689 	}
    690 
    691 	found = optstr_get(args, "root", bootdevstr, sizeof(bootdevstr));
    692 	if (found) {
    693 		bootspec = bootdevstr;
    694 	}
    695 }
    696 
    697 
    698 void
    699 init_riscv(register_t hartid, paddr_t dtb)
    700 {
    701 
    702 	/* set temporally to work printf()/panic() even before consinit() */
    703 	cn_tab = &earlycons;
    704 
    705 	/* Load FDT */
    706 	const vaddr_t dtbva = VM_KERNEL_DTB_BASE + (dtb & (NBSEG - 1));
    707 	void *fdt_data = (void *)dtbva;
    708 	int error = fdt_check_header(fdt_data);
    709 	if (error != 0)
    710 	    panic("fdt_check_header failed: %s", fdt_strerror(error));
    711 
    712 	fdtbus_init(fdt_data);
    713 
    714 	/* Lookup platform specific backend */
    715 	const struct fdt_platform * const plat = fdt_platform_find();
    716 	if (plat == NULL)
    717 		panic("Kernel does not support this device");
    718 
    719 	/* Early console may be available, announce ourselves. */
    720 	VPRINTF("FDT<%p>\n", fdt_data);
    721 
    722 	boot_args = fdt_get_bootargs();
    723 
    724 	VPRINTF("devmap %p\n", plat->fp_devmap());
    725 	pmap_devmap_bootstrap(0, plat->fp_devmap());
    726 
    727 	VPRINTF("bootstrap\n");
    728 	plat->fp_bootstrap();
    729 
    730 	/*
    731 	 * If stdout-path is specified on the command line, override the
    732 	 * value in /chosen/stdout-path before initializing console.
    733 	 */
    734 	VPRINTF("stdout\n");
    735 	fdt_update_stdout_path(fdt_data, boot_args);
    736 
    737 	/*
    738 	 * Done making changes to the FDT.
    739 	 */
    740 	fdt_pack(fdt_data);
    741 
    742 	const uint32_t dtbsize = round_page(fdt_totalsize(fdt_data));
    743 
    744 	VPRINTF("fdt size %x/%x\n", dtbsize, fdt_totalsize(fdt_data));
    745 
    746 	VPRINTF("consinit ");
    747 	consinit();
    748 	VPRINTF("ok\n");
    749 
    750 	/* Talk to the user */
    751 	printf("NetBSD/riscv (fdt) booting ...\n");
    752 
    753 #ifdef BOOT_ARGS
    754 	char mi_bootargs[] = BOOT_ARGS;
    755 	parse_mi_bootargs(mi_bootargs);
    756 #endif
    757 
    758 	uint64_t memory_start, memory_end;
    759 	fdt_memory_get(&memory_start, &memory_end);
    760 	physical_start = memory_start;
    761 	physical_end = memory_end;
    762 
    763 	fdt_memory_foreach(riscv_print_memory, NULL);
    764 
    765 	/* Cannot map memory above largest page number */
    766 	const uint64_t maxppn = __SHIFTOUT_MASK(PTE_PPN) - 1;
    767 	const uint64_t memory_limit = ptoa(maxppn);
    768 
    769 	if (memory_end > memory_limit) {
    770 		fdt_memory_remove_range(memory_limit, memory_end);
    771 		memory_end = memory_limit;
    772 	}
    773 
    774 	uint64_t memory_size __unused = memory_end - memory_start;
    775 
    776 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
    777 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
    778 
    779 	/* Parse ramdisk, rndseed, and firmware's RNG from EFI */
    780 	fdt_probe_initrd();
    781 	fdt_probe_rndseed();
    782 	fdt_probe_efirng();
    783 
    784 	fdt_memory_remove_reserved(memory_start, memory_end);
    785 
    786 	fdt_memory_remove_range(dtb, dtbsize);
    787 	fdt_reserve_initrd();
    788 	fdt_reserve_rndseed();
    789 	fdt_reserve_efirng();
    790 
    791 	/* Perform PT build and VM init */
    792 	cpu_kernel_vm_init(memory_start, memory_end);
    793 
    794 	VPRINTF("bootargs: %s\n", boot_args);
    795 
    796 	parse_mi_bootargs(boot_args);
    797 
    798 #ifdef DDB
    799 	if (boothowto & RB_KDB) {
    800 		printf("Entering DDB...\n");
    801 		cpu_Debugger();
    802 	}
    803 #endif
    804 
    805 	extern char __kernel_text[];
    806 	extern char _end[];
    807 //	extern char __data_start[];
    808 //	extern char __rodata_start[];
    809 
    810 	vaddr_t kernstart = trunc_page((vaddr_t)__kernel_text);
    811 	vaddr_t kernend = round_page((vaddr_t)_end);
    812 	paddr_t kernstart_phys __unused = KERN_VTOPHYS(kernstart);
    813 	paddr_t kernend_phys __unused = KERN_VTOPHYS(kernend);
    814 
    815 	vaddr_t kernelvmstart;
    816 
    817 	vaddr_t kernstart_mega __unused = MEGAPAGE_TRUNC(kernstart);
    818 	vaddr_t kernend_mega = MEGAPAGE_ROUND(kernend);
    819 
    820 	kernelvmstart = kernend_mega;
    821 
    822 #if 0
    823 #ifdef MODULAR
    824 #define MODULE_RESERVED_MAX	(1024 * 1024 * 128)
    825 #define MODULE_RESERVED_SIZE	(1024 * 1024 * 32)	/* good enough? */
    826 	module_start = kernelvmstart;
    827 	module_end = kernend_mega + MODULE_RESERVED_SIZE;
    828 	if (module_end >= kernstart_mega + MODULE_RESERVED_MAX)
    829 		module_end = kernstart_mega + MODULE_RESERVED_MAX;
    830 	KASSERT(module_end > kernend_mega);
    831 	kernelvmstart = module_end;
    832 #endif /* MODULAR */
    833 #endif
    834 	KASSERT(kernelvmstart < VM_KERNEL_VM_BASE);
    835 
    836 	kernelvmstart = VM_KERNEL_VM_BASE;
    837 
    838 	/*
    839 	 * msgbuf is allocated from the top of the last biggest memory block.
    840 	 */
    841 	paddr_t msgbufaddr = 0;
    842 
    843 #ifdef _LP64
    844 	/* XXX check all ranges for last one with a big enough hole */
    845 	msgbufaddr = memory_end - MSGBUFSIZE;
    846 	KASSERT(msgbufaddr != 0);	/* no space for msgbuf */
    847 	fdt_memory_remove_range(msgbufaddr, msgbufaddr + MSGBUFSIZE);
    848 	msgbufaddr = RISCV_PA_TO_KVA(msgbufaddr);
    849 	VPRINTF("msgbufaddr = %#lx\n", msgbufaddr);
    850 	initmsgbuf((void *)msgbufaddr, MSGBUFSIZE);
    851 #endif
    852 
    853 	KASSERT(msgbufaddr != 0);	/* no space for msgbuf */
    854 #ifdef _LP64
    855 	initmsgbuf((void *)RISCV_PA_TO_KVA(msgbufaddr), MSGBUFSIZE);
    856 #endif
    857 
    858 #define	DPRINTF(v)	VPRINTF("%24s = 0x%16lx\n", #v, (unsigned long)v);
    859 
    860 	VPRINTF("------------------------------------------\n");
    861 	DPRINTF(kern_vtopdiff);
    862 	DPRINTF(memory_start);
    863 	DPRINTF(memory_end);
    864 	DPRINTF(memory_size);
    865 	DPRINTF(kernstart_phys);
    866 	DPRINTF(kernend_phys)
    867 	DPRINTF(msgbufaddr);
    868 //	DPRINTF(physical_end);
    869 	DPRINTF(VM_MIN_KERNEL_ADDRESS);
    870 	DPRINTF(kernstart_mega);
    871 	DPRINTF(kernstart);
    872 	DPRINTF(kernend);
    873 	DPRINTF(kernend_mega);
    874 #if 0
    875 #ifdef MODULAR
    876 	DPRINTF(module_start);
    877 	DPRINTF(module_end);
    878 #endif
    879 #endif
    880 	DPRINTF(VM_MAX_KERNEL_ADDRESS);
    881 #ifdef _LP64
    882 	DPRINTF(pmap_direct_base);
    883 #endif
    884 	VPRINTF("------------------------------------------\n");
    885 
    886 #undef DPRINTF
    887 
    888 	uvm_md_init();
    889 
    890 	/*
    891 	 * pass memory pages to uvm
    892 	 */
    893 	physmem = 0;
    894 	fdt_memory_foreach(riscv_add_memory, NULL);
    895 
    896 	pmap_bootstrap(kernelvmstart, VM_MAX_KERNEL_ADDRESS);
    897 
    898 	kasan_init();
    899 
    900 	/* Finish setting up lwp0 on our end before we call main() */
    901 	riscv_init_lwp0_uarea();
    902 
    903 
    904 	error = 0;
    905 	if ((boothowto & RB_MD1) == 0) {
    906 		VPRINTF("mpstart\n");
    907 		if (plat->fp_mpstart)
    908 			error = plat->fp_mpstart();
    909 	}
    910 	if (error)
    911 		printf("AP startup problems\n");
    912 }
    913 
    914 
    915 #ifdef _LP64
    916 static void
    917 pte_bits(void (*pr)(const char *, ...), pt_entry_t pte)
    918 {
    919 	(*pr)("%c%c%c%c%c%c%c%c",
    920 	    (pte & PTE_D) ? 'D' : '.',
    921 	    (pte & PTE_A) ? 'A' : '.',
    922 	    (pte & PTE_G) ? 'G' : '.',
    923 	    (pte & PTE_U) ? 'U' : '.',
    924 	    (pte & PTE_X) ? 'X' : '.',
    925 	    (pte & PTE_W) ? 'W' : '.',
    926 	    (pte & PTE_R) ? 'R' : '.',
    927 	    (pte & PTE_V) ? 'V' : '.');
    928 }
    929 
    930 static void
    931 dump_ln_table(paddr_t pdp_pa, int topbit, int level, vaddr_t va,
    932     void (*pr)(const char *, ...) __printflike(1, 2))
    933 {
    934 	pd_entry_t *pdp = (void *)PMAP_DIRECT_MAP(pdp_pa);
    935 
    936 	(*pr)("l%u     @  pa %#16" PRIxREGISTER "\n", level, pdp_pa);
    937 	for (size_t i = 0; i < PAGE_SIZE / sizeof(pd_entry_t); i++) {
    938 		pd_entry_t entry = pdp[i];
    939 
    940 		if (topbit) {
    941 			va = i << (PGSHIFT + level * SEGLENGTH);
    942 			if (va & __BIT(topbit)) {
    943 				va |= __BITS(63, topbit);
    944 			}
    945 		}
    946 		if (entry != 0) {
    947 			paddr_t pa = __SHIFTOUT(entry, PTE_PPN) << PGSHIFT;
    948 			// check level PPN bits.
    949 			if (PTE_ISLEAF_P(entry)) {
    950 				(*pr)("l%u %3zu    va 0x%016lx  pa 0x%012lx - ",
    951 				      level, i, va, pa);
    952 				pte_bits(pr, entry);
    953 				(*pr)("\n");
    954 			} else {
    955 				(*pr)("l%u %3zu    va 0x%016lx  -> 0x%012lx - ",
    956 				      level, i, va, pa);
    957 				pte_bits(pr, entry);
    958 				(*pr)("\n");
    959 				if (level == 0) {
    960 					(*pr)("wtf\n");
    961 					continue;
    962 				}
    963 				if (pte_pde_valid_p(entry))
    964 					dump_ln_table(pa, 0, level - 1, va, pr);
    965 			}
    966 		}
    967 		va += 1UL << (PGSHIFT + level * SEGLENGTH);
    968 	}
    969 }
    970 
    971 void
    972 pt_dump(void (*pr)(const char *, ...) __printflike(1, 2))
    973 {
    974 	const register_t satp = csr_satp_read();
    975 	size_t topbit = sizeof(long) * NBBY - 1;
    976 
    977 #ifdef _LP64
    978 	const paddr_t satp_pa = __SHIFTOUT(satp, SATP_PPN) << PGSHIFT;
    979 	const uint8_t mode = __SHIFTOUT(satp, SATP_MODE);
    980 	u_int level = 1;
    981 
    982 	switch (mode) {
    983 	case SATP_MODE_SV39:
    984 	case SATP_MODE_SV48:
    985 		topbit = (39 - 1) + (mode - 8) * SEGLENGTH;
    986 		level = mode - 6;
    987 		break;
    988 	}
    989 #endif
    990 	(*pr)("topbit = %zu\n", topbit);
    991 
    992 	(*pr)("satp   = 0x%" PRIxREGISTER "\n", satp);
    993 #ifdef _LP64
    994 	dump_ln_table(satp_pa, topbit, level, 0, pr);
    995 #endif
    996 }
    997 #endif
    998 
    999 void
   1000 consinit(void)
   1001 {
   1002 	static bool initialized = false;
   1003 	const struct fdt_console *cons = fdtbus_get_console();
   1004 	const struct fdt_platform *plat = fdt_platform_find();
   1005 
   1006 	if (initialized || cons == NULL)
   1007 		return;
   1008 
   1009 	u_int uart_freq = 0;
   1010 	extern struct bus_space riscv_generic_bs_tag;
   1011 	struct fdt_attach_args faa = {
   1012 		.faa_bst = &riscv_generic_bs_tag,
   1013 	};
   1014 
   1015 	faa.faa_phandle = fdtbus_get_stdout_phandle();
   1016 	if (plat->fp_uart_freq != NULL)
   1017 		uart_freq = plat->fp_uart_freq();
   1018 
   1019 	cons->consinit(&faa, uart_freq);
   1020 
   1021 	initialized = true;
   1022 }
   1023