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