2 * Copyright (c) 2014 Andrew Turner
3 * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
6 * Portions of this software were developed by SRI International and the
7 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
10 * Portions of this software were developed by the University of Cambridge
11 * Computer Laboratory as part of the CTSRD Project, with support from the
12 * UK Higher Education Innovation Fund (HEIF).
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "opt_platform.h"
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
46 #include <sys/devmap.h>
48 #include <sys/imgact.h>
50 #include <sys/kernel.h>
52 #include <sys/limits.h>
53 #include <sys/linker.h>
54 #include <sys/msgbuf.h>
56 #include <sys/physmem.h>
58 #include <sys/ptrace.h>
59 #include <sys/reboot.h>
61 #include <sys/rwlock.h>
62 #include <sys/sched.h>
63 #include <sys/signalvar.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysent.h>
66 #include <sys/sysproto.h>
67 #include <sys/tslog.h>
68 #include <sys/ucontext.h>
69 #include <sys/vmmeter.h>
72 #include <vm/vm_param.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_phys.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_pager.h>
81 #include <machine/cpu.h>
82 #include <machine/fpe.h>
83 #include <machine/intr.h>
84 #include <machine/kdb.h>
85 #include <machine/machdep.h>
86 #include <machine/metadata.h>
87 #include <machine/pcb.h>
88 #include <machine/pte.h>
89 #include <machine/riscvreg.h>
90 #include <machine/sbi.h>
91 #include <machine/trap.h>
92 #include <machine/vmparam.h>
95 #include <contrib/libfdt/libfdt.h>
96 #include <dev/fdt/fdt_common.h>
97 #include <dev/ofw/openfirm.h>
100 struct pcpu __pcpu[MAXCPU];
102 static struct trapframe proc0_tf;
107 #define DTB_SIZE_MAX (1024 * 1024)
109 struct kva_md_info kmi;
111 int64_t dcache_line_size; /* The minimum D cache line size */
112 int64_t icache_line_size; /* The minimum I cache line size */
113 int64_t idcache_line_size; /* The minimum cache line size */
115 #define BOOT_HART_INVALID 0xffffffff
116 uint32_t boot_hart = BOOT_HART_INVALID; /* The hart we booted on. */
122 static char static_kenv[PAGE_SIZE];
125 cpu_startup(void *dummy)
131 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
132 ptoa((uintmax_t)realmem) / (1024 * 1024));
135 * Display any holes after the first chunk of extended memory.
140 printf("Physical memory chunk(s):\n");
141 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
144 size = phys_avail[indx + 1] - phys_avail[indx];
146 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
147 (uintmax_t)phys_avail[indx],
148 (uintmax_t)phys_avail[indx + 1] - 1,
149 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
153 vm_ksubmap_init(&kmi);
155 printf("avail memory = %ju (%ju MB)\n",
156 ptoa((uintmax_t)vm_free_count()),
157 ptoa((uintmax_t)vm_free_count()) / (1024 * 1024));
159 devmap_print_table();
162 vm_pager_bufferinit();
165 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
168 cpu_idle_wakeup(int cpu)
181 if (!sched_runnable())
195 * Try to power down using the HSM SBI extension and fall back to a
199 if (sbi_probe_extension(SBI_EXT_ID_HSM) != 0)
202 __asm __volatile("wfi");
207 * Flush the D-cache for non-DMA I/O so that the I-cache can
208 * be made coherent later.
211 cpu_flush_dcache(void *ptr, size_t len)
217 /* Get current clock frequency for the given CPU ID. */
219 cpu_est_clockrate(int cpu_id, uint64_t *rate)
222 panic("cpu_est_clockrate");
226 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
237 if (td->td_md.md_spinlock_count == 0) {
238 reg = intr_disable();
239 td->td_md.md_spinlock_count = 1;
240 td->td_md.md_saved_sstatus_ie = reg;
243 td->td_md.md_spinlock_count++;
250 register_t sstatus_ie;
253 sstatus_ie = td->td_md.md_saved_sstatus_ie;
254 td->td_md.md_spinlock_count--;
255 if (td->td_md.md_spinlock_count == 0) {
257 intr_restore(sstatus_ie);
262 * Construct a PCB from a trapframe. This is called from kdb_trap() where
263 * we want to start a backtrace from the function that caused us to enter
264 * the debugger. We have the context in the trapframe, but base the trace
265 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
266 * enough for a backtrace.
269 makectx(struct trapframe *tf, struct pcb *pcb)
272 memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s));
274 pcb->pcb_ra = tf->tf_sepc;
275 pcb->pcb_sp = tf->tf_sp;
276 pcb->pcb_gp = tf->tf_gp;
277 pcb->pcb_tp = tf->tf_tp;
281 init_proc0(vm_offset_t kstack)
287 proc_linkup0(&proc0, &thread0);
288 thread0.td_kstack = kstack;
289 thread0.td_kstack_pages = kstack_pages;
290 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
291 thread0.td_kstack_pages * PAGE_SIZE) - 1;
292 thread0.td_pcb->pcb_fpflags = 0;
293 thread0.td_frame = &proc0_tf;
294 pcpup->pc_curpcb = thread0.td_pcb;
299 try_load_dtb(caddr_t kmdp)
303 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
305 #if defined(FDT_DTB_STATIC)
307 * In case the device tree blob was not retrieved (from metadata) try
308 * to use the statically embedded one.
310 if (dtbp == (vm_offset_t)NULL)
311 dtbp = (vm_offset_t)&fdt_static_dtb;
314 if (dtbp == (vm_offset_t)NULL) {
315 printf("ERROR loading DTB\n");
319 if (OF_install(OFW_FDT, 0) == FALSE)
320 panic("Cannot install FDT");
322 if (OF_init((void *)dtbp) != 0)
323 panic("OF_init failed with the found device tree");
333 dcache_line_size = 0;
334 icache_line_size = 0;
335 idcache_line_size = 0;
339 * Fake up a boot descriptor table.
342 fake_preload_metadata(struct riscv_bootparams *rvbp)
344 static uint32_t fake_preload[48];
345 vm_offset_t lastaddr;
346 size_t fake_size, dtb_size;
348 #define PRELOAD_PUSH_VALUE(type, value) do { \
349 *(type *)((char *)fake_preload + fake_size) = (value); \
350 fake_size += sizeof(type); \
353 #define PRELOAD_PUSH_STRING(str) do { \
355 ssize = strlen(str) + 1; \
356 PRELOAD_PUSH_VALUE(uint32_t, ssize); \
357 strcpy(((char *)fake_preload + fake_size), str); \
358 fake_size += ssize; \
359 fake_size = roundup(fake_size, sizeof(u_long)); \
363 lastaddr = (vm_offset_t)&end;
365 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_NAME);
366 PRELOAD_PUSH_STRING("kernel");
367 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_TYPE);
368 PRELOAD_PUSH_STRING("elf kernel");
370 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_ADDR);
371 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
372 PRELOAD_PUSH_VALUE(uint64_t, KERNBASE);
374 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_SIZE);
375 PRELOAD_PUSH_VALUE(uint32_t, sizeof(size_t));
376 PRELOAD_PUSH_VALUE(uint64_t, (size_t)((vm_offset_t)&end - KERNBASE));
378 /* Copy the DTB to KVA space. */
379 lastaddr = roundup(lastaddr, sizeof(int));
380 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_DTBP);
381 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
382 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
383 dtb_size = fdt_totalsize(rvbp->dtbp_virt);
384 memmove((void *)lastaddr, (const void *)rvbp->dtbp_virt, dtb_size);
385 lastaddr = roundup(lastaddr + dtb_size, sizeof(int));
387 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_KERNEND);
388 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
389 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
391 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_HOWTO);
392 PRELOAD_PUSH_VALUE(uint32_t, sizeof(int));
393 PRELOAD_PUSH_VALUE(int, RB_VERBOSE);
396 PRELOAD_PUSH_VALUE(uint32_t, 0);
397 PRELOAD_PUSH_VALUE(uint32_t, 0);
398 preload_metadata = (caddr_t)fake_preload;
400 /* Check if bootloader clobbered part of the kernel with the DTB. */
401 KASSERT(rvbp->dtbp_phys + dtb_size <= rvbp->kern_phys ||
402 rvbp->dtbp_phys >= rvbp->kern_phys + (lastaddr - KERNBASE),
403 ("FDT (%lx-%lx) and kernel (%lx-%lx) overlap", rvbp->dtbp_phys,
404 rvbp->dtbp_phys + dtb_size, rvbp->kern_phys,
405 rvbp->kern_phys + (lastaddr - KERNBASE)));
406 KASSERT(fake_size < sizeof(fake_preload),
407 ("Too many fake_preload items"));
409 if (boothowto & RB_VERBOSE)
410 printf("FDT phys (%lx-%lx), kernel phys (%lx-%lx)\n",
411 rvbp->dtbp_phys, rvbp->dtbp_phys + dtb_size,
412 rvbp->kern_phys, rvbp->kern_phys + (lastaddr - KERNBASE));
415 /* Support for FDT configurations only. */
420 parse_fdt_bootargs(void)
424 bootargs[sizeof(bootargs) - 1] = '\0';
425 if (fdt_get_chosen_bootargs(bootargs, sizeof(bootargs) - 1) == 0) {
426 boothowto |= boot_parse_cmdline(bootargs);
435 vm_offset_t lastaddr;
437 vm_offset_t ksym_start, ksym_end;
441 /* Find the kernel address */
442 kmdp = preload_search_by_type("elf kernel");
444 kmdp = preload_search_by_type("elf64 kernel");
445 KASSERT(kmdp != NULL, ("No preload metadata found!"));
447 /* Read the boot metadata */
448 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
449 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
450 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
451 if (kern_envp != NULL)
452 init_static_kenv(kern_envp, 0);
454 init_static_kenv(static_kenv, sizeof(static_kenv));
456 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
457 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
458 db_fetch_ksymtab(ksym_start, ksym_end);
462 if (kern_envp == NULL)
463 parse_fdt_bootargs();
469 initriscv(struct riscv_bootparams *rvbp)
471 struct mem_region mem_regions[FDT_MEM_REGIONS];
474 vm_offset_t lastaddr;
482 TSRAW(&thread0, TS_ENTER, __func__, NULL);
484 /* Set the pcpu data, this is needed by pmap_bootstrap */
486 pcpu_init(pcpup, 0, sizeof(struct pcpu));
488 /* Set the pcpu pointer */
489 __asm __volatile("mv tp, %0" :: "r"(pcpup));
491 PCPU_SET(curthread, &thread0);
493 /* Initialize SBI interface. */
496 /* Parse the boot metadata. */
497 if (rvbp->modulep != 0) {
498 preload_metadata = (caddr_t)rvbp->modulep;
500 fake_preload_metadata(rvbp);
502 lastaddr = parse_metadata();
506 * Look for the boot hart ID. This was either passed in directly from
507 * the SBI firmware and handled by locore, or was stored in the device
508 * tree by an earlier boot stage.
510 chosen = OF_finddevice("/chosen");
511 if (OF_getencprop(chosen, "boot-hartid", &hart, sizeof(hart)) != -1) {
515 if (boot_hart == BOOT_HART_INVALID) {
516 panic("Boot hart ID was not properly set");
518 pcpup->pc_hart = boot_hart;
522 * Exclude reserved memory specified by the device tree. Typically,
523 * this contains an entry for memory used by the runtime SBI firmware.
525 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
526 physmem_exclude_regions(mem_regions, mem_regions_sz,
527 EXFLAG_NODUMP | EXFLAG_NOALLOC);
530 /* Grab physical memory regions information from device tree. */
531 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) {
532 panic("Cannot get physical memory regions");
534 physmem_hardware_regions(mem_regions, mem_regions_sz);
538 * Identify CPU/ISA features.
542 /* Do basic tuning, hz etc */
547 /* Bootstrap enough of pmap to enter the kernel proper */
548 kernlen = (lastaddr - KERNBASE);
549 pmap_bootstrap(rvbp->kern_l1pt, rvbp->kern_phys, kernlen);
553 * XXX: Unconditionally exclude the lowest 2MB of physical memory, as
554 * this area is assumed to contain the SBI firmware. This is a little
555 * fragile, but it is consistent with the platforms we support so far.
557 * TODO: remove this when the all regular booting methods properly
558 * report their reserved memory in the device tree.
560 physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
561 EXFLAG_NODUMP | EXFLAG_NOALLOC);
563 physmem_init_kernel_globals();
565 /* Establish static device mappings */
566 devmap_bootstrap(0, NULL);
571 * Dump the boot metadata. We have to wait for cninit() since console
572 * output is required. If it's grossly incorrect the kernel will never
575 if (getenv_is_true("debug.dump_modinfo_at_boot"))
578 init_proc0(rvbp->kern_stack);
580 msgbufinit(msgbufp, msgbufsize);
582 init_param2(physmem);
585 if ((boothowto & RB_KDB) != 0)
586 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
589 env = kern_getenv("kernelname");
591 strlcpy(kernelname, env, sizeof(kernelname));
593 if (boothowto & RB_VERBOSE)
594 physmem_print_tables();