Home | History | Annotate | Line # | Download | only in ibm4xx
trap.c revision 1.58
      1 /*	$NetBSD: trap.c,v 1.58 2011/01/18 01:02:54 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.58 2011/01/18 01:02:54 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/sa.h>
     82 #include <sys/savar.h>
     83 #include <sys/userret.h>
     84 #include <sys/kauth.h>
     85 #include <sys/kmem.h>
     86 
     87 #if defined(KGDB)
     88 #include <sys/kgdb.h>
     89 #endif
     90 
     91 #include <uvm/uvm_extern.h>
     92 
     93 #include <dev/cons.h>
     94 
     95 #include <machine/cpu.h>
     96 #include <machine/db_machdep.h>
     97 #include <machine/fpu.h>
     98 #include <machine/frame.h>
     99 #include <machine/pcb.h>
    100 #include <machine/psl.h>
    101 #include <machine/trap.h>
    102 
    103 #include <powerpc/spr.h>
    104 #include <powerpc/ibm4xx/spr.h>
    105 #include <powerpc/ibm4xx/pmap.h>
    106 #include <powerpc/ibm4xx/tlb.h>
    107 #include <powerpc/fpu/fpu_extern.h>
    108 
    109 /* These definitions should probably be somewhere else			XXX */
    110 #define	FIRSTARG	3		/* first argument is in reg 3 */
    111 #define	NARGREG		8		/* 8 args are in registers */
    112 #define	MOREARGS(sp)	((void *)((int)(sp) + 8)) /* more args go here */
    113 
    114 static int fix_unaligned(struct lwp *l, struct trapframe *frame);
    115 
    116 void trap(struct trapframe *);	/* Called from locore / trap_subr */
    117 /* Why are these not defined in a header? */
    118 int badaddr(void *, size_t);
    119 int badaddr_read(void *, size_t, int *);
    120 int ctx_setup(int, int);
    121 
    122 #ifdef DEBUG
    123 #define TDB_ALL	0x1
    124 int trapdebug = /* TDB_ALL */ 0;
    125 #define	DBPRINTF(x, y)	if (trapdebug & (x)) printf y
    126 #else
    127 #define DBPRINTF(x, y)
    128 #endif
    129 
    130 void
    131 trap(struct trapframe *tf)
    132 {
    133 	struct lwp *l = curlwp;
    134 	struct proc *p = l->l_proc;
    135 	struct pcb *pcb;
    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 			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 //		KASSERT(curpcb->pcb_onfault == NULL);
    249 		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_dear),
    250 		    ftype);
    251 		if (rv == 0) {
    252 			l->l_pflag &= ~LP_SA_PAGEFAULT;
    253 			break;
    254 		}
    255 		KSI_INIT_TRAP(&ksi);
    256 		ksi.ksi_signo = SIGSEGV;
    257 		ksi.ksi_trap = EXC_DSI;
    258 		ksi.ksi_addr = (void *)tf->tf_dear;
    259 		if (rv == ENOMEM) {
    260 			printf("UVM: pid %d (%s) lid %d, uid %d killed: "
    261 			    "out of swap\n",
    262 			    p->p_pid, p->p_comm, l->l_lid,
    263 			    l->l_cred ?
    264 			    kauth_cred_geteuid(l->l_cred) : -1);
    265 			ksi.ksi_signo = SIGKILL;
    266 		}
    267 		trapsignal(l, &ksi);
    268 		l->l_pflag &= ~LP_SA_PAGEFAULT;
    269 		break;
    270 
    271 	case EXC_ITMISS|EXC_USER:
    272 	case EXC_ISI|EXC_USER:
    273 		if (l->l_flag & LW_SA) {
    274 			l->l_savp->savp_faultaddr = (vaddr_t)tf->tf_srr0;
    275 			l->l_pflag |= LP_SA_PAGEFAULT;
    276 		}
    277 		ftype = VM_PROT_EXECUTE;
    278 		DBPRINTF(TDB_ALL,
    279 		    ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
    280 		    tf->tf_srr0, tf));
    281 //		KASSERT(curpcb->pcb_onfault == NULL);
    282 		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_srr0),
    283 		    ftype);
    284 		if (rv == 0) {
    285 			l->l_pflag &= ~LP_SA_PAGEFAULT;
    286 			break;
    287 		}
    288 		KSI_INIT_TRAP(&ksi);
    289 		ksi.ksi_signo = SIGSEGV;
    290 		ksi.ksi_trap = EXC_ISI;
    291 		ksi.ksi_addr = (void *)tf->tf_srr0;
    292 		ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
    293 		trapsignal(l, &ksi);
    294 		l->l_pflag &= ~LP_SA_PAGEFAULT;
    295 		break;
    296 
    297 	case EXC_AST|EXC_USER:
    298 		curcpu()->ci_astpending = 0;	/* we are about to do it */
    299 		//curcpu()->ci_data.cpu_nast++;
    300 		if (l->l_pflag & LP_OWEUPC) {
    301 			l->l_pflag &= ~LP_OWEUPC;
    302 			ADDUPROF(l);
    303 		}
    304 		/* Check whether we are being preempted. */
    305 		if (curcpu()->ci_want_resched)
    306 			preempt();
    307 		break;
    308 
    309 
    310 	case EXC_ALI|EXC_USER:
    311 		if (fix_unaligned(l, tf) != 0) {
    312 			KSI_INIT_TRAP(&ksi);
    313 			ksi.ksi_signo = SIGBUS;
    314 			ksi.ksi_trap = EXC_ALI;
    315 			ksi.ksi_addr = (void *)tf->tf_dear;
    316 			trapsignal(l, &ksi);
    317 		} else
    318 			tf->tf_srr0 += 4;
    319 		break;
    320 
    321 	case EXC_PGM|EXC_USER:
    322 		/*
    323 		 * Illegal insn:
    324 		 *
    325 		 * let's try to see if it's FPU and can be emulated.
    326 		 */
    327 		curcpu()->ci_data.cpu_ntrap++;
    328 		pcb = lwp_getpcb(l);
    329 
    330 		if (!(l->l_md.md_flags & MDLWP_USEDFPU)) {
    331 			memset(&pcb->pcb_fpu, 0, sizeof(pcb->pcb_fpu));
    332 			l->l_md.md_flags |= MDLWP_USEDFPU;
    333 		}
    334 
    335 		if ((rv = fpu_emulate(tf, (struct fpreg *)&pcb->pcb_fpu))) {
    336 			KSI_INIT_TRAP(&ksi);
    337 			ksi.ksi_signo = rv;
    338 			ksi.ksi_trap = EXC_PGM;
    339 			ksi.ksi_addr = (void *)tf->tf_srr0;
    340 			trapsignal(l, &ksi);
    341 		}
    342 		break;
    343 
    344 	case EXC_MCHK:
    345 		{
    346 			struct faultbuf *fb;
    347 
    348 			pcb = lwp_getpcb(l);
    349 			if ((fb = pcb->pcb_onfault) != NULL) {
    350 				tf->tf_pid = KERNEL_PID;
    351 				tf->tf_srr0 = fb->fb_pc;
    352 				tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
    353 				tf->tf_fixreg[1] = fb->fb_sp;
    354 				tf->tf_fixreg[2] = fb->fb_r2;
    355 				tf->tf_fixreg[3] = 1; /* Return TRUE */
    356 				tf->tf_cr = fb->fb_cr;
    357 				memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
    358 				    sizeof(fb->fb_fixreg));
    359 				goto done;
    360 			}
    361 		}
    362 		goto brain_damage;
    363 	default:
    364  brain_damage:
    365 		printf("trap type 0x%x at 0x%lx\n", type, tf->tf_srr0);
    366 #if defined(DDB) || defined(KGDB)
    367 		if (kdb_trap(type, tf))
    368 			goto done;
    369 #endif
    370 #ifdef TRAP_PANICWAIT
    371 		printf("Press a key to panic.\n");
    372 		cngetc();
    373 #endif
    374 		panic("trap");
    375 	}
    376 
    377 	/* Invoke MI userret code */
    378 	mi_userret(l);
    379  done:
    380 	return;
    381 }
    382 
    383 int
    384 ctx_setup(int ctx, int srr1)
    385 {
    386 	volatile struct pmap *pm;
    387 
    388 	/* Update PID if we're returning to user mode. */
    389 	if (srr1 & PSL_PR) {
    390 		pm = curproc->p_vmspace->vm_map.pmap;
    391 		if (!pm->pm_ctx) {
    392 			ctx_alloc(__UNVOLATILE(pm));
    393 		}
    394 		ctx = pm->pm_ctx;
    395 		if (srr1 & PSL_SE) {
    396 			int dbreg, mask = 0x48000000;
    397 				/*
    398 				 * Set the Internal Debug and
    399 				 * Instruction Completion bits of
    400 				 * the DBCR0 register.
    401 				 *
    402 				 * XXX this is also used by jtag debuggers...
    403 				 */
    404 			__asm volatile("mfspr %0,0x3f2;"
    405 			    "or %0,%0,%1;"
    406 			    "mtspr 0x3f2,%0;" :
    407 			    "=&r" (dbreg) : "r" (mask));
    408 		}
    409 	}
    410 	else if (!ctx) {
    411 		ctx = KERNEL_PID;
    412 	}
    413 	return (ctx);
    414 }
    415 
    416 /*
    417  * Used by copyin()/copyout()
    418  */
    419 extern vaddr_t vmaprange(struct proc *, vaddr_t, vsize_t, int);
    420 extern void vunmaprange(vaddr_t, vsize_t);
    421 static int bigcopyin(const void *, void *, size_t );
    422 static int bigcopyout(const void *, void *, size_t );
    423 
    424 int
    425 copyin(const void *udaddr, void *kaddr, size_t len)
    426 {
    427 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    428 	int rv, msr, pid, tmp, ctx, count = 0;
    429 	struct faultbuf env;
    430 
    431 	/* For bigger buffers use the faster copy */
    432 	if (len > 1024)
    433 		return (bigcopyin(udaddr, kaddr, len));
    434 
    435 	if ((rv = setfault(&env))) {
    436 		curpcb->pcb_onfault = NULL;
    437 		return rv;
    438 	}
    439 
    440 	if (!(ctx = pm->pm_ctx)) {
    441 		/* No context -- assign it one */
    442 		ctx_alloc(pm);
    443 		ctx = pm->pm_ctx;
    444 	}
    445 
    446 	__asm volatile(
    447 		"   mfmsr %[msr];"          /* Save MSR */
    448 		"   li %[pid],0x20; "
    449 		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */
    450 		"   mfpid %[pid];"          /* Save old PID */
    451 		"   sync; isync;"
    452 
    453 		"   srwi. %[count],%[len],0x2;"     /* How many words? */
    454 		"   beq-  2f;"              /* No words. Go do bytes */
    455 		"   mtctr %[count];"
    456 		"1: mtpid %[ctx]; sync;"
    457 		"   lswi %[tmp],%[udaddr],4;"       /* Load user word */
    458 		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
    459 		"   sync; isync;"
    460 		"   mtpid %[pid];sync;"
    461 		"   stswi %[tmp],%[kaddr],4;"        /* Store kernel word */
    462 		"   dcbf 0,%[kaddr];"           /* flush cache */
    463 		"   addi %[kaddr],%[kaddr],0x4;"    /* next udaddr word */
    464 		"   sync; isync;"
    465 		"   bdnz 1b;"               /* repeat */
    466 
    467 		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
    468 		"   addi %[count],%[count],0x1;"
    469 		"   mtctr %[count];"
    470 		"3: bdz 10f;"               /* while count */
    471 		"   mtpid %[ctx];sync;"
    472 		"   lbz %[tmp],0(%[udaddr]);"       /* Load user byte */
    473 		"   addi %[udaddr],%[udaddr],0x1;"  /* next udaddr byte */
    474 		"   sync; isync;"
    475 		"   mtpid %[pid]; sync;"
    476 		"   stb %[tmp],0(%[kaddr]);"        /* Store kernel byte */
    477 		"   dcbf 0,%[kaddr];"           /* flush cache */
    478 		"   addi %[kaddr],%[kaddr],0x1;"
    479 		"   sync; isync;"
    480 		"   b 3b;"
    481 		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
    482 		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
    483 		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
    484 
    485 	curpcb->pcb_onfault = NULL;
    486 	return 0;
    487 }
    488 
    489 static int
    490 bigcopyin(const void *udaddr, void *kaddr, size_t len)
    491 {
    492 	const char *up;
    493 	char *kp = kaddr;
    494 	struct lwp *l = curlwp;
    495 	struct proc *p;
    496 	struct faultbuf env;
    497 	int error;
    498 
    499 	p = l->l_proc;
    500 
    501 	/*
    502 	 * Stolen from physio():
    503 	 */
    504 	error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
    505 	if (error) {
    506 		return error;
    507 	}
    508 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
    509 
    510 	if ((error = setfault(&env)) == 0) {
    511 		memcpy(kp, up, len);
    512 	}
    513 
    514 	curpcb->pcb_onfault = NULL;
    515 	vunmaprange((vaddr_t)up, len);
    516 	uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
    517 
    518 	return error;
    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 rv, 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 ((rv = setfault(&env))) {
    533 		curpcb->pcb_onfault = NULL;
    534 		return rv;
    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 = NULL;
    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 	struct faultbuf env;
    594 	int error;
    595 
    596 	p = l->l_proc;
    597 
    598 	/*
    599 	 * Stolen from physio():
    600 	 */
    601 	error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
    602 	if (error) {
    603 		return error;
    604 	}
    605 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
    606 	    VM_PROT_READ | VM_PROT_WRITE);
    607 
    608 	if ((error = setfault(&env)) == 0) {
    609 		memcpy(up, kp, len);
    610 	}
    611 
    612 	curpcb->pcb_onfault = NULL;
    613 	vunmaprange((vaddr_t)up, len);
    614 	uvm_vsunlock(p->p_vmspace, udaddr, len);
    615 
    616 	return error;
    617 }
    618 
    619 /*
    620  * kcopy(const void *src, void *dst, size_t len);
    621  *
    622  * Copy len bytes from src to dst, aborting if we encounter a fatal
    623  * page fault.
    624  *
    625  * kcopy() _must_ save and restore the old fault handler since it is
    626  * called by uiomove(), which may be in the path of servicing a non-fatal
    627  * page fault.
    628  */
    629 int
    630 kcopy(const void *src, void *dst, size_t len)
    631 {
    632 	struct faultbuf env, *oldfault;
    633 	int rv;
    634 
    635 	oldfault = curpcb->pcb_onfault;
    636 	if ((rv = setfault(&env))) {
    637 		curpcb->pcb_onfault = oldfault;
    638 		return rv;
    639 	}
    640 
    641 	memcpy(dst, src, len);
    642 
    643 	curpcb->pcb_onfault = oldfault;
    644 	return 0;
    645 }
    646 
    647 int
    648 badaddr(void *addr, size_t size)
    649 {
    650 
    651 	return badaddr_read(addr, size, NULL);
    652 }
    653 
    654 int
    655 badaddr_read(void *addr, size_t size, int *rptr)
    656 {
    657 	struct faultbuf env;
    658 	int x;
    659 
    660 	/* Get rid of any stale machine checks that have been waiting.  */
    661 	__asm volatile ("sync; isync");
    662 
    663 	if (setfault(&env)) {
    664 		curpcb->pcb_onfault = NULL;
    665 		__asm volatile ("sync");
    666 		return 1;
    667 	}
    668 
    669 	__asm volatile ("sync");
    670 
    671 	switch (size) {
    672 	case 1:
    673 		x = *(volatile int8_t *)addr;
    674 		break;
    675 	case 2:
    676 		x = *(volatile int16_t *)addr;
    677 		break;
    678 	case 4:
    679 		x = *(volatile int32_t *)addr;
    680 		break;
    681 	default:
    682 		panic("badaddr: invalid size (%d)", size);
    683 	}
    684 
    685 	/* Make sure we took the machine check, if we caused one. */
    686 	__asm volatile ("sync; isync");
    687 
    688 	curpcb->pcb_onfault = NULL;
    689 	__asm volatile ("sync");	/* To be sure. */
    690 
    691 	/* Use the value to avoid reorder. */
    692 	if (rptr)
    693 		*rptr = x;
    694 
    695 	return 0;
    696 }
    697 
    698 /*
    699  * For now, this only deals with the particular unaligned access case
    700  * that gcc tends to generate.  Eventually it should handle all of the
    701  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
    702  */
    703 
    704 static int
    705 fix_unaligned(struct lwp *l, struct trapframe *tf)
    706 {
    707 
    708 	return -1;
    709 }
    710 
    711 /*
    712  * Start a new LWP
    713  */
    714 void
    715 startlwp(void *arg)
    716 {
    717 	ucontext_t *uc = arg;
    718 	lwp_t *l = curlwp;
    719 	int error;
    720 
    721 	error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
    722 	KASSERT(error == 0);
    723 
    724 	kmem_free(uc, sizeof(ucontext_t));
    725 	upcallret(l);
    726 }
    727 
    728 /*
    729  * XXX This is a terrible name.
    730  */
    731 void
    732 upcallret(struct lwp *l)
    733 {
    734 
    735 	/* Invoke MI userret code */
    736 	mi_userret(l);
    737 }
    738