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