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