Home | History | Annotate | Line # | Download | only in ibm4xx
trap.c revision 1.67
      1 /*	$NetBSD: trap.c,v 1.67 2014/10/18 08:33:26 snj 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.67 2014/10/18 08:33:26 snj 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/userret.h>
     82 #include <sys/kauth.h>
     83 #include <sys/cpu.h>
     84 
     85 #if defined(KGDB)
     86 #include <sys/kgdb.h>
     87 #endif
     88 
     89 #include <uvm/uvm_extern.h>
     90 
     91 #include <dev/cons.h>
     92 
     93 #include <machine/fpu.h>
     94 #include <machine/frame.h>
     95 #include <machine/pcb.h>
     96 #include <machine/psl.h>
     97 #include <machine/trap.h>
     98 
     99 #include <powerpc/db_machdep.h>
    100 #include <powerpc/spr.h>
    101 #include <powerpc/ibm4xx/spr.h>
    102 
    103 #include <powerpc/ibm4xx/cpu.h>
    104 #include <powerpc/ibm4xx/pmap.h>
    105 #include <powerpc/ibm4xx/tlb.h>
    106 
    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 *tf);
    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 			}
    193 
    194 			if (tf->tf_esr & (ESR_DST|ESR_DIZ))
    195 				ftype = VM_PROT_WRITE;
    196 
    197 			DBPRINTF(TDB_ALL,
    198 			    ("trap(EXC_DSI) at %lx %s fault on %p esr %x\n",
    199 			    tf->tf_srr0,
    200 			    (ftype & VM_PROT_WRITE) ? "write" : "read",
    201 			    (void *)va, tf->tf_esr));
    202 
    203 			pcb = lwp_getpcb(l);
    204 			fb = pcb->pcb_onfault;
    205 			pcb->pcb_onfault = NULL;
    206 			rv = uvm_fault(map, trunc_page(va), ftype);
    207 			pcb->pcb_onfault = fb;
    208 			if (rv == 0)
    209 				goto done;
    210 			if (fb != NULL) {
    211 				tf->tf_pid = KERNEL_PID;
    212 				tf->tf_srr0 = fb->fb_pc;
    213 				tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
    214 				tf->tf_cr = fb->fb_cr;
    215 				tf->tf_fixreg[1] = fb->fb_sp;
    216 				tf->tf_fixreg[2] = fb->fb_r2;
    217 				tf->tf_fixreg[3] = 1; /* Return TRUE */
    218 				memcpy(&tf->tf_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 		if (tf->tf_esr & (ESR_DST|ESR_DIZ))
    229 			ftype = VM_PROT_WRITE;
    230 
    231 		DBPRINTF(TDB_ALL,
    232 		    ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
    233 		    tf->tf_srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
    234 		    tf->tf_dear, tf->tf_esr));
    235 		KASSERT(l == curlwp && (l->l_stat == LSONPROC));
    236 //		KASSERT(curpcb->pcb_onfault == NULL);
    237 		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_dear),
    238 		    ftype);
    239 		if (rv == 0) {
    240 			break;
    241 		}
    242 		KSI_INIT_TRAP(&ksi);
    243 		ksi.ksi_signo = SIGSEGV;
    244 		ksi.ksi_trap = EXC_DSI;
    245 		ksi.ksi_addr = (void *)tf->tf_dear;
    246 		if (rv == ENOMEM) {
    247 			printf("UVM: pid %d (%s) lid %d, uid %d killed: "
    248 			    "out of swap\n",
    249 			    p->p_pid, p->p_comm, l->l_lid,
    250 			    l->l_cred ?
    251 			    kauth_cred_geteuid(l->l_cred) : -1);
    252 			ksi.ksi_signo = SIGKILL;
    253 		}
    254 		trapsignal(l, &ksi);
    255 		break;
    256 
    257 	case EXC_ITMISS|EXC_USER:
    258 	case EXC_ISI|EXC_USER:
    259 		ftype = VM_PROT_EXECUTE;
    260 		DBPRINTF(TDB_ALL,
    261 		    ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
    262 		    tf->tf_srr0, tf));
    263 //		KASSERT(curpcb->pcb_onfault == NULL);
    264 		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_srr0),
    265 		    ftype);
    266 		if (rv == 0) {
    267 			break;
    268 		}
    269 		KSI_INIT_TRAP(&ksi);
    270 		ksi.ksi_signo = SIGSEGV;
    271 		ksi.ksi_trap = EXC_ISI;
    272 		ksi.ksi_addr = (void *)tf->tf_srr0;
    273 		ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
    274 		trapsignal(l, &ksi);
    275 		break;
    276 
    277 	case EXC_AST|EXC_USER:
    278 		cpu_ast(l, curcpu());
    279 		break;
    280 
    281 	case EXC_ALI|EXC_USER:
    282 		if (fix_unaligned(l, tf) != 0) {
    283 			KSI_INIT_TRAP(&ksi);
    284 			ksi.ksi_signo = SIGBUS;
    285 			ksi.ksi_trap = EXC_ALI;
    286 			ksi.ksi_addr = (void *)tf->tf_dear;
    287 			trapsignal(l, &ksi);
    288 		} else
    289 			tf->tf_srr0 += 4;
    290 		break;
    291 
    292 	case EXC_PGM|EXC_USER:
    293 		/*
    294 		 * Illegal insn:
    295 		 *
    296 		 * let's try to see if its FPU and can be emulated.
    297 		 */
    298 		curcpu()->ci_data.cpu_ntrap++;
    299 		pcb = lwp_getpcb(l);
    300 
    301 		if (__predict_false(!fpu_used_p(l))) {
    302 			memset(&pcb->pcb_fpu, 0, sizeof(pcb->pcb_fpu));
    303 			fpu_mark_used(l);
    304 		}
    305 
    306 		if (fpu_emulate(tf, &pcb->pcb_fpu, &ksi)) {
    307 			if (ksi.ksi_signo == 0)	/* was emulated */
    308 				break;
    309 		} else {
    310 			ksi.ksi_signo = SIGILL;
    311 			ksi.ksi_code = ILL_ILLOPC;
    312 			ksi.ksi_trap = EXC_PGM;
    313 			ksi.ksi_addr = (void *)tf->tf_srr0;
    314 		}
    315 
    316 		trapsignal(l, &ksi);
    317 		break;
    318 
    319 	case EXC_MCHK:
    320 		{
    321 			struct faultbuf *fb;
    322 
    323 			pcb = lwp_getpcb(l);
    324 			if ((fb = pcb->pcb_onfault) != NULL) {
    325 				tf->tf_pid = KERNEL_PID;
    326 				tf->tf_srr0 = fb->fb_pc;
    327 				tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
    328 				tf->tf_fixreg[1] = fb->fb_sp;
    329 				tf->tf_fixreg[2] = fb->fb_r2;
    330 				tf->tf_fixreg[3] = 1; /* Return TRUE */
    331 				tf->tf_cr = fb->fb_cr;
    332 				memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
    333 				    sizeof(fb->fb_fixreg));
    334 				goto done;
    335 			}
    336 		}
    337 		goto brain_damage;
    338 	default:
    339  brain_damage:
    340 		printf("trap type 0x%x at 0x%lx\n", type, tf->tf_srr0);
    341 #if defined(DDB) || defined(KGDB)
    342 		if (kdb_trap(type, tf))
    343 			goto done;
    344 #endif
    345 #ifdef TRAP_PANICWAIT
    346 		printf("Press a key to panic.\n");
    347 		cngetc();
    348 #endif
    349 		panic("trap");
    350 	}
    351 
    352 	/* Invoke MI userret code */
    353 	mi_userret(l);
    354  done:
    355 	return;
    356 }
    357 
    358 int
    359 ctx_setup(int ctx, int srr1)
    360 {
    361 	volatile struct pmap *pm;
    362 
    363 	/* Update PID if we're returning to user mode. */
    364 	if (srr1 & PSL_PR) {
    365 		pm = curproc->p_vmspace->vm_map.pmap;
    366 		if (!pm->pm_ctx) {
    367 			ctx_alloc(__UNVOLATILE(pm));
    368 		}
    369 		ctx = pm->pm_ctx;
    370 		if (srr1 & PSL_SE) {
    371 			int dbreg, mask = 0x48000000;
    372 				/*
    373 				 * Set the Internal Debug and
    374 				 * Instruction Completion bits of
    375 				 * the DBCR0 register.
    376 				 *
    377 				 * XXX this is also used by jtag debuggers...
    378 				 */
    379 			__asm volatile("mfspr %0,0x3f2;"
    380 			    "or %0,%0,%1;"
    381 			    "mtspr 0x3f2,%0;" :
    382 			    "=&r" (dbreg) : "r" (mask));
    383 		}
    384 	}
    385 	else if (!ctx) {
    386 		ctx = KERNEL_PID;
    387 	}
    388 	return (ctx);
    389 }
    390 
    391 /*
    392  * Used by copyin()/copyout()
    393  */
    394 extern vaddr_t vmaprange(struct proc *, vaddr_t, vsize_t, int);
    395 extern void vunmaprange(vaddr_t, vsize_t);
    396 static int bigcopyin(const void *, void *, size_t );
    397 static int bigcopyout(const void *, void *, size_t );
    398 
    399 int
    400 copyin(const void *udaddr, void *kaddr, size_t len)
    401 {
    402 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    403 	int rv, msr, pid, tmp, ctx, count = 0;
    404 	struct faultbuf env;
    405 
    406 	/* For bigger buffers use the faster copy */
    407 	if (len > 1024)
    408 		return (bigcopyin(udaddr, kaddr, len));
    409 
    410 	if ((rv = setfault(&env))) {
    411 		curpcb->pcb_onfault = NULL;
    412 		return rv;
    413 	}
    414 
    415 	if (!(ctx = pm->pm_ctx)) {
    416 		/* No context -- assign it one */
    417 		ctx_alloc(pm);
    418 		ctx = pm->pm_ctx;
    419 	}
    420 
    421 	__asm volatile(
    422 		"   mfmsr %[msr];"          /* Save MSR */
    423 		"   li %[pid],0x20; "
    424 		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */
    425 		"   mfpid %[pid];"          /* Save old PID */
    426 		"   sync; isync;"
    427 
    428 		"   srwi. %[count],%[len],0x2;"     /* How many words? */
    429 		"   beq-  2f;"              /* No words. Go do bytes */
    430 		"   mtctr %[count];"
    431 		"1: mtpid %[ctx]; sync;"
    432 		"   lswi %[tmp],%[udaddr],4;"       /* Load user word */
    433 		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
    434 		"   sync; isync;"
    435 		"   mtpid %[pid];sync;"
    436 		"   stswi %[tmp],%[kaddr],4;"        /* Store kernel word */
    437 		"   dcbf 0,%[kaddr];"           /* flush cache */
    438 		"   addi %[kaddr],%[kaddr],0x4;"    /* next udaddr word */
    439 		"   sync; isync;"
    440 		"   bdnz 1b;"               /* repeat */
    441 
    442 		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
    443 		"   addi %[count],%[count],0x1;"
    444 		"   mtctr %[count];"
    445 		"3: bdz 10f;"               /* while count */
    446 		"   mtpid %[ctx];sync;"
    447 		"   lbz %[tmp],0(%[udaddr]);"       /* Load user byte */
    448 		"   addi %[udaddr],%[udaddr],0x1;"  /* next udaddr byte */
    449 		"   sync; isync;"
    450 		"   mtpid %[pid]; sync;"
    451 		"   stb %[tmp],0(%[kaddr]);"        /* Store kernel byte */
    452 		"   dcbf 0,%[kaddr];"           /* flush cache */
    453 		"   addi %[kaddr],%[kaddr],0x1;"
    454 		"   sync; isync;"
    455 		"   b 3b;"
    456 		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
    457 		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
    458 		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
    459 
    460 	curpcb->pcb_onfault = NULL;
    461 	return 0;
    462 }
    463 
    464 static int
    465 bigcopyin(const void *udaddr, void *kaddr, size_t len)
    466 {
    467 	const char *up;
    468 	char *kp = kaddr;
    469 	struct lwp *l = curlwp;
    470 	struct proc *p;
    471 	struct faultbuf env;
    472 	int error;
    473 
    474 	p = l->l_proc;
    475 
    476 	/*
    477 	 * Stolen from physio():
    478 	 */
    479 	error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
    480 	if (error) {
    481 		return error;
    482 	}
    483 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
    484 
    485 	if ((error = setfault(&env)) == 0) {
    486 		memcpy(kp, up, len);
    487 	}
    488 
    489 	curpcb->pcb_onfault = NULL;
    490 	vunmaprange((vaddr_t)up, len);
    491 	uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
    492 
    493 	return error;
    494 }
    495 
    496 int
    497 copyout(const void *kaddr, void *udaddr, size_t len)
    498 {
    499 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    500 	int rv, msr, pid, tmp, ctx, count = 0;
    501 	struct faultbuf env;
    502 
    503 	/* For big copies use more efficient routine */
    504 	if (len > 1024)
    505 		return (bigcopyout(kaddr, udaddr, len));
    506 
    507 	if ((rv = setfault(&env))) {
    508 		curpcb->pcb_onfault = NULL;
    509 		return rv;
    510 	}
    511 
    512 	if (!(ctx = pm->pm_ctx)) {
    513 		/* No context -- assign it one */
    514 		ctx_alloc(pm);
    515 		ctx = pm->pm_ctx;
    516 	}
    517 
    518 	__asm volatile(
    519 		"   mfmsr %[msr];"          /* Save MSR */ \
    520 		"   li %[pid],0x20; " \
    521 		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */ \
    522 		"   mfpid %[pid];"          /* Save old PID */ \
    523 		"   sync; isync;"
    524 
    525 		"   srwi. %[count],%[len],0x2;"     /* How many words? */
    526 		"   beq-  2f;"              /* No words. Go do bytes */
    527 		"   mtctr %[count];"
    528 		"1: mtpid %[pid];sync;"
    529 		"   lswi %[tmp],%[kaddr],4;"        /* Load kernel word */
    530 		"   addi %[kaddr],%[kaddr],0x4;"    /* next kaddr word */
    531 		"   sync; isync;"
    532 		"   mtpid %[ctx]; sync;"
    533 		"   stswi %[tmp],%[udaddr],4;"       /* Store user word */
    534 		"   dcbf 0,%[udaddr];"          /* flush cache */
    535 		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
    536 		"   sync; isync;"
    537 		"   bdnz 1b;"               /* repeat */
    538 
    539 		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
    540 		"   addi %[count],%[count],0x1;"
    541 		"   mtctr %[count];"
    542 		"3: bdz  10f;"              /* while count */
    543 		"   mtpid %[pid];sync;"
    544 		"   lbz %[tmp],0(%[kaddr]);"        /* Load kernel byte */
    545 		"   addi %[kaddr],%[kaddr],0x1;"    /* next kaddr byte */
    546 		"   sync; isync;"
    547 		"   mtpid %[ctx]; sync;"
    548 		"   stb %[tmp],0(%[udaddr]);"       /* Store user byte */
    549 		"   dcbf 0,%[udaddr];"          /* flush cache */
    550 		"   addi %[udaddr],%[udaddr],0x1;"
    551 		"   sync; isync;"
    552 		"   b 3b;"
    553 		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
    554 		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
    555 		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
    556 
    557 	curpcb->pcb_onfault = NULL;
    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 	struct faultbuf env;
    569 	int error;
    570 
    571 	p = l->l_proc;
    572 
    573 	/*
    574 	 * Stolen from physio():
    575 	 */
    576 	error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
    577 	if (error) {
    578 		return error;
    579 	}
    580 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
    581 	    VM_PROT_READ | VM_PROT_WRITE);
    582 
    583 	if ((error = setfault(&env)) == 0) {
    584 		memcpy(up, kp, len);
    585 	}
    586 
    587 	curpcb->pcb_onfault = NULL;
    588 	vunmaprange((vaddr_t)up, len);
    589 	uvm_vsunlock(p->p_vmspace, udaddr, len);
    590 
    591 	return error;
    592 }
    593 
    594 /*
    595  * kcopy(const void *src, void *dst, size_t len);
    596  *
    597  * Copy len bytes from src to dst, aborting if we encounter a fatal
    598  * page fault.
    599  *
    600  * kcopy() _must_ save and restore the old fault handler since it is
    601  * called by uiomove(), which may be in the path of servicing a non-fatal
    602  * page fault.
    603  */
    604 int
    605 kcopy(const void *src, void *dst, size_t len)
    606 {
    607 	struct faultbuf env, *oldfault;
    608 	int rv;
    609 
    610 	oldfault = curpcb->pcb_onfault;
    611 	if ((rv = setfault(&env))) {
    612 		curpcb->pcb_onfault = oldfault;
    613 		return rv;
    614 	}
    615 
    616 	memcpy(dst, src, len);
    617 
    618 	curpcb->pcb_onfault = oldfault;
    619 	return 0;
    620 }
    621 
    622 int
    623 badaddr(void *addr, size_t size)
    624 {
    625 
    626 	return badaddr_read(addr, size, NULL);
    627 }
    628 
    629 int
    630 badaddr_read(void *addr, size_t size, int *rptr)
    631 {
    632 	struct faultbuf env;
    633 	int x;
    634 
    635 	/* Get rid of any stale machine checks that have been waiting.  */
    636 	__asm volatile ("sync; isync");
    637 
    638 	if (setfault(&env)) {
    639 		curpcb->pcb_onfault = NULL;
    640 		__asm volatile ("sync");
    641 		return 1;
    642 	}
    643 
    644 	__asm volatile ("sync");
    645 
    646 	switch (size) {
    647 	case 1:
    648 		x = *(volatile int8_t *)addr;
    649 		break;
    650 	case 2:
    651 		x = *(volatile int16_t *)addr;
    652 		break;
    653 	case 4:
    654 		x = *(volatile int32_t *)addr;
    655 		break;
    656 	default:
    657 		panic("badaddr: invalid size (%d)", size);
    658 	}
    659 
    660 	/* Make sure we took the machine check, if we caused one. */
    661 	__asm volatile ("sync; isync");
    662 
    663 	curpcb->pcb_onfault = NULL;
    664 	__asm volatile ("sync");	/* To be sure. */
    665 
    666 	/* Use the value to avoid reorder. */
    667 	if (rptr)
    668 		*rptr = x;
    669 
    670 	return 0;
    671 }
    672 
    673 /*
    674  * For now, this only deals with the particular unaligned access case
    675  * that gcc tends to generate.  Eventually it should handle all of the
    676  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
    677  */
    678 
    679 static int
    680 fix_unaligned(struct lwp *l, struct trapframe *tf)
    681 {
    682 
    683 	return -1;
    684 }
    685