machdep.c revision 242531
1226031Sstas/*	$NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $	*/
2226031Sstas
3226031Sstas/*-
4226031Sstas * Copyright (c) 2004 Olivier Houchard
5226031Sstas * Copyright (c) 1994-1998 Mark Brinicombe.
6226031Sstas * Copyright (c) 1994 Brini.
7226031Sstas * All rights reserved.
8226031Sstas *
9226031Sstas * This code is derived from software written for Brini by Mark Brinicombe
10226031Sstas *
11226031Sstas * Redistribution and use in source and binary forms, with or without
12226031Sstas * modification, are permitted provided that the following conditions
13226031Sstas * are met:
14226031Sstas * 1. Redistributions of source code must retain the above copyright
15226031Sstas *    notice, this list of conditions and the following disclaimer.
16226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
17226031Sstas *    notice, this list of conditions and the following disclaimer in the
18226031Sstas *    documentation and/or other materials provided with the distribution.
19226031Sstas * 3. All advertising materials mentioning features or use of this software
20226031Sstas *    must display the following acknowledgement:
21226031Sstas *	This product includes software developed by Mark Brinicombe
22226031Sstas *	for the NetBSD Project.
23226031Sstas * 4. The name of the company nor the name of the author may be used to
24226031Sstas *    endorse or promote products derived from this software without specific
25226031Sstas *    prior written permission.
26226031Sstas *
27226031Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
28226031Sstas * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29226031Sstas * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30226031Sstas * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31226031Sstas * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32226031Sstas * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33226031Sstas * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37226031Sstas * SUCH DAMAGE.
38226031Sstas *
39226031Sstas * Machine dependant functions for kernel setup
40226031Sstas *
41226031Sstas * Created      : 17/09/94
42226031Sstas * Updated	: 18/04/01 updated for new wscons
43226031Sstas */
44226031Sstas
45226031Sstas#include "opt_compat.h"
46226031Sstas#include "opt_ddb.h"
47226031Sstas#include "opt_platform.h"
48226031Sstas#include "opt_timer.h"
49226031Sstas
50226031Sstas#include <sys/cdefs.h>
51226031Sstas__FBSDID("$FreeBSD: head/sys/arm/arm/machdep.c 242531 2012-11-03 22:39:07Z andrew $");
52226031Sstas
53226031Sstas#include <sys/param.h>
54226031Sstas#include <sys/proc.h>
55226031Sstas#include <sys/systm.h>
56226031Sstas#include <sys/bio.h>
57226031Sstas#include <sys/buf.h>
58226031Sstas#include <sys/bus.h>
59226031Sstas#include <sys/cons.h>
60226031Sstas#include <sys/cpu.h>
61226031Sstas#include <sys/exec.h>
62226031Sstas#include <sys/imgact.h>
63226031Sstas#include <sys/kdb.h>
64226031Sstas#include <sys/kernel.h>
65226031Sstas#include <sys/ktr.h>
66226031Sstas#include <sys/linker.h>
67226031Sstas#include <sys/lock.h>
68226031Sstas#include <sys/malloc.h>
69226031Sstas#include <sys/msgbuf.h>
70226031Sstas#include <sys/mutex.h>
71226031Sstas#include <sys/pcpu.h>
72226031Sstas#include <sys/ptrace.h>
73226031Sstas#include <sys/signalvar.h>
74226031Sstas#include <sys/syscallsubr.h>
75226031Sstas#include <sys/sysent.h>
76226031Sstas#include <sys/sysproto.h>
77226031Sstas#include <sys/uio.h>
78226031Sstas
79226031Sstas#include <vm/vm.h>
80226031Sstas#include <vm/pmap.h>
81226031Sstas#include <vm/vm_map.h>
82226031Sstas#include <vm/vm_object.h>
83226031Sstas#include <vm/vm_page.h>
84226031Sstas#include <vm/vm_pager.h>
85226031Sstas
86226031Sstas#include <machine/armreg.h>
87226031Sstas#include <machine/atags.h>
88226031Sstas#include <machine/cpu.h>
89226031Sstas#include <machine/machdep.h>
90226031Sstas#include <machine/md_var.h>
91226031Sstas#include <machine/metadata.h>
92226031Sstas#include <machine/pcb.h>
93226031Sstas#include <machine/pmap.h>
94226031Sstas#include <machine/reg.h>
95226031Sstas#include <machine/trap.h>
96226031Sstas#include <machine/undefined.h>
97226031Sstas#include <machine/vmparam.h>
98226031Sstas#include <machine/sysarch.h>
99226031Sstas
100226031Sstas#ifdef FDT
101226031Sstas#include <dev/fdt/fdt_common.h>
102226031Sstas#include <dev/ofw/openfirm.h>
103226031Sstas#endif
104226031Sstas
105226031Sstas#ifdef DEBUG
106226031Sstas#define	debugf(fmt, args...) printf(fmt, ##args)
107226031Sstas#else
108226031Sstas#define	debugf(fmt, args...)
109226031Sstas#endif
110226031Sstas
111226031Sstasstruct pcpu __pcpu[MAXCPU];
112226031Sstasstruct pcpu *pcpup = &__pcpu[0];
113226031Sstas
114226031Sstasstatic struct trapframe proc0_tf;
115226031Sstasuint32_t cpu_reset_address = 0;
116226031Sstasint cold = 1;
117226031Sstasvm_offset_t vector_page;
118226031Sstas
119226031Sstaslong realmem = 0;
120226031Sstas
121226031Sstasint (*_arm_memcpy)(void *, void *, int, int) = NULL;
122226031Sstasint (*_arm_bzero)(void *, int, int) = NULL;
123226031Sstasint _min_memcpy_size = 0;
124226031Sstasint _min_bzero_size = 0;
125226031Sstas
126226031Sstasextern int *end;
127226031Sstas#ifdef DDB
128226031Sstasextern vm_offset_t ksym_start, ksym_end;
129226031Sstas#endif
130226031Sstas
131226031Sstas#ifdef FDT
132226031Sstas/*
133226031Sstas * This is the number of L2 page tables required for covering max
134226031Sstas * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
135226031Sstas * stacks etc.), uprounded to be divisible by 4.
136226031Sstas */
137226031Sstas#define KERNEL_PT_MAX	78
138226031Sstas
139226031Sstasstatic struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
140226031Sstas
141226031Sstasvm_paddr_t phys_avail[10];
142226031Sstasvm_paddr_t dump_avail[4];
143226031Sstas
144226031Sstasextern u_int data_abort_handler_address;
145226031Sstasextern u_int prefetch_abort_handler_address;
146226031Sstasextern u_int undefined_handler_address;
147226031Sstas
148226031Sstasvm_paddr_t pmap_pa;
149226031Sstas
150226031Sstasstruct pv_addr systempage;
151226031Sstasstatic struct pv_addr msgbufpv;
152226031Sstasstruct pv_addr irqstack;
153226031Sstasstruct pv_addr undstack;
154226031Sstasstruct pv_addr abtstack;
155226031Sstasstatic struct pv_addr kernelstack;
156226031Sstas
157226031Sstasconst struct pmap_devmap *pmap_devmap_bootstrap_table;
158226031Sstas#endif
159226031Sstas
160226031Sstas#if defined(LINUX_BOOT_ABI)
161226031Sstas#define LBABI_MAX_BANKS	10
162226031Sstas
163226031Sstasuint32_t board_id;
164226031Sstasstruct arm_lbabi_tag *atag_list;
165226031Sstasuint32_t revision;
166226031Sstasuint64_t serial;
167226031Sstaschar linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
168226031Sstaschar atags[LBABI_MAX_COMMAND_LINE * 2];
169226031Sstasuint32_t memstart[LBABI_MAX_BANKS];
170226031Sstasuint32_t memsize[LBABI_MAX_BANKS];
171226031Sstasuint32_t membanks;
172226031Sstas#endif
173226031Sstas
174226031Sstasvoid
175226031Sstassendsig(catcher, ksi, mask)
176226031Sstas	sig_t catcher;
177226031Sstas	ksiginfo_t *ksi;
178226031Sstas	sigset_t *mask;
179226031Sstas{
180226031Sstas	struct thread *td;
181226031Sstas	struct proc *p;
182226031Sstas	struct trapframe *tf;
183226031Sstas	struct sigframe *fp, frame;
184226031Sstas	struct sigacts *psp;
185226031Sstas	int onstack;
186226031Sstas	int sig;
187226031Sstas	int code;
188226031Sstas
189226031Sstas	td = curthread;
190226031Sstas	p = td->td_proc;
191226031Sstas	PROC_LOCK_ASSERT(p, MA_OWNED);
192226031Sstas	sig = ksi->ksi_signo;
193226031Sstas	code = ksi->ksi_code;
194226031Sstas	psp = p->p_sigacts;
195226031Sstas	mtx_assert(&psp->ps_mtx, MA_OWNED);
196226031Sstas	tf = td->td_frame;
197226031Sstas	onstack = sigonstack(tf->tf_usr_sp);
198226031Sstas
199226031Sstas	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
200226031Sstas	    catcher, sig);
201226031Sstas
202226031Sstas	/* Allocate and validate space for the signal handler context. */
203226031Sstas	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
204226031Sstas	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
205226031Sstas		fp = (struct sigframe *)(td->td_sigstk.ss_sp +
206226031Sstas		    td->td_sigstk.ss_size);
207226031Sstas#if defined(COMPAT_43)
208226031Sstas		td->td_sigstk.ss_flags |= SS_ONSTACK;
209226031Sstas#endif
210226031Sstas	} else
211226031Sstas		fp = (struct sigframe *)td->td_frame->tf_usr_sp;
212226031Sstas
213226031Sstas	/* make room on the stack */
214226031Sstas	fp--;
215226031Sstas
216226031Sstas	/* make the stack aligned */
217226031Sstas	fp = (struct sigframe *)STACKALIGN(fp);
218226031Sstas	/* Populate the siginfo frame. */
219226031Sstas	get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
220226031Sstas	frame.sf_si = ksi->ksi_info;
221226031Sstas	frame.sf_uc.uc_sigmask = *mask;
222226031Sstas	frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
223226031Sstas	    ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
224226031Sstas	frame.sf_uc.uc_stack = td->td_sigstk;
225226031Sstas	mtx_unlock(&psp->ps_mtx);
226226031Sstas	PROC_UNLOCK(td->td_proc);
227
228	/* Copy the sigframe out to the user's stack. */
229	if (copyout(&frame, fp, sizeof(*fp)) != 0) {
230		/* Process has trashed its stack. Kill it. */
231		CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
232		PROC_LOCK(p);
233		sigexit(td, SIGILL);
234	}
235
236	/* Translate the signal if appropriate. */
237	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
238		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
239
240	/*
241	 * Build context to run handler in.  We invoke the handler
242	 * directly, only returning via the trampoline.  Note the
243	 * trampoline version numbers are coordinated with machine-
244	 * dependent code in libc.
245	 */
246
247	tf->tf_r0 = sig;
248	tf->tf_r1 = (register_t)&fp->sf_si;
249	tf->tf_r2 = (register_t)&fp->sf_uc;
250
251	/* the trampoline uses r5 as the uc address */
252	tf->tf_r5 = (register_t)&fp->sf_uc;
253	tf->tf_pc = (register_t)catcher;
254	tf->tf_usr_sp = (register_t)fp;
255	tf->tf_usr_lr = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
256
257	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
258	    tf->tf_usr_sp);
259
260	PROC_LOCK(p);
261	mtx_lock(&psp->ps_mtx);
262}
263
264struct kva_md_info kmi;
265
266/*
267 * arm32_vector_init:
268 *
269 *	Initialize the vector page, and select whether or not to
270 *	relocate the vectors.
271 *
272 *	NOTE: We expect the vector page to be mapped at its expected
273 *	destination.
274 */
275
276extern unsigned int page0[], page0_data[];
277void
278arm_vector_init(vm_offset_t va, int which)
279{
280	unsigned int *vectors = (int *) va;
281	unsigned int *vectors_data = vectors + (page0_data - page0);
282	int vec;
283
284	/*
285	 * Loop through the vectors we're taking over, and copy the
286	 * vector's insn and data word.
287	 */
288	for (vec = 0; vec < ARM_NVEC; vec++) {
289		if ((which & (1 << vec)) == 0) {
290			/* Don't want to take over this vector. */
291			continue;
292		}
293		vectors[vec] = page0[vec];
294		vectors_data[vec] = page0_data[vec];
295	}
296
297	/* Now sync the vectors. */
298	cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
299
300	vector_page = va;
301
302	if (va == ARM_VECTORS_HIGH) {
303		/*
304		 * Assume the MD caller knows what it's doing here, and
305		 * really does want the vector page relocated.
306		 *
307		 * Note: This has to be done here (and not just in
308		 * cpu_setup()) because the vector page needs to be
309		 * accessible *before* cpu_startup() is called.
310		 * Think ddb(9) ...
311		 *
312		 * NOTE: If the CPU control register is not readable,
313		 * this will totally fail!  We'll just assume that
314		 * any system that has high vector support has a
315		 * readable CPU control register, for now.  If we
316		 * ever encounter one that does not, we'll have to
317		 * rethink this.
318		 */
319		cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
320	}
321}
322
323static void
324cpu_startup(void *dummy)
325{
326	struct pcb *pcb = thread0.td_pcb;
327#ifdef ARM_TP_ADDRESS
328#ifndef ARM_CACHE_LOCK_ENABLE
329	vm_page_t m;
330#endif
331#endif
332
333	cpu_setup("");
334	identify_arm_cpu();
335
336	printf("real memory  = %ju (%ju MB)\n", (uintmax_t)ptoa(physmem),
337	    (uintmax_t)ptoa(physmem) / 1048576);
338	realmem = physmem;
339
340	/*
341	 * Display the RAM layout.
342	 */
343	if (bootverbose) {
344		int indx;
345
346		printf("Physical memory chunk(s):\n");
347		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
348			vm_paddr_t size;
349
350			size = phys_avail[indx + 1] - phys_avail[indx];
351			printf("%#08jx - %#08jx, %ju bytes (%ju pages)\n",
352			    (uintmax_t)phys_avail[indx],
353			    (uintmax_t)phys_avail[indx + 1] - 1,
354			    (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
355		}
356	}
357
358	vm_ksubmap_init(&kmi);
359
360	printf("avail memory = %ju (%ju MB)\n",
361	    (uintmax_t)ptoa(cnt.v_free_count),
362	    (uintmax_t)ptoa(cnt.v_free_count) / 1048576);
363
364	bufinit();
365	vm_pager_bufferinit();
366	pcb->un_32.pcb32_und_sp = (u_int)thread0.td_kstack +
367	    USPACE_UNDEF_STACK_TOP;
368	pcb->un_32.pcb32_sp = (u_int)thread0.td_kstack +
369	    USPACE_SVC_STACK_TOP;
370	vector_page_setprot(VM_PROT_READ);
371	pmap_set_pcb_pagedir(pmap_kernel(), pcb);
372	pmap_postinit();
373#ifdef ARM_TP_ADDRESS
374#ifdef ARM_CACHE_LOCK_ENABLE
375	pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
376	arm_lock_cache_line(ARM_TP_ADDRESS);
377#else
378	m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
379	pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
380#endif
381	*(uint32_t *)ARM_RAS_START = 0;
382	*(uint32_t *)ARM_RAS_END = 0xffffffff;
383#endif
384}
385
386SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
387
388/*
389 * Flush the D-cache for non-DMA I/O so that the I-cache can
390 * be made coherent later.
391 */
392void
393cpu_flush_dcache(void *ptr, size_t len)
394{
395
396	cpu_dcache_wb_range((uintptr_t)ptr, len);
397	cpu_l2cache_wb_range((uintptr_t)ptr, len);
398}
399
400/* Get current clock frequency for the given cpu id. */
401int
402cpu_est_clockrate(int cpu_id, uint64_t *rate)
403{
404
405	return (ENXIO);
406}
407
408void
409cpu_idle(int busy)
410{
411
412#ifndef NO_EVENTTIMERS
413	if (!busy) {
414		critical_enter();
415		cpu_idleclock();
416	}
417#endif
418	cpu_sleep(0);
419#ifndef NO_EVENTTIMERS
420	if (!busy) {
421		cpu_activeclock();
422		critical_exit();
423	}
424#endif
425}
426
427int
428cpu_idle_wakeup(int cpu)
429{
430
431	return (0);
432}
433
434int
435fill_regs(struct thread *td, struct reg *regs)
436{
437	struct trapframe *tf = td->td_frame;
438	bcopy(&tf->tf_r0, regs->r, sizeof(regs->r));
439	regs->r_sp = tf->tf_usr_sp;
440	regs->r_lr = tf->tf_usr_lr;
441	regs->r_pc = tf->tf_pc;
442	regs->r_cpsr = tf->tf_spsr;
443	return (0);
444}
445int
446fill_fpregs(struct thread *td, struct fpreg *regs)
447{
448	bzero(regs, sizeof(*regs));
449	return (0);
450}
451
452int
453set_regs(struct thread *td, struct reg *regs)
454{
455	struct trapframe *tf = td->td_frame;
456
457	bcopy(regs->r, &tf->tf_r0, sizeof(regs->r));
458	tf->tf_usr_sp = regs->r_sp;
459	tf->tf_usr_lr = regs->r_lr;
460	tf->tf_pc = regs->r_pc;
461	tf->tf_spsr &=  ~PSR_FLAGS;
462	tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
463	return (0);
464}
465
466int
467set_fpregs(struct thread *td, struct fpreg *regs)
468{
469	return (0);
470}
471
472int
473fill_dbregs(struct thread *td, struct dbreg *regs)
474{
475	return (0);
476}
477int
478set_dbregs(struct thread *td, struct dbreg *regs)
479{
480	return (0);
481}
482
483
484static int
485ptrace_read_int(struct thread *td, vm_offset_t addr, u_int32_t *v)
486{
487	struct iovec iov;
488	struct uio uio;
489
490	PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
491	iov.iov_base = (caddr_t) v;
492	iov.iov_len = sizeof(u_int32_t);
493	uio.uio_iov = &iov;
494	uio.uio_iovcnt = 1;
495	uio.uio_offset = (off_t)addr;
496	uio.uio_resid = sizeof(u_int32_t);
497	uio.uio_segflg = UIO_SYSSPACE;
498	uio.uio_rw = UIO_READ;
499	uio.uio_td = td;
500	return proc_rwmem(td->td_proc, &uio);
501}
502
503static int
504ptrace_write_int(struct thread *td, vm_offset_t addr, u_int32_t v)
505{
506	struct iovec iov;
507	struct uio uio;
508
509	PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
510	iov.iov_base = (caddr_t) &v;
511	iov.iov_len = sizeof(u_int32_t);
512	uio.uio_iov = &iov;
513	uio.uio_iovcnt = 1;
514	uio.uio_offset = (off_t)addr;
515	uio.uio_resid = sizeof(u_int32_t);
516	uio.uio_segflg = UIO_SYSSPACE;
517	uio.uio_rw = UIO_WRITE;
518	uio.uio_td = td;
519	return proc_rwmem(td->td_proc, &uio);
520}
521
522int
523ptrace_single_step(struct thread *td)
524{
525	struct proc *p;
526	int error;
527
528	KASSERT(td->td_md.md_ptrace_instr == 0,
529	 ("Didn't clear single step"));
530	p = td->td_proc;
531	PROC_UNLOCK(p);
532	error = ptrace_read_int(td, td->td_frame->tf_pc + 4,
533	    &td->td_md.md_ptrace_instr);
534	if (error)
535		goto out;
536	error = ptrace_write_int(td, td->td_frame->tf_pc + 4,
537	    PTRACE_BREAKPOINT);
538	if (error)
539		td->td_md.md_ptrace_instr = 0;
540	td->td_md.md_ptrace_addr = td->td_frame->tf_pc + 4;
541out:
542	PROC_LOCK(p);
543	return (error);
544}
545
546int
547ptrace_clear_single_step(struct thread *td)
548{
549	struct proc *p;
550
551	if (td->td_md.md_ptrace_instr) {
552		p = td->td_proc;
553		PROC_UNLOCK(p);
554		ptrace_write_int(td, td->td_md.md_ptrace_addr,
555		    td->td_md.md_ptrace_instr);
556		PROC_LOCK(p);
557		td->td_md.md_ptrace_instr = 0;
558	}
559	return (0);
560}
561
562int
563ptrace_set_pc(struct thread *td, unsigned long addr)
564{
565	td->td_frame->tf_pc = addr;
566	return (0);
567}
568
569void
570cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
571{
572}
573
574void
575spinlock_enter(void)
576{
577	struct thread *td;
578	register_t cspr;
579
580	td = curthread;
581	if (td->td_md.md_spinlock_count == 0) {
582		cspr = disable_interrupts(I32_bit | F32_bit);
583		td->td_md.md_spinlock_count = 1;
584		td->td_md.md_saved_cspr = cspr;
585	} else
586		td->td_md.md_spinlock_count++;
587	critical_enter();
588}
589
590void
591spinlock_exit(void)
592{
593	struct thread *td;
594	register_t cspr;
595
596	td = curthread;
597	critical_exit();
598	cspr = td->td_md.md_saved_cspr;
599	td->td_md.md_spinlock_count--;
600	if (td->td_md.md_spinlock_count == 0)
601		restore_interrupts(cspr);
602}
603
604/*
605 * Clear registers on exec
606 */
607void
608exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
609{
610	struct trapframe *tf = td->td_frame;
611
612	memset(tf, 0, sizeof(*tf));
613	tf->tf_usr_sp = stack;
614	tf->tf_usr_lr = imgp->entry_addr;
615	tf->tf_svc_lr = 0x77777777;
616	tf->tf_pc = imgp->entry_addr;
617	tf->tf_spsr = PSR_USR32_MODE;
618}
619
620/*
621 * Get machine context.
622 */
623int
624get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
625{
626	struct trapframe *tf = td->td_frame;
627	__greg_t *gr = mcp->__gregs;
628
629	if (clear_ret & GET_MC_CLEAR_RET)
630		gr[_REG_R0] = 0;
631	else
632		gr[_REG_R0]   = tf->tf_r0;
633	gr[_REG_R1]   = tf->tf_r1;
634	gr[_REG_R2]   = tf->tf_r2;
635	gr[_REG_R3]   = tf->tf_r3;
636	gr[_REG_R4]   = tf->tf_r4;
637	gr[_REG_R5]   = tf->tf_r5;
638	gr[_REG_R6]   = tf->tf_r6;
639	gr[_REG_R7]   = tf->tf_r7;
640	gr[_REG_R8]   = tf->tf_r8;
641	gr[_REG_R9]   = tf->tf_r9;
642	gr[_REG_R10]  = tf->tf_r10;
643	gr[_REG_R11]  = tf->tf_r11;
644	gr[_REG_R12]  = tf->tf_r12;
645	gr[_REG_SP]   = tf->tf_usr_sp;
646	gr[_REG_LR]   = tf->tf_usr_lr;
647	gr[_REG_PC]   = tf->tf_pc;
648	gr[_REG_CPSR] = tf->tf_spsr;
649
650	return (0);
651}
652
653/*
654 * Set machine context.
655 *
656 * However, we don't set any but the user modifiable flags, and we won't
657 * touch the cs selector.
658 */
659int
660set_mcontext(struct thread *td, const mcontext_t *mcp)
661{
662	struct trapframe *tf = td->td_frame;
663	const __greg_t *gr = mcp->__gregs;
664
665	tf->tf_r0 = gr[_REG_R0];
666	tf->tf_r1 = gr[_REG_R1];
667	tf->tf_r2 = gr[_REG_R2];
668	tf->tf_r3 = gr[_REG_R3];
669	tf->tf_r4 = gr[_REG_R4];
670	tf->tf_r5 = gr[_REG_R5];
671	tf->tf_r6 = gr[_REG_R6];
672	tf->tf_r7 = gr[_REG_R7];
673	tf->tf_r8 = gr[_REG_R8];
674	tf->tf_r9 = gr[_REG_R9];
675	tf->tf_r10 = gr[_REG_R10];
676	tf->tf_r11 = gr[_REG_R11];
677	tf->tf_r12 = gr[_REG_R12];
678	tf->tf_usr_sp = gr[_REG_SP];
679	tf->tf_usr_lr = gr[_REG_LR];
680	tf->tf_pc = gr[_REG_PC];
681	tf->tf_spsr = gr[_REG_CPSR];
682
683	return (0);
684}
685
686/*
687 * MPSAFE
688 */
689int
690sys_sigreturn(td, uap)
691	struct thread *td;
692	struct sigreturn_args /* {
693		const struct __ucontext *sigcntxp;
694	} */ *uap;
695{
696	struct sigframe sf;
697	struct trapframe *tf;
698	int spsr;
699
700	if (uap == NULL)
701		return (EFAULT);
702	if (copyin(uap->sigcntxp, &sf, sizeof(sf)))
703		return (EFAULT);
704	/*
705	 * Make sure the processor mode has not been tampered with and
706	 * interrupts have not been disabled.
707	 */
708	spsr = sf.sf_uc.uc_mcontext.__gregs[_REG_CPSR];
709	if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
710	    (spsr & (I32_bit | F32_bit)) != 0)
711		return (EINVAL);
712		/* Restore register context. */
713	tf = td->td_frame;
714	set_mcontext(td, &sf.sf_uc.uc_mcontext);
715
716	/* Restore signal mask. */
717	kern_sigprocmask(td, SIG_SETMASK, &sf.sf_uc.uc_sigmask, NULL, 0);
718
719	return (EJUSTRETURN);
720}
721
722
723/*
724 * Construct a PCB from a trapframe. This is called from kdb_trap() where
725 * we want to start a backtrace from the function that caused us to enter
726 * the debugger. We have the context in the trapframe, but base the trace
727 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
728 * enough for a backtrace.
729 */
730void
731makectx(struct trapframe *tf, struct pcb *pcb)
732{
733	pcb->un_32.pcb32_r8 = tf->tf_r8;
734	pcb->un_32.pcb32_r9 = tf->tf_r9;
735	pcb->un_32.pcb32_r10 = tf->tf_r10;
736	pcb->un_32.pcb32_r11 = tf->tf_r11;
737	pcb->un_32.pcb32_r12 = tf->tf_r12;
738	pcb->un_32.pcb32_pc = tf->tf_pc;
739	pcb->un_32.pcb32_lr = tf->tf_usr_lr;
740	pcb->un_32.pcb32_sp = tf->tf_usr_sp;
741}
742
743/*
744 * Make a standard dump_avail array.  Can't make the phys_avail
745 * since we need to do that after we call pmap_bootstrap, but this
746 * is needed before pmap_boostrap.
747 *
748 * ARM_USE_SMALL_ALLOC uses dump_avail, so it must be filled before
749 * calling pmap_bootstrap.
750 */
751void
752arm_dump_avail_init(vm_offset_t ramsize, size_t max)
753{
754#ifdef LINUX_BOOT_ABI
755	/*
756	 * Linux boot loader passes us the actual banks of memory, so use them
757	 * to construct the dump_avail array.
758	 */
759	if (membanks > 0)
760	{
761		int i, j;
762
763		if (max < (membanks + 1) * 2)
764			panic("dump_avail[%d] too small for %d banks\n",
765			    max, membanks);
766		for (j = 0, i = 0; i < membanks; i++) {
767			dump_avail[j++] = round_page(memstart[i]);
768			dump_avail[j++] = trunc_page(memstart[i] + memsize[i]);
769		}
770		dump_avail[j++] = 0;
771		dump_avail[j++] = 0;
772		return;
773	}
774#endif
775	if (max < 4)
776		panic("dump_avail too small\n");
777
778	dump_avail[0] = round_page(PHYSADDR);
779	dump_avail[1] = trunc_page(PHYSADDR + ramsize);
780	dump_avail[2] = 0;
781	dump_avail[3] = 0;
782}
783
784/*
785 * Fake up a boot descriptor table
786 */
787vm_offset_t
788fake_preload_metadata(struct arm_boot_params *abp __unused)
789{
790#ifdef DDB
791	vm_offset_t zstart = 0, zend = 0;
792#endif
793	vm_offset_t lastaddr;
794	int i = 0;
795	static uint32_t fake_preload[35];
796
797	fake_preload[i++] = MODINFO_NAME;
798	fake_preload[i++] = strlen("kernel") + 1;
799	strcpy((char*)&fake_preload[i++], "kernel");
800	i += 1;
801	fake_preload[i++] = MODINFO_TYPE;
802	fake_preload[i++] = strlen("elf kernel") + 1;
803	strcpy((char*)&fake_preload[i++], "elf kernel");
804	i += 2;
805	fake_preload[i++] = MODINFO_ADDR;
806	fake_preload[i++] = sizeof(vm_offset_t);
807	fake_preload[i++] = KERNVIRTADDR;
808	fake_preload[i++] = MODINFO_SIZE;
809	fake_preload[i++] = sizeof(uint32_t);
810	fake_preload[i++] = (uint32_t)&end - KERNVIRTADDR;
811#ifdef DDB
812	if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
813		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
814		fake_preload[i++] = sizeof(vm_offset_t);
815		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
816		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
817		fake_preload[i++] = sizeof(vm_offset_t);
818		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
819		lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
820		zend = lastaddr;
821		zstart = *(uint32_t *)(KERNVIRTADDR + 4);
822		ksym_start = zstart;
823		ksym_end = zend;
824	} else
825#endif
826		lastaddr = (vm_offset_t)&end;
827	fake_preload[i++] = 0;
828	fake_preload[i] = 0;
829	preload_metadata = (void *)fake_preload;
830
831	return (lastaddr);
832}
833
834void
835pcpu0_init(void)
836{
837#if ARM_ARCH_6 || ARM_ARCH_7A || defined(CPU_MV_PJ4B)
838	set_pcpu(pcpup);
839#endif
840	pcpu_init(pcpup, 0, sizeof(struct pcpu));
841	PCPU_SET(curthread, &thread0);
842#ifdef ARM_VFP_SUPPORT
843	PCPU_SET(cpu, 0);
844#endif
845}
846
847#if defined(LINUX_BOOT_ABI)
848vm_offset_t
849linux_parse_boot_param(struct arm_boot_params *abp)
850{
851	struct arm_lbabi_tag *walker;
852
853	/*
854	 * Linux boot ABI: r0 = 0, r1 is the board type (!= 0) and r2
855	 * is atags or dtb pointer.  If all of these aren't satisfied,
856	 * then punt.
857	 */
858	if (!(abp->abp_r0 == 0 && abp->abp_r1 != 0 && abp->abp_r2 != 0))
859		return 0;
860
861	board_id = abp->abp_r1;
862	walker = (struct arm_lbabi_tag *)
863	    (abp->abp_r2 + KERNVIRTADDR - KERNPHYSADDR);
864
865	/* xxx - Need to also look for binary device tree */
866	if (ATAG_TAG(walker) != ATAG_CORE)
867		return 0;
868
869	atag_list = walker;
870	while (ATAG_TAG(walker) != ATAG_NONE) {
871		switch (ATAG_TAG(walker)) {
872		case ATAG_CORE:
873			break;
874		case ATAG_MEM:
875			if (membanks < LBABI_MAX_BANKS) {
876				memstart[membanks] = walker->u.tag_mem.start;
877				memsize[membanks] = walker->u.tag_mem.size;
878			}
879			membanks++;
880			break;
881		case ATAG_INITRD2:
882			break;
883		case ATAG_SERIAL:
884			serial = walker->u.tag_sn.low |
885			    ((uint64_t)walker->u.tag_sn.high << 32);
886			break;
887		case ATAG_REVISION:
888			revision = walker->u.tag_rev.rev;
889			break;
890		case ATAG_CMDLINE:
891			/* XXX open question: Parse this for boothowto? */
892			bcopy(walker->u.tag_cmd.command, linux_command_line,
893			      ATAG_SIZE(walker));
894			break;
895		default:
896			break;
897		}
898		walker = ATAG_NEXT(walker);
899	}
900
901	/* Save a copy for later */
902	bcopy(atag_list, atags,
903	    (char *)walker - (char *)atag_list + ATAG_SIZE(walker));
904
905	return fake_preload_metadata(abp);
906}
907#endif
908
909#if defined(FREEBSD_BOOT_LOADER)
910vm_offset_t
911freebsd_parse_boot_param(struct arm_boot_params *abp)
912{
913	vm_offset_t lastaddr = 0;
914	void *mdp;
915	void *kmdp;
916
917	/*
918	 * Mask metadata pointer: it is supposed to be on page boundary. If
919	 * the first argument (mdp) doesn't point to a valid address the
920	 * bootloader must have passed us something else than the metadata
921	 * ptr, so we give up.  Also give up if we cannot find metadta section
922	 * the loader creates that we get all this data out of.
923	 */
924
925	if ((mdp = (void *)(abp->abp_r0 & ~PAGE_MASK)) == NULL)
926		return 0;
927	preload_metadata = mdp;
928	kmdp = preload_search_by_type("elf kernel");
929	if (kmdp == NULL)
930		return 0;
931
932	boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
933	kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
934	lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
935#ifdef DDB
936	ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
937	ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
938#endif
939	preload_addr_relocate = KERNVIRTADDR - KERNPHYSADDR;
940	return lastaddr;
941}
942#endif
943
944vm_offset_t
945default_parse_boot_param(struct arm_boot_params *abp)
946{
947	vm_offset_t lastaddr;
948
949#if defined(LINUX_BOOT_ABI)
950	if ((lastaddr = linux_parse_boot_param(abp)) != 0)
951		return lastaddr;
952#endif
953#if defined(FREEBSD_BOOT_LOADER)
954	if ((lastaddr = freebsd_parse_boot_param(abp)) != 0)
955		return lastaddr;
956#endif
957	/* Fall back to hardcoded metadata. */
958	lastaddr = fake_preload_metadata(abp);
959
960	return lastaddr;
961}
962
963/*
964 * Stub version of the boot parameter parsing routine.  We are
965 * called early in initarm, before even VM has been initialized.
966 * This routine needs to preserve any data that the boot loader
967 * has passed in before the kernel starts to grow past the end
968 * of the BSS, traditionally the place boot-loaders put this data.
969 *
970 * Since this is called so early, things that depend on the vm system
971 * being setup (including access to some SoC's serial ports), about
972 * all that can be done in this routine is to copy the arguments.
973 *
974 * This is the default boot parameter parsing routine.  Individual
975 * kernels/boards can override this weak function with one of their
976 * own.  We just fake metadata...
977 */
978__weak_reference(default_parse_boot_param, parse_boot_param);
979
980/*
981 * Initialize proc0
982 */
983void
984init_proc0(vm_offset_t kstack)
985{
986	proc_linkup0(&proc0, &thread0);
987	thread0.td_kstack = kstack;
988	thread0.td_pcb = (struct pcb *)
989		(thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
990	thread0.td_pcb->pcb_flags = 0;
991	thread0.td_frame = &proc0_tf;
992	pcpup->pc_curpcb = thread0.td_pcb;
993}
994
995void
996set_stackptrs(int cpu)
997{
998
999	set_stackptr(PSR_IRQ32_MODE,
1000	    irqstack.pv_va + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1001	set_stackptr(PSR_ABT32_MODE,
1002	    abtstack.pv_va + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1003	set_stackptr(PSR_UND32_MODE,
1004	    undstack.pv_va + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1005}
1006
1007#ifdef FDT
1008static char *
1009kenv_next(char *cp)
1010{
1011
1012	if (cp != NULL) {
1013		while (*cp != 0)
1014			cp++;
1015		cp++;
1016		if (*cp == 0)
1017			cp = NULL;
1018	}
1019	return (cp);
1020}
1021
1022static void
1023print_kenv(void)
1024{
1025	int len;
1026	char *cp;
1027
1028	debugf("loader passed (static) kenv:\n");
1029	if (kern_envp == NULL) {
1030		debugf(" no env, null ptr\n");
1031		return;
1032	}
1033	debugf(" kern_envp = 0x%08x\n", (uint32_t)kern_envp);
1034
1035	len = 0;
1036	for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
1037		debugf(" %x %s\n", (uint32_t)cp, cp);
1038}
1039
1040static void
1041print_kernel_section_addr(void)
1042{
1043
1044	debugf("kernel image addresses:\n");
1045	debugf(" kernbase       = 0x%08x\n", (uint32_t)kernbase);
1046	debugf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
1047	debugf(" _edata         = 0x%08x\n", (uint32_t)_edata);
1048	debugf(" __bss_start    = 0x%08x\n", (uint32_t)__bss_start);
1049	debugf(" _end           = 0x%08x\n", (uint32_t)_end);
1050}
1051
1052static void
1053physmap_init(struct mem_region *availmem_regions, int availmem_regions_sz)
1054{
1055	int i, j, cnt;
1056	vm_offset_t phys_kernelend, kernload;
1057	uint32_t s, e, sz;
1058	struct mem_region *mp, *mp1;
1059
1060	phys_kernelend = KERNPHYSADDR + (virtual_avail - KERNVIRTADDR);
1061	kernload = KERNPHYSADDR;
1062
1063	/*
1064	 * Remove kernel physical address range from avail
1065	 * regions list. Page align all regions.
1066	 * Non-page aligned memory isn't very interesting to us.
1067	 * Also, sort the entries for ascending addresses.
1068	 */
1069	sz = 0;
1070	cnt = availmem_regions_sz;
1071	debugf("processing avail regions:\n");
1072	for (mp = availmem_regions; mp->mr_size; mp++) {
1073		s = mp->mr_start;
1074		e = mp->mr_start + mp->mr_size;
1075		debugf(" %08x-%08x -> ", s, e);
1076		/* Check whether this region holds all of the kernel. */
1077		if (s < kernload && e > phys_kernelend) {
1078			availmem_regions[cnt].mr_start = phys_kernelend;
1079			availmem_regions[cnt++].mr_size = e - phys_kernelend;
1080			e = kernload;
1081		}
1082		/* Look whether this regions starts within the kernel. */
1083		if (s >= kernload && s < phys_kernelend) {
1084			if (e <= phys_kernelend)
1085				goto empty;
1086			s = phys_kernelend;
1087		}
1088		/* Now look whether this region ends within the kernel. */
1089		if (e > kernload && e <= phys_kernelend) {
1090			if (s >= kernload) {
1091				goto empty;
1092			}
1093			e = kernload;
1094		}
1095		/* Now page align the start and size of the region. */
1096		s = round_page(s);
1097		e = trunc_page(e);
1098		if (e < s)
1099			e = s;
1100		sz = e - s;
1101		debugf("%08x-%08x = %x\n", s, e, sz);
1102
1103		/* Check whether some memory is left here. */
1104		if (sz == 0) {
1105		empty:
1106			printf("skipping\n");
1107			bcopy(mp + 1, mp,
1108			    (cnt - (mp - availmem_regions)) * sizeof(*mp));
1109			cnt--;
1110			mp--;
1111			continue;
1112		}
1113
1114		/* Do an insertion sort. */
1115		for (mp1 = availmem_regions; mp1 < mp; mp1++)
1116			if (s < mp1->mr_start)
1117				break;
1118		if (mp1 < mp) {
1119			bcopy(mp1, mp1 + 1, (char *)mp - (char *)mp1);
1120			mp1->mr_start = s;
1121			mp1->mr_size = sz;
1122		} else {
1123			mp->mr_start = s;
1124			mp->mr_size = sz;
1125		}
1126	}
1127	availmem_regions_sz = cnt;
1128
1129	/* Fill in phys_avail table, based on availmem_regions */
1130	debugf("fill in phys_avail:\n");
1131	for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
1132
1133		debugf(" region: 0x%08x - 0x%08x (0x%08x)\n",
1134		    availmem_regions[i].mr_start,
1135		    availmem_regions[i].mr_start + availmem_regions[i].mr_size,
1136		    availmem_regions[i].mr_size);
1137
1138		/*
1139		 * We should not map the page at PA 0x0000000, the VM can't
1140		 * handle it, as pmap_extract() == 0 means failure.
1141		 */
1142		if (availmem_regions[i].mr_start > 0 ||
1143		    availmem_regions[i].mr_size > PAGE_SIZE) {
1144			phys_avail[j] = availmem_regions[i].mr_start;
1145			if (phys_avail[j] == 0)
1146				phys_avail[j] += PAGE_SIZE;
1147			phys_avail[j + 1] = availmem_regions[i].mr_start +
1148			    availmem_regions[i].mr_size;
1149		} else
1150			j -= 2;
1151	}
1152	phys_avail[j] = 0;
1153	phys_avail[j + 1] = 0;
1154}
1155
1156void *
1157initarm(struct arm_boot_params *abp)
1158{
1159	struct mem_region availmem_regions[FDT_MEM_REGIONS];
1160	struct pv_addr kernel_l1pt;
1161	struct pv_addr dpcpu;
1162	vm_offset_t dtbp, freemempos, l2_start, lastaddr;
1163	vm_offset_t pmap_bootstrap_lastaddr;
1164	uint32_t memsize, l2size;
1165	char *env;
1166	void *kmdp;
1167	u_int l1pagetable;
1168	int i = 0, j = 0, err_devmap = 0;
1169	int availmem_regions_sz;
1170
1171	lastaddr = parse_boot_param(abp);
1172	memsize = 0;
1173	set_cpufuncs();
1174
1175	/*
1176	 * Find the dtb passed in by the boot loader.
1177	 */
1178	kmdp = preload_search_by_type("elf kernel");
1179	if (kmdp != NULL)
1180		dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1181	else
1182		dtbp = (vm_offset_t)NULL;
1183
1184#if defined(FDT_DTB_STATIC)
1185	/*
1186	 * In case the device tree blob was not retrieved (from metadata) try
1187	 * to use the statically embedded one.
1188	 */
1189	if (dtbp == (vm_offset_t)NULL)
1190		dtbp = (vm_offset_t)&fdt_static_dtb;
1191#endif
1192
1193	if (OF_install(OFW_FDT, 0) == FALSE)
1194		while (1);
1195
1196	if (OF_init((void *)dtbp) != 0)
1197		while (1);
1198
1199	/* Grab physical memory regions information from device tree. */
1200	if (fdt_get_mem_regions(availmem_regions, &availmem_regions_sz,
1201	    &memsize) != 0)
1202		while(1);
1203
1204	/* Platform-specific initialisation */
1205	pmap_bootstrap_lastaddr = initarm_lastaddr();
1206
1207	pcpu0_init();
1208
1209	/* Calculate number of L2 tables needed for mapping vm_page_array */
1210	l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
1211	l2size = (l2size >> L1_S_SHIFT) + 1;
1212
1213	/*
1214	 * Add one table for end of kernel map, one for stacks, msgbuf and
1215	 * L1 and L2 tables map and one for vectors map.
1216	 */
1217	l2size += 3;
1218
1219	/* Make it divisible by 4 */
1220	l2size = (l2size + 3) & ~3;
1221
1222#define KERNEL_TEXT_BASE (KERNBASE)
1223	freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
1224
1225	/* Define a macro to simplify memory allocation */
1226#define valloc_pages(var, np)                   \
1227	alloc_pages((var).pv_va, (np));         \
1228	(var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
1229
1230#define alloc_pages(var, np)			\
1231	(var) = freemempos;		\
1232	freemempos += (np * PAGE_SIZE);		\
1233	memset((char *)(var), 0, ((np) * PAGE_SIZE));
1234
1235	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
1236		freemempos += PAGE_SIZE;
1237	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
1238
1239	for (i = 0; i < l2size; ++i) {
1240		if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
1241			valloc_pages(kernel_pt_table[i],
1242			    L2_TABLE_SIZE / PAGE_SIZE);
1243			j = i;
1244		} else {
1245			kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
1246			    L2_TABLE_SIZE_REAL * (i - j);
1247			kernel_pt_table[i].pv_pa =
1248			    kernel_pt_table[i].pv_va - KERNVIRTADDR +
1249			    KERNPHYSADDR;
1250
1251		}
1252	}
1253	/*
1254	 * Allocate a page for the system page mapped to 0x00000000
1255	 * or 0xffff0000. This page will just contain the system vectors
1256	 * and can be shared by all processes.
1257	 */
1258	valloc_pages(systempage, 1);
1259
1260	/* Allocate dynamic per-cpu area. */
1261	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
1262	dpcpu_init((void *)dpcpu.pv_va, 0);
1263
1264	/* Allocate stacks for all modes */
1265	valloc_pages(irqstack, (IRQ_STACK_SIZE * MAXCPU));
1266	valloc_pages(abtstack, (ABT_STACK_SIZE * MAXCPU));
1267	valloc_pages(undstack, (UND_STACK_SIZE * MAXCPU));
1268	valloc_pages(kernelstack, (KSTACK_PAGES * MAXCPU));
1269
1270	init_param1();
1271
1272	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
1273
1274	/*
1275	 * Now we start construction of the L1 page table
1276	 * We start by mapping the L2 page tables into the L1.
1277	 * This means that we can replace L1 mappings later on if necessary
1278	 */
1279	l1pagetable = kernel_l1pt.pv_va;
1280
1281	/*
1282	 * Try to map as much as possible of kernel text and data using
1283	 * 1MB section mapping and for the rest of initial kernel address
1284	 * space use L2 coarse tables.
1285	 *
1286	 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
1287	 * and kernel structures
1288	 */
1289	l2_start = lastaddr & ~(L1_S_OFFSET);
1290	for (i = 0 ; i < l2size - 1; i++)
1291		pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
1292		    &kernel_pt_table[i]);
1293
1294	pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
1295
1296	/* Map kernel code and data */
1297	pmap_map_chunk(l1pagetable, KERNVIRTADDR, KERNPHYSADDR,
1298	   (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
1299	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1300
1301
1302	/* Map L1 directory and allocated L2 page tables */
1303	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
1304	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1305
1306	pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
1307	    kernel_pt_table[0].pv_pa,
1308	    L2_TABLE_SIZE_REAL * l2size,
1309	    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1310
1311	/* Map allocated DPCPU, stacks and msgbuf */
1312	pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
1313	    freemempos - dpcpu.pv_va,
1314	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1315
1316	/* Link and map the vector page */
1317	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
1318	    &kernel_pt_table[l2size - 1]);
1319	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
1320	    VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE, PTE_CACHE);
1321
1322	/* Map pmap_devmap[] entries */
1323	err_devmap = platform_devmap_init();
1324	pmap_devmap_bootstrap(l1pagetable, pmap_devmap_bootstrap_table);
1325
1326	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
1327	    DOMAIN_CLIENT);
1328	pmap_pa = kernel_l1pt.pv_pa;
1329	setttb(kernel_l1pt.pv_pa);
1330	cpu_tlb_flushID();
1331	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
1332
1333	/*
1334	 * Only after the SOC registers block is mapped we can perform device
1335	 * tree fixups, as they may attempt to read parameters from hardware.
1336	 */
1337	OF_interpret("perform-fixup", 0);
1338
1339	initarm_gpio_init();
1340
1341	cninit();
1342
1343	physmem = memsize / PAGE_SIZE;
1344
1345	debugf("initarm: console initialized\n");
1346	debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
1347	debugf(" boothowto = 0x%08x\n", boothowto);
1348	debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
1349	print_kernel_section_addr();
1350	print_kenv();
1351
1352	env = getenv("kernelname");
1353	if (env != NULL)
1354		strlcpy(kernelname, env, sizeof(kernelname));
1355
1356	if (err_devmap != 0)
1357		printf("WARNING: could not fully configure devmap, error=%d\n",
1358		    err_devmap);
1359
1360	initarm_late_init();
1361
1362	/*
1363	 * Pages were allocated during the secondary bootstrap for the
1364	 * stacks for different CPU modes.
1365	 * We must now set the r13 registers in the different CPU modes to
1366	 * point to these stacks.
1367	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1368	 * of the stack memory.
1369	 */
1370	cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
1371
1372	set_stackptrs(0);
1373
1374	/*
1375	 * We must now clean the cache again....
1376	 * Cleaning may be done by reading new data to displace any
1377	 * dirty data in the cache. This will have happened in setttb()
1378	 * but since we are boot strapping the addresses used for the read
1379	 * may have just been remapped and thus the cache could be out
1380	 * of sync. A re-clean after the switch will cure this.
1381	 * After booting there are no gross relocations of the kernel thus
1382	 * this problem will not occur after initarm().
1383	 */
1384	cpu_idcache_wbinv_all();
1385
1386	/* Set stack for exception handlers */
1387	data_abort_handler_address = (u_int)data_abort_handler;
1388	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
1389	undefined_handler_address = (u_int)undefinedinstruction_bounce;
1390	undefined_init();
1391
1392	init_proc0(kernelstack.pv_va);
1393
1394	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1395	arm_dump_avail_init(memsize, sizeof(dump_avail) / sizeof(dump_avail[0]));
1396	pmap_bootstrap(freemempos, pmap_bootstrap_lastaddr, &kernel_l1pt);
1397	msgbufp = (void *)msgbufpv.pv_va;
1398	msgbufinit(msgbufp, msgbufsize);
1399	mutex_init();
1400
1401	/*
1402	 * Prepare map of physical memory regions available to vm subsystem.
1403	 */
1404	physmap_init(availmem_regions, availmem_regions_sz);
1405
1406	/* Do basic tuning, hz etc */
1407	init_param2(physmem);
1408	kdb_init();
1409
1410	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
1411	    sizeof(struct pcb)));
1412}
1413#endif
1414
1415