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