]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/machdep.c
MFV r351075: 10406 large_dnode changes broke zfs recv of legacy stream
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / machdep.c
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include "opt_acpi.h"
29 #include "opt_platform.h"
30 #include "opt_ddb.h"
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/buf.h>
38 #include <sys/bus.h>
39 #include <sys/cons.h>
40 #include <sys/cpu.h>
41 #include <sys/devmap.h>
42 #include <sys/efi.h>
43 #include <sys/exec.h>
44 #include <sys/imgact.h>
45 #include <sys/kdb.h>
46 #include <sys/kernel.h>
47 #include <sys/ktr.h>
48 #include <sys/limits.h>
49 #include <sys/linker.h>
50 #include <sys/msgbuf.h>
51 #include <sys/pcpu.h>
52 #include <sys/proc.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>
62 #include <sys/vdso.h>
63
64 #include <vm/vm.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_page.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_pager.h>
71
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>
83
84 #include <arm/include/physmem.h>
85
86 #ifdef VFP
87 #include <machine/vfp.h>
88 #endif
89
90 #ifdef DEV_ACPI
91 #include <contrib/dev/acpica/include/acpi.h>
92 #include <machine/acpica_machdep.h>
93 #endif
94
95 #ifdef FDT
96 #include <dev/fdt/fdt_common.h>
97 #include <dev/ofw/openfirm.h>
98 #endif
99
100
101 enum arm64_bus arm64_bus_method = ARM64_BUS_NONE;
102
103 struct pcpu __pcpu[MAXCPU];
104
105 static struct trapframe proc0_tf;
106
107 int early_boot = 1;
108 int cold = 1;
109
110 struct kva_md_info kmi;
111
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 */
116 int has_pan;
117
118 /*
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.
121  */
122 vm_paddr_t efi_systbl_phys;
123
124 /* pagezero_* implementations are provided in support.S */
125 void pagezero_simple(void *);
126 void pagezero_cache(void *);
127
128 /* pagezero_simple is default pagezero */
129 void (*pagezero)(void *p) = pagezero_simple;
130
131 static void
132 pan_setup(void)
133 {
134         uint64_t id_aa64mfr1;
135
136         id_aa64mfr1 = READ_SPECIALREG(id_aa64mmfr1_el1);
137         if (ID_AA64MMFR1_PAN(id_aa64mfr1) != ID_AA64MMFR1_PAN_NONE)
138                 has_pan = 1;
139 }
140
141 void
142 pan_enable(void)
143 {
144
145         /*
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:
149          * msr pan, #1
150          *
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.
154          */
155         if (has_pan) {
156                 WRITE_SPECIALREG(sctlr_el1,
157                     READ_SPECIALREG(sctlr_el1) & ~SCTLR_SPAN);
158                 __asm __volatile(".inst 0xd500409f | (0x1 << 8)");
159         }
160 }
161
162 static void
163 cpu_startup(void *dummy)
164 {
165
166         undef_init();
167         identify_cpu();
168         install_cpu_errata();
169
170         vm_ksubmap_init(&kmi);
171         bufinit();
172         vm_pager_bufferinit();
173 }
174
175 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
176
177 int
178 cpu_idle_wakeup(int cpu)
179 {
180
181         return (0);
182 }
183
184 int
185 fill_regs(struct thread *td, struct reg *regs)
186 {
187         struct trapframe *frame;
188
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;
194
195         memcpy(regs->x, frame->tf_x, sizeof(regs->x));
196
197 #ifdef COMPAT_FREEBSD32
198         /*
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.
201          */
202         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
203                 regs->x[15] = frame->tf_elr;
204                 regs->x[16] = frame->tf_spsr;
205         }
206 #endif
207         return (0);
208 }
209
210 int
211 set_regs(struct thread *td, struct reg *regs)
212 {
213         struct trapframe *frame;
214
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;
221
222         memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
223
224 #ifdef COMPAT_FREEBSD32
225         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
226                 /*
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
229                  * it put it.
230                  */
231                 frame->tf_elr = regs->x[15];
232                 frame->tf_spsr = regs->x[16] & PSR_FLAGS;
233         }
234 #endif
235         return (0);
236 }
237
238 int
239 fill_fpregs(struct thread *td, struct fpreg *regs)
240 {
241 #ifdef VFP
242         struct pcb *pcb;
243
244         pcb = td->td_pcb;
245         if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
246                 /*
247                  * If we have just been running VFP instructions we will
248                  * need to save the state to memcpy it below.
249                  */
250                 if (td == curthread)
251                         vfp_save_state(td, pcb);
252
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,
256                     sizeof(regs->fp_q));
257                 regs->fp_cr = pcb->pcb_fpustate.vfp_fpcr;
258                 regs->fp_sr = pcb->pcb_fpustate.vfp_fpsr;
259         } else
260 #endif
261                 memset(regs, 0, sizeof(*regs));
262         return (0);
263 }
264
265 int
266 set_fpregs(struct thread *td, struct fpreg *regs)
267 {
268 #ifdef VFP
269         struct pcb *pcb;
270
271         pcb = td->td_pcb;
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;
277 #endif
278         return (0);
279 }
280
281 int
282 fill_dbregs(struct thread *td, struct dbreg *regs)
283 {
284
285         printf("ARM64TODO: fill_dbregs");
286         return (EDOOFUS);
287 }
288
289 int
290 set_dbregs(struct thread *td, struct dbreg *regs)
291 {
292
293         printf("ARM64TODO: set_dbregs");
294         return (EDOOFUS);
295 }
296
297 #ifdef COMPAT_FREEBSD32
298 int
299 fill_regs32(struct thread *td, struct reg32 *regs)
300 {
301         int i;
302         struct trapframe *tf;
303
304         tf = td->td_frame;
305         for (i = 0; i < 13; i++)
306                 regs->r[i] = tf->tf_x[i];
307         /* For arm32, SP is r13 and LR is r14 */
308         regs->r_sp = tf->tf_x[13];
309         regs->r_lr = tf->tf_x[14];
310         regs->r_pc = tf->tf_elr;
311         regs->r_cpsr = tf->tf_spsr;
312
313         return (0);
314 }
315
316 int
317 set_regs32(struct thread *td, struct reg32 *regs)
318 {
319         int i;
320         struct trapframe *tf;
321
322         tf = td->td_frame;
323         for (i = 0; i < 13; i++)
324                 tf->tf_x[i] = regs->r[i];
325         /* For arm 32, SP is r13 an LR is r14 */
326         tf->tf_x[13] = regs->r_sp;
327         tf->tf_x[14] = regs->r_lr;
328         tf->tf_elr = regs->r_pc;
329         tf->tf_spsr = regs->r_cpsr;
330
331
332         return (0);
333 }
334
335 int
336 fill_fpregs32(struct thread *td, struct fpreg32 *regs)
337 {
338
339         printf("ARM64TODO: fill_fpregs32");
340         return (EDOOFUS);
341 }
342
343 int
344 set_fpregs32(struct thread *td, struct fpreg32 *regs)
345 {
346
347         printf("ARM64TODO: set_fpregs32");
348         return (EDOOFUS);
349 }
350
351 int
352 fill_dbregs32(struct thread *td, struct dbreg32 *regs)
353 {
354
355         printf("ARM64TODO: fill_dbregs32");
356         return (EDOOFUS);
357 }
358
359 int
360 set_dbregs32(struct thread *td, struct dbreg32 *regs)
361 {
362
363         printf("ARM64TODO: set_dbregs32");
364         return (EDOOFUS);
365 }
366 #endif
367
368 int
369 ptrace_set_pc(struct thread *td, u_long addr)
370 {
371
372         printf("ARM64TODO: ptrace_set_pc");
373         return (EDOOFUS);
374 }
375
376 int
377 ptrace_single_step(struct thread *td)
378 {
379
380         td->td_frame->tf_spsr |= PSR_SS;
381         td->td_pcb->pcb_flags |= PCB_SINGLE_STEP;
382         return (0);
383 }
384
385 int
386 ptrace_clear_single_step(struct thread *td)
387 {
388
389         td->td_frame->tf_spsr &= ~PSR_SS;
390         td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
391         return (0);
392 }
393
394 void
395 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
396 {
397         struct trapframe *tf = td->td_frame;
398
399         memset(tf, 0, sizeof(struct trapframe));
400
401         tf->tf_x[0] = stack;
402         tf->tf_sp = STACKALIGN(stack);
403         tf->tf_lr = imgp->entry_addr;
404         tf->tf_elr = imgp->entry_addr;
405 }
406
407 /* Sanity check these are the same size, they will be memcpy'd to and fro */
408 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
409     sizeof((struct gpregs *)0)->gp_x);
410 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
411     sizeof((struct reg *)0)->x);
412
413 int
414 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
415 {
416         struct trapframe *tf = td->td_frame;
417
418         if (clear_ret & GET_MC_CLEAR_RET) {
419                 mcp->mc_gpregs.gp_x[0] = 0;
420                 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
421         } else {
422                 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
423                 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
424         }
425
426         memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
427             sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
428
429         mcp->mc_gpregs.gp_sp = tf->tf_sp;
430         mcp->mc_gpregs.gp_lr = tf->tf_lr;
431         mcp->mc_gpregs.gp_elr = tf->tf_elr;
432
433         return (0);
434 }
435
436 int
437 set_mcontext(struct thread *td, mcontext_t *mcp)
438 {
439         struct trapframe *tf = td->td_frame;
440         uint32_t spsr;
441
442         spsr = mcp->mc_gpregs.gp_spsr;
443         if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
444             (spsr & (PSR_AARCH32 | PSR_F | PSR_I | PSR_A | PSR_D)) != 0)
445                 return (EINVAL); 
446
447         memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
448
449         tf->tf_sp = mcp->mc_gpregs.gp_sp;
450         tf->tf_lr = mcp->mc_gpregs.gp_lr;
451         tf->tf_elr = mcp->mc_gpregs.gp_elr;
452         tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
453
454         return (0);
455 }
456
457 static void
458 get_fpcontext(struct thread *td, mcontext_t *mcp)
459 {
460 #ifdef VFP
461         struct pcb *curpcb;
462
463         critical_enter();
464
465         curpcb = curthread->td_pcb;
466
467         if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
468                 /*
469                  * If we have just been running VFP instructions we will
470                  * need to save the state to memcpy it below.
471                  */
472                 vfp_save_state(td, curpcb);
473
474                 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
475                     ("Called get_fpcontext while the kernel is using the VFP"));
476                 KASSERT((curpcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
477                     ("Non-userspace FPU flags set in get_fpcontext"));
478                 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_fpustate.vfp_regs,
479                     sizeof(mcp->mc_fpregs));
480                 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpustate.vfp_fpcr;
481                 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpustate.vfp_fpsr;
482                 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
483                 mcp->mc_flags |= _MC_FP_VALID;
484         }
485
486         critical_exit();
487 #endif
488 }
489
490 static void
491 set_fpcontext(struct thread *td, mcontext_t *mcp)
492 {
493 #ifdef VFP
494         struct pcb *curpcb;
495
496         critical_enter();
497
498         if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
499                 curpcb = curthread->td_pcb;
500
501                 /*
502                  * Discard any vfp state for the current thread, we
503                  * are about to override it.
504                  */
505                 vfp_discard(td);
506
507                 KASSERT(curpcb->pcb_fpusaved == &curpcb->pcb_fpustate,
508                     ("Called set_fpcontext while the kernel is using the VFP"));
509                 memcpy(curpcb->pcb_fpustate.vfp_regs, mcp->mc_fpregs.fp_q,
510                     sizeof(mcp->mc_fpregs));
511                 curpcb->pcb_fpustate.vfp_fpcr = mcp->mc_fpregs.fp_cr;
512                 curpcb->pcb_fpustate.vfp_fpsr = mcp->mc_fpregs.fp_sr;
513                 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags & PCB_FP_USERMASK;
514         }
515
516         critical_exit();
517 #endif
518 }
519
520 void
521 cpu_idle(int busy)
522 {
523
524         spinlock_enter();
525         if (!busy)
526                 cpu_idleclock();
527         if (!sched_runnable())
528                 __asm __volatile(
529                     "dsb sy \n"
530                     "wfi    \n");
531         if (!busy)
532                 cpu_activeclock();
533         spinlock_exit();
534 }
535
536 void
537 cpu_halt(void)
538 {
539
540         /* We should have shutdown by now, if not enter a low power sleep */
541         intr_disable();
542         while (1) {
543                 __asm __volatile("wfi");
544         }
545 }
546
547 /*
548  * Flush the D-cache for non-DMA I/O so that the I-cache can
549  * be made coherent later.
550  */
551 void
552 cpu_flush_dcache(void *ptr, size_t len)
553 {
554
555         /* ARM64TODO TBD */
556 }
557
558 /* Get current clock frequency for the given CPU ID. */
559 int
560 cpu_est_clockrate(int cpu_id, uint64_t *rate)
561 {
562         struct pcpu *pc;
563
564         pc = pcpu_find(cpu_id);
565         if (pc == NULL || rate == NULL)
566                 return (EINVAL);
567
568         if (pc->pc_clock == 0)
569                 return (EOPNOTSUPP);
570
571         *rate = pc->pc_clock;
572         return (0);
573 }
574
575 void
576 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
577 {
578
579         pcpu->pc_acpi_id = 0xffffffff;
580 }
581
582 void
583 spinlock_enter(void)
584 {
585         struct thread *td;
586         register_t daif;
587
588         td = curthread;
589         if (td->td_md.md_spinlock_count == 0) {
590                 daif = intr_disable();
591                 td->td_md.md_spinlock_count = 1;
592                 td->td_md.md_saved_daif = daif;
593         } else
594                 td->td_md.md_spinlock_count++;
595         critical_enter();
596 }
597
598 void
599 spinlock_exit(void)
600 {
601         struct thread *td;
602         register_t daif;
603
604         td = curthread;
605         critical_exit();
606         daif = td->td_md.md_saved_daif;
607         td->td_md.md_spinlock_count--;
608         if (td->td_md.md_spinlock_count == 0)
609                 intr_restore(daif);
610 }
611
612 #ifndef _SYS_SYSPROTO_H_
613 struct sigreturn_args {
614         ucontext_t *ucp;
615 };
616 #endif
617
618 int
619 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
620 {
621         ucontext_t uc;
622         int error;
623
624         if (uap == NULL)
625                 return (EFAULT);
626         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
627                 return (EFAULT);
628
629         error = set_mcontext(td, &uc.uc_mcontext);
630         if (error != 0)
631                 return (error);
632         set_fpcontext(td, &uc.uc_mcontext);
633
634         /* Restore signal mask. */
635         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
636
637         return (EJUSTRETURN);
638 }
639
640 /*
641  * Construct a PCB from a trapframe. This is called from kdb_trap() where
642  * we want to start a backtrace from the function that caused us to enter
643  * the debugger. We have the context in the trapframe, but base the trace
644  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
645  * enough for a backtrace.
646  */
647 void
648 makectx(struct trapframe *tf, struct pcb *pcb)
649 {
650         int i;
651
652         for (i = 0; i < PCB_LR; i++)
653                 pcb->pcb_x[i] = tf->tf_x[i];
654
655         pcb->pcb_x[PCB_LR] = tf->tf_lr;
656         pcb->pcb_pc = tf->tf_elr;
657         pcb->pcb_sp = tf->tf_sp;
658 }
659
660 void
661 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
662 {
663         struct thread *td;
664         struct proc *p;
665         struct trapframe *tf;
666         struct sigframe *fp, frame;
667         struct sigacts *psp;
668         struct sysentvec *sysent;
669         int onstack, sig;
670
671         td = curthread;
672         p = td->td_proc;
673         PROC_LOCK_ASSERT(p, MA_OWNED);
674
675         sig = ksi->ksi_signo;
676         psp = p->p_sigacts;
677         mtx_assert(&psp->ps_mtx, MA_OWNED);
678
679         tf = td->td_frame;
680         onstack = sigonstack(tf->tf_sp);
681
682         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
683             catcher, sig);
684
685         /* Allocate and validate space for the signal handler context. */
686         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
687             SIGISMEMBER(psp->ps_sigonstack, sig)) {
688                 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
689                     td->td_sigstk.ss_size);
690 #if defined(COMPAT_43)
691                 td->td_sigstk.ss_flags |= SS_ONSTACK;
692 #endif
693         } else {
694                 fp = (struct sigframe *)td->td_frame->tf_sp;
695         }
696
697         /* Make room, keeping the stack aligned */
698         fp--;
699         fp = (struct sigframe *)STACKALIGN(fp);
700
701         /* Fill in the frame to copy out */
702         bzero(&frame, sizeof(frame));
703         get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
704         get_fpcontext(td, &frame.sf_uc.uc_mcontext);
705         frame.sf_si = ksi->ksi_info;
706         frame.sf_uc.uc_sigmask = *mask;
707         frame.sf_uc.uc_stack = td->td_sigstk;
708         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
709             (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
710         mtx_unlock(&psp->ps_mtx);
711         PROC_UNLOCK(td->td_proc);
712
713         /* Copy the sigframe out to the user's stack. */
714         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
715                 /* Process has trashed its stack. Kill it. */
716                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
717                 PROC_LOCK(p);
718                 sigexit(td, SIGILL);
719         }
720
721         tf->tf_x[0]= sig;
722         tf->tf_x[1] = (register_t)&fp->sf_si;
723         tf->tf_x[2] = (register_t)&fp->sf_uc;
724
725         tf->tf_elr = (register_t)catcher;
726         tf->tf_sp = (register_t)fp;
727         sysent = p->p_sysent;
728         if (sysent->sv_sigcode_base != 0)
729                 tf->tf_lr = (register_t)sysent->sv_sigcode_base;
730         else
731                 tf->tf_lr = (register_t)(sysent->sv_psstrings -
732                     *(sysent->sv_szsigcode));
733
734         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
735             tf->tf_sp);
736
737         PROC_LOCK(p);
738         mtx_lock(&psp->ps_mtx);
739 }
740
741 static void
742 init_proc0(vm_offset_t kstack)
743 {
744         struct pcpu *pcpup = &__pcpu[0];
745
746         proc_linkup0(&proc0, &thread0);
747         thread0.td_kstack = kstack;
748         thread0.td_pcb = (struct pcb *)(thread0.td_kstack) - 1;
749         thread0.td_pcb->pcb_fpflags = 0;
750         thread0.td_pcb->pcb_fpusaved = &thread0.td_pcb->pcb_fpustate;
751         thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
752         thread0.td_frame = &proc0_tf;
753         pcpup->pc_curpcb = thread0.td_pcb;
754
755         /* Set the base address of translation table 0. */
756         thread0.td_proc->p_md.md_l0addr = READ_SPECIALREG(ttbr0_el1);
757 }
758
759 typedef struct {
760         uint32_t type;
761         uint64_t phys_start;
762         uint64_t virt_start;
763         uint64_t num_pages;
764         uint64_t attr;
765 } EFI_MEMORY_DESCRIPTOR;
766
767 typedef void (*efi_map_entry_cb)(struct efi_md *);
768
769 static void
770 foreach_efi_map_entry(struct efi_map_header *efihdr, efi_map_entry_cb cb)
771 {
772         struct efi_md *map, *p;
773         size_t efisz;
774         int ndesc, i;
775
776         /*
777          * Memory map data provided by UEFI via the GetMemoryMap
778          * Boot Services API.
779          */
780         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
781         map = (struct efi_md *)((uint8_t *)efihdr + efisz); 
782
783         if (efihdr->descriptor_size == 0)
784                 return;
785         ndesc = efihdr->memory_size / efihdr->descriptor_size;
786
787         for (i = 0, p = map; i < ndesc; i++,
788             p = efi_next_descriptor(p, efihdr->descriptor_size)) {
789                 cb(p);
790         }
791 }
792
793 static void
794 exclude_efi_map_entry(struct efi_md *p)
795 {
796
797         switch (p->md_type) {
798         case EFI_MD_TYPE_CODE:
799         case EFI_MD_TYPE_DATA:
800         case EFI_MD_TYPE_BS_CODE:
801         case EFI_MD_TYPE_BS_DATA:
802         case EFI_MD_TYPE_FREE:
803                 /*
804                  * We're allowed to use any entry with these types.
805                  */
806                 break;
807         default:
808                 arm_physmem_exclude_region(p->md_phys, p->md_pages * PAGE_SIZE,
809                     EXFLAG_NOALLOC);
810         }
811 }
812
813 static void
814 exclude_efi_map_entries(struct efi_map_header *efihdr)
815 {
816
817         foreach_efi_map_entry(efihdr, exclude_efi_map_entry);
818 }
819
820 static void
821 add_efi_map_entry(struct efi_md *p)
822 {
823
824         switch (p->md_type) {
825         case EFI_MD_TYPE_RT_DATA:
826                 /*
827                  * Runtime data will be excluded after the DMAP
828                  * region is created to stop it from being added
829                  * to phys_avail.
830                  */
831         case EFI_MD_TYPE_CODE:
832         case EFI_MD_TYPE_DATA:
833         case EFI_MD_TYPE_BS_CODE:
834         case EFI_MD_TYPE_BS_DATA:
835         case EFI_MD_TYPE_FREE:
836                 /*
837                  * We're allowed to use any entry with these types.
838                  */
839                 arm_physmem_hardware_region(p->md_phys,
840                     p->md_pages * PAGE_SIZE);
841                 break;
842         }
843 }
844
845 static void
846 add_efi_map_entries(struct efi_map_header *efihdr)
847 {
848
849         foreach_efi_map_entry(efihdr, add_efi_map_entry);
850 }
851
852 static void
853 print_efi_map_entry(struct efi_md *p)
854 {
855         const char *type;
856         static const char *types[] = {
857                 "Reserved",
858                 "LoaderCode",
859                 "LoaderData",
860                 "BootServicesCode",
861                 "BootServicesData",
862                 "RuntimeServicesCode",
863                 "RuntimeServicesData",
864                 "ConventionalMemory",
865                 "UnusableMemory",
866                 "ACPIReclaimMemory",
867                 "ACPIMemoryNVS",
868                 "MemoryMappedIO",
869                 "MemoryMappedIOPortSpace",
870                 "PalCode",
871                 "PersistentMemory"
872         };
873
874         if (p->md_type < nitems(types))
875                 type = types[p->md_type];
876         else
877                 type = "<INVALID>";
878         printf("%23s %012lx %12p %08lx ", type, p->md_phys,
879             p->md_virt, p->md_pages);
880         if (p->md_attr & EFI_MD_ATTR_UC)
881                 printf("UC ");
882         if (p->md_attr & EFI_MD_ATTR_WC)
883                 printf("WC ");
884         if (p->md_attr & EFI_MD_ATTR_WT)
885                 printf("WT ");
886         if (p->md_attr & EFI_MD_ATTR_WB)
887                 printf("WB ");
888         if (p->md_attr & EFI_MD_ATTR_UCE)
889                 printf("UCE ");
890         if (p->md_attr & EFI_MD_ATTR_WP)
891                 printf("WP ");
892         if (p->md_attr & EFI_MD_ATTR_RP)
893                 printf("RP ");
894         if (p->md_attr & EFI_MD_ATTR_XP)
895                 printf("XP ");
896         if (p->md_attr & EFI_MD_ATTR_NV)
897                 printf("NV ");
898         if (p->md_attr & EFI_MD_ATTR_MORE_RELIABLE)
899                 printf("MORE_RELIABLE ");
900         if (p->md_attr & EFI_MD_ATTR_RO)
901                 printf("RO ");
902         if (p->md_attr & EFI_MD_ATTR_RT)
903                 printf("RUNTIME");
904         printf("\n");
905 }
906
907 static void
908 print_efi_map_entries(struct efi_map_header *efihdr)
909 {
910
911         printf("%23s %12s %12s %8s %4s\n",
912             "Type", "Physical", "Virtual", "#Pages", "Attr");
913         foreach_efi_map_entry(efihdr, print_efi_map_entry);
914 }
915
916 #ifdef FDT
917 static void
918 try_load_dtb(caddr_t kmdp)
919 {
920         vm_offset_t dtbp;
921
922         dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
923         if (dtbp == (vm_offset_t)NULL) {
924                 printf("ERROR loading DTB\n");
925                 return;
926         }
927
928         if (OF_install(OFW_FDT, 0) == FALSE)
929                 panic("Cannot install FDT");
930
931         if (OF_init((void *)dtbp) != 0)
932                 panic("OF_init failed with the found device tree");
933 }
934 #endif
935
936 static bool
937 bus_probe(void)
938 {
939         bool has_acpi, has_fdt;
940         char *order, *env;
941
942         has_acpi = has_fdt = false;
943
944 #ifdef FDT
945         has_fdt = (OF_peer(0) != 0);
946 #endif
947 #ifdef DEV_ACPI
948         has_acpi = (acpi_find_table(ACPI_SIG_SPCR) != 0);
949 #endif
950
951         env = kern_getenv("kern.cfg.order");
952         if (env != NULL) {
953                 order = env;
954                 while (order != NULL) {
955                         if (has_acpi &&
956                             strncmp(order, "acpi", 4) == 0 &&
957                             (order[4] == ',' || order[4] == '\0')) {
958                                 arm64_bus_method = ARM64_BUS_ACPI;
959                                 break;
960                         }
961                         if (has_fdt &&
962                             strncmp(order, "fdt", 3) == 0 &&
963                             (order[3] == ',' || order[3] == '\0')) {
964                                 arm64_bus_method = ARM64_BUS_FDT;
965                                 break;
966                         }
967                         order = strchr(order, ',');
968                 }
969                 freeenv(env);
970
971                 /* If we set the bus method it is valid */
972                 if (arm64_bus_method != ARM64_BUS_NONE)
973                         return (true);
974         }
975         /* If no order or an invalid order was set use the default */
976         if (arm64_bus_method == ARM64_BUS_NONE) {
977                 if (has_fdt)
978                         arm64_bus_method = ARM64_BUS_FDT;
979                 else if (has_acpi)
980                         arm64_bus_method = ARM64_BUS_ACPI;
981         }
982
983         /*
984          * If no option was set the default is valid, otherwise we are
985          * setting one to get cninit() working, then calling panic to tell
986          * the user about the invalid bus setup.
987          */
988         return (env == NULL);
989 }
990
991 static void
992 cache_setup(void)
993 {
994         int dcache_line_shift, icache_line_shift, dczva_line_shift;
995         uint32_t ctr_el0;
996         uint32_t dczid_el0;
997
998         ctr_el0 = READ_SPECIALREG(ctr_el0);
999
1000         /* Read the log2 words in each D cache line */
1001         dcache_line_shift = CTR_DLINE_SIZE(ctr_el0);
1002         /* Get the D cache line size */
1003         dcache_line_size = sizeof(int) << dcache_line_shift;
1004
1005         /* And the same for the I cache */
1006         icache_line_shift = CTR_ILINE_SIZE(ctr_el0);
1007         icache_line_size = sizeof(int) << icache_line_shift;
1008
1009         idcache_line_size = MIN(dcache_line_size, icache_line_size);
1010
1011         dczid_el0 = READ_SPECIALREG(dczid_el0);
1012
1013         /* Check if dc zva is not prohibited */
1014         if (dczid_el0 & DCZID_DZP)
1015                 dczva_line_size = 0;
1016         else {
1017                 /* Same as with above calculations */
1018                 dczva_line_shift = DCZID_BS_SIZE(dczid_el0);
1019                 dczva_line_size = sizeof(int) << dczva_line_shift;
1020
1021                 /* Change pagezero function */
1022                 pagezero = pagezero_cache;
1023         }
1024 }
1025
1026 void
1027 initarm(struct arm64_bootparams *abp)
1028 {
1029         struct efi_fb *efifb;
1030         struct efi_map_header *efihdr;
1031         struct pcpu *pcpup;
1032         char *env;
1033 #ifdef FDT
1034         struct mem_region mem_regions[FDT_MEM_REGIONS];
1035         int mem_regions_sz;
1036 #endif
1037         vm_offset_t lastaddr;
1038         caddr_t kmdp;
1039         bool valid;
1040
1041         /* Set the module data location */
1042         preload_metadata = (caddr_t)(uintptr_t)(abp->modulep);
1043
1044         /* Find the kernel address */
1045         kmdp = preload_search_by_type("elf kernel");
1046         if (kmdp == NULL)
1047                 kmdp = preload_search_by_type("elf64 kernel");
1048
1049         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
1050         init_static_kenv(MD_FETCH(kmdp, MODINFOMD_ENVP, char *), 0);
1051         link_elf_ireloc(kmdp);
1052
1053 #ifdef FDT
1054         try_load_dtb(kmdp);
1055 #endif
1056
1057         efi_systbl_phys = MD_FETCH(kmdp, MODINFOMD_FW_HANDLE, vm_paddr_t);
1058
1059         /* Find the address to start allocating from */
1060         lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
1061
1062         /* Load the physical memory ranges */
1063         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1064             MODINFO_METADATA | MODINFOMD_EFI_MAP);
1065         if (efihdr != NULL)
1066                 add_efi_map_entries(efihdr);
1067 #ifdef FDT
1068         else {
1069                 /* Grab physical memory regions information from device tree. */
1070                 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,
1071                     NULL) != 0)
1072                         panic("Cannot get physical memory regions");
1073                 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1074         }
1075         if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0)
1076                 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1077                     EXFLAG_NODUMP | EXFLAG_NOALLOC);
1078 #endif
1079
1080         /* Exclude the EFI framebuffer from our view of physical memory. */
1081         efifb = (struct efi_fb *)preload_search_info(kmdp,
1082             MODINFO_METADATA | MODINFOMD_EFI_FB);
1083         if (efifb != NULL)
1084                 arm_physmem_exclude_region(efifb->fb_addr, efifb->fb_size,
1085                     EXFLAG_NOALLOC);
1086
1087         /* Set the pcpu data, this is needed by pmap_bootstrap */
1088         pcpup = &__pcpu[0];
1089         pcpu_init(pcpup, 0, sizeof(struct pcpu));
1090
1091         /*
1092          * Set the pcpu pointer with a backup in tpidr_el1 to be
1093          * loaded when entering the kernel from userland.
1094          */
1095         __asm __volatile(
1096             "mov x18, %0 \n"
1097             "msr tpidr_el1, %0" :: "r"(pcpup));
1098
1099         PCPU_SET(curthread, &thread0);
1100
1101         /* Do basic tuning, hz etc */
1102         init_param1();
1103
1104         cache_setup();
1105         pan_setup();
1106
1107         /* Bootstrap enough of pmap  to enter the kernel proper */
1108         pmap_bootstrap(abp->kern_l0pt, abp->kern_l1pt,
1109             KERNBASE - abp->kern_delta, lastaddr - KERNBASE);
1110         /* Exclude entries neexed in teh DMAP region, but not phys_avail */
1111         if (efihdr != NULL)
1112                 exclude_efi_map_entries(efihdr);
1113         arm_physmem_init_kernel_globals();
1114
1115         devmap_bootstrap(0, NULL);
1116
1117         valid = bus_probe();
1118
1119         cninit();
1120
1121         if (!valid)
1122                 panic("Invalid bus configuration: %s",
1123                     kern_getenv("kern.cfg.order"));
1124
1125         init_proc0(abp->kern_stack);
1126         msgbufinit(msgbufp, msgbufsize);
1127         mutex_init();
1128         init_param2(physmem);
1129
1130         dbg_init();
1131         kdb_init();
1132         pan_enable();
1133
1134         env = kern_getenv("kernelname");
1135         if (env != NULL)
1136                 strlcpy(kernelname, env, sizeof(kernelname));
1137
1138         if (boothowto & RB_VERBOSE) {
1139                 print_efi_map_entries(efihdr);
1140                 arm_physmem_print_tables();
1141         }
1142
1143         early_boot = 0;
1144 }
1145
1146 void
1147 dbg_init(void)
1148 {
1149
1150         /* Clear OS lock */
1151         WRITE_SPECIALREG(OSLAR_EL1, 0);
1152
1153         /* This permits DDB to use debug registers for watchpoints. */
1154         dbg_monitor_init();
1155
1156         /* TODO: Eventually will need to initialize debug registers here. */
1157 }
1158
1159 #ifdef DDB
1160 #include <ddb/ddb.h>
1161
1162 DB_SHOW_COMMAND(specialregs, db_show_spregs)
1163 {
1164 #define PRINT_REG(reg)  \
1165     db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
1166
1167         PRINT_REG(actlr_el1);
1168         PRINT_REG(afsr0_el1);
1169         PRINT_REG(afsr1_el1);
1170         PRINT_REG(aidr_el1);
1171         PRINT_REG(amair_el1);
1172         PRINT_REG(ccsidr_el1);
1173         PRINT_REG(clidr_el1);
1174         PRINT_REG(contextidr_el1);
1175         PRINT_REG(cpacr_el1);
1176         PRINT_REG(csselr_el1);
1177         PRINT_REG(ctr_el0);
1178         PRINT_REG(currentel);
1179         PRINT_REG(daif);
1180         PRINT_REG(dczid_el0);
1181         PRINT_REG(elr_el1);
1182         PRINT_REG(esr_el1);
1183         PRINT_REG(far_el1);
1184 #if 0
1185         /* ARM64TODO: Enable VFP before reading floating-point registers */
1186         PRINT_REG(fpcr);
1187         PRINT_REG(fpsr);
1188 #endif
1189         PRINT_REG(id_aa64afr0_el1);
1190         PRINT_REG(id_aa64afr1_el1);
1191         PRINT_REG(id_aa64dfr0_el1);
1192         PRINT_REG(id_aa64dfr1_el1);
1193         PRINT_REG(id_aa64isar0_el1);
1194         PRINT_REG(id_aa64isar1_el1);
1195         PRINT_REG(id_aa64pfr0_el1);
1196         PRINT_REG(id_aa64pfr1_el1);
1197         PRINT_REG(id_afr0_el1);
1198         PRINT_REG(id_dfr0_el1);
1199         PRINT_REG(id_isar0_el1);
1200         PRINT_REG(id_isar1_el1);
1201         PRINT_REG(id_isar2_el1);
1202         PRINT_REG(id_isar3_el1);
1203         PRINT_REG(id_isar4_el1);
1204         PRINT_REG(id_isar5_el1);
1205         PRINT_REG(id_mmfr0_el1);
1206         PRINT_REG(id_mmfr1_el1);
1207         PRINT_REG(id_mmfr2_el1);
1208         PRINT_REG(id_mmfr3_el1);
1209 #if 0
1210         /* Missing from llvm */
1211         PRINT_REG(id_mmfr4_el1);
1212 #endif
1213         PRINT_REG(id_pfr0_el1);
1214         PRINT_REG(id_pfr1_el1);
1215         PRINT_REG(isr_el1);
1216         PRINT_REG(mair_el1);
1217         PRINT_REG(midr_el1);
1218         PRINT_REG(mpidr_el1);
1219         PRINT_REG(mvfr0_el1);
1220         PRINT_REG(mvfr1_el1);
1221         PRINT_REG(mvfr2_el1);
1222         PRINT_REG(revidr_el1);
1223         PRINT_REG(sctlr_el1);
1224         PRINT_REG(sp_el0);
1225         PRINT_REG(spsel);
1226         PRINT_REG(spsr_el1);
1227         PRINT_REG(tcr_el1);
1228         PRINT_REG(tpidr_el0);
1229         PRINT_REG(tpidr_el1);
1230         PRINT_REG(tpidrro_el0);
1231         PRINT_REG(ttbr0_el1);
1232         PRINT_REG(ttbr1_el1);
1233         PRINT_REG(vbar_el1);
1234 #undef PRINT_REG
1235 }
1236
1237 DB_SHOW_COMMAND(vtop, db_show_vtop)
1238 {
1239         uint64_t phys;
1240
1241         if (have_addr) {
1242                 phys = arm64_address_translate_s1e1r(addr);
1243                 db_printf("EL1 physical address reg (read):  0x%016lx\n", phys);
1244                 phys = arm64_address_translate_s1e1w(addr);
1245                 db_printf("EL1 physical address reg (write): 0x%016lx\n", phys);
1246                 phys = arm64_address_translate_s1e0r(addr);
1247                 db_printf("EL0 physical address reg (read):  0x%016lx\n", phys);
1248                 phys = arm64_address_translate_s1e0w(addr);
1249                 db_printf("EL0 physical address reg (write): 0x%016lx\n", phys);
1250         } else
1251                 db_printf("show vtop <virt_addr>\n");
1252 }
1253 #endif