2003-04-01 00:30:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 unmodified, 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/resourcevar.h>
|
2003-04-11 19:24:37 +00:00
|
|
|
#include <sys/sched.h>
|
2004-09-05 02:09:54 +00:00
|
|
|
#include <sys/smp.h>
|
2003-04-01 00:30:30 +00:00
|
|
|
#include <sys/sysent.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sysproto.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/ucontext.h>
|
|
|
|
#include <sys/thr.h>
|
|
|
|
|
|
|
|
#include <machine/frame.h>
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
extern int max_threads_per_proc;
|
|
|
|
extern int max_groups_per_proc;
|
|
|
|
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
|
|
|
* Back end support functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* System call interface.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
thr_create(struct thread *td, struct thr_create_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* ucontext_t *ctx, long *id, int flags */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
2004-09-05 02:09:54 +00:00
|
|
|
struct thread *newtd;
|
2003-04-01 00:30:30 +00:00
|
|
|
ucontext_t ctx;
|
2004-07-02 00:40:07 +00:00
|
|
|
long id;
|
2003-04-01 00:30:30 +00:00
|
|
|
int error;
|
2004-09-05 02:09:54 +00:00
|
|
|
struct ksegrp *kg, *newkg;
|
|
|
|
struct proc *p;
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
kg = td->td_ksegrp;
|
2003-04-01 00:30:30 +00:00
|
|
|
if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
|
|
|
|
return (error);
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
/* Have race condition but it is cheap */
|
|
|
|
if ((p->p_numksegrps >= max_groups_per_proc) ||
|
|
|
|
(p->p_numthreads >= max_threads_per_proc)) {
|
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
|
|
|
/* Initialize our td and new ksegrp.. */
|
|
|
|
newtd = thread_alloc();
|
|
|
|
newkg = ksegrp_alloc();
|
2003-04-01 00:30:30 +00:00
|
|
|
/*
|
|
|
|
* Try the copyout as soon as we allocate the td so we don't have to
|
|
|
|
* tear things down in a failure case below.
|
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
id = newtd->td_tid;
|
2004-07-02 00:40:07 +00:00
|
|
|
if ((error = copyout(&id, uap->id, sizeof(long)))) {
|
2004-09-05 02:09:54 +00:00
|
|
|
ksegrp_free(newkg);
|
|
|
|
thread_free(newtd);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
bzero(&newtd->td_startzero,
|
|
|
|
(unsigned) RANGEOF(struct thread, td_startzero, td_endzero));
|
|
|
|
bcopy(&td->td_startcopy, &newtd->td_startcopy,
|
2003-04-01 00:30:30 +00:00
|
|
|
(unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
|
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
bzero(&newkg->kg_startzero,
|
|
|
|
(unsigned) RANGEOF(struct ksegrp, kg_startzero, kg_endzero));
|
|
|
|
bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
|
|
|
|
(unsigned) RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy));
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
newtd->td_proc = td->td_proc;
|
|
|
|
newtd->td_ucred = crhold(td->td_ucred);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
|
|
|
/* Set up our machine context. */
|
2004-09-05 02:09:54 +00:00
|
|
|
cpu_set_upcall(newtd, td);
|
|
|
|
error = set_mcontext(newtd, &ctx.uc_mcontext);
|
2003-04-01 00:30:30 +00:00
|
|
|
if (error != 0) {
|
2004-09-05 02:09:54 +00:00
|
|
|
ksegrp_free(newkg);
|
|
|
|
thread_free(newtd);
|
|
|
|
crfree(td->td_ucred);
|
2003-04-01 00:30:30 +00:00
|
|
|
goto out;
|
2004-07-02 00:40:07 +00:00
|
|
|
}
|
2003-04-01 00:30:30 +00:00
|
|
|
|
|
|
|
/* Link the thread and kse into the ksegrp and make it runnable. */
|
2004-09-05 02:09:54 +00:00
|
|
|
PROC_LOCK(td->td_proc);
|
|
|
|
td->td_proc->p_flag |= P_HADTHREADS;
|
|
|
|
newtd->td_sigmask = td->td_sigmask;
|
2003-04-01 00:30:30 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2004-09-05 02:09:54 +00:00
|
|
|
ksegrp_link(newkg, p);
|
|
|
|
thread_link(newtd, newkg);
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
sched_init_concurrency(newkg);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
/* let the scheduler know about these things. */
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
sched_fork_ksegrp(td, newkg);
|
|
|
|
sched_fork_thread(td, newtd);
|
2003-04-11 19:24:37 +00:00
|
|
|
|
2004-09-05 02:09:54 +00:00
|
|
|
TD_SET_CAN_RUN(newtd);
|
2003-04-01 00:30:30 +00:00
|
|
|
if ((uap->flags & THR_SUSPENDED) == 0)
|
2004-09-05 02:09:54 +00:00
|
|
|
setrunqueue(newtd, SRQ_BORING);
|
2003-04-01 00:30:30 +00:00
|
|
|
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_self(struct thread *td, struct thr_self_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long *id */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
2004-07-02 00:40:07 +00:00
|
|
|
long id;
|
2003-04-01 00:30:30 +00:00
|
|
|
int error;
|
|
|
|
|
2004-07-02 00:40:07 +00:00
|
|
|
id = td->td_tid;
|
|
|
|
if ((error = copyout(&id, uap->id, sizeof(long))))
|
2003-04-01 00:30:30 +00:00
|
|
|
return (error);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_exit(struct thread *td, struct thr_exit_args *uap)
|
|
|
|
/* NULL */
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
|
|
|
|
PROC_LOCK(p);
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
|
|
|
|
/*
|
2004-09-05 02:09:54 +00:00
|
|
|
* Shutting down last thread in the proc. This will actually
|
|
|
|
* call exit() in the trampoline when it returns.
|
2003-04-01 00:30:30 +00:00
|
|
|
*/
|
2004-09-05 02:09:54 +00:00
|
|
|
if (p->p_numthreads != 1) {
|
|
|
|
thread_exit();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2003-04-01 00:30:30 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2004-09-05 02:09:54 +00:00
|
|
|
PROC_UNLOCK(p);
|
2003-04-01 00:30:30 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_kill(struct thread *td, struct thr_kill_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id, int sig */
|
2003-04-01 00:30:30 +00:00
|
|
|
{
|
|
|
|
struct thread *ttd;
|
|
|
|
struct proc *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
error = 0;
|
|
|
|
PROC_LOCK(p);
|
2003-07-04 06:59:28 +00:00
|
|
|
FOREACH_THREAD_IN_PROC(p, ttd) {
|
2004-07-02 00:40:07 +00:00
|
|
|
if (ttd->td_tid == uap->id)
|
2003-04-01 00:30:30 +00:00
|
|
|
break;
|
2003-07-04 06:59:28 +00:00
|
|
|
}
|
2003-04-01 00:30:30 +00:00
|
|
|
if (ttd == NULL) {
|
|
|
|
error = ESRCH;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (uap->sig == 0)
|
|
|
|
goto out;
|
|
|
|
if (!_SIG_VALID(uap->sig)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2003-07-03 19:09:59 +00:00
|
|
|
tdsignal(ttd, uap->sig, SIGTARGET_TD);
|
2003-04-01 00:30:30 +00:00
|
|
|
out:
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (error);
|
|
|
|
}
|
2004-03-27 14:30:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
thr_suspend(struct thread *td, struct thr_suspend_args *uap)
|
|
|
|
/* const struct timespec *timeout */
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
struct timeval tv;
|
|
|
|
int error;
|
|
|
|
int hz;
|
|
|
|
|
|
|
|
hz = 0;
|
|
|
|
error = 0;
|
|
|
|
if (uap->timeout != NULL) {
|
|
|
|
error = copyin((const void *)uap->timeout, (void *)&ts,
|
|
|
|
sizeof(struct timespec));
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
|
|
|
|
return (EINVAL);
|
|
|
|
if (ts.tv_sec == 0 && ts.tv_nsec == 0)
|
|
|
|
return (ETIMEDOUT);
|
|
|
|
TIMESPEC_TO_TIMEVAL(&tv, &ts);
|
|
|
|
hz = tvtohz(&tv);
|
|
|
|
}
|
|
|
|
PROC_LOCK(td->td_proc);
|
2004-07-12 15:28:31 +00:00
|
|
|
if ((td->td_flags & TDF_THRWAKEUP) == 0)
|
2004-03-27 14:30:43 +00:00
|
|
|
error = msleep((void *)td, &td->td_proc->p_mtx,
|
|
|
|
td->td_priority | PCATCH, "lthr", hz);
|
2004-07-12 15:28:31 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2004-03-27 14:30:43 +00:00
|
|
|
td->td_flags &= ~TDF_THRWAKEUP;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(td->td_proc);
|
|
|
|
return (error == EWOULDBLOCK ? ETIMEDOUT : error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thr_wake(struct thread *td, struct thr_wake_args *uap)
|
2004-07-02 00:40:07 +00:00
|
|
|
/* long id */
|
2004-03-27 14:30:43 +00:00
|
|
|
{
|
2004-07-02 00:40:07 +00:00
|
|
|
struct thread *ttd;
|
2004-03-27 14:30:43 +00:00
|
|
|
|
2004-04-19 14:20:01 +00:00
|
|
|
PROC_LOCK(td->td_proc);
|
|
|
|
FOREACH_THREAD_IN_PROC(td->td_proc, ttd) {
|
2004-07-02 00:40:07 +00:00
|
|
|
if (ttd->td_tid == uap->id)
|
2004-03-27 14:30:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ttd == NULL) {
|
2004-04-19 14:20:01 +00:00
|
|
|
PROC_UNLOCK(td->td_proc);
|
2004-03-27 14:30:43 +00:00
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
mtx_lock_spin(&sched_lock);
|
2004-07-02 00:40:07 +00:00
|
|
|
ttd->td_flags |= TDF_THRWAKEUP;
|
2004-03-27 14:30:43 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2004-07-02 00:40:07 +00:00
|
|
|
wakeup_one((void *)ttd);
|
2004-04-19 14:20:01 +00:00
|
|
|
PROC_UNLOCK(td->td_proc);
|
2004-03-27 14:30:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|