1 /*-
2 * Copyright (c) 2014 Andrew Turner
3 * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
4 * All rights reserved.
5 *
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.
9 *
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).
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 #include "opt_ddb.h"
37 #include "opt_kstack_pages.h"
38 #include "opt_platform.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/boot.h>
43 #include <sys/buf.h>
44 #include <sys/bus.h>
45 #include <sys/cons.h>
46 #include <sys/cpu.h>
47 #include <sys/devmap.h>
48 #include <sys/efi_map.h>
49 #include <sys/exec.h>
50 #include <sys/imgact.h>
51 #include <sys/kdb.h>
52 #include <sys/kernel.h>
53 #include <sys/ktr.h>
54 #include <sys/limits.h>
55 #include <sys/linker.h>
56 #include <sys/msgbuf.h>
57 #include <sys/pcpu.h>
58 #include <sys/physmem.h>
59 #include <sys/proc.h>
60 #include <sys/ptrace.h>
61 #include <sys/reboot.h>
62 #include <sys/reg.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>
72
73 #include <vm/vm.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>
79 #include <vm/pmap.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_pager.h>
82
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>
95
96 #ifdef DDB
97 #include <ddb/ddb.h>
98 #endif
99
100 #ifdef FDT
101 #include <contrib/libfdt/libfdt.h>
102 #include <dev/fdt/fdt_common.h>
103 #include <dev/ofw/openfirm.h>
104 #endif
105
106 struct pcpu __pcpu[MAXCPU];
107
108 static struct trapframe proc0_tf;
109
110 int early_boot = 1;
111 int cold = 1;
112
113 #define DTB_SIZE_MAX (1024 * 1024)
114
115 struct kva_md_info kmi;
116
117 #define BOOT_HART_INVALID 0xffffffff
118 uint32_t boot_hart = BOOT_HART_INVALID; /* The hart we booted on. */
119
120 cpuset_t all_harts;
121
122 extern int *end;
123
124 static char static_kenv[PAGE_SIZE];
125
126 static void
cpu_startup(void * dummy)127 cpu_startup(void *dummy)
128 {
129
130 sbi_print_version();
131 printcpuinfo(0);
132
133 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
134 ptoa((uintmax_t)realmem) / (1024 * 1024));
135
136 /*
137 * Display any holes after the first chunk of extended memory.
138 */
139 if (bootverbose) {
140 int indx;
141
142 printf("Physical memory chunk(s):\n");
143 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
144 vm_paddr_t size;
145
146 size = phys_avail[indx + 1] - phys_avail[indx];
147 printf(
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);
152 }
153 }
154
155 vm_ksubmap_init(&kmi);
156
157 printf("avail memory = %ju (%ju MB)\n",
158 ptoa((uintmax_t)vm_free_count()),
159 ptoa((uintmax_t)vm_free_count()) / (1024 * 1024));
160 if (bootverbose)
161 devmap_print_table();
162
163 bufinit();
164 vm_pager_bufferinit();
165 }
166
167 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
168
169 int
cpu_idle_wakeup(int cpu)170 cpu_idle_wakeup(int cpu)
171 {
172
173 return (0);
174 }
175
176 void
cpu_idle(int busy)177 cpu_idle(int busy)
178 {
179
180 spinlock_enter();
181 if (!busy)
182 cpu_idleclock();
183 if (!sched_runnable())
184 __asm __volatile(
185 "fence \n"
186 "wfi \n");
187 if (!busy)
188 cpu_activeclock();
189 spinlock_exit();
190 }
191
192 void
cpu_halt(void)193 cpu_halt(void)
194 {
195
196 /*
197 * Try to power down using the HSM SBI extension and fall back to a
198 * simple wfi loop.
199 */
200 intr_disable();
201 if (sbi_probe_extension(SBI_EXT_ID_HSM) != 0)
202 sbi_hsm_hart_stop();
203 for (;;)
204 __asm __volatile("wfi");
205 /* NOTREACHED */
206 }
207
208 /*
209 * Flush the D-cache for non-DMA I/O so that the I-cache can
210 * be made coherent later.
211 */
212 void
cpu_flush_dcache(void * ptr,size_t len)213 cpu_flush_dcache(void *ptr, size_t len)
214 {
215
216 /* TBD */
217 }
218
219 /* Get current clock frequency for the given CPU ID. */
220 int
cpu_est_clockrate(int cpu_id,uint64_t * rate)221 cpu_est_clockrate(int cpu_id, uint64_t *rate)
222 {
223 struct pcpu *pc;
224
225 pc = pcpu_find(cpu_id);
226 if (pc == NULL || rate == NULL)
227 return (EINVAL);
228
229 if (pc->pc_clock == 0)
230 return (EOPNOTSUPP);
231
232 *rate = pc->pc_clock;
233
234 return (0);
235 }
236
237 void
cpu_pcpu_init(struct pcpu * pcpu,int cpuid,size_t size)238 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
239 {
240 }
241
242 void
spinlock_enter(void)243 spinlock_enter(void)
244 {
245 struct thread *td;
246 register_t reg;
247
248 td = curthread;
249 if (td->td_md.md_spinlock_count == 0) {
250 reg = intr_disable();
251 td->td_md.md_spinlock_count = 1;
252 td->td_md.md_saved_sstatus_ie = reg;
253 critical_enter();
254 } else
255 td->td_md.md_spinlock_count++;
256 }
257
258 void
spinlock_exit(void)259 spinlock_exit(void)
260 {
261 struct thread *td;
262 register_t sstatus_ie;
263
264 td = curthread;
265 sstatus_ie = td->td_md.md_saved_sstatus_ie;
266 td->td_md.md_spinlock_count--;
267 if (td->td_md.md_spinlock_count == 0) {
268 critical_exit();
269 intr_restore(sstatus_ie);
270 }
271 }
272
273 /*
274 * Construct a PCB from a trapframe. This is called from kdb_trap() where
275 * we want to start a backtrace from the function that caused us to enter
276 * the debugger. We have the context in the trapframe, but base the trace
277 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
278 * enough for a backtrace.
279 */
280 void
makectx(struct trapframe * tf,struct pcb * pcb)281 makectx(struct trapframe *tf, struct pcb *pcb)
282 {
283
284 memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s));
285
286 pcb->pcb_ra = tf->tf_sepc;
287 pcb->pcb_sp = tf->tf_sp;
288 pcb->pcb_gp = tf->tf_gp;
289 pcb->pcb_tp = tf->tf_tp;
290 }
291
292 static void
init_proc0(vm_offset_t kstack)293 init_proc0(vm_offset_t kstack)
294 {
295 struct pcpu *pcpup;
296
297 pcpup = &__pcpu[0];
298
299 proc_linkup0(&proc0, &thread0);
300 thread0.td_kstack = kstack;
301 thread0.td_kstack_pages = KSTACK_PAGES;
302 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
303 thread0.td_kstack_pages * PAGE_SIZE) - 1;
304 thread0.td_pcb->pcb_fpflags = 0;
305 thread0.td_frame = &proc0_tf;
306 pcpup->pc_curpcb = thread0.td_pcb;
307 }
308
309 #ifdef FDT
310 static void
try_load_dtb(void)311 try_load_dtb(void)
312 {
313 vm_offset_t dtbp;
314
315 dtbp = MD_FETCH(preload_kmdp, MODINFOMD_DTBP, vm_offset_t);
316
317 #if defined(FDT_DTB_STATIC)
318 /*
319 * In case the device tree blob was not retrieved (from metadata) try
320 * to use the statically embedded one.
321 */
322 if (dtbp == (vm_offset_t)NULL)
323 dtbp = (vm_offset_t)&fdt_static_dtb;
324 #endif
325
326 if (dtbp == (vm_offset_t)NULL) {
327 printf("ERROR loading DTB\n");
328 return;
329 }
330
331 if (!OF_install(OFW_FDT, 0))
332 panic("Cannot install FDT");
333
334 if (OF_init((void *)dtbp) != 0)
335 panic("OF_init failed with the found device tree");
336 }
337 #endif
338
339 /*
340 * Fake up a boot descriptor table.
341 */
342 static void
fake_preload_metadata(struct riscv_bootparams * rvbp)343 fake_preload_metadata(struct riscv_bootparams *rvbp)
344 {
345 static uint32_t fake_preload[48];
346 vm_offset_t lastaddr;
347 size_t fake_size, dtb_size;
348
349 #define PRELOAD_PUSH_VALUE(type, value) do { \
350 *(type *)((char *)fake_preload + fake_size) = (value); \
351 fake_size += sizeof(type); \
352 } while (0)
353
354 #define PRELOAD_PUSH_STRING(str) do { \
355 uint32_t ssize; \
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)); \
361 } while (0)
362
363 fake_size = 0;
364 lastaddr = (vm_offset_t)&end;
365
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(preload_kerntype);
370
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);
374
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));
378
379 /*
380 * Copy the DTB to KVA space. We are able to dereference the physical
381 * address due to the identity map created in locore.
382 */
383 lastaddr = roundup(lastaddr, sizeof(int));
384 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_DTBP);
385 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
386 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
387 dtb_size = fdt_totalsize(rvbp->dtbp_phys);
388 memmove((void *)lastaddr, (const void *)rvbp->dtbp_phys, dtb_size);
389 lastaddr = roundup(lastaddr + dtb_size, sizeof(int));
390
391 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_KERNEND);
392 PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
393 PRELOAD_PUSH_VALUE(vm_offset_t, lastaddr);
394
395 PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_HOWTO);
396 PRELOAD_PUSH_VALUE(uint32_t, sizeof(int));
397 PRELOAD_PUSH_VALUE(int, RB_VERBOSE);
398
399 /* End marker */
400 PRELOAD_PUSH_VALUE(uint32_t, 0);
401 PRELOAD_PUSH_VALUE(uint32_t, 0);
402 preload_metadata = (caddr_t)fake_preload;
403
404 /* Check if bootloader clobbered part of the kernel with the DTB. */
405 KASSERT(rvbp->dtbp_phys + dtb_size <= rvbp->kern_phys ||
406 rvbp->dtbp_phys >= rvbp->kern_phys + (lastaddr - KERNBASE),
407 ("FDT (%lx-%lx) and kernel (%lx-%lx) overlap", rvbp->dtbp_phys,
408 rvbp->dtbp_phys + dtb_size, rvbp->kern_phys,
409 rvbp->kern_phys + (lastaddr - KERNBASE)));
410 KASSERT(fake_size < sizeof(fake_preload),
411 ("Too many fake_preload items"));
412
413 if (boothowto & RB_VERBOSE)
414 printf("FDT phys (%lx-%lx), kernel phys (%lx-%lx)\n",
415 rvbp->dtbp_phys, rvbp->dtbp_phys + dtb_size,
416 rvbp->kern_phys, rvbp->kern_phys + (lastaddr - KERNBASE));
417 }
418
419 /* Support for FDT configurations only. */
420 CTASSERT(FDT);
421
422 static void
parse_boot_hartid(void)423 parse_boot_hartid(void)
424 {
425 uint64_t *mdp;
426 #ifdef FDT
427 phandle_t chosen;
428 uint32_t hart;
429 #endif
430
431 mdp = (uint64_t *)preload_search_info(preload_kmdp,
432 MODINFO_METADATA | MODINFOMD_BOOT_HARTID);
433 if (mdp != NULL && *mdp < UINT32_MAX) {
434 boot_hart = (uint32_t)*mdp;
435 goto out;
436 }
437
438 #ifdef FDT
439 /*
440 * Deprecated:
441 *
442 * Look for the boot hart ID. This was either passed in directly from
443 * the SBI firmware and handled by locore, or was stored in the device
444 * tree by an earlier boot stage.
445 */
446 chosen = OF_finddevice("/chosen");
447 if (OF_getencprop(chosen, "boot-hartid", &hart, sizeof(hart)) != -1) {
448 boot_hart = hart;
449 }
450 #endif
451
452 /* We failed... */
453 if (boot_hart == BOOT_HART_INVALID) {
454 panic("Boot hart ID was not properly set");
455 }
456
457 out:
458 PCPU_SET(hart, boot_hart);
459 }
460
461 #ifdef FDT
462 static void
parse_fdt_bootargs(void)463 parse_fdt_bootargs(void)
464 {
465 char bootargs[512];
466
467 bootargs[sizeof(bootargs) - 1] = '\0';
468 if (fdt_get_chosen_bootargs(bootargs, sizeof(bootargs) - 1) == 0) {
469 boothowto |= boot_parse_cmdline(bootargs);
470 }
471 }
472 #endif
473
474 static vm_offset_t
parse_metadata(void)475 parse_metadata(void)
476 {
477 vm_offset_t lastaddr;
478 #ifdef DDB
479 vm_offset_t ksym_start, ksym_end;
480 #endif
481 char *kern_envp;
482
483 /* Initialize preload_kmdp */
484 preload_initkmdp(true);
485
486 /* Read the boot metadata */
487 boothowto = MD_FETCH(preload_kmdp, MODINFOMD_HOWTO, int);
488 lastaddr = MD_FETCH(preload_kmdp, MODINFOMD_KERNEND, vm_offset_t);
489 kern_envp = MD_FETCH(preload_kmdp, MODINFOMD_ENVP, char *);
490 if (kern_envp != NULL)
491 init_static_kenv(kern_envp, 0);
492 else
493 init_static_kenv(static_kenv, sizeof(static_kenv));
494 #ifdef DDB
495 ksym_start = MD_FETCH(preload_kmdp, MODINFOMD_SSYM, uintptr_t);
496 ksym_end = MD_FETCH(preload_kmdp, MODINFOMD_ESYM, uintptr_t);
497 db_fetch_ksymtab(ksym_start, ksym_end, 0);
498 #endif
499 #ifdef FDT
500 try_load_dtb();
501 if (kern_envp == NULL)
502 parse_fdt_bootargs();
503 #endif
504 parse_boot_hartid();
505
506 return (lastaddr);
507 }
508
509 void
initriscv(struct riscv_bootparams * rvbp)510 initriscv(struct riscv_bootparams *rvbp)
511 {
512 struct mem_region mem_regions[FDT_MEM_REGIONS];
513 struct efi_map_header *efihdr;
514 struct pcpu *pcpup;
515 int mem_regions_sz;
516 vm_offset_t lastaddr;
517 vm_size_t kernlen;
518 char *env;
519
520 TSRAW(&thread0, TS_ENTER, __func__, NULL);
521
522 /* Set the pcpu data, this is needed by pmap_bootstrap */
523 pcpup = &__pcpu[0];
524 pcpu_init(pcpup, 0, sizeof(struct pcpu));
525
526 /* Set the pcpu pointer */
527 __asm __volatile("mv tp, %0" :: "r"(pcpup));
528
529 PCPU_SET(curthread, &thread0);
530
531 /* Initialize SBI interface. */
532 sbi_init();
533
534 /* Parse the boot metadata. */
535 if (rvbp->modulep != 0) {
536 preload_metadata = (caddr_t)rvbp->modulep;
537 } else {
538 fake_preload_metadata(rvbp);
539 }
540 lastaddr = parse_metadata();
541
542 efihdr = (struct efi_map_header *)preload_search_info(preload_kmdp,
543 MODINFO_METADATA | MODINFOMD_EFI_MAP);
544 if (efihdr != NULL) {
545 efi_map_add_entries(efihdr);
546 efi_map_exclude_entries(efihdr);
547 }
548 #ifdef FDT
549 else {
550 /* Exclude reserved memory specified by the device tree. */
551 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
552 physmem_exclude_regions(mem_regions, mem_regions_sz,
553 EXFLAG_NODUMP | EXFLAG_NOALLOC);
554 }
555
556 /* Grab physical memory regions information from device tree. */
557 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0)
558 panic("Cannot get physical memory regions");
559 physmem_hardware_regions(mem_regions, mem_regions_sz);
560
561 /*
562 * XXX: Unconditionally exclude the lowest 2MB of physical
563 * memory, as this area is assumed to contain the SBI firmware,
564 * and this is not properly reserved in all cases (e.g. in
565 * older firmware like BBL).
566 *
567 * This is a little fragile, but it is consistent with the
568 * platforms we support so far.
569 *
570 * TODO: remove this when the all regular booting methods
571 * properly report their reserved memory in the device tree.
572 */
573 physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
574 EXFLAG_NODUMP | EXFLAG_NOALLOC);
575 }
576 #endif
577
578 /*
579 * Identify CPU/ISA features.
580 */
581 identify_cpu(0);
582
583 /* Do basic tuning, hz etc */
584 init_param1();
585
586 /* Bootstrap enough of pmap to enter the kernel proper */
587 kernlen = (lastaddr - KERNBASE);
588 pmap_bootstrap(rvbp->kern_phys, kernlen);
589
590 physmem_init_kernel_globals();
591
592 /* Establish static device mappings */
593 devmap_bootstrap();
594
595 cninit();
596
597 /*
598 * Dump the boot metadata. We have to wait for cninit() since console
599 * output is required. If it's grossly incorrect the kernel will never
600 * make it this far.
601 */
602 if (getenv_is_true("debug.dump_modinfo_at_boot"))
603 preload_dump();
604
605 init_proc0(rvbp->kern_stack);
606
607 msgbufinit(msgbufp, msgbufsize);
608 mutex_init();
609 init_param2(physmem);
610 kdb_init();
611 #ifdef KDB
612 if ((boothowto & RB_KDB) != 0)
613 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
614 #endif
615
616 env = kern_getenv("kernelname");
617 if (env != NULL)
618 strlcpy(kernelname, env, sizeof(kernelname));
619
620 if (boothowto & RB_VERBOSE) {
621 if (efihdr != NULL)
622 efi_map_print_entries(efihdr);
623 physmem_print_tables();
624 }
625
626 early_boot = 0;
627
628 if (bootverbose && kstack_pages != KSTACK_PAGES)
629 printf("kern.kstack_pages = %d ignored for thread0\n",
630 kstack_pages);
631
632 TSEXIT();
633 }
634