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>
42 #include <sys/devmap.h>
45 #include <sys/imgact.h>
47 #include <sys/kernel.h>
49 #include <sys/limits.h>
50 #include <sys/linker.h>
51 #include <sys/msgbuf.h>
53 #include <sys/physmem.h>
55 #include <sys/ptrace.h>
56 #include <sys/reboot.h>
57 #include <sys/rwlock.h>
58 #include <sys/sched.h>
59 #include <sys/signalvar.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysent.h>
62 #include <sys/sysproto.h>
63 #include <sys/ucontext.h>
65 #include <sys/vmmeter.h>
68 #include <vm/vm_param.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_phys.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_pager.h>
77 #include <machine/armreg.h>
78 #include <machine/cpu.h>
79 #include <machine/debug_monitor.h>
80 #include <machine/kdb.h>
81 #include <machine/machdep.h>
82 #include <machine/metadata.h>
83 #include <machine/md_var.h>
84 #include <machine/pcb.h>
85 #include <machine/reg.h>
86 #include <machine/undefined.h>
87 #include <machine/vmparam.h>
90 #include <machine/vfp.h>
94 #include <contrib/dev/acpica/include/acpi.h>
95 #include <machine/acpica_machdep.h>
99 #include <dev/fdt/fdt_common.h>
100 #include <dev/ofw/openfirm.h>
103 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
104 static void set_fpcontext(struct thread *td, mcontext_t *mcp);
106 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
108 struct pcpu __pcpu[MAXCPU];
110 static struct trapframe proc0_tf;
116 struct kva_md_info kmi;
118 int64_t dczva_line_size; /* The size of cache line the dc zva zeroes */
122 * Physical address of the EFI System Table. Stashed from the metadata hints
123 * passed into the kernel and used by the EFI code to call runtime services.
125 vm_paddr_t efi_systbl_phys;
126 static struct efi_map_header *efihdr;
128 /* pagezero_* implementations are provided in support.S */
129 void pagezero_simple(void *);
130 void pagezero_cache(void *);
132 /* pagezero_simple is default pagezero */
133 void (*pagezero)(void *p) = pagezero_simple;
135 int (*apei_nmi)(void);
140 uint64_t id_aa64mfr1;
142 id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
143 if (ID_AA64MMFR1_PAN_VAL(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)");
172 return (boot_el == 2);
176 cpu_startup(void *dummy)
181 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)realmem),
182 ptoa((uintmax_t)realmem) / 1024 / 1024);
185 printf("Physical memory chunk(s):\n");
186 for (i = 0; phys_avail[i + 1] != 0; i += 2) {
187 size = phys_avail[i + 1] - phys_avail[i];
188 printf("%#016jx - %#016jx, %ju bytes (%ju pages)\n",
189 (uintmax_t)phys_avail[i],
190 (uintmax_t)phys_avail[i + 1] - 1,
191 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
195 printf("avail memory = %ju (%ju MB)\n",
196 ptoa((uintmax_t)vm_free_count()),
197 ptoa((uintmax_t)vm_free_count()) / 1024 / 1024);
200 install_cpu_errata();
202 vm_ksubmap_init(&kmi);
204 vm_pager_bufferinit();
207 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
210 late_ifunc_resolve(void *dummy __unused)
212 link_elf_late_ireloc();
214 SYSINIT(late_ifunc_resolve, SI_SUB_CPU, SI_ORDER_ANY, late_ifunc_resolve, NULL);
217 cpu_idle_wakeup(int cpu)
224 fill_regs(struct thread *td, struct reg *regs)
226 struct trapframe *frame;
228 frame = td->td_frame;
229 regs->sp = frame->tf_sp;
230 regs->lr = frame->tf_lr;
231 regs->elr = frame->tf_elr;
232 regs->spsr = frame->tf_spsr;
234 memcpy(regs->x, frame->tf_x, sizeof(regs->x));
236 #ifdef COMPAT_FREEBSD32
238 * We may be called here for a 32bits process, if we're using a
239 * 64bits debugger. If so, put PC and SPSR where it expects it.
241 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
242 regs->x[15] = frame->tf_elr;
243 regs->x[16] = frame->tf_spsr;
250 set_regs(struct thread *td, struct reg *regs)
252 struct trapframe *frame;
254 frame = td->td_frame;
255 frame->tf_sp = regs->sp;
256 frame->tf_lr = regs->lr;
257 frame->tf_elr = regs->elr;
258 frame->tf_spsr &= ~PSR_FLAGS;
259 frame->tf_spsr |= regs->spsr & PSR_FLAGS;
261 memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
263 #ifdef COMPAT_FREEBSD32
264 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
266 * We may be called for a 32bits process if we're using
267 * a 64bits debugger. If so, get PC and SPSR from where
270 frame->tf_elr = regs->x[15];
271 frame->tf_spsr = regs->x[16] & PSR_FLAGS;
278 fill_fpregs(struct thread *td, struct fpreg *regs)
284 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
286 * If we have just been running VFP instructions we will
287 * need to save the state to memcpy it below.
290 vfp_save_state(td, pcb);
292 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
293 ("Called fill_fpregs while the kernel is using the VFP"));
294 memcpy(regs->fp_q, pcb->pcb_fpustate.vfp_regs,
296 regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
297 regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
300 memset(regs, 0, sizeof(*regs));
305 set_fpregs(struct thread *td, struct fpreg *regs)
311 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
312 ("Called set_fpregs while the kernel is using the VFP"));
313 memcpy(pcb->pcb_fpustate.vfp_regs, regs->fp_q, sizeof(regs->fp_q));
314 pcb->pcb_fpustate.vfp_fpcr = regs->fp_cr;
315 pcb->pcb_fpustate.vfp_fpsr = regs->fp_sr;
321 fill_dbregs(struct thread *td, struct dbreg *regs)
323 struct debug_monitor_state *monitor;
325 uint8_t debug_ver, nbkpts;
327 memset(regs, 0, sizeof(*regs));
329 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_DebugVer_SHIFT,
331 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_BRPs_SHIFT,
335 * The BRPs field contains the number of breakpoints - 1. Armv8-A
336 * allows the hardware to provide 2-16 breakpoints so this won't
337 * overflow an 8 bit value.
341 regs->db_info = debug_ver;
343 regs->db_info |= count;
345 monitor = &td->td_pcb->pcb_dbg_regs;
346 if ((monitor->dbg_flags & DBGMON_ENABLED) != 0) {
347 for (i = 0; i < count; i++) {
348 regs->db_regs[i].dbr_addr = monitor->dbg_bvr[i];
349 regs->db_regs[i].dbr_ctrl = monitor->dbg_bcr[i];
357 set_dbregs(struct thread *td, struct dbreg *regs)
359 struct debug_monitor_state *monitor;
363 monitor = &td->td_pcb->pcb_dbg_regs;
365 monitor->dbg_enable_count = 0;
366 for (i = 0; i < DBG_BRP_MAX; i++) {
367 /* TODO: Check these values */
368 monitor->dbg_bvr[i] = regs->db_regs[i].dbr_addr;
369 monitor->dbg_bcr[i] = regs->db_regs[i].dbr_ctrl;
370 if ((monitor->dbg_bcr[i] & 1) != 0)
371 monitor->dbg_enable_count++;
373 if (monitor->dbg_enable_count > 0)
374 monitor->dbg_flags |= DBGMON_ENABLED;
379 #ifdef COMPAT_FREEBSD32
381 fill_regs32(struct thread *td, struct reg32 *regs)
384 struct trapframe *tf;
387 for (i = 0; i < 13; i++)
388 regs->r[i] = tf->tf_x[i];
389 /* For arm32, SP is r13 and LR is r14 */
390 regs->r_sp = tf->tf_x[13];
391 regs->r_lr = tf->tf_x[14];
392 regs->r_pc = tf->tf_elr;
393 regs->r_cpsr = tf->tf_spsr;
399 set_regs32(struct thread *td, struct reg32 *regs)
402 struct trapframe *tf;
405 for (i = 0; i < 13; i++)
406 tf->tf_x[i] = regs->r[i];
407 /* For arm 32, SP is r13 an LR is r14 */
408 tf->tf_x[13] = regs->r_sp;
409 tf->tf_x[14] = regs->r_lr;
410 tf->tf_elr = regs->r_pc;
411 tf->tf_spsr = regs->r_cpsr;
417 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
420 printf("ARM64TODO: fill_fpregs32");
425 set_fpregs32(struct thread *td, struct fpreg32 *regs)
428 printf("ARM64TODO: set_fpregs32");
433 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
436 printf("ARM64TODO: fill_dbregs32");
441 set_dbregs32(struct thread *td, struct dbreg32 *regs)
444 printf("ARM64TODO: set_dbregs32");
450 ptrace_set_pc(struct thread *td, u_long addr)
453 td->td_frame->tf_elr = addr;
458 ptrace_single_step(struct thread *td)
461 td->td_frame->tf_spsr |= PSR_SS;
462 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
467 ptrace_clear_single_step(struct thread *td)
470 td->td_frame->tf_spsr &= ~PSR_SS;
471 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
476 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
478 struct trapframe *tf = td->td_frame;
480 memset(tf, 0, sizeof(struct trapframe));
483 tf->tf_sp = STACKALIGN(stack);
484 tf->tf_lr = imgp->entry_addr;
485 tf->tf_elr = imgp->entry_addr;
488 /* Sanity check these are the same size, they will be memcpy'd to and fro */
489 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
490 sizeof((struct gpregs *)0)->gp_x);
491 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
492 sizeof((struct reg *)0)->x);
495 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
497 struct trapframe *tf = td->td_frame;
499 if (clear_ret & GET_MC_CLEAR_RET) {
500 mcp->mc_gpregs.gp_x[0] = 0;
501 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
503 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
504 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
507 memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
508 sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
510 mcp->mc_gpregs.gp_sp = tf->tf_sp;
511 mcp->mc_gpregs.gp_lr = tf->tf_lr;
512 mcp->mc_gpregs.gp_elr = tf->tf_elr;
513 get_fpcontext(td, mcp);
519 set_mcontext(struct thread *td, mcontext_t *mcp)
521 struct trapframe *tf = td->td_frame;
524 spsr = mcp->mc_gpregs.gp_spsr;
525 if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
526 (spsr & PSR_AARCH32) != 0 ||
527 (spsr & PSR_DAIF) != (td->td_frame->tf_spsr & PSR_DAIF))
530 memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
532 tf->tf_sp = mcp->mc_gpregs.gp_sp;
533 tf->tf_lr = mcp->mc_gpregs.gp_lr;
534 tf->tf_elr = mcp->mc_gpregs.gp_elr;
535 tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
536 set_fpcontext(td, mcp);
542 get_fpcontext(struct thread *td, mcontext_t *mcp)
549 curpcb = curthread->td_pcb;
551 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
553 * If we have just been running VFP instructions we will
554 * need to save the state to memcpy it below.
556 vfp_save_state(td, curpcb);
558 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
559 ("Called get_fpcontext while the kernel is using the VFP"));
560 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
561 ("Non-userspace FPU flags set in get_fpcontext"));
562 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
563 sizeof(mcp->mc_fpregs));
564 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
565 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
566 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
567 mcp->mc_flags |= _MC_FP_VALID;
575 set_fpcontext(struct thread *td, mcontext_t *mcp)
582 if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
583 curpcb = curthread->td_pcb;
586 * Discard any vfp state for the current thread, we
587 * are about to override it.
591 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
592 ("Called set_fpcontext while the kernel is using the VFP"));
593 memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
594 sizeof(mcp->mc_fpregs));
595 curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
596 curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
597 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
611 if (!sched_runnable())
624 /* We should have shutdown by now, if not enter a low power sleep */
627 __asm __volatile("wfi");
632 * Flush the D-cache for non-DMA I/O so that the I-cache can
633 * be made coherent later.
636 cpu_flush_dcache(void *ptr, size_t len)
642 /* Get current clock frequency for the given CPU ID. */
644 cpu_est_clockrate(int cpu_id, uint64_t *rate)
648 pc = pcpu_find(cpu_id);
649 if (pc == NULL || rate == NULL)
652 if (pc->pc_clock == 0)
655 *rate = pc->pc_clock;
660 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
663 pcpu->pc_acpi_id = 0xffffffff;
664 pcpu->pc_mpidr = 0xffffffff;
674 if (td->td_md.md_spinlock_count == 0) {
675 daif = intr_disable();
676 td->td_md.md_spinlock_count = 1;
677 td->td_md.md_saved_daif = daif;
680 td->td_md.md_spinlock_count++;
690 daif = td->td_md.md_saved_daif;
691 td->td_md.md_spinlock_count--;
692 if (td->td_md.md_spinlock_count == 0) {
698 #ifndef _SYS_SYSPROTO_H_
699 struct sigreturn_args {
705 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
710 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
713 error = set_mcontext(td, &uc.uc_mcontext);
717 /* Restore signal mask. */
718 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
720 return (EJUSTRETURN);
724 * Construct a PCB from a trapframe. This is called from kdb_trap() where
725 * we want to start a backtrace from the function that caused us to enter
726 * the debugger. We have the context in the trapframe, but base the trace
727 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
728 * enough for a backtrace.
731 makectx(struct trapframe *tf, struct pcb *pcb)
735 for (i = 0; i < PCB_LR; i++)
736 pcb->pcb_x[i] = tf->tf_x[i];
738 pcb->pcb_x[PCB_LR] = tf->tf_lr;
739 pcb->pcb_pc = tf->tf_elr;
740 pcb->pcb_sp = tf->tf_sp;
744 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
748 struct trapframe *tf;
749 struct sigframe *fp, frame;
751 struct sysentvec *sysent;
756 PROC_LOCK_ASSERT(p, MA_OWNED);
758 sig = ksi->ksi_signo;
760 mtx_assert(&psp->ps_mtx, MA_OWNED);
763 onstack = sigonstack(tf->tf_sp);
765 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
768 /* Allocate and validate space for the signal handler context. */
769 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
770 SIGISMEMBER(psp->ps_sigonstack, sig)) {
771 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
772 td->td_sigstk.ss_size);
773 #if defined(COMPAT_43)
774 td->td_sigstk.ss_flags |= SS_ONSTACK;
777 fp = (struct sigframe *)td->td_frame->tf_sp;
780 /* Make room, keeping the stack aligned */
782 fp = (struct sigframe *)STACKALIGN(fp);
784 /* Fill in the frame to copy out */
785 bzero(&frame, sizeof(frame));
786 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
787 frame.sf_si = ksi->ksi_info;
788 frame.sf_uc.uc_sigmask = *mask;
789 frame.sf_uc.uc_stack = td->td_sigstk;
790 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
791 (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
792 mtx_unlock(&psp->ps_mtx);
793 PROC_UNLOCK(td->td_proc);
795 /* Copy the sigframe out to the user's stack. */
796 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
797 /* Process has trashed its stack. Kill it. */
798 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
804 tf->tf_x[1] = (register_t)&fp->sf_si;
805 tf->tf_x[2] = (register_t)&fp->sf_uc;
807 tf->tf_elr = (register_t)catcher;
808 tf->tf_sp = (register_t)fp;
809 sysent = p->p_sysent;
810 if (sysent->sv_sigcode_base != 0)
811 tf->tf_lr = (register_t)sysent->sv_sigcode_base;
813 tf->tf_lr = (register_t)(sysent->sv_psstrings -
814 *(sysent->sv_szsigcode));
816 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
820 mtx_lock(&psp->ps_mtx);
824 init_proc0(vm_offset_t kstack)
826 struct pcpu *pcpup = &__pcpu[0];
828 proc_linkup0(&proc0, &thread0);
829 thread0.td_kstack = kstack;
830 thread0.td_kstack_pages = KSTACK_PAGES;
831 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
832 thread0.td_kstack_pages * PAGE_SIZE) - 1;
833 thread0.td_pcb->pcb_fpflags = 0;
834 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
835 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
836 thread0.td_frame = &proc0_tf;
837 pcpup->pc_curpcb = thread0.td_pcb;
846 } EFI_MEMORY_DESCRIPTOR;
848 typedef void (*efi_map_entry_cb)(struct efi_md *);
851 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb)
853 struct efi_md *map, *p;
858 * Memory map data provided by UEFI via the GetMemoryMap
861 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
862 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
864 if (efihdr->descriptor_size == 0)
866 ndesc = efihdr->memory_size / efihdr->descriptor_size;
868 for (i = 0, p = map; i < ndesc; i++,
869 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
875 exclude_efi_map_entry(struct efi_md *p)
878 switch (p->md_type) {
879 case EFI_MD_TYPE_CODE:
880 case EFI_MD_TYPE_DATA:
881 case EFI_MD_TYPE_BS_CODE:
882 case EFI_MD_TYPE_BS_DATA:
883 case EFI_MD_TYPE_FREE:
885 * We're allowed to use any entry with these types.
889 physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
895 exclude_efi_map_entries(struct efi_map_header *efihdr)
898 foreach_efi_map_entry(efihdr, exclude_efi_map_entry);
902 add_efi_map_entry(struct efi_md *p)
905 switch (p->md_type) {
906 case EFI_MD_TYPE_RT_DATA:
908 * Runtime data will be excluded after the DMAP
909 * region is created to stop it from being added
912 case EFI_MD_TYPE_CODE:
913 case EFI_MD_TYPE_DATA:
914 case EFI_MD_TYPE_BS_CODE:
915 case EFI_MD_TYPE_BS_DATA:
916 case EFI_MD_TYPE_FREE:
918 * We're allowed to use any entry with these types.
920 physmem_hardware_region(p->md_phys,
921 p->md_pages * PAGE_SIZE);
927 add_efi_map_entries(struct efi_map_header *efihdr)
930 foreach_efi_map_entry(efihdr, add_efi_map_entry);
934 print_efi_map_entry(struct efi_md *p)
937 static const char *types[] = {
943 "RuntimeServicesCode",
944 "RuntimeServicesData",
945 "ConventionalMemory",
950 "MemoryMappedIOPortSpace",
955 if (p->md_type < nitems(types))
956 type = types[p->md_type];
959 printf("%23s %012lx %12p %08lx ", type, p->md_phys,
960 p->md_virt, p->md_pages);
961 if (p->md_attr & EFI_MD_ATTR_UC)
963 if (p->md_attr & EFI_MD_ATTR_WC)
965 if (p->md_attr & EFI_MD_ATTR_WT)
967 if (p->md_attr & EFI_MD_ATTR_WB)
969 if (p->md_attr & EFI_MD_ATTR_UCE)
971 if (p->md_attr & EFI_MD_ATTR_WP)
973 if (p->md_attr & EFI_MD_ATTR_RP)
975 if (p->md_attr & EFI_MD_ATTR_XP)
977 if (p->md_attr & EFI_MD_ATTR_NV)
979 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
980 printf("MORE_RELIABLE ");
981 if (p->md_attr & EFI_MD_ATTR_RO)
983 if (p->md_attr & EFI_MD_ATTR_RT)
989 print_efi_map_entries(struct efi_map_header *efihdr)
992 printf("%23s %12s %12s %8s %4s\n",
993 "Type", "Physical", "Virtual", "#Pages", "Attr");
994 foreach_efi_map_entry(efihdr, print_efi_map_entry);
999 try_load_dtb(caddr_t kmdp)
1003 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1004 #if defined(FDT_DTB_STATIC)
1006 * In case the device tree blob was not retrieved (from metadata) try
1007 * to use the statically embedded one.
1010 dtbp = (vm_offset_t)&fdt_static_dtb;
1013 if (dtbp == (vm_offset_t)NULL) {
1014 printf("ERROR loading DTB\n");
1018 if (OF_install(OFW_FDT, 0) == FALSE)
1019 panic("Cannot install FDT");
1021 if (OF_init((void *)dtbp) != 0)
1022 panic("OF_init failed with the found device tree");
1024 parse_fdt_bootargs();
1031 bool has_acpi, has_fdt;
1034 has_acpi = has_fdt = false;
1037 has_fdt = (OF_peer(0) != 0);
1040 has_acpi = (AcpiOsGetRootPointer() != 0);
1043 env = kern_getenv("kern.cfg.order");
1046 while (order != NULL) {
1048 strncmp(order, "acpi", 4) == 0 &&
1049 (order[4] == ',' || order[4] == '\0')) {
1050 arm64_bus_method = ARM64_BUS_ACPI;
1054 strncmp(order, "fdt", 3) == 0 &&
1055 (order[3] == ',' || order[3] == '\0')) {
1056 arm64_bus_method = ARM64_BUS_FDT;
1059 order = strchr(order, ',');
1063 /* If we set the bus method it is valid */
1064 if (arm64_bus_method != ARM64_BUS_NONE)
1067 /* If no order or an invalid order was set use the default */
1068 if (arm64_bus_method == ARM64_BUS_NONE) {
1070 arm64_bus_method = ARM64_BUS_FDT;
1072 arm64_bus_method = ARM64_BUS_ACPI;
1076 * If no option was set the default is valid, otherwise we are
1077 * setting one to get cninit() working, then calling panic to tell
1078 * the user about the invalid bus setup.
1080 return (env == NULL);
1086 int dczva_line_shift;
1089 identify_cache(READ_SPECIALREG(ctr_el0));
1091 dczid_el0 = READ_SPECIALREG(dczid_el0);
1093 /* Check if dc zva is not prohibited */
1094 if (dczid_el0 & DCZID_DZP)
1095 dczva_line_size = 0;
1097 /* Same as with above calculations */
1098 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
1099 dczva_line_size = sizeof(int) << dczva_line_shift;
1101 /* Change pagezero function */
1102 pagezero = pagezero_cache;
1107 memory_mapping_mode(vm_paddr_t pa)
1109 struct efi_md *map, *p;
1114 return (VM_MEMATTR_WRITE_BACK);
1117 * Memory map data provided by UEFI via the GetMemoryMap
1118 * Boot Services API.
1120 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
1121 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
1123 if (efihdr->descriptor_size == 0)
1124 return (VM_MEMATTR_WRITE_BACK);
1125 ndesc = efihdr->memory_size / efihdr->descriptor_size;
1127 for (i = 0, p = map; i < ndesc; i++,
1128 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
1129 if (pa < p->md_phys ||
1130 pa >= p->md_phys + p->md_pages * EFI_PAGE_SIZE)
1132 if (p->md_type == EFI_MD_TYPE_IOMEM ||
1133 p->md_type == EFI_MD_TYPE_IOPORT)
1134 return (VM_MEMATTR_DEVICE);
1135 else if ((p->md_attr & EFI_MD_ATTR_WB) != 0 ||
1136 p->md_type == EFI_MD_TYPE_RECLAIM)
1137 return (VM_MEMATTR_WRITE_BACK);
1138 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
1139 return (VM_MEMATTR_WRITE_THROUGH);
1140 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
1141 return (VM_MEMATTR_WRITE_COMBINING);
1145 return (VM_MEMATTR_DEVICE);
1149 initarm(struct arm64_bootparams *abp)
1151 struct efi_fb *efifb;
1155 struct mem_region mem_regions[FDT_MEM_REGIONS];
1158 vm_offset_t lastaddr;
1162 boot_el = abp->boot_el;
1164 /* Parse loader or FDT boot parametes. Determine last used address. */
1165 lastaddr = parse_boot_param(abp);
1167 /* Find the kernel address */
1168 kmdp = preload_search_by_type("elf kernel");
1170 kmdp = preload_search_by_type("elf64 kernel");
1173 update_special_regs(0);
1175 link_elf_ireloc(kmdp);
1178 efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
1180 /* Load the physical memory ranges */
1181 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1182 MODINFO_METADATA | MODINFOMD_EFI_MAP);
1184 add_efi_map_entries(efihdr);
1187 /* Grab physical memory regions information from device tree. */
1188 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
1190 panic("Cannot get physical memory regions");
1191 physmem_hardware_regions(mem_regions, mem_regions_sz);
1193 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
1194 physmem_exclude_regions(mem_regions, mem_regions_sz,
1195 EXFLAG_NODUMP | EXFLAG_NOALLOC);
1198 /* Exclude the EFI framebuffer from our view of physical memory. */
1199 efifb = (struct efi_fb *)preload_search_info(kmdp,
1200 MODINFO_METADATA | MODINFOMD_EFI_FB);
1202 physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
1205 /* Set the pcpu data, this is needed by pmap_bootstrap */
1207 pcpu_init(pcpup, 0, sizeof(struct pcpu));
1210 * Set the pcpu pointer with a backup in tpidr_el1 to be
1211 * loaded when entering the kernel from userland.
1215 "msr tpidr_el1, %0" :: "r"(pcpup));
1217 PCPU_SET(curthread, &thread0);
1218 PCPU_SET(midr, get_midr());
1220 /* Do basic tuning, hz etc */
1226 /* Bootstrap enough of pmap to enter the kernel proper */
1227 pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
1228 KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
1229 /* Exclude entries neexed in teh DMAP region, but not phys_avail */
1231 exclude_efi_map_entries(efihdr);
1232 physmem_init_kernel_globals();
1234 devmap_bootstrap(0, NULL);
1236 valid = bus_probe();
1239 set_ttbr0(abp->kern_ttbr0);
1243 panic("Invalid bus configuration: %s",
1244 kern_getenv("kern.cfg.order"));
1247 * Dump the boot metadata. We have to wait for cninit() since console
1248 * output is required. If it's grossly incorrect the kernel will never
1251 if (getenv_is_true("debug.dump_modinfo_at_boot"))
1254 init_proc0(abp->kern_stack);
1255 msgbufinit(msgbufp, msgbufsize);
1257 init_param2(physmem);
1265 env = kern_getenv("kernelname");
1267 strlcpy(kernelname, env, sizeof(kernelname));
1269 if (boothowto & RB_VERBOSE) {
1271 print_efi_map_entries(efihdr);
1272 physmem_print_tables();
1283 WRITE_SPECIALREG(oslar_el1, 0);
1285 /* This permits DDB to use debug registers for watchpoints. */
1288 /* TODO: Eventually will need to initialize debug registers here. */
1292 #include <ddb/ddb.h>
1294 DB_SHOW_COMMAND(specialregs, db_show_spregs)
1296 #define PRINT_REG(reg) \
1297 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
1299 PRINT_REG(actlr_el1);
1300 PRINT_REG(afsr0_el1);
1301 PRINT_REG(afsr1_el1);
1302 PRINT_REG(aidr_el1);
1303 PRINT_REG(amair_el1);
1304 PRINT_REG(ccsidr_el1);
1305 PRINT_REG(clidr_el1);
1306 PRINT_REG(contextidr_el1);
1307 PRINT_REG(cpacr_el1);
1308 PRINT_REG(csselr_el1);
1310 PRINT_REG(currentel);
1312 PRINT_REG(dczid_el0);
1317 /* ARM64TODO: Enable VFP before reading floating-point registers */
1321 PRINT_REG(id_aa64afr0_el1);
1322 PRINT_REG(id_aa64afr1_el1);
1323 PRINT_REG(id_aa64dfr0_el1);
1324 PRINT_REG(id_aa64dfr1_el1);
1325 PRINT_REG(id_aa64isar0_el1);
1326 PRINT_REG(id_aa64isar1_el1);
1327 PRINT_REG(id_aa64pfr0_el1);
1328 PRINT_REG(id_aa64pfr1_el1);
1329 PRINT_REG(id_afr0_el1);
1330 PRINT_REG(id_dfr0_el1);
1331 PRINT_REG(id_isar0_el1);
1332 PRINT_REG(id_isar1_el1);
1333 PRINT_REG(id_isar2_el1);
1334 PRINT_REG(id_isar3_el1);
1335 PRINT_REG(id_isar4_el1);
1336 PRINT_REG(id_isar5_el1);
1337 PRINT_REG(id_mmfr0_el1);
1338 PRINT_REG(id_mmfr1_el1);
1339 PRINT_REG(id_mmfr2_el1);
1340 PRINT_REG(id_mmfr3_el1);
1342 /* Missing from llvm */
1343 PRINT_REG(id_mmfr4_el1);
1345 PRINT_REG(id_pfr0_el1);
1346 PRINT_REG(id_pfr1_el1);
1348 PRINT_REG(mair_el1);
1349 PRINT_REG(midr_el1);
1350 PRINT_REG(mpidr_el1);
1351 PRINT_REG(mvfr0_el1);
1352 PRINT_REG(mvfr1_el1);
1353 PRINT_REG(mvfr2_el1);
1354 PRINT_REG(revidr_el1);
1355 PRINT_REG(sctlr_el1);
1358 PRINT_REG(spsr_el1);
1360 PRINT_REG(tpidr_el0);
1361 PRINT_REG(tpidr_el1);
1362 PRINT_REG(tpidrro_el0);
1363 PRINT_REG(ttbr0_el1);
1364 PRINT_REG(ttbr1_el1);
1365 PRINT_REG(vbar_el1);
1369 DB_SHOW_COMMAND(vtop, db_show_vtop)
1374 phys = arm64_address_translate_s1e1r(addr);
1375 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
1376 phys = arm64_address_translate_s1e1w(addr);
1377 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1378 phys = arm64_address_translate_s1e0r(addr);
1379 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
1380 phys = arm64_address_translate_s1e0w(addr);
1381 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1383 db_printf("show vtop <virt_addr>\n");