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
37 #include "opt_kstack_pages.h"
38 #include "opt_platform.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>
100 #include <contrib/libfdt/libfdt.h>
101 #include <dev/fdt/fdt_common.h>
102 #include <dev/ofw/openfirm.h>
105 struct pcpu __pcpu[MAXCPU];
107 static struct trapframe proc0_tf;
112 #define DTB_SIZE_MAX (1024 * 1024)
114 struct kva_md_info kmi;
116 int64_t dcache_line_size; /* The minimum D cache line size */
117 int64_t icache_line_size; /* The minimum I cache line size */
118 int64_t idcache_line_size; /* The minimum cache line size */
120 #define BOOT_HART_INVALID 0xffffffff
121 uint32_t boot_hart = BOOT_HART_INVALID; /* The hart we booted on. */
127 static char static_kenv[PAGE_SIZE];
130 cpu_startup(void *dummy)
136 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
137 ptoa((uintmax_t)realmem) / (1024 * 1024));
140 * Display any holes after the first chunk of extended memory.
145 printf("Physical memory chunk(s):\n");
146 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
149 size = phys_avail[indx + 1] - phys_avail[indx];
151 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
152 (uintmax_t)phys_avail[indx],
153 (uintmax_t)phys_avail[indx + 1] - 1,
154 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
158 vm_ksubmap_init(&kmi);
160 printf("avail memory = %ju (%ju MB)\n",
161 ptoa((uintmax_t)vm_free_count()),
162 ptoa((uintmax_t)vm_free_count()) / (1024 * 1024));
164 devmap_print_table();
167 vm_pager_bufferinit();
170 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
173 cpu_idle_wakeup(int cpu)
186 if (!sched_runnable())
200 * Try to power down using the HSM SBI extension and fall back to a
204 if (sbi_probe_extension(SBI_EXT_ID_HSM) != 0)
207 __asm __volatile("wfi");
212 * Flush the D-cache for non-DMA I/O so that the I-cache can
213 * be made coherent later.
216 cpu_flush_dcache(void *ptr, size_t len)
222 /* Get current clock frequency for the given CPU ID. */
224 cpu_est_clockrate(int cpu_id, uint64_t *rate)
227 panic("cpu_est_clockrate");
231 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
242 if (td->td_md.md_spinlock_count == 0) {
243 reg = intr_disable();
244 td->td_md.md_spinlock_count = 1;
245 td->td_md.md_saved_sstatus_ie = reg;
248 td->td_md.md_spinlock_count++;
255 register_t sstatus_ie;
258 sstatus_ie = td->td_md.md_saved_sstatus_ie;
259 td->td_md.md_spinlock_count--;
260 if (td->td_md.md_spinlock_count == 0) {
262 intr_restore(sstatus_ie);
267 * Construct a PCB from a trapframe. This is called from kdb_trap() where
268 * we want to start a backtrace from the function that caused us to enter
269 * the debugger. We have the context in the trapframe, but base the trace
270 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
271 * enough for a backtrace.
274 makectx(struct trapframe *tf, struct pcb *pcb)
277 memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s));
279 pcb->pcb_ra = tf->tf_sepc;
280 pcb->pcb_sp = tf->tf_sp;
281 pcb->pcb_gp = tf->tf_gp;
282 pcb->pcb_tp = tf->tf_tp;
286 init_proc0(vm_offset_t kstack)
292 proc_linkup0(&proc0, &thread0);
293 thread0.td_kstack = kstack;
294 thread0.td_kstack_pages = KSTACK_PAGES;
295 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
296 thread0.td_kstack_pages * PAGE_SIZE) - 1;
297 thread0.td_pcb->pcb_fpflags = 0;
298 thread0.td_frame = &proc0_tf;
299 pcpup->pc_curpcb = thread0.td_pcb;
304 try_load_dtb(caddr_t kmdp)
308 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
310 #if defined(FDT_DTB_STATIC)
312 * In case the device tree blob was not retrieved (from metadata) try
313 * to use the statically embedded one.
315 if (dtbp == (vm_offset_t)NULL)
316 dtbp = (vm_offset_t)&fdt_static_dtb;
319 if (dtbp == (vm_offset_t)NULL) {
320 printf("ERROR loading DTB\n");
324 if (OF_install(OFW_FDT, 0) == FALSE)
325 panic("Cannot install FDT");
327 if (OF_init((void *)dtbp) != 0)
328 panic("OF_init failed with the found device tree");
338 dcache_line_size = 0;
339 icache_line_size = 0;
340 idcache_line_size = 0;
344 * Fake up a boot descriptor table.
347 fake_preload_metadata(struct riscv_bootparams *rvbp)
349 static uint32_t fake_preload[48];
350 vm_offset_t lastaddr;
351 size_t fake_size, dtb_size;
353 #define PRELOAD_PUSH_VALUE(type, value) do { \
354 *(type *)((char *)fake_preload + fake_size) = (value); \
355 fake_size += sizeof(type); \
358 #define PRELOAD_PUSH_STRING(str) do { \
360 ssize = strlen(str) + 1; \
361 PRELOAD_PUSH_VALUE(uint32_t, ssize); \
362 strcpy(((char *)fake_preload + fake_size), str); \
363 fake_size += ssize; \
364 fake_size = roundup(fake_size, sizeof(u_long)); \
368 lastaddr = (vm_offset_t)&end;
370 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_NAME);
371 PRELOAD_PUSH_STRING("kernel");
372 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_TYPE);
373 PRELOAD_PUSH_STRING("elf kernel");
375 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_ADDR);
376 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
377 PRELOAD_PUSH_VALUE(uint64_t, KERNBASE);
379 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_SIZE);
380 PRELOAD_PUSH_VALUE(uint32_t, sizeof(size_t));
381 PRELOAD_PUSH_VALUE(uint64_t, (size_t)((vm_offset_t)&end - KERNBASE));
383 /* Copy the DTB to KVA space. */
384 lastaddr = roundup(lastaddr, sizeof(int));
385 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_DTBP);
386 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
387 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
388 dtb_size = fdt_totalsize(rvbp->dtbp_virt);
389 memmove((void *)lastaddr, (const void *)rvbp->dtbp_virt, dtb_size);
390 lastaddr = roundup(lastaddr + dtb_size, sizeof(int));
392 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_KERNEND);
393 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
394 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
396 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_HOWTO);
397 PRELOAD_PUSH_VALUE(uint32_t, sizeof(int));
398 PRELOAD_PUSH_VALUE(int, RB_VERBOSE);
401 PRELOAD_PUSH_VALUE(uint32_t, 0);
402 PRELOAD_PUSH_VALUE(uint32_t, 0);
403 preload_metadata = (caddr_t)fake_preload;
405 /* Check if bootloader clobbered part of the kernel with the DTB. */
406 KASSERT(rvbp->dtbp_phys + dtb_size <= rvbp->kern_phys ||
407 rvbp->dtbp_phys >= rvbp->kern_phys + (lastaddr - KERNBASE),
408 ("FDT (%lx-%lx) and kernel (%lx-%lx) overlap", rvbp->dtbp_phys,
409 rvbp->dtbp_phys + dtb_size, rvbp->kern_phys,
410 rvbp->kern_phys + (lastaddr - KERNBASE)));
411 KASSERT(fake_size < sizeof(fake_preload),
412 ("Too many fake_preload items"));
414 if (boothowto & RB_VERBOSE)
415 printf("FDT phys (%lx-%lx), kernel phys (%lx-%lx)\n",
416 rvbp->dtbp_phys, rvbp->dtbp_phys + dtb_size,
417 rvbp->kern_phys, rvbp->kern_phys + (lastaddr - KERNBASE));
420 /* Support for FDT configurations only. */
425 parse_fdt_bootargs(void)
429 bootargs[sizeof(bootargs) - 1] = '\0';
430 if (fdt_get_chosen_bootargs(bootargs, sizeof(bootargs) - 1) == 0) {
431 boothowto |= boot_parse_cmdline(bootargs);
440 vm_offset_t lastaddr;
442 vm_offset_t ksym_start, ksym_end;
446 /* Find the kernel address */
447 kmdp = preload_search_by_type("elf kernel");
449 kmdp = preload_search_by_type("elf64 kernel");
450 KASSERT(kmdp != NULL, ("No preload metadata found!"));
452 /* Read the boot metadata */
453 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
454 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
455 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
456 if (kern_envp != NULL)
457 init_static_kenv(kern_envp, 0);
459 init_static_kenv(static_kenv, sizeof(static_kenv));
461 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
462 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
463 db_fetch_ksymtab(ksym_start, ksym_end, 0);
467 if (kern_envp == NULL)
468 parse_fdt_bootargs();
474 initriscv(struct riscv_bootparams *rvbp)
476 struct mem_region mem_regions[FDT_MEM_REGIONS];
479 vm_offset_t lastaddr;
487 TSRAW(&thread0, TS_ENTER, __func__, NULL);
489 /* Set the pcpu data, this is needed by pmap_bootstrap */
491 pcpu_init(pcpup, 0, sizeof(struct pcpu));
493 /* Set the pcpu pointer */
494 __asm __volatile("mv tp, %0" :: "r"(pcpup));
496 PCPU_SET(curthread, &thread0);
498 /* Initialize SBI interface. */
501 /* Parse the boot metadata. */
502 if (rvbp->modulep != 0) {
503 preload_metadata = (caddr_t)rvbp->modulep;
505 fake_preload_metadata(rvbp);
507 lastaddr = parse_metadata();
511 * Look for the boot hart ID. This was either passed in directly from
512 * the SBI firmware and handled by locore, or was stored in the device
513 * tree by an earlier boot stage.
515 chosen = OF_finddevice("/chosen");
516 if (OF_getencprop(chosen, "boot-hartid", &hart, sizeof(hart)) != -1) {
520 if (boot_hart == BOOT_HART_INVALID) {
521 panic("Boot hart ID was not properly set");
523 pcpup->pc_hart = boot_hart;
527 * Exclude reserved memory specified by the device tree. Typically,
528 * this contains an entry for memory used by the runtime SBI firmware.
530 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
531 physmem_exclude_regions(mem_regions, mem_regions_sz,
532 EXFLAG_NODUMP | EXFLAG_NOALLOC);
535 /* Grab physical memory regions information from device tree. */
536 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) {
537 panic("Cannot get physical memory regions");
539 physmem_hardware_regions(mem_regions, mem_regions_sz);
543 * Identify CPU/ISA features.
547 /* Do basic tuning, hz etc */
552 /* Bootstrap enough of pmap to enter the kernel proper */
553 kernlen = (lastaddr - KERNBASE);
554 pmap_bootstrap(rvbp->kern_l1pt, rvbp->kern_phys, kernlen);
558 * XXX: Unconditionally exclude the lowest 2MB of physical memory, as
559 * this area is assumed to contain the SBI firmware. This is a little
560 * fragile, but it is consistent with the platforms we support so far.
562 * TODO: remove this when the all regular booting methods properly
563 * report their reserved memory in the device tree.
565 physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
566 EXFLAG_NODUMP | EXFLAG_NOALLOC);
568 physmem_init_kernel_globals();
570 /* Establish static device mappings */
571 devmap_bootstrap(0, NULL);
576 * Dump the boot metadata. We have to wait for cninit() since console
577 * output is required. If it's grossly incorrect the kernel will never
580 if (getenv_is_true("debug.dump_modinfo_at_boot"))
583 init_proc0(rvbp->kern_stack);
585 msgbufinit(msgbufp, msgbufsize);
587 init_param2(physmem);
590 if ((boothowto & RB_KDB) != 0)
591 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
594 env = kern_getenv("kernelname");
596 strlcpy(kernelname, env, sizeof(kernelname));
598 if (boothowto & RB_VERBOSE)
599 physmem_print_tables();
603 if (bootverbose && kstack_pages != KSTACK_PAGES)
604 printf("kern.kstack_pages = %d ignored for thread0\n",