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