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>
41 #include <sys/devmap.h>
44 #include <sys/imgact.h>
46 #include <sys/kernel.h>
47 #include <sys/limits.h>
48 #include <sys/linker.h>
49 #include <sys/msgbuf.h>
52 #include <sys/ptrace.h>
53 #include <sys/reboot.h>
54 #include <sys/rwlock.h>
55 #include <sys/sched.h>
56 #include <sys/signalvar.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysent.h>
59 #include <sys/sysproto.h>
60 #include <sys/ucontext.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_page.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_pager.h>
71 #include <machine/armreg.h>
72 #include <machine/cpu.h>
73 #include <machine/debug_monitor.h>
74 #include <machine/kdb.h>
75 #include <machine/machdep.h>
76 #include <machine/metadata.h>
77 #include <machine/md_var.h>
78 #include <machine/pcb.h>
79 #include <machine/reg.h>
80 #include <machine/undefined.h>
81 #include <machine/vmparam.h>
84 #include <machine/vfp.h>
88 #include <contrib/dev/acpica/include/acpi.h>
89 #include <machine/acpica_machdep.h>
93 #include <dev/fdt/fdt_common.h>
94 #include <dev/ofw/openfirm.h>
98 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
100 struct pcpu __pcpu[MAXCPU];
102 static struct trapframe proc0_tf;
104 vm_paddr_t phys_avail[PHYS_AVAIL_SIZE + 2];
105 vm_paddr_t dump_avail[PHYS_AVAIL_SIZE + 2];
112 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
113 vm_paddr_t physmap[PHYSMAP_SIZE];
116 struct kva_md_info kmi;
118 int64_t dcache_line_size; /* The minimum D cache line size */
119 int64_t icache_line_size; /* The minimum I cache line size */
120 int64_t idcache_line_size; /* The minimum cache line size */
121 int64_t dczva_line_size; /* The size of cache line the dc zva zeroes */
125 * Physical address of the EFI System Table. Stashed from the metadata hints
126 * passed into the kernel and used by the EFI code to call runtime services.
128 vm_paddr_t efi_systbl_phys;
130 /* pagezero_* implementations are provided in support.S */
131 void pagezero_simple(void *);
132 void pagezero_cache(void *);
134 /* pagezero_simple is default pagezero */
135 void (*pagezero)(void *p) = pagezero_simple;
140 uint64_t id_aa64mfr1;
142 id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
143 if (ID_AA64MMFR1_PAN(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE)
152 * The LLVM integrated assembler doesn't understand the PAN
153 * PSTATE field. Because of this we need to manually create
154 * the instruction in an asm block. This is equivalent to:
157 * This sets the PAN bit, stopping the kernel from accessing
158 * memory when userspace can also access it unless the kernel
159 * uses the userspace load/store instructions.
162 WRITE_SPECIALREG(sctlr_el1,
163 READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
164 __asm __volatile(".inst 0xd500409f | (0x1 << 8)");
169 cpu_startup(void *dummy)
175 vm_ksubmap_init(&kmi);
177 vm_pager_bufferinit();
180 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
183 cpu_idle_wakeup(int cpu)
190 fill_regs(struct thread *td, struct reg *regs)
192 struct trapframe *frame;
194 frame = td->td_frame;
195 regs->sp = frame->tf_sp;
196 regs->lr = frame->tf_lr;
197 regs->elr = frame->tf_elr;
198 regs->spsr = frame->tf_spsr;
200 memcpy(regs->x, frame->tf_x, sizeof(regs->x));
206 set_regs(struct thread *td, struct reg *regs)
208 struct trapframe *frame;
210 frame = td->td_frame;
211 frame->tf_sp = regs->sp;
212 frame->tf_lr = regs->lr;
213 frame->tf_elr = regs->elr;
214 frame->tf_spsr &= ~PSR_FLAGS;
215 frame->tf_spsr |= regs->spsr & PSR_FLAGS;
217 memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
223 fill_fpregs(struct thread *td, struct fpreg *regs)
229 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
231 * If we have just been running VFP instructions we will
232 * need to save the state to memcpy it below.
235 vfp_save_state(td, pcb);
237 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
238 ("Called fill_fpregs while the kernel is using the VFP"));
239 memcpy(regs->fp_q, pcb->pcb_fpustate.vfp_regs,
241 regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
242 regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
245 memset(regs->fp_q, 0, sizeof(regs->fp_q));
250 set_fpregs(struct thread *td, struct fpreg *regs)
256 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
257 ("Called set_fpregs while the kernel is using the VFP"));
258 memcpy(pcb->pcb_fpustate.vfp_regs, regs->fp_q, sizeof(regs->fp_q));
259 pcb->pcb_fpustate.vfp_fpcr = regs->fp_cr;
260 pcb->pcb_fpustate.vfp_fpsr = regs->fp_sr;
266 fill_dbregs(struct thread *td, struct dbreg *regs)
269 printf("ARM64TODO: fill_dbregs");
274 set_dbregs(struct thread *td, struct dbreg *regs)
277 printf("ARM64TODO: set_dbregs");
282 ptrace_set_pc(struct thread *td, u_long addr)
285 printf("ARM64TODO: ptrace_set_pc");
290 ptrace_single_step(struct thread *td)
293 td->td_frame->tf_spsr |= PSR_SS;
294 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
299 ptrace_clear_single_step(struct thread *td)
302 td->td_frame->tf_spsr &= ~PSR_SS;
303 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
308 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
310 struct trapframe *tf = td->td_frame;
312 memset(tf, 0, sizeof(struct trapframe));
315 tf->tf_sp = STACKALIGN(stack);
316 tf->tf_lr = imgp->entry_addr;
317 tf->tf_elr = imgp->entry_addr;
320 /* Sanity check these are the same size, they will be memcpy'd to and fro */
321 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
322 sizeof((struct gpregs *)0)->gp_x);
323 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
324 sizeof((struct reg *)0)->x);
327 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
329 struct trapframe *tf = td->td_frame;
331 if (clear_ret & GET_MC_CLEAR_RET) {
332 mcp->mc_gpregs.gp_x[0] = 0;
333 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
335 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
336 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
339 memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
340 sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
342 mcp->mc_gpregs.gp_sp = tf->tf_sp;
343 mcp->mc_gpregs.gp_lr = tf->tf_lr;
344 mcp->mc_gpregs.gp_elr = tf->tf_elr;
350 set_mcontext(struct thread *td, mcontext_t *mcp)
352 struct trapframe *tf = td->td_frame;
355 spsr = mcp->mc_gpregs.gp_spsr;
356 if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
357 (spsr & (PSR_F | PSR_I | PSR_A | PSR_D)) != 0)
360 memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
362 tf->tf_sp = mcp->mc_gpregs.gp_sp;
363 tf->tf_lr = mcp->mc_gpregs.gp_lr;
364 tf->tf_elr = mcp->mc_gpregs.gp_elr;
365 tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
371 get_fpcontext(struct thread *td, mcontext_t *mcp)
378 curpcb = curthread->td_pcb;
380 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
382 * If we have just been running VFP instructions we will
383 * need to save the state to memcpy it below.
385 vfp_save_state(td, curpcb);
387 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
388 ("Called get_fpcontext while the kernel is using the VFP"));
389 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
390 ("Non-userspace FPU flags set in get_fpcontext"));
391 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
392 sizeof(mcp->mc_fpregs));
393 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
394 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
395 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
396 mcp->mc_flags |= _MC_FP_VALID;
404 set_fpcontext(struct thread *td, mcontext_t *mcp)
411 if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
412 curpcb = curthread->td_pcb;
415 * Discard any vfp state for the current thread, we
416 * are about to override it.
420 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
421 ("Called set_fpcontext while the kernel is using the VFP"));
422 memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
423 sizeof(mcp->mc_fpregs));
424 curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
425 curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
426 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
440 if (!sched_runnable())
453 /* We should have shutdown by now, if not enter a low power sleep */
456 __asm __volatile("wfi");
461 * Flush the D-cache for non-DMA I/O so that the I-cache can
462 * be made coherent later.
465 cpu_flush_dcache(void *ptr, size_t len)
471 /* Get current clock frequency for the given CPU ID. */
473 cpu_est_clockrate(int cpu_id, uint64_t *rate)
477 pc = pcpu_find(cpu_id);
478 if (pc == NULL || rate == NULL)
481 if (pc->pc_clock == 0)
484 *rate = pc->pc_clock;
489 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
492 pcpu->pc_acpi_id = 0xffffffff;
502 if (td->td_md.md_spinlock_count == 0) {
503 daif = intr_disable();
504 td->td_md.md_spinlock_count = 1;
505 td->td_md.md_saved_daif = daif;
507 td->td_md.md_spinlock_count++;
519 daif = td->td_md.md_saved_daif;
520 td->td_md.md_spinlock_count--;
521 if (td->td_md.md_spinlock_count == 0)
525 #ifndef _SYS_SYSPROTO_H_
526 struct sigreturn_args {
532 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
539 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
542 error = set_mcontext(td, &uc.uc_mcontext);
545 set_fpcontext(td, &uc.uc_mcontext);
547 /* Restore signal mask. */
548 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
550 return (EJUSTRETURN);
554 * Construct a PCB from a trapframe. This is called from kdb_trap() where
555 * we want to start a backtrace from the function that caused us to enter
556 * the debugger. We have the context in the trapframe, but base the trace
557 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
558 * enough for a backtrace.
561 makectx(struct trapframe *tf, struct pcb *pcb)
565 for (i = 0; i < PCB_LR; i++)
566 pcb->pcb_x[i] = tf->tf_x[i];
568 pcb->pcb_x[PCB_LR] = tf->tf_lr;
569 pcb->pcb_pc = tf->tf_elr;
570 pcb->pcb_sp = tf->tf_sp;
574 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
578 struct trapframe *tf;
579 struct sigframe *fp, frame;
581 struct sysentvec *sysent;
582 int code, onstack, sig;
586 PROC_LOCK_ASSERT(p, MA_OWNED);
588 sig = ksi->ksi_signo;
589 code = ksi->ksi_code;
591 mtx_assert(&psp->ps_mtx, MA_OWNED);
594 onstack = sigonstack(tf->tf_sp);
596 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
599 /* Allocate and validate space for the signal handler context. */
600 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
601 SIGISMEMBER(psp->ps_sigonstack, sig)) {
602 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
603 td->td_sigstk.ss_size);
604 #if defined(COMPAT_43)
605 td->td_sigstk.ss_flags |= SS_ONSTACK;
608 fp = (struct sigframe *)td->td_frame->tf_sp;
611 /* Make room, keeping the stack aligned */
613 fp = (struct sigframe *)STACKALIGN(fp);
615 /* Fill in the frame to copy out */
616 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
617 get_fpcontext(td, &frame.sf_uc.uc_mcontext);
618 frame.sf_si = ksi->ksi_info;
619 frame.sf_uc.uc_sigmask = *mask;
620 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
621 ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
622 frame.sf_uc.uc_stack = td->td_sigstk;
623 mtx_unlock(&psp->ps_mtx);
624 PROC_UNLOCK(td->td_proc);
626 /* Copy the sigframe out to the user's stack. */
627 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
628 /* Process has trashed its stack. Kill it. */
629 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
635 tf->tf_x[1] = (register_t)&fp->sf_si;
636 tf->tf_x[2] = (register_t)&fp->sf_uc;
638 tf->tf_elr = (register_t)catcher;
639 tf->tf_sp = (register_t)fp;
640 sysent = p->p_sysent;
641 if (sysent->sv_sigcode_base != 0)
642 tf->tf_lr = (register_t)sysent->sv_sigcode_base;
644 tf->tf_lr = (register_t)(sysent->sv_psstrings -
645 *(sysent->sv_szsigcode));
647 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
651 mtx_lock(&psp->ps_mtx);
655 init_proc0(vm_offset_t kstack)
657 struct pcpu *pcpup = &__pcpu[0];
659 proc_linkup0(&proc0, &thread0);
660 thread0.td_kstack = kstack;
661 thread0.td_pcb = (struct pcb *)(thread0.td_kstack) - 1;
662 thread0.td_pcb->pcb_fpflags = 0;
663 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
664 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
665 thread0.td_frame = &proc0_tf;
666 pcpup->pc_curpcb = thread0.td_pcb;
675 } EFI_MEMORY_DESCRIPTOR;
678 add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
681 u_int i, insert_idx, _physmap_idx;
683 _physmap_idx = *physmap_idxp;
689 * Find insertion point while checking for overlap. Start off by
690 * assuming the new entry will be added to the end.
692 insert_idx = _physmap_idx;
693 for (i = 0; i <= _physmap_idx; i += 2) {
694 if (base < physmap[i + 1]) {
695 if (base + length <= physmap[i]) {
699 if (boothowto & RB_VERBOSE)
701 "Overlapping memory regions, ignoring second region\n");
706 /* See if we can prepend to the next entry. */
707 if (insert_idx <= _physmap_idx &&
708 base + length == physmap[insert_idx]) {
709 physmap[insert_idx] = base;
713 /* See if we can append to the previous entry. */
714 if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
715 physmap[insert_idx - 1] += length;
720 *physmap_idxp = _physmap_idx;
721 if (_physmap_idx == PHYSMAP_SIZE) {
723 "Too many segments in the physical address map, giving up\n");
728 * Move the last 'N' entries down to make room for the new
731 for (i = _physmap_idx; i > insert_idx; i -= 2) {
732 physmap[i] = physmap[i - 2];
733 physmap[i + 1] = physmap[i - 1];
736 /* Insert the new entry. */
737 physmap[insert_idx] = base;
738 physmap[insert_idx + 1] = base + length;
744 add_fdt_mem_regions(struct mem_region *mr, int mrcnt, vm_paddr_t *physmap,
748 for (int i = 0; i < mrcnt; i++) {
749 if (!add_physmap_entry(mr[i].mr_start, mr[i].mr_size, physmap,
757 add_efi_map_entries(struct efi_map_header *efihdr, vm_paddr_t *physmap,
760 struct efi_md *map, *p;
765 static const char *types[] = {
771 "RuntimeServicesCode",
772 "RuntimeServicesData",
773 "ConventionalMemory",
778 "MemoryMappedIOPortSpace",
784 * Memory map data provided by UEFI via the GetMemoryMap
787 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
788 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
790 if (efihdr->descriptor_size == 0)
792 ndesc = efihdr->memory_size / efihdr->descriptor_size;
794 if (boothowto & RB_VERBOSE)
795 printf("%23s %12s %12s %8s %4s\n",
796 "Type", "Physical", "Virtual", "#Pages", "Attr");
798 for (i = 0, p = map; i < ndesc; i++,
799 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
800 if (boothowto & RB_VERBOSE) {
801 if (p->md_type < nitems(types))
802 type = types[p->md_type];
805 printf("%23s %012lx %12p %08lx ", type, p->md_phys,
806 p->md_virt, p->md_pages);
807 if (p->md_attr & EFI_MD_ATTR_UC)
809 if (p->md_attr & EFI_MD_ATTR_WC)
811 if (p->md_attr & EFI_MD_ATTR_WT)
813 if (p->md_attr & EFI_MD_ATTR_WB)
815 if (p->md_attr & EFI_MD_ATTR_UCE)
817 if (p->md_attr & EFI_MD_ATTR_WP)
819 if (p->md_attr & EFI_MD_ATTR_RP)
821 if (p->md_attr & EFI_MD_ATTR_XP)
823 if (p->md_attr & EFI_MD_ATTR_NV)
825 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
826 printf("MORE_RELIABLE ");
827 if (p->md_attr & EFI_MD_ATTR_RO)
829 if (p->md_attr & EFI_MD_ATTR_RT)
834 switch (p->md_type) {
835 case EFI_MD_TYPE_CODE:
836 case EFI_MD_TYPE_DATA:
837 case EFI_MD_TYPE_BS_CODE:
838 case EFI_MD_TYPE_BS_DATA:
839 case EFI_MD_TYPE_FREE:
841 * We're allowed to use any entry with these types.
848 if (!add_physmap_entry(p->md_phys, (p->md_pages * PAGE_SIZE),
849 physmap, physmap_idxp))
856 try_load_dtb(caddr_t kmdp)
860 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
861 if (dtbp == (vm_offset_t)NULL) {
862 printf("ERROR loading DTB\n");
866 if (OF_install(OFW_FDT, 0) == FALSE)
867 panic("Cannot install FDT");
869 if (OF_init((void *)dtbp) != 0)
870 panic("OF_init failed with the found device tree");
877 bool has_acpi, has_fdt;
880 has_acpi = has_fdt = false;
883 has_fdt = (OF_peer(0) != 0);
886 has_acpi = (acpi_find_table(ACPI_SIG_SPCR) != 0);
889 env = kern_getenv("kern.cfg.order");
892 while (order != NULL) {
894 strncmp(order, "acpi", 4) == 0 &&
895 (order[4] == ',' || order[4] == '\0')) {
896 arm64_bus_method = ARM64_BUS_ACPI;
900 strncmp(order, "fdt", 3) == 0 &&
901 (order[3] == ',' || order[3] == '\0')) {
902 arm64_bus_method = ARM64_BUS_FDT;
905 order = strchr(order, ',');
909 /* If we set the bus method it is valid */
910 if (arm64_bus_method != ARM64_BUS_NONE)
913 /* If no order or an invalid order was set use the default */
914 if (arm64_bus_method == ARM64_BUS_NONE) {
916 arm64_bus_method = ARM64_BUS_FDT;
918 arm64_bus_method = ARM64_BUS_ACPI;
922 * If no option was set the default is valid, otherwise we are
923 * setting one to get cninit() working, then calling panic to tell
924 * the user about the invalid bus setup.
926 return (env == NULL);
932 int dcache_line_shift, icache_line_shift, dczva_line_shift;
936 ctr_el0 = READ_SPECIALREG(ctr_el0);
938 /* Read the log2 words in each D cache line */
939 dcache_line_shift = CTR_DLINE_SIZE(ctr_el0);
940 /* Get the D cache line size */
941 dcache_line_size = sizeof(int) << dcache_line_shift;
943 /* And the same for the I cache */
944 icache_line_shift = CTR_ILINE_SIZE(ctr_el0);
945 icache_line_size = sizeof(int) << icache_line_shift;
947 idcache_line_size = MIN(dcache_line_size, icache_line_size);
949 dczid_el0 = READ_SPECIALREG(dczid_el0);
951 /* Check if dc zva is not prohibited */
952 if (dczid_el0 & DCZID_DZP)
955 /* Same as with above calculations */
956 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
957 dczva_line_size = sizeof(int) << dczva_line_shift;
959 /* Change pagezero function */
960 pagezero = pagezero_cache;
965 initarm(struct arm64_bootparams *abp)
967 struct efi_map_header *efihdr;
970 struct mem_region mem_regions[FDT_MEM_REGIONS];
973 vm_offset_t lastaddr;
979 /* Set the module data location */
980 preload_metadata = (caddr_t)(uintptr_t)(abp->modulep);
982 /* Find the kernel address */
983 kmdp = preload_search_by_type("elf kernel");
985 kmdp = preload_search_by_type("elf64 kernel");
987 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
988 init_static_kenv(MD_FETCH(kmdp, MODINFOMD_ENVP, char *), 0);
994 efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
996 /* Find the address to start allocating from */
997 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
999 /* Load the physical memory ranges */
1001 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1002 MODINFO_METADATA | MODINFOMD_EFI_MAP);
1004 add_efi_map_entries(efihdr, physmap, &physmap_idx);
1007 /* Grab physical memory regions information from device tree. */
1008 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
1010 panic("Cannot get physical memory regions");
1011 add_fdt_mem_regions(mem_regions, mem_regions_sz, physmap,
1016 /* Print the memory map */
1018 for (i = 0; i < physmap_idx; i += 2) {
1019 dump_avail[i] = physmap[i];
1020 dump_avail[i + 1] = physmap[i + 1];
1021 mem_len += physmap[i + 1] - physmap[i];
1024 dump_avail[i + 1] = 0;
1026 /* Set the pcpu data, this is needed by pmap_bootstrap */
1028 pcpu_init(pcpup, 0, sizeof(struct pcpu));
1031 * Set the pcpu pointer with a backup in tpidr_el1 to be
1032 * loaded when entering the kernel from userland.
1036 "msr tpidr_el1, %0" :: "r"(pcpup));
1038 PCPU_SET(curthread, &thread0);
1040 /* Do basic tuning, hz etc */
1046 /* Bootstrap enough of pmap to enter the kernel proper */
1047 pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
1048 KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
1050 devmap_bootstrap(0, NULL);
1052 valid = bus_probe();
1057 panic("Invalid bus configuration: %s",
1058 kern_getenv("kern.cfg.order"));
1060 init_proc0(abp->kern_stack);
1061 msgbufinit(msgbufp, msgbufsize);
1063 init_param2(physmem);
1077 WRITE_SPECIALREG(OSLAR_EL1, 0);
1079 /* This permits DDB to use debug registers for watchpoints. */
1082 /* TODO: Eventually will need to initialize debug registers here. */
1086 #include <ddb/ddb.h>
1088 DB_SHOW_COMMAND(specialregs, db_show_spregs)
1090 #define PRINT_REG(reg) \
1091 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
1093 PRINT_REG(actlr_el1);
1094 PRINT_REG(afsr0_el1);
1095 PRINT_REG(afsr1_el1);
1096 PRINT_REG(aidr_el1);
1097 PRINT_REG(amair_el1);
1098 PRINT_REG(ccsidr_el1);
1099 PRINT_REG(clidr_el1);
1100 PRINT_REG(contextidr_el1);
1101 PRINT_REG(cpacr_el1);
1102 PRINT_REG(csselr_el1);
1104 PRINT_REG(currentel);
1106 PRINT_REG(dczid_el0);
1111 /* ARM64TODO: Enable VFP before reading floating-point registers */
1115 PRINT_REG(id_aa64afr0_el1);
1116 PRINT_REG(id_aa64afr1_el1);
1117 PRINT_REG(id_aa64dfr0_el1);
1118 PRINT_REG(id_aa64dfr1_el1);
1119 PRINT_REG(id_aa64isar0_el1);
1120 PRINT_REG(id_aa64isar1_el1);
1121 PRINT_REG(id_aa64pfr0_el1);
1122 PRINT_REG(id_aa64pfr1_el1);
1123 PRINT_REG(id_afr0_el1);
1124 PRINT_REG(id_dfr0_el1);
1125 PRINT_REG(id_isar0_el1);
1126 PRINT_REG(id_isar1_el1);
1127 PRINT_REG(id_isar2_el1);
1128 PRINT_REG(id_isar3_el1);
1129 PRINT_REG(id_isar4_el1);
1130 PRINT_REG(id_isar5_el1);
1131 PRINT_REG(id_mmfr0_el1);
1132 PRINT_REG(id_mmfr1_el1);
1133 PRINT_REG(id_mmfr2_el1);
1134 PRINT_REG(id_mmfr3_el1);
1136 /* Missing from llvm */
1137 PRINT_REG(id_mmfr4_el1);
1139 PRINT_REG(id_pfr0_el1);
1140 PRINT_REG(id_pfr1_el1);
1142 PRINT_REG(mair_el1);
1143 PRINT_REG(midr_el1);
1144 PRINT_REG(mpidr_el1);
1145 PRINT_REG(mvfr0_el1);
1146 PRINT_REG(mvfr1_el1);
1147 PRINT_REG(mvfr2_el1);
1148 PRINT_REG(revidr_el1);
1149 PRINT_REG(sctlr_el1);
1152 PRINT_REG(spsr_el1);
1154 PRINT_REG(tpidr_el0);
1155 PRINT_REG(tpidr_el1);
1156 PRINT_REG(tpidrro_el0);
1157 PRINT_REG(ttbr0_el1);
1158 PRINT_REG(ttbr1_el1);
1159 PRINT_REG(vbar_el1);
1163 DB_SHOW_COMMAND(vtop, db_show_vtop)
1168 phys = arm64_address_translate_s1e1r(addr);
1169 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
1170 phys = arm64_address_translate_s1e1w(addr);
1171 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1172 phys = arm64_address_translate_s1e0r(addr);
1173 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
1174 phys = arm64_address_translate_s1e0w(addr);
1175 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1177 db_printf("show vtop <virt_addr>\n");