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