xref: /freebsd/sys/kern/kern_thr.c (revision 7562eaabc01a48e6b11d5b558c41e3b92dae5c2d)
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/sysctl.h>
38 #include <sys/smp.h>
39 #include <sys/sysent.h>
40 #include <sys/systm.h>
41 #include <sys/sysproto.h>
42 #include <sys/signalvar.h>
43 #include <sys/ucontext.h>
44 #include <sys/thr.h>
45 
46 #include <machine/frame.h>
47 
48 extern int max_threads_per_proc;
49 extern int max_groups_per_proc;
50 
51 SYSCTL_DECL(_kern_threads);
52 static int thr_scope_sys = 0;
53 SYSCTL_INT(_kern_threads, OID_AUTO, thr_scope_sys, CTLFLAG_RW,
54 	&thr_scope_sys, 0, "sys or proc scope scheduling");
55 
56 static int thr_concurrency = 0;
57 SYSCTL_INT(_kern_threads, OID_AUTO, thr_concurrency, CTLFLAG_RW,
58 	&thr_concurrency, 0, "a concurrency value if not default");
59 
60 /*
61  * Back end support functions.
62  */
63 
64 #define	RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
65 
66 /*
67  * System call interface.
68  */
69 int
70 thr_create(struct thread *td, struct thr_create_args *uap)
71     /* ucontext_t *ctx, long *id, int flags */
72 {
73 	struct thread *newtd;
74 	ucontext_t ctx;
75 	long id;
76 	int error;
77 	struct ksegrp *kg, *newkg;
78 	struct proc *p;
79 	int scope_sys;
80 
81 	p = td->td_proc;
82 	kg = td->td_ksegrp;
83 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
84 		return (error);
85 
86 	/* Have race condition but it is cheap */
87 	if ((p->p_numksegrps >= max_groups_per_proc) ||
88 	    (p->p_numthreads >= max_threads_per_proc)) {
89 		return (EPROCLIM);
90 	}
91 
92 	scope_sys = thr_scope_sys;
93 	/* Initialize our td and new ksegrp.. */
94 	newtd = thread_alloc();
95 	if (scope_sys)
96 		newkg = ksegrp_alloc();
97 	else
98 		newkg = kg;
99 	/*
100 	 * Try the copyout as soon as we allocate the td so we don't have to
101 	 * tear things down in a failure case below.
102 	 */
103 	id = newtd->td_tid;
104 	if ((error = copyout(&id, uap->id, sizeof(long)))) {
105 		if (scope_sys)
106 			ksegrp_free(newkg);
107 		thread_free(newtd);
108 		return (error);
109 	}
110 
111 	bzero(&newtd->td_startzero,
112 	    (unsigned) RANGEOF(struct thread, td_startzero, td_endzero));
113 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
114 	    (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
115 
116 	if (scope_sys) {
117 		bzero(&newkg->kg_startzero,
118 		    (unsigned)RANGEOF(struct ksegrp, kg_startzero, kg_endzero));
119 		bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
120 		    (unsigned)RANGEOF(struct ksegrp, kg_startcopy, kg_endcopy));
121 	}
122 
123 	newtd->td_proc = td->td_proc;
124 	newtd->td_ucred = crhold(td->td_ucred);
125 
126 	/* Set up our machine context. */
127 	cpu_set_upcall(newtd, td);
128 	error = set_mcontext(newtd, &ctx.uc_mcontext);
129 	if (error != 0) {
130 		if (scope_sys)
131 			ksegrp_free(newkg);
132 		thread_free(newtd);
133 		crfree(td->td_ucred);
134 		goto out;
135 	}
136 
137 	/* Link the thread and kse into the ksegrp and make it runnable. */
138 	PROC_LOCK(td->td_proc);
139 	if (scope_sys) {
140 			sched_init_concurrency(newkg);
141 	} else {
142 		if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
143 			sched_set_concurrency(kg,
144 			    thr_concurrency ? thr_concurrency : (2*mp_ncpus));
145 		}
146 	}
147 
148 	td->td_proc->p_flag |= P_HADTHREADS;
149 	newtd->td_sigmask = td->td_sigmask;
150 	mtx_lock_spin(&sched_lock);
151 	if (scope_sys)
152 		ksegrp_link(newkg, p);
153 	thread_link(newtd, newkg);
154 	mtx_unlock_spin(&sched_lock);
155 	PROC_UNLOCK(p);
156 
157 	/* let the scheduler know about these things. */
158 	mtx_lock_spin(&sched_lock);
159 	if (scope_sys)
160 		sched_fork_ksegrp(td, newkg);
161 	sched_fork_thread(td, newtd);
162 
163 	TD_SET_CAN_RUN(newtd);
164 	if ((uap->flags & THR_SUSPENDED) == 0)
165 		setrunqueue(newtd, SRQ_BORING);
166 
167 	mtx_unlock_spin(&sched_lock);
168 
169 out:
170 	return (error);
171 }
172 
173 int
174 thr_self(struct thread *td, struct thr_self_args *uap)
175     /* long *id */
176 {
177 	long id;
178 	int error;
179 
180 	id = td->td_tid;
181 	if ((error = copyout(&id, uap->id, sizeof(long))))
182 		return (error);
183 
184 	return (0);
185 }
186 
187 int
188 thr_exit(struct thread *td, struct thr_exit_args *uap)
189     /* long *state */
190 {
191 	struct proc *p;
192 
193 	p = td->td_proc;
194 
195 	/* Signal userland that it can free the stack. */
196 	if ((void *)uap->state != NULL)
197 		suword((void *)uap->state, 1);
198 
199 	PROC_LOCK(p);
200 	mtx_lock_spin(&sched_lock);
201 
202 	/*
203 	 * Shutting down last thread in the proc.  This will actually
204 	 * call exit() in the trampoline when it returns.
205 	 */
206 	if (p->p_numthreads != 1) {
207 		thread_exit();
208 		/* NOTREACHED */
209 	}
210 	mtx_unlock_spin(&sched_lock);
211 	PROC_UNLOCK(p);
212 	return (0);
213 }
214 
215 int
216 thr_kill(struct thread *td, struct thr_kill_args *uap)
217     /* long id, int sig */
218 {
219 	struct thread *ttd;
220 	struct proc *p;
221 	int error;
222 
223 	p = td->td_proc;
224 	error = 0;
225 	PROC_LOCK(p);
226 	FOREACH_THREAD_IN_PROC(p, ttd) {
227 		if (ttd->td_tid == uap->id)
228 			break;
229 	}
230 	if (ttd == NULL) {
231 		error = ESRCH;
232 		goto out;
233 	}
234 	if (uap->sig == 0)
235 		goto out;
236 	if (!_SIG_VALID(uap->sig)) {
237 		error = EINVAL;
238 		goto out;
239 	}
240 	tdsignal(ttd, uap->sig, SIGTARGET_TD);
241 out:
242 	PROC_UNLOCK(p);
243 	return (error);
244 }
245 
246 int
247 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
248 	/* const struct timespec *timeout */
249 {
250 	struct timespec ts;
251 	struct timeval	tv;
252 	int error;
253 	int hz;
254 
255 	hz = 0;
256 	error = 0;
257 	if (uap->timeout != NULL) {
258 		error = copyin((const void *)uap->timeout, (void *)&ts,
259 		    sizeof(struct timespec));
260 		if (error != 0)
261 			return (error);
262 		if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
263 			return (EINVAL);
264 		if (ts.tv_sec == 0 && ts.tv_nsec == 0)
265 			return (ETIMEDOUT);
266 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
267 		hz = tvtohz(&tv);
268 	}
269 	PROC_LOCK(td->td_proc);
270 	if ((td->td_flags & TDF_THRWAKEUP) == 0)
271 		error = msleep((void *)td, &td->td_proc->p_mtx,
272 		    td->td_priority | PCATCH, "lthr", hz);
273 	mtx_lock_spin(&sched_lock);
274 	td->td_flags &= ~TDF_THRWAKEUP;
275 	mtx_unlock_spin(&sched_lock);
276 	PROC_UNLOCK(td->td_proc);
277 	return (error == EWOULDBLOCK ? ETIMEDOUT : error);
278 }
279 
280 int
281 thr_wake(struct thread *td, struct thr_wake_args *uap)
282 	/* long id */
283 {
284 	struct thread *ttd;
285 
286 	PROC_LOCK(td->td_proc);
287 	FOREACH_THREAD_IN_PROC(td->td_proc, ttd) {
288 		if (ttd->td_tid == uap->id)
289 			break;
290 	}
291 	if (ttd == NULL) {
292 		PROC_UNLOCK(td->td_proc);
293 		return (ESRCH);
294 	}
295 	mtx_lock_spin(&sched_lock);
296 	ttd->td_flags |= TDF_THRWAKEUP;
297 	mtx_unlock_spin(&sched_lock);
298 	wakeup_one((void *)ttd);
299 	PROC_UNLOCK(td->td_proc);
300 	return (0);
301 }
302