xref: /freebsd/sys/kern/kern_thr.c (revision cd28f17da2f89e4e3f6dc3874912ac7e1947cfc0)
189bb1cefSJeff Roberson /*
289bb1cefSJeff Roberson  * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
389bb1cefSJeff Roberson  * All rights reserved.
489bb1cefSJeff Roberson  *
589bb1cefSJeff Roberson  * Redistribution and use in source and binary forms, with or without
689bb1cefSJeff Roberson  * modification, are permitted provided that the following conditions
789bb1cefSJeff Roberson  * are met:
889bb1cefSJeff Roberson  * 1. Redistributions of source code must retain the above copyright
989bb1cefSJeff Roberson  *    notice unmodified, this list of conditions, and the following
1089bb1cefSJeff Roberson  *    disclaimer.
1189bb1cefSJeff Roberson  * 2. Redistributions in binary form must reproduce the above copyright
1289bb1cefSJeff Roberson  *    notice, this list of conditions and the following disclaimer in the
1389bb1cefSJeff Roberson  *    documentation and/or other materials provided with the distribution.
1489bb1cefSJeff Roberson  *
1589bb1cefSJeff Roberson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1689bb1cefSJeff Roberson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1789bb1cefSJeff Roberson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1889bb1cefSJeff Roberson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1989bb1cefSJeff Roberson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2089bb1cefSJeff Roberson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2189bb1cefSJeff Roberson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2289bb1cefSJeff Roberson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2389bb1cefSJeff Roberson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2489bb1cefSJeff Roberson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2589bb1cefSJeff Roberson  */
2689bb1cefSJeff Roberson 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29677b542eSDavid E. O'Brien 
3089bb1cefSJeff Roberson #include <sys/param.h>
3189bb1cefSJeff Roberson #include <sys/kernel.h>
3289bb1cefSJeff Roberson #include <sys/lock.h>
3389bb1cefSJeff Roberson #include <sys/mutex.h>
3489bb1cefSJeff Roberson #include <sys/proc.h>
3589bb1cefSJeff Roberson #include <sys/resourcevar.h>
36a22ec9d8SJeff Roberson #include <sys/sched.h>
3789bb1cefSJeff Roberson #include <sys/sysent.h>
3889bb1cefSJeff Roberson #include <sys/systm.h>
3989bb1cefSJeff Roberson #include <sys/sysproto.h>
4089bb1cefSJeff Roberson #include <sys/signalvar.h>
4189bb1cefSJeff Roberson #include <sys/ucontext.h>
4289bb1cefSJeff Roberson #include <sys/thr.h>
4389bb1cefSJeff Roberson 
4489bb1cefSJeff Roberson #include <machine/frame.h>
4589bb1cefSJeff Roberson 
4689bb1cefSJeff Roberson /*
4789bb1cefSJeff Roberson  * Back end support functions.
4889bb1cefSJeff Roberson  */
4989bb1cefSJeff Roberson 
5089bb1cefSJeff Roberson void
5189bb1cefSJeff Roberson thr_exit1(void)
5289bb1cefSJeff Roberson {
5389bb1cefSJeff Roberson 	struct ksegrp *kg;
5489bb1cefSJeff Roberson 	struct thread *td;
5589bb1cefSJeff Roberson 	struct kse *ke;
5689bb1cefSJeff Roberson 	struct proc *p;
5789bb1cefSJeff Roberson 
5889bb1cefSJeff Roberson 	td = curthread;
5989bb1cefSJeff Roberson 	p = td->td_proc;
6089bb1cefSJeff Roberson 	kg = td->td_ksegrp;
6189bb1cefSJeff Roberson 	ke = td->td_kse;
6289bb1cefSJeff Roberson 
6389bb1cefSJeff Roberson 	mtx_assert(&sched_lock, MA_OWNED);
6489bb1cefSJeff Roberson 	PROC_LOCK_ASSERT(p, MA_OWNED);
6589bb1cefSJeff Roberson 	KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
6689bb1cefSJeff Roberson 
6789bb1cefSJeff Roberson 	/*
6889bb1cefSJeff Roberson 	 * Shutting down last thread in the proc.  This will actually
6989bb1cefSJeff Roberson 	 * call exit() in the trampoline when it returns.
7089bb1cefSJeff Roberson 	 */
7189bb1cefSJeff Roberson 	if (p->p_numthreads == 1) {
7289bb1cefSJeff Roberson 		PROC_UNLOCK(p);
7389bb1cefSJeff Roberson 		return;
7489bb1cefSJeff Roberson 	}
7589bb1cefSJeff Roberson 
7689bb1cefSJeff Roberson 	/*
7789bb1cefSJeff Roberson 	 * XXX Undelivered process wide signals should be reposted to the
7889bb1cefSJeff Roberson 	 * proc.
7989bb1cefSJeff Roberson 	 */
8089bb1cefSJeff Roberson 
8189bb1cefSJeff Roberson 	/* Clean up cpu resources. */
8289bb1cefSJeff Roberson 	cpu_thread_exit(td);
8389bb1cefSJeff Roberson 
84c5af6006SKris Kennaway 	/* Unlink the thread from the process and kseg. */
854f73277aSJulian Elischer 	thread_unlink(td);
8689bb1cefSJeff Roberson 
8789bb1cefSJeff Roberson 	ke->ke_state = KES_UNQUEUED;
8889bb1cefSJeff Roberson 	ke->ke_thread = NULL;
8989bb1cefSJeff Roberson 	kse_unlink(ke);
90a22ec9d8SJeff Roberson 	sched_exit_kse(TAILQ_NEXT(ke, ke_kglist), ke);
9189bb1cefSJeff Roberson 
9289bb1cefSJeff Roberson 	/*
9389bb1cefSJeff Roberson 	 * If we were stopped while waiting for all threads to exit and this
9489bb1cefSJeff Roberson 	 * is the last thread wakeup the exiting thread.
9589bb1cefSJeff Roberson 	 */
9689bb1cefSJeff Roberson 	if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE)
9789bb1cefSJeff Roberson 		if (p->p_numthreads == 1)
9889bb1cefSJeff Roberson 			thread_unsuspend_one(p->p_singlethread);
9989bb1cefSJeff Roberson 
10089bb1cefSJeff Roberson 	PROC_UNLOCK(p);
10189bb1cefSJeff Roberson 	td->td_kse = NULL;
10289bb1cefSJeff Roberson 	td->td_state = TDS_INACTIVE;
103a12efae1SJake Burkholder #if 0
10489bb1cefSJeff Roberson 	td->td_proc = NULL;
105a12efae1SJake Burkholder #endif
10689bb1cefSJeff Roberson 	td->td_ksegrp = NULL;
10789bb1cefSJeff Roberson 	td->td_last_kse = NULL;
108a22ec9d8SJeff Roberson 	sched_exit_thread(TAILQ_NEXT(td, td_kglist), td);
10989bb1cefSJeff Roberson 	thread_stash(td);
11089bb1cefSJeff Roberson 
111cc66ebe2SPeter Wemm 	cpu_throw(td, choosethread());
11289bb1cefSJeff Roberson }
11389bb1cefSJeff Roberson 
11489bb1cefSJeff Roberson #define	RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
11589bb1cefSJeff Roberson 
11689bb1cefSJeff Roberson /*
11789bb1cefSJeff Roberson  * System call interface.
11889bb1cefSJeff Roberson  */
11989bb1cefSJeff Roberson int
12089bb1cefSJeff Roberson thr_create(struct thread *td, struct thr_create_args *uap)
121cd28f17dSMarcel Moolenaar     /* ucontext_t *ctx, long *id, int flags */
12289bb1cefSJeff Roberson {
12389bb1cefSJeff Roberson 	struct kse *ke0;
12489bb1cefSJeff Roberson 	struct thread *td0;
12589bb1cefSJeff Roberson 	ucontext_t ctx;
126cd28f17dSMarcel Moolenaar 	long id;
12789bb1cefSJeff Roberson 	int error;
12889bb1cefSJeff Roberson 
12989bb1cefSJeff Roberson 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
13089bb1cefSJeff Roberson 		return (error);
13189bb1cefSJeff Roberson 
13289bb1cefSJeff Roberson 	/* Initialize our td. */
13389bb1cefSJeff Roberson 	td0 = thread_alloc();
13489bb1cefSJeff Roberson 
13589bb1cefSJeff Roberson 	/*
13689bb1cefSJeff Roberson 	 * Try the copyout as soon as we allocate the td so we don't have to
13789bb1cefSJeff Roberson 	 * tear things down in a failure case below.
13889bb1cefSJeff Roberson 	 */
139cd28f17dSMarcel Moolenaar 	id = td0->td_tid;
140cd28f17dSMarcel Moolenaar 	if ((error = copyout(&id, uap->id, sizeof(long)))) {
14189bb1cefSJeff Roberson 		thread_free(td0);
14289bb1cefSJeff Roberson 		return (error);
14389bb1cefSJeff Roberson 	}
14489bb1cefSJeff Roberson 
14589bb1cefSJeff Roberson 	bzero(&td0->td_startzero,
14689bb1cefSJeff Roberson 	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
14789bb1cefSJeff Roberson 	bcopy(&td->td_startcopy, &td0->td_startcopy,
14889bb1cefSJeff Roberson 	    (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
14989bb1cefSJeff Roberson 
15089bb1cefSJeff Roberson 	td0->td_proc = td->td_proc;
15194df4b85SJohn Baldwin 	PROC_LOCK(td->td_proc);
15289bb1cefSJeff Roberson 	td0->td_sigmask = td->td_sigmask;
15394df4b85SJohn Baldwin 	PROC_UNLOCK(td->td_proc);
15489bb1cefSJeff Roberson 	td0->td_ucred = crhold(td->td_ucred);
15589bb1cefSJeff Roberson 
15689bb1cefSJeff Roberson 	/* Initialize our kse structure. */
15789bb1cefSJeff Roberson 	ke0 = kse_alloc();
15889bb1cefSJeff Roberson 	bzero(&ke0->ke_startzero,
15989bb1cefSJeff Roberson 	    RANGEOF(struct kse, ke_startzero, ke_endzero));
16089bb1cefSJeff Roberson 
16189bb1cefSJeff Roberson 	/* Set up our machine context. */
16211e0f8e1SMarcel Moolenaar 	cpu_set_upcall(td0, td);
16389bb1cefSJeff Roberson 	error = set_mcontext(td0, &ctx.uc_mcontext);
16489bb1cefSJeff Roberson 	if (error != 0) {
16589bb1cefSJeff Roberson 		kse_free(ke0);
16689bb1cefSJeff Roberson 		thread_free(td0);
16789bb1cefSJeff Roberson 		goto out;
16889bb1cefSJeff Roberson 	}
16989bb1cefSJeff Roberson 
17089bb1cefSJeff Roberson 	/* Link the thread and kse into the ksegrp and make it runnable. */
17189bb1cefSJeff Roberson 	mtx_lock_spin(&sched_lock);
17289bb1cefSJeff Roberson 
17389bb1cefSJeff Roberson 	thread_link(td0, td->td_ksegrp);
17489bb1cefSJeff Roberson 	kse_link(ke0, td->td_ksegrp);
17589bb1cefSJeff Roberson 
17689bb1cefSJeff Roberson 	/* Bind this thread and kse together. */
17789bb1cefSJeff Roberson 	td0->td_kse = ke0;
17889bb1cefSJeff Roberson 	ke0->ke_thread = td0;
17989bb1cefSJeff Roberson 
180a22ec9d8SJeff Roberson 	sched_fork_kse(td->td_kse, ke0);
181a22ec9d8SJeff Roberson 	sched_fork_thread(td, td0);
182a22ec9d8SJeff Roberson 
18389bb1cefSJeff Roberson 	TD_SET_CAN_RUN(td0);
18489bb1cefSJeff Roberson 	if ((uap->flags & THR_SUSPENDED) == 0)
18589bb1cefSJeff Roberson 		setrunqueue(td0);
18689bb1cefSJeff Roberson 
18789bb1cefSJeff Roberson 	mtx_unlock_spin(&sched_lock);
18889bb1cefSJeff Roberson 
18989bb1cefSJeff Roberson out:
19089bb1cefSJeff Roberson 	return (error);
19189bb1cefSJeff Roberson }
19289bb1cefSJeff Roberson 
19389bb1cefSJeff Roberson int
19489bb1cefSJeff Roberson thr_self(struct thread *td, struct thr_self_args *uap)
195cd28f17dSMarcel Moolenaar     /* long *id */
19689bb1cefSJeff Roberson {
197cd28f17dSMarcel Moolenaar 	long id;
19889bb1cefSJeff Roberson 	int error;
19989bb1cefSJeff Roberson 
200cd28f17dSMarcel Moolenaar 	id = td->td_tid;
201cd28f17dSMarcel Moolenaar 	if ((error = copyout(&id, uap->id, sizeof(long))))
20289bb1cefSJeff Roberson 		return (error);
20389bb1cefSJeff Roberson 
20489bb1cefSJeff Roberson 	return (0);
20589bb1cefSJeff Roberson }
20689bb1cefSJeff Roberson 
20789bb1cefSJeff Roberson int
20889bb1cefSJeff Roberson thr_exit(struct thread *td, struct thr_exit_args *uap)
20989bb1cefSJeff Roberson     /* NULL */
21089bb1cefSJeff Roberson {
21189bb1cefSJeff Roberson 	struct proc *p;
21289bb1cefSJeff Roberson 
21389bb1cefSJeff Roberson 	p = td->td_proc;
21489bb1cefSJeff Roberson 
21589bb1cefSJeff Roberson 	PROC_LOCK(p);
21689bb1cefSJeff Roberson 	mtx_lock_spin(&sched_lock);
21789bb1cefSJeff Roberson 
21889bb1cefSJeff Roberson 	/*
21989bb1cefSJeff Roberson 	 * This unlocks proc and doesn't return unless this is the last
22089bb1cefSJeff Roberson 	 * thread.
22189bb1cefSJeff Roberson 	 */
22289bb1cefSJeff Roberson 	thr_exit1();
22389bb1cefSJeff Roberson 	mtx_unlock_spin(&sched_lock);
22489bb1cefSJeff Roberson 
22589bb1cefSJeff Roberson 	return (0);
22689bb1cefSJeff Roberson }
22789bb1cefSJeff Roberson 
22889bb1cefSJeff Roberson int
22989bb1cefSJeff Roberson thr_kill(struct thread *td, struct thr_kill_args *uap)
230cd28f17dSMarcel Moolenaar     /* long id, int sig */
23189bb1cefSJeff Roberson {
23289bb1cefSJeff Roberson 	struct thread *ttd;
23389bb1cefSJeff Roberson 	struct proc *p;
23489bb1cefSJeff Roberson 	int error;
23589bb1cefSJeff Roberson 
23689bb1cefSJeff Roberson 	p = td->td_proc;
23789bb1cefSJeff Roberson 	error = 0;
23889bb1cefSJeff Roberson 	PROC_LOCK(p);
23971cfaac0SMike Makonnen 	FOREACH_THREAD_IN_PROC(p, ttd) {
240cd28f17dSMarcel Moolenaar 		if (ttd->td_tid == uap->id)
24189bb1cefSJeff Roberson 			break;
24271cfaac0SMike Makonnen 	}
24389bb1cefSJeff Roberson 	if (ttd == NULL) {
24489bb1cefSJeff Roberson 		error = ESRCH;
24589bb1cefSJeff Roberson 		goto out;
24689bb1cefSJeff Roberson 	}
24789bb1cefSJeff Roberson 	if (uap->sig == 0)
24889bb1cefSJeff Roberson 		goto out;
24989bb1cefSJeff Roberson 	if (!_SIG_VALID(uap->sig)) {
25089bb1cefSJeff Roberson 		error = EINVAL;
25189bb1cefSJeff Roberson 		goto out;
25289bb1cefSJeff Roberson 	}
253c197abc4SMike Makonnen 	tdsignal(ttd, uap->sig, SIGTARGET_TD);
25489bb1cefSJeff Roberson out:
25589bb1cefSJeff Roberson 	PROC_UNLOCK(p);
25689bb1cefSJeff Roberson 	return (error);
25789bb1cefSJeff Roberson }
2581713a516SMike Makonnen 
2591713a516SMike Makonnen int
2601713a516SMike Makonnen thr_suspend(struct thread *td, struct thr_suspend_args *uap)
2611713a516SMike Makonnen 	/* const struct timespec *timeout */
2621713a516SMike Makonnen {
2631713a516SMike Makonnen 	struct timespec ts;
2641713a516SMike Makonnen 	struct timeval	tv;
2651713a516SMike Makonnen 	int error;
2661713a516SMike Makonnen 	int hz;
2671713a516SMike Makonnen 
2681713a516SMike Makonnen 	hz = 0;
2691713a516SMike Makonnen 	error = 0;
2701713a516SMike Makonnen 	if (uap->timeout != NULL) {
2711713a516SMike Makonnen 		error = copyin((const void *)uap->timeout, (void *)&ts,
2721713a516SMike Makonnen 		    sizeof(struct timespec));
2731713a516SMike Makonnen 		if (error != 0)
2741713a516SMike Makonnen 			return (error);
2751713a516SMike Makonnen 		if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
2761713a516SMike Makonnen 			return (EINVAL);
2771713a516SMike Makonnen 		if (ts.tv_sec == 0 && ts.tv_nsec == 0)
2781713a516SMike Makonnen 			return (ETIMEDOUT);
2791713a516SMike Makonnen 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
2801713a516SMike Makonnen 		hz = tvtohz(&tv);
2811713a516SMike Makonnen 	}
2821713a516SMike Makonnen 	PROC_LOCK(td->td_proc);
2831713a516SMike Makonnen 	mtx_lock_spin(&sched_lock);
2841713a516SMike Makonnen 	if ((td->td_flags & TDF_THRWAKEUP) == 0) {
2851713a516SMike Makonnen 		mtx_unlock_spin(&sched_lock);
2861713a516SMike Makonnen 		error = msleep((void *)td, &td->td_proc->p_mtx,
2871713a516SMike Makonnen 		    td->td_priority | PCATCH, "lthr", hz);
2881713a516SMike Makonnen 		mtx_lock_spin(&sched_lock);
2891713a516SMike Makonnen 	}
2901713a516SMike Makonnen 	td->td_flags &= ~TDF_THRWAKEUP;
2911713a516SMike Makonnen 	mtx_unlock_spin(&sched_lock);
2921713a516SMike Makonnen 	PROC_UNLOCK(td->td_proc);
2931713a516SMike Makonnen 	return (error == EWOULDBLOCK ? ETIMEDOUT : error);
2941713a516SMike Makonnen }
2951713a516SMike Makonnen 
2961713a516SMike Makonnen int
2971713a516SMike Makonnen thr_wake(struct thread *td, struct thr_wake_args *uap)
298cd28f17dSMarcel Moolenaar 	/* long id */
2991713a516SMike Makonnen {
300cd28f17dSMarcel Moolenaar 	struct thread *ttd;
3011713a516SMike Makonnen 
302b9fb5d42SMike Makonnen 	PROC_LOCK(td->td_proc);
303b9fb5d42SMike Makonnen 	FOREACH_THREAD_IN_PROC(td->td_proc, ttd) {
304cd28f17dSMarcel Moolenaar 		if (ttd->td_tid == uap->id)
3051713a516SMike Makonnen 			break;
3061713a516SMike Makonnen 	}
3071713a516SMike Makonnen 	if (ttd == NULL) {
308b9fb5d42SMike Makonnen 		PROC_UNLOCK(td->td_proc);
3091713a516SMike Makonnen 		return (ESRCH);
3101713a516SMike Makonnen 	}
3111713a516SMike Makonnen 	mtx_lock_spin(&sched_lock);
312cd28f17dSMarcel Moolenaar 	ttd->td_flags |= TDF_THRWAKEUP;
3131713a516SMike Makonnen 	mtx_unlock_spin(&sched_lock);
314cd28f17dSMarcel Moolenaar 	wakeup_one((void *)ttd);
315b9fb5d42SMike Makonnen 	PROC_UNLOCK(td->td_proc);
3161713a516SMike Makonnen 	return (0);
3171713a516SMike Makonnen }
318