trap.c revision 1.2.6.4 1 /* $NetBSD: trap.c,v 1.2.6.4 2002/02/28 04:11:25 nathanw 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 "opt_altivec.h"
70 #include "opt_ddb.h"
71 #include "opt_ktrace.h"
72 #include "opt_syscall_debug.h"
73
74 #include <sys/param.h>
75 #include <sys/proc.h>
76 #include <sys/reboot.h>
77 #include <sys/syscall.h>
78 #include <sys/systm.h>
79 #include <sys/user.h>
80 #include <sys/ktrace.h>
81 #include <sys/lwp.h>
82 #include <sys/pool.h>
83 #include <sys/sa.h>
84 #include <sys/savar.h>
85
86 #include <uvm/uvm_extern.h>
87
88 #include <dev/cons.h>
89
90 #include <machine/cpu.h>
91 #include <machine/db_machdep.h>
92 #include <machine/fpu.h>
93 #include <machine/frame.h>
94 #include <machine/pcb.h>
95 #include <machine/psl.h>
96 #include <machine/trap.h>
97
98 #include <powerpc/spr.h>
99 #include <powerpc/ibm4xx/pmap.h>
100 #include <powerpc/ibm4xx/tlb.h>
101 #include <powerpc/fpu/fpu_extern.h>
102
103 /* These definitions should probably be somewhere else XXX */
104 #define FIRSTARG 3 /* first argument is in reg 3 */
105 #define NARGREG 8 /* 8 args are in registers */
106 #define MOREARGS(sp) ((caddr_t)((int)(sp) + 8)) /* more args go here */
107
108 #ifndef MULTIPROCESSOR
109 volatile int astpending;
110 volatile int want_resched;
111 #endif
112
113 void *syscall = NULL; /* XXX dummy symbol for emul_netbsd */
114
115 static int fix_unaligned __P((struct lwp *p, struct trapframe *frame));
116
117 void trap __P((struct trapframe *)); /* Called from locore / trap_subr */
118 int setfault __P((faultbuf)); /* defined in locore.S */
119 /* Why are these not defined in a header? */
120 int badaddr __P((void *, size_t));
121 int badaddr_read __P((void *, size_t, int *));
122 int ctx_setup __P((int, int));
123
124 #ifdef DEBUG
125 #define TDB_ALL 0x1
126 int trapdebug = /* TDB_ALL */ 0;
127 #define DBPRINTF(x, y) if (trapdebug & (x)) printf y
128 #else
129 #define DBPRINTF(x, y)
130 #endif
131
132 void
133 trap(struct trapframe *frame)
134 {
135 struct lwp *l = curproc;
136 struct proc *p = l ? l->l_proc : NULL;
137 int type = frame->exc;
138 int ftype, rv;
139
140 KASSERT(l == 0 || (l->l_stat == LSONPROC));
141
142 if (frame->srr1 & PSL_PR)
143 type |= EXC_USER;
144
145 ftype = VM_PROT_READ;
146
147 DBPRINTF(TDB_ALL, ("trap(%x) at %x from frame %p &frame %p\n",
148 type, frame->srr0, frame, &frame));
149
150 switch (type) {
151 case EXC_DEBUG|EXC_USER:
152 {
153 int srr2, srr3;
154 __asm __volatile("mfspr %0,0x3f0" : "=r" (rv), "=r" (srr2), "=r" (srr3) :);
155 printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2, srr3);
156 }
157 /*
158 * DEBUG intr -- probably single-step.
159 */
160 case EXC_TRC|EXC_USER:
161 KERNEL_PROC_LOCK(l);
162 frame->srr1 &= ~PSL_SE;
163 trapsignal(l, SIGTRAP, EXC_TRC);
164 KERNEL_PROC_UNLOCK(l);
165 break;
166
167 /* If we could not find and install appropriate TLB entry, fall through */
168
169 case EXC_DSI:
170 /* FALLTHROUGH */
171 case EXC_DTMISS:
172 {
173 struct vm_map *map;
174 vaddr_t va;
175 faultbuf *fb = NULL;
176
177 KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE);
178 va = frame->dear;
179 if (frame->pid == KERNEL_PID) {
180 map = kernel_map;
181 } else {
182 map = &p->p_vmspace->vm_map;
183 }
184
185 if (frame->esr & (ESR_DST|ESR_DIZ))
186 ftype = VM_PROT_WRITE;
187
188 DBPRINTF(TDB_ALL, ("trap(EXC_DSI) at %x %s fault on %p esr %x\n",
189 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", (void *)va, frame->esr));
190 rv = uvm_fault(map, trunc_page(va), 0, ftype);
191 KERNEL_UNLOCK();
192 if (rv == 0)
193 goto done;
194 if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
195 frame->pid = KERNEL_PID;
196 frame->srr0 = (*fb)[0];
197 frame->srr1 |= PSL_IR; /* Re-enable IMMU */
198 frame->fixreg[1] = (*fb)[1];
199 frame->fixreg[2] = (*fb)[2];
200 frame->fixreg[3] = 1; /* Return TRUE */
201 frame->cr = (*fb)[3];
202 memcpy(&frame->fixreg[13], &(*fb)[4],
203 19 * sizeof(register_t));
204 goto done;
205 }
206 }
207 goto brain_damage;
208
209 case EXC_DSI|EXC_USER:
210 /* FALLTHROUGH */
211 case EXC_DTMISS|EXC_USER:
212 KERNEL_PROC_LOCK(l);
213
214 if (frame->esr & (ESR_DST|ESR_DIZ))
215 ftype = VM_PROT_WRITE;
216
217 DBPRINTF(TDB_ALL, ("trap(EXC_DSI|EXC_USER) at %x %s fault on %x %x\n",
218 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->dear, frame->esr));
219 KASSERT(l == curproc && (l->l_stat == LSONPROC));
220 rv = uvm_fault(&p->p_vmspace->vm_map,
221 trunc_page(frame->dear), 0, ftype);
222 if (rv == 0) {
223 KERNEL_PROC_UNLOCK(l);
224 break;
225 }
226 if (rv == ENOMEM) {
227 printf("UVM: pid %d (%s) lid %d, uid %d killed: "
228 "out of swap\n",
229 p->p_pid, p->p_comm, l->l_lid,
230 p->p_cred && p->p_ucred ?
231 p->p_ucred->cr_uid : -1);
232 trapsignal(l, SIGKILL, EXC_DSI);
233 } else {
234 trapsignal(l, SIGSEGV, EXC_DSI);
235 }
236 KERNEL_PROC_UNLOCK(l);
237 break;
238 case EXC_ITMISS|EXC_USER:
239 case EXC_ISI|EXC_USER:
240 KERNEL_PROC_LOCK(l);
241 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
242 DBPRINTF(TDB_ALL, ("trap(EXC_ISI|EXC_USER) at %x %s fault on %x tf %p\n",
243 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->srr0, frame));
244 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0), 0, ftype);
245 if (rv == 0) {
246 KERNEL_PROC_UNLOCK(l);
247 break;
248 }
249 trapsignal(l, SIGSEGV, EXC_ISI);
250 KERNEL_PROC_UNLOCK(l);
251 break;
252 case EXC_SC|EXC_USER:
253 {
254 const struct sysent *callp;
255 size_t argsize;
256 register_t code, error;
257 register_t *params, rval[2];
258 int n;
259 register_t args[10];
260
261 KERNEL_PROC_LOCK(l);
262
263 uvmexp.syscalls++;
264
265 code = frame->fixreg[0];
266 callp = p->p_emul->e_sysent;
267 params = frame->fixreg + FIRSTARG;
268 n = NARGREG;
269
270 switch (code) {
271 case SYS_syscall:
272 /*
273 * code is first argument,
274 * followed by actual args.
275 */
276 code = *params++;
277 n -= 1;
278 break;
279 case SYS___syscall:
280 params++;
281 code = *params++;
282 n -= 2;
283 break;
284 default:
285 break;
286 }
287
288 code &= (SYS_NSYSENT - 1);
289 callp += code;
290 argsize = callp->sy_argsize;
291
292 if (argsize > n * sizeof(register_t)) {
293 memcpy(args, params, n * sizeof(register_t));
294 error = copyin(MOREARGS(frame->fixreg[1]),
295 args + n,
296 argsize - n * sizeof(register_t));
297 if (error)
298 goto syscall_bad;
299 params = args;
300 }
301
302 #ifdef KTRACE
303 if (KTRPOINT(p, KTR_SYSCALL))
304 ktrsyscall(p, code, argsize, params);
305 #endif
306 #ifdef SYSCALL_DEBUG
307 if (trapdebug)
308 scdebug_call(p, code, args);
309 #endif
310 rval[0] = 0;
311 rval[1] = 0;
312
313 error = (*callp->sy_call)(l, params, rval);
314 #ifdef SYSCALL_DEBUG
315 if (trapdebug)
316 scdebug_ret(p, code, error, rval);
317 #endif
318 switch (error) {
319 case 0:
320 frame->fixreg[FIRSTARG] = rval[0];
321 frame->fixreg[FIRSTARG + 1] = rval[1];
322 frame->cr &= ~0x10000000;
323 break;
324 case ERESTART:
325 /*
326 * Set user's pc back to redo the system call.
327 */
328 frame->srr0 -= 4;
329 break;
330 case EJUSTRETURN:
331 /* nothing to do */
332 break;
333 default:
334 syscall_bad:
335 if (p->p_emul->e_errno)
336 error = p->p_emul->e_errno[error];
337 frame->fixreg[FIRSTARG] = error;
338 frame->cr |= 0x10000000;
339 break;
340 }
341
342 #ifdef KTRACE
343 if (KTRPOINT(p, KTR_SYSRET))
344 ktrsysret(p, code, error, rval[0]);
345 #endif
346 }
347 KERNEL_PROC_UNLOCK(l);
348 break;
349
350 case EXC_AST|EXC_USER:
351 astpending = 0; /* we are about to do it */
352 KERNEL_PROC_LOCK(l);
353 uvmexp.softs++;
354 if (p->p_flag & P_OWEUPC) {
355 p->p_flag &= ~P_OWEUPC;
356 ADDUPROF(p);
357 }
358 /* Check whether we are being preempted. */
359 if (want_resched)
360 preempt(NULL);
361 KERNEL_PROC_UNLOCK(l);
362 break;
363
364
365 case EXC_ALI|EXC_USER:
366 KERNEL_PROC_LOCK(l);
367 if (fix_unaligned(l, frame) != 0)
368 trapsignal(l, SIGBUS, EXC_ALI);
369 else
370 frame->srr0 += 4;
371 KERNEL_PROC_UNLOCK(l);
372 break;
373
374 case EXC_PGM|EXC_USER:
375 /*
376 * Illegal insn:
377 *
378 * let's try to see if it's FPU and can be emulated.
379 */
380 uvmexp.traps ++;
381 if (!(l->l_addr->u_pcb.pcb_flags & PCB_FPU)) {
382 memset(&l->l_addr->u_pcb.pcb_fpu, 0,
383 sizeof l->l_addr->u_pcb.pcb_fpu);
384 l->l_addr->u_pcb.pcb_flags |= PCB_FPU;
385 }
386
387 if ((rv = fpu_emulate(frame,
388 (struct fpreg *)&l->l_addr->u_pcb.pcb_fpu))) {
389 KERNEL_PROC_LOCK(l);
390 trapsignal(l, rv, EXC_PGM);
391 KERNEL_PROC_UNLOCK(l);
392 }
393 break;
394
395 case EXC_MCHK:
396 {
397 faultbuf *fb;
398
399 if ((fb = l->l_addr->u_pcb.pcb_onfault) != NULL) {
400 frame->pid = KERNEL_PID;
401 frame->srr0 = (*fb)[0];
402 frame->srr1 |= PSL_IR; /* Re-enable IMMU */
403 frame->fixreg[1] = (*fb)[1];
404 frame->fixreg[2] = (*fb)[2];
405 frame->fixreg[3] = 1; /* Return TRUE */
406 frame->cr = (*fb)[3];
407 memcpy(&frame->fixreg[13], &(*fb)[4],
408 19 * sizeof(register_t));
409 goto done;
410 }
411 }
412 goto brain_damage;
413 default:
414 brain_damage:
415 printf("trap type 0x%x at 0x%x\n", type, frame->srr0);
416 #ifdef DDB
417 if (kdb_trap(type, frame))
418 goto done;
419 #endif
420 #ifdef TRAP_PANICWAIT
421 printf("Press a key to panic.\n");
422 cngetc();
423 #endif
424 panic("trap");
425 }
426
427 /* Take pending signals. */
428 {
429 int sig;
430
431 while ((sig = CURSIG(l)) != 0)
432 postsig(sig);
433 }
434
435 /* If our process is on the way out, die. */
436 if (p->p_flag & P_WEXIT)
437 lwp_exit(l);
438
439 /* Invoke any pending upcalls */
440 if (l->l_flag & L_SA_UPCALL)
441 sa_upcall_userret(l);
442
443 curcpu()->ci_schedstate.spc_curpriority = l->l_priority = l->l_usrpri;
444 done:
445 }
446
447 int
448 ctx_setup(int ctx, int srr1)
449 {
450 volatile struct pmap *pm;
451
452 /* Update PID if we're returning to user mode. */
453 if (srr1 & PSL_PR) {
454 pm = curproc->l_proc->p_vmspace->vm_map.pmap;
455 if (!pm->pm_ctx) {
456 ctx_alloc((struct pmap *)pm);
457 }
458 ctx = pm->pm_ctx;
459 if (srr1 & PSL_SE) {
460 int dbreg, mask = 0x48000000;
461 /*
462 * Set the Internal Debug and
463 * Instruction Completion bits of
464 * the DBCR0 register.
465 *
466 * XXX this is also used by jtag debuggers...
467 */
468 __asm __volatile("mfspr %0,0x3f2;"
469 "or %0,%0,%1;"
470 "mtspr 0x3f2,%0;" :
471 "=&r" (dbreg) : "r" (mask));
472 }
473 }
474 else if (!ctx) {
475 ctx = KERNEL_PID;
476 }
477 return (ctx);
478 }
479
480 void
481 child_return(void *arg)
482 {
483 struct lwp *l = arg;
484 struct proc *p = l->l_proc;
485 struct trapframe *tf = trapframe(l);
486
487 KERNEL_PROC_UNLOCK(l);
488
489 tf->fixreg[FIRSTARG] = 0;
490 tf->fixreg[FIRSTARG + 1] = 1;
491 tf->cr &= ~0x10000000;
492 tf->srr1 &= ~(PSL_FP|PSL_VEC); /* Disable FP & AltiVec, as we can't be them */
493 #ifdef KTRACE
494 if (KTRPOINT(p, KTR_SYSRET)) {
495 KERNEL_PROC_LOCK(l);
496 ktrsysret(p, SYS_fork, 0, 0);
497 KERNEL_PROC_UNLOCK(l);
498 }
499 #endif
500 /* Profiling? XXX */
501 curcpu()->ci_schedstate.spc_curpriority = l->l_priority;
502 }
503
504 /*
505 * Used by copyin()/copyout()
506 */
507 extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
508 extern void vunmaprange __P((vaddr_t, vsize_t));
509 static int bigcopyin __P((const void *, void *, size_t ));
510 static int bigcopyout __P((const void *, void *, size_t ));
511
512 int
513 copyin(const void *udaddr, void *kaddr, size_t len)
514 {
515 struct pmap *pm = curproc->l_proc->p_vmspace->vm_map.pmap;
516 int msr, pid, tmp, ctx;
517 faultbuf env;
518
519 /* For bigger buffers use the faster copy */
520 if (len > 256) return (bigcopyin(udaddr, kaddr, len));
521
522 if (setfault(env)) {
523 curpcb->pcb_onfault = 0;
524 return EFAULT;
525 }
526
527 if (!(ctx = pm->pm_ctx)) {
528 /* No context -- assign it one */
529 ctx_alloc(pm);
530 ctx = pm->pm_ctx;
531 }
532
533 asm volatile("addi %6,%6,1; mtctr %6;" /* Set up counter */
534 "mfmsr %0;" /* Save MSR */
535 "li %1,0x20; "
536 "andc %1,%0,%1; mtmsr %1;" /* Disable IMMU */
537 "mfpid %1;" /* Save old PID */
538 "sync; isync;"
539
540 "1: bdz 2f;" /* while len */
541 "mtpid %3; sync;" /* Load user ctx */
542 "lbz %2,0(%4); addi %4,%4,1;" /* Load byte */
543 "sync; isync;"
544 "mtpid %1;sync;"
545 "stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;" /* Store kernel byte */
546 "sync; isync;"
547 "b 1b;" /* repeat */
548
549 "2: mtpid %1; mtmsr %0;" /* Restore PID and MSR */
550 "sync; isync;"
551 : "=&r" (msr), "=&r" (pid), "=&r" (tmp)
552 : "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
553
554 curpcb->pcb_onfault = 0;
555 return 0;
556 }
557
558 static int
559 bigcopyin(const void *udaddr, void *kaddr, size_t len)
560 {
561 const char *up;
562 char *kp = kaddr;
563 struct lwp *l = curproc;
564 struct proc *p;
565 int error;
566
567 if (!l) {
568 return EFAULT;
569 }
570
571 p = l->l_proc;
572
573 /*
574 * Stolen from physio():
575 */
576 PHOLD(l);
577 error = uvm_vslock(p, (caddr_t)udaddr, len, VM_PROT_READ);
578 if (error) {
579 PRELE(l);
580 return EFAULT;
581 }
582 up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
583
584 memcpy(kp, up, len);
585 vunmaprange((vaddr_t)up, len);
586 uvm_vsunlock(p, (caddr_t)udaddr, len);
587 PRELE(l);
588
589 return 0;
590 }
591
592 int
593 copyout(const void *kaddr, void *udaddr, size_t len)
594 {
595 struct pmap *pm = curproc->l_proc->p_vmspace->vm_map.pmap;
596 int msr, pid, tmp, ctx;
597 faultbuf env;
598
599 /* For big copies use more efficient routine */
600 if (len > 256) return (bigcopyout(kaddr, udaddr, len));
601
602 if (setfault(env)) {
603 curpcb->pcb_onfault = 0;
604 return EFAULT;
605 }
606
607 if (!(ctx = pm->pm_ctx)) {
608 /* No context -- assign it one */
609 ctx_alloc(pm);
610 ctx = pm->pm_ctx;
611 }
612
613 asm volatile("addi %6,%6,1; mtctr %6;" /* Set up counter */
614 "mfmsr %0;" /* Save MSR */
615 "li %1,0x20; "
616 "andc %1,%0,%1; mtmsr %1;" /* Disable IMMU */
617 "mfpid %1;" /* Save old PID */
618 "sync; isync;"
619
620 "1: bdz 2f;" /* while len */
621 "mtpid %1;sync;"
622 "lbz %2,0(%5); addi %5,%5,1;" /* Load kernel byte */
623 "sync; isync;"
624 "mtpid %3; sync;" /* Load user ctx */
625 "stb %2,0(%4); dcbf 0,%4; addi %4,%4,1;" /* Store user byte */
626 "sync; isync;"
627 "b 1b;" /* repeat */
628
629 "2: mtpid %1; mtmsr %0;" /* Restore PID and MSR */
630 "sync; isync;"
631 : "=&r" (msr), "=&r" (pid), "=&r" (tmp)
632 : "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
633
634 curpcb->pcb_onfault = 0;
635 return 0;
636 }
637
638 static int
639 bigcopyout(const void *kaddr, void *udaddr, size_t len)
640 {
641 char *up;
642 const char *kp = (char *)kaddr;
643 struct lwp *l = curproc;
644 struct proc *p;
645 int error;
646
647 if (!l) {
648 return EFAULT;
649 }
650
651 p = l->l_proc;
652
653 /*
654 * Stolen from physio():
655 */
656 PHOLD(l);
657 error = uvm_vslock(p, udaddr, len, VM_PROT_WRITE);
658 if (error) {
659 PRELE(l);
660 return EFAULT;
661 }
662 up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
663 VM_PROT_READ|VM_PROT_WRITE);
664
665 memcpy(up, kp, len);
666 vunmaprange((vaddr_t)up, len);
667 uvm_vsunlock(p, udaddr, len);
668 PRELE(l);
669
670 return 0;
671 }
672
673 /*
674 * kcopy(const void *src, void *dst, size_t len);
675 *
676 * Copy len bytes from src to dst, aborting if we encounter a fatal
677 * page fault.
678 *
679 * kcopy() _must_ save and restore the old fault handler since it is
680 * called by uiomove(), which may be in the path of servicing a non-fatal
681 * page fault.
682 */
683 int
684 kcopy(const void *src, void *dst, size_t len)
685 {
686 faultbuf env, *oldfault;
687
688 oldfault = curpcb->pcb_onfault;
689 if (setfault(env)) {
690 curpcb->pcb_onfault = oldfault;
691 return EFAULT;
692 }
693
694 memcpy(dst, src, len);
695
696 curpcb->pcb_onfault = oldfault;
697 return 0;
698 }
699
700 int
701 badaddr(void *addr, size_t size)
702 {
703
704 return badaddr_read(addr, size, NULL);
705 }
706
707 int
708 badaddr_read(void *addr, size_t size, int *rptr)
709 {
710 faultbuf env;
711 int x;
712
713 /* Get rid of any stale machine checks that have been waiting. */
714 __asm __volatile ("sync; isync");
715
716 if (setfault(env)) {
717 curpcb->pcb_onfault = 0;
718 __asm __volatile ("sync");
719 return 1;
720 }
721
722 __asm __volatile ("sync");
723
724 switch (size) {
725 case 1:
726 x = *(volatile int8_t *)addr;
727 break;
728 case 2:
729 x = *(volatile int16_t *)addr;
730 break;
731 case 4:
732 x = *(volatile int32_t *)addr;
733 break;
734 default:
735 panic("badaddr: invalid size (%d)", size);
736 }
737
738 /* Make sure we took the machine check, if we caused one. */
739 __asm __volatile ("sync; isync");
740
741 curpcb->pcb_onfault = 0;
742 __asm __volatile ("sync"); /* To be sure. */
743
744 /* Use the value to avoid reorder. */
745 if (rptr)
746 *rptr = x;
747
748 return 0;
749 }
750
751 /*
752 * For now, this only deals with the particular unaligned access case
753 * that gcc tends to generate. Eventually it should handle all of the
754 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
755 */
756
757 static int
758 fix_unaligned(struct lwp *l, struct trapframe *frame)
759 {
760
761 return -1;
762 }
763
764 /*
765 * Start a new LWP
766 */
767 void
768 startlwp(arg)
769 void *arg;
770 {
771 int err;
772 ucontext_t *uc = arg;
773 struct lwp *l = curproc;
774
775 err = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
776 #if DIAGNOSTIC
777 if (err) {
778 printf("Error %d from cpu_setmcontext.", err);
779 }
780 #endif
781 pool_put(&lwp_uc_pool, uc);
782
783 upcallret(l);
784 }
785
786 /*
787 * XXX This is a terrible name.
788 */
789 void
790 upcallret(arg)
791 void *arg;
792 {
793 struct lwp *l = curproc;
794 int sig;
795
796 /* Take pending signals. */
797 while ((sig = CURSIG(l)) != 0)
798 postsig(sig);
799
800 /* If our process is on the way out, die. */
801 if (l->l_proc->p_flag & P_WEXIT)
802 lwp_exit(l);
803
804 /* Invoke any pending upcalls */
805 if (l->l_flag & L_SA_UPCALL)
806 sa_upcall_userret(l);
807
808 curcpu()->ci_schedstate.spc_curpriority = l->l_priority = l->l_usrpri;
809 }
810