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