]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/subr_trap.c
MFC r208453:
[FreeBSD/stable/8.git] / sys / kern / subr_trap.c
1 /*-
2  * Copyright (C) 1994, David Greenman
3  * Copyright (c) 1990, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  * Copyright (c) 2007 The FreeBSD Foundation
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the University of Utah, and William Jolitz.
9  *
10  * Portions of this software were developed by A. Joseph Koshy under
11  * sponsorship from the FreeBSD Foundation and Google, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *      from: @(#)trap.c        7.4 (Berkeley) 5/13/91
42  */
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include "opt_ktrace.h"
48 #include "opt_kdtrace.h"
49 #ifdef __i386__
50 #include "opt_npx.h"
51 #endif
52 #include "opt_sched.h"
53
54 #include <sys/param.h>
55 #include <sys/bus.h>
56 #include <sys/kernel.h>
57 #include <sys/lock.h>
58 #include <sys/mutex.h>
59 #include <sys/pmckern.h>
60 #include <sys/proc.h>
61 #include <sys/ktr.h>
62 #include <sys/pioctl.h>
63 #include <sys/ptrace.h>
64 #include <sys/resourcevar.h>
65 #include <sys/sched.h>
66 #include <sys/signalvar.h>
67 #include <sys/syscall.h>
68 #include <sys/sysent.h>
69 #include <sys/systm.h>
70 #include <sys/vmmeter.h>
71 #ifdef KTRACE
72 #include <sys/uio.h>
73 #include <sys/ktrace.h>
74 #endif
75 #include <security/audit/audit.h>
76
77 #include <machine/cpu.h>
78 #include <machine/pcb.h>
79
80 #ifdef XEN
81 #include <vm/vm.h>
82 #include <vm/vm_param.h>
83 #include <vm/pmap.h>
84 #endif
85
86 #include <security/mac/mac_framework.h>
87
88 /*
89  * Define the code needed before returning to user mode, for trap and
90  * syscall.
91  */
92 void
93 userret(struct thread *td, struct trapframe *frame)
94 {
95         struct proc *p = td->td_proc;
96
97         CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
98             td->td_name);
99 #if 0
100 #ifdef DIAGNOSTIC
101         /* Check that we called signotify() enough. */
102         PROC_LOCK(p);
103         thread_lock(td);
104         if (SIGPENDING(td) && ((td->td_flags & TDF_NEEDSIGCHK) == 0 ||
105             (td->td_flags & TDF_ASTPENDING) == 0))
106                 printf("failed to set signal flags properly for ast()\n");
107         thread_unlock(td);
108         PROC_UNLOCK(p);
109 #endif
110 #endif
111 #ifdef KTRACE
112         KTRUSERRET(td);
113 #endif
114         /*
115          * If this thread tickled GEOM, we need to wait for the giggling to
116          * stop before we return to userland
117          */
118         if (td->td_pflags & TDP_GEOM)
119                 g_waitidle();
120
121         /*
122          * Charge system time if profiling.
123          */
124         if (p->p_flag & P_PROFIL)
125                 addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
126         /*
127          * Let the scheduler adjust our priority etc.
128          */
129         sched_userret(td);
130         KASSERT(td->td_locks == 0,
131             ("userret: Returning with %d locks held.", td->td_locks));
132 #ifdef XEN
133         PT_UPDATES_FLUSH();
134 #endif
135 }
136
137 /*
138  * Process an asynchronous software trap.
139  * This is relatively easy.
140  * This function will return with preemption disabled.
141  */
142 void
143 ast(struct trapframe *framep)
144 {
145         struct thread *td;
146         struct proc *p;
147         int flags;
148         int sig;
149 #if defined(DEV_NPX) && !defined(SMP)
150         int ucode;
151         ksiginfo_t ksi;
152 #endif
153
154         td = curthread;
155         p = td->td_proc;
156
157         CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
158             p->p_comm);
159         KASSERT(TRAPF_USERMODE(framep), ("ast in kernel mode"));
160         WITNESS_WARN(WARN_PANIC, NULL, "Returning to user mode");
161         mtx_assert(&Giant, MA_NOTOWNED);
162         THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
163         td->td_frame = framep;
164         td->td_pticks = 0;
165
166         /*
167          * This updates the td_flag's for the checks below in one
168          * "atomic" operation with turning off the astpending flag.
169          * If another AST is triggered while we are handling the
170          * AST's saved in flags, the astpending flag will be set and
171          * ast() will be called again.
172          */
173         thread_lock(td);
174         flags = td->td_flags;
175         td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK |
176             TDF_NEEDRESCHED | TDF_ALRMPEND | TDF_PROFPEND | TDF_MACPEND);
177         thread_unlock(td);
178         PCPU_INC(cnt.v_trap);
179
180         if (td->td_ucred != p->p_ucred) 
181                 cred_update_thread(td);
182         if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
183                 addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
184                 td->td_profil_ticks = 0;
185                 td->td_pflags &= ~TDP_OWEUPC;
186         }
187         if (flags & TDF_ALRMPEND) {
188                 PROC_LOCK(p);
189                 psignal(p, SIGVTALRM);
190                 PROC_UNLOCK(p);
191         }
192 #if defined(DEV_NPX) && !defined(SMP)
193         if (PCPU_GET(curpcb)->pcb_flags & PCB_NPXTRAP) {
194                 atomic_clear_int(&PCPU_GET(curpcb)->pcb_flags,
195                     PCB_NPXTRAP);
196                 ucode = npxtrap();
197                 if (ucode != -1) {
198                         ksiginfo_init_trap(&ksi);
199                         ksi.ksi_signo = SIGFPE;
200                         ksi.ksi_code = ucode;
201                         trapsignal(td, &ksi);
202                 }
203         }
204 #endif
205         if (flags & TDF_PROFPEND) {
206                 PROC_LOCK(p);
207                 psignal(p, SIGPROF);
208                 PROC_UNLOCK(p);
209         }
210 #ifdef MAC
211         if (flags & TDF_MACPEND)
212                 mac_thread_userret(td);
213 #endif
214         if (flags & TDF_NEEDRESCHED) {
215 #ifdef KTRACE
216                 if (KTRPOINT(td, KTR_CSW))
217                         ktrcsw(1, 1);
218 #endif
219                 thread_lock(td);
220                 sched_prio(td, td->td_user_pri);
221                 mi_switch(SW_INVOL | SWT_NEEDRESCHED, NULL);
222                 thread_unlock(td);
223 #ifdef KTRACE
224                 if (KTRPOINT(td, KTR_CSW))
225                         ktrcsw(0, 1);
226 #endif
227         }
228
229         /*
230          * Check for signals. Unlocked reads of p_pendingcnt or
231          * p_siglist might cause process-directed signal to be handled
232          * later.
233          */
234         if (flags & TDF_NEEDSIGCHK || p->p_pendingcnt > 0 ||
235             !SIGISEMPTY(p->p_siglist)) {
236                 PROC_LOCK(p);
237                 mtx_lock(&p->p_sigacts->ps_mtx);
238                 while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
239                         postsig(sig);
240                 mtx_unlock(&p->p_sigacts->ps_mtx);
241                 PROC_UNLOCK(p);
242         }
243         /*
244          * We need to check to see if we have to exit or wait due to a
245          * single threading requirement or some other STOP condition.
246          */
247         if (flags & TDF_NEEDSUSPCHK) {
248                 PROC_LOCK(p);
249                 thread_suspend_check(0);
250                 PROC_UNLOCK(p);
251         }
252
253         if (td->td_pflags & TDP_OLDMASK) {
254                 td->td_pflags &= ~TDP_OLDMASK;
255                 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
256         }
257
258         userret(td, framep);
259         mtx_assert(&Giant, MA_NOTOWNED);
260 }
261
262 #ifdef HAVE_SYSCALL_ARGS_DEF
263 const char *
264 syscallname(struct proc *p, u_int code)
265 {
266         static const char unknown[] = "unknown";
267         struct sysentvec *sv;
268
269         sv = p->p_sysent;
270         if (sv->sv_syscallnames == NULL || code >= sv->sv_size)
271                 return (unknown);
272         return (sv->sv_syscallnames[code]);
273 }
274
275 int
276 syscallenter(struct thread *td, struct syscall_args *sa)
277 {
278         struct proc *p;
279         int error, traced;
280
281         PCPU_INC(cnt.v_syscall);
282         p = td->td_proc;
283         td->td_syscalls++;
284
285         td->td_pticks = 0;
286         if (td->td_ucred != p->p_ucred)
287                 cred_update_thread(td);
288         if (p->p_flag & P_TRACED) {
289                 traced = 1;
290                 PROC_LOCK(p);
291                 td->td_dbgflags &= ~TDB_USERWR;
292                 td->td_dbgflags |= TDB_SCE;
293                 PROC_UNLOCK(p);
294         } else
295                 traced = 0;
296         error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
297 #ifdef KTRACE
298         if (KTRPOINT(td, KTR_SYSCALL))
299                 ktrsyscall(sa->code, sa->narg, sa->args);
300 #endif
301
302         CTR6(KTR_SYSC,
303 "syscall: td=%p pid %d %s (%#lx, %#lx, %#lx)",
304             td, td->td_proc->p_pid, syscallname(p, sa->code),
305             sa->args[0], sa->args[1], sa->args[2]);
306
307         if (error == 0) {
308                 STOPEVENT(p, S_SCE, sa->narg);
309                 PTRACESTOP_SC(p, td, S_PT_SCE);
310                 if (td->td_dbgflags & TDB_USERWR) {
311                         /*
312                          * Reread syscall number and arguments if
313                          * debugger modified registers or memory.
314                          */
315                         error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
316 #ifdef KTRACE
317                         if (KTRPOINT(td, KTR_SYSCALL))
318                                 ktrsyscall(sa->code, sa->narg, sa->args);
319 #endif
320                         if (error != 0)
321                                 goto retval;
322                 }
323
324 #ifdef KDTRACE_HOOKS
325                 /*
326                  * If the systrace module has registered it's probe
327                  * callback and if there is a probe active for the
328                  * syscall 'entry', process the probe.
329                  */
330                 if (systrace_probe_func != NULL && sa->callp->sy_entry != 0)
331                         (*systrace_probe_func)(sa->callp->sy_entry, sa->code,
332                             sa->callp, sa->args);
333 #endif
334
335                 AUDIT_SYSCALL_ENTER(sa->code, td);
336                 error = (sa->callp->sy_call)(td, sa->args);
337                 AUDIT_SYSCALL_EXIT(error, td);
338
339                 /* Save the latest error return value. */
340                 td->td_errno = error;
341
342 #ifdef KDTRACE_HOOKS
343                 /*
344                  * If the systrace module has registered it's probe
345                  * callback and if there is a probe active for the
346                  * syscall 'return', process the probe.
347                  */
348                 if (systrace_probe_func != NULL && sa->callp->sy_return != 0)
349                         (*systrace_probe_func)(sa->callp->sy_return, sa->code,
350                             sa->callp, sa->args);
351 #endif
352                 CTR4(KTR_SYSC, "syscall: p=%p error=%d return %#lx %#lx",
353                     p, error, td->td_retval[0], td->td_retval[1]);
354         }
355  retval:
356         if (traced) {
357                 PROC_LOCK(p);
358                 td->td_dbgflags &= ~TDB_SCE;
359                 PROC_UNLOCK(p);
360         }
361         (p->p_sysent->sv_set_syscall_retval)(td, error);
362         return (error);
363 }
364
365 void
366 syscallret(struct thread *td, int error, struct syscall_args *sa __unused)
367 {
368         struct proc *p;
369         int traced;
370
371         p = td->td_proc;
372
373         /*
374          * Check for misbehavior.
375          */
376         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
377             syscallname(p, sa->code));
378         KASSERT(td->td_critnest == 0,
379             ("System call %s returning in a critical section",
380             syscallname(p, sa->code)));
381         KASSERT(td->td_locks == 0,
382             ("System call %s returning with %d locks held",
383              syscallname(p, sa->code), td->td_locks));
384
385         /*
386          * Handle reschedule and other end-of-syscall issues
387          */
388         userret(td, td->td_frame);
389
390         CTR4(KTR_SYSC, "syscall %s exit thread %p pid %d proc %s",
391             syscallname(p, sa->code), td, td->td_proc->p_pid, td->td_name);
392
393 #ifdef KTRACE
394         if (KTRPOINT(td, KTR_SYSRET))
395                 ktrsysret(sa->code, error, td->td_retval[0]);
396 #endif
397
398         if (p->p_flag & P_TRACED) {
399                 traced = 1;
400                 PROC_LOCK(p);
401                 td->td_dbgflags |= TDB_SCX;
402                 PROC_UNLOCK(p);
403         } else
404                 traced = 0;
405         /*
406          * This works because errno is findable through the
407          * register set.  If we ever support an emulation where this
408          * is not the case, this code will need to be revisited.
409          */
410         STOPEVENT(p, S_SCX, sa->code);
411         PTRACESTOP_SC(p, td, S_PT_SCX);
412         if (traced || (td->td_dbgflags & TDB_EXEC) != 0) {
413                 PROC_LOCK(p);
414                 td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC);
415                 PROC_UNLOCK(p);
416         }
417 }
418 #endif /* HAVE_SYSCALL_ARGS_DEF */