trap.c revision 1.61
1/*	$NetBSD: trap.c,v 1.61 2011/06/18 06:41:41 matt 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.61 2011/06/18 06:41:41 matt 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/sa.h>
82#include <sys/savar.h>
83#include <sys/userret.h>
84#include <sys/kauth.h>
85#include <sys/cpu.h>
86
87#if defined(KGDB)
88#include <sys/kgdb.h>
89#endif
90
91#include <uvm/uvm_extern.h>
92
93#include <dev/cons.h>
94
95#include <machine/fpu.h>
96#include <machine/frame.h>
97#include <machine/pcb.h>
98#include <machine/psl.h>
99#include <machine/trap.h>
100
101#include <powerpc/db_machdep.h>
102#include <powerpc/spr.h>
103#include <powerpc/ibm4xx/spr.h>
104
105#include <powerpc/ibm4xx/cpu.h>
106#include <powerpc/ibm4xx/pmap.h>
107#include <powerpc/ibm4xx/tlb.h>
108
109#include <powerpc/fpu/fpu_extern.h>
110
111/* These definitions should probably be somewhere else			XXX */
112#define	FIRSTARG	3		/* first argument is in reg 3 */
113#define	NARGREG		8		/* 8 args are in registers */
114#define	MOREARGS(sp)	((void *)((int)(sp) + 8)) /* more args go here */
115
116static int fix_unaligned(struct lwp *l, struct trapframe *frame);
117
118void trap(struct trapframe *);	/* Called from locore / trap_subr */
119/* Why are these not defined in a header? */
120int badaddr(void *, size_t);
121int badaddr_read(void *, size_t, int *);
122int ctx_setup(int, int);
123
124#ifdef DEBUG
125#define TDB_ALL	0x1
126int trapdebug = /* TDB_ALL */ 0;
127#define	DBPRINTF(x, y)	if (trapdebug & (x)) printf y
128#else
129#define DBPRINTF(x, y)
130#endif
131
132void
133trap(struct trapframe *tf)
134{
135	struct lwp *l = curlwp;
136	struct proc *p = l->l_proc;
137	struct pcb *pcb;
138	int type = tf->tf_exc;
139	int ftype, rv;
140	ksiginfo_t ksi;
141
142	KASSERT(l->l_stat == LSONPROC);
143
144	if (tf->tf_srr1 & PSL_PR) {
145		LWP_CACHE_CREDS(l, p);
146		type |= EXC_USER;
147	}
148
149	ftype = VM_PROT_READ;
150
151	DBPRINTF(TDB_ALL, ("trap(%x) at %lx from frame %p &frame %p\n",
152	    type, tf->tf_srr0, tf, &tf));
153
154	switch (type) {
155	case EXC_DEBUG|EXC_USER:
156		{
157			int srr2, srr3;
158
159			__asm volatile("mfspr %0,0x3f0" :
160			    "=r" (rv), "=r" (srr2), "=r" (srr3) :);
161			printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2,
162			    srr3);
163			/* XXX fall through or break here?! */
164		}
165		/*
166		 * DEBUG intr -- probably single-step.
167		 */
168	case EXC_TRC|EXC_USER:
169		tf->tf_srr1 &= ~PSL_SE;
170		KSI_INIT_TRAP(&ksi);
171		ksi.ksi_signo = SIGTRAP;
172		ksi.ksi_trap = EXC_TRC;
173		ksi.ksi_addr = (void *)tf->tf_srr0;
174		trapsignal(l, &ksi);
175		break;
176
177	/*
178	 * If we could not find and install appropriate TLB entry, fall through.
179	 */
180
181	case EXC_DSI:
182		/* FALLTHROUGH */
183	case EXC_DTMISS:
184		{
185			struct vm_map *map;
186			vaddr_t va;
187			struct faultbuf *fb = NULL;
188
189			va = tf->tf_dear;
190			if (tf->tf_pid == KERNEL_PID) {
191				map = kernel_map;
192			} else {
193				map = &p->p_vmspace->vm_map;
194				if ((l->l_flag & LW_SA)
195				    && (~l->l_pflag & LP_SA_NOBLOCK)) {
196					l->l_savp->savp_faultaddr = va;
197					l->l_pflag |= LP_SA_PAGEFAULT;
198				}
199			}
200
201			if (tf->tf_esr & (ESR_DST|ESR_DIZ))
202				ftype = VM_PROT_WRITE;
203
204			DBPRINTF(TDB_ALL,
205			    ("trap(EXC_DSI) at %lx %s fault on %p esr %x\n",
206			    tf->tf_srr0,
207			    (ftype & VM_PROT_WRITE) ? "write" : "read",
208			    (void *)va, tf->tf_esr));
209
210			pcb = lwp_getpcb(l);
211			fb = pcb->pcb_onfault;
212			pcb->pcb_onfault = NULL;
213			rv = uvm_fault(map, trunc_page(va), ftype);
214			pcb->pcb_onfault = fb;
215			if (map != kernel_map) {
216				l->l_pflag &= ~LP_SA_PAGEFAULT;
217			}
218			if (rv == 0)
219				goto done;
220			if (fb != NULL) {
221				tf->tf_pid = KERNEL_PID;
222				tf->tf_srr0 = fb->fb_pc;
223				tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
224				tf->tf_cr = fb->fb_cr;
225				tf->tf_fixreg[1] = fb->fb_sp;
226				tf->tf_fixreg[2] = fb->fb_r2;
227				tf->tf_fixreg[3] = 1; /* Return TRUE */
228				memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
229				    sizeof(fb->fb_fixreg));
230				goto done;
231			}
232		}
233		goto brain_damage;
234
235	case EXC_DSI|EXC_USER:
236		/* FALLTHROUGH */
237	case EXC_DTMISS|EXC_USER:
238		if (tf->tf_esr & (ESR_DST|ESR_DIZ))
239			ftype = VM_PROT_WRITE;
240
241		DBPRINTF(TDB_ALL,
242		    ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
243		    tf->tf_srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
244		    tf->tf_dear, tf->tf_esr));
245		KASSERT(l == curlwp && (l->l_stat == LSONPROC));
246		if (l->l_flag & LW_SA) {
247			l->l_savp->savp_faultaddr = (vaddr_t)tf->tf_dear;
248			l->l_pflag |= LP_SA_PAGEFAULT;
249		}
250//		KASSERT(curpcb->pcb_onfault == NULL);
251		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_dear),
252		    ftype);
253		if (rv == 0) {
254			l->l_pflag &= ~LP_SA_PAGEFAULT;
255			break;
256		}
257		KSI_INIT_TRAP(&ksi);
258		ksi.ksi_signo = SIGSEGV;
259		ksi.ksi_trap = EXC_DSI;
260		ksi.ksi_addr = (void *)tf->tf_dear;
261		if (rv == ENOMEM) {
262			printf("UVM: pid %d (%s) lid %d, uid %d killed: "
263			    "out of swap\n",
264			    p->p_pid, p->p_comm, l->l_lid,
265			    l->l_cred ?
266			    kauth_cred_geteuid(l->l_cred) : -1);
267			ksi.ksi_signo = SIGKILL;
268		}
269		trapsignal(l, &ksi);
270		l->l_pflag &= ~LP_SA_PAGEFAULT;
271		break;
272
273	case EXC_ITMISS|EXC_USER:
274	case EXC_ISI|EXC_USER:
275		if (l->l_flag & LW_SA) {
276			l->l_savp->savp_faultaddr = (vaddr_t)tf->tf_srr0;
277			l->l_pflag |= LP_SA_PAGEFAULT;
278		}
279		ftype = VM_PROT_EXECUTE;
280		DBPRINTF(TDB_ALL,
281		    ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
282		    tf->tf_srr0, tf));
283//		KASSERT(curpcb->pcb_onfault == NULL);
284		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_srr0),
285		    ftype);
286		if (rv == 0) {
287			l->l_pflag &= ~LP_SA_PAGEFAULT;
288			break;
289		}
290		KSI_INIT_TRAP(&ksi);
291		ksi.ksi_signo = SIGSEGV;
292		ksi.ksi_trap = EXC_ISI;
293		ksi.ksi_addr = (void *)tf->tf_srr0;
294		ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
295		trapsignal(l, &ksi);
296		l->l_pflag &= ~LP_SA_PAGEFAULT;
297		break;
298
299	case EXC_AST|EXC_USER:
300		l->l_md.md_astpending = 0;	/* we are about to do it */
301		//curcpu()->ci_data.cpu_nast++;
302		if (l->l_pflag & LP_OWEUPC) {
303			l->l_pflag &= ~LP_OWEUPC;
304			ADDUPROF(l);
305		}
306		/* Check whether we are being preempted. */
307		if (curcpu()->ci_want_resched)
308			preempt();
309		break;
310
311
312	case EXC_ALI|EXC_USER:
313		if (fix_unaligned(l, tf) != 0) {
314			KSI_INIT_TRAP(&ksi);
315			ksi.ksi_signo = SIGBUS;
316			ksi.ksi_trap = EXC_ALI;
317			ksi.ksi_addr = (void *)tf->tf_dear;
318			trapsignal(l, &ksi);
319		} else
320			tf->tf_srr0 += 4;
321		break;
322
323	case EXC_PGM|EXC_USER:
324		/*
325		 * Illegal insn:
326		 *
327		 * let's try to see if it's FPU and can be emulated.
328		 */
329		curcpu()->ci_data.cpu_ntrap++;
330		pcb = lwp_getpcb(l);
331
332		if (!(l->l_md.md_flags & MDLWP_USEDFPU)) {
333			memset(&pcb->pcb_fpu, 0, sizeof(pcb->pcb_fpu));
334			l->l_md.md_flags |= MDLWP_USEDFPU;
335		}
336
337		if ((rv = fpu_emulate(tf, (struct fpreg *)&pcb->pcb_fpu))) {
338			KSI_INIT_TRAP(&ksi);
339			ksi.ksi_signo = rv;
340			ksi.ksi_trap = EXC_PGM;
341			ksi.ksi_addr = (void *)tf->tf_srr0;
342			trapsignal(l, &ksi);
343		}
344		break;
345
346	case EXC_MCHK:
347		{
348			struct faultbuf *fb;
349
350			pcb = lwp_getpcb(l);
351			if ((fb = pcb->pcb_onfault) != NULL) {
352				tf->tf_pid = KERNEL_PID;
353				tf->tf_srr0 = fb->fb_pc;
354				tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
355				tf->tf_fixreg[1] = fb->fb_sp;
356				tf->tf_fixreg[2] = fb->fb_r2;
357				tf->tf_fixreg[3] = 1; /* Return TRUE */
358				tf->tf_cr = fb->fb_cr;
359				memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
360				    sizeof(fb->fb_fixreg));
361				goto done;
362			}
363		}
364		goto brain_damage;
365	default:
366 brain_damage:
367		printf("trap type 0x%x at 0x%lx\n", type, tf->tf_srr0);
368#if defined(DDB) || defined(KGDB)
369		if (kdb_trap(type, tf))
370			goto done;
371#endif
372#ifdef TRAP_PANICWAIT
373		printf("Press a key to panic.\n");
374		cngetc();
375#endif
376		panic("trap");
377	}
378
379	/* Invoke MI userret code */
380	mi_userret(l);
381 done:
382	return;
383}
384
385int
386ctx_setup(int ctx, int srr1)
387{
388	volatile struct pmap *pm;
389
390	/* Update PID if we're returning to user mode. */
391	if (srr1 & PSL_PR) {
392		pm = curproc->p_vmspace->vm_map.pmap;
393		if (!pm->pm_ctx) {
394			ctx_alloc(__UNVOLATILE(pm));
395		}
396		ctx = pm->pm_ctx;
397		if (srr1 & PSL_SE) {
398			int dbreg, mask = 0x48000000;
399				/*
400				 * Set the Internal Debug and
401				 * Instruction Completion bits of
402				 * the DBCR0 register.
403				 *
404				 * XXX this is also used by jtag debuggers...
405				 */
406			__asm volatile("mfspr %0,0x3f2;"
407			    "or %0,%0,%1;"
408			    "mtspr 0x3f2,%0;" :
409			    "=&r" (dbreg) : "r" (mask));
410		}
411	}
412	else if (!ctx) {
413		ctx = KERNEL_PID;
414	}
415	return (ctx);
416}
417
418/*
419 * Used by copyin()/copyout()
420 */
421extern vaddr_t vmaprange(struct proc *, vaddr_t, vsize_t, int);
422extern void vunmaprange(vaddr_t, vsize_t);
423static int bigcopyin(const void *, void *, size_t );
424static int bigcopyout(const void *, void *, size_t );
425
426int
427copyin(const void *udaddr, void *kaddr, size_t len)
428{
429	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
430	int rv, msr, pid, tmp, ctx, count = 0;
431	struct faultbuf env;
432
433	/* For bigger buffers use the faster copy */
434	if (len > 1024)
435		return (bigcopyin(udaddr, kaddr, len));
436
437	if ((rv = setfault(&env))) {
438		curpcb->pcb_onfault = NULL;
439		return rv;
440	}
441
442	if (!(ctx = pm->pm_ctx)) {
443		/* No context -- assign it one */
444		ctx_alloc(pm);
445		ctx = pm->pm_ctx;
446	}
447
448	__asm volatile(
449		"   mfmsr %[msr];"          /* Save MSR */
450		"   li %[pid],0x20; "
451		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */
452		"   mfpid %[pid];"          /* Save old PID */
453		"   sync; isync;"
454
455		"   srwi. %[count],%[len],0x2;"     /* How many words? */
456		"   beq-  2f;"              /* No words. Go do bytes */
457		"   mtctr %[count];"
458		"1: mtpid %[ctx]; sync;"
459		"   lswi %[tmp],%[udaddr],4;"       /* Load user word */
460		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
461		"   sync; isync;"
462		"   mtpid %[pid];sync;"
463		"   stswi %[tmp],%[kaddr],4;"        /* Store kernel word */
464		"   dcbf 0,%[kaddr];"           /* flush cache */
465		"   addi %[kaddr],%[kaddr],0x4;"    /* next udaddr word */
466		"   sync; isync;"
467		"   bdnz 1b;"               /* repeat */
468
469		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
470		"   addi %[count],%[count],0x1;"
471		"   mtctr %[count];"
472		"3: bdz 10f;"               /* while count */
473		"   mtpid %[ctx];sync;"
474		"   lbz %[tmp],0(%[udaddr]);"       /* Load user byte */
475		"   addi %[udaddr],%[udaddr],0x1;"  /* next udaddr byte */
476		"   sync; isync;"
477		"   mtpid %[pid]; sync;"
478		"   stb %[tmp],0(%[kaddr]);"        /* Store kernel byte */
479		"   dcbf 0,%[kaddr];"           /* flush cache */
480		"   addi %[kaddr],%[kaddr],0x1;"
481		"   sync; isync;"
482		"   b 3b;"
483		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
484		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
485		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
486
487	curpcb->pcb_onfault = NULL;
488	return 0;
489}
490
491static int
492bigcopyin(const void *udaddr, void *kaddr, size_t len)
493{
494	const char *up;
495	char *kp = kaddr;
496	struct lwp *l = curlwp;
497	struct proc *p;
498	struct faultbuf env;
499	int error;
500
501	p = l->l_proc;
502
503	/*
504	 * Stolen from physio():
505	 */
506	error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
507	if (error) {
508		return error;
509	}
510	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
511
512	if ((error = setfault(&env)) == 0) {
513		memcpy(kp, up, len);
514	}
515
516	curpcb->pcb_onfault = NULL;
517	vunmaprange((vaddr_t)up, len);
518	uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
519
520	return error;
521}
522
523int
524copyout(const void *kaddr, void *udaddr, size_t len)
525{
526	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
527	int rv, msr, pid, tmp, ctx, count = 0;
528	struct faultbuf env;
529
530	/* For big copies use more efficient routine */
531	if (len > 1024)
532		return (bigcopyout(kaddr, udaddr, len));
533
534	if ((rv = setfault(&env))) {
535		curpcb->pcb_onfault = NULL;
536		return rv;
537	}
538
539	if (!(ctx = pm->pm_ctx)) {
540		/* No context -- assign it one */
541		ctx_alloc(pm);
542		ctx = pm->pm_ctx;
543	}
544
545	__asm volatile(
546		"   mfmsr %[msr];"          /* Save MSR */ \
547		"   li %[pid],0x20; " \
548		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */ \
549		"   mfpid %[pid];"          /* Save old PID */ \
550		"   sync; isync;"
551
552		"   srwi. %[count],%[len],0x2;"     /* How many words? */
553		"   beq-  2f;"              /* No words. Go do bytes */
554		"   mtctr %[count];"
555		"1: mtpid %[pid];sync;"
556		"   lswi %[tmp],%[kaddr],4;"        /* Load kernel word */
557		"   addi %[kaddr],%[kaddr],0x4;"    /* next kaddr word */
558		"   sync; isync;"
559		"   mtpid %[ctx]; sync;"
560		"   stswi %[tmp],%[udaddr],4;"       /* Store user word */
561		"   dcbf 0,%[udaddr];"          /* flush cache */
562		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
563		"   sync; isync;"
564		"   bdnz 1b;"               /* repeat */
565
566		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
567		"   addi %[count],%[count],0x1;"
568		"   mtctr %[count];"
569		"3: bdz  10f;"              /* while count */
570		"   mtpid %[pid];sync;"
571		"   lbz %[tmp],0(%[kaddr]);"        /* Load kernel byte */
572		"   addi %[kaddr],%[kaddr],0x1;"    /* next kaddr byte */
573		"   sync; isync;"
574		"   mtpid %[ctx]; sync;"
575		"   stb %[tmp],0(%[udaddr]);"       /* Store user byte */
576		"   dcbf 0,%[udaddr];"          /* flush cache */
577		"   addi %[udaddr],%[udaddr],0x1;"
578		"   sync; isync;"
579		"   b 3b;"
580		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
581		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
582		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
583
584	curpcb->pcb_onfault = NULL;
585	return 0;
586}
587
588static int
589bigcopyout(const void *kaddr, void *udaddr, size_t len)
590{
591	char *up;
592	const char *kp = (const char *)kaddr;
593	struct lwp *l = curlwp;
594	struct proc *p;
595	struct faultbuf env;
596	int error;
597
598	p = l->l_proc;
599
600	/*
601	 * Stolen from physio():
602	 */
603	error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
604	if (error) {
605		return error;
606	}
607	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
608	    VM_PROT_READ | VM_PROT_WRITE);
609
610	if ((error = setfault(&env)) == 0) {
611		memcpy(up, kp, len);
612	}
613
614	curpcb->pcb_onfault = NULL;
615	vunmaprange((vaddr_t)up, len);
616	uvm_vsunlock(p->p_vmspace, udaddr, len);
617
618	return error;
619}
620
621/*
622 * kcopy(const void *src, void *dst, size_t len);
623 *
624 * Copy len bytes from src to dst, aborting if we encounter a fatal
625 * page fault.
626 *
627 * kcopy() _must_ save and restore the old fault handler since it is
628 * called by uiomove(), which may be in the path of servicing a non-fatal
629 * page fault.
630 */
631int
632kcopy(const void *src, void *dst, size_t len)
633{
634	struct faultbuf env, *oldfault;
635	int rv;
636
637	oldfault = curpcb->pcb_onfault;
638	if ((rv = setfault(&env))) {
639		curpcb->pcb_onfault = oldfault;
640		return rv;
641	}
642
643	memcpy(dst, src, len);
644
645	curpcb->pcb_onfault = oldfault;
646	return 0;
647}
648
649int
650badaddr(void *addr, size_t size)
651{
652
653	return badaddr_read(addr, size, NULL);
654}
655
656int
657badaddr_read(void *addr, size_t size, int *rptr)
658{
659	struct faultbuf env;
660	int x;
661
662	/* Get rid of any stale machine checks that have been waiting.  */
663	__asm volatile ("sync; isync");
664
665	if (setfault(&env)) {
666		curpcb->pcb_onfault = NULL;
667		__asm volatile ("sync");
668		return 1;
669	}
670
671	__asm volatile ("sync");
672
673	switch (size) {
674	case 1:
675		x = *(volatile int8_t *)addr;
676		break;
677	case 2:
678		x = *(volatile int16_t *)addr;
679		break;
680	case 4:
681		x = *(volatile int32_t *)addr;
682		break;
683	default:
684		panic("badaddr: invalid size (%d)", size);
685	}
686
687	/* Make sure we took the machine check, if we caused one. */
688	__asm volatile ("sync; isync");
689
690	curpcb->pcb_onfault = NULL;
691	__asm volatile ("sync");	/* To be sure. */
692
693	/* Use the value to avoid reorder. */
694	if (rptr)
695		*rptr = x;
696
697	return 0;
698}
699
700/*
701 * For now, this only deals with the particular unaligned access case
702 * that gcc tends to generate.  Eventually it should handle all of the
703 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
704 */
705
706static int
707fix_unaligned(struct lwp *l, struct trapframe *tf)
708{
709
710	return -1;
711}
712