trap.c revision 1.3.8.2 1 /* $NetBSD: trap.c,v 1.3.8.2 2002/07/16 13:09:57 gehenna 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_systrace.h"
73 #include "opt_syscall_debug.h"
74
75 #include <sys/param.h>
76 #include <sys/proc.h>
77 #include <sys/reboot.h>
78 #include <sys/syscall.h>
79 #include <sys/systm.h>
80 #include <sys/user.h>
81 #ifdef KTRACE
82 #include <sys/ktrace.h>
83 #endif
84 #ifdef SYSTRACE
85 #include <sys/systrace.h>
86 #endif
87
88 #include <uvm/uvm_extern.h>
89
90 #include <dev/cons.h>
91
92 #include <machine/cpu.h>
93 #include <machine/db_machdep.h>
94 #include <machine/fpu.h>
95 #include <machine/frame.h>
96 #include <machine/pcb.h>
97 #include <machine/psl.h>
98 #include <machine/trap.h>
99
100 #include <powerpc/spr.h>
101 #include <powerpc/ibm4xx/pmap.h>
102 #include <powerpc/ibm4xx/tlb.h>
103 #include <powerpc/fpu/fpu_extern.h>
104
105 /* These definitions should probably be somewhere else XXX */
106 #define FIRSTARG 3 /* first argument is in reg 3 */
107 #define NARGREG 8 /* 8 args are in registers */
108 #define MOREARGS(sp) ((caddr_t)((int)(sp) + 8)) /* more args go here */
109
110 #ifndef MULTIPROCESSOR
111 volatile int astpending;
112 volatile int want_resched;
113 #endif
114
115 static int fix_unaligned __P((struct proc *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 proc *p = curproc;
136 int type = frame->exc;
137 int ftype, rv;
138
139 KASSERT(p == 0 || (p->p_stat == SONPROC));
140
141 if (frame->srr1 & PSL_PR)
142 type |= EXC_USER;
143
144 ftype = VM_PROT_READ;
145
146 DBPRINTF(TDB_ALL, ("trap(%x) at %x 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 __asm __volatile("mfspr %0,0x3f0" : "=r" (rv), "=r" (srr2), "=r" (srr3) :);
154 printf("debug reg is %x srr2 %x srr3 %x\n", rv, srr2, srr3);
155 }
156 /*
157 * DEBUG intr -- probably single-step.
158 */
159 case EXC_TRC|EXC_USER:
160 KERNEL_PROC_LOCK(p);
161 frame->srr1 &= ~PSL_SE;
162 trapsignal(p, SIGTRAP, EXC_TRC);
163 KERNEL_PROC_UNLOCK(p);
164 break;
165
166 /* If we could not find and install appropriate TLB entry, fall through */
167
168 case EXC_DSI:
169 /* FALLTHROUGH */
170 case EXC_DTMISS:
171 {
172 struct vm_map *map;
173 vaddr_t va;
174 faultbuf *fb = NULL;
175
176 KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE);
177 va = frame->dear;
178 if (frame->pid == KERNEL_PID) {
179 map = kernel_map;
180 } else {
181 map = &p->p_vmspace->vm_map;
182 }
183
184 if (frame->esr & (ESR_DST|ESR_DIZ))
185 ftype = VM_PROT_WRITE;
186
187 DBPRINTF(TDB_ALL, ("trap(EXC_DSI) at %x %s fault on %p esr %x\n",
188 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", (void *)va, frame->esr));
189 rv = uvm_fault(map, trunc_page(va), 0, ftype);
190 KERNEL_UNLOCK();
191 if (rv == 0)
192 goto done;
193 if ((fb = p->p_addr->u_pcb.pcb_onfault) != NULL) {
194 frame->pid = KERNEL_PID;
195 frame->srr0 = (*fb)[0];
196 frame->srr1 |= PSL_IR; /* Re-enable IMMU */
197 frame->fixreg[1] = (*fb)[1];
198 frame->fixreg[2] = (*fb)[2];
199 frame->fixreg[3] = 1; /* Return TRUE */
200 frame->cr = (*fb)[3];
201 memcpy(&frame->fixreg[13], &(*fb)[4],
202 19 * sizeof(register_t));
203 goto done;
204 }
205 }
206 goto brain_damage;
207
208 case EXC_DSI|EXC_USER:
209 /* FALLTHROUGH */
210 case EXC_DTMISS|EXC_USER:
211 KERNEL_PROC_LOCK(p);
212
213 if (frame->esr & (ESR_DST|ESR_DIZ))
214 ftype = VM_PROT_WRITE;
215
216 DBPRINTF(TDB_ALL, ("trap(EXC_DSI|EXC_USER) at %x %s fault on %x %x\n",
217 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->dear, frame->esr));
218 KASSERT(p == curproc && (p->p_stat == SONPROC));
219 rv = uvm_fault(&p->p_vmspace->vm_map,
220 trunc_page(frame->dear), 0, ftype);
221 if (rv == 0) {
222 KERNEL_PROC_UNLOCK(p);
223 break;
224 }
225 if (rv == ENOMEM) {
226 printf("UVM: pid %d (%s), uid %d killed: "
227 "out of swap\n",
228 p->p_pid, p->p_comm,
229 p->p_cred && p->p_ucred ?
230 p->p_ucred->cr_uid : -1);
231 trapsignal(p, SIGKILL, EXC_DSI);
232 } else {
233 trapsignal(p, SIGSEGV, EXC_DSI);
234 }
235 KERNEL_PROC_UNLOCK(p);
236 break;
237 case EXC_ITMISS|EXC_USER:
238 case EXC_ISI|EXC_USER:
239 KERNEL_PROC_LOCK(p);
240 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
241 DBPRINTF(TDB_ALL, ("trap(EXC_ISI|EXC_USER) at %x %s fault on %x tf %p\n",
242 frame->srr0, (ftype&VM_PROT_WRITE) ? "write" : "read", frame->srr0, frame));
243 rv = uvm_fault(&p->p_vmspace->vm_map, trunc_page(frame->srr0), 0, ftype);
244 if (rv == 0) {
245 KERNEL_PROC_UNLOCK(p);
246 break;
247 }
248 trapsignal(p, SIGSEGV, EXC_ISI);
249 KERNEL_PROC_UNLOCK(p);
250 break;
251
252 case EXC_SC|EXC_USER:
253 (*p->p_md.md_syscall)(frame);
254 break;
255
256 case EXC_AST|EXC_USER:
257 astpending = 0; /* we are about to do it */
258 KERNEL_PROC_LOCK(p);
259 uvmexp.softs++;
260 if (p->p_flag & P_OWEUPC) {
261 p->p_flag &= ~P_OWEUPC;
262 ADDUPROF(p);
263 }
264 /* Check whether we are being preempted. */
265 if (want_resched)
266 preempt(NULL);
267 KERNEL_PROC_UNLOCK(p);
268 break;
269
270
271 case EXC_ALI|EXC_USER:
272 KERNEL_PROC_LOCK(p);
273 if (fix_unaligned(p, frame) != 0)
274 trapsignal(p, SIGBUS, EXC_ALI);
275 else
276 frame->srr0 += 4;
277 KERNEL_PROC_UNLOCK(p);
278 break;
279
280 case EXC_PGM|EXC_USER:
281 /*
282 * Illegal insn:
283 *
284 * let's try to see if it's FPU and can be emulated.
285 */
286 uvmexp.traps ++;
287 if (!(p->p_addr->u_pcb.pcb_flags & PCB_FPU)) {
288 memset(&p->p_addr->u_pcb.pcb_fpu, 0,
289 sizeof p->p_addr->u_pcb.pcb_fpu);
290 p->p_addr->u_pcb.pcb_flags |= PCB_FPU;
291 }
292
293 if ((rv = fpu_emulate(frame,
294 (struct fpreg *)&p->p_addr->u_pcb.pcb_fpu))) {
295 KERNEL_PROC_LOCK(p);
296 trapsignal(p, rv, EXC_PGM);
297 KERNEL_PROC_UNLOCK(p);
298 }
299 break;
300
301 case EXC_MCHK:
302 {
303 faultbuf *fb;
304
305 if ((fb = p->p_addr->u_pcb.pcb_onfault) != NULL) {
306 frame->pid = KERNEL_PID;
307 frame->srr0 = (*fb)[0];
308 frame->srr1 |= PSL_IR; /* Re-enable IMMU */
309 frame->fixreg[1] = (*fb)[1];
310 frame->fixreg[2] = (*fb)[2];
311 frame->fixreg[3] = 1; /* Return TRUE */
312 frame->cr = (*fb)[3];
313 memcpy(&frame->fixreg[13], &(*fb)[4],
314 19 * sizeof(register_t));
315 goto done;
316 }
317 }
318 goto brain_damage;
319 default:
320 brain_damage:
321 printf("trap type 0x%x at 0x%x\n", type, frame->srr0);
322 #ifdef DDB
323 if (kdb_trap(type, frame))
324 goto done;
325 #endif
326 #ifdef TRAP_PANICWAIT
327 printf("Press a key to panic.\n");
328 cngetc();
329 #endif
330 panic("trap");
331 }
332
333 /* Take pending signals. */
334 {
335 int sig;
336
337 while ((sig = CURSIG(p)) != 0)
338 postsig(sig);
339 }
340
341 curcpu()->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri;
342 done:
343 }
344
345 int
346 ctx_setup(int ctx, int srr1)
347 {
348 volatile struct pmap *pm;
349
350 /* Update PID if we're returning to user mode. */
351 if (srr1 & PSL_PR) {
352 pm = curproc->p_vmspace->vm_map.pmap;
353 if (!pm->pm_ctx) {
354 ctx_alloc((struct pmap *)pm);
355 }
356 ctx = pm->pm_ctx;
357 if (srr1 & PSL_SE) {
358 int dbreg, mask = 0x48000000;
359 /*
360 * Set the Internal Debug and
361 * Instruction Completion bits of
362 * the DBCR0 register.
363 *
364 * XXX this is also used by jtag debuggers...
365 */
366 __asm __volatile("mfspr %0,0x3f2;"
367 "or %0,%0,%1;"
368 "mtspr 0x3f2,%0;" :
369 "=&r" (dbreg) : "r" (mask));
370 }
371 }
372 else if (!ctx) {
373 ctx = KERNEL_PID;
374 }
375 return (ctx);
376 }
377
378 /*
379 * Used by copyin()/copyout()
380 */
381 extern vaddr_t vmaprange __P((struct proc *, vaddr_t, vsize_t, int));
382 extern void vunmaprange __P((vaddr_t, vsize_t));
383 static int bigcopyin __P((const void *, void *, size_t ));
384 static int bigcopyout __P((const void *, void *, size_t ));
385
386 int
387 copyin(const void *udaddr, void *kaddr, size_t len)
388 {
389 struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
390 int msr, pid, tmp, ctx;
391 faultbuf env;
392
393 /* For bigger buffers use the faster copy */
394 if (len > 256) return (bigcopyin(udaddr, kaddr, len));
395
396 if (setfault(env)) {
397 curpcb->pcb_onfault = 0;
398 return EFAULT;
399 }
400
401 if (!(ctx = pm->pm_ctx)) {
402 /* No context -- assign it one */
403 ctx_alloc(pm);
404 ctx = pm->pm_ctx;
405 }
406
407 asm volatile("addi %6,%6,1; mtctr %6;" /* Set up counter */
408 "mfmsr %0;" /* Save MSR */
409 "li %1,0x20; "
410 "andc %1,%0,%1; mtmsr %1;" /* Disable IMMU */
411 "mfpid %1;" /* Save old PID */
412 "sync; isync;"
413
414 "1: bdz 2f;" /* while len */
415 "mtpid %3; sync;" /* Load user ctx */
416 "lbz %2,0(%4); addi %4,%4,1;" /* Load byte */
417 "sync; isync;"
418 "mtpid %1;sync;"
419 "stb %2,0(%5); dcbf 0,%5; addi %5,%5,1;" /* Store kernel byte */
420 "sync; isync;"
421 "b 1b;" /* repeat */
422
423 "2: mtpid %1; mtmsr %0;" /* Restore PID and MSR */
424 "sync; isync;"
425 : "=&r" (msr), "=&r" (pid), "=&r" (tmp)
426 : "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
427
428 curpcb->pcb_onfault = 0;
429 return 0;
430 }
431
432 static int
433 bigcopyin(const void *udaddr, void *kaddr, size_t len)
434 {
435 const char *up;
436 char *kp = kaddr;
437 struct proc *p = curproc;
438 int error;
439
440 if (!p) {
441 return EFAULT;
442 }
443
444 /*
445 * Stolen from physio():
446 */
447 PHOLD(p);
448 error = uvm_vslock(p, (caddr_t)udaddr, len, VM_PROT_READ);
449 if (error) {
450 PRELE(p);
451 return EFAULT;
452 }
453 up = (char *)vmaprange(p, (vaddr_t)udaddr, len, VM_PROT_READ);
454
455 memcpy(kp, up, len);
456 vunmaprange((vaddr_t)up, len);
457 uvm_vsunlock(p, (caddr_t)udaddr, len);
458 PRELE(p);
459
460 return 0;
461 }
462
463 int
464 copyout(const void *kaddr, void *udaddr, size_t len)
465 {
466 struct pmap *pm = curproc->p_vmspace->vm_map.pmap;
467 int msr, pid, tmp, ctx;
468 faultbuf env;
469
470 /* For big copies use more efficient routine */
471 if (len > 256) return (bigcopyout(kaddr, udaddr, len));
472
473 if (setfault(env)) {
474 curpcb->pcb_onfault = 0;
475 return EFAULT;
476 }
477
478 if (!(ctx = pm->pm_ctx)) {
479 /* No context -- assign it one */
480 ctx_alloc(pm);
481 ctx = pm->pm_ctx;
482 }
483
484 asm volatile("addi %6,%6,1; mtctr %6;" /* Set up counter */
485 "mfmsr %0;" /* Save MSR */
486 "li %1,0x20; "
487 "andc %1,%0,%1; mtmsr %1;" /* Disable IMMU */
488 "mfpid %1;" /* Save old PID */
489 "sync; isync;"
490
491 "1: bdz 2f;" /* while len */
492 "mtpid %1;sync;"
493 "lbz %2,0(%5); addi %5,%5,1;" /* Load kernel byte */
494 "sync; isync;"
495 "mtpid %3; sync;" /* Load user ctx */
496 "stb %2,0(%4); dcbf 0,%4; addi %4,%4,1;" /* Store user byte */
497 "sync; isync;"
498 "b 1b;" /* repeat */
499
500 "2: mtpid %1; mtmsr %0;" /* Restore PID and MSR */
501 "sync; isync;"
502 : "=&r" (msr), "=&r" (pid), "=&r" (tmp)
503 : "r" (ctx), "r" (udaddr), "r" (kaddr), "r" (len));
504
505 curpcb->pcb_onfault = 0;
506 return 0;
507 }
508
509 static int
510 bigcopyout(const void *kaddr, void *udaddr, size_t len)
511 {
512 char *up;
513 const char *kp = (char *)kaddr;
514 struct proc *p = curproc;
515 int error;
516
517 if (!p) {
518 return EFAULT;
519 }
520
521 /*
522 * Stolen from physio():
523 */
524 PHOLD(p);
525 error = uvm_vslock(p, udaddr, len, VM_PROT_WRITE);
526 if (error) {
527 PRELE(p);
528 return EFAULT;
529 }
530 up = (char *)vmaprange(p, (vaddr_t)udaddr, len,
531 VM_PROT_READ|VM_PROT_WRITE);
532
533 memcpy(up, kp, len);
534 vunmaprange((vaddr_t)up, len);
535 uvm_vsunlock(p, udaddr, len);
536 PRELE(p);
537
538 return 0;
539 }
540
541 /*
542 * kcopy(const void *src, void *dst, size_t len);
543 *
544 * Copy len bytes from src to dst, aborting if we encounter a fatal
545 * page fault.
546 *
547 * kcopy() _must_ save and restore the old fault handler since it is
548 * called by uiomove(), which may be in the path of servicing a non-fatal
549 * page fault.
550 */
551 int
552 kcopy(const void *src, void *dst, size_t len)
553 {
554 faultbuf env, *oldfault;
555
556 oldfault = curpcb->pcb_onfault;
557 if (setfault(env)) {
558 curpcb->pcb_onfault = oldfault;
559 return EFAULT;
560 }
561
562 memcpy(dst, src, len);
563
564 curpcb->pcb_onfault = oldfault;
565 return 0;
566 }
567
568 int
569 badaddr(void *addr, size_t size)
570 {
571
572 return badaddr_read(addr, size, NULL);
573 }
574
575 int
576 badaddr_read(void *addr, size_t size, int *rptr)
577 {
578 faultbuf env;
579 int x;
580
581 /* Get rid of any stale machine checks that have been waiting. */
582 __asm __volatile ("sync; isync");
583
584 if (setfault(env)) {
585 curpcb->pcb_onfault = 0;
586 __asm __volatile ("sync");
587 return 1;
588 }
589
590 __asm __volatile ("sync");
591
592 switch (size) {
593 case 1:
594 x = *(volatile int8_t *)addr;
595 break;
596 case 2:
597 x = *(volatile int16_t *)addr;
598 break;
599 case 4:
600 x = *(volatile int32_t *)addr;
601 break;
602 default:
603 panic("badaddr: invalid size (%d)", size);
604 }
605
606 /* Make sure we took the machine check, if we caused one. */
607 __asm __volatile ("sync; isync");
608
609 curpcb->pcb_onfault = 0;
610 __asm __volatile ("sync"); /* To be sure. */
611
612 /* Use the value to avoid reorder. */
613 if (rptr)
614 *rptr = x;
615
616 return 0;
617 }
618
619 /*
620 * For now, this only deals with the particular unaligned access case
621 * that gcc tends to generate. Eventually it should handle all of the
622 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
623 */
624
625 static int
626 fix_unaligned(struct proc *p, struct trapframe *frame)
627 {
628
629 return -1;
630 }
631