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