2003-05-14 04:10:49 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (C) 1994, David Greenman
|
|
|
|
* Copyright (c) 1990, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-07-25 21:19:19 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2003-05-14 04:10:49 +00:00
|
|
|
/*
|
|
|
|
* 386 Trap and System call handling
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opt_clock.h"
|
|
|
|
#include "opt_cpu.h"
|
|
|
|
#include "opt_isa.h"
|
|
|
|
#include "opt_ktrace.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/pioctl.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/ktr.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2006-07-27 19:50:16 +00:00
|
|
|
#include <sys/ptrace.h>
|
2003-05-14 04:10:49 +00:00
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/vmmeter.h>
|
|
|
|
#ifdef KTRACE
|
|
|
|
#include <sys/ktrace.h>
|
|
|
|
#endif
|
2006-02-04 20:37:20 +00:00
|
|
|
#include <security/audit/audit.h>
|
2003-05-14 04:10:49 +00:00
|
|
|
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_param.h>
|
|
|
|
#include <vm/pmap.h>
|
|
|
|
#include <vm/vm_kern.h>
|
|
|
|
#include <vm/vm_map.h>
|
|
|
|
#include <vm/vm_page.h>
|
|
|
|
#include <vm/vm_extern.h>
|
|
|
|
|
|
|
|
#include <machine/cpu.h>
|
2003-11-17 08:58:16 +00:00
|
|
|
#include <machine/intr_machdep.h>
|
2003-05-14 04:10:49 +00:00
|
|
|
#include <machine/md_var.h>
|
|
|
|
|
|
|
|
#define IDTVEC(name) __CONCAT(X,name)
|
|
|
|
|
|
|
|
extern inthand_t IDTVEC(int0x80_syscall), IDTVEC(rsvd);
|
2003-08-23 00:04:53 +00:00
|
|
|
extern const char *freebsd32_syscallnames[];
|
2003-05-14 04:10:49 +00:00
|
|
|
|
2006-12-17 06:48:40 +00:00
|
|
|
void ia32_syscall(struct trapframe *frame); /* Called from asm code */
|
2003-05-14 04:10:49 +00:00
|
|
|
|
2010-01-23 11:45:35 +00:00
|
|
|
struct ia32_syscall_args {
|
|
|
|
u_int code;
|
2003-05-14 04:10:49 +00:00
|
|
|
caddr_t params;
|
|
|
|
struct sysent *callp;
|
2010-01-23 11:45:35 +00:00
|
|
|
u_int64_t args64[8];
|
2003-05-14 04:10:49 +00:00
|
|
|
int narg;
|
2010-01-23 11:45:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
fetch_ia32_syscall_args(struct thread *td, struct ia32_syscall_args *sa)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
struct trapframe *frame;
|
2003-05-14 04:10:49 +00:00
|
|
|
u_int32_t args[8];
|
2010-01-23 11:45:35 +00:00
|
|
|
int error, i;
|
2003-05-14 04:10:49 +00:00
|
|
|
|
2010-01-23 11:45:35 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
frame = td->td_frame;
|
|
|
|
|
|
|
|
sa->params = (caddr_t)frame->tf_rsp + sizeof(u_int32_t);
|
|
|
|
sa->code = frame->tf_rax;
|
2003-05-14 04:10:49 +00:00
|
|
|
|
|
|
|
if (p->p_sysent->sv_prepsyscall) {
|
|
|
|
/*
|
|
|
|
* The prep code is MP aware.
|
|
|
|
*/
|
2010-01-23 11:45:35 +00:00
|
|
|
(*p->p_sysent->sv_prepsyscall)(frame, args, &sa->code,
|
|
|
|
&sa->params);
|
2003-05-14 04:10:49 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Need to check if this is a 32 bit or 64 bit syscall.
|
|
|
|
* fuword is MP aware.
|
|
|
|
*/
|
2010-01-23 11:45:35 +00:00
|
|
|
if (sa->code == SYS_syscall) {
|
2003-05-14 04:10:49 +00:00
|
|
|
/*
|
|
|
|
* Code is first argument, followed by actual args.
|
|
|
|
*/
|
2010-01-23 11:45:35 +00:00
|
|
|
sa->code = fuword32(sa->params);
|
|
|
|
sa->params += sizeof(int);
|
|
|
|
} else if (sa->code == SYS___syscall) {
|
2003-05-14 04:10:49 +00:00
|
|
|
/*
|
|
|
|
* Like syscall, but code is a quad, so as to maintain
|
|
|
|
* quad alignment for the rest of the arguments.
|
|
|
|
* We use a 32-bit fetch in case params is not
|
|
|
|
* aligned.
|
|
|
|
*/
|
2010-01-23 11:45:35 +00:00
|
|
|
sa->code = fuword32(sa->params);
|
|
|
|
sa->params += sizeof(quad_t);
|
2003-05-14 04:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->p_sysent->sv_mask)
|
2010-01-23 11:45:35 +00:00
|
|
|
sa->code &= p->p_sysent->sv_mask;
|
|
|
|
if (sa->code >= p->p_sysent->sv_size)
|
|
|
|
sa->callp = &p->p_sysent->sv_table[0];
|
2003-05-14 04:10:49 +00:00
|
|
|
else
|
2010-01-23 11:45:35 +00:00
|
|
|
sa->callp = &p->p_sysent->sv_table[sa->code];
|
|
|
|
sa->narg = sa->callp->sy_narg;
|
2003-05-14 04:10:49 +00:00
|
|
|
|
2010-01-23 11:45:35 +00:00
|
|
|
if (sa->params != NULL && sa->narg != 0)
|
|
|
|
error = copyin(sa->params, (caddr_t)args,
|
|
|
|
(u_int)(sa->narg * sizeof(int)));
|
2003-05-14 04:10:49 +00:00
|
|
|
else
|
|
|
|
error = 0;
|
|
|
|
|
2010-01-23 11:45:35 +00:00
|
|
|
for (i = 0; i < sa->narg; i++)
|
|
|
|
sa->args64[i] = args[i];
|
2003-05-14 04:10:49 +00:00
|
|
|
|
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(td, KTR_SYSCALL))
|
2010-01-23 11:45:35 +00:00
|
|
|
ktrsyscall(sa->code, sa->narg, sa->args64);
|
2003-05-14 04:10:49 +00:00
|
|
|
#endif
|
2010-01-23 11:45:35 +00:00
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ia32_syscall(struct trapframe *frame)
|
|
|
|
{
|
|
|
|
struct thread *td;
|
|
|
|
struct proc *p;
|
|
|
|
struct ia32_syscall_args sa;
|
|
|
|
register_t orig_tf_rflags;
|
|
|
|
int error;
|
|
|
|
ksiginfo_t ksi;
|
|
|
|
|
|
|
|
PCPU_INC(cnt.v_syscall);
|
|
|
|
td = curthread;
|
|
|
|
p = td->td_proc;
|
|
|
|
td->td_syscalls++;
|
|
|
|
|
|
|
|
td->td_pticks = 0;
|
|
|
|
td->td_frame = frame;
|
|
|
|
if (td->td_ucred != p->p_ucred)
|
|
|
|
cred_update_thread(td);
|
|
|
|
orig_tf_rflags = frame->tf_rflags;
|
|
|
|
if (p->p_flag & P_TRACED) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
td->td_dbgflags &= ~TDB_USERWR;
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
|
|
|
error = fetch_ia32_syscall_args(td, &sa);
|
|
|
|
|
2006-07-27 21:25:50 +00:00
|
|
|
CTR4(KTR_SYSC, "syscall enter thread %p pid %d proc %s code %d", td,
|
2010-01-23 11:45:35 +00:00
|
|
|
td->td_proc->p_pid, td->td_name, sa.code);
|
2006-07-27 21:25:50 +00:00
|
|
|
|
2003-05-14 04:10:49 +00:00
|
|
|
if (error == 0) {
|
|
|
|
td->td_retval[0] = 0;
|
2006-12-17 06:48:40 +00:00
|
|
|
td->td_retval[1] = frame->tf_rdx;
|
2003-05-14 04:10:49 +00:00
|
|
|
|
2010-01-23 11:45:35 +00:00
|
|
|
STOPEVENT(p, S_SCE, sa.narg);
|
2006-07-27 19:50:16 +00:00
|
|
|
PTRACESTOP_SC(p, td, S_PT_SCE);
|
2010-01-23 11:45:35 +00:00
|
|
|
if (td->td_dbgflags & TDB_USERWR) {
|
|
|
|
/*
|
|
|
|
* Reread syscall number and arguments if
|
|
|
|
* debugger modified registers or memory.
|
|
|
|
*/
|
|
|
|
error = fetch_ia32_syscall_args(td, &sa);
|
|
|
|
if (error != 0)
|
|
|
|
goto retval;
|
|
|
|
td->td_retval[1] = frame->tf_rdx;
|
|
|
|
}
|
2006-07-27 19:50:16 +00:00
|
|
|
|
2010-01-23 11:45:35 +00:00
|
|
|
AUDIT_SYSCALL_ENTER(sa.code, td);
|
|
|
|
error = (*sa.callp->sy_call)(td, sa.args64);
|
2006-02-04 20:37:20 +00:00
|
|
|
AUDIT_SYSCALL_EXIT(error, td);
|
2003-05-14 04:10:49 +00:00
|
|
|
|
2010-01-23 11:45:35 +00:00
|
|
|
/* Save the latest error return value. */
|
|
|
|
td->td_errno = error;
|
|
|
|
}
|
|
|
|
retval:
|
2009-12-12 20:11:31 +00:00
|
|
|
cpu_set_syscall_retval(td, error);
|
2003-05-14 04:10:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Traced syscall.
|
|
|
|
*/
|
|
|
|
if (orig_tf_rflags & PSL_T) {
|
2006-12-17 06:48:40 +00:00
|
|
|
frame->tf_rflags &= ~PSL_T;
|
1. Change prototype of trapsignal and sendsig to use ksiginfo_t *, most
changes in MD code are trivial, before this change, trapsignal and
sendsig use discrete parameters, now they uses member fields of
ksiginfo_t structure. For sendsig, this change allows us to pass
POSIX realtime signal value to user code.
2. Remove cpu_thread_siginfo, it is no longer needed because we now always
generate ksiginfo_t data and feed it to libpthread.
3. Add p_sigqueue to proc structure to hold shared signals which were
blocked by all threads in the proc.
4. Add td_sigqueue to thread structure to hold all signals delivered to
thread.
5. i386 and amd64 now return POSIX standard si_code, other arches will
be fixed.
6. In this sigqueue implementation, pending signal set is kept as before,
an extra siginfo list holds additional siginfo_t data for signals.
kernel code uses psignal() still behavior as before, it won't be failed
even under memory pressure, only exception is when deleting a signal,
we should call sigqueue_delete to remove signal from sigqueue but
not SIGDELSET. Current there is no kernel code will deliver a signal
with additional data, so kernel should be as stable as before,
a ksiginfo can carry more information, for example, allow signal to
be delivered but throw away siginfo data if memory is not enough.
SIGKILL and SIGSTOP have fast path in sigqueue_add, because they can
not be caught or masked.
The sigqueue() syscall allows user code to queue a signal to target
process, if resource is unavailable, EAGAIN will be returned as
specification said.
Just before thread exits, signal queue memory will be freed by
sigqueue_flush.
Current, all signals are allowed to be queued, not only realtime signals.
Earlier patch reviewed by: jhb, deischen
Tested on: i386, amd64
2005-10-14 12:43:47 +00:00
|
|
|
ksiginfo_init_trap(&ksi);
|
|
|
|
ksi.ksi_signo = SIGTRAP;
|
|
|
|
ksi.ksi_code = TRAP_TRACE;
|
2006-12-17 06:48:40 +00:00
|
|
|
ksi.ksi_addr = (void *)frame->tf_rip;
|
1. Change prototype of trapsignal and sendsig to use ksiginfo_t *, most
changes in MD code are trivial, before this change, trapsignal and
sendsig use discrete parameters, now they uses member fields of
ksiginfo_t structure. For sendsig, this change allows us to pass
POSIX realtime signal value to user code.
2. Remove cpu_thread_siginfo, it is no longer needed because we now always
generate ksiginfo_t data and feed it to libpthread.
3. Add p_sigqueue to proc structure to hold shared signals which were
blocked by all threads in the proc.
4. Add td_sigqueue to thread structure to hold all signals delivered to
thread.
5. i386 and amd64 now return POSIX standard si_code, other arches will
be fixed.
6. In this sigqueue implementation, pending signal set is kept as before,
an extra siginfo list holds additional siginfo_t data for signals.
kernel code uses psignal() still behavior as before, it won't be failed
even under memory pressure, only exception is when deleting a signal,
we should call sigqueue_delete to remove signal from sigqueue but
not SIGDELSET. Current there is no kernel code will deliver a signal
with additional data, so kernel should be as stable as before,
a ksiginfo can carry more information, for example, allow signal to
be delivered but throw away siginfo data if memory is not enough.
SIGKILL and SIGSTOP have fast path in sigqueue_add, because they can
not be caught or masked.
The sigqueue() syscall allows user code to queue a signal to target
process, if resource is unavailable, EAGAIN will be returned as
specification said.
Just before thread exits, signal queue memory will be freed by
sigqueue_flush.
Current, all signals are allowed to be queued, not only realtime signals.
Earlier patch reviewed by: jhb, deischen
Tested on: i386, amd64
2005-10-14 12:43:47 +00:00
|
|
|
trapsignal(td, &ksi);
|
2003-05-14 04:10:49 +00:00
|
|
|
}
|
|
|
|
|
2006-07-27 22:32:30 +00:00
|
|
|
/*
|
|
|
|
* Check for misbehavior.
|
|
|
|
*/
|
|
|
|
WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
|
2010-01-23 11:45:35 +00:00
|
|
|
(sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
|
|
|
|
freebsd32_syscallnames[sa.code] : "???");
|
2006-07-27 22:32:30 +00:00
|
|
|
KASSERT(td->td_critnest == 0,
|
|
|
|
("System call %s returning in a critical section",
|
2010-01-23 11:45:35 +00:00
|
|
|
(sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
|
|
|
|
freebsd32_syscallnames[sa.code] : "???"));
|
2006-07-27 22:32:30 +00:00
|
|
|
KASSERT(td->td_locks == 0,
|
|
|
|
("System call %s returning with %d locks held",
|
2010-01-23 11:45:35 +00:00
|
|
|
(sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
|
|
|
|
freebsd32_syscallnames[sa.code] : "???", td->td_locks));
|
2006-07-27 22:32:30 +00:00
|
|
|
|
2003-05-14 04:10:49 +00:00
|
|
|
/*
|
|
|
|
* Handle reschedule and other end-of-syscall issues
|
|
|
|
*/
|
2006-12-17 06:48:40 +00:00
|
|
|
userret(td, frame);
|
2003-05-14 04:10:49 +00:00
|
|
|
|
2006-07-27 21:25:50 +00:00
|
|
|
CTR4(KTR_SYSC, "syscall exit thread %p pid %d proc %s code %d", td,
|
2010-01-23 11:45:35 +00:00
|
|
|
td->td_proc->p_pid, td->td_proc->p_comm, sa.code);
|
2003-05-14 04:10:49 +00:00
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(td, KTR_SYSRET))
|
2010-01-23 11:45:35 +00:00
|
|
|
ktrsysret(sa.code, error, td->td_retval[0]);
|
2003-05-14 04:10:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-01-23 11:45:35 +00:00
|
|
|
STOPEVENT(p, S_SCX, sa.code);
|
2006-07-27 19:50:16 +00:00
|
|
|
|
|
|
|
PTRACESTOP_SC(p, td, S_PT_SCX);
|
2003-05-14 04:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ia32_syscall_enable(void *dummy)
|
|
|
|
{
|
|
|
|
|
2003-09-22 22:09:02 +00:00
|
|
|
setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYSIGT, SEL_UPL, 0);
|
2003-05-14 04:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ia32_syscall_disable(void *dummy)
|
|
|
|
{
|
|
|
|
|
2003-09-22 22:09:02 +00:00
|
|
|
setidt(IDT_SYSCALL, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0);
|
2003-05-14 04:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SYSINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_enable, NULL);
|
|
|
|
SYSUNINIT(ia32_syscall, SI_SUB_EXEC, SI_ORDER_ANY, ia32_syscall_disable, NULL);
|