xref: /freebsd/sys/kern/kern_thr.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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$");
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 
50 void
51 thr_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), td);
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  */
119 int
120 thr_create(struct thread *td, struct thr_create_args *uap)
121     /* ucontext_t *ctx, long *id, int flags */
122 {
123 	struct kse *ke0;
124 	struct thread *td0;
125 	ucontext_t ctx;
126 	long id;
127 	int error;
128 
129 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
130 		return (error);
131 
132 	/* Initialize our td. */
133 	td0 = thread_alloc();
134 
135 	/*
136 	 * Try the copyout as soon as we allocate the td so we don't have to
137 	 * tear things down in a failure case below.
138 	 */
139 	id = td0->td_tid;
140 	if ((error = copyout(&id, uap->id, sizeof(long)))) {
141 		thread_free(td0);
142 		return (error);
143 	}
144 
145 	bzero(&td0->td_startzero,
146 	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
147 	bcopy(&td->td_startcopy, &td0->td_startcopy,
148 	    (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
149 
150 	td0->td_proc = td->td_proc;
151 	PROC_LOCK(td->td_proc);
152 	td0->td_sigmask = td->td_sigmask;
153 	PROC_UNLOCK(td->td_proc);
154 	td0->td_ucred = crhold(td->td_ucred);
155 
156 	/* Initialize our kse structure. */
157 	ke0 = kse_alloc();
158 	bzero(&ke0->ke_startzero,
159 	    RANGEOF(struct kse, ke_startzero, ke_endzero));
160 
161 	/* Set up our machine context. */
162 	cpu_set_upcall(td0, td);
163 	error = set_mcontext(td0, &ctx.uc_mcontext);
164 	if (error != 0) {
165 		kse_free(ke0);
166 		thread_free(td0);
167 		goto out;
168 	}
169 
170 	/* Link the thread and kse into the ksegrp and make it runnable. */
171 	mtx_lock_spin(&sched_lock);
172 
173 	thread_link(td0, td->td_ksegrp);
174 	kse_link(ke0, td->td_ksegrp);
175 
176 	/* Bind this thread and kse together. */
177 	td0->td_kse = ke0;
178 	ke0->ke_thread = td0;
179 
180 	sched_fork_kse(td, ke0);
181 	sched_fork_thread(td, td0);
182 
183 	TD_SET_CAN_RUN(td0);
184 	if ((uap->flags & THR_SUSPENDED) == 0)
185 		setrunqueue(td0);
186 
187 	mtx_unlock_spin(&sched_lock);
188 
189 out:
190 	return (error);
191 }
192 
193 int
194 thr_self(struct thread *td, struct thr_self_args *uap)
195     /* long *id */
196 {
197 	long id;
198 	int error;
199 
200 	id = td->td_tid;
201 	if ((error = copyout(&id, uap->id, sizeof(long))))
202 		return (error);
203 
204 	return (0);
205 }
206 
207 int
208 thr_exit(struct thread *td, struct thr_exit_args *uap)
209     /* NULL */
210 {
211 	struct proc *p;
212 
213 	p = td->td_proc;
214 
215 	PROC_LOCK(p);
216 	mtx_lock_spin(&sched_lock);
217 
218 	/*
219 	 * This unlocks proc and doesn't return unless this is the last
220 	 * thread.
221 	 */
222 	thr_exit1();
223 	mtx_unlock_spin(&sched_lock);
224 
225 	return (0);
226 }
227 
228 int
229 thr_kill(struct thread *td, struct thr_kill_args *uap)
230     /* long id, int sig */
231 {
232 	struct thread *ttd;
233 	struct proc *p;
234 	int error;
235 
236 	p = td->td_proc;
237 	error = 0;
238 	PROC_LOCK(p);
239 	FOREACH_THREAD_IN_PROC(p, ttd) {
240 		if (ttd->td_tid == uap->id)
241 			break;
242 	}
243 	if (ttd == NULL) {
244 		error = ESRCH;
245 		goto out;
246 	}
247 	if (uap->sig == 0)
248 		goto out;
249 	if (!_SIG_VALID(uap->sig)) {
250 		error = EINVAL;
251 		goto out;
252 	}
253 	tdsignal(ttd, uap->sig, SIGTARGET_TD);
254 out:
255 	PROC_UNLOCK(p);
256 	return (error);
257 }
258 
259 int
260 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
261 	/* const struct timespec *timeout */
262 {
263 	struct timespec ts;
264 	struct timeval	tv;
265 	int error;
266 	int hz;
267 
268 	hz = 0;
269 	error = 0;
270 	if (uap->timeout != NULL) {
271 		error = copyin((const void *)uap->timeout, (void *)&ts,
272 		    sizeof(struct timespec));
273 		if (error != 0)
274 			return (error);
275 		if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
276 			return (EINVAL);
277 		if (ts.tv_sec == 0 && ts.tv_nsec == 0)
278 			return (ETIMEDOUT);
279 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
280 		hz = tvtohz(&tv);
281 	}
282 	PROC_LOCK(td->td_proc);
283 	if ((td->td_flags & TDF_THRWAKEUP) == 0)
284 		error = msleep((void *)td, &td->td_proc->p_mtx,
285 		    td->td_priority | PCATCH, "lthr", hz);
286 	mtx_lock_spin(&sched_lock);
287 	td->td_flags &= ~TDF_THRWAKEUP;
288 	mtx_unlock_spin(&sched_lock);
289 	PROC_UNLOCK(td->td_proc);
290 	return (error == EWOULDBLOCK ? ETIMEDOUT : error);
291 }
292 
293 int
294 thr_wake(struct thread *td, struct thr_wake_args *uap)
295 	/* long id */
296 {
297 	struct thread *ttd;
298 
299 	PROC_LOCK(td->td_proc);
300 	FOREACH_THREAD_IN_PROC(td->td_proc, ttd) {
301 		if (ttd->td_tid == uap->id)
302 			break;
303 	}
304 	if (ttd == NULL) {
305 		PROC_UNLOCK(td->td_proc);
306 		return (ESRCH);
307 	}
308 	mtx_lock_spin(&sched_lock);
309 	ttd->td_flags |= TDF_THRWAKEUP;
310 	mtx_unlock_spin(&sched_lock);
311 	wakeup_one((void *)ttd);
312 	PROC_UNLOCK(td->td_proc);
313 	return (0);
314 }
315