trap.c revision 1.49
1/*	$NetBSD: trap.c,v 1.49 2008/10/15 06:51:18 wrstuden Exp $	*/
2
3/*
4 * Copyright 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *      This product includes software developed for the NetBSD Project by
20 *      Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 *    or promote products derived from this software without specific prior
23 *    written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
40 * Copyright (C) 1995, 1996 TooLs GmbH.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 *    must display the following acknowledgement:
53 *	This product includes software developed by TooLs GmbH.
54 * 4. The name of TooLs GmbH may not be used to endorse or promote products
55 *    derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
63 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
64 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
65 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 */
68
69#include <sys/cdefs.h>
70__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.49 2008/10/15 06:51:18 wrstuden Exp $");
71
72#include "opt_altivec.h"
73#include "opt_ddb.h"
74#include "opt_kgdb.h"
75
76#include <sys/param.h>
77#include <sys/proc.h>
78#include <sys/reboot.h>
79#include <sys/syscall.h>
80#include <sys/systm.h>
81#include <sys/user.h>
82#include <sys/pool.h>
83#include <sys/sa.h>
84#include <sys/savar.h>
85#include <sys/userret.h>
86#include <sys/kauth.h>
87
88#if defined(KGDB)
89#include <sys/kgdb.h>
90#endif
91
92#include <uvm/uvm_extern.h>
93
94#include <dev/cons.h>
95
96#include <machine/cpu.h>
97#include <machine/db_machdep.h>
98#include <machine/fpu.h>
99#include <machine/frame.h>
100#include <machine/pcb.h>
101#include <machine/psl.h>
102#include <machine/trap.h>
103
104#include <powerpc/spr.h>
105#include <powerpc/ibm4xx/pmap.h>
106#include <powerpc/ibm4xx/tlb.h>
107#include <powerpc/fpu/fpu_extern.h>
108
109/* These definitions should probably be somewhere else			XXX */
110#define	FIRSTARG	3		/* first argument is in reg 3 */
111#define	NARGREG		8		/* 8 args are in registers */
112#define	MOREARGS(sp)	((void *)((int)(sp) + 8)) /* more args go here */
113
114static int fix_unaligned __P((struct lwp *l, struct trapframe *frame));
115
116void trap __P((struct trapframe *));	/* Called from locore / trap_subr */
117/* Why are these not defined in a header? */
118int badaddr __P((void *, size_t));
119int badaddr_read __P((void *, size_t, int *));
120int ctx_setup __P((int, int));
121
122#ifdef DEBUG
123#define TDB_ALL	0x1
124int trapdebug = /* TDB_ALL */ 0;
125#define	DBPRINTF(x, y)	if (trapdebug & (x)) printf y
126#else
127#define DBPRINTF(x, y)
128#endif
129
130void
131trap(struct trapframe *frame)
132{
133	struct lwp *l = curlwp;
134	struct proc *p = l ? l->l_proc : NULL;
135	int type = frame->exc;
136	int ftype, rv;
137	ksiginfo_t ksi;
138
139	KASSERT(l == 0 || (l->l_stat == LSONPROC));
140
141	if (frame->srr1 & PSL_PR) {
142		LWP_CACHE_CREDS(l, p);
143		type |= EXC_USER;
144	}
145
146	ftype = VM_PROT_READ;
147
148	DBPRINTF(TDB_ALL, ("trap(%x) at %lx from frame %p &frame %p\n",
149	    type, frame->srr0, frame, &frame));
150
151	switch (type) {
152	case EXC_DEBUG|EXC_USER:
153		{
154			int srr2, srr3;
155
156			__asm volatile("mfspr %0,0x3f0" :
157			    "=r" (rv), "=r" (srr2), "=r" (srr3) :);
158			printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2,
159			    srr3);
160			/* XXX fall through or break here?! */
161		}
162		/*
163		 * DEBUG intr -- probably single-step.
164		 */
165	case EXC_TRC|EXC_USER:
166		frame->srr1 &= ~PSL_SE;
167		KSI_INIT_TRAP(&ksi);
168		ksi.ksi_signo = SIGTRAP;
169		ksi.ksi_trap = EXC_TRC;
170		ksi.ksi_addr = (void *)frame->srr0;
171		trapsignal(l, &ksi);
172		break;
173
174	/*
175	 * If we could not find and install appropriate TLB entry, fall through.
176	 */
177
178	case EXC_DSI:
179		/* FALLTHROUGH */
180	case EXC_DTMISS:
181		{
182			struct vm_map *map;
183			vaddr_t va;
184			struct faultbuf *fb = NULL;
185
186			va = frame->dar;
187			if (frame->tf_xtra[TF_PID] == KERNEL_PID) {
188				map = kernel_map;
189			} else {
190				map = &p->p_vmspace->vm_map;
191				if ((l->l_flag & LW_SA)
192				    && (~l->l_pflag & LP_SA_NOBLOCK)) {
193					l->l_savp->savp_faultaddr = va;
194					l->l_pflag |= LP_SA_PAGEFAULT;
195				}
196			}
197
198			if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
199				ftype = VM_PROT_WRITE;
200
201			DBPRINTF(TDB_ALL,
202			    ("trap(EXC_DSI) at %lx %s fault on %p esr %x\n",
203			    frame->srr0,
204			    (ftype & VM_PROT_WRITE) ? "write" : "read",
205			    (void *)va, frame->tf_xtra[TF_ESR]));
206			rv = uvm_fault(map, trunc_page(va), ftype);
207			if (map != kernel_map) {
208				l->l_pflag &= ~LP_SA_PAGEFAULT;
209			}
210			if (rv == 0)
211				goto done;
212			if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
213				frame->tf_xtra[TF_PID] = KERNEL_PID;
214				frame->srr0 = fb->fb_pc;
215				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
216				frame->fixreg[1] = fb->fb_sp;
217				frame->fixreg[2] = fb->fb_r2;
218				frame->fixreg[3] = 1; /* Return TRUE */
219				frame->cr = fb->fb_cr;
220				memcpy(&frame->fixreg[13], fb->fb_fixreg,
221				    sizeof(fb->fb_fixreg));
222				goto done;
223			}
224		}
225		goto brain_damage;
226
227	case EXC_DSI|EXC_USER:
228		/* FALLTHROUGH */
229	case EXC_DTMISS|EXC_USER:
230		if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
231			ftype = VM_PROT_WRITE;
232
233		DBPRINTF(TDB_ALL,
234		    ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
235		    frame->srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
236		    frame->dar, frame->tf_xtra[TF_ESR]));
237		KASSERT(l == curlwp && (l->l_stat == LSONPROC));
238		if (l->l_flag & LW_SA) {
239			l->l_savp->savp_faultaddr = (vaddr_t)frame->dar;
240			l->l_pflag |= LP_SA_PAGEFAULT;
241		}
242		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->dar),
243		    ftype);
244		if (rv == 0) {
245			l->l_pflag &= ~LP_SA_PAGEFAULT;
246			break;
247		}
248		KSI_INIT_TRAP(&ksi);
249		ksi.ksi_signo = SIGSEGV;
250		ksi.ksi_trap = EXC_DSI;
251		ksi.ksi_addr = (void *)frame->dar;
252		if (rv == ENOMEM) {
253			printf("UVM: pid %d (%s) lid %d, uid %d killed: "
254			    "out of swap\n",
255			    p->p_pid, p->p_comm, l->l_lid,
256			    l->l_cred ?
257			    kauth_cred_geteuid(l->l_cred) : -1);
258			ksi.ksi_signo = SIGKILL;
259		}
260		trapsignal(l, &ksi);
261		l->l_pflag &= ~LP_SA_PAGEFAULT;
262		break;
263
264	case EXC_ITMISS|EXC_USER:
265	case EXC_ISI|EXC_USER:
266		if (l->l_flag & LW_SA) {
267			l->l_savp->savp_faultaddr = (vaddr_t)frame->srr0;
268			l->l_pflag |= LP_SA_PAGEFAULT;
269		}
270		ftype = VM_PROT_EXECUTE;
271		DBPRINTF(TDB_ALL,
272		    ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
273		    frame->srr0, frame));
274		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0),
275		    ftype);
276		if (rv == 0) {
277			l->l_pflag &= ~LP_SA_PAGEFAULT;
278			break;
279		}
280		KSI_INIT_TRAP(&ksi);
281		ksi.ksi_signo = SIGSEGV;
282		ksi.ksi_trap = EXC_ISI;
283		ksi.ksi_addr = (void *)frame->srr0;
284		ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
285		trapsignal(l, &ksi);
286		l->l_pflag &= ~LP_SA_PAGEFAULT;
287		break;
288
289	case EXC_AST|EXC_USER:
290		curcpu()->ci_astpending = 0;	/* we are about to do it */
291		uvmexp.softs++;
292		if (l->l_pflag & LP_OWEUPC) {
293			l->l_pflag &= ~LP_OWEUPC;
294			ADDUPROF(l);
295		}
296		/* Check whether we are being preempted. */
297		if (curcpu()->ci_want_resched)
298			preempt();
299		break;
300
301
302	case EXC_ALI|EXC_USER:
303		if (fix_unaligned(l, frame) != 0) {
304			KSI_INIT_TRAP(&ksi);
305			ksi.ksi_signo = SIGBUS;
306			ksi.ksi_trap = EXC_ALI;
307			ksi.ksi_addr = (void *)frame->dar;
308			trapsignal(l, &ksi);
309		} else
310			frame->srr0 += 4;
311		break;
312
313	case EXC_PGM|EXC_USER:
314		/*
315		 * Illegal insn:
316		 *
317		 * let's try to see if it's FPU and can be emulated.
318		 */
319		uvmexp.traps++;
320		if (!(l->l_addr->u_pcb.pcb_flags & PCB_FPU)) {
321			memset(&l->l_addr->u_pcb.pcb_fpu, 0,
322				sizeof l->l_addr->u_pcb.pcb_fpu);
323			l->l_addr->u_pcb.pcb_flags |= PCB_FPU;
324		}
325
326		if ((rv = fpu_emulate(frame,
327			(struct fpreg *)&l->l_addr->u_pcb.pcb_fpu))) {
328			KSI_INIT_TRAP(&ksi);
329			ksi.ksi_signo = rv;
330			ksi.ksi_trap = EXC_PGM;
331			ksi.ksi_addr = (void *)frame->srr0;
332			trapsignal(l, &ksi);
333		}
334		break;
335
336	case EXC_MCHK:
337		{
338			struct faultbuf *fb;
339
340			if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
341				frame->tf_xtra[TF_PID] = KERNEL_PID;
342				frame->srr0 = fb->fb_pc;
343				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
344				frame->fixreg[1] = fb->fb_sp;
345				frame->fixreg[2] = fb->fb_r2;
346				frame->fixreg[3] = 1; /* Return TRUE */
347				frame->cr = fb->fb_cr;
348				memcpy(&frame->fixreg[13], fb->fb_fixreg,
349				    sizeof(fb->fb_fixreg));
350				goto done;
351			}
352		}
353		goto brain_damage;
354	default:
355 brain_damage:
356		printf("trap type 0x%x at 0x%lx\n", type, frame->srr0);
357#if defined(DDB) || defined(KGDB)
358		if (kdb_trap(type, frame))
359			goto done;
360#endif
361#ifdef TRAP_PANICWAIT
362		printf("Press a key to panic.\n");
363		cngetc();
364#endif
365		panic("trap");
366	}
367
368	/* Invoke MI userret code */
369	mi_userret(l);
370 done:
371	return;
372}
373
374int
375ctx_setup(int ctx, int srr1)
376{
377	volatile struct pmap *pm;
378
379	/* Update PID if we're returning to user mode. */
380	if (srr1 & PSL_PR) {
381		pm = curproc->p_vmspace->vm_map.pmap;
382		if (!pm->pm_ctx) {
383			ctx_alloc(__UNVOLATILE(pm));
384		}
385		ctx = pm->pm_ctx;
386		if (srr1 & PSL_SE) {
387			int dbreg, mask = 0x48000000;
388				/*
389				 * Set the Internal Debug and
390				 * Instruction Completion bits of
391				 * the DBCR0 register.
392				 *
393				 * XXX this is also used by jtag debuggers...
394				 */
395			__asm volatile("mfspr %0,0x3f2;"
396			    "or %0,%0,%1;"
397			    "mtspr 0x3f2,%0;" :
398			    "=&r" (dbreg) : "r" (mask));
399		}
400	}
401	else if (!ctx) {
402		ctx = KERNEL_PID;
403	}
404	return (ctx);
405}
406
407/*
408 * Used by copyin()/copyout()
409 */
410extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
411extern void vunmaprange __P((vaddr_t, vsize_t));
412static int bigcopyin __P((const void *, void *, size_t ));
413static int bigcopyout __P((const void *, void *, size_t ));
414
415int
416copyin(const void *udaddr, void *kaddr, size_t len)
417{
418	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
419	int msr, pid, tmp, ctx, count=0;
420	struct faultbuf env;
421
422	/* For bigger buffers use the faster copy */
423	if (len > 1024)
424		return (bigcopyin(udaddr, kaddr, len));
425
426	if (setfault(&env)) {
427		curpcb->pcb_onfault = 0;
428		return EFAULT;
429	}
430
431	if (!(ctx = pm->pm_ctx)) {
432		/* No context -- assign it one */
433		ctx_alloc(pm);
434		ctx = pm->pm_ctx;
435	}
436
437	__asm volatile(
438		"   mfmsr %[msr];"          /* Save MSR */
439		"   li %[pid],0x20; "
440		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */
441		"   mfpid %[pid];"          /* Save old PID */
442		"   sync; isync;"
443
444		"   srwi. %[count],%[len],0x2;"     /* How many words? */
445		"   beq-  2f;"              /* No words. Go do bytes */
446		"   mtctr %[count];"
447		"1: mtpid %[ctx]; sync;"
448		"   lswi %[tmp],%[udaddr],4;"       /* Load user word */
449		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
450		"   sync; isync;"
451		"   mtpid %[pid];sync;"
452		"   stswi %[tmp],%[kaddr],4;"        /* Store kernel word */
453		"   dcbf 0,%[kaddr];"           /* flush cache */
454		"   addi %[kaddr],%[kaddr],0x4;"    /* next udaddr word */
455		"   sync; isync;"
456		"   bdnz 1b;"               /* repeat */
457
458		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
459		"   addi %[count],%[count],0x1;"
460		"   mtctr %[count];"
461		"3: bdz 10f;"               /* while count */
462		"   mtpid %[ctx];sync;"
463		"   lbz %[tmp],0(%[udaddr]);"       /* Load user byte */
464		"   addi %[udaddr],%[udaddr],0x1;"  /* next udaddr byte */
465		"   sync; isync;"
466		"   mtpid %[pid]; sync;"
467		"   stb %[tmp],0(%[kaddr]);"        /* Store kernel byte */
468		"   dcbf 0,%[kaddr];"           /* flush cache */
469		"   addi %[kaddr],%[kaddr],0x1;"
470		"   sync; isync;"
471		"   b 3b;"
472		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
473		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
474		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
475
476	curpcb->pcb_onfault = 0;
477	return 0;
478}
479
480static int
481bigcopyin(const void *udaddr, void *kaddr, size_t len)
482{
483	const char *up;
484	char *kp = kaddr;
485	struct lwp *l = curlwp;
486	struct proc *p;
487	int error;
488
489	if (!l) {
490		return EFAULT;
491	}
492
493	p = l->l_proc;
494
495	/*
496	 * Stolen from physio():
497	 */
498	uvm_lwp_hold(l);
499	error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
500	if (error) {
501		uvm_lwp_rele(l);
502		return EFAULT;
503	}
504	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
505
506	memcpy(kp, up, len);
507	vunmaprange((vaddr_t)up, len);
508	uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
509	uvm_lwp_rele(l);
510
511	return 0;
512}
513
514int
515copyout(const void *kaddr, void *udaddr, size_t len)
516{
517	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
518	int msr, pid, tmp, ctx, count=0;
519	struct faultbuf env;
520
521	/* For big copies use more efficient routine */
522	if (len > 1024)
523		return (bigcopyout(kaddr, udaddr, len));
524
525	if (setfault(&env)) {
526		curpcb->pcb_onfault = 0;
527		return EFAULT;
528	}
529
530	if (!(ctx = pm->pm_ctx)) {
531		/* No context -- assign it one */
532		ctx_alloc(pm);
533		ctx = pm->pm_ctx;
534	}
535
536	__asm volatile(
537		"   mfmsr %[msr];"          /* Save MSR */ \
538		"   li %[pid],0x20; " \
539		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */ \
540		"   mfpid %[pid];"          /* Save old PID */ \
541		"   sync; isync;"
542
543		"   srwi. %[count],%[len],0x2;"     /* How many words? */
544		"   beq-  2f;"              /* No words. Go do bytes */
545		"   mtctr %[count];"
546		"1: mtpid %[pid];sync;"
547		"   lswi %[tmp],%[kaddr],4;"        /* Load kernel word */
548		"   addi %[kaddr],%[kaddr],0x4;"    /* next kaddr word */
549		"   sync; isync;"
550		"   mtpid %[ctx]; sync;"
551		"   stswi %[tmp],%[udaddr],4;"       /* Store user word */
552		"   dcbf 0,%[udaddr];"          /* flush cache */
553		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
554		"   sync; isync;"
555		"   bdnz 1b;"               /* repeat */
556
557		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
558		"   addi %[count],%[count],0x1;"
559		"   mtctr %[count];"
560		"3: bdz  10f;"              /* while count */
561		"   mtpid %[pid];sync;"
562		"   lbz %[tmp],0(%[kaddr]);"        /* Load kernel byte */
563		"   addi %[kaddr],%[kaddr],0x1;"    /* next kaddr byte */
564		"   sync; isync;"
565		"   mtpid %[ctx]; sync;"
566		"   stb %[tmp],0(%[udaddr]);"       /* Store user byte */
567		"   dcbf 0,%[udaddr];"          /* flush cache */
568		"   addi %[udaddr],%[udaddr],0x1;"
569		"   sync; isync;"
570		"   b 3b;"
571		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
572		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
573		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
574
575	curpcb->pcb_onfault = 0;
576	return 0;
577}
578
579static int
580bigcopyout(const void *kaddr, void *udaddr, size_t len)
581{
582	char *up;
583	const char *kp = (const char *)kaddr;
584	struct lwp *l = curlwp;
585	struct proc *p;
586	int error;
587
588	if (!l) {
589		return EFAULT;
590	}
591
592	p = l->l_proc;
593
594	/*
595	 * Stolen from physio():
596	 */
597	uvm_lwp_hold(l);
598	error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
599	if (error) {
600		uvm_lwp_rele(l);
601		return EFAULT;
602	}
603	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
604	    VM_PROT_READ | VM_PROT_WRITE);
605
606	memcpy(up, kp, len);
607	vunmaprange((vaddr_t)up, len);
608	uvm_vsunlock(p->p_vmspace, udaddr, len);
609	uvm_lwp_rele(l);
610
611	return 0;
612}
613
614/*
615 * kcopy(const void *src, void *dst, size_t len);
616 *
617 * Copy len bytes from src to dst, aborting if we encounter a fatal
618 * page fault.
619 *
620 * kcopy() _must_ save and restore the old fault handler since it is
621 * called by uiomove(), which may be in the path of servicing a non-fatal
622 * page fault.
623 */
624int
625kcopy(const void *src, void *dst, size_t len)
626{
627	struct faultbuf env, *oldfault;
628
629	oldfault = curpcb->pcb_onfault;
630	if (setfault(&env)) {
631		curpcb->pcb_onfault = oldfault;
632		return EFAULT;
633	}
634
635	memcpy(dst, src, len);
636
637	curpcb->pcb_onfault = oldfault;
638	return 0;
639}
640
641int
642badaddr(void *addr, size_t size)
643{
644
645	return badaddr_read(addr, size, NULL);
646}
647
648int
649badaddr_read(void *addr, size_t size, int *rptr)
650{
651	struct faultbuf env;
652	int x;
653
654	/* Get rid of any stale machine checks that have been waiting.  */
655	__asm volatile ("sync; isync");
656
657	if (setfault(&env)) {
658		curpcb->pcb_onfault = 0;
659		__asm volatile ("sync");
660		return 1;
661	}
662
663	__asm volatile ("sync");
664
665	switch (size) {
666	case 1:
667		x = *(volatile int8_t *)addr;
668		break;
669	case 2:
670		x = *(volatile int16_t *)addr;
671		break;
672	case 4:
673		x = *(volatile int32_t *)addr;
674		break;
675	default:
676		panic("badaddr: invalid size (%d)", size);
677	}
678
679	/* Make sure we took the machine check, if we caused one. */
680	__asm volatile ("sync; isync");
681
682	curpcb->pcb_onfault = 0;
683	__asm volatile ("sync");	/* To be sure. */
684
685	/* Use the value to avoid reorder. */
686	if (rptr)
687		*rptr = x;
688
689	return 0;
690}
691
692/*
693 * For now, this only deals with the particular unaligned access case
694 * that gcc tends to generate.  Eventually it should handle all of the
695 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
696 */
697
698static int
699fix_unaligned(struct lwp *l, struct trapframe *frame)
700{
701
702	return -1;
703}
704
705/*
706 * Start a new LWP
707 */
708void
709startlwp(arg)
710	void *arg;
711{
712	int err;
713	ucontext_t *uc = arg;
714	struct lwp *l = curlwp;
715
716	err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
717#if DIAGNOSTIC
718	if (err) {
719		printf("Error %d from cpu_setmcontext.", err);
720	}
721#endif
722	pool_put(&lwp_uc_pool, uc);
723
724	upcallret(l);
725}
726
727/*
728 * XXX This is a terrible name.
729 */
730void
731upcallret(l)
732	struct lwp *l;
733{
734
735	/* Invoke MI userret code */
736	mi_userret(l);
737}
738