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