2011-09-11 16:05:09 +00:00
|
|
|
/*-
|
2017-11-18 14:26:50 +00:00
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
|
|
|
*
|
2011-09-11 16:05:09 +00:00
|
|
|
* Copyright (C) 1994, David Greenman
|
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* the University of Utah, and William Jolitz.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* from: @(#)trap.c 7.4 (Berkeley) 5/13/91
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opt_capsicum.h"
|
|
|
|
#include "opt_ktrace.h"
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2014-03-16 10:55:57 +00:00
|
|
|
#include <sys/capsicum.h>
|
2011-09-11 16:05:09 +00:00
|
|
|
#include <sys/ktr.h>
|
2017-04-17 17:07:51 +00:00
|
|
|
#include <sys/vmmeter.h>
|
2011-09-11 16:05:09 +00:00
|
|
|
#ifdef KTRACE
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/ktrace.h>
|
|
|
|
#endif
|
|
|
|
#include <security/audit/audit.h>
|
|
|
|
|
2019-07-15 21:25:16 +00:00
|
|
|
static inline void
|
2017-06-12 21:03:23 +00:00
|
|
|
syscallenter(struct thread *td)
|
2011-09-11 16:05:09 +00:00
|
|
|
{
|
|
|
|
struct proc *p;
|
2017-06-12 21:03:23 +00:00
|
|
|
struct syscall_args *sa;
|
2011-09-11 16:05:09 +00:00
|
|
|
int error, traced;
|
|
|
|
|
- Remove 'struct vmmeter' from 'struct pcpu', leaving only global vmmeter
in place. To do per-cpu stats, convert all fields that previously were
maintained in the vmmeters that sit in pcpus to counter(9).
- Since some vmmeter stats may be touched at very early stages of boot,
before we have set up UMA and we can do counter_u64_alloc(), provide an
early counter mechanism:
o Leave one spare uint64_t in struct pcpu, named pc_early_dummy_counter.
o Point counter(9) fields of vmmeter to pcpu[0].pc_early_dummy_counter,
so that at early stages of boot, before counters are allocated we already
point to a counter that can be safely written to.
o For sparc64 that required a whole dummy pcpu[MAXCPU] array.
Further related changes:
- Don't include vmmeter.h into pcpu.h.
- vm.stats.vm.v_swappgsout and vm.stats.vm.v_swappgsin changed to 64-bit,
to match kernel representation.
- struct vmmeter hidden under _KERNEL, and only vmstat(1) is an exclusion.
This is based on benno@'s 4-year old patch:
https://lists.freebsd.org/pipermail/freebsd-arch/2013-July/014471.html
Reviewed by: kib, gallatin, marius, lidl
Differential Revision: https://reviews.freebsd.org/D10156
2017-04-17 17:34:47 +00:00
|
|
|
VM_CNT_INC(v_syscall);
|
2011-09-11 16:05:09 +00:00
|
|
|
p = td->td_proc;
|
2017-06-12 21:03:23 +00:00
|
|
|
sa = &td->td_sa;
|
2011-09-11 16:05:09 +00:00
|
|
|
|
|
|
|
td->td_pticks = 0;
|
2018-11-29 04:48:22 +00:00
|
|
|
if (__predict_false(td->td_cowgen != p->p_cowgen))
|
2015-06-10 10:43:59 +00:00
|
|
|
thread_cow_update(td);
|
2015-09-16 20:55:00 +00:00
|
|
|
traced = (p->p_flag & P_TRACED) != 0;
|
2020-02-14 13:08:46 +00:00
|
|
|
if (__predict_false(traced || td->td_dbgflags & TDB_USERWR)) {
|
2011-09-11 16:05:09 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
td->td_dbgflags &= ~TDB_USERWR;
|
2015-09-16 20:55:00 +00:00
|
|
|
if (traced)
|
|
|
|
td->td_dbgflags |= TDB_SCE;
|
2011-09-11 16:05:09 +00:00
|
|
|
PROC_UNLOCK(p);
|
2015-09-16 20:55:00 +00:00
|
|
|
}
|
2017-06-12 21:03:23 +00:00
|
|
|
error = (p->p_sysent->sv_fetch_syscall_args)(td);
|
2011-09-11 16:05:09 +00:00
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(td, KTR_SYSCALL))
|
2020-09-27 18:47:06 +00:00
|
|
|
ktrsyscall(sa->code, sa->callp->sy_narg, sa->args);
|
2011-09-11 16:05:09 +00:00
|
|
|
#endif
|
2012-12-19 20:10:00 +00:00
|
|
|
KTR_START4(KTR_SYSC, "syscall", syscallname(p, sa->code),
|
2012-12-28 06:52:53 +00:00
|
|
|
(uintptr_t)td, "pid:%d", td->td_proc->p_pid, "arg0:%p", sa->args[0],
|
2012-12-19 20:10:00 +00:00
|
|
|
"arg1:%p", sa->args[1], "arg2:%p", sa->args[2]);
|
2011-09-11 16:05:09 +00:00
|
|
|
|
2020-02-14 13:08:46 +00:00
|
|
|
if (__predict_false(error != 0)) {
|
2019-07-15 21:16:01 +00:00
|
|
|
td->td_errno = error;
|
2019-07-09 23:58:12 +00:00
|
|
|
goto retval;
|
2019-07-15 21:16:01 +00:00
|
|
|
}
|
2012-12-19 20:10:00 +00:00
|
|
|
|
2020-09-29 11:18:48 +00:00
|
|
|
if (__predict_false(traced)) {
|
2019-07-09 23:58:12 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
if (p->p_ptevents & PTRACE_SCE)
|
|
|
|
ptracestop((td), SIGTRAP, NULL);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
2020-02-14 13:08:46 +00:00
|
|
|
if (__predict_false((td->td_dbgflags & TDB_USERWR) != 0)) {
|
2019-07-09 23:58:12 +00:00
|
|
|
/*
|
|
|
|
* Reread syscall number and arguments if debugger
|
|
|
|
* modified registers or memory.
|
|
|
|
*/
|
|
|
|
error = (p->p_sysent->sv_fetch_syscall_args)(td);
|
2011-09-11 16:05:09 +00:00
|
|
|
#ifdef KTRACE
|
2019-07-09 23:58:12 +00:00
|
|
|
if (KTRPOINT(td, KTR_SYSCALL))
|
2020-09-27 18:47:06 +00:00
|
|
|
ktrsyscall(sa->code, sa->callp->sy_narg, sa->args);
|
2011-09-11 16:05:09 +00:00
|
|
|
#endif
|
2019-07-15 21:16:01 +00:00
|
|
|
if (error != 0) {
|
|
|
|
td->td_errno = error;
|
2019-07-09 23:58:12 +00:00
|
|
|
goto retval;
|
2019-07-15 21:16:01 +00:00
|
|
|
}
|
2019-07-09 23:58:12 +00:00
|
|
|
}
|
2011-09-11 16:05:09 +00:00
|
|
|
|
|
|
|
#ifdef CAPABILITY_MODE
|
2019-07-09 23:58:12 +00:00
|
|
|
/*
|
|
|
|
* In capability mode, we only allow access to system calls
|
|
|
|
* flagged with SYF_CAPENABLED.
|
|
|
|
*/
|
2020-02-14 13:08:46 +00:00
|
|
|
if (__predict_false(IN_CAPABILITY_MODE(td) &&
|
|
|
|
!(sa->callp->sy_flags & SYF_CAPENABLED))) {
|
2019-07-15 21:16:01 +00:00
|
|
|
td->td_errno = error = ECAPMODE;
|
2019-07-09 23:58:12 +00:00
|
|
|
goto retval;
|
|
|
|
}
|
2011-09-11 16:05:09 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-09 23:58:12 +00:00
|
|
|
error = syscall_thread_enter(td, sa->callp);
|
2019-07-15 21:16:01 +00:00
|
|
|
if (error != 0) {
|
|
|
|
td->td_errno = error;
|
2019-07-09 23:58:12 +00:00
|
|
|
goto retval;
|
2019-07-15 21:16:01 +00:00
|
|
|
}
|
2011-09-11 16:05:09 +00:00
|
|
|
|
Add a way to manage thread signal mask using shared word, instead of syscall.
A new syscall sigfastblock(2) is added which registers a uint32_t
variable as containing the count of blocks for signal delivery. Its
content is read by kernel on each syscall entry and on AST processing,
non-zero count of blocks is interpreted same as the signal mask
blocking all signals.
The biggest downside of the feature that I see is that memory
corruption that affects the registered fast sigblock location, would
cause quite strange application misbehavior. For instance, the process
would be immune to ^C (but killable by SIGKILL).
With consumers (rtld and libthr added), benchmarks do not show a
slow-down of the syscalls in micro-measurements, and macro benchmarks
like buildworld do not demonstrate a difference. Part of the reason is
that buildworld time is dominated by compiler, and clang already links
to libthr. On the other hand, small utilities typically used by shell
scripts have the total number of syscalls cut by half.
The syscall is not exported from the stable libc version namespace on
purpose. It is intended to be used only by our C runtime
implementation internals.
Tested by: pho
Disscussed with: cem, emaste, jilles
Sponsored by: The FreeBSD Foundation
Differential revision: https://reviews.freebsd.org/D12773
2020-02-09 11:53:12 +00:00
|
|
|
/*
|
2020-02-20 15:34:02 +00:00
|
|
|
* Fetch fast sigblock value at the time of syscall entry to
|
|
|
|
* handle sleepqueue primitives which might call cursig().
|
Add a way to manage thread signal mask using shared word, instead of syscall.
A new syscall sigfastblock(2) is added which registers a uint32_t
variable as containing the count of blocks for signal delivery. Its
content is read by kernel on each syscall entry and on AST processing,
non-zero count of blocks is interpreted same as the signal mask
blocking all signals.
The biggest downside of the feature that I see is that memory
corruption that affects the registered fast sigblock location, would
cause quite strange application misbehavior. For instance, the process
would be immune to ^C (but killable by SIGKILL).
With consumers (rtld and libthr added), benchmarks do not show a
slow-down of the syscalls in micro-measurements, and macro benchmarks
like buildworld do not demonstrate a difference. Part of the reason is
that buildworld time is dominated by compiler, and clang already links
to libthr. On the other hand, small utilities typically used by shell
scripts have the total number of syscalls cut by half.
The syscall is not exported from the stable libc version namespace on
purpose. It is intended to be used only by our C runtime
implementation internals.
Tested by: pho
Disscussed with: cem, emaste, jilles
Sponsored by: The FreeBSD Foundation
Differential revision: https://reviews.freebsd.org/D12773
2020-02-09 11:53:12 +00:00
|
|
|
*/
|
2020-02-20 15:34:02 +00:00
|
|
|
if (__predict_false(sigfastblock_fetch_always))
|
|
|
|
(void)sigfastblock_fetch(td);
|
Add a way to manage thread signal mask using shared word, instead of syscall.
A new syscall sigfastblock(2) is added which registers a uint32_t
variable as containing the count of blocks for signal delivery. Its
content is read by kernel on each syscall entry and on AST processing,
non-zero count of blocks is interpreted same as the signal mask
blocking all signals.
The biggest downside of the feature that I see is that memory
corruption that affects the registered fast sigblock location, would
cause quite strange application misbehavior. For instance, the process
would be immune to ^C (but killable by SIGKILL).
With consumers (rtld and libthr added), benchmarks do not show a
slow-down of the syscalls in micro-measurements, and macro benchmarks
like buildworld do not demonstrate a difference. Part of the reason is
that buildworld time is dominated by compiler, and clang already links
to libthr. On the other hand, small utilities typically used by shell
scripts have the total number of syscalls cut by half.
The syscall is not exported from the stable libc version namespace on
purpose. It is intended to be used only by our C runtime
implementation internals.
Tested by: pho
Disscussed with: cem, emaste, jilles
Sponsored by: The FreeBSD Foundation
Differential revision: https://reviews.freebsd.org/D12773
2020-02-09 11:53:12 +00:00
|
|
|
|
2020-02-14 13:09:41 +00:00
|
|
|
/* Let system calls set td_errno directly. */
|
|
|
|
td->td_pflags &= ~TDP_NERRNO;
|
2011-09-11 16:05:09 +00:00
|
|
|
|
2020-02-28 17:05:04 +00:00
|
|
|
if (__predict_false(SYSTRACE_ENABLED() ||
|
|
|
|
AUDIT_SYSCALL_ENTER(sa->code, td))) {
|
2011-09-11 16:05:09 +00:00
|
|
|
#ifdef KDTRACE_HOOKS
|
2020-02-14 13:09:41 +00:00
|
|
|
/* Give the syscall:::entry DTrace probe a chance to fire. */
|
|
|
|
if (__predict_false(sa->callp->sy_entry != 0))
|
|
|
|
(*systrace_probe_func)(sa, SYSTRACE_ENTRY, 0);
|
2011-09-11 16:05:09 +00:00
|
|
|
#endif
|
2020-02-14 13:09:41 +00:00
|
|
|
error = (sa->callp->sy_call)(td, sa->args);
|
|
|
|
/* Save the latest error return value. */
|
|
|
|
if (__predict_false((td->td_pflags & TDP_NERRNO) == 0))
|
|
|
|
td->td_errno = error;
|
|
|
|
AUDIT_SYSCALL_EXIT(error, td);
|
|
|
|
#ifdef KDTRACE_HOOKS
|
|
|
|
/* Give the syscall:::return DTrace probe a chance to fire. */
|
|
|
|
if (__predict_false(sa->callp->sy_return != 0))
|
|
|
|
(*systrace_probe_func)(sa, SYSTRACE_RETURN,
|
|
|
|
error ? -1 : td->td_retval[0]);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
error = (sa->callp->sy_call)(td, sa->args);
|
|
|
|
/* Save the latest error return value. */
|
|
|
|
if (__predict_false((td->td_pflags & TDP_NERRNO) == 0))
|
|
|
|
td->td_errno = error;
|
|
|
|
}
|
2019-07-09 23:58:12 +00:00
|
|
|
syscall_thread_exit(td, sa->callp);
|
|
|
|
|
2011-09-11 16:05:09 +00:00
|
|
|
retval:
|
2012-12-19 20:10:00 +00:00
|
|
|
KTR_STOP4(KTR_SYSC, "syscall", syscallname(p, sa->code),
|
2012-12-28 06:52:53 +00:00
|
|
|
(uintptr_t)td, "pid:%d", td->td_proc->p_pid, "error:%d", error,
|
2012-12-19 20:10:00 +00:00
|
|
|
"retval0:%#lx", td->td_retval[0], "retval1:%#lx",
|
|
|
|
td->td_retval[1]);
|
2020-02-14 13:08:46 +00:00
|
|
|
if (__predict_false(traced)) {
|
2011-09-11 16:05:09 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
td->td_dbgflags &= ~TDB_SCE;
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
|
|
|
(p->p_sysent->sv_set_syscall_retval)(td, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2019-07-15 21:25:16 +00:00
|
|
|
syscallret(struct thread *td)
|
2011-09-11 16:05:09 +00:00
|
|
|
{
|
2018-12-19 20:27:26 +00:00
|
|
|
struct proc *p;
|
2017-06-12 21:03:23 +00:00
|
|
|
struct syscall_args *sa;
|
2016-09-21 08:23:33 +00:00
|
|
|
ksiginfo_t ksi;
|
2019-07-15 21:16:01 +00:00
|
|
|
int traced;
|
2011-09-11 16:05:09 +00:00
|
|
|
|
2015-10-22 09:33:34 +00:00
|
|
|
KASSERT((td->td_pflags & TDP_FORKING) == 0,
|
|
|
|
("fork() did not clear TDP_FORKING upon completion"));
|
|
|
|
|
2011-09-11 16:05:09 +00:00
|
|
|
p = td->td_proc;
|
2017-06-12 21:03:23 +00:00
|
|
|
sa = &td->td_sa;
|
2020-02-14 13:08:46 +00:00
|
|
|
if (__predict_false(td->td_errno == ENOTCAPABLE ||
|
|
|
|
td->td_errno == ECAPMODE)) {
|
|
|
|
if ((trap_enotcap ||
|
|
|
|
(p->p_flag2 & P2_TRAPCAP) != 0) && IN_CAPABILITY_MODE(td)) {
|
2016-09-21 08:23:33 +00:00
|
|
|
ksiginfo_init_trap(&ksi);
|
|
|
|
ksi.ksi_signo = SIGTRAP;
|
2019-07-15 21:16:01 +00:00
|
|
|
ksi.ksi_errno = td->td_errno;
|
2016-09-21 08:23:33 +00:00
|
|
|
ksi.ksi_code = TRAP_CAP;
|
|
|
|
trapsignal(td, &ksi);
|
|
|
|
}
|
|
|
|
}
|
2011-09-11 16:05:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle reschedule and other end-of-syscall issues
|
|
|
|
*/
|
|
|
|
userret(td, td->td_frame);
|
|
|
|
|
|
|
|
#ifdef KTRACE
|
2012-04-12 10:48:43 +00:00
|
|
|
if (KTRPOINT(td, KTR_SYSRET)) {
|
2019-07-15 21:16:01 +00:00
|
|
|
ktrsysret(sa->code, td->td_errno, td->td_retval[0]);
|
2012-04-12 10:48:43 +00:00
|
|
|
}
|
2011-09-11 16:05:09 +00:00
|
|
|
#endif
|
|
|
|
|
2020-02-14 13:08:46 +00:00
|
|
|
traced = 0;
|
|
|
|
if (__predict_false(p->p_flag & P_TRACED)) {
|
2011-09-11 16:05:09 +00:00
|
|
|
traced = 1;
|
|
|
|
PROC_LOCK(p);
|
|
|
|
td->td_dbgflags |= TDB_SCX;
|
|
|
|
PROC_UNLOCK(p);
|
2020-02-14 13:08:46 +00:00
|
|
|
}
|
|
|
|
if (__predict_false(traced ||
|
|
|
|
(td->td_dbgflags & (TDB_EXEC | TDB_FORK)) != 0)) {
|
2011-09-11 16:05:09 +00:00
|
|
|
PROC_LOCK(p);
|
2011-09-27 13:17:02 +00:00
|
|
|
/*
|
|
|
|
* If tracing the execed process, trap to the debugger
|
|
|
|
* so that breakpoints can be set before the program
|
|
|
|
* executes. If debugger requested tracing of syscall
|
|
|
|
* returns, do it now too.
|
|
|
|
*/
|
2012-01-30 20:00:29 +00:00
|
|
|
if (traced &&
|
|
|
|
((td->td_dbgflags & (TDB_FORK | TDB_EXEC)) != 0 ||
|
2016-07-15 15:32:09 +00:00
|
|
|
(p->p_ptevents & PTRACE_SCX) != 0))
|
2017-02-20 15:53:16 +00:00
|
|
|
ptracestop(td, SIGTRAP, NULL);
|
2011-09-11 16:05:09 +00:00
|
|
|
td->td_dbgflags &= ~(TDB_SCX | TDB_EXEC | TDB_FORK);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
2012-02-27 21:10:10 +00:00
|
|
|
|
2018-12-19 20:27:26 +00:00
|
|
|
if (__predict_false(td->td_pflags & TDP_RFPPWAIT))
|
|
|
|
fork_rfppwait(td);
|
2011-09-11 16:05:09 +00:00
|
|
|
}
|