Home | History | Annotate | Line # | Download | only in ibm4xx
trap.c revision 1.3.8.1
      1 /*	$NetBSD: trap.c,v 1.3.8.1 2002/07/15 00:33:10 gehenna 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 "opt_altivec.h"
     70 #include "opt_ddb.h"
     71 #include "opt_ktrace.h"
     72 #include "opt_systrace.h"
     73 #include "opt_syscall_debug.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/proc.h>
     77 #include <sys/reboot.h>
     78 #include <sys/syscall.h>
     79 #include <sys/systm.h>
     80 #include <sys/user.h>
     81 #ifdef KTRACE
     82 #include <sys/ktrace.h>
     83 #endif
     84 #ifdef SYSTRACE
     85 #include <sys/systrace.h>
     86 #endif
     87 
     88 #include <uvm/uvm_extern.h>
     89 
     90 #include <dev/cons.h>
     91 
     92 #include <machine/cpu.h>
     93 #include <machine/db_machdep.h>
     94 #include <machine/fpu.h>
     95 #include <machine/frame.h>
     96 #include <machine/pcb.h>
     97 #include <machine/psl.h>
     98 #include <machine/trap.h>
     99 
    100 #include <powerpc/spr.h>
    101 #include <powerpc/ibm4xx/pmap.h>
    102 #include <powerpc/ibm4xx/tlb.h>
    103 #include <powerpc/fpu/fpu_extern.h>
    104 
    105 /* These definitions should probably be somewhere else			XXX */
    106 #define	FIRSTARG	3		/* first argument is in reg 3 */
    107 #define	NARGREG		8		/* 8 args are in registers */
    108 #define	MOREARGS(sp)	((caddr_t)((int)(sp) + 8)) /* more args go here */
    109 
    110 #ifndef MULTIPROCESSOR
    111 volatile int astpending;
    112 volatile int want_resched;
    113 #endif
    114 
    115 void *syscall = NULL;	/* XXX dummy symbol for emul_netbsd */
    116 
    117 static int fix_unaligned __P((struct proc *p, struct trapframe *frame));
    118 
    119 void trap __P((struct trapframe *));	/* Called from locore / trap_subr */
    120 int setfault __P((faultbuf));	/* defined in locore.S */
    121 /* Why are these not defined in a header? */
    122 int badaddr __P((void *, size_t));
    123 int badaddr_read __P((void *, size_t, int *));
    124 int ctx_setup __P((int, int));
    125 
    126 #ifdef DEBUG
    127 #define TDB_ALL	0x1
    128 int trapdebug = /* TDB_ALL */ 0;
    129 #define	DBPRINTF(x, y)	if (trapdebug & (x)) printf y
    130 #else
    131 #define DBPRINTF(x, y)
    132 #endif
    133 
    134 void
    135 trap(struct trapframe *frame)
    136 {
    137 	struct proc *p = curproc;
    138 	int type = frame->exc;
    139 	int ftype, rv;
    140 
    141 	KASSERT(p == 0 || (p->p_stat == SONPROC));
    142 
    143 	if (frame->srr1 & PSL_PR)
    144 		type |= EXC_USER;
    145 
    146 	ftype = VM_PROT_READ;
    147 
    148 DBPRINTF(TDB_ALL, ("trap(%x) at %x 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 __asm __volatile("mfspr %0,0x3f0" : "=r" (rv), "=r" (srr2), "=r" (srr3) :);
    156 printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2, srr3);
    157 }
    158 		/*
    159 		 * DEBUG intr -- probably single-step.
    160 		 */
    161 	case EXC_TRC|EXC_USER:
    162 		KERNEL_PROC_LOCK(p);
    163 		frame->srr1 &= ~PSL_SE;
    164 		trapsignal(p, SIGTRAP, EXC_TRC);
    165 		KERNEL_PROC_UNLOCK(p);
    166 		break;
    167 
    168 	  /* If we could not find and install appropriate TLB entry, fall through */
    169 
    170 	case EXC_DSI:
    171 		/* FALLTHROUGH */
    172 	case EXC_DTMISS:
    173 		{
    174 			struct vm_map *map;
    175 			vaddr_t va;
    176 			faultbuf *fb = NULL;
    177 
    178 			KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE);
    179 			va = frame->dear;
    180 			if (frame->pid == KERNEL_PID) {
    181 				map = kernel_map;
    182 			} else {
    183 				map = &p->p_vmspace->vm_map;
    184 			}
    185 
    186 			if (frame->esr & (ESR_DST|ESR_DIZ))
    187 				ftype = VM_PROT_WRITE;
    188 
    189 DBPRINTF(TDB_ALL, ("trap(EXC_DSI) at %x %s fault on %p esr %x\n",
    190 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", (void *)va, frame->esr));
    191 			rv = uvm_fault(map, trunc_page(va), 0, ftype);
    192 			KERNEL_UNLOCK();
    193 			if (rv == 0)
    194 				goto done;
    195 			if ((fb = p->p_addr->u_pcb.pcb_onfault) != NULL) {
    196 				frame->pid = KERNEL_PID;
    197 				frame->srr0 = (*fb)[0];
    198 				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
    199 				frame->fixreg[1] = (*fb)[1];
    200 				frame->fixreg[2] = (*fb)[2];
    201 				frame->fixreg[3] = 1; /* Return TRUE */
    202 				frame->cr = (*fb)[3];
    203 				memcpy(&frame->fixreg[13], &(*fb)[4],
    204 				      19 * sizeof(register_t));
    205 				goto done;
    206 			}
    207 		}
    208 		goto brain_damage;
    209 
    210 	case EXC_DSI|EXC_USER:
    211 		/* FALLTHROUGH */
    212 	case EXC_DTMISS|EXC_USER:
    213 		KERNEL_PROC_LOCK(p);
    214 
    215 		if (frame->esr & (ESR_DST|ESR_DIZ))
    216 			ftype = VM_PROT_WRITE;
    217 
    218 DBPRINTF(TDB_ALL, ("trap(EXC_DSI|EXC_USER) at %x %s fault on %x %x\n",
    219 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->dear, frame->esr));
    220 KASSERT(p == curproc && (p->p_stat == SONPROC));
    221 		rv = uvm_fault(&p->p_vmspace->vm_map,
    222 			       trunc_page(frame->dear), 0, ftype);
    223 		if (rv == 0) {
    224 		  KERNEL_PROC_UNLOCK(p);
    225 		  break;
    226 		}
    227 		if (rv == ENOMEM) {
    228 			printf("UVM: pid %d (%s), uid %d killed: "
    229 			       "out of swap\n",
    230 			       p->p_pid, p->p_comm,
    231 			       p->p_cred && p->p_ucred ?
    232 			       p->p_ucred->cr_uid : -1);
    233 			trapsignal(p, SIGKILL, EXC_DSI);
    234 		} else {
    235 			trapsignal(p, SIGSEGV, EXC_DSI);
    236 		}
    237 		KERNEL_PROC_UNLOCK(p);
    238 		break;
    239 	case EXC_ITMISS|EXC_USER:
    240 	case EXC_ISI|EXC_USER:
    241 		KERNEL_PROC_LOCK(p);
    242 		ftype = VM_PROT_READ | VM_PROT_EXECUTE;
    243 DBPRINTF(TDB_ALL, ("trap(EXC_ISI|EXC_USER) at %x %s fault on %x tf %p\n",
    244 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->srr0, frame));
    245 		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0), 0, ftype);
    246 		if (rv == 0) {
    247 		  KERNEL_PROC_UNLOCK(p);
    248 		  break;
    249 		}
    250 		trapsignal(p, SIGSEGV, EXC_ISI);
    251 		KERNEL_PROC_UNLOCK(p);
    252 		break;
    253 	case EXC_SC|EXC_USER:
    254 		{
    255 			const struct sysent *callp;
    256 			size_t argsize;
    257 			register_t code, error;
    258 			register_t *params, rval[2];
    259 			int n;
    260 			register_t args[10];
    261 
    262 			KERNEL_PROC_LOCK(p);
    263 
    264 			uvmexp.syscalls++;
    265 
    266 			code = frame->fixreg[0];
    267 			callp = p->p_emul->e_sysent;
    268 			params = frame->fixreg + FIRSTARG;
    269 			n = NARGREG;
    270 
    271 			switch (code) {
    272 			case SYS_syscall:
    273 				/*
    274 				 * code is first argument,
    275 				 * followed by actual args.
    276 				 */
    277 				code = *params++;
    278 				n -= 1;
    279 				break;
    280 			case SYS___syscall:
    281 				params++;
    282 				code = *params++;
    283 				n -= 2;
    284 				break;
    285 			default:
    286 				break;
    287 			}
    288 
    289 			code &= (SYS_NSYSENT - 1);
    290 			callp += code;
    291 			argsize = callp->sy_argsize;
    292 
    293 			if (argsize > n * sizeof(register_t)) {
    294 				memcpy(args, params, n * sizeof(register_t));
    295 				error = copyin(MOREARGS(frame->fixreg[1]),
    296 					       args + n,
    297 					       argsize - n * sizeof(register_t));
    298 				if (error)
    299 					goto syscall_bad;
    300 				params = args;
    301 			}
    302 
    303 
    304 			if ((error = trace_enter(p, code, args, rval)) != 0)
    305 				goto syscall_bad;
    306 
    307 			rval[0] = 0;
    308 			rval[1] = 0;
    309 
    310 			error = (*callp->sy_call)(p, params, rval);
    311 			switch (error) {
    312 			case 0:
    313 				frame->fixreg[FIRSTARG] = rval[0];
    314 				frame->fixreg[FIRSTARG + 1] = rval[1];
    315 				frame->cr &= ~0x10000000;
    316 				break;
    317 			case ERESTART:
    318 				/*
    319 				 * Set user's pc back to redo the system call.
    320 				 */
    321 				frame->srr0 -= 4;
    322 				break;
    323 			case EJUSTRETURN:
    324 				/* nothing to do */
    325 				break;
    326 			default:
    327 syscall_bad:
    328 				if (p->p_emul->e_errno)
    329 					error = p->p_emul->e_errno[error];
    330 				frame->fixreg[FIRSTARG] = error;
    331 				frame->cr |= 0x10000000;
    332 				break;
    333 			}
    334 		}
    335 		KERNEL_PROC_UNLOCK(p);
    336 
    337 		trace_exit(p, code, args, rval, error);
    338 
    339 		break;
    340 
    341 	case EXC_AST|EXC_USER:
    342 		astpending = 0;		/* we are about to do it */
    343 		KERNEL_PROC_LOCK(p);
    344 		uvmexp.softs++;
    345 		if (p->p_flag & P_OWEUPC) {
    346 			p->p_flag &= ~P_OWEUPC;
    347 			ADDUPROF(p);
    348 		}
    349 		/* Check whether we are being preempted. */
    350 		if (want_resched)
    351 			preempt(NULL);
    352 		KERNEL_PROC_UNLOCK(p);
    353 		break;
    354 
    355 
    356 	case EXC_ALI|EXC_USER:
    357 		KERNEL_PROC_LOCK(p);
    358 		if (fix_unaligned(p, frame) != 0)
    359 			trapsignal(p, SIGBUS, EXC_ALI);
    360 		else
    361 			frame->srr0 += 4;
    362 		KERNEL_PROC_UNLOCK(p);
    363 		break;
    364 
    365 	case EXC_PGM|EXC_USER:
    366 		/*
    367 		 * Illegal insn:
    368 		 *
    369 		 * let's try to see if it's FPU and can be emulated.
    370 		 */
    371 		uvmexp.traps ++;
    372 		if (!(p->p_addr->u_pcb.pcb_flags & PCB_FPU)) {
    373 			memset(&p->p_addr->u_pcb.pcb_fpu, 0,
    374 				sizeof p->p_addr->u_pcb.pcb_fpu);
    375 			p->p_addr->u_pcb.pcb_flags |= PCB_FPU;
    376 		}
    377 
    378 		if ((rv = fpu_emulate(frame,
    379 			(struct fpreg *)&p->p_addr->u_pcb.pcb_fpu))) {
    380 			KERNEL_PROC_LOCK(p);
    381 			trapsignal(p, rv, EXC_PGM);
    382 			KERNEL_PROC_UNLOCK(p);
    383 		}
    384 		break;
    385 
    386 	case EXC_MCHK:
    387 		{
    388 			faultbuf *fb;
    389 
    390 			if ((fb = p->p_addr->u_pcb.pcb_onfault) != NULL) {
    391 				frame->pid = KERNEL_PID;
    392 				frame->srr0 = (*fb)[0];
    393 				frame->srr1 |= PSL_IR; /* Re-enable IMMU */
    394 				frame->fixreg[1] = (*fb)[1];
    395 				frame->fixreg[2] = (*fb)[2];
    396 				frame->fixreg[3] = 1; /* Return TRUE */
    397 				frame->cr = (*fb)[3];
    398 				memcpy(&frame->fixreg[13], &(*fb)[4],
    399 				      19 * sizeof(register_t));
    400 				goto done;
    401 			}
    402 		}
    403 		goto brain_damage;
    404 	default:
    405 brain_damage:
    406 		printf("trap type 0x%x at 0x%x\n", type, frame->srr0);
    407 #ifdef DDB
    408 		if (kdb_trap(type, frame))
    409 			goto done;
    410 #endif
    411 #ifdef TRAP_PANICWAIT
    412 		printf("Press a key to panic.\n");
    413 		cngetc();
    414 #endif
    415 		panic("trap");
    416 	}
    417 
    418 	/* Take pending signals. */
    419 	{
    420 		int sig;
    421 
    422 		while ((sig = CURSIG(p)) != 0)
    423 			postsig(sig);
    424 	}
    425 
    426 	curcpu()->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri;
    427   done:
    428 }
    429 
    430 int
    431 ctx_setup(int ctx, int srr1)
    432 {
    433 	volatile struct pmap *pm;
    434 
    435 	/* Update PID if we're returning to user mode. */
    436 	if (srr1 & PSL_PR) {
    437 		pm = curproc->p_vmspace->vm_map.pmap;
    438 		if (!pm->pm_ctx) {
    439 			ctx_alloc((struct pmap *)pm);
    440 		}
    441 		ctx = pm->pm_ctx;
    442 		if (srr1 & PSL_SE) {
    443 			int dbreg, mask = 0x48000000;
    444 				/*
    445 				 * Set the Internal Debug and
    446 				 * Instruction Completion bits of
    447 				 * the DBCR0 register.
    448 				 *
    449 				 * XXX this is also used by jtag debuggers...
    450 				 */
    451 			__asm __volatile("mfspr %0,0x3f2;"
    452 				"or %0,%0,%1;"
    453 				"mtspr 0x3f2,%0;" :
    454 				"=&r" (dbreg) : "r" (mask));
    455 		}
    456 	}
    457 	else if (!ctx) {
    458 		ctx = KERNEL_PID;
    459 	}
    460 	return (ctx);
    461 }
    462 
    463 void
    464 child_return(void *arg)
    465 {
    466 	struct proc *p = arg;
    467 	struct trapframe *tf = trapframe(p);
    468 
    469 	KERNEL_PROC_UNLOCK(p);
    470 
    471 	tf->fixreg[FIRSTARG] = 0;
    472 	tf->fixreg[FIRSTARG + 1] = 1;
    473 	tf->cr &= ~0x10000000;
    474 	tf->srr1 &= ~(PSL_FP|PSL_VEC);	/* Disable FP & AltiVec, as we can't be them */
    475 #ifdef	KTRACE
    476 	if (KTRPOINT(p, KTR_SYSRET)) {
    477 		KERNEL_PROC_LOCK(p);
    478 		ktrsysret(p, SYS_fork, 0, 0);
    479 		KERNEL_PROC_UNLOCK(p);
    480 	}
    481 #endif
    482 	/* Profiling?							XXX */
    483 	curcpu()->ci_schedstate.spc_curpriority = p->p_priority;
    484 }
    485 
    486 /*
    487  * Used by copyin()/copyout()
    488  */
    489 extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
    490 extern void vunmaprange __P((vaddr_t, vsize_t));
    491 static int bigcopyin __P((const void *,	void *,	size_t ));
    492 static int bigcopyout __P((const void *, void *, size_t ));
    493 
    494 int
    495 copyin(const void *udaddr, void *kaddr, size_t len)
    496 {
    497 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    498 	int msr, pid, tmp, ctx;
    499 	faultbuf env;
    500 
    501 	/* For bigger buffers use the faster copy */
    502 	if (len > 256) return (bigcopyin(udaddr, kaddr, len));
    503 
    504 	if (setfault(env)) {
    505 		curpcb->pcb_onfault = 0;
    506 		return EFAULT;
    507 	}
    508 
    509 	if (!(ctx = pm->pm_ctx)) {
    510 		/* No context -- assign it one */
    511 		ctx_alloc(pm);
    512 		ctx = pm->pm_ctx;
    513 	}
    514 
    515 	asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
    516 		"mfmsr %0;"			/* Save MSR */
    517 		"li %1,0x20; "
    518 		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
    519 		"mfpid %1;"			/* Save old PID */
    520 		"sync; isync;"
    521 
    522 		"1: bdz 2f;"			/* while len */
    523 		"mtpid %3; sync;"		/* Load user ctx */
    524 		"lbz %2,0(%4); addi %4,%4,1;"	/* Load byte */
    525 		"sync; isync;"
    526 		"mtpid %1;sync;"
    527 		"stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;"	/* Store kernel byte */
    528 		"sync; isync;"
    529 		"b 1b;"				/* repeat */
    530 
    531 		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
    532 		"sync; isync;"
    533 		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
    534 		: "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
    535 
    536 	curpcb->pcb_onfault = 0;
    537 	return 0;
    538 }
    539 
    540 static int
    541 bigcopyin(const void *udaddr, void *kaddr, size_t len)
    542 {
    543 	const char *up;
    544 	char *kp = kaddr;
    545 	struct proc *p = curproc;
    546 	int error;
    547 
    548 	if (!p) {
    549 		return EFAULT;
    550 	}
    551 
    552 	/*
    553 	 * Stolen from physio():
    554 	 */
    555 	PHOLD(p);
    556 	error = uvm_vslock(p, (caddr_t)udaddr, len, VM_PROT_READ);
    557 	if (error) {
    558 		PRELE(p);
    559 		return EFAULT;
    560 	}
    561 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
    562 
    563 	memcpy(kp, up, len);
    564 	vunmaprange((vaddr_t)up, len);
    565 	uvm_vsunlock(p, (caddr_t)udaddr, len);
    566 	PRELE(p);
    567 
    568 	return 0;
    569 }
    570 
    571 int
    572 copyout(const void *kaddr, void *udaddr, size_t len)
    573 {
    574 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    575 	int msr, pid, tmp, ctx;
    576 	faultbuf env;
    577 
    578 	/* For big copies use more efficient routine */
    579 	if (len > 256) return (bigcopyout(kaddr, udaddr, len));
    580 
    581 	if (setfault(env)) {
    582 		curpcb->pcb_onfault = 0;
    583 		return EFAULT;
    584 	}
    585 
    586 	if (!(ctx = pm->pm_ctx)) {
    587 		/* No context -- assign it one */
    588 		ctx_alloc(pm);
    589 		ctx = pm->pm_ctx;
    590 	}
    591 
    592 	asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
    593 		"mfmsr %0;"			/* Save MSR */
    594 		"li %1,0x20; "
    595 		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
    596 		"mfpid %1;"			/* Save old PID */
    597 		"sync; isync;"
    598 
    599 		"1: bdz 2f;"			/* while len */
    600 		"mtpid %1;sync;"
    601 		"lbz %2,0(%5); addi %5,%5,1;"	/* Load kernel byte */
    602 		"sync; isync;"
    603 		"mtpid %3; sync;"		/* Load user ctx */
    604 		"stb %2,0(%4);  dcbf 0,%4; addi %4,%4,1;"	/* Store user byte */
    605 		"sync; isync;"
    606 		"b 1b;"				/* repeat */
    607 
    608 		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
    609 		"sync; isync;"
    610 		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
    611 		: "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
    612 
    613 	curpcb->pcb_onfault = 0;
    614 	return 0;
    615 }
    616 
    617 static int
    618 bigcopyout(const void *kaddr, void *udaddr, size_t len)
    619 {
    620 	char *up;
    621 	const char *kp = (char *)kaddr;
    622 	struct proc *p = curproc;
    623 	int error;
    624 
    625 	if (!p) {
    626 		return EFAULT;
    627 	}
    628 
    629 	/*
    630 	 * Stolen from physio():
    631 	 */
    632 	PHOLD(p);
    633 	error = uvm_vslock(p, udaddr, len, VM_PROT_WRITE);
    634 	if (error) {
    635 		PRELE(p);
    636 		return EFAULT;
    637 	}
    638 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
    639 		VM_PROT_READ|VM_PROT_WRITE);
    640 
    641 	memcpy(up, kp, len);
    642 	vunmaprange((vaddr_t)up, len);
    643 	uvm_vsunlock(p, udaddr, len);
    644 	PRELE(p);
    645 
    646 	return 0;
    647 }
    648 
    649 /*
    650  * kcopy(const void *src, void *dst, size_t len);
    651  *
    652  * Copy len bytes from src to dst, aborting if we encounter a fatal
    653  * page fault.
    654  *
    655  * kcopy() _must_ save and restore the old fault handler since it is
    656  * called by uiomove(), which may be in the path of servicing a non-fatal
    657  * page fault.
    658  */
    659 int
    660 kcopy(const void *src, void *dst, size_t len)
    661 {
    662 	faultbuf env, *oldfault;
    663 
    664 	oldfault = curpcb->pcb_onfault;
    665 	if (setfault(env)) {
    666 		curpcb->pcb_onfault = oldfault;
    667 		return EFAULT;
    668 	}
    669 
    670 	memcpy(dst, src, len);
    671 
    672 	curpcb->pcb_onfault = oldfault;
    673 	return 0;
    674 }
    675 
    676 int
    677 badaddr(void *addr, size_t size)
    678 {
    679 
    680 	return badaddr_read(addr, size, NULL);
    681 }
    682 
    683 int
    684 badaddr_read(void *addr, size_t size, int *rptr)
    685 {
    686 	faultbuf env;
    687 	int x;
    688 
    689 	/* Get rid of any stale machine checks that have been waiting.  */
    690 	__asm __volatile ("sync; isync");
    691 
    692 	if (setfault(env)) {
    693 		curpcb->pcb_onfault = 0;
    694 		__asm __volatile ("sync");
    695 		return 1;
    696 	}
    697 
    698 	__asm __volatile ("sync");
    699 
    700 	switch (size) {
    701 	case 1:
    702 		x = *(volatile int8_t *)addr;
    703 		break;
    704 	case 2:
    705 		x = *(volatile int16_t *)addr;
    706 		break;
    707 	case 4:
    708 		x = *(volatile int32_t *)addr;
    709 		break;
    710 	default:
    711 		panic("badaddr: invalid size (%d)", size);
    712 	}
    713 
    714 	/* Make sure we took the machine check, if we caused one. */
    715 	__asm __volatile ("sync; isync");
    716 
    717 	curpcb->pcb_onfault = 0;
    718 	__asm __volatile ("sync");	/* To be sure. */
    719 
    720 	/* Use the value to avoid reorder. */
    721 	if (rptr)
    722 		*rptr = x;
    723 
    724 	return 0;
    725 }
    726 
    727 /*
    728  * For now, this only deals with the particular unaligned access case
    729  * that gcc tends to generate.  Eventually it should handle all of the
    730  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
    731  */
    732 
    733 static int
    734 fix_unaligned(struct proc *p, struct trapframe *frame)
    735 {
    736 
    737 	return -1;
    738 }
    739