]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/powerpc/aim/trap.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / powerpc / aim / 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_ktrace.h"
38
39 #include <sys/param.h>
40 #include <sys/kdb.h>
41 #include <sys/proc.h>
42 #include <sys/ktr.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/pioctl.h>
46 #include <sys/ptrace.h>
47 #include <sys/reboot.h>
48 #include <sys/syscall.h>
49 #include <sys/sysent.h>
50 #include <sys/systm.h>
51 #include <sys/uio.h>
52 #include <sys/signalvar.h>
53 #ifdef KTRACE
54 #include <sys/ktrace.h>
55 #endif
56 #include <sys/vmmeter.h>
57
58 #include <security/audit/audit.h>
59
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_param.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_page.h>
67
68 #include <machine/altivec.h>
69 #include <machine/cpu.h>
70 #include <machine/db_machdep.h>
71 #include <machine/fpu.h>
72 #include <machine/frame.h>
73 #include <machine/pcb.h>
74 #include <machine/pmap.h>
75 #include <machine/psl.h>
76 #include <machine/trap.h>
77 #include <machine/spr.h>
78 #include <machine/sr.h>
79
80 static void     trap_fatal(struct trapframe *frame);
81 static void     printtrap(u_int vector, struct trapframe *frame, int isfatal,
82                     int user);
83 static int      trap_pfault(struct trapframe *frame, int user);
84 static int      fix_unaligned(struct thread *td, struct trapframe *frame);
85 static int      handle_onfault(struct trapframe *frame);
86 static void     syscall(struct trapframe *frame);
87
88 static __inline void    setusr(u_int);
89
90 int     setfault(faultbuf);             /* defined in locore.S */
91
92 /* Why are these not defined in a header? */
93 int     badaddr(void *, size_t);
94 int     badaddr_read(void *, size_t, int *);
95
96 extern char     *syscallnames[];
97
98 struct powerpc_exception {
99         u_int   vector;
100         char    *name;
101 };
102
103 static struct powerpc_exception powerpc_exceptions[] = {
104         { 0x0100, "system reset" },
105         { 0x0200, "machine check" },
106         { 0x0300, "data storage interrupt" },
107         { 0x0400, "instruction storage interrupt" },
108         { 0x0500, "external interrupt" },
109         { 0x0600, "alignment" },
110         { 0x0700, "program" },
111         { 0x0800, "floating-point unavailable" },
112         { 0x0900, "decrementer" },
113         { 0x0c00, "system call" },
114         { 0x0d00, "trace" },
115         { 0x0e00, "floating-point assist" },
116         { 0x0f00, "performance monitoring" },
117         { 0x0f20, "altivec unavailable" },
118         { 0x1000, "instruction tlb miss" },
119         { 0x1100, "data load tlb miss" },
120         { 0x1200, "data store tlb miss" },
121         { 0x1300, "instruction breakpoint" },
122         { 0x1400, "system management" },
123         { 0x1600, "altivec assist" },
124         { 0x1700, "thermal management" },
125         { 0x2000, "run mode/trace" },
126         { 0x3000, NULL }
127 };
128
129 static const char *
130 trapname(u_int vector)
131 {
132         struct  powerpc_exception *pe;
133
134         for (pe = powerpc_exceptions; pe->vector != 0x3000; pe++) {
135                 if (pe->vector == vector)
136                         return (pe->name);
137         }
138
139         return ("unknown");
140 }
141
142 void
143 trap(struct trapframe *frame)
144 {
145         struct thread   *td;
146         struct proc     *p;
147         int             sig, type, user;
148         u_int           ucode;
149         ksiginfo_t      ksi;
150
151         PCPU_INC(cnt.v_trap);
152
153         td = PCPU_GET(curthread);
154         p = td->td_proc;
155
156         type = ucode = frame->exc;
157         sig = 0;
158         user = frame->srr1 & PSL_PR;
159
160         CTR3(KTR_TRAP, "trap: %s type=%s (%s)", td->td_name,
161             trapname(type), user ? "user" : "kernel");
162
163         if (user) {
164                 td->td_pticks = 0;
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_RUNMODETRC:
172                 case EXC_TRC:
173                         frame->srr1 &= ~PSL_SE;
174                         sig = SIGTRAP;
175                         break;
176
177                 case EXC_DSI:
178                 case EXC_ISI:
179                         sig = trap_pfault(frame, 1);
180                         break;
181
182                 case EXC_SC:
183                         syscall(frame);
184                         break;
185
186                 case EXC_FPU:
187                         KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU,
188                             ("FPU already enabled for thread"));
189                         enable_fpu(td);
190                         break;
191
192                 case EXC_VEC:
193                         KASSERT((td->td_pcb->pcb_flags & PCB_VEC) != PCB_VEC,
194                             ("Altivec already enabled for thread"));
195                         enable_vec(td);
196                         break;
197
198                 case EXC_VECAST:
199                         printf("Vector assist exception!\n");
200                         sig = SIGILL;
201                         break;
202
203                 case EXC_ALI:
204                         if (fix_unaligned(td, frame) != 0)
205                                 sig = SIGBUS;
206                         else
207                                 frame->srr0 += 4;
208                         break;
209
210                 case EXC_PGM:
211                         /* Identify the trap reason */
212                         if (frame->srr1 & EXC_PGM_TRAP)
213                                 sig = SIGTRAP;
214                         else
215                                 sig = SIGILL;
216                         break;
217
218                 default:
219                         trap_fatal(frame);
220                 }
221         } else {
222                 /* Kernel Mode Traps */
223
224                 KASSERT(cold || td->td_ucred != NULL,
225                     ("kernel trap doesn't have ucred"));
226                 switch (type) {
227                 case EXC_DSI:
228                         if (trap_pfault(frame, 0) == 0)
229                                 return;
230                         break;
231                 case EXC_MCHK:
232                         if (handle_onfault(frame))
233                                 return;
234                         break;
235                 default:
236                         break;
237                 }
238                 trap_fatal(frame);
239         }
240
241 #ifdef  ALTIVEC
242         if (td != PCPU_GET(vecthread) ||
243             td->td_pcb->pcb_veccpu != PCPU_GET(cpuid))
244                 frame->srr1 &= ~PSL_VEC;
245 #endif /* ALTIVEC */
246
247         if (sig != 0) {
248                 if (p->p_sysent->sv_transtrap != NULL)
249                         sig = (p->p_sysent->sv_transtrap)(sig, type);
250                 ksiginfo_init_trap(&ksi);
251                 ksi.ksi_signo = sig;
252                 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
253                 /* ksi.ksi_addr = ? */
254                 ksi.ksi_trapno = type;
255                 trapsignal(td, &ksi);
256         }
257
258         userret(td, frame);
259         mtx_assert(&Giant, MA_NOTOWNED);
260 }
261
262 static void
263 trap_fatal(struct trapframe *frame)
264 {
265
266         printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
267 #ifdef KDB
268         if ((debugger_on_panic || kdb_active) &&
269             kdb_trap(frame->exc, 0, frame))
270                 return;
271 #endif
272         panic("%s trap", trapname(frame->exc));
273 }
274
275 static void
276 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
277 {
278
279         printf("\n");
280         printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
281             user ? "user" : "kernel");
282         printf("\n");
283         printf("   exception       = 0x%x (%s)\n", vector >> 8,
284             trapname(vector));
285         switch (vector) {
286         case EXC_DSI:
287                 printf("   virtual address = 0x%x\n", frame->cpu.aim.dar);
288                 break;
289         case EXC_ISI:
290                 printf("   virtual address = 0x%x\n", frame->srr0);
291                 break;
292         }
293         printf("   srr0            = 0x%x\n", frame->srr0);
294         printf("   srr1            = 0x%x\n", frame->srr1);
295         printf("   lr              = 0x%x\n", frame->lr);
296         printf("   curthread       = %p\n", curthread);
297         if (curthread != NULL)
298                 printf("          pid = %d, comm = %s\n",
299                     curthread->td_proc->p_pid, curthread->td_name);
300         printf("\n");
301 }
302
303 /*
304  * Handles a fatal fault when we have onfault state to recover.  Returns
305  * non-zero if there was onfault recovery state available.
306  */
307 static int
308 handle_onfault(struct trapframe *frame)
309 {
310         struct          thread *td;
311         faultbuf        *fb;
312
313         td = curthread;
314         fb = td->td_pcb->pcb_onfault;
315         if (fb != NULL) {
316                 frame->srr0 = (*fb)[0];
317                 frame->fixreg[1] = (*fb)[1];
318                 frame->fixreg[2] = (*fb)[2];
319                 frame->fixreg[3] = 1;
320                 frame->cr = (*fb)[3];
321                 bcopy(&(*fb)[4], &frame->fixreg[13],
322                     19 * sizeof(register_t));
323                 return (1);
324         }
325         return (0);
326 }
327
328 void
329 syscall(struct trapframe *frame)
330 {
331         caddr_t         params;
332         struct          sysent *callp;
333         struct          thread *td;
334         struct          proc *p;
335         int             error, n;
336         size_t          narg;
337         register_t      args[10];
338         u_int           code;
339
340         td = PCPU_GET(curthread);
341         p = td->td_proc;
342
343         PCPU_INC(cnt.v_syscall);
344
345         code = frame->fixreg[0];
346         params = (caddr_t)(frame->fixreg + FIRSTARG);
347         n = NARGREG;
348
349         if (p->p_sysent->sv_prepsyscall) {
350                 /*
351                  * The prep code is MP aware.
352                  */
353                 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, &params);
354         } else if (code == SYS_syscall) {
355                 /*
356                  * code is first argument,
357                  * followed by actual args.
358                  */
359                 code = *(u_int *) params;
360                 params += sizeof(register_t);
361                 n -= 1;
362         } else if (code == SYS___syscall) {
363                 /*
364                  * Like syscall, but code is a quad,
365                  * so as to maintain quad alignment
366                  * for the rest of the args.
367                  */
368                 params += sizeof(register_t);
369                 code = *(u_int *) params;
370                 params += sizeof(register_t);
371                 n -= 2;
372         }
373
374         if (p->p_sysent->sv_mask)
375                 code &= p->p_sysent->sv_mask;
376
377         if (code >= p->p_sysent->sv_size)
378                 callp = &p->p_sysent->sv_table[0];
379         else
380                 callp = &p->p_sysent->sv_table[code];
381
382         narg = callp->sy_narg;
383
384         if (narg > n) {
385                 bcopy(params, args, n * sizeof(register_t));
386                 error = copyin(MOREARGS(frame->fixreg[1]), args + n,
387                                (narg - n) * sizeof(register_t));
388                 params = (caddr_t)args;
389         } else
390                 error = 0;
391
392         CTR5(KTR_SYSC, "syscall: p=%s %s(%x %x %x)", td->td_name,
393              syscallnames[code],
394              frame->fixreg[FIRSTARG],
395              frame->fixreg[FIRSTARG+1],
396              frame->fixreg[FIRSTARG+2]);
397
398 #ifdef  KTRACE
399         if (KTRPOINT(td, KTR_SYSCALL))
400                 ktrsyscall(code, narg, (register_t *)params);
401 #endif
402
403         td->td_syscalls++;
404
405         if (error == 0) {
406                 td->td_retval[0] = 0;
407                 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
408
409                 STOPEVENT(p, S_SCE, narg);
410
411                 PTRACESTOP_SC(p, td, S_PT_SCE);
412
413                 AUDIT_SYSCALL_ENTER(code, td);
414                 error = (*callp->sy_call)(td, params);
415                 AUDIT_SYSCALL_EXIT(error, td);
416
417                 CTR3(KTR_SYSC, "syscall: p=%s %s ret=%x", td->td_name,
418                      syscallnames[code], td->td_retval[0]);
419         }
420         switch (error) {
421         case 0:
422                 if (frame->fixreg[0] == SYS___syscall &&
423                     code != SYS_freebsd6_lseek && code != SYS_lseek) {
424                         /*
425                          * 64-bit return, 32-bit syscall. Fixup byte order
426                          */
427                         frame->fixreg[FIRSTARG] = 0;
428                         frame->fixreg[FIRSTARG + 1] = td->td_retval[0];
429                 } else {
430                         frame->fixreg[FIRSTARG] = td->td_retval[0];
431                         frame->fixreg[FIRSTARG + 1] = td->td_retval[1];
432                 }
433                 /* XXX: Magic number */
434                 frame->cr &= ~0x10000000;
435                 break;
436         case ERESTART:
437                 /*
438                  * Set user's pc back to redo the system call.
439                  */
440                 frame->srr0 -= 4;
441                 break;
442         case EJUSTRETURN:
443                 /* nothing to do */
444                 break;
445         default:
446                 if (p->p_sysent->sv_errsize) {
447                         if (error >= p->p_sysent->sv_errsize)
448                                 error = -1;     /* XXX */
449                         else
450                                 error = p->p_sysent->sv_errtbl[error];
451                 }
452                 frame->fixreg[FIRSTARG] = error;
453                 /* XXX: Magic number: Carry Flag Equivalent? */
454                 frame->cr |= 0x10000000;
455                 break;
456         }
457
458         /*
459          * Check for misbehavior.
460          */
461         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
462             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
463         KASSERT(td->td_critnest == 0,
464             ("System call %s returning in a critical section",
465             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
466         KASSERT(td->td_locks == 0,
467             ("System call %s returning with %d locks held",
468             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
469             td->td_locks));
470
471 #ifdef  KTRACE
472         if (KTRPOINT(td, KTR_SYSRET))
473                 ktrsysret(code, error, td->td_retval[0]);
474 #endif
475
476         /*
477          * Does the comment in the i386 code about errno apply here?
478          */
479         STOPEVENT(p, S_SCX, code);
480  
481         PTRACESTOP_SC(p, td, S_PT_SCX);
482 }
483
484 static int
485 trap_pfault(struct trapframe *frame, int user)
486 {
487         vm_offset_t     eva, va;
488         struct          thread *td;
489         struct          proc *p;
490         vm_map_t        map;
491         vm_prot_t       ftype;
492         int             rv;
493         u_int           user_sr;
494
495         td = curthread;
496         p = td->td_proc;
497         if (frame->exc == EXC_ISI) {
498                 eva = frame->srr0;
499                 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
500         } else {
501                 eva = frame->cpu.aim.dar;
502                 if (frame->cpu.aim.dsisr & DSISR_STORE)
503                         ftype = VM_PROT_WRITE;
504                 else
505                         ftype = VM_PROT_READ;
506         }
507
508         if (user) {
509                 map = &p->p_vmspace->vm_map;
510         } else {
511                 if ((eva >> ADDR_SR_SHFT) == USER_SR) {
512                         if (p->p_vmspace == NULL)
513                                 return (SIGSEGV);
514
515                         __asm ("mfsr %0, %1"
516                             : "=r"(user_sr)
517                             : "K"(USER_SR));
518                         eva &= ADDR_PIDX | ADDR_POFF;
519                         eva |= user_sr << ADDR_SR_SHFT;
520                         map = &p->p_vmspace->vm_map;
521                 } else {
522                         map = kernel_map;
523                 }
524         }
525         va = trunc_page(eva);
526
527         if (map != kernel_map) {
528                 /*
529                  * Keep swapout from messing with us during this
530                  *      critical time.
531                  */
532                 PROC_LOCK(p);
533                 ++p->p_lock;
534                 PROC_UNLOCK(p);
535
536                 /* Fault in the user page: */
537                 rv = vm_fault(map, va, ftype,
538                       (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
539                                               : VM_FAULT_NORMAL);
540
541                 PROC_LOCK(p);
542                 --p->p_lock;
543                 PROC_UNLOCK(p);
544         } else {
545                 /*
546                  * Don't have to worry about process locking or stacks in the
547                  * kernel.
548                  */
549                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
550         }
551
552         if (rv == KERN_SUCCESS)
553                 return (0);
554
555         if (!user && handle_onfault(frame))
556                 return (0);
557
558         return (SIGSEGV);
559 }
560
561 static __inline void
562 setusr(u_int content)
563 {
564         __asm __volatile ("isync; mtsr %0,%1; isync"
565                       :: "n"(USER_SR), "r"(content));
566 }
567
568 int
569 badaddr(void *addr, size_t size)
570 {
571         return (badaddr_read(addr, size, NULL));
572 }
573
574 int
575 badaddr_read(void *addr, size_t size, int *rptr)
576 {
577         struct thread   *td;
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         td = PCPU_GET(curthread);
585
586         if (setfault(env)) {
587                 td->td_pcb->pcb_onfault = 0;
588                 __asm __volatile ("sync");
589                 return 1;
590         }
591
592         __asm __volatile ("sync");
593
594         switch (size) {
595         case 1:
596                 x = *(volatile int8_t *)addr;
597                 break;
598         case 2:
599                 x = *(volatile int16_t *)addr;
600                 break;
601         case 4:
602                 x = *(volatile int32_t *)addr;
603                 break;
604         default:
605                 panic("badaddr: invalid size (%d)", size);
606         }
607
608         /* Make sure we took the machine check, if we caused one. */
609         __asm __volatile ("sync; isync");
610
611         td->td_pcb->pcb_onfault = 0;
612         __asm __volatile ("sync");      /* To be sure. */
613
614         /* Use the value to avoid reorder. */
615         if (rptr)
616                 *rptr = x;
617
618         return (0);
619 }
620
621 /*
622  * For now, this only deals with the particular unaligned access case
623  * that gcc tends to generate.  Eventually it should handle all of the
624  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
625  */
626
627 static int
628 fix_unaligned(struct thread *td, struct trapframe *frame)
629 {
630         struct thread   *fputhread;
631         int             indicator, reg;
632         double          *fpr;
633
634         indicator = EXC_ALI_OPCODE_INDICATOR(frame->cpu.aim.dsisr);
635
636         switch (indicator) {
637         case EXC_ALI_LFD:
638         case EXC_ALI_STFD:
639                 reg = EXC_ALI_RST(frame->cpu.aim.dsisr);
640                 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
641                 fputhread = PCPU_GET(fputhread);
642
643                 /* Juggle the FPU to ensure that we've initialized
644                  * the FPRs, and that their current state is in
645                  * the PCB.
646                  */
647                 if (fputhread != td) {
648                         if (fputhread)
649                                 save_fpu(fputhread);
650                         enable_fpu(td);
651                 }
652                 save_fpu(td);
653
654                 if (indicator == EXC_ALI_LFD) {
655                         if (copyin((void *)frame->cpu.aim.dar, fpr,
656                             sizeof(double)) != 0)
657                                 return -1;
658                         enable_fpu(td);
659                 } else {
660                         if (copyout(fpr, (void *)frame->cpu.aim.dar,
661                             sizeof(double)) != 0)
662                                 return -1;
663                 }
664                 return 0;
665                 break;
666         }
667
668         return -1;
669 }