trap.c revision 1.68 1 /* $NetBSD: trap.c,v 1.68 2016/12/16 06:29:11 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 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.68 2016/12/16 06:29:11 rin 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/userret.h>
82 #include <sys/kauth.h>
83 #include <sys/cpu.h>
84
85 #if defined(KGDB)
86 #include <sys/kgdb.h>
87 #endif
88
89 #include <uvm/uvm_extern.h>
90
91 #include <dev/cons.h>
92
93 #include <machine/fpu.h>
94 #include <machine/frame.h>
95 #include <machine/pcb.h>
96 #include <machine/psl.h>
97 #include <machine/trap.h>
98
99 #include <powerpc/db_machdep.h>
100 #include <powerpc/spr.h>
101 #include <powerpc/ibm4xx/spr.h>
102
103 #include <powerpc/ibm4xx/cpu.h>
104 #include <powerpc/ibm4xx/pmap.h>
105 #include <powerpc/ibm4xx/tlb.h>
106
107 #include <powerpc/fpu/fpu_extern.h>
108
109 /* These definitions should probably be somewhere else XXX */
110 #define FIRSTARG 3 /* first argument is in reg 3 */
111 #define NARGREG 8 /* 8 args are in registers */
112 #define MOREARGS(sp) ((void *)((int)(sp) + 8)) /* more args go here */
113
114 static int fix_unaligned(struct lwp *l, struct trapframe *tf);
115
116 void trap(struct trapframe *); /* Called from locore / trap_subr */
117 /* Why are these not defined in a header? */
118 int badaddr(void *, size_t);
119 int badaddr_read(void *, size_t, int *);
120 int ctx_setup(int, int);
121
122 #ifdef DEBUG
123 #define TDB_ALL 0x1
124 int trapdebug = /* TDB_ALL */ 0;
125 #define DBPRINTF(x, y) if (trapdebug & (x)) printf y
126 #else
127 #define DBPRINTF(x, y)
128 #endif
129
130 void
131 trap(struct trapframe *tf)
132 {
133 struct lwp *l = curlwp;
134 struct proc *p = l->l_proc;
135 struct pcb *pcb;
136 int type = tf->tf_exc;
137 int ftype, rv;
138 ksiginfo_t ksi;
139
140 KASSERT(l->l_stat == LSONPROC);
141
142 if (tf->tf_srr1 & PSL_PR) {
143 LWP_CACHE_CREDS(l, p);
144 type |= EXC_USER;
145 }
146
147 ftype = VM_PROT_READ;
148
149 DBPRINTF(TDB_ALL, ("trap(%x) at %lx from frame %p &frame %p\n",
150 type, tf->tf_srr0, tf, &tf));
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 tf->tf_srr1 &= ~PSL_SE;
168 KSI_INIT_TRAP(&ksi);
169 ksi.ksi_signo = SIGTRAP;
170 ksi.ksi_trap = EXC_TRC;
171 ksi.ksi_addr = (void *)tf->tf_srr0;
172 trapsignal(l, &ksi);
173 break;
174
175 /*
176 * If we could not find and install appropriate TLB entry, fall through.
177 */
178
179 case EXC_DSI:
180 /* FALLTHROUGH */
181 case EXC_DTMISS:
182 {
183 struct vm_map *map;
184 vaddr_t va;
185 struct faultbuf *fb = NULL;
186
187 va = tf->tf_dear;
188 if (tf->tf_pid == KERNEL_PID) {
189 map = kernel_map;
190 } else {
191 map = &p->p_vmspace->vm_map;
192 }
193
194 if (tf->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 tf->tf_srr0,
200 (ftype & VM_PROT_WRITE) ? "write" : "read",
201 (void *)va, tf->tf_esr));
202
203 pcb = lwp_getpcb(l);
204 fb = pcb->pcb_onfault;
205 pcb->pcb_onfault = NULL;
206 rv = uvm_fault(map, trunc_page(va), ftype);
207 pcb->pcb_onfault = fb;
208 if (rv == 0)
209 return;
210 if (fb != NULL) {
211 tf->tf_pid = KERNEL_PID;
212 tf->tf_srr0 = fb->fb_pc;
213 tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
214 tf->tf_cr = fb->fb_cr;
215 tf->tf_fixreg[1] = fb->fb_sp;
216 tf->tf_fixreg[2] = fb->fb_r2;
217 tf->tf_fixreg[3] = 1; /* Return TRUE */
218 memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
219 sizeof(fb->fb_fixreg));
220 return;
221 }
222 }
223 goto brain_damage;
224
225 case EXC_DSI|EXC_USER:
226 /* FALLTHROUGH */
227 case EXC_DTMISS|EXC_USER:
228 if (tf->tf_esr & (ESR_DST|ESR_DIZ))
229 ftype = VM_PROT_WRITE;
230
231 DBPRINTF(TDB_ALL,
232 ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
233 tf->tf_srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
234 tf->tf_dear, tf->tf_esr));
235 KASSERT(l == curlwp && (l->l_stat == LSONPROC));
236 // KASSERT(curpcb->pcb_onfault == NULL);
237 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_dear),
238 ftype);
239 if (rv == 0) {
240 break;
241 }
242 KSI_INIT_TRAP(&ksi);
243 ksi.ksi_signo = SIGSEGV;
244 ksi.ksi_trap = EXC_DSI;
245 ksi.ksi_addr = (void *)tf->tf_dear;
246 if (rv == ENOMEM) {
247 printf("UVM: pid %d (%s) lid %d, uid %d killed: "
248 "out of swap\n",
249 p->p_pid, p->p_comm, l->l_lid,
250 l->l_cred ?
251 kauth_cred_geteuid(l->l_cred) : -1);
252 ksi.ksi_signo = SIGKILL;
253 }
254 trapsignal(l, &ksi);
255 break;
256
257 case EXC_ITMISS|EXC_USER:
258 case EXC_ISI|EXC_USER:
259 ftype = VM_PROT_EXECUTE;
260 DBPRINTF(TDB_ALL,
261 ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
262 tf->tf_srr0, tf));
263 // KASSERT(curpcb->pcb_onfault == NULL);
264 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(tf->tf_srr0),
265 ftype);
266 if (rv == 0) {
267 break;
268 }
269 KSI_INIT_TRAP(&ksi);
270 ksi.ksi_signo = SIGSEGV;
271 ksi.ksi_trap = EXC_ISI;
272 ksi.ksi_addr = (void *)tf->tf_srr0;
273 ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
274 trapsignal(l, &ksi);
275 break;
276
277 case EXC_AST|EXC_USER:
278 cpu_ast(l, curcpu());
279 break;
280
281 case EXC_ALI|EXC_USER:
282 if (fix_unaligned(l, tf) != 0) {
283 KSI_INIT_TRAP(&ksi);
284 ksi.ksi_signo = SIGBUS;
285 ksi.ksi_trap = EXC_ALI;
286 ksi.ksi_addr = (void *)tf->tf_dear;
287 trapsignal(l, &ksi);
288 } else
289 tf->tf_srr0 += 4;
290 break;
291
292 case EXC_PGM|EXC_USER:
293 /*
294 * Illegal insn:
295 *
296 * let's try to see if its FPU and can be emulated.
297 */
298 curcpu()->ci_data.cpu_ntrap++;
299 pcb = lwp_getpcb(l);
300
301 if (__predict_false(!fpu_used_p(l))) {
302 memset(&pcb->pcb_fpu, 0, sizeof(pcb->pcb_fpu));
303 fpu_mark_used(l);
304 }
305
306 if (fpu_emulate(tf, &pcb->pcb_fpu, &ksi)) {
307 if (ksi.ksi_signo == 0) /* was emulated */
308 break;
309 } else {
310 ksi.ksi_signo = SIGILL;
311 ksi.ksi_code = ILL_ILLOPC;
312 ksi.ksi_trap = EXC_PGM;
313 ksi.ksi_addr = (void *)tf->tf_srr0;
314 }
315
316 trapsignal(l, &ksi);
317 break;
318
319 case EXC_MCHK:
320 {
321 struct faultbuf *fb;
322
323 pcb = lwp_getpcb(l);
324 if ((fb = pcb->pcb_onfault) != NULL) {
325 tf->tf_pid = KERNEL_PID;
326 tf->tf_srr0 = fb->fb_pc;
327 tf->tf_srr1 |= PSL_IR; /* Re-enable IMMU */
328 tf->tf_fixreg[1] = fb->fb_sp;
329 tf->tf_fixreg[2] = fb->fb_r2;
330 tf->tf_fixreg[3] = 1; /* Return TRUE */
331 tf->tf_cr = fb->fb_cr;
332 memcpy(&tf->tf_fixreg[13], fb->fb_fixreg,
333 sizeof(fb->fb_fixreg));
334 return;
335 }
336 }
337 goto brain_damage;
338
339 default:
340 brain_damage:
341 printf("trap type 0x%x at 0x%lx\n", type, tf->tf_srr0);
342 #if defined(DDB) || defined(KGDB)
343 if (kdb_trap(type, tf))
344 return;
345 #endif
346 #ifdef TRAP_PANICWAIT
347 printf("Press a key to panic.\n");
348 cngetc();
349 #endif
350 panic("trap");
351 }
352
353 /* Invoke MI userret code */
354 mi_userret(l);
355 }
356
357 int
358 ctx_setup(int ctx, int srr1)
359 {
360 volatile struct pmap *pm;
361
362 /* Update PID if we're returning to user mode. */
363 if (srr1 & PSL_PR) {
364 pm = curproc->p_vmspace->vm_map.pmap;
365 if (!pm->pm_ctx) {
366 ctx_alloc(__UNVOLATILE(pm));
367 }
368 ctx = pm->pm_ctx;
369 if (srr1 & PSL_SE) {
370 int dbreg, mask = 0x48000000;
371 /*
372 * Set the Internal Debug and
373 * Instruction Completion bits of
374 * the DBCR0 register.
375 *
376 * XXX this is also used by jtag debuggers...
377 */
378 __asm volatile("mfspr %0,0x3f2;"
379 "or %0,%0,%1;"
380 "mtspr 0x3f2,%0;" :
381 "=&r" (dbreg) : "r" (mask));
382 }
383 }
384 else if (!ctx) {
385 ctx = KERNEL_PID;
386 }
387 return (ctx);
388 }
389
390 /*
391 * Used by copyin()/copyout()
392 */
393 extern vaddr_t vmaprange(struct proc *, vaddr_t, vsize_t, int);
394 extern void vunmaprange(vaddr_t, vsize_t);
395 static int bigcopyin(const void *, void *, size_t );
396 static int bigcopyout(const void *, void *, size_t );
397
398 int
399 copyin(const void *udaddr, void *kaddr, size_t len)
400 {
401 struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
402 int rv, msr, pid, tmp, ctx, count = 0;
403 struct faultbuf env;
404
405 /* For bigger buffers use the faster copy */
406 if (len > 1024)
407 return (bigcopyin(udaddr, kaddr, len));
408
409 if ((rv = setfault(&env))) {
410 curpcb->pcb_onfault = NULL;
411 return rv;
412 }
413
414 if (!(ctx = pm->pm_ctx)) {
415 /* No context -- assign it one */
416 ctx_alloc(pm);
417 ctx = pm->pm_ctx;
418 }
419
420 __asm volatile(
421 " mfmsr %[msr];" /* Save MSR */
422 " li %[pid],0x20; "
423 " andc %[pid],%[msr],%[pid]; mtmsr %[pid];" /* Disable IMMU */
424 " mfpid %[pid];" /* Save old PID */
425 " sync; isync;"
426
427 " srwi. %[count],%[len],0x2;" /* How many words? */
428 " beq- 2f;" /* No words. Go do bytes */
429 " mtctr %[count];"
430 "1: mtpid %[ctx]; sync;"
431 " lswi %[tmp],%[udaddr],4;" /* Load user word */
432 " addi %[udaddr],%[udaddr],0x4;" /* next udaddr word */
433 " sync; isync;"
434 " mtpid %[pid];sync;"
435 " stswi %[tmp],%[kaddr],4;" /* Store kernel word */
436 " dcbf 0,%[kaddr];" /* flush cache */
437 " addi %[kaddr],%[kaddr],0x4;" /* next udaddr word */
438 " sync; isync;"
439 " bdnz 1b;" /* repeat */
440
441 "2: andi. %[count],%[len],0x3;" /* How many remaining bytes? */
442 " addi %[count],%[count],0x1;"
443 " mtctr %[count];"
444 "3: bdz 10f;" /* while count */
445 " mtpid %[ctx];sync;"
446 " lbz %[tmp],0(%[udaddr]);" /* Load user byte */
447 " addi %[udaddr],%[udaddr],0x1;" /* next udaddr byte */
448 " sync; isync;"
449 " mtpid %[pid]; sync;"
450 " stb %[tmp],0(%[kaddr]);" /* Store kernel byte */
451 " dcbf 0,%[kaddr];" /* flush cache */
452 " addi %[kaddr],%[kaddr],0x1;"
453 " sync; isync;"
454 " b 3b;"
455 "10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
456 : [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
457 : [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
458
459 curpcb->pcb_onfault = NULL;
460 return 0;
461 }
462
463 static int
464 bigcopyin(const void *udaddr, void *kaddr, size_t len)
465 {
466 const char *up;
467 char *kp = kaddr;
468 struct lwp *l = curlwp;
469 struct proc *p;
470 struct faultbuf env;
471 int error;
472
473 p = l->l_proc;
474
475 /*
476 * Stolen from physio():
477 */
478 error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
479 if (error) {
480 return error;
481 }
482 up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
483
484 if ((error = setfault(&env)) == 0) {
485 memcpy(kp, up, len);
486 }
487
488 curpcb->pcb_onfault = NULL;
489 vunmaprange((vaddr_t)up, len);
490 uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
491
492 return error;
493 }
494
495 int
496 copyout(const void *kaddr, void *udaddr, size_t len)
497 {
498 struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
499 int rv, msr, pid, tmp, ctx, count = 0;
500 struct faultbuf env;
501
502 /* For big copies use more efficient routine */
503 if (len > 1024)
504 return (bigcopyout(kaddr, udaddr, len));
505
506 if ((rv = setfault(&env))) {
507 curpcb->pcb_onfault = NULL;
508 return rv;
509 }
510
511 if (!(ctx = pm->pm_ctx)) {
512 /* No context -- assign it one */
513 ctx_alloc(pm);
514 ctx = pm->pm_ctx;
515 }
516
517 __asm volatile(
518 " mfmsr %[msr];" /* Save MSR */ \
519 " li %[pid],0x20; " \
520 " andc %[pid],%[msr],%[pid]; mtmsr %[pid];" /* Disable IMMU */ \
521 " mfpid %[pid];" /* Save old PID */ \
522 " sync; isync;"
523
524 " srwi. %[count],%[len],0x2;" /* How many words? */
525 " beq- 2f;" /* No words. Go do bytes */
526 " mtctr %[count];"
527 "1: mtpid %[pid];sync;"
528 " lswi %[tmp],%[kaddr],4;" /* Load kernel word */
529 " addi %[kaddr],%[kaddr],0x4;" /* next kaddr word */
530 " sync; isync;"
531 " mtpid %[ctx]; sync;"
532 " stswi %[tmp],%[udaddr],4;" /* Store user word */
533 " dcbf 0,%[udaddr];" /* flush cache */
534 " addi %[udaddr],%[udaddr],0x4;" /* next udaddr word */
535 " sync; isync;"
536 " bdnz 1b;" /* repeat */
537
538 "2: andi. %[count],%[len],0x3;" /* How many remaining bytes? */
539 " addi %[count],%[count],0x1;"
540 " mtctr %[count];"
541 "3: bdz 10f;" /* while count */
542 " mtpid %[pid];sync;"
543 " lbz %[tmp],0(%[kaddr]);" /* Load kernel byte */
544 " addi %[kaddr],%[kaddr],0x1;" /* next kaddr byte */
545 " sync; isync;"
546 " mtpid %[ctx]; sync;"
547 " stb %[tmp],0(%[udaddr]);" /* Store user byte */
548 " dcbf 0,%[udaddr];" /* flush cache */
549 " addi %[udaddr],%[udaddr],0x1;"
550 " sync; isync;"
551 " b 3b;"
552 "10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
553 : [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
554 : [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
555
556 curpcb->pcb_onfault = NULL;
557 return 0;
558 }
559
560 static int
561 bigcopyout(const void *kaddr, void *udaddr, size_t len)
562 {
563 char *up;
564 const char *kp = (const char *)kaddr;
565 struct lwp *l = curlwp;
566 struct proc *p;
567 struct faultbuf env;
568 int error;
569
570 p = l->l_proc;
571
572 /*
573 * Stolen from physio():
574 */
575 error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
576 if (error) {
577 return error;
578 }
579 up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
580 VM_PROT_READ | VM_PROT_WRITE);
581
582 if ((error = setfault(&env)) == 0) {
583 memcpy(up, kp, len);
584 }
585
586 curpcb->pcb_onfault = NULL;
587 vunmaprange((vaddr_t)up, len);
588 uvm_vsunlock(p->p_vmspace, udaddr, len);
589
590 return error;
591 }
592
593 /*
594 * kcopy(const void *src, void *dst, size_t len);
595 *
596 * Copy len bytes from src to dst, aborting if we encounter a fatal
597 * page fault.
598 *
599 * kcopy() _must_ save and restore the old fault handler since it is
600 * called by uiomove(), which may be in the path of servicing a non-fatal
601 * page fault.
602 */
603 int
604 kcopy(const void *src, void *dst, size_t len)
605 {
606 struct faultbuf env, *oldfault;
607 int rv;
608
609 oldfault = curpcb->pcb_onfault;
610 if ((rv = setfault(&env))) {
611 curpcb->pcb_onfault = oldfault;
612 return rv;
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 = NULL;
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 = NULL;
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 *tf)
680 {
681
682 return -1;
683 }
684