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