1 /* $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 2004 Olivier Houchard
7 * Copyright (c) 1994-1998 Mark Brinicombe.
8 * Copyright (c) 1994 Brini.
11 * This code is derived from software written for Brini by Mark Brinicombe
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Mark Brinicombe
24 * for the NetBSD Project.
25 * 4. The name of the company nor the name of the author may be used to
26 * endorse or promote products derived from this software without specific
27 * prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Machine dependent functions for kernel setup
44 * Updated : 18/04/01 updated for new wscons
48 #include "opt_kstack_pages.h"
49 #include "opt_platform.h"
50 #include "opt_sched.h"
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
60 #include <sys/devmap.h>
62 #include <sys/imgact.h>
64 #include <sys/kernel.h>
66 #include <sys/linker.h>
67 #include <sys/msgbuf.h>
68 #include <sys/physmem.h>
69 #include <sys/reboot.h>
70 #include <sys/rwlock.h>
71 #include <sys/sched.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysent.h>
74 #include <sys/sysproto.h>
75 #include <sys/vmmeter.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pager.h>
81 #include <machine/asm.h>
82 #include <machine/debug_monitor.h>
83 #include <machine/machdep.h>
84 #include <machine/metadata.h>
85 #include <machine/pcb.h>
86 #include <machine/platform.h>
87 #include <machine/sysarch.h>
88 #include <machine/undefined.h>
89 #include <machine/vfp.h>
90 #include <machine/vmparam.h>
93 #include <dev/fdt/fdt_common.h>
94 #include <machine/ofw_machdep.h>
98 #define debugf(fmt, args...) printf(fmt, ##args)
100 #define debugf(fmt, args...)
103 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
104 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7) || \
105 defined(COMPAT_FREEBSD9)
106 #error FreeBSD/arm doesn't provide compatibility with releases prior to 10
111 #error FreeBSD requires ARMv6 or later
114 struct pcpu __pcpu[MAXCPU];
115 struct pcpu *pcpup = &__pcpu[0];
117 static struct trapframe proc0_tf;
118 uint32_t cpu_reset_address = 0;
120 vm_offset_t vector_page;
122 /* The address at which the kernel was loaded. Set early in initarm(). */
123 vm_paddr_t arm_physmem_kernaddr;
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;
134 vm_offset_t systempage;
135 vm_offset_t irqstack;
136 vm_offset_t undstack;
137 vm_offset_t abtstack;
141 static delay_func *delay_impl;
142 static void *delay_arg;
145 struct kva_md_info kmi;
149 * Initialize the vector page, and select whether or not to
150 * relocate the vectors.
152 * NOTE: We expect the vector page to be mapped at its expected
156 extern unsigned int page0[], page0_data[];
158 arm_vector_init(vm_offset_t va, int which)
160 unsigned int *vectors = (int *) va;
161 unsigned int *vectors_data = vectors + (page0_data - page0);
165 * Loop through the vectors we're taking over, and copy the
166 * vector's insn and data word.
168 for (vec = 0; vec < ARM_NVEC; vec++) {
169 if ((which & (1 << vec)) == 0) {
170 /* Don't want to take over this vector. */
173 vectors[vec] = page0[vec];
174 vectors_data[vec] = page0_data[vec];
177 /* Now sync the vectors. */
178 icache_sync(va, (ARM_NVEC * 2) * sizeof(u_int));
184 cpu_startup(void *dummy)
186 struct pcb *pcb = thread0.td_pcb;
187 const unsigned int mbyte = 1024 * 1024;
191 vm_ksubmap_init(&kmi);
194 * Display the RAM layout.
196 printf("real memory = %ju (%ju MB)\n",
197 (uintmax_t)arm32_ptob(realmem),
198 (uintmax_t)arm32_ptob(realmem) / mbyte);
199 printf("avail memory = %ju (%ju MB)\n",
200 (uintmax_t)arm32_ptob(vm_free_count()),
201 (uintmax_t)arm32_ptob(vm_free_count()) / mbyte);
203 physmem_print_tables();
204 devmap_print_table();
208 vm_pager_bufferinit();
209 pcb->pcb_regs.sf_sp = (u_int)thread0.td_kstack +
210 USPACE_SVC_STACK_TOP;
211 pmap_set_pcb_pagedir(kernel_pmap, pcb);
214 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
217 * Flush the D-cache for non-DMA I/O so that the I-cache can
218 * be made coherent later.
221 cpu_flush_dcache(void *ptr, size_t len)
224 dcache_wb_poc((vm_offset_t)ptr, (vm_paddr_t)vtophys(ptr), len);
227 /* Get current clock frequency for the given cpu id. */
229 cpu_est_clockrate(int cpu_id, uint64_t *rate)
233 pc = pcpu_find(cpu_id);
234 if (pc == NULL || rate == NULL)
237 if (pc->pc_clock == 0)
240 *rate = pc->pc_clock;
249 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu);
253 if (!sched_runnable())
258 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu);
262 cpu_idle_wakeup(int cpu)
273 if (PCPU_GET(cpuid) == 0)
274 cpu_initclocks_bsp();
278 cpu_initclocks_bsp();
284 arm_set_delay(delay_func *impl, void *arg)
287 KASSERT(impl != NULL, ("No DELAY implementation"));
297 delay_impl(usec, delay_arg);
303 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
306 pcpu->pc_mpidr = 0xffffffff;
316 if (td->td_md.md_spinlock_count == 0) {
317 cspr = disable_interrupts(PSR_I | PSR_F);
318 td->td_md.md_spinlock_count = 1;
319 td->td_md.md_saved_cspr = cspr;
322 td->td_md.md_spinlock_count++;
332 cspr = td->td_md.md_saved_cspr;
333 td->td_md.md_spinlock_count--;
334 if (td->td_md.md_spinlock_count == 0) {
336 restore_interrupts(cspr);
341 * Clear registers on exec
344 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
346 struct trapframe *tf = td->td_frame;
348 memset(tf, 0, sizeof(*tf));
349 tf->tf_usr_sp = stack;
350 tf->tf_usr_lr = imgp->entry_addr;
351 tf->tf_svc_lr = 0x77777777;
352 tf->tf_pc = imgp->entry_addr;
353 tf->tf_spsr = PSR_USR32_MODE;
358 * Get machine VFP context.
361 get_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
366 if (td == curthread) {
368 vfp_store(&pcb->pcb_vfpstate, false);
371 MPASS(TD_IS_SUSPENDED(td));
372 memcpy(vfp->mcv_reg, pcb->pcb_vfpstate.reg,
373 sizeof(vfp->mcv_reg));
374 vfp->mcv_fpscr = pcb->pcb_vfpstate.fpscr;
378 * Set machine VFP context.
381 set_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
386 if (td == curthread) {
391 MPASS(TD_IS_SUSPENDED(td));
392 memcpy(pcb->pcb_vfpstate.reg, vfp->mcv_reg,
393 sizeof(pcb->pcb_vfpstate.reg));
394 pcb->pcb_vfpstate.fpscr = vfp->mcv_fpscr;
399 arm_get_vfpstate(struct thread *td, void *args)
402 struct arm_get_vfpstate_args ua;
403 mcontext_vfp_t mcontext_vfp;
405 rv = copyin(args, &ua, sizeof(ua));
408 if (ua.mc_vfp_size != sizeof(mcontext_vfp_t))
411 get_vfpcontext(td, &mcontext_vfp);
413 bzero(&mcontext_vfp, sizeof(mcontext_vfp));
416 rv = copyout(&mcontext_vfp, ua.mc_vfp, sizeof(mcontext_vfp));
423 * Get machine context.
426 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
428 struct trapframe *tf = td->td_frame;
429 __greg_t *gr = mcp->__gregs;
431 if (clear_ret & GET_MC_CLEAR_RET) {
433 gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C;
435 gr[_REG_R0] = tf->tf_r0;
436 gr[_REG_CPSR] = tf->tf_spsr;
438 gr[_REG_R1] = tf->tf_r1;
439 gr[_REG_R2] = tf->tf_r2;
440 gr[_REG_R3] = tf->tf_r3;
441 gr[_REG_R4] = tf->tf_r4;
442 gr[_REG_R5] = tf->tf_r5;
443 gr[_REG_R6] = tf->tf_r6;
444 gr[_REG_R7] = tf->tf_r7;
445 gr[_REG_R8] = tf->tf_r8;
446 gr[_REG_R9] = tf->tf_r9;
447 gr[_REG_R10] = tf->tf_r10;
448 gr[_REG_R11] = tf->tf_r11;
449 gr[_REG_R12] = tf->tf_r12;
450 gr[_REG_SP] = tf->tf_usr_sp;
451 gr[_REG_LR] = tf->tf_usr_lr;
452 gr[_REG_PC] = tf->tf_pc;
454 mcp->mc_vfp_size = 0;
455 mcp->mc_vfp_ptr = NULL;
456 memset(&mcp->mc_spare, 0, sizeof(mcp->mc_spare));
462 * Set machine context.
464 * However, we don't set any but the user modifiable flags, and we won't
465 * touch the cs selector.
468 set_mcontext(struct thread *td, mcontext_t *mcp)
470 mcontext_vfp_t mc_vfp, *vfp;
471 struct trapframe *tf = td->td_frame;
472 const __greg_t *gr = mcp->__gregs;
476 * Make sure the processor mode has not been tampered with and
477 * interrupts have not been disabled.
479 spsr = gr[_REG_CPSR];
480 if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
481 (spsr & (PSR_I | PSR_F)) != 0)
485 if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_size != sizeof(mc_vfp)) {
486 printf("%s: %s: Malformed mc_vfp_size: %d (0x%08X)\n",
487 td->td_proc->p_comm, __func__,
488 mcp->mc_vfp_size, mcp->mc_vfp_size);
489 } else if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_ptr == NULL) {
490 printf("%s: %s: c_vfp_size != 0 but mc_vfp_ptr == NULL\n",
491 td->td_proc->p_comm, __func__);
495 if (mcp->mc_vfp_size == sizeof(mc_vfp) && mcp->mc_vfp_ptr != NULL) {
496 if (copyin(mcp->mc_vfp_ptr, &mc_vfp, sizeof(mc_vfp)) != 0)
503 tf->tf_r0 = gr[_REG_R0];
504 tf->tf_r1 = gr[_REG_R1];
505 tf->tf_r2 = gr[_REG_R2];
506 tf->tf_r3 = gr[_REG_R3];
507 tf->tf_r4 = gr[_REG_R4];
508 tf->tf_r5 = gr[_REG_R5];
509 tf->tf_r6 = gr[_REG_R6];
510 tf->tf_r7 = gr[_REG_R7];
511 tf->tf_r8 = gr[_REG_R8];
512 tf->tf_r9 = gr[_REG_R9];
513 tf->tf_r10 = gr[_REG_R10];
514 tf->tf_r11 = gr[_REG_R11];
515 tf->tf_r12 = gr[_REG_R12];
516 tf->tf_usr_sp = gr[_REG_SP];
517 tf->tf_usr_lr = gr[_REG_LR];
518 tf->tf_pc = gr[_REG_PC];
519 tf->tf_spsr = gr[_REG_CPSR];
522 set_vfpcontext(td, vfp);
528 sendsig(catcher, ksi, mask)
535 struct trapframe *tf;
536 struct sigframe *fp, frame;
538 struct sysentvec *sysent;
545 PROC_LOCK_ASSERT(p, MA_OWNED);
546 sig = ksi->ksi_signo;
547 code = ksi->ksi_code;
549 mtx_assert(&psp->ps_mtx, MA_OWNED);
551 onstack = sigonstack(tf->tf_usr_sp);
553 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
556 /* Allocate and validate space for the signal handler context. */
557 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
558 SIGISMEMBER(psp->ps_sigonstack, sig)) {
559 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
560 td->td_sigstk.ss_size);
561 #if defined(COMPAT_43)
562 td->td_sigstk.ss_flags |= SS_ONSTACK;
565 fp = (struct sigframe *)td->td_frame->tf_usr_sp;
567 /* make room on the stack */
570 /* make the stack aligned */
571 fp = (struct sigframe *)STACKALIGN(fp);
572 /* Populate the siginfo frame. */
573 bzero(&frame, sizeof(frame));
574 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
576 get_vfpcontext(td, &frame.sf_vfp);
577 frame.sf_uc.uc_mcontext.mc_vfp_size = sizeof(fp->sf_vfp);
578 frame.sf_uc.uc_mcontext.mc_vfp_ptr = &fp->sf_vfp;
580 frame.sf_uc.uc_mcontext.mc_vfp_size = 0;
581 frame.sf_uc.uc_mcontext.mc_vfp_ptr = NULL;
583 frame.sf_si = ksi->ksi_info;
584 frame.sf_uc.uc_sigmask = *mask;
585 frame.sf_uc.uc_stack = td->td_sigstk;
586 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
587 (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
588 mtx_unlock(&psp->ps_mtx);
589 PROC_UNLOCK(td->td_proc);
591 /* Copy the sigframe out to the user's stack. */
592 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
593 /* Process has trashed its stack. Kill it. */
594 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
600 * Build context to run handler in. We invoke the handler
601 * directly, only returning via the trampoline. Note the
602 * trampoline version numbers are coordinated with machine-
603 * dependent code in libc.
607 tf->tf_r1 = (register_t)&fp->sf_si;
608 tf->tf_r2 = (register_t)&fp->sf_uc;
610 /* the trampoline uses r5 as the uc address */
611 tf->tf_r5 = (register_t)&fp->sf_uc;
612 tf->tf_pc = (register_t)catcher;
613 tf->tf_usr_sp = (register_t)fp;
614 sysent = p->p_sysent;
615 if (sysent->sv_sigcode_base != 0)
616 tf->tf_usr_lr = (register_t)sysent->sv_sigcode_base;
618 tf->tf_usr_lr = (register_t)(sysent->sv_psstrings -
619 *(sysent->sv_szsigcode));
620 /* Set the mode to enter in the signal handler */
622 if ((register_t)catcher & 1)
623 tf->tf_spsr |= PSR_T;
625 tf->tf_spsr &= ~PSR_T;
628 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
632 mtx_lock(&psp->ps_mtx);
636 sys_sigreturn(td, uap)
638 struct sigreturn_args /* {
639 const struct __ucontext *sigcntxp;
647 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
649 /* Restore register context. */
650 error = set_mcontext(td, &uc.uc_mcontext);
654 /* Restore signal mask. */
655 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
657 return (EJUSTRETURN);
661 * Construct a PCB from a trapframe. This is called from kdb_trap() where
662 * we want to start a backtrace from the function that caused us to enter
663 * the debugger. We have the context in the trapframe, but base the trace
664 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
665 * enough for a backtrace.
668 makectx(struct trapframe *tf, struct pcb *pcb)
670 pcb->pcb_regs.sf_r4 = tf->tf_r4;
671 pcb->pcb_regs.sf_r5 = tf->tf_r5;
672 pcb->pcb_regs.sf_r6 = tf->tf_r6;
673 pcb->pcb_regs.sf_r7 = tf->tf_r7;
674 pcb->pcb_regs.sf_r8 = tf->tf_r8;
675 pcb->pcb_regs.sf_r9 = tf->tf_r9;
676 pcb->pcb_regs.sf_r10 = tf->tf_r10;
677 pcb->pcb_regs.sf_r11 = tf->tf_r11;
678 pcb->pcb_regs.sf_r12 = tf->tf_r12;
679 pcb->pcb_regs.sf_pc = tf->tf_pc;
680 pcb->pcb_regs.sf_lr = tf->tf_usr_lr;
681 pcb->pcb_regs.sf_sp = tf->tf_usr_sp;
687 set_curthread(&thread0);
688 pcpu_init(pcpup, 0, sizeof(struct pcpu));
689 pcpup->pc_mpidr = cp15_mpidr_get() & 0xFFFFFF;
690 PCPU_SET(curthread, &thread0);
697 init_proc0(vm_offset_t kstack)
699 proc_linkup0(&proc0, &thread0);
700 thread0.td_kstack = kstack;
701 thread0.td_kstack_pages = kstack_pages;
702 thread0.td_pcb = (struct pcb *)(thread0.td_kstack +
703 thread0.td_kstack_pages * PAGE_SIZE) - 1;
704 thread0.td_pcb->pcb_flags = 0;
705 thread0.td_pcb->pcb_vfpcpu = -1;
706 thread0.td_pcb->pcb_vfpstate.fpscr = VFPSCR_DN;
707 thread0.td_frame = &proc0_tf;
708 pcpup->pc_curpcb = thread0.td_pcb;
712 set_stackptrs(int cpu)
715 set_stackptr(PSR_IRQ32_MODE,
716 irqstack + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
717 set_stackptr(PSR_ABT32_MODE,
718 abtstack + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
719 set_stackptr(PSR_UND32_MODE,
720 undstack + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
729 if (boothowto & RB_KDB)
730 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
736 initarm(struct arm_boot_params *abp)
738 struct mem_region mem_regions[FDT_MEM_REGIONS];
740 vm_offset_t dtbp, kernelstack, dpcpu;
743 int err_devmap, mem_regions_sz;
745 char dts_version[255];
747 struct efi_map_header *efihdr;
750 /* get last allocated physical address */
751 arm_physmem_kernaddr = abp->abp_physaddr;
752 lastaddr = parse_boot_param(abp) - KERNVIRTADDR + arm_physmem_kernaddr;
758 * Find the dtb passed in by the boot loader.
760 kmdp = preload_search_by_type("elf kernel");
761 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
762 #if defined(FDT_DTB_STATIC)
764 * In case the device tree blob was not retrieved (from metadata) try
765 * to use the statically embedded one.
767 if (dtbp == (vm_offset_t)NULL)
768 dtbp = (vm_offset_t)&fdt_static_dtb;
771 if (OF_install(OFW_FDT, 0) == FALSE)
772 panic("Cannot install FDT");
774 if (OF_init((void *)dtbp) != 0)
775 panic("OF_init failed with the found device tree");
777 #if defined(LINUX_BOOT_ABI)
778 arm_parse_fdt_bootargs();
782 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
783 MODINFO_METADATA | MODINFOMD_EFI_MAP);
784 if (efihdr != NULL) {
785 arm_add_efi_map_entries(efihdr, mem_regions, &mem_regions_sz);
789 /* Grab physical memory regions information from device tree. */
790 if (fdt_get_mem_regions(mem_regions, &mem_regions_sz,NULL) != 0)
791 panic("Cannot get physical memory regions");
793 physmem_hardware_regions(mem_regions, mem_regions_sz);
795 /* Grab reserved memory regions information from device tree. */
796 if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
797 physmem_exclude_regions(mem_regions, mem_regions_sz,
798 EXFLAG_NODUMP | EXFLAG_NOALLOC);
801 * Set TEX remapping registers.
802 * Setup kernel page tables and switch to kernel L1 page table.
805 pmap_bootstrap_prepare(lastaddr);
808 * If EARLY_PRINTF support is enabled, we need to re-establish the
809 * mapping after pmap_bootstrap_prepare() switches to new page tables.
810 * Note that we can only do the remapping if the VA is outside the
811 * kernel, now that we have real virtual (not VA=PA) mappings in effect.
812 * Early printf does not work between the time pmap_set_tex() does
813 * cp15_prrr_set() and this code remaps the VA.
815 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE
816 pmap_preboot_map_attr(SOCDEV_PA, SOCDEV_VA, 1024 * 1024,
817 VM_PROT_READ | VM_PROT_WRITE, VM_MEMATTR_DEVICE);
821 * Now that proper page tables are installed, call cpu_setup() to enable
822 * instruction and data caches and other chip-specific features.
826 /* Platform-specific initialisation */
827 platform_probe_and_attach();
830 /* Do basic tuning, hz etc */
834 * Allocate a page for the system page mapped to 0xffff0000
835 * This page will just contain the system vectors and can be
836 * shared by all processes.
838 systempage = pmap_preboot_get_pages(1);
840 /* Map the vector page. */
841 pmap_preboot_map_pages(systempage, ARM_VECTORS_HIGH, 1);
842 if (virtual_end >= ARM_VECTORS_HIGH)
843 virtual_end = ARM_VECTORS_HIGH - 1;
845 /* Allocate dynamic per-cpu area. */
846 dpcpu = pmap_preboot_get_vpages(DPCPU_SIZE / PAGE_SIZE);
847 dpcpu_init((void *)dpcpu, 0);
849 /* Allocate stacks for all modes */
850 irqstack = pmap_preboot_get_vpages(IRQ_STACK_SIZE * MAXCPU);
851 abtstack = pmap_preboot_get_vpages(ABT_STACK_SIZE * MAXCPU);
852 undstack = pmap_preboot_get_vpages(UND_STACK_SIZE * MAXCPU );
853 kernelstack = pmap_preboot_get_vpages(kstack_pages);
855 /* Allocate message buffer. */
856 msgbufp = (void *)pmap_preboot_get_vpages(
857 round_page(msgbufsize) / PAGE_SIZE);
860 * Pages were allocated during the secondary bootstrap for the
861 * stacks for different CPU modes.
862 * We must now set the r13 registers in the different CPU modes to
863 * point to these stacks.
864 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
865 * of the stack memory.
870 /* Establish static device mappings. */
871 err_devmap = platform_devmap_init();
872 devmap_bootstrap(0, NULL);
873 vm_max_kernel_address = platform_lastaddr();
876 * Only after the SOC registers block is mapped we can perform device
877 * tree fixups, as they may attempt to read parameters from hardware.
879 OF_interpret("perform-fixup", 0);
880 platform_gpio_init();
884 * If we made a mapping for EARLY_PRINTF after pmap_bootstrap_prepare(),
885 * undo it now that the normal console printf works.
887 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE
888 pmap_kremove(SOCDEV_VA);
891 debugf("initarm: console initialized\n");
892 debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
893 debugf(" boothowto = 0x%08x\n", boothowto);
894 debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
895 debugf(" lastaddr1: 0x%08x\n", lastaddr);
898 env = kern_getenv("kernelname");
900 strlcpy(kernelname, env, sizeof(kernelname));
903 printf("WARNING: could not fully configure devmap, error=%d\n",
906 platform_late_init();
908 root = OF_finddevice("/");
909 if (OF_getprop(root, "freebsd,dts-version", dts_version, sizeof(dts_version)) > 0) {
910 if (strcmp(LINUX_DTS_VERSION, dts_version) != 0)
911 printf("WARNING: DTB version is %s while kernel expects %s, "
912 "please update the DTB in the ESP\n",
916 printf("WARNING: Cannot find freebsd,dts-version property, "
917 "cannot check DTB compliance\n");
921 * We must now clean the cache again....
922 * Cleaning may be done by reading new data to displace any
923 * dirty data in the cache. This will have happened in cpu_setttb()
924 * but since we are boot strapping the addresses used for the read
925 * may have just been remapped and thus the cache could be out
926 * of sync. A re-clean after the switch will cure this.
927 * After booting there are no gross relocations of the kernel thus
928 * this problem will not occur after initarm().
930 /* Set stack for exception handlers */
932 init_proc0(kernelstack);
933 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
934 enable_interrupts(PSR_A);
937 /* Exclude the kernel (and all the things we allocated which immediately
938 * follow the kernel) from the VM allocation pool but not from crash
939 * dumps. virtual_avail is a global variable which tracks the kva we've
940 * "allocated" while setting up pmaps.
942 * Prepare the list of physical memory available to the vm subsystem.
944 physmem_exclude_region(abp->abp_physaddr,
945 pmap_preboot_get_pages(0) - abp->abp_physaddr, EXFLAG_NOALLOC);
946 physmem_init_kernel_globals();
948 init_param2(physmem);
949 /* Init message buffer. */
950 msgbufinit(msgbufp, msgbufsize);
953 /* Apply possible BP hardening. */
954 cpuinfo_init_bp_hardening();
955 return ((void *)STACKALIGN(thread0.td_pcb));