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