]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/arm/arm/machdep.c
MFC 257774, 256760, 262916, 262905, 262918, 262919, 262920, 262921, 262924,
[FreeBSD/stable/10.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  * Copyright (c) 2004 Olivier Houchard
5  * Copyright (c) 1994-1998 Mark Brinicombe.
6  * Copyright (c) 1994 Brini.
7  * All rights reserved.
8  *
9  * This code is derived from software written for Brini by Mark Brinicombe
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
26  *
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
37  * SUCH DAMAGE.
38  *
39  * Machine dependant functions for kernel setup
40  *
41  * Created      : 17/09/94
42  * Updated      : 18/04/01 updated for new wscons
43  */
44
45 #include "opt_compat.h"
46 #include "opt_ddb.h"
47 #include "opt_platform.h"
48 #include "opt_sched.h"
49 #include "opt_timer.h"
50
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
53
54 #include <sys/param.h>
55 #include <sys/proc.h>
56 #include <sys/systm.h>
57 #include <sys/bio.h>
58 #include <sys/buf.h>
59 #include <sys/bus.h>
60 #include <sys/cons.h>
61 #include <sys/cpu.h>
62 #include <sys/exec.h>
63 #include <sys/imgact.h>
64 #include <sys/kdb.h>
65 #include <sys/kernel.h>
66 #include <sys/ktr.h>
67 #include <sys/linker.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/msgbuf.h>
71 #include <sys/mutex.h>
72 #include <sys/pcpu.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>
81 #include <sys/uio.h>
82
83 #include <vm/vm.h>
84 #include <vm/pmap.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>
89
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/vmparam.h>
105 #include <machine/sysarch.h>
106
107 #ifdef FDT
108 #include <dev/fdt/fdt_common.h>
109 #include <dev/ofw/openfirm.h>
110 #endif
111
112 #ifdef DEBUG
113 #define debugf(fmt, args...) printf(fmt, ##args)
114 #else
115 #define debugf(fmt, args...)
116 #endif
117
118 struct pcpu __pcpu[MAXCPU];
119 struct pcpu *pcpup = &__pcpu[0];
120
121 static struct trapframe proc0_tf;
122 uint32_t cpu_reset_address = 0;
123 int cold = 1;
124 vm_offset_t vector_page;
125
126 int (*_arm_memcpy)(void *, void *, int, int) = NULL;
127 int (*_arm_bzero)(void *, int, int) = NULL;
128 int _min_memcpy_size = 0;
129 int _min_bzero_size = 0;
130
131 extern int *end;
132 #ifdef DDB
133 extern vm_offset_t ksym_start, ksym_end;
134 #endif
135
136 #ifdef FDT
137 /*
138  * This is the number of L2 page tables required for covering max
139  * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
140  * stacks etc.), uprounded to be divisible by 4.
141  */
142 #define KERNEL_PT_MAX   78
143
144 static struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
145
146 extern u_int data_abort_handler_address;
147 extern u_int prefetch_abort_handler_address;
148 extern u_int undefined_handler_address;
149
150 vm_paddr_t pmap_pa;
151
152 struct pv_addr systempage;
153 static struct pv_addr msgbufpv;
154 struct pv_addr irqstack;
155 struct pv_addr undstack;
156 struct pv_addr abtstack;
157 static struct pv_addr kernelstack;
158
159 #endif
160
161 #if defined(LINUX_BOOT_ABI)
162 #define LBABI_MAX_BANKS 10
163
164 uint32_t board_id;
165 struct arm_lbabi_tag *atag_list;
166 char linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
167 char atags[LBABI_MAX_COMMAND_LINE * 2];
168 uint32_t memstart[LBABI_MAX_BANKS];
169 uint32_t memsize[LBABI_MAX_BANKS];
170 uint32_t membanks;
171 #endif
172
173 static uint32_t board_revision;
174 /* hex representation of uint64_t */
175 static char board_serial[32];
176
177 SYSCTL_NODE(_hw, OID_AUTO, board, CTLFLAG_RD, 0, "Board attributes");
178 SYSCTL_UINT(_hw_board, OID_AUTO, revision, CTLFLAG_RD,
179     &board_revision, 0, "Board revision");
180 SYSCTL_STRING(_hw_board, OID_AUTO, serial, CTLFLAG_RD,
181     board_serial, 0, "Board serial");
182
183 int vfp_exists;
184 SYSCTL_INT(_hw, HW_FLOATINGPT, floatingpoint, CTLFLAG_RD,
185     &vfp_exists, 0, "Floating point support enabled");
186
187 void
188 board_set_serial(uint64_t serial)
189 {
190
191         snprintf(board_serial, sizeof(board_serial)-1, 
192                     "%016jx", serial);
193 }
194
195 void
196 board_set_revision(uint32_t revision)
197 {
198
199         board_revision = revision;
200 }
201
202 void
203 sendsig(catcher, ksi, mask)
204         sig_t catcher;
205         ksiginfo_t *ksi;
206         sigset_t *mask;
207 {
208         struct thread *td;
209         struct proc *p;
210         struct trapframe *tf;
211         struct sigframe *fp, frame;
212         struct sigacts *psp;
213         int onstack;
214         int sig;
215         int code;
216
217         td = curthread;
218         p = td->td_proc;
219         PROC_LOCK_ASSERT(p, MA_OWNED);
220         sig = ksi->ksi_signo;
221         code = ksi->ksi_code;
222         psp = p->p_sigacts;
223         mtx_assert(&psp->ps_mtx, MA_OWNED);
224         tf = td->td_frame;
225         onstack = sigonstack(tf->tf_usr_sp);
226
227         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
228             catcher, sig);
229
230         /* Allocate and validate space for the signal handler context. */
231         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
232             SIGISMEMBER(psp->ps_sigonstack, sig)) {
233                 fp = (struct sigframe *)(td->td_sigstk.ss_sp +
234                     td->td_sigstk.ss_size);
235 #if defined(COMPAT_43)
236                 td->td_sigstk.ss_flags |= SS_ONSTACK;
237 #endif
238         } else
239                 fp = (struct sigframe *)td->td_frame->tf_usr_sp;
240
241         /* make room on the stack */
242         fp--;
243         
244         /* make the stack aligned */
245         fp = (struct sigframe *)STACKALIGN(fp);
246         /* Populate the siginfo frame. */
247         get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
248         frame.sf_si = ksi->ksi_info;
249         frame.sf_uc.uc_sigmask = *mask;
250         frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK )
251             ? ((onstack) ? SS_ONSTACK : 0) : SS_DISABLE;
252         frame.sf_uc.uc_stack = td->td_sigstk;
253         mtx_unlock(&psp->ps_mtx);
254         PROC_UNLOCK(td->td_proc);
255
256         /* Copy the sigframe out to the user's stack. */
257         if (copyout(&frame, fp, sizeof(*fp)) != 0) {
258                 /* Process has trashed its stack. Kill it. */
259                 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
260                 PROC_LOCK(p);
261                 sigexit(td, SIGILL);
262         }
263
264         /* Translate the signal if appropriate. */
265         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
266                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
267
268         /*
269          * Build context to run handler in.  We invoke the handler
270          * directly, only returning via the trampoline.  Note the
271          * trampoline version numbers are coordinated with machine-
272          * dependent code in libc.
273          */
274         
275         tf->tf_r0 = sig;
276         tf->tf_r1 = (register_t)&fp->sf_si;
277         tf->tf_r2 = (register_t)&fp->sf_uc;
278
279         /* the trampoline uses r5 as the uc address */
280         tf->tf_r5 = (register_t)&fp->sf_uc;
281         tf->tf_pc = (register_t)catcher;
282         tf->tf_usr_sp = (register_t)fp;
283         tf->tf_usr_lr = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
284
285         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
286             tf->tf_usr_sp);
287
288         PROC_LOCK(p);
289         mtx_lock(&psp->ps_mtx);
290 }
291
292 struct kva_md_info kmi;
293
294 /*
295  * arm32_vector_init:
296  *
297  *      Initialize the vector page, and select whether or not to
298  *      relocate the vectors.
299  *
300  *      NOTE: We expect the vector page to be mapped at its expected
301  *      destination.
302  */
303
304 extern unsigned int page0[], page0_data[];
305 void
306 arm_vector_init(vm_offset_t va, int which)
307 {
308         unsigned int *vectors = (int *) va;
309         unsigned int *vectors_data = vectors + (page0_data - page0);
310         int vec;
311
312         /*
313          * Loop through the vectors we're taking over, and copy the
314          * vector's insn and data word.
315          */
316         for (vec = 0; vec < ARM_NVEC; vec++) {
317                 if ((which & (1 << vec)) == 0) {
318                         /* Don't want to take over this vector. */
319                         continue;
320                 }
321                 vectors[vec] = page0[vec];
322                 vectors_data[vec] = page0_data[vec];
323         }
324
325         /* Now sync the vectors. */
326         cpu_icache_sync_range(va, (ARM_NVEC * 2) * sizeof(u_int));
327
328         vector_page = va;
329
330         if (va == ARM_VECTORS_HIGH) {
331                 /*
332                  * Assume the MD caller knows what it's doing here, and
333                  * really does want the vector page relocated.
334                  *
335                  * Note: This has to be done here (and not just in
336                  * cpu_setup()) because the vector page needs to be
337                  * accessible *before* cpu_startup() is called.
338                  * Think ddb(9) ...
339                  *
340                  * NOTE: If the CPU control register is not readable,
341                  * this will totally fail!  We'll just assume that
342                  * any system that has high vector support has a
343                  * readable CPU control register, for now.  If we
344                  * ever encounter one that does not, we'll have to
345                  * rethink this.
346                  */
347                 cpu_control(CPU_CONTROL_VECRELOC, CPU_CONTROL_VECRELOC);
348         }
349 }
350
351 static void
352 cpu_startup(void *dummy)
353 {
354         struct pcb *pcb = thread0.td_pcb;
355         const unsigned int mbyte = 1024 * 1024;
356 #ifdef ARM_TP_ADDRESS
357 #ifndef ARM_CACHE_LOCK_ENABLE
358         vm_page_t m;
359 #endif
360 #endif
361
362         identify_arm_cpu();
363
364         vm_ksubmap_init(&kmi);
365
366         /*
367          * Display the RAM layout.
368          */
369         printf("real memory  = %ju (%ju MB)\n", 
370             (uintmax_t)arm32_ptob(realmem),
371             (uintmax_t)arm32_ptob(realmem) / mbyte);
372         printf("avail memory = %ju (%ju MB)\n",
373             (uintmax_t)arm32_ptob(cnt.v_free_count),
374             (uintmax_t)arm32_ptob(cnt.v_free_count) / mbyte);
375         if (bootverbose) {
376                 arm_physmem_print_tables();
377                 arm_devmap_print_table();
378         }
379
380         bufinit();
381         vm_pager_bufferinit();
382         pcb->un_32.pcb32_sp = (u_int)thread0.td_kstack +
383             USPACE_SVC_STACK_TOP;
384         vector_page_setprot(VM_PROT_READ);
385         pmap_set_pcb_pagedir(pmap_kernel(), pcb);
386         pmap_postinit();
387 #ifdef ARM_TP_ADDRESS
388 #ifdef ARM_CACHE_LOCK_ENABLE
389         pmap_kenter_user(ARM_TP_ADDRESS, ARM_TP_ADDRESS);
390         arm_lock_cache_line(ARM_TP_ADDRESS);
391 #else
392         m = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ | VM_ALLOC_ZERO);
393         pmap_kenter_user(ARM_TP_ADDRESS, VM_PAGE_TO_PHYS(m));
394 #endif
395         *(uint32_t *)ARM_RAS_START = 0;
396         *(uint32_t *)ARM_RAS_END = 0xffffffff;
397 #endif
398 }
399
400 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
401
402 /*
403  * Flush the D-cache for non-DMA I/O so that the I-cache can
404  * be made coherent later.
405  */
406 void
407 cpu_flush_dcache(void *ptr, size_t len)
408 {
409
410         cpu_dcache_wb_range((uintptr_t)ptr, len);
411 #ifdef ARM_L2_PIPT
412         cpu_l2cache_wb_range((uintptr_t)vtophys(ptr), len);
413 #else
414         cpu_l2cache_wb_range((uintptr_t)ptr, len);
415 #endif
416 }
417
418 /* Get current clock frequency for the given cpu id. */
419 int
420 cpu_est_clockrate(int cpu_id, uint64_t *rate)
421 {
422
423         return (ENXIO);
424 }
425
426 void
427 cpu_idle(int busy)
428 {
429         
430         CTR2(KTR_SPARE2, "cpu_idle(%d) at %d",
431             busy, curcpu);
432 #ifndef NO_EVENTTIMERS
433         if (!busy) {
434                 critical_enter();
435                 cpu_idleclock();
436         }
437 #endif
438         if (!sched_runnable())
439                 cpu_sleep(0);
440 #ifndef NO_EVENTTIMERS
441         if (!busy) {
442                 cpu_activeclock();
443                 critical_exit();
444         }
445 #endif
446         CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done",
447             busy, curcpu);
448 }
449
450 int
451 cpu_idle_wakeup(int cpu)
452 {
453
454         return (0);
455 }
456
457 /*
458  * Most ARM platforms don't need to do anything special to init their clocks
459  * (they get intialized during normal device attachment), and by not defining a
460  * cpu_initclocks() function they get this generic one.  Any platform that needs
461  * to do something special can just provide their own implementation, which will
462  * override this one due to the weak linkage.
463  */
464 void
465 arm_generic_initclocks(void)
466 {
467
468 #ifndef NO_EVENTTIMERS
469 #ifdef SMP
470         if (PCPU_GET(cpuid) == 0)
471                 cpu_initclocks_bsp();
472         else
473                 cpu_initclocks_ap();
474 #else
475         cpu_initclocks_bsp();
476 #endif
477 #endif
478 }
479 __weak_reference(arm_generic_initclocks, cpu_initclocks);
480
481 int
482 fill_regs(struct thread *td, struct reg *regs)
483 {
484         struct trapframe *tf = td->td_frame;
485         bcopy(&tf->tf_r0, regs->r, sizeof(regs->r));
486         regs->r_sp = tf->tf_usr_sp;
487         regs->r_lr = tf->tf_usr_lr;
488         regs->r_pc = tf->tf_pc;
489         regs->r_cpsr = tf->tf_spsr;
490         return (0);
491 }
492 int
493 fill_fpregs(struct thread *td, struct fpreg *regs)
494 {
495         bzero(regs, sizeof(*regs));
496         return (0);
497 }
498
499 int
500 set_regs(struct thread *td, struct reg *regs)
501 {
502         struct trapframe *tf = td->td_frame;
503         
504         bcopy(regs->r, &tf->tf_r0, sizeof(regs->r));
505         tf->tf_usr_sp = regs->r_sp;
506         tf->tf_usr_lr = regs->r_lr;
507         tf->tf_pc = regs->r_pc;
508         tf->tf_spsr &=  ~PSR_FLAGS;
509         tf->tf_spsr |= regs->r_cpsr & PSR_FLAGS;
510         return (0);                                                             
511 }
512
513 int
514 set_fpregs(struct thread *td, struct fpreg *regs)
515 {
516         return (0);
517 }
518
519 int
520 fill_dbregs(struct thread *td, struct dbreg *regs)
521 {
522         return (0);
523 }
524 int
525 set_dbregs(struct thread *td, struct dbreg *regs)
526 {
527         return (0);
528 }
529
530
531 static int
532 ptrace_read_int(struct thread *td, vm_offset_t addr, u_int32_t *v)
533 {
534         struct iovec iov;
535         struct uio uio;
536
537         PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
538         iov.iov_base = (caddr_t) v;
539         iov.iov_len = sizeof(u_int32_t);
540         uio.uio_iov = &iov;
541         uio.uio_iovcnt = 1;
542         uio.uio_offset = (off_t)addr;
543         uio.uio_resid = sizeof(u_int32_t);
544         uio.uio_segflg = UIO_SYSSPACE;
545         uio.uio_rw = UIO_READ;
546         uio.uio_td = td;
547         return proc_rwmem(td->td_proc, &uio);
548 }
549
550 static int
551 ptrace_write_int(struct thread *td, vm_offset_t addr, u_int32_t v)
552 {
553         struct iovec iov;
554         struct uio uio;
555
556         PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
557         iov.iov_base = (caddr_t) &v;
558         iov.iov_len = sizeof(u_int32_t);
559         uio.uio_iov = &iov;
560         uio.uio_iovcnt = 1;
561         uio.uio_offset = (off_t)addr;
562         uio.uio_resid = sizeof(u_int32_t);
563         uio.uio_segflg = UIO_SYSSPACE;
564         uio.uio_rw = UIO_WRITE;
565         uio.uio_td = td;
566         return proc_rwmem(td->td_proc, &uio);
567 }
568
569 int
570 ptrace_single_step(struct thread *td)
571 {
572         struct proc *p;
573         int error;
574         
575         KASSERT(td->td_md.md_ptrace_instr == 0,
576          ("Didn't clear single step"));
577         p = td->td_proc;
578         PROC_UNLOCK(p);
579         error = ptrace_read_int(td, td->td_frame->tf_pc + 4,
580             &td->td_md.md_ptrace_instr);
581         if (error)
582                 goto out;
583         error = ptrace_write_int(td, td->td_frame->tf_pc + 4,
584             PTRACE_BREAKPOINT);
585         if (error)
586                 td->td_md.md_ptrace_instr = 0;
587         td->td_md.md_ptrace_addr = td->td_frame->tf_pc + 4;
588 out:
589         PROC_LOCK(p);
590         return (error);
591 }
592
593 int
594 ptrace_clear_single_step(struct thread *td)
595 {
596         struct proc *p;
597
598         if (td->td_md.md_ptrace_instr) {
599                 p = td->td_proc;
600                 PROC_UNLOCK(p);
601                 ptrace_write_int(td, td->td_md.md_ptrace_addr,
602                     td->td_md.md_ptrace_instr);
603                 PROC_LOCK(p);
604                 td->td_md.md_ptrace_instr = 0;
605         }
606         return (0);
607 }
608
609 int
610 ptrace_set_pc(struct thread *td, unsigned long addr)
611 {
612         td->td_frame->tf_pc = addr;
613         return (0);
614 }
615
616 void
617 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
618 {
619 }
620
621 void
622 spinlock_enter(void)
623 {
624         struct thread *td;
625         register_t cspr;
626
627         td = curthread;
628         if (td->td_md.md_spinlock_count == 0) {
629                 cspr = disable_interrupts(I32_bit | F32_bit);
630                 td->td_md.md_spinlock_count = 1;
631                 td->td_md.md_saved_cspr = cspr;
632         } else
633                 td->td_md.md_spinlock_count++;
634         critical_enter();
635 }
636
637 void
638 spinlock_exit(void)
639 {
640         struct thread *td;
641         register_t cspr;
642
643         td = curthread;
644         critical_exit();
645         cspr = td->td_md.md_saved_cspr;
646         td->td_md.md_spinlock_count--;
647         if (td->td_md.md_spinlock_count == 0)
648                 restore_interrupts(cspr);
649 }
650
651 /*
652  * Clear registers on exec
653  */
654 void
655 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
656 {
657         struct trapframe *tf = td->td_frame;
658
659         memset(tf, 0, sizeof(*tf));
660         tf->tf_usr_sp = stack;
661         tf->tf_usr_lr = imgp->entry_addr;
662         tf->tf_svc_lr = 0x77777777;
663         tf->tf_pc = imgp->entry_addr;
664         tf->tf_spsr = PSR_USR32_MODE;
665 }
666
667 /*
668  * Get machine context.
669  */
670 int
671 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
672 {
673         struct trapframe *tf = td->td_frame;
674         __greg_t *gr = mcp->__gregs;
675
676         if (clear_ret & GET_MC_CLEAR_RET)
677                 gr[_REG_R0] = 0;
678         else
679                 gr[_REG_R0]   = tf->tf_r0;
680         gr[_REG_R1]   = tf->tf_r1;
681         gr[_REG_R2]   = tf->tf_r2;
682         gr[_REG_R3]   = tf->tf_r3;
683         gr[_REG_R4]   = tf->tf_r4;
684         gr[_REG_R5]   = tf->tf_r5;
685         gr[_REG_R6]   = tf->tf_r6;
686         gr[_REG_R7]   = tf->tf_r7;
687         gr[_REG_R8]   = tf->tf_r8;
688         gr[_REG_R9]   = tf->tf_r9;
689         gr[_REG_R10]  = tf->tf_r10;
690         gr[_REG_R11]  = tf->tf_r11;
691         gr[_REG_R12]  = tf->tf_r12;
692         gr[_REG_SP]   = tf->tf_usr_sp;
693         gr[_REG_LR]   = tf->tf_usr_lr;
694         gr[_REG_PC]   = tf->tf_pc;
695         gr[_REG_CPSR] = tf->tf_spsr;
696
697         return (0);
698 }
699
700 /*
701  * Set machine context.
702  *
703  * However, we don't set any but the user modifiable flags, and we won't
704  * touch the cs selector.
705  */
706 int
707 set_mcontext(struct thread *td, const mcontext_t *mcp)
708 {
709         struct trapframe *tf = td->td_frame;
710         const __greg_t *gr = mcp->__gregs;
711
712         tf->tf_r0 = gr[_REG_R0];
713         tf->tf_r1 = gr[_REG_R1];
714         tf->tf_r2 = gr[_REG_R2];
715         tf->tf_r3 = gr[_REG_R3];
716         tf->tf_r4 = gr[_REG_R4];
717         tf->tf_r5 = gr[_REG_R5];
718         tf->tf_r6 = gr[_REG_R6];
719         tf->tf_r7 = gr[_REG_R7];
720         tf->tf_r8 = gr[_REG_R8];
721         tf->tf_r9 = gr[_REG_R9];
722         tf->tf_r10 = gr[_REG_R10];
723         tf->tf_r11 = gr[_REG_R11];
724         tf->tf_r12 = gr[_REG_R12];
725         tf->tf_usr_sp = gr[_REG_SP];
726         tf->tf_usr_lr = gr[_REG_LR];
727         tf->tf_pc = gr[_REG_PC];
728         tf->tf_spsr = gr[_REG_CPSR];
729
730         return (0);
731 }
732
733 /*
734  * MPSAFE
735  */
736 int
737 sys_sigreturn(td, uap)
738         struct thread *td;
739         struct sigreturn_args /* {
740                 const struct __ucontext *sigcntxp;
741         } */ *uap;
742 {
743         ucontext_t uc;
744         int spsr;
745         
746         if (uap == NULL)
747                 return (EFAULT);
748         if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
749                 return (EFAULT);
750         /*
751          * Make sure the processor mode has not been tampered with and
752          * interrupts have not been disabled.
753          */
754         spsr = uc.uc_mcontext.__gregs[_REG_CPSR];
755         if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
756             (spsr & (I32_bit | F32_bit)) != 0)
757                 return (EINVAL);
758                 /* Restore register context. */
759         set_mcontext(td, &uc.uc_mcontext);
760
761         /* Restore signal mask. */
762         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
763
764         return (EJUSTRETURN);
765 }
766
767
768 /*
769  * Construct a PCB from a trapframe. This is called from kdb_trap() where
770  * we want to start a backtrace from the function that caused us to enter
771  * the debugger. We have the context in the trapframe, but base the trace
772  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
773  * enough for a backtrace.
774  */
775 void
776 makectx(struct trapframe *tf, struct pcb *pcb)
777 {
778         pcb->un_32.pcb32_r8 = tf->tf_r8;
779         pcb->un_32.pcb32_r9 = tf->tf_r9;
780         pcb->un_32.pcb32_r10 = tf->tf_r10;
781         pcb->un_32.pcb32_r11 = tf->tf_r11;
782         pcb->un_32.pcb32_r12 = tf->tf_r12;
783         pcb->un_32.pcb32_pc = tf->tf_pc;
784         pcb->un_32.pcb32_lr = tf->tf_usr_lr;
785         pcb->un_32.pcb32_sp = tf->tf_usr_sp;
786 }
787
788 /*
789  * Fake up a boot descriptor table
790  */
791 vm_offset_t
792 fake_preload_metadata(struct arm_boot_params *abp __unused)
793 {
794 #ifdef DDB
795         vm_offset_t zstart = 0, zend = 0;
796 #endif
797         vm_offset_t lastaddr;
798         int i = 0;
799         static uint32_t fake_preload[35];
800
801         fake_preload[i++] = MODINFO_NAME;
802         fake_preload[i++] = strlen("kernel") + 1;
803         strcpy((char*)&fake_preload[i++], "kernel");
804         i += 1;
805         fake_preload[i++] = MODINFO_TYPE;
806         fake_preload[i++] = strlen("elf kernel") + 1;
807         strcpy((char*)&fake_preload[i++], "elf kernel");
808         i += 2;
809         fake_preload[i++] = MODINFO_ADDR;
810         fake_preload[i++] = sizeof(vm_offset_t);
811         fake_preload[i++] = KERNVIRTADDR;
812         fake_preload[i++] = MODINFO_SIZE;
813         fake_preload[i++] = sizeof(uint32_t);
814         fake_preload[i++] = (uint32_t)&end - KERNVIRTADDR;
815 #ifdef DDB
816         if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
817                 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
818                 fake_preload[i++] = sizeof(vm_offset_t);
819                 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
820                 fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
821                 fake_preload[i++] = sizeof(vm_offset_t);
822                 fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
823                 lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
824                 zend = lastaddr;
825                 zstart = *(uint32_t *)(KERNVIRTADDR + 4);
826                 ksym_start = zstart;
827                 ksym_end = zend;
828         } else
829 #endif
830                 lastaddr = (vm_offset_t)&end;
831         fake_preload[i++] = 0;
832         fake_preload[i] = 0;
833         preload_metadata = (void *)fake_preload;
834
835         return (lastaddr);
836 }
837
838 void
839 pcpu0_init(void)
840 {
841 #if ARM_ARCH_6 || ARM_ARCH_7A || defined(CPU_MV_PJ4B)
842         set_curthread(&thread0);
843 #endif
844         pcpu_init(pcpup, 0, sizeof(struct pcpu));
845         PCPU_SET(curthread, &thread0);
846 #ifdef VFP
847         PCPU_SET(cpu, 0);
848 #endif
849 }
850
851 #if defined(LINUX_BOOT_ABI)
852 vm_offset_t
853 linux_parse_boot_param(struct arm_boot_params *abp)
854 {
855         struct arm_lbabi_tag *walker;
856         uint32_t revision;
857         uint64_t serial;
858
859         /*
860          * Linux boot ABI: r0 = 0, r1 is the board type (!= 0) and r2
861          * is atags or dtb pointer.  If all of these aren't satisfied,
862          * then punt.
863          */
864         if (!(abp->abp_r0 == 0 && abp->abp_r1 != 0 && abp->abp_r2 != 0))
865                 return 0;
866
867         board_id = abp->abp_r1;
868         walker = (struct arm_lbabi_tag *)
869             (abp->abp_r2 + KERNVIRTADDR - abp->abp_physaddr);
870
871         /* xxx - Need to also look for binary device tree */
872         if (ATAG_TAG(walker) != ATAG_CORE)
873                 return 0;
874
875         atag_list = walker;
876         while (ATAG_TAG(walker) != ATAG_NONE) {
877                 switch (ATAG_TAG(walker)) {
878                 case ATAG_CORE:
879                         break;
880                 case ATAG_MEM:
881                         arm_physmem_hardware_region(walker->u.tag_mem.start,
882                             walker->u.tag_mem.size);
883                         break;
884                 case ATAG_INITRD2:
885                         break;
886                 case ATAG_SERIAL:
887                         serial = walker->u.tag_sn.low |
888                             ((uint64_t)walker->u.tag_sn.high << 32);
889                         board_set_serial(serial);
890                         break;
891                 case ATAG_REVISION:
892                         revision = walker->u.tag_rev.rev;
893                         board_set_revision(revision);
894                         break;
895                 case ATAG_CMDLINE:
896                         /* XXX open question: Parse this for boothowto? */
897                         bcopy(walker->u.tag_cmd.command, linux_command_line,
898                               ATAG_SIZE(walker));
899                         break;
900                 default:
901                         break;
902                 }
903                 walker = ATAG_NEXT(walker);
904         }
905
906         /* Save a copy for later */
907         bcopy(atag_list, atags,
908             (char *)walker - (char *)atag_list + ATAG_SIZE(walker));
909
910         return fake_preload_metadata(abp);
911 }
912 #endif
913
914 #if defined(FREEBSD_BOOT_LOADER)
915 vm_offset_t
916 freebsd_parse_boot_param(struct arm_boot_params *abp)
917 {
918         vm_offset_t lastaddr = 0;
919         void *mdp;
920         void *kmdp;
921
922         /*
923          * Mask metadata pointer: it is supposed to be on page boundary. If
924          * the first argument (mdp) doesn't point to a valid address the
925          * bootloader must have passed us something else than the metadata
926          * ptr, so we give up.  Also give up if we cannot find metadta section
927          * the loader creates that we get all this data out of.
928          */
929
930         if ((mdp = (void *)(abp->abp_r0 & ~PAGE_MASK)) == NULL)
931                 return 0;
932         preload_metadata = mdp;
933         kmdp = preload_search_by_type("elf kernel");
934         if (kmdp == NULL)
935                 return 0;
936
937         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
938         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
939         lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
940 #ifdef DDB
941         ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
942         ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
943 #endif
944         preload_addr_relocate = KERNVIRTADDR - abp->abp_physaddr;
945         return lastaddr;
946 }
947 #endif
948
949 vm_offset_t
950 default_parse_boot_param(struct arm_boot_params *abp)
951 {
952         vm_offset_t lastaddr;
953
954 #if defined(LINUX_BOOT_ABI)
955         if ((lastaddr = linux_parse_boot_param(abp)) != 0)
956                 return lastaddr;
957 #endif
958 #if defined(FREEBSD_BOOT_LOADER)
959         if ((lastaddr = freebsd_parse_boot_param(abp)) != 0)
960                 return lastaddr;
961 #endif
962         /* Fall back to hardcoded metadata. */
963         lastaddr = fake_preload_metadata(abp);
964
965         return lastaddr;
966 }
967
968 /*
969  * Stub version of the boot parameter parsing routine.  We are
970  * called early in initarm, before even VM has been initialized.
971  * This routine needs to preserve any data that the boot loader
972  * has passed in before the kernel starts to grow past the end
973  * of the BSS, traditionally the place boot-loaders put this data.
974  *
975  * Since this is called so early, things that depend on the vm system
976  * being setup (including access to some SoC's serial ports), about
977  * all that can be done in this routine is to copy the arguments.
978  *
979  * This is the default boot parameter parsing routine.  Individual
980  * kernels/boards can override this weak function with one of their
981  * own.  We just fake metadata...
982  */
983 __weak_reference(default_parse_boot_param, parse_boot_param);
984
985 /*
986  * Initialize proc0
987  */
988 void
989 init_proc0(vm_offset_t kstack)
990 {
991         proc_linkup0(&proc0, &thread0);
992         thread0.td_kstack = kstack;
993         thread0.td_pcb = (struct pcb *)
994                 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
995         thread0.td_pcb->pcb_flags = 0;
996         thread0.td_pcb->pcb_vfpcpu = -1;
997         thread0.td_frame = &proc0_tf;
998         pcpup->pc_curpcb = thread0.td_pcb;
999 }
1000
1001 void
1002 set_stackptrs(int cpu)
1003 {
1004
1005         set_stackptr(PSR_IRQ32_MODE,
1006             irqstack.pv_va + ((IRQ_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1007         set_stackptr(PSR_ABT32_MODE,
1008             abtstack.pv_va + ((ABT_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1009         set_stackptr(PSR_UND32_MODE,
1010             undstack.pv_va + ((UND_STACK_SIZE * PAGE_SIZE) * (cpu + 1)));
1011 }
1012
1013 #ifdef FDT
1014 static char *
1015 kenv_next(char *cp)
1016 {
1017
1018         if (cp != NULL) {
1019                 while (*cp != 0)
1020                         cp++;
1021                 cp++;
1022                 if (*cp == 0)
1023                         cp = NULL;
1024         }
1025         return (cp);
1026 }
1027
1028 static void
1029 print_kenv(void)
1030 {
1031         int len;
1032         char *cp;
1033
1034         debugf("loader passed (static) kenv:\n");
1035         if (kern_envp == NULL) {
1036                 debugf(" no env, null ptr\n");
1037                 return;
1038         }
1039         debugf(" kern_envp = 0x%08x\n", (uint32_t)kern_envp);
1040
1041         len = 0;
1042         for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
1043                 debugf(" %x %s\n", (uint32_t)cp, cp);
1044 }
1045
1046 void *
1047 initarm(struct arm_boot_params *abp)
1048 {
1049         struct mem_region mem_regions[FDT_MEM_REGIONS];
1050         struct pv_addr kernel_l1pt;
1051         struct pv_addr dpcpu;
1052         vm_offset_t dtbp, freemempos, l2_start, lastaddr;
1053         uint32_t memsize, l2size;
1054         char *env;
1055         void *kmdp;
1056         u_int l1pagetable;
1057         int i, j, err_devmap, mem_regions_sz;
1058
1059         lastaddr = parse_boot_param(abp);
1060         arm_physmem_kernaddr = abp->abp_physaddr;
1061
1062         memsize = 0;
1063         set_cpufuncs();
1064
1065         /*
1066          * Find the dtb passed in by the boot loader.
1067          */
1068         kmdp = preload_search_by_type("elf kernel");
1069         if (kmdp != NULL)
1070                 dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
1071         else
1072                 dtbp = (vm_offset_t)NULL;
1073
1074 #if defined(FDT_DTB_STATIC)
1075         /*
1076          * In case the device tree blob was not retrieved (from metadata) try
1077          * to use the statically embedded one.
1078          */
1079         if (dtbp == (vm_offset_t)NULL)
1080                 dtbp = (vm_offset_t)&fdt_static_dtb;
1081 #endif
1082
1083         if (OF_install(OFW_FDT, 0) == FALSE)
1084                 panic("Cannot install FDT");
1085
1086         if (OF_init((void *)dtbp) != 0)
1087                 panic("OF_init failed with the found device tree");
1088
1089         /* Grab physical memory regions information from device tree. */
1090         if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, &memsize) != 0)
1091                 panic("Cannot get physical memory regions");
1092         arm_physmem_hardware_regions(mem_regions, mem_regions_sz);
1093
1094         /* Grab reserved memory regions information from device tree. */
1095         if (fdt_get_reserved_regions(mem_regions, &mem_regions_sz) == 0)
1096                 arm_physmem_exclude_regions(mem_regions, mem_regions_sz, 
1097                     EXFLAG_NODUMP | EXFLAG_NOALLOC);
1098
1099         /* Platform-specific initialisation */
1100         initarm_early_init();
1101
1102         pcpu0_init();
1103
1104         /* Do basic tuning, hz etc */
1105         init_param1();
1106
1107         /* Calculate number of L2 tables needed for mapping vm_page_array */
1108         l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
1109         l2size = (l2size >> L1_S_SHIFT) + 1;
1110
1111         /*
1112          * Add one table for end of kernel map, one for stacks, msgbuf and
1113          * L1 and L2 tables map and one for vectors map.
1114          */
1115         l2size += 3;
1116
1117         /* Make it divisible by 4 */
1118         l2size = (l2size + 3) & ~3;
1119
1120         freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
1121
1122         /* Define a macro to simplify memory allocation */
1123 #define valloc_pages(var, np)                                           \
1124         alloc_pages((var).pv_va, (np));                                 \
1125         (var).pv_pa = (var).pv_va + (abp->abp_physaddr - KERNVIRTADDR);
1126
1127 #define alloc_pages(var, np)                                            \
1128         (var) = freemempos;                                             \
1129         freemempos += (np * PAGE_SIZE);                                 \
1130         memset((char *)(var), 0, ((np) * PAGE_SIZE));
1131
1132         while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
1133                 freemempos += PAGE_SIZE;
1134         valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
1135
1136         for (i = 0, j = 0; i < l2size; ++i) {
1137                 if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
1138                         valloc_pages(kernel_pt_table[i],
1139                             L2_TABLE_SIZE / PAGE_SIZE);
1140                         j = i;
1141                 } else {
1142                         kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
1143                             L2_TABLE_SIZE_REAL * (i - j);
1144                         kernel_pt_table[i].pv_pa =
1145                             kernel_pt_table[i].pv_va - KERNVIRTADDR +
1146                             abp->abp_physaddr;
1147
1148                 }
1149         }
1150         /*
1151          * Allocate a page for the system page mapped to 0x00000000
1152          * or 0xffff0000. This page will just contain the system vectors
1153          * and can be shared by all processes.
1154          */
1155         valloc_pages(systempage, 1);
1156
1157         /* Allocate dynamic per-cpu area. */
1158         valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
1159         dpcpu_init((void *)dpcpu.pv_va, 0);
1160
1161         /* Allocate stacks for all modes */
1162         valloc_pages(irqstack, IRQ_STACK_SIZE * MAXCPU);
1163         valloc_pages(abtstack, ABT_STACK_SIZE * MAXCPU);
1164         valloc_pages(undstack, UND_STACK_SIZE * MAXCPU);
1165         valloc_pages(kernelstack, KSTACK_PAGES * MAXCPU);
1166         valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
1167
1168         /*
1169          * Now we start construction of the L1 page table
1170          * We start by mapping the L2 page tables into the L1.
1171          * This means that we can replace L1 mappings later on if necessary
1172          */
1173         l1pagetable = kernel_l1pt.pv_va;
1174
1175         /*
1176          * Try to map as much as possible of kernel text and data using
1177          * 1MB section mapping and for the rest of initial kernel address
1178          * space use L2 coarse tables.
1179          *
1180          * Link L2 tables for mapping remainder of kernel (modulo 1MB)
1181          * and kernel structures
1182          */
1183         l2_start = lastaddr & ~(L1_S_OFFSET);
1184         for (i = 0 ; i < l2size - 1; i++)
1185                 pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
1186                     &kernel_pt_table[i]);
1187
1188         pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
1189
1190         /* Map kernel code and data */
1191         pmap_map_chunk(l1pagetable, KERNVIRTADDR, abp->abp_physaddr,
1192            (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
1193             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1194
1195         /* Map L1 directory and allocated L2 page tables */
1196         pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
1197             L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1198
1199         pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
1200             kernel_pt_table[0].pv_pa,
1201             L2_TABLE_SIZE_REAL * l2size,
1202             VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
1203
1204         /* Map allocated DPCPU, stacks and msgbuf */
1205         pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
1206             freemempos - dpcpu.pv_va,
1207             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
1208
1209         /* Link and map the vector page */
1210         pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
1211             &kernel_pt_table[l2size - 1]);
1212         pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
1213             VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE, PTE_CACHE);
1214
1215         /* Establish static device mappings. */
1216         err_devmap = initarm_devmap_init();
1217         arm_devmap_bootstrap(l1pagetable, NULL);
1218         vm_max_kernel_address = initarm_lastaddr();
1219
1220         cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) | DOMAIN_CLIENT);
1221         pmap_pa = kernel_l1pt.pv_pa;
1222         setttb(kernel_l1pt.pv_pa);
1223         cpu_tlb_flushID();
1224         cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
1225
1226         /*
1227          * Now that proper page tables are installed, call cpu_setup() to enable
1228          * instruction and data caches and other chip-specific features.
1229          */
1230         cpu_setup("");
1231
1232         /*
1233          * Only after the SOC registers block is mapped we can perform device
1234          * tree fixups, as they may attempt to read parameters from hardware.
1235          */
1236         OF_interpret("perform-fixup", 0);
1237
1238         initarm_gpio_init();
1239
1240         cninit();
1241
1242         debugf("initarm: console initialized\n");
1243         debugf(" arg1 kmdp = 0x%08x\n", (uint32_t)kmdp);
1244         debugf(" boothowto = 0x%08x\n", boothowto);
1245         debugf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
1246         print_kenv();
1247
1248         env = getenv("kernelname");
1249         if (env != NULL)
1250                 strlcpy(kernelname, env, sizeof(kernelname));
1251
1252         if (err_devmap != 0)
1253                 printf("WARNING: could not fully configure devmap, error=%d\n",
1254                     err_devmap);
1255
1256         initarm_late_init();
1257
1258         /*
1259          * Pages were allocated during the secondary bootstrap for the
1260          * stacks for different CPU modes.
1261          * We must now set the r13 registers in the different CPU modes to
1262          * point to these stacks.
1263          * Since the ARM stacks use STMFD etc. we must set r13 to the top end
1264          * of the stack memory.
1265          */
1266         cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
1267
1268         set_stackptrs(0);
1269
1270         /*
1271          * We must now clean the cache again....
1272          * Cleaning may be done by reading new data to displace any
1273          * dirty data in the cache. This will have happened in setttb()
1274          * but since we are boot strapping the addresses used for the read
1275          * may have just been remapped and thus the cache could be out
1276          * of sync. A re-clean after the switch will cure this.
1277          * After booting there are no gross relocations of the kernel thus
1278          * this problem will not occur after initarm().
1279          */
1280         cpu_idcache_wbinv_all();
1281
1282         /* Set stack for exception handlers */
1283         data_abort_handler_address = (u_int)data_abort_handler;
1284         prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
1285         undefined_handler_address = (u_int)undefinedinstruction_bounce;
1286         undefined_init();
1287
1288         init_proc0(kernelstack.pv_va);
1289
1290         arm_intrnames_init();
1291         arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
1292         pmap_bootstrap(freemempos, &kernel_l1pt);
1293         msgbufp = (void *)msgbufpv.pv_va;
1294         msgbufinit(msgbufp, msgbufsize);
1295         mutex_init();
1296
1297         /*
1298          * Exclude the kernel (and all the things we allocated which immediately
1299          * follow the kernel) from the VM allocation pool but not from crash
1300          * dumps.  virtual_avail is a global variable which tracks the kva we've
1301          * "allocated" while setting up pmaps.
1302          *
1303          * Prepare the list of physical memory available to the vm subsystem.
1304          */
1305         arm_physmem_exclude_region(abp->abp_physaddr, 
1306             (virtual_avail - KERNVIRTADDR), EXFLAG_NOALLOC);
1307         arm_physmem_init_kernel_globals();
1308
1309         init_param2(physmem);
1310         kdb_init();
1311
1312         return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
1313             sizeof(struct pcb)));
1314 }
1315 #endif