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>
56 #include <security/audit/audit.h>
58 #ifdef COMPAT_FREEBSD32
61 suword_lwpid(void *addr, lwpid_t lwpid)
65 if (SV_CURPROC_FLAG(SV_LP64))
66 error = suword(addr, lwpid);
68 error = suword32(addr, lwpid);
73 #define suword_lwpid suword
76 extern int max_threads_per_proc;
77 extern int max_threads_hits;
79 static int create_thread(struct thread *td, mcontext_t *ctx,
80 void (*start_func)(void *), void *arg,
81 char *stack_base, size_t stack_size,
83 long *child_tid, long *parent_tid,
84 int flags, struct rtprio *rtp);
87 * System call interface.
90 thr_create(struct thread *td, struct thr_create_args *uap)
91 /* ucontext_t *ctx, long *id, int flags */
96 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
99 error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
100 NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
105 thr_new(struct thread *td, struct thr_new_args *uap)
106 /* struct thr_param * */
108 struct thr_param param;
111 if (uap->param_size < 0 || uap->param_size > sizeof(param))
113 bzero(¶m, sizeof(param));
114 if ((error = copyin(uap->param, ¶m, uap->param_size)))
116 return (kern_thr_new(td, ¶m));
120 kern_thr_new(struct thread *td, struct thr_param *param)
122 struct rtprio rtp, *rtpp;
126 if (param->rtp != 0) {
127 error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
132 error = create_thread(td, NULL, param->start_func, param->arg,
133 param->stack_base, param->stack_size, param->tls_base,
134 param->child_tid, param->parent_tid, param->flags,
140 create_thread(struct thread *td, mcontext_t *ctx,
141 void (*start_func)(void *), void *arg,
142 char *stack_base, size_t stack_size,
144 long *child_tid, long *parent_tid,
145 int flags, struct rtprio *rtp)
148 struct thread *newtd;
154 /* Have race condition but it is cheap. */
155 if (p->p_numthreads >= max_threads_per_proc) {
162 case RTP_PRIO_REALTIME:
164 /* Only root can set scheduler policy */
165 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
167 if (rtp->prio > RTP_PRIO_MAX)
170 case RTP_PRIO_NORMAL:
178 /* Initialize our td */
179 newtd = thread_alloc(0);
184 * Try the copyout as soon as we allocate the td so we don't
185 * have to tear things down in a failure case below.
186 * Here we copy out tid to two places, one for child and one
187 * for parent, because pthread can create a detached thread,
188 * if parent wants to safely access child tid, it has to provide
189 * its storage, because child thread may exit quickly and
190 * memory is freed before parent thread can access it.
192 if ((child_tid != NULL &&
193 suword_lwpid(child_tid, newtd->td_tid)) ||
194 (parent_tid != NULL &&
195 suword_lwpid(parent_tid, newtd->td_tid))) {
200 bzero(&newtd->td_startzero,
201 __rangeof(struct thread, td_startzero, td_endzero));
202 bzero(&newtd->td_rux, sizeof(newtd->td_rux));
203 newtd->td_map_def_user = NULL;
204 bcopy(&td->td_startcopy, &newtd->td_startcopy,
205 __rangeof(struct thread, td_startcopy, td_endcopy));
206 newtd->td_proc = td->td_proc;
207 newtd->td_ucred = crhold(td->td_ucred);
209 cpu_set_upcall(newtd, td);
211 if (ctx != NULL) { /* old way to set user context */
212 error = set_mcontext(newtd, ctx);
215 crfree(td->td_ucred);
219 /* Set up our machine context. */
220 stack.ss_sp = stack_base;
221 stack.ss_size = stack_size;
222 /* Set upcall address to user thread entry function. */
223 cpu_set_upcall_kse(newtd, start_func, arg, &stack);
224 /* Setup user TLS address and TLS pointer register. */
225 error = cpu_set_user_tls(newtd, tls_base);
228 crfree(td->td_ucred);
233 PROC_LOCK(td->td_proc);
234 td->td_proc->p_flag |= P_HADTHREADS;
235 newtd->td_sigmask = td->td_sigmask;
236 thread_link(newtd, p);
237 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
239 /* let the scheduler know about these things. */
240 sched_fork_thread(td, newtd);
243 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
247 if (!(td->td_pri_class == PRI_TIMESHARE &&
248 rtp->type == RTP_PRIO_NORMAL)) {
249 rtp_to_pri(rtp, newtd);
250 sched_prio(newtd, newtd->td_user_pri);
251 } /* ignore timesharing class */
253 TD_SET_CAN_RUN(newtd);
254 sched_add(newtd, SRQ_BORING);
255 thread_unlock(newtd);
261 thr_self(struct thread *td, struct thr_self_args *uap)
266 error = suword_lwpid(uap->id, (unsigned)td->td_tid);
273 thr_exit(struct thread *td, struct thr_exit_args *uap)
280 /* Signal userland that it can free the stack. */
281 if ((void *)uap->state != NULL) {
282 suword_lwpid(uap->state, 1);
283 kern_umtx_wake(td, uap->state, INT_MAX, 0);
291 * Shutting down last thread in the proc. This will actually
292 * call exit() in the trampoline when it returns.
294 if (p->p_numthreads != 1) {
305 thr_kill(struct thread *td, struct thr_kill_args *uap)
306 /* long id, int sig */
316 ksi.ksi_signo = uap->sig;
317 ksi.ksi_code = SI_USER;
318 ksi.ksi_pid = p->p_pid;
319 ksi.ksi_uid = td->td_ucred->cr_ruid;
322 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
326 FOREACH_THREAD_IN_PROC(p, ttd) {
331 tdsignal(p, ttd, uap->sig, &ksi);
336 if (uap->id != td->td_tid)
337 ttd = thread_find(p, uap->id);
342 else if (uap->sig == 0)
344 else if (!_SIG_VALID(uap->sig))
347 tdsignal(p, ttd, uap->sig, &ksi);
354 thr_kill2(struct thread *td, struct thr_kill2_args *uap)
355 /* pid_t pid, long id, int sig */
362 AUDIT_ARG_SIGNUM(uap->sig);
364 if (uap->pid == td->td_proc->p_pid) {
367 } else if ((p = pfind(uap->pid)) == NULL) {
370 AUDIT_ARG_PROCESS(p);
372 error = p_cansignal(td, p, uap->sig);
375 ksi.ksi_signo = uap->sig;
376 ksi.ksi_code = SI_USER;
377 ksi.ksi_pid = td->td_proc->p_pid;
378 ksi.ksi_uid = td->td_ucred->cr_ruid;
380 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
384 FOREACH_THREAD_IN_PROC(p, ttd) {
389 tdsignal(p, ttd, uap->sig,
395 if (uap->id != td->td_tid)
396 ttd = thread_find(p, uap->id);
401 else if (uap->sig == 0)
403 else if (!_SIG_VALID(uap->sig))
406 tdsignal(p, ttd, uap->sig, &ksi);
414 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
415 /* const struct timespec *timeout */
417 struct timespec ts, *tsp;
421 if (uap->timeout != NULL) {
422 error = copyin((const void *)uap->timeout, (void *)&ts,
423 sizeof(struct timespec));
429 return (kern_thr_suspend(td, tsp));
433 kern_thr_suspend(struct thread *td, struct timespec *tsp)
436 int error = 0, hz = 0;
439 if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
441 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
443 TIMESPEC_TO_TIMEVAL(&tv, tsp);
447 if (td->td_pflags & TDP_WAKEUP) {
448 td->td_pflags &= ~TDP_WAKEUP;
452 PROC_LOCK(td->td_proc);
453 if ((td->td_flags & TDF_THRWAKEUP) == 0)
454 error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
456 if (td->td_flags & TDF_THRWAKEUP) {
458 td->td_flags &= ~TDF_THRWAKEUP;
460 PROC_UNLOCK(td->td_proc);
463 PROC_UNLOCK(td->td_proc);
464 if (error == EWOULDBLOCK)
466 else if (error == ERESTART) {
474 thr_wake(struct thread *td, struct thr_wake_args *uap)
480 if (uap->id == td->td_tid) {
481 td->td_pflags |= TDP_WAKEUP;
487 ttd = thread_find(p, uap->id);
493 ttd->td_flags |= TDF_THRWAKEUP;
501 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
503 struct proc *p = td->td_proc;
504 char name[MAXCOMLEN + 1];
510 if (uap->name != NULL) {
511 error = copyinstr(uap->name, name, sizeof(name),
517 if (uap->id == td->td_tid)
520 ttd = thread_find(p, uap->id);
522 strcpy(ttd->td_name, name);