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, nwtpts;
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,
333 extract_user_id_field(ID_AA64DFR0_EL1, ID_AA64DFR0_WRPs_SHIFT,
337 * The BRPs field contains the number of breakpoints - 1. Armv8-A
338 * allows the hardware to provide 2-16 breakpoints so this won't
339 * overflow an 8 bit value. The same applies to the WRPs field.
344 regs->db_debug_ver = debug_ver;
345 regs->db_nbkpts = nbkpts;
346 regs->db_nwtpts = nwtpts;
348 monitor = &td->td_pcb->pcb_dbg_regs;
349 if ((monitor->dbg_flags & DBGMON_ENABLED) != 0) {
350 for (i = 0; i < nbkpts; i++) {
351 regs->db_breakregs[i].dbr_addr = monitor->dbg_bvr[i];
352 regs->db_breakregs[i].dbr_ctrl = monitor->dbg_bcr[i];
354 for (i = 0; i < nwtpts; i++) {
355 regs->db_watchregs[i].dbw_addr = monitor->dbg_wvr[i];
356 regs->db_watchregs[i].dbw_ctrl = monitor->dbg_wcr[i];
364 set_dbregs(struct thread *td, struct dbreg *regs)
366 struct debug_monitor_state *monitor;
372 monitor = &td->td_pcb->pcb_dbg_regs;
374 monitor->dbg_enable_count = 0;
376 for (i = 0; i < DBG_BRP_MAX; i++) {
377 addr = regs->db_breakregs[i].dbr_addr;
378 ctrl = regs->db_breakregs[i].dbr_ctrl;
380 /* Don't let the user set a breakpoint on a kernel address. */
381 if (addr >= VM_MAXUSER_ADDRESS)
385 * The lowest 2 bits are ignored, so record the effective
388 addr = rounddown2(addr, 4);
391 * Some control fields are ignored, and other bits reserved.
392 * Only unlinked, address-matching breakpoints are supported.
394 * XXX: fields that appear unvalidated, such as BAS, have
395 * constrained undefined behaviour. If the user mis-programs
396 * these, there is no risk to the system.
398 ctrl &= DBG_BCR_EN | DBG_BCR_PMC | DBG_BCR_BAS;
399 if ((ctrl & DBG_BCR_EN) != 0) {
400 /* Only target EL0. */
401 if ((ctrl & DBG_BCR_PMC) != DBG_BCR_PMC_EL0)
404 monitor->dbg_enable_count++;
407 monitor->dbg_bvr[i] = addr;
408 monitor->dbg_bcr[i] = ctrl;
411 for (i = 0; i < DBG_WRP_MAX; i++) {
412 addr = regs->db_watchregs[i].dbw_addr;
413 ctrl = regs->db_watchregs[i].dbw_ctrl;
415 /* Don't let the user set a watchpoint on a kernel address. */
416 if (addr >= VM_MAXUSER_ADDRESS)
420 * Some control fields are ignored, and other bits reserved.
421 * Only unlinked watchpoints are supported.
423 ctrl &= DBG_WCR_EN | DBG_WCR_PAC | DBG_WCR_LSC | DBG_WCR_BAS |
426 if ((ctrl & DBG_WCR_EN) != 0) {
427 /* Only target EL0. */
428 if ((ctrl & DBG_WCR_PAC) != DBG_WCR_PAC_EL0)
431 /* Must set at least one of the load/store bits. */
432 if ((ctrl & DBG_WCR_LSC) == 0)
436 * When specifying the address range with BAS, the MASK
437 * field must be zero.
439 if ((ctrl & DBG_WCR_BAS) != DBG_WCR_BAS_MASK &&
440 (ctrl & DBG_WCR_MASK) != 0)
443 monitor->dbg_enable_count++;
445 monitor->dbg_wvr[i] = addr;
446 monitor->dbg_wcr[i] = ctrl;
449 if (monitor->dbg_enable_count > 0)
450 monitor->dbg_flags |= DBGMON_ENABLED;
455 #ifdef COMPAT_FREEBSD32
457 fill_regs32(struct thread *td, struct reg32 *regs)
460 struct trapframe *tf;
463 for (i = 0; i < 13; i++)
464 regs->r[i] = tf->tf_x[i];
465 /* For arm32, SP is r13 and LR is r14 */
466 regs->r_sp = tf->tf_x[13];
467 regs->r_lr = tf->tf_x[14];
468 regs->r_pc = tf->tf_elr;
469 regs->r_cpsr = tf->tf_spsr;
475 set_regs32(struct thread *td, struct reg32 *regs)
478 struct trapframe *tf;
481 for (i = 0; i < 13; i++)
482 tf->tf_x[i] = regs->r[i];
483 /* For arm 32, SP is r13 an LR is r14 */
484 tf->tf_x[13] = regs->r_sp;
485 tf->tf_x[14] = regs->r_lr;
486 tf->tf_elr = regs->r_pc;
487 tf->tf_spsr = regs->r_cpsr;
492 /* XXX fill/set dbregs/fpregs are stubbed on 32-bit arm. */
494 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
497 memset(regs, 0, sizeof(*regs));
502 set_fpregs32(struct thread *td, struct fpreg32 *regs)
509 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
512 memset(regs, 0, sizeof(*regs));
517 set_dbregs32(struct thread *td, struct dbreg32 *regs)
525 ptrace_set_pc(struct thread *td, u_long addr)
528 td->td_frame->tf_elr = addr;
533 ptrace_single_step(struct thread *td)
536 td->td_frame->tf_spsr |= PSR_SS;
537 td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
542 ptrace_clear_single_step(struct thread *td)
545 td->td_frame->tf_spsr &= ~PSR_SS;
546 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
551 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
553 struct trapframe *tf = td->td_frame;
554 struct pcb *pcb = td->td_pcb;
556 memset(tf, 0, sizeof(struct trapframe));
559 tf->tf_sp = STACKALIGN(stack);
560 tf->tf_lr = imgp->entry_addr;
561 tf->tf_elr = imgp->entry_addr;
563 td->td_pcb->pcb_tpidr_el0 = 0;
564 td->td_pcb->pcb_tpidrro_el0 = 0;
565 WRITE_SPECIALREG(tpidrro_el0, 0);
566 WRITE_SPECIALREG(tpidr_el0, 0);
569 vfp_reset_state(td, pcb);
573 * Clear debug register state. It is not applicable to the new process.
575 bzero(&pcb->pcb_dbg_regs, sizeof(pcb->pcb_dbg_regs));
578 /* Sanity check these are the same size, they will be memcpy'd to and fro */
579 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
580 sizeof((struct gpregs *)0)->gp_x);
581 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
582 sizeof((struct reg *)0)->x);
585 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
587 struct trapframe *tf = td->td_frame;
589 if (clear_ret & GET_MC_CLEAR_RET) {
590 mcp->mc_gpregs.gp_x[0] = 0;
591 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
593 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
594 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
597 memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
598 sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
600 mcp->mc_gpregs.gp_sp = tf->tf_sp;
601 mcp->mc_gpregs.gp_lr = tf->tf_lr;
602 mcp->mc_gpregs.gp_elr = tf->tf_elr;
603 get_fpcontext(td, mcp);
609 set_mcontext(struct thread *td, mcontext_t *mcp)
611 struct trapframe *tf = td->td_frame;
614 spsr = mcp->mc_gpregs.gp_spsr;
615 if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
616 (spsr & PSR_AARCH32) != 0 ||
617 (spsr & PSR_DAIF) != (td->td_frame->tf_spsr & PSR_DAIF))
620 memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
622 tf->tf_sp = mcp->mc_gpregs.gp_sp;
623 tf->tf_lr = mcp->mc_gpregs.gp_lr;
624 tf->tf_elr = mcp->mc_gpregs.gp_elr;
625 tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
626 set_fpcontext(td, mcp);
632 get_fpcontext(struct thread *td, mcontext_t *mcp)
639 curpcb = curthread->td_pcb;
641 if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
643 * If we have just been running VFP instructions we will
644 * need to save the state to memcpy it below.
646 vfp_save_state(td, curpcb);
648 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
649 ("Called get_fpcontext while the kernel is using the VFP"));
650 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
651 ("Non-userspace FPU flags set in get_fpcontext"));
652 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
653 sizeof(mcp->mc_fpregs));
654 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
655 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
656 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
657 mcp->mc_flags |= _MC_FP_VALID;
665 set_fpcontext(struct thread *td, mcontext_t *mcp)
672 if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
673 curpcb = curthread->td_pcb;
676 * Discard any vfp state for the current thread, we
677 * are about to override it.
681 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
682 ("Called set_fpcontext while the kernel is using the VFP"));
683 memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
684 sizeof(mcp->mc_fpregs));
685 curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
686 curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
687 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
701 if (!sched_runnable())
714 /* We should have shutdown by now, if not enter a low power sleep */
717 __asm __volatile("wfi");
722 * Flush the D-cache for non-DMA I/O so that the I-cache can
723 * be made coherent later.
726 cpu_flush_dcache(void *ptr, size_t len)
732 /* Get current clock frequency for the given CPU ID. */
734 cpu_est_clockrate(int cpu_id, uint64_t *rate)
738 pc = pcpu_find(cpu_id);
739 if (pc == NULL || rate == NULL)
742 if (pc->pc_clock == 0)
745 *rate = pc->pc_clock;
750 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
753 pcpu->pc_acpi_id = 0xffffffff;
754 pcpu->pc_mpidr = 0xffffffff;
764 if (td->td_md.md_spinlock_count == 0) {
765 daif = intr_disable();
766 td->td_md.md_spinlock_count = 1;
767 td->td_md.md_saved_daif = daif;
770 td->td_md.md_spinlock_count++;
780 daif = td->td_md.md_saved_daif;
781 td->td_md.md_spinlock_count--;
782 if (td->td_md.md_spinlock_count == 0) {
788 #ifndef _SYS_SYSPROTO_H_
789 struct sigreturn_args {
795 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
800 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
803 error = set_mcontext(td, &uc.uc_mcontext);
807 /* Restore signal mask. */
808 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
810 return (EJUSTRETURN);
814 * Construct a PCB from a trapframe. This is called from kdb_trap() where
815 * we want to start a backtrace from the function that caused us to enter
816 * the debugger. We have the context in the trapframe, but base the trace
817 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
818 * enough for a backtrace.
821 makectx(struct trapframe *tf, struct pcb *pcb)
825 for (i = 0; i < nitems(pcb->pcb_x); i++)
826 pcb->pcb_x[i] = tf->tf_x[i];
828 /* NB: pcb_lr is the PC, see PC_REGS() in db_machdep.h */
829 pcb->pcb_lr = tf->tf_elr;
830 pcb->pcb_sp = tf->tf_sp;
834 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
838 struct trapframe *tf;
839 struct sigframe *fp, frame;
841 struct sysentvec *sysent;
846 PROC_LOCK_ASSERT(p, MA_OWNED);
848 sig = ksi->ksi_signo;
850 mtx_assert(&psp->ps_mtx, MA_OWNED);
853 onstack = sigonstack(tf->tf_sp);
855 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
858 /* Allocate and validate space for the signal handler context. */
859 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
860 SIGISMEMBER(psp->ps_sigonstack, sig)) {
861 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
862 td->td_sigstk.ss_size);
863 #if defined(COMPAT_43)
864 td->td_sigstk.ss_flags |= SS_ONSTACK;
867 fp = (struct sigframe *)td->td_frame->tf_sp;
870 /* Make room, keeping the stack aligned */
872 fp = (struct sigframe *)STACKALIGN(fp);
874 /* Fill in the frame to copy out */
875 bzero(&frame, sizeof(frame));
876 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
877 frame.sf_si = ksi->ksi_info;
878 frame.sf_uc.uc_sigmask = *mask;
879 frame.sf_uc.uc_stack = td->td_sigstk;
880 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
881 (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
882 mtx_unlock(&psp->ps_mtx);
883 PROC_UNLOCK(td->td_proc);
885 /* Copy the sigframe out to the user's stack. */
886 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
887 /* Process has trashed its stack. Kill it. */
888 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
894 tf->tf_x[1] = (register_t)&fp->sf_si;
895 tf->tf_x[2] = (register_t)&fp->sf_uc;
897 tf->tf_elr = (register_t)catcher;
898 tf->tf_sp = (register_t)fp;
899 sysent = p->p_sysent;
900 if (sysent->sv_sigcode_base != 0)
901 tf->tf_lr = (register_t)sysent->sv_sigcode_base;
903 tf->tf_lr = (register_t)(sysent->sv_psstrings -
904 *(sysent->sv_szsigcode));
906 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
910 mtx_lock(&psp->ps_mtx);
914 init_proc0(vm_offset_t kstack)
916 struct pcpu *pcpup = &__pcpu[0];
918 proc_linkup0(&proc0, &thread0);
919 thread0.td_kstack = kstack;
920 thread0.td_kstack_pages = KSTACK_PAGES;
921 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
922 thread0.td_kstack_pages * PAGE_SIZE) - 1;
923 thread0.td_pcb->pcb_fpflags = 0;
924 thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
925 thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
926 thread0.td_frame = &proc0_tf;
927 pcpup->pc_curpcb = thread0.td_pcb;
936 } EFI_MEMORY_DESCRIPTOR;
938 typedef void (*efi_map_entry_cb)(struct efi_md *);
941 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb)
943 struct efi_md *map, *p;
948 * Memory map data provided by UEFI via the GetMemoryMap
951 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
952 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
954 if (efihdr->descriptor_size == 0)
956 ndesc = efihdr->memory_size / efihdr->descriptor_size;
958 for (i = 0, p = map; i < ndesc; i++,
959 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
965 exclude_efi_map_entry(struct efi_md *p)
968 switch (p->md_type) {
969 case EFI_MD_TYPE_CODE:
970 case EFI_MD_TYPE_DATA:
971 case EFI_MD_TYPE_BS_CODE:
972 case EFI_MD_TYPE_BS_DATA:
973 case EFI_MD_TYPE_FREE:
975 * We're allowed to use any entry with these types.
979 physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
985 exclude_efi_map_entries(struct efi_map_header *efihdr)
988 foreach_efi_map_entry(efihdr, exclude_efi_map_entry);
992 add_efi_map_entry(struct efi_md *p)
995 switch (p->md_type) {
996 case EFI_MD_TYPE_RT_DATA:
998 * Runtime data will be excluded after the DMAP
999 * region is created to stop it from being added
1002 case EFI_MD_TYPE_CODE:
1003 case EFI_MD_TYPE_DATA:
1004 case EFI_MD_TYPE_BS_CODE:
1005 case EFI_MD_TYPE_BS_DATA:
1006 case EFI_MD_TYPE_FREE:
1008 * We're allowed to use any entry with these types.
1010 physmem_hardware_region(p->md_phys,
1011 p->md_pages * PAGE_SIZE);
1017 add_efi_map_entries(struct efi_map_header *efihdr)
1020 foreach_efi_map_entry(efihdr, add_efi_map_entry);
1024 print_efi_map_entry(struct efi_md *p)
1027 static const char *types[] = {
1033 "RuntimeServicesCode",
1034 "RuntimeServicesData",
1035 "ConventionalMemory",
1037 "ACPIReclaimMemory",
1040 "MemoryMappedIOPortSpace",
1045 if (p->md_type < nitems(types))
1046 type = types[p->md_type];
1049 printf("%23s %012lx %12p %08lx ", type, p->md_phys,
1050 p->md_virt, p->md_pages);
1051 if (p->md_attr & EFI_MD_ATTR_UC)
1053 if (p->md_attr & EFI_MD_ATTR_WC)
1055 if (p->md_attr & EFI_MD_ATTR_WT)
1057 if (p->md_attr & EFI_MD_ATTR_WB)
1059 if (p->md_attr & EFI_MD_ATTR_UCE)
1061 if (p->md_attr & EFI_MD_ATTR_WP)
1063 if (p->md_attr & EFI_MD_ATTR_RP)
1065 if (p->md_attr & EFI_MD_ATTR_XP)
1067 if (p->md_attr & EFI_MD_ATTR_NV)
1069 if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
1070 printf("MORE_RELIABLE ");
1071 if (p->md_attr & EFI_MD_ATTR_RO)
1073 if (p->md_attr & EFI_MD_ATTR_RT)
1079 print_efi_map_entries(struct efi_map_header *efihdr)
1082 printf("%23s %12s %12s %8s %4s\n",
1083 "Type", "Physical", "Virtual", "#Pages", "Attr");
1084 foreach_efi_map_entry(efihdr, print_efi_map_entry);
1089 try_load_dtb(caddr_t kmdp)
1093 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1094 #if defined(FDT_DTB_STATIC)
1096 * In case the device tree blob was not retrieved (from metadata) try
1097 * to use the statically embedded one.
1100 dtbp = (vm_offset_t)&fdt_static_dtb;
1103 if (dtbp == (vm_offset_t)NULL) {
1104 printf("ERROR loading DTB\n");
1108 if (OF_install(OFW_FDT, 0) == FALSE)
1109 panic("Cannot install FDT");
1111 if (OF_init((void *)dtbp) != 0)
1112 panic("OF_init failed with the found device tree");
1114 parse_fdt_bootargs();
1121 bool has_acpi, has_fdt;
1124 has_acpi = has_fdt = false;
1127 has_fdt = (OF_peer(0) != 0);
1130 has_acpi = (AcpiOsGetRootPointer() != 0);
1133 env = kern_getenv("kern.cfg.order");
1136 while (order != NULL) {
1138 strncmp(order, "acpi", 4) == 0 &&
1139 (order[4] == ',' || order[4] == '\0')) {
1140 arm64_bus_method = ARM64_BUS_ACPI;
1144 strncmp(order, "fdt", 3) == 0 &&
1145 (order[3] == ',' || order[3] == '\0')) {
1146 arm64_bus_method = ARM64_BUS_FDT;
1149 order = strchr(order, ',');
1153 /* If we set the bus method it is valid */
1154 if (arm64_bus_method != ARM64_BUS_NONE)
1157 /* If no order or an invalid order was set use the default */
1158 if (arm64_bus_method == ARM64_BUS_NONE) {
1160 arm64_bus_method = ARM64_BUS_FDT;
1162 arm64_bus_method = ARM64_BUS_ACPI;
1166 * If no option was set the default is valid, otherwise we are
1167 * setting one to get cninit() working, then calling panic to tell
1168 * the user about the invalid bus setup.
1170 return (env == NULL);
1176 int dczva_line_shift;
1179 identify_cache(READ_SPECIALREG(ctr_el0));
1181 dczid_el0 = READ_SPECIALREG(dczid_el0);
1183 /* Check if dc zva is not prohibited */
1184 if (dczid_el0 & DCZID_DZP)
1185 dczva_line_size = 0;
1187 /* Same as with above calculations */
1188 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
1189 dczva_line_size = sizeof(int) << dczva_line_shift;
1191 /* Change pagezero function */
1192 pagezero = pagezero_cache;
1197 memory_mapping_mode(vm_paddr_t pa)
1199 struct efi_md *map, *p;
1204 return (VM_MEMATTR_WRITE_BACK);
1207 * Memory map data provided by UEFI via the GetMemoryMap
1208 * Boot Services API.
1210 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
1211 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
1213 if (efihdr->descriptor_size == 0)
1214 return (VM_MEMATTR_WRITE_BACK);
1215 ndesc = efihdr->memory_size / efihdr->descriptor_size;
1217 for (i = 0, p = map; i < ndesc; i++,
1218 p = efi_next_descriptor(p, efihdr->descriptor_size)) {
1219 if (pa < p->md_phys ||
1220 pa >= p->md_phys + p->md_pages * EFI_PAGE_SIZE)
1222 if (p->md_type == EFI_MD_TYPE_IOMEM ||
1223 p->md_type == EFI_MD_TYPE_IOPORT)
1224 return (VM_MEMATTR_DEVICE);
1225 else if ((p->md_attr & EFI_MD_ATTR_WB) != 0 ||
1226 p->md_type == EFI_MD_TYPE_RECLAIM)
1227 return (VM_MEMATTR_WRITE_BACK);
1228 else if ((p->md_attr & EFI_MD_ATTR_WT) != 0)
1229 return (VM_MEMATTR_WRITE_THROUGH);
1230 else if ((p->md_attr & EFI_MD_ATTR_WC) != 0)
1231 return (VM_MEMATTR_WRITE_COMBINING);
1235 return (VM_MEMATTR_DEVICE);
1239 initarm(struct arm64_bootparams *abp)
1241 struct efi_fb *efifb;
1245 struct mem_region mem_regions[FDT_MEM_REGIONS];
1248 vm_offset_t lastaddr;
1252 boot_el = abp->boot_el;
1254 /* Parse loader or FDT boot parametes. Determine last used address. */
1255 lastaddr = parse_boot_param(abp);
1257 /* Find the kernel address */
1258 kmdp = preload_search_by_type("elf kernel");
1260 kmdp = preload_search_by_type("elf64 kernel");
1263 update_special_regs(0);
1265 link_elf_ireloc(kmdp);
1268 efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
1270 /* Load the physical memory ranges */
1271 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1272 MODINFO_METADATA | MODINFOMD_EFI_MAP);
1274 add_efi_map_entries(efihdr);
1277 /* Grab physical memory regions information from device tree. */
1278 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
1280 panic("Cannot get physical memory regions");
1281 physmem_hardware_regions(mem_regions, mem_regions_sz);
1283 if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
1284 physmem_exclude_regions(mem_regions, mem_regions_sz,
1285 EXFLAG_NODUMP | EXFLAG_NOALLOC);
1288 /* Exclude the EFI framebuffer from our view of physical memory. */
1289 efifb = (struct efi_fb *)preload_search_info(kmdp,
1290 MODINFO_METADATA | MODINFOMD_EFI_FB);
1292 physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
1295 /* Set the pcpu data, this is needed by pmap_bootstrap */
1297 pcpu_init(pcpup, 0, sizeof(struct pcpu));
1300 * Set the pcpu pointer with a backup in tpidr_el1 to be
1301 * loaded when entering the kernel from userland.
1305 "msr tpidr_el1, %0" :: "r"(pcpup));
1307 PCPU_SET(curthread, &thread0);
1308 PCPU_SET(midr, get_midr());
1310 /* Do basic tuning, hz etc */
1316 /* Bootstrap enough of pmap to enter the kernel proper */
1317 pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
1318 KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
1319 /* Exclude entries neexed in teh DMAP region, but not phys_avail */
1321 exclude_efi_map_entries(efihdr);
1322 physmem_init_kernel_globals();
1324 devmap_bootstrap(0, NULL);
1326 valid = bus_probe();
1329 set_ttbr0(abp->kern_ttbr0);
1333 panic("Invalid bus configuration: %s",
1334 kern_getenv("kern.cfg.order"));
1337 * Dump the boot metadata. We have to wait for cninit() since console
1338 * output is required. If it's grossly incorrect the kernel will never
1341 if (getenv_is_true("debug.dump_modinfo_at_boot"))
1344 init_proc0(abp->kern_stack);
1345 msgbufinit(msgbufp, msgbufsize);
1347 init_param2(physmem);
1355 env = kern_getenv("kernelname");
1357 strlcpy(kernelname, env, sizeof(kernelname));
1359 if (boothowto & RB_VERBOSE) {
1361 print_efi_map_entries(efihdr);
1362 physmem_print_tables();
1373 WRITE_SPECIALREG(oslar_el1, 0);
1375 /* This permits DDB to use debug registers for watchpoints. */
1378 /* TODO: Eventually will need to initialize debug registers here. */
1382 #include <ddb/ddb.h>
1384 DB_SHOW_COMMAND(specialregs, db_show_spregs)
1386 #define PRINT_REG(reg) \
1387 db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
1389 PRINT_REG(actlr_el1);
1390 PRINT_REG(afsr0_el1);
1391 PRINT_REG(afsr1_el1);
1392 PRINT_REG(aidr_el1);
1393 PRINT_REG(amair_el1);
1394 PRINT_REG(ccsidr_el1);
1395 PRINT_REG(clidr_el1);
1396 PRINT_REG(contextidr_el1);
1397 PRINT_REG(cpacr_el1);
1398 PRINT_REG(csselr_el1);
1400 PRINT_REG(currentel);
1402 PRINT_REG(dczid_el0);
1407 /* ARM64TODO: Enable VFP before reading floating-point registers */
1411 PRINT_REG(id_aa64afr0_el1);
1412 PRINT_REG(id_aa64afr1_el1);
1413 PRINT_REG(id_aa64dfr0_el1);
1414 PRINT_REG(id_aa64dfr1_el1);
1415 PRINT_REG(id_aa64isar0_el1);
1416 PRINT_REG(id_aa64isar1_el1);
1417 PRINT_REG(id_aa64pfr0_el1);
1418 PRINT_REG(id_aa64pfr1_el1);
1419 PRINT_REG(id_afr0_el1);
1420 PRINT_REG(id_dfr0_el1);
1421 PRINT_REG(id_isar0_el1);
1422 PRINT_REG(id_isar1_el1);
1423 PRINT_REG(id_isar2_el1);
1424 PRINT_REG(id_isar3_el1);
1425 PRINT_REG(id_isar4_el1);
1426 PRINT_REG(id_isar5_el1);
1427 PRINT_REG(id_mmfr0_el1);
1428 PRINT_REG(id_mmfr1_el1);
1429 PRINT_REG(id_mmfr2_el1);
1430 PRINT_REG(id_mmfr3_el1);
1432 /* Missing from llvm */
1433 PRINT_REG(id_mmfr4_el1);
1435 PRINT_REG(id_pfr0_el1);
1436 PRINT_REG(id_pfr1_el1);
1438 PRINT_REG(mair_el1);
1439 PRINT_REG(midr_el1);
1440 PRINT_REG(mpidr_el1);
1441 PRINT_REG(mvfr0_el1);
1442 PRINT_REG(mvfr1_el1);
1443 PRINT_REG(mvfr2_el1);
1444 PRINT_REG(revidr_el1);
1445 PRINT_REG(sctlr_el1);
1448 PRINT_REG(spsr_el1);
1450 PRINT_REG(tpidr_el0);
1451 PRINT_REG(tpidr_el1);
1452 PRINT_REG(tpidrro_el0);
1453 PRINT_REG(ttbr0_el1);
1454 PRINT_REG(ttbr1_el1);
1455 PRINT_REG(vbar_el1);
1459 DB_SHOW_COMMAND(vtop, db_show_vtop)
1464 phys = arm64_address_translate_s1e1r(addr);
1465 db_printf("EL1 physical address reg (read): 0x%016lx\n", phys);
1466 phys = arm64_address_translate_s1e1w(addr);
1467 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1468 phys = arm64_address_translate_s1e0r(addr);
1469 db_printf("EL0 physical address reg (read): 0x%016lx\n", phys);
1470 phys = arm64_address_translate_s1e0w(addr);
1471 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1473 db_printf("show vtop <virt_addr>\n");