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
7 * This code is derived from software contributed to Berkeley by
8 * the University of Utah, and William Jolitz.
10 * Portions of this software were developed by A. Joseph Koshy under
11 * sponsorship from the FreeBSD Foundation and Google, Inc.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
41 * from: @(#)trap.c 7.4 (Berkeley) 5/13/91
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include "opt_ktrace.h"
48 #include "opt_kdtrace.h"
52 #include "opt_sched.h"
54 #include <sys/param.h>
56 #include <sys/kernel.h>
58 #include <sys/mutex.h>
59 #include <sys/pmckern.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>
73 #include <sys/ktrace.h>
75 #include <security/audit/audit.h>
77 #include <machine/cpu.h>
78 #include <machine/pcb.h>
82 #include <vm/vm_param.h>
86 #include <security/mac/mac_framework.h>
89 * Define the code needed before returning to user mode, for trap and
93 userret(struct thread *td, struct trapframe *frame)
95 struct proc *p = td->td_proc;
97 CTR3(KTR_SYSC, "userret: thread %p (pid %d, %s)", td, p->p_pid,
101 /* Check that we called signotify() enough. */
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");
115 * If this thread tickled GEOM, we need to wait for the giggling to
116 * stop before we return to userland
118 if (td->td_pflags & TDP_GEOM)
122 * Charge system time if profiling.
124 if (p->p_flag & P_PROFIL)
125 addupc_task(td, TRAPF_PC(frame), td->td_pticks * psratio);
127 * Let the scheduler adjust our priority etc.
130 KASSERT(td->td_locks == 0,
131 ("userret: Returning with %d locks held.", td->td_locks));
138 * Process an asynchronous software trap.
139 * This is relatively easy.
140 * This function will return with preemption disabled.
143 ast(struct trapframe *framep)
149 #if defined(DEV_NPX) && !defined(SMP)
157 CTR3(KTR_SYSC, "ast: thread %p (pid %d, %s)", td, p->p_pid,
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;
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.
174 flags = td->td_flags;
175 td->td_flags &= ~(TDF_ASTPENDING | TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK |
176 TDF_NEEDRESCHED | TDF_ALRMPEND | TDF_PROFPEND | TDF_MACPEND);
178 PCPU_INC(cnt.v_trap);
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;
187 if (flags & TDF_ALRMPEND) {
189 psignal(p, SIGVTALRM);
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,
198 ksiginfo_init_trap(&ksi);
199 ksi.ksi_signo = SIGFPE;
200 ksi.ksi_code = ucode;
201 trapsignal(td, &ksi);
205 if (flags & TDF_PROFPEND) {
211 if (flags & TDF_MACPEND)
212 mac_thread_userret(td);
214 if (flags & TDF_NEEDRESCHED) {
216 if (KTRPOINT(td, KTR_CSW))
220 sched_prio(td, td->td_user_pri);
221 mi_switch(SW_INVOL | SWT_NEEDRESCHED, NULL);
224 if (KTRPOINT(td, KTR_CSW))
230 * Check for signals. Unlocked reads of p_pendingcnt or
231 * p_siglist might cause process-directed signal to be handled
234 if (flags & TDF_NEEDSIGCHK || p->p_pendingcnt > 0 ||
235 !SIGISEMPTY(p->p_siglist)) {
237 mtx_lock(&p->p_sigacts->ps_mtx);
238 while ((sig = cursig(td, SIG_STOP_ALLOWED)) != 0)
240 mtx_unlock(&p->p_sigacts->ps_mtx);
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.
247 if (flags & TDF_NEEDSUSPCHK) {
249 thread_suspend_check(0);
253 if (td->td_pflags & TDP_OLDMASK) {
254 td->td_pflags &= ~TDP_OLDMASK;
255 kern_sigprocmask(td, SIG_SETMASK, &td->td_oldsigmask, NULL, 0);
259 mtx_assert(&Giant, MA_NOTOWNED);
262 #ifdef HAVE_SYSCALL_ARGS_DEF
264 syscallname(struct proc *p, u_int code)
266 static const char unknown[] = "unknown";
267 struct sysentvec *sv;
270 if (sv->sv_syscallnames == NULL || code >= sv->sv_size)
272 return (sv->sv_syscallnames[code]);
276 syscallenter(struct thread *td, struct syscall_args *sa)
281 PCPU_INC(cnt.v_syscall);
286 if (td->td_ucred != p->p_ucred)
287 cred_update_thread(td);
288 if (p->p_flag & P_TRACED) {
291 td->td_dbgflags &= ~TDB_USERWR;
292 td->td_dbgflags |= TDB_SCE;
296 error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
298 if (KTRPOINT(td, KTR_SYSCALL))
299 ktrsyscall(sa->code, sa->narg, sa->args);
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]);
308 STOPEVENT(p, S_SCE, sa->narg);
309 PTRACESTOP_SC(p, td, S_PT_SCE);
310 if (td->td_dbgflags & TDB_USERWR) {
312 * Reread syscall number and arguments if
313 * debugger modified registers or memory.
315 error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
317 if (KTRPOINT(td, KTR_SYSCALL))
318 ktrsyscall(sa->code, sa->narg, sa->args);
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.
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);
335 AUDIT_SYSCALL_ENTER(sa->code, td);
336 error = (sa->callp->sy_call)(td, sa->args);
337 AUDIT_SYSCALL_EXIT(error, td);
339 /* Save the latest error return value. */
340 td->td_errno = error;
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.
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);
352 CTR4(KTR_SYSC, "syscall: p=%p error=%d return %#lx %#lx",
353 p, error, td->td_retval[0], td->td_retval[1]);
358 td->td_dbgflags &= ~TDB_SCE;
361 (p->p_sysent->sv_set_syscall_retval)(td, error);
366 syscallret(struct thread *td, int error, struct syscall_args *sa __unused)
374 * Check for misbehavior.
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));
386 * Handle reschedule and other end-of-syscall issues
388 userret(td, td->td_frame);
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);
394 if (KTRPOINT(td, KTR_SYSRET))
395 ktrsysret(sa->code, error, td->td_retval[0]);
398 if (p->p_flag & P_TRACED) {
401 td->td_dbgflags |= TDB_SCX;
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.
410 STOPEVENT(p, S_SCX, sa->code);
411 PTRACESTOP_SC(p, td, S_PT_SCX);
412 if (traced || (td->td_dbgflags & TDB_EXEC) != 0) {
414 td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC);
418 #endif /* HAVE_SYSCALL_ARGS_DEF */