Home | History | Annotate | Line # | Download | only in ibm4xx
trap.c revision 1.43.12.1
      1 /*	$NetBSD: trap.c,v 1.43.12.1 2007/10/18 08:32:37 yamt 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.12.1 2007/10/18 08:32:37 yamt 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 
    364 	curcpu()->ci_schedstate.spc_curpriority = l->l_priority = l->l_usrpri;
    365  done:
    366 	return;
    367 }
    368 
    369 int
    370 ctx_setup(int ctx, int srr1)
    371 {
    372 	volatile struct pmap *pm;
    373 
    374 	/* Update PID if we're returning to user mode. */
    375 	if (srr1 & PSL_PR) {
    376 		pm = curproc->p_vmspace->vm_map.pmap;
    377 		if (!pm->pm_ctx) {
    378 			ctx_alloc(__UNVOLATILE(pm));
    379 		}
    380 		ctx = pm->pm_ctx;
    381 		if (srr1 & PSL_SE) {
    382 			int dbreg, mask = 0x48000000;
    383 				/*
    384 				 * Set the Internal Debug and
    385 				 * Instruction Completion bits of
    386 				 * the DBCR0 register.
    387 				 *
    388 				 * XXX this is also used by jtag debuggers...
    389 				 */
    390 			__asm volatile("mfspr %0,0x3f2;"
    391 			    "or %0,%0,%1;"
    392 			    "mtspr 0x3f2,%0;" :
    393 			    "=&r" (dbreg) : "r" (mask));
    394 		}
    395 	}
    396 	else if (!ctx) {
    397 		ctx = KERNEL_PID;
    398 	}
    399 	return (ctx);
    400 }
    401 
    402 /*
    403  * Used by copyin()/copyout()
    404  */
    405 extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
    406 extern void vunmaprange __P((vaddr_t, vsize_t));
    407 static int bigcopyin __P((const void *, void *, size_t ));
    408 static int bigcopyout __P((const void *, void *, size_t ));
    409 
    410 int
    411 copyin(const void *udaddr, void *kaddr, size_t len)
    412 {
    413 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    414 	int msr, pid, tmp, ctx;
    415 	struct faultbuf env;
    416 
    417 	/* For bigger buffers use the faster copy */
    418 	if (len > 256)
    419 		return (bigcopyin(udaddr, kaddr, len));
    420 
    421 	if (setfault(&env)) {
    422 		curpcb->pcb_onfault = 0;
    423 		return EFAULT;
    424 	}
    425 
    426 	if (!(ctx = pm->pm_ctx)) {
    427 		/* No context -- assign it one */
    428 		ctx_alloc(pm);
    429 		ctx = pm->pm_ctx;
    430 	}
    431 
    432 	__asm volatile("addi %6,%6,1; mtctr %6;"	/* Set up counter */
    433 		"mfmsr %0;"			/* Save MSR */
    434 		"li %1,0x20; "
    435 		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
    436 		"mfpid %1;"			/* Save old PID */
    437 		"sync; isync;"
    438 
    439 		"1: bdz 2f;"			/* while len */
    440 		"mtpid %3; sync;"		/* Load user ctx */
    441 		"lbz %2,0(%4); addi %4,%4,1;"	/* Load byte */
    442 		"sync; isync;"
    443 		"mtpid %1;sync;"
    444 		"stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;" /* Store kernel byte */
    445 		"sync; isync;"
    446 		"b 1b;"				/* repeat */
    447 
    448 		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
    449 		"sync; isync;"
    450 		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
    451 		: "r" (ctx), "b" (udaddr), "b" (kaddr), "b" (len));
    452 
    453 	curpcb->pcb_onfault = 0;
    454 	return 0;
    455 }
    456 
    457 static int
    458 bigcopyin(const void *udaddr, void *kaddr, size_t len)
    459 {
    460 	const char *up;
    461 	char *kp = kaddr;
    462 	struct lwp *l = curlwp;
    463 	struct proc *p;
    464 	int error;
    465 
    466 	if (!l) {
    467 		return EFAULT;
    468 	}
    469 
    470 	p = l->l_proc;
    471 
    472 	/*
    473 	 * Stolen from physio():
    474 	 */
    475 	uvm_lwp_hold(l);
    476 	error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
    477 	if (error) {
    478 		uvm_lwp_rele(l);
    479 		return EFAULT;
    480 	}
    481 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
    482 
    483 	memcpy(kp, up, len);
    484 	vunmaprange((vaddr_t)up, len);
    485 	uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
    486 	uvm_lwp_rele(l);
    487 
    488 	return 0;
    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 msr, pid, tmp, ctx;
    496 	struct faultbuf env;
    497 
    498 	/* For big copies use more efficient routine */
    499 	if (len > 256)
    500 		return (bigcopyout(kaddr, udaddr, len));
    501 
    502 	if (setfault(&env)) {
    503 		curpcb->pcb_onfault = 0;
    504 		return EFAULT;
    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("addi %6,%6,1; mtctr %6;"	/* Set up counter */
    514 		"mfmsr %0;"			/* Save MSR */
    515 		"li %1,0x20; "
    516 		"andc %1,%0,%1; mtmsr %1;"	/* Disable IMMU */
    517 		"mfpid %1;"			/* Save old PID */
    518 		"sync; isync;"
    519 
    520 		"1: bdz 2f;"			/* while len */
    521 		"mtpid %1;sync;"
    522 		"lbz %2,0(%5); addi %5,%5,1;"	/* Load kernel byte */
    523 		"sync; isync;"
    524 		"mtpid %3; sync;"		/* Load user ctx */
    525 		"stb %2,0(%4);  dcbf 0,%4; addi %4,%4,1;" /* Store user byte */
    526 		"sync; isync;"
    527 		"b 1b;"				/* repeat */
    528 
    529 		"2: mtpid %1; mtmsr %0;"	/* Restore PID and MSR */
    530 		"sync; isync;"
    531 		: "=&r" (msr), "=&r" (pid), "=&r" (tmp)
    532 		: "r" (ctx), "b" (udaddr), "b" (kaddr), "b" (len));
    533 
    534 	curpcb->pcb_onfault = 0;
    535 	return 0;
    536 }
    537 
    538 static int
    539 bigcopyout(const void *kaddr, void *udaddr, size_t len)
    540 {
    541 	char *up;
    542 	const char *kp = (const char *)kaddr;
    543 	struct lwp *l = curlwp;
    544 	struct proc *p;
    545 	int error;
    546 
    547 	if (!l) {
    548 		return EFAULT;
    549 	}
    550 
    551 	p = l->l_proc;
    552 
    553 	/*
    554 	 * Stolen from physio():
    555 	 */
    556 	uvm_lwp_hold(l);
    557 	error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
    558 	if (error) {
    559 		uvm_lwp_rele(l);
    560 		return EFAULT;
    561 	}
    562 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
    563 	    VM_PROT_READ | VM_PROT_WRITE);
    564 
    565 	memcpy(up, kp, len);
    566 	vunmaprange((vaddr_t)up, len);
    567 	uvm_vsunlock(p->p_vmspace, udaddr, len);
    568 	uvm_lwp_rele(l);
    569 
    570 	return 0;
    571 }
    572 
    573 /*
    574  * kcopy(const void *src, void *dst, size_t len);
    575  *
    576  * Copy len bytes from src to dst, aborting if we encounter a fatal
    577  * page fault.
    578  *
    579  * kcopy() _must_ save and restore the old fault handler since it is
    580  * called by uiomove(), which may be in the path of servicing a non-fatal
    581  * page fault.
    582  */
    583 int
    584 kcopy(const void *src, void *dst, size_t len)
    585 {
    586 	struct faultbuf env, *oldfault;
    587 
    588 	oldfault = curpcb->pcb_onfault;
    589 	if (setfault(&env)) {
    590 		curpcb->pcb_onfault = oldfault;
    591 		return EFAULT;
    592 	}
    593 
    594 	memcpy(dst, src, len);
    595 
    596 	curpcb->pcb_onfault = oldfault;
    597 	return 0;
    598 }
    599 
    600 int
    601 badaddr(void *addr, size_t size)
    602 {
    603 
    604 	return badaddr_read(addr, size, NULL);
    605 }
    606 
    607 int
    608 badaddr_read(void *addr, size_t size, int *rptr)
    609 {
    610 	struct faultbuf env;
    611 	int x;
    612 
    613 	/* Get rid of any stale machine checks that have been waiting.  */
    614 	__asm volatile ("sync; isync");
    615 
    616 	if (setfault(&env)) {
    617 		curpcb->pcb_onfault = 0;
    618 		__asm volatile ("sync");
    619 		return 1;
    620 	}
    621 
    622 	__asm volatile ("sync");
    623 
    624 	switch (size) {
    625 	case 1:
    626 		x = *(volatile int8_t *)addr;
    627 		break;
    628 	case 2:
    629 		x = *(volatile int16_t *)addr;
    630 		break;
    631 	case 4:
    632 		x = *(volatile int32_t *)addr;
    633 		break;
    634 	default:
    635 		panic("badaddr: invalid size (%d)", size);
    636 	}
    637 
    638 	/* Make sure we took the machine check, if we caused one. */
    639 	__asm volatile ("sync; isync");
    640 
    641 	curpcb->pcb_onfault = 0;
    642 	__asm volatile ("sync");	/* To be sure. */
    643 
    644 	/* Use the value to avoid reorder. */
    645 	if (rptr)
    646 		*rptr = x;
    647 
    648 	return 0;
    649 }
    650 
    651 /*
    652  * For now, this only deals with the particular unaligned access case
    653  * that gcc tends to generate.  Eventually it should handle all of the
    654  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
    655  */
    656 
    657 static int
    658 fix_unaligned(struct lwp *l, struct trapframe *frame)
    659 {
    660 
    661 	return -1;
    662 }
    663 
    664 /*
    665  * Start a new LWP
    666  */
    667 void
    668 startlwp(arg)
    669 	void *arg;
    670 {
    671 	int err;
    672 	ucontext_t *uc = arg;
    673 	struct lwp *l = curlwp;
    674 
    675 	err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
    676 #if DIAGNOSTIC
    677 	if (err) {
    678 		printf("Error %d from cpu_setmcontext.", err);
    679 	}
    680 #endif
    681 	pool_put(&lwp_uc_pool, uc);
    682 
    683 	/* Invoke MI userret code */
    684 	mi_userret(l);
    685 
    686 	curcpu()->ci_schedstate.spc_curpriority = l->l_priority = l->l_usrpri;
    687 }
    688