kern_thr.c revision 127739
1/*
2 * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/kern_thr.c 127739 2004-04-02 04:57:40Z kris $");
29
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/lock.h>
33#include <sys/mutex.h>
34#include <sys/proc.h>
35#include <sys/resourcevar.h>
36#include <sys/sched.h>
37#include <sys/sysent.h>
38#include <sys/systm.h>
39#include <sys/sysproto.h>
40#include <sys/signalvar.h>
41#include <sys/ucontext.h>
42#include <sys/thr.h>
43
44#include <machine/frame.h>
45
46/*
47 * Back end support functions.
48 */
49
50void
51thr_exit1(void)
52{
53	struct ksegrp *kg;
54	struct thread *td;
55	struct kse *ke;
56	struct proc *p;
57
58	td = curthread;
59	p = td->td_proc;
60	kg = td->td_ksegrp;
61	ke = td->td_kse;
62
63	mtx_assert(&sched_lock, MA_OWNED);
64	PROC_LOCK_ASSERT(p, MA_OWNED);
65	KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
66
67	/*
68	 * Shutting down last thread in the proc.  This will actually
69	 * call exit() in the trampoline when it returns.
70	 */
71	if (p->p_numthreads == 1) {
72		PROC_UNLOCK(p);
73		return;
74	}
75
76	/*
77	 * XXX Undelivered process wide signals should be reposted to the
78	 * proc.
79	 */
80
81	/* Clean up cpu resources. */
82	cpu_thread_exit(td);
83
84	/* Unlink the thread from the process and kseg. */
85	thread_unlink(td);
86
87	ke->ke_state = KES_UNQUEUED;
88	ke->ke_thread = NULL;
89	kse_unlink(ke);
90	sched_exit_kse(TAILQ_NEXT(ke, ke_kglist), ke);
91
92	/*
93	 * If we were stopped while waiting for all threads to exit and this
94	 * is the last thread wakeup the exiting thread.
95	 */
96	if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE)
97		if (p->p_numthreads == 1)
98			thread_unsuspend_one(p->p_singlethread);
99
100	PROC_UNLOCK(p);
101	td->td_kse = NULL;
102	td->td_state = TDS_INACTIVE;
103#if 0
104	td->td_proc = NULL;
105#endif
106	td->td_ksegrp = NULL;
107	td->td_last_kse = NULL;
108	sched_exit_thread(TAILQ_NEXT(td, td_kglist), td);
109	thread_stash(td);
110
111	cpu_throw(td, choosethread());
112}
113
114#define	RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
115
116/*
117 * System call interface.
118 */
119int
120thr_create(struct thread *td, struct thr_create_args *uap)
121    /* ucontext_t *ctx, thr_id_t *id, int flags */
122{
123	struct kse *ke0;
124	struct thread *td0;
125	ucontext_t ctx;
126	int error;
127
128	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
129		return (error);
130
131	/* Initialize our td. */
132	td0 = thread_alloc();
133
134	/*
135	 * Try the copyout as soon as we allocate the td so we don't have to
136	 * tear things down in a failure case below.
137	 */
138	if ((error = copyout(&td0, uap->id, sizeof(thr_id_t)))) {
139		thread_free(td0);
140		return (error);
141	}
142
143	bzero(&td0->td_startzero,
144	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
145	bcopy(&td->td_startcopy, &td0->td_startcopy,
146	    (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
147
148	td0->td_proc = td->td_proc;
149	PROC_LOCK(td->td_proc);
150	td0->td_sigmask = td->td_sigmask;
151	PROC_UNLOCK(td->td_proc);
152	td0->td_ucred = crhold(td->td_ucred);
153
154	/* Initialize our kse structure. */
155	ke0 = kse_alloc();
156	bzero(&ke0->ke_startzero,
157	    RANGEOF(struct kse, ke_startzero, ke_endzero));
158
159	/* Set up our machine context. */
160	cpu_set_upcall(td0, td);
161	error = set_mcontext(td0, &ctx.uc_mcontext);
162	if (error != 0) {
163		kse_free(ke0);
164		thread_free(td0);
165		goto out;
166	}
167
168	/* Link the thread and kse into the ksegrp and make it runnable. */
169	mtx_lock_spin(&sched_lock);
170
171	thread_link(td0, td->td_ksegrp);
172	kse_link(ke0, td->td_ksegrp);
173
174	/* Bind this thread and kse together. */
175	td0->td_kse = ke0;
176	ke0->ke_thread = td0;
177
178	sched_fork_kse(td->td_kse, ke0);
179	sched_fork_thread(td, td0);
180
181	TD_SET_CAN_RUN(td0);
182	if ((uap->flags & THR_SUSPENDED) == 0)
183		setrunqueue(td0);
184
185	mtx_unlock_spin(&sched_lock);
186
187out:
188	return (error);
189}
190
191int
192thr_self(struct thread *td, struct thr_self_args *uap)
193    /* thr_id_t *id */
194{
195	int error;
196
197	if ((error = copyout(&td, uap->id, sizeof(thr_id_t))))
198		return (error);
199
200	return (0);
201}
202
203int
204thr_exit(struct thread *td, struct thr_exit_args *uap)
205    /* NULL */
206{
207	struct proc *p;
208
209	p = td->td_proc;
210
211	PROC_LOCK(p);
212	mtx_lock_spin(&sched_lock);
213
214	/*
215	 * This unlocks proc and doesn't return unless this is the last
216	 * thread.
217	 */
218	thr_exit1();
219	mtx_unlock_spin(&sched_lock);
220
221	return (0);
222}
223
224int
225thr_kill(struct thread *td, struct thr_kill_args *uap)
226    /* thr_id_t id, int sig */
227{
228	struct thread *ttd;
229	struct proc *p;
230	int error;
231
232	p = td->td_proc;
233	error = 0;
234	PROC_LOCK(p);
235	FOREACH_THREAD_IN_PROC(p, ttd) {
236		if (ttd == uap->id)
237			break;
238	}
239	if (ttd == NULL) {
240		error = ESRCH;
241		goto out;
242	}
243	if (uap->sig == 0)
244		goto out;
245	if (!_SIG_VALID(uap->sig)) {
246		error = EINVAL;
247		goto out;
248	}
249	tdsignal(ttd, uap->sig, SIGTARGET_TD);
250out:
251	PROC_UNLOCK(p);
252	return (error);
253}
254
255int
256thr_suspend(struct thread *td, struct thr_suspend_args *uap)
257	/* const struct timespec *timeout */
258{
259	struct timespec ts;
260	struct timeval	tv;
261	int error;
262	int hz;
263
264	hz = 0;
265	error = 0;
266	if (uap->timeout != NULL) {
267		error = copyin((const void *)uap->timeout, (void *)&ts,
268		    sizeof(struct timespec));
269		if (error != 0)
270			return (error);
271		if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
272			return (EINVAL);
273		if (ts.tv_sec == 0 && ts.tv_nsec == 0)
274			return (ETIMEDOUT);
275		TIMESPEC_TO_TIMEVAL(&tv, &ts);
276		hz = tvtohz(&tv);
277	}
278	PROC_LOCK(td->td_proc);
279	mtx_lock_spin(&sched_lock);
280	if ((td->td_flags & TDF_THRWAKEUP) == 0) {
281		mtx_unlock_spin(&sched_lock);
282		error = msleep((void *)td, &td->td_proc->p_mtx,
283		    td->td_priority | PCATCH, "lthr", hz);
284		mtx_lock_spin(&sched_lock);
285	}
286	td->td_flags &= ~TDF_THRWAKEUP;
287	mtx_unlock_spin(&sched_lock);
288	PROC_UNLOCK(td->td_proc);
289	return (error == EWOULDBLOCK ? ETIMEDOUT : error);
290}
291
292int
293thr_wake(struct thread *td, struct thr_wake_args *uap)
294	/* thr_id_t id */
295{
296	struct thread *tdsleeper, *ttd;
297
298	tdsleeper = ((struct thread *)uap->id);
299	PROC_LOCK(tdsleeper->td_proc);
300	FOREACH_THREAD_IN_PROC(tdsleeper->td_proc, ttd) {
301		if (ttd == tdsleeper)
302			break;
303	}
304	if (ttd == NULL) {
305		PROC_UNLOCK(tdsleeper->td_proc);
306		return (ESRCH);
307	}
308	mtx_lock_spin(&sched_lock);
309	tdsleeper->td_flags |= TDF_THRWAKEUP;
310	mtx_unlock_spin(&sched_lock);
311	wakeup_one((void *)tdsleeper);
312	PROC_UNLOCK(tdsleeper->td_proc);
313	return (0);
314}
315