xref: /freebsd/sys/powerpc/powerpc/machdep.c (revision 8b9023cfc1851a18518111d286ebd59b573008c1)
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/sched.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/sigframe.h>
123 #include <machine/spr.h>
124 #include <machine/trap.h>
125 #include <machine/vmparam.h>
126 #include <machine/ofw_machdep.h>
127 
128 #include <ddb/ddb.h>
129 
130 #include <dev/ofw/openfirm.h>
131 #include <dev/ofw/ofw_subr.h>
132 
133 int cold = 1;
134 #ifdef __powerpc64__
135 int cacheline_size = 128;
136 #else
137 int cacheline_size = 32;
138 #endif
139 #ifdef __powerpc64__
140 int hw_direct_map = -1;
141 #else
142 int hw_direct_map = 1;
143 #endif
144 
145 #ifdef BOOKE
146 extern vm_paddr_t kernload;
147 #endif
148 
149 extern void *ap_pcpu;
150 
151 struct pcpu __pcpu[MAXCPU] __aligned(PAGE_SIZE);
152 static char init_kenv[2048];
153 
154 static struct trapframe frame0;
155 
156 const char	machine[] = "powerpc";
157 SYSCTL_CONST_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD | CTLFLAG_CAPRD,
158     machine, "Machine class");
159 
160 static void	cpu_startup(void *);
161 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
162 
163 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
164 	   CTLFLAG_RD, &cacheline_size, 0, "");
165 
166 uintptr_t	powerpc_init(vm_offset_t, vm_offset_t, vm_offset_t, void *,
167 		    uint32_t);
168 
169 static void	fake_preload_metadata(void);
170 
171 long		Maxmem = 0;
172 long		realmem = 0;
173 
174 /* Default MSR values set in the AIM/Book-E early startup code */
175 register_t	psl_kernset;
176 register_t	psl_userset;
177 register_t	psl_userstatic;
178 #ifdef __powerpc64__
179 register_t	psl_userset32;
180 #endif
181 
182 struct kva_md_info kmi;
183 
184 static void
cpu_startup(void * dummy)185 cpu_startup(void *dummy)
186 {
187 
188 	/*
189 	 * Initialise the decrementer-based clock.
190 	 */
191 	decr_init();
192 
193 	/*
194 	 * Good {morning,afternoon,evening,night}.
195 	 */
196 	cpu_setup(PCPU_GET(cpuid));
197 
198 	printf("real memory  = %ju (%ju MB)\n", ptoa((uintmax_t)physmem),
199 	    ptoa((uintmax_t)physmem) / 1048576);
200 	realmem = physmem;
201 
202 	if (bootverbose)
203 		printf("available KVA = %zu (%zu MB)\n",
204 		    virtual_end - virtual_avail,
205 		    (virtual_end - virtual_avail) / 1048576);
206 
207 	/*
208 	 * Display any holes after the first chunk of extended memory.
209 	 */
210 	if (bootverbose) {
211 		int indx;
212 
213 		printf("Physical memory chunk(s):\n");
214 		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
215 			vm_paddr_t size1 =
216 			    phys_avail[indx + 1] - phys_avail[indx];
217 
218 			#ifdef __powerpc64__
219 			printf("0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
220 			#else
221 			printf("0x%09jx - 0x%09jx, %ju bytes (%ju pages)\n",
222 			#endif
223 			    (uintmax_t)phys_avail[indx],
224 			    (uintmax_t)phys_avail[indx + 1] - 1,
225 			    (uintmax_t)size1, (uintmax_t)size1 / PAGE_SIZE);
226 		}
227 	}
228 
229 	vm_ksubmap_init(&kmi);
230 
231 	printf("avail memory = %ju (%ju MB)\n",
232 	    ptoa((uintmax_t)vm_free_count()),
233 	    ptoa((uintmax_t)vm_free_count()) / 1048576);
234 
235 	/*
236 	 * Set up buffers, so they can be used to read disk labels.
237 	 */
238 	bufinit();
239 	vm_pager_bufferinit();
240 }
241 
242 extern vm_offset_t	__startkernel, __endkernel;
243 extern unsigned char	__bss_start[];
244 extern unsigned char	__sbss_start[];
245 extern unsigned char	__sbss_end[];
246 extern unsigned char	_end[];
247 
248 void aim_early_init(vm_offset_t fdt, vm_offset_t toc, vm_offset_t ofentry,
249     void *mdp, uint32_t mdp_cookie);
250 void aim_cpu_init(vm_offset_t toc);
251 void booke_cpu_init(void);
252 
253 #ifdef DDB
254 static void	load_external_symtab(void);
255 #endif
256 
257 uintptr_t
powerpc_init(vm_offset_t fdt,vm_offset_t toc,vm_offset_t ofentry,void * mdp,uint32_t mdp_cookie)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         bool		ofw_bootargs = false;
266 #ifdef DDB
267 	bool		symbols_provided = false;
268 	vm_offset_t ksym_start;
269 	vm_offset_t ksym_end;
270 #endif
271 
272 	/* First guess at start/end kernel positions */
273 	startkernel = __startkernel;
274 	endkernel = __endkernel;
275 
276 	/*
277 	 * If the metadata pointer cookie is not set to the magic value,
278 	 * the number in mdp should be treated as nonsense.
279 	 */
280 	if (mdp_cookie != 0xfb5d104d)
281 		mdp = NULL;
282 
283 #if !defined(BOOKE)
284 	/*
285 	 * On BOOKE the BSS is already cleared and some variables
286 	 * initialized.  Do not wipe them out.
287 	 */
288 	bzero(__sbss_start, __sbss_end - __sbss_start);
289 	bzero(__bss_start, _end - __bss_start);
290 #endif
291 
292 	cpu_feature_setup();
293 
294 #ifdef AIM
295 	aim_early_init(fdt, toc, ofentry, mdp, mdp_cookie);
296 #endif
297 
298 	/*
299 	 * At this point, we are executing in our correct memory space.
300 	 * Book-E started there, and AIM has done an rfi and restarted
301 	 * execution from _start.
302 	 *
303 	 * We may still be in real mode, however. If we are running out of
304 	 * the direct map on 64 bit, this is possible to do.
305 	 */
306 
307 	/*
308 	 * Parse metadata if present and fetch parameters.  Must be done
309 	 * before console is inited so cninit gets the right value of
310 	 * boothowto.
311 	 */
312 	if (mdp != NULL) {
313 		/*
314 		 * Starting up from loader.
315 		 *
316 		 * Full metadata has been provided, but we need to figure
317 		 * out the correct address to relocate it to.
318 		 */
319 		char *envp = NULL;
320 		uintptr_t md_offset = 0;
321 		vm_paddr_t kernelendphys;
322 
323 #ifdef AIM
324 		if ((uintptr_t)&powerpc_init > DMAP_BASE_ADDRESS)
325 			md_offset = DMAP_BASE_ADDRESS;
326 #else /* BOOKE */
327 		md_offset = VM_MIN_KERNEL_ADDRESS - kernload;
328 #endif
329 
330 		preload_metadata = mdp;
331 		if (md_offset > 0) {
332 			/* Translate phys offset into DMAP offset. */
333 			preload_metadata += md_offset;
334 			preload_bootstrap_relocate(md_offset);
335 		}
336 
337 		/* Initialize preload_kmdp */
338 		preload_initkmdp(true);
339 
340 		boothowto = MD_FETCH(preload_kmdp, MODINFOMD_HOWTO, int);
341 		envp = MD_FETCH(preload_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(preload_kmdp, MODINFOMD_DTBP, uintptr_t);
347 			if (fdt != 0)
348 				fdt += md_offset;
349 		}
350 		/* kernelstartphys is already relocated. */
351 		kernelendphys = MD_FETCH(preload_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(preload_kmdp, MODINFOMD_SSYM, uintptr_t);
358 		ksym_end = MD_FETCH(preload_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 	} else {
365 		/*
366 		 * Self-loading kernel, we have to fake up metadata.
367 		 *
368 		 * Since we are creating the metadata from the final
369 		 * memory space, we don't need to call
370 		 * preload_boostrap_relocate().
371 		 */
372 		fake_preload_metadata();
373 		/* Initialize preload_kmdp */
374 		preload_initkmdp(true);
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 	sched_instance_select();
469 	link_elf_ireloc();
470 	pmap_bootstrap(startkernel, endkernel);
471 	mtmsr(psl_kernset & ~PSL_EE);
472 
473 	/*
474 	 * Initialize params/tunables that are derived from memsize
475 	 */
476 	init_param2(physmem);
477 
478 	/*
479 	 * Grab booted kernel's name
480 	 */
481         env = kern_getenv("kernelname");
482         if (env != NULL) {
483 		strlcpy(kernelname, env, sizeof(kernelname));
484 		freeenv(env);
485 	}
486 
487 	/*
488 	 * Finish setting up thread0.
489 	 */
490 	thread0.td_pcb = (struct pcb *)__align_down(td_kstack_top(&thread0) -
491 	    sizeof(struct pcb), 16);
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
load_external_symtab(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 = 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
fake_preload_metadata(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(preload_kerntype) + 1;
651 	strcpy((char *)&fake_preload[i], preload_kerntype);
652 	i += howmany(fake_preload[i - 1], sizeof(uint32_t));
653 
654 #ifdef __powerpc64__
655 	/* Padding -- Fields start on u_long boundaries */
656 	fake_preload[i++] = 0;
657 #endif
658 
659 	fake_preload[i++] = MODINFO_ADDR;
660 	fake_preload[i++] = sizeof(vm_offset_t);
661 	*(vm_offset_t *)&fake_preload[i] =
662 	    (vm_offset_t)(__startkernel);
663 	i += (sizeof(vm_offset_t) / 4);
664 
665 	fake_preload[i++] = MODINFO_SIZE;
666 	fake_preload[i++] = sizeof(vm_offset_t);
667 	*(vm_offset_t *)&fake_preload[i] =
668 	    (vm_offset_t)(__endkernel) - (vm_offset_t)(__startkernel);
669 	i += (sizeof(vm_offset_t) / 4);
670 
671 	/*
672 	 * MODINFOMD_SSYM and MODINFOMD_ESYM cannot be provided here,
673 	 * as the memory comes from outside the loaded ELF sections.
674 	 *
675 	 * If the symbols are being provided by other means (MFS), the
676 	 * tables will be loaded into the debugger directly.
677 	 */
678 
679 	/* Null field at end to mark end of data. */
680 	fake_preload[i++] = 0;
681 	fake_preload[i] = 0;
682 	preload_metadata = (void*)fake_preload;
683 }
684 
685 /*
686  * Flush the D-cache for non-DMA I/O so that the I-cache can
687  * be made coherent later.
688  */
689 void
cpu_flush_dcache(void * ptr,size_t len)690 cpu_flush_dcache(void *ptr, size_t len)
691 {
692 	register_t addr, off;
693 
694 	/*
695 	 * Align the address to a cacheline and adjust the length
696 	 * accordingly. Then round the length to a multiple of the
697 	 * cacheline for easy looping.
698 	 */
699 	addr = (uintptr_t)ptr;
700 	off = addr & (cacheline_size - 1);
701 	addr -= off;
702 	len = roundup2(len + off, cacheline_size);
703 
704 	while (len > 0) {
705 		__asm __volatile ("dcbf 0,%0" :: "r"(addr));
706 		__asm __volatile ("sync");
707 		addr += cacheline_size;
708 		len -= cacheline_size;
709 	}
710 }
711 
712 int
ptrace_set_pc(struct thread * td,unsigned long addr)713 ptrace_set_pc(struct thread *td, unsigned long addr)
714 {
715 	struct trapframe *tf;
716 
717 	tf = td->td_frame;
718 	tf->srr0 = (register_t)addr;
719 
720 	return (0);
721 }
722 
723 void
spinlock_enter(void)724 spinlock_enter(void)
725 {
726 	struct thread *td;
727 	register_t msr;
728 
729 	td = curthread;
730 	if (td->td_md.md_spinlock_count == 0) {
731 		nop_prio_mhigh();
732 		msr = intr_disable();
733 		td->td_md.md_spinlock_count = 1;
734 		td->td_md.md_saved_msr = msr;
735 		critical_enter();
736 	} else
737 		td->td_md.md_spinlock_count++;
738 }
739 
740 void
spinlock_exit(void)741 spinlock_exit(void)
742 {
743 	struct thread *td;
744 	register_t msr;
745 
746 	td = curthread;
747 	msr = td->td_md.md_saved_msr;
748 	td->td_md.md_spinlock_count--;
749 	if (td->td_md.md_spinlock_count == 0) {
750 		critical_exit();
751 		intr_restore(msr);
752 		nop_prio_medium();
753 	}
754 }
755 
756 /*
757  * Simple ddb(4) command/hack to view any SPR on the running CPU.
758  * Uses a trivial asm function to perform the mfspr, and rewrites the mfspr
759  * instruction each time.
760  * XXX: Since it uses code modification, it won't work if the kernel code pages
761  * are marked RO.
762  */
763 extern register_t get_spr(int);
764 
765 #ifdef DDB
DB_SHOW_COMMAND(spr,db_show_spr)766 DB_SHOW_COMMAND(spr, db_show_spr)
767 {
768 	register_t spr;
769 	volatile uint32_t *p;
770 	int sprno, saved_sprno;
771 
772 	if (!have_addr)
773 		return;
774 
775 	saved_sprno = sprno = (intptr_t) addr;
776 	sprno = ((sprno & 0x3e0) >> 5) | ((sprno & 0x1f) << 5);
777 	p = (uint32_t *)(void *)&get_spr;
778 #ifdef __powerpc64__
779 #if defined(_CALL_ELF) && _CALL_ELF == 2
780 	/* Account for ELFv2 function prologue. */
781 	p += 2;
782 #else
783 	p = *(volatile uint32_t * volatile *)p;
784 #endif
785 #endif
786 	*p = (*p & ~0x001ff800) | (sprno << 11);
787 	__syncicache(__DEVOLATILE(uint32_t *, p), cacheline_size);
788 	spr = get_spr(sprno);
789 
790 	db_printf("SPR %d(%x): %lx\n", saved_sprno, saved_sprno,
791 	    (unsigned long)spr);
792 }
793 
DB_SHOW_COMMAND(frame,db_show_frame)794 DB_SHOW_COMMAND(frame, db_show_frame)
795 {
796 	struct trapframe *tf;
797 	long reg;
798 	int i;
799 
800 	tf = have_addr ? (struct trapframe *)addr : curthread->td_frame;
801 
802 	/*
803 	 * Everything casts through long to simplify the printing.
804 	 * 'long' is native register size anyway.
805 	 */
806 	db_printf("trap frame %p\n", tf);
807 	for (i = 0; i < nitems(tf->fixreg); i++) {
808 		reg = tf->fixreg[i];
809 		db_printf("  r%d:\t%#lx (%ld)\n", i, reg, reg);
810 	}
811 	reg = tf->lr;
812 	db_printf("  lr:\t%#lx\n", reg);
813 	reg = tf->cr;
814 	db_printf("  cr:\t%#lx\n", reg);
815 	reg = tf->xer;
816 	db_printf("  xer:\t%#lx\n", reg);
817 	reg = tf->ctr;
818 	db_printf("  ctr:\t%#lx (%ld)\n", reg, reg);
819 	reg = tf->srr0;
820 	db_printf("  srr0:\t%#lx\n", reg);
821 	reg = tf->srr1;
822 	db_printf("  srr1:\t%#lx\n", reg);
823 	reg = tf->exc;
824 	db_printf("  exc:\t%#lx\n", reg);
825 	reg = tf->dar;
826 	db_printf("  dar:\t%#lx\n", reg);
827 #ifdef AIM
828 	reg = tf->cpu.aim.dsisr;
829 	db_printf("  dsisr:\t%#lx\n", reg);
830 #else
831 	reg = tf->cpu.booke.esr;
832 	db_printf("  esr:\t%#lx\n", reg);
833 	reg = tf->cpu.booke.dbcr0;
834 	db_printf("  dbcr0:\t%#lx\n", reg);
835 #endif
836 }
837 #endif
838 
839 /* __stack_chk_fail_local() is called in secure-plt (32-bit). */
840 #if !defined(__powerpc64__)
841 extern void __stack_chk_fail(void);
842 void __stack_chk_fail_local(void);
843 
844 void
__stack_chk_fail_local(void)845 __stack_chk_fail_local(void)
846 {
847 
848 	__stack_chk_fail();
849 }
850 #endif
851