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