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