2 * Copyright (c) 2014 Andrew Turner
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include "opt_platform.h"
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
43 #include <sys/devmap.h>
46 #include <sys/imgact.h>
48 #include <sys/kernel.h>
50 #include <sys/limits.h>
51 #include <sys/linker.h>
52 #include <sys/msgbuf.h>
54 #include <sys/physmem.h>
56 #include <sys/ptrace.h>
57 #include <sys/reboot.h>
59 #include <sys/rwlock.h>
60 #include <sys/sched.h>
61 #include <sys/signalvar.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysent.h>
64 #include <sys/sysproto.h>
65 #include <sys/ucontext.h>
67 #include <sys/vmmeter.h>
70 #include <vm/vm_param.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_phys.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_pager.h>
79 #include <machine/armreg.h>
80 #include <machine/cpu.h>
81 #include <machine/debug_monitor.h>
82 #include <machine/hypervisor.h>
83 #include <machine/kdb.h>
84 #include <machine/machdep.h>
85 #include <machine/metadata.h>
86 #include <machine/md_var.h>
87 #include <machine/pcb.h>
88 #include <machine/undefined.h>
89 #include <machine/vmparam.h>
92 #include <machine/vfp.h>
96 #include <contrib/dev/acpica/include/acpi.h>
97 #include <machine/acpica_machdep.h>
101 #include <dev/fdt/fdt_common.h>
102 #include <dev/ofw/openfirm.h>
105 #include <dev/smbios/smbios.h>
107 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
110 * XXX: The .bss is assumed to be in the boot CPU NUMA domain. If not we
111 * could relocate this, but will need to keep the same virtual address as
112 * it's reverenced by the EARLY_COUNTER macro.
116 #if defined(PERTHREAD_SSP)
118 * The boot SSP canary. Will be replaced with a per-thread canary when
119 * scheduling has started.
121 uintptr_t boot_canary = 0x49a2d892bc05a0b1ul;
124 static struct trapframe proc0_tf;
129 static uint64_t hcr_el2;
131 struct kva_md_info kmi;
133 int64_t dczva_line_size; /* The size of cache line the dc zva zeroes */
137 * Physical address of the EFI System Table. Stashed from the metadata hints
138 * passed into the kernel and used by the EFI code to call runtime services.
140 vm_paddr_t efi_systbl_phys;
141 static struct efi_map_header *efihdr;
143 /* pagezero_* implementations are provided in support.S */
144 void pagezero_simple(void *);
145 void pagezero_cache(void *);
147 /* pagezero_simple is default pagezero */
148 void (*pagezero)(void *p) = pagezero_simple;
150 int (*apei_nmi)(void);
152 #if defined(PERTHREAD_SSP_WARNING)
154 print_ssp_warning(void *data __unused)
156 printf("WARNING: Per-thread SSP is enabled but the compiler is too old to support it\n");
158 SYSINIT(ssp_warn, SI_SUB_COPYRIGHT, SI_ORDER_ANY, print_ssp_warning, NULL);
159 SYSINIT(ssp_warn2, SI_SUB_LAST, SI_ORDER_ANY, print_ssp_warning, NULL);
165 uint64_t id_aa64mfr1;
167 id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
168 if (ID_AA64MMFR1_PAN_VAL(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE)
177 * The LLVM integrated assembler doesn't understand the PAN
178 * PSTATE field. Because of this we need to manually create
179 * the instruction in an asm block. This is equivalent to:
182 * This sets the PAN bit, stopping the kernel from accessing
183 * memory when userspace can also access it unless the kernel
184 * uses the userspace load/store instructions.
187 WRITE_SPECIALREG(sctlr_el1,
188 READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
189 __asm __volatile(".inst 0xd500409f | (0x1 << 8)");
198 * XXX The E2H check is wrong, but it's close enough for now. Needs to
199 * be re-evaluated once we're running regularly in EL2.
201 return (boot_el == 2 && (hcr_el2 & HCR_E2H) == 0);
205 cpu_startup(void *dummy)
210 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
211 ptoa((uintmax_t)realmem) / 1024 / 1024);
214 printf("Physical memory chunk(s):\n");
215 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
216 size = phys_avail[i + 1] - phys_avail[i];
217 printf("%#016jx - %#016jx, %ju bytes (%ju pages)\n",
218 (uintmax_t)phys_avail[i],
219 (uintmax_t)phys_avail[i + 1] - 1,
220 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
224 printf("avail memory = %ju (%ju MB)\n",
225 ptoa((uintmax_t)vm_free_count()),
226 ptoa((uintmax_t)vm_free_count()) / 1024 / 1024);
229 install_cpu_errata();
231 vm_ksubmap_init(&kmi);
233 vm_pager_bufferinit();
236 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
239 late_ifunc_resolve(void *dummy __unused)
241 link_elf_late_ireloc();
243 SYSINIT(late_ifunc_resolve, SI_SUB_CPU, SI_ORDER_ANY, late_ifunc_resolve, NULL);
246 cpu_idle_wakeup(int cpu)
259 if (!sched_runnable())
272 /* We should have shutdown by now, if not enter a low power sleep */
275 __asm __volatile("wfi");
280 * Flush the D-cache for non-DMA I/O so that the I-cache can
281 * be made coherent later.
284 cpu_flush_dcache(void *ptr, size_t len)
290 /* Get current clock frequency for the given CPU ID. */
292 cpu_est_clockrate(int cpu_id, uint64_t *rate)
296 pc = pcpu_find(cpu_id);
297 if (pc == NULL || rate == NULL)
300 if (pc->pc_clock == 0)
303 *rate = pc->pc_clock;
308 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
311 pcpu->pc_acpi_id = 0xffffffff;
312 pcpu->pc_mpidr_low = 0xffffffff;
313 pcpu->pc_mpidr_high = 0xffffffff;
323 if (td->td_md.md_spinlock_count == 0) {
324 daif = intr_disable();
325 td->td_md.md_spinlock_count = 1;
326 td->td_md.md_saved_daif = daif;
329 td->td_md.md_spinlock_count++;
339 daif = td->td_md.md_saved_daif;
340 td->td_md.md_spinlock_count--;
341 if (td->td_md.md_spinlock_count == 0) {
348 * Construct a PCB from a trapframe. This is called from kdb_trap() where
349 * we want to start a backtrace from the function that caused us to enter
350 * the debugger. We have the context in the trapframe, but base the trace
351 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
352 * enough for a backtrace.
355 makectx(struct trapframe *tf, struct pcb *pcb)
359 for (i = 0; i < nitems(pcb->pcb_x); i++)
360 pcb->pcb_x[i] = tf->tf_x[i + PCB_X_START];
362 /* NB: pcb_x[PCB_LR] is the PC, see PC_REGS() in db_machdep.h */
363 pcb->pcb_x[PCB_LR] = tf->tf_elr;
364 pcb->pcb_sp = tf->tf_sp;
368 init_proc0(vm_offset_t kstack)
372 pcpup = cpuid_to_pcpu[0];
373 MPASS(pcpup != NULL);
375 proc_linkup0(&proc0, &thread0);
376 thread0.td_kstack = kstack;
377 thread0.td_kstack_pages = kstack_pages;
378 #if defined(PERTHREAD_SSP)
379 thread0.td_md.md_canary = boot_canary;
381 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
382 thread0.td_kstack_pages * PAGE_SIZE) - 1;
383 thread0.td_pcb->pcb_flags = 0;
384 thread0.td_pcb->pcb_fpflags = 0;
385 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
386 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
387 thread0.td_frame = &proc0_tf;
388 ptrauth_thread0(&thread0);
389 pcpup->pc_curpcb = thread0.td_pcb;
392 * Unmask SError exceptions. They are used to signal a RAS failure,
393 * or other hardware error.
399 * Get an address to be used to write to kernel data that may be mapped
400 * read-only, e.g. to patch kernel code.
403 arm64_get_writable_addr(vm_offset_t addr, vm_offset_t *out)
407 /* Check if the page is writable */
408 if (PAR_SUCCESS(arm64_address_translate_s1e1w(addr))) {
414 * Find the physical address of the given page.
416 if (!pmap_klookup(addr, &pa)) {
421 * If it is within the DMAP region and is writable use that.
423 if (PHYS_IN_DMAP(pa)) {
424 addr = PHYS_TO_DMAP(pa);
425 if (PAR_SUCCESS(arm64_address_translate_s1e1w(addr))) {
434 typedef void (*efi_map_entry_cb)(struct efi_md *, void *argp);
437 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb, void *argp)
439 struct efi_md *map, *p;
444 * Memory map data provided by UEFI via the GetMemoryMap
447 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
448 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
450 if (efihdr->descriptor_size == 0)
452 ndesc = efihdr->memory_size / efihdr->descriptor_size;
454 for (i = 0, p = map; i < ndesc; i++,
455 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
461 * Handle the EFI memory map list.
463 * We will make two passes at this, the first (exclude == false) to populate
464 * physmem with valid physical memory ranges from recognized map entry types.
465 * In the second pass we will exclude memory ranges from physmem which must not
466 * be used for general allocations, either because they are used by runtime
467 * firmware or otherwise reserved.
469 * Adding the runtime-reserved memory ranges to physmem and excluding them
470 * later ensures that they are included in the DMAP, but excluded from
473 * Entry types not explicitly listed here are ignored and not mapped.
476 handle_efi_map_entry(struct efi_md *p, void *argp)
478 bool exclude = *(bool *)argp;
480 switch (p->md_type) {
481 case EFI_MD_TYPE_RECLAIM:
483 * The recomended location for ACPI tables. Map into the
484 * DMAP so we can access them from userspace via /dev/mem.
486 case EFI_MD_TYPE_RT_CODE:
488 * Some UEFI implementations put the system table in the
489 * runtime code section. Include it in the DMAP, but will
490 * be excluded from phys_avail.
492 case EFI_MD_TYPE_RT_DATA:
494 * Runtime data will be excluded after the DMAP
495 * region is created to stop it from being added
499 physmem_exclude_region(p->md_phys,
500 p->md_pages * EFI_PAGE_SIZE, EXFLAG_NOALLOC);
504 case EFI_MD_TYPE_CODE:
505 case EFI_MD_TYPE_DATA:
506 case EFI_MD_TYPE_BS_CODE:
507 case EFI_MD_TYPE_BS_DATA:
508 case EFI_MD_TYPE_FREE:
510 * We're allowed to use any entry with these types.
513 physmem_hardware_region(p->md_phys,
514 p->md_pages * EFI_PAGE_SIZE);
517 /* Other types shall not be handled by physmem. */
523 add_efi_map_entries(struct efi_map_header *efihdr)
525 bool exclude = false;
526 foreach_efi_map_entry(efihdr, handle_efi_map_entry, &exclude);
530 exclude_efi_map_entries(struct efi_map_header *efihdr)
533 foreach_efi_map_entry(efihdr, handle_efi_map_entry, &exclude);
537 print_efi_map_entry(struct efi_md *p, void *argp __unused)
540 static const char *types[] = {
546 "RuntimeServicesCode",
547 "RuntimeServicesData",
548 "ConventionalMemory",
553 "MemoryMappedIOPortSpace",
558 if (p->md_type < nitems(types))
559 type = types[p->md_type];
562 printf("%23s %012lx %012lx %08lx ", type, p->md_phys,
563 p->md_virt, p->md_pages);
564 if (p->md_attr & EFI_MD_ATTR_UC)
566 if (p->md_attr & EFI_MD_ATTR_WC)
568 if (p->md_attr & EFI_MD_ATTR_WT)
570 if (p->md_attr & EFI_MD_ATTR_WB)
572 if (p->md_attr & EFI_MD_ATTR_UCE)
574 if (p->md_attr & EFI_MD_ATTR_WP)
576 if (p->md_attr & EFI_MD_ATTR_RP)
578 if (p->md_attr & EFI_MD_ATTR_XP)
580 if (p->md_attr & EFI_MD_ATTR_NV)
582 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
583 printf("MORE_RELIABLE ");
584 if (p->md_attr & EFI_MD_ATTR_RO)
586 if (p->md_attr & EFI_MD_ATTR_RT)
592 print_efi_map_entries(struct efi_map_header *efihdr)
595 printf("%23s %12s %12s %8s %4s\n",
596 "Type", "Physical", "Virtual", "#Pages", "Attr");
597 foreach_efi_map_entry(efihdr, print_efi_map_entry, NULL);
601 * Map the passed in VA in EFI space to a void * using the efi memory table to
602 * find the PA and return it in the DMAP, if it exists. We're used between the
603 * calls to pmap_bootstrap() and physmem_init_kernel_globals() to parse CFG
604 * tables We assume that either the entry you are mapping fits within its page,
605 * or if it spills to the next page, that's contiguous in PA and in the DMAP.
606 * All observed tables obey the first part of this precondition.
608 struct early_map_data
615 efi_early_map_entry(struct efi_md *p, void *argp)
617 struct early_map_data *emdp = argp;
622 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
625 e = p->md_virt + p->md_pages * EFI_PAGE_SIZE;
626 if (emdp->va < s || emdp->va >= e)
628 emdp->pa = p->md_phys + (emdp->va - p->md_virt);
632 efi_early_map(vm_offset_t va)
634 struct early_map_data emd = { .va = va };
636 foreach_efi_map_entry(efihdr, efi_early_map_entry, &emd);
639 return (void *)PHYS_TO_DMAP(emd.pa);
644 * When booted via kboot, the prior kernel will pass in reserved memory areas in
645 * a EFI config table. We need to find that table and walk through it excluding
646 * the memory ranges in it. btw, this is called too early for the printf to do
647 * anything since msgbufp isn't initialized, let alone a console...
650 exclude_efi_memreserve(vm_offset_t efi_systbl_phys)
652 struct efi_systbl *systbl;
653 struct uuid efi_memreserve = LINUX_EFI_MEMRESERVE_TABLE;
655 systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
656 if (systbl == NULL) {
657 printf("can't map systbl\n");
660 if (systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
661 printf("Bad signature for systbl %#lx\n", systbl->st_hdr.th_sig);
666 * We don't yet have the pmap system booted enough to create a pmap for
667 * the efi firmware's preferred address space from the GetMemoryMap()
668 * table. The st_cfgtbl is a VA in this space, so we need to do the
669 * mapping ourselves to a kernel VA with efi_early_map. We assume that
670 * the cfgtbl entries don't span a page. Other pointers are PAs, as
673 if (systbl->st_cfgtbl == 0) /* Failsafe st_entries should == 0 in this case */
675 for (int i = 0; i < systbl->st_entries; i++) {
676 struct efi_cfgtbl *cfgtbl;
677 struct linux_efi_memreserve *mr;
679 cfgtbl = efi_early_map(systbl->st_cfgtbl + i * sizeof(*cfgtbl));
681 panic("Can't map the config table entry %d\n", i);
682 if (memcmp(&cfgtbl->ct_uuid, &efi_memreserve, sizeof(struct uuid)) != 0)
686 * cfgtbl points are either VA or PA, depending on the GUID of
687 * the table. memreserve GUID pointers are PA and not converted
688 * after a SetVirtualAddressMap(). The list's mr_next pointer
691 mr = (struct linux_efi_memreserve *)PHYS_TO_DMAP(
692 (vm_offset_t)cfgtbl->ct_data);
694 for (int j = 0; j < mr->mr_count; j++) {
695 struct linux_efi_memreserve_entry *mre;
697 mre = &mr->mr_entry[j];
698 physmem_exclude_region(mre->mre_base, mre->mre_size,
699 EXFLAG_NODUMP | EXFLAG_NOALLOC);
701 if (mr->mr_next == 0)
703 mr = (struct linux_efi_memreserve *)PHYS_TO_DMAP(mr->mr_next);
711 try_load_dtb(caddr_t kmdp)
715 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
716 #if defined(FDT_DTB_STATIC)
718 * In case the device tree blob was not retrieved (from metadata) try
719 * to use the statically embedded one.
722 dtbp = (vm_offset_t)&fdt_static_dtb;
725 if (dtbp == (vm_offset_t)NULL) {
727 printf("ERROR loading DTB\n");
732 if (OF_install(OFW_FDT, 0) == FALSE)
733 panic("Cannot install FDT");
735 if (OF_init((void *)dtbp) != 0)
736 panic("OF_init failed with the found device tree");
738 parse_fdt_bootargs();
745 bool has_acpi, has_fdt;
748 has_acpi = has_fdt = false;
751 has_fdt = (OF_peer(0) != 0);
754 has_acpi = (AcpiOsGetRootPointer() != 0);
757 env = kern_getenv("kern.cfg.order");
760 while (order != NULL) {
762 strncmp(order, "acpi", 4) == 0 &&
763 (order[4] == ',' || order[4] == '\0')) {
764 arm64_bus_method = ARM64_BUS_ACPI;
768 strncmp(order, "fdt", 3) == 0 &&
769 (order[3] == ',' || order[3] == '\0')) {
770 arm64_bus_method = ARM64_BUS_FDT;
773 order = strchr(order, ',');
775 order++; /* Skip comma */
779 /* If we set the bus method it is valid */
780 if (arm64_bus_method != ARM64_BUS_NONE)
783 /* If no order or an invalid order was set use the default */
784 if (arm64_bus_method == ARM64_BUS_NONE) {
786 arm64_bus_method = ARM64_BUS_FDT;
788 arm64_bus_method = ARM64_BUS_ACPI;
792 * If no option was set the default is valid, otherwise we are
793 * setting one to get cninit() working, then calling panic to tell
794 * the user about the invalid bus setup.
796 return (env == NULL);
802 int dczva_line_shift;
805 identify_cache(READ_SPECIALREG(ctr_el0));
807 dczid_el0 = READ_SPECIALREG(dczid_el0);
809 /* Check if dc zva is not prohibited */
810 if (dczid_el0 & DCZID_DZP)
813 /* Same as with above calculations */
814 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
815 dczva_line_size = sizeof(int) << dczva_line_shift;
817 /* Change pagezero function */
818 pagezero = pagezero_cache;
823 memory_mapping_mode(vm_paddr_t pa)
825 struct efi_md *map, *p;
830 return (VM_MEMATTR_WRITE_BACK);
833 * Memory map data provided by UEFI via the GetMemoryMap
836 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
837 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
839 if (efihdr->descriptor_size == 0)
840 return (VM_MEMATTR_WRITE_BACK);
841 ndesc = efihdr->memory_size / efihdr->descriptor_size;
843 for (i = 0, p = map; i < ndesc; i++,
844 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
845 if (pa < p->md_phys ||
846 pa >= p->md_phys + p->md_pages * EFI_PAGE_SIZE)
848 if (p->md_type == EFI_MD_TYPE_IOMEM ||
849 p->md_type == EFI_MD_TYPE_IOPORT)
850 return (VM_MEMATTR_DEVICE);
851 else if ((p->md_attr & EFI_MD_ATTR_WB) != 0 ||
852 p->md_type == EFI_MD_TYPE_RECLAIM)
853 return (VM_MEMATTR_WRITE_BACK);
854 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
855 return (VM_MEMATTR_WRITE_THROUGH);
856 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
857 return (VM_MEMATTR_WRITE_COMBINING);
861 return (VM_MEMATTR_DEVICE);
865 initarm(struct arm64_bootparams *abp)
867 struct efi_fb *efifb;
871 struct mem_region mem_regions[FDT_MEM_REGIONS];
874 char dts_version[255];
876 vm_offset_t lastaddr;
880 TSRAW(&thread0, TS_ENTER, __func__, NULL);
882 boot_el = abp->boot_el;
883 hcr_el2 = abp->hcr_el2;
885 /* Parse loader or FDT boot parametes. Determine last used address. */
886 lastaddr = parse_boot_param(abp);
888 /* Find the kernel address */
889 kmdp = preload_search_by_type("elf kernel");
891 kmdp = preload_search_by_type("elf64 kernel");
894 identify_hypervisor_smbios();
896 update_special_regs(0);
898 link_elf_ireloc(kmdp);
903 efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
905 /* Load the physical memory ranges */
906 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
907 MODINFO_METADATA | MODINFOMD_EFI_MAP);
909 add_efi_map_entries(efihdr);
912 /* Grab physical memory regions information from device tree. */
913 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
915 panic("Cannot get physical memory regions");
916 physmem_hardware_regions(mem_regions, mem_regions_sz);
918 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
919 physmem_exclude_regions(mem_regions, mem_regions_sz,
920 EXFLAG_NODUMP | EXFLAG_NOALLOC);
923 /* Exclude the EFI framebuffer from our view of physical memory. */
924 efifb = (struct efi_fb *)preload_search_info(kmdp,
925 MODINFO_METADATA | MODINFOMD_EFI_FB);
927 physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
930 /* Set the pcpu data, this is needed by pmap_bootstrap */
932 pcpu_init(pcpup, 0, sizeof(struct pcpu));
935 * Set the pcpu pointer with a backup in tpidr_el1 to be
936 * loaded when entering the kernel from userland.
940 "msr tpidr_el1, %0" :: "r"(pcpup));
942 /* locore.S sets sp_el0 to &thread0 so no need to set it here. */
943 PCPU_SET(curthread, &thread0);
944 PCPU_SET(midr, get_midr());
946 /* Do basic tuning, hz etc */
952 /* Bootstrap enough of pmap to enter the kernel proper */
953 pmap_bootstrap(KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
954 /* Exclude entries needed in the DMAP region, but not phys_avail */
956 exclude_efi_map_entries(efihdr);
957 /* Do the same for reserve entries in the EFI MEMRESERVE table */
958 if (efi_systbl_phys != 0)
959 exclude_efi_memreserve(efi_systbl_phys);
962 * We carefully bootstrap the sanitizer map after we've excluded
963 * absolutely everything else that could impact phys_avail. There's not
964 * always enough room for the initial shadow map after the kernel, so
965 * we'll end up searching for segments that we can safely use. Those
966 * segments also get excluded from phys_avail.
969 pmap_bootstrap_san(KERNBASE - abp->kern_delta);
972 physmem_init_kernel_globals();
974 devmap_bootstrap(0, NULL);
979 set_ttbr0(abp->kern_ttbr0);
983 panic("Invalid bus configuration: %s",
984 kern_getenv("kern.cfg.order"));
987 * Check if pointer authentication is available on this system, and
988 * if so enable its use. This needs to be called before init_proc0
989 * as that will configure the thread0 pointer authentication keys.
994 * Dump the boot metadata. We have to wait for cninit() since console
995 * output is required. If it's grossly incorrect the kernel will never
998 if (getenv_is_true("debug.dump_modinfo_at_boot"))
1001 init_proc0(abp->kern_stack);
1002 msgbufinit(msgbufp, msgbufsize);
1004 init_param2(physmem);
1009 if ((boothowto & RB_KDB) != 0)
1010 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
1017 env = kern_getenv("kernelname");
1019 strlcpy(kernelname, env, sizeof(kernelname));
1022 if (arm64_bus_method == ARM64_BUS_FDT) {
1023 root = OF_finddevice("/");
1024 if (OF_getprop(root, "freebsd,dts-version", dts_version, sizeof(dts_version)) > 0) {
1025 if (strcmp(LINUX_DTS_VERSION, dts_version) != 0)
1026 printf("WARNING: DTB version is %s while kernel expects %s, "
1027 "please update the DTB in the ESP\n",
1031 printf("WARNING: Cannot find freebsd,dts-version property, "
1032 "cannot check DTB compliance\n");
1037 if (boothowto & RB_VERBOSE) {
1039 print_efi_map_entries(efihdr);
1040 physmem_print_tables();
1053 WRITE_SPECIALREG(oslar_el1, 0);
1055 /* This permits DDB to use debug registers for watchpoints. */
1058 /* TODO: Eventually will need to initialize debug registers here. */
1062 #include <ddb/ddb.h>
1064 DB_SHOW_COMMAND(specialregs, db_show_spregs)
1066 #define PRINT_REG(reg) \
1067 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
1069 PRINT_REG(actlr_el1);
1070 PRINT_REG(afsr0_el1);
1071 PRINT_REG(afsr1_el1);
1072 PRINT_REG(aidr_el1);
1073 PRINT_REG(amair_el1);
1074 PRINT_REG(ccsidr_el1);
1075 PRINT_REG(clidr_el1);
1076 PRINT_REG(contextidr_el1);
1077 PRINT_REG(cpacr_el1);
1078 PRINT_REG(csselr_el1);
1080 PRINT_REG(currentel);
1082 PRINT_REG(dczid_el0);
1087 /* ARM64TODO: Enable VFP before reading floating-point registers */
1091 PRINT_REG(id_aa64afr0_el1);
1092 PRINT_REG(id_aa64afr1_el1);
1093 PRINT_REG(id_aa64dfr0_el1);
1094 PRINT_REG(id_aa64dfr1_el1);
1095 PRINT_REG(id_aa64isar0_el1);
1096 PRINT_REG(id_aa64isar1_el1);
1097 PRINT_REG(id_aa64pfr0_el1);
1098 PRINT_REG(id_aa64pfr1_el1);
1099 PRINT_REG(id_afr0_el1);
1100 PRINT_REG(id_dfr0_el1);
1101 PRINT_REG(id_isar0_el1);
1102 PRINT_REG(id_isar1_el1);
1103 PRINT_REG(id_isar2_el1);
1104 PRINT_REG(id_isar3_el1);
1105 PRINT_REG(id_isar4_el1);
1106 PRINT_REG(id_isar5_el1);
1107 PRINT_REG(id_mmfr0_el1);
1108 PRINT_REG(id_mmfr1_el1);
1109 PRINT_REG(id_mmfr2_el1);
1110 PRINT_REG(id_mmfr3_el1);
1112 /* Missing from llvm */
1113 PRINT_REG(id_mmfr4_el1);
1115 PRINT_REG(id_pfr0_el1);
1116 PRINT_REG(id_pfr1_el1);
1118 PRINT_REG(mair_el1);
1119 PRINT_REG(midr_el1);
1120 PRINT_REG(mpidr_el1);
1121 PRINT_REG(mvfr0_el1);
1122 PRINT_REG(mvfr1_el1);
1123 PRINT_REG(mvfr2_el1);
1124 PRINT_REG(revidr_el1);
1125 PRINT_REG(sctlr_el1);
1128 PRINT_REG(spsr_el1);
1130 PRINT_REG(tpidr_el0);
1131 PRINT_REG(tpidr_el1);
1132 PRINT_REG(tpidrro_el0);
1133 PRINT_REG(ttbr0_el1);
1134 PRINT_REG(ttbr1_el1);
1135 PRINT_REG(vbar_el1);
1139 DB_SHOW_COMMAND(vtop, db_show_vtop)
1144 phys = arm64_address_translate_s1e1r(addr);
1145 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
1146 phys = arm64_address_translate_s1e1w(addr);
1147 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1148 phys = arm64_address_translate_s1e0r(addr);
1149 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
1150 phys = arm64_address_translate_s1e0w(addr);
1151 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1153 db_printf("show vtop <virt_addr>\n");