1 /* $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $ */
4 * Copyright (c) 2004 Olivier Houchard
5 * Copyright (c) 1994-1998 Mark Brinicombe.
6 * Copyright (c) 1994 Brini.
9 * This code is derived from software written for Brini by Mark Brinicombe
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
39 * Machine dependant functions for kernel setup
42 * Updated : 18/04/01 updated for new wscons
45 #include "opt_compat.h"
47 #include "opt_platform.h"
48 #include "opt_sched.h"
49 #include "opt_timer.h"
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
54 #include <sys/param.h>
56 #include <sys/systm.h>
63 #include <sys/imgact.h>
65 #include <sys/kernel.h>
67 #include <sys/linker.h>
69 #include <sys/malloc.h>
70 #include <sys/msgbuf.h>
71 #include <sys/mutex.h>
73 #include <sys/ptrace.h>
74 #include <sys/rwlock.h>
75 #include <sys/sched.h>
76 #include <sys/signalvar.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysctl.h>
79 #include <sys/sysent.h>
80 #include <sys/sysproto.h>
85 #include <vm/vm_map.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pager.h>
90 #include <machine/armreg.h>
91 #include <machine/atags.h>
92 #include <machine/cpu.h>
93 #include <machine/machdep.h>
94 #include <machine/md_var.h>
95 #include <machine/metadata.h>
96 #include <machine/pcb.h>
97 #include <machine/pmap.h>
98 #include <machine/reg.h>
99 #include <machine/trap.h>
100 #include <machine/undefined.h>
101 #include <machine/vmparam.h>
102 #include <machine/sysarch.h>
105 #include <dev/fdt/fdt_common.h>
106 #include <dev/ofw/openfirm.h>
110 #define debugf(fmt, args...) printf(fmt, ##args)
112 #define debugf(fmt, args...)
115 struct pcpu __pcpu[MAXCPU];
116 struct pcpu *pcpup = &__pcpu[0];
118 static struct trapframe proc0_tf;
119 uint32_t cpu_reset_address = 0;
121 vm_offset_t vector_page;
125 int (*_arm_memcpy)(void *, void *, int, int) = NULL;
126 int (*_arm_bzero)(void *, int, int) = NULL;
127 int _min_memcpy_size = 0;
128 int _min_bzero_size = 0;
132 extern vm_offset_t ksym_start, ksym_end;
137 * This is the number of L2 page tables required for covering max
138 * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
139 * stacks etc.), uprounded to be divisible by 4.
141 #define KERNEL_PT_MAX 78
143 static struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
145 vm_paddr_t phys_avail[10];
146 vm_paddr_t dump_avail[4];
148 extern u_int data_abort_handler_address;
149 extern u_int prefetch_abort_handler_address;
150 extern u_int undefined_handler_address;
154 struct pv_addr systempage;
155 static struct pv_addr msgbufpv;
156 struct pv_addr irqstack;
157 struct pv_addr undstack;
158 struct pv_addr abtstack;
159 static struct pv_addr kernelstack;
161 const struct pmap_devmap *pmap_devmap_bootstrap_table;
164 #if defined(LINUX_BOOT_ABI)
165 #define LBABI_MAX_BANKS 10
168 struct arm_lbabi_tag *atag_list;
169 char linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
170 char atags[LBABI_MAX_COMMAND_LINE * 2];
171 uint32_t memstart[LBABI_MAX_BANKS];
172 uint32_t memsize[LBABI_MAX_BANKS];
176 static uint32_t board_revision;
177 /* hex representation of uint64_t */
178 static char board_serial[32];
180 SYSCTL_NODE(_hw, OID_AUTO, board, CTLFLAG_RD, 0, "Board attributes");
181 SYSCTL_UINT(_hw_board, OID_AUTO, revision, CTLFLAG_RD,
182 &board_revision, 0, "Board revision");
183 SYSCTL_STRING(_hw_board, OID_AUTO, serial, CTLFLAG_RD,
184 board_serial, 0, "Board serial");
187 SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
188 &vfp_exists, 0, "Floating point support enabled");
191 board_set_serial(uint64_t serial)
194 snprintf(board_serial, sizeof(board_serial)-1,
199 board_set_revision(uint32_t revision)
202 board_revision = revision;
206 sendsig(catcher, ksi, mask)
213 struct trapframe *tf;
214 struct sigframe *fp, frame;
222 PROC_LOCK_ASSERT(p, MA_OWNED);
223 sig = ksi->ksi_signo;
224 code = ksi->ksi_code;
226 mtx_assert(&psp->ps_mtx, MA_OWNED);
228 onstack = sigonstack(tf->tf_usr_sp);
230 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
233 /* Allocate and validate space for the signal handler context. */
234 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
235 SIGISMEMBER(psp->ps_sigonstack, sig)) {
236 fp = (struct sigframe *)(td->td_sigstk.ss_sp +
237 td->td_sigstk.ss_size);
238 #if defined(COMPAT_43)
239 td->td_sigstk.ss_flags |= SS_ONSTACK;
242 fp = (struct sigframe *)td->td_frame->tf_usr_sp;
244 /* make room on the stack */
247 /* make the stack aligned */
248 fp = (struct sigframe *)STACKALIGN(fp);
249 /* Populate the siginfo frame. */
250 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
251 frame.sf_si = ksi->ksi_info;
252 frame.sf_uc.uc_sigmask = *mask;
253 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
254 ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
255 frame.sf_uc.uc_stack = td->td_sigstk;
256 mtx_unlock(&psp->ps_mtx);
257 PROC_UNLOCK(td->td_proc);
259 /* Copy the sigframe out to the user's stack. */
260 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
261 /* Process has trashed its stack. Kill it. */
262 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
267 /* Translate the signal if appropriate. */
268 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
269 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
272 * Build context to run handler in. We invoke the handler
273 * directly, only returning via the trampoline. Note the
274 * trampoline version numbers are coordinated with machine-
275 * dependent code in libc.
279 tf->tf_r1 = (register_t)&fp->sf_si;
280 tf->tf_r2 = (register_t)&fp->sf_uc;
282 /* the trampoline uses r5 as the uc address */
283 tf->tf_r5 = (register_t)&fp->sf_uc;
284 tf->tf_pc = (register_t)catcher;
285 tf->tf_usr_sp = (register_t)fp;
286 tf->tf_usr_lr = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
288 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
292 mtx_lock(&psp->ps_mtx);
295 struct kva_md_info kmi;
300 * Initialize the vector page, and select whether or not to
301 * relocate the vectors.
303 * NOTE: We expect the vector page to be mapped at its expected
307 extern unsigned int page0[], page0_data[];
309 arm_vector_init(vm_offset_t va, int which)
311 unsigned int *vectors = (int *) va;
312 unsigned int *vectors_data = vectors + (page0_data - page0);
316 * Loop through the vectors we're taking over, and copy the
317 * vector's insn and data word.
319 for (vec = 0; vec < ARM_NVEC; vec++) {
320 if ((which & (1 << vec)) == 0) {
321 /* Don't want to take over this vector. */
324 vectors[vec] = page0[vec];
325 vectors_data[vec] = page0_data[vec];
328 /* Now sync the vectors. */
329 cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
333 if (va == ARM_VECTORS_HIGH) {
335 * Assume the MD caller knows what it's doing here, and
336 * really does want the vector page relocated.
338 * Note: This has to be done here (and not just in
339 * cpu_setup()) because the vector page needs to be
340 * accessible *before* cpu_startup() is called.
343 * NOTE: If the CPU control register is not readable,
344 * this will totally fail! We'll just assume that
345 * any system that has high vector support has a
346 * readable CPU control register, for now. If we
347 * ever encounter one that does not, we'll have to
350 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
355 cpu_startup(void *dummy)
357 struct pcb *pcb = thread0.td_pcb;
358 #ifdef ARM_TP_ADDRESS
359 #ifndef ARM_CACHE_LOCK_ENABLE
367 printf("real memory = %ju (%ju MB)\n", (uintmax_t)ptoa(physmem),
368 (uintmax_t)ptoa(physmem) / 1048576);
372 * Display the RAM layout.
377 printf("Physical memory chunk(s):\n");
378 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
381 size = phys_avail[indx + 1] - phys_avail[indx];
382 printf("%#08jx - %#08jx, %ju bytes (%ju pages)\n",
383 (uintmax_t)phys_avail[indx],
384 (uintmax_t)phys_avail[indx + 1] - 1,
385 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
389 vm_ksubmap_init(&kmi);
391 printf("avail memory = %ju (%ju MB)\n",
392 (uintmax_t)ptoa(cnt.v_free_count),
393 (uintmax_t)ptoa(cnt.v_free_count) / 1048576);
396 vm_pager_bufferinit();
397 pcb->un_32.pcb32_und_sp = (u_int)thread0.td_kstack +
398 USPACE_UNDEF_STACK_TOP;
399 pcb->un_32.pcb32_sp = (u_int)thread0.td_kstack +
400 USPACE_SVC_STACK_TOP;
401 vector_page_setprot(VM_PROT_READ);
402 pmap_set_pcb_pagedir(pmap_kernel(), pcb);
404 #ifdef ARM_TP_ADDRESS
405 #ifdef ARM_CACHE_LOCK_ENABLE
406 pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
407 arm_lock_cache_line(ARM_TP_ADDRESS);
409 m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
410 pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
412 *(uint32_t *)ARM_RAS_START = 0;
413 *(uint32_t *)ARM_RAS_END = 0xffffffff;
417 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
420 * Flush the D-cache for non-DMA I/O so that the I-cache can
421 * be made coherent later.
424 cpu_flush_dcache(void *ptr, size_t len)
427 cpu_dcache_wb_range((uintptr_t)ptr, len);
428 cpu_l2cache_wb_range((uintptr_t)ptr, len);
431 /* Get current clock frequency for the given cpu id. */
433 cpu_est_clockrate(int cpu_id, uint64_t *rate)
443 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d",
445 #ifndef NO_EVENTTIMERS
451 if (!sched_runnable())
453 #ifndef NO_EVENTTIMERS
459 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done",
464 cpu_idle_wakeup(int cpu)
471 fill_regs(struct thread *td, struct reg *regs)
473 struct trapframe *tf = td->td_frame;
474 bcopy(&tf->tf_r0, regs->r, sizeof(regs->r));
475 regs->r_sp = tf->tf_usr_sp;
476 regs->r_lr = tf->tf_usr_lr;
477 regs->r_pc = tf->tf_pc;
478 regs->r_cpsr = tf->tf_spsr;
482 fill_fpregs(struct thread *td, struct fpreg *regs)
484 bzero(regs, sizeof(*regs));
489 set_regs(struct thread *td, struct reg *regs)
491 struct trapframe *tf = td->td_frame;
493 bcopy(regs->r, &tf->tf_r0, sizeof(regs->r));
494 tf->tf_usr_sp = regs->r_sp;
495 tf->tf_usr_lr = regs->r_lr;
496 tf->tf_pc = regs->r_pc;
497 tf->tf_spsr &= ~PSR_FLAGS;
498 tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
503 set_fpregs(struct thread *td, struct fpreg *regs)
509 fill_dbregs(struct thread *td, struct dbreg *regs)
514 set_dbregs(struct thread *td, struct dbreg *regs)
521 ptrace_read_int(struct thread *td, vm_offset_t addr, u_int32_t *v)
526 PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
527 iov.iov_base = (caddr_t) v;
528 iov.iov_len = sizeof(u_int32_t);
531 uio.uio_offset = (off_t)addr;
532 uio.uio_resid = sizeof(u_int32_t);
533 uio.uio_segflg = UIO_SYSSPACE;
534 uio.uio_rw = UIO_READ;
536 return proc_rwmem(td->td_proc, &uio);
540 ptrace_write_int(struct thread *td, vm_offset_t addr, u_int32_t v)
545 PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
546 iov.iov_base = (caddr_t) &v;
547 iov.iov_len = sizeof(u_int32_t);
550 uio.uio_offset = (off_t)addr;
551 uio.uio_resid = sizeof(u_int32_t);
552 uio.uio_segflg = UIO_SYSSPACE;
553 uio.uio_rw = UIO_WRITE;
555 return proc_rwmem(td->td_proc, &uio);
559 ptrace_single_step(struct thread *td)
564 KASSERT(td->td_md.md_ptrace_instr == 0,
565 ("Didn't clear single step"));
568 error = ptrace_read_int(td, td->td_frame->tf_pc + 4,
569 &td->td_md.md_ptrace_instr);
572 error = ptrace_write_int(td, td->td_frame->tf_pc + 4,
575 td->td_md.md_ptrace_instr = 0;
576 td->td_md.md_ptrace_addr = td->td_frame->tf_pc + 4;
583 ptrace_clear_single_step(struct thread *td)
587 if (td->td_md.md_ptrace_instr) {
590 ptrace_write_int(td, td->td_md.md_ptrace_addr,
591 td->td_md.md_ptrace_instr);
593 td->td_md.md_ptrace_instr = 0;
599 ptrace_set_pc(struct thread *td, unsigned long addr)
601 td->td_frame->tf_pc = addr;
606 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
617 if (td->td_md.md_spinlock_count == 0) {
618 cspr = disable_interrupts(I32_bit | F32_bit);
619 td->td_md.md_spinlock_count = 1;
620 td->td_md.md_saved_cspr = cspr;
622 td->td_md.md_spinlock_count++;
634 cspr = td->td_md.md_saved_cspr;
635 td->td_md.md_spinlock_count--;
636 if (td->td_md.md_spinlock_count == 0)
637 restore_interrupts(cspr);
641 * Clear registers on exec
644 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
646 struct trapframe *tf = td->td_frame;
648 memset(tf, 0, sizeof(*tf));
649 tf->tf_usr_sp = stack;
650 tf->tf_usr_lr = imgp->entry_addr;
651 tf->tf_svc_lr = 0x77777777;
652 tf->tf_pc = imgp->entry_addr;
653 tf->tf_spsr = PSR_USR32_MODE;
657 * Get machine context.
660 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
662 struct trapframe *tf = td->td_frame;
663 __greg_t *gr = mcp->__gregs;
665 if (clear_ret & GET_MC_CLEAR_RET)
668 gr[_REG_R0] = tf->tf_r0;
669 gr[_REG_R1] = tf->tf_r1;
670 gr[_REG_R2] = tf->tf_r2;
671 gr[_REG_R3] = tf->tf_r3;
672 gr[_REG_R4] = tf->tf_r4;
673 gr[_REG_R5] = tf->tf_r5;
674 gr[_REG_R6] = tf->tf_r6;
675 gr[_REG_R7] = tf->tf_r7;
676 gr[_REG_R8] = tf->tf_r8;
677 gr[_REG_R9] = tf->tf_r9;
678 gr[_REG_R10] = tf->tf_r10;
679 gr[_REG_R11] = tf->tf_r11;
680 gr[_REG_R12] = tf->tf_r12;
681 gr[_REG_SP] = tf->tf_usr_sp;
682 gr[_REG_LR] = tf->tf_usr_lr;
683 gr[_REG_PC] = tf->tf_pc;
684 gr[_REG_CPSR] = tf->tf_spsr;
690 * Set machine context.
692 * However, we don't set any but the user modifiable flags, and we won't
693 * touch the cs selector.
696 set_mcontext(struct thread *td, const mcontext_t *mcp)
698 struct trapframe *tf = td->td_frame;
699 const __greg_t *gr = mcp->__gregs;
701 tf->tf_r0 = gr[_REG_R0];
702 tf->tf_r1 = gr[_REG_R1];
703 tf->tf_r2 = gr[_REG_R2];
704 tf->tf_r3 = gr[_REG_R3];
705 tf->tf_r4 = gr[_REG_R4];
706 tf->tf_r5 = gr[_REG_R5];
707 tf->tf_r6 = gr[_REG_R6];
708 tf->tf_r7 = gr[_REG_R7];
709 tf->tf_r8 = gr[_REG_R8];
710 tf->tf_r9 = gr[_REG_R9];
711 tf->tf_r10 = gr[_REG_R10];
712 tf->tf_r11 = gr[_REG_R11];
713 tf->tf_r12 = gr[_REG_R12];
714 tf->tf_usr_sp = gr[_REG_SP];
715 tf->tf_usr_lr = gr[_REG_LR];
716 tf->tf_pc = gr[_REG_PC];
717 tf->tf_spsr = gr[_REG_CPSR];
726 sys_sigreturn(td, uap)
728 struct sigreturn_args /* {
729 const struct __ucontext *sigcntxp;
733 struct trapframe *tf;
738 if (copyin(uap->sigcntxp, &sf, sizeof(sf)))
741 * Make sure the processor mode has not been tampered with and
742 * interrupts have not been disabled.
744 spsr = sf.sf_uc.uc_mcontext.__gregs[_REG_CPSR];
745 if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
746 (spsr & (I32_bit | F32_bit)) != 0)
748 /* Restore register context. */
750 set_mcontext(td, &sf.sf_uc.uc_mcontext);
752 /* Restore signal mask. */
753 kern_sigprocmask(td, SIG_SETMASK, &sf.sf_uc.uc_sigmask, NULL, 0);
755 return (EJUSTRETURN);
760 * Construct a PCB from a trapframe. This is called from kdb_trap() where
761 * we want to start a backtrace from the function that caused us to enter
762 * the debugger. We have the context in the trapframe, but base the trace
763 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
764 * enough for a backtrace.
767 makectx(struct trapframe *tf, struct pcb *pcb)
769 pcb->un_32.pcb32_r8 = tf->tf_r8;
770 pcb->un_32.pcb32_r9 = tf->tf_r9;
771 pcb->un_32.pcb32_r10 = tf->tf_r10;
772 pcb->un_32.pcb32_r11 = tf->tf_r11;
773 pcb->un_32.pcb32_r12 = tf->tf_r12;
774 pcb->un_32.pcb32_pc = tf->tf_pc;
775 pcb->un_32.pcb32_lr = tf->tf_usr_lr;
776 pcb->un_32.pcb32_sp = tf->tf_usr_sp;
780 * Make a standard dump_avail array. Can't make the phys_avail
781 * since we need to do that after we call pmap_bootstrap, but this
782 * is needed before pmap_boostrap.
784 * ARM_USE_SMALL_ALLOC uses dump_avail, so it must be filled before
785 * calling pmap_bootstrap.
788 arm_dump_avail_init(vm_offset_t ramsize, size_t max)
790 #ifdef LINUX_BOOT_ABI
792 * Linux boot loader passes us the actual banks of memory, so use them
793 * to construct the dump_avail array.
799 if (max < (membanks + 1) * 2)
800 panic("dump_avail[%d] too small for %d banks\n",
802 for (j = 0, i = 0; i < membanks; i++) {
803 dump_avail[j++] = round_page(memstart[i]);
804 dump_avail[j++] = trunc_page(memstart[i] + memsize[i]);
812 panic("dump_avail too small\n");
814 dump_avail[0] = round_page(PHYSADDR);
815 dump_avail[1] = trunc_page(PHYSADDR + ramsize);
821 * Fake up a boot descriptor table
824 fake_preload_metadata(struct arm_boot_params *abp __unused)
827 vm_offset_t zstart = 0, zend = 0;
829 vm_offset_t lastaddr;
831 static uint32_t fake_preload[35];
833 fake_preload[i++] = MODINFO_NAME;
834 fake_preload[i++] = strlen("kernel") + 1;
835 strcpy((char*)&fake_preload[i++], "kernel");
837 fake_preload[i++] = MODINFO_TYPE;
838 fake_preload[i++] = strlen("elf kernel") + 1;
839 strcpy((char*)&fake_preload[i++], "elf kernel");
841 fake_preload[i++] = MODINFO_ADDR;
842 fake_preload[i++] = sizeof(vm_offset_t);
843 fake_preload[i++] = KERNVIRTADDR;
844 fake_preload[i++] = MODINFO_SIZE;
845 fake_preload[i++] = sizeof(uint32_t);
846 fake_preload[i++] = (uint32_t)&end - KERNVIRTADDR;
848 if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
849 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
850 fake_preload[i++] = sizeof(vm_offset_t);
851 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
852 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
853 fake_preload[i++] = sizeof(vm_offset_t);
854 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
855 lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
857 zstart = *(uint32_t *)(KERNVIRTADDR + 4);
862 lastaddr = (vm_offset_t)&end;
863 fake_preload[i++] = 0;
865 preload_metadata = (void *)fake_preload;
873 #if ARM_ARCH_6 || ARM_ARCH_7A || defined(CPU_MV_PJ4B)
876 pcpu_init(pcpup, 0, sizeof(struct pcpu));
877 PCPU_SET(curthread, &thread0);
883 #if defined(LINUX_BOOT_ABI)
885 linux_parse_boot_param(struct arm_boot_params *abp)
887 struct arm_lbabi_tag *walker;
892 * Linux boot ABI: r0 = 0, r1 is the board type (!= 0) and r2
893 * is atags or dtb pointer. If all of these aren't satisfied,
896 if (!(abp->abp_r0 == 0 && abp->abp_r1 != 0 && abp->abp_r2 != 0))
899 board_id = abp->abp_r1;
900 walker = (struct arm_lbabi_tag *)
901 (abp->abp_r2 + KERNVIRTADDR - KERNPHYSADDR);
903 /* xxx - Need to also look for binary device tree */
904 if (ATAG_TAG(walker) != ATAG_CORE)
908 while (ATAG_TAG(walker) != ATAG_NONE) {
909 switch (ATAG_TAG(walker)) {
913 if (membanks < LBABI_MAX_BANKS) {
914 memstart[membanks] = walker->u.tag_mem.start;
915 memsize[membanks] = walker->u.tag_mem.size;
922 serial = walker->u.tag_sn.low |
923 ((uint64_t)walker->u.tag_sn.high << 32);
924 board_set_serial(serial);
927 revision = walker->u.tag_rev.rev;
928 board_set_revision(revision);
931 /* XXX open question: Parse this for boothowto? */
932 bcopy(walker->u.tag_cmd.command, linux_command_line,
938 walker = ATAG_NEXT(walker);
941 /* Save a copy for later */
942 bcopy(atag_list, atags,
943 (char *)walker - (char *)atag_list + ATAG_SIZE(walker));
945 return fake_preload_metadata(abp);
949 #if defined(FREEBSD_BOOT_LOADER)
951 freebsd_parse_boot_param(struct arm_boot_params *abp)
953 vm_offset_t lastaddr = 0;
958 * Mask metadata pointer: it is supposed to be on page boundary. If
959 * the first argument (mdp) doesn't point to a valid address the
960 * bootloader must have passed us something else than the metadata
961 * ptr, so we give up. Also give up if we cannot find metadta section
962 * the loader creates that we get all this data out of.
965 if ((mdp = (void *)(abp->abp_r0 & ~PAGE_MASK)) == NULL)
967 preload_metadata = mdp;
968 kmdp = preload_search_by_type("elf kernel");
972 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
973 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
974 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
976 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
977 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
979 preload_addr_relocate = KERNVIRTADDR - KERNPHYSADDR;
985 default_parse_boot_param(struct arm_boot_params *abp)
987 vm_offset_t lastaddr;
989 #if defined(LINUX_BOOT_ABI)
990 if ((lastaddr = linux_parse_boot_param(abp)) != 0)
993 #if defined(FREEBSD_BOOT_LOADER)
994 if ((lastaddr = freebsd_parse_boot_param(abp)) != 0)
997 /* Fall back to hardcoded metadata. */
998 lastaddr = fake_preload_metadata(abp);
1004 * Stub version of the boot parameter parsing routine. We are
1005 * called early in initarm, before even VM has been initialized.
1006 * This routine needs to preserve any data that the boot loader
1007 * has passed in before the kernel starts to grow past the end
1008 * of the BSS, traditionally the place boot-loaders put this data.
1010 * Since this is called so early, things that depend on the vm system
1011 * being setup (including access to some SoC's serial ports), about
1012 * all that can be done in this routine is to copy the arguments.
1014 * This is the default boot parameter parsing routine. Individual
1015 * kernels/boards can override this weak function with one of their
1016 * own. We just fake metadata...
1018 __weak_reference(default_parse_boot_param, parse_boot_param);
1024 init_proc0(vm_offset_t kstack)
1026 proc_linkup0(&proc0, &thread0);
1027 thread0.td_kstack = kstack;
1028 thread0.td_pcb = (struct pcb *)
1029 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
1030 thread0.td_pcb->pcb_flags = 0;
1031 thread0.td_frame = &proc0_tf;
1032 pcpup->pc_curpcb = thread0.td_pcb;
1036 set_stackptrs(int cpu)
1039 set_stackptr(PSR_IRQ32_MODE,
1040 irqstack.pv_va + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1041 set_stackptr(PSR_ABT32_MODE,
1042 abtstack.pv_va + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1043 set_stackptr(PSR_UND32_MODE,
1044 undstack.pv_va + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1068 debugf("loader passed (static) kenv:\n");
1069 if (kern_envp == NULL) {
1070 debugf(" no env, null ptr\n");
1073 debugf(" kern_envp = 0x%08x\n", (uint32_t)kern_envp);
1076 for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
1077 debugf(" %x %s\n", (uint32_t)cp, cp);
1081 physmap_init(struct mem_region *availmem_regions, int availmem_regions_sz)
1084 vm_offset_t phys_kernelend, kernload;
1086 struct mem_region *mp, *mp1;
1088 phys_kernelend = KERNPHYSADDR + (virtual_avail - KERNVIRTADDR);
1089 kernload = KERNPHYSADDR;
1092 * Remove kernel physical address range from avail
1093 * regions list. Page align all regions.
1094 * Non-page aligned memory isn't very interesting to us.
1095 * Also, sort the entries for ascending addresses.
1098 cnt = availmem_regions_sz;
1099 debugf("processing avail regions:\n");
1100 for (mp = availmem_regions; mp->mr_size; mp++) {
1102 e = mp->mr_start + mp->mr_size;
1103 debugf(" %08x-%08x -> ", s, e);
1104 /* Check whether this region holds all of the kernel. */
1105 if (s < kernload && e > phys_kernelend) {
1106 availmem_regions[cnt].mr_start = phys_kernelend;
1107 availmem_regions[cnt++].mr_size = e - phys_kernelend;
1110 /* Look whether this regions starts within the kernel. */
1111 if (s >= kernload && s < phys_kernelend) {
1112 if (e <= phys_kernelend)
1116 /* Now look whether this region ends within the kernel. */
1117 if (e > kernload && e <= phys_kernelend) {
1118 if (s >= kernload) {
1123 /* Now page align the start and size of the region. */
1129 debugf("%08x-%08x = %x\n", s, e, sz);
1131 /* Check whether some memory is left here. */
1134 printf("skipping\n");
1136 (cnt - (mp - availmem_regions)) * sizeof(*mp));
1142 /* Do an insertion sort. */
1143 for (mp1 = availmem_regions; mp1 < mp; mp1++)
1144 if (s < mp1->mr_start)
1147 bcopy(mp1, mp1 + 1, (char *)mp - (char *)mp1);
1155 availmem_regions_sz = cnt;
1157 /* Fill in phys_avail table, based on availmem_regions */
1158 debugf("fill in phys_avail:\n");
1159 for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
1161 debugf(" region: 0x%08x - 0x%08x (0x%08x)\n",
1162 availmem_regions[i].mr_start,
1163 availmem_regions[i].mr_start + availmem_regions[i].mr_size,
1164 availmem_regions[i].mr_size);
1167 * We should not map the page at PA 0x0000000, the VM can't
1168 * handle it, as pmap_extract() == 0 means failure.
1170 if (availmem_regions[i].mr_start > 0 ||
1171 availmem_regions[i].mr_size > PAGE_SIZE) {
1172 phys_avail[j] = availmem_regions[i].mr_start;
1173 if (phys_avail[j] == 0)
1174 phys_avail[j] += PAGE_SIZE;
1175 phys_avail[j + 1] = availmem_regions[i].mr_start +
1176 availmem_regions[i].mr_size;
1181 phys_avail[j + 1] = 0;
1185 initarm(struct arm_boot_params *abp)
1187 struct mem_region memory_regions[FDT_MEM_REGIONS];
1188 struct mem_region availmem_regions[FDT_MEM_REGIONS];
1189 struct mem_region reserved_regions[FDT_MEM_REGIONS];
1190 struct pv_addr kernel_l1pt;
1191 struct pv_addr dpcpu;
1192 vm_offset_t dtbp, freemempos, l2_start, lastaddr;
1193 uint32_t memsize, l2size;
1197 int i = 0, j = 0, err_devmap = 0;
1198 int memory_regions_sz;
1199 int availmem_regions_sz;
1200 int reserved_regions_sz;
1201 vm_offset_t start, end;
1202 vm_offset_t rstart, rend;
1205 lastaddr = parse_boot_param(abp);
1210 * Find the dtb passed in by the boot loader.
1212 kmdp = preload_search_by_type("elf kernel");
1214 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1216 dtbp = (vm_offset_t)NULL;
1218 #if defined(FDT_DTB_STATIC)
1220 * In case the device tree blob was not retrieved (from metadata) try
1221 * to use the statically embedded one.
1223 if (dtbp == (vm_offset_t)NULL)
1224 dtbp = (vm_offset_t)&fdt_static_dtb;
1227 if (OF_install(OFW_FDT, 0) == FALSE)
1230 if (OF_init((void *)dtbp) != 0)
1233 /* Grab physical memory regions information from device tree. */
1234 if (fdt_get_mem_regions(memory_regions, &memory_regions_sz,
1238 /* Grab physical memory regions information from device tree. */
1239 if (fdt_get_reserved_regions(reserved_regions, &reserved_regions_sz) != 0)
1240 reserved_regions_sz = 0;
1243 * Now exclude all the reserved regions
1246 for (i = 0; i < memory_regions_sz; i++) {
1247 start = memory_regions[i].mr_start;
1248 end = start + memory_regions[i].mr_size;
1249 for (j = 0; j < reserved_regions_sz; j++) {
1250 rstart = reserved_regions[j].mr_start;
1251 rend = rstart + reserved_regions[j].mr_size;
1253 * Restricted region is before available
1254 * Skip restricted region
1259 * Restricted region is behind available
1260 * No further processing required
1265 * Restricted region includes memory region
1266 * skip available region
1268 if ((start >= rstart) && (rend >= end)) {
1274 * Memory region includes restricted region
1276 if ((rstart > start) && (end > rend)) {
1277 availmem_regions[curr].mr_start = start;
1278 availmem_regions[curr++].mr_size = rstart - start;
1283 * Memory region partially overlaps with restricted
1285 if ((rstart >= start) && (rstart <= end)) {
1288 else if ((rend >= start) && (rend <= end)) {
1294 availmem_regions[curr].mr_start = start;
1295 availmem_regions[curr++].mr_size = end - start;
1299 availmem_regions_sz = curr;
1301 /* Platform-specific initialisation */
1302 vm_max_kernel_address = initarm_lastaddr();
1306 /* Do basic tuning, hz etc */
1309 /* Calculate number of L2 tables needed for mapping vm_page_array */
1310 l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
1311 l2size = (l2size >> L1_S_SHIFT) + 1;
1314 * Add one table for end of kernel map, one for stacks, msgbuf and
1315 * L1 and L2 tables map and one for vectors map.
1319 /* Make it divisible by 4 */
1320 l2size = (l2size + 3) & ~3;
1322 freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
1324 /* Define a macro to simplify memory allocation */
1325 #define valloc_pages(var, np) \
1326 alloc_pages((var).pv_va, (np)); \
1327 (var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
1329 #define alloc_pages(var, np) \
1330 (var) = freemempos; \
1331 freemempos += (np * PAGE_SIZE); \
1332 memset((char *)(var), 0, ((np) * PAGE_SIZE));
1334 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
1335 freemempos += PAGE_SIZE;
1336 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
1338 for (i = 0; i < l2size; ++i) {
1339 if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
1340 valloc_pages(kernel_pt_table[i],
1341 L2_TABLE_SIZE / PAGE_SIZE);
1344 kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
1345 L2_TABLE_SIZE_REAL * (i - j);
1346 kernel_pt_table[i].pv_pa =
1347 kernel_pt_table[i].pv_va - KERNVIRTADDR +
1353 * Allocate a page for the system page mapped to 0x00000000
1354 * or 0xffff0000. This page will just contain the system vectors
1355 * and can be shared by all processes.
1357 valloc_pages(systempage, 1);
1359 /* Allocate dynamic per-cpu area. */
1360 valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
1361 dpcpu_init((void *)dpcpu.pv_va, 0);
1363 /* Allocate stacks for all modes */
1364 valloc_pages(irqstack, IRQ_STACK_SIZE * MAXCPU);
1365 valloc_pages(abtstack, ABT_STACK_SIZE * MAXCPU);
1366 valloc_pages(undstack, UND_STACK_SIZE * MAXCPU);
1367 valloc_pages(kernelstack, KSTACK_PAGES * MAXCPU);
1368 valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
1371 * Now we start construction of the L1 page table
1372 * We start by mapping the L2 page tables into the L1.
1373 * This means that we can replace L1 mappings later on if necessary
1375 l1pagetable = kernel_l1pt.pv_va;
1378 * Try to map as much as possible of kernel text and data using
1379 * 1MB section mapping and for the rest of initial kernel address
1380 * space use L2 coarse tables.
1382 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
1383 * and kernel structures
1385 l2_start = lastaddr & ~(L1_S_OFFSET);
1386 for (i = 0 ; i < l2size - 1; i++)
1387 pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
1388 &kernel_pt_table[i]);
1390 pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
1392 /* Map kernel code and data */
1393 pmap_map_chunk(l1pagetable, KERNVIRTADDR, KERNPHYSADDR,
1394 (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
1395 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1398 /* Map L1 directory and allocated L2 page tables */
1399 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
1400 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1402 pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
1403 kernel_pt_table[0].pv_pa,
1404 L2_TABLE_SIZE_REAL * l2size,
1405 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1407 /* Map allocated DPCPU, stacks and msgbuf */
1408 pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
1409 freemempos - dpcpu.pv_va,
1410 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1412 /* Link and map the vector page */
1413 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
1414 &kernel_pt_table[l2size - 1]);
1415 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
1416 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE, PTE_CACHE);
1418 /* Map pmap_devmap[] entries */
1419 err_devmap = platform_devmap_init();
1420 pmap_devmap_bootstrap(l1pagetable, pmap_devmap_bootstrap_table);
1422 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) | DOMAIN_CLIENT);
1423 pmap_pa = kernel_l1pt.pv_pa;
1424 setttb(kernel_l1pt.pv_pa);
1426 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
1429 * Only after the SOC registers block is mapped we can perform device
1430 * tree fixups, as they may attempt to read parameters from hardware.
1432 OF_interpret("perform-fixup", 0);
1434 initarm_gpio_init();
1438 physmem = memsize / PAGE_SIZE;
1440 debugf("initarm: console initialized\n");
1441 debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
1442 debugf(" boothowto = 0x%08x\n", boothowto);
1443 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
1446 env = getenv("kernelname");
1448 strlcpy(kernelname, env, sizeof(kernelname));
1450 if (err_devmap != 0)
1451 printf("WARNING: could not fully configure devmap, error=%d\n",
1454 initarm_late_init();
1457 * Pages were allocated during the secondary bootstrap for the
1458 * stacks for different CPU modes.
1459 * We must now set the r13 registers in the different CPU modes to
1460 * point to these stacks.
1461 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1462 * of the stack memory.
1464 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
1469 * We must now clean the cache again....
1470 * Cleaning may be done by reading new data to displace any
1471 * dirty data in the cache. This will have happened in setttb()
1472 * but since we are boot strapping the addresses used for the read
1473 * may have just been remapped and thus the cache could be out
1474 * of sync. A re-clean after the switch will cure this.
1475 * After booting there are no gross relocations of the kernel thus
1476 * this problem will not occur after initarm().
1478 cpu_idcache_wbinv_all();
1480 /* Set stack for exception handlers */
1481 data_abort_handler_address = (u_int)data_abort_handler;
1482 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
1483 undefined_handler_address = (u_int)undefinedinstruction_bounce;
1486 init_proc0(kernelstack.pv_va);
1488 arm_intrnames_init();
1489 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1490 arm_dump_avail_init(memsize, sizeof(dump_avail) / sizeof(dump_avail[0]));
1491 pmap_bootstrap(freemempos, &kernel_l1pt);
1492 msgbufp = (void *)msgbufpv.pv_va;
1493 msgbufinit(msgbufp, msgbufsize);
1497 * Prepare map of physical memory regions available to vm subsystem.
1499 physmap_init(availmem_regions, availmem_regions_sz);
1501 init_param2(physmem);
1504 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
1505 sizeof(struct pcb)));