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