]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/machdep.c
Update clang to trunk r256945.
[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_platform.h"
29 #include "opt_ddb.h"
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/buf.h>
37 #include <sys/bus.h>
38 #include <sys/cons.h>
39 #include <sys/cpu.h>
40 #include <sys/efi.h>
41 #include <sys/exec.h>
42 #include <sys/imgact.h>
43 #include <sys/kdb.h> 
44 #include <sys/kernel.h>
45 #include <sys/limits.h>
46 #include <sys/linker.h>
47 #include <sys/msgbuf.h>
48 #include <sys/pcpu.h>
49 #include <sys/proc.h>
50 #include <sys/ptrace.h>
51 #include <sys/reboot.h>
52 #include <sys/rwlock.h>
53 #include <sys/sched.h>
54 #include <sys/signalvar.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/sysent.h>
57 #include <sys/sysproto.h>
58 #include <sys/ucontext.h>
59 #include <sys/vdso.h>
60
61 #include <vm/vm.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_page.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_pager.h>
68
69 #include <machine/armreg.h>
70 #include <machine/cpu.h>
71 #include <machine/debug_monitor.h>
72 #include <machine/kdb.h>
73 #include <machine/devmap.h>
74 #include <machine/machdep.h>
75 #include <machine/metadata.h>
76 #include <machine/md_var.h>
77 #include <machine/pcb.h>
78 #include <machine/reg.h>
79 #include <machine/vmparam.h>
80
81 #ifdef VFP
82 #include <machine/vfp.h>
83 #endif
84
85 #ifdef FDT
86 #include <dev/fdt/fdt_common.h>
87 #include <dev/ofw/openfirm.h>
88 #endif
89
90 struct pcpu __pcpu[MAXCPU];
91
92 static struct trapframe proc0_tf;
93
94 vm_paddr_t phys_avail[PHYS_AVAIL_SIZE + 2];
95 vm_paddr_t dump_avail[PHYS_AVAIL_SIZE + 2];
96
97 int early_boot = 1;
98 int cold = 1;
99 long realmem = 0;
100 long Maxmem = 0;
101
102 #define PHYSMAP_SIZE    (2 * (VM_PHYSSEG_MAX - 1))
103 vm_paddr_t physmap[PHYSMAP_SIZE];
104 u_int physmap_idx;
105
106 struct kva_md_info kmi;
107
108 int64_t dcache_line_size;       /* The minimum D cache line size */
109 int64_t icache_line_size;       /* The minimum I cache line size */
110 int64_t idcache_line_size;      /* The minimum cache line size */
111
112 static void
113 cpu_startup(void *dummy)
114 {
115
116         identify_cpu();
117
118         vm_ksubmap_init(&kmi);
119         bufinit();
120         vm_pager_bufferinit();
121 }
122
123 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
124
125 int
126 cpu_idle_wakeup(int cpu)
127 {
128
129         return (0);
130 }
131
132 void
133 bzero(void *buf, size_t len)
134 {
135         uint8_t *p;
136
137         p = buf;
138         while(len-- > 0)
139                 *p++ = 0;
140 }
141
142 int
143 fill_regs(struct thread *td, struct reg *regs)
144 {
145         struct trapframe *frame;
146
147         frame = td->td_frame;
148         regs->sp = frame->tf_sp;
149         regs->lr = frame->tf_lr;
150         regs->elr = frame->tf_elr;
151         regs->spsr = frame->tf_spsr;
152
153         memcpy(regs->x, frame->tf_x, sizeof(regs->x));
154
155         return (0);
156 }
157
158 int
159 set_regs(struct thread *td, struct reg *regs)
160 {
161         struct trapframe *frame;
162
163         frame = td->td_frame;
164         frame->tf_sp = regs->sp;
165         frame->tf_lr = regs->lr;
166         frame->tf_elr = regs->elr;
167         frame->tf_spsr = regs->spsr;
168
169         memcpy(frame->tf_x, regs->x, sizeof(frame->tf_x));
170
171         return (0);
172 }
173
174 int
175 fill_fpregs(struct thread *td, struct fpreg *regs)
176 {
177 #ifdef VFP
178         struct pcb *pcb;
179
180         pcb = td->td_pcb;
181         if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
182                 /*
183                  * If we have just been running VFP instructions we will
184                  * need to save the state to memcpy it below.
185                  */
186                 vfp_save_state(td, pcb);
187
188                 memcpy(regs->fp_q, pcb->pcb_vfp, sizeof(regs->fp_q));
189                 regs->fp_cr = pcb->pcb_fpcr;
190                 regs->fp_sr = pcb->pcb_fpsr;
191         } else
192 #endif
193                 memset(regs->fp_q, 0, sizeof(regs->fp_q));
194         return (0);
195 }
196
197 int
198 set_fpregs(struct thread *td, struct fpreg *regs)
199 {
200 #ifdef VFP
201         struct pcb *pcb;
202
203         pcb = td->td_pcb;
204         memcpy(pcb->pcb_vfp, regs->fp_q, sizeof(regs->fp_q));
205         pcb->pcb_fpcr = regs->fp_cr;
206         pcb->pcb_fpsr = regs->fp_sr;
207 #endif
208         return (0);
209 }
210
211 int
212 fill_dbregs(struct thread *td, struct dbreg *regs)
213 {
214
215         panic("ARM64TODO: fill_dbregs");
216 }
217
218 int
219 set_dbregs(struct thread *td, struct dbreg *regs)
220 {
221
222         panic("ARM64TODO: set_dbregs");
223 }
224
225 int
226 ptrace_set_pc(struct thread *td, u_long addr)
227 {
228
229         panic("ARM64TODO: ptrace_set_pc");
230         return (0);
231 }
232
233 int
234 ptrace_single_step(struct thread *td)
235 {
236
237         /* TODO; */
238         return (0);
239 }
240
241 int
242 ptrace_clear_single_step(struct thread *td)
243 {
244
245         /* TODO; */
246         return (0);
247 }
248
249 void
250 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
251 {
252         struct trapframe *tf = td->td_frame;
253
254         memset(tf, 0, sizeof(struct trapframe));
255
256         /*
257          * We need to set x0 for init as it doesn't call
258          * cpu_set_syscall_retval to copy the value. We also
259          * need to set td_retval for the cases where we do.
260          */
261         tf->tf_x[0] = td->td_retval[0] = stack;
262         tf->tf_sp = STACKALIGN(stack);
263         tf->tf_lr = imgp->entry_addr;
264         tf->tf_elr = imgp->entry_addr;
265 }
266
267 /* Sanity check these are the same size, they will be memcpy'd to and fro */
268 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
269     sizeof((struct gpregs *)0)->gp_x);
270 CTASSERT(sizeof(((struct trapframe *)0)->tf_x) ==
271     sizeof((struct reg *)0)->x);
272
273 int
274 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
275 {
276         struct trapframe *tf = td->td_frame;
277
278         if (clear_ret & GET_MC_CLEAR_RET) {
279                 mcp->mc_gpregs.gp_x[0] = 0;
280                 mcp->mc_gpregs.gp_spsr = tf->tf_spsr & ~PSR_C;
281         } else {
282                 mcp->mc_gpregs.gp_x[0] = tf->tf_x[0];
283                 mcp->mc_gpregs.gp_spsr = tf->tf_spsr;
284         }
285
286         memcpy(&mcp->mc_gpregs.gp_x[1], &tf->tf_x[1],
287             sizeof(mcp->mc_gpregs.gp_x[1]) * (nitems(mcp->mc_gpregs.gp_x) - 1));
288
289         mcp->mc_gpregs.gp_sp = tf->tf_sp;
290         mcp->mc_gpregs.gp_lr = tf->tf_lr;
291         mcp->mc_gpregs.gp_elr = tf->tf_elr;
292
293         return (0);
294 }
295
296 int
297 set_mcontext(struct thread *td, mcontext_t *mcp)
298 {
299         struct trapframe *tf = td->td_frame;
300
301         memcpy(tf->tf_x, mcp->mc_gpregs.gp_x, sizeof(tf->tf_x));
302
303         tf->tf_sp = mcp->mc_gpregs.gp_sp;
304         tf->tf_lr = mcp->mc_gpregs.gp_lr;
305         tf->tf_elr = mcp->mc_gpregs.gp_elr;
306         tf->tf_spsr = mcp->mc_gpregs.gp_spsr;
307
308         return (0);
309 }
310
311 static void
312 get_fpcontext(struct thread *td, mcontext_t *mcp)
313 {
314 #ifdef VFP
315         struct pcb *curpcb;
316
317         critical_enter();
318
319         curpcb = curthread->td_pcb;
320
321         if ((curpcb->pcb_fpflags & PCB_FP_STARTED) != 0) {
322                 /*
323                  * If we have just been running VFP instructions we will
324                  * need to save the state to memcpy it below.
325                  */
326                 vfp_save_state(td, curpcb);
327
328                 memcpy(mcp->mc_fpregs.fp_q, curpcb->pcb_vfp,
329                     sizeof(mcp->mc_fpregs));
330                 mcp->mc_fpregs.fp_cr = curpcb->pcb_fpcr;
331                 mcp->mc_fpregs.fp_sr = curpcb->pcb_fpsr;
332                 mcp->mc_fpregs.fp_flags = curpcb->pcb_fpflags;
333                 mcp->mc_flags |= _MC_FP_VALID;
334         }
335
336         critical_exit();
337 #endif
338 }
339
340 static void
341 set_fpcontext(struct thread *td, mcontext_t *mcp)
342 {
343 #ifdef VFP
344         struct pcb *curpcb;
345
346         critical_enter();
347
348         if ((mcp->mc_flags & _MC_FP_VALID) != 0) {
349                 curpcb = curthread->td_pcb;
350
351                 /*
352                  * Discard any vfp state for the current thread, we
353                  * are about to override it.
354                  */
355                 vfp_discard(td);
356
357                 memcpy(curpcb->pcb_vfp, mcp->mc_fpregs.fp_q,
358                     sizeof(mcp->mc_fpregs));
359                 curpcb->pcb_fpcr = mcp->mc_fpregs.fp_cr;
360                 curpcb->pcb_fpsr = mcp->mc_fpregs.fp_sr;
361                 curpcb->pcb_fpflags = mcp->mc_fpregs.fp_flags;
362         }
363
364         critical_exit();
365 #endif
366 }
367
368 void
369 cpu_idle(int busy)
370 {
371
372         spinlock_enter();
373         if (!busy)
374                 cpu_idleclock();
375         if (!sched_runnable())
376                 __asm __volatile(
377                     "dsb sy \n"
378                     "wfi    \n");
379         if (!busy)
380                 cpu_activeclock();
381         spinlock_exit();
382 }
383
384 void
385 cpu_halt(void)
386 {
387
388         /* We should have shutdown by now, if not enter a low power sleep */
389         intr_disable();
390         while (1) {
391                 __asm __volatile("wfi");
392         }
393 }
394
395 /*
396  * Flush the D-cache for non-DMA I/O so that the I-cache can
397  * be made coherent later.
398  */
399 void
400 cpu_flush_dcache(void *ptr, size_t len)
401 {
402
403         /* ARM64TODO TBD */
404 }
405
406 /* Get current clock frequency for the given CPU ID. */
407 int
408 cpu_est_clockrate(int cpu_id, uint64_t *rate)
409 {
410
411         panic("ARM64TODO: cpu_est_clockrate");
412 }
413
414 void
415 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
416 {
417
418         pcpu->pc_acpi_id = 0xffffffff;
419 }
420
421 void
422 spinlock_enter(void)
423 {
424         struct thread *td;
425         register_t daif;
426
427         td = curthread;
428         if (td->td_md.md_spinlock_count == 0) {
429                 daif = intr_disable();
430                 td->td_md.md_spinlock_count = 1;
431                 td->td_md.md_saved_daif = daif;
432         } else
433                 td->td_md.md_spinlock_count++;
434         critical_enter();
435 }
436
437 void
438 spinlock_exit(void)
439 {
440         struct thread *td;
441         register_t daif;
442
443         td = curthread;
444         critical_exit();
445         daif = td->td_md.md_saved_daif;
446         td->td_md.md_spinlock_count--;
447         if (td->td_md.md_spinlock_count == 0)
448                 intr_restore(daif);
449 }
450
451 #ifndef _SYS_SYSPROTO_H_
452 struct sigreturn_args {
453         ucontext_t *ucp;
454 };
455 #endif
456
457 int
458 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
459 {
460         ucontext_t uc;
461         uint32_t spsr;
462
463         if (uap == NULL)
464                 return (EFAULT);
465         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
466                 return (EFAULT);
467
468         spsr = uc.uc_mcontext.mc_gpregs.gp_spsr;
469         if ((spsr & PSR_M_MASK) != PSR_M_EL0t ||
470             (spsr & (PSR_F | PSR_I | PSR_A | PSR_D)) != 0)
471                 return (EINVAL); 
472
473         set_mcontext(td, &uc.uc_mcontext);
474         set_fpcontext(td, &uc.uc_mcontext);
475
476         /* Restore signal mask. */
477         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
478
479         return (EJUSTRETURN);
480 }
481
482 /*
483  * Construct a PCB from a trapframe. This is called from kdb_trap() where
484  * we want to start a backtrace from the function that caused us to enter
485  * the debugger. We have the context in the trapframe, but base the trace
486  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
487  * enough for a backtrace.
488  */
489 void
490 makectx(struct trapframe *tf, struct pcb *pcb)
491 {
492         int i;
493
494         for (i = 0; i < PCB_LR; i++)
495                 pcb->pcb_x[i] = tf->tf_x[i];
496
497         pcb->pcb_x[PCB_LR] = tf->tf_lr;
498         pcb->pcb_pc = tf->tf_elr;
499         pcb->pcb_sp = tf->tf_sp;
500 }
501
502 void
503 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
504 {
505         struct thread *td;
506         struct proc *p;
507         struct trapframe *tf;
508         struct sigframe *fp, frame;
509         struct sigacts *psp;
510         struct sysentvec *sysent;
511         int code, onstack, sig;
512
513         td = curthread;
514         p = td->td_proc;
515         PROC_LOCK_ASSERT(p, MA_OWNED);
516
517         sig = ksi->ksi_signo;
518         code = ksi->ksi_code;
519         psp = p->p_sigacts;
520         mtx_assert(&psp->ps_mtx, MA_OWNED);
521
522         tf = td->td_frame;
523         onstack = sigonstack(tf->tf_sp);
524
525         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
526             catcher, sig);
527
528         /* Allocate and validate space for the signal handler context. */
529         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !onstack &&
530             SIGISMEMBER(psp->ps_sigonstack, sig)) {
531                 fp = (struct sigframe *)(td->td_sigstk.ss_sp +
532                     td->td_sigstk.ss_size);
533 #if defined(COMPAT_43)
534                 td->td_sigstk.ss_flags |= SS_ONSTACK;
535 #endif
536         } else {
537                 fp = (struct sigframe *)td->td_frame->tf_sp;
538         }
539
540         /* Make room, keeping the stack aligned */
541         fp--;
542         fp = (struct sigframe *)STACKALIGN(fp);
543
544         /* Fill in the frame to copy out */
545         get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
546         get_fpcontext(td, &frame.sf_uc.uc_mcontext);
547         frame.sf_si = ksi->ksi_info;
548         frame.sf_uc.uc_sigmask = *mask;
549         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) ?
550             ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
551         frame.sf_uc.uc_stack = td->td_sigstk;
552         mtx_unlock(&psp->ps_mtx);
553         PROC_UNLOCK(td->td_proc);
554
555         /* Copy the sigframe out to the user's stack. */
556         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
557                 /* Process has trashed its stack. Kill it. */
558                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
559                 PROC_LOCK(p);
560                 sigexit(td, SIGILL);
561         }
562
563         tf->tf_x[0]= sig;
564         tf->tf_x[1] = (register_t)&fp->sf_si;
565         tf->tf_x[2] = (register_t)&fp->sf_uc;
566
567         tf->tf_elr = (register_t)catcher;
568         tf->tf_sp = (register_t)fp;
569         sysent = p->p_sysent;
570         if (sysent->sv_sigcode_base != 0)
571                 tf->tf_lr = (register_t)sysent->sv_sigcode_base;
572         else
573                 tf->tf_lr = (register_t)(sysent->sv_psstrings -
574                     *(sysent->sv_szsigcode));
575
576         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_elr,
577             tf->tf_sp);
578
579         PROC_LOCK(p);
580         mtx_lock(&psp->ps_mtx);
581 }
582
583 static void
584 init_proc0(vm_offset_t kstack)
585 {
586         struct pcpu *pcpup = &__pcpu[0];
587
588         proc_linkup0(&proc0, &thread0);
589         thread0.td_kstack = kstack;
590         thread0.td_pcb = (struct pcb *)(thread0.td_kstack) - 1;
591         thread0.td_pcb->pcb_fpflags = 0;
592         thread0.td_pcb->pcb_vfpcpu = UINT_MAX;
593         thread0.td_frame = &proc0_tf;
594         pcpup->pc_curpcb = thread0.td_pcb;
595 }
596
597 typedef struct {
598         uint32_t type;
599         uint64_t phys_start;
600         uint64_t virt_start;
601         uint64_t num_pages;
602         uint64_t attr;
603 } EFI_MEMORY_DESCRIPTOR;
604
605 static int
606 add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
607     u_int *physmap_idxp)
608 {
609         u_int i, insert_idx, _physmap_idx;
610
611         _physmap_idx = *physmap_idxp;
612
613         if (length == 0)
614                 return (1);
615
616         /*
617          * Find insertion point while checking for overlap.  Start off by
618          * assuming the new entry will be added to the end.
619          */
620         insert_idx = _physmap_idx;
621         for (i = 0; i <= _physmap_idx; i += 2) {
622                 if (base < physmap[i + 1]) {
623                         if (base + length <= physmap[i]) {
624                                 insert_idx = i;
625                                 break;
626                         }
627                         if (boothowto & RB_VERBOSE)
628                                 printf(
629                     "Overlapping memory regions, ignoring second region\n");
630                         return (1);
631                 }
632         }
633
634         /* See if we can prepend to the next entry. */
635         if (insert_idx <= _physmap_idx &&
636             base + length == physmap[insert_idx]) {
637                 physmap[insert_idx] = base;
638                 return (1);
639         }
640
641         /* See if we can append to the previous entry. */
642         if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
643                 physmap[insert_idx - 1] += length;
644                 return (1);
645         }
646
647         _physmap_idx += 2;
648         *physmap_idxp = _physmap_idx;
649         if (_physmap_idx == PHYSMAP_SIZE) {
650                 printf(
651                 "Too many segments in the physical address map, giving up\n");
652                 return (0);
653         }
654
655         /*
656          * Move the last 'N' entries down to make room for the new
657          * entry if needed.
658          */
659         for (i = _physmap_idx; i > insert_idx; i -= 2) {
660                 physmap[i] = physmap[i - 2];
661                 physmap[i + 1] = physmap[i - 1];
662         }
663
664         /* Insert the new entry. */
665         physmap[insert_idx] = base;
666         physmap[insert_idx + 1] = base + length;
667         return (1);
668 }
669
670 #define efi_next_descriptor(ptr, size) \
671         ((struct efi_md *)(((uint8_t *) ptr) + size))
672
673 static void
674 add_efi_map_entries(struct efi_map_header *efihdr, vm_paddr_t *physmap,
675     u_int *physmap_idxp)
676 {
677         struct efi_md *map, *p;
678         const char *type;
679         size_t efisz;
680         int ndesc, i;
681
682         static const char *types[] = {
683                 "Reserved",
684                 "LoaderCode",
685                 "LoaderData",
686                 "BootServicesCode",
687                 "BootServicesData",
688                 "RuntimeServicesCode",
689                 "RuntimeServicesData",
690                 "ConventionalMemory",
691                 "UnusableMemory",
692                 "ACPIReclaimMemory",
693                 "ACPIMemoryNVS",
694                 "MemoryMappedIO",
695                 "MemoryMappedIOPortSpace",
696                 "PalCode"
697         };
698
699         /*
700          * Memory map data provided by UEFI via the GetMemoryMap
701          * Boot Services API.
702          */
703         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
704         map = (struct efi_md *)((uint8_t *)efihdr + efisz); 
705
706         if (efihdr->descriptor_size == 0)
707                 return;
708         ndesc = efihdr->memory_size / efihdr->descriptor_size;
709
710         if (boothowto & RB_VERBOSE)
711                 printf("%23s %12s %12s %8s %4s\n",
712                     "Type", "Physical", "Virtual", "#Pages", "Attr");
713
714         for (i = 0, p = map; i < ndesc; i++,
715             p = efi_next_descriptor(p, efihdr->descriptor_size)) {
716                 if (boothowto & RB_VERBOSE) {
717                         if (p->md_type <= EFI_MD_TYPE_PALCODE)
718                                 type = types[p->md_type];
719                         else
720                                 type = "<INVALID>";
721                         printf("%23s %012lx %12p %08lx ", type, p->md_phys,
722                             p->md_virt, p->md_pages);
723                         if (p->md_attr & EFI_MD_ATTR_UC)
724                                 printf("UC ");
725                         if (p->md_attr & EFI_MD_ATTR_WC)
726                                 printf("WC ");
727                         if (p->md_attr & EFI_MD_ATTR_WT)
728                                 printf("WT ");
729                         if (p->md_attr & EFI_MD_ATTR_WB)
730                                 printf("WB ");
731                         if (p->md_attr & EFI_MD_ATTR_UCE)
732                                 printf("UCE ");
733                         if (p->md_attr & EFI_MD_ATTR_WP)
734                                 printf("WP ");
735                         if (p->md_attr & EFI_MD_ATTR_RP)
736                                 printf("RP ");
737                         if (p->md_attr & EFI_MD_ATTR_XP)
738                                 printf("XP ");
739                         if (p->md_attr & EFI_MD_ATTR_RT)
740                                 printf("RUNTIME");
741                         printf("\n");
742                 }
743
744                 switch (p->md_type) {
745                 case EFI_MD_TYPE_CODE:
746                 case EFI_MD_TYPE_DATA:
747                 case EFI_MD_TYPE_BS_CODE:
748                 case EFI_MD_TYPE_BS_DATA:
749                 case EFI_MD_TYPE_FREE:
750                         /*
751                          * We're allowed to use any entry with these types.
752                          */
753                         break;
754                 default:
755                         continue;
756                 }
757
758                 if (!add_physmap_entry(p->md_phys, (p->md_pages * PAGE_SIZE),
759                     physmap, physmap_idxp))
760                         break;
761         }
762 }
763
764 #ifdef FDT
765 static void
766 try_load_dtb(caddr_t kmdp)
767 {
768         vm_offset_t dtbp;
769
770         dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
771         if (dtbp == (vm_offset_t)NULL) {
772                 printf("ERROR loading DTB\n");
773                 return;
774         }
775
776         if (OF_install(OFW_FDT, 0) == FALSE)
777                 panic("Cannot install FDT");
778
779         if (OF_init((void *)dtbp) != 0)
780                 panic("OF_init failed with the found device tree");
781 }
782 #endif
783
784 static void
785 cache_setup(void)
786 {
787         int dcache_line_shift, icache_line_shift;
788         uint32_t ctr_el0;
789
790         ctr_el0 = READ_SPECIALREG(ctr_el0);
791
792         /* Read the log2 words in each D cache line */
793         dcache_line_shift = CTR_DLINE_SIZE(ctr_el0);
794         /* Get the D cache line size */
795         dcache_line_size = sizeof(int) << dcache_line_shift;
796
797         /* And the same for the I cache */
798         icache_line_shift = CTR_ILINE_SIZE(ctr_el0);
799         icache_line_size = sizeof(int) << icache_line_shift;
800
801         idcache_line_size = MIN(dcache_line_size, icache_line_size);
802 }
803
804 void
805 initarm(struct arm64_bootparams *abp)
806 {
807         struct efi_map_header *efihdr;
808         struct pcpu *pcpup;
809         vm_offset_t lastaddr;
810         caddr_t kmdp;
811         vm_paddr_t mem_len;
812         int i;
813
814         /* Set the module data location */
815         preload_metadata = (caddr_t)(uintptr_t)(abp->modulep);
816
817         /* Find the kernel address */
818         kmdp = preload_search_by_type("elf kernel");
819         if (kmdp == NULL)
820                 kmdp = preload_search_by_type("elf64 kernel");
821
822         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
823         init_static_kenv(MD_FETCH(kmdp, MODINFOMD_ENVP, char *), 0);
824
825 #ifdef FDT
826         try_load_dtb(kmdp);
827 #endif
828
829         /* Find the address to start allocating from */
830         lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
831
832         /* Load the physical memory ranges */
833         physmap_idx = 0;
834         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
835             MODINFO_METADATA | MODINFOMD_EFI_MAP);
836         add_efi_map_entries(efihdr, physmap, &physmap_idx);
837
838         /* Print the memory map */
839         mem_len = 0;
840         for (i = 0; i < physmap_idx; i += 2) {
841                 dump_avail[i] = physmap[i];
842                 dump_avail[i + 1] = physmap[i + 1];
843                 mem_len += physmap[i + 1] - physmap[i];
844         }
845         dump_avail[i] = 0;
846         dump_avail[i + 1] = 0;
847
848         /* Set the pcpu data, this is needed by pmap_bootstrap */
849         pcpup = &__pcpu[0];
850         pcpu_init(pcpup, 0, sizeof(struct pcpu));
851
852         /*
853          * Set the pcpu pointer with a backup in tpidr_el1 to be
854          * loaded when entering the kernel from userland.
855          */
856         __asm __volatile(
857             "mov x18, %0 \n"
858             "msr tpidr_el1, %0" :: "r"(pcpup));
859
860         PCPU_SET(curthread, &thread0);
861
862         /* Do basic tuning, hz etc */
863         init_param1();
864
865         cache_setup();
866
867         /* Bootstrap enough of pmap  to enter the kernel proper */
868         pmap_bootstrap(abp->kern_l1pt, KERNBASE - abp->kern_delta,
869             lastaddr - KERNBASE);
870
871         arm_devmap_bootstrap(0, NULL);
872
873         cninit();
874
875         init_proc0(abp->kern_stack);
876         msgbufinit(msgbufp, msgbufsize);
877         mutex_init();
878         init_param2(physmem);
879
880         dbg_monitor_init();
881         kdb_init();
882
883         early_boot = 0;
884 }
885
886 uint32_t (*arm_cpu_fill_vdso_timehands)(struct vdso_timehands *,
887     struct timecounter *);
888
889 uint32_t
890 cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc)
891 {
892
893         return (arm_cpu_fill_vdso_timehands != NULL ?
894             arm_cpu_fill_vdso_timehands(vdso_th, tc) : 0);
895 }
896
897 #ifdef DDB
898 #include <ddb/ddb.h>
899
900 DB_SHOW_COMMAND(specialregs, db_show_spregs)
901 {
902 #define PRINT_REG(reg)  \
903     db_printf(__STRING(reg) " = %#016lx\n", READ_SPECIALREG(reg))
904
905         PRINT_REG(actlr_el1);
906         PRINT_REG(afsr0_el1);
907         PRINT_REG(afsr1_el1);
908         PRINT_REG(aidr_el1);
909         PRINT_REG(amair_el1);
910         PRINT_REG(ccsidr_el1);
911         PRINT_REG(clidr_el1);
912         PRINT_REG(contextidr_el1);
913         PRINT_REG(cpacr_el1);
914         PRINT_REG(csselr_el1);
915         PRINT_REG(ctr_el0);
916         PRINT_REG(currentel);
917         PRINT_REG(daif);
918         PRINT_REG(dczid_el0);
919         PRINT_REG(elr_el1);
920         PRINT_REG(esr_el1);
921         PRINT_REG(far_el1);
922 #if 0
923         /* ARM64TODO: Enable VFP before reading floating-point registers */
924         PRINT_REG(fpcr);
925         PRINT_REG(fpsr);
926 #endif
927         PRINT_REG(id_aa64afr0_el1);
928         PRINT_REG(id_aa64afr1_el1);
929         PRINT_REG(id_aa64dfr0_el1);
930         PRINT_REG(id_aa64dfr1_el1);
931         PRINT_REG(id_aa64isar0_el1);
932         PRINT_REG(id_aa64isar1_el1);
933         PRINT_REG(id_aa64pfr0_el1);
934         PRINT_REG(id_aa64pfr1_el1);
935         PRINT_REG(id_afr0_el1);
936         PRINT_REG(id_dfr0_el1);
937         PRINT_REG(id_isar0_el1);
938         PRINT_REG(id_isar1_el1);
939         PRINT_REG(id_isar2_el1);
940         PRINT_REG(id_isar3_el1);
941         PRINT_REG(id_isar4_el1);
942         PRINT_REG(id_isar5_el1);
943         PRINT_REG(id_mmfr0_el1);
944         PRINT_REG(id_mmfr1_el1);
945         PRINT_REG(id_mmfr2_el1);
946         PRINT_REG(id_mmfr3_el1);
947 #if 0
948         /* Missing from llvm */
949         PRINT_REG(id_mmfr4_el1);
950 #endif
951         PRINT_REG(id_pfr0_el1);
952         PRINT_REG(id_pfr1_el1);
953         PRINT_REG(isr_el1);
954         PRINT_REG(mair_el1);
955         PRINT_REG(midr_el1);
956         PRINT_REG(mpidr_el1);
957         PRINT_REG(mvfr0_el1);
958         PRINT_REG(mvfr1_el1);
959         PRINT_REG(mvfr2_el1);
960         PRINT_REG(revidr_el1);
961         PRINT_REG(sctlr_el1);
962         PRINT_REG(sp_el0);
963         PRINT_REG(spsel);
964         PRINT_REG(spsr_el1);
965         PRINT_REG(tcr_el1);
966         PRINT_REG(tpidr_el0);
967         PRINT_REG(tpidr_el1);
968         PRINT_REG(tpidrro_el0);
969         PRINT_REG(ttbr0_el1);
970         PRINT_REG(ttbr1_el1);
971         PRINT_REG(vbar_el1);
972 #undef PRINT_REG
973 }
974
975 DB_SHOW_COMMAND(vtop, db_show_vtop)
976 {
977         uint64_t phys;
978
979         if (have_addr) {
980                 phys = arm64_address_translate_s1e1r(addr);
981                 db_printf("Physical address reg: 0x%016lx\n", phys);
982         } else
983                 db_printf("show vtop <virt_addr>\n");
984 }
985 #endif