]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/aim/trap.c
This commit was generated by cvs2svn to compensate for changes in r168988,
[FreeBSD/FreeBSD.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/cpu.h>
69 #include <machine/db_machdep.h>
70 #include <machine/fpu.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 #include <machine/sr.h>
78
79 void            trap(struct trapframe *);
80
81 static void     trap_fatal(struct trapframe *frame);
82 static void     printtrap(u_int vector, struct trapframe *frame, int isfatal,
83                     int user);
84 static int      trap_pfault(struct trapframe *frame, int user);
85 static int      fix_unaligned(struct thread *td, 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_LAZY_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)", p->p_comm,
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 #ifdef  ALTIVEC
194                 case EXC_VEC:
195                         if ((vecthread = PCPU_GET(vecthread)) != NULL) {
196                                 KASSERT(vecthread != td,
197                                     ("altivec already enabled"));
198                                 save_vec(vecthread);
199                         }
200                         PCPU_SET(vecthread, td);
201                         td->td_pcb->pcb_veccpu = PCPU_GET(cpuid);
202                         enable_vec(td);
203                         frame->srr1 |= PSL_VEC;
204                         break;
205 #else
206                 case EXC_VEC:
207                 case EXC_VECAST:
208                         sig = SIGILL;
209                         break;
210 #endif /* ALTIVEC */
211
212                 case EXC_ALI:
213                         if (fix_unaligned(td, frame) != 0)
214                                 sig = SIGBUS;
215                         else
216                                 frame->srr0 += 4;
217                         break;
218
219                 case EXC_PGM:
220                         /* XXX temporarily */
221                         /* XXX: Magic Number? */
222                         if (frame->srr1 & 0x0002000)
223                                 sig = SIGTRAP;
224                         else
225                                 sig = SIGILL;
226                         break;
227
228                 default:
229                         trap_fatal(frame);
230                 }
231         } else {
232                 /* Kernel Mode Traps */
233
234                 KASSERT(cold || td->td_ucred != NULL,
235                     ("kernel trap doesn't have ucred"));
236                 switch (type) {
237                 case EXC_DSI:
238                         if (trap_pfault(frame, 0) == 0)
239                                 return;
240                         break;
241                 case EXC_MCHK:
242                         if (handle_onfault(frame))
243                                 return;
244                         break;
245                 default:
246                         break;
247                 }
248                 trap_fatal(frame);
249         }
250
251 #ifdef  ALTIVEC
252         if (td != PCPU_GET(vecthread) ||
253             td->td_pcb->pcb_veccpu != PCPU_GET(cpuid))
254                 frame->srr1 &= ~PSL_VEC;
255 #endif /* ALTIVEC */
256
257         if (sig != 0) {
258                 if (p->p_sysent->sv_transtrap != NULL)
259                         sig = (p->p_sysent->sv_transtrap)(sig, type);
260                 ksiginfo_init_trap(&ksi);
261                 ksi.ksi_signo = sig;
262                 ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
263                 /* ksi.ksi_addr = ? */
264                 ksi.ksi_trapno = type;
265                 trapsignal(td, &ksi);
266         }
267
268         userret(td, frame);
269         mtx_assert(&Giant, MA_NOTOWNED);
270 }
271
272 static void
273 trap_fatal(struct trapframe *frame)
274 {
275
276         printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
277 #ifdef KDB
278         if ((debugger_on_panic || kdb_active) &&
279             kdb_trap(frame->exc, 0, frame))
280                 return;
281 #endif
282         panic("%s trap", trapname(frame->exc));
283 }
284
285 static void
286 printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
287 {
288
289         printf("\n");
290         printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
291             user ? "user" : "kernel");
292         printf("\n");
293         printf("   exception       = 0x%x (%s)\n", vector >> 8,
294             trapname(vector));
295         switch (vector) {
296         case EXC_DSI:
297                 printf("   virtual address = 0x%x\n", frame->dar);
298                 break;
299         case EXC_ISI:
300                 printf("   virtual address = 0x%x\n", frame->srr0);
301                 break;
302         }
303         printf("   srr0            = 0x%x\n", frame->srr0);
304         printf("   srr1            = 0x%x\n", frame->srr1);
305         printf("   curthread       = %p\n", curthread);
306         if (curthread != NULL)
307                 printf("          pid = %d, comm = %s\n",
308                     curthread->td_proc->p_pid, curthread->td_proc->p_comm);
309         printf("\n");
310 }
311
312 /*
313  * Handles a fatal fault when we have onfault state to recover.  Returns
314  * non-zero if there was onfault recovery state available.
315  */
316 static int
317 handle_onfault(struct trapframe *frame)
318 {
319         struct          thread *td;
320         faultbuf        *fb;
321
322         td = curthread;
323         fb = td->td_pcb->pcb_onfault;
324         if (fb != NULL) {
325                 frame->srr0 = (*fb)[0];
326                 frame->fixreg[1] = (*fb)[1];
327                 frame->fixreg[2] = (*fb)[2];
328                 frame->fixreg[3] = 1;
329                 frame->cr = (*fb)[3];
330                 bcopy(&(*fb)[4], &frame->fixreg[13],
331                     19 * sizeof(register_t));
332                 return (1);
333         }
334         return (0);
335 }
336
337 void
338 syscall(struct trapframe *frame)
339 {
340         caddr_t         params;
341         struct          sysent *callp;
342         struct          thread *td;
343         struct          proc *p;
344         int             error, n;
345         size_t          narg;
346         register_t      args[10];
347         u_int           code;
348
349         td = PCPU_GET(curthread);
350         p = td->td_proc;
351
352         PCPU_LAZY_INC(cnt.v_syscall);
353
354 #ifdef KSE
355         if (p->p_flag & P_SA)
356                 thread_user_enter(td);
357 #endif
358
359         code = frame->fixreg[0];
360         params = (caddr_t)(frame->fixreg + FIRSTARG);
361         n = NARGREG;
362
363         if (p->p_sysent->sv_prepsyscall) {
364                 /*
365                  * The prep code is MP aware.
366                  */
367                 (*p->p_sysent->sv_prepsyscall)(frame, args, &code, &params);
368         } else if (code == SYS_syscall) {
369                 /*
370                  * code is first argument,
371                  * followed by actual args.
372                  */
373                 code = *(u_int *) params;
374                 params += sizeof(register_t);
375                 n -= 1;
376         } else if (code == SYS___syscall) {
377                 /*
378                  * Like syscall, but code is a quad,
379                  * so as to maintain quad alignment
380                  * for the rest of the args.
381                  */
382                 params += sizeof(register_t);
383                 code = *(u_int *) params;
384                 params += sizeof(register_t);
385                 n -= 2;
386         }
387
388         if (p->p_sysent->sv_mask)
389                 code &= p->p_sysent->sv_mask;
390
391         if (code >= p->p_sysent->sv_size)
392                 callp = &p->p_sysent->sv_table[0];
393         else
394                 callp = &p->p_sysent->sv_table[code];
395
396         narg = callp->sy_narg;
397
398         if (narg > n) {
399                 bcopy(params, args, n * sizeof(register_t));
400                 error = copyin(MOREARGS(frame->fixreg[1]), args + n,
401                                (narg - n) * sizeof(register_t));
402                 params = (caddr_t)args;
403         } else
404                 error = 0;
405
406         CTR5(KTR_SYSC, "syscall: p=%s %s(%x %x %x)", p->p_comm,
407              syscallnames[code],
408              frame->fixreg[FIRSTARG],
409              frame->fixreg[FIRSTARG+1],
410              frame->fixreg[FIRSTARG+2]);
411
412 #ifdef  KTRACE
413         if (KTRPOINT(td, KTR_SYSCALL))
414                 ktrsyscall(code, narg, (register_t *)params);
415 #endif
416
417         td->td_syscalls++;
418
419         if (error == 0) {
420                 td->td_retval[0] = 0;
421                 td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
422
423                 STOPEVENT(p, S_SCE, narg);
424
425                 PTRACESTOP_SC(p, td, S_PT_SCE);
426
427                 AUDIT_SYSCALL_ENTER(code, td);
428                 error = (*callp->sy_call)(td, params);
429                 AUDIT_SYSCALL_EXIT(error, td);
430
431                 CTR3(KTR_SYSC, "syscall: p=%s %s ret=%x", p->p_comm,
432                      syscallnames[code], td->td_retval[0]);
433         }
434         switch (error) {
435         case 0:
436                 if ((frame->fixreg[0] == SYS___syscall) &&
437                     (code != SYS_lseek)) {
438                         /*
439                          * 64-bit return, 32-bit syscall. Fixup byte order
440                          */
441                         frame->fixreg[FIRSTARG] = 0;
442                         frame->fixreg[FIRSTARG + 1] = td->td_retval[0];
443                 } else {
444                         frame->fixreg[FIRSTARG] = td->td_retval[0];
445                         frame->fixreg[FIRSTARG + 1] = td->td_retval[1];
446                 }
447                 /* XXX: Magic number */
448                 frame->cr &= ~0x10000000;
449                 break;
450         case ERESTART:
451                 /*
452                  * Set user's pc back to redo the system call.
453                  */
454                 frame->srr0 -= 4;
455                 break;
456         case EJUSTRETURN:
457                 /* nothing to do */
458                 break;
459         default:
460                 if (p->p_sysent->sv_errsize) {
461                         if (error >= p->p_sysent->sv_errsize)
462                                 error = -1;     /* XXX */
463                         else
464                                 error = p->p_sysent->sv_errtbl[error];
465                 }
466                 frame->fixreg[FIRSTARG] = error;
467                 /* XXX: Magic number: Carry Flag Equivalent? */
468                 frame->cr |= 0x10000000;
469                 break;
470         }
471
472         /*
473          * Check for misbehavior.
474          */
475         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
476             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
477         KASSERT(td->td_critnest == 0,
478             ("System call %s returning in a critical section",
479             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???"));
480         KASSERT(td->td_locks == 0,
481             ("System call %s returning with %d locks held",
482             (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???",
483             td->td_locks));
484
485 #ifdef  KTRACE
486         if (KTRPOINT(td, KTR_SYSRET))
487                 ktrsysret(code, error, td->td_retval[0]);
488 #endif
489
490         /*
491          * Does the comment in the i386 code about errno apply here?
492          */
493         STOPEVENT(p, S_SCX, code);
494  
495         PTRACESTOP_SC(p, td, S_PT_SCX);
496 }
497
498 static int
499 trap_pfault(struct trapframe *frame, int user)
500 {
501         vm_offset_t     eva, va;
502         struct          thread *td;
503         struct          proc *p;
504         vm_map_t        map;
505         vm_prot_t       ftype;
506         int             rv;
507         u_int           user_sr;
508
509         td = curthread;
510         p = td->td_proc;
511         if (frame->exc == EXC_ISI) {
512                 eva = frame->srr0;
513                 ftype = VM_PROT_READ | VM_PROT_EXECUTE;
514         } else {
515                 eva = frame->dar;
516                 if (frame->dsisr & DSISR_STORE)
517                         ftype = VM_PROT_WRITE;
518                 else
519                         ftype = VM_PROT_READ;
520         }
521
522         if (user) {
523                 map = &p->p_vmspace->vm_map;
524         } else {
525                 if ((eva >> ADDR_SR_SHFT) == USER_SR) {
526                         if (p->p_vmspace == NULL)
527                                 return (SIGSEGV);
528
529                         __asm ("mfsr %0, %1"
530                             : "=r"(user_sr)
531                             : "K"(USER_SR));
532                         eva &= ADDR_PIDX | ADDR_POFF;
533                         eva |= user_sr << ADDR_SR_SHFT;
534                         map = &p->p_vmspace->vm_map;
535                 } else {
536                         map = kernel_map;
537                 }
538         }
539         va = trunc_page(eva);
540
541         if (map != kernel_map) {
542                 /*
543                  * Keep swapout from messing with us during this
544                  *      critical time.
545                  */
546                 PROC_LOCK(p);
547                 ++p->p_lock;
548                 PROC_UNLOCK(p);
549
550                 /* Fault in the user page: */
551                 rv = vm_fault(map, va, ftype,
552                       (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
553                                               : VM_FAULT_NORMAL);
554
555                 PROC_LOCK(p);
556                 --p->p_lock;
557                 PROC_UNLOCK(p);
558         } else {
559                 /*
560                  * Don't have to worry about process locking or stacks in the
561                  * kernel.
562                  */
563                 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
564         }
565
566         if (rv == KERN_SUCCESS)
567                 return (0);
568
569         if (!user && handle_onfault(frame))
570                 return (0);
571
572         return (SIGSEGV);
573 }
574
575 static __inline void
576 setusr(u_int content)
577 {
578         __asm __volatile ("isync; mtsr %0,%1; isync"
579                       :: "n"(USER_SR), "r"(content));
580 }
581
582 int
583 badaddr(void *addr, size_t size)
584 {
585         return (badaddr_read(addr, size, NULL));
586 }
587
588 int
589 badaddr_read(void *addr, size_t size, int *rptr)
590 {
591         struct thread   *td;
592         faultbuf        env;
593         int             x;
594
595         /* Get rid of any stale machine checks that have been waiting.  */
596         __asm __volatile ("sync; isync");
597
598         td = PCPU_GET(curthread);
599
600         if (setfault(env)) {
601                 td->td_pcb->pcb_onfault = 0;
602                 __asm __volatile ("sync");
603                 return 1;
604         }
605
606         __asm __volatile ("sync");
607
608         switch (size) {
609         case 1:
610                 x = *(volatile int8_t *)addr;
611                 break;
612         case 2:
613                 x = *(volatile int16_t *)addr;
614                 break;
615         case 4:
616                 x = *(volatile int32_t *)addr;
617                 break;
618         default:
619                 panic("badaddr: invalid size (%d)", size);
620         }
621
622         /* Make sure we took the machine check, if we caused one. */
623         __asm __volatile ("sync; isync");
624
625         td->td_pcb->pcb_onfault = 0;
626         __asm __volatile ("sync");      /* To be sure. */
627
628         /* Use the value to avoid reorder. */
629         if (rptr)
630                 *rptr = x;
631
632         return (0);
633 }
634
635 /*
636  * For now, this only deals with the particular unaligned access case
637  * that gcc tends to generate.  Eventually it should handle all of the
638  * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
639  */
640
641 static int
642 fix_unaligned(struct thread *td, struct trapframe *frame)
643 {
644         struct thread   *fputhread;
645         int             indicator, reg;
646         double          *fpr;
647
648         indicator = EXC_ALI_OPCODE_INDICATOR(frame->dsisr);
649
650         switch (indicator) {
651         case EXC_ALI_LFD:
652         case EXC_ALI_STFD:
653                 reg = EXC_ALI_RST(frame->dsisr);
654                 fpr = &td->td_pcb->pcb_fpu.fpr[reg];
655                 fputhread = PCPU_GET(fputhread);
656
657                 /* Juggle the FPU to ensure that we've initialized
658                  * the FPRs, and that their current state is in
659                  * the PCB.
660                  */
661                 if (fputhread != td) {
662                         if (fputhread)
663                                 save_fpu(fputhread);
664                         enable_fpu(td);
665                 }
666                 save_fpu(td);
667
668                 if (indicator == EXC_ALI_LFD) {
669                         if (copyin((void *)frame->dar, fpr,
670                             sizeof(double)) != 0)
671                                 return -1;
672                         enable_fpu(td);
673                 } else {
674                         if (copyout(fpr, (void *)frame->dar,
675                             sizeof(double)) != 0)
676                                 return -1;
677                 }
678                 return 0;
679                 break;
680         }
681
682         return -1;
683 }