]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/machdep.c
sysctl(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / sys / arm / arm / machdep.c
1 /*      $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $        */
2
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 2004 Olivier Houchard
7  * Copyright (c) 1994-1998 Mark Brinicombe.
8  * Copyright (c) 1994 Brini.
9  * All rights reserved.
10  *
11  * This code is derived from software written for Brini by Mark Brinicombe
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
28  *
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
39  * SUCH DAMAGE.
40  *
41  * Machine dependent functions for kernel setup
42  *
43  * Created      : 17/09/94
44  * Updated      : 18/04/01 updated for new wscons
45  */
46
47 #include "opt_ddb.h"
48 #include "opt_kstack_pages.h"
49 #include "opt_platform.h"
50 #include "opt_sched.h"
51
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
54
55 #include <sys/param.h>
56 #include <sys/buf.h>
57 #include <sys/bus.h>
58 #include <sys/cons.h>
59 #include <sys/cpu.h>
60 #include <sys/devmap.h>
61 #include <sys/efi.h>
62 #include <sys/imgact.h>
63 #include <sys/kdb.h>
64 #include <sys/kernel.h>
65 #include <sys/ktr.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>
76
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pager.h>
80
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>
91
92 #ifdef FDT
93 #include <dev/fdt/fdt_common.h>
94 #include <machine/ofw_machdep.h>
95 #endif
96
97 #ifdef DEBUG
98 #define debugf(fmt, args...) printf(fmt, ##args)
99 #else
100 #define debugf(fmt, args...)
101 #endif
102
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
107 #endif
108
109
110 #ifndef _ARM_ARCH_6
111 #error FreeBSD requires ARMv6 or later
112 #endif
113
114 struct pcpu __pcpu[MAXCPU];
115 struct pcpu *pcpup = &__pcpu[0];
116
117 static struct trapframe proc0_tf;
118 uint32_t cpu_reset_address = 0;
119 int cold = 1;
120 vm_offset_t vector_page;
121
122 /* The address at which the kernel was loaded.  Set early in initarm(). */
123 vm_paddr_t arm_physmem_kernaddr;
124
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;
129
130 extern int *end;
131
132 #ifdef FDT
133 vm_paddr_t pmap_pa;
134 vm_offset_t systempage;
135 vm_offset_t irqstack;
136 vm_offset_t undstack;
137 vm_offset_t abtstack;
138 #endif /* FDT */
139
140 #ifdef PLATFORM
141 static delay_func *delay_impl;
142 static void *delay_arg;
143 #endif
144
145 struct kva_md_info kmi;
146 /*
147  * arm32_vector_init:
148  *
149  *      Initialize the vector page, and select whether or not to
150  *      relocate the vectors.
151  *
152  *      NOTE: We expect the vector page to be mapped at its expected
153  *      destination.
154  */
155
156 extern unsigned int page0[], page0_data[];
157 void
158 arm_vector_init(vm_offset_t va, int which)
159 {
160         unsigned int *vectors = (int *) va;
161         unsigned int *vectors_data = vectors + (page0_data - page0);
162         int vec;
163
164         /*
165          * Loop through the vectors we're taking over, and copy the
166          * vector's insn and data word.
167          */
168         for (vec = 0; vec < ARM_NVEC; vec++) {
169                 if ((which & (1 << vec)) == 0) {
170                         /* Don't want to take over this vector. */
171                         continue;
172                 }
173                 vectors[vec] = page0[vec];
174                 vectors_data[vec] = page0_data[vec];
175         }
176
177         /* Now sync the vectors. */
178         icache_sync(va, (ARM_NVEC * 2) * sizeof(u_int));
179
180         vector_page = va;
181 }
182
183 static void
184 cpu_startup(void *dummy)
185 {
186         struct pcb *pcb = thread0.td_pcb;
187         const unsigned int mbyte = 1024 * 1024;
188
189         identify_arm_cpu();
190
191         vm_ksubmap_init(&kmi);
192
193         /*
194          * Display the RAM layout.
195          */
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);
202         if (bootverbose) {
203                 physmem_print_tables();
204                 devmap_print_table();
205         }
206
207         bufinit();
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);
212 }
213
214 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
215
216 /*
217  * Flush the D-cache for non-DMA I/O so that the I-cache can
218  * be made coherent later.
219  */
220 void
221 cpu_flush_dcache(void *ptr, size_t len)
222 {
223
224         dcache_wb_poc((vm_offset_t)ptr, (vm_paddr_t)vtophys(ptr), len);
225 }
226
227 /* Get current clock frequency for the given cpu id. */
228 int
229 cpu_est_clockrate(int cpu_id, uint64_t *rate)
230 {
231         struct pcpu *pc;
232
233         pc = pcpu_find(cpu_id);
234         if (pc == NULL || rate == NULL)
235                 return (EINVAL);
236
237         if (pc->pc_clock == 0)
238                 return (EOPNOTSUPP);
239
240         *rate = pc->pc_clock;
241
242         return (0);
243 }
244
245 void
246 cpu_idle(int busy)
247 {
248
249         CTR2(KTR_SPARE2, "cpu_idle(%d) at %d", busy, curcpu);
250         spinlock_enter();
251         if (!busy)
252                 cpu_idleclock();
253         if (!sched_runnable())
254                 cpu_sleep(0);
255         if (!busy)
256                 cpu_activeclock();
257         spinlock_exit();
258         CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done", busy, curcpu);
259 }
260
261 int
262 cpu_idle_wakeup(int cpu)
263 {
264
265         return (0);
266 }
267
268 void
269 cpu_initclocks(void)
270 {
271
272 #ifdef SMP
273         if (PCPU_GET(cpuid) == 0)
274                 cpu_initclocks_bsp();
275         else
276                 cpu_initclocks_ap();
277 #else
278         cpu_initclocks_bsp();
279 #endif
280 }
281
282 #ifdef PLATFORM
283 void
284 arm_set_delay(delay_func *impl, void *arg)
285 {
286
287         KASSERT(impl != NULL, ("No DELAY implementation"));
288         delay_impl = impl;
289         delay_arg = arg;
290 }
291
292 void
293 DELAY(int usec)
294 {
295
296         TSENTER();
297         delay_impl(usec, delay_arg);
298         TSEXIT();
299 }
300 #endif
301
302 void
303 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
304 {
305
306         pcpu->pc_mpidr = 0xffffffff;
307 }
308
309 void
310 spinlock_enter(void)
311 {
312         struct thread *td;
313         register_t cspr;
314
315         td = curthread;
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;
320                 critical_enter();
321         } else
322                 td->td_md.md_spinlock_count++;
323 }
324
325 void
326 spinlock_exit(void)
327 {
328         struct thread *td;
329         register_t cspr;
330
331         td = curthread;
332         cspr = td->td_md.md_saved_cspr;
333         td->td_md.md_spinlock_count--;
334         if (td->td_md.md_spinlock_count == 0) {
335                 critical_exit();
336                 restore_interrupts(cspr);
337         }
338 }
339
340 /*
341  * Clear registers on exec
342  */
343 void
344 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
345 {
346         struct trapframe *tf = td->td_frame;
347
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;
354 }
355
356 #ifdef VFP
357 /*
358  * Get machine VFP context.
359  */
360 void
361 get_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
362 {
363         struct pcb *pcb;
364
365         pcb = td->td_pcb;
366         if (td == curthread) {
367                 critical_enter();
368                 vfp_store(&pcb->pcb_vfpstate, false);
369                 critical_exit();
370         } else
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;
375 }
376
377 /*
378  * Set machine VFP context.
379  */
380 void
381 set_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
382 {
383         struct pcb *pcb;
384
385         pcb = td->td_pcb;
386         if (td == curthread) {
387                 critical_enter();
388                 vfp_discard(td);
389                 critical_exit();
390         } else
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;
395 }
396 #endif
397
398 int
399 arm_get_vfpstate(struct thread *td, void *args)
400 {
401         int rv;
402         struct arm_get_vfpstate_args ua;
403         mcontext_vfp_t  mcontext_vfp;
404
405         rv = copyin(args, &ua, sizeof(ua));
406         if (rv != 0)
407                 return (rv);
408         if (ua.mc_vfp_size != sizeof(mcontext_vfp_t))
409                 return (EINVAL);
410 #ifdef VFP
411         get_vfpcontext(td, &mcontext_vfp);
412 #else
413         bzero(&mcontext_vfp, sizeof(mcontext_vfp));
414 #endif
415
416         rv = copyout(&mcontext_vfp, ua.mc_vfp,  sizeof(mcontext_vfp));
417         if (rv != 0)
418                 return (rv);
419         return (0);
420 }
421
422 /*
423  * Get machine context.
424  */
425 int
426 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
427 {
428         struct trapframe *tf = td->td_frame;
429         __greg_t *gr = mcp->__gregs;
430
431         if (clear_ret & GET_MC_CLEAR_RET) {
432                 gr[_REG_R0] = 0;
433                 gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C;
434         } else {
435                 gr[_REG_R0]   = tf->tf_r0;
436                 gr[_REG_CPSR] = tf->tf_spsr;
437         }
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;
453
454         mcp->mc_vfp_size = 0;
455         mcp->mc_vfp_ptr = NULL;
456         memset(&mcp->mc_spare, 0, sizeof(mcp->mc_spare));
457
458         return (0);
459 }
460
461 /*
462  * Set machine context.
463  *
464  * However, we don't set any but the user modifiable flags, and we won't
465  * touch the cs selector.
466  */
467 int
468 set_mcontext(struct thread *td, mcontext_t *mcp)
469 {
470         mcontext_vfp_t mc_vfp, *vfp;
471         struct trapframe *tf = td->td_frame;
472         const __greg_t *gr = mcp->__gregs;
473         int spsr;
474
475         /*
476          * Make sure the processor mode has not been tampered with and
477          * interrupts have not been disabled.
478          */
479         spsr = gr[_REG_CPSR];
480         if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
481             (spsr & (PSR_I | PSR_F)) != 0)
482                 return (EINVAL);
483
484 #ifdef WITNESS
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__);
492         }
493 #endif
494
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)
497                         return (EFAULT);
498                 vfp = &mc_vfp;
499         } else {
500                 vfp = NULL;
501         }
502
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];
520 #ifdef VFP
521         if (vfp != NULL)
522                 set_vfpcontext(td, vfp);
523 #endif
524         return (0);
525 }
526
527 void
528 sendsig(catcher, ksi, mask)
529         sig_t catcher;
530         ksiginfo_t *ksi;
531         sigset_t *mask;
532 {
533         struct thread *td;
534         struct proc *p;
535         struct trapframe *tf;
536         struct sigframe *fp, frame;
537         struct sigacts *psp;
538         struct sysentvec *sysent;
539         int onstack;
540         int sig;
541         int code;
542
543         td = curthread;
544         p = td->td_proc;
545         PROC_LOCK_ASSERT(p, MA_OWNED);
546         sig = ksi->ksi_signo;
547         code = ksi->ksi_code;
548         psp = p->p_sigacts;
549         mtx_assert(&psp->ps_mtx, MA_OWNED);
550         tf = td->td_frame;
551         onstack = sigonstack(tf->tf_usr_sp);
552
553         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
554             catcher, sig);
555
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;
563 #endif
564         } else
565                 fp = (struct sigframe *)td->td_frame->tf_usr_sp;
566
567         /* make room on the stack */
568         fp--;
569
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);
575 #ifdef VFP
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;
579 #else
580         frame.sf_uc.uc_mcontext.mc_vfp_size = 0;
581         frame.sf_uc.uc_mcontext.mc_vfp_ptr = NULL;
582 #endif
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);
590
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);
595                 PROC_LOCK(p);
596                 sigexit(td, SIGILL);
597         }
598
599         /*
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.
604          */
605
606         tf->tf_r0 = sig;
607         tf->tf_r1 = (register_t)&fp->sf_si;
608         tf->tf_r2 = (register_t)&fp->sf_uc;
609
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;
617         else
618                 tf->tf_usr_lr = (register_t)(sysent->sv_psstrings -
619                     *(sysent->sv_szsigcode));
620         /* Set the mode to enter in the signal handler */
621 #if __ARM_ARCH >= 7
622         if ((register_t)catcher & 1)
623                 tf->tf_spsr |= PSR_T;
624         else
625                 tf->tf_spsr &= ~PSR_T;
626 #endif
627
628         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
629             tf->tf_usr_sp);
630
631         PROC_LOCK(p);
632         mtx_lock(&psp->ps_mtx);
633 }
634
635 int
636 sys_sigreturn(td, uap)
637         struct thread *td;
638         struct sigreturn_args /* {
639                 const struct __ucontext *sigcntxp;
640         } */ *uap;
641 {
642         ucontext_t uc;
643         int error;
644
645         if (uap == NULL)
646                 return (EFAULT);
647         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
648                 return (EFAULT);
649         /* Restore register context. */
650         error = set_mcontext(td, &uc.uc_mcontext);
651         if (error != 0)
652                 return (error);
653
654         /* Restore signal mask. */
655         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
656
657         return (EJUSTRETURN);
658 }
659
660 /*
661  * Construct a PCB from a trapframe. This is called from kdb_trap() where
662  * we want to start a backtrace from the function that caused us to enter
663  * the debugger. We have the context in the trapframe, but base the trace
664  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
665  * enough for a backtrace.
666  */
667 void
668 makectx(struct trapframe *tf, struct pcb *pcb)
669 {
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;
682 }
683
684 void
685 pcpu0_init(void)
686 {
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);
691 }
692
693 /*
694  * Initialize proc0
695  */
696 void
697 init_proc0(vm_offset_t kstack)
698 {
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;
709 }
710
711 void
712 set_stackptrs(int cpu)
713 {
714
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)));
721 }
722
723 static void
724 arm_kdb_init(void)
725 {
726
727         kdb_init();
728 #ifdef KDB
729         if (boothowto & RB_KDB)
730                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
731 #endif
732 }
733
734 #ifdef FDT
735 void *
736 initarm(struct arm_boot_params *abp)
737 {
738         struct mem_region mem_regions[FDT_MEM_REGIONS];
739         vm_paddr_t lastaddr;
740         vm_offset_t dtbp, kernelstack, dpcpu;
741         char *env;
742         void *kmdp;
743         int err_devmap, mem_regions_sz;
744         phandle_t root;
745         char dts_version[255];
746 #ifdef EFI
747         struct efi_map_header *efihdr;
748 #endif
749
750         /* get last allocated physical address */
751         arm_physmem_kernaddr = abp->abp_physaddr;
752         lastaddr = parse_boot_param(abp) - KERNVIRTADDR + arm_physmem_kernaddr;
753
754         set_cpufuncs();
755         cpuinfo_init();
756
757         /*
758          * Find the dtb passed in by the boot loader.
759          */
760         kmdp = preload_search_by_type("elf kernel");
761         dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
762 #if defined(FDT_DTB_STATIC)
763         /*
764          * In case the device tree blob was not retrieved (from metadata) try
765          * to use the statically embedded one.
766          */
767         if (dtbp == (vm_offset_t)NULL)
768                 dtbp = (vm_offset_t)&fdt_static_dtb;
769 #endif
770
771         if (OF_install(OFW_FDT, 0) == FALSE)
772                 panic("Cannot install FDT");
773
774         if (OF_init((void *)dtbp) != 0)
775                 panic("OF_init failed with the found device tree");
776
777 #if defined(LINUX_BOOT_ABI)
778         arm_parse_fdt_bootargs();
779 #endif
780
781 #ifdef EFI
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);
786         } else
787 #endif
788         {
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");
792         }
793         physmem_hardware_regions(mem_regions, mem_regions_sz);
794
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);
799
800         /*
801          * Set TEX remapping registers.
802          * Setup kernel page tables and switch to kernel L1 page table.
803          */
804         pmap_set_tex();
805         pmap_bootstrap_prepare(lastaddr);
806
807         /*
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.
814          */
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);
818 #endif
819
820         /*
821          * Now that proper page tables are installed, call cpu_setup() to enable
822          * instruction and data caches and other chip-specific features.
823          */
824         cpu_setup();
825
826         /* Platform-specific initialisation */
827         platform_probe_and_attach();
828         pcpu0_init();
829
830         /* Do basic tuning, hz etc */
831         init_param1();
832
833         /*
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.
837          */
838         systempage = pmap_preboot_get_pages(1);
839
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;
844
845         /* Allocate dynamic per-cpu area. */
846         dpcpu = pmap_preboot_get_vpages(DPCPU_SIZE / PAGE_SIZE);
847         dpcpu_init((void *)dpcpu, 0);
848
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);
854
855         /* Allocate message buffer. */
856         msgbufp = (void *)pmap_preboot_get_vpages(
857             round_page(msgbufsize) / PAGE_SIZE);
858
859         /*
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.
866          */
867         set_stackptrs(0);
868         mutex_init();
869
870         /* Establish static device mappings. */
871         err_devmap = platform_devmap_init();
872         devmap_bootstrap(0, NULL);
873         vm_max_kernel_address = platform_lastaddr();
874
875         /*
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.
878          */
879         OF_interpret("perform-fixup", 0);
880         platform_gpio_init();
881         cninit();
882
883         /*
884          * If we made a mapping for EARLY_PRINTF after pmap_bootstrap_prepare(),
885          * undo it now that the normal console printf works.
886          */
887 #if defined(EARLY_PRINTF) && defined(SOCDEV_PA) && defined(SOCDEV_VA) && SOCDEV_VA < KERNBASE
888         pmap_kremove(SOCDEV_VA);
889 #endif
890
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);
896         arm_print_kenv();
897
898         env = kern_getenv("kernelname");
899         if (env != NULL)
900                 strlcpy(kernelname, env, sizeof(kernelname));
901
902         if (err_devmap != 0)
903                 printf("WARNING: could not fully configure devmap, error=%d\n",
904                     err_devmap);
905
906         platform_late_init();
907
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",
913                             dts_version,
914                             LINUX_DTS_VERSION);
915         } else {
916                 printf("WARNING: Cannot find freebsd,dts-version property, "
917                     "cannot check DTB compliance\n");
918         }
919
920         /*
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().
929          */
930         /* Set stack for exception handlers */
931         undefined_init();
932         init_proc0(kernelstack);
933         arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
934         enable_interrupts(PSR_A);
935         pmap_bootstrap(0);
936
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.
941          *
942          * Prepare the list of physical memory available to the vm subsystem.
943          */
944         physmem_exclude_region(abp->abp_physaddr,
945                 pmap_preboot_get_pages(0) - abp->abp_physaddr, EXFLAG_NOALLOC);
946         physmem_init_kernel_globals();
947
948         init_param2(physmem);
949         /* Init message buffer. */
950         msgbufinit(msgbufp, msgbufsize);
951         dbg_monitor_init();
952         arm_kdb_init();
953         /* Apply possible BP hardening. */
954         cpuinfo_init_bp_hardening();
955         return ((void *)STACKALIGN(thread0.td_pcb));
956
957 }
958 #endif /* FDT */