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