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