machdep.c revision 177253
1/*-
2 * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
19 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27/*-
28 * Copyright (C) 2001 Benno Rice
29 * All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 *    notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 *    notice, this list of conditions and the following disclaimer in the
38 *    documentation and/or other materials provided with the distribution.
39 *
40 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
41 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
43 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
46 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
49 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
51 */
52/*-
53 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
54 * Copyright (C) 1995, 1996 TooLs GmbH.
55 * All rights reserved.
56 *
57 * Redistribution and use in source and binary forms, with or without
58 * modification, are permitted provided that the following conditions
59 * are met:
60 * 1. Redistributions of source code must retain the above copyright
61 *    notice, this list of conditions and the following disclaimer.
62 * 2. Redistributions in binary form must reproduce the above copyright
63 *    notice, this list of conditions and the following disclaimer in the
64 *    documentation and/or other materials provided with the distribution.
65 * 3. All advertising materials mentioning features or use of this software
66 *    must display the following acknowledgement:
67 *      This product includes software developed by TooLs GmbH.
68 * 4. The name of TooLs GmbH may not be used to endorse or promote products
69 *    derived from this software without specific prior written permission.
70 *
71 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
72 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
74 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
75 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
76 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
77 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
79 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
80 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81 */
82
83#include <sys/cdefs.h>
84__FBSDID("$FreeBSD: head/sys/powerpc/booke/machdep.c 177253 2008-03-16 10:58:09Z rwatson $");
85
86#include "opt_compat.h"
87#include "opt_kstack_pages.h"
88
89#include <sys/cdefs.h>
90#include <sys/types.h>
91
92#include <sys/param.h>
93#include <sys/proc.h>
94#include <sys/systm.h>
95#include <sys/time.h>
96#include <sys/bio.h>
97#include <sys/buf.h>
98#include <sys/bus.h>
99#include <sys/cons.h>
100#include <sys/cpu.h>
101#include <sys/kdb.h>
102#include <sys/kernel.h>
103#include <sys/lock.h>
104#include <sys/mutex.h>
105#include <sys/sysctl.h>
106#include <sys/exec.h>
107#include <sys/ktr.h>
108#include <sys/sysproto.h>
109#include <sys/signalvar.h>
110#include <sys/sysent.h>
111#include <sys/imgact.h>
112#include <sys/msgbuf.h>
113#include <sys/ptrace.h>
114
115#include <vm/vm.h>
116#include <vm/pmap.h>
117#include <vm/vm_page.h>
118#include <vm/vm_object.h>
119#include <vm/vm_pager.h>
120
121#include <machine/cpu.h>
122#include <machine/kdb.h>
123#include <machine/reg.h>
124#include <machine/vmparam.h>
125#include <machine/spr.h>
126#include <machine/hid.h>
127#include <machine/psl.h>
128#include <machine/trap.h>
129#include <machine/md_var.h>
130#include <machine/mmuvar.h>
131#include <machine/pmap.h>
132#include <machine/sigframe.h>
133#include <machine/metadata.h>
134#include <machine/bootinfo.h>
135#include <machine/powerpc.h>
136
137#include <sys/linker.h>
138#include <sys/reboot.h>
139
140#ifdef  DEBUG
141#define debugf(fmt, args...) printf(fmt, ##args)
142#else
143#define debugf(fmt, args...)
144#endif
145
146extern unsigned char kernel_text[];
147extern unsigned char _etext[];
148extern unsigned char _edata[];
149extern unsigned char __bss_start[];
150extern unsigned char __sbss_start[];
151extern unsigned char __sbss_end[];
152extern unsigned char _end[];
153
154extern struct mem_region availmem_regions[];
155extern int availmem_regions_sz;
156
157extern void *trapcode, *trapsize;
158
159extern unsigned char kstack0_space[];
160
161extern void dcache_enable(void);
162extern void dcache_inval(void);
163extern void icache_enable(void);
164extern void icache_inval(void);
165
166struct kva_md_info kmi;
167struct pcpu __pcpu[MAXCPU];
168struct trapframe frame0;
169int cold = 1;
170long realmem = 0;
171long Maxmem = 0;
172
173struct bootinfo *bootinfo;
174
175char machine[] = "powerpc";
176SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
177
178static int cacheline_size = CACHELINESIZE;
179SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
180    CTLFLAG_RD, &cacheline_size, 0, "");
181
182static void cpu_e500_startup(void *);
183SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_e500_startup, NULL);
184
185void print_kernel_section_addr(void);
186void dump_bootinfo(void);
187void dump_kenv(void);
188void e500_init(u_int32_t, u_int32_t, void *);
189void setPQL2(int *const size, int *const ways);
190
191void
192setPQL2(int *const size, int *const ways)
193{
194
195	return;
196}
197
198static void
199cpu_e500_startup(void *dummy)
200{
201
202	/* Initialise the decrementer-based clock. */
203	decr_init();
204
205	/* Good {morning,afternoon,evening,night}. */
206	cpu_setup(PCPU_GET(cpuid));
207
208	printf("real memory  = %ld (%ld MB)\n", ptoa(physmem),
209			ptoa(physmem) / 1048576);
210	realmem = physmem;
211
212	/* Display any holes after the first chunk of extended memory. */
213	if (bootverbose) {
214		int indx;
215
216		printf("Physical memory chunk(s):\n");
217		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
218			int size1 = phys_avail[indx + 1] - phys_avail[indx];
219
220			printf("0x%08x - 0x%08x, %d bytes (%d pages)\n",
221					phys_avail[indx], phys_avail[indx + 1] - 1, size1,
222					size1 / PAGE_SIZE);
223		}
224	}
225
226	vm_ksubmap_init(&kmi);
227
228	printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
229			ptoa(cnt.v_free_count) / 1048576);
230
231	/* Set up buffers, so they can be used to read disk labels. */
232	bufinit();
233	vm_pager_bufferinit();
234}
235
236static char *
237kenv_next(char *cp)
238{
239
240	if (cp != NULL) {
241		while (*cp != 0)
242			cp++;
243		cp++;
244		if (*cp == 0)
245			cp = NULL;
246	}
247	return (cp);
248}
249
250void
251dump_kenv(void)
252{
253	int len;
254	char *cp;
255
256	debugf("loader passed (static) kenv:\n");
257	if (kern_envp == NULL) {
258		debugf(" no env, null ptr\n");
259		return;
260	}
261	debugf(" kern_envp = 0x%08x\n", (u_int32_t)kern_envp);
262
263	len = 0;
264	for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
265		debugf(" %x %s\n", (u_int32_t)cp, cp);
266}
267
268void
269dump_bootinfo(void)
270{
271	struct bi_mem_region *mr;
272	struct bi_eth_addr *eth;
273	int i, j;
274
275	debugf("bootinfo:\n");
276	if (bootinfo == NULL) {
277		debugf(" no bootinfo, null ptr\n");
278		return;
279	}
280
281	debugf(" version = 0x%08x\n", bootinfo->bi_version);
282	debugf(" ccsrbar = 0x%08x\n", bootinfo->bi_bar_base);
283	debugf(" cpu_clk = 0x%08x\n", bootinfo->bi_cpu_clk);
284	debugf(" bus_clk = 0x%08x\n", bootinfo->bi_bus_clk);
285
286	debugf(" mem regions:\n");
287	mr = (struct bi_mem_region *)bootinfo->bi_data;
288	for (i = 0; i < bootinfo->bi_mem_reg_no; i++, mr++)
289		debugf("    #%d, base = 0x%08x, size = 0x%08x\n", i,
290		    mr->mem_base, mr->mem_size);
291
292	debugf(" eth addresses:\n");
293	eth = (struct bi_eth_addr *)mr;
294	for (i = 0; i < bootinfo->bi_eth_addr_no; i++, eth++) {
295		debugf("    #%d, addr = ", i);
296		for (j = 0; j < 6; j++)
297			debugf("%02x ", eth->mac_addr[j]);
298		debugf("\n");
299	}
300}
301
302void
303print_kernel_section_addr(void)
304{
305
306	debugf("kernel image addresses:\n");
307	debugf(" kernel_text    = 0x%08x\n", (u_int32_t)kernel_text);
308	debugf(" _etext (sdata) = 0x%08x\n", (u_int32_t)_etext);
309	debugf(" _edata         = 0x%08x\n", (u_int32_t)_edata);
310	debugf(" __sbss_start   = 0x%08x\n", (u_int32_t)__sbss_start);
311	debugf(" __sbss_end     = 0x%08x\n", (u_int32_t)__sbss_end);
312	debugf(" __sbss_start   = 0x%08x\n", (u_int32_t)__bss_start);
313	debugf(" _end           = 0x%08x\n", (u_int32_t)_end);
314}
315
316struct bi_mem_region *
317bootinfo_mr(void)
318{
319
320	return((struct bi_mem_region *)bootinfo->bi_data);
321}
322
323struct bi_eth_addr *
324bootinfo_eth(void)
325{
326	struct bi_mem_region *mr;
327	struct bi_eth_addr *eth;
328	int i;
329
330	/* Advance to the eth section */
331	mr = bootinfo_mr();
332	for (i = 0; i < bootinfo->bi_mem_reg_no; i++, mr++)
333		;
334
335	eth = (struct bi_eth_addr *)mr;
336	return (eth);
337}
338
339void
340e500_init(u_int32_t startkernel, u_int32_t endkernel, void *mdp)
341{
342	struct pcpu *pc;
343	void *kmdp;
344	vm_offset_t end;
345	struct bi_mem_region *mr;
346	uint32_t csr;
347	int i;
348
349	kmdp = NULL;
350
351	end = endkernel;
352
353	/*
354	 * Parse metadata and fetch parameters. This must be done as the first
355	 * step as we need bootinfo data to at least init the console
356	 */
357	if (mdp != NULL) {
358		preload_metadata = mdp;
359		kmdp = preload_search_by_type("elf kernel");
360		if (kmdp != NULL) {
361			bootinfo = (struct bootinfo *)preload_search_info(kmdp,
362			    MODINFO_METADATA|MODINFOMD_BOOTINFO);
363
364			boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
365			kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
366			end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
367		}
368	} else {
369		/*
370		 * We should scream but how? - without CCSR bar (in bootinfo)
371		 * cannot even output anything...
372		 */
373
374		 /*
375		  * FIXME add return value and handle in the locore so we can
376		  * return to the loader maybe? (this seems not very easy to
377		  * restore everything as the TLB have all been reprogrammed
378		  * in the locore etc...)
379		  */
380		while(1);
381	}
382
383	/* Initialize memory regions table */
384	mr = bootinfo_mr();
385	for (i = 0; i < bootinfo->bi_mem_reg_no; i++, mr++) {
386		if (i == MEM_REGIONS)
387			break;
388		availmem_regions[i].mr_start = mr->mem_base;
389		availmem_regions[i].mr_size = mr->mem_size;
390	}
391	availmem_regions_sz = i;
392
393	/* Initialize TLB1 handling */
394	tlb1_init(bootinfo->bi_bar_base);
395
396	/*
397	 * Time Base and Decrementer are updated every 8 CCB bus clocks.
398	 * HID0[SEL_TBCLK] = 0
399	 */
400	decr_config(bootinfo->bi_bus_clk/8);
401
402	/* Init params/tunables that can be overridden by the loader. */
403	init_param1();
404
405	/* Start initializing proc0 and thread0. */
406	proc_linkup(&proc0, &thread0);
407	thread0.td_frame = &frame0;
408
409	/* Set up per-cpu data and store the pointer in SPR general 0. */
410	pc = &__pcpu[0];
411	pcpu_init(pc, 0, sizeof(struct pcpu));
412	pc->pc_curthread = &thread0;
413	pc->pc_cpuid = 0;
414	__asm __volatile("mtsprg 0, %0" :: "r"(pc));
415
416	/* Initialize system mutexes. */
417	mutex_init();
418
419	/* Initialize the console before printing anything. */
420	cninit();
421
422	/* Print out some debug info... */
423	debugf("e500_init: console initialized\n");
424	debugf(" arg1 startkernel = 0x%08x\n", startkernel);
425	debugf(" arg2 endkernel = 0x%08x\n", endkernel);
426	debugf(" arg3 midp = 0x%08x\n", (u_int32_t)mdp);
427	debugf(" end = 0x%08x\n", (u_int32_t)end);
428	debugf(" boothowto = 0x%08x\n", boothowto);
429	debugf(" kernel ccsrbar = 0x%08x\n", CCSRBAR_VA);
430	debugf(" MSR = 0x%08x\n", mfmsr());
431	dump_bootinfo();
432	print_kernel_section_addr();
433	dump_kenv();
434	//tlb1_print_entries();
435	//tlb1_print_tlbentries();
436
437	kdb_init();
438
439#ifdef KDB
440	if (boothowto & RB_KDB)
441		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
442#endif
443	kobj_machdep_init();
444
445	/* Initialise virtual memory. */
446	pmap_mmu_install(MMU_TYPE_BOOKE, 0);
447	pmap_bootstrap(startkernel, end);
448	debugf("MSR = 0x%08x\n", mfmsr());
449	//tlb1_print_entries();
450	//tlb1_print_tlbentries();
451
452	/* Initialize params/tunables that are derived from memsize. */
453	init_param2(physmem);
454
455	/* Finish setting up thread0. */
456	thread0.td_kstack = (vm_offset_t)kstack0_space;
457	thread0.td_pcb = (struct pcb *)
458	    (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
459	bzero((void *)thread0.td_pcb, sizeof(struct pcb));
460	pc->pc_curpcb = thread0.td_pcb;
461
462	/* Initialise the message buffer. */
463	msgbufinit(msgbufp, MSGBUF_SIZE);
464
465	/* Enable Machine Check interrupt. */
466	mtmsr(mfmsr() | PSL_ME);
467	isync();
468
469	/* Enable D-cache if applicable */
470	csr = mfspr(SPR_L1CSR0);
471	if ((csr & L1CSR0_DCE) == 0) {
472		dcache_inval();
473		dcache_enable();
474	}
475	csr = mfspr(SPR_L1CSR0);
476	if ((boothowto & RB_VERBOSE) != 0 || (csr & L1CSR0_DCE) == 0)
477		printf("L1 D-cache %sabled\n",
478		    (csr & L1CSR0_DCE) ? "en" : "dis");
479
480	/* Enable L1 I-cache if applicable. */
481	csr = mfspr(SPR_L1CSR1);
482	if ((csr & L1CSR1_ICE) == 0) {
483		icache_inval();
484		icache_enable();
485	}
486	csr = mfspr(SPR_L1CSR1);
487	if ((boothowto & RB_VERBOSE) != 0 || (csr & L1CSR1_ICE) == 0)
488		printf("L1 I-cache %sabled\n",
489		    (csr & L1CSR1_ICE) ? "en" : "dis");
490
491	debugf("e500_init: e\n");
492}
493
494/* Initialise a struct pcpu. */
495void
496cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
497{
498
499}
500
501/* Set set up registers on exec. */
502void
503exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
504{
505	struct trapframe *tf;
506	struct ps_strings arginfo;
507
508	tf = trapframe(td);
509	bzero(tf, sizeof *tf);
510	tf->fixreg[1] = -roundup(-stack + 8, 16);
511
512	/*
513	 * XXX Machine-independent code has already copied arguments and
514	 * XXX environment to userland.  Get them back here.
515	 */
516	(void)copyin((char *)PS_STRINGS, &arginfo, sizeof(arginfo));
517
518	/*
519	 * Set up arguments for _start():
520	 *	_start(argc, argv, envp, obj, cleanup, ps_strings);
521	 *
522	 * Notes:
523	 *	- obj and cleanup are the auxilliary and termination
524	 *	  vectors.  They are fixed up by ld.elf_so.
525	 *	- ps_strings is a NetBSD extention, and will be
526	 * 	  ignored by executables which are strictly
527	 *	  compliant with the SVR4 ABI.
528	 *
529	 * XXX We have to set both regs and retval here due to different
530	 * XXX calling convention in trap.c and init_main.c.
531	 */
532	/*
533	 * XXX PG: these get overwritten in the syscall return code.
534	 * execve() should return EJUSTRETURN, like it does on NetBSD.
535	 * Emulate by setting the syscall return value cells. The
536	 * registers still have to be set for init's fork trampoline.
537	 */
538	td->td_retval[0] = arginfo.ps_nargvstr;
539	td->td_retval[1] = (register_t)arginfo.ps_argvstr;
540	tf->fixreg[3] = arginfo.ps_nargvstr;
541	tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
542	tf->fixreg[5] = (register_t)arginfo.ps_envstr;
543	tf->fixreg[6] = 0;			/* auxillary vector */
544	tf->fixreg[7] = 0;			/* termination vector */
545	tf->fixreg[8] = (register_t)PS_STRINGS;	/* NetBSD extension */
546
547	tf->srr0 = entry;
548	tf->srr1 = PSL_USERSET;
549	td->td_pcb->pcb_flags = 0;
550}
551
552int
553fill_regs(struct thread *td, struct reg *regs)
554{
555	struct trapframe *tf;
556
557	tf = td->td_frame;
558	memcpy(regs, tf, sizeof(struct reg));
559
560	return (0);
561}
562
563int
564fill_fpregs(struct thread *td, struct fpreg *fpregs)
565{
566
567	return (0);
568}
569
570/* Get current clock frequency for the given cpu id. */
571int
572cpu_est_clockrate(int cpu_id, uint64_t *rate)
573{
574
575	return (ENXIO);
576}
577
578/*
579 * Construct a PCB from a trapframe. This is called from kdb_trap() where
580 * we want to start a backtrace from the function that caused us to enter
581 * the debugger. We have the context in the trapframe, but base the trace
582 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
583 * enough for a backtrace.
584 */
585void
586makectx(struct trapframe *tf, struct pcb *pcb)
587{
588
589	pcb->pcb_lr = tf->srr0;
590	pcb->pcb_sp = tf->fixreg[1];
591}
592
593/*
594 * get_mcontext/sendsig helper routine that doesn't touch the
595 * proc lock.
596 */
597static int
598grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
599{
600	struct pcb *pcb;
601
602	pcb = td->td_pcb;
603	memset(mcp, 0, sizeof(mcontext_t));
604
605	mcp->mc_vers = _MC_VERSION;
606	mcp->mc_flags = 0;
607	memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
608	if (flags & GET_MC_CLEAR_RET) {
609		mcp->mc_gpr[3] = 0;
610		mcp->mc_gpr[4] = 0;
611	}
612
613	/* XXX Altivec context ? */
614
615	mcp->mc_len = sizeof(*mcp);
616	return (0);
617}
618
619int
620get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
621{
622	int error;
623
624	error = grab_mcontext(td, mcp, flags);
625	if (error == 0) {
626		PROC_LOCK(curthread->td_proc);
627		mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
628		PROC_UNLOCK(curthread->td_proc);
629	}
630
631	return (error);
632}
633
634int
635set_mcontext(struct thread *td, const mcontext_t *mcp)
636{
637	struct pcb *pcb;
638	struct trapframe *tf;
639
640	pcb = td->td_pcb;
641	tf = td->td_frame;
642
643	if (mcp->mc_vers != _MC_VERSION || mcp->mc_len != sizeof(*mcp))
644		return (EINVAL);
645
646	memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
647
648	/* XXX Altivec context? */
649
650	return (0);
651}
652
653int
654sigreturn(struct thread *td, struct sigreturn_args *uap)
655{
656	struct proc *p;
657	ucontext_t uc;
658	int error;
659
660	CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
661
662	if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
663		CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
664		return (EFAULT);
665	}
666
667	error = set_mcontext(td, &uc.uc_mcontext);
668	if (error != 0)
669		return (error);
670
671	p = td->td_proc;
672	PROC_LOCK(p);
673	td->td_sigmask = uc.uc_sigmask;
674	SIG_CANTMASK(td->td_sigmask);
675	signotify(td);
676	PROC_UNLOCK(p);
677
678	CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
679	    td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
680
681	return (EJUSTRETURN);
682}
683
684#ifdef COMPAT_FREEBSD4
685int
686freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
687{
688
689	return sigreturn(td, (struct sigreturn_args *)uap);
690}
691#endif
692
693/*
694 * cpu_idle
695 *
696 * Set Wait state enable.
697 */
698void
699cpu_idle (void)
700{
701	register_t msr;
702
703	msr = mfmsr();
704#ifdef INVARIANTS
705	if ((msr & PSL_EE) != PSL_EE) {
706		struct thread *td = curthread;
707		printf("td msr %x\n", td->td_md.md_saved_msr);
708		panic("ints disabled in idleproc!");
709	}
710#endif
711#if 0
712	/*
713	 * Freescale E500 core RM section 6.4.1
714	 */
715	msr = msr | PSL_WE;
716
717	__asm__("	msync;"
718		"	mtmsr	%0;"
719		"	isync;"
720		"loop:	b	loop" :
721		/* no output */	:
722		"r" (msr));
723#endif
724}
725
726void
727spinlock_enter(void)
728{
729	struct thread *td;
730
731	td = curthread;
732	if (td->td_md.md_spinlock_count == 0)
733		td->td_md.md_saved_msr = intr_disable();
734	td->td_md.md_spinlock_count++;
735	critical_enter();
736}
737
738void
739spinlock_exit(void)
740{
741	struct thread *td;
742
743	td = curthread;
744	critical_exit();
745	td->td_md.md_spinlock_count--;
746	if (td->td_md.md_spinlock_count == 0)
747		intr_restore(td->td_md.md_saved_msr);
748}
749
750/* Shutdown the CPU as much as possible. */
751void
752cpu_halt(void)
753{
754
755	mtmsr(mfmsr() & ~(PSL_CE | PSL_EE | PSL_ME | PSL_DE));
756	while (1);
757}
758
759int
760set_regs(struct thread *td, struct reg *regs)
761{
762	struct trapframe *tf;
763
764	tf = td->td_frame;
765	memcpy(tf, regs, sizeof(struct reg));
766	return (0);
767}
768
769int
770fill_dbregs(struct thread *td, struct dbreg *dbregs)
771{
772
773	/* No debug registers on PowerPC */
774	return (ENOSYS);
775}
776
777int
778set_dbregs(struct thread *td, struct dbreg *dbregs)
779{
780
781	/* No debug registers on PowerPC */
782	return (ENOSYS);
783}
784
785int
786set_fpregs(struct thread *td, struct fpreg *fpregs)
787{
788
789	return (0);
790}
791
792int
793ptrace_set_pc(struct thread *td, unsigned long addr)
794{
795	struct trapframe *tf;
796
797	tf = td->td_frame;
798	tf->srr0 = (register_t)addr;
799
800	return (0);
801}
802
803int
804ptrace_single_step(struct thread *td)
805{
806	struct trapframe *tf;
807	u_int reg;
808
809	reg = mfspr(SPR_DBCR0);
810	reg |= DBCR0_IC | DBCR0_IDM;
811	mtspr(SPR_DBCR0, reg);
812
813	tf = td->td_frame;
814	tf->srr1 |= PSL_DE;
815	return (0);
816}
817
818int
819ptrace_clear_single_step(struct thread *td)
820{
821	struct trapframe *tf;
822
823	tf = td->td_frame;
824	tf->srr1 &= ~PSL_DE;
825	return (0);
826}
827
828void
829kdb_cpu_clear_singlestep(void)
830{
831	register_t r;
832
833	r = mfspr(SPR_DBCR0);
834	mtspr(SPR_DBCR0, r & ~DBCR0_IC);
835	kdb_frame->srr1 &= ~PSL_DE;
836}
837
838void
839kdb_cpu_set_singlestep(void)
840{
841	register_t r;
842
843	r = mfspr(SPR_DBCR0);
844	mtspr(SPR_DBCR0, r | DBCR0_IC | DBCR0_IDM);
845	kdb_frame->srr1 |= PSL_DE;
846}
847
848void
849sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
850{
851	struct trapframe *tf;
852	struct sigframe *sfp;
853	struct sigacts *psp;
854	struct sigframe sf;
855	struct thread *td;
856	struct proc *p;
857	int oonstack, rndfsize;
858	int sig, code;
859
860	td = curthread;
861	p = td->td_proc;
862	PROC_LOCK_ASSERT(p, MA_OWNED);
863	sig = ksi->ksi_signo;
864	code = ksi->ksi_code;
865	psp = p->p_sigacts;
866	mtx_assert(&psp->ps_mtx, MA_OWNED);
867	tf = td->td_frame;
868	oonstack = sigonstack(tf->fixreg[1]);
869
870	rndfsize = ((sizeof(sf) + 15) / 16) * 16;
871
872	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
873	    catcher, sig);
874
875	/*
876	 * Save user context
877	 */
878	memset(&sf, 0, sizeof(sf));
879	grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
880	sf.sf_uc.uc_sigmask = *mask;
881	sf.sf_uc.uc_stack = td->td_sigstk;
882	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
883		? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
884
885	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
886
887	/*
888	 * Allocate and validate space for the signal handler context.
889	 */
890	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
891	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
892		sfp = (struct sigframe *)((caddr_t)td->td_sigstk.ss_sp +
893		    td->td_sigstk.ss_size - rndfsize);
894	} else {
895		sfp = (struct sigframe *)(tf->fixreg[1] - rndfsize);
896	}
897
898	/*
899	 * Translate the signal if appropriate (Linux emu ?)
900	 */
901	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
902		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
903
904	/*
905	 * Save the floating-point state, if necessary, then copy it.
906	 */
907	/* XXX */
908
909	/*
910	 * Set up the registers to return to sigcode.
911	 *
912	 *   r1/sp - sigframe ptr
913	 *   lr    - sig function, dispatched to by blrl in trampoline
914	 *   r3    - sig number
915	 *   r4    - SIGINFO ? &siginfo : exception code
916	 *   r5    - user context
917	 *   srr0  - trampoline function addr
918	 */
919	tf->lr = (register_t)catcher;
920	tf->fixreg[1] = (register_t)sfp;
921	tf->fixreg[FIRSTARG] = sig;
922	tf->fixreg[FIRSTARG+2] = (register_t)&sfp->sf_uc;
923	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
924		/*
925		 * Signal handler installed with SA_SIGINFO.
926		 */
927		tf->fixreg[FIRSTARG+1] = (register_t)&sfp->sf_si;
928
929		/*
930		 * Fill siginfo structure.
931		 */
932		sf.sf_si = ksi->ksi_info;
933		sf.sf_si.si_signo = sig;
934		sf.sf_si.si_addr = (void *) ((tf->exc == EXC_DSI) ?
935		    tf->cpu.booke.dear : tf->srr0);
936	} else {
937		/* Old FreeBSD-style arguments. */
938		tf->fixreg[FIRSTARG+1] = code;
939		tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ?
940		    tf->cpu.booke.dear : tf->srr0;
941	}
942	mtx_unlock(&psp->ps_mtx);
943	PROC_UNLOCK(p);
944
945	tf->srr0 = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
946
947	/*
948	 * copy the frame out to userland.
949	 */
950	if (copyout((caddr_t)&sf, (caddr_t)sfp, sizeof(sf)) != 0) {
951		/*
952		 * Process has trashed its stack. Kill it.
953		 */
954		CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
955		PROC_LOCK(p);
956		sigexit(td, SIGILL);
957	}
958
959	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
960	    tf->srr0, tf->fixreg[1]);
961
962	PROC_LOCK(p);
963	mtx_lock(&psp->ps_mtx);
964}
965
966void
967bzero(void *buf, size_t len)
968{
969	caddr_t p;
970
971	p = buf;
972
973	while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
974		*p++ = 0;
975		len--;
976	}
977
978	while (len >= sizeof(u_long) * 8) {
979		*(u_long*) p = 0;
980		*((u_long*) p + 1) = 0;
981		*((u_long*) p + 2) = 0;
982		*((u_long*) p + 3) = 0;
983		len -= sizeof(u_long) * 8;
984		*((u_long*) p + 4) = 0;
985		*((u_long*) p + 5) = 0;
986		*((u_long*) p + 6) = 0;
987		*((u_long*) p + 7) = 0;
988		p += sizeof(u_long) * 8;
989	}
990
991	while (len >= sizeof(u_long)) {
992		*(u_long*) p = 0;
993		len -= sizeof(u_long);
994		p += sizeof(u_long);
995	}
996
997	while (len) {
998		*p++ = 0;
999		len--;
1000	}
1001}
1002
1003/*
1004 * XXX what is the better/proper place for this routine?
1005 */
1006int
1007mem_valid(vm_offset_t addr, int len)
1008{
1009
1010	return (1);
1011}
1012