2 * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
33 #include <sys/mutex.h>
35 #include <sys/resourcevar.h>
36 #include <sys/sched.h>
37 #include <sys/sysctl.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>
46 #include <sys/limits.h>
48 #include <machine/frame.h>
50 extern int max_threads_per_proc;
51 extern int max_groups_per_proc;
53 SYSCTL_DECL(_kern_threads);
54 static int thr_scope = 0;
55 SYSCTL_INT(_kern_threads, OID_AUTO, thr_scope, CTLFLAG_RW,
56 &thr_scope, 0, "sys or proc scope scheduling");
58 static int thr_concurrency = 0;
59 SYSCTL_INT(_kern_threads, OID_AUTO, thr_concurrency, CTLFLAG_RW,
60 &thr_concurrency, 0, "a concurrency value if not default");
62 static int create_thread(struct thread *td, mcontext_t *ctx,
63 void (*start_func)(void *), void *arg,
64 char *stack_base, size_t stack_size,
66 long *child_tid, long *parent_tid,
70 * System call interface.
73 thr_create(struct thread *td, struct thr_create_args *uap)
74 /* ucontext_t *ctx, long *id, int flags */
79 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
82 error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
83 NULL, 0, NULL, uap->id, NULL, uap->flags);
88 thr_new(struct thread *td, struct thr_new_args *uap)
89 /* struct thr_param * */
91 struct thr_param param;
94 if (uap->param_size < sizeof(param))
96 if ((error = copyin(uap->param, ¶m, sizeof(param))))
98 error = create_thread(td, NULL, param.start_func, param.arg,
99 param.stack_base, param.stack_size, param.tls_base,
100 param.child_tid, param.parent_tid, param.flags);
105 create_thread(struct thread *td, mcontext_t *ctx,
106 void (*start_func)(void *), void *arg,
107 char *stack_base, size_t stack_size,
109 long *child_tid, long *parent_tid,
113 struct thread *newtd;
114 struct ksegrp *kg, *newkg;
117 int error, scope_sys, linkkg;
123 /* Have race condition but it is cheap. */
124 if ((p->p_numksegrps >= max_groups_per_proc) ||
125 (p->p_numthreads >= max_threads_per_proc)) {
129 /* Check PTHREAD_SCOPE_SYSTEM */
130 scope_sys = (flags & THR_SYSTEM_SCOPE) != 0;
132 /* sysctl overrides user's flag */
135 else if (thr_scope == 2)
138 /* Initialize our td and new ksegrp.. */
139 newtd = thread_alloc();
142 * Try the copyout as soon as we allocate the td so we don't
143 * have to tear things down in a failure case below.
144 * Here we copy out tid to two places, one for child and one
145 * for parent, because pthread can create a detached thread,
146 * if parent wants to safely access child tid, it has to provide
147 * its storage, because child thread may exit quickly and
148 * memory is freed before parent thread can access it.
151 if ((child_tid != NULL &&
152 (error = copyout(&id, child_tid, sizeof(long)))) ||
153 (parent_tid != NULL &&
154 (error = copyout(&id, parent_tid, sizeof(long))))) {
158 bzero(&newtd->td_startzero,
159 __rangeof(struct thread, td_startzero, td_endzero));
160 bcopy(&td->td_startcopy, &newtd->td_startcopy,
161 __rangeof(struct thread, td_startcopy, td_endcopy));
162 newtd->td_proc = td->td_proc;
163 newtd->td_ucred = crhold(td->td_ucred);
165 cpu_set_upcall(newtd, td);
167 if (ctx != NULL) { /* old way to set user context */
168 error = set_mcontext(newtd, ctx);
171 crfree(td->td_ucred);
175 /* Set up our machine context. */
176 stack.ss_sp = stack_base;
177 stack.ss_size = stack_size;
178 /* Set upcall address to user thread entry function. */
179 cpu_set_upcall_kse(newtd, start_func, arg, &stack);
180 /* Setup user TLS address and TLS pointer register. */
181 error = cpu_set_user_tls(newtd, tls_base);
184 crfree(td->td_ucred);
189 if ((td->td_proc->p_flag & P_HADTHREADS) == 0) {
190 /* Treat initial thread as it has PTHREAD_SCOPE_PROCESS. */
191 p->p_procscopegrp = kg;
192 mtx_lock_spin(&sched_lock);
193 sched_set_concurrency(kg,
194 thr_concurrency ? thr_concurrency : (2*mp_ncpus));
195 mtx_unlock_spin(&sched_lock);
201 newkg = ksegrp_alloc();
202 bzero(&newkg->kg_startzero,
203 __rangeof(struct ksegrp, kg_startzero, kg_endzero));
204 bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
205 __rangeof(struct ksegrp, kg_startcopy, kg_endcopy));
206 sched_init_concurrency(newkg);
207 PROC_LOCK(td->td_proc);
210 * Try to create a KSE group which will be shared
211 * by all PTHREAD_SCOPE_PROCESS threads.
214 PROC_LOCK(td->td_proc);
215 if ((newkg = p->p_procscopegrp) == NULL) {
217 newkg = ksegrp_alloc();
218 bzero(&newkg->kg_startzero,
219 __rangeof(struct ksegrp, kg_startzero, kg_endzero));
220 bcopy(&kg->kg_startcopy, &newkg->kg_startcopy,
221 __rangeof(struct ksegrp, kg_startcopy, kg_endcopy));
223 if (p->p_procscopegrp == NULL) {
224 p->p_procscopegrp = newkg;
225 sched_init_concurrency(newkg);
226 sched_set_concurrency(newkg,
227 thr_concurrency ? thr_concurrency : (2*mp_ncpus));
237 td->td_proc->p_flag |= P_HADTHREADS;
238 newtd->td_sigmask = td->td_sigmask;
239 mtx_lock_spin(&sched_lock);
241 ksegrp_link(newkg, p);
242 thread_link(newtd, newkg);
245 /* let the scheduler know about these things. */
247 sched_fork_ksegrp(td, newkg);
248 sched_fork_thread(td, newtd);
249 TD_SET_CAN_RUN(newtd);
250 /* if ((flags & THR_SUSPENDED) == 0) */
251 setrunqueue(newtd, SRQ_BORING);
252 mtx_unlock_spin(&sched_lock);
258 thr_self(struct thread *td, struct thr_self_args *uap)
265 if ((error = copyout(&id, uap->id, sizeof(long))))
272 thr_exit(struct thread *td, struct thr_exit_args *uap)
279 /* Signal userland that it can free the stack. */
280 if ((void *)uap->state != NULL) {
281 suword((void *)uap->state, 1);
282 kern_umtx_wake(td, uap->state, INT_MAX);
286 sigqueue_flush(&td->td_sigqueue);
287 mtx_lock_spin(&sched_lock);
290 * Shutting down last thread in the proc. This will actually
291 * call exit() in the trampoline when it returns.
293 if (p->p_numthreads != 1) {
297 mtx_unlock_spin(&sched_lock);
303 thr_kill(struct thread *td, struct thr_kill_args *uap)
304 /* long id, int sig */
314 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
318 FOREACH_THREAD_IN_PROC(p, ttd) {
323 tdsignal(p, ttd, uap->sig, NULL);
328 if (uap->id != td->td_tid)
329 ttd = thread_find(p, uap->id);
334 else if (uap->sig == 0)
336 else if (!_SIG_VALID(uap->sig))
339 tdsignal(p, ttd, uap->sig, NULL);
346 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
347 /* const struct timespec *timeout */
356 if (uap->timeout != NULL) {
357 error = copyin((const void *)uap->timeout, (void *)&ts,
358 sizeof(struct timespec));
361 if (ts.tv_nsec < 0 || ts.tv_nsec > 1000000000)
363 if (ts.tv_sec == 0 && ts.tv_nsec == 0)
365 TIMESPEC_TO_TIMEVAL(&tv, &ts);
368 PROC_LOCK(td->td_proc);
369 if ((td->td_flags & TDF_THRWAKEUP) == 0)
370 error = msleep((void *)td, &td->td_proc->p_mtx,
371 td->td_priority | PCATCH, "lthr", hz);
372 if (td->td_flags & TDF_THRWAKEUP) {
373 mtx_lock_spin(&sched_lock);
374 td->td_flags &= ~TDF_THRWAKEUP;
375 mtx_unlock_spin(&sched_lock);
376 PROC_UNLOCK(td->td_proc);
379 PROC_UNLOCK(td->td_proc);
380 if (error == EWOULDBLOCK)
382 else if (error == ERESTART) {
390 thr_wake(struct thread *td, struct thr_wake_args *uap)
398 ttd = thread_find(p, uap->id);
403 mtx_lock_spin(&sched_lock);
404 ttd->td_flags |= TDF_THRWAKEUP;
405 mtx_unlock_spin(&sched_lock);