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