trap.c revision 1.62
1/*	$NetBSD: trap.c,v 1.62 2011/06/21 06:38:50 matt 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 2011/06/21 06:38:50 matt 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/sa.h>
82#include <sys/savar.h>
83#include <sys/userret.h>
84#include <sys/kauth.h>
85#include <sys/cpu.h>
86
87#if defined(KGDB)
88#include <sys/kgdb.h>
89#endif
90
91#include <uvm/uvm_extern.h>
92
93#include <dev/cons.h>
94
95#include <machine/fpu.h>
96#include <machine/frame.h>
97#include <machine/pcb.h>
98#include <machine/psl.h>
99#include <machine/trap.h>
100
101#include <powerpc/db_machdep.h>
102#include <powerpc/spr.h>
103#include <powerpc/ibm4xx/spr.h>
104
105#include <powerpc/ibm4xx/cpu.h>
106#include <powerpc/ibm4xx/pmap.h>
107#include <powerpc/ibm4xx/tlb.h>
108
109#include <powerpc/fpu/fpu_extern.h>
110
111/* These definitions should probably be somewhere else			XXX */
112#define	FIRSTARG	3		/* first argument is in reg 3 */
113#define	NARGREG		8		/* 8 args are in registers */
114#define	MOREARGS(sp)	((void *)((int)(sp) + 8)) /* more args go here */
115
116static int fix_unaligned(struct lwp *l, struct trapframe *frame);
117
118void trap(struct trapframe *);	/* Called from locore / trap_subr */
119/* Why are these not defined in a header? */
120int badaddr(void *, size_t);
121int badaddr_read(void *, size_t, int *);
122int ctx_setup(int, int);
123
124#ifdef DEBUG
125#define TDB_ALL	0x1
126int trapdebug = /* TDB_ALL */ 0;
127#define	DBPRINTF(x, y)	if (trapdebug & (x)) printf y
128#else
129#define DBPRINTF(x, y)
130#endif
131
132void
133trap(struct trapframe *tf)
134{
135	struct lwp *l = curlwp;
136	struct proc *p = l->l_proc;
137	struct pcb *pcb;
138	int type = tf->tf_exc;
139	int ftype, rv;
140	ksiginfo_t ksi;
141
142	KASSERT(l->l_stat == LSONPROC);
143
144	if (tf->tf_srr1 & PSL_PR) {
145		LWP_CACHE_CREDS(l, p);
146		type |= EXC_USER;
147	}
148
149	ftype = VM_PROT_READ;
150
151	DBPRINTF(TDB_ALL, ("trap(%x) at %lx from frame %p &frame %p\n",
152	    type, tf->tf_srr0, tf, &tf));
153
154	switch (type) {
155	case EXC_DEBUG|EXC_USER:
156		{
157			int srr2, srr3;
158
159			__asm volatile("mfspr %0,0x3f0" :
160			    "=r" (rv), "=r" (srr2), "=r" (srr3) :);
161			printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2,
162			    srr3);
163			/* XXX fall through or break here?! */
164		}
165		/*
166		 * DEBUG intr -- probably single-step.
167		 */
168	case EXC_TRC|EXC_USER:
169		tf->tf_srr1 &= ~PSL_SE;
170		KSI_INIT_TRAP(&ksi);
171		ksi.ksi_signo = SIGTRAP;
172		ksi.ksi_trap = EXC_TRC;
173		ksi.ksi_addr = (void *)tf->tf_srr0;
174		trapsignal(l, &ksi);
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			va = tf->tf_dear;
190			if (tf->tf_pid == KERNEL_PID) {
191				map = kernel_map;
192			} else {
193				map = &p->p_vmspace->vm_map;
194				if ((l->l_flag & LW_SA)
195				    && (~l->l_pflag & LP_SA_NOBLOCK)) {
196					l->l_savp->savp_faultaddr = va;
197					l->l_pflag |= LP_SA_PAGEFAULT;
198				}
199			}
200
201			if (tf->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			    tf->tf_srr0,
207			    (ftype & VM_PROT_WRITE) ? "write" : "read",
208			    (void *)va, tf->tf_esr));
209
210			pcb = lwp_getpcb(l);
211			fb = pcb->pcb_onfault;
212			pcb->pcb_onfault = NULL;
213			rv = uvm_fault(map, trunc_page(va), ftype);
214			pcb->pcb_onfault = fb;
215			if (map != kernel_map) {
216				l->l_pflag &= ~LP_SA_PAGEFAULT;
217			}
218			if (rv == 0)
219				goto done;
220			if (fb != NULL) {
221				tf->tf_pid = KERNEL_PID;
222				tf->tf_srr0 = fb->fb_pc;
223				tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
224				tf->tf_cr = fb->fb_cr;
225				tf->tf_fixreg[1] = fb->fb_sp;
226				tf->tf_fixreg[2] = fb->fb_r2;
227				tf->tf_fixreg[3] = 1; /* Return TRUE */
228				memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
229				    sizeof(fb->fb_fixreg));
230				goto done;
231			}
232		}
233		goto brain_damage;
234
235	case EXC_DSI|EXC_USER:
236		/* FALLTHROUGH */
237	case EXC_DTMISS|EXC_USER:
238		if (tf->tf_esr & (ESR_DST|ESR_DIZ))
239			ftype = VM_PROT_WRITE;
240
241		DBPRINTF(TDB_ALL,
242		    ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
243		    tf->tf_srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
244		    tf->tf_dear, tf->tf_esr));
245		KASSERT(l == curlwp && (l->l_stat == LSONPROC));
246		if (l->l_flag & LW_SA) {
247			l->l_savp->savp_faultaddr = (vaddr_t)tf->tf_dear;
248			l->l_pflag |= LP_SA_PAGEFAULT;
249		}
250//		KASSERT(curpcb->pcb_onfault == NULL);
251		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_dear),
252		    ftype);
253		if (rv == 0) {
254			l->l_pflag &= ~LP_SA_PAGEFAULT;
255			break;
256		}
257		KSI_INIT_TRAP(&ksi);
258		ksi.ksi_signo = SIGSEGV;
259		ksi.ksi_trap = EXC_DSI;
260		ksi.ksi_addr = (void *)tf->tf_dear;
261		if (rv == ENOMEM) {
262			printf("UVM: pid %d (%s) lid %d, uid %d killed: "
263			    "out of swap\n",
264			    p->p_pid, p->p_comm, l->l_lid,
265			    l->l_cred ?
266			    kauth_cred_geteuid(l->l_cred) : -1);
267			ksi.ksi_signo = SIGKILL;
268		}
269		trapsignal(l, &ksi);
270		l->l_pflag &= ~LP_SA_PAGEFAULT;
271		break;
272
273	case EXC_ITMISS|EXC_USER:
274	case EXC_ISI|EXC_USER:
275		if (l->l_flag & LW_SA) {
276			l->l_savp->savp_faultaddr = (vaddr_t)tf->tf_srr0;
277			l->l_pflag |= LP_SA_PAGEFAULT;
278		}
279		ftype = VM_PROT_EXECUTE;
280		DBPRINTF(TDB_ALL,
281		    ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
282		    tf->tf_srr0, tf));
283//		KASSERT(curpcb->pcb_onfault == NULL);
284		rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_srr0),
285		    ftype);
286		if (rv == 0) {
287			l->l_pflag &= ~LP_SA_PAGEFAULT;
288			break;
289		}
290		KSI_INIT_TRAP(&ksi);
291		ksi.ksi_signo = SIGSEGV;
292		ksi.ksi_trap = EXC_ISI;
293		ksi.ksi_addr = (void *)tf->tf_srr0;
294		ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
295		trapsignal(l, &ksi);
296		l->l_pflag &= ~LP_SA_PAGEFAULT;
297		break;
298
299	case EXC_AST|EXC_USER:
300		cpu_ast(l, curcpu());
301		break;
302
303	case EXC_ALI|EXC_USER:
304		if (fix_unaligned(l, tf) != 0) {
305			KSI_INIT_TRAP(&ksi);
306			ksi.ksi_signo = SIGBUS;
307			ksi.ksi_trap = EXC_ALI;
308			ksi.ksi_addr = (void *)tf->tf_dear;
309			trapsignal(l, &ksi);
310		} else
311			tf->tf_srr0 += 4;
312		break;
313
314	case EXC_PGM|EXC_USER:
315		/*
316		 * Illegal insn:
317		 *
318		 * let's try to see if it's FPU and can be emulated.
319		 */
320		curcpu()->ci_data.cpu_ntrap++;
321		pcb = lwp_getpcb(l);
322
323		if (!(l->l_md.md_flags & MDLWP_USEDFPU)) {
324			memset(&pcb->pcb_fpu, 0, sizeof(pcb->pcb_fpu));
325			l->l_md.md_flags |= MDLWP_USEDFPU;
326		}
327
328		if ((rv = fpu_emulate(tf, (struct fpreg *)&pcb->pcb_fpu))) {
329			KSI_INIT_TRAP(&ksi);
330			ksi.ksi_signo = rv;
331			ksi.ksi_trap = EXC_PGM;
332			ksi.ksi_addr = (void *)tf->tf_srr0;
333			trapsignal(l, &ksi);
334		}
335		break;
336
337	case EXC_MCHK:
338		{
339			struct faultbuf *fb;
340
341			pcb = lwp_getpcb(l);
342			if ((fb = pcb->pcb_onfault) != NULL) {
343				tf->tf_pid = KERNEL_PID;
344				tf->tf_srr0 = fb->fb_pc;
345				tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
346				tf->tf_fixreg[1] = fb->fb_sp;
347				tf->tf_fixreg[2] = fb->fb_r2;
348				tf->tf_fixreg[3] = 1; /* Return TRUE */
349				tf->tf_cr = fb->fb_cr;
350				memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
351				    sizeof(fb->fb_fixreg));
352				goto done;
353			}
354		}
355		goto brain_damage;
356	default:
357 brain_damage:
358		printf("trap type 0x%x at 0x%lx\n", type, tf->tf_srr0);
359#if defined(DDB) || defined(KGDB)
360		if (kdb_trap(type, tf))
361			goto done;
362#endif
363#ifdef TRAP_PANICWAIT
364		printf("Press a key to panic.\n");
365		cngetc();
366#endif
367		panic("trap");
368	}
369
370	/* Invoke MI userret code */
371	mi_userret(l);
372 done:
373	return;
374}
375
376int
377ctx_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 */
412extern vaddr_t vmaprange(struct proc *, vaddr_t, vsize_t, int);
413extern void vunmaprange(vaddr_t, vsize_t);
414static int bigcopyin(const void *, void *, size_t );
415static int bigcopyout(const void *, void *, size_t );
416
417int
418copyin(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		"   mfpid %[pid];"          /* Save old PID */
444		"   sync; isync;"
445
446		"   srwi. %[count],%[len],0x2;"     /* How many words? */
447		"   beq-  2f;"              /* No words. Go do bytes */
448		"   mtctr %[count];"
449		"1: mtpid %[ctx]; sync;"
450		"   lswi %[tmp],%[udaddr],4;"       /* Load user word */
451		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
452		"   sync; isync;"
453		"   mtpid %[pid];sync;"
454		"   stswi %[tmp],%[kaddr],4;"        /* Store kernel word */
455		"   dcbf 0,%[kaddr];"           /* flush cache */
456		"   addi %[kaddr],%[kaddr],0x4;"    /* next udaddr word */
457		"   sync; isync;"
458		"   bdnz 1b;"               /* repeat */
459
460		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
461		"   addi %[count],%[count],0x1;"
462		"   mtctr %[count];"
463		"3: bdz 10f;"               /* while count */
464		"   mtpid %[ctx];sync;"
465		"   lbz %[tmp],0(%[udaddr]);"       /* Load user byte */
466		"   addi %[udaddr],%[udaddr],0x1;"  /* next udaddr byte */
467		"   sync; isync;"
468		"   mtpid %[pid]; sync;"
469		"   stb %[tmp],0(%[kaddr]);"        /* Store kernel byte */
470		"   dcbf 0,%[kaddr];"           /* flush cache */
471		"   addi %[kaddr],%[kaddr],0x1;"
472		"   sync; isync;"
473		"   b 3b;"
474		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
475		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
476		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
477
478	curpcb->pcb_onfault = NULL;
479	return 0;
480}
481
482static int
483bigcopyin(const void *udaddr, void *kaddr, size_t len)
484{
485	const char *up;
486	char *kp = kaddr;
487	struct lwp *l = curlwp;
488	struct proc *p;
489	struct faultbuf env;
490	int error;
491
492	p = l->l_proc;
493
494	/*
495	 * Stolen from physio():
496	 */
497	error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
498	if (error) {
499		return error;
500	}
501	up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
502
503	if ((error = setfault(&env)) == 0) {
504		memcpy(kp, up, len);
505	}
506
507	curpcb->pcb_onfault = NULL;
508	vunmaprange((vaddr_t)up, len);
509	uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
510
511	return error;
512}
513
514int
515copyout(const void *kaddr, void *udaddr, size_t len)
516{
517	struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
518	int rv, msr, pid, tmp, ctx, count = 0;
519	struct faultbuf env;
520
521	/* For big copies use more efficient routine */
522	if (len > 1024)
523		return (bigcopyout(kaddr, udaddr, len));
524
525	if ((rv = setfault(&env))) {
526		curpcb->pcb_onfault = NULL;
527		return rv;
528	}
529
530	if (!(ctx = pm->pm_ctx)) {
531		/* No context -- assign it one */
532		ctx_alloc(pm);
533		ctx = pm->pm_ctx;
534	}
535
536	__asm volatile(
537		"   mfmsr %[msr];"          /* Save MSR */ \
538		"   li %[pid],0x20; " \
539		"   andc %[pid],%[msr],%[pid]; mtmsr %[pid];"   /* Disable IMMU */ \
540		"   mfpid %[pid];"          /* Save old PID */ \
541		"   sync; isync;"
542
543		"   srwi. %[count],%[len],0x2;"     /* How many words? */
544		"   beq-  2f;"              /* No words. Go do bytes */
545		"   mtctr %[count];"
546		"1: mtpid %[pid];sync;"
547		"   lswi %[tmp],%[kaddr],4;"        /* Load kernel word */
548		"   addi %[kaddr],%[kaddr],0x4;"    /* next kaddr word */
549		"   sync; isync;"
550		"   mtpid %[ctx]; sync;"
551		"   stswi %[tmp],%[udaddr],4;"       /* Store user word */
552		"   dcbf 0,%[udaddr];"          /* flush cache */
553		"   addi %[udaddr],%[udaddr],0x4;"  /* next udaddr word */
554		"   sync; isync;"
555		"   bdnz 1b;"               /* repeat */
556
557		"2: andi. %[count],%[len],0x3;"     /* How many remaining bytes? */
558		"   addi %[count],%[count],0x1;"
559		"   mtctr %[count];"
560		"3: bdz  10f;"              /* while count */
561		"   mtpid %[pid];sync;"
562		"   lbz %[tmp],0(%[kaddr]);"        /* Load kernel byte */
563		"   addi %[kaddr],%[kaddr],0x1;"    /* next kaddr byte */
564		"   sync; isync;"
565		"   mtpid %[ctx]; sync;"
566		"   stb %[tmp],0(%[udaddr]);"       /* Store user byte */
567		"   dcbf 0,%[udaddr];"          /* flush cache */
568		"   addi %[udaddr],%[udaddr],0x1;"
569		"   sync; isync;"
570		"   b 3b;"
571		"10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
572		: [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
573		: [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
574
575	curpcb->pcb_onfault = NULL;
576	return 0;
577}
578
579static int
580bigcopyout(const void *kaddr, void *udaddr, size_t len)
581{
582	char *up;
583	const char *kp = (const char *)kaddr;
584	struct lwp *l = curlwp;
585	struct proc *p;
586	struct faultbuf env;
587	int error;
588
589	p = l->l_proc;
590
591	/*
592	 * Stolen from physio():
593	 */
594	error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
595	if (error) {
596		return error;
597	}
598	up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
599	    VM_PROT_READ | VM_PROT_WRITE);
600
601	if ((error = setfault(&env)) == 0) {
602		memcpy(up, kp, len);
603	}
604
605	curpcb->pcb_onfault = NULL;
606	vunmaprange((vaddr_t)up, len);
607	uvm_vsunlock(p->p_vmspace, udaddr, len);
608
609	return error;
610}
611
612/*
613 * kcopy(const void *src, void *dst, size_t len);
614 *
615 * Copy len bytes from src to dst, aborting if we encounter a fatal
616 * page fault.
617 *
618 * kcopy() _must_ save and restore the old fault handler since it is
619 * called by uiomove(), which may be in the path of servicing a non-fatal
620 * page fault.
621 */
622int
623kcopy(const void *src, void *dst, size_t len)
624{
625	struct faultbuf env, *oldfault;
626	int rv;
627
628	oldfault = curpcb->pcb_onfault;
629	if ((rv = setfault(&env))) {
630		curpcb->pcb_onfault = oldfault;
631		return rv;
632	}
633
634	memcpy(dst, src, len);
635
636	curpcb->pcb_onfault = oldfault;
637	return 0;
638}
639
640int
641badaddr(void *addr, size_t size)
642{
643
644	return badaddr_read(addr, size, NULL);
645}
646
647int
648badaddr_read(void *addr, size_t size, int *rptr)
649{
650	struct faultbuf env;
651	int x;
652
653	/* Get rid of any stale machine checks that have been waiting.  */
654	__asm volatile ("sync; isync");
655
656	if (setfault(&env)) {
657		curpcb->pcb_onfault = NULL;
658		__asm volatile ("sync");
659		return 1;
660	}
661
662	__asm volatile ("sync");
663
664	switch (size) {
665	case 1:
666		x = *(volatile int8_t *)addr;
667		break;
668	case 2:
669		x = *(volatile int16_t *)addr;
670		break;
671	case 4:
672		x = *(volatile int32_t *)addr;
673		break;
674	default:
675		panic("badaddr: invalid size (%d)", size);
676	}
677
678	/* Make sure we took the machine check, if we caused one. */
679	__asm volatile ("sync; isync");
680
681	curpcb->pcb_onfault = NULL;
682	__asm volatile ("sync");	/* To be sure. */
683
684	/* Use the value to avoid reorder. */
685	if (rptr)
686		*rptr = x;
687
688	return 0;
689}
690
691/*
692 * For now, this only deals with the particular unaligned access case
693 * that gcc tends to generate.  Eventually it should handle all of the
694 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
695 */
696
697static int
698fix_unaligned(struct lwp *l, struct trapframe *tf)
699{
700
701	return -1;
702}
703