Home | History | Annotate | Line # | Download | only in ibm4xx
trap.c revision 1.62.2.1
      1 /*	$NetBSD: trap.c,v 1.62.2.1 2012/04/17 00:06:46 yamt 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.62.2.1 2012/04/17 00:06:46 yamt 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 it's FPU and can be emulated.
    297 		 */
    298 		curcpu()->ci_data.cpu_ntrap++;
    299 		pcb = lwp_getpcb(l);
    300 
    301 		if (!(l->l_md.md_flags & MDLWP_USEDFPU)) {
    302 			memset(&pcb->pcb_fpu, 0, sizeof(pcb->pcb_fpu));
    303 			l->l_md.md_flags |= MDLWP_USEDFPU;
    304 		}
    305 
    306 		if ((rv = fpu_emulate(tf, (struct fpreg *)&pcb->pcb_fpu))) {
    307 			KSI_INIT_TRAP(&ksi);
    308 			ksi.ksi_signo = rv;
    309 			ksi.ksi_trap = EXC_PGM;
    310 			ksi.ksi_addr = (void *)tf->tf_srr0;
    311 			trapsignal(l, &ksi);
    312 		}
    313 		break;
    314 
    315 	case EXC_MCHK:
    316 		{
    317 			struct faultbuf *fb;
    318 
    319 			pcb = lwp_getpcb(l);
    320 			if ((fb = pcb->pcb_onfault) != NULL) {
    321 				tf->tf_pid = KERNEL_PID;
    322 				tf->tf_srr0 = fb->fb_pc;
    323 				tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
    324 				tf->tf_fixreg[1] = fb->fb_sp;
    325 				tf->tf_fixreg[2] = fb->fb_r2;
    326 				tf->tf_fixreg[3] = 1; /* Return TRUE */
    327 				tf->tf_cr = fb->fb_cr;
    328 				memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
    329 				    sizeof(fb->fb_fixreg));
    330 				goto done;
    331 			}
    332 		}
    333 		goto brain_damage;
    334 	default:
    335  brain_damage:
    336 		printf("trap type 0x%x at 0x%lx\n", type, tf->tf_srr0);
    337 #if defined(DDB) || defined(KGDB)
    338 		if (kdb_trap(type, tf))
    339 			goto done;
    340 #endif
    341 #ifdef TRAP_PANICWAIT
    342 		printf("Press a key to panic.\n");
    343 		cngetc();
    344 #endif
    345 		panic("trap");
    346 	}
    347 
    348 	/* Invoke MI userret code */
    349 	mi_userret(l);
    350  done:
    351 	return;
    352 }
    353 
    354 int
    355 ctx_setup(int ctx, int srr1)
    356 {
    357 	volatile struct pmap *pm;
    358 
    359 	/* Update PID if we're returning to user mode. */
    360 	if (srr1 & PSL_PR) {
    361 		pm = curproc->p_vmspace->vm_map.pmap;
    362 		if (!pm->pm_ctx) {
    363 			ctx_alloc(__UNVOLATILE(pm));
    364 		}
    365 		ctx = pm->pm_ctx;
    366 		if (srr1 & PSL_SE) {
    367 			int dbreg, mask = 0x48000000;
    368 				/*
    369 				 * Set the Internal Debug and
    370 				 * Instruction Completion bits of
    371 				 * the DBCR0 register.
    372 				 *
    373 				 * XXX this is also used by jtag debuggers...
    374 				 */
    375 			__asm volatile("mfspr %0,0x3f2;"
    376 			    "or %0,%0,%1;"
    377 			    "mtspr 0x3f2,%0;" :
    378 			    "=&r" (dbreg) : "r" (mask));
    379 		}
    380 	}
    381 	else if (!ctx) {
    382 		ctx = KERNEL_PID;
    383 	}
    384 	return (ctx);
    385 }
    386 
    387 /*
    388  * Used by copyin()/copyout()
    389  */
    390 extern vaddr_t vmaprange(struct proc *, vaddr_t, vsize_t, int);
    391 extern void vunmaprange(vaddr_t, vsize_t);
    392 static int bigcopyin(const void *, void *, size_t );
    393 static int bigcopyout(const void *, void *, size_t );
    394 
    395 int
    396 copyin(const void *udaddr, void *kaddr, size_t len)
    397 {
    398 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    399 	int rv, msr, pid, tmp, ctx, count = 0;
    400 	struct faultbuf env;
    401 
    402 	/* For bigger buffers use the faster copy */
    403 	if (len > 1024)
    404 		return (bigcopyin(udaddr, kaddr, len));
    405 
    406 	if ((rv = setfault(&env))) {
    407 		curpcb->pcb_onfault = NULL;
    408 		return rv;
    409 	}
    410 
    411 	if (!(ctx = pm->pm_ctx)) {
    412 		/* No context -- assign it one */
    413 		ctx_alloc(pm);
    414 		ctx = pm->pm_ctx;
    415 	}
    416 
    417 	__asm volatile(
    418 		"   mfmsr %[msr];"          /* Save MSR */
    419 		"   li %[pid],0x20; "
    420 		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */
    421 		"   mfpid %[pid];"          /* Save old PID */
    422 		"   sync; isync;"
    423 
    424 		"   srwi. %[count],%[len],0x2;"     /* How many words? */
    425 		"   beq-  2f;"              /* No words. Go do bytes */
    426 		"   mtctr %[count];"
    427 		"1: mtpid %[ctx]; sync;"
    428 		"   lswi %[tmp],%[udaddr],4;"       /* Load user word */
    429 		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
    430 		"   sync; isync;"
    431 		"   mtpid %[pid];sync;"
    432 		"   stswi %[tmp],%[kaddr],4;"        /* Store kernel word */
    433 		"   dcbf 0,%[kaddr];"           /* flush cache */
    434 		"   addi %[kaddr],%[kaddr],0x4;"    /* next udaddr word */
    435 		"   sync; isync;"
    436 		"   bdnz 1b;"               /* repeat */
    437 
    438 		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
    439 		"   addi %[count],%[count],0x1;"
    440 		"   mtctr %[count];"
    441 		"3: bdz 10f;"               /* while count */
    442 		"   mtpid %[ctx];sync;"
    443 		"   lbz %[tmp],0(%[udaddr]);"       /* Load user byte */
    444 		"   addi %[udaddr],%[udaddr],0x1;"  /* next udaddr byte */
    445 		"   sync; isync;"
    446 		"   mtpid %[pid]; sync;"
    447 		"   stb %[tmp],0(%[kaddr]);"        /* Store kernel byte */
    448 		"   dcbf 0,%[kaddr];"           /* flush cache */
    449 		"   addi %[kaddr],%[kaddr],0x1;"
    450 		"   sync; isync;"
    451 		"   b 3b;"
    452 		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
    453 		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
    454 		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
    455 
    456 	curpcb->pcb_onfault = NULL;
    457 	return 0;
    458 }
    459 
    460 static int
    461 bigcopyin(const void *udaddr, void *kaddr, size_t len)
    462 {
    463 	const char *up;
    464 	char *kp = kaddr;
    465 	struct lwp *l = curlwp;
    466 	struct proc *p;
    467 	struct faultbuf env;
    468 	int error;
    469 
    470 	p = l->l_proc;
    471 
    472 	/*
    473 	 * Stolen from physio():
    474 	 */
    475 	error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
    476 	if (error) {
    477 		return error;
    478 	}
    479 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
    480 
    481 	if ((error = setfault(&env)) == 0) {
    482 		memcpy(kp, up, len);
    483 	}
    484 
    485 	curpcb->pcb_onfault = NULL;
    486 	vunmaprange((vaddr_t)up, len);
    487 	uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
    488 
    489 	return error;
    490 }
    491 
    492 int
    493 copyout(const void *kaddr, void *udaddr, size_t len)
    494 {
    495 	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
    496 	int rv, msr, pid, tmp, ctx, count = 0;
    497 	struct faultbuf env;
    498 
    499 	/* For big copies use more efficient routine */
    500 	if (len > 1024)
    501 		return (bigcopyout(kaddr, udaddr, len));
    502 
    503 	if ((rv = setfault(&env))) {
    504 		curpcb->pcb_onfault = NULL;
    505 		return rv;
    506 	}
    507 
    508 	if (!(ctx = pm->pm_ctx)) {
    509 		/* No context -- assign it one */
    510 		ctx_alloc(pm);
    511 		ctx = pm->pm_ctx;
    512 	}
    513 
    514 	__asm volatile(
    515 		"   mfmsr %[msr];"          /* Save MSR */ \
    516 		"   li %[pid],0x20; " \
    517 		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */ \
    518 		"   mfpid %[pid];"          /* Save old PID */ \
    519 		"   sync; isync;"
    520 
    521 		"   srwi. %[count],%[len],0x2;"     /* How many words? */
    522 		"   beq-  2f;"              /* No words. Go do bytes */
    523 		"   mtctr %[count];"
    524 		"1: mtpid %[pid];sync;"
    525 		"   lswi %[tmp],%[kaddr],4;"        /* Load kernel word */
    526 		"   addi %[kaddr],%[kaddr],0x4;"    /* next kaddr word */
    527 		"   sync; isync;"
    528 		"   mtpid %[ctx]; sync;"
    529 		"   stswi %[tmp],%[udaddr],4;"       /* Store user word */
    530 		"   dcbf 0,%[udaddr];"          /* flush cache */
    531 		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
    532 		"   sync; isync;"
    533 		"   bdnz 1b;"               /* repeat */
    534 
    535 		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
    536 		"   addi %[count],%[count],0x1;"
    537 		"   mtctr %[count];"
    538 		"3: bdz  10f;"              /* while count */
    539 		"   mtpid %[pid];sync;"
    540 		"   lbz %[tmp],0(%[kaddr]);"        /* Load kernel byte */
    541 		"   addi %[kaddr],%[kaddr],0x1;"    /* next kaddr byte */
    542 		"   sync; isync;"
    543 		"   mtpid %[ctx]; sync;"
    544 		"   stb %[tmp],0(%[udaddr]);"       /* Store user byte */
    545 		"   dcbf 0,%[udaddr];"          /* flush cache */
    546 		"   addi %[udaddr],%[udaddr],0x1;"
    547 		"   sync; isync;"
    548 		"   b 3b;"
    549 		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
    550 		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
    551 		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
    552 
    553 	curpcb->pcb_onfault = NULL;
    554 	return 0;
    555 }
    556 
    557 static int
    558 bigcopyout(const void *kaddr, void *udaddr, size_t len)
    559 {
    560 	char *up;
    561 	const char *kp = (const char *)kaddr;
    562 	struct lwp *l = curlwp;
    563 	struct proc *p;
    564 	struct faultbuf env;
    565 	int error;
    566 
    567 	p = l->l_proc;
    568 
    569 	/*
    570 	 * Stolen from physio():
    571 	 */
    572 	error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
    573 	if (error) {
    574 		return error;
    575 	}
    576 	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
    577 	    VM_PROT_READ | VM_PROT_WRITE);
    578 
    579 	if ((error = setfault(&env)) == 0) {
    580 		memcpy(up, kp, len);
    581 	}
    582 
    583 	curpcb->pcb_onfault = NULL;
    584 	vunmaprange((vaddr_t)up, len);
    585 	uvm_vsunlock(p->p_vmspace, udaddr, len);
    586 
    587 	return error;
    588 }
    589 
    590 /*
    591  * kcopy(const void *src, void *dst, size_t len);
    592  *
    593  * Copy len bytes from src to dst, aborting if we encounter a fatal
    594  * page fault.
    595  *
    596  * kcopy() _must_ save and restore the old fault handler since it is
    597  * called by uiomove(), which may be in the path of servicing a non-fatal
    598  * page fault.
    599  */
    600 int
    601 kcopy(const void *src, void *dst, size_t len)
    602 {
    603 	struct faultbuf env, *oldfault;
    604 	int rv;
    605 
    606 	oldfault = curpcb->pcb_onfault;
    607 	if ((rv = setfault(&env))) {
    608 		curpcb->pcb_onfault = oldfault;
    609 		return rv;
    610 	}
    611 
    612 	memcpy(dst, src, len);
    613 
    614 	curpcb->pcb_onfault = oldfault;
    615 	return 0;
    616 }
    617 
    618 int
    619 badaddr(void *addr, size_t size)
    620 {
    621 
    622 	return badaddr_read(addr, size, NULL);
    623 }
    624 
    625 int
    626 badaddr_read(void *addr, size_t size, int *rptr)
    627 {
    628 	struct faultbuf env;
    629 	int x;
    630 
    631 	/* Get rid of any stale machine checks that have been waiting.  */
    632 	__asm volatile ("sync; isync");
    633 
    634 	if (setfault(&env)) {
    635 		curpcb->pcb_onfault = NULL;
    636 		__asm volatile ("sync");
    637 		return 1;
    638 	}
    639 
    640 	__asm volatile ("sync");
    641 
    642 	switch (size) {
    643 	case 1:
    644 		x = *(volatile int8_t *)addr;
    645 		break;
    646 	case 2:
    647 		x = *(volatile int16_t *)addr;
    648 		break;
    649 	case 4:
    650 		x = *(volatile int32_t *)addr;
    651 		break;
    652 	default:
    653 		panic("badaddr: invalid size (%d)", size);
    654 	}
    655 
    656 	/* Make sure we took the machine check, if we caused one. */
    657 	__asm volatile ("sync; isync");
    658 
    659 	curpcb->pcb_onfault = NULL;
    660 	__asm volatile ("sync");	/* To be sure. */
    661 
    662 	/* Use the value to avoid reorder. */
    663 	if (rptr)
    664 		*rptr = x;
    665 
    666 	return 0;
    667 }
    668 
    669 /*
    670  * For now, this only deals with the particular unaligned access case
    671  * that gcc tends to generate.  Eventually it should handle all of the
    672  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
    673  */
    674 
    675 static int
    676 fix_unaligned(struct lwp *l, struct trapframe *tf)
    677 {
    678 
    679 	return -1;
    680 }
    681