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 "opt_compat.h"
31 #include "opt_posix.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/mutex.h>
38 #include <sys/posix4.h>
39 #include <sys/resourcevar.h>
40 #include <sys/sched.h>
41 #include <sys/sysctl.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysent.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/signalvar.h>
48 #include <sys/ucontext.h>
50 #include <sys/rtprio.h>
52 #include <sys/limits.h>
54 #include <machine/frame.h>
58 extern struct sysentvec ia32_freebsd_sysvec;
61 suword_lwpid(void *addr, lwpid_t lwpid)
65 if (curproc->p_sysent != &ia32_freebsd_sysvec)
66 error = suword(addr, lwpid);
68 error = suword32(addr, lwpid);
73 #define suword_lwpid suword
76 extern int max_threads_per_proc;
78 static int create_thread(struct thread *td, mcontext_t *ctx,
79 void (*start_func)(void *), void *arg,
80 char *stack_base, size_t stack_size,
82 long *child_tid, long *parent_tid,
83 int flags, struct rtprio *rtp);
86 * System call interface.
89 thr_create(struct thread *td, struct thr_create_args *uap)
90 /* ucontext_t *ctx, long *id, int flags */
95 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
98 error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
99 NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
104 thr_new(struct thread *td, struct thr_new_args *uap)
105 /* struct thr_param * */
107 struct thr_param param;
110 if (uap->param_size < 0 || uap->param_size > sizeof(param))
112 bzero(¶m, sizeof(param));
113 if ((error = copyin(uap->param, ¶m, uap->param_size)))
115 return (kern_thr_new(td, ¶m));
119 kern_thr_new(struct thread *td, struct thr_param *param)
121 struct rtprio rtp, *rtpp;
125 if (param->rtp != 0) {
126 error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
129 error = create_thread(td, NULL, param->start_func, param->arg,
130 param->stack_base, param->stack_size, param->tls_base,
131 param->child_tid, param->parent_tid, param->flags,
137 create_thread(struct thread *td, mcontext_t *ctx,
138 void (*start_func)(void *), void *arg,
139 char *stack_base, size_t stack_size,
141 long *child_tid, long *parent_tid,
142 int flags, struct rtprio *rtp)
145 struct thread *newtd;
152 /* Have race condition but it is cheap. */
153 if (p->p_numthreads >= max_threads_per_proc)
158 case RTP_PRIO_REALTIME:
160 /* Only root can set scheduler policy */
161 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
163 if (rtp->prio > RTP_PRIO_MAX)
166 case RTP_PRIO_NORMAL:
174 /* Initialize our td */
175 newtd = thread_alloc();
178 * Try the copyout as soon as we allocate the td so we don't
179 * have to tear things down in a failure case below.
180 * Here we copy out tid to two places, one for child and one
181 * for parent, because pthread can create a detached thread,
182 * if parent wants to safely access child tid, it has to provide
183 * its storage, because child thread may exit quickly and
184 * memory is freed before parent thread can access it.
186 if ((child_tid != NULL &&
187 suword_lwpid(child_tid, newtd->td_tid)) ||
188 (parent_tid != NULL &&
189 suword_lwpid(parent_tid, newtd->td_tid))) {
194 bzero(&newtd->td_startzero,
195 __rangeof(struct thread, td_startzero, td_endzero));
196 bcopy(&td->td_startcopy, &newtd->td_startcopy,
197 __rangeof(struct thread, td_startcopy, td_endcopy));
198 newtd->td_proc = td->td_proc;
199 newtd->td_ucred = crhold(td->td_ucred);
201 cpu_set_upcall(newtd, td);
203 if (ctx != NULL) { /* old way to set user context */
204 error = set_mcontext(newtd, ctx);
207 crfree(td->td_ucred);
211 /* Set up our machine context. */
212 stack.ss_sp = stack_base;
213 stack.ss_size = stack_size;
214 /* Set upcall address to user thread entry function. */
215 cpu_set_upcall_kse(newtd, start_func, arg, &stack);
216 /* Setup user TLS address and TLS pointer register. */
217 error = cpu_set_user_tls(newtd, tls_base);
220 crfree(td->td_ucred);
225 PROC_LOCK(td->td_proc);
226 td->td_proc->p_flag |= P_HADTHREADS;
227 newtd->td_sigmask = td->td_sigmask;
229 thread_link(newtd, p);
231 /* let the scheduler know about these things. */
232 sched_fork_thread(td, newtd);
238 if (!(td->td_pri_class == PRI_TIMESHARE &&
239 rtp->type == RTP_PRIO_NORMAL)) {
240 rtp_to_pri(rtp, newtd);
241 sched_prio(newtd, newtd->td_user_pri);
242 } /* ignore timesharing class */
244 TD_SET_CAN_RUN(newtd);
245 /* if ((flags & THR_SUSPENDED) == 0) */
246 sched_add(newtd, SRQ_BORING);
247 thread_unlock(newtd);
253 thr_self(struct thread *td, struct thr_self_args *uap)
258 error = suword_lwpid(uap->id, (unsigned)td->td_tid);
265 thr_exit(struct thread *td, struct thr_exit_args *uap)
272 /* Signal userland that it can free the stack. */
273 if ((void *)uap->state != NULL) {
274 suword_lwpid(uap->state, 1);
275 kern_umtx_wake(td, uap->state, INT_MAX);
279 sigqueue_flush(&td->td_sigqueue);
283 * Shutting down last thread in the proc. This will actually
284 * call exit() in the trampoline when it returns.
286 if (p->p_numthreads != 1) {
297 thr_kill(struct thread *td, struct thr_kill_args *uap)
298 /* long id, int sig */
308 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
312 FOREACH_THREAD_IN_PROC(p, ttd) {
317 tdsignal(p, ttd, uap->sig, NULL);
322 if (uap->id != td->td_tid)
323 ttd = thread_find(p, uap->id);
328 else if (uap->sig == 0)
330 else if (!_SIG_VALID(uap->sig))
333 tdsignal(p, ttd, uap->sig, NULL);
340 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
341 /* const struct timespec *timeout */
343 struct timespec ts, *tsp;
348 if (uap->timeout != NULL) {
349 error = copyin((const void *)uap->timeout, (void *)&ts,
350 sizeof(struct timespec));
356 return (kern_thr_suspend(td, tsp));
360 kern_thr_suspend(struct thread *td, struct timespec *tsp)
363 int error = 0, hz = 0;
366 if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
368 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
370 TIMESPEC_TO_TIMEVAL(&tv, tsp);
374 if (td->td_pflags & TDP_WAKEUP) {
375 td->td_pflags &= ~TDP_WAKEUP;
379 PROC_LOCK(td->td_proc);
380 if ((td->td_flags & TDF_THRWAKEUP) == 0)
381 error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
383 if (td->td_flags & TDF_THRWAKEUP) {
385 td->td_flags &= ~TDF_THRWAKEUP;
387 PROC_UNLOCK(td->td_proc);
390 PROC_UNLOCK(td->td_proc);
391 if (error == EWOULDBLOCK)
393 else if (error == ERESTART) {
401 thr_wake(struct thread *td, struct thr_wake_args *uap)
407 if (uap->id == td->td_tid) {
408 td->td_pflags |= TDP_WAKEUP;
414 ttd = thread_find(p, uap->id);
420 ttd->td_flags |= TDF_THRWAKEUP;
428 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
430 struct proc *p = td->td_proc;
431 char name[MAXCOMLEN + 1];
437 if (uap->name != NULL) {
438 error = copyinstr(uap->name, name, sizeof(name),
444 if (uap->id == td->td_tid)
447 ttd = thread_find(p, uap->id);
449 strcpy(ttd->td_name, name);