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 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
48 #include <sys/devmap.h>
50 #include <sys/imgact.h>
52 #include <sys/kernel.h>
54 #include <sys/limits.h>
55 #include <sys/linker.h>
56 #include <sys/msgbuf.h>
58 #include <sys/physmem.h>
60 #include <sys/ptrace.h>
61 #include <sys/reboot.h>
63 #include <sys/rwlock.h>
64 #include <sys/sched.h>
65 #include <sys/signalvar.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysent.h>
68 #include <sys/sysproto.h>
69 #include <sys/tslog.h>
70 #include <sys/ucontext.h>
71 #include <sys/vmmeter.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_phys.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_pager.h>
83 #include <machine/cpu.h>
84 #include <machine/fpe.h>
85 #include <machine/intr.h>
86 #include <machine/kdb.h>
87 #include <machine/machdep.h>
88 #include <machine/metadata.h>
89 #include <machine/pcb.h>
90 #include <machine/pte.h>
91 #include <machine/riscvreg.h>
92 #include <machine/sbi.h>
93 #include <machine/trap.h>
94 #include <machine/vmparam.h>
97 #include <contrib/libfdt/libfdt.h>
98 #include <dev/fdt/fdt_common.h>
99 #include <dev/ofw/openfirm.h>
102 struct pcpu __pcpu[MAXCPU];
104 static struct trapframe proc0_tf;
109 #define DTB_SIZE_MAX (1024 * 1024)
111 struct kva_md_info kmi;
113 int64_t dcache_line_size; /* The minimum D cache line size */
114 int64_t icache_line_size; /* The minimum I cache line size */
115 int64_t idcache_line_size; /* The minimum cache line size */
117 #define BOOT_HART_INVALID 0xffffffff
118 uint32_t boot_hart = BOOT_HART_INVALID; /* The hart we booted on. */
124 static char static_kenv[PAGE_SIZE];
127 cpu_startup(void *dummy)
133 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
134 ptoa((uintmax_t)realmem) / (1024 * 1024));
137 * Display any holes after the first chunk of extended memory.
142 printf("Physical memory chunk(s):\n");
143 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
146 size = phys_avail[indx + 1] - phys_avail[indx];
148 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
149 (uintmax_t)phys_avail[indx],
150 (uintmax_t)phys_avail[indx + 1] - 1,
151 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
155 vm_ksubmap_init(&kmi);
157 printf("avail memory = %ju (%ju MB)\n",
158 ptoa((uintmax_t)vm_free_count()),
159 ptoa((uintmax_t)vm_free_count()) / (1024 * 1024));
161 devmap_print_table();
164 vm_pager_bufferinit();
167 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
170 cpu_idle_wakeup(int cpu)
183 if (!sched_runnable())
197 * Try to power down using the HSM SBI extension and fall back to a
201 if (sbi_probe_extension(SBI_EXT_ID_HSM) != 0)
204 __asm __volatile("wfi");
209 * Flush the D-cache for non-DMA I/O so that the I-cache can
210 * be made coherent later.
213 cpu_flush_dcache(void *ptr, size_t len)
219 /* Get current clock frequency for the given CPU ID. */
221 cpu_est_clockrate(int cpu_id, uint64_t *rate)
224 panic("cpu_est_clockrate");
228 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
239 if (td->td_md.md_spinlock_count == 0) {
240 reg = intr_disable();
241 td->td_md.md_spinlock_count = 1;
242 td->td_md.md_saved_sstatus_ie = reg;
245 td->td_md.md_spinlock_count++;
252 register_t sstatus_ie;
255 sstatus_ie = td->td_md.md_saved_sstatus_ie;
256 td->td_md.md_spinlock_count--;
257 if (td->td_md.md_spinlock_count == 0) {
259 intr_restore(sstatus_ie);
264 * Construct a PCB from a trapframe. This is called from kdb_trap() where
265 * we want to start a backtrace from the function that caused us to enter
266 * the debugger. We have the context in the trapframe, but base the trace
267 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
268 * enough for a backtrace.
271 makectx(struct trapframe *tf, struct pcb *pcb)
274 memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s));
276 pcb->pcb_ra = tf->tf_sepc;
277 pcb->pcb_sp = tf->tf_sp;
278 pcb->pcb_gp = tf->tf_gp;
279 pcb->pcb_tp = tf->tf_tp;
283 init_proc0(vm_offset_t kstack)
289 proc_linkup0(&proc0, &thread0);
290 thread0.td_kstack = kstack;
291 thread0.td_kstack_pages = kstack_pages;
292 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
293 thread0.td_kstack_pages * PAGE_SIZE) - 1;
294 thread0.td_pcb->pcb_fpflags = 0;
295 thread0.td_frame = &proc0_tf;
296 pcpup->pc_curpcb = thread0.td_pcb;
301 try_load_dtb(caddr_t kmdp)
305 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
307 #if defined(FDT_DTB_STATIC)
309 * In case the device tree blob was not retrieved (from metadata) try
310 * to use the statically embedded one.
312 if (dtbp == (vm_offset_t)NULL)
313 dtbp = (vm_offset_t)&fdt_static_dtb;
316 if (dtbp == (vm_offset_t)NULL) {
317 printf("ERROR loading DTB\n");
321 if (OF_install(OFW_FDT, 0) == FALSE)
322 panic("Cannot install FDT");
324 if (OF_init((void *)dtbp) != 0)
325 panic("OF_init failed with the found device tree");
335 dcache_line_size = 0;
336 icache_line_size = 0;
337 idcache_line_size = 0;
341 * Fake up a boot descriptor table.
344 fake_preload_metadata(struct riscv_bootparams *rvbp)
346 static uint32_t fake_preload[48];
347 vm_offset_t lastaddr;
348 size_t fake_size, dtb_size;
350 #define PRELOAD_PUSH_VALUE(type, value) do { \
351 *(type *)((char *)fake_preload + fake_size) = (value); \
352 fake_size += sizeof(type); \
355 #define PRELOAD_PUSH_STRING(str) do { \
357 ssize = strlen(str) + 1; \
358 PRELOAD_PUSH_VALUE(uint32_t, ssize); \
359 strcpy(((char *)fake_preload + fake_size), str); \
360 fake_size += ssize; \
361 fake_size = roundup(fake_size, sizeof(u_long)); \
365 lastaddr = (vm_offset_t)&end;
367 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_NAME);
368 PRELOAD_PUSH_STRING("kernel");
369 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_TYPE);
370 PRELOAD_PUSH_STRING("elf kernel");
372 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_ADDR);
373 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
374 PRELOAD_PUSH_VALUE(uint64_t, KERNBASE);
376 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_SIZE);
377 PRELOAD_PUSH_VALUE(uint32_t, sizeof(size_t));
378 PRELOAD_PUSH_VALUE(uint64_t, (size_t)((vm_offset_t)&end - KERNBASE));
380 /* Copy the DTB to KVA space. */
381 lastaddr = roundup(lastaddr, sizeof(int));
382 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_DTBP);
383 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
384 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
385 dtb_size = fdt_totalsize(rvbp->dtbp_virt);
386 memmove((void *)lastaddr, (const void *)rvbp->dtbp_virt, dtb_size);
387 lastaddr = roundup(lastaddr + dtb_size, sizeof(int));
389 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_KERNEND);
390 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
391 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
393 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_HOWTO);
394 PRELOAD_PUSH_VALUE(uint32_t, sizeof(int));
395 PRELOAD_PUSH_VALUE(int, RB_VERBOSE);
398 PRELOAD_PUSH_VALUE(uint32_t, 0);
399 PRELOAD_PUSH_VALUE(uint32_t, 0);
400 preload_metadata = (caddr_t)fake_preload;
402 /* Check if bootloader clobbered part of the kernel with the DTB. */
403 KASSERT(rvbp->dtbp_phys + dtb_size <= rvbp->kern_phys ||
404 rvbp->dtbp_phys >= rvbp->kern_phys + (lastaddr - KERNBASE),
405 ("FDT (%lx-%lx) and kernel (%lx-%lx) overlap", rvbp->dtbp_phys,
406 rvbp->dtbp_phys + dtb_size, rvbp->kern_phys,
407 rvbp->kern_phys + (lastaddr - KERNBASE)));
408 KASSERT(fake_size < sizeof(fake_preload),
409 ("Too many fake_preload items"));
411 if (boothowto & RB_VERBOSE)
412 printf("FDT phys (%lx-%lx), kernel phys (%lx-%lx)\n",
413 rvbp->dtbp_phys, rvbp->dtbp_phys + dtb_size,
414 rvbp->kern_phys, rvbp->kern_phys + (lastaddr - KERNBASE));
417 /* Support for FDT configurations only. */
422 parse_fdt_bootargs(void)
426 bootargs[sizeof(bootargs) - 1] = '\0';
427 if (fdt_get_chosen_bootargs(bootargs, sizeof(bootargs) - 1) == 0) {
428 boothowto |= boot_parse_cmdline(bootargs);
437 vm_offset_t lastaddr;
439 vm_offset_t ksym_start, ksym_end;
443 /* Find the kernel address */
444 kmdp = preload_search_by_type("elf kernel");
446 kmdp = preload_search_by_type("elf64 kernel");
447 KASSERT(kmdp != NULL, ("No preload metadata found!"));
449 /* Read the boot metadata */
450 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
451 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
452 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
453 if (kern_envp != NULL)
454 init_static_kenv(kern_envp, 0);
456 init_static_kenv(static_kenv, sizeof(static_kenv));
458 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
459 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
460 db_fetch_ksymtab(ksym_start, ksym_end);
464 if (kern_envp == NULL)
465 parse_fdt_bootargs();
471 initriscv(struct riscv_bootparams *rvbp)
473 struct mem_region mem_regions[FDT_MEM_REGIONS];
476 vm_offset_t lastaddr;
484 TSRAW(&thread0, TS_ENTER, __func__, NULL);
486 /* Set the pcpu data, this is needed by pmap_bootstrap */
488 pcpu_init(pcpup, 0, sizeof(struct pcpu));
490 /* Set the pcpu pointer */
491 __asm __volatile("mv tp, %0" :: "r"(pcpup));
493 PCPU_SET(curthread, &thread0);
495 /* Initialize SBI interface. */
498 /* Parse the boot metadata. */
499 if (rvbp->modulep != 0) {
500 preload_metadata = (caddr_t)rvbp->modulep;
502 fake_preload_metadata(rvbp);
504 lastaddr = parse_metadata();
508 * Look for the boot hart ID. This was either passed in directly from
509 * the SBI firmware and handled by locore, or was stored in the device
510 * tree by an earlier boot stage.
512 chosen = OF_finddevice("/chosen");
513 if (OF_getencprop(chosen, "boot-hartid", &hart, sizeof(hart)) != -1) {
517 if (boot_hart == BOOT_HART_INVALID) {
518 panic("Boot hart ID was not properly set");
520 pcpup->pc_hart = boot_hart;
524 * Exclude reserved memory specified by the device tree. Typically,
525 * this contains an entry for memory used by the runtime SBI firmware.
527 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
528 physmem_exclude_regions(mem_regions, mem_regions_sz,
529 EXFLAG_NODUMP | EXFLAG_NOALLOC);
532 /* Grab physical memory regions information from device tree. */
533 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) {
534 panic("Cannot get physical memory regions");
536 physmem_hardware_regions(mem_regions, mem_regions_sz);
540 * Identify CPU/ISA features.
544 /* Do basic tuning, hz etc */
549 /* Bootstrap enough of pmap to enter the kernel proper */
550 kernlen = (lastaddr - KERNBASE);
551 pmap_bootstrap(rvbp->kern_l1pt, rvbp->kern_phys, kernlen);
555 * XXX: Unconditionally exclude the lowest 2MB of physical memory, as
556 * this area is assumed to contain the SBI firmware. This is a little
557 * fragile, but it is consistent with the platforms we support so far.
559 * TODO: remove this when the all regular booting methods properly
560 * report their reserved memory in the device tree.
562 physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
563 EXFLAG_NODUMP | EXFLAG_NOALLOC);
565 physmem_init_kernel_globals();
567 /* Establish static device mappings */
568 devmap_bootstrap(0, NULL);
573 * Dump the boot metadata. We have to wait for cninit() since console
574 * output is required. If it's grossly incorrect the kernel will never
577 if (getenv_is_true("debug.dump_modinfo_at_boot"))
580 init_proc0(rvbp->kern_stack);
582 msgbufinit(msgbufp, msgbufsize);
584 init_param2(physmem);
587 if ((boothowto & RB_KDB) != 0)
588 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
591 env = kern_getenv("kernelname");
593 strlcpy(kernelname, env, sizeof(kernelname));
595 if (boothowto & RB_VERBOSE)
596 physmem_print_tables();