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_kstack_pages.h"
37 #include "opt_platform.h"
39 #include <sys/cdefs.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
47 #include <sys/devmap.h>
49 #include <sys/imgact.h>
51 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/linker.h>
55 #include <sys/msgbuf.h>
57 #include <sys/physmem.h>
59 #include <sys/ptrace.h>
60 #include <sys/reboot.h>
62 #include <sys/rwlock.h>
63 #include <sys/sched.h>
64 #include <sys/signalvar.h>
65 #include <sys/syscallsubr.h>
66 #include <sys/sysent.h>
67 #include <sys/sysproto.h>
68 #include <sys/tslog.h>
69 #include <sys/ucontext.h>
70 #include <sys/vmmeter.h>
73 #include <vm/vm_param.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_phys.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_pager.h>
82 #include <machine/cpu.h>
83 #include <machine/fpe.h>
84 #include <machine/intr.h>
85 #include <machine/kdb.h>
86 #include <machine/machdep.h>
87 #include <machine/metadata.h>
88 #include <machine/pcb.h>
89 #include <machine/pte.h>
90 #include <machine/riscvreg.h>
91 #include <machine/sbi.h>
92 #include <machine/trap.h>
93 #include <machine/vmparam.h>
96 #include <contrib/libfdt/libfdt.h>
97 #include <dev/fdt/fdt_common.h>
98 #include <dev/ofw/openfirm.h>
101 struct pcpu __pcpu[MAXCPU];
103 static struct trapframe proc0_tf;
108 #define DTB_SIZE_MAX (1024 * 1024)
110 struct kva_md_info kmi;
112 int64_t dcache_line_size; /* The minimum D cache line size */
113 int64_t icache_line_size; /* The minimum I cache line size */
114 int64_t idcache_line_size; /* The minimum cache line size */
116 #define BOOT_HART_INVALID 0xffffffff
117 uint32_t boot_hart = BOOT_HART_INVALID; /* The hart we booted on. */
123 static char static_kenv[PAGE_SIZE];
126 cpu_startup(void *dummy)
132 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
133 ptoa((uintmax_t)realmem) / (1024 * 1024));
136 * Display any holes after the first chunk of extended memory.
141 printf("Physical memory chunk(s):\n");
142 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
145 size = phys_avail[indx + 1] - phys_avail[indx];
147 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
148 (uintmax_t)phys_avail[indx],
149 (uintmax_t)phys_avail[indx + 1] - 1,
150 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
154 vm_ksubmap_init(&kmi);
156 printf("avail memory = %ju (%ju MB)\n",
157 ptoa((uintmax_t)vm_free_count()),
158 ptoa((uintmax_t)vm_free_count()) / (1024 * 1024));
160 devmap_print_table();
163 vm_pager_bufferinit();
166 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
169 cpu_idle_wakeup(int cpu)
182 if (!sched_runnable())
196 * Try to power down using the HSM SBI extension and fall back to a
200 if (sbi_probe_extension(SBI_EXT_ID_HSM) != 0)
203 __asm __volatile("wfi");
208 * Flush the D-cache for non-DMA I/O so that the I-cache can
209 * be made coherent later.
212 cpu_flush_dcache(void *ptr, size_t len)
218 /* Get current clock frequency for the given CPU ID. */
220 cpu_est_clockrate(int cpu_id, uint64_t *rate)
223 panic("cpu_est_clockrate");
227 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
238 if (td->td_md.md_spinlock_count == 0) {
239 reg = intr_disable();
240 td->td_md.md_spinlock_count = 1;
241 td->td_md.md_saved_sstatus_ie = reg;
244 td->td_md.md_spinlock_count++;
251 register_t sstatus_ie;
254 sstatus_ie = td->td_md.md_saved_sstatus_ie;
255 td->td_md.md_spinlock_count--;
256 if (td->td_md.md_spinlock_count == 0) {
258 intr_restore(sstatus_ie);
263 * Construct a PCB from a trapframe. This is called from kdb_trap() where
264 * we want to start a backtrace from the function that caused us to enter
265 * the debugger. We have the context in the trapframe, but base the trace
266 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
267 * enough for a backtrace.
270 makectx(struct trapframe *tf, struct pcb *pcb)
273 memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s));
275 pcb->pcb_ra = tf->tf_sepc;
276 pcb->pcb_sp = tf->tf_sp;
277 pcb->pcb_gp = tf->tf_gp;
278 pcb->pcb_tp = tf->tf_tp;
282 init_proc0(vm_offset_t kstack)
288 proc_linkup0(&proc0, &thread0);
289 thread0.td_kstack = kstack;
290 thread0.td_kstack_pages = KSTACK_PAGES;
291 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
292 thread0.td_kstack_pages * PAGE_SIZE) - 1;
293 thread0.td_pcb->pcb_fpflags = 0;
294 thread0.td_frame = &proc0_tf;
295 pcpup->pc_curpcb = thread0.td_pcb;
300 try_load_dtb(caddr_t kmdp)
304 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
306 #if defined(FDT_DTB_STATIC)
308 * In case the device tree blob was not retrieved (from metadata) try
309 * to use the statically embedded one.
311 if (dtbp == (vm_offset_t)NULL)
312 dtbp = (vm_offset_t)&fdt_static_dtb;
315 if (dtbp == (vm_offset_t)NULL) {
316 printf("ERROR loading DTB\n");
320 if (OF_install(OFW_FDT, 0) == FALSE)
321 panic("Cannot install FDT");
323 if (OF_init((void *)dtbp) != 0)
324 panic("OF_init failed with the found device tree");
334 dcache_line_size = 0;
335 icache_line_size = 0;
336 idcache_line_size = 0;
340 * Fake up a boot descriptor table.
343 fake_preload_metadata(struct riscv_bootparams *rvbp)
345 static uint32_t fake_preload[48];
346 vm_offset_t lastaddr;
347 size_t fake_size, dtb_size;
349 #define PRELOAD_PUSH_VALUE(type, value) do { \
350 *(type *)((char *)fake_preload + fake_size) = (value); \
351 fake_size += sizeof(type); \
354 #define PRELOAD_PUSH_STRING(str) do { \
356 ssize = strlen(str) + 1; \
357 PRELOAD_PUSH_VALUE(uint32_t, ssize); \
358 strcpy(((char *)fake_preload + fake_size), str); \
359 fake_size += ssize; \
360 fake_size = roundup(fake_size, sizeof(u_long)); \
364 lastaddr = (vm_offset_t)&end;
366 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_NAME);
367 PRELOAD_PUSH_STRING("kernel");
368 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_TYPE);
369 PRELOAD_PUSH_STRING("elf kernel");
371 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_ADDR);
372 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
373 PRELOAD_PUSH_VALUE(uint64_t, KERNBASE);
375 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_SIZE);
376 PRELOAD_PUSH_VALUE(uint32_t, sizeof(size_t));
377 PRELOAD_PUSH_VALUE(uint64_t, (size_t)((vm_offset_t)&end - KERNBASE));
379 /* Copy the DTB to KVA space. */
380 lastaddr = roundup(lastaddr, sizeof(int));
381 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_DTBP);
382 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
383 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
384 dtb_size = fdt_totalsize(rvbp->dtbp_virt);
385 memmove((void *)lastaddr, (const void *)rvbp->dtbp_virt, dtb_size);
386 lastaddr = roundup(lastaddr + dtb_size, sizeof(int));
388 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_KERNEND);
389 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
390 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
392 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_HOWTO);
393 PRELOAD_PUSH_VALUE(uint32_t, sizeof(int));
394 PRELOAD_PUSH_VALUE(int, RB_VERBOSE);
397 PRELOAD_PUSH_VALUE(uint32_t, 0);
398 PRELOAD_PUSH_VALUE(uint32_t, 0);
399 preload_metadata = (caddr_t)fake_preload;
401 /* Check if bootloader clobbered part of the kernel with the DTB. */
402 KASSERT(rvbp->dtbp_phys + dtb_size <= rvbp->kern_phys ||
403 rvbp->dtbp_phys >= rvbp->kern_phys + (lastaddr - KERNBASE),
404 ("FDT (%lx-%lx) and kernel (%lx-%lx) overlap", rvbp->dtbp_phys,
405 rvbp->dtbp_phys + dtb_size, rvbp->kern_phys,
406 rvbp->kern_phys + (lastaddr - KERNBASE)));
407 KASSERT(fake_size < sizeof(fake_preload),
408 ("Too many fake_preload items"));
410 if (boothowto & RB_VERBOSE)
411 printf("FDT phys (%lx-%lx), kernel phys (%lx-%lx)\n",
412 rvbp->dtbp_phys, rvbp->dtbp_phys + dtb_size,
413 rvbp->kern_phys, rvbp->kern_phys + (lastaddr - KERNBASE));
416 /* Support for FDT configurations only. */
421 parse_fdt_bootargs(void)
425 bootargs[sizeof(bootargs) - 1] = '\0';
426 if (fdt_get_chosen_bootargs(bootargs, sizeof(bootargs) - 1) == 0) {
427 boothowto |= boot_parse_cmdline(bootargs);
436 vm_offset_t lastaddr;
438 vm_offset_t ksym_start, ksym_end;
442 /* Find the kernel address */
443 kmdp = preload_search_by_type("elf kernel");
445 kmdp = preload_search_by_type("elf64 kernel");
446 KASSERT(kmdp != NULL, ("No preload metadata found!"));
448 /* Read the boot metadata */
449 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
450 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
451 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
452 if (kern_envp != NULL)
453 init_static_kenv(kern_envp, 0);
455 init_static_kenv(static_kenv, sizeof(static_kenv));
457 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
458 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
459 db_fetch_ksymtab(ksym_start, ksym_end);
463 if (kern_envp == NULL)
464 parse_fdt_bootargs();
470 initriscv(struct riscv_bootparams *rvbp)
472 struct mem_region mem_regions[FDT_MEM_REGIONS];
475 vm_offset_t lastaddr;
483 TSRAW(&thread0, TS_ENTER, __func__, NULL);
485 /* Set the pcpu data, this is needed by pmap_bootstrap */
487 pcpu_init(pcpup, 0, sizeof(struct pcpu));
489 /* Set the pcpu pointer */
490 __asm __volatile("mv tp, %0" :: "r"(pcpup));
492 PCPU_SET(curthread, &thread0);
494 /* Initialize SBI interface. */
497 /* Parse the boot metadata. */
498 if (rvbp->modulep != 0) {
499 preload_metadata = (caddr_t)rvbp->modulep;
501 fake_preload_metadata(rvbp);
503 lastaddr = parse_metadata();
507 * Look for the boot hart ID. This was either passed in directly from
508 * the SBI firmware and handled by locore, or was stored in the device
509 * tree by an earlier boot stage.
511 chosen = OF_finddevice("/chosen");
512 if (OF_getencprop(chosen, "boot-hartid", &hart, sizeof(hart)) != -1) {
516 if (boot_hart == BOOT_HART_INVALID) {
517 panic("Boot hart ID was not properly set");
519 pcpup->pc_hart = boot_hart;
523 * Exclude reserved memory specified by the device tree. Typically,
524 * this contains an entry for memory used by the runtime SBI firmware.
526 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
527 physmem_exclude_regions(mem_regions, mem_regions_sz,
528 EXFLAG_NODUMP | EXFLAG_NOALLOC);
531 /* Grab physical memory regions information from device tree. */
532 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) {
533 panic("Cannot get physical memory regions");
535 physmem_hardware_regions(mem_regions, mem_regions_sz);
539 * Identify CPU/ISA features.
543 /* Do basic tuning, hz etc */
548 /* Bootstrap enough of pmap to enter the kernel proper */
549 kernlen = (lastaddr - KERNBASE);
550 pmap_bootstrap(rvbp->kern_l1pt, rvbp->kern_phys, kernlen);
554 * XXX: Unconditionally exclude the lowest 2MB of physical memory, as
555 * this area is assumed to contain the SBI firmware. This is a little
556 * fragile, but it is consistent with the platforms we support so far.
558 * TODO: remove this when the all regular booting methods properly
559 * report their reserved memory in the device tree.
561 physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
562 EXFLAG_NODUMP | EXFLAG_NOALLOC);
564 physmem_init_kernel_globals();
566 /* Establish static device mappings */
567 devmap_bootstrap(0, NULL);
572 * Dump the boot metadata. We have to wait for cninit() since console
573 * output is required. If it's grossly incorrect the kernel will never
576 if (getenv_is_true("debug.dump_modinfo_at_boot"))
579 init_proc0(rvbp->kern_stack);
581 msgbufinit(msgbufp, msgbufsize);
583 init_param2(physmem);
586 if ((boothowto & RB_KDB) != 0)
587 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
590 env = kern_getenv("kernelname");
592 strlcpy(kernelname, env, sizeof(kernelname));
594 if (boothowto & RB_VERBOSE)
595 physmem_print_tables();
599 if (bootverbose && kstack_pages != KSTACK_PAGES)
600 printf("kern.kstack_pages = %d ignored for thread0\n",