]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/powerpc/booke/trap.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / powerpc / booke / trap.c
1 /*-
2  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3  * Copyright (C) 1995, 1996 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by TooLs GmbH.
17  * 4. The name of TooLs GmbH may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $NetBSD: trap.c,v 1.58 2002/03/04 04:07:35 dbj Exp $
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_fpu_emu.h"
38 #include "opt_ktrace.h"
39
40 #include <sys/param.h>
41 #include <sys/kdb.h>
42 #include <sys/proc.h>
43 #include <sys/ktr.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/pioctl.h>
47 #include <sys/ptrace.h>
48 #include <sys/reboot.h>
49 #include <sys/syscall.h>
50 #include <sys/sysent.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
53 #include <sys/signalvar.h>
54 #ifdef KTRACE
55 #include <sys/ktrace.h>
56 #endif
57 #include <sys/vmmeter.h>
58
59 #include <security/audit/audit.h>
60
61 #include <vm/vm.h>
62 #include <vm/pmap.h>
63 #include <vm/vm_extern.h>
64 #include <vm/vm_param.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_map.h>
67 #include <vm/vm_page.h>
68
69 #include <machine/cpu.h>
70 #include <machine/db_machdep.h>
71 #include <machine/frame.h>
72 #include <machine/pcb.h>
73 #include <machine/pmap.h>
74 #include <machine/psl.h>
75 #include <machine/trap.h>
76 #include <machine/spr.h>
77
78 #ifdef FPU_EMU
79 #include <powerpc/fpu/fpu_extern.h>
80 #endif
81
82 #define FAULTBUF_LR     0
83 #define FAULTBUF_R1     1
84 #define FAULTBUF_R2     2
85 #define FAULTBUF_CR     3
86 #define FAULTBUF_CTR    4
87 #define FAULTBUF_XER    5
88 #define FAULTBUF_R13    6
89
90 static void     trap_fatal(struct trapframe *frame);
91 static void     printtrap(u_int vector, struct trapframe *frame, int isfatal,
92     int user);
93 static int      trap_pfault(struct trapframe *frame, int user);
94 static int      fix_unaligned(struct thread *td, struct trapframe *frame);
95 static int      handle_onfault(struct trapframe *frame);
96 static void     syscall(struct trapframe *frame);
97
98 int     setfault(faultbuf);             /* defined in locore.S */
99
100 /* Why are these not defined in a header? */
101 int     badaddr(void *, size_t);
102 int     badaddr_read(void *, size_t, int *);
103
104 extern char     *syscallnames[];
105
106 struct powerpc_exception {
107         u_int   vector;
108         char    *name;
109 };
110
111 static struct powerpc_exception powerpc_exceptions[] = {
112         { EXC_CRIT,     "critical input" },
113         { EXC_MCHK,     "machine check" },
114         { EXC_DSI,      "data storage interrupt" },
115         { EXC_ISI,      "instruction storage interrupt" },
116         { EXC_EXI,      "external interrupt" },
117         { EXC_ALI,      "alignment" },
118         { EXC_PGM,      "program" },
119         { EXC_SC,       "system call" },
120         { EXC_APU,      "auxiliary proc unavailable" },
121         { EXC_DECR,     "decrementer" },
122         { EXC_FIT,      "fixed-interval timer" },
123         { EXC_WDOG,     "watchdog timer" },
124         { EXC_DTMISS,   "data tlb miss" },
125         { EXC_ITMISS,   "instruction tlb miss" },
126         { EXC_DEBUG,    "debug" },
127         { EXC_PERF,     "performance monitoring" },
128         { EXC_LAST,     NULL }
129 };
130
131 static const char *
132 trapname(u_int vector)
133 {
134         struct  powerpc_exception *pe;
135
136         for (pe = powerpc_exceptions; pe->vector != EXC_LAST; pe++) {
137                 if (pe->vector == vector)
138                         return (pe->name);
139         }
140
141         return ("unknown");
142 }
143
144 void
145 trap(struct trapframe *frame)
146 {
147         struct thread   *td;
148         struct proc     *p;
149         int             sig, type, user;
150         ksiginfo_t      ksi;
151
152         PCPU_INC(cnt.v_trap);
153
154         td = PCPU_GET(curthread);
155         p = td->td_proc;
156
157         type = frame->exc;
158         sig = 0;
159         user = (frame->srr1 & PSL_PR) ? 1 : 0;
160
161         CTR3(KTR_TRAP, "trap: %s type=%s (%s)", p->p_comm,
162             trapname(type), user ? "user" : "kernel");
163
164         if (user) {
165                 td->td_frame = frame;
166                 if (td->td_ucred != p->p_ucred)
167                         cred_update_thread(td);
168
169                 /* User Mode Traps */
170                 switch (type) {
171                 case EXC_DSI:
172                 case EXC_ISI:
173                         sig = trap_pfault(frame, 1);
174                         break;
175
176                 case EXC_SC:
177                         syscall(frame);
178                         break;
179
180                 case EXC_ALI:
181                         if (fix_unaligned(td, frame) != 0)
182                                 sig = SIGBUS;
183                         else
184                                 frame->srr0 += 4;
185                         break;
186
187                 case EXC_DEBUG: /* Single stepping */
188                         mtspr(SPR_DBSR, mfspr(SPR_DBSR));
189                         frame->srr1 &= ~PSL_DE;
190                         frame->cpu.booke.dbcr0 &= ~(DBCR0_IDM || DBCR0_IC);
191                         sig = SIGTRAP;
192                         break;
193
194                 case EXC_PGM:   /* Program exception */
195 #ifdef FPU_EMU
196                         sig = fpu_emulate(frame,
197                             (struct fpreg *)&td->td_pcb->pcb_fpu);
198 #else
199                         /* XXX SIGILL for non-trap instructions. */
200                         sig = SIGTRAP;
201 #endif
202                         break;
203
204                 default:
205                         trap_fatal(frame);
206                 }
207         } else {
208                 /* Kernel Mode Traps */
209                 KASSERT(cold || td->td_ucred != NULL,
210                     ("kernel trap doesn't have ucred"));
211
212                 switch (type) {
213                 case EXC_DEBUG:
214                         mtspr(SPR_DBSR, mfspr(SPR_DBSR));
215                         kdb_trap(frame->exc, 0, frame);
216                         return;
217
218                 case EXC_DSI:
219                         if (trap_pfault(frame, 0) == 0)
220                                 return;
221                         break;
222
223                 case EXC_MCHK:
224                         if (handle_onfault(frame))
225                                 return;
226                         break;
227 #ifdef KDB
228                 case EXC_PGM:
229                         if (frame->cpu.booke.esr & ESR_PTR)
230                                 kdb_trap(EXC_PGM, 0, frame);
231                         return;
232 #endif
233                 default:
234                         break;
235                 }
236                 trap_fatal(frame);
237         }
238
239         if (sig != 0) {
240                 if (p->p_sysent->sv_transtrap != NULL)
241                         sig = (p->p_sysent->sv_transtrap)(sig, type);
242                 ksiginfo_init_trap(&ksi);
243                 ksi.ksi_signo = sig;
244                 ksi.ksi_code = type; /* XXX, not POSIX */
245                 /* ksi.ksi_addr = ? */
246                 ksi.ksi_trapno = type;
247                 trapsignal(td, &ksi);
248         }
249
250         userret(td, frame);
251         mtx_assert(&Giant, MA_NOTOWNED);
252 }
253
254 static void
255 trap_fatal(struct trapframe *frame)
256 {
257
258         printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
259 #ifdef KDB
260         if ((debugger_on_panic || kdb_active) &&
261             kdb_trap(frame->exc, 0, frame))
262                 return;
263 #endif
264         panic("%s trap", trapname(frame->exc));
265 }
266
267 static void
268 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
269 {
270         register_t va = 0;
271
272         printf("\n");
273         printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
274             user ? "user" : "kernel");
275         printf("\n");
276         printf("   exception       = 0x%x (%s)\n", vector, trapname(vector));
277         
278         switch (vector) {
279         case EXC_DTMISS:
280         case EXC_DSI:
281                 va = frame->cpu.booke.dear;
282                 break;
283
284         case EXC_ITMISS:
285         case EXC_ISI:
286                 va = frame->srr0;
287                 break;
288         }
289
290         printf("   virtual address = 0x%08x\n", va);
291         printf("   srr0            = 0x%08x\n", frame->srr0);
292         printf("   srr1            = 0x%08x\n", frame->srr1);
293         printf("   curthread       = %p\n", curthread);
294         if (curthread != NULL)
295                 printf("          pid = %d, comm = %s\n",
296                     curthread->td_proc->p_pid, curthread->td_proc->p_comm);
297         printf("\n");
298 }
299
300 /*
301  * Handles a fatal fault when we have onfault state to recover.  Returns
302  * non-zero if there was onfault recovery state available.
303  */
304 static int
305 handle_onfault(struct trapframe *frame)
306 {
307         struct          thread *td;
308         faultbuf        *fb;
309
310         td = curthread;
311         fb = td->td_pcb->pcb_onfault;
312         if (fb != NULL) {
313                 frame->srr0 = (*fb)[FAULTBUF_LR];
314                 frame->fixreg[1] = (*fb)[FAULTBUF_R1];
315                 frame->fixreg[2] = (*fb)[FAULTBUF_R2];
316                 frame->fixreg[3] = 1;
317                 frame->cr = (*fb)[FAULTBUF_CR];
318                 frame->ctr = (*fb)[FAULTBUF_CTR];
319                 frame->xer = (*fb)[FAULTBUF_XER];
320                 bcopy(&(*fb)[FAULTBUF_R13], &frame->fixreg[13],
321                     19 * sizeof(register_t));
322                 return (1);
323         }
324         return (0);
325 }
326
327 void
328 syscall(struct trapframe *frame)
329 {
330         caddr_t         params;
331         struct          sysent *callp;
332         struct          thread *td;
333         struct          proc *p;
334         int             error, n;
335         size_t          narg;
336         register_t      args[10];
337         u_int           code;
338
339         td = PCPU_GET(curthread);
340         p = td->td_proc;
341
342         PCPU_INC(cnt.v_syscall);
343
344         code = frame->fixreg[0];
345         params = (caddr_t)(frame->fixreg + FIRSTARG);
346         n = NARGREG;
347
348         if (p->p_sysent->sv_prepsyscall) {
349                 /*
350                  * The prep code is MP aware.
351                  */
352                 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, &params);
353         } else if (code == SYS_syscall) {
354                 /*
355                  * code is first argument,
356                  * followed by actual args.
357                  */
358                 code = *(u_int *) params;
359                 params += sizeof(register_t);
360                 n -= 1;
361         } else if (code == SYS___syscall) {
362                 /*
363                  * Like syscall, but code is a quad,
364                  * so as to maintain quad alignment
365                  * for the rest of the args.
366                  */
367                 params += sizeof(register_t);
368                 code = *(u_int *) params;
369                 params += sizeof(register_t);
370                 n -= 2;
371         }
372
373         if (p->p_sysent->sv_mask)
374                 code &= p->p_sysent->sv_mask;
375
376         if (code >= p->p_sysent->sv_size)
377                 callp = &p->p_sysent->sv_table[0];
378         else
379                 callp = &p->p_sysent->sv_table[code];
380
381         narg = callp->sy_narg;
382
383         if (narg > n) {
384                 bcopy(params, args, n * sizeof(register_t));
385                 error = copyin(MOREARGS(frame->fixreg[1]), args + n,
386                     (narg - n) * sizeof(register_t));
387                 params = (caddr_t)args;
388         } else
389                 error = 0;
390
391         CTR5(KTR_SYSC, "syscall: p=%s %s(%x %x %x)", p->p_comm,
392              syscallnames[code],
393              frame->fixreg[FIRSTARG],
394              frame->fixreg[FIRSTARG+1],
395              frame->fixreg[FIRSTARG+2]);
396
397 #ifdef  KTRACE
398         if (KTRPOINT(td, KTR_SYSCALL))
399                 ktrsyscall(code, narg, (register_t *)params);
400 #endif
401
402         td->td_syscalls++;
403
404         if (error == 0) {
405                 td->td_retval[0] = 0;
406                 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
407
408                 STOPEVENT(p, S_SCE, narg);
409
410                 PTRACESTOP_SC(p, td, S_PT_SCE);
411
412                 AUDIT_SYSCALL_ENTER(code, td);
413                 error = (*callp->sy_call)(td, params);
414                 AUDIT_SYSCALL_EXIT(error, td);
415
416                 CTR3(KTR_SYSC, "syscall: p=%s %s ret=%x", p->p_comm,
417                      syscallnames[code], td->td_retval[0]);
418         }
419
420         cpu_set_syscall_retval(td, error);
421
422         /*
423          * Check for misbehavior.
424          */
425         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
426             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
427         KASSERT(td->td_critnest == 0,
428             ("System call %s returning in a critical section",
429             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
430         KASSERT(td->td_locks == 0,
431             ("System call %s returning with %d locks held",
432             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
433             td->td_locks));
434
435 #ifdef  KTRACE
436         if (KTRPOINT(td, KTR_SYSRET))
437                 ktrsysret(code, error, td->td_retval[0]);
438 #endif
439
440         /*
441          * Does the comment in the i386 code about errno apply here?
442          */
443         STOPEVENT(p, S_SCX, code);
444
445         PTRACESTOP_SC(p, td, S_PT_SCX);
446 }
447
448 static int
449 trap_pfault(struct trapframe *frame, int user)
450 {
451         vm_offset_t     eva, va;
452         struct          thread *td;
453         struct          proc *p;
454         vm_map_t        map;
455         vm_prot_t       ftype;
456         int             rv;
457
458         td = curthread;
459         p = td->td_proc;
460
461         if (frame->exc == EXC_ISI) {
462                 eva = frame->srr0;
463                 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
464
465         } else {
466                 eva = frame->cpu.booke.dear;
467                 if (frame->cpu.booke.esr & ESR_ST)
468                         ftype = VM_PROT_WRITE;
469                 else
470                         ftype = VM_PROT_READ;
471         }
472
473         if (user) {
474                 KASSERT(p->p_vmspace != NULL, ("trap_pfault: vmspace  NULL"));
475                 map = &p->p_vmspace->vm_map;
476         } else {
477                 if (eva < VM_MAXUSER_ADDRESS) {
478
479                         if (p->p_vmspace == NULL)
480                                 return (SIGSEGV);
481
482                         map = &p->p_vmspace->vm_map;
483
484                 } else {
485                         map = kernel_map;
486                 }
487         }
488         va = trunc_page(eva);
489
490         if (map != kernel_map) {
491                 /*
492                  * Keep swapout from messing with us during this
493                  *      critical time.
494                  */
495                 PROC_LOCK(p);
496                 ++p->p_lock;
497                 PROC_UNLOCK(p);
498
499                 /* Fault in the user page: */
500                 rv = vm_fault(map, va, ftype,
501                     (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY : VM_FAULT_NORMAL);
502
503                 PROC_LOCK(p);
504                 --p->p_lock;
505                 PROC_UNLOCK(p);
506         } else {
507                 /*
508                  * Don't have to worry about process locking or stacks in the
509                  * kernel.
510                  */
511                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
512         }
513
514         if (rv == KERN_SUCCESS)
515                 return (0);
516
517         if (!user && handle_onfault(frame))
518                 return (0);
519
520         return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
521 }
522
523 int
524 badaddr(void *addr, size_t size)
525 {
526
527         return (badaddr_read(addr, size, NULL));
528 }
529
530 int
531 badaddr_read(void *addr, size_t size, int *rptr)
532 {
533         struct thread   *td;
534         faultbuf        env;
535         int             x;
536
537         /* Get rid of any stale machine checks that have been waiting.  */
538         __asm __volatile ("sync; isync");
539
540         td = PCPU_GET(curthread);
541
542         if (setfault(env)) {
543                 td->td_pcb->pcb_onfault = 0;
544                 __asm __volatile ("sync");
545                 return (1);
546         }
547
548         __asm __volatile ("sync");
549
550         switch (size) {
551         case 1:
552                 x = *(volatile int8_t *)addr;
553                 break;
554         case 2:
555                 x = *(volatile int16_t *)addr;
556                 break;
557         case 4:
558                 x = *(volatile int32_t *)addr;
559                 break;
560         default:
561                 panic("badaddr: invalid size (%d)", size);
562         }
563
564         /* Make sure we took the machine check, if we caused one. */
565         __asm __volatile ("sync; isync");
566
567         td->td_pcb->pcb_onfault = 0;
568         __asm __volatile ("sync");      /* To be sure. */
569
570         /* Use the value to avoid reorder. */
571         if (rptr)
572                 *rptr = x;
573
574         return (0);
575 }
576
577 /*
578  * For now, this only deals with the particular unaligned access case
579  * that gcc tends to generate.  Eventually it should handle all of the
580  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
581  */
582
583 static int
584 fix_unaligned(struct thread *td, struct trapframe *frame)
585 {
586 #if 0
587         struct thread   *fputhread;
588         int             indicator, reg;
589         double          *fpr;
590
591         indicator = EXC_ALI_OPCODE_INDICATOR(frame->dsisr);
592
593         switch (indicator) {
594         case EXC_ALI_LFD:
595         case EXC_ALI_STFD:
596                 reg = EXC_ALI_RST(frame->dsisr);
597                 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
598                 fputhread = PCPU_GET(fputhread);
599                 /* Juggle the FPU to ensure that we've initialized
600                  * the FPRs, and that their current state is in
601                  * the PCB.
602                  */
603                 if (fputhread != td) {
604                         if (fputhread)
605                                 save_fpu(fputhread);
606                         enable_fpu(td);
607                 }
608                 save_fpu(td);
609
610                 if (indicator == EXC_ALI_LFD) {
611                         if (copyin((void *)frame->dar, fpr,
612                             sizeof(double)) != 0)
613                                 return -1;
614                         enable_fpu(td);
615                 } else {
616                         if (copyout(fpr, (void *)frame->dar,
617                             sizeof(double)) != 0)
618                                 return -1;
619                 }
620                 return 0;
621                 break;
622         }
623
624 #endif
625         return (-1);
626 }
627
628 #ifdef KDB
629 int db_trap_glue(struct trapframe *);
630 int
631 db_trap_glue(struct trapframe *tf)
632 {
633         if (!(tf->srr1 & PSL_PR))
634                 return (kdb_trap(tf->exc, 0, tf));
635         return (0);
636 }
637 #endif