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