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