xref: /freebsd/sys/powerpc/powerpc/machdep.c (revision 2726a7014867ad7224d09b66836c5d385f0350f4)
1 /*-
2  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3  * Copyright (C) 1995, 1996 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
19  *
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.
30  */
31 /*-
32  * Copyright (C) 2001 Benno Rice
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
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.
43  *
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 $
55  */
56 
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
59 
60 #include "opt_ddb.h"
61 #include "opt_kstack_pages.h"
62 #include "opt_platform.h"
63 
64 #include <sys/param.h>
65 #include <sys/proc.h>
66 #include <sys/systm.h>
67 #include <sys/bio.h>
68 #include <sys/buf.h>
69 #include <sys/bus.h>
70 #include <sys/cons.h>
71 #include <sys/cpu.h>
72 #include <sys/eventhandler.h>
73 #include <sys/exec.h>
74 #include <sys/imgact.h>
75 #include <sys/kdb.h>
76 #include <sys/kernel.h>
77 #include <sys/ktr.h>
78 #include <sys/linker.h>
79 #include <sys/lock.h>
80 #include <sys/malloc.h>
81 #include <sys/mbuf.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>
93 #include <sys/uio.h>
94 #include <sys/vmmeter.h>
95 #include <sys/vnode.h>
96 
97 #include <net/netisr.h>
98 
99 #include <vm/vm.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>
107 
108 #include <machine/altivec.h>
109 #ifndef __powerpc64__
110 #include <machine/bat.h>
111 #endif
112 #include <machine/cpu.h>
113 #include <machine/elf.h>
114 #include <machine/fpu.h>
115 #include <machine/hid.h>
116 #include <machine/ifunc.h>
117 #include <machine/kdb.h>
118 #include <machine/md_var.h>
119 #include <machine/metadata.h>
120 #include <machine/mmuvar.h>
121 #include <machine/pcb.h>
122 #include <machine/reg.h>
123 #include <machine/sigframe.h>
124 #include <machine/spr.h>
125 #include <machine/trap.h>
126 #include <machine/vmparam.h>
127 #include <machine/ofw_machdep.h>
128 
129 #include <ddb/ddb.h>
130 
131 #include <dev/ofw/openfirm.h>
132 #include <dev/ofw/ofw_subr.h>
133 
134 int cold = 1;
135 #ifdef __powerpc64__
136 int cacheline_size = 128;
137 #else
138 int cacheline_size = 32;
139 #endif
140 int hw_direct_map = 1;
141 
142 #ifdef BOOKE
143 extern vm_paddr_t kernload;
144 #endif
145 
146 extern void *ap_pcpu;
147 
148 struct pcpu __pcpu[MAXCPU] __aligned(PAGE_SIZE);
149 static char init_kenv[2048];
150 
151 static struct trapframe frame0;
152 
153 char		machine[] = "powerpc";
154 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
155 
156 static void	cpu_startup(void *);
157 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
158 
159 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
160 	   CTLFLAG_RD, &cacheline_size, 0, "");
161 
162 uintptr_t	powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *,
163 		    uint32_t);
164 
165 static void	fake_preload_metadata(void);
166 
167 long		Maxmem = 0;
168 long		realmem = 0;
169 
170 /* Default MSR values set in the AIM/Book-E early startup code */
171 register_t	psl_kernset;
172 register_t	psl_userset;
173 register_t	psl_userstatic;
174 #ifdef __powerpc64__
175 register_t	psl_userset32;
176 #endif
177 
178 struct kva_md_info kmi;
179 
180 static void
181 cpu_startup(void *dummy)
182 {
183 
184 	/*
185 	 * Initialise the decrementer-based clock.
186 	 */
187 	decr_init();
188 
189 	/*
190 	 * Good {morning,afternoon,evening,night}.
191 	 */
192 	cpu_setup(PCPU_GET(cpuid));
193 
194 #ifdef PERFMON
195 	perfmon_init();
196 #endif
197 	printf("real memory  = %ju (%ju MB)\n", ptoa((uintmax_t)physmem),
198 	    ptoa((uintmax_t)physmem) / 1048576);
199 	realmem = physmem;
200 
201 	if (bootverbose)
202 		printf("available KVA = %zu (%zu MB)\n",
203 		    virtual_end - virtual_avail,
204 		    (virtual_end - virtual_avail) / 1048576);
205 
206 	/*
207 	 * Display any holes after the first chunk of extended memory.
208 	 */
209 	if (bootverbose) {
210 		int indx;
211 
212 		printf("Physical memory chunk(s):\n");
213 		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
214 			vm_paddr_t size1 =
215 			    phys_avail[indx + 1] - phys_avail[indx];
216 
217 			#ifdef __powerpc64__
218 			printf("0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
219 			#else
220 			printf("0x%09jx - 0x%09jx, %ju bytes (%ju pages)\n",
221 			#endif
222 			    (uintmax_t)phys_avail[indx],
223 			    (uintmax_t)phys_avail[indx + 1] - 1,
224 			    (uintmax_t)size1, (uintmax_t)size1 / PAGE_SIZE);
225 		}
226 	}
227 
228 	vm_ksubmap_init(&kmi);
229 
230 	printf("avail memory = %ju (%ju MB)\n",
231 	    ptoa((uintmax_t)vm_free_count()),
232 	    ptoa((uintmax_t)vm_free_count()) / 1048576);
233 
234 	/*
235 	 * Set up buffers, so they can be used to read disk labels.
236 	 */
237 	bufinit();
238 	vm_pager_bufferinit();
239 }
240 
241 extern vm_offset_t	__startkernel, __endkernel;
242 extern unsigned char	__bss_start[];
243 extern unsigned char	__sbss_start[];
244 extern unsigned char	__sbss_end[];
245 extern unsigned char	_end[];
246 
247 void aim_early_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry,
248     void *mdp, uint32_t mdp_cookie);
249 void aim_cpu_init(vm_offset_t toc);
250 void booke_cpu_init(void);
251 
252 #ifdef DDB
253 static void	load_external_symtab(void);
254 static void	displace_symbol_table(vm_offset_t, vm_offset_t, vm_offset_t);
255 #endif
256 
257 uintptr_t
258 powerpc_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry, void *mdp,
259     uint32_t mdp_cookie)
260 {
261 	struct		pcpu *pc;
262 	struct cpuref	bsp;
263 	vm_offset_t	startkernel, endkernel;
264 	char		*env;
265 	void		*kmdp = NULL;
266         bool		ofw_bootargs = false;
267 	bool		symbols_provided = false;
268 #ifdef DDB
269 	vm_offset_t ksym_start;
270 	vm_offset_t ksym_end;
271 	vm_offset_t ksym_sz;
272 #endif
273 
274 	/* First guess at start/end kernel positions */
275 	startkernel = __startkernel;
276 	endkernel = __endkernel;
277 
278 	/*
279 	 * If the metadata pointer cookie is not set to the magic value,
280 	 * the number in mdp should be treated as nonsense.
281 	 */
282 	if (mdp_cookie != 0xfb5d104d)
283 		mdp = NULL;
284 
285 #if !defined(BOOKE)
286 	/*
287 	 * On BOOKE the BSS is already cleared and some variables
288 	 * initialized.  Do not wipe them out.
289 	 */
290 	bzero(__sbss_start, __sbss_end - __sbss_start);
291 	bzero(__bss_start, _end - __bss_start);
292 #endif
293 
294 	cpu_feature_setup();
295 
296 #ifdef AIM
297 	aim_early_init(fdt, toc, ofentry, mdp, mdp_cookie);
298 #endif
299 
300 	/*
301 	 * At this point, we are executing in our correct memory space.
302 	 * Book-E started there, and AIM has done an rfi and restarted
303 	 * execution from _start.
304 	 *
305 	 * We may still be in real mode, however. If we are running out of
306 	 * the direct map on 64 bit, this is possible to do.
307 	 */
308 
309 	/*
310 	 * Parse metadata if present and fetch parameters.  Must be done
311 	 * before console is inited so cninit gets the right value of
312 	 * boothowto.
313 	 */
314 	if (mdp != NULL) {
315 		/*
316 		 * Starting up from loader.
317 		 *
318 		 * Full metadata has been provided, but we need to figure
319 		 * out the correct address to relocate it to.
320 		 */
321 		char *envp = NULL;
322 		uintptr_t md_offset = 0;
323 		vm_paddr_t kernelstartphys, kernelendphys;
324 
325 #ifdef AIM
326 		if ((uintptr_t)&powerpc_init > DMAP_BASE_ADDRESS)
327 			md_offset = DMAP_BASE_ADDRESS;
328 #else /* BOOKE */
329 		md_offset = VM_MIN_KERNEL_ADDRESS - kernload;
330 #endif
331 
332 		preload_metadata = mdp;
333 		if (md_offset > 0) {
334 			/* Translate phys offset into DMAP offset. */
335 			preload_metadata += md_offset;
336 			preload_bootstrap_relocate(md_offset);
337 		}
338 		kmdp = preload_search_by_type("elf kernel");
339 		if (kmdp != NULL) {
340 			boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
341 			envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
342 			if (envp != NULL)
343 				envp += md_offset;
344 			init_static_kenv(envp, 0);
345 			if (fdt == 0) {
346 				fdt = MD_FETCH(kmdp, MODINFOMD_DTBP, uintptr_t);
347 				if (fdt != 0)
348 					fdt += md_offset;
349 			}
350 			kernelstartphys = MD_FETCH(kmdp, MODINFO_ADDR,
351 			    vm_offset_t);
352 			/* kernelstartphys is already relocated. */
353 			kernelendphys = MD_FETCH(kmdp, MODINFOMD_KERNEND,
354 			    vm_offset_t);
355 			if (kernelendphys != 0)
356 				kernelendphys += md_offset;
357 			endkernel = ulmax(endkernel, kernelendphys);
358 #ifdef DDB
359 			ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
360 			ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
361 			ksym_sz = *(Elf_Size*)ksym_start;
362 
363 			/*
364 			 * Loader already handled displacing to the load
365 			 * address, but we still need to displace it to the
366 			 * DMAP.
367 			 */
368 			displace_symbol_table(
369 			    (vm_offset_t)(ksym_start + sizeof(Elf_Size)),
370 			    ksym_sz, md_offset);
371 
372 			db_fetch_ksymtab(ksym_start, ksym_end);
373 			symbols_provided = true;
374 #endif
375 		}
376 	} else {
377 		/*
378 		 * Self-loading kernel, we have to fake up metadata.
379 		 *
380 		 * Since we are creating the metadata from the final
381 		 * memory space, we don't need to call
382 		 * preload_boostrap_relocate().
383 		 */
384 		fake_preload_metadata();
385 		kmdp = preload_search_by_type("elf kernel");
386 		init_static_kenv(init_kenv, sizeof(init_kenv));
387 		ofw_bootargs = true;
388 	}
389 
390 	/* Store boot environment state */
391 	OF_initial_setup((void *)fdt, NULL, (int (*)(void *))ofentry);
392 
393 	/*
394 	 * Init params/tunables that can be overridden by the loader
395 	 */
396 	init_param1();
397 
398 	/*
399 	 * Start initializing proc0 and thread0.
400 	 */
401 	proc_linkup0(&proc0, &thread0);
402 	thread0.td_frame = &frame0;
403 #ifdef __powerpc64__
404 	__asm __volatile("mr 13,%0" :: "r"(&thread0));
405 #else
406 	__asm __volatile("mr 2,%0" :: "r"(&thread0));
407 #endif
408 
409 	/*
410 	 * Init mutexes, which we use heavily in PMAP
411 	 */
412 	mutex_init();
413 
414 	/*
415 	 * Install the OF client interface
416 	 */
417 	OF_bootstrap();
418 
419 #ifdef DDB
420 	if (!symbols_provided && hw_direct_map)
421 		load_external_symtab();
422 #endif
423 
424 	if (ofw_bootargs)
425 		ofw_parse_bootargs();
426 
427 	/*
428 	 * Initialize the console before printing anything.
429 	 */
430 	cninit();
431 
432 #ifdef AIM
433 	aim_cpu_init(toc);
434 #else /* BOOKE */
435 	booke_cpu_init();
436 
437 	/* Make sure the kernel icache is valid before we go too much further */
438 	__syncicache((caddr_t)startkernel, endkernel - startkernel);
439 #endif
440 
441 	/*
442 	 * Choose a platform module so we can get the physical memory map.
443 	 */
444 
445 	platform_probe_and_attach();
446 
447 	/*
448 	 * Set up per-cpu data for the BSP now that the platform can tell
449 	 * us which that is.
450 	 */
451 	if (platform_smp_get_bsp(&bsp) != 0)
452 		bsp.cr_cpuid = 0;
453 	pc = &__pcpu[bsp.cr_cpuid];
454 	__asm __volatile("mtsprg 0, %0" :: "r"(pc));
455 	pcpu_init(pc, bsp.cr_cpuid, sizeof(struct pcpu));
456 	pc->pc_curthread = &thread0;
457 	thread0.td_oncpu = bsp.cr_cpuid;
458 	pc->pc_cpuid = bsp.cr_cpuid;
459 	pc->pc_hwref = bsp.cr_hwref;
460 
461 	/*
462 	 * Init KDB
463 	 */
464 	kdb_init();
465 
466 	/*
467 	 * Bring up MMU
468 	 */
469 	pmap_mmu_init();
470 	link_elf_ireloc(kmdp);
471 	pmap_bootstrap(startkernel, endkernel);
472 	mtmsr(psl_kernset & ~PSL_EE);
473 
474 	/*
475 	 * Initialize params/tunables that are derived from memsize
476 	 */
477 	init_param2(physmem);
478 
479 	/*
480 	 * Grab booted kernel's name
481 	 */
482         env = kern_getenv("kernelname");
483         if (env != NULL) {
484 		strlcpy(kernelname, env, sizeof(kernelname));
485 		freeenv(env);
486 	}
487 
488 	/*
489 	 * Finish setting up thread0.
490 	 */
491 	thread0.td_pcb = (struct pcb *)
492 	    ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
493 	    sizeof(struct pcb)) & ~15UL);
494 	bzero((void *)thread0.td_pcb, sizeof(struct pcb));
495 	pc->pc_curpcb = thread0.td_pcb;
496 
497 	/* Initialise the message buffer. */
498 	msgbufinit(msgbufp, msgbufsize);
499 
500 #ifdef KDB
501 	if (boothowto & RB_KDB)
502 		kdb_enter(KDB_WHY_BOOTFLAGS,
503 		    "Boot flags requested debugger");
504 #endif
505 
506 	return (((uintptr_t)thread0.td_pcb -
507 	    (sizeof(struct callframe) - 3*sizeof(register_t))) & ~15UL);
508 }
509 
510 #ifdef DDB
511 /*
512  * XXX Figure out where to move this.
513  */
514 static void
515 displace_symbol_table(vm_offset_t ksym_start,
516     vm_offset_t ksym_sz, vm_offset_t displacement) {
517 	Elf_Sym *sym;
518 
519 	/*
520 	 * Relocate the symbol table to our final load address.
521 	 */
522 	for (sym = (Elf_Sym *)ksym_start;
523 	    (vm_paddr_t)sym < (ksym_start + ksym_sz);
524 	    sym++) {
525 		if (sym->st_name == 0 ||
526 		    sym->st_shndx == SHN_UNDEF ||
527 		    sym->st_value == 0)
528 			continue;
529 		if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
530 		    ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
531 		    ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
532 			continue;
533 		/* Skip relocating any implausible symbols */
534 		if (sym->st_value > KERNBASE)
535 			sym->st_value += displacement;
536 	}
537 }
538 
539 /*
540  * On powernv, we might not have symbols loaded via loader. However, if the
541  * user passed the kernel in as the initrd as well, we can manually load it
542  * via reinterpreting the initrd copy of the kernel.
543  */
544 static void
545 load_external_symtab(void) {
546 	phandle_t chosen;
547 	vm_paddr_t start, end;
548 	pcell_t cell[2];
549 	ssize_t size;
550 	u_char *kernelimg;
551 
552 	int i;
553 
554 	Elf_Ehdr *ehdr;
555 	Elf_Phdr *phdr;
556 	Elf_Shdr *shdr;
557 
558         vm_offset_t ksym_start, ksym_sz, kstr_start, kstr_sz;
559 
560 	if (!hw_direct_map)
561 		return;
562 
563 	chosen = OF_finddevice("/chosen");
564 	if (chosen <= 0)
565 		return;
566 
567 	if (!OF_hasprop(chosen, "linux,initrd-start") ||
568 	    !OF_hasprop(chosen, "linux,initrd-end"))
569 		return;
570 
571 	size = OF_getencprop(chosen, "linux,initrd-start", cell, sizeof(cell));
572 	if (size == 4)
573 		start = cell[0];
574 	else if (size == 8)
575 		start = (uint64_t)cell[0] << 32 | cell[1];
576 	else
577 		return;
578 
579 	size = OF_getencprop(chosen, "linux,initrd-end", cell, sizeof(cell));
580 	if (size == 4)
581 		end = cell[0];
582 	else if (size == 8)
583 		end = (uint64_t)cell[0] << 32 | cell[1];
584 	else
585 		return;
586 
587 	if (!(end - start > 0))
588 		return;
589 
590 	kernelimg = (u_char *) PHYS_TO_DMAP(start);
591 
592 	ehdr = (Elf_Ehdr *)kernelimg;
593 
594 	if (!IS_ELF(*ehdr))
595 		return;
596 
597 	phdr = (Elf_Phdr *)(kernelimg + ehdr->e_phoff);
598 	shdr = (Elf_Shdr *)(kernelimg + ehdr->e_shoff);
599 
600 	ksym_start = 0;
601 	ksym_sz = 0;
602 	kstr_start = 0;
603 	kstr_sz = 0;
604 	for (i = 0; i < ehdr->e_shnum; i++) {
605 		if (shdr[i].sh_type == SHT_SYMTAB) {
606 			ksym_start = (vm_offset_t)(kernelimg +
607 			    shdr[i].sh_offset);
608 			ksym_sz = (vm_offset_t)(shdr[i].sh_size);
609 			kstr_start = (vm_offset_t)(kernelimg +
610 			    shdr[shdr[i].sh_link].sh_offset);
611 			kstr_sz = (vm_offset_t)
612 			    (shdr[shdr[i].sh_link].sh_size);
613 		}
614 	}
615 
616 	if (ksym_start != 0 && kstr_start != 0 && ksym_sz != 0 &&
617 	    kstr_sz != 0 && ksym_start < kstr_start) {
618 
619 		displace_symbol_table(ksym_start, ksym_sz,
620 		    (__startkernel - KERNBASE));
621 		ksymtab = ksym_start;
622 		ksymtab_size = ksym_sz;
623 		kstrtab = kstr_start;
624 	}
625 
626 };
627 #endif
628 
629 /*
630  * When not being loaded from loader, we need to create our own metadata
631  * so we can interact with the kernel linker.
632  */
633 static void
634 fake_preload_metadata(void) {
635 	/* We depend on dword alignment here. */
636 	static uint32_t fake_preload[36] __aligned(8);
637 	int i = 0;
638 
639 	fake_preload[i++] = MODINFO_NAME;
640 	fake_preload[i++] = strlen("kernel") + 1;
641 	strcpy((char*)&fake_preload[i], "kernel");
642 	/* ['k' 'e' 'r' 'n'] ['e' 'l' '\0' ..] */
643 	i += 2;
644 
645 	fake_preload[i++] = MODINFO_TYPE;
646 	fake_preload[i++] = strlen("elf kernel") + 1;
647 	strcpy((char*)&fake_preload[i], "elf kernel");
648 	/* ['e' 'l' 'f' ' '] ['k' 'e' 'r' 'n'] ['e' 'l' '\0' ..] */
649 	i += 3;
650 
651 #ifdef __powerpc64__
652 	/* Padding -- Fields start on u_long boundaries */
653 	fake_preload[i++] = 0;
654 #endif
655 
656 	fake_preload[i++] = MODINFO_ADDR;
657 	fake_preload[i++] = sizeof(vm_offset_t);
658 	*(vm_offset_t *)&fake_preload[i] =
659 	    (vm_offset_t)(__startkernel);
660 	i += (sizeof(vm_offset_t) / 4);
661 
662 	fake_preload[i++] = MODINFO_SIZE;
663 	fake_preload[i++] = sizeof(vm_offset_t);
664 	*(vm_offset_t *)&fake_preload[i] =
665 	    (vm_offset_t)(__endkernel) - (vm_offset_t)(__startkernel);
666 	i += (sizeof(vm_offset_t) / 4);
667 
668 	/*
669 	 * MODINFOMD_SSYM and MODINFOMD_ESYM cannot be provided here,
670 	 * as the memory comes from outside the loaded ELF sections.
671 	 *
672 	 * If the symbols are being provided by other means (MFS), the
673 	 * tables will be loaded into the debugger directly.
674 	 */
675 
676 	/* Null field at end to mark end of data. */
677 	fake_preload[i++] = 0;
678 	fake_preload[i] = 0;
679 	preload_metadata = (void*)fake_preload;
680 }
681 
682 /*
683  * Flush the D-cache for non-DMA I/O so that the I-cache can
684  * be made coherent later.
685  */
686 void
687 cpu_flush_dcache(void *ptr, size_t len)
688 {
689 	register_t addr, off;
690 
691 	/*
692 	 * Align the address to a cacheline and adjust the length
693 	 * accordingly. Then round the length to a multiple of the
694 	 * cacheline for easy looping.
695 	 */
696 	addr = (uintptr_t)ptr;
697 	off = addr & (cacheline_size - 1);
698 	addr -= off;
699 	len = roundup2(len + off, cacheline_size);
700 
701 	while (len > 0) {
702 		__asm __volatile ("dcbf 0,%0" :: "r"(addr));
703 		__asm __volatile ("sync");
704 		addr += cacheline_size;
705 		len -= cacheline_size;
706 	}
707 }
708 
709 int
710 ptrace_set_pc(struct thread *td, unsigned long addr)
711 {
712 	struct trapframe *tf;
713 
714 	tf = td->td_frame;
715 	tf->srr0 = (register_t)addr;
716 
717 	return (0);
718 }
719 
720 void
721 spinlock_enter(void)
722 {
723 	struct thread *td;
724 	register_t msr;
725 
726 	td = curthread;
727 	if (td->td_md.md_spinlock_count == 0) {
728 		nop_prio_mhigh();
729 		msr = intr_disable();
730 		td->td_md.md_spinlock_count = 1;
731 		td->td_md.md_saved_msr = msr;
732 		critical_enter();
733 	} else
734 		td->td_md.md_spinlock_count++;
735 }
736 
737 void
738 spinlock_exit(void)
739 {
740 	struct thread *td;
741 	register_t msr;
742 
743 	td = curthread;
744 	msr = td->td_md.md_saved_msr;
745 	td->td_md.md_spinlock_count--;
746 	if (td->td_md.md_spinlock_count == 0) {
747 		critical_exit();
748 		intr_restore(msr);
749 		nop_prio_medium();
750 	}
751 }
752 
753 /*
754  * Simple ddb(4) command/hack to view any SPR on the running CPU.
755  * Uses a trivial asm function to perform the mfspr, and rewrites the mfspr
756  * instruction each time.
757  * XXX: Since it uses code modification, it won't work if the kernel code pages
758  * are marked RO.
759  */
760 extern register_t get_spr(int);
761 
762 #ifdef DDB
763 DB_SHOW_COMMAND(spr, db_show_spr)
764 {
765 	register_t spr;
766 	volatile uint32_t *p;
767 	int sprno, saved_sprno;
768 
769 	if (!have_addr)
770 		return;
771 
772 	saved_sprno = sprno = (intptr_t) addr;
773 	sprno = ((sprno & 0x3e0) >> 5) | ((sprno & 0x1f) << 5);
774 	p = (uint32_t *)(void *)&get_spr;
775 #ifdef __powerpc64__
776 #if defined(_CALL_ELF) && _CALL_ELF == 2
777 	/* Account for ELFv2 function prologue. */
778 	p += 2;
779 #else
780 	p = *(volatile uint32_t * volatile *)p;
781 #endif
782 #endif
783 	*p = (*p & ~0x001ff800) | (sprno << 11);
784 	__syncicache(__DEVOLATILE(uint32_t *, p), cacheline_size);
785 	spr = get_spr(sprno);
786 
787 	db_printf("SPR %d(%x): %lx\n", saved_sprno, saved_sprno,
788 	    (unsigned long)spr);
789 }
790 
791 DB_SHOW_COMMAND(frame, db_show_frame)
792 {
793 	struct trapframe *tf;
794 	long reg;
795 	int i;
796 
797 	tf = have_addr ? (struct trapframe *)addr : curthread->td_frame;
798 
799 	/*
800 	 * Everything casts through long to simplify the printing.
801 	 * 'long' is native register size anyway.
802 	 */
803 	db_printf("trap frame %p\n", tf);
804 	for (i = 0; i < nitems(tf->fixreg); i++) {
805 		reg = tf->fixreg[i];
806 		db_printf("  r%d:\t%#lx (%ld)\n", i, reg, reg);
807 	}
808 	reg = tf->lr;
809 	db_printf("  lr:\t%#lx\n", reg);
810 	reg = tf->cr;
811 	db_printf("  cr:\t%#lx\n", reg);
812 	reg = tf->xer;
813 	db_printf("  xer:\t%#lx\n", reg);
814 	reg = tf->ctr;
815 	db_printf("  ctr:\t%#lx (%ld)\n", reg, reg);
816 	reg = tf->srr0;
817 	db_printf("  srr0:\t%#lx\n", reg);
818 	reg = tf->srr1;
819 	db_printf("  srr1:\t%#lx\n", reg);
820 	reg = tf->exc;
821 	db_printf("  exc:\t%#lx\n", reg);
822 	reg = tf->dar;
823 	db_printf("  dar:\t%#lx\n", reg);
824 #ifdef AIM
825 	reg = tf->cpu.aim.dsisr;
826 	db_printf("  dsisr:\t%#lx\n", reg);
827 #else
828 	reg = tf->cpu.booke.esr;
829 	db_printf("  esr:\t%#lx\n", reg);
830 	reg = tf->cpu.booke.dbcr0;
831 	db_printf("  dbcr0:\t%#lx\n", reg);
832 #endif
833 }
834 #endif
835 
836 #undef bzero
837 void
838 bzero(void *buf, size_t len)
839 {
840 	caddr_t	p;
841 
842 	p = buf;
843 
844 	while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
845 		*p++ = 0;
846 		len--;
847 	}
848 
849 	while (len >= sizeof(u_long) * 8) {
850 		*(u_long*) p = 0;
851 		*((u_long*) p + 1) = 0;
852 		*((u_long*) p + 2) = 0;
853 		*((u_long*) p + 3) = 0;
854 		len -= sizeof(u_long) * 8;
855 		*((u_long*) p + 4) = 0;
856 		*((u_long*) p + 5) = 0;
857 		*((u_long*) p + 6) = 0;
858 		*((u_long*) p + 7) = 0;
859 		p += sizeof(u_long) * 8;
860 	}
861 
862 	while (len >= sizeof(u_long)) {
863 		*(u_long*) p = 0;
864 		len -= sizeof(u_long);
865 		p += sizeof(u_long);
866 	}
867 
868 	while (len) {
869 		*p++ = 0;
870 		len--;
871 	}
872 }
873 
874 /* __stack_chk_fail_local() is called in secure-plt (32-bit). */
875 #if !defined(__powerpc64__)
876 extern void __stack_chk_fail(void);
877 void __stack_chk_fail_local(void);
878 
879 void
880 __stack_chk_fail_local(void)
881 {
882 
883 	__stack_chk_fail();
884 }
885 #endif
886