2004-01-10 18:34:01 +00:00
|
|
|
/*
|
2002-06-29 07:04:59 +00:00
|
|
|
* Copyright (C) 2001 Julian Elischer <julian@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(s), this list of conditions and the following disclaimer as
|
2004-01-10 18:34:01 +00:00
|
|
|
* the first lines of this file unmodified other than the possible
|
2002-06-29 07:04:59 +00:00
|
|
|
* addition of one or more copyright notices.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice(s), 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 COPYRIGHT HOLDER(S) ``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 COPYRIGHT HOLDER(S) 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$");
|
|
|
|
|
2002-06-29 07:04:59 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/proc.h>
|
2002-11-17 23:26:42 +00:00
|
|
|
#include <sys/smp.h>
|
2002-06-29 07:04:59 +00:00
|
|
|
#include <sys/sysctl.h>
|
2002-10-24 08:46:34 +00:00
|
|
|
#include <sys/sysproto.h>
|
2002-11-21 01:22:38 +00:00
|
|
|
#include <sys/sched.h>
|
2002-06-29 07:04:59 +00:00
|
|
|
#include <sys/signalvar.h>
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
#include <sys/sleepqueue.h>
|
2002-06-29 07:04:59 +00:00
|
|
|
#include <sys/kse.h>
|
|
|
|
#include <sys/ktr.h>
|
|
|
|
#include <vm/uma.h>
|
2002-07-17 23:43:55 +00:00
|
|
|
|
2002-06-29 07:04:59 +00:00
|
|
|
/*
|
2002-09-15 23:52:25 +00:00
|
|
|
* KSEGRP related storage.
|
2002-06-29 07:04:59 +00:00
|
|
|
*/
|
2003-02-17 05:14:26 +00:00
|
|
|
static uma_zone_t upcall_zone;
|
2002-06-29 07:04:59 +00:00
|
|
|
|
2002-09-15 23:52:25 +00:00
|
|
|
/* DEBUG ONLY */
|
2004-06-07 07:25:03 +00:00
|
|
|
extern int virtual_cpu;
|
|
|
|
extern int thread_debug;
|
|
|
|
extern int max_threads_per_proc;
|
|
|
|
extern int max_groups_per_proc;
|
|
|
|
extern int max_threads_hits;
|
|
|
|
extern struct mtx kse_zombie_lock;
|
2002-09-15 23:52:25 +00:00
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
|
2002-06-29 07:04:59 +00:00
|
|
|
#define RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
|
|
|
|
|
2004-01-10 18:34:01 +00:00
|
|
|
TAILQ_HEAD(, kse_upcall) zombie_upcalls =
|
2003-02-17 05:14:26 +00:00
|
|
|
TAILQ_HEAD_INITIALIZER(zombie_upcalls);
|
2002-06-29 07:04:59 +00:00
|
|
|
|
2003-02-26 00:58:23 +00:00
|
|
|
static int thread_update_usr_ticks(struct thread *td, int user);
|
2003-02-17 05:14:26 +00:00
|
|
|
static void thread_alloc_spare(struct thread *td, struct thread *spare);
|
|
|
|
|
2004-06-07 07:25:03 +00:00
|
|
|
/* move to proc.h */
|
|
|
|
extern void kse_purge(struct proc *p, struct thread *td);
|
|
|
|
extern void kse_purge_group(struct thread *td);
|
|
|
|
void kseinit(void);
|
|
|
|
void kse_GC(void);
|
|
|
|
|
|
|
|
static int virtual_cpu;
|
|
|
|
SYSCTL_DECL(_kern_threads);
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
static int
|
|
|
|
sysctl_kse_virtual_cpu(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
int error, new_val;
|
|
|
|
int def_val;
|
|
|
|
|
|
|
|
#ifdef SMP
|
|
|
|
def_val = mp_ncpus;
|
|
|
|
#else
|
|
|
|
def_val = 1;
|
|
|
|
#endif
|
|
|
|
if (virtual_cpu == 0)
|
|
|
|
new_val = def_val;
|
|
|
|
else
|
|
|
|
new_val = virtual_cpu;
|
|
|
|
error = sysctl_handle_int(oidp, &new_val, 0, req);
|
|
|
|
if (error != 0 || req->newptr == NULL)
|
|
|
|
return (error);
|
|
|
|
if (new_val < 0)
|
|
|
|
return (EINVAL);
|
|
|
|
virtual_cpu = new_val;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* DEBUG ONLY */
|
|
|
|
SYSCTL_PROC(_kern_threads, OID_AUTO, virtual_cpu, CTLTYPE_INT|CTLFLAG_RW,
|
|
|
|
0, sizeof(virtual_cpu), sysctl_kse_virtual_cpu, "I",
|
|
|
|
"debug virtual cpus");
|
2002-10-24 08:46:34 +00:00
|
|
|
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
struct kse_upcall *
|
|
|
|
upcall_alloc(void)
|
|
|
|
{
|
|
|
|
struct kse_upcall *ku;
|
|
|
|
|
2003-02-19 09:59:12 +00:00
|
|
|
ku = uma_zalloc(upcall_zone, M_WAITOK);
|
2003-02-17 05:14:26 +00:00
|
|
|
bzero(ku, sizeof(*ku));
|
|
|
|
return (ku);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
upcall_free(struct kse_upcall *ku)
|
|
|
|
{
|
|
|
|
|
|
|
|
uma_zfree(upcall_zone, ku);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
upcall_link(struct kse_upcall *ku, struct ksegrp *kg)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
TAILQ_INSERT_TAIL(&kg->kg_upcalls, ku, ku_link);
|
|
|
|
ku->ku_ksegrp = kg;
|
|
|
|
kg->kg_numupcalls++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
upcall_unlink(struct kse_upcall *ku)
|
|
|
|
{
|
|
|
|
struct ksegrp *kg = ku->ku_ksegrp;
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
KASSERT(ku->ku_owner == NULL, ("%s: have owner", __func__));
|
2004-01-10 18:34:01 +00:00
|
|
|
TAILQ_REMOVE(&kg->kg_upcalls, ku, ku_link);
|
2003-02-17 05:14:26 +00:00
|
|
|
kg->kg_numupcalls--;
|
|
|
|
upcall_stash(ku);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
upcall_remove(struct thread *td)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (td->td_upcall) {
|
|
|
|
td->td_upcall->ku_owner = NULL;
|
|
|
|
upcall_unlink(td->td_upcall);
|
|
|
|
td->td_upcall = 0;
|
2004-01-10 18:34:01 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 08:46:34 +00:00
|
|
|
|
2003-12-07 19:34:29 +00:00
|
|
|
#ifndef _SYS_SYSPROTO_H_
|
|
|
|
struct kse_switchin_args {
|
|
|
|
const struct __mcontext *mcp;
|
|
|
|
long val;
|
|
|
|
long *loc;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
kse_switchin(struct thread *td, struct kse_switchin_args *uap)
|
|
|
|
{
|
|
|
|
mcontext_t mc;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = (uap->mcp == NULL) ? EINVAL : 0;
|
|
|
|
if (!error)
|
|
|
|
error = copyin(uap->mcp, &mc, sizeof(mc));
|
2003-12-10 01:59:23 +00:00
|
|
|
if (!error && uap->loc != NULL)
|
|
|
|
error = (suword(uap->loc, uap->val) != 0) ? EINVAL : 0;
|
2003-12-07 19:34:29 +00:00
|
|
|
if (!error)
|
|
|
|
error = set_mcontext(td, &mc);
|
|
|
|
return ((error == 0) ? EJUSTRETURN : error);
|
|
|
|
}
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
struct kse_thr_interrupt_args {
|
|
|
|
struct kse_thr_mailbox * tmbx;
|
2003-07-17 22:45:33 +00:00
|
|
|
int cmd;
|
|
|
|
long data;
|
2003-02-17 05:14:26 +00:00
|
|
|
};
|
|
|
|
*/
|
2002-10-24 08:46:34 +00:00
|
|
|
int
|
|
|
|
kse_thr_interrupt(struct thread *td, struct kse_thr_interrupt_args *uap)
|
|
|
|
{
|
2002-10-30 02:28:41 +00:00
|
|
|
struct proc *p;
|
|
|
|
struct thread *td2;
|
2002-10-24 08:46:34 +00:00
|
|
|
|
2002-10-31 08:00:51 +00:00
|
|
|
p = td->td_proc;
|
2003-08-26 11:33:15 +00:00
|
|
|
|
2003-07-17 22:45:33 +00:00
|
|
|
if (!(p->p_flag & P_SA))
|
2002-10-30 05:09:29 +00:00
|
|
|
return (EINVAL);
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
|
2003-07-17 22:45:33 +00:00
|
|
|
switch (uap->cmd) {
|
|
|
|
case KSE_INTR_SENDSIG:
|
|
|
|
if (uap->data < 0 || uap->data > _SIG_MAXSIG)
|
|
|
|
return (EINVAL);
|
|
|
|
case KSE_INTR_INTERRUPT:
|
|
|
|
case KSE_INTR_RESTART:
|
|
|
|
PROC_LOCK(p);
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
FOREACH_THREAD_IN_PROC(p, td2) {
|
|
|
|
if (td2->td_mailbox == uap->tmbx)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (td2 == NULL) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
if (uap->cmd == KSE_INTR_SENDSIG) {
|
|
|
|
if (uap->data > 0) {
|
|
|
|
td2->td_flags &= ~TDF_INTERRUPT;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
tdsignal(td2, (int)uap->data, SIGTARGET_TD);
|
|
|
|
} else {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
td2->td_flags |= TDF_INTERRUPT | TDF_ASTPENDING;
|
|
|
|
if (TD_CAN_UNBIND(td2))
|
|
|
|
td2->td_upcall->ku_flags |= KUF_DOUPCALL;
|
|
|
|
if (uap->cmd == KSE_INTR_INTERRUPT)
|
|
|
|
td2->td_intrval = EINTR;
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
else
|
2003-07-17 22:45:33 +00:00
|
|
|
td2->td_intrval = ERESTART;
|
Switch the sleep/wakeup and condition variable implementations to use the
sleep queue interface:
- Sleep queues attempt to merge some of the benefits of both sleep queues
and condition variables. Having sleep qeueus in a hash table avoids
having to allocate a queue head for each wait channel. Thus, struct cv
has shrunk down to just a single char * pointer now. However, the
hash table does not hold threads directly, but queue heads. This means
that once you have located a queue in the hash bucket, you no longer have
to walk the rest of the hash chain looking for threads. Instead, you have
a list of all the threads sleeping on that wait channel.
- Outside of the sleepq code and the sleep/cv code the kernel no longer
differentiates between cv's and sleep/wakeup. For example, calls to
abortsleep() and cv_abort() are replaced with a call to sleepq_abort().
Thus, the TDF_CVWAITQ flag is removed. Also, calls to unsleep() and
cv_waitq_remove() have been replaced with calls to sleepq_remove().
- The sched_sleep() function no longer accepts a priority argument as
sleep's no longer inherently bump the priority. Instead, this is soley
a propery of msleep() which explicitly calls sched_prio() before
blocking.
- The TDF_ONSLEEPQ flag has been dropped as it was never used. The
associated TDF_SET_ONSLEEPQ and TDF_CLR_ON_SLEEPQ macros have also been
dropped and replaced with a single explicit clearing of td_wchan.
TD_SET_ONSLEEPQ() would really have only made sense if it had taken
the wait channel and message as arguments anyway. Now that that only
happens in one place, a macro would be overkill.
2004-02-27 18:52:44 +00:00
|
|
|
if (TD_ON_SLEEPQ(td2) && (td2->td_flags & TDF_SINTR))
|
|
|
|
sleepq_abort(td2);
|
2003-07-17 22:45:33 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2002-10-30 02:28:41 +00:00
|
|
|
}
|
2003-07-17 22:45:33 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
break;
|
|
|
|
case KSE_INTR_SIGEXIT:
|
|
|
|
if (uap->data < 1 || uap->data > _SIG_MAXSIG)
|
|
|
|
return (EINVAL);
|
|
|
|
PROC_LOCK(p);
|
|
|
|
sigexit(td, (int)uap->data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
2002-10-30 02:28:41 +00:00
|
|
|
}
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
return (0);
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
struct kse_exit_args {
|
|
|
|
register_t dummy;
|
|
|
|
};
|
|
|
|
*/
|
2002-10-24 08:46:34 +00:00
|
|
|
int
|
|
|
|
kse_exit(struct thread *td, struct kse_exit_args *uap)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
struct ksegrp *kg;
|
2003-01-04 05:59:25 +00:00
|
|
|
struct kse *ke;
|
2003-06-04 00:12:57 +00:00
|
|
|
struct kse_upcall *ku, *ku2;
|
|
|
|
int error, count;
|
2002-10-24 08:46:34 +00:00
|
|
|
|
|
|
|
p = td->td_proc;
|
2003-06-04 00:12:57 +00:00
|
|
|
if ((ku = td->td_upcall) == NULL || TD_CAN_UNBIND(td))
|
2002-10-30 03:01:28 +00:00
|
|
|
return (EINVAL);
|
2002-10-24 08:46:34 +00:00
|
|
|
kg = td->td_ksegrp;
|
2003-06-04 00:12:57 +00:00
|
|
|
count = 0;
|
2002-10-24 08:46:34 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
mtx_lock_spin(&sched_lock);
|
2003-06-04 00:12:57 +00:00
|
|
|
FOREACH_UPCALL_IN_GROUP(kg, ku2) {
|
|
|
|
if (ku2->ku_flags & KUF_EXITING)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if ((kg->kg_numupcalls - count) == 1 &&
|
|
|
|
(kg->kg_numthreads > 1)) {
|
2002-10-24 08:46:34 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (EDEADLK);
|
|
|
|
}
|
2003-06-04 00:12:57 +00:00
|
|
|
ku->ku_flags |= KUF_EXITING;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
error = suword(&ku->ku_mailbox->km_flags, ku->ku_mflags|KMF_DONE);
|
|
|
|
PROC_LOCK(p);
|
|
|
|
if (error)
|
|
|
|
psignal(p, SIGSEGV);
|
|
|
|
mtx_lock_spin(&sched_lock);
|
2003-02-17 05:14:26 +00:00
|
|
|
upcall_remove(td);
|
2003-06-04 00:12:57 +00:00
|
|
|
ke = td->td_kse;
|
2003-01-04 05:59:25 +00:00
|
|
|
if (p->p_numthreads == 1) {
|
2003-02-17 05:14:26 +00:00
|
|
|
kse_purge(p, td);
|
2003-06-15 00:31:24 +00:00
|
|
|
p->p_flag &= ~P_SA;
|
2002-10-24 08:46:34 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
} else {
|
2003-02-17 05:14:26 +00:00
|
|
|
if (kg->kg_numthreads == 1) { /* Shutdown a group */
|
|
|
|
kse_purge_group(td);
|
|
|
|
ke->ke_flags |= KEF_EXIT;
|
|
|
|
}
|
2003-03-11 00:07:53 +00:00
|
|
|
thread_stopped(p);
|
2002-10-24 08:46:34 +00:00
|
|
|
thread_exit();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2002-10-30 03:01:28 +00:00
|
|
|
return (0);
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
|
|
|
|
2002-12-10 02:33:45 +00:00
|
|
|
/*
|
2002-12-28 01:23:07 +00:00
|
|
|
* Either becomes an upcall or waits for an awakening event and
|
2003-02-17 05:14:26 +00:00
|
|
|
* then becomes an upcall. Only error cases return.
|
2002-12-10 02:33:45 +00:00
|
|
|
*/
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
struct kse_release_args {
|
2003-02-20 08:18:15 +00:00
|
|
|
struct timespec *timeout;
|
2003-02-17 05:14:26 +00:00
|
|
|
};
|
|
|
|
*/
|
2002-10-24 08:46:34 +00:00
|
|
|
int
|
2003-02-17 05:14:26 +00:00
|
|
|
kse_release(struct thread *td, struct kse_release_args *uap)
|
2002-10-24 08:46:34 +00:00
|
|
|
{
|
|
|
|
struct proc *p;
|
2002-12-10 02:33:45 +00:00
|
|
|
struct ksegrp *kg;
|
2003-06-15 12:51:26 +00:00
|
|
|
struct kse_upcall *ku;
|
|
|
|
struct timespec timeout;
|
2003-02-20 08:18:15 +00:00
|
|
|
struct timeval tv;
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
sigset_t sigset;
|
2003-02-20 08:18:15 +00:00
|
|
|
int error;
|
2002-10-24 08:46:34 +00:00
|
|
|
|
|
|
|
p = td->td_proc;
|
2002-12-10 02:33:45 +00:00
|
|
|
kg = td->td_ksegrp;
|
2003-06-15 12:51:26 +00:00
|
|
|
if ((ku = td->td_upcall) == NULL || TD_CAN_UNBIND(td))
|
2002-12-10 02:33:45 +00:00
|
|
|
return (EINVAL);
|
2003-02-20 08:18:15 +00:00
|
|
|
if (uap->timeout != NULL) {
|
|
|
|
if ((error = copyin(uap->timeout, &timeout, sizeof(timeout))))
|
|
|
|
return (error);
|
|
|
|
TIMESPEC_TO_TIMEVAL(&tv, &timeout);
|
|
|
|
}
|
2004-06-02 07:52:36 +00:00
|
|
|
if (td->td_pflags & TDP_SA)
|
2003-06-15 12:51:26 +00:00
|
|
|
td->td_pflags |= TDP_UPCALLING;
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
else {
|
|
|
|
ku->ku_mflags = fuword(&ku->ku_mailbox->km_flags);
|
|
|
|
if (ku->ku_mflags == -1) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
sigexit(td, SIGSEGV);
|
|
|
|
}
|
|
|
|
}
|
2003-02-20 08:18:15 +00:00
|
|
|
PROC_LOCK(p);
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
if (ku->ku_mflags & KMF_WAITSIGEVENT) {
|
|
|
|
/* UTS wants to wait for signal event */
|
2004-04-28 20:36:53 +00:00
|
|
|
if (!(p->p_flag & P_SIGEVENT) && !(ku->ku_flags & KUF_DOUPCALL)) {
|
|
|
|
td->td_kflags |= TDK_KSERELSIG;
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
error = msleep(&p->p_siglist, &p->p_mtx, PPAUSE|PCATCH,
|
|
|
|
"ksesigwait", (uap->timeout ? tvtohz(&tv) : 0));
|
2004-04-28 20:36:53 +00:00
|
|
|
td->td_kflags &= ~(TDK_KSERELSIG | TDK_WAKEUP);
|
|
|
|
}
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
p->p_flag &= ~P_SIGEVENT;
|
|
|
|
sigset = p->p_siglist;
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
error = copyout(&sigset, &ku->ku_mailbox->km_sigscaught,
|
|
|
|
sizeof(sigset));
|
|
|
|
} else {
|
|
|
|
if (! kg->kg_completed && !(ku->ku_flags & KUF_DOUPCALL)) {
|
|
|
|
kg->kg_upsleeps++;
|
2004-04-28 20:36:53 +00:00
|
|
|
td->td_kflags |= TDK_KSEREL;
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
error = msleep(&kg->kg_completed, &p->p_mtx,
|
|
|
|
PPAUSE|PCATCH, "kserel",
|
|
|
|
(uap->timeout ? tvtohz(&tv) : 0));
|
2004-04-28 20:36:53 +00:00
|
|
|
td->td_kflags &= ~(TDK_KSEREL | TDK_WAKEUP);
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
kg->kg_upsleeps--;
|
|
|
|
}
|
|
|
|
PROC_UNLOCK(p);
|
2003-06-15 12:51:26 +00:00
|
|
|
}
|
|
|
|
if (ku->ku_flags & KUF_DOUPCALL) {
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
ku->ku_flags &= ~KUF_DOUPCALL;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
2002-12-10 02:33:45 +00:00
|
|
|
return (0);
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* struct kse_wakeup_args {
|
|
|
|
struct kse_mailbox *mbx;
|
|
|
|
}; */
|
|
|
|
int
|
|
|
|
kse_wakeup(struct thread *td, struct kse_wakeup_args *uap)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
struct ksegrp *kg;
|
2003-02-17 05:14:26 +00:00
|
|
|
struct kse_upcall *ku;
|
2002-12-28 01:23:07 +00:00
|
|
|
struct thread *td2;
|
2002-10-24 08:46:34 +00:00
|
|
|
|
|
|
|
p = td->td_proc;
|
2002-12-28 01:23:07 +00:00
|
|
|
td2 = NULL;
|
2003-02-17 05:14:26 +00:00
|
|
|
ku = NULL;
|
2002-10-24 08:46:34 +00:00
|
|
|
/* KSE-enabled processes only, please. */
|
2003-06-15 00:31:24 +00:00
|
|
|
if (!(p->p_flag & P_SA))
|
2003-02-17 05:14:26 +00:00
|
|
|
return (EINVAL);
|
|
|
|
PROC_LOCK(p);
|
2003-01-03 20:41:49 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2002-10-24 08:46:34 +00:00
|
|
|
if (uap->mbx) {
|
|
|
|
FOREACH_KSEGRP_IN_PROC(p, kg) {
|
2003-02-17 05:14:26 +00:00
|
|
|
FOREACH_UPCALL_IN_GROUP(kg, ku) {
|
2003-02-21 07:11:38 +00:00
|
|
|
if (ku->ku_mailbox == uap->mbx)
|
2003-02-17 05:14:26 +00:00
|
|
|
break;
|
2003-01-03 20:41:49 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
if (ku)
|
2002-12-28 01:23:07 +00:00
|
|
|
break;
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kg = td->td_ksegrp;
|
2003-02-17 05:14:26 +00:00
|
|
|
if (kg->kg_upsleeps) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2004-04-28 20:36:53 +00:00
|
|
|
wakeup_one(&kg->kg_completed);
|
2003-02-17 05:14:26 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (0);
|
2002-12-28 01:23:07 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
ku = TAILQ_FIRST(&kg->kg_upcalls);
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
2004-04-28 20:36:53 +00:00
|
|
|
if (ku == NULL) {
|
2002-10-24 08:46:34 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-02-17 05:14:26 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-04-28 20:36:53 +00:00
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
if ((td2 = ku->ku_owner) == NULL) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
panic("%s: no owner", __func__);
|
|
|
|
} else if (td2->td_kflags & (TDK_KSEREL | TDK_KSERELSIG)) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
if (!(td2->td_kflags & TDK_WAKEUP)) {
|
|
|
|
td2->td_kflags |= TDK_WAKEUP;
|
|
|
|
if (td2->td_kflags & TDK_KSEREL)
|
|
|
|
sleepq_remove(td2, &kg->kg_completed);
|
|
|
|
else
|
|
|
|
sleepq_remove(td2, &p->p_siglist);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ku->ku_flags |= KUF_DOUPCALL;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-01-03 20:41:49 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
PROC_UNLOCK(p);
|
2004-04-28 20:36:53 +00:00
|
|
|
return (0);
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
|
|
|
|
2004-01-10 18:34:01 +00:00
|
|
|
/*
|
2002-10-24 08:46:34 +00:00
|
|
|
* No new KSEG: first call: use current KSE, don't schedule an upcall
|
2003-02-17 05:14:26 +00:00
|
|
|
* All other situations, do allocate max new KSEs and schedule an upcall.
|
2002-10-24 08:46:34 +00:00
|
|
|
*/
|
|
|
|
/* struct kse_create_args {
|
|
|
|
struct kse_mailbox *mbx;
|
|
|
|
int newgroup;
|
|
|
|
}; */
|
|
|
|
int
|
|
|
|
kse_create(struct thread *td, struct kse_create_args *uap)
|
|
|
|
{
|
|
|
|
struct kse *newke;
|
|
|
|
struct ksegrp *newkg;
|
|
|
|
struct ksegrp *kg;
|
|
|
|
struct proc *p;
|
|
|
|
struct kse_mailbox mbx;
|
2003-02-17 05:14:26 +00:00
|
|
|
struct kse_upcall *newku;
|
2003-06-15 12:51:26 +00:00
|
|
|
int err, ncpus, sa = 0, first = 0;
|
|
|
|
struct thread *newtd;
|
2002-10-24 08:46:34 +00:00
|
|
|
|
|
|
|
p = td->td_proc;
|
|
|
|
if ((err = copyin(uap->mbx, &mbx, sizeof(mbx))))
|
|
|
|
return (err);
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/* Too bad, why hasn't kernel always a cpu counter !? */
|
|
|
|
#ifdef SMP
|
|
|
|
ncpus = mp_ncpus;
|
|
|
|
#else
|
|
|
|
ncpus = 1;
|
|
|
|
#endif
|
2003-06-15 12:51:26 +00:00
|
|
|
if (virtual_cpu != 0)
|
2003-02-17 05:14:26 +00:00
|
|
|
ncpus = virtual_cpu;
|
2003-06-15 12:51:26 +00:00
|
|
|
if (!(mbx.km_flags & KMF_BOUND))
|
2004-06-02 07:52:36 +00:00
|
|
|
sa = TDP_SA;
|
2003-06-16 13:14:52 +00:00
|
|
|
else
|
|
|
|
ncpus = 1;
|
2003-03-11 03:16:02 +00:00
|
|
|
PROC_LOCK(p);
|
2003-06-15 12:51:26 +00:00
|
|
|
if (!(p->p_flag & P_SA)) {
|
|
|
|
first = 1;
|
|
|
|
p->p_flag |= P_SA;
|
|
|
|
}
|
2003-03-11 03:16:02 +00:00
|
|
|
PROC_UNLOCK(p);
|
2003-06-15 12:51:26 +00:00
|
|
|
if (!sa && !uap->newgroup && !first)
|
|
|
|
return (EINVAL);
|
2002-10-24 08:46:34 +00:00
|
|
|
kg = td->td_ksegrp;
|
|
|
|
if (uap->newgroup) {
|
2004-01-10 18:34:01 +00:00
|
|
|
/* Have race condition but it is cheap */
|
2003-06-15 12:51:26 +00:00
|
|
|
if (p->p_numksegrps >= max_groups_per_proc)
|
2002-11-17 11:47:03 +00:00
|
|
|
return (EPROCLIM);
|
2004-01-10 18:34:01 +00:00
|
|
|
/*
|
2002-10-24 08:46:34 +00:00
|
|
|
* If we want a new KSEGRP it doesn't matter whether
|
|
|
|
* we have already fired up KSE mode before or not.
|
2003-02-17 05:14:26 +00:00
|
|
|
* We put the process in KSE mode and create a new KSEGRP.
|
2002-10-24 08:46:34 +00:00
|
|
|
*/
|
|
|
|
newkg = ksegrp_alloc();
|
|
|
|
bzero(&newkg->kg_startzero, RANGEOF(struct ksegrp,
|
2003-02-17 05:14:26 +00:00
|
|
|
kg_startzero, kg_endzero));
|
2002-10-24 08:46:34 +00:00
|
|
|
bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
|
|
|
|
RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy));
|
2004-01-10 18:34:01 +00:00
|
|
|
PROC_LOCK(p);
|
2003-02-17 05:14:26 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
if (p->p_numksegrps >= max_groups_per_proc) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-08-26 11:33:15 +00:00
|
|
|
PROC_UNLOCK(p);
|
2003-02-28 15:57:33 +00:00
|
|
|
ksegrp_free(newkg);
|
2003-02-17 05:14:26 +00:00
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
2003-02-28 15:57:33 +00:00
|
|
|
ksegrp_link(newkg, p);
|
2003-08-26 11:33:15 +00:00
|
|
|
sched_fork_ksegrp(kg, newkg);
|
2003-02-17 05:14:26 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-08-26 11:33:15 +00:00
|
|
|
PROC_UNLOCK(p);
|
2002-10-24 08:46:34 +00:00
|
|
|
} else {
|
2004-06-02 07:52:36 +00:00
|
|
|
if (!first && ((td->td_pflags & TDP_SA) ^ sa) != 0)
|
2003-06-16 23:46:41 +00:00
|
|
|
return (EINVAL);
|
2003-02-17 05:14:26 +00:00
|
|
|
newkg = kg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creating upcalls more than number of physical cpu does
|
2004-01-10 18:34:01 +00:00
|
|
|
* not help performance.
|
2003-02-17 05:14:26 +00:00
|
|
|
*/
|
|
|
|
if (newkg->kg_numupcalls >= ncpus)
|
|
|
|
return (EPROCLIM);
|
|
|
|
|
|
|
|
if (newkg->kg_numupcalls == 0) {
|
|
|
|
/*
|
2003-06-15 12:51:26 +00:00
|
|
|
* Initialize KSE group
|
|
|
|
*
|
|
|
|
* For multiplxed group, create KSEs as many as physical
|
|
|
|
* cpus. This increases concurrent even if userland
|
|
|
|
* is not MP safe and can only run on single CPU.
|
2003-02-17 05:14:26 +00:00
|
|
|
* In ideal world, every physical cpu should execute a thread.
|
|
|
|
* If there is enough KSEs, threads in kernel can be
|
2004-01-10 18:34:01 +00:00
|
|
|
* executed parallel on different cpus with full speed,
|
|
|
|
* Concurrent in kernel shouldn't be restricted by number of
|
2003-06-15 12:51:26 +00:00
|
|
|
* upcalls userland provides. Adding more upcall structures
|
|
|
|
* only increases concurrent in userland.
|
|
|
|
*
|
|
|
|
* For bound thread group, because there is only thread in the
|
|
|
|
* group, we only create one KSE for the group. Thread in this
|
|
|
|
* kind of group will never schedule an upcall when blocked,
|
|
|
|
* this intends to simulate pthread system scope thread.
|
2002-10-24 08:46:34 +00:00
|
|
|
*/
|
2003-02-17 05:14:26 +00:00
|
|
|
while (newkg->kg_kses < ncpus) {
|
2002-10-24 08:46:34 +00:00
|
|
|
newke = kse_alloc();
|
2003-02-17 05:14:26 +00:00
|
|
|
bzero(&newke->ke_startzero, RANGEOF(struct kse,
|
|
|
|
ke_startzero, ke_endzero));
|
2002-10-24 08:46:34 +00:00
|
|
|
#if 0
|
2003-02-17 05:14:26 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
bcopy(&ke->ke_startcopy, &newke->ke_startcopy,
|
|
|
|
RANGEOF(struct kse, ke_startcopy, ke_endcopy));
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2002-10-24 08:46:34 +00:00
|
|
|
#endif
|
2003-02-17 05:14:26 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
kse_link(newke, newkg);
|
2003-08-26 11:33:15 +00:00
|
|
|
sched_fork_kse(td->td_kse, newke);
|
2003-02-17 05:14:26 +00:00
|
|
|
/* Add engine */
|
|
|
|
kse_reassign(newke);
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-02-01 12:17:09 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
}
|
|
|
|
newku = upcall_alloc();
|
|
|
|
newku->ku_mailbox = uap->mbx;
|
|
|
|
newku->ku_func = mbx.km_func;
|
|
|
|
bcopy(&mbx.km_stack, &newku->ku_stack, sizeof(stack_t));
|
|
|
|
|
|
|
|
/* For the first call this may not have been set */
|
|
|
|
if (td->td_standin == NULL)
|
|
|
|
thread_alloc_spare(td, NULL);
|
|
|
|
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
PROC_LOCK(p);
|
2003-02-17 05:14:26 +00:00
|
|
|
if (newkg->kg_numupcalls >= ncpus) {
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
PROC_UNLOCK(p);
|
2003-02-27 05:42:01 +00:00
|
|
|
upcall_free(newku);
|
2003-02-17 05:14:26 +00:00
|
|
|
return (EPROCLIM);
|
|
|
|
}
|
2003-07-15 14:04:38 +00:00
|
|
|
if (first && sa) {
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
SIGSETOR(p->p_siglist, td->td_siglist);
|
|
|
|
SIGEMPTYSET(td->td_siglist);
|
|
|
|
SIGFILLSET(td->td_sigmask);
|
|
|
|
SIG_CANTMASK(td->td_sigmask);
|
|
|
|
}
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
PROC_UNLOCK(p);
|
2003-02-17 05:14:26 +00:00
|
|
|
upcall_link(newku, newkg);
|
2003-03-19 05:49:38 +00:00
|
|
|
if (mbx.km_quantum)
|
|
|
|
newkg->kg_upquantum = max(1, mbx.km_quantum/tick);
|
2003-02-17 05:14:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each upcall structure has an owner thread, find which
|
|
|
|
* one owns it.
|
|
|
|
*/
|
|
|
|
if (uap->newgroup) {
|
2004-01-10 18:34:01 +00:00
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* Because new ksegrp hasn't thread,
|
|
|
|
* create an initial upcall thread to own it.
|
|
|
|
*/
|
2003-06-15 12:51:26 +00:00
|
|
|
newtd = thread_schedule_upcall(td, newku);
|
2002-10-24 08:46:34 +00:00
|
|
|
} else {
|
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* If current thread hasn't an upcall structure,
|
|
|
|
* just assign the upcall to it.
|
2002-10-24 08:46:34 +00:00
|
|
|
*/
|
2003-02-17 05:14:26 +00:00
|
|
|
if (td->td_upcall == NULL) {
|
|
|
|
newku->ku_owner = td;
|
|
|
|
td->td_upcall = newku;
|
2003-06-15 12:51:26 +00:00
|
|
|
newtd = td;
|
2003-02-17 05:14:26 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Create a new upcall thread to own it.
|
|
|
|
*/
|
2003-06-15 12:51:26 +00:00
|
|
|
newtd = thread_schedule_upcall(td, newku);
|
2003-02-17 05:14:26 +00:00
|
|
|
}
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
2003-06-15 12:51:26 +00:00
|
|
|
if (!sa) {
|
|
|
|
newtd->td_mailbox = mbx.km_curthread;
|
2004-06-02 07:52:36 +00:00
|
|
|
newtd->td_pflags &= ~TDP_SA;
|
2003-06-20 09:12:12 +00:00
|
|
|
if (newtd != td) {
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
cpu_set_upcall_kse(newtd, newku);
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
}
|
2003-06-15 12:51:26 +00:00
|
|
|
} else {
|
2004-06-02 07:52:36 +00:00
|
|
|
newtd->td_pflags |= TDP_SA;
|
2003-06-15 12:51:26 +00:00
|
|
|
}
|
2003-06-20 09:12:12 +00:00
|
|
|
if (newtd != td)
|
|
|
|
setrunqueue(newtd);
|
2003-02-17 05:14:26 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2002-10-24 08:46:34 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-06-29 07:04:59 +00:00
|
|
|
/*
|
2004-06-07 07:25:03 +00:00
|
|
|
* Initialize global kse related resources.
|
2002-06-29 07:04:59 +00:00
|
|
|
*/
|
|
|
|
void
|
2004-06-07 07:25:03 +00:00
|
|
|
kseinit(void)
|
2002-06-29 07:04:59 +00:00
|
|
|
{
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
upcall_zone = uma_zcreate("UPCALL", sizeof(struct kse_upcall),
|
|
|
|
NULL, NULL, NULL, NULL, UMA_ALIGN_CACHE, 0);
|
2002-06-29 07:04:59 +00:00
|
|
|
}
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
* Stash an embarasingly extra upcall into the zombie upcall queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
upcall_stash(struct kse_upcall *ku)
|
|
|
|
{
|
|
|
|
mtx_lock_spin(&kse_zombie_lock);
|
|
|
|
TAILQ_INSERT_HEAD(&zombie_upcalls, ku, ku_link);
|
|
|
|
mtx_unlock_spin(&kse_zombie_lock);
|
2002-10-24 08:46:34 +00:00
|
|
|
}
|
|
|
|
|
2002-09-16 19:26:48 +00:00
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* Reap zombie kse resource.
|
2002-06-29 07:04:59 +00:00
|
|
|
*/
|
|
|
|
void
|
2004-06-07 07:25:03 +00:00
|
|
|
kse_GC(void)
|
2002-06-29 07:04:59 +00:00
|
|
|
{
|
2003-02-17 05:14:26 +00:00
|
|
|
struct kse_upcall *ku_first, *ku_next;
|
2002-06-29 07:04:59 +00:00
|
|
|
|
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* Don't even bother to lock if none at this instant,
|
|
|
|
* we really don't care about the next instant..
|
2002-06-29 07:04:59 +00:00
|
|
|
*/
|
2004-06-07 07:25:03 +00:00
|
|
|
if ((!TAILQ_EMPTY(&zombie_upcalls))) {
|
2003-02-17 05:14:26 +00:00
|
|
|
mtx_lock_spin(&kse_zombie_lock);
|
|
|
|
ku_first = TAILQ_FIRST(&zombie_upcalls);
|
|
|
|
if (ku_first)
|
|
|
|
TAILQ_INIT(&zombie_upcalls);
|
|
|
|
mtx_unlock_spin(&kse_zombie_lock);
|
|
|
|
while (ku_first) {
|
|
|
|
ku_next = TAILQ_NEXT(ku_first, ku_link);
|
|
|
|
upcall_free(ku_first);
|
|
|
|
ku_first = ku_next;
|
|
|
|
}
|
2002-06-29 07:04:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the thread context in the UTS's mailbox.
|
2002-09-27 07:11:11 +00:00
|
|
|
* then add the mailbox at the head of a list we are building in user space.
|
|
|
|
* The list is anchored in the ksegrp structure.
|
2002-06-29 07:04:59 +00:00
|
|
|
*/
|
|
|
|
int
|
2003-07-17 22:45:33 +00:00
|
|
|
thread_export_context(struct thread *td, int willexit)
|
2002-06-29 07:04:59 +00:00
|
|
|
{
|
2002-10-05 04:49:46 +00:00
|
|
|
struct proc *p;
|
2002-09-27 07:11:11 +00:00
|
|
|
struct ksegrp *kg;
|
|
|
|
uintptr_t mbx;
|
|
|
|
void *addr;
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
int error = 0, temp, sig;
|
2003-06-04 00:12:57 +00:00
|
|
|
mcontext_t mc;
|
2002-06-29 07:04:59 +00:00
|
|
|
|
2002-10-05 04:49:46 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
kg = td->td_ksegrp;
|
|
|
|
|
2002-09-16 19:26:48 +00:00
|
|
|
/* Export the user/machine context. */
|
2003-06-04 00:12:57 +00:00
|
|
|
get_mcontext(td, &mc, 0);
|
|
|
|
addr = (void *)(&td->td_mailbox->tm_context.uc_mcontext);
|
|
|
|
error = copyout(&mc, addr, sizeof(mcontext_t));
|
|
|
|
if (error)
|
2002-12-28 01:23:07 +00:00
|
|
|
goto bad;
|
2002-06-29 07:04:59 +00:00
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/* Exports clock ticks in kernel mode */
|
|
|
|
addr = (caddr_t)(&td->td_mailbox->tm_sticks);
|
2003-06-28 19:45:15 +00:00
|
|
|
temp = fuword32(addr) + td->td_usticks;
|
|
|
|
if (suword32(addr, temp)) {
|
2003-06-04 00:12:57 +00:00
|
|
|
error = EFAULT;
|
2003-02-17 05:14:26 +00:00
|
|
|
goto bad;
|
2003-06-04 00:12:57 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
/*
|
|
|
|
* Post sync signal, or process SIGKILL and SIGSTOP.
|
|
|
|
* For sync signal, it is only possible when the signal is not
|
|
|
|
* caught by userland or process is being debugged.
|
|
|
|
*/
|
2003-07-17 22:45:33 +00:00
|
|
|
PROC_LOCK(p);
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
if (td->td_flags & TDF_NEEDSIGCHK) {
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
td->td_flags &= ~TDF_NEEDSIGCHK;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
mtx_lock(&p->p_sigacts->ps_mtx);
|
|
|
|
while ((sig = cursig(td)) != 0)
|
|
|
|
postsig(sig);
|
|
|
|
mtx_unlock(&p->p_sigacts->ps_mtx);
|
|
|
|
}
|
2003-07-17 22:45:33 +00:00
|
|
|
if (willexit)
|
|
|
|
SIGFILLSET(td->td_sigmask);
|
|
|
|
PROC_UNLOCK(p);
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/* Get address in latest mbox of list pointer */
|
2002-09-27 07:11:11 +00:00
|
|
|
addr = (void *)(&td->td_mailbox->tm_next);
|
|
|
|
/*
|
|
|
|
* Put the saved address of the previous first
|
|
|
|
* entry into this one
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
mbx = (uintptr_t)kg->kg_completed;
|
|
|
|
if (suword(addr, mbx)) {
|
2002-12-28 01:23:07 +00:00
|
|
|
error = EFAULT;
|
2002-11-18 01:59:31 +00:00
|
|
|
goto bad;
|
2002-09-27 07:11:11 +00:00
|
|
|
}
|
2002-09-29 02:48:37 +00:00
|
|
|
PROC_LOCK(p);
|
2002-09-27 07:11:11 +00:00
|
|
|
if (mbx == (uintptr_t)kg->kg_completed) {
|
|
|
|
kg->kg_completed = td->td_mailbox;
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
* The thread context may be taken away by
|
|
|
|
* other upcall threads when we unlock
|
|
|
|
* process lock. it's no longer valid to
|
|
|
|
* use it again in any other places.
|
|
|
|
*/
|
|
|
|
td->td_mailbox = NULL;
|
2002-09-29 02:48:37 +00:00
|
|
|
PROC_UNLOCK(p);
|
2002-09-27 07:11:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-09-29 02:48:37 +00:00
|
|
|
PROC_UNLOCK(p);
|
2002-09-27 07:11:11 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
td->td_usticks = 0;
|
2002-09-27 07:11:11 +00:00
|
|
|
return (0);
|
2002-11-18 01:59:31 +00:00
|
|
|
|
|
|
|
bad:
|
|
|
|
PROC_LOCK(p);
|
2003-07-17 22:45:33 +00:00
|
|
|
sigexit(td, SIGILL);
|
2002-12-28 01:23:07 +00:00
|
|
|
return (error);
|
2002-06-29 07:04:59 +00:00
|
|
|
}
|
|
|
|
|
2002-09-27 07:11:11 +00:00
|
|
|
/*
|
|
|
|
* Take the list of completed mailboxes for this KSEGRP and put them on this
|
2003-02-17 05:14:26 +00:00
|
|
|
* upcall's mailbox as it's the next one going up.
|
2002-09-27 07:11:11 +00:00
|
|
|
*/
|
|
|
|
static int
|
2003-02-17 05:14:26 +00:00
|
|
|
thread_link_mboxes(struct ksegrp *kg, struct kse_upcall *ku)
|
2002-09-27 07:11:11 +00:00
|
|
|
{
|
2002-09-29 02:48:37 +00:00
|
|
|
struct proc *p = kg->kg_proc;
|
2002-09-27 07:11:11 +00:00
|
|
|
void *addr;
|
|
|
|
uintptr_t mbx;
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
addr = (void *)(&ku->ku_mailbox->km_completed);
|
2002-09-27 07:11:11 +00:00
|
|
|
for (;;) {
|
|
|
|
mbx = (uintptr_t)kg->kg_completed;
|
|
|
|
if (suword(addr, mbx)) {
|
2002-09-29 02:48:37 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
psignal(p, SIGSEGV);
|
|
|
|
PROC_UNLOCK(p);
|
2002-09-27 07:11:11 +00:00
|
|
|
return (EFAULT);
|
|
|
|
}
|
2002-09-29 02:48:37 +00:00
|
|
|
PROC_LOCK(p);
|
2002-09-27 07:11:11 +00:00
|
|
|
if (mbx == (uintptr_t)kg->kg_completed) {
|
|
|
|
kg->kg_completed = NULL;
|
2002-09-29 02:48:37 +00:00
|
|
|
PROC_UNLOCK(p);
|
2002-09-27 07:11:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-09-29 02:48:37 +00:00
|
|
|
PROC_UNLOCK(p);
|
2002-09-27 07:11:11 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
2002-06-29 07:04:59 +00:00
|
|
|
|
2002-11-18 01:59:31 +00:00
|
|
|
/*
|
|
|
|
* This function should be called at statclock interrupt time
|
|
|
|
*/
|
|
|
|
int
|
2003-02-17 05:14:26 +00:00
|
|
|
thread_statclock(int user)
|
2002-11-18 01:59:31 +00:00
|
|
|
{
|
|
|
|
struct thread *td = curthread;
|
2003-06-15 12:51:26 +00:00
|
|
|
struct ksegrp *kg = td->td_ksegrp;
|
2004-01-10 18:34:01 +00:00
|
|
|
|
2004-06-02 07:52:36 +00:00
|
|
|
if (kg->kg_numupcalls == 0 || !(td->td_pflags & TDP_SA))
|
2003-06-15 12:51:26 +00:00
|
|
|
return (0);
|
2002-11-18 01:59:31 +00:00
|
|
|
if (user) {
|
|
|
|
/* Current always do via ast() */
|
2003-03-08 04:09:04 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2003-02-17 09:55:10 +00:00
|
|
|
td->td_flags |= (TDF_USTATCLOCK|TDF_ASTPENDING);
|
2003-03-08 04:09:04 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-02-17 05:14:26 +00:00
|
|
|
td->td_uuticks++;
|
2002-11-18 01:59:31 +00:00
|
|
|
} else {
|
|
|
|
if (td->td_mailbox != NULL)
|
2003-02-17 05:14:26 +00:00
|
|
|
td->td_usticks++;
|
|
|
|
else {
|
|
|
|
/* XXXKSE
|
|
|
|
* We will call thread_user_enter() for every
|
|
|
|
* kernel entry in future, so if the thread mailbox
|
|
|
|
* is NULL, it must be a UTS kernel, don't account
|
|
|
|
* clock ticks for it.
|
|
|
|
*/
|
|
|
|
}
|
2002-11-18 01:59:31 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
return (0);
|
2002-11-18 01:59:31 +00:00
|
|
|
}
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
2003-02-26 00:58:23 +00:00
|
|
|
* Export state clock ticks for userland
|
2003-02-17 05:14:26 +00:00
|
|
|
*/
|
2002-11-18 01:59:31 +00:00
|
|
|
static int
|
2003-02-26 00:58:23 +00:00
|
|
|
thread_update_usr_ticks(struct thread *td, int user)
|
2002-11-18 01:59:31 +00:00
|
|
|
{
|
|
|
|
struct proc *p = td->td_proc;
|
|
|
|
struct kse_thr_mailbox *tmbx;
|
2003-02-17 05:14:26 +00:00
|
|
|
struct kse_upcall *ku;
|
2003-03-19 05:49:38 +00:00
|
|
|
struct ksegrp *kg;
|
2002-11-18 01:59:31 +00:00
|
|
|
caddr_t addr;
|
2003-08-07 15:04:27 +00:00
|
|
|
u_int uticks;
|
2003-02-01 12:17:09 +00:00
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
if ((ku = td->td_upcall) == NULL)
|
|
|
|
return (-1);
|
2004-01-10 18:34:01 +00:00
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread);
|
2002-11-18 01:59:31 +00:00
|
|
|
if ((tmbx == NULL) || (tmbx == (void *)-1))
|
2003-02-17 05:14:26 +00:00
|
|
|
return (-1);
|
2003-02-26 00:58:23 +00:00
|
|
|
if (user) {
|
|
|
|
uticks = td->td_uuticks;
|
|
|
|
td->td_uuticks = 0;
|
2003-02-17 05:14:26 +00:00
|
|
|
addr = (caddr_t)&tmbx->tm_uticks;
|
2003-02-26 00:58:23 +00:00
|
|
|
} else {
|
|
|
|
uticks = td->td_usticks;
|
|
|
|
td->td_usticks = 0;
|
|
|
|
addr = (caddr_t)&tmbx->tm_sticks;
|
|
|
|
}
|
|
|
|
if (uticks) {
|
2003-06-28 19:45:15 +00:00
|
|
|
if (suword32(addr, uticks+fuword32(addr))) {
|
2003-02-17 05:14:26 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
psignal(p, SIGSEGV);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
return (-2);
|
|
|
|
}
|
2003-03-19 05:49:38 +00:00
|
|
|
}
|
|
|
|
kg = td->td_ksegrp;
|
|
|
|
if (kg->kg_upquantum && ticks >= kg->kg_nextupcall) {
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
td->td_upcall->ku_flags |= KUF_DOUPCALL;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2002-11-18 01:59:31 +00:00
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
return (0);
|
2002-11-18 01:59:31 +00:00
|
|
|
}
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
* This function is intended to be used to initialize a spare thread
|
|
|
|
* for upcall. Initialize thread's large data area outside sched_lock
|
|
|
|
* for thread_schedule_upcall().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
thread_alloc_spare(struct thread *td, struct thread *spare)
|
|
|
|
{
|
2004-03-13 22:31:39 +00:00
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
if (td->td_standin)
|
|
|
|
return;
|
2004-04-03 15:59:13 +00:00
|
|
|
if (spare == NULL) {
|
2003-02-17 05:14:26 +00:00
|
|
|
spare = thread_alloc();
|
2004-04-03 15:59:13 +00:00
|
|
|
spare->td_tid = thread_new_tid();
|
|
|
|
}
|
2003-02-17 05:14:26 +00:00
|
|
|
td->td_standin = spare;
|
|
|
|
bzero(&spare->td_startzero,
|
|
|
|
(unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
|
|
|
|
spare->td_proc = td->td_proc;
|
|
|
|
spare->td_ucred = crhold(td->td_ucred);
|
|
|
|
}
|
2002-10-24 08:46:34 +00:00
|
|
|
|
2002-06-29 07:04:59 +00:00
|
|
|
/*
|
2002-09-16 19:26:48 +00:00
|
|
|
* Create a thread and schedule it for upcall on the KSE given.
|
2002-12-28 01:23:07 +00:00
|
|
|
* Use our thread's standin so that we don't have to allocate one.
|
2002-06-29 07:04:59 +00:00
|
|
|
*/
|
|
|
|
struct thread *
|
2003-02-17 05:14:26 +00:00
|
|
|
thread_schedule_upcall(struct thread *td, struct kse_upcall *ku)
|
2002-06-29 07:04:59 +00:00
|
|
|
{
|
|
|
|
struct thread *td2;
|
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
2002-10-09 02:33:36 +00:00
|
|
|
|
2004-01-10 18:34:01 +00:00
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* Schedule an upcall thread on specified kse_upcall,
|
|
|
|
* the kse_upcall must be free.
|
|
|
|
* td must have a spare thread.
|
2002-10-09 02:33:36 +00:00
|
|
|
*/
|
2003-02-17 05:14:26 +00:00
|
|
|
KASSERT(ku->ku_owner == NULL, ("%s: upcall has owner", __func__));
|
2002-10-09 02:33:36 +00:00
|
|
|
if ((td2 = td->td_standin) != NULL) {
|
|
|
|
td->td_standin = NULL;
|
2002-06-29 07:04:59 +00:00
|
|
|
} else {
|
2003-02-17 05:14:26 +00:00
|
|
|
panic("no reserve thread when scheduling an upcall");
|
2002-10-30 03:01:28 +00:00
|
|
|
return (NULL);
|
2002-06-29 07:04:59 +00:00
|
|
|
}
|
|
|
|
CTR3(KTR_PROC, "thread_schedule_upcall: thread %p (pid %d, %s)",
|
2002-10-09 02:33:36 +00:00
|
|
|
td2, td->td_proc->p_pid, td->td_proc->p_comm);
|
2002-09-06 07:00:37 +00:00
|
|
|
bcopy(&td->td_startcopy, &td2->td_startcopy,
|
|
|
|
(unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
|
2003-02-17 05:14:26 +00:00
|
|
|
thread_link(td2, ku->ku_ksegrp);
|
2003-04-08 07:45:56 +00:00
|
|
|
/* inherit blocked thread's context */
|
2003-06-04 21:13:21 +00:00
|
|
|
cpu_set_upcall(td2, td);
|
2003-02-17 05:14:26 +00:00
|
|
|
/* Let the new thread become owner of the upcall */
|
|
|
|
ku->ku_owner = td2;
|
|
|
|
td2->td_upcall = ku;
|
2004-06-02 07:52:36 +00:00
|
|
|
td2->td_flags = 0;
|
|
|
|
td2->td_pflags = TDP_SA|TDP_UPCALLING;
|
2003-02-17 05:14:26 +00:00
|
|
|
td2->td_kse = NULL;
|
|
|
|
td2->td_state = TDS_CAN_RUN;
|
2002-10-09 02:33:36 +00:00
|
|
|
td2->td_inhibitors = 0;
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
SIGFILLSET(td2->td_sigmask);
|
|
|
|
SIG_CANTMASK(td2->td_sigmask);
|
2003-08-26 11:33:15 +00:00
|
|
|
sched_fork_thread(td, td2);
|
2002-10-09 02:33:36 +00:00
|
|
|
return (td2); /* bogus.. should be a void function */
|
2002-06-29 07:04:59 +00:00
|
|
|
}
|
|
|
|
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
/*
|
|
|
|
* It is only used when thread generated a trap and process is being
|
|
|
|
* debugged.
|
|
|
|
*/
|
2003-02-17 09:58:11 +00:00
|
|
|
void
|
|
|
|
thread_signal_add(struct thread *td, int sig)
|
2002-09-16 19:26:48 +00:00
|
|
|
{
|
2003-02-17 09:58:11 +00:00
|
|
|
struct proc *p;
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
siginfo_t siginfo;
|
|
|
|
struct sigacts *ps;
|
2002-09-16 19:26:48 +00:00
|
|
|
int error;
|
|
|
|
|
2003-06-06 02:17:38 +00:00
|
|
|
p = td->td_proc;
|
|
|
|
PROC_LOCK_ASSERT(p, MA_OWNED);
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
ps = p->p_sigacts;
|
|
|
|
mtx_assert(&ps->ps_mtx, MA_OWNED);
|
2003-02-17 09:58:11 +00:00
|
|
|
|
2003-07-15 04:26:26 +00:00
|
|
|
cpu_thread_siginfo(sig, 0, &siginfo);
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
mtx_unlock(&ps->ps_mtx);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
error = copyout(&siginfo, &td->td_mailbox->tm_syncsig, sizeof(siginfo));
|
|
|
|
if (error) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
sigexit(td, SIGILL);
|
|
|
|
}
|
2003-02-17 09:58:11 +00:00
|
|
|
PROC_LOCK(p);
|
o Change kse_thr_interrupt to allow send a signal to a specified thread,
or unblock a thread in kernel, and allow UTS to specify whether syscall
should be restarted.
o Add ability for UTS to monitor signal comes in and removed from process,
the flag PS_SIGEVENT is used to indicate the events.
o Add a KMF_WAITSIGEVENT for KSE mailbox flag, UTS call kse_release with
this flag set to wait for above signal event.
o For SA based thread, kernel masks all signal in its signal mask, let
UTS to use kse_thr_interrupt interrupt a thread, and install a signal
frame in userland for the thread.
o Add a tm_syncsig in thread mailbox, when a hardware trap occurs,
it is used to deliver synchronous signal to userland, and upcall
is schedule, so UTS can process the synchronous signal for the thread.
Reviewed by: julian (mentor)
2003-06-28 08:29:05 +00:00
|
|
|
SIGADDSET(td->td_sigmask, sig);
|
|
|
|
mtx_lock(&ps->ps_mtx);
|
2002-09-16 19:26:48 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 05:49:38 +00:00
|
|
|
void
|
|
|
|
thread_switchout(struct thread *td)
|
|
|
|
{
|
|
|
|
struct kse_upcall *ku;
|
2003-06-20 09:12:12 +00:00
|
|
|
struct thread *td2;
|
2003-03-19 05:49:38 +00:00
|
|
|
|
|
|
|
mtx_assert(&sched_lock, MA_OWNED);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the outgoing thread is in threaded group and has never
|
|
|
|
* scheduled an upcall, decide whether this is a short
|
|
|
|
* or long term event and thus whether or not to schedule
|
|
|
|
* an upcall.
|
|
|
|
* If it is a short term event, just suspend it in
|
|
|
|
* a way that takes its KSE with it.
|
|
|
|
* Select the events for which we want to schedule upcalls.
|
|
|
|
* For now it's just sleep.
|
|
|
|
* XXXKSE eventually almost any inhibition could do.
|
|
|
|
*/
|
|
|
|
if (TD_CAN_UNBIND(td) && (td->td_standin) && TD_ON_SLEEPQ(td)) {
|
2004-01-10 18:34:01 +00:00
|
|
|
/*
|
2003-03-19 05:49:38 +00:00
|
|
|
* Release ownership of upcall, and schedule an upcall
|
|
|
|
* thread, this new upcall thread becomes the owner of
|
|
|
|
* the upcall structure.
|
|
|
|
*/
|
|
|
|
ku = td->td_upcall;
|
|
|
|
ku->ku_owner = NULL;
|
2004-01-10 18:34:01 +00:00
|
|
|
td->td_upcall = NULL;
|
2003-03-19 05:49:38 +00:00
|
|
|
td->td_flags &= ~TDF_CAN_UNBIND;
|
2003-06-20 09:12:12 +00:00
|
|
|
td2 = thread_schedule_upcall(td, ku);
|
|
|
|
setrunqueue(td2);
|
2003-03-19 05:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-24 23:09:48 +00:00
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* Setup done on the thread when it enters the kernel.
|
2002-10-24 23:09:48 +00:00
|
|
|
* XXXKSE Presently only for syscalls but eventually all kernel entries.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
thread_user_enter(struct proc *p, struct thread *td)
|
|
|
|
{
|
2003-02-17 05:14:26 +00:00
|
|
|
struct ksegrp *kg;
|
|
|
|
struct kse_upcall *ku;
|
2003-04-21 07:27:59 +00:00
|
|
|
struct kse_thr_mailbox *tmbx;
|
2003-08-05 12:00:55 +00:00
|
|
|
uint32_t tflags;
|
2002-10-24 23:09:48 +00:00
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
kg = td->td_ksegrp;
|
2003-04-21 07:27:59 +00:00
|
|
|
|
2002-10-24 23:09:48 +00:00
|
|
|
/*
|
|
|
|
* First check that we shouldn't just abort.
|
|
|
|
* But check if we are the single thread first!
|
|
|
|
*/
|
2003-06-15 12:51:26 +00:00
|
|
|
if (p->p_flag & P_SINGLE_EXIT) {
|
|
|
|
PROC_LOCK(p);
|
2002-10-24 23:09:48 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2003-03-11 00:07:53 +00:00
|
|
|
thread_stopped(p);
|
2002-10-24 23:09:48 +00:00
|
|
|
thread_exit();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are doing a syscall in a KSE environment,
|
|
|
|
* note where our mailbox is. There is always the
|
2002-12-28 01:23:07 +00:00
|
|
|
* possibility that we could do this lazily (in kse_reassign()),
|
2002-10-24 23:09:48 +00:00
|
|
|
* but for now do it every time.
|
|
|
|
*/
|
2003-02-17 05:14:26 +00:00
|
|
|
kg = td->td_ksegrp;
|
2004-06-02 07:52:36 +00:00
|
|
|
if (td->td_pflags & TDP_SA) {
|
2003-02-17 05:14:26 +00:00
|
|
|
ku = td->td_upcall;
|
|
|
|
KASSERT(ku, ("%s: no upcall owned", __func__));
|
|
|
|
KASSERT((ku->ku_owner == td), ("%s: wrong owner", __func__));
|
2003-04-21 07:27:59 +00:00
|
|
|
KASSERT(!TD_CAN_UNBIND(td), ("%s: can unbind", __func__));
|
2003-06-28 19:45:15 +00:00
|
|
|
ku->ku_mflags = fuword32((void *)&ku->ku_mailbox->km_flags);
|
2003-04-21 07:27:59 +00:00
|
|
|
tmbx = (void *)fuword((void *)&ku->ku_mailbox->km_curthread);
|
2003-08-05 12:00:55 +00:00
|
|
|
if ((tmbx == NULL) || (tmbx == (void *)-1L) ||
|
|
|
|
(ku->ku_mflags & KMF_NOUPCALL)) {
|
2003-02-17 05:14:26 +00:00
|
|
|
td->td_mailbox = NULL;
|
2002-10-24 23:09:48 +00:00
|
|
|
} else {
|
2003-02-19 04:01:55 +00:00
|
|
|
if (td->td_standin == NULL)
|
|
|
|
thread_alloc_spare(td, NULL);
|
2003-08-08 20:23:10 +00:00
|
|
|
tflags = fuword32(&tmbx->tm_flags);
|
2003-08-05 12:00:55 +00:00
|
|
|
/*
|
|
|
|
* On some architectures, TP register points to thread
|
2004-01-10 18:34:01 +00:00
|
|
|
* mailbox but not points to kse mailbox, and userland
|
|
|
|
* can not atomically clear km_curthread, but can
|
2003-08-05 12:00:55 +00:00
|
|
|
* use TP register, and set TMF_NOUPCALL in thread
|
|
|
|
* flag to indicate a critical region.
|
|
|
|
*/
|
|
|
|
if (tflags & TMF_NOUPCALL) {
|
|
|
|
td->td_mailbox = NULL;
|
|
|
|
} else {
|
|
|
|
td->td_mailbox = tmbx;
|
|
|
|
mtx_lock_spin(&sched_lock);
|
2003-04-21 07:27:59 +00:00
|
|
|
td->td_flags |= TDF_CAN_UNBIND;
|
2003-08-05 12:00:55 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
}
|
2002-10-24 23:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-16 19:26:48 +00:00
|
|
|
/*
|
|
|
|
* The extra work we go through if we are a threaded process when we
|
|
|
|
* return to userland.
|
2002-06-29 07:04:59 +00:00
|
|
|
*
|
|
|
|
* If we are a KSE process and returning to user mode, check for
|
|
|
|
* extra work to do before we return (e.g. for more syscalls
|
|
|
|
* to complete first). If we were in a critical section, we should
|
|
|
|
* just return to let it finish. Same if we were in the UTS (in
|
2002-09-16 19:26:48 +00:00
|
|
|
* which case the mailbox's context's busy indicator will be set).
|
|
|
|
* The only traps we suport will have set the mailbox.
|
|
|
|
* We will clear it here.
|
2002-06-29 07:04:59 +00:00
|
|
|
*/
|
|
|
|
int
|
2002-09-23 06:14:30 +00:00
|
|
|
thread_userret(struct thread *td, struct trapframe *frame)
|
2002-06-29 07:04:59 +00:00
|
|
|
{
|
2003-04-21 07:27:59 +00:00
|
|
|
int error = 0, upcalls, uts_crit;
|
2003-02-17 05:14:26 +00:00
|
|
|
struct kse_upcall *ku;
|
2003-02-19 04:01:55 +00:00
|
|
|
struct ksegrp *kg, *kg2;
|
2002-10-09 02:33:36 +00:00
|
|
|
struct proc *p;
|
2002-11-18 12:28:15 +00:00
|
|
|
struct timespec ts;
|
2002-06-29 07:04:59 +00:00
|
|
|
|
2003-02-01 12:17:09 +00:00
|
|
|
p = td->td_proc;
|
2003-02-17 05:14:26 +00:00
|
|
|
kg = td->td_ksegrp;
|
2003-06-15 12:51:26 +00:00
|
|
|
ku = td->td_upcall;
|
2002-12-28 01:23:07 +00:00
|
|
|
|
2003-06-15 12:51:26 +00:00
|
|
|
/* Nothing to do with bound thread */
|
2004-06-02 07:52:36 +00:00
|
|
|
if (!(td->td_pflags & TDP_SA))
|
2003-02-17 05:14:26 +00:00
|
|
|
return (0);
|
2002-10-09 02:33:36 +00:00
|
|
|
|
2002-09-16 19:26:48 +00:00
|
|
|
/*
|
2004-01-10 18:34:01 +00:00
|
|
|
* Stat clock interrupt hit in userland, it
|
2003-02-17 05:14:26 +00:00
|
|
|
* is returning from interrupt, charge thread's
|
|
|
|
* userland time for UTS.
|
2002-09-16 19:26:48 +00:00
|
|
|
*/
|
2003-02-17 05:14:26 +00:00
|
|
|
if (td->td_flags & TDF_USTATCLOCK) {
|
2003-02-26 00:58:23 +00:00
|
|
|
thread_update_usr_ticks(td, 1);
|
2003-02-17 05:14:26 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
td->td_flags &= ~TDF_USTATCLOCK;
|
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-06-15 12:51:26 +00:00
|
|
|
if (kg->kg_completed ||
|
2003-02-26 00:58:23 +00:00
|
|
|
(td->td_upcall->ku_flags & KUF_DOUPCALL))
|
|
|
|
thread_user_enter(p, td);
|
2003-02-17 05:14:26 +00:00
|
|
|
}
|
2002-12-28 01:23:07 +00:00
|
|
|
|
2003-04-21 07:27:59 +00:00
|
|
|
uts_crit = (td->td_mailbox == NULL);
|
2004-01-10 18:34:01 +00:00
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* Optimisation:
|
|
|
|
* This thread has not started any upcall.
|
|
|
|
* If there is no work to report other than ourself,
|
|
|
|
* then it can return direct to userland.
|
|
|
|
*/
|
2002-12-28 01:23:07 +00:00
|
|
|
if (TD_CAN_UNBIND(td)) {
|
2003-02-17 05:14:26 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
td->td_flags &= ~TDF_CAN_UNBIND;
|
2003-03-31 22:49:17 +00:00
|
|
|
if ((td->td_flags & TDF_NEEDSIGCHK) == 0 &&
|
2003-03-11 02:59:50 +00:00
|
|
|
(kg->kg_completed == NULL) &&
|
2003-03-19 05:49:38 +00:00
|
|
|
(ku->ku_flags & KUF_DOUPCALL) == 0 &&
|
2003-04-19 06:16:04 +00:00
|
|
|
(kg->kg_upquantum && ticks < kg->kg_nextupcall)) {
|
2003-03-31 22:49:17 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-02-26 00:58:23 +00:00
|
|
|
thread_update_usr_ticks(td, 0);
|
2003-03-14 03:52:16 +00:00
|
|
|
nanotime(&ts);
|
2003-03-19 05:49:38 +00:00
|
|
|
error = copyout(&ts,
|
2003-03-14 03:52:16 +00:00
|
|
|
(caddr_t)&ku->ku_mailbox->km_timeofday,
|
|
|
|
sizeof(ts));
|
2003-03-11 02:59:50 +00:00
|
|
|
td->td_mailbox = 0;
|
2003-04-21 07:27:59 +00:00
|
|
|
ku->ku_mflags = 0;
|
2003-03-14 03:52:16 +00:00
|
|
|
if (error)
|
|
|
|
goto out;
|
2003-03-11 02:59:50 +00:00
|
|
|
return (0);
|
2002-12-28 01:23:07 +00:00
|
|
|
}
|
2003-03-31 22:49:17 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-07-17 22:45:33 +00:00
|
|
|
thread_export_context(td, 0);
|
2002-09-27 07:11:11 +00:00
|
|
|
/*
|
2003-02-17 05:14:26 +00:00
|
|
|
* There is something to report, and we own an upcall
|
|
|
|
* strucuture, we can go to userland.
|
|
|
|
* Turn ourself into an upcall thread.
|
2002-09-27 07:11:11 +00:00
|
|
|
*/
|
2003-06-15 03:18:58 +00:00
|
|
|
td->td_pflags |= TDP_UPCALLING;
|
2003-04-21 07:27:59 +00:00
|
|
|
} else if (td->td_mailbox && (ku == NULL)) {
|
2003-07-17 22:45:33 +00:00
|
|
|
thread_export_context(td, 1);
|
2003-03-11 00:07:53 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
/*
|
|
|
|
* There are upcall threads waiting for
|
|
|
|
* work to do, wake one of them up.
|
2004-01-10 18:34:01 +00:00
|
|
|
* XXXKSE Maybe wake all of them up.
|
2003-03-11 00:07:53 +00:00
|
|
|
*/
|
2003-07-17 22:45:33 +00:00
|
|
|
if (kg->kg_upsleeps)
|
2003-03-11 00:07:53 +00:00
|
|
|
wakeup_one(&kg->kg_completed);
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
thread_stopped(p);
|
2002-12-28 01:23:07 +00:00
|
|
|
thread_exit();
|
2003-02-17 05:14:26 +00:00
|
|
|
/* NOTREACHED */
|
2002-10-09 02:33:36 +00:00
|
|
|
}
|
|
|
|
|
2003-06-15 12:51:26 +00:00
|
|
|
KASSERT(ku != NULL, ("upcall is NULL\n"));
|
2003-02-20 01:11:17 +00:00
|
|
|
KASSERT(TD_CAN_UNBIND(td) == 0, ("can unbind"));
|
|
|
|
|
|
|
|
if (p->p_numthreads > max_threads_per_proc) {
|
|
|
|
max_threads_hits++;
|
|
|
|
PROC_LOCK(p);
|
2003-04-23 18:46:51 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
2003-06-11 01:08:33 +00:00
|
|
|
p->p_maxthrwaits++;
|
2003-02-20 01:11:17 +00:00
|
|
|
while (p->p_numthreads > max_threads_per_proc) {
|
|
|
|
upcalls = 0;
|
|
|
|
FOREACH_KSEGRP_IN_PROC(p, kg2) {
|
|
|
|
if (kg2->kg_numupcalls == 0)
|
|
|
|
upcalls++;
|
|
|
|
else
|
|
|
|
upcalls += kg2->kg_numupcalls;
|
|
|
|
}
|
|
|
|
if (upcalls >= max_threads_per_proc)
|
|
|
|
break;
|
2003-04-27 04:32:40 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-06-10 02:21:32 +00:00
|
|
|
if (msleep(&p->p_numthreads, &p->p_mtx, PPAUSE|PCATCH,
|
2003-12-23 02:28:42 +00:00
|
|
|
"maxthreads", 0)) {
|
2003-06-11 01:08:33 +00:00
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
mtx_lock_spin(&sched_lock);
|
|
|
|
}
|
2003-02-20 01:11:17 +00:00
|
|
|
}
|
2003-06-11 01:08:33 +00:00
|
|
|
p->p_maxthrwaits--;
|
2003-04-23 18:46:51 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
2003-02-20 01:11:17 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
|
|
|
|
2003-06-15 03:18:58 +00:00
|
|
|
if (td->td_pflags & TDP_UPCALLING) {
|
2003-04-21 07:27:59 +00:00
|
|
|
uts_crit = 0;
|
2003-03-19 05:49:38 +00:00
|
|
|
kg->kg_nextupcall = ticks+kg->kg_upquantum;
|
2004-01-10 18:34:01 +00:00
|
|
|
/*
|
2002-12-28 01:23:07 +00:00
|
|
|
* There is no more work to do and we are going to ride
|
2003-02-17 05:14:26 +00:00
|
|
|
* this thread up to userland as an upcall.
|
2002-12-28 01:23:07 +00:00
|
|
|
* Do the last parts of the setup needed for the upcall.
|
|
|
|
*/
|
|
|
|
CTR3(KTR_PROC, "userret: upcall thread %p (pid %d, %s)",
|
|
|
|
td, td->td_proc->p_pid, td->td_proc->p_comm);
|
2002-10-09 02:33:36 +00:00
|
|
|
|
2003-06-15 03:18:58 +00:00
|
|
|
td->td_pflags &= ~TDP_UPCALLING;
|
2003-06-15 12:51:26 +00:00
|
|
|
if (ku->ku_flags & KUF_DOUPCALL) {
|
|
|
|
mtx_lock_spin(&sched_lock);
|
2003-02-17 05:14:26 +00:00
|
|
|
ku->ku_flags &= ~KUF_DOUPCALL;
|
2003-06-15 12:51:26 +00:00
|
|
|
mtx_unlock_spin(&sched_lock);
|
|
|
|
}
|
2003-04-21 07:27:59 +00:00
|
|
|
/*
|
|
|
|
* Set user context to the UTS
|
|
|
|
*/
|
|
|
|
if (!(ku->ku_mflags & KMF_NOUPCALL)) {
|
|
|
|
cpu_set_upcall_kse(td, ku);
|
|
|
|
error = suword(&ku->ku_mailbox->km_curthread, 0);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
2002-12-28 01:23:07 +00:00
|
|
|
* Unhook the list of completed threads.
|
2004-01-10 18:34:01 +00:00
|
|
|
* anything that completes after this gets to
|
2002-12-28 01:23:07 +00:00
|
|
|
* come in next time.
|
|
|
|
* Put the list of completed thread mailboxes on
|
|
|
|
* this KSE's mailbox.
|
|
|
|
*/
|
2003-04-21 07:27:59 +00:00
|
|
|
if (!(ku->ku_mflags & KMF_NOCOMPLETED) &&
|
|
|
|
(error = thread_link_mboxes(kg, ku)) != 0)
|
2003-02-19 04:01:55 +00:00
|
|
|
goto out;
|
2003-04-21 07:27:59 +00:00
|
|
|
}
|
|
|
|
if (!uts_crit) {
|
2002-11-18 12:28:15 +00:00
|
|
|
nanotime(&ts);
|
2003-04-21 07:27:59 +00:00
|
|
|
error = copyout(&ts, &ku->ku_mailbox->km_timeofday, sizeof(ts));
|
2003-02-19 04:01:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (error) {
|
|
|
|
/*
|
2003-02-19 13:40:24 +00:00
|
|
|
* Things are going to be so screwed we should just kill
|
|
|
|
* the process.
|
2003-02-19 04:01:55 +00:00
|
|
|
* how do we do that?
|
|
|
|
*/
|
|
|
|
PROC_LOCK(td->td_proc);
|
|
|
|
psignal(td->td_proc, SIGSEGV);
|
|
|
|
PROC_UNLOCK(td->td_proc);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Optimisation:
|
|
|
|
* Ensure that we have a spare thread available,
|
|
|
|
* for when we re-enter the kernel.
|
|
|
|
*/
|
|
|
|
if (td->td_standin == NULL)
|
|
|
|
thread_alloc_spare(td, NULL);
|
2002-11-18 12:28:15 +00:00
|
|
|
}
|
2002-12-28 01:23:07 +00:00
|
|
|
|
2003-04-21 07:27:59 +00:00
|
|
|
ku->ku_mflags = 0;
|
2003-02-17 05:14:26 +00:00
|
|
|
/*
|
|
|
|
* Clear thread mailbox first, then clear system tick count.
|
2004-01-10 18:34:01 +00:00
|
|
|
* The order is important because thread_statclock() use
|
2003-02-17 05:14:26 +00:00
|
|
|
* mailbox pointer to see if it is an userland thread or
|
|
|
|
* an UTS kernel thread.
|
|
|
|
*/
|
2002-12-28 01:23:07 +00:00
|
|
|
td->td_mailbox = NULL;
|
2003-02-17 05:14:26 +00:00
|
|
|
td->td_usticks = 0;
|
2002-10-09 02:33:36 +00:00
|
|
|
return (error); /* go sync */
|
2002-06-29 07:04:59 +00:00
|
|
|
}
|
|
|
|
|
2004-04-28 20:36:53 +00:00
|
|
|
int
|
|
|
|
thread_upcall_check(struct thread *td)
|
|
|
|
{
|
|
|
|
PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
|
|
|
|
if (td->td_kflags & TDK_WAKEUP)
|
|
|
|
return (1);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|