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/devmap.h>
94 #include <machine/frame.h>
95 #include <machine/intr.h>
96 #include <machine/machdep.h>
97 #include <machine/md_var.h>
98 #include <machine/metadata.h>
99 #include <machine/pcb.h>
100 #include <machine/physmem.h>
101 #include <machine/reg.h>
102 #include <machine/trap.h>
103 #include <machine/undefined.h>
104 #include <machine/vfp.h>
105 #include <machine/vmparam.h>
106 #include <machine/sysarch.h>
109 #include <dev/fdt/fdt_common.h>
110 #include <dev/ofw/openfirm.h>
114 #define debugf(fmt, args...) printf(fmt, ##args)
116 #define debugf(fmt, args...)
119 struct pcpu __pcpu[MAXCPU];
120 struct pcpu *pcpup = &__pcpu[0];
122 static struct trapframe proc0_tf;
123 uint32_t cpu_reset_address = 0;
125 vm_offset_t vector_page;
127 int (*_arm_memcpy)(void *, void *, int, int) = NULL;
128 int (*_arm_bzero)(void *, int, int) = NULL;
129 int _min_memcpy_size = 0;
130 int _min_bzero_size = 0;
134 extern vm_offset_t ksym_start, ksym_end;
139 * This is the number of L2 page tables required for covering max
140 * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
141 * stacks etc.), uprounded to be divisible by 4.
143 #define KERNEL_PT_MAX 78
145 static struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
149 struct pv_addr systempage;
150 static struct pv_addr msgbufpv;
151 struct pv_addr irqstack;
152 struct pv_addr undstack;
153 struct pv_addr abtstack;
154 static struct pv_addr kernelstack;
158 #if defined(LINUX_BOOT_ABI)
159 #define LBABI_MAX_BANKS 10
162 struct arm_lbabi_tag *atag_list;
163 char linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
164 char atags[LBABI_MAX_COMMAND_LINE * 2];
165 uint32_t memstart[LBABI_MAX_BANKS];
166 uint32_t memsize[LBABI_MAX_BANKS];
170 static uint32_t board_revision;
171 /* hex representation of uint64_t */
172 static char board_serial[32];
174 SYSCTL_NODE(_hw, OID_AUTO, board, CTLFLAG_RD, 0, "Board attributes");
175 SYSCTL_UINT(_hw_board, OID_AUTO, revision, CTLFLAG_RD,
176 &board_revision, 0, "Board revision");
177 SYSCTL_STRING(_hw_board, OID_AUTO, serial, CTLFLAG_RD,
178 board_serial, 0, "Board serial");
181 SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
182 &vfp_exists, 0, "Floating point support enabled");
185 board_set_serial(uint64_t serial)
188 snprintf(board_serial, sizeof(board_serial)-1,
193 board_set_revision(uint32_t revision)
196 board_revision = revision;
200 sendsig(catcher, ksi, mask)
207 struct trapframe *tf;
208 struct sigframe *fp, frame;
216 PROC_LOCK_ASSERT(p, MA_OWNED);
217 sig = ksi->ksi_signo;
218 code = ksi->ksi_code;
220 mtx_assert(&psp->ps_mtx, MA_OWNED);
222 onstack = sigonstack(tf->tf_usr_sp);
224 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
227 /* Allocate and validate space for the signal handler context. */
228 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
229 SIGISMEMBER(psp->ps_sigonstack, sig)) {
230 fp = (struct sigframe *)(td->td_sigstk.ss_sp +
231 td->td_sigstk.ss_size);
232 #if defined(COMPAT_43)
233 td->td_sigstk.ss_flags |= SS_ONSTACK;
236 fp = (struct sigframe *)td->td_frame->tf_usr_sp;
238 /* make room on the stack */
241 /* make the stack aligned */
242 fp = (struct sigframe *)STACKALIGN(fp);
243 /* Populate the siginfo frame. */
244 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
245 frame.sf_si = ksi->ksi_info;
246 frame.sf_uc.uc_sigmask = *mask;
247 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
248 ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
249 frame.sf_uc.uc_stack = td->td_sigstk;
250 mtx_unlock(&psp->ps_mtx);
251 PROC_UNLOCK(td->td_proc);
253 /* Copy the sigframe out to the user's stack. */
254 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
255 /* Process has trashed its stack. Kill it. */
256 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
261 /* Translate the signal if appropriate. */
262 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
263 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
266 * Build context to run handler in. We invoke the handler
267 * directly, only returning via the trampoline. Note the
268 * trampoline version numbers are coordinated with machine-
269 * dependent code in libc.
273 tf->tf_r1 = (register_t)&fp->sf_si;
274 tf->tf_r2 = (register_t)&fp->sf_uc;
276 /* the trampoline uses r5 as the uc address */
277 tf->tf_r5 = (register_t)&fp->sf_uc;
278 tf->tf_pc = (register_t)catcher;
279 tf->tf_usr_sp = (register_t)fp;
280 tf->tf_usr_lr = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
282 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
286 mtx_lock(&psp->ps_mtx);
289 struct kva_md_info kmi;
294 * Initialize the vector page, and select whether or not to
295 * relocate the vectors.
297 * NOTE: We expect the vector page to be mapped at its expected
301 extern unsigned int page0[], page0_data[];
303 arm_vector_init(vm_offset_t va, int which)
305 unsigned int *vectors = (int *) va;
306 unsigned int *vectors_data = vectors + (page0_data - page0);
310 * Loop through the vectors we're taking over, and copy the
311 * vector's insn and data word.
313 for (vec = 0; vec < ARM_NVEC; vec++) {
314 if ((which & (1 << vec)) == 0) {
315 /* Don't want to take over this vector. */
318 vectors[vec] = page0[vec];
319 vectors_data[vec] = page0_data[vec];
322 /* Now sync the vectors. */
323 cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
327 if (va == ARM_VECTORS_HIGH) {
329 * Assume the MD caller knows what it's doing here, and
330 * really does want the vector page relocated.
332 * Note: This has to be done here (and not just in
333 * cpu_setup()) because the vector page needs to be
334 * accessible *before* cpu_startup() is called.
337 * NOTE: If the CPU control register is not readable,
338 * this will totally fail! We'll just assume that
339 * any system that has high vector support has a
340 * readable CPU control register, for now. If we
341 * ever encounter one that does not, we'll have to
344 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
349 cpu_startup(void *dummy)
351 struct pcb *pcb = thread0.td_pcb;
352 const unsigned int mbyte = 1024 * 1024;
353 #ifdef ARM_TP_ADDRESS
354 #ifndef ARM_CACHE_LOCK_ENABLE
361 vm_ksubmap_init(&kmi);
364 * Display the RAM layout.
366 printf("real memory = %ju (%ju MB)\n",
367 (uintmax_t)arm32_ptob(realmem),
368 (uintmax_t)arm32_ptob(realmem) / mbyte);
369 printf("avail memory = %ju (%ju MB)\n",
370 (uintmax_t)arm32_ptob(cnt.v_free_count),
371 (uintmax_t)arm32_ptob(cnt.v_free_count) / mbyte);
373 arm_physmem_print_tables();
374 arm_devmap_print_table();
378 vm_pager_bufferinit();
379 pcb->pcb_regs.sf_sp = (u_int)thread0.td_kstack +
380 USPACE_SVC_STACK_TOP;
381 vector_page_setprot(VM_PROT_READ);
382 pmap_set_pcb_pagedir(pmap_kernel(), pcb);
384 #ifdef ARM_TP_ADDRESS
385 #ifdef ARM_CACHE_LOCK_ENABLE
386 pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
387 arm_lock_cache_line(ARM_TP_ADDRESS);
389 m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
390 pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
392 *(uint32_t *)ARM_RAS_START = 0;
393 *(uint32_t *)ARM_RAS_END = 0xffffffff;
397 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
400 * Flush the D-cache for non-DMA I/O so that the I-cache can
401 * be made coherent later.
404 cpu_flush_dcache(void *ptr, size_t len)
407 cpu_dcache_wb_range((uintptr_t)ptr, len);
409 cpu_l2cache_wb_range((uintptr_t)vtophys(ptr), len);
411 cpu_l2cache_wb_range((uintptr_t)ptr, len);
415 /* Get current clock frequency for the given cpu id. */
417 cpu_est_clockrate(int cpu_id, uint64_t *rate)
427 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu);
429 #ifndef NO_EVENTTIMERS
433 if (!sched_runnable())
435 #ifndef NO_EVENTTIMERS
440 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu);
444 cpu_idle_wakeup(int cpu)
451 * Most ARM platforms don't need to do anything special to init their clocks
452 * (they get intialized during normal device attachment), and by not defining a
453 * cpu_initclocks() function they get this generic one. Any platform that needs
454 * to do something special can just provide their own implementation, which will
455 * override this one due to the weak linkage.
458 arm_generic_initclocks(void)
461 #ifndef NO_EVENTTIMERS
463 if (PCPU_GET(cpuid) == 0)
464 cpu_initclocks_bsp();
468 cpu_initclocks_bsp();
472 __weak_reference(arm_generic_initclocks, cpu_initclocks);
475 fill_regs(struct thread *td, struct reg *regs)
477 struct trapframe *tf = td->td_frame;
478 bcopy(&tf->tf_r0, regs->r, sizeof(regs->r));
479 regs->r_sp = tf->tf_usr_sp;
480 regs->r_lr = tf->tf_usr_lr;
481 regs->r_pc = tf->tf_pc;
482 regs->r_cpsr = tf->tf_spsr;
486 fill_fpregs(struct thread *td, struct fpreg *regs)
488 bzero(regs, sizeof(*regs));
493 set_regs(struct thread *td, struct reg *regs)
495 struct trapframe *tf = td->td_frame;
497 bcopy(regs->r, &tf->tf_r0, sizeof(regs->r));
498 tf->tf_usr_sp = regs->r_sp;
499 tf->tf_usr_lr = regs->r_lr;
500 tf->tf_pc = regs->r_pc;
501 tf->tf_spsr &= ~PSR_FLAGS;
502 tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
507 set_fpregs(struct thread *td, struct fpreg *regs)
513 fill_dbregs(struct thread *td, struct dbreg *regs)
518 set_dbregs(struct thread *td, struct dbreg *regs)
525 ptrace_read_int(struct thread *td, vm_offset_t addr, u_int32_t *v)
530 PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
531 iov.iov_base = (caddr_t) v;
532 iov.iov_len = sizeof(u_int32_t);
535 uio.uio_offset = (off_t)addr;
536 uio.uio_resid = sizeof(u_int32_t);
537 uio.uio_segflg = UIO_SYSSPACE;
538 uio.uio_rw = UIO_READ;
540 return proc_rwmem(td->td_proc, &uio);
544 ptrace_write_int(struct thread *td, vm_offset_t addr, u_int32_t v)
549 PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
550 iov.iov_base = (caddr_t) &v;
551 iov.iov_len = sizeof(u_int32_t);
554 uio.uio_offset = (off_t)addr;
555 uio.uio_resid = sizeof(u_int32_t);
556 uio.uio_segflg = UIO_SYSSPACE;
557 uio.uio_rw = UIO_WRITE;
559 return proc_rwmem(td->td_proc, &uio);
563 ptrace_single_step(struct thread *td)
568 KASSERT(td->td_md.md_ptrace_instr == 0,
569 ("Didn't clear single step"));
572 error = ptrace_read_int(td, td->td_frame->tf_pc + 4,
573 &td->td_md.md_ptrace_instr);
576 error = ptrace_write_int(td, td->td_frame->tf_pc + 4,
579 td->td_md.md_ptrace_instr = 0;
580 td->td_md.md_ptrace_addr = td->td_frame->tf_pc + 4;
587 ptrace_clear_single_step(struct thread *td)
591 if (td->td_md.md_ptrace_instr) {
594 ptrace_write_int(td, td->td_md.md_ptrace_addr,
595 td->td_md.md_ptrace_instr);
597 td->td_md.md_ptrace_instr = 0;
603 ptrace_set_pc(struct thread *td, unsigned long addr)
605 td->td_frame->tf_pc = addr;
610 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
621 if (td->td_md.md_spinlock_count == 0) {
622 cspr = disable_interrupts(PSR_I | PSR_F);
623 td->td_md.md_spinlock_count = 1;
624 td->td_md.md_saved_cspr = cspr;
626 td->td_md.md_spinlock_count++;
638 cspr = td->td_md.md_saved_cspr;
639 td->td_md.md_spinlock_count--;
640 if (td->td_md.md_spinlock_count == 0)
641 restore_interrupts(cspr);
645 * Clear registers on exec
648 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
650 struct trapframe *tf = td->td_frame;
652 memset(tf, 0, sizeof(*tf));
653 tf->tf_usr_sp = stack;
654 tf->tf_usr_lr = imgp->entry_addr;
655 tf->tf_svc_lr = 0x77777777;
656 tf->tf_pc = imgp->entry_addr;
657 tf->tf_spsr = PSR_USR32_MODE;
661 * Get machine context.
664 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
666 struct trapframe *tf = td->td_frame;
667 __greg_t *gr = mcp->__gregs;
669 if (clear_ret & GET_MC_CLEAR_RET)
672 gr[_REG_R0] = tf->tf_r0;
673 gr[_REG_R1] = tf->tf_r1;
674 gr[_REG_R2] = tf->tf_r2;
675 gr[_REG_R3] = tf->tf_r3;
676 gr[_REG_R4] = tf->tf_r4;
677 gr[_REG_R5] = tf->tf_r5;
678 gr[_REG_R6] = tf->tf_r6;
679 gr[_REG_R7] = tf->tf_r7;
680 gr[_REG_R8] = tf->tf_r8;
681 gr[_REG_R9] = tf->tf_r9;
682 gr[_REG_R10] = tf->tf_r10;
683 gr[_REG_R11] = tf->tf_r11;
684 gr[_REG_R12] = tf->tf_r12;
685 gr[_REG_SP] = tf->tf_usr_sp;
686 gr[_REG_LR] = tf->tf_usr_lr;
687 gr[_REG_PC] = tf->tf_pc;
688 gr[_REG_CPSR] = tf->tf_spsr;
694 * Set machine context.
696 * However, we don't set any but the user modifiable flags, and we won't
697 * touch the cs selector.
700 set_mcontext(struct thread *td, mcontext_t *mcp)
702 struct trapframe *tf = td->td_frame;
703 const __greg_t *gr = mcp->__gregs;
705 tf->tf_r0 = gr[_REG_R0];
706 tf->tf_r1 = gr[_REG_R1];
707 tf->tf_r2 = gr[_REG_R2];
708 tf->tf_r3 = gr[_REG_R3];
709 tf->tf_r4 = gr[_REG_R4];
710 tf->tf_r5 = gr[_REG_R5];
711 tf->tf_r6 = gr[_REG_R6];
712 tf->tf_r7 = gr[_REG_R7];
713 tf->tf_r8 = gr[_REG_R8];
714 tf->tf_r9 = gr[_REG_R9];
715 tf->tf_r10 = gr[_REG_R10];
716 tf->tf_r11 = gr[_REG_R11];
717 tf->tf_r12 = gr[_REG_R12];
718 tf->tf_usr_sp = gr[_REG_SP];
719 tf->tf_usr_lr = gr[_REG_LR];
720 tf->tf_pc = gr[_REG_PC];
721 tf->tf_spsr = gr[_REG_CPSR];
730 sys_sigreturn(td, uap)
732 struct sigreturn_args /* {
733 const struct __ucontext *sigcntxp;
741 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
744 * Make sure the processor mode has not been tampered with and
745 * interrupts have not been disabled.
747 spsr = uc.uc_mcontext.__gregs[_REG_CPSR];
748 if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
749 (spsr & (PSR_I | PSR_F)) != 0)
751 /* Restore register context. */
752 set_mcontext(td, &uc.uc_mcontext);
754 /* Restore signal mask. */
755 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
757 return (EJUSTRETURN);
762 * Construct a PCB from a trapframe. This is called from kdb_trap() where
763 * we want to start a backtrace from the function that caused us to enter
764 * the debugger. We have the context in the trapframe, but base the trace
765 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
766 * enough for a backtrace.
769 makectx(struct trapframe *tf, struct pcb *pcb)
771 pcb->pcb_regs.sf_r4 = tf->tf_r4;
772 pcb->pcb_regs.sf_r5 = tf->tf_r5;
773 pcb->pcb_regs.sf_r6 = tf->tf_r6;
774 pcb->pcb_regs.sf_r7 = tf->tf_r7;
775 pcb->pcb_regs.sf_r8 = tf->tf_r8;
776 pcb->pcb_regs.sf_r9 = tf->tf_r9;
777 pcb->pcb_regs.sf_r10 = tf->tf_r10;
778 pcb->pcb_regs.sf_r11 = tf->tf_r11;
779 pcb->pcb_regs.sf_r12 = tf->tf_r12;
780 pcb->pcb_regs.sf_pc = tf->tf_pc;
781 pcb->pcb_regs.sf_lr = tf->tf_usr_lr;
782 pcb->pcb_regs.sf_sp = tf->tf_usr_sp;
786 * Fake up a boot descriptor table
789 fake_preload_metadata(struct arm_boot_params *abp __unused)
792 vm_offset_t zstart = 0, zend = 0;
794 vm_offset_t lastaddr;
796 static uint32_t fake_preload[35];
798 fake_preload[i++] = MODINFO_NAME;
799 fake_preload[i++] = strlen("kernel") + 1;
800 strcpy((char*)&fake_preload[i++], "kernel");
802 fake_preload[i++] = MODINFO_TYPE;
803 fake_preload[i++] = strlen("elf kernel") + 1;
804 strcpy((char*)&fake_preload[i++], "elf kernel");
806 fake_preload[i++] = MODINFO_ADDR;
807 fake_preload[i++] = sizeof(vm_offset_t);
808 fake_preload[i++] = KERNVIRTADDR;
809 fake_preload[i++] = MODINFO_SIZE;
810 fake_preload[i++] = sizeof(uint32_t);
811 fake_preload[i++] = (uint32_t)&end - KERNVIRTADDR;
813 if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
814 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
815 fake_preload[i++] = sizeof(vm_offset_t);
816 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
817 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
818 fake_preload[i++] = sizeof(vm_offset_t);
819 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
820 lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
822 zstart = *(uint32_t *)(KERNVIRTADDR + 4);
827 lastaddr = (vm_offset_t)&end;
828 fake_preload[i++] = 0;
830 preload_metadata = (void *)fake_preload;
838 #if ARM_ARCH_6 || ARM_ARCH_7A || defined(CPU_MV_PJ4B)
839 set_curthread(&thread0);
841 pcpu_init(pcpup, 0, sizeof(struct pcpu));
842 PCPU_SET(curthread, &thread0);
848 #if defined(LINUX_BOOT_ABI)
850 linux_parse_boot_param(struct arm_boot_params *abp)
852 struct arm_lbabi_tag *walker;
857 * Linux boot ABI: r0 = 0, r1 is the board type (!= 0) and r2
858 * is atags or dtb pointer. If all of these aren't satisfied,
861 if (!(abp->abp_r0 == 0 && abp->abp_r1 != 0 && abp->abp_r2 != 0))
864 board_id = abp->abp_r1;
865 walker = (struct arm_lbabi_tag *)
866 (abp->abp_r2 + KERNVIRTADDR - abp->abp_physaddr);
868 /* xxx - Need to also look for binary device tree */
869 if (ATAG_TAG(walker) != ATAG_CORE)
873 while (ATAG_TAG(walker) != ATAG_NONE) {
874 switch (ATAG_TAG(walker)) {
878 arm_physmem_hardware_region(walker->u.tag_mem.start,
879 walker->u.tag_mem.size);
884 serial = walker->u.tag_sn.low |
885 ((uint64_t)walker->u.tag_sn.high << 32);
886 board_set_serial(serial);
889 revision = walker->u.tag_rev.rev;
890 board_set_revision(revision);
893 /* XXX open question: Parse this for boothowto? */
894 bcopy(walker->u.tag_cmd.command, linux_command_line,
900 walker = ATAG_NEXT(walker);
903 /* Save a copy for later */
904 bcopy(atag_list, atags,
905 (char *)walker - (char *)atag_list + ATAG_SIZE(walker));
907 return fake_preload_metadata(abp);
911 #if defined(FREEBSD_BOOT_LOADER)
913 freebsd_parse_boot_param(struct arm_boot_params *abp)
915 vm_offset_t lastaddr = 0;
920 * Mask metadata pointer: it is supposed to be on page boundary. If
921 * the first argument (mdp) doesn't point to a valid address the
922 * bootloader must have passed us something else than the metadata
923 * ptr, so we give up. Also give up if we cannot find metadta section
924 * the loader creates that we get all this data out of.
927 if ((mdp = (void *)(abp->abp_r0 & ~PAGE_MASK)) == NULL)
929 preload_metadata = mdp;
930 kmdp = preload_search_by_type("elf kernel");
934 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
935 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
936 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
938 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
939 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
941 preload_addr_relocate = KERNVIRTADDR - abp->abp_physaddr;
947 default_parse_boot_param(struct arm_boot_params *abp)
949 vm_offset_t lastaddr;
951 #if defined(LINUX_BOOT_ABI)
952 if ((lastaddr = linux_parse_boot_param(abp)) != 0)
955 #if defined(FREEBSD_BOOT_LOADER)
956 if ((lastaddr = freebsd_parse_boot_param(abp)) != 0)
959 /* Fall back to hardcoded metadata. */
960 lastaddr = fake_preload_metadata(abp);
966 * Stub version of the boot parameter parsing routine. We are
967 * called early in initarm, before even VM has been initialized.
968 * This routine needs to preserve any data that the boot loader
969 * has passed in before the kernel starts to grow past the end
970 * of the BSS, traditionally the place boot-loaders put this data.
972 * Since this is called so early, things that depend on the vm system
973 * being setup (including access to some SoC's serial ports), about
974 * all that can be done in this routine is to copy the arguments.
976 * This is the default boot parameter parsing routine. Individual
977 * kernels/boards can override this weak function with one of their
978 * own. We just fake metadata...
980 __weak_reference(default_parse_boot_param, parse_boot_param);
986 init_proc0(vm_offset_t kstack)
988 proc_linkup0(&proc0, &thread0);
989 thread0.td_kstack = kstack;
990 thread0.td_pcb = (struct pcb *)
991 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
992 thread0.td_pcb->pcb_flags = 0;
993 thread0.td_pcb->pcb_vfpcpu = -1;
994 thread0.td_pcb->pcb_vfpstate.fpscr = VFPSCR_DN | VFPSCR_FZ;
995 thread0.td_frame = &proc0_tf;
996 pcpup->pc_curpcb = thread0.td_pcb;
1000 set_stackptrs(int cpu)
1003 set_stackptr(PSR_IRQ32_MODE,
1004 irqstack.pv_va + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1005 set_stackptr(PSR_ABT32_MODE,
1006 abtstack.pv_va + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1007 set_stackptr(PSR_UND32_MODE,
1008 undstack.pv_va + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1032 debugf("loader passed (static) kenv:\n");
1033 if (kern_envp == NULL) {
1034 debugf(" no env, null ptr\n");
1037 debugf(" kern_envp = 0x%08x\n", (uint32_t)kern_envp);
1040 for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
1041 debugf(" %x %s\n", (uint32_t)cp, cp);
1045 initarm(struct arm_boot_params *abp)
1047 struct mem_region mem_regions[FDT_MEM_REGIONS];
1048 struct pv_addr kernel_l1pt;
1049 struct pv_addr dpcpu;
1050 vm_offset_t dtbp, freemempos, l2_start, lastaddr;
1051 uint32_t memsize, l2size;
1055 int i, j, err_devmap, mem_regions_sz;
1057 lastaddr = parse_boot_param(abp);
1058 arm_physmem_kernaddr = abp->abp_physaddr;
1064 * Find the dtb passed in by the boot loader.
1066 kmdp = preload_search_by_type("elf kernel");
1068 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1070 dtbp = (vm_offset_t)NULL;
1072 #if defined(FDT_DTB_STATIC)
1074 * In case the device tree blob was not retrieved (from metadata) try
1075 * to use the statically embedded one.
1077 if (dtbp == (vm_offset_t)NULL)
1078 dtbp = (vm_offset_t)&fdt_static_dtb;
1081 if (OF_install(OFW_FDT, 0) == FALSE)
1082 panic("Cannot install FDT");
1084 if (OF_init((void *)dtbp) != 0)
1085 panic("OF_init failed with the found device tree");
1087 /* Grab physical memory regions information from device tree. */
1088 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, &memsize) != 0)
1089 panic("Cannot get physical memory regions");
1090 arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1092 /* Grab reserved memory regions information from device tree. */
1093 if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
1094 arm_physmem_exclude_regions(mem_regions, mem_regions_sz,
1095 EXFLAG_NODUMP | EXFLAG_NOALLOC);
1097 /* Platform-specific initialisation */
1098 initarm_early_init();
1102 /* Do basic tuning, hz etc */
1105 /* Calculate number of L2 tables needed for mapping vm_page_array */
1106 l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
1107 l2size = (l2size >> L1_S_SHIFT) + 1;
1110 * Add one table for end of kernel map, one for stacks, msgbuf and
1111 * L1 and L2 tables map and one for vectors map.
1115 /* Make it divisible by 4 */
1116 l2size = (l2size + 3) & ~3;
1118 freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
1120 /* Define a macro to simplify memory allocation */
1121 #define valloc_pages(var, np) \
1122 alloc_pages((var).pv_va, (np)); \
1123 (var).pv_pa = (var).pv_va + (abp->abp_physaddr - KERNVIRTADDR);
1125 #define alloc_pages(var, np) \
1126 (var) = freemempos; \
1127 freemempos += (np * PAGE_SIZE); \
1128 memset((char *)(var), 0, ((np) * PAGE_SIZE));
1130 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
1131 freemempos += PAGE_SIZE;
1132 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
1134 for (i = 0, j = 0; i < l2size; ++i) {
1135 if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
1136 valloc_pages(kernel_pt_table[i],
1137 L2_TABLE_SIZE / PAGE_SIZE);
1140 kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
1141 L2_TABLE_SIZE_REAL * (i - j);
1142 kernel_pt_table[i].pv_pa =
1143 kernel_pt_table[i].pv_va - KERNVIRTADDR +
1149 * Allocate a page for the system page mapped to 0x00000000
1150 * or 0xffff0000. This page will just contain the system vectors
1151 * and can be shared by all processes.
1153 valloc_pages(systempage, 1);
1155 /* Allocate dynamic per-cpu area. */
1156 valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
1157 dpcpu_init((void *)dpcpu.pv_va, 0);
1159 /* Allocate stacks for all modes */
1160 valloc_pages(irqstack, IRQ_STACK_SIZE * MAXCPU);
1161 valloc_pages(abtstack, ABT_STACK_SIZE * MAXCPU);
1162 valloc_pages(undstack, UND_STACK_SIZE * MAXCPU);
1163 valloc_pages(kernelstack, KSTACK_PAGES * MAXCPU);
1164 valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
1167 * Now we start construction of the L1 page table
1168 * We start by mapping the L2 page tables into the L1.
1169 * This means that we can replace L1 mappings later on if necessary
1171 l1pagetable = kernel_l1pt.pv_va;
1174 * Try to map as much as possible of kernel text and data using
1175 * 1MB section mapping and for the rest of initial kernel address
1176 * space use L2 coarse tables.
1178 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
1179 * and kernel structures
1181 l2_start = lastaddr & ~(L1_S_OFFSET);
1182 for (i = 0 ; i < l2size - 1; i++)
1183 pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
1184 &kernel_pt_table[i]);
1186 pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
1188 /* Map kernel code and data */
1189 pmap_map_chunk(l1pagetable, KERNVIRTADDR, abp->abp_physaddr,
1190 (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
1191 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1193 /* Map L1 directory and allocated L2 page tables */
1194 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
1195 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1197 pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
1198 kernel_pt_table[0].pv_pa,
1199 L2_TABLE_SIZE_REAL * l2size,
1200 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1202 /* Map allocated DPCPU, stacks and msgbuf */
1203 pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
1204 freemempos - dpcpu.pv_va,
1205 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1207 /* Link and map the vector page */
1208 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
1209 &kernel_pt_table[l2size - 1]);
1210 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
1211 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE, PTE_CACHE);
1213 /* Establish static device mappings. */
1214 err_devmap = initarm_devmap_init();
1215 arm_devmap_bootstrap(l1pagetable, NULL);
1216 vm_max_kernel_address = initarm_lastaddr();
1218 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) | DOMAIN_CLIENT);
1219 pmap_pa = kernel_l1pt.pv_pa;
1220 setttb(kernel_l1pt.pv_pa);
1222 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
1225 * Now that proper page tables are installed, call cpu_setup() to enable
1226 * instruction and data caches and other chip-specific features.
1231 * Only after the SOC registers block is mapped we can perform device
1232 * tree fixups, as they may attempt to read parameters from hardware.
1234 OF_interpret("perform-fixup", 0);
1236 initarm_gpio_init();
1240 debugf("initarm: console initialized\n");
1241 debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
1242 debugf(" boothowto = 0x%08x\n", boothowto);
1243 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
1246 env = getenv("kernelname");
1248 strlcpy(kernelname, env, sizeof(kernelname));
1250 if (err_devmap != 0)
1251 printf("WARNING: could not fully configure devmap, error=%d\n",
1254 initarm_late_init();
1257 * Pages were allocated during the secondary bootstrap for the
1258 * stacks for different CPU modes.
1259 * We must now set the r13 registers in the different CPU modes to
1260 * point to these stacks.
1261 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1262 * of the stack memory.
1264 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
1269 * We must now clean the cache again....
1270 * Cleaning may be done by reading new data to displace any
1271 * dirty data in the cache. This will have happened in setttb()
1272 * but since we are boot strapping the addresses used for the read
1273 * may have just been remapped and thus the cache could be out
1274 * of sync. A re-clean after the switch will cure this.
1275 * After booting there are no gross relocations of the kernel thus
1276 * this problem will not occur after initarm().
1278 cpu_idcache_wbinv_all();
1282 init_proc0(kernelstack.pv_va);
1284 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1285 pmap_bootstrap(freemempos, &kernel_l1pt);
1286 msgbufp = (void *)msgbufpv.pv_va;
1287 msgbufinit(msgbufp, msgbufsize);
1291 * Exclude the kernel (and all the things we allocated which immediately
1292 * follow the kernel) from the VM allocation pool but not from crash
1293 * dumps. virtual_avail is a global variable which tracks the kva we've
1294 * "allocated" while setting up pmaps.
1296 * Prepare the list of physical memory available to the vm subsystem.
1298 arm_physmem_exclude_region(abp->abp_physaddr,
1299 (virtual_avail - KERNVIRTADDR), EXFLAG_NOALLOC);
1300 arm_physmem_init_kernel_globals();
1302 init_param2(physmem);
1305 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
1306 sizeof(struct pcb)));