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