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_kstack_pages.h" 37 #include "opt_platform.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/boot.h> 42 #include <sys/buf.h> 43 #include <sys/bus.h> 44 #include <sys/cons.h> 45 #include <sys/cpu.h> 46 #include <sys/devmap.h> 47 #include <sys/exec.h> 48 #include <sys/imgact.h> 49 #include <sys/kdb.h> 50 #include <sys/kernel.h> 51 #include <sys/ktr.h> 52 #include <sys/limits.h> 53 #include <sys/linker.h> 54 #include <sys/msgbuf.h> 55 #include <sys/pcpu.h> 56 #include <sys/physmem.h> 57 #include <sys/proc.h> 58 #include <sys/ptrace.h> 59 #include <sys/reboot.h> 60 #include <sys/reg.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> 70 71 #include <vm/vm.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> 77 #include <vm/pmap.h> 78 #include <vm/vm_map.h> 79 #include <vm/vm_pager.h> 80 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> 93 94 #ifdef FDT 95 #include <contrib/libfdt/libfdt.h> 96 #include <dev/fdt/fdt_common.h> 97 #include <dev/ofw/openfirm.h> 98 #endif 99 100 struct pcpu __pcpu[MAXCPU]; 101 102 static struct trapframe proc0_tf; 103 104 int early_boot = 1; 105 int cold = 1; 106 107 #define DTB_SIZE_MAX (1024 * 1024) 108 109 struct kva_md_info kmi; 110 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 */ 114 115 #define BOOT_HART_INVALID 0xffffffff 116 uint32_t boot_hart = BOOT_HART_INVALID; /* The hart we booted on. */ 117 118 cpuset_t all_harts; 119 120 extern int *end; 121 122 static char static_kenv[PAGE_SIZE]; 123 124 static void 125 cpu_startup(void *dummy) 126 { 127 128 sbi_print_version(); 129 printcpuinfo(0); 130 131 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem), 132 ptoa((uintmax_t)realmem) / (1024 * 1024)); 133 134 /* 135 * Display any holes after the first chunk of extended memory. 136 */ 137 if (bootverbose) { 138 int indx; 139 140 printf("Physical memory chunk(s):\n"); 141 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) { 142 vm_paddr_t size; 143 144 size = phys_avail[indx + 1] - phys_avail[indx]; 145 printf( 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); 150 } 151 } 152 153 vm_ksubmap_init(&kmi); 154 155 printf("avail memory = %ju (%ju MB)\n", 156 ptoa((uintmax_t)vm_free_count()), 157 ptoa((uintmax_t)vm_free_count()) / (1024 * 1024)); 158 if (bootverbose) 159 devmap_print_table(); 160 161 bufinit(); 162 vm_pager_bufferinit(); 163 } 164 165 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL); 166 167 int 168 cpu_idle_wakeup(int cpu) 169 { 170 171 return (0); 172 } 173 174 void 175 cpu_idle(int busy) 176 { 177 178 spinlock_enter(); 179 if (!busy) 180 cpu_idleclock(); 181 if (!sched_runnable()) 182 __asm __volatile( 183 "fence \n" 184 "wfi \n"); 185 if (!busy) 186 cpu_activeclock(); 187 spinlock_exit(); 188 } 189 190 void 191 cpu_halt(void) 192 { 193 194 /* 195 * Try to power down using the HSM SBI extension and fall back to a 196 * simple wfi loop. 197 */ 198 intr_disable(); 199 if (sbi_probe_extension(SBI_EXT_ID_HSM) != 0) 200 sbi_hsm_hart_stop(); 201 for (;;) 202 __asm __volatile("wfi"); 203 /* NOTREACHED */ 204 } 205 206 /* 207 * Flush the D-cache for non-DMA I/O so that the I-cache can 208 * be made coherent later. 209 */ 210 void 211 cpu_flush_dcache(void *ptr, size_t len) 212 { 213 214 /* TBD */ 215 } 216 217 /* Get current clock frequency for the given CPU ID. */ 218 int 219 cpu_est_clockrate(int cpu_id, uint64_t *rate) 220 { 221 222 panic("cpu_est_clockrate"); 223 } 224 225 void 226 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) 227 { 228 } 229 230 void 231 spinlock_enter(void) 232 { 233 struct thread *td; 234 register_t reg; 235 236 td = curthread; 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; 241 critical_enter(); 242 } else 243 td->td_md.md_spinlock_count++; 244 } 245 246 void 247 spinlock_exit(void) 248 { 249 struct thread *td; 250 register_t sstatus_ie; 251 252 td = curthread; 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) { 256 critical_exit(); 257 intr_restore(sstatus_ie); 258 } 259 } 260 261 /* 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. 267 */ 268 void 269 makectx(struct trapframe *tf, struct pcb *pcb) 270 { 271 272 memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s)); 273 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; 278 } 279 280 static void 281 init_proc0(vm_offset_t kstack) 282 { 283 struct pcpu *pcpup; 284 285 pcpup = &__pcpu[0]; 286 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; 295 } 296 297 #ifdef FDT 298 static void 299 try_load_dtb(caddr_t kmdp) 300 { 301 vm_offset_t dtbp; 302 303 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t); 304 305 #if defined(FDT_DTB_STATIC) 306 /* 307 * In case the device tree blob was not retrieved (from metadata) try 308 * to use the statically embedded one. 309 */ 310 if (dtbp == (vm_offset_t)NULL) 311 dtbp = (vm_offset_t)&fdt_static_dtb; 312 #endif 313 314 if (dtbp == (vm_offset_t)NULL) { 315 printf("ERROR loading DTB\n"); 316 return; 317 } 318 319 if (OF_install(OFW_FDT, 0) == FALSE) 320 panic("Cannot install FDT"); 321 322 if (OF_init((void *)dtbp) != 0) 323 panic("OF_init failed with the found device tree"); 324 } 325 #endif 326 327 static void 328 cache_setup(void) 329 { 330 331 /* TODO */ 332 333 dcache_line_size = 0; 334 icache_line_size = 0; 335 idcache_line_size = 0; 336 } 337 338 /* 339 * Fake up a boot descriptor table. 340 */ 341 static void 342 fake_preload_metadata(struct riscv_bootparams *rvbp) 343 { 344 static uint32_t fake_preload[48]; 345 vm_offset_t lastaddr; 346 size_t fake_size, dtb_size; 347 348 #define PRELOAD_PUSH_VALUE(type, value) do { \ 349 *(type *)((char *)fake_preload + fake_size) = (value); \ 350 fake_size += sizeof(type); \ 351 } while (0) 352 353 #define PRELOAD_PUSH_STRING(str) do { \ 354 uint32_t ssize; \ 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)); \ 360 } while (0) 361 362 fake_size = 0; 363 lastaddr = (vm_offset_t)&end; 364 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"); 369 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); 373 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)); 377 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)); 386 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); 390 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); 394 395 /* End marker */ 396 PRELOAD_PUSH_VALUE(uint32_t, 0); 397 PRELOAD_PUSH_VALUE(uint32_t, 0); 398 preload_metadata = (caddr_t)fake_preload; 399 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")); 408 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)); 413 } 414 415 /* Support for FDT configurations only. */ 416 CTASSERT(FDT); 417 418 #ifdef FDT 419 static void 420 parse_fdt_bootargs(void) 421 { 422 char bootargs[512]; 423 424 bootargs[sizeof(bootargs) - 1] = '\0'; 425 if (fdt_get_chosen_bootargs(bootargs, sizeof(bootargs) - 1) == 0) { 426 boothowto |= boot_parse_cmdline(bootargs); 427 } 428 } 429 #endif 430 431 static vm_offset_t 432 parse_metadata(void) 433 { 434 caddr_t kmdp; 435 vm_offset_t lastaddr; 436 #ifdef DDB 437 vm_offset_t ksym_start, ksym_end; 438 #endif 439 char *kern_envp; 440 441 /* Find the kernel address */ 442 kmdp = preload_search_by_type("elf kernel"); 443 if (kmdp == NULL) 444 kmdp = preload_search_by_type("elf64 kernel"); 445 KASSERT(kmdp != NULL, ("No preload metadata found!")); 446 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); 453 else 454 init_static_kenv(static_kenv, sizeof(static_kenv)); 455 #ifdef DDB 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); 459 #endif 460 #ifdef FDT 461 try_load_dtb(kmdp); 462 if (kern_envp == NULL) 463 parse_fdt_bootargs(); 464 #endif 465 return (lastaddr); 466 } 467 468 void 469 initriscv(struct riscv_bootparams *rvbp) 470 { 471 struct mem_region mem_regions[FDT_MEM_REGIONS]; 472 struct pcpu *pcpup; 473 int mem_regions_sz; 474 vm_offset_t lastaddr; 475 vm_size_t kernlen; 476 #ifdef FDT 477 phandle_t chosen; 478 uint32_t hart; 479 #endif 480 char *env; 481 482 TSRAW(&thread0, TS_ENTER, __func__, NULL); 483 484 /* Set the pcpu data, this is needed by pmap_bootstrap */ 485 pcpup = &__pcpu[0]; 486 pcpu_init(pcpup, 0, sizeof(struct pcpu)); 487 488 /* Set the pcpu pointer */ 489 __asm __volatile("mv tp, %0" :: "r"(pcpup)); 490 491 PCPU_SET(curthread, &thread0); 492 493 /* Initialize SBI interface. */ 494 sbi_init(); 495 496 /* Parse the boot metadata. */ 497 if (rvbp->modulep != 0) { 498 preload_metadata = (caddr_t)rvbp->modulep; 499 } else { 500 fake_preload_metadata(rvbp); 501 } 502 lastaddr = parse_metadata(); 503 504 #ifdef FDT 505 /* 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. 509 */ 510 chosen = OF_finddevice("/chosen"); 511 if (OF_getencprop(chosen, "boot-hartid", &hart, sizeof(hart)) != -1) { 512 boot_hart = hart; 513 } 514 #endif 515 if (boot_hart == BOOT_HART_INVALID) { 516 panic("Boot hart ID was not properly set"); 517 } 518 pcpup->pc_hart = boot_hart; 519 520 #ifdef FDT 521 /* 522 * Exclude reserved memory specified by the device tree. Typically, 523 * this contains an entry for memory used by the runtime SBI firmware. 524 */ 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); 528 } 529 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"); 533 } 534 physmem_hardware_regions(mem_regions, mem_regions_sz); 535 #endif 536 537 /* 538 * Identify CPU/ISA features. 539 */ 540 identify_cpu(0); 541 542 /* Do basic tuning, hz etc */ 543 init_param1(); 544 545 cache_setup(); 546 547 /* Bootstrap enough of pmap to enter the kernel proper */ 548 kernlen = (lastaddr - KERNBASE); 549 pmap_bootstrap(rvbp->kern_l1pt, rvbp->kern_phys, kernlen); 550 551 #ifdef FDT 552 /* 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. 556 * 557 * TODO: remove this when the all regular booting methods properly 558 * report their reserved memory in the device tree. 559 */ 560 physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE, 561 EXFLAG_NODUMP | EXFLAG_NOALLOC); 562 #endif 563 physmem_init_kernel_globals(); 564 565 /* Establish static device mappings */ 566 devmap_bootstrap(0, NULL); 567 568 cninit(); 569 570 /* 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 573 * make it this far. 574 */ 575 if (getenv_is_true("debug.dump_modinfo_at_boot")) 576 preload_dump(); 577 578 init_proc0(rvbp->kern_stack); 579 580 msgbufinit(msgbufp, msgbufsize); 581 mutex_init(); 582 init_param2(physmem); 583 kdb_init(); 584 #ifdef KDB 585 if ((boothowto & RB_KDB) != 0) 586 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger"); 587 #endif 588 589 env = kern_getenv("kernelname"); 590 if (env != NULL) 591 strlcpy(kernelname, env, sizeof(kernelname)); 592 593 if (boothowto & RB_VERBOSE) 594 physmem_print_tables(); 595 596 early_boot = 0; 597 598 if (bootverbose && kstack_pages != KSTACK_PAGES) 599 printf("kern.kstack_pages = %d ignored for thread0\n", 600 kstack_pages); 601 602 TSEXIT(); 603 } 604