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_platform.h" 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/boot.h> 44 #include <sys/buf.h> 45 #include <sys/bus.h> 46 #include <sys/cons.h> 47 #include <sys/cpu.h> 48 #include <sys/devmap.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 FDT 97 #include <contrib/libfdt/libfdt.h> 98 #include <dev/fdt/fdt_common.h> 99 #include <dev/ofw/openfirm.h> 100 #endif 101 102 struct pcpu __pcpu[MAXCPU]; 103 104 static struct trapframe proc0_tf; 105 106 int early_boot = 1; 107 int cold = 1; 108 109 #define DTB_SIZE_MAX (1024 * 1024) 110 111 struct kva_md_info kmi; 112 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 */ 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 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 170 cpu_idle_wakeup(int cpu) 171 { 172 173 return (0); 174 } 175 176 void 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 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 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 221 cpu_est_clockrate(int cpu_id, uint64_t *rate) 222 { 223 224 panic("cpu_est_clockrate"); 225 } 226 227 void 228 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) 229 { 230 } 231 232 void 233 spinlock_enter(void) 234 { 235 struct thread *td; 236 register_t reg; 237 238 td = curthread; 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; 243 critical_enter(); 244 } else 245 td->td_md.md_spinlock_count++; 246 } 247 248 void 249 spinlock_exit(void) 250 { 251 struct thread *td; 252 register_t sstatus_ie; 253 254 td = curthread; 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) { 258 critical_exit(); 259 intr_restore(sstatus_ie); 260 } 261 } 262 263 /* 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. 269 */ 270 void 271 makectx(struct trapframe *tf, struct pcb *pcb) 272 { 273 274 memcpy(pcb->pcb_s, tf->tf_s, sizeof(tf->tf_s)); 275 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; 280 } 281 282 static void 283 init_proc0(vm_offset_t kstack) 284 { 285 struct pcpu *pcpup; 286 287 pcpup = &__pcpu[0]; 288 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; 297 } 298 299 #ifdef FDT 300 static void 301 try_load_dtb(caddr_t kmdp) 302 { 303 vm_offset_t dtbp; 304 305 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t); 306 307 #if defined(FDT_DTB_STATIC) 308 /* 309 * In case the device tree blob was not retrieved (from metadata) try 310 * to use the statically embedded one. 311 */ 312 if (dtbp == (vm_offset_t)NULL) 313 dtbp = (vm_offset_t)&fdt_static_dtb; 314 #endif 315 316 if (dtbp == (vm_offset_t)NULL) { 317 printf("ERROR loading DTB\n"); 318 return; 319 } 320 321 if (OF_install(OFW_FDT, 0) == FALSE) 322 panic("Cannot install FDT"); 323 324 if (OF_init((void *)dtbp) != 0) 325 panic("OF_init failed with the found device tree"); 326 } 327 #endif 328 329 static void 330 cache_setup(void) 331 { 332 333 /* TODO */ 334 335 dcache_line_size = 0; 336 icache_line_size = 0; 337 idcache_line_size = 0; 338 } 339 340 /* 341 * Fake up a boot descriptor table. 342 */ 343 static void 344 fake_preload_metadata(struct riscv_bootparams *rvbp) 345 { 346 static uint32_t fake_preload[48]; 347 vm_offset_t lastaddr; 348 size_t fake_size, dtb_size; 349 350 #define PRELOAD_PUSH_VALUE(type, value) do { \ 351 *(type *)((char *)fake_preload + fake_size) = (value); \ 352 fake_size += sizeof(type); \ 353 } while (0) 354 355 #define PRELOAD_PUSH_STRING(str) do { \ 356 uint32_t ssize; \ 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)); \ 362 } while (0) 363 364 fake_size = 0; 365 lastaddr = (vm_offset_t)&end; 366 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"); 371 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); 375 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)); 379 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)); 388 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); 392 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); 396 397 /* End marker */ 398 PRELOAD_PUSH_VALUE(uint32_t, 0); 399 PRELOAD_PUSH_VALUE(uint32_t, 0); 400 preload_metadata = (caddr_t)fake_preload; 401 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")); 410 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)); 415 } 416 417 /* Support for FDT configurations only. */ 418 CTASSERT(FDT); 419 420 #ifdef FDT 421 static void 422 parse_fdt_bootargs(void) 423 { 424 char bootargs[512]; 425 426 bootargs[sizeof(bootargs) - 1] = '\0'; 427 if (fdt_get_chosen_bootargs(bootargs, sizeof(bootargs) - 1) == 0) { 428 boothowto |= boot_parse_cmdline(bootargs); 429 } 430 } 431 #endif 432 433 static vm_offset_t 434 parse_metadata(void) 435 { 436 caddr_t kmdp; 437 vm_offset_t lastaddr; 438 #ifdef DDB 439 vm_offset_t ksym_start, ksym_end; 440 #endif 441 char *kern_envp; 442 443 /* Find the kernel address */ 444 kmdp = preload_search_by_type("elf kernel"); 445 if (kmdp == NULL) 446 kmdp = preload_search_by_type("elf64 kernel"); 447 KASSERT(kmdp != NULL, ("No preload metadata found!")); 448 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); 455 else 456 init_static_kenv(static_kenv, sizeof(static_kenv)); 457 #ifdef DDB 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); 461 #endif 462 #ifdef FDT 463 try_load_dtb(kmdp); 464 if (kern_envp == NULL) 465 parse_fdt_bootargs(); 466 #endif 467 return (lastaddr); 468 } 469 470 void 471 initriscv(struct riscv_bootparams *rvbp) 472 { 473 struct mem_region mem_regions[FDT_MEM_REGIONS]; 474 struct pcpu *pcpup; 475 int mem_regions_sz; 476 vm_offset_t lastaddr; 477 vm_size_t kernlen; 478 #ifdef FDT 479 phandle_t chosen; 480 uint32_t hart; 481 #endif 482 char *env; 483 484 TSRAW(&thread0, TS_ENTER, __func__, NULL); 485 486 /* Set the pcpu data, this is needed by pmap_bootstrap */ 487 pcpup = &__pcpu[0]; 488 pcpu_init(pcpup, 0, sizeof(struct pcpu)); 489 490 /* Set the pcpu pointer */ 491 __asm __volatile("mv tp, %0" :: "r"(pcpup)); 492 493 PCPU_SET(curthread, &thread0); 494 495 /* Initialize SBI interface. */ 496 sbi_init(); 497 498 /* Parse the boot metadata. */ 499 if (rvbp->modulep != 0) { 500 preload_metadata = (caddr_t)rvbp->modulep; 501 } else { 502 fake_preload_metadata(rvbp); 503 } 504 lastaddr = parse_metadata(); 505 506 #ifdef FDT 507 /* 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. 511 */ 512 chosen = OF_finddevice("/chosen"); 513 if (OF_getencprop(chosen, "boot-hartid", &hart, sizeof(hart)) != -1) { 514 boot_hart = hart; 515 } 516 #endif 517 if (boot_hart == BOOT_HART_INVALID) { 518 panic("Boot hart ID was not properly set"); 519 } 520 pcpup->pc_hart = boot_hart; 521 522 #ifdef FDT 523 /* 524 * Exclude reserved memory specified by the device tree. Typically, 525 * this contains an entry for memory used by the runtime SBI firmware. 526 */ 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); 530 } 531 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"); 535 } 536 physmem_hardware_regions(mem_regions, mem_regions_sz); 537 #endif 538 539 /* 540 * Identify CPU/ISA features. 541 */ 542 identify_cpu(0); 543 544 /* Do basic tuning, hz etc */ 545 init_param1(); 546 547 cache_setup(); 548 549 /* Bootstrap enough of pmap to enter the kernel proper */ 550 kernlen = (lastaddr - KERNBASE); 551 pmap_bootstrap(rvbp->kern_l1pt, rvbp->kern_phys, kernlen); 552 553 #ifdef FDT 554 /* 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. 558 * 559 * TODO: remove this when the all regular booting methods properly 560 * report their reserved memory in the device tree. 561 */ 562 physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE, 563 EXFLAG_NODUMP | EXFLAG_NOALLOC); 564 #endif 565 physmem_init_kernel_globals(); 566 567 /* Establish static device mappings */ 568 devmap_bootstrap(0, NULL); 569 570 cninit(); 571 572 /* 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 575 * make it this far. 576 */ 577 if (getenv_is_true("debug.dump_modinfo_at_boot")) 578 preload_dump(); 579 580 init_proc0(rvbp->kern_stack); 581 582 msgbufinit(msgbufp, msgbufsize); 583 mutex_init(); 584 init_param2(physmem); 585 kdb_init(); 586 #ifdef KDB 587 if ((boothowto & RB_KDB) != 0) 588 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger"); 589 #endif 590 591 env = kern_getenv("kernelname"); 592 if (env != NULL) 593 strlcpy(kernelname, env, sizeof(kernelname)); 594 595 if (boothowto & RB_VERBOSE) 596 physmem_print_tables(); 597 598 early_boot = 0; 599 600 TSEXIT(); 601 } 602