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