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