machdep.c revision 134791
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__FBSDID("$FreeBSD: head/sys/powerpc/aim/machdep.c 134791 2004-09-05 02:09:54Z julian $");
59
60#include "opt_compat.h"
61#include "opt_ddb.h"
62#include "opt_kstack_pages.h"
63#include "opt_msgbuf.h"
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/kdb.h>
68#include <sys/eventhandler.h>
69#include <sys/imgact.h>
70#include <sys/sysproto.h>
71#include <sys/lock.h>
72#include <sys/mutex.h>
73#include <sys/ktr.h>
74#include <sys/signalvar.h>
75#include <sys/kernel.h>
76#include <sys/proc.h>
77#include <sys/malloc.h>
78#include <sys/reboot.h>
79#include <sys/bio.h>
80#include <sys/buf.h>
81#include <sys/bus.h>
82#include <sys/mbuf.h>
83#include <sys/vmmeter.h>
84#include <sys/msgbuf.h>
85#include <sys/exec.h>
86#include <sys/sysctl.h>
87#include <sys/uio.h>
88#include <sys/linker.h>
89#include <sys/cons.h>
90#include <sys/ucontext.h>
91#include <sys/sysent.h>
92#include <net/netisr.h>
93#include <vm/vm.h>
94#include <vm/vm_kern.h>
95#include <vm/vm_page.h>
96#include <vm/vm_map.h>
97#include <vm/vm_extern.h>
98#include <vm/vm_object.h>
99#include <vm/vm_pager.h>
100#include <sys/user.h>
101#include <sys/ptrace.h>
102#include <machine/bat.h>
103#include <machine/clock.h>
104#include <machine/cpu.h>
105#include <machine/md_var.h>
106#include <machine/metadata.h>
107#include <machine/reg.h>
108#include <machine/fpu.h>
109#include <machine/vmparam.h>
110#include <machine/elf.h>
111#include <machine/trap.h>
112#include <machine/powerpc.h>
113#include <dev/ofw/openfirm.h>
114#include <sys/vnode.h>
115#include <machine/sigframe.h>
116
117#include <ddb/ddb.h>
118
119#ifdef DDB
120extern vm_offset_t ksym_start, ksym_end;
121#endif
122
123int cold = 1;
124
125char		pcpu0[PAGE_SIZE];
126char		uarea0[UAREA_PAGES * PAGE_SIZE];
127struct		trapframe frame0;
128
129vm_offset_t	kstack0;
130vm_offset_t	kstack0_phys;
131
132char		machine[] = "powerpc";
133SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
134
135static char	model[128];
136SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, model, 0, "");
137
138static int cacheline_size = CACHELINESIZE;
139SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
140	   CTLFLAG_RD, &cacheline_size, 0, "");
141
142static void	cpu_startup(void *);
143SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
144
145void		powerpc_init(u_int, u_int, u_int, void *);
146
147int		save_ofw_mapping(void);
148int		restore_ofw_mapping(void);
149
150void		install_extint(void (*)(void));
151
152int             setfault(faultbuf);             /* defined in locore.S */
153
154static int	grab_mcontext(struct thread *, mcontext_t *, int);
155
156void		asm_panic(char *);
157
158long		Maxmem = 0;
159
160struct pmap	ofw_pmap;
161extern int	ofmsr;
162
163struct bat	battable[16];
164
165struct kva_md_info kmi;
166
167static void
168powerpc_ofw_shutdown(void *junk, int howto)
169{
170	if (howto & RB_HALT) {
171		OF_halt();
172	}
173	OF_reboot();
174}
175
176static void
177cpu_startup(void *dummy)
178{
179
180	/*
181	 * Initialise the decrementer-based clock.
182	 */
183	decr_init();
184
185	/*
186	 * Good {morning,afternoon,evening,night}.
187	 */
188	cpu_setup(PCPU_GET(cpuid));
189
190	/* startrtclock(); */
191#ifdef PERFMON
192	perfmon_init();
193#endif
194	printf("real memory  = %ld (%ld MB)\n", ptoa(Maxmem),
195	    ptoa(Maxmem) / 1048576);
196
197	/*
198	 * Display any holes after the first chunk of extended memory.
199	 */
200	if (bootverbose) {
201		int indx;
202
203		printf("Physical memory chunk(s):\n");
204		for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
205			int size1 = phys_avail[indx + 1] - phys_avail[indx];
206
207			printf("0x%08x - 0x%08x, %d bytes (%d pages)\n",
208			    phys_avail[indx], phys_avail[indx + 1] - 1, size1,
209			    size1 / PAGE_SIZE);
210		}
211	}
212
213	vm_ksubmap_init(&kmi);
214
215	printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
216	    ptoa(cnt.v_free_count) / 1048576);
217
218	/*
219	 * Set up buffers, so they can be used to read disk labels.
220	 */
221	bufinit();
222	vm_pager_bufferinit();
223
224	EVENTHANDLER_REGISTER(shutdown_final, powerpc_ofw_shutdown, 0,
225	    SHUTDOWN_PRI_LAST);
226
227#ifdef SMP
228	/*
229	 * OK, enough kmem_alloc/malloc state should be up, lets get on with it!
230	 */
231	mp_start();			/* fire up the secondaries */
232	mp_announce();
233#endif  /* SMP */
234}
235
236extern char	kernel_text[], _end[];
237
238extern void	*trapcode, *trapsize;
239extern void	*alitrap, *alisize;
240extern void	*dsitrap, *dsisize;
241extern void	*decrint, *decrsize;
242extern void     *extint, *extsize;
243extern void	*dblow, *dbsize;
244
245void
246powerpc_init(u_int startkernel, u_int endkernel, u_int basekernel, void *mdp)
247{
248	struct		pcpu *pc;
249	vm_offset_t	end, off;
250	void		*kmdp;
251
252	end = 0;
253	kmdp = NULL;
254
255	/*
256	 * Parse metadata if present and fetch parameters.  Must be done
257	 * before console is inited so cninit gets the right value of
258	 * boothowto.
259	 */
260	if (mdp != NULL) {
261		preload_metadata = mdp;
262		kmdp = preload_search_by_type("elf kernel");
263		if (kmdp != NULL) {
264			boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
265			kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
266			end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
267#ifdef DDB
268			ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
269			ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
270#endif
271		}
272	}
273
274	/*
275	 * Init params/tunables that can be overridden by the loader
276	 */
277	init_param1();
278
279	/*
280	 * Start initializing proc0 and thread0.
281	 */
282	proc_linkup(&proc0, &ksegrp0, &thread0);
283	proc0.p_uarea = (struct user *)uarea0;
284	proc0.p_stats = &proc0.p_uarea->u_stats;
285	thread0.td_frame = &frame0;
286
287	/*
288	 * Set up per-cpu data.
289	 */
290	pc = (struct pcpu *)(pcpu0 + PAGE_SIZE) - 1;
291	pcpu_init(pc, 0, sizeof(struct pcpu));
292	pc->pc_curthread = &thread0;
293	pc->pc_curpcb = thread0.td_pcb;
294	pc->pc_cpuid = 0;
295	/* pc->pc_mid = mid; */
296
297	__asm __volatile("mtsprg 0, %0" :: "r"(pc));
298
299	mutex_init();
300
301	/*
302	 * Initialize the console before printing anything.
303	 */
304	cninit();
305
306	/*
307	 * Complain if there is no metadata.
308	 */
309	if (mdp == NULL || kmdp == NULL) {
310		printf("powerpc_init: no loader metadata.\n");
311	}
312
313	kdb_init();
314
315	/*
316	 * XXX: Initialize the interrupt tables.
317	 *      Disable translation in case the vector area
318	 *      hasn't been mapped (G5)
319	 */
320	mtmsr(mfmsr() & ~(PSL_IR | PSL_DR));
321	isync();
322	bcopy(&trapcode, (void *)EXC_RST,  (size_t)&trapsize);
323	bcopy(&trapcode, (void *)EXC_MCHK, (size_t)&trapsize);
324	bcopy(&dsitrap,  (void *)EXC_DSI,  (size_t)&dsisize);
325	bcopy(&trapcode, (void *)EXC_ISI,  (size_t)&trapsize);
326	bcopy(&trapcode, (void *)EXC_EXI,  (size_t)&trapsize);
327	bcopy(&trapcode, (void *)EXC_ALI,  (size_t)&trapsize);
328	bcopy(&trapcode, (void *)EXC_PGM,  (size_t)&trapsize);
329	bcopy(&trapcode, (void *)EXC_FPU,  (size_t)&trapsize);
330	bcopy(&trapcode, (void *)EXC_DECR, (size_t)&trapsize);
331	bcopy(&trapcode, (void *)EXC_SC,   (size_t)&trapsize);
332	bcopy(&trapcode, (void *)EXC_TRC,  (size_t)&trapsize);
333	bcopy(&trapcode, (void *)EXC_FPA,  (size_t)&trapsize);
334	bcopy(&trapcode, (void *)EXC_THRM, (size_t)&trapsize);
335	bcopy(&trapcode, (void *)EXC_BPT,  (size_t)&trapsize);
336#ifdef KDB
337	bcopy(&dblow,	 (void *)EXC_RST,  (size_t)&dbsize);
338	bcopy(&dblow,	 (void *)EXC_MCHK, (size_t)&dbsize);
339	bcopy(&dblow,   (void *)EXC_PGM,  (size_t)&dbsize);
340	bcopy(&dblow,   (void *)EXC_TRC,  (size_t)&dbsize);
341	bcopy(&dblow,   (void *)EXC_BPT,  (size_t)&dbsize);
342#endif
343	__syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
344
345	/*
346	 * Make sure translation has been enabled
347	 */
348	mtmsr(mfmsr() | PSL_IR|PSL_DR|PSL_ME|PSL_RI);
349	isync();
350
351	/*
352	 * Initialise virtual memory.
353	 */
354	pmap_bootstrap(startkernel, endkernel);
355
356	/*
357	 * Initialize params/tunables that are derived from memsize
358	 */
359	init_param2(physmem);
360
361	/*
362	 * Finish setting up thread0.
363	 */
364	thread0.td_kstack = kstack0;
365	thread0.td_pcb = (struct pcb *)
366	    (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
367
368	/*
369	 * Map and initialise the message buffer.
370	 */
371	for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
372		pmap_kenter((vm_offset_t)msgbufp + off, msgbuf_phys + off);
373	msgbufinit(msgbufp, MSGBUF_SIZE);
374
375#ifdef KDB
376	if (boothowto & RB_KDB)
377		kdb_enter("Boot flags requested debugger");
378#endif
379}
380
381void
382bzero(void *buf, size_t len)
383{
384	caddr_t	p;
385
386	p = buf;
387
388	while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
389		*p++ = 0;
390		len--;
391	}
392
393	while (len >= sizeof(u_long) * 8) {
394		*(u_long*) p = 0;
395		*((u_long*) p + 1) = 0;
396		*((u_long*) p + 2) = 0;
397		*((u_long*) p + 3) = 0;
398		len -= sizeof(u_long) * 8;
399		*((u_long*) p + 4) = 0;
400		*((u_long*) p + 5) = 0;
401		*((u_long*) p + 6) = 0;
402		*((u_long*) p + 7) = 0;
403		p += sizeof(u_long) * 8;
404	}
405
406	while (len >= sizeof(u_long)) {
407		*(u_long*) p = 0;
408		len -= sizeof(u_long);
409		p += sizeof(u_long);
410	}
411
412	while (len) {
413		*p++ = 0;
414		len--;
415	}
416}
417
418void
419sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
420{
421	struct trapframe *tf;
422	struct sigframe *sfp;
423	struct sigacts *psp;
424	struct sigframe sf;
425	struct thread *td;
426	struct proc *p;
427	int oonstack, rndfsize;
428
429	td = curthread;
430	p = td->td_proc;
431	PROC_LOCK_ASSERT(p, MA_OWNED);
432	psp = p->p_sigacts;
433	mtx_assert(&psp->ps_mtx, MA_OWNED);
434	tf = td->td_frame;
435	oonstack = sigonstack(tf->fixreg[1]);
436
437	rndfsize = ((sizeof(sf) + 15) / 16) * 16;
438
439	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
440	     catcher, sig);
441
442	/*
443	 * Save user context
444	 */
445	memset(&sf, 0, sizeof(sf));
446	grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
447	sf.sf_uc.uc_sigmask = *mask;
448	sf.sf_uc.uc_stack = td->td_sigstk;
449	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
450	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
451
452	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
453
454	/*
455	 * Allocate and validate space for the signal handler context.
456	 */
457	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
458	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
459		sfp = (struct sigframe *)((caddr_t)td->td_sigstk.ss_sp +
460		   td->td_sigstk.ss_size - rndfsize);
461	} else {
462		sfp = (struct sigframe *)(tf->fixreg[1] - rndfsize);
463	}
464
465	/*
466	 * Translate the signal if appropriate (Linux emu ?)
467	 */
468	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
469		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
470
471	/*
472	 * Save the floating-point state, if necessary, then copy it.
473	 */
474	/* XXX */
475
476	/*
477	 * Set up the registers to return to sigcode.
478	 *
479	 *   r1/sp - sigframe ptr
480	 *   lr    - sig function, dispatched to by blrl in trampoline
481	 *   r3    - sig number
482	 *   r4    - SIGINFO ? &siginfo : exception code
483	 *   r5    - user context
484	 *   srr0  - trampoline function addr
485	 */
486	tf->lr = (register_t)catcher;
487	tf->fixreg[1] = (register_t)sfp;
488	tf->fixreg[FIRSTARG] = sig;
489	tf->fixreg[FIRSTARG+2] = (register_t)&sfp->sf_uc;
490	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
491		/*
492		 * Signal handler installed with SA_SIGINFO.
493		 */
494		tf->fixreg[FIRSTARG+1] = (register_t)&sfp->sf_si;
495
496		/*
497		 * Fill siginfo structure.
498		 */
499		sf.sf_si.si_signo = sig;
500		sf.sf_si.si_code = code;
501		sf.sf_si.si_addr = (void *)tf->srr0;
502	} else {
503		/* Old FreeBSD-style arguments. */
504		tf->fixreg[FIRSTARG+1] = code;
505	}
506	mtx_unlock(&psp->ps_mtx);
507	PROC_UNLOCK(p);
508
509	tf->srr0 = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
510
511	/*
512	 * copy the frame out to userland.
513	 */
514	if (copyout((caddr_t)&sf, (caddr_t)sfp, sizeof(sf)) != 0) {
515		/*
516		 * Process has trashed its stack. Kill it.
517		 */
518		CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
519		PROC_LOCK(p);
520		sigexit(td, SIGILL);
521	}
522
523	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
524	     tf->srr0, tf->fixreg[1]);
525
526	PROC_LOCK(p);
527	mtx_lock(&psp->ps_mtx);
528}
529
530/*
531 * Build siginfo_t for SA thread
532 */
533void
534cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
535{
536	struct proc *p;
537	struct thread *td;
538
539	td = curthread;
540	p = td->td_proc;
541	PROC_LOCK_ASSERT(p, MA_OWNED);
542
543	bzero(si, sizeof(*si));
544	si->si_signo = sig;
545	si->si_code = code;
546	/* XXXKSE fill other fields */
547}
548
549int
550sigreturn(struct thread *td, struct sigreturn_args *uap)
551{
552	struct proc *p;
553	ucontext_t uc;
554	int error;
555
556	CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
557
558	if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
559		CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
560		return (EFAULT);
561	}
562
563	error = set_mcontext(td, &uc.uc_mcontext);
564	if (error != 0)
565		return (error);
566
567	p = td->td_proc;
568	PROC_LOCK(p);
569	td->td_sigmask = uc.uc_sigmask;
570	SIG_CANTMASK(td->td_sigmask);
571	signotify(td);
572	PROC_UNLOCK(p);
573
574	CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
575	     td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
576
577	return (EJUSTRETURN);
578}
579
580#ifdef COMPAT_FREEBSD4
581int
582freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
583{
584
585	return sigreturn(td, (struct sigreturn_args *)uap);
586}
587#endif
588
589/*
590 * Construct a PCB from a trapframe. This is called from kdb_trap() where
591 * we want to start a backtrace from the function that caused us to enter
592 * the debugger. We have the context in the trapframe, but base the trace
593 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
594 * enough for a backtrace.
595 */
596void
597makectx(struct trapframe *tf, struct pcb *pcb)
598{
599
600	pcb->pcb_lr = tf->srr0;
601	pcb->pcb_sp = tf->fixreg[1];
602}
603
604/*
605 * get_mcontext/sendsig helper routine that doesn't touch the
606 * proc lock
607 */
608static int
609grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
610{
611	struct pcb *pcb;
612
613	pcb = td->td_pcb;
614
615	memset(mcp, 0, sizeof(mcontext_t));
616
617	mcp->mc_vers = _MC_VERSION;
618	mcp->mc_flags = 0;
619	memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
620	if (flags & GET_MC_CLEAR_RET) {
621		mcp->mc_gpr[3] = 0;
622		mcp->mc_gpr[4] = 0;
623	}
624
625	/*
626	 * This assumes that floating-point context is *not* lazy,
627	 * so if the thread has used FP there would have been a
628	 * FP-unavailable exception that would have set things up
629	 * correctly.
630	 */
631	if (pcb->pcb_flags & PCB_FPU) {
632		KASSERT(td == curthread,
633			("get_mcontext: fp save not curthread"));
634		critical_enter();
635		save_fpu(td);
636		critical_exit();
637		mcp->mc_flags |= _MC_FP_VALID;
638		memcpy(&mcp->mc_fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
639		memcpy(mcp->mc_fpreg, pcb->pcb_fpu.fpr, 32*sizeof(double));
640	}
641
642	/* XXX Altivec context ? */
643
644	mcp->mc_len = sizeof(*mcp);
645
646	return (0);
647}
648
649int
650get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
651{
652	int error;
653
654	error = grab_mcontext(td, mcp, flags);
655	if (error == 0) {
656		PROC_LOCK(curthread->td_proc);
657		mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
658		PROC_UNLOCK(curthread->td_proc);
659	}
660
661	return (error);
662}
663
664int
665set_mcontext(struct thread *td, const mcontext_t *mcp)
666{
667	struct pcb *pcb;
668	struct trapframe *tf;
669
670	pcb = td->td_pcb;
671	tf = td->td_frame;
672
673	if (mcp->mc_vers != _MC_VERSION ||
674	    mcp->mc_len != sizeof(*mcp))
675		return (EINVAL);
676
677	/*
678	 * Don't let the user set privileged MSR bits
679	 */
680	if ((mcp->mc_srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC)) {
681		return (EINVAL);
682	}
683
684	memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
685
686	if (mcp->mc_flags & _MC_FP_VALID) {
687		if ((pcb->pcb_flags & PCB_FPU) != PCB_FPU) {
688			critical_enter();
689			enable_fpu(td);
690			critical_exit();
691		}
692		memcpy(&pcb->pcb_fpu.fpscr, &mcp->mc_fpscr, sizeof(double));
693		memcpy(pcb->pcb_fpu.fpr, mcp->mc_fpreg, 32*sizeof(double));
694	}
695
696	/* XXX Altivec context? */
697
698	return (0);
699}
700
701void
702cpu_boot(int howto)
703{
704}
705
706/*
707 * Shutdown the CPU as much as possible.
708 */
709void
710cpu_halt(void)
711{
712
713	OF_exit();
714}
715
716void
717cpu_idle(void)
718{
719	/* Insert code to halt (until next interrupt) for the idle loop */
720}
721
722/*
723 * Set set up registers on exec.
724 */
725void
726exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
727{
728	struct trapframe	*tf;
729	struct ps_strings	arginfo;
730
731	tf = trapframe(td);
732	bzero(tf, sizeof *tf);
733	tf->fixreg[1] = -roundup(-stack + 8, 16);
734
735	/*
736	 * XXX Machine-independent code has already copied arguments and
737	 * XXX environment to userland.  Get them back here.
738	 */
739	(void)copyin((char *)PS_STRINGS, &arginfo, sizeof(arginfo));
740
741	/*
742	 * Set up arguments for _start():
743	 *	_start(argc, argv, envp, obj, cleanup, ps_strings);
744	 *
745	 * Notes:
746	 *	- obj and cleanup are the auxilliary and termination
747	 *	  vectors.  They are fixed up by ld.elf_so.
748	 *	- ps_strings is a NetBSD extention, and will be
749	 * 	  ignored by executables which are strictly
750	 *	  compliant with the SVR4 ABI.
751	 *
752	 * XXX We have to set both regs and retval here due to different
753	 * XXX calling convention in trap.c and init_main.c.
754	 */
755        /*
756         * XXX PG: these get overwritten in the syscall return code.
757         * execve() should return EJUSTRETURN, like it does on NetBSD.
758         * Emulate by setting the syscall return value cells. The
759         * registers still have to be set for init's fork trampoline.
760         */
761        td->td_retval[0] = arginfo.ps_nargvstr;
762        td->td_retval[1] = (register_t)arginfo.ps_argvstr;
763	tf->fixreg[3] = arginfo.ps_nargvstr;
764	tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
765	tf->fixreg[5] = (register_t)arginfo.ps_envstr;
766	tf->fixreg[6] = 0;			/* auxillary vector */
767	tf->fixreg[7] = 0;			/* termination vector */
768	tf->fixreg[8] = (register_t)PS_STRINGS;	/* NetBSD extension */
769
770	tf->srr0 = entry;
771	tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
772	td->td_pcb->pcb_flags = 0;
773}
774
775int
776fill_regs(struct thread *td, struct reg *regs)
777{
778	struct trapframe *tf;
779
780	tf = td->td_frame;
781	memcpy(regs, tf, sizeof(struct reg));
782
783	return (0);
784}
785
786int
787fill_dbregs(struct thread *td, struct dbreg *dbregs)
788{
789	/* No debug registers on PowerPC */
790	return (ENOSYS);
791}
792
793int
794fill_fpregs(struct thread *td, struct fpreg *fpregs)
795{
796	struct pcb *pcb;
797
798	pcb = td->td_pcb;
799
800	if ((pcb->pcb_flags & PCB_FPU) == 0)
801		memset(fpregs, 0, sizeof(struct fpreg));
802	else
803		memcpy(fpregs, &pcb->pcb_fpu, sizeof(struct fpreg));
804
805	return (0);
806}
807
808int
809set_regs(struct thread *td, struct reg *regs)
810{
811	struct trapframe *tf;
812
813	tf = td->td_frame;
814	memcpy(tf, regs, sizeof(struct reg));
815
816	return (0);
817}
818
819int
820set_dbregs(struct thread *td, struct dbreg *dbregs)
821{
822	/* No debug registers on PowerPC */
823	return (ENOSYS);
824}
825
826int
827set_fpregs(struct thread *td, struct fpreg *fpregs)
828{
829	struct pcb *pcb;
830
831	pcb = td->td_pcb;
832	if ((pcb->pcb_flags & PCB_FPU) == 0)
833		enable_fpu(td);
834	memcpy(&pcb->pcb_fpu, fpregs, sizeof(struct fpreg));
835
836	return (0);
837}
838
839int
840ptrace_set_pc(struct thread *td, unsigned long addr)
841{
842	struct trapframe *tf;
843
844	tf = td->td_frame;
845	tf->srr0 = (register_t)addr;
846
847	return (0);
848}
849
850int
851ptrace_single_step(struct thread *td)
852{
853	struct trapframe *tf;
854
855	tf = td->td_frame;
856	tf->srr1 |= PSL_SE;
857
858	return (0);
859}
860
861int
862ptrace_clear_single_step(struct thread *td)
863{
864	struct trapframe *tf;
865
866	tf = td->td_frame;
867	tf->srr1 &= ~PSL_SE;
868
869	return (0);
870}
871
872/*
873 * Initialise a struct pcpu.
874 */
875void
876cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
877{
878
879}
880
881/*
882 * kcopy(const void *src, void *dst, size_t len);
883 *
884 * Copy len bytes from src to dst, aborting if we encounter a fatal
885 * page fault.
886 *
887 * kcopy() _must_ save and restore the old fault handler since it is
888 * called by uiomove(), which may be in the path of servicing a non-fatal
889 * page fault.
890 */
891int
892kcopy(const void *src, void *dst, size_t len)
893{
894	struct thread	*td;
895	faultbuf	env, *oldfault;
896	int		rv;
897
898	td = PCPU_GET(curthread);
899	oldfault = td->td_pcb->pcb_onfault;
900	if ((rv = setfault(env)) != 0) {
901		td->td_pcb->pcb_onfault = oldfault;
902		return rv;
903	}
904
905	memcpy(dst, src, len);
906
907	td->td_pcb->pcb_onfault = oldfault;
908	return (0);
909}
910
911void
912asm_panic(char *pstr)
913{
914	panic(pstr);
915}
916
917int db_trap_glue(struct trapframe *);		/* Called from trap_subr.S */
918
919int
920db_trap_glue(struct trapframe *frame)
921{
922	if (!(frame->srr1 & PSL_PR)
923	    && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
924		|| (frame->exc == EXC_PGM
925		    && (frame->srr1 & 0x20000))
926		|| frame->exc == EXC_BPT
927		|| frame->exc == EXC_DSI)) {
928		int type = frame->exc;
929		if (type == EXC_PGM && (frame->srr1 & 0x20000)) {
930			type = T_BREAKPOINT;
931		}
932		return (kdb_trap(type, 0, frame));
933	}
934
935	return (0);
936}
937