summaryrefslogtreecommitdiffstats
path: root/sys/kern/subr_trap.c
diff options
context:
space:
mode:
authorkib <kib@FreeBSD.org>2010-05-23 18:32:02 +0000
committerkib <kib@FreeBSD.org>2010-05-23 18:32:02 +0000
commit4208ccbe79fd8e0ec189a25823cc3bdbe848155d (patch)
tree56fad7d2ceb7c32121f981b707c967936cda94bf /sys/kern/subr_trap.c
parent2b2103d8ae2c5f46d069ef8b5d67cc0061230612 (diff)
downloadFreeBSD-src-4208ccbe79fd8e0ec189a25823cc3bdbe848155d.zip
FreeBSD-src-4208ccbe79fd8e0ec189a25823cc3bdbe848155d.tar.gz
Reorganize syscall entry and leave handling.
Extend struct sysvec with three new elements: sv_fetch_syscall_args - the method to fetch syscall arguments from usermode into struct syscall_args. The structure is machine-depended (this might be reconsidered after all architectures are converted). sv_set_syscall_retval - the method to set a return value for usermode from the syscall. It is a generalization of cpu_set_syscall_retval(9) to allow ABIs to override the way to set a return value. sv_syscallnames - the table of syscall names. Use sv_set_syscall_retval in kern_sigsuspend() instead of hardcoding the call to cpu_set_syscall_retval(). The new functions syscallenter(9) and syscallret(9) are provided that use sv_*syscall* pointers and contain the common repeated code from the syscall() implementations for the architecture-specific syscall trap handlers. Syscallenter() fetches arguments, calls syscall implementation from ABI sysent table, and set up return frame. The end of syscall bookkeeping is done by syscallret(). Take advantage of single place for MI syscall handling code and implement ptrace_lwpinfo pl_flags PL_FLAG_SCE, PL_FLAG_SCX and PL_FLAG_EXEC. The SCE and SCX flags notify the debugger that the thread is stopped at syscall entry or return point respectively. The EXEC flag augments SCX and notifies debugger that the process address space was changed by one of exec(2)-family syscalls. The i386, amd64, sparc64, sun4v, powerpc and ia64 syscall()s are changed to use syscallenter()/syscallret(). MIPS and arm are not converted and use the mostly unchanged syscall() implementation. Reviewed by: jhb, marcel, marius, nwhitehorn, stas Tested by: marcel (ia64), marius (sparc64), nwhitehorn (powerpc), stas (mips) MFC after: 1 month
Diffstat (limited to 'sys/kern/subr_trap.c')
-rw-r--r--sys/kern/subr_trap.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/sys/kern/subr_trap.c b/sys/kern/subr_trap.c
index 4d20ebd..4cc56cc 100644
--- a/sys/kern/subr_trap.c
+++ b/sys/kern/subr_trap.c
@@ -58,15 +58,20 @@ __FBSDID("$FreeBSD$");
#include <sys/pmckern.h>
#include <sys/proc.h>
#include <sys/ktr.h>
+#include <sys/pioctl.h>
+#include <sys/ptrace.h>
#include <sys/resourcevar.h>
#include <sys/sched.h>
#include <sys/signalvar.h>
+#include <sys/syscall.h>
+#include <sys/sysent.h>
#include <sys/systm.h>
#include <sys/vmmeter.h>
#ifdef KTRACE
#include <sys/uio.h>
#include <sys/ktrace.h>
#endif
+#include <security/audit/audit.h>
#include <machine/cpu.h>
#include <machine/pcb.h>
@@ -253,3 +258,160 @@ ast(struct trapframe *framep)
userret(td, framep);
mtx_assert(&Giant, MA_NOTOWNED);
}
+
+#ifdef HAVE_SYSCALL_ARGS_DEF
+static const char *syscallname(struct proc *p, u_int code) __unused;
+static const char *
+syscallname(struct proc *p, u_int code)
+{
+ static const char unknown[] = "unknown";
+
+ if (p->p_sysent->sv_syscallnames == NULL)
+ return (unknown);
+ return (p->p_sysent->sv_syscallnames[code]);
+}
+
+int
+syscallenter(struct thread *td, struct syscall_args *sa)
+{
+ struct proc *p;
+ int error, traced;
+
+ PCPU_INC(cnt.v_syscall);
+ p = td->td_proc;
+ td->td_syscalls++;
+
+ td->td_pticks = 0;
+ if (td->td_ucred != p->p_ucred)
+ cred_update_thread(td);
+ if (p->p_flag & P_TRACED) {
+ traced = 1;
+ PROC_LOCK(p);
+ td->td_dbgflags &= ~TDB_USERWR;
+ td->td_dbgflags |= TDB_SCE;
+ PROC_UNLOCK(p);
+ } else
+ traced = 0;
+ error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
+#ifdef KTRACE
+ if (KTRPOINT(td, KTR_SYSCALL))
+ ktrsyscall(sa->code, sa->narg, sa->args);
+#endif
+
+ CTR6(KTR_SYSC,
+"syscall: td=%p pid %d %s (%#lx, %#lx, %#lx)",
+ td, td->td_proc->p_pid, syscallname(p, sa->code),
+ sa->args[0], sa->args[1], sa->args[2]);
+
+ if (error == 0) {
+ STOPEVENT(p, S_SCE, sa->narg);
+ PTRACESTOP_SC(p, td, S_PT_SCE);
+ if (td->td_dbgflags & TDB_USERWR) {
+ /*
+ * Reread syscall number and arguments if
+ * debugger modified registers or memory.
+ */
+ error = (p->p_sysent->sv_fetch_syscall_args)(td, sa);
+#ifdef KTRACE
+ if (KTRPOINT(td, KTR_SYSCALL))
+ ktrsyscall(sa->code, sa->narg, sa->args);
+#endif
+ if (error != 0)
+ goto retval;
+ }
+
+#ifdef KDTRACE_HOOKS
+ /*
+ * If the systrace module has registered it's probe
+ * callback and if there is a probe active for the
+ * syscall 'entry', process the probe.
+ */
+ if (systrace_probe_func != NULL && sa->callp->sy_entry != 0)
+ (*systrace_probe_func)(sa->callp->sy_entry, sa->code,
+ sa->callp, sa->args);
+#endif
+
+ AUDIT_SYSCALL_ENTER(sa->code, td);
+ error = (sa->callp->sy_call)(td, sa->args);
+ AUDIT_SYSCALL_EXIT(error, td);
+
+ /* Save the latest error return value. */
+ td->td_errno = error;
+
+#ifdef KDTRACE_HOOKS
+ /*
+ * If the systrace module has registered it's probe
+ * callback and if there is a probe active for the
+ * syscall 'return', process the probe.
+ */
+ if (systrace_probe_func != NULL && sa->callp->sy_return != 0)
+ (*systrace_probe_func)(sa->callp->sy_return, sa->code,
+ sa->callp, sa->args);
+#endif
+ CTR4(KTR_SYSC, "syscall: p=%p error=%d return %#lx %#lx",
+ p, error, td->td_retval[0], td->td_retval[1]);
+ }
+ retval:
+ if (traced) {
+ PROC_LOCK(p);
+ td->td_dbgflags &= ~TDB_SCE;
+ PROC_UNLOCK(p);
+ }
+ (p->p_sysent->sv_set_syscall_retval)(td, error);
+ return (error);
+}
+
+void
+syscallret(struct thread *td, int error, struct syscall_args *sa __unused)
+{
+ struct proc *p;
+ int traced;
+
+ p = td->td_proc;
+
+ /*
+ * Check for misbehavior.
+ */
+ WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
+ syscallname(p, sa->code));
+ KASSERT(td->td_critnest == 0,
+ ("System call %s returning in a critical section",
+ syscallname(p, sa->code)));
+ KASSERT(td->td_locks == 0,
+ ("System call %s returning with %d locks held",
+ syscallname(p, sa->code), td->td_locks));
+
+ /*
+ * Handle reschedule and other end-of-syscall issues
+ */
+ userret(td, td->td_frame);
+
+ CTR4(KTR_SYSC, "syscall %s exit thread %p pid %d proc %s",
+ syscallname(p, sa->code), td, td->td_proc->p_pid, td->td_name);
+
+#ifdef KTRACE
+ if (KTRPOINT(td, KTR_SYSRET))
+ ktrsysret(sa->code, error, td->td_retval[0]);
+#endif
+
+ if (p->p_flag & P_TRACED) {
+ traced = 1;
+ PROC_LOCK(p);
+ td->td_dbgflags |= TDB_SCX;
+ PROC_UNLOCK(p);
+ } else
+ traced = 0;
+ /*
+ * This works because errno is findable through the
+ * register set. If we ever support an emulation where this
+ * is not the case, this code will need to be revisited.
+ */
+ STOPEVENT(p, S_SCX, sa->code);
+ PTRACESTOP_SC(p, td, S_PT_SCX);
+ if (traced || (td->td_dbgflags & TDB_EXEC) != 0) {
+ PROC_LOCK(p);
+ td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC);
+ PROC_UNLOCK(p);
+ }
+}
+#endif /* HAVE_SYSCALL_ARGS_DEF */
OpenPOWER on IntegriCloud