trap.c revision 1.48.12.2 1 /* $NetBSD: trap.c,v 1.48.12.2 2008/06/22 18:12:03 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.2 2008/06/22 18:12:03 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 if (l->l_flag & LW_SA) {
192 l->l_savp->savp_faultaddr = va;
193 l->l_pflag |= LP_SA_PAGEFAULT;
194 }
195 }
196
197 if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
198 ftype = VM_PROT_WRITE;
199
200 DBPRINTF(TDB_ALL,
201 ("trap(EXC_DSI) at %lx %s fault on %p esr %x\n",
202 frame->srr0,
203 (ftype & VM_PROT_WRITE) ? "write" : "read",
204 (void *)va, frame->tf_xtra[TF_ESR]));
205 rv = uvm_fault(map, trunc_page(va), ftype);
206 if (map != kernel_map) {
207 l->l_pflag &= ~LP_SA_PAGEFAULT;
208 }
209 if (rv == 0)
210 goto done;
211 if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
212 frame->tf_xtra[TF_PID] = KERNEL_PID;
213 frame->srr0 = fb->fb_pc;
214 frame->srr1 |= PSL_IR; /* Re-enable IMMU */
215 frame->fixreg[1] = fb->fb_sp;
216 frame->fixreg[2] = fb->fb_r2;
217 frame->fixreg[3] = 1; /* Return TRUE */
218 frame->cr = fb->fb_cr;
219 memcpy(&frame->fixreg[13], fb->fb_fixreg,
220 sizeof(fb->fb_fixreg));
221 goto done;
222 }
223 }
224 goto brain_damage;
225
226 case EXC_DSI|EXC_USER:
227 /* FALLTHROUGH */
228 case EXC_DTMISS|EXC_USER:
229 if (frame->tf_xtra[TF_ESR] & (ESR_DST|ESR_DIZ))
230 ftype = VM_PROT_WRITE;
231
232 DBPRINTF(TDB_ALL,
233 ("trap(EXC_DSI|EXC_USER) at %lx %s fault on %lx %x\n",
234 frame->srr0, (ftype & VM_PROT_WRITE) ? "write" : "read",
235 frame->dar, frame->tf_xtra[TF_ESR]));
236 KASSERT(l == curlwp && (l->l_stat == LSONPROC));
237 if (l->l_flag & LW_SA) {
238 l->l_savp->savp_faultaddr = (vaddr_t)frame->dar;
239 l->l_pflag |= LP_SA_PAGEFAULT;
240 }
241 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->dar),
242 ftype);
243 if (rv == 0) {
244 l->l_pflag &= ~LP_SA_PAGEFAULT;
245 break;
246 }
247 KSI_INIT_TRAP(&ksi);
248 ksi.ksi_signo = SIGSEGV;
249 ksi.ksi_trap = EXC_DSI;
250 ksi.ksi_addr = (void *)frame->dar;
251 if (rv == ENOMEM) {
252 printf("UVM: pid %d (%s) lid %d, uid %d killed: "
253 "out of swap\n",
254 p->p_pid, p->p_comm, l->l_lid,
255 l->l_cred ?
256 kauth_cred_geteuid(l->l_cred) : -1);
257 ksi.ksi_signo = SIGKILL;
258 }
259 trapsignal(l, &ksi);
260 l->l_pflag &= ~LP_SA_PAGEFAULT;
261 break;
262
263 case EXC_ITMISS|EXC_USER:
264 case EXC_ISI|EXC_USER:
265 if (l->l_flag & LW_SA) {
266 l->l_savp->savp_faultaddr = (vaddr_t)frame->srr0;
267 l->l_pflag |= LP_SA_PAGEFAULT;
268 }
269 ftype = VM_PROT_EXECUTE;
270 DBPRINTF(TDB_ALL,
271 ("trap(EXC_ISI|EXC_USER) at %lx execute fault tf %p\n",
272 frame->srr0, frame));
273 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0),
274 ftype);
275 if (rv == 0) {
276 l->l_pflag &= ~LP_SA_PAGEFAULT;
277 break;
278 }
279 KSI_INIT_TRAP(&ksi);
280 ksi.ksi_signo = SIGSEGV;
281 ksi.ksi_trap = EXC_ISI;
282 ksi.ksi_addr = (void *)frame->srr0;
283 ksi.ksi_code = (rv == EACCES ? SEGV_ACCERR : SEGV_MAPERR);
284 trapsignal(l, &ksi);
285 l->l_pflag &= ~LP_SA_PAGEFAULT;
286 break;
287
288 case EXC_AST|EXC_USER:
289 curcpu()->ci_astpending = 0; /* we are about to do it */
290 uvmexp.softs++;
291 if (l->l_pflag & LP_OWEUPC) {
292 l->l_pflag &= ~LP_OWEUPC;
293 ADDUPROF(l);
294 }
295 /* Check whether we are being preempted. */
296 if (curcpu()->ci_want_resched)
297 preempt();
298 break;
299
300
301 case EXC_ALI|EXC_USER:
302 if (fix_unaligned(l, frame) != 0) {
303 KSI_INIT_TRAP(&ksi);
304 ksi.ksi_signo = SIGBUS;
305 ksi.ksi_trap = EXC_ALI;
306 ksi.ksi_addr = (void *)frame->dar;
307 trapsignal(l, &ksi);
308 } else
309 frame->srr0 += 4;
310 break;
311
312 case EXC_PGM|EXC_USER:
313 /*
314 * Illegal insn:
315 *
316 * let's try to see if it's FPU and can be emulated.
317 */
318 uvmexp.traps++;
319 if (!(l->l_addr->u_pcb.pcb_flags & PCB_FPU)) {
320 memset(&l->l_addr->u_pcb.pcb_fpu, 0,
321 sizeof l->l_addr->u_pcb.pcb_fpu);
322 l->l_addr->u_pcb.pcb_flags |= PCB_FPU;
323 }
324
325 if ((rv = fpu_emulate(frame,
326 (struct fpreg *)&l->l_addr->u_pcb.pcb_fpu))) {
327 KSI_INIT_TRAP(&ksi);
328 ksi.ksi_signo = rv;
329 ksi.ksi_trap = EXC_PGM;
330 ksi.ksi_addr = (void *)frame->srr0;
331 trapsignal(l, &ksi);
332 }
333 break;
334
335 case EXC_MCHK:
336 {
337 struct faultbuf *fb;
338
339 if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
340 frame->tf_xtra[TF_PID] = KERNEL_PID;
341 frame->srr0 = fb->fb_pc;
342 frame->srr1 |= PSL_IR; /* Re-enable IMMU */
343 frame->fixreg[1] = fb->fb_sp;
344 frame->fixreg[2] = fb->fb_r2;
345 frame->fixreg[3] = 1; /* Return TRUE */
346 frame->cr = fb->fb_cr;
347 memcpy(&frame->fixreg[13], fb->fb_fixreg,
348 sizeof(fb->fb_fixreg));
349 goto done;
350 }
351 }
352 goto brain_damage;
353 default:
354 brain_damage:
355 printf("trap type 0x%x at 0x%lx\n", type, frame->srr0);
356 #if defined(DDB) || defined(KGDB)
357 if (kdb_trap(type, frame))
358 goto done;
359 #endif
360 #ifdef TRAP_PANICWAIT
361 printf("Press a key to panic.\n");
362 cngetc();
363 #endif
364 panic("trap");
365 }
366
367 /* Invoke MI userret code */
368 mi_userret(l);
369 done:
370 return;
371 }
372
373 int
374 ctx_setup(int ctx, int srr1)
375 {
376 volatile struct pmap *pm;
377
378 /* Update PID if we're returning to user mode. */
379 if (srr1 & PSL_PR) {
380 pm = curproc->p_vmspace->vm_map.pmap;
381 if (!pm->pm_ctx) {
382 ctx_alloc(__UNVOLATILE(pm));
383 }
384 ctx = pm->pm_ctx;
385 if (srr1 & PSL_SE) {
386 int dbreg, mask = 0x48000000;
387 /*
388 * Set the Internal Debug and
389 * Instruction Completion bits of
390 * the DBCR0 register.
391 *
392 * XXX this is also used by jtag debuggers...
393 */
394 __asm volatile("mfspr %0,0x3f2;"
395 "or %0,%0,%1;"
396 "mtspr 0x3f2,%0;" :
397 "=&r" (dbreg) : "r" (mask));
398 }
399 }
400 else if (!ctx) {
401 ctx = KERNEL_PID;
402 }
403 return (ctx);
404 }
405
406 /*
407 * Used by copyin()/copyout()
408 */
409 extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
410 extern void vunmaprange __P((vaddr_t, vsize_t));
411 static int bigcopyin __P((const void *, void *, size_t ));
412 static int bigcopyout __P((const void *, void *, size_t ));
413
414 int
415 copyin(const void *udaddr, void *kaddr, size_t len)
416 {
417 struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
418 int msr, pid, tmp, ctx, count=0;
419 struct faultbuf env;
420
421 /* For bigger buffers use the faster copy */
422 if (len > 1024)
423 return (bigcopyin(udaddr, kaddr, len));
424
425 if (setfault(&env)) {
426 curpcb->pcb_onfault = 0;
427 return EFAULT;
428 }
429
430 if (!(ctx = pm->pm_ctx)) {
431 /* No context -- assign it one */
432 ctx_alloc(pm);
433 ctx = pm->pm_ctx;
434 }
435
436 __asm volatile(
437 " mfmsr %[msr];" /* Save MSR */
438 " li %[pid],0x20; "
439 " andc %[pid],%[msr],%[pid]; mtmsr %[pid];" /* Disable IMMU */
440 " mfpid %[pid];" /* Save old PID */
441 " sync; isync;"
442
443 " srwi. %[count],%[len],0x2;" /* How many words? */
444 " beq- 2f;" /* No words. Go do bytes */
445 " mtctr %[count];"
446 "1: mtpid %[ctx]; sync;"
447 " lswi %[tmp],%[udaddr],4;" /* Load user word */
448 " addi %[udaddr],%[udaddr],0x4;" /* next udaddr word */
449 " sync; isync;"
450 " mtpid %[pid];sync;"
451 " stswi %[tmp],%[kaddr],4;" /* Store kernel word */
452 " dcbf 0,%[kaddr];" /* flush cache */
453 " addi %[kaddr],%[kaddr],0x4;" /* next udaddr word */
454 " sync; isync;"
455 " bdnz 1b;" /* repeat */
456
457 "2: andi. %[count],%[len],0x3;" /* How many remaining bytes? */
458 " addi %[count],%[count],0x1;"
459 " mtctr %[count];"
460 "3: bdz 10f;" /* while count */
461 " mtpid %[ctx];sync;"
462 " lbz %[tmp],0(%[udaddr]);" /* Load user byte */
463 " addi %[udaddr],%[udaddr],0x1;" /* next udaddr byte */
464 " sync; isync;"
465 " mtpid %[pid]; sync;"
466 " stb %[tmp],0(%[kaddr]);" /* Store kernel byte */
467 " dcbf 0,%[kaddr];" /* flush cache */
468 " addi %[kaddr],%[kaddr],0x1;"
469 " sync; isync;"
470 " b 3b;"
471 "10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
472 : [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
473 : [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
474
475 curpcb->pcb_onfault = 0;
476 return 0;
477 }
478
479 static int
480 bigcopyin(const void *udaddr, void *kaddr, size_t len)
481 {
482 const char *up;
483 char *kp = kaddr;
484 struct lwp *l = curlwp;
485 struct proc *p;
486 int error;
487
488 if (!l) {
489 return EFAULT;
490 }
491
492 p = l->l_proc;
493
494 /*
495 * Stolen from physio():
496 */
497 uvm_lwp_hold(l);
498 error = uvm_vslock(p->p_vmspace, __UNCONST(udaddr), len, VM_PROT_READ);
499 if (error) {
500 uvm_lwp_rele(l);
501 return EFAULT;
502 }
503 up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
504
505 memcpy(kp, up, len);
506 vunmaprange((vaddr_t)up, len);
507 uvm_vsunlock(p->p_vmspace, __UNCONST(udaddr), len);
508 uvm_lwp_rele(l);
509
510 return 0;
511 }
512
513 int
514 copyout(const void *kaddr, void *udaddr, size_t len)
515 {
516 struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
517 int msr, pid, tmp, ctx, count=0;
518 struct faultbuf env;
519
520 /* For big copies use more efficient routine */
521 if (len > 1024)
522 return (bigcopyout(kaddr, udaddr, len));
523
524 if (setfault(&env)) {
525 curpcb->pcb_onfault = 0;
526 return EFAULT;
527 }
528
529 if (!(ctx = pm->pm_ctx)) {
530 /* No context -- assign it one */
531 ctx_alloc(pm);
532 ctx = pm->pm_ctx;
533 }
534
535 __asm volatile(
536 " mfmsr %[msr];" /* Save MSR */ \
537 " li %[pid],0x20; " \
538 " andc %[pid],%[msr],%[pid]; mtmsr %[pid];" /* Disable IMMU */ \
539 " mfpid %[pid];" /* Save old PID */ \
540 " sync; isync;"
541
542 " srwi. %[count],%[len],0x2;" /* How many words? */
543 " beq- 2f;" /* No words. Go do bytes */
544 " mtctr %[count];"
545 "1: mtpid %[pid];sync;"
546 " lswi %[tmp],%[kaddr],4;" /* Load kernel word */
547 " addi %[kaddr],%[kaddr],0x4;" /* next kaddr word */
548 " sync; isync;"
549 " mtpid %[ctx]; sync;"
550 " stswi %[tmp],%[udaddr],4;" /* Store user word */
551 " dcbf 0,%[udaddr];" /* flush cache */
552 " addi %[udaddr],%[udaddr],0x4;" /* next udaddr word */
553 " sync; isync;"
554 " bdnz 1b;" /* repeat */
555
556 "2: andi. %[count],%[len],0x3;" /* How many remaining bytes? */
557 " addi %[count],%[count],0x1;"
558 " mtctr %[count];"
559 "3: bdz 10f;" /* while count */
560 " mtpid %[pid];sync;"
561 " lbz %[tmp],0(%[kaddr]);" /* Load kernel byte */
562 " addi %[kaddr],%[kaddr],0x1;" /* next kaddr byte */
563 " sync; isync;"
564 " mtpid %[ctx]; sync;"
565 " stb %[tmp],0(%[udaddr]);" /* Store user byte */
566 " dcbf 0,%[udaddr];" /* flush cache */
567 " addi %[udaddr],%[udaddr],0x1;"
568 " sync; isync;"
569 " b 3b;"
570 "10:mtpid %[pid]; mtmsr %[msr]; sync; isync;" /* Restore PID and MSR */
571 : [msr] "=&r" (msr), [pid] "=&r" (pid), [tmp] "=&r" (tmp)
572 : [udaddr] "b" (udaddr), [ctx] "b" (ctx), [kaddr] "b" (kaddr), [len] "b" (len), [count] "b" (count));
573
574 curpcb->pcb_onfault = 0;
575 return 0;
576 }
577
578 static int
579 bigcopyout(const void *kaddr, void *udaddr, size_t len)
580 {
581 char *up;
582 const char *kp = (const char *)kaddr;
583 struct lwp *l = curlwp;
584 struct proc *p;
585 int error;
586
587 if (!l) {
588 return EFAULT;
589 }
590
591 p = l->l_proc;
592
593 /*
594 * Stolen from physio():
595 */
596 uvm_lwp_hold(l);
597 error = uvm_vslock(p->p_vmspace, udaddr, len, VM_PROT_WRITE);
598 if (error) {
599 uvm_lwp_rele(l);
600 return EFAULT;
601 }
602 up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
603 VM_PROT_READ | VM_PROT_WRITE);
604
605 memcpy(up, kp, len);
606 vunmaprange((vaddr_t)up, len);
607 uvm_vsunlock(p->p_vmspace, udaddr, len);
608 uvm_lwp_rele(l);
609
610 return 0;
611 }
612
613 /*
614 * kcopy(const void *src, void *dst, size_t len);
615 *
616 * Copy len bytes from src to dst, aborting if we encounter a fatal
617 * page fault.
618 *
619 * kcopy() _must_ save and restore the old fault handler since it is
620 * called by uiomove(), which may be in the path of servicing a non-fatal
621 * page fault.
622 */
623 int
624 kcopy(const void *src, void *dst, size_t len)
625 {
626 struct faultbuf env, *oldfault;
627
628 oldfault = curpcb->pcb_onfault;
629 if (setfault(&env)) {
630 curpcb->pcb_onfault = oldfault;
631 return EFAULT;
632 }
633
634 memcpy(dst, src, len);
635
636 curpcb->pcb_onfault = oldfault;
637 return 0;
638 }
639
640 int
641 badaddr(void *addr, size_t size)
642 {
643
644 return badaddr_read(addr, size, NULL);
645 }
646
647 int
648 badaddr_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 = 0;
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 = 0;
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
697 static int
698 fix_unaligned(struct lwp *l, struct trapframe *frame)
699 {
700
701 return -1;
702 }
703
704 /*
705 * Start a new LWP
706 */
707 void
708 startlwp(arg)
709 void *arg;
710 {
711 int err;
712 ucontext_t *uc = arg;
713 struct lwp *l = curlwp;
714
715 err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
716 #if DIAGNOSTIC
717 if (err) {
718 printf("Error %d from cpu_setmcontext.", err);
719 }
720 #endif
721 pool_put(&lwp_uc_pool, uc);
722
723 upcallret(l);
724 }
725
726 /*
727 * XXX This is a terrible name.
728 */
729 void
730 upcallret(l)
731 struct lwp *l;
732 {
733
734 /* Invoke MI userret code */
735 mi_userret(l);
736 }
737