machdep.c revision 237044
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
48#include <sys/cdefs.h>
49__FBSDID("$FreeBSD: head/sys/arm/arm/machdep.c 237044 2012-06-14 04:16:16Z imp $");
50
51#include <sys/param.h>
52#include <sys/proc.h>
53#include <sys/systm.h>
54#include <sys/bio.h>
55#include <sys/buf.h>
56#include <sys/bus.h>
57#include <sys/cons.h>
58#include <sys/cpu.h>
59#include <sys/exec.h>
60#include <sys/imgact.h>
61#include <sys/kernel.h>
62#include <sys/ktr.h>
63#include <sys/linker.h>
64#include <sys/lock.h>
65#include <sys/malloc.h>
66#include <sys/mutex.h>
67#include <sys/pcpu.h>
68#include <sys/ptrace.h>
69#include <sys/signalvar.h>
70#include <sys/syscallsubr.h>
71#include <sys/sysent.h>
72#include <sys/sysproto.h>
73#include <sys/uio.h>
74
75#include <vm/vm.h>
76#include <vm/pmap.h>
77#include <vm/vm_map.h>
78#include <vm/vm_object.h>
79#include <vm/vm_page.h>
80#include <vm/vm_pager.h>
81
82#include <machine/armreg.h>
83#include <machine/atags.h>
84#include <machine/cpu.h>
85#include <machine/machdep.h>
86#include <machine/md_var.h>
87#include <machine/metadata.h>
88#include <machine/pcb.h>
89#include <machine/pmap.h>
90#include <machine/reg.h>
91#include <machine/trap.h>
92#include <machine/undefined.h>
93#include <machine/vmparam.h>
94#include <machine/sysarch.h>
95
96static struct trapframe proc0_tf;
97
98uint32_t cpu_reset_address = 0;
99int cold = 1;
100vm_offset_t vector_page;
101
102long realmem = 0;
103
104int (*_arm_memcpy)(void *, void *, int, int) = NULL;
105int (*_arm_bzero)(void *, int, int) = NULL;
106int _min_memcpy_size = 0;
107int _min_bzero_size = 0;
108
109extern int *end;
110#ifdef DDB
111extern vm_offset_t ksym_start, ksym_end;
112#endif
113
114#if defined(LINUX_BOOT_ABI)
115#define LBABI_MAX_BANKS	10
116
117uint32_t board_id;
118struct arm_lbabi_tag *atag_list;
119uint32_t revision;
120uint64_t serial;
121char linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
122char atags[LBABI_MAX_COMMAND_LINE * 2];
123uint32_t memstart[LBABI_MAX_BANKS];
124uint32_t memsize[LBABI_MAX_BANKS];
125uint32_t membanks;
126#endif
127
128void
129sendsig(catcher, ksi, mask)
130	sig_t catcher;
131	ksiginfo_t *ksi;
132	sigset_t *mask;
133{
134	struct thread *td;
135	struct proc *p;
136	struct trapframe *tf;
137	struct sigframe *fp, frame;
138	struct sigacts *psp;
139	int onstack;
140	int sig;
141	int code;
142
143	td = curthread;
144	p = td->td_proc;
145	PROC_LOCK_ASSERT(p, MA_OWNED);
146	sig = ksi->ksi_signo;
147	code = ksi->ksi_code;
148	psp = p->p_sigacts;
149	mtx_assert(&psp->ps_mtx, MA_OWNED);
150	tf = td->td_frame;
151	onstack = sigonstack(tf->tf_usr_sp);
152
153	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
154	    catcher, sig);
155
156	/* Allocate and validate space for the signal handler context. */
157	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
158	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
159		fp = (struct sigframe *)(td->td_sigstk.ss_sp +
160		    td->td_sigstk.ss_size);
161#if defined(COMPAT_43)
162		td->td_sigstk.ss_flags |= SS_ONSTACK;
163#endif
164	} else
165		fp = (struct sigframe *)td->td_frame->tf_usr_sp;
166
167	/* make room on the stack */
168	fp--;
169
170	/* make the stack aligned */
171	fp = (struct sigframe *)STACKALIGN(fp);
172	/* Populate the siginfo frame. */
173	get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
174	frame.sf_si = ksi->ksi_info;
175	frame.sf_uc.uc_sigmask = *mask;
176	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
177	    ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
178	frame.sf_uc.uc_stack = td->td_sigstk;
179	mtx_unlock(&psp->ps_mtx);
180	PROC_UNLOCK(td->td_proc);
181
182	/* Copy the sigframe out to the user's stack. */
183	if (copyout(&frame, fp, sizeof(*fp)) != 0) {
184		/* Process has trashed its stack. Kill it. */
185		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
186		PROC_LOCK(p);
187		sigexit(td, SIGILL);
188	}
189
190	/* Translate the signal if appropriate. */
191	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
192		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
193
194	/*
195	 * Build context to run handler in.  We invoke the handler
196	 * directly, only returning via the trampoline.  Note the
197	 * trampoline version numbers are coordinated with machine-
198	 * dependent code in libc.
199	 */
200
201	tf->tf_r0 = sig;
202	tf->tf_r1 = (register_t)&fp->sf_si;
203	tf->tf_r2 = (register_t)&fp->sf_uc;
204
205	/* the trampoline uses r5 as the uc address */
206	tf->tf_r5 = (register_t)&fp->sf_uc;
207	tf->tf_pc = (register_t)catcher;
208	tf->tf_usr_sp = (register_t)fp;
209	tf->tf_usr_lr = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
210
211	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
212	    tf->tf_usr_sp);
213
214	PROC_LOCK(p);
215	mtx_lock(&psp->ps_mtx);
216}
217
218struct kva_md_info kmi;
219
220/*
221 * arm32_vector_init:
222 *
223 *	Initialize the vector page, and select whether or not to
224 *	relocate the vectors.
225 *
226 *	NOTE: We expect the vector page to be mapped at its expected
227 *	destination.
228 */
229
230extern unsigned int page0[], page0_data[];
231void
232arm_vector_init(vm_offset_t va, int which)
233{
234	unsigned int *vectors = (int *) va;
235	unsigned int *vectors_data = vectors + (page0_data - page0);
236	int vec;
237
238	/*
239	 * Loop through the vectors we're taking over, and copy the
240	 * vector's insn and data word.
241	 */
242	for (vec = 0; vec < ARM_NVEC; vec++) {
243		if ((which & (1 << vec)) == 0) {
244			/* Don't want to take over this vector. */
245			continue;
246		}
247		vectors[vec] = page0[vec];
248		vectors_data[vec] = page0_data[vec];
249	}
250
251	/* Now sync the vectors. */
252	cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
253
254	vector_page = va;
255
256	if (va == ARM_VECTORS_HIGH) {
257		/*
258		 * Assume the MD caller knows what it's doing here, and
259		 * really does want the vector page relocated.
260		 *
261		 * Note: This has to be done here (and not just in
262		 * cpu_setup()) because the vector page needs to be
263		 * accessible *before* cpu_startup() is called.
264		 * Think ddb(9) ...
265		 *
266		 * NOTE: If the CPU control register is not readable,
267		 * this will totally fail!  We'll just assume that
268		 * any system that has high vector support has a
269		 * readable CPU control register, for now.  If we
270		 * ever encounter one that does not, we'll have to
271		 * rethink this.
272		 */
273		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
274	}
275}
276
277static void
278cpu_startup(void *dummy)
279{
280	struct pcb *pcb = thread0.td_pcb;
281#ifndef ARM_CACHE_LOCK_ENABLE
282	vm_page_t m;
283#endif
284
285	cpu_setup("");
286	identify_arm_cpu();
287
288	printf("real memory  = %ju (%ju MB)\n", (uintmax_t)ptoa(physmem),
289	    (uintmax_t)ptoa(physmem) / 1048576);
290	realmem = physmem;
291
292	/*
293	 * Display the RAM layout.
294	 */
295	if (bootverbose) {
296		int indx;
297
298		printf("Physical memory chunk(s):\n");
299		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
300			vm_paddr_t size;
301
302			size = phys_avail[indx + 1] - phys_avail[indx];
303			printf("%#08jx - %#08jx, %ju bytes (%ju pages)\n",
304			    (uintmax_t)phys_avail[indx],
305			    (uintmax_t)phys_avail[indx + 1] - 1,
306			    (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
307		}
308	}
309
310	vm_ksubmap_init(&kmi);
311
312	printf("avail memory = %ju (%ju MB)\n",
313	    (uintmax_t)ptoa(cnt.v_free_count),
314	    (uintmax_t)ptoa(cnt.v_free_count) / 1048576);
315
316	bufinit();
317	vm_pager_bufferinit();
318	pcb->un_32.pcb32_und_sp = (u_int)thread0.td_kstack +
319	    USPACE_UNDEF_STACK_TOP;
320	pcb->un_32.pcb32_sp = (u_int)thread0.td_kstack +
321	    USPACE_SVC_STACK_TOP;
322	vector_page_setprot(VM_PROT_READ);
323	pmap_set_pcb_pagedir(pmap_kernel(), pcb);
324	pmap_postinit();
325#ifdef ARM_CACHE_LOCK_ENABLE
326	pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
327	arm_lock_cache_line(ARM_TP_ADDRESS);
328#else
329	m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
330	pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
331#endif
332	*(uint32_t *)ARM_RAS_START = 0;
333	*(uint32_t *)ARM_RAS_END = 0xffffffff;
334}
335
336SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
337
338/*
339 * Flush the D-cache for non-DMA I/O so that the I-cache can
340 * be made coherent later.
341 */
342void
343cpu_flush_dcache(void *ptr, size_t len)
344{
345
346	cpu_dcache_wb_range((uintptr_t)ptr, len);
347	cpu_l2cache_wb_range((uintptr_t)ptr, len);
348}
349
350/* Get current clock frequency for the given cpu id. */
351int
352cpu_est_clockrate(int cpu_id, uint64_t *rate)
353{
354
355	return (ENXIO);
356}
357
358void
359cpu_idle(int busy)
360{
361	cpu_sleep(0);
362}
363
364int
365cpu_idle_wakeup(int cpu)
366{
367
368	return (0);
369}
370
371int
372fill_regs(struct thread *td, struct reg *regs)
373{
374	struct trapframe *tf = td->td_frame;
375	bcopy(&tf->tf_r0, regs->r, sizeof(regs->r));
376	regs->r_sp = tf->tf_usr_sp;
377	regs->r_lr = tf->tf_usr_lr;
378	regs->r_pc = tf->tf_pc;
379	regs->r_cpsr = tf->tf_spsr;
380	return (0);
381}
382int
383fill_fpregs(struct thread *td, struct fpreg *regs)
384{
385	bzero(regs, sizeof(*regs));
386	return (0);
387}
388
389int
390set_regs(struct thread *td, struct reg *regs)
391{
392	struct trapframe *tf = td->td_frame;
393
394	bcopy(regs->r, &tf->tf_r0, sizeof(regs->r));
395	tf->tf_usr_sp = regs->r_sp;
396	tf->tf_usr_lr = regs->r_lr;
397	tf->tf_pc = regs->r_pc;
398	tf->tf_spsr &=  ~PSR_FLAGS;
399	tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
400	return (0);
401}
402
403int
404set_fpregs(struct thread *td, struct fpreg *regs)
405{
406	return (0);
407}
408
409int
410fill_dbregs(struct thread *td, struct dbreg *regs)
411{
412	return (0);
413}
414int
415set_dbregs(struct thread *td, struct dbreg *regs)
416{
417	return (0);
418}
419
420
421static int
422ptrace_read_int(struct thread *td, vm_offset_t addr, u_int32_t *v)
423{
424	struct iovec iov;
425	struct uio uio;
426
427	PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
428	iov.iov_base = (caddr_t) v;
429	iov.iov_len = sizeof(u_int32_t);
430	uio.uio_iov = &iov;
431	uio.uio_iovcnt = 1;
432	uio.uio_offset = (off_t)addr;
433	uio.uio_resid = sizeof(u_int32_t);
434	uio.uio_segflg = UIO_SYSSPACE;
435	uio.uio_rw = UIO_READ;
436	uio.uio_td = td;
437	return proc_rwmem(td->td_proc, &uio);
438}
439
440static int
441ptrace_write_int(struct thread *td, vm_offset_t addr, u_int32_t v)
442{
443	struct iovec iov;
444	struct uio uio;
445
446	PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
447	iov.iov_base = (caddr_t) &v;
448	iov.iov_len = sizeof(u_int32_t);
449	uio.uio_iov = &iov;
450	uio.uio_iovcnt = 1;
451	uio.uio_offset = (off_t)addr;
452	uio.uio_resid = sizeof(u_int32_t);
453	uio.uio_segflg = UIO_SYSSPACE;
454	uio.uio_rw = UIO_WRITE;
455	uio.uio_td = td;
456	return proc_rwmem(td->td_proc, &uio);
457}
458
459int
460ptrace_single_step(struct thread *td)
461{
462	struct proc *p;
463	int error;
464
465	KASSERT(td->td_md.md_ptrace_instr == 0,
466	 ("Didn't clear single step"));
467	p = td->td_proc;
468	PROC_UNLOCK(p);
469	error = ptrace_read_int(td, td->td_frame->tf_pc + 4,
470	    &td->td_md.md_ptrace_instr);
471	if (error)
472		goto out;
473	error = ptrace_write_int(td, td->td_frame->tf_pc + 4,
474	    PTRACE_BREAKPOINT);
475	if (error)
476		td->td_md.md_ptrace_instr = 0;
477	td->td_md.md_ptrace_addr = td->td_frame->tf_pc + 4;
478out:
479	PROC_LOCK(p);
480	return (error);
481}
482
483int
484ptrace_clear_single_step(struct thread *td)
485{
486	struct proc *p;
487
488	if (td->td_md.md_ptrace_instr) {
489		p = td->td_proc;
490		PROC_UNLOCK(p);
491		ptrace_write_int(td, td->td_md.md_ptrace_addr,
492		    td->td_md.md_ptrace_instr);
493		PROC_LOCK(p);
494		td->td_md.md_ptrace_instr = 0;
495	}
496	return (0);
497}
498
499int
500ptrace_set_pc(struct thread *td, unsigned long addr)
501{
502	td->td_frame->tf_pc = addr;
503	return (0);
504}
505
506void
507cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
508{
509}
510
511void
512spinlock_enter(void)
513{
514	struct thread *td;
515	register_t cspr;
516
517	td = curthread;
518	if (td->td_md.md_spinlock_count == 0) {
519		cspr = disable_interrupts(I32_bit | F32_bit);
520		td->td_md.md_spinlock_count = 1;
521		td->td_md.md_saved_cspr = cspr;
522	} else
523		td->td_md.md_spinlock_count++;
524	critical_enter();
525}
526
527void
528spinlock_exit(void)
529{
530	struct thread *td;
531	register_t cspr;
532
533	td = curthread;
534	critical_exit();
535	cspr = td->td_md.md_saved_cspr;
536	td->td_md.md_spinlock_count--;
537	if (td->td_md.md_spinlock_count == 0)
538		restore_interrupts(cspr);
539}
540
541/*
542 * Clear registers on exec
543 */
544void
545exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
546{
547	struct trapframe *tf = td->td_frame;
548
549	memset(tf, 0, sizeof(*tf));
550	tf->tf_usr_sp = stack;
551	tf->tf_usr_lr = imgp->entry_addr;
552	tf->tf_svc_lr = 0x77777777;
553	tf->tf_pc = imgp->entry_addr;
554	tf->tf_spsr = PSR_USR32_MODE;
555}
556
557/*
558 * Get machine context.
559 */
560int
561get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
562{
563	struct trapframe *tf = td->td_frame;
564	__greg_t *gr = mcp->__gregs;
565
566	if (clear_ret & GET_MC_CLEAR_RET)
567		gr[_REG_R0] = 0;
568	else
569		gr[_REG_R0]   = tf->tf_r0;
570	gr[_REG_R1]   = tf->tf_r1;
571	gr[_REG_R2]   = tf->tf_r2;
572	gr[_REG_R3]   = tf->tf_r3;
573	gr[_REG_R4]   = tf->tf_r4;
574	gr[_REG_R5]   = tf->tf_r5;
575	gr[_REG_R6]   = tf->tf_r6;
576	gr[_REG_R7]   = tf->tf_r7;
577	gr[_REG_R8]   = tf->tf_r8;
578	gr[_REG_R9]   = tf->tf_r9;
579	gr[_REG_R10]  = tf->tf_r10;
580	gr[_REG_R11]  = tf->tf_r11;
581	gr[_REG_R12]  = tf->tf_r12;
582	gr[_REG_SP]   = tf->tf_usr_sp;
583	gr[_REG_LR]   = tf->tf_usr_lr;
584	gr[_REG_PC]   = tf->tf_pc;
585	gr[_REG_CPSR] = tf->tf_spsr;
586
587	return (0);
588}
589
590/*
591 * Set machine context.
592 *
593 * However, we don't set any but the user modifiable flags, and we won't
594 * touch the cs selector.
595 */
596int
597set_mcontext(struct thread *td, const mcontext_t *mcp)
598{
599	struct trapframe *tf = td->td_frame;
600	const __greg_t *gr = mcp->__gregs;
601
602	tf->tf_r0 = gr[_REG_R0];
603	tf->tf_r1 = gr[_REG_R1];
604	tf->tf_r2 = gr[_REG_R2];
605	tf->tf_r3 = gr[_REG_R3];
606	tf->tf_r4 = gr[_REG_R4];
607	tf->tf_r5 = gr[_REG_R5];
608	tf->tf_r6 = gr[_REG_R6];
609	tf->tf_r7 = gr[_REG_R7];
610	tf->tf_r8 = gr[_REG_R8];
611	tf->tf_r9 = gr[_REG_R9];
612	tf->tf_r10 = gr[_REG_R10];
613	tf->tf_r11 = gr[_REG_R11];
614	tf->tf_r12 = gr[_REG_R12];
615	tf->tf_usr_sp = gr[_REG_SP];
616	tf->tf_usr_lr = gr[_REG_LR];
617	tf->tf_pc = gr[_REG_PC];
618	tf->tf_spsr = gr[_REG_CPSR];
619
620	return (0);
621}
622
623/*
624 * MPSAFE
625 */
626int
627sys_sigreturn(td, uap)
628	struct thread *td;
629	struct sigreturn_args /* {
630		const struct __ucontext *sigcntxp;
631	} */ *uap;
632{
633	struct sigframe sf;
634	struct trapframe *tf;
635	int spsr;
636
637	if (uap == NULL)
638		return (EFAULT);
639	if (copyin(uap->sigcntxp, &sf, sizeof(sf)))
640		return (EFAULT);
641	/*
642	 * Make sure the processor mode has not been tampered with and
643	 * interrupts have not been disabled.
644	 */
645	spsr = sf.sf_uc.uc_mcontext.__gregs[_REG_CPSR];
646	if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
647	    (spsr & (I32_bit | F32_bit)) != 0)
648		return (EINVAL);
649		/* Restore register context. */
650	tf = td->td_frame;
651	set_mcontext(td, &sf.sf_uc.uc_mcontext);
652
653	/* Restore signal mask. */
654	kern_sigprocmask(td, SIG_SETMASK, &sf.sf_uc.uc_sigmask, NULL, 0);
655
656	return (EJUSTRETURN);
657}
658
659
660/*
661 * Construct a PCB from a trapframe. This is called from kdb_trap() where
662 * we want to start a backtrace from the function that caused us to enter
663 * the debugger. We have the context in the trapframe, but base the trace
664 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
665 * enough for a backtrace.
666 */
667void
668makectx(struct trapframe *tf, struct pcb *pcb)
669{
670	pcb->un_32.pcb32_r8 = tf->tf_r8;
671	pcb->un_32.pcb32_r9 = tf->tf_r9;
672	pcb->un_32.pcb32_r10 = tf->tf_r10;
673	pcb->un_32.pcb32_r11 = tf->tf_r11;
674	pcb->un_32.pcb32_r12 = tf->tf_r12;
675	pcb->un_32.pcb32_pc = tf->tf_pc;
676	pcb->un_32.pcb32_lr = tf->tf_usr_lr;
677	pcb->un_32.pcb32_sp = tf->tf_usr_sp;
678}
679
680/*
681 * Fake up a boot descriptor table
682 */
683vm_offset_t
684fake_preload_metadata(struct arm_boot_params *abp __unused)
685{
686#ifdef DDB
687	vm_offset_t zstart = 0, zend = 0;
688#endif
689	vm_offset_t lastaddr;
690	int i = 0;
691	static uint32_t fake_preload[35];
692
693	fake_preload[i++] = MODINFO_NAME;
694	fake_preload[i++] = strlen("kernel") + 1;
695	strcpy((char*)&fake_preload[i++], "kernel");
696	i += 1;
697	fake_preload[i++] = MODINFO_TYPE;
698	fake_preload[i++] = strlen("elf kernel") + 1;
699	strcpy((char*)&fake_preload[i++], "elf kernel");
700	i += 2;
701	fake_preload[i++] = MODINFO_ADDR;
702	fake_preload[i++] = sizeof(vm_offset_t);
703	fake_preload[i++] = KERNVIRTADDR;
704	fake_preload[i++] = MODINFO_SIZE;
705	fake_preload[i++] = sizeof(uint32_t);
706	fake_preload[i++] = (uint32_t)&end - KERNVIRTADDR;
707#ifdef DDB
708	if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
709		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
710		fake_preload[i++] = sizeof(vm_offset_t);
711		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
712		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
713		fake_preload[i++] = sizeof(vm_offset_t);
714		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
715		lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
716		zend = lastaddr;
717		zstart = *(uint32_t *)(KERNVIRTADDR + 4);
718		ksym_start = zstart;
719		ksym_end = zend;
720	} else
721#endif
722		lastaddr = (vm_offset_t)&end;
723	fake_preload[i++] = 0;
724	fake_preload[i] = 0;
725	preload_metadata = (void *)fake_preload;
726
727	return (lastaddr);
728}
729
730#if defined(LINUX_BOOT_ABI)
731vm_offset_t
732linux_parse_boot_param(struct arm_boot_params *abp)
733{
734	struct arm_lbabi_tag *walker;
735
736	/*
737	 * Linux boot ABI: r0 = 0, r1 is the board type (!= 0) and r2
738	 * is atags or dtb pointer.  If all of these aren't satisfied,
739	 * then punt.
740	 */
741	if (!(abp->abp_r0 == 0 && abp->abp_r1 != 0 && abp->abp_r2 != 0))
742		return 0;
743
744	board_id = abp->abp_r1;
745	walker = (struct arm_lbabi_tag *)
746	    (abp->abp_r2 + KERNVIRTADDR - KERNPHYSADDR);
747
748	/* xxx - Need to also look for binary device tree */
749	if (ATAG_TAG(walker) != ATAG_CORE)
750		return 0;
751
752	atag_list = walker;
753	while (ATAG_TAG(walker) != ATAG_NONE) {
754		switch (ATAG_TAG(walker)) {
755		case ATAG_CORE:
756			break;
757		case ATAG_MEM:
758			if (membanks < LBABI_MAX_BANKS) {
759				memstart[membanks] = walker->u.tag_mem.start;
760				memsize[membanks] = walker->u.tag_mem.size;
761			}
762			membanks++;
763			break;
764		case ATAG_INITRD2:
765			break;
766		case ATAG_SERIAL:
767			serial = walker->u.tag_sn.low |
768			    ((uint64_t)walker->u.tag_sn.high << 32);
769			break;
770		case ATAG_REVISION:
771			revision = walker->u.tag_rev.rev;
772			break;
773		case ATAG_CMDLINE:
774			/* XXX open question: Parse this for boothowto? */
775			bcopy(walker->u.tag_cmd.command, linux_command_line,
776			      ATAG_SIZE(walker));
777			break;
778		default:
779			break;
780		}
781		walker = ATAG_NEXT(walker);
782	}
783
784	/* Save a copy for later */
785	bcopy(atag_list, atags,
786	    (char *)walker - (char *)atag_list + ATAG_SIZE(walker));
787
788	return fake_preload_metadata(abp);
789}
790#endif
791
792#if defined(FREEBSD_BOOT_LOADER)
793vm_offset_t
794freebsd_parse_boot_param(struct arm_boot_params *abp)
795{
796	vm_offset_t lastaddr = 0;
797	void *mdp;
798	void *kmdp;
799
800	/*
801	 * Mask metadata pointer: it is supposed to be on page boundary. If
802	 * the first argument (mdp) doesn't point to a valid address the
803	 * bootloader must have passed us something else than the metadata
804	 * ptr, so we give up.  Also give up if we cannot find metadta section
805	 * the loader creates that we get all this data out of.
806	 */
807
808	if ((mdp = (void *)(abp->abp_r0 & ~PAGE_MASK)) == NULL)
809		return 0;
810	preload_metadata = mdp;
811	kmdp = preload_search_by_type("elf kernel");
812	if (kmdp == NULL)
813		return 0;
814
815	boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
816	kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
817	lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
818#ifdef DDB
819	ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
820	ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
821#endif
822	preload_addr_relocate = KERNVIRTADDR - KERNPHYSADDR;
823	return lastaddr;
824}
825#endif
826
827vm_offset_t
828default_parse_boot_param(struct arm_boot_params *abp)
829{
830	vm_offset_t lastaddr;
831
832#if defined(LINUX_BOOT_ABI)
833	if ((lastaddr = linux_parse_boot_param(abp)) != 0)
834		return lastaddr;
835#endif
836#if defined(FREEBSD_BOOT_LOADER)
837	if ((lastaddr = freebsd_parse_boot_param(abp)) != 0)
838		return lastaddr;
839#endif
840	/* Fall back to hardcoded metadata. */
841	lastaddr = fake_preload_metadata(abp);
842
843	return lastaddr;
844}
845
846/*
847 * Stub version of the boot parameter parsing routine.  We are
848 * called early in initarm, before even VM has been initialized.
849 * This routine needs to preserve any data that the boot loader
850 * has passed in before the kernel starts to grow past the end
851 * of the BSS, traditionally the place boot-loaders put this data.
852 *
853 * Since this is called so early, things that depend on the vm system
854 * being setup (including access to some SoC's serial ports), about
855 * all that can be done in this routine is to copy the arguments.
856 *
857 * This is the default boot parameter parsing routine.  Individual
858 * kernels/boards can override this weak function with one of their
859 * own.  We just fake metadata...
860 */
861__weak_reference(default_parse_boot_param, parse_boot_param);
862
863/*
864 * Initialize proc0
865 */
866void
867init_proc0(vm_offset_t kstack)
868{
869	proc_linkup0(&proc0, &thread0);
870	thread0.td_kstack = kstack;
871	thread0.td_pcb = (struct pcb *)
872		(thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
873	thread0.td_pcb->pcb_flags = 0;
874	thread0.td_frame = &proc0_tf;
875	pcpup->pc_curpcb = thread0.td_pcb;
876}
877