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