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