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