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>
54 #include <sys/ptrace.h>
55 #include <sys/reboot.h>
56 #include <sys/rwlock.h>
57 #include <sys/sched.h>
58 #include <sys/signalvar.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysent.h>
61 #include <sys/sysproto.h>
62 #include <sys/ucontext.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_page.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_pager.h>
73 #include <machine/armreg.h>
74 #include <machine/cpu.h>
75 #include <machine/debug_monitor.h>
76 #include <machine/kdb.h>
77 #include <machine/machdep.h>
78 #include <machine/metadata.h>
79 #include <machine/md_var.h>
80 #include <machine/pcb.h>
81 #include <machine/reg.h>
82 #include <machine/undefined.h>
83 #include <machine/vmparam.h>
85 #include <arm/include/physmem.h>
88 #include <machine/vfp.h>
92 #include <contrib/dev/acpica/include/acpi.h>
93 #include <machine/acpica_machdep.h>
97 #include <dev/fdt/fdt_common.h>
98 #include <dev/ofw/openfirm.h>
102 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
104 struct pcpu __pcpu[MAXCPU];
106 static struct trapframe proc0_tf;
111 struct kva_md_info kmi;
113 int64_t dcache_line_size; /* The minimum D cache line size */
114 int64_t icache_line_size; /* The minimum I cache line size */
115 int64_t idcache_line_size; /* The minimum cache line size */
116 int64_t dczva_line_size; /* The size of cache line the dc zva zeroes */
120 * Physical address of the EFI System Table. Stashed from the metadata hints
121 * passed into the kernel and used by the EFI code to call runtime services.
123 vm_paddr_t efi_systbl_phys;
125 /* pagezero_* implementations are provided in support.S */
126 void pagezero_simple(void *);
127 void pagezero_cache(void *);
129 /* pagezero_simple is default pagezero */
130 void (*pagezero)(void *p) = pagezero_simple;
135 uint64_t id_aa64mfr1;
137 id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
138 if (ID_AA64MMFR1_PAN_VAL(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE)
147 * The LLVM integrated assembler doesn't understand the PAN
148 * PSTATE field. Because of this we need to manually create
149 * the instruction in an asm block. This is equivalent to:
152 * This sets the PAN bit, stopping the kernel from accessing
153 * memory when userspace can also access it unless the kernel
154 * uses the userspace load/store instructions.
157 WRITE_SPECIALREG(sctlr_el1,
158 READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
159 __asm __volatile(".inst 0xd500409f | (0x1 << 8)");
164 cpu_startup(void *dummy)
169 install_cpu_errata();
171 vm_ksubmap_init(&kmi);
173 vm_pager_bufferinit();
176 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
179 cpu_idle_wakeup(int cpu)
186 fill_regs(struct thread *td, struct reg *regs)
188 struct trapframe *frame;
190 frame = td->td_frame;
191 regs->sp = frame->tf_sp;
192 regs->lr = frame->tf_lr;
193 regs->elr = frame->tf_elr;
194 regs->spsr = frame->tf_spsr;
196 memcpy(regs->x, frame->tf_x, sizeof(regs->x));
198 #ifdef COMPAT_FREEBSD32
200 * We may be called here for a 32bits process, if we're using a
201 * 64bits debugger. If so, put PC and SPSR where it expects it.
203 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
204 regs->x[15] = frame->tf_elr;
205 regs->x[16] = frame->tf_spsr;
212 set_regs(struct thread *td, struct reg *regs)
214 struct trapframe *frame;
216 frame = td->td_frame;
217 frame->tf_sp = regs->sp;
218 frame->tf_lr = regs->lr;
219 frame->tf_elr = regs->elr;
220 frame->tf_spsr &= ~PSR_FLAGS;
221 frame->tf_spsr |= regs->spsr & PSR_FLAGS;
223 memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
225 #ifdef COMPAT_FREEBSD32
226 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
228 * We may be called for a 32bits process if we're using
229 * a 64bits debugger. If so, get PC and SPSR from where
232 frame->tf_elr = regs->x[15];
233 frame->tf_spsr = regs->x[16] & PSR_FLAGS;
240 fill_fpregs(struct thread *td, struct fpreg *regs)
246 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
248 * If we have just been running VFP instructions we will
249 * need to save the state to memcpy it below.
252 vfp_save_state(td, pcb);
254 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
255 ("Called fill_fpregs while the kernel is using the VFP"));
256 memcpy(regs->fp_q, pcb->pcb_fpustate.vfp_regs,
258 regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
259 regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
262 memset(regs, 0, sizeof(*regs));
267 set_fpregs(struct thread *td, struct fpreg *regs)
273 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
274 ("Called set_fpregs while the kernel is using the VFP"));
275 memcpy(pcb->pcb_fpustate.vfp_regs, regs->fp_q, sizeof(regs->fp_q));
276 pcb->pcb_fpustate.vfp_fpcr = regs->fp_cr;
277 pcb->pcb_fpustate.vfp_fpsr = regs->fp_sr;
283 fill_dbregs(struct thread *td, struct dbreg *regs)
285 struct debug_monitor_state *monitor;
287 uint8_t debug_ver, nbkpts;
289 memset(regs, 0, sizeof(*regs));
291 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_DebugVer_SHIFT,
293 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_BRPs_SHIFT,
297 * The BRPs field contains the number of breakpoints - 1. Armv8-A
298 * allows the hardware to provide 2-16 breakpoints so this won't
299 * overflow an 8 bit value.
303 regs->db_info = debug_ver;
305 regs->db_info |= count;
307 monitor = &td->td_pcb->pcb_dbg_regs;
308 if ((monitor->dbg_flags & DBGMON_ENABLED) != 0) {
309 for (i = 0; i < count; i++) {
310 regs->db_regs[i].dbr_addr = monitor->dbg_bvr[i];
311 regs->db_regs[i].dbr_ctrl = monitor->dbg_bcr[i];
319 set_dbregs(struct thread *td, struct dbreg *regs)
321 struct debug_monitor_state *monitor;
325 monitor = &td->td_pcb->pcb_dbg_regs;
327 monitor->dbg_enable_count = 0;
328 for (i = 0; i < DBG_BRP_MAX; i++) {
329 /* TODO: Check these values */
330 monitor->dbg_bvr[i] = regs->db_regs[i].dbr_addr;
331 monitor->dbg_bcr[i] = regs->db_regs[i].dbr_ctrl;
332 if ((monitor->dbg_bcr[i] & 1) != 0)
333 monitor->dbg_enable_count++;
335 if (monitor->dbg_enable_count > 0)
336 monitor->dbg_flags |= DBGMON_ENABLED;
341 #ifdef COMPAT_FREEBSD32
343 fill_regs32(struct thread *td, struct reg32 *regs)
346 struct trapframe *tf;
349 for (i = 0; i < 13; i++)
350 regs->r[i] = tf->tf_x[i];
351 /* For arm32, SP is r13 and LR is r14 */
352 regs->r_sp = tf->tf_x[13];
353 regs->r_lr = tf->tf_x[14];
354 regs->r_pc = tf->tf_elr;
355 regs->r_cpsr = tf->tf_spsr;
361 set_regs32(struct thread *td, struct reg32 *regs)
364 struct trapframe *tf;
367 for (i = 0; i < 13; i++)
368 tf->tf_x[i] = regs->r[i];
369 /* For arm 32, SP is r13 an LR is r14 */
370 tf->tf_x[13] = regs->r_sp;
371 tf->tf_x[14] = regs->r_lr;
372 tf->tf_elr = regs->r_pc;
373 tf->tf_spsr = regs->r_cpsr;
380 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
383 printf("ARM64TODO: fill_fpregs32");
388 set_fpregs32(struct thread *td, struct fpreg32 *regs)
391 printf("ARM64TODO: set_fpregs32");
396 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
399 printf("ARM64TODO: fill_dbregs32");
404 set_dbregs32(struct thread *td, struct dbreg32 *regs)
407 printf("ARM64TODO: set_dbregs32");
413 ptrace_set_pc(struct thread *td, u_long addr)
416 td->td_frame->tf_elr = addr;
421 ptrace_single_step(struct thread *td)
424 td->td_frame->tf_spsr |= PSR_SS;
425 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
430 ptrace_clear_single_step(struct thread *td)
433 td->td_frame->tf_spsr &= ~PSR_SS;
434 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
439 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
441 struct trapframe *tf = td->td_frame;
443 memset(tf, 0, sizeof(struct trapframe));
446 tf->tf_sp = STACKALIGN(stack);
447 tf->tf_lr = imgp->entry_addr;
448 tf->tf_elr = imgp->entry_addr;
451 /* Sanity check these are the same size, they will be memcpy'd to and fro */
452 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
453 sizeof((struct gpregs *)0)->gp_x);
454 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
455 sizeof((struct reg *)0)->x);
458 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
460 struct trapframe *tf = td->td_frame;
462 if (clear_ret & GET_MC_CLEAR_RET) {
463 mcp->mc_gpregs.gp_x[0] = 0;
464 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
466 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
467 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
470 memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
471 sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
473 mcp->mc_gpregs.gp_sp = tf->tf_sp;
474 mcp->mc_gpregs.gp_lr = tf->tf_lr;
475 mcp->mc_gpregs.gp_elr = tf->tf_elr;
481 set_mcontext(struct thread *td, mcontext_t *mcp)
483 struct trapframe *tf = td->td_frame;
486 spsr = mcp->mc_gpregs.gp_spsr;
487 if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
488 (spsr & PSR_AARCH32) != 0 ||
489 (spsr & PSR_DAIF) != (td->td_frame->tf_spsr & PSR_DAIF))
492 memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
494 tf->tf_sp = mcp->mc_gpregs.gp_sp;
495 tf->tf_lr = mcp->mc_gpregs.gp_lr;
496 tf->tf_elr = mcp->mc_gpregs.gp_elr;
497 tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
503 get_fpcontext(struct thread *td, mcontext_t *mcp)
510 curpcb = curthread->td_pcb;
512 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
514 * If we have just been running VFP instructions we will
515 * need to save the state to memcpy it below.
517 vfp_save_state(td, curpcb);
519 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
520 ("Called get_fpcontext while the kernel is using the VFP"));
521 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
522 ("Non-userspace FPU flags set in get_fpcontext"));
523 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
524 sizeof(mcp->mc_fpregs));
525 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
526 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
527 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
528 mcp->mc_flags |= _MC_FP_VALID;
536 set_fpcontext(struct thread *td, mcontext_t *mcp)
543 if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
544 curpcb = curthread->td_pcb;
547 * Discard any vfp state for the current thread, we
548 * are about to override it.
552 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
553 ("Called set_fpcontext while the kernel is using the VFP"));
554 memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
555 sizeof(mcp->mc_fpregs));
556 curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
557 curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
558 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
572 if (!sched_runnable())
585 /* We should have shutdown by now, if not enter a low power sleep */
588 __asm __volatile("wfi");
593 * Flush the D-cache for non-DMA I/O so that the I-cache can
594 * be made coherent later.
597 cpu_flush_dcache(void *ptr, size_t len)
603 /* Get current clock frequency for the given CPU ID. */
605 cpu_est_clockrate(int cpu_id, uint64_t *rate)
609 pc = pcpu_find(cpu_id);
610 if (pc == NULL || rate == NULL)
613 if (pc->pc_clock == 0)
616 *rate = pc->pc_clock;
621 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
624 pcpu->pc_acpi_id = 0xffffffff;
634 if (td->td_md.md_spinlock_count == 0) {
635 daif = intr_disable();
636 td->td_md.md_spinlock_count = 1;
637 td->td_md.md_saved_daif = daif;
640 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) {
658 #ifndef _SYS_SYSPROTO_H_
659 struct sigreturn_args {
665 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
672 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
675 error = set_mcontext(td, &uc.uc_mcontext);
678 set_fpcontext(td, &uc.uc_mcontext);
680 /* Restore signal mask. */
681 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
683 return (EJUSTRETURN);
687 * Construct a PCB from a trapframe. This is called from kdb_trap() where
688 * we want to start a backtrace from the function that caused us to enter
689 * the debugger. We have the context in the trapframe, but base the trace
690 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
691 * enough for a backtrace.
694 makectx(struct trapframe *tf, struct pcb *pcb)
698 for (i = 0; i < PCB_LR; i++)
699 pcb->pcb_x[i] = tf->tf_x[i];
701 pcb->pcb_x[PCB_LR] = tf->tf_lr;
702 pcb->pcb_pc = tf->tf_elr;
703 pcb->pcb_sp = tf->tf_sp;
707 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
711 struct trapframe *tf;
712 struct sigframe *fp, frame;
714 struct sysentvec *sysent;
719 PROC_LOCK_ASSERT(p, MA_OWNED);
721 sig = ksi->ksi_signo;
723 mtx_assert(&psp->ps_mtx, MA_OWNED);
726 onstack = sigonstack(tf->tf_sp);
728 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
731 /* Allocate and validate space for the signal handler context. */
732 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
733 SIGISMEMBER(psp->ps_sigonstack, sig)) {
734 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
735 td->td_sigstk.ss_size);
736 #if defined(COMPAT_43)
737 td->td_sigstk.ss_flags |= SS_ONSTACK;
740 fp = (struct sigframe *)td->td_frame->tf_sp;
743 /* Make room, keeping the stack aligned */
745 fp = (struct sigframe *)STACKALIGN(fp);
747 /* Fill in the frame to copy out */
748 bzero(&frame, sizeof(frame));
749 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
750 get_fpcontext(td, &frame.sf_uc.uc_mcontext);
751 frame.sf_si = ksi->ksi_info;
752 frame.sf_uc.uc_sigmask = *mask;
753 frame.sf_uc.uc_stack = td->td_sigstk;
754 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
755 (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
756 mtx_unlock(&psp->ps_mtx);
757 PROC_UNLOCK(td->td_proc);
759 /* Copy the sigframe out to the user's stack. */
760 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
761 /* Process has trashed its stack. Kill it. */
762 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
768 tf->tf_x[1] = (register_t)&fp->sf_si;
769 tf->tf_x[2] = (register_t)&fp->sf_uc;
771 tf->tf_elr = (register_t)catcher;
772 tf->tf_sp = (register_t)fp;
773 sysent = p->p_sysent;
774 if (sysent->sv_sigcode_base != 0)
775 tf->tf_lr = (register_t)sysent->sv_sigcode_base;
777 tf->tf_lr = (register_t)(sysent->sv_psstrings -
778 *(sysent->sv_szsigcode));
780 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
784 mtx_lock(&psp->ps_mtx);
788 init_proc0(vm_offset_t kstack)
790 struct pcpu *pcpup = &__pcpu[0];
792 proc_linkup0(&proc0, &thread0);
793 thread0.td_kstack = kstack;
794 thread0.td_kstack_pages = KSTACK_PAGES;
795 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
796 thread0.td_kstack_pages * PAGE_SIZE) - 1;
797 thread0.td_pcb->pcb_fpflags = 0;
798 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
799 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
800 thread0.td_frame = &proc0_tf;
801 pcpup->pc_curpcb = thread0.td_pcb;
810 } EFI_MEMORY_DESCRIPTOR;
812 typedef void (*efi_map_entry_cb)(struct efi_md *);
815 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb)
817 struct efi_md *map, *p;
822 * Memory map data provided by UEFI via the GetMemoryMap
825 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
826 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
828 if (efihdr->descriptor_size == 0)
830 ndesc = efihdr->memory_size / efihdr->descriptor_size;
832 for (i = 0, p = map; i < ndesc; i++,
833 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
839 exclude_efi_map_entry(struct efi_md *p)
842 switch (p->md_type) {
843 case EFI_MD_TYPE_CODE:
844 case EFI_MD_TYPE_DATA:
845 case EFI_MD_TYPE_BS_CODE:
846 case EFI_MD_TYPE_BS_DATA:
847 case EFI_MD_TYPE_FREE:
849 * We're allowed to use any entry with these types.
853 arm_physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
859 exclude_efi_map_entries(struct efi_map_header *efihdr)
862 foreach_efi_map_entry(efihdr, exclude_efi_map_entry);
866 add_efi_map_entry(struct efi_md *p)
869 switch (p->md_type) {
870 case EFI_MD_TYPE_RT_DATA:
872 * Runtime data will be excluded after the DMAP
873 * region is created to stop it from being added
876 case EFI_MD_TYPE_CODE:
877 case EFI_MD_TYPE_DATA:
878 case EFI_MD_TYPE_BS_CODE:
879 case EFI_MD_TYPE_BS_DATA:
880 case EFI_MD_TYPE_FREE:
882 * We're allowed to use any entry with these types.
884 arm_physmem_hardware_region(p->md_phys,
885 p->md_pages * PAGE_SIZE);
891 add_efi_map_entries(struct efi_map_header *efihdr)
894 foreach_efi_map_entry(efihdr, add_efi_map_entry);
898 print_efi_map_entry(struct efi_md *p)
901 static const char *types[] = {
907 "RuntimeServicesCode",
908 "RuntimeServicesData",
909 "ConventionalMemory",
914 "MemoryMappedIOPortSpace",
919 if (p->md_type < nitems(types))
920 type = types[p->md_type];
923 printf("%23s %012lx %12p %08lx ", type, p->md_phys,
924 p->md_virt, p->md_pages);
925 if (p->md_attr & EFI_MD_ATTR_UC)
927 if (p->md_attr & EFI_MD_ATTR_WC)
929 if (p->md_attr & EFI_MD_ATTR_WT)
931 if (p->md_attr & EFI_MD_ATTR_WB)
933 if (p->md_attr & EFI_MD_ATTR_UCE)
935 if (p->md_attr & EFI_MD_ATTR_WP)
937 if (p->md_attr & EFI_MD_ATTR_RP)
939 if (p->md_attr & EFI_MD_ATTR_XP)
941 if (p->md_attr & EFI_MD_ATTR_NV)
943 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
944 printf("MORE_RELIABLE ");
945 if (p->md_attr & EFI_MD_ATTR_RO)
947 if (p->md_attr & EFI_MD_ATTR_RT)
953 print_efi_map_entries(struct efi_map_header *efihdr)
956 printf("%23s %12s %12s %8s %4s\n",
957 "Type", "Physical", "Virtual", "#Pages", "Attr");
958 foreach_efi_map_entry(efihdr, print_efi_map_entry);
963 try_load_dtb(caddr_t kmdp)
967 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
968 #if defined(FDT_DTB_STATIC)
970 * In case the device tree blob was not retrieved (from metadata) try
971 * to use the statically embedded one.
974 dtbp = (vm_offset_t)&fdt_static_dtb;
977 if (dtbp == (vm_offset_t)NULL) {
978 printf("ERROR loading DTB\n");
982 if (OF_install(OFW_FDT, 0) == FALSE)
983 panic("Cannot install FDT");
985 if (OF_init((void *)dtbp) != 0)
986 panic("OF_init failed with the found device tree");
988 parse_fdt_bootargs();
995 bool has_acpi, has_fdt;
998 has_acpi = has_fdt = false;
1001 has_fdt = (OF_peer(0) != 0);
1004 has_acpi = (acpi_find_table(ACPI_SIG_SPCR) != 0);
1007 env = kern_getenv("kern.cfg.order");
1010 while (order != NULL) {
1012 strncmp(order, "acpi", 4) == 0 &&
1013 (order[4] == ',' || order[4] == '\0')) {
1014 arm64_bus_method = ARM64_BUS_ACPI;
1018 strncmp(order, "fdt", 3) == 0 &&
1019 (order[3] == ',' || order[3] == '\0')) {
1020 arm64_bus_method = ARM64_BUS_FDT;
1023 order = strchr(order, ',');
1027 /* If we set the bus method it is valid */
1028 if (arm64_bus_method != ARM64_BUS_NONE)
1031 /* If no order or an invalid order was set use the default */
1032 if (arm64_bus_method == ARM64_BUS_NONE) {
1034 arm64_bus_method = ARM64_BUS_FDT;
1036 arm64_bus_method = ARM64_BUS_ACPI;
1040 * If no option was set the default is valid, otherwise we are
1041 * setting one to get cninit() working, then calling panic to tell
1042 * the user about the invalid bus setup.
1044 return (env == NULL);
1050 int dcache_line_shift, icache_line_shift, dczva_line_shift;
1054 ctr_el0 = READ_SPECIALREG(ctr_el0);
1056 /* Read the log2 words in each D cache line */
1057 dcache_line_shift = CTR_DLINE_SIZE(ctr_el0);
1058 /* Get the D cache line size */
1059 dcache_line_size = sizeof(int) << dcache_line_shift;
1061 /* And the same for the I cache */
1062 icache_line_shift = CTR_ILINE_SIZE(ctr_el0);
1063 icache_line_size = sizeof(int) << icache_line_shift;
1065 idcache_line_size = MIN(dcache_line_size, icache_line_size);
1067 dczid_el0 = READ_SPECIALREG(dczid_el0);
1069 /* Check if dc zva is not prohibited */
1070 if (dczid_el0 & DCZID_DZP)
1071 dczva_line_size = 0;
1073 /* Same as with above calculations */
1074 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
1075 dczva_line_size = sizeof(int) << dczva_line_shift;
1077 /* Change pagezero function */
1078 pagezero = pagezero_cache;
1083 initarm(struct arm64_bootparams *abp)
1085 struct efi_fb *efifb;
1086 struct efi_map_header *efihdr;
1090 struct mem_region mem_regions[FDT_MEM_REGIONS];
1093 vm_offset_t lastaddr;
1097 /* Parse loader or FDT boot parametes. Determine last used address. */
1098 lastaddr = parse_boot_param(abp);
1100 /* Find the kernel address */
1101 kmdp = preload_search_by_type("elf kernel");
1103 kmdp = preload_search_by_type("elf64 kernel");
1105 link_elf_ireloc(kmdp);
1108 efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
1110 /* Load the physical memory ranges */
1111 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1112 MODINFO_METADATA | MODINFOMD_EFI_MAP);
1114 add_efi_map_entries(efihdr);
1117 /* Grab physical memory regions information from device tree. */
1118 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
1120 panic("Cannot get physical memory regions");
1121 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1123 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
1124 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1125 EXFLAG_NODUMP | EXFLAG_NOALLOC);
1128 /* Exclude the EFI framebuffer from our view of physical memory. */
1129 efifb = (struct efi_fb *)preload_search_info(kmdp,
1130 MODINFO_METADATA | MODINFOMD_EFI_FB);
1132 arm_physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
1135 /* Set the pcpu data, this is needed by pmap_bootstrap */
1137 pcpu_init(pcpup, 0, sizeof(struct pcpu));
1140 * Set the pcpu pointer with a backup in tpidr_el1 to be
1141 * loaded when entering the kernel from userland.
1145 "msr tpidr_el1, %0" :: "r"(pcpup));
1147 PCPU_SET(curthread, &thread0);
1149 /* Do basic tuning, hz etc */
1155 /* Bootstrap enough of pmap to enter the kernel proper */
1156 pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
1157 KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
1158 /* Exclude entries neexed in teh DMAP region, but not phys_avail */
1160 exclude_efi_map_entries(efihdr);
1161 arm_physmem_init_kernel_globals();
1163 devmap_bootstrap(0, NULL);
1165 valid = bus_probe();
1170 panic("Invalid bus configuration: %s",
1171 kern_getenv("kern.cfg.order"));
1173 init_proc0(abp->kern_stack);
1174 msgbufinit(msgbufp, msgbufsize);
1176 init_param2(physmem);
1184 env = kern_getenv("kernelname");
1186 strlcpy(kernelname, env, sizeof(kernelname));
1188 if (boothowto & RB_VERBOSE) {
1189 print_efi_map_entries(efihdr);
1190 arm_physmem_print_tables();
1201 WRITE_SPECIALREG(oslar_el1, 0);
1203 /* This permits DDB to use debug registers for watchpoints. */
1206 /* TODO: Eventually will need to initialize debug registers here. */
1210 #include <ddb/ddb.h>
1212 DB_SHOW_COMMAND(specialregs, db_show_spregs)
1214 #define PRINT_REG(reg) \
1215 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
1217 PRINT_REG(actlr_el1);
1218 PRINT_REG(afsr0_el1);
1219 PRINT_REG(afsr1_el1);
1220 PRINT_REG(aidr_el1);
1221 PRINT_REG(amair_el1);
1222 PRINT_REG(ccsidr_el1);
1223 PRINT_REG(clidr_el1);
1224 PRINT_REG(contextidr_el1);
1225 PRINT_REG(cpacr_el1);
1226 PRINT_REG(csselr_el1);
1228 PRINT_REG(currentel);
1230 PRINT_REG(dczid_el0);
1235 /* ARM64TODO: Enable VFP before reading floating-point registers */
1239 PRINT_REG(id_aa64afr0_el1);
1240 PRINT_REG(id_aa64afr1_el1);
1241 PRINT_REG(id_aa64dfr0_el1);
1242 PRINT_REG(id_aa64dfr1_el1);
1243 PRINT_REG(id_aa64isar0_el1);
1244 PRINT_REG(id_aa64isar1_el1);
1245 PRINT_REG(id_aa64pfr0_el1);
1246 PRINT_REG(id_aa64pfr1_el1);
1247 PRINT_REG(id_afr0_el1);
1248 PRINT_REG(id_dfr0_el1);
1249 PRINT_REG(id_isar0_el1);
1250 PRINT_REG(id_isar1_el1);
1251 PRINT_REG(id_isar2_el1);
1252 PRINT_REG(id_isar3_el1);
1253 PRINT_REG(id_isar4_el1);
1254 PRINT_REG(id_isar5_el1);
1255 PRINT_REG(id_mmfr0_el1);
1256 PRINT_REG(id_mmfr1_el1);
1257 PRINT_REG(id_mmfr2_el1);
1258 PRINT_REG(id_mmfr3_el1);
1260 /* Missing from llvm */
1261 PRINT_REG(id_mmfr4_el1);
1263 PRINT_REG(id_pfr0_el1);
1264 PRINT_REG(id_pfr1_el1);
1266 PRINT_REG(mair_el1);
1267 PRINT_REG(midr_el1);
1268 PRINT_REG(mpidr_el1);
1269 PRINT_REG(mvfr0_el1);
1270 PRINT_REG(mvfr1_el1);
1271 PRINT_REG(mvfr2_el1);
1272 PRINT_REG(revidr_el1);
1273 PRINT_REG(sctlr_el1);
1276 PRINT_REG(spsr_el1);
1278 PRINT_REG(tpidr_el0);
1279 PRINT_REG(tpidr_el1);
1280 PRINT_REG(tpidrro_el0);
1281 PRINT_REG(ttbr0_el1);
1282 PRINT_REG(ttbr1_el1);
1283 PRINT_REG(vbar_el1);
1287 DB_SHOW_COMMAND(vtop, db_show_vtop)
1292 phys = arm64_address_translate_s1e1r(addr);
1293 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
1294 phys = arm64_address_translate_s1e1w(addr);
1295 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1296 phys = arm64_address_translate_s1e0r(addr);
1297 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
1298 phys = arm64_address_translate_s1e0w(addr);
1299 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1301 db_printf("show vtop <virt_addr>\n");