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