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