2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (C) 2001 Benno Rice
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
61 #include "opt_kstack_pages.h"
62 #include "opt_platform.h"
64 #include <sys/param.h>
66 #include <sys/systm.h>
72 #include <sys/eventhandler.h>
74 #include <sys/imgact.h>
76 #include <sys/kernel.h>
78 #include <sys/linker.h>
80 #include <sys/malloc.h>
82 #include <sys/msgbuf.h>
83 #include <sys/mutex.h>
84 #include <sys/ptrace.h>
85 #include <sys/reboot.h>
86 #include <sys/rwlock.h>
87 #include <sys/signalvar.h>
88 #include <sys/syscallsubr.h>
89 #include <sys/sysctl.h>
90 #include <sys/sysent.h>
91 #include <sys/sysproto.h>
92 #include <sys/ucontext.h>
94 #include <sys/vmmeter.h>
95 #include <sys/vnode.h>
97 #include <net/netisr.h>
100 #include <vm/vm_extern.h>
101 #include <vm/vm_kern.h>
102 #include <vm/vm_page.h>
103 #include <vm/vm_phys.h>
104 #include <vm/vm_map.h>
105 #include <vm/vm_object.h>
106 #include <vm/vm_pager.h>
108 #include <machine/altivec.h>
109 #ifndef __powerpc64__
110 #include <machine/bat.h>
112 #include <machine/cpu.h>
113 #include <machine/elf.h>
114 #include <machine/fpu.h>
115 #include <machine/hid.h>
116 #include <machine/kdb.h>
117 #include <machine/md_var.h>
118 #include <machine/metadata.h>
119 #include <machine/mmuvar.h>
120 #include <machine/pcb.h>
121 #include <machine/reg.h>
122 #include <machine/sigframe.h>
123 #include <machine/spr.h>
124 #include <machine/trap.h>
125 #include <machine/vmparam.h>
126 #include <machine/ofw_machdep.h>
130 #include <dev/ofw/openfirm.h>
131 #include <dev/ofw/ofw_subr.h>
135 int cacheline_size = 128;
137 int cacheline_size = 32;
139 int hw_direct_map = 1;
142 extern vm_paddr_t kernload;
145 extern void *ap_pcpu;
147 struct pcpu __pcpu[MAXCPU];
148 static char init_kenv[2048];
150 static struct trapframe frame0;
152 char machine[] = "powerpc";
153 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
155 static void cpu_startup(void *);
156 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
158 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
159 CTLFLAG_RD, &cacheline_size, 0, "");
161 uintptr_t powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *,
167 /* Default MSR values set in the AIM/Book-E early startup code */
168 register_t psl_kernset;
169 register_t psl_userset;
170 register_t psl_userstatic;
172 register_t psl_userset32;
175 struct kva_md_info kmi;
178 cpu_startup(void *dummy)
182 * Initialise the decrementer-based clock.
187 * Good {morning,afternoon,evening,night}.
189 cpu_setup(PCPU_GET(cpuid));
194 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)physmem),
195 ptoa((uintmax_t)physmem) / 1048576);
199 printf("available KVA = %zu (%zu MB)\n",
200 virtual_end - virtual_avail,
201 (virtual_end - virtual_avail) / 1048576);
204 * Display any holes after the first chunk of extended memory.
209 printf("Physical memory chunk(s):\n");
210 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
212 phys_avail[indx + 1] - phys_avail[indx];
215 printf("0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
217 printf("0x%09jx - 0x%09jx, %ju bytes (%ju pages)\n",
219 (uintmax_t)phys_avail[indx],
220 (uintmax_t)phys_avail[indx + 1] - 1,
221 (uintmax_t)size1, (uintmax_t)size1 / PAGE_SIZE);
225 vm_ksubmap_init(&kmi);
227 printf("avail memory = %ju (%ju MB)\n",
228 ptoa((uintmax_t)vm_free_count()),
229 ptoa((uintmax_t)vm_free_count()) / 1048576);
232 * Set up buffers, so they can be used to read disk labels.
235 vm_pager_bufferinit();
238 extern vm_offset_t __startkernel, __endkernel;
239 extern unsigned char __bss_start[];
240 extern unsigned char __sbss_start[];
241 extern unsigned char __sbss_end[];
242 extern unsigned char _end[];
244 void aim_early_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry,
245 void *mdp, uint32_t mdp_cookie);
246 void aim_cpu_init(vm_offset_t toc);
247 void booke_cpu_init(void);
250 powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp,
255 vm_offset_t startkernel, endkernel;
257 bool ofw_bootargs = false;
259 vm_offset_t ksym_start;
260 vm_offset_t ksym_end;
263 /* First guess at start/end kernel positions */
264 startkernel = __startkernel;
265 endkernel = __endkernel;
268 * If the metadata pointer cookie is not set to the magic value,
269 * the number in mdp should be treated as nonsense.
271 if (mdp_cookie != 0xfb5d104d)
276 * On BOOKE the BSS is already cleared and some variables
277 * initialized. Do not wipe them out.
279 bzero(__sbss_start, __sbss_end - __sbss_start);
280 bzero(__bss_start, _end - __bss_start);
286 aim_early_init(fdt, toc, ofentry, mdp, mdp_cookie);
290 * Parse metadata if present and fetch parameters. Must be done
291 * before console is inited so cninit gets the right value of
297 uintptr_t md_offset = 0;
298 vm_paddr_t kernelendphys;
301 if ((uintptr_t)&powerpc_init > DMAP_BASE_ADDRESS)
302 md_offset = DMAP_BASE_ADDRESS;
304 md_offset = VM_MIN_KERNEL_ADDRESS - kernload;
307 preload_metadata = mdp;
309 preload_metadata += md_offset;
310 preload_bootstrap_relocate(md_offset);
312 kmdp = preload_search_by_type("elf kernel");
314 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
315 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
318 init_static_kenv(envp, 0);
320 fdt = MD_FETCH(kmdp, MODINFOMD_DTBP, uintptr_t);
324 kernelendphys = MD_FETCH(kmdp, MODINFOMD_KERNEND,
326 if (kernelendphys != 0)
327 kernelendphys += md_offset;
328 endkernel = ulmax(endkernel, kernelendphys);
330 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
331 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
332 db_fetch_ksymtab(ksym_start, ksym_end);
336 init_static_kenv(init_kenv, sizeof(init_kenv));
339 /* Store boot environment state */
340 OF_initial_setup((void *)fdt, NULL, (int (*)(void *))ofentry);
343 * Init params/tunables that can be overridden by the loader
348 * Start initializing proc0 and thread0.
350 proc_linkup0(&proc0, &thread0);
351 thread0.td_frame = &frame0;
353 __asm __volatile("mr 13,%0" :: "r"(&thread0));
355 __asm __volatile("mr 2,%0" :: "r"(&thread0));
359 * Init mutexes, which we use heavily in PMAP
364 * Install the OF client interface
369 ofw_parse_bootargs();
372 * Initialize the console before printing anything.
381 /* Make sure the kernel icache is valid before we go too much further */
382 __syncicache((caddr_t)startkernel, endkernel - startkernel);
386 * Choose a platform module so we can get the physical memory map.
389 platform_probe_and_attach();
392 * Set up per-cpu data for the BSP now that the platform can tell
395 if (platform_smp_get_bsp(&bsp) != 0)
397 pc = &__pcpu[bsp.cr_cpuid];
398 __asm __volatile("mtsprg 0, %0" :: "r"(pc));
399 pcpu_init(pc, bsp.cr_cpuid, sizeof(struct pcpu));
400 pc->pc_curthread = &thread0;
401 thread0.td_oncpu = bsp.cr_cpuid;
402 pc->pc_cpuid = bsp.cr_cpuid;
403 pc->pc_hwref = bsp.cr_hwref;
413 pmap_bootstrap(startkernel, endkernel);
414 mtmsr(psl_kernset & ~PSL_EE);
417 * Initialize params/tunables that are derived from memsize
419 init_param2(physmem);
422 * Grab booted kernel's name
424 env = kern_getenv("kernelname");
426 strlcpy(kernelname, env, sizeof(kernelname));
431 * Finish setting up thread0.
433 thread0.td_pcb = (struct pcb *)
434 ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
435 sizeof(struct pcb)) & ~15UL);
436 bzero((void *)thread0.td_pcb, sizeof(struct pcb));
437 pc->pc_curpcb = thread0.td_pcb;
439 /* Initialise the message buffer. */
440 msgbufinit(msgbufp, msgbufsize);
443 if (boothowto & RB_KDB)
444 kdb_enter(KDB_WHY_BOOTFLAGS,
445 "Boot flags requested debugger");
448 return (((uintptr_t)thread0.td_pcb -
449 (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
453 * Flush the D-cache for non-DMA I/O so that the I-cache can
454 * be made coherent later.
457 cpu_flush_dcache(void *ptr, size_t len)
459 register_t addr, off;
462 * Align the address to a cacheline and adjust the length
463 * accordingly. Then round the length to a multiple of the
464 * cacheline for easy looping.
466 addr = (uintptr_t)ptr;
467 off = addr & (cacheline_size - 1);
469 len = roundup2(len + off, cacheline_size);
472 __asm __volatile ("dcbf 0,%0" :: "r"(addr));
473 __asm __volatile ("sync");
474 addr += cacheline_size;
475 len -= cacheline_size;
480 ptrace_set_pc(struct thread *td, unsigned long addr)
482 struct trapframe *tf;
485 tf->srr0 = (register_t)addr;
497 if (td->td_md.md_spinlock_count == 0) {
499 msr = intr_disable();
500 td->td_md.md_spinlock_count = 1;
501 td->td_md.md_saved_msr = msr;
503 td->td_md.md_spinlock_count++;
515 msr = td->td_md.md_saved_msr;
516 td->td_md.md_spinlock_count--;
517 if (td->td_md.md_spinlock_count == 0) {
524 * Simple ddb(4) command/hack to view any SPR on the running CPU.
525 * Uses a trivial asm function to perform the mfspr, and rewrites the mfspr
526 * instruction each time.
527 * XXX: Since it uses code modification, it won't work if the kernel code pages
530 extern register_t get_spr(int);
533 DB_SHOW_COMMAND(spr, db_show_spr)
536 volatile uint32_t *p;
537 int sprno, saved_sprno;
542 saved_sprno = sprno = (intptr_t) addr;
543 sprno = ((sprno & 0x3e0) >> 5) | ((sprno & 0x1f) << 5);
544 p = (uint32_t *)(void *)&get_spr;
546 #if defined(_CALL_ELF) && _CALL_ELF == 2
547 /* Account for ELFv2 function prologue. */
550 p = *(volatile uint32_t * volatile *)p;
553 *p = (*p & ~0x001ff800) | (sprno << 11);
554 __syncicache(__DEVOLATILE(uint32_t *, p), cacheline_size);
555 spr = get_spr(sprno);
557 db_printf("SPR %d(%x): %lx\n", saved_sprno, saved_sprno,
564 bzero(void *buf, size_t len)
570 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
575 while (len >= sizeof(u_long) * 8) {
577 *((u_long*) p + 1) = 0;
578 *((u_long*) p + 2) = 0;
579 *((u_long*) p + 3) = 0;
580 len -= sizeof(u_long) * 8;
581 *((u_long*) p + 4) = 0;
582 *((u_long*) p + 5) = 0;
583 *((u_long*) p + 6) = 0;
584 *((u_long*) p + 7) = 0;
585 p += sizeof(u_long) * 8;
588 while (len >= sizeof(u_long)) {
590 len -= sizeof(u_long);
600 /* __stack_chk_fail_local() is called in secure-plt (32-bit). */
601 #if !defined(__powerpc64__)
602 extern void __stack_chk_fail(void);
603 void __stack_chk_fail_local(void);
606 __stack_chk_fail_local(void)