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