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>
48 #include <sys/limits.h>
49 #include <sys/linker.h>
50 #include <sys/msgbuf.h>
53 #include <sys/ptrace.h>
54 #include <sys/reboot.h>
55 #include <sys/rwlock.h>
56 #include <sys/sched.h>
57 #include <sys/signalvar.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysent.h>
60 #include <sys/sysproto.h>
61 #include <sys/ucontext.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_page.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_pager.h>
72 #include <machine/armreg.h>
73 #include <machine/cpu.h>
74 #include <machine/debug_monitor.h>
75 #include <machine/kdb.h>
76 #include <machine/machdep.h>
77 #include <machine/metadata.h>
78 #include <machine/md_var.h>
79 #include <machine/pcb.h>
80 #include <machine/reg.h>
81 #include <machine/undefined.h>
82 #include <machine/vmparam.h>
84 #include <arm/include/physmem.h>
87 #include <machine/vfp.h>
91 #include <contrib/dev/acpica/include/acpi.h>
92 #include <machine/acpica_machdep.h>
96 #include <dev/fdt/fdt_common.h>
97 #include <dev/ofw/openfirm.h>
101 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
103 struct pcpu __pcpu[MAXCPU];
105 static struct trapframe proc0_tf;
110 struct kva_md_info kmi;
112 int64_t dcache_line_size; /* The minimum D cache line size */
113 int64_t icache_line_size; /* The minimum I cache line size */
114 int64_t idcache_line_size; /* The minimum cache line size */
115 int64_t dczva_line_size; /* The size of cache line the dc zva zeroes */
119 * Physical address of the EFI System Table. Stashed from the metadata hints
120 * passed into the kernel and used by the EFI code to call runtime services.
122 vm_paddr_t efi_systbl_phys;
124 /* pagezero_* implementations are provided in support.S */
125 void pagezero_simple(void *);
126 void pagezero_cache(void *);
128 /* pagezero_simple is default pagezero */
129 void (*pagezero)(void *p) = pagezero_simple;
134 uint64_t id_aa64mfr1;
136 id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
137 if (ID_AA64MMFR1_PAN_VAL(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE)
146 * The LLVM integrated assembler doesn't understand the PAN
147 * PSTATE field. Because of this we need to manually create
148 * the instruction in an asm block. This is equivalent to:
151 * This sets the PAN bit, stopping the kernel from accessing
152 * memory when userspace can also access it unless the kernel
153 * uses the userspace load/store instructions.
156 WRITE_SPECIALREG(sctlr_el1,
157 READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
158 __asm __volatile(".inst 0xd500409f | (0x1 << 8)");
163 cpu_startup(void *dummy)
168 install_cpu_errata();
170 vm_ksubmap_init(&kmi);
172 vm_pager_bufferinit();
175 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
178 cpu_idle_wakeup(int cpu)
185 fill_regs(struct thread *td, struct reg *regs)
187 struct trapframe *frame;
189 frame = td->td_frame;
190 regs->sp = frame->tf_sp;
191 regs->lr = frame->tf_lr;
192 regs->elr = frame->tf_elr;
193 regs->spsr = frame->tf_spsr;
195 memcpy(regs->x, frame->tf_x, sizeof(regs->x));
197 #ifdef COMPAT_FREEBSD32
199 * We may be called here for a 32bits process, if we're using a
200 * 64bits debugger. If so, put PC and SPSR where it expects it.
202 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
203 regs->x[15] = frame->tf_elr;
204 regs->x[16] = frame->tf_spsr;
211 set_regs(struct thread *td, struct reg *regs)
213 struct trapframe *frame;
215 frame = td->td_frame;
216 frame->tf_sp = regs->sp;
217 frame->tf_lr = regs->lr;
218 frame->tf_elr = regs->elr;
219 frame->tf_spsr &= ~PSR_FLAGS;
220 frame->tf_spsr |= regs->spsr & PSR_FLAGS;
222 memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
224 #ifdef COMPAT_FREEBSD32
225 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
227 * We may be called for a 32bits process if we're using
228 * a 64bits debugger. If so, get PC and SPSR from where
231 frame->tf_elr = regs->x[15];
232 frame->tf_spsr = regs->x[16] & PSR_FLAGS;
239 fill_fpregs(struct thread *td, struct fpreg *regs)
245 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
247 * If we have just been running VFP instructions we will
248 * need to save the state to memcpy it below.
251 vfp_save_state(td, pcb);
253 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
254 ("Called fill_fpregs while the kernel is using the VFP"));
255 memcpy(regs->fp_q, pcb->pcb_fpustate.vfp_regs,
257 regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
258 regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
261 memset(regs, 0, sizeof(*regs));
266 set_fpregs(struct thread *td, struct fpreg *regs)
272 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
273 ("Called set_fpregs while the kernel is using the VFP"));
274 memcpy(pcb->pcb_fpustate.vfp_regs, regs->fp_q, sizeof(regs->fp_q));
275 pcb->pcb_fpustate.vfp_fpcr = regs->fp_cr;
276 pcb->pcb_fpustate.vfp_fpsr = regs->fp_sr;
282 fill_dbregs(struct thread *td, struct dbreg *regs)
284 struct debug_monitor_state *monitor;
286 uint8_t debug_ver, nbkpts;
288 memset(regs, 0, sizeof(*regs));
290 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_DebugVer_SHIFT,
292 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_BRPs_SHIFT,
296 * The BRPs field contains the number of breakpoints - 1. Armv8-A
297 * allows the hardware to provide 2-16 breakpoints so this won't
298 * overflow an 8 bit value.
302 regs->db_info = debug_ver;
304 regs->db_info |= count;
306 monitor = &td->td_pcb->pcb_dbg_regs;
307 if ((monitor->dbg_flags & DBGMON_ENABLED) != 0) {
308 for (i = 0; i < count; i++) {
309 regs->db_regs[i].dbr_addr = monitor->dbg_bvr[i];
310 regs->db_regs[i].dbr_ctrl = monitor->dbg_bcr[i];
318 set_dbregs(struct thread *td, struct dbreg *regs)
320 struct debug_monitor_state *monitor;
324 monitor = &td->td_pcb->pcb_dbg_regs;
326 monitor->dbg_enable_count = 0;
327 for (i = 0; i < DBG_BRP_MAX; i++) {
328 /* TODO: Check these values */
329 monitor->dbg_bvr[i] = regs->db_regs[i].dbr_addr;
330 monitor->dbg_bcr[i] = regs->db_regs[i].dbr_ctrl;
331 if ((monitor->dbg_bcr[i] & 1) != 0)
332 monitor->dbg_enable_count++;
334 if (monitor->dbg_enable_count > 0)
335 monitor->dbg_flags |= DBGMON_ENABLED;
340 #ifdef COMPAT_FREEBSD32
342 fill_regs32(struct thread *td, struct reg32 *regs)
345 struct trapframe *tf;
348 for (i = 0; i < 13; i++)
349 regs->r[i] = tf->tf_x[i];
350 /* For arm32, SP is r13 and LR is r14 */
351 regs->r_sp = tf->tf_x[13];
352 regs->r_lr = tf->tf_x[14];
353 regs->r_pc = tf->tf_elr;
354 regs->r_cpsr = tf->tf_spsr;
360 set_regs32(struct thread *td, struct reg32 *regs)
363 struct trapframe *tf;
366 for (i = 0; i < 13; i++)
367 tf->tf_x[i] = regs->r[i];
368 /* For arm 32, SP is r13 an LR is r14 */
369 tf->tf_x[13] = regs->r_sp;
370 tf->tf_x[14] = regs->r_lr;
371 tf->tf_elr = regs->r_pc;
372 tf->tf_spsr = regs->r_cpsr;
379 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
382 printf("ARM64TODO: fill_fpregs32");
387 set_fpregs32(struct thread *td, struct fpreg32 *regs)
390 printf("ARM64TODO: set_fpregs32");
395 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
398 printf("ARM64TODO: fill_dbregs32");
403 set_dbregs32(struct thread *td, struct dbreg32 *regs)
406 printf("ARM64TODO: set_dbregs32");
412 ptrace_set_pc(struct thread *td, u_long addr)
415 printf("ARM64TODO: ptrace_set_pc");
420 ptrace_single_step(struct thread *td)
423 td->td_frame->tf_spsr |= PSR_SS;
424 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
429 ptrace_clear_single_step(struct thread *td)
432 td->td_frame->tf_spsr &= ~PSR_SS;
433 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
438 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
440 struct trapframe *tf = td->td_frame;
442 memset(tf, 0, sizeof(struct trapframe));
445 tf->tf_sp = STACKALIGN(stack);
446 tf->tf_lr = imgp->entry_addr;
447 tf->tf_elr = imgp->entry_addr;
450 /* Sanity check these are the same size, they will be memcpy'd to and fro */
451 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
452 sizeof((struct gpregs *)0)->gp_x);
453 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
454 sizeof((struct reg *)0)->x);
457 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
459 struct trapframe *tf = td->td_frame;
461 if (clear_ret & GET_MC_CLEAR_RET) {
462 mcp->mc_gpregs.gp_x[0] = 0;
463 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
465 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
466 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
469 memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
470 sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
472 mcp->mc_gpregs.gp_sp = tf->tf_sp;
473 mcp->mc_gpregs.gp_lr = tf->tf_lr;
474 mcp->mc_gpregs.gp_elr = tf->tf_elr;
480 set_mcontext(struct thread *td, mcontext_t *mcp)
482 struct trapframe *tf = td->td_frame;
485 spsr = mcp->mc_gpregs.gp_spsr;
486 if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
487 (spsr & PSR_AARCH32) != 0 ||
488 (spsr & PSR_DAIF) != (td->td_frame->tf_spsr & PSR_DAIF))
491 memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
493 tf->tf_sp = mcp->mc_gpregs.gp_sp;
494 tf->tf_lr = mcp->mc_gpregs.gp_lr;
495 tf->tf_elr = mcp->mc_gpregs.gp_elr;
496 tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
502 get_fpcontext(struct thread *td, mcontext_t *mcp)
509 curpcb = curthread->td_pcb;
511 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
513 * If we have just been running VFP instructions we will
514 * need to save the state to memcpy it below.
516 vfp_save_state(td, curpcb);
518 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
519 ("Called get_fpcontext while the kernel is using the VFP"));
520 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
521 ("Non-userspace FPU flags set in get_fpcontext"));
522 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
523 sizeof(mcp->mc_fpregs));
524 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
525 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
526 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
527 mcp->mc_flags |= _MC_FP_VALID;
535 set_fpcontext(struct thread *td, mcontext_t *mcp)
542 if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
543 curpcb = curthread->td_pcb;
546 * Discard any vfp state for the current thread, we
547 * are about to override it.
551 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
552 ("Called set_fpcontext while the kernel is using the VFP"));
553 memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
554 sizeof(mcp->mc_fpregs));
555 curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
556 curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
557 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
571 if (!sched_runnable())
584 /* We should have shutdown by now, if not enter a low power sleep */
587 __asm __volatile("wfi");
592 * Flush the D-cache for non-DMA I/O so that the I-cache can
593 * be made coherent later.
596 cpu_flush_dcache(void *ptr, size_t len)
602 /* Get current clock frequency for the given CPU ID. */
604 cpu_est_clockrate(int cpu_id, uint64_t *rate)
608 pc = pcpu_find(cpu_id);
609 if (pc == NULL || rate == NULL)
612 if (pc->pc_clock == 0)
615 *rate = pc->pc_clock;
620 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
623 pcpu->pc_acpi_id = 0xffffffff;
633 if (td->td_md.md_spinlock_count == 0) {
634 daif = intr_disable();
635 td->td_md.md_spinlock_count = 1;
636 td->td_md.md_saved_daif = daif;
638 td->td_md.md_spinlock_count++;
650 daif = td->td_md.md_saved_daif;
651 td->td_md.md_spinlock_count--;
652 if (td->td_md.md_spinlock_count == 0)
656 #ifndef _SYS_SYSPROTO_H_
657 struct sigreturn_args {
663 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
670 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
673 error = set_mcontext(td, &uc.uc_mcontext);
676 set_fpcontext(td, &uc.uc_mcontext);
678 /* Restore signal mask. */
679 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
681 return (EJUSTRETURN);
685 * Construct a PCB from a trapframe. This is called from kdb_trap() where
686 * we want to start a backtrace from the function that caused us to enter
687 * the debugger. We have the context in the trapframe, but base the trace
688 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
689 * enough for a backtrace.
692 makectx(struct trapframe *tf, struct pcb *pcb)
696 for (i = 0; i < PCB_LR; i++)
697 pcb->pcb_x[i] = tf->tf_x[i];
699 pcb->pcb_x[PCB_LR] = tf->tf_lr;
700 pcb->pcb_pc = tf->tf_elr;
701 pcb->pcb_sp = tf->tf_sp;
705 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
709 struct trapframe *tf;
710 struct sigframe *fp, frame;
712 struct sysentvec *sysent;
717 PROC_LOCK_ASSERT(p, MA_OWNED);
719 sig = ksi->ksi_signo;
721 mtx_assert(&psp->ps_mtx, MA_OWNED);
724 onstack = sigonstack(tf->tf_sp);
726 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
729 /* Allocate and validate space for the signal handler context. */
730 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
731 SIGISMEMBER(psp->ps_sigonstack, sig)) {
732 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
733 td->td_sigstk.ss_size);
734 #if defined(COMPAT_43)
735 td->td_sigstk.ss_flags |= SS_ONSTACK;
738 fp = (struct sigframe *)td->td_frame->tf_sp;
741 /* Make room, keeping the stack aligned */
743 fp = (struct sigframe *)STACKALIGN(fp);
745 /* Fill in the frame to copy out */
746 bzero(&frame, sizeof(frame));
747 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
748 get_fpcontext(td, &frame.sf_uc.uc_mcontext);
749 frame.sf_si = ksi->ksi_info;
750 frame.sf_uc.uc_sigmask = *mask;
751 frame.sf_uc.uc_stack = td->td_sigstk;
752 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
753 (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
754 mtx_unlock(&psp->ps_mtx);
755 PROC_UNLOCK(td->td_proc);
757 /* Copy the sigframe out to the user's stack. */
758 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
759 /* Process has trashed its stack. Kill it. */
760 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
766 tf->tf_x[1] = (register_t)&fp->sf_si;
767 tf->tf_x[2] = (register_t)&fp->sf_uc;
769 tf->tf_elr = (register_t)catcher;
770 tf->tf_sp = (register_t)fp;
771 sysent = p->p_sysent;
772 if (sysent->sv_sigcode_base != 0)
773 tf->tf_lr = (register_t)sysent->sv_sigcode_base;
775 tf->tf_lr = (register_t)(sysent->sv_psstrings -
776 *(sysent->sv_szsigcode));
778 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
782 mtx_lock(&psp->ps_mtx);
786 init_proc0(vm_offset_t kstack)
788 struct pcpu *pcpup = &__pcpu[0];
790 proc_linkup0(&proc0, &thread0);
791 thread0.td_kstack = kstack;
792 thread0.td_kstack_pages = KSTACK_PAGES;
793 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
794 thread0.td_kstack_pages * PAGE_SIZE) - 1;
795 thread0.td_pcb->pcb_fpflags = 0;
796 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
797 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
798 thread0.td_frame = &proc0_tf;
799 pcpup->pc_curpcb = thread0.td_pcb;
808 } EFI_MEMORY_DESCRIPTOR;
810 typedef void (*efi_map_entry_cb)(struct efi_md *);
813 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb)
815 struct efi_md *map, *p;
820 * Memory map data provided by UEFI via the GetMemoryMap
823 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
824 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
826 if (efihdr->descriptor_size == 0)
828 ndesc = efihdr->memory_size / efihdr->descriptor_size;
830 for (i = 0, p = map; i < ndesc; i++,
831 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
837 exclude_efi_map_entry(struct efi_md *p)
840 switch (p->md_type) {
841 case EFI_MD_TYPE_CODE:
842 case EFI_MD_TYPE_DATA:
843 case EFI_MD_TYPE_BS_CODE:
844 case EFI_MD_TYPE_BS_DATA:
845 case EFI_MD_TYPE_FREE:
847 * We're allowed to use any entry with these types.
851 arm_physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
857 exclude_efi_map_entries(struct efi_map_header *efihdr)
860 foreach_efi_map_entry(efihdr, exclude_efi_map_entry);
864 add_efi_map_entry(struct efi_md *p)
867 switch (p->md_type) {
868 case EFI_MD_TYPE_RT_DATA:
870 * Runtime data will be excluded after the DMAP
871 * region is created to stop it from being added
874 case EFI_MD_TYPE_CODE:
875 case EFI_MD_TYPE_DATA:
876 case EFI_MD_TYPE_BS_CODE:
877 case EFI_MD_TYPE_BS_DATA:
878 case EFI_MD_TYPE_FREE:
880 * We're allowed to use any entry with these types.
882 arm_physmem_hardware_region(p->md_phys,
883 p->md_pages * PAGE_SIZE);
889 add_efi_map_entries(struct efi_map_header *efihdr)
892 foreach_efi_map_entry(efihdr, add_efi_map_entry);
896 print_efi_map_entry(struct efi_md *p)
899 static const char *types[] = {
905 "RuntimeServicesCode",
906 "RuntimeServicesData",
907 "ConventionalMemory",
912 "MemoryMappedIOPortSpace",
917 if (p->md_type < nitems(types))
918 type = types[p->md_type];
921 printf("%23s %012lx %12p %08lx ", type, p->md_phys,
922 p->md_virt, p->md_pages);
923 if (p->md_attr & EFI_MD_ATTR_UC)
925 if (p->md_attr & EFI_MD_ATTR_WC)
927 if (p->md_attr & EFI_MD_ATTR_WT)
929 if (p->md_attr & EFI_MD_ATTR_WB)
931 if (p->md_attr & EFI_MD_ATTR_UCE)
933 if (p->md_attr & EFI_MD_ATTR_WP)
935 if (p->md_attr & EFI_MD_ATTR_RP)
937 if (p->md_attr & EFI_MD_ATTR_XP)
939 if (p->md_attr & EFI_MD_ATTR_NV)
941 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
942 printf("MORE_RELIABLE ");
943 if (p->md_attr & EFI_MD_ATTR_RO)
945 if (p->md_attr & EFI_MD_ATTR_RT)
951 print_efi_map_entries(struct efi_map_header *efihdr)
954 printf("%23s %12s %12s %8s %4s\n",
955 "Type", "Physical", "Virtual", "#Pages", "Attr");
956 foreach_efi_map_entry(efihdr, print_efi_map_entry);
961 try_load_dtb(caddr_t kmdp)
965 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
967 #if defined(FDT_DTB_STATIC)
969 * In case the device tree blob was not retrieved (from metadata) try
970 * to use the statically embedded one.
973 dtbp = (vm_offset_t)&fdt_static_dtb;
976 if (dtbp == (vm_offset_t)NULL) {
977 printf("ERROR loading DTB\n");
981 if (OF_install(OFW_FDT, 0) == FALSE)
982 panic("Cannot install FDT");
984 if (OF_init((void *)dtbp) != 0)
985 panic("OF_init failed with the found device tree");
992 bool has_acpi, has_fdt;
995 has_acpi = has_fdt = false;
998 has_fdt = (OF_peer(0) != 0);
1001 has_acpi = (acpi_find_table(ACPI_SIG_SPCR) != 0);
1004 env = kern_getenv("kern.cfg.order");
1007 while (order != NULL) {
1009 strncmp(order, "acpi", 4) == 0 &&
1010 (order[4] == ',' || order[4] == '\0')) {
1011 arm64_bus_method = ARM64_BUS_ACPI;
1015 strncmp(order, "fdt", 3) == 0 &&
1016 (order[3] == ',' || order[3] == '\0')) {
1017 arm64_bus_method = ARM64_BUS_FDT;
1020 order = strchr(order, ',');
1024 /* If we set the bus method it is valid */
1025 if (arm64_bus_method != ARM64_BUS_NONE)
1028 /* If no order or an invalid order was set use the default */
1029 if (arm64_bus_method == ARM64_BUS_NONE) {
1031 arm64_bus_method = ARM64_BUS_FDT;
1033 arm64_bus_method = ARM64_BUS_ACPI;
1037 * If no option was set the default is valid, otherwise we are
1038 * setting one to get cninit() working, then calling panic to tell
1039 * the user about the invalid bus setup.
1041 return (env == NULL);
1047 int dcache_line_shift, icache_line_shift, dczva_line_shift;
1051 ctr_el0 = READ_SPECIALREG(ctr_el0);
1053 /* Read the log2 words in each D cache line */
1054 dcache_line_shift = CTR_DLINE_SIZE(ctr_el0);
1055 /* Get the D cache line size */
1056 dcache_line_size = sizeof(int) << dcache_line_shift;
1058 /* And the same for the I cache */
1059 icache_line_shift = CTR_ILINE_SIZE(ctr_el0);
1060 icache_line_size = sizeof(int) << icache_line_shift;
1062 idcache_line_size = MIN(dcache_line_size, icache_line_size);
1064 dczid_el0 = READ_SPECIALREG(dczid_el0);
1066 /* Check if dc zva is not prohibited */
1067 if (dczid_el0 & DCZID_DZP)
1068 dczva_line_size = 0;
1070 /* Same as with above calculations */
1071 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
1072 dczva_line_size = sizeof(int) << dczva_line_shift;
1074 /* Change pagezero function */
1075 pagezero = pagezero_cache;
1080 freebsd_parse_boot_param(struct arm64_bootparams *abp)
1082 vm_offset_t lastaddr;
1084 static char *loader_envp;
1086 preload_metadata = (caddr_t)(uintptr_t)(abp->modulep);
1087 kmdp = preload_search_by_type("elf kernel");
1091 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
1092 loader_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
1093 init_static_kenv(loader_envp, 0);
1094 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
1100 initarm(struct arm64_bootparams *abp)
1102 struct efi_fb *efifb;
1103 struct efi_map_header *efihdr;
1107 struct mem_region mem_regions[FDT_MEM_REGIONS];
1110 vm_offset_t lastaddr;
1114 if ((abp->modulep & VM_MIN_KERNEL_ADDRESS) ==
1115 VM_MIN_KERNEL_ADDRESS)
1116 /* Booted from loader. */
1117 lastaddr = freebsd_parse_boot_param(abp);
1118 #ifdef LINUX_BOOT_ABI
1120 /* Booted from U-Boot. */
1121 lastaddr = linux_parse_boot_param(abp);
1124 /* Find the kernel address */
1125 kmdp = preload_search_by_type("elf kernel");
1127 kmdp = preload_search_by_type("elf64 kernel");
1129 link_elf_ireloc(kmdp);
1133 #ifdef LINUX_BOOT_ABI
1134 parse_bootargs(&lastaddr, abp);
1138 efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
1140 /* Load the physical memory ranges */
1141 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1142 MODINFO_METADATA | MODINFOMD_EFI_MAP);
1144 add_efi_map_entries(efihdr);
1147 /* Grab physical memory regions information from device tree. */
1148 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
1150 panic("Cannot get physical memory regions");
1151 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1153 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
1154 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1155 EXFLAG_NODUMP | EXFLAG_NOALLOC);
1158 /* Exclude the EFI framebuffer from our view of physical memory. */
1159 efifb = (struct efi_fb *)preload_search_info(kmdp,
1160 MODINFO_METADATA | MODINFOMD_EFI_FB);
1162 arm_physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
1165 /* Set the pcpu data, this is needed by pmap_bootstrap */
1167 pcpu_init(pcpup, 0, sizeof(struct pcpu));
1170 * Set the pcpu pointer with a backup in tpidr_el1 to be
1171 * loaded when entering the kernel from userland.
1175 "msr tpidr_el1, %0" :: "r"(pcpup));
1177 PCPU_SET(curthread, &thread0);
1179 /* Do basic tuning, hz etc */
1185 /* Bootstrap enough of pmap to enter the kernel proper */
1186 pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
1187 KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
1188 /* Exclude entries neexed in teh DMAP region, but not phys_avail */
1190 exclude_efi_map_entries(efihdr);
1191 arm_physmem_init_kernel_globals();
1193 devmap_bootstrap(0, NULL);
1195 valid = bus_probe();
1200 panic("Invalid bus configuration: %s",
1201 kern_getenv("kern.cfg.order"));
1203 init_proc0(abp->kern_stack);
1204 msgbufinit(msgbufp, msgbufsize);
1206 init_param2(physmem);
1212 env = kern_getenv("kernelname");
1214 strlcpy(kernelname, env, sizeof(kernelname));
1216 if (boothowto & RB_VERBOSE) {
1217 print_efi_map_entries(efihdr);
1218 arm_physmem_print_tables();
1229 WRITE_SPECIALREG(oslar_el1, 0);
1231 /* This permits DDB to use debug registers for watchpoints. */
1234 /* TODO: Eventually will need to initialize debug registers here. */
1238 #include <ddb/ddb.h>
1240 DB_SHOW_COMMAND(specialregs, db_show_spregs)
1242 #define PRINT_REG(reg) \
1243 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
1245 PRINT_REG(actlr_el1);
1246 PRINT_REG(afsr0_el1);
1247 PRINT_REG(afsr1_el1);
1248 PRINT_REG(aidr_el1);
1249 PRINT_REG(amair_el1);
1250 PRINT_REG(ccsidr_el1);
1251 PRINT_REG(clidr_el1);
1252 PRINT_REG(contextidr_el1);
1253 PRINT_REG(cpacr_el1);
1254 PRINT_REG(csselr_el1);
1256 PRINT_REG(currentel);
1258 PRINT_REG(dczid_el0);
1263 /* ARM64TODO: Enable VFP before reading floating-point registers */
1267 PRINT_REG(id_aa64afr0_el1);
1268 PRINT_REG(id_aa64afr1_el1);
1269 PRINT_REG(id_aa64dfr0_el1);
1270 PRINT_REG(id_aa64dfr1_el1);
1271 PRINT_REG(id_aa64isar0_el1);
1272 PRINT_REG(id_aa64isar1_el1);
1273 PRINT_REG(id_aa64pfr0_el1);
1274 PRINT_REG(id_aa64pfr1_el1);
1275 PRINT_REG(id_afr0_el1);
1276 PRINT_REG(id_dfr0_el1);
1277 PRINT_REG(id_isar0_el1);
1278 PRINT_REG(id_isar1_el1);
1279 PRINT_REG(id_isar2_el1);
1280 PRINT_REG(id_isar3_el1);
1281 PRINT_REG(id_isar4_el1);
1282 PRINT_REG(id_isar5_el1);
1283 PRINT_REG(id_mmfr0_el1);
1284 PRINT_REG(id_mmfr1_el1);
1285 PRINT_REG(id_mmfr2_el1);
1286 PRINT_REG(id_mmfr3_el1);
1288 /* Missing from llvm */
1289 PRINT_REG(id_mmfr4_el1);
1291 PRINT_REG(id_pfr0_el1);
1292 PRINT_REG(id_pfr1_el1);
1294 PRINT_REG(mair_el1);
1295 PRINT_REG(midr_el1);
1296 PRINT_REG(mpidr_el1);
1297 PRINT_REG(mvfr0_el1);
1298 PRINT_REG(mvfr1_el1);
1299 PRINT_REG(mvfr2_el1);
1300 PRINT_REG(revidr_el1);
1301 PRINT_REG(sctlr_el1);
1304 PRINT_REG(spsr_el1);
1306 PRINT_REG(tpidr_el0);
1307 PRINT_REG(tpidr_el1);
1308 PRINT_REG(tpidrro_el0);
1309 PRINT_REG(ttbr0_el1);
1310 PRINT_REG(ttbr1_el1);
1311 PRINT_REG(vbar_el1);
1315 DB_SHOW_COMMAND(vtop, db_show_vtop)
1320 phys = arm64_address_translate_s1e1r(addr);
1321 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
1322 phys = arm64_address_translate_s1e1w(addr);
1323 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1324 phys = arm64_address_translate_s1e0r(addr);
1325 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
1326 phys = arm64_address_translate_s1e0w(addr);
1327 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1329 db_printf("show vtop <virt_addr>\n");