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>
83 #include <arm/include/physmem.h>
86 #include <machine/vfp.h>
90 #include <contrib/dev/acpica/include/acpi.h>
91 #include <machine/acpica_machdep.h>
95 #include <dev/fdt/fdt_common.h>
96 #include <dev/ofw/openfirm.h>
100 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
102 struct pcpu __pcpu[MAXCPU];
104 static struct trapframe proc0_tf;
109 struct kva_md_info kmi;
111 int64_t dcache_line_size; /* The minimum D cache line size */
112 int64_t icache_line_size; /* The minimum I cache line size */
113 int64_t idcache_line_size; /* The minimum cache line size */
114 int64_t dczva_line_size; /* The size of cache line the dc zva zeroes */
118 * Physical address of the EFI System Table. Stashed from the metadata hints
119 * passed into the kernel and used by the EFI code to call runtime services.
121 vm_paddr_t efi_systbl_phys;
123 /* pagezero_* implementations are provided in support.S */
124 void pagezero_simple(void *);
125 void pagezero_cache(void *);
127 /* pagezero_simple is default pagezero */
128 void (*pagezero)(void *p) = pagezero_simple;
133 uint64_t id_aa64mfr1;
135 id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
136 if (ID_AA64MMFR1_PAN(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE)
145 * The LLVM integrated assembler doesn't understand the PAN
146 * PSTATE field. Because of this we need to manually create
147 * the instruction in an asm block. This is equivalent to:
150 * This sets the PAN bit, stopping the kernel from accessing
151 * memory when userspace can also access it unless the kernel
152 * uses the userspace load/store instructions.
155 WRITE_SPECIALREG(sctlr_el1,
156 READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
157 __asm __volatile(".inst 0xd500409f | (0x1 << 8)");
162 cpu_startup(void *dummy)
167 install_cpu_errata();
169 vm_ksubmap_init(&kmi);
171 vm_pager_bufferinit();
174 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
177 cpu_idle_wakeup(int cpu)
184 fill_regs(struct thread *td, struct reg *regs)
186 struct trapframe *frame;
188 frame = td->td_frame;
189 regs->sp = frame->tf_sp;
190 regs->lr = frame->tf_lr;
191 regs->elr = frame->tf_elr;
192 regs->spsr = frame->tf_spsr;
194 memcpy(regs->x, frame->tf_x, sizeof(regs->x));
200 set_regs(struct thread *td, struct reg *regs)
202 struct trapframe *frame;
204 frame = td->td_frame;
205 frame->tf_sp = regs->sp;
206 frame->tf_lr = regs->lr;
207 frame->tf_elr = regs->elr;
208 frame->tf_spsr &= ~PSR_FLAGS;
209 frame->tf_spsr |= regs->spsr & PSR_FLAGS;
211 memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
217 fill_fpregs(struct thread *td, struct fpreg *regs)
223 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
225 * If we have just been running VFP instructions we will
226 * need to save the state to memcpy it below.
229 vfp_save_state(td, pcb);
231 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
232 ("Called fill_fpregs while the kernel is using the VFP"));
233 memcpy(regs->fp_q, pcb->pcb_fpustate.vfp_regs,
235 regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
236 regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
239 memset(regs->fp_q, 0, sizeof(regs->fp_q));
244 set_fpregs(struct thread *td, struct fpreg *regs)
250 KASSERT(pcb->pcb_fpusaved == &pcb->pcb_fpustate,
251 ("Called set_fpregs while the kernel is using the VFP"));
252 memcpy(pcb->pcb_fpustate.vfp_regs, regs->fp_q, sizeof(regs->fp_q));
253 pcb->pcb_fpustate.vfp_fpcr = regs->fp_cr;
254 pcb->pcb_fpustate.vfp_fpsr = regs->fp_sr;
260 fill_dbregs(struct thread *td, struct dbreg *regs)
263 printf("ARM64TODO: fill_dbregs");
268 set_dbregs(struct thread *td, struct dbreg *regs)
271 printf("ARM64TODO: set_dbregs");
275 #ifdef COMPAT_FREEBSD32
277 fill_regs32(struct thread *td, struct reg32 *regs)
280 printf("ARM64TODO: fill_regs32");
285 set_regs32(struct thread *td, struct reg32 *regs)
288 printf("ARM64TODO: set_regs32");
293 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
296 printf("ARM64TODO: fill_fpregs32");
301 set_fpregs32(struct thread *td, struct fpreg32 *regs)
304 printf("ARM64TODO: set_fpregs32");
309 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
312 printf("ARM64TODO: fill_dbregs32");
317 set_dbregs32(struct thread *td, struct dbreg32 *regs)
320 printf("ARM64TODO: set_dbregs32");
326 ptrace_set_pc(struct thread *td, u_long addr)
329 printf("ARM64TODO: ptrace_set_pc");
334 ptrace_single_step(struct thread *td)
337 td->td_frame->tf_spsr |= PSR_SS;
338 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
343 ptrace_clear_single_step(struct thread *td)
346 td->td_frame->tf_spsr &= ~PSR_SS;
347 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
352 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
354 struct trapframe *tf = td->td_frame;
356 memset(tf, 0, sizeof(struct trapframe));
359 tf->tf_sp = STACKALIGN(stack);
360 tf->tf_lr = imgp->entry_addr;
361 tf->tf_elr = imgp->entry_addr;
364 /* Sanity check these are the same size, they will be memcpy'd to and fro */
365 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
366 sizeof((struct gpregs *)0)->gp_x);
367 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
368 sizeof((struct reg *)0)->x);
371 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
373 struct trapframe *tf = td->td_frame;
375 if (clear_ret & GET_MC_CLEAR_RET) {
376 mcp->mc_gpregs.gp_x[0] = 0;
377 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
379 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
380 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
383 memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
384 sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
386 mcp->mc_gpregs.gp_sp = tf->tf_sp;
387 mcp->mc_gpregs.gp_lr = tf->tf_lr;
388 mcp->mc_gpregs.gp_elr = tf->tf_elr;
394 set_mcontext(struct thread *td, mcontext_t *mcp)
396 struct trapframe *tf = td->td_frame;
399 spsr = mcp->mc_gpregs.gp_spsr;
400 if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
401 (spsr & (PSR_AARCH32 | PSR_F | PSR_I | PSR_A | PSR_D)) != 0)
404 memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
406 tf->tf_sp = mcp->mc_gpregs.gp_sp;
407 tf->tf_lr = mcp->mc_gpregs.gp_lr;
408 tf->tf_elr = mcp->mc_gpregs.gp_elr;
409 tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
415 get_fpcontext(struct thread *td, mcontext_t *mcp)
422 curpcb = curthread->td_pcb;
424 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
426 * If we have just been running VFP instructions we will
427 * need to save the state to memcpy it below.
429 vfp_save_state(td, curpcb);
431 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
432 ("Called get_fpcontext while the kernel is using the VFP"));
433 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
434 ("Non-userspace FPU flags set in get_fpcontext"));
435 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
436 sizeof(mcp->mc_fpregs));
437 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
438 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
439 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
440 mcp->mc_flags |= _MC_FP_VALID;
448 set_fpcontext(struct thread *td, mcontext_t *mcp)
455 if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
456 curpcb = curthread->td_pcb;
459 * Discard any vfp state for the current thread, we
460 * are about to override it.
464 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
465 ("Called set_fpcontext while the kernel is using the VFP"));
466 memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
467 sizeof(mcp->mc_fpregs));
468 curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
469 curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
470 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
484 if (!sched_runnable())
497 /* We should have shutdown by now, if not enter a low power sleep */
500 __asm __volatile("wfi");
505 * Flush the D-cache for non-DMA I/O so that the I-cache can
506 * be made coherent later.
509 cpu_flush_dcache(void *ptr, size_t len)
515 /* Get current clock frequency for the given CPU ID. */
517 cpu_est_clockrate(int cpu_id, uint64_t *rate)
521 pc = pcpu_find(cpu_id);
522 if (pc == NULL || rate == NULL)
525 if (pc->pc_clock == 0)
528 *rate = pc->pc_clock;
533 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
536 pcpu->pc_acpi_id = 0xffffffff;
546 if (td->td_md.md_spinlock_count == 0) {
547 daif = intr_disable();
548 td->td_md.md_spinlock_count = 1;
549 td->td_md.md_saved_daif = daif;
551 td->td_md.md_spinlock_count++;
563 daif = td->td_md.md_saved_daif;
564 td->td_md.md_spinlock_count--;
565 if (td->td_md.md_spinlock_count == 0)
569 #ifndef _SYS_SYSPROTO_H_
570 struct sigreturn_args {
576 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
583 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
586 error = set_mcontext(td, &uc.uc_mcontext);
589 set_fpcontext(td, &uc.uc_mcontext);
591 /* Restore signal mask. */
592 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
594 return (EJUSTRETURN);
598 * Construct a PCB from a trapframe. This is called from kdb_trap() where
599 * we want to start a backtrace from the function that caused us to enter
600 * the debugger. We have the context in the trapframe, but base the trace
601 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
602 * enough for a backtrace.
605 makectx(struct trapframe *tf, struct pcb *pcb)
609 for (i = 0; i < PCB_LR; i++)
610 pcb->pcb_x[i] = tf->tf_x[i];
612 pcb->pcb_x[PCB_LR] = tf->tf_lr;
613 pcb->pcb_pc = tf->tf_elr;
614 pcb->pcb_sp = tf->tf_sp;
618 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
622 struct trapframe *tf;
623 struct sigframe *fp, frame;
625 struct sysentvec *sysent;
630 PROC_LOCK_ASSERT(p, MA_OWNED);
632 sig = ksi->ksi_signo;
634 mtx_assert(&psp->ps_mtx, MA_OWNED);
637 onstack = sigonstack(tf->tf_sp);
639 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
642 /* Allocate and validate space for the signal handler context. */
643 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
644 SIGISMEMBER(psp->ps_sigonstack, sig)) {
645 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
646 td->td_sigstk.ss_size);
647 #if defined(COMPAT_43)
648 td->td_sigstk.ss_flags |= SS_ONSTACK;
651 fp = (struct sigframe *)td->td_frame->tf_sp;
654 /* Make room, keeping the stack aligned */
656 fp = (struct sigframe *)STACKALIGN(fp);
658 /* Fill in the frame to copy out */
659 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
660 get_fpcontext(td, &frame.sf_uc.uc_mcontext);
661 frame.sf_si = ksi->ksi_info;
662 frame.sf_uc.uc_sigmask = *mask;
663 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
664 ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
665 frame.sf_uc.uc_stack = td->td_sigstk;
666 mtx_unlock(&psp->ps_mtx);
667 PROC_UNLOCK(td->td_proc);
669 /* Copy the sigframe out to the user's stack. */
670 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
671 /* Process has trashed its stack. Kill it. */
672 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
678 tf->tf_x[1] = (register_t)&fp->sf_si;
679 tf->tf_x[2] = (register_t)&fp->sf_uc;
681 tf->tf_elr = (register_t)catcher;
682 tf->tf_sp = (register_t)fp;
683 sysent = p->p_sysent;
684 if (sysent->sv_sigcode_base != 0)
685 tf->tf_lr = (register_t)sysent->sv_sigcode_base;
687 tf->tf_lr = (register_t)(sysent->sv_psstrings -
688 *(sysent->sv_szsigcode));
690 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
694 mtx_lock(&psp->ps_mtx);
698 init_proc0(vm_offset_t kstack)
700 struct pcpu *pcpup = &__pcpu[0];
702 proc_linkup0(&proc0, &thread0);
703 thread0.td_kstack = kstack;
704 thread0.td_pcb = (struct pcb *)(thread0.td_kstack) - 1;
705 thread0.td_pcb->pcb_fpflags = 0;
706 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
707 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
708 thread0.td_frame = &proc0_tf;
709 pcpup->pc_curpcb = thread0.td_pcb;
711 /* Set the base address of translation table 0. */
712 thread0.td_proc->p_md.md_l0addr = READ_SPECIALREG(ttbr0_el1);
721 } EFI_MEMORY_DESCRIPTOR;
723 typedef void (*efi_map_entry_cb)(struct efi_md *);
726 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb)
728 struct efi_md *map, *p;
733 * Memory map data provided by UEFI via the GetMemoryMap
736 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
737 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
739 if (efihdr->descriptor_size == 0)
741 ndesc = efihdr->memory_size / efihdr->descriptor_size;
743 for (i = 0, p = map; i < ndesc; i++,
744 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
750 exclude_efi_map_entry(struct efi_md *p)
753 switch (p->md_type) {
754 case EFI_MD_TYPE_CODE:
755 case EFI_MD_TYPE_DATA:
756 case EFI_MD_TYPE_BS_CODE:
757 case EFI_MD_TYPE_BS_DATA:
758 case EFI_MD_TYPE_FREE:
760 * We're allowed to use any entry with these types.
764 arm_physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
770 exclude_efi_map_entries(struct efi_map_header *efihdr)
773 foreach_efi_map_entry(efihdr, exclude_efi_map_entry);
777 add_efi_map_entry(struct efi_md *p)
780 switch (p->md_type) {
781 case EFI_MD_TYPE_RT_DATA:
783 * Runtime data will be excluded after the DMAP
784 * region is created to stop it from being added
787 case EFI_MD_TYPE_CODE:
788 case EFI_MD_TYPE_DATA:
789 case EFI_MD_TYPE_BS_CODE:
790 case EFI_MD_TYPE_BS_DATA:
791 case EFI_MD_TYPE_FREE:
793 * We're allowed to use any entry with these types.
795 arm_physmem_hardware_region(p->md_phys,
796 p->md_pages * PAGE_SIZE);
802 add_efi_map_entries(struct efi_map_header *efihdr)
805 foreach_efi_map_entry(efihdr, add_efi_map_entry);
809 print_efi_map_entry(struct efi_md *p)
812 static const char *types[] = {
818 "RuntimeServicesCode",
819 "RuntimeServicesData",
820 "ConventionalMemory",
825 "MemoryMappedIOPortSpace",
830 if (p->md_type < nitems(types))
831 type = types[p->md_type];
834 printf("%23s %012lx %12p %08lx ", type, p->md_phys,
835 p->md_virt, p->md_pages);
836 if (p->md_attr & EFI_MD_ATTR_UC)
838 if (p->md_attr & EFI_MD_ATTR_WC)
840 if (p->md_attr & EFI_MD_ATTR_WT)
842 if (p->md_attr & EFI_MD_ATTR_WB)
844 if (p->md_attr & EFI_MD_ATTR_UCE)
846 if (p->md_attr & EFI_MD_ATTR_WP)
848 if (p->md_attr & EFI_MD_ATTR_RP)
850 if (p->md_attr & EFI_MD_ATTR_XP)
852 if (p->md_attr & EFI_MD_ATTR_NV)
854 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
855 printf("MORE_RELIABLE ");
856 if (p->md_attr & EFI_MD_ATTR_RO)
858 if (p->md_attr & EFI_MD_ATTR_RT)
864 print_efi_map_entries(struct efi_map_header *efihdr)
867 printf("%23s %12s %12s %8s %4s\n",
868 "Type", "Physical", "Virtual", "#Pages", "Attr");
869 foreach_efi_map_entry(efihdr, print_efi_map_entry);
874 try_load_dtb(caddr_t kmdp)
878 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
879 if (dtbp == (vm_offset_t)NULL) {
880 printf("ERROR loading DTB\n");
884 if (OF_install(OFW_FDT, 0) == FALSE)
885 panic("Cannot install FDT");
887 if (OF_init((void *)dtbp) != 0)
888 panic("OF_init failed with the found device tree");
895 bool has_acpi, has_fdt;
898 has_acpi = has_fdt = false;
901 has_fdt = (OF_peer(0) != 0);
904 has_acpi = (acpi_find_table(ACPI_SIG_SPCR) != 0);
907 env = kern_getenv("kern.cfg.order");
910 while (order != NULL) {
912 strncmp(order, "acpi", 4) == 0 &&
913 (order[4] == ',' || order[4] == '\0')) {
914 arm64_bus_method = ARM64_BUS_ACPI;
918 strncmp(order, "fdt", 3) == 0 &&
919 (order[3] == ',' || order[3] == '\0')) {
920 arm64_bus_method = ARM64_BUS_FDT;
923 order = strchr(order, ',');
927 /* If we set the bus method it is valid */
928 if (arm64_bus_method != ARM64_BUS_NONE)
931 /* If no order or an invalid order was set use the default */
932 if (arm64_bus_method == ARM64_BUS_NONE) {
934 arm64_bus_method = ARM64_BUS_FDT;
936 arm64_bus_method = ARM64_BUS_ACPI;
940 * If no option was set the default is valid, otherwise we are
941 * setting one to get cninit() working, then calling panic to tell
942 * the user about the invalid bus setup.
944 return (env == NULL);
950 int dcache_line_shift, icache_line_shift, dczva_line_shift;
954 ctr_el0 = READ_SPECIALREG(ctr_el0);
956 /* Read the log2 words in each D cache line */
957 dcache_line_shift = CTR_DLINE_SIZE(ctr_el0);
958 /* Get the D cache line size */
959 dcache_line_size = sizeof(int) << dcache_line_shift;
961 /* And the same for the I cache */
962 icache_line_shift = CTR_ILINE_SIZE(ctr_el0);
963 icache_line_size = sizeof(int) << icache_line_shift;
965 idcache_line_size = MIN(dcache_line_size, icache_line_size);
967 dczid_el0 = READ_SPECIALREG(dczid_el0);
969 /* Check if dc zva is not prohibited */
970 if (dczid_el0 & DCZID_DZP)
973 /* Same as with above calculations */
974 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
975 dczva_line_size = sizeof(int) << dczva_line_shift;
977 /* Change pagezero function */
978 pagezero = pagezero_cache;
983 initarm(struct arm64_bootparams *abp)
985 struct efi_map_header *efihdr;
989 struct mem_region mem_regions[FDT_MEM_REGIONS];
992 vm_offset_t lastaddr;
996 /* Set the module data location */
997 preload_metadata = (caddr_t)(uintptr_t)(abp->modulep);
999 /* Find the kernel address */
1000 kmdp = preload_search_by_type("elf kernel");
1002 kmdp = preload_search_by_type("elf64 kernel");
1004 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
1005 init_static_kenv(MD_FETCH(kmdp, MODINFOMD_ENVP, char *), 0);
1011 efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
1013 /* Find the address to start allocating from */
1014 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
1016 /* Load the physical memory ranges */
1017 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1018 MODINFO_METADATA | MODINFOMD_EFI_MAP);
1020 add_efi_map_entries(efihdr);
1023 /* Grab physical memory regions information from device tree. */
1024 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
1026 panic("Cannot get physical memory regions");
1027 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1029 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
1030 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1031 EXFLAG_NODUMP | EXFLAG_NOALLOC);
1034 /* Set the pcpu data, this is needed by pmap_bootstrap */
1036 pcpu_init(pcpup, 0, sizeof(struct pcpu));
1039 * Set the pcpu pointer with a backup in tpidr_el1 to be
1040 * loaded when entering the kernel from userland.
1044 "msr tpidr_el1, %0" :: "r"(pcpup));
1046 PCPU_SET(curthread, &thread0);
1048 /* Do basic tuning, hz etc */
1054 /* Bootstrap enough of pmap to enter the kernel proper */
1055 pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
1056 KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
1057 /* Exclude entries neexed in teh DMAP region, but not phys_avail */
1059 exclude_efi_map_entries(efihdr);
1060 arm_physmem_init_kernel_globals();
1062 devmap_bootstrap(0, NULL);
1064 valid = bus_probe();
1069 panic("Invalid bus configuration: %s",
1070 kern_getenv("kern.cfg.order"));
1072 init_proc0(abp->kern_stack);
1073 msgbufinit(msgbufp, msgbufsize);
1075 init_param2(physmem);
1081 env = kern_getenv("kernelname");
1083 strlcpy(kernelname, env, sizeof(kernelname));
1085 if (boothowto & RB_VERBOSE) {
1086 print_efi_map_entries(efihdr);
1087 arm_physmem_print_tables();
1098 WRITE_SPECIALREG(OSLAR_EL1, 0);
1100 /* This permits DDB to use debug registers for watchpoints. */
1103 /* TODO: Eventually will need to initialize debug registers here. */
1107 #include <ddb/ddb.h>
1109 DB_SHOW_COMMAND(specialregs, db_show_spregs)
1111 #define PRINT_REG(reg) \
1112 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
1114 PRINT_REG(actlr_el1);
1115 PRINT_REG(afsr0_el1);
1116 PRINT_REG(afsr1_el1);
1117 PRINT_REG(aidr_el1);
1118 PRINT_REG(amair_el1);
1119 PRINT_REG(ccsidr_el1);
1120 PRINT_REG(clidr_el1);
1121 PRINT_REG(contextidr_el1);
1122 PRINT_REG(cpacr_el1);
1123 PRINT_REG(csselr_el1);
1125 PRINT_REG(currentel);
1127 PRINT_REG(dczid_el0);
1132 /* ARM64TODO: Enable VFP before reading floating-point registers */
1136 PRINT_REG(id_aa64afr0_el1);
1137 PRINT_REG(id_aa64afr1_el1);
1138 PRINT_REG(id_aa64dfr0_el1);
1139 PRINT_REG(id_aa64dfr1_el1);
1140 PRINT_REG(id_aa64isar0_el1);
1141 PRINT_REG(id_aa64isar1_el1);
1142 PRINT_REG(id_aa64pfr0_el1);
1143 PRINT_REG(id_aa64pfr1_el1);
1144 PRINT_REG(id_afr0_el1);
1145 PRINT_REG(id_dfr0_el1);
1146 PRINT_REG(id_isar0_el1);
1147 PRINT_REG(id_isar1_el1);
1148 PRINT_REG(id_isar2_el1);
1149 PRINT_REG(id_isar3_el1);
1150 PRINT_REG(id_isar4_el1);
1151 PRINT_REG(id_isar5_el1);
1152 PRINT_REG(id_mmfr0_el1);
1153 PRINT_REG(id_mmfr1_el1);
1154 PRINT_REG(id_mmfr2_el1);
1155 PRINT_REG(id_mmfr3_el1);
1157 /* Missing from llvm */
1158 PRINT_REG(id_mmfr4_el1);
1160 PRINT_REG(id_pfr0_el1);
1161 PRINT_REG(id_pfr1_el1);
1163 PRINT_REG(mair_el1);
1164 PRINT_REG(midr_el1);
1165 PRINT_REG(mpidr_el1);
1166 PRINT_REG(mvfr0_el1);
1167 PRINT_REG(mvfr1_el1);
1168 PRINT_REG(mvfr2_el1);
1169 PRINT_REG(revidr_el1);
1170 PRINT_REG(sctlr_el1);
1173 PRINT_REG(spsr_el1);
1175 PRINT_REG(tpidr_el0);
1176 PRINT_REG(tpidr_el1);
1177 PRINT_REG(tpidrro_el0);
1178 PRINT_REG(ttbr0_el1);
1179 PRINT_REG(ttbr1_el1);
1180 PRINT_REG(vbar_el1);
1184 DB_SHOW_COMMAND(vtop, db_show_vtop)
1189 phys = arm64_address_translate_s1e1r(addr);
1190 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
1191 phys = arm64_address_translate_s1e1w(addr);
1192 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1193 phys = arm64_address_translate_s1e0r(addr);
1194 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
1195 phys = arm64_address_translate_s1e0w(addr);
1196 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1198 db_printf("show vtop <virt_addr>\n");