machdep.c revision 298627
1/*	$NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $	*/
2
3/*-
4 * Copyright (c) 2004 Olivier Houchard
5 * Copyright (c) 1994-1998 Mark Brinicombe.
6 * Copyright (c) 1994 Brini.
7 * All rights reserved.
8 *
9 * This code is derived from software written for Brini by Mark Brinicombe
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by Mark Brinicombe
22 *	for the NetBSD Project.
23 * 4. The name of the company nor the name of the author may be used to
24 *    endorse or promote products derived from this software without specific
25 *    prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * Machine dependant functions for kernel setup
40 *
41 * Created      : 17/09/94
42 * Updated	: 18/04/01 updated for new wscons
43 */
44
45#include "opt_compat.h"
46#include "opt_ddb.h"
47#include "opt_kstack_pages.h"
48#include "opt_platform.h"
49#include "opt_sched.h"
50#include "opt_timer.h"
51
52#include <sys/cdefs.h>
53__FBSDID("$FreeBSD: head/sys/arm/arm/machdep.c 298627 2016-04-26 11:53:37Z br $");
54
55#include <sys/param.h>
56#include <sys/proc.h>
57#include <sys/systm.h>
58#include <sys/bio.h>
59#include <sys/buf.h>
60#include <sys/bus.h>
61#include <sys/cons.h>
62#include <sys/cpu.h>
63#include <sys/ctype.h>
64#include <sys/devmap.h>
65#include <sys/efi.h>
66#include <sys/exec.h>
67#include <sys/imgact.h>
68#include <sys/kdb.h>
69#include <sys/kernel.h>
70#include <sys/ktr.h>
71#include <sys/linker.h>
72#include <sys/lock.h>
73#include <sys/malloc.h>
74#include <sys/msgbuf.h>
75#include <sys/mutex.h>
76#include <sys/pcpu.h>
77#include <sys/ptrace.h>
78#include <sys/reboot.h>
79#include <sys/boot.h>
80#include <sys/rwlock.h>
81#include <sys/sched.h>
82#include <sys/signalvar.h>
83#include <sys/syscallsubr.h>
84#include <sys/sysctl.h>
85#include <sys/sysent.h>
86#include <sys/sysproto.h>
87#include <sys/uio.h>
88#include <sys/vdso.h>
89
90#include <vm/vm.h>
91#include <vm/pmap.h>
92#include <vm/vm_map.h>
93#include <vm/vm_object.h>
94#include <vm/vm_page.h>
95#include <vm/vm_pager.h>
96
97#include <machine/acle-compat.h>
98#include <machine/armreg.h>
99#include <machine/atags.h>
100#include <machine/cpu.h>
101#include <machine/cpuinfo.h>
102#include <machine/debug_monitor.h>
103#include <machine/db_machdep.h>
104#include <machine/frame.h>
105#include <machine/intr.h>
106#include <machine/machdep.h>
107#include <machine/md_var.h>
108#include <machine/metadata.h>
109#include <machine/pcb.h>
110#include <machine/physmem.h>
111#include <machine/platform.h>
112#include <machine/reg.h>
113#include <machine/trap.h>
114#include <machine/undefined.h>
115#include <machine/vfp.h>
116#include <machine/vmparam.h>
117#include <machine/sysarch.h>
118
119#ifdef FDT
120#include <contrib/libfdt/libfdt.h>
121#include <dev/fdt/fdt_common.h>
122#include <dev/ofw/openfirm.h>
123#endif
124
125#ifdef DDB
126#include <ddb/ddb.h>
127
128#if __ARM_ARCH >= 6
129
130DB_SHOW_COMMAND(cp15, db_show_cp15)
131{
132	u_int reg;
133
134	reg = cp15_midr_get();
135	db_printf("Cpu ID: 0x%08x\n", reg);
136	reg = cp15_ctr_get();
137	db_printf("Current Cache Lvl ID: 0x%08x\n",reg);
138
139	reg = cp15_sctlr_get();
140	db_printf("Ctrl: 0x%08x\n",reg);
141	reg = cp15_actlr_get();
142	db_printf("Aux Ctrl: 0x%08x\n",reg);
143
144	reg = cp15_id_pfr0_get();
145	db_printf("Processor Feat 0: 0x%08x\n", reg);
146	reg = cp15_id_pfr1_get();
147	db_printf("Processor Feat 1: 0x%08x\n", reg);
148	reg = cp15_id_dfr0_get();
149	db_printf("Debug Feat 0: 0x%08x\n", reg);
150	reg = cp15_id_afr0_get();
151	db_printf("Auxiliary Feat 0: 0x%08x\n", reg);
152	reg = cp15_id_mmfr0_get();
153	db_printf("Memory Model Feat 0: 0x%08x\n", reg);
154	reg = cp15_id_mmfr1_get();
155	db_printf("Memory Model Feat 1: 0x%08x\n", reg);
156	reg = cp15_id_mmfr2_get();
157	db_printf("Memory Model Feat 2: 0x%08x\n", reg);
158	reg = cp15_id_mmfr3_get();
159	db_printf("Memory Model Feat 3: 0x%08x\n", reg);
160	reg = cp15_ttbr_get();
161	db_printf("TTB0: 0x%08x\n", reg);
162}
163
164DB_SHOW_COMMAND(vtop, db_show_vtop)
165{
166	u_int reg;
167
168	if (have_addr) {
169		cp15_ats1cpr_set(addr);
170		reg = cp15_par_get();
171		db_printf("Physical address reg: 0x%08x\n",reg);
172	} else
173		db_printf("show vtop <virt_addr>\n");
174}
175#endif /* __ARM_ARCH >= 6 */
176#endif /* DDB */
177
178#ifdef DEBUG
179#define	debugf(fmt, args...) printf(fmt, ##args)
180#else
181#define	debugf(fmt, args...)
182#endif
183
184#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
185    defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) || \
186    defined(COMPAT_FREEBSD9)
187#error FreeBSD/arm doesn't provide compatibility with releases prior to 10
188#endif
189
190struct pcpu __pcpu[MAXCPU];
191struct pcpu *pcpup = &__pcpu[0];
192
193static struct trapframe proc0_tf;
194uint32_t cpu_reset_address = 0;
195int cold = 1;
196vm_offset_t vector_page;
197
198int (*_arm_memcpy)(void *, void *, int, int) = NULL;
199int (*_arm_bzero)(void *, int, int) = NULL;
200int _min_memcpy_size = 0;
201int _min_bzero_size = 0;
202
203extern int *end;
204
205#ifdef FDT
206static char *loader_envp;
207
208vm_paddr_t pmap_pa;
209
210#if __ARM_ARCH >= 6
211vm_offset_t systempage;
212vm_offset_t irqstack;
213vm_offset_t undstack;
214vm_offset_t abtstack;
215#else
216/*
217 * This is the number of L2 page tables required for covering max
218 * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
219 * stacks etc.), uprounded to be divisible by 4.
220 */
221#define KERNEL_PT_MAX	78
222
223static struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
224
225struct pv_addr systempage;
226static struct pv_addr msgbufpv;
227struct pv_addr irqstack;
228struct pv_addr undstack;
229struct pv_addr abtstack;
230static struct pv_addr kernelstack;
231#endif
232#endif
233
234#if defined(LINUX_BOOT_ABI)
235#define LBABI_MAX_BANKS	10
236
237#define CMDLINE_GUARD "FreeBSD:"
238uint32_t board_id;
239struct arm_lbabi_tag *atag_list;
240char linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
241char atags[LBABI_MAX_COMMAND_LINE * 2];
242uint32_t memstart[LBABI_MAX_BANKS];
243uint32_t memsize[LBABI_MAX_BANKS];
244uint32_t membanks;
245#endif
246
247static uint32_t board_revision;
248/* hex representation of uint64_t */
249static char board_serial[32];
250
251SYSCTL_NODE(_hw, OID_AUTO, board, CTLFLAG_RD, 0, "Board attributes");
252SYSCTL_UINT(_hw_board, OID_AUTO, revision, CTLFLAG_RD,
253    &board_revision, 0, "Board revision");
254SYSCTL_STRING(_hw_board, OID_AUTO, serial, CTLFLAG_RD,
255    board_serial, 0, "Board serial");
256
257int vfp_exists;
258SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
259    &vfp_exists, 0, "Floating point support enabled");
260
261void
262board_set_serial(uint64_t serial)
263{
264
265	snprintf(board_serial, sizeof(board_serial)-1,
266		    "%016jx", serial);
267}
268
269void
270board_set_revision(uint32_t revision)
271{
272
273	board_revision = revision;
274}
275
276void
277sendsig(catcher, ksi, mask)
278	sig_t catcher;
279	ksiginfo_t *ksi;
280	sigset_t *mask;
281{
282	struct thread *td;
283	struct proc *p;
284	struct trapframe *tf;
285	struct sigframe *fp, frame;
286	struct sigacts *psp;
287	struct sysentvec *sysent;
288	int onstack;
289	int sig;
290	int code;
291
292	td = curthread;
293	p = td->td_proc;
294	PROC_LOCK_ASSERT(p, MA_OWNED);
295	sig = ksi->ksi_signo;
296	code = ksi->ksi_code;
297	psp = p->p_sigacts;
298	mtx_assert(&psp->ps_mtx, MA_OWNED);
299	tf = td->td_frame;
300	onstack = sigonstack(tf->tf_usr_sp);
301
302	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
303	    catcher, sig);
304
305	/* Allocate and validate space for the signal handler context. */
306	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
307	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
308		fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
309		    td->td_sigstk.ss_size);
310#if defined(COMPAT_43)
311		td->td_sigstk.ss_flags |= SS_ONSTACK;
312#endif
313	} else
314		fp = (struct sigframe *)td->td_frame->tf_usr_sp;
315
316	/* make room on the stack */
317	fp--;
318
319	/* make the stack aligned */
320	fp = (struct sigframe *)STACKALIGN(fp);
321	/* Populate the siginfo frame. */
322	get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
323	frame.sf_si = ksi->ksi_info;
324	frame.sf_uc.uc_sigmask = *mask;
325	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
326	    ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
327	frame.sf_uc.uc_stack = td->td_sigstk;
328	mtx_unlock(&psp->ps_mtx);
329	PROC_UNLOCK(td->td_proc);
330
331	/* Copy the sigframe out to the user's stack. */
332	if (copyout(&frame, fp, sizeof(*fp)) != 0) {
333		/* Process has trashed its stack. Kill it. */
334		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
335		PROC_LOCK(p);
336		sigexit(td, SIGILL);
337	}
338
339	/*
340	 * Build context to run handler in.  We invoke the handler
341	 * directly, only returning via the trampoline.  Note the
342	 * trampoline version numbers are coordinated with machine-
343	 * dependent code in libc.
344	 */
345
346	tf->tf_r0 = sig;
347	tf->tf_r1 = (register_t)&fp->sf_si;
348	tf->tf_r2 = (register_t)&fp->sf_uc;
349
350	/* the trampoline uses r5 as the uc address */
351	tf->tf_r5 = (register_t)&fp->sf_uc;
352	tf->tf_pc = (register_t)catcher;
353	tf->tf_usr_sp = (register_t)fp;
354	sysent = p->p_sysent;
355	if (sysent->sv_sigcode_base != 0)
356		tf->tf_usr_lr = (register_t)sysent->sv_sigcode_base;
357	else
358		tf->tf_usr_lr = (register_t)(sysent->sv_psstrings -
359		    *(sysent->sv_szsigcode));
360	/* Set the mode to enter in the signal handler */
361#if __ARM_ARCH >= 7
362	if ((register_t)catcher & 1)
363		tf->tf_spsr |= PSR_T;
364	else
365		tf->tf_spsr &= ~PSR_T;
366#endif
367
368	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
369	    tf->tf_usr_sp);
370
371	PROC_LOCK(p);
372	mtx_lock(&psp->ps_mtx);
373}
374
375struct kva_md_info kmi;
376
377/*
378 * arm32_vector_init:
379 *
380 *	Initialize the vector page, and select whether or not to
381 *	relocate the vectors.
382 *
383 *	NOTE: We expect the vector page to be mapped at its expected
384 *	destination.
385 */
386
387extern unsigned int page0[], page0_data[];
388void
389arm_vector_init(vm_offset_t va, int which)
390{
391	unsigned int *vectors = (int *) va;
392	unsigned int *vectors_data = vectors + (page0_data - page0);
393	int vec;
394
395	/*
396	 * Loop through the vectors we're taking over, and copy the
397	 * vector's insn and data word.
398	 */
399	for (vec = 0; vec < ARM_NVEC; vec++) {
400		if ((which & (1 << vec)) == 0) {
401			/* Don't want to take over this vector. */
402			continue;
403		}
404		vectors[vec] = page0[vec];
405		vectors_data[vec] = page0_data[vec];
406	}
407
408	/* Now sync the vectors. */
409	icache_sync(va, (ARM_NVEC * 2) * sizeof(u_int));
410
411	vector_page = va;
412
413	if (va == ARM_VECTORS_HIGH) {
414		/*
415		 * Assume the MD caller knows what it's doing here, and
416		 * really does want the vector page relocated.
417		 *
418		 * Note: This has to be done here (and not just in
419		 * cpu_setup()) because the vector page needs to be
420		 * accessible *before* cpu_startup() is called.
421		 * Think ddb(9) ...
422		 *
423		 * NOTE: If the CPU control register is not readable,
424		 * this will totally fail!  We'll just assume that
425		 * any system that has high vector support has a
426		 * readable CPU control register, for now.  If we
427		 * ever encounter one that does not, we'll have to
428		 * rethink this.
429		 */
430		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
431	}
432}
433
434static void
435cpu_startup(void *dummy)
436{
437	struct pcb *pcb = thread0.td_pcb;
438	const unsigned int mbyte = 1024 * 1024;
439#if __ARM_ARCH < 6 && !defined(ARM_CACHE_LOCK_ENABLE)
440	vm_page_t m;
441#endif
442
443	identify_arm_cpu();
444
445	vm_ksubmap_init(&kmi);
446
447	/*
448	 * Display the RAM layout.
449	 */
450	printf("real memory  = %ju (%ju MB)\n",
451	    (uintmax_t)arm32_ptob(realmem),
452	    (uintmax_t)arm32_ptob(realmem) / mbyte);
453	printf("avail memory = %ju (%ju MB)\n",
454	    (uintmax_t)arm32_ptob(vm_cnt.v_free_count),
455	    (uintmax_t)arm32_ptob(vm_cnt.v_free_count) / mbyte);
456	if (bootverbose) {
457		arm_physmem_print_tables();
458		devmap_print_table();
459	}
460
461	bufinit();
462	vm_pager_bufferinit();
463	pcb->pcb_regs.sf_sp = (u_int)thread0.td_kstack +
464	    USPACE_SVC_STACK_TOP;
465	pmap_set_pcb_pagedir(kernel_pmap, pcb);
466#if __ARM_ARCH < 6
467	vector_page_setprot(VM_PROT_READ);
468	pmap_postinit();
469#ifdef ARM_CACHE_LOCK_ENABLE
470	pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
471	arm_lock_cache_line(ARM_TP_ADDRESS);
472#else
473	m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
474	pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
475#endif
476	*(uint32_t *)ARM_RAS_START = 0;
477	*(uint32_t *)ARM_RAS_END = 0xffffffff;
478#endif
479}
480
481SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
482
483/*
484 * Flush the D-cache for non-DMA I/O so that the I-cache can
485 * be made coherent later.
486 */
487void
488cpu_flush_dcache(void *ptr, size_t len)
489{
490
491	dcache_wb_poc((vm_offset_t)ptr, (vm_paddr_t)vtophys(ptr), len);
492}
493
494/* Get current clock frequency for the given cpu id. */
495int
496cpu_est_clockrate(int cpu_id, uint64_t *rate)
497{
498
499	return (ENXIO);
500}
501
502void
503cpu_idle(int busy)
504{
505
506	CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu);
507	spinlock_enter();
508#ifndef NO_EVENTTIMERS
509	if (!busy)
510		cpu_idleclock();
511#endif
512	if (!sched_runnable())
513		cpu_sleep(0);
514#ifndef NO_EVENTTIMERS
515	if (!busy)
516		cpu_activeclock();
517#endif
518	spinlock_exit();
519	CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu);
520}
521
522int
523cpu_idle_wakeup(int cpu)
524{
525
526	return (0);
527}
528
529/*
530 * Most ARM platforms don't need to do anything special to init their clocks
531 * (they get intialized during normal device attachment), and by not defining a
532 * cpu_initclocks() function they get this generic one.  Any platform that needs
533 * to do something special can just provide their own implementation, which will
534 * override this one due to the weak linkage.
535 */
536void
537arm_generic_initclocks(void)
538{
539
540#ifndef NO_EVENTTIMERS
541#ifdef SMP
542	if (PCPU_GET(cpuid) == 0)
543		cpu_initclocks_bsp();
544	else
545		cpu_initclocks_ap();
546#else
547	cpu_initclocks_bsp();
548#endif
549#endif
550}
551__weak_reference(arm_generic_initclocks, cpu_initclocks);
552
553int
554fill_regs(struct thread *td, struct reg *regs)
555{
556	struct trapframe *tf = td->td_frame;
557	bcopy(&tf->tf_r0, regs->r, sizeof(regs->r));
558	regs->r_sp = tf->tf_usr_sp;
559	regs->r_lr = tf->tf_usr_lr;
560	regs->r_pc = tf->tf_pc;
561	regs->r_cpsr = tf->tf_spsr;
562	return (0);
563}
564int
565fill_fpregs(struct thread *td, struct fpreg *regs)
566{
567	bzero(regs, sizeof(*regs));
568	return (0);
569}
570
571int
572set_regs(struct thread *td, struct reg *regs)
573{
574	struct trapframe *tf = td->td_frame;
575
576	bcopy(regs->r, &tf->tf_r0, sizeof(regs->r));
577	tf->tf_usr_sp = regs->r_sp;
578	tf->tf_usr_lr = regs->r_lr;
579	tf->tf_pc = regs->r_pc;
580	tf->tf_spsr &=  ~PSR_FLAGS;
581	tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
582	return (0);
583}
584
585int
586set_fpregs(struct thread *td, struct fpreg *regs)
587{
588	return (0);
589}
590
591int
592fill_dbregs(struct thread *td, struct dbreg *regs)
593{
594	return (0);
595}
596int
597set_dbregs(struct thread *td, struct dbreg *regs)
598{
599	return (0);
600}
601
602
603static int
604ptrace_read_int(struct thread *td, vm_offset_t addr, uint32_t *v)
605{
606
607	if (proc_readmem(td, td->td_proc, addr, v, sizeof(*v)) != sizeof(*v))
608		return (ENOMEM);
609	return (0);
610}
611
612static int
613ptrace_write_int(struct thread *td, vm_offset_t addr, uint32_t v)
614{
615
616	if (proc_writemem(td, td->td_proc, addr, &v, sizeof(v)) != sizeof(v))
617		return (ENOMEM);
618	return (0);
619}
620
621static u_int
622ptrace_get_usr_reg(void *cookie, int reg)
623{
624	int ret;
625	struct thread *td = cookie;
626
627	KASSERT(((reg >= 0) && (reg <= ARM_REG_NUM_PC)),
628	 ("reg is outside range"));
629
630	switch(reg) {
631	case ARM_REG_NUM_PC:
632		ret = td->td_frame->tf_pc;
633		break;
634	case ARM_REG_NUM_LR:
635		ret = td->td_frame->tf_usr_lr;
636		break;
637	case ARM_REG_NUM_SP:
638		ret = td->td_frame->tf_usr_sp;
639		break;
640	default:
641		ret = *((register_t*)&td->td_frame->tf_r0 + reg);
642		break;
643	}
644
645	return (ret);
646}
647
648static u_int
649ptrace_get_usr_int(void* cookie, vm_offset_t offset, u_int* val)
650{
651	struct thread *td = cookie;
652	u_int error;
653
654	error = ptrace_read_int(td, offset, val);
655
656	return (error);
657}
658
659/**
660 * This function parses current instruction opcode and decodes
661 * any possible jump (change in PC) which might occur after
662 * the instruction is executed.
663 *
664 * @param     td                Thread structure of analysed task
665 * @param     cur_instr         Currently executed instruction
666 * @param     alt_next_address  Pointer to the variable where
667 *                              the destination address of the
668 *                              jump instruction shall be stored.
669 *
670 * @return    <0>               when jump is possible
671 *            <EINVAL>          otherwise
672 */
673static int
674ptrace_get_alternative_next(struct thread *td, uint32_t cur_instr,
675    uint32_t *alt_next_address)
676{
677	int error;
678
679	if (inst_branch(cur_instr) || inst_call(cur_instr) ||
680	    inst_return(cur_instr)) {
681		error = arm_predict_branch(td, cur_instr, td->td_frame->tf_pc,
682		    alt_next_address, ptrace_get_usr_reg, ptrace_get_usr_int);
683
684		return (error);
685	}
686
687	return (EINVAL);
688}
689
690int
691ptrace_single_step(struct thread *td)
692{
693	struct proc *p;
694	int error, error_alt;
695	uint32_t cur_instr, alt_next = 0;
696
697	/* TODO: This needs to be updated for Thumb-2 */
698	if ((td->td_frame->tf_spsr & PSR_T) != 0)
699		return (EINVAL);
700
701	KASSERT(td->td_md.md_ptrace_instr == 0,
702	 ("Didn't clear single step"));
703	KASSERT(td->td_md.md_ptrace_instr_alt == 0,
704	 ("Didn't clear alternative single step"));
705	p = td->td_proc;
706	PROC_UNLOCK(p);
707
708	error = ptrace_read_int(td, td->td_frame->tf_pc,
709	    &cur_instr);
710	if (error)
711		goto out;
712
713	error = ptrace_read_int(td, td->td_frame->tf_pc + INSN_SIZE,
714	    &td->td_md.md_ptrace_instr);
715	if (error == 0) {
716		error = ptrace_write_int(td, td->td_frame->tf_pc + INSN_SIZE,
717		    PTRACE_BREAKPOINT);
718		if (error) {
719			td->td_md.md_ptrace_instr = 0;
720		} else {
721			td->td_md.md_ptrace_addr = td->td_frame->tf_pc +
722			    INSN_SIZE;
723		}
724	}
725
726	error_alt = ptrace_get_alternative_next(td, cur_instr, &alt_next);
727	if (error_alt == 0) {
728		error_alt = ptrace_read_int(td, alt_next,
729		    &td->td_md.md_ptrace_instr_alt);
730		if (error_alt) {
731			td->td_md.md_ptrace_instr_alt = 0;
732		} else {
733			error_alt = ptrace_write_int(td, alt_next,
734			    PTRACE_BREAKPOINT);
735			if (error_alt)
736				td->td_md.md_ptrace_instr_alt = 0;
737			else
738				td->td_md.md_ptrace_addr_alt = alt_next;
739		}
740	}
741
742out:
743	PROC_LOCK(p);
744	return ((error != 0) && (error_alt != 0));
745}
746
747int
748ptrace_clear_single_step(struct thread *td)
749{
750	struct proc *p;
751
752	/* TODO: This needs to be updated for Thumb-2 */
753	if ((td->td_frame->tf_spsr & PSR_T) != 0)
754		return (EINVAL);
755
756	if (td->td_md.md_ptrace_instr != 0) {
757		p = td->td_proc;
758		PROC_UNLOCK(p);
759		ptrace_write_int(td, td->td_md.md_ptrace_addr,
760		    td->td_md.md_ptrace_instr);
761		PROC_LOCK(p);
762		td->td_md.md_ptrace_instr = 0;
763	}
764
765	if (td->td_md.md_ptrace_instr_alt != 0) {
766		p = td->td_proc;
767		PROC_UNLOCK(p);
768		ptrace_write_int(td, td->td_md.md_ptrace_addr_alt,
769		    td->td_md.md_ptrace_instr_alt);
770		PROC_LOCK(p);
771		td->td_md.md_ptrace_instr_alt = 0;
772	}
773
774	return (0);
775}
776
777int
778ptrace_set_pc(struct thread *td, unsigned long addr)
779{
780	td->td_frame->tf_pc = addr;
781	return (0);
782}
783
784void
785cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
786{
787}
788
789void
790spinlock_enter(void)
791{
792	struct thread *td;
793	register_t cspr;
794
795	td = curthread;
796	if (td->td_md.md_spinlock_count == 0) {
797		cspr = disable_interrupts(PSR_I | PSR_F);
798		td->td_md.md_spinlock_count = 1;
799		td->td_md.md_saved_cspr = cspr;
800	} else
801		td->td_md.md_spinlock_count++;
802	critical_enter();
803}
804
805void
806spinlock_exit(void)
807{
808	struct thread *td;
809	register_t cspr;
810
811	td = curthread;
812	critical_exit();
813	cspr = td->td_md.md_saved_cspr;
814	td->td_md.md_spinlock_count--;
815	if (td->td_md.md_spinlock_count == 0)
816		restore_interrupts(cspr);
817}
818
819/*
820 * Clear registers on exec
821 */
822void
823exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
824{
825	struct trapframe *tf = td->td_frame;
826
827	memset(tf, 0, sizeof(*tf));
828	tf->tf_usr_sp = stack;
829	tf->tf_usr_lr = imgp->entry_addr;
830	tf->tf_svc_lr = 0x77777777;
831	tf->tf_pc = imgp->entry_addr;
832	tf->tf_spsr = PSR_USR32_MODE;
833}
834
835/*
836 * Get machine context.
837 */
838int
839get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
840{
841	struct trapframe *tf = td->td_frame;
842	__greg_t *gr = mcp->__gregs;
843
844	if (clear_ret & GET_MC_CLEAR_RET) {
845		gr[_REG_R0] = 0;
846		gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C;
847	} else {
848		gr[_REG_R0]   = tf->tf_r0;
849		gr[_REG_CPSR] = tf->tf_spsr;
850	}
851	gr[_REG_R1]   = tf->tf_r1;
852	gr[_REG_R2]   = tf->tf_r2;
853	gr[_REG_R3]   = tf->tf_r3;
854	gr[_REG_R4]   = tf->tf_r4;
855	gr[_REG_R5]   = tf->tf_r5;
856	gr[_REG_R6]   = tf->tf_r6;
857	gr[_REG_R7]   = tf->tf_r7;
858	gr[_REG_R8]   = tf->tf_r8;
859	gr[_REG_R9]   = tf->tf_r9;
860	gr[_REG_R10]  = tf->tf_r10;
861	gr[_REG_R11]  = tf->tf_r11;
862	gr[_REG_R12]  = tf->tf_r12;
863	gr[_REG_SP]   = tf->tf_usr_sp;
864	gr[_REG_LR]   = tf->tf_usr_lr;
865	gr[_REG_PC]   = tf->tf_pc;
866
867	return (0);
868}
869
870/*
871 * Set machine context.
872 *
873 * However, we don't set any but the user modifiable flags, and we won't
874 * touch the cs selector.
875 */
876int
877set_mcontext(struct thread *td, mcontext_t *mcp)
878{
879	struct trapframe *tf = td->td_frame;
880	const __greg_t *gr = mcp->__gregs;
881
882	tf->tf_r0 = gr[_REG_R0];
883	tf->tf_r1 = gr[_REG_R1];
884	tf->tf_r2 = gr[_REG_R2];
885	tf->tf_r3 = gr[_REG_R3];
886	tf->tf_r4 = gr[_REG_R4];
887	tf->tf_r5 = gr[_REG_R5];
888	tf->tf_r6 = gr[_REG_R6];
889	tf->tf_r7 = gr[_REG_R7];
890	tf->tf_r8 = gr[_REG_R8];
891	tf->tf_r9 = gr[_REG_R9];
892	tf->tf_r10 = gr[_REG_R10];
893	tf->tf_r11 = gr[_REG_R11];
894	tf->tf_r12 = gr[_REG_R12];
895	tf->tf_usr_sp = gr[_REG_SP];
896	tf->tf_usr_lr = gr[_REG_LR];
897	tf->tf_pc = gr[_REG_PC];
898	tf->tf_spsr = gr[_REG_CPSR];
899
900	return (0);
901}
902
903/*
904 * MPSAFE
905 */
906int
907sys_sigreturn(td, uap)
908	struct thread *td;
909	struct sigreturn_args /* {
910		const struct __ucontext *sigcntxp;
911	} */ *uap;
912{
913	ucontext_t uc;
914	int spsr;
915
916	if (uap == NULL)
917		return (EFAULT);
918	if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
919		return (EFAULT);
920	/*
921	 * Make sure the processor mode has not been tampered with and
922	 * interrupts have not been disabled.
923	 */
924	spsr = uc.uc_mcontext.__gregs[_REG_CPSR];
925	if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
926	    (spsr & (PSR_I | PSR_F)) != 0)
927		return (EINVAL);
928		/* Restore register context. */
929	set_mcontext(td, &uc.uc_mcontext);
930
931	/* Restore signal mask. */
932	kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
933
934	return (EJUSTRETURN);
935}
936
937
938/*
939 * Construct a PCB from a trapframe. This is called from kdb_trap() where
940 * we want to start a backtrace from the function that caused us to enter
941 * the debugger. We have the context in the trapframe, but base the trace
942 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
943 * enough for a backtrace.
944 */
945void
946makectx(struct trapframe *tf, struct pcb *pcb)
947{
948	pcb->pcb_regs.sf_r4 = tf->tf_r4;
949	pcb->pcb_regs.sf_r5 = tf->tf_r5;
950	pcb->pcb_regs.sf_r6 = tf->tf_r6;
951	pcb->pcb_regs.sf_r7 = tf->tf_r7;
952	pcb->pcb_regs.sf_r8 = tf->tf_r8;
953	pcb->pcb_regs.sf_r9 = tf->tf_r9;
954	pcb->pcb_regs.sf_r10 = tf->tf_r10;
955	pcb->pcb_regs.sf_r11 = tf->tf_r11;
956	pcb->pcb_regs.sf_r12 = tf->tf_r12;
957	pcb->pcb_regs.sf_pc = tf->tf_pc;
958	pcb->pcb_regs.sf_lr = tf->tf_usr_lr;
959	pcb->pcb_regs.sf_sp = tf->tf_usr_sp;
960}
961
962/*
963 * Fake up a boot descriptor table
964 */
965vm_offset_t
966fake_preload_metadata(struct arm_boot_params *abp __unused, void *dtb_ptr,
967    size_t dtb_size)
968{
969#ifdef DDB
970	vm_offset_t zstart = 0, zend = 0;
971#endif
972	vm_offset_t lastaddr;
973	int i = 0;
974	static uint32_t fake_preload[35];
975
976	fake_preload[i++] = MODINFO_NAME;
977	fake_preload[i++] = strlen("kernel") + 1;
978	strcpy((char*)&fake_preload[i++], "kernel");
979	i += 1;
980	fake_preload[i++] = MODINFO_TYPE;
981	fake_preload[i++] = strlen("elf kernel") + 1;
982	strcpy((char*)&fake_preload[i++], "elf kernel");
983	i += 2;
984	fake_preload[i++] = MODINFO_ADDR;
985	fake_preload[i++] = sizeof(vm_offset_t);
986	fake_preload[i++] = KERNVIRTADDR;
987	fake_preload[i++] = MODINFO_SIZE;
988	fake_preload[i++] = sizeof(uint32_t);
989	fake_preload[i++] = (uint32_t)&end - KERNVIRTADDR;
990#ifdef DDB
991	if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
992		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
993		fake_preload[i++] = sizeof(vm_offset_t);
994		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
995		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
996		fake_preload[i++] = sizeof(vm_offset_t);
997		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
998		lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
999		zend = lastaddr;
1000		zstart = *(uint32_t *)(KERNVIRTADDR + 4);
1001		db_fetch_ksymtab(zstart, zend);
1002	} else
1003#endif
1004		lastaddr = (vm_offset_t)&end;
1005	if (dtb_ptr != NULL) {
1006		/* Copy DTB to KVA space and insert it into module chain. */
1007		lastaddr = roundup(lastaddr, sizeof(int));
1008		fake_preload[i++] = MODINFO_METADATA | MODINFOMD_DTBP;
1009		fake_preload[i++] = sizeof(uint32_t);
1010		fake_preload[i++] = (uint32_t)lastaddr;
1011		memmove((void *)lastaddr, dtb_ptr, dtb_size);
1012		lastaddr += dtb_size;
1013		lastaddr = roundup(lastaddr, sizeof(int));
1014	}
1015	fake_preload[i++] = 0;
1016	fake_preload[i] = 0;
1017	preload_metadata = (void *)fake_preload;
1018
1019	init_static_kenv(NULL, 0);
1020
1021	return (lastaddr);
1022}
1023
1024void
1025pcpu0_init(void)
1026{
1027#if __ARM_ARCH >= 6
1028	set_curthread(&thread0);
1029#endif
1030	pcpu_init(pcpup, 0, sizeof(struct pcpu));
1031	PCPU_SET(curthread, &thread0);
1032}
1033
1034#if defined(LINUX_BOOT_ABI)
1035
1036/* Convert the U-Boot command line into FreeBSD kenv and boot options. */
1037static void
1038cmdline_set_env(char *cmdline, const char *guard)
1039{
1040	char *cmdline_next, *env;
1041	size_t size, guard_len;
1042	int i;
1043
1044	size = strlen(cmdline);
1045	/* Skip leading spaces. */
1046	for (; isspace(*cmdline) && (size > 0); cmdline++)
1047		size--;
1048
1049	/* Test and remove guard. */
1050	if (guard != NULL && guard[0] != '\0') {
1051		guard_len  =  strlen(guard);
1052		if (strncasecmp(cmdline, guard, guard_len) != 0)
1053			return;
1054		cmdline += guard_len;
1055		size -= guard_len;
1056	}
1057
1058	/* Skip leading spaces. */
1059	for (; isspace(*cmdline) && (size > 0); cmdline++)
1060		size--;
1061
1062	/* Replace ',' with '\0'. */
1063	/* TODO: implement escaping for ',' character. */
1064	cmdline_next = cmdline;
1065	while(strsep(&cmdline_next, ",") != NULL)
1066		;
1067	init_static_kenv(cmdline, 0);
1068	/* Parse boothowto. */
1069	for (i = 0; howto_names[i].ev != NULL; i++) {
1070		env = kern_getenv(howto_names[i].ev);
1071		if (env != NULL) {
1072			if (strtoul(env, NULL, 10) != 0)
1073				boothowto |= howto_names[i].mask;
1074			freeenv(env);
1075		}
1076	}
1077}
1078
1079vm_offset_t
1080linux_parse_boot_param(struct arm_boot_params *abp)
1081{
1082	struct arm_lbabi_tag *walker;
1083	uint32_t revision;
1084	uint64_t serial;
1085	int size;
1086	vm_offset_t lastaddr;
1087#ifdef FDT
1088	struct fdt_header *dtb_ptr;
1089	uint32_t dtb_size;
1090#endif
1091
1092	/*
1093	 * Linux boot ABI: r0 = 0, r1 is the board type (!= 0) and r2
1094	 * is atags or dtb pointer.  If all of these aren't satisfied,
1095	 * then punt. Unfortunately, it looks like DT enabled kernels
1096	 * doesn't uses board type and U-Boot delivers 0 in r1 for them.
1097	 */
1098	if (abp->abp_r0 != 0 || abp->abp_r2 == 0)
1099		return (0);
1100#ifdef FDT
1101	/* Test if r2 point to valid DTB. */
1102	dtb_ptr = (struct fdt_header *)abp->abp_r2;
1103	if (fdt_check_header(dtb_ptr) == 0) {
1104		dtb_size = fdt_totalsize(dtb_ptr);
1105		return (fake_preload_metadata(abp, dtb_ptr, dtb_size));
1106	}
1107#endif
1108
1109	board_id = abp->abp_r1;
1110	walker = (struct arm_lbabi_tag *)abp->abp_r2;
1111
1112	if (ATAG_TAG(walker) != ATAG_CORE)
1113		return 0;
1114
1115	atag_list = walker;
1116	while (ATAG_TAG(walker) != ATAG_NONE) {
1117		switch (ATAG_TAG(walker)) {
1118		case ATAG_CORE:
1119			break;
1120		case ATAG_MEM:
1121			arm_physmem_hardware_region(walker->u.tag_mem.start,
1122			    walker->u.tag_mem.size);
1123			break;
1124		case ATAG_INITRD2:
1125			break;
1126		case ATAG_SERIAL:
1127			serial = walker->u.tag_sn.high;
1128			serial <<= 32;
1129			serial |= walker->u.tag_sn.low;
1130			board_set_serial(serial);
1131			break;
1132		case ATAG_REVISION:
1133			revision = walker->u.tag_rev.rev;
1134			board_set_revision(revision);
1135			break;
1136		case ATAG_CMDLINE:
1137			size = ATAG_SIZE(walker) -
1138			    sizeof(struct arm_lbabi_header);
1139			size = min(size, LBABI_MAX_COMMAND_LINE);
1140			strncpy(linux_command_line, walker->u.tag_cmd.command,
1141			    size);
1142			linux_command_line[size] = '\0';
1143			break;
1144		default:
1145			break;
1146		}
1147		walker = ATAG_NEXT(walker);
1148	}
1149
1150	/* Save a copy for later */
1151	bcopy(atag_list, atags,
1152	    (char *)walker - (char *)atag_list + ATAG_SIZE(walker));
1153
1154	lastaddr = fake_preload_metadata(abp, NULL, 0);
1155	cmdline_set_env(linux_command_line, CMDLINE_GUARD);
1156	return lastaddr;
1157}
1158#endif
1159
1160#if defined(FREEBSD_BOOT_LOADER)
1161vm_offset_t
1162freebsd_parse_boot_param(struct arm_boot_params *abp)
1163{
1164	vm_offset_t lastaddr = 0;
1165	void *mdp;
1166	void *kmdp;
1167#ifdef DDB
1168	vm_offset_t ksym_start;
1169	vm_offset_t ksym_end;
1170#endif
1171
1172	/*
1173	 * Mask metadata pointer: it is supposed to be on page boundary. If
1174	 * the first argument (mdp) doesn't point to a valid address the
1175	 * bootloader must have passed us something else than the metadata
1176	 * ptr, so we give up.  Also give up if we cannot find metadta section
1177	 * the loader creates that we get all this data out of.
1178	 */
1179
1180	if ((mdp = (void *)(abp->abp_r0 & ~PAGE_MASK)) == NULL)
1181		return 0;
1182	preload_metadata = mdp;
1183	kmdp = preload_search_by_type("elf kernel");
1184	if (kmdp == NULL)
1185		return 0;
1186
1187	boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
1188	loader_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
1189	init_static_kenv(loader_envp, 0);
1190	lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
1191#ifdef DDB
1192	ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
1193	ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
1194	db_fetch_ksymtab(ksym_start, ksym_end);
1195#endif
1196	return lastaddr;
1197}
1198#endif
1199
1200vm_offset_t
1201default_parse_boot_param(struct arm_boot_params *abp)
1202{
1203	vm_offset_t lastaddr;
1204
1205#if defined(LINUX_BOOT_ABI)
1206	if ((lastaddr = linux_parse_boot_param(abp)) != 0)
1207		return lastaddr;
1208#endif
1209#if defined(FREEBSD_BOOT_LOADER)
1210	if ((lastaddr = freebsd_parse_boot_param(abp)) != 0)
1211		return lastaddr;
1212#endif
1213	/* Fall back to hardcoded metadata. */
1214	lastaddr = fake_preload_metadata(abp, NULL, 0);
1215
1216	return lastaddr;
1217}
1218
1219/*
1220 * Stub version of the boot parameter parsing routine.  We are
1221 * called early in initarm, before even VM has been initialized.
1222 * This routine needs to preserve any data that the boot loader
1223 * has passed in before the kernel starts to grow past the end
1224 * of the BSS, traditionally the place boot-loaders put this data.
1225 *
1226 * Since this is called so early, things that depend on the vm system
1227 * being setup (including access to some SoC's serial ports), about
1228 * all that can be done in this routine is to copy the arguments.
1229 *
1230 * This is the default boot parameter parsing routine.  Individual
1231 * kernels/boards can override this weak function with one of their
1232 * own.  We just fake metadata...
1233 */
1234__weak_reference(default_parse_boot_param, parse_boot_param);
1235
1236/*
1237 * Initialize proc0
1238 */
1239void
1240init_proc0(vm_offset_t kstack)
1241{
1242	proc_linkup0(&proc0, &thread0);
1243	thread0.td_kstack = kstack;
1244	thread0.td_pcb = (struct pcb *)
1245		(thread0.td_kstack + kstack_pages * PAGE_SIZE) - 1;
1246	thread0.td_pcb->pcb_flags = 0;
1247	thread0.td_pcb->pcb_vfpcpu = -1;
1248	thread0.td_pcb->pcb_vfpstate.fpscr = VFPSCR_DN;
1249	thread0.td_frame = &proc0_tf;
1250	pcpup->pc_curpcb = thread0.td_pcb;
1251}
1252
1253int
1254arm_predict_branch(void *cookie, u_int insn, register_t pc, register_t *new_pc,
1255    u_int (*fetch_reg)(void*, int), u_int (*read_int)(void*, vm_offset_t, u_int*))
1256{
1257	u_int addr, nregs, offset = 0;
1258	int error = 0;
1259
1260	switch ((insn >> 24) & 0xf) {
1261	case 0x2:	/* add pc, reg1, #value */
1262	case 0x0:	/* add pc, reg1, reg2, lsl #offset */
1263		addr = fetch_reg(cookie, (insn >> 16) & 0xf);
1264		if (((insn >> 16) & 0xf) == 15)
1265			addr += 8;
1266		if (insn & 0x0200000) {
1267			offset = (insn >> 7) & 0x1e;
1268			offset = (insn & 0xff) << (32 - offset) |
1269			    (insn & 0xff) >> offset;
1270		} else {
1271
1272			offset = fetch_reg(cookie, insn & 0x0f);
1273			if ((insn & 0x0000ff0) != 0x00000000) {
1274				if (insn & 0x10)
1275					nregs = fetch_reg(cookie,
1276					    (insn >> 8) & 0xf);
1277				else
1278					nregs = (insn >> 7) & 0x1f;
1279				switch ((insn >> 5) & 3) {
1280				case 0:
1281					/* lsl */
1282					offset = offset << nregs;
1283					break;
1284				case 1:
1285					/* lsr */
1286					offset = offset >> nregs;
1287					break;
1288				default:
1289					break; /* XXX */
1290				}
1291
1292			}
1293			*new_pc = addr + offset;
1294			return (0);
1295
1296		}
1297
1298	case 0xa:	/* b ... */
1299	case 0xb:	/* bl ... */
1300		addr = ((insn << 2) & 0x03ffffff);
1301		if (addr & 0x02000000)
1302			addr |= 0xfc000000;
1303		*new_pc = (pc + 8 + addr);
1304		return (0);
1305	case 0x7:	/* ldr pc, [pc, reg, lsl #2] */
1306		addr = fetch_reg(cookie, insn & 0xf);
1307		addr = pc + 8 + (addr << 2);
1308		error = read_int(cookie, addr, &addr);
1309		*new_pc = addr;
1310		return (error);
1311	case 0x1:	/* mov pc, reg */
1312		*new_pc = fetch_reg(cookie, insn & 0xf);
1313		return (0);
1314	case 0x4:
1315	case 0x5:	/* ldr pc, [reg] */
1316		addr = fetch_reg(cookie, (insn >> 16) & 0xf);
1317		/* ldr pc, [reg, #offset] */
1318		if (insn & (1 << 24))
1319			offset = insn & 0xfff;
1320		if (insn & 0x00800000)
1321			addr += offset;
1322		else
1323			addr -= offset;
1324		error = read_int(cookie, addr, &addr);
1325		*new_pc = addr;
1326
1327		return (error);
1328	case 0x8:	/* ldmxx reg, {..., pc} */
1329	case 0x9:
1330		addr = fetch_reg(cookie, (insn >> 16) & 0xf);
1331		nregs = (insn  & 0x5555) + ((insn  >> 1) & 0x5555);
1332		nregs = (nregs & 0x3333) + ((nregs >> 2) & 0x3333);
1333		nregs = (nregs + (nregs >> 4)) & 0x0f0f;
1334		nregs = (nregs + (nregs >> 8)) & 0x001f;
1335		switch ((insn >> 23) & 0x3) {
1336		case 0x0:	/* ldmda */
1337			addr = addr - 0;
1338			break;
1339		case 0x1:	/* ldmia */
1340			addr = addr + 0 + ((nregs - 1) << 2);
1341			break;
1342		case 0x2:	/* ldmdb */
1343			addr = addr - 4;
1344			break;
1345		case 0x3:	/* ldmib */
1346			addr = addr + 4 + ((nregs - 1) << 2);
1347			break;
1348		}
1349		error = read_int(cookie, addr, &addr);
1350		*new_pc = addr;
1351
1352		return (error);
1353	default:
1354		return (EINVAL);
1355	}
1356}
1357
1358#if __ARM_ARCH >= 6
1359void
1360set_stackptrs(int cpu)
1361{
1362
1363	set_stackptr(PSR_IRQ32_MODE,
1364	    irqstack + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1365	set_stackptr(PSR_ABT32_MODE,
1366	    abtstack + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1367	set_stackptr(PSR_UND32_MODE,
1368	    undstack + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1369}
1370#else
1371void
1372set_stackptrs(int cpu)
1373{
1374
1375	set_stackptr(PSR_IRQ32_MODE,
1376	    irqstack.pv_va + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1377	set_stackptr(PSR_ABT32_MODE,
1378	    abtstack.pv_va + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1379	set_stackptr(PSR_UND32_MODE,
1380	    undstack.pv_va + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1381}
1382#endif
1383
1384#ifdef EFI
1385#define efi_next_descriptor(ptr, size) \
1386	((struct efi_md *)(((uint8_t *) ptr) + size))
1387
1388static void
1389add_efi_map_entries(struct efi_map_header *efihdr, struct mem_region *mr,
1390    int *mrcnt)
1391{
1392	struct efi_md *map, *p;
1393	const char *type;
1394	size_t efisz, memory_size;
1395	int ndesc, i, j;
1396
1397	static const char *types[] = {
1398		"Reserved",
1399		"LoaderCode",
1400		"LoaderData",
1401		"BootServicesCode",
1402		"BootServicesData",
1403		"RuntimeServicesCode",
1404		"RuntimeServicesData",
1405		"ConventionalMemory",
1406		"UnusableMemory",
1407		"ACPIReclaimMemory",
1408		"ACPIMemoryNVS",
1409		"MemoryMappedIO",
1410		"MemoryMappedIOPortSpace",
1411		"PalCode"
1412	};
1413
1414	*mrcnt = 0;
1415
1416	/*
1417	 * Memory map data provided by UEFI via the GetMemoryMap
1418	 * Boot Services API.
1419	 */
1420	efisz = roundup2(sizeof(struct efi_map_header), 0x10);
1421	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
1422
1423	if (efihdr->descriptor_size == 0)
1424		return;
1425	ndesc = efihdr->memory_size / efihdr->descriptor_size;
1426
1427	if (boothowto & RB_VERBOSE)
1428		printf("%23s %12s %12s %8s %4s\n",
1429		    "Type", "Physical", "Virtual", "#Pages", "Attr");
1430
1431	memory_size = 0;
1432	for (i = 0, j = 0, p = map; i < ndesc; i++,
1433	    p = efi_next_descriptor(p, efihdr->descriptor_size)) {
1434		if (boothowto & RB_VERBOSE) {
1435			if (p->md_type <= EFI_MD_TYPE_PALCODE)
1436				type = types[p->md_type];
1437			else
1438				type = "<INVALID>";
1439			printf("%23s %012llx %12p %08llx ", type, p->md_phys,
1440			    p->md_virt, p->md_pages);
1441			if (p->md_attr & EFI_MD_ATTR_UC)
1442				printf("UC ");
1443			if (p->md_attr & EFI_MD_ATTR_WC)
1444				printf("WC ");
1445			if (p->md_attr & EFI_MD_ATTR_WT)
1446				printf("WT ");
1447			if (p->md_attr & EFI_MD_ATTR_WB)
1448				printf("WB ");
1449			if (p->md_attr & EFI_MD_ATTR_UCE)
1450				printf("UCE ");
1451			if (p->md_attr & EFI_MD_ATTR_WP)
1452				printf("WP ");
1453			if (p->md_attr & EFI_MD_ATTR_RP)
1454				printf("RP ");
1455			if (p->md_attr & EFI_MD_ATTR_XP)
1456				printf("XP ");
1457			if (p->md_attr & EFI_MD_ATTR_RT)
1458				printf("RUNTIME");
1459			printf("\n");
1460		}
1461
1462		switch (p->md_type) {
1463		case EFI_MD_TYPE_CODE:
1464		case EFI_MD_TYPE_DATA:
1465		case EFI_MD_TYPE_BS_CODE:
1466		case EFI_MD_TYPE_BS_DATA:
1467		case EFI_MD_TYPE_FREE:
1468			/*
1469			 * We're allowed to use any entry with these types.
1470			 */
1471			break;
1472		default:
1473			continue;
1474		}
1475
1476		j++;
1477		if (j >= FDT_MEM_REGIONS)
1478			break;
1479
1480		mr[j].mr_start = p->md_phys;
1481		mr[j].mr_size = p->md_pages * PAGE_SIZE;
1482		memory_size += mr[j].mr_size;
1483	}
1484
1485	*mrcnt = j;
1486}
1487#endif /* EFI */
1488
1489#ifdef FDT
1490static char *
1491kenv_next(char *cp)
1492{
1493
1494	if (cp != NULL) {
1495		while (*cp != 0)
1496			cp++;
1497		cp++;
1498		if (*cp == 0)
1499			cp = NULL;
1500	}
1501	return (cp);
1502}
1503
1504static void
1505print_kenv(void)
1506{
1507	char *cp;
1508
1509	debugf("loader passed (static) kenv:\n");
1510	if (loader_envp == NULL) {
1511		debugf(" no env, null ptr\n");
1512		return;
1513	}
1514	debugf(" loader_envp = 0x%08x\n", (uint32_t)loader_envp);
1515
1516	for (cp = loader_envp; cp != NULL; cp = kenv_next(cp))
1517		debugf(" %x %s\n", (uint32_t)cp, cp);
1518}
1519
1520#if __ARM_ARCH < 6
1521void *
1522initarm(struct arm_boot_params *abp)
1523{
1524	struct mem_region mem_regions[FDT_MEM_REGIONS];
1525	struct pv_addr kernel_l1pt;
1526	struct pv_addr dpcpu;
1527	vm_offset_t dtbp, freemempos, l2_start, lastaddr;
1528	uint64_t memsize;
1529	uint32_t l2size;
1530	char *env;
1531	void *kmdp;
1532	u_int l1pagetable;
1533	int i, j, err_devmap, mem_regions_sz;
1534
1535	lastaddr = parse_boot_param(abp);
1536	arm_physmem_kernaddr = abp->abp_physaddr;
1537
1538	memsize = 0;
1539
1540	cpuinfo_init();
1541	set_cpufuncs();
1542
1543	/*
1544	 * Find the dtb passed in by the boot loader.
1545	 */
1546	kmdp = preload_search_by_type("elf kernel");
1547	if (kmdp != NULL)
1548		dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1549	else
1550		dtbp = (vm_offset_t)NULL;
1551
1552#if defined(FDT_DTB_STATIC)
1553	/*
1554	 * In case the device tree blob was not retrieved (from metadata) try
1555	 * to use the statically embedded one.
1556	 */
1557	if (dtbp == (vm_offset_t)NULL)
1558		dtbp = (vm_offset_t)&fdt_static_dtb;
1559#endif
1560
1561	if (OF_install(OFW_FDT, 0) == FALSE)
1562		panic("Cannot install FDT");
1563
1564	if (OF_init((void *)dtbp) != 0)
1565		panic("OF_init failed with the found device tree");
1566
1567	/* Grab physical memory regions information from device tree. */
1568	if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, &memsize) != 0)
1569		panic("Cannot get physical memory regions");
1570	arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1571
1572	/* Grab reserved memory regions information from device tree. */
1573	if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
1574		arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1575		    EXFLAG_NODUMP | EXFLAG_NOALLOC);
1576
1577	/* Platform-specific initialisation */
1578	platform_probe_and_attach();
1579
1580	pcpu0_init();
1581
1582	/* Do basic tuning, hz etc */
1583	init_param1();
1584
1585	/* Calculate number of L2 tables needed for mapping vm_page_array */
1586	l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
1587	l2size = (l2size >> L1_S_SHIFT) + 1;
1588
1589	/*
1590	 * Add one table for end of kernel map, one for stacks, msgbuf and
1591	 * L1 and L2 tables map and one for vectors map.
1592	 */
1593	l2size += 3;
1594
1595	/* Make it divisible by 4 */
1596	l2size = (l2size + 3) & ~3;
1597
1598	freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
1599
1600	/* Define a macro to simplify memory allocation */
1601#define valloc_pages(var, np)						\
1602	alloc_pages((var).pv_va, (np));					\
1603	(var).pv_pa = (var).pv_va + (abp->abp_physaddr - KERNVIRTADDR);
1604
1605#define alloc_pages(var, np)						\
1606	(var) = freemempos;						\
1607	freemempos += (np * PAGE_SIZE);					\
1608	memset((char *)(var), 0, ((np) * PAGE_SIZE));
1609
1610	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
1611		freemempos += PAGE_SIZE;
1612	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
1613
1614	for (i = 0, j = 0; i < l2size; ++i) {
1615		if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
1616			valloc_pages(kernel_pt_table[i],
1617			    L2_TABLE_SIZE / PAGE_SIZE);
1618			j = i;
1619		} else {
1620			kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
1621			    L2_TABLE_SIZE_REAL * (i - j);
1622			kernel_pt_table[i].pv_pa =
1623			    kernel_pt_table[i].pv_va - KERNVIRTADDR +
1624			    abp->abp_physaddr;
1625
1626		}
1627	}
1628	/*
1629	 * Allocate a page for the system page mapped to 0x00000000
1630	 * or 0xffff0000. This page will just contain the system vectors
1631	 * and can be shared by all processes.
1632	 */
1633	valloc_pages(systempage, 1);
1634
1635	/* Allocate dynamic per-cpu area. */
1636	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
1637	dpcpu_init((void *)dpcpu.pv_va, 0);
1638
1639	/* Allocate stacks for all modes */
1640	valloc_pages(irqstack, IRQ_STACK_SIZE * MAXCPU);
1641	valloc_pages(abtstack, ABT_STACK_SIZE * MAXCPU);
1642	valloc_pages(undstack, UND_STACK_SIZE * MAXCPU);
1643	valloc_pages(kernelstack, kstack_pages * MAXCPU);
1644	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
1645
1646	/*
1647	 * Now we start construction of the L1 page table
1648	 * We start by mapping the L2 page tables into the L1.
1649	 * This means that we can replace L1 mappings later on if necessary
1650	 */
1651	l1pagetable = kernel_l1pt.pv_va;
1652
1653	/*
1654	 * Try to map as much as possible of kernel text and data using
1655	 * 1MB section mapping and for the rest of initial kernel address
1656	 * space use L2 coarse tables.
1657	 *
1658	 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
1659	 * and kernel structures
1660	 */
1661	l2_start = lastaddr & ~(L1_S_OFFSET);
1662	for (i = 0 ; i < l2size - 1; i++)
1663		pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
1664		    &kernel_pt_table[i]);
1665
1666	pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
1667
1668	/* Map kernel code and data */
1669	pmap_map_chunk(l1pagetable, KERNVIRTADDR, abp->abp_physaddr,
1670	   (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
1671	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1672
1673	/* Map L1 directory and allocated L2 page tables */
1674	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
1675	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1676
1677	pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
1678	    kernel_pt_table[0].pv_pa,
1679	    L2_TABLE_SIZE_REAL * l2size,
1680	    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1681
1682	/* Map allocated DPCPU, stacks and msgbuf */
1683	pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
1684	    freemempos - dpcpu.pv_va,
1685	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1686
1687	/* Link and map the vector page */
1688	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
1689	    &kernel_pt_table[l2size - 1]);
1690	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
1691	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE, PTE_CACHE);
1692
1693	/* Establish static device mappings. */
1694	err_devmap = platform_devmap_init();
1695	devmap_bootstrap(l1pagetable, NULL);
1696	vm_max_kernel_address = platform_lastaddr();
1697
1698	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) | DOMAIN_CLIENT);
1699	pmap_pa = kernel_l1pt.pv_pa;
1700	cpu_setttb(kernel_l1pt.pv_pa);
1701	cpu_tlb_flushID();
1702	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
1703
1704	/*
1705	 * Now that proper page tables are installed, call cpu_setup() to enable
1706	 * instruction and data caches and other chip-specific features.
1707	 */
1708	cpu_setup();
1709
1710	/*
1711	 * Only after the SOC registers block is mapped we can perform device
1712	 * tree fixups, as they may attempt to read parameters from hardware.
1713	 */
1714	OF_interpret("perform-fixup", 0);
1715
1716	platform_gpio_init();
1717
1718	cninit();
1719
1720	debugf("initarm: console initialized\n");
1721	debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
1722	debugf(" boothowto = 0x%08x\n", boothowto);
1723	debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
1724	print_kenv();
1725
1726	env = kern_getenv("kernelname");
1727	if (env != NULL) {
1728		strlcpy(kernelname, env, sizeof(kernelname));
1729		freeenv(env);
1730	}
1731
1732	if (err_devmap != 0)
1733		printf("WARNING: could not fully configure devmap, error=%d\n",
1734		    err_devmap);
1735
1736	platform_late_init();
1737
1738	/*
1739	 * Pages were allocated during the secondary bootstrap for the
1740	 * stacks for different CPU modes.
1741	 * We must now set the r13 registers in the different CPU modes to
1742	 * point to these stacks.
1743	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1744	 * of the stack memory.
1745	 */
1746	cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
1747
1748	set_stackptrs(0);
1749
1750	/*
1751	 * We must now clean the cache again....
1752	 * Cleaning may be done by reading new data to displace any
1753	 * dirty data in the cache. This will have happened in cpu_setttb()
1754	 * but since we are boot strapping the addresses used for the read
1755	 * may have just been remapped and thus the cache could be out
1756	 * of sync. A re-clean after the switch will cure this.
1757	 * After booting there are no gross relocations of the kernel thus
1758	 * this problem will not occur after initarm().
1759	 */
1760	cpu_idcache_wbinv_all();
1761
1762	undefined_init();
1763
1764	init_proc0(kernelstack.pv_va);
1765
1766	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1767	pmap_bootstrap(freemempos, &kernel_l1pt);
1768	msgbufp = (void *)msgbufpv.pv_va;
1769	msgbufinit(msgbufp, msgbufsize);
1770	mutex_init();
1771
1772	/*
1773	 * Exclude the kernel (and all the things we allocated which immediately
1774	 * follow the kernel) from the VM allocation pool but not from crash
1775	 * dumps.  virtual_avail is a global variable which tracks the kva we've
1776	 * "allocated" while setting up pmaps.
1777	 *
1778	 * Prepare the list of physical memory available to the vm subsystem.
1779	 */
1780	arm_physmem_exclude_region(abp->abp_physaddr,
1781	    (virtual_avail - KERNVIRTADDR), EXFLAG_NOALLOC);
1782	arm_physmem_init_kernel_globals();
1783
1784	init_param2(physmem);
1785	dbg_monitor_init();
1786	kdb_init();
1787
1788	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
1789	    sizeof(struct pcb)));
1790}
1791#else /* __ARM_ARCH < 6 */
1792void *
1793initarm(struct arm_boot_params *abp)
1794{
1795	struct mem_region mem_regions[FDT_MEM_REGIONS];
1796	vm_paddr_t lastaddr;
1797	vm_offset_t dtbp, kernelstack, dpcpu;
1798	char *env;
1799	void *kmdp;
1800	int err_devmap, mem_regions_sz;
1801#ifdef EFI
1802	struct efi_map_header *efihdr;
1803#endif
1804
1805	/* get last allocated physical address */
1806	arm_physmem_kernaddr = abp->abp_physaddr;
1807	lastaddr = parse_boot_param(abp) - KERNVIRTADDR + arm_physmem_kernaddr;
1808
1809	set_cpufuncs();
1810	cpuinfo_init();
1811
1812	/*
1813	 * Find the dtb passed in by the boot loader.
1814	 */
1815	kmdp = preload_search_by_type("elf kernel");
1816	dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1817#if defined(FDT_DTB_STATIC)
1818	/*
1819	 * In case the device tree blob was not retrieved (from metadata) try
1820	 * to use the statically embedded one.
1821	 */
1822	if (dtbp == (vm_offset_t)NULL)
1823		dtbp = (vm_offset_t)&fdt_static_dtb;
1824#endif
1825
1826	if (OF_install(OFW_FDT, 0) == FALSE)
1827		panic("Cannot install FDT");
1828
1829	if (OF_init((void *)dtbp) != 0)
1830		panic("OF_init failed with the found device tree");
1831
1832#if defined(LINUX_BOOT_ABI)
1833	if (loader_envp == NULL && fdt_get_chosen_bootargs(linux_command_line,
1834	    LBABI_MAX_COMMAND_LINE) == 0)
1835		cmdline_set_env(linux_command_line, CMDLINE_GUARD);
1836#endif
1837
1838#ifdef EFI
1839	efihdr = (struct efi_map_header *)preload_search_info(kmdp,
1840	    MODINFO_METADATA | MODINFOMD_EFI_MAP);
1841	if (efihdr != NULL) {
1842		add_efi_map_entries(efihdr, mem_regions, &mem_regions_sz);
1843	} else
1844#endif
1845	{
1846		/* Grab physical memory regions information from device tree. */
1847		if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,NULL) != 0)
1848			panic("Cannot get physical memory regions");
1849	}
1850	arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1851
1852	/* Grab reserved memory regions information from device tree. */
1853	if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
1854		arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1855		    EXFLAG_NODUMP | EXFLAG_NOALLOC);
1856
1857	/*
1858	 * Set TEX remapping registers.
1859	 * Setup kernel page tables and switch to kernel L1 page table.
1860	 */
1861	pmap_set_tex();
1862	pmap_bootstrap_prepare(lastaddr);
1863
1864	/*
1865	 * Now that proper page tables are installed, call cpu_setup() to enable
1866	 * instruction and data caches and other chip-specific features.
1867	 */
1868	cpu_setup();
1869
1870	/* Platform-specific initialisation */
1871	platform_probe_and_attach();
1872	pcpu0_init();
1873
1874	/* Do basic tuning, hz etc */
1875	init_param1();
1876
1877	/*
1878	 * Allocate a page for the system page mapped to 0xffff0000
1879	 * This page will just contain the system vectors and can be
1880	 * shared by all processes.
1881	 */
1882	systempage = pmap_preboot_get_pages(1);
1883
1884	/* Map the vector page. */
1885	pmap_preboot_map_pages(systempage, ARM_VECTORS_HIGH,  1);
1886	if (virtual_end >= ARM_VECTORS_HIGH)
1887		virtual_end = ARM_VECTORS_HIGH - 1;
1888
1889	/* Allocate dynamic per-cpu area. */
1890	dpcpu = pmap_preboot_get_vpages(DPCPU_SIZE / PAGE_SIZE);
1891	dpcpu_init((void *)dpcpu, 0);
1892
1893	/* Allocate stacks for all modes */
1894	irqstack    = pmap_preboot_get_vpages(IRQ_STACK_SIZE * MAXCPU);
1895	abtstack    = pmap_preboot_get_vpages(ABT_STACK_SIZE * MAXCPU);
1896	undstack    = pmap_preboot_get_vpages(UND_STACK_SIZE * MAXCPU );
1897	kernelstack = pmap_preboot_get_vpages(kstack_pages * MAXCPU);
1898
1899	/* Allocate message buffer. */
1900	msgbufp = (void *)pmap_preboot_get_vpages(
1901	    round_page(msgbufsize) / PAGE_SIZE);
1902
1903	/*
1904	 * Pages were allocated during the secondary bootstrap for the
1905	 * stacks for different CPU modes.
1906	 * We must now set the r13 registers in the different CPU modes to
1907	 * point to these stacks.
1908	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1909	 * of the stack memory.
1910	 */
1911	set_stackptrs(0);
1912	mutex_init();
1913
1914	/* Establish static device mappings. */
1915	err_devmap = platform_devmap_init();
1916	devmap_bootstrap(0, NULL);
1917	vm_max_kernel_address = platform_lastaddr();
1918
1919	/*
1920	 * Only after the SOC registers block is mapped we can perform device
1921	 * tree fixups, as they may attempt to read parameters from hardware.
1922	 */
1923	OF_interpret("perform-fixup", 0);
1924	platform_gpio_init();
1925	cninit();
1926
1927	debugf("initarm: console initialized\n");
1928	debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
1929	debugf(" boothowto = 0x%08x\n", boothowto);
1930	debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
1931	debugf(" lastaddr1: 0x%08x\n", lastaddr);
1932	print_kenv();
1933
1934	env = kern_getenv("kernelname");
1935	if (env != NULL)
1936		strlcpy(kernelname, env, sizeof(kernelname));
1937
1938	if (err_devmap != 0)
1939		printf("WARNING: could not fully configure devmap, error=%d\n",
1940		    err_devmap);
1941
1942	platform_late_init();
1943
1944	/*
1945	 * We must now clean the cache again....
1946	 * Cleaning may be done by reading new data to displace any
1947	 * dirty data in the cache. This will have happened in cpu_setttb()
1948	 * but since we are boot strapping the addresses used for the read
1949	 * may have just been remapped and thus the cache could be out
1950	 * of sync. A re-clean after the switch will cure this.
1951	 * After booting there are no gross relocations of the kernel thus
1952	 * this problem will not occur after initarm().
1953	 */
1954	/* Set stack for exception handlers */
1955	undefined_init();
1956	init_proc0(kernelstack);
1957	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1958	enable_interrupts(PSR_A);
1959	pmap_bootstrap(0);
1960
1961	/* Exclude the kernel (and all the things we allocated which immediately
1962	 * follow the kernel) from the VM allocation pool but not from crash
1963	 * dumps.  virtual_avail is a global variable which tracks the kva we've
1964	 * "allocated" while setting up pmaps.
1965	 *
1966	 * Prepare the list of physical memory available to the vm subsystem.
1967	 */
1968	arm_physmem_exclude_region(abp->abp_physaddr,
1969		pmap_preboot_get_pages(0) - abp->abp_physaddr, EXFLAG_NOALLOC);
1970	arm_physmem_init_kernel_globals();
1971
1972	init_param2(physmem);
1973	/* Init message buffer. */
1974	msgbufinit(msgbufp, msgbufsize);
1975	dbg_monitor_init();
1976	kdb_init();
1977	return ((void *)STACKALIGN(thread0.td_pcb));
1978
1979}
1980
1981#endif /* __ARM_ARCH < 6 */
1982#endif /* FDT */
1983
1984uint32_t (*arm_cpu_fill_vdso_timehands)(struct vdso_timehands *,
1985    struct timecounter *);
1986
1987uint32_t
1988cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc)
1989{
1990
1991	return (arm_cpu_fill_vdso_timehands != NULL ?
1992	    arm_cpu_fill_vdso_timehands(vdso_th, tc) : 0);
1993}
1994