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