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