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