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