19454b2d8SWarner Losh /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni *
489bb1cefSJeff Roberson * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org>
589bb1cefSJeff Roberson * All rights reserved.
689bb1cefSJeff Roberson *
789bb1cefSJeff Roberson * Redistribution and use in source and binary forms, with or without
889bb1cefSJeff Roberson * modification, are permitted provided that the following conditions
989bb1cefSJeff Roberson * are met:
1089bb1cefSJeff Roberson * 1. Redistributions of source code must retain the above copyright
1189bb1cefSJeff Roberson * notice unmodified, this list of conditions, and the following
1289bb1cefSJeff Roberson * disclaimer.
1389bb1cefSJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright
1489bb1cefSJeff Roberson * notice, this list of conditions and the following disclaimer in the
1589bb1cefSJeff Roberson * documentation and/or other materials provided with the distribution.
1689bb1cefSJeff Roberson *
1789bb1cefSJeff Roberson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1889bb1cefSJeff Roberson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1989bb1cefSJeff Roberson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2089bb1cefSJeff Roberson * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2189bb1cefSJeff Roberson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2289bb1cefSJeff Roberson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2389bb1cefSJeff Roberson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2489bb1cefSJeff Roberson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2589bb1cefSJeff Roberson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2689bb1cefSJeff Roberson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2789bb1cefSJeff Roberson */
2889bb1cefSJeff Roberson
2960088160SDavid Xu #include "opt_posix.h"
306161b98cSMatt Macy #include "opt_hwpmc_hooks.h"
312effad53SKonstantin Belousov
32*0c11c179SKonstantin Belousov #include <sys/systm.h>
3389bb1cefSJeff Roberson #include <sys/kernel.h>
34*0c11c179SKonstantin Belousov #include <sys/limits.h>
3589bb1cefSJeff Roberson #include <sys/lock.h>
3689bb1cefSJeff Roberson #include <sys/mutex.h>
37acd3428bSRobert Watson #include <sys/priv.h>
3889bb1cefSJeff Roberson #include <sys/proc.h>
39c4f7f0fdSTom Rhodes #include <sys/posix4.h>
408d570f64SJohn Baldwin #include <sys/ptrace.h>
4158c77a9dSEdward Tomasz Napierala #include <sys/racct.h>
4289bb1cefSJeff Roberson #include <sys/resourcevar.h>
43*0c11c179SKonstantin Belousov #include <sys/rtprio.h>
440d036d55SDavid Xu #include <sys/rwlock.h>
45a22ec9d8SJeff Roberson #include <sys/sched.h>
46a8b491c1SJulian Elischer #include <sys/sysctl.h>
47ed062c8dSJulian Elischer #include <sys/smp.h>
48a66fde8dSJohn Baldwin #include <sys/syscallsubr.h>
4989bb1cefSJeff Roberson #include <sys/sysent.h>
5089bb1cefSJeff Roberson #include <sys/sysproto.h>
5189bb1cefSJeff Roberson #include <sys/signalvar.h>
5225a9cfc9SKonstantin Belousov #include <sys/sysctl.h>
5389bb1cefSJeff Roberson #include <sys/thr.h>
54*0c11c179SKonstantin Belousov #include <sys/ucontext.h>
55af29f399SDmitry Chagin #include <sys/umtxvar.h>
566161b98cSMatt Macy #ifdef HWPMC_HOOKS
576161b98cSMatt Macy #include <sys/pmckern.h>
586161b98cSMatt Macy #endif
5989bb1cefSJeff Roberson
6089bb1cefSJeff Roberson #include <machine/frame.h>
6189bb1cefSJeff Roberson
620b1f0611SDavid Xu #include <security/audit/audit.h>
630b1f0611SDavid Xu
647029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
656472ac3dSEd Schouten "thread allocation");
6625a9cfc9SKonstantin Belousov
671bd3cf5dSMateusz Guzik int max_threads_per_proc = 1500;
6825a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW,
6925a9cfc9SKonstantin Belousov &max_threads_per_proc, 0, "Limit on threads per proc");
7025a9cfc9SKonstantin Belousov
7125a9cfc9SKonstantin Belousov static int max_threads_hits;
7225a9cfc9SKonstantin Belousov SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD,
733eb9ab52SEitan Adler &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count");
7425a9cfc9SKonstantin Belousov
75841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
76cda9a0d1SDavid Xu
77cda9a0d1SDavid Xu static inline int
suword_lwpid(void * addr,lwpid_t lwpid)78cda9a0d1SDavid Xu suword_lwpid(void *addr, lwpid_t lwpid)
79cda9a0d1SDavid Xu {
80cda9a0d1SDavid Xu int error;
81cda9a0d1SDavid Xu
82b4cf0e62SKonstantin Belousov if (SV_CURPROC_FLAG(SV_LP64))
83cda9a0d1SDavid Xu error = suword(addr, lwpid);
84cda9a0d1SDavid Xu else
85cda9a0d1SDavid Xu error = suword32(addr, lwpid);
86cda9a0d1SDavid Xu return (error);
87cda9a0d1SDavid Xu }
88cda9a0d1SDavid Xu
89cda9a0d1SDavid Xu #else
90cda9a0d1SDavid Xu #define suword_lwpid suword
91cda9a0d1SDavid Xu #endif
92cda9a0d1SDavid Xu
9389bb1cefSJeff Roberson /*
9489bb1cefSJeff Roberson * System call interface.
9589bb1cefSJeff Roberson */
965a170c1bSEd Schouten
975a170c1bSEd Schouten struct thr_create_initthr_args {
985a170c1bSEd Schouten ucontext_t ctx;
995a170c1bSEd Schouten long *tid;
1005a170c1bSEd Schouten };
1015a170c1bSEd Schouten
1025a170c1bSEd Schouten static int
thr_create_initthr(struct thread * td,void * thunk)1035a170c1bSEd Schouten thr_create_initthr(struct thread *td, void *thunk)
1045a170c1bSEd Schouten {
1055a170c1bSEd Schouten struct thr_create_initthr_args *args;
1065a170c1bSEd Schouten
1075a170c1bSEd Schouten /* Copy out the child tid. */
1085a170c1bSEd Schouten args = thunk;
1095a170c1bSEd Schouten if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid))
1105a170c1bSEd Schouten return (EFAULT);
1115a170c1bSEd Schouten
1125a170c1bSEd Schouten return (set_mcontext(td, &args->ctx.uc_mcontext));
1135a170c1bSEd Schouten }
1145a170c1bSEd Schouten
11589bb1cefSJeff Roberson int
sys_thr_create(struct thread * td,struct thr_create_args * uap)1168451d0ddSKip Macy sys_thr_create(struct thread *td, struct thr_create_args *uap)
117cd28f17dSMarcel Moolenaar /* ucontext_t *ctx, long *id, int flags */
11889bb1cefSJeff Roberson {
1195a170c1bSEd Schouten struct thr_create_initthr_args args;
12089bb1cefSJeff Roberson int error;
12189bb1cefSJeff Roberson
1225a170c1bSEd Schouten if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx))))
12389bb1cefSJeff Roberson return (error);
1245a170c1bSEd Schouten args.tid = uap->id;
1255a170c1bSEd Schouten return (thread_create(td, NULL, thr_create_initthr, &args));
126c4bd610fSDavid Xu }
127c4bd610fSDavid Xu
128c4bd610fSDavid Xu int
sys_thr_new(struct thread * td,struct thr_new_args * uap)1298451d0ddSKip Macy sys_thr_new(struct thread *td, struct thr_new_args *uap)
130c4bd610fSDavid Xu /* struct thr_param * */
131c4bd610fSDavid Xu {
132c4bd610fSDavid Xu struct thr_param param;
133cda9a0d1SDavid Xu int error;
134cda9a0d1SDavid Xu
135cda9a0d1SDavid Xu if (uap->param_size < 0 || uap->param_size > sizeof(param))
136cda9a0d1SDavid Xu return (EINVAL);
137cda9a0d1SDavid Xu bzero(¶m, sizeof(param));
138cda9a0d1SDavid Xu if ((error = copyin(uap->param, ¶m, uap->param_size)))
139cda9a0d1SDavid Xu return (error);
140cda9a0d1SDavid Xu return (kern_thr_new(td, ¶m));
141cda9a0d1SDavid Xu }
142cda9a0d1SDavid Xu
1435a170c1bSEd Schouten static int
thr_new_initthr(struct thread * td,void * thunk)1445a170c1bSEd Schouten thr_new_initthr(struct thread *td, void *thunk)
1455a170c1bSEd Schouten {
1465a170c1bSEd Schouten stack_t stack;
1475a170c1bSEd Schouten struct thr_param *param;
1487b68fb5aSMark Johnston int error;
1495a170c1bSEd Schouten
1505a170c1bSEd Schouten /*
1515a170c1bSEd Schouten * Here we copy out tid to two places, one for child and one
1525a170c1bSEd Schouten * for parent, because pthread can create a detached thread,
1535a170c1bSEd Schouten * if parent wants to safely access child tid, it has to provide
1545a170c1bSEd Schouten * its storage, because child thread may exit quickly and
1555a170c1bSEd Schouten * memory is freed before parent thread can access it.
1565a170c1bSEd Schouten */
1575a170c1bSEd Schouten param = thunk;
1585a170c1bSEd Schouten if ((param->child_tid != NULL &&
1595a170c1bSEd Schouten suword_lwpid(param->child_tid, td->td_tid)) ||
1605a170c1bSEd Schouten (param->parent_tid != NULL &&
1615a170c1bSEd Schouten suword_lwpid(param->parent_tid, td->td_tid)))
1625a170c1bSEd Schouten return (EFAULT);
1635a170c1bSEd Schouten
1645a170c1bSEd Schouten /* Set up our machine context. */
1655a170c1bSEd Schouten stack.ss_sp = param->stack_base;
1665a170c1bSEd Schouten stack.ss_size = param->stack_size;
1675a170c1bSEd Schouten /* Set upcall address to user thread entry function. */
1687b68fb5aSMark Johnston error = cpu_set_upcall(td, param->start_func, param->arg, &stack);
1697b68fb5aSMark Johnston if (error != 0)
1707b68fb5aSMark Johnston return (error);
1715a170c1bSEd Schouten /* Setup user TLS address and TLS pointer register. */
1725a170c1bSEd Schouten return (cpu_set_user_tls(td, param->tls_base));
1735a170c1bSEd Schouten }
1745a170c1bSEd Schouten
175cda9a0d1SDavid Xu int
kern_thr_new(struct thread * td,struct thr_param * param)176cda9a0d1SDavid Xu kern_thr_new(struct thread *td, struct thr_param *param)
177cda9a0d1SDavid Xu {
17873fa3e5bSDavid Xu struct rtprio rtp, *rtpp;
179c4bd610fSDavid Xu int error;
180c4bd610fSDavid Xu
18173fa3e5bSDavid Xu rtpp = NULL;
182cda9a0d1SDavid Xu if (param->rtp != 0) {
183cda9a0d1SDavid Xu error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
184e4866772SRoman Divacky if (error)
185e4866772SRoman Divacky return (error);
18673fa3e5bSDavid Xu rtpp = &rtp;
187a0712c99SDavid Xu }
1885a170c1bSEd Schouten return (thread_create(td, rtpp, thr_new_initthr, param));
189c4bd610fSDavid Xu }
190c4bd610fSDavid Xu
1915a170c1bSEd Schouten int
thread_create(struct thread * td,struct rtprio * rtp,int (* initialize_thread)(struct thread *,void *),void * thunk)1925a170c1bSEd Schouten thread_create(struct thread *td, struct rtprio *rtp,
1935a170c1bSEd Schouten int (*initialize_thread)(struct thread *, void *), void *thunk)
194c4bd610fSDavid Xu {
195c4bd610fSDavid Xu struct thread *newtd;
196c4bd610fSDavid Xu struct proc *p;
197adc9c950SDavid Xu int error;
198c4bd610fSDavid Xu
199c4bd610fSDavid Xu p = td->td_proc;
200c4bd610fSDavid Xu
20173fa3e5bSDavid Xu if (rtp != NULL) {
20273fa3e5bSDavid Xu switch(rtp->type) {
20373fa3e5bSDavid Xu case RTP_PRIO_REALTIME:
20473fa3e5bSDavid Xu case RTP_PRIO_FIFO:
205a0712c99SDavid Xu /* Only root can set scheduler policy */
206acd3428bSRobert Watson if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
207a0712c99SDavid Xu return (EPERM);
20873fa3e5bSDavid Xu if (rtp->prio > RTP_PRIO_MAX)
209a0712c99SDavid Xu return (EINVAL);
2102dca4ca7SDavid Xu break;
21173fa3e5bSDavid Xu case RTP_PRIO_NORMAL:
21273fa3e5bSDavid Xu rtp->prio = 0;
2132dca4ca7SDavid Xu break;
2142dca4ca7SDavid Xu default:
2152dca4ca7SDavid Xu return (EINVAL);
216a0712c99SDavid Xu }
217a0712c99SDavid Xu }
218a0712c99SDavid Xu
219afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2204b5c9cf6SEdward Tomasz Napierala if (racct_enable) {
221203322f9SMateusz Guzik PROC_LOCK(p);
22258c77a9dSEdward Tomasz Napierala error = racct_add(p, RACCT_NTHR, 1);
223203322f9SMateusz Guzik PROC_UNLOCK(p);
22458c77a9dSEdward Tomasz Napierala if (error != 0)
22558c77a9dSEdward Tomasz Napierala return (EPROCLIM);
2264b5c9cf6SEdward Tomasz Napierala }
227afcc55f3SEdward Tomasz Napierala #endif
22858c77a9dSEdward Tomasz Napierala
229ad1e7d28SJulian Elischer /* Initialize our td */
23009baafb4SDmitry Chagin error = kern_thr_alloc(p, 0, &newtd);
23109baafb4SDmitry Chagin if (error)
23258c77a9dSEdward Tomasz Napierala goto fail;
233c4bd610fSDavid Xu
234ed062c8dSJulian Elischer bzero(&newtd->td_startzero,
2356db36923SDavid Schultz __rangeof(struct thread, td_startzero, td_endzero));
236ed062c8dSJulian Elischer bcopy(&td->td_startcopy, &newtd->td_startcopy,
2376db36923SDavid Schultz __rangeof(struct thread, td_startcopy, td_endcopy));
238c4bd610fSDavid Xu newtd->td_proc = td->td_proc;
2398a06de9eSKonstantin Belousov newtd->td_rb_list = newtd->td_rbp_list = newtd->td_rb_inact = 0;
2404ea6a9a2SMateusz Guzik thread_cow_get(newtd, td);
24189bb1cefSJeff Roberson
2421b0a4974SKonstantin Belousov cpu_copy_thread(newtd, td);
2431b0a4974SKonstantin Belousov
2445a170c1bSEd Schouten error = initialize_thread(newtd, thunk);
245c4bd610fSDavid Xu if (error != 0) {
2464ea6a9a2SMateusz Guzik thread_cow_free(newtd);
247c4bd610fSDavid Xu thread_free(newtd);
24858c77a9dSEdward Tomasz Napierala goto fail;
249c4bd610fSDavid Xu }
250c4bd610fSDavid Xu
251203322f9SMateusz Guzik PROC_LOCK(p);
252203322f9SMateusz Guzik p->p_flag |= P_HADTHREADS;
2538460a577SJohn Birrell thread_link(newtd, p);
254c67ddc21SJulian Elischer bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
255982d11f8SJeff Roberson thread_lock(td);
256ed062c8dSJulian Elischer /* let the scheduler know about these things. */
257ed062c8dSJulian Elischer sched_fork_thread(td, newtd);
258982d11f8SJeff Roberson thread_unlock(td);
259b7edba77SJeff Roberson if (P_SHOULDSTOP(p))
260c6d31b83SKonstantin Belousov ast_sched(newtd, TDA_SUSPEND);
2618d570f64SJohn Baldwin if (p->p_ptevents & PTRACE_LWP)
2625fcfab6eSJohn Baldwin newtd->td_dbgflags |= TDB_BORN;
2636520495aSAdrian Chadd
264982d11f8SJeff Roberson PROC_UNLOCK(p);
2656161b98cSMatt Macy #ifdef HWPMC_HOOKS
2666161b98cSMatt Macy if (PMC_PROC_IS_USING_PMCS(p))
2676161b98cSMatt Macy PMC_CALL_HOOK(newtd, PMC_FN_THR_CREATE, NULL);
268ebfaf69cSMatt Macy else if (PMC_SYSTEM_SAMPLING_ACTIVE())
269ebfaf69cSMatt Macy PMC_CALL_HOOK_UNLOCKED(newtd, PMC_FN_THR_CREATE_LOG, NULL);
2706161b98cSMatt Macy #endif
271cf7d9a8cSDavid Xu
272cf7d9a8cSDavid Xu tidhash_add(newtd);
273cf7d9a8cSDavid Xu
274c8ea36e8SJeff Roberson /* ignore timesharing class */
275c8ea36e8SJeff Roberson if (rtp != NULL && !(td->td_pri_class == PRI_TIMESHARE &&
276c8ea36e8SJeff Roberson rtp->type == RTP_PRIO_NORMAL))
2778460a577SJohn Birrell rtp_to_pri(rtp, newtd);
278c8ea36e8SJeff Roberson
279c8ea36e8SJeff Roberson thread_lock(newtd);
280ed062c8dSJulian Elischer TD_SET_CAN_RUN(newtd);
281f0393f06SJeff Roberson sched_add(newtd, SRQ_BORING);
28289bb1cefSJeff Roberson
283c90c9021SEd Schouten return (0);
28458c77a9dSEdward Tomasz Napierala
28558c77a9dSEdward Tomasz Napierala fail:
286afcc55f3SEdward Tomasz Napierala #ifdef RACCT
2874b5c9cf6SEdward Tomasz Napierala if (racct_enable) {
28858c77a9dSEdward Tomasz Napierala PROC_LOCK(p);
28958c77a9dSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1);
29058c77a9dSEdward Tomasz Napierala PROC_UNLOCK(p);
2914b5c9cf6SEdward Tomasz Napierala }
292afcc55f3SEdward Tomasz Napierala #endif
29358c77a9dSEdward Tomasz Napierala return (error);
29489bb1cefSJeff Roberson }
29589bb1cefSJeff Roberson
29689bb1cefSJeff Roberson int
sys_thr_self(struct thread * td,struct thr_self_args * uap)2978451d0ddSKip Macy sys_thr_self(struct thread *td, struct thr_self_args *uap)
298cd28f17dSMarcel Moolenaar /* long *id */
29989bb1cefSJeff Roberson {
30089bb1cefSJeff Roberson int error;
30189bb1cefSJeff Roberson
302cda9a0d1SDavid Xu error = suword_lwpid(uap->id, (unsigned)td->td_tid);
303cda9a0d1SDavid Xu if (error == -1)
304cda9a0d1SDavid Xu return (EFAULT);
30589bb1cefSJeff Roberson return (0);
30689bb1cefSJeff Roberson }
30789bb1cefSJeff Roberson
30889bb1cefSJeff Roberson int
sys_thr_exit(struct thread * td,struct thr_exit_args * uap)3098451d0ddSKip Macy sys_thr_exit(struct thread *td, struct thr_exit_args *uap)
310401901acSMike Makonnen /* long *state */
31189bb1cefSJeff Roberson {
31289bb1cefSJeff Roberson
3132a339d9eSKonstantin Belousov umtx_thread_exit(td);
3142a339d9eSKonstantin Belousov
315401901acSMike Makonnen /* Signal userland that it can free the stack. */
3164938faa6SDavid Xu if ((void *)uap->state != NULL) {
317f450277fSMark Johnston (void)suword_lwpid(uap->state, 1);
318f450277fSMark Johnston (void)kern_umtx_wake(td, uap->state, INT_MAX, 0);
3194938faa6SDavid Xu }
320401901acSMike Makonnen
32195be6d2bSDmitry Chagin return (kern_thr_exit(td));
32295be6d2bSDmitry Chagin }
32358c77a9dSEdward Tomasz Napierala
32495be6d2bSDmitry Chagin int
kern_thr_exit(struct thread * td)32595be6d2bSDmitry Chagin kern_thr_exit(struct thread *td)
32695be6d2bSDmitry Chagin {
32795be6d2bSDmitry Chagin struct proc *p;
32895be6d2bSDmitry Chagin
32995be6d2bSDmitry Chagin p = td->td_proc;
33095be6d2bSDmitry Chagin
3315fcfab6eSJohn Baldwin /*
3325fcfab6eSJohn Baldwin * If all of the threads in a process call this routine to
3335fcfab6eSJohn Baldwin * exit (e.g. all threads call pthread_exit()), exactly one
3345fcfab6eSJohn Baldwin * thread should return to the caller to terminate the process
3355fcfab6eSJohn Baldwin * instead of the thread.
3365fcfab6eSJohn Baldwin *
3375fcfab6eSJohn Baldwin * Checking p_numthreads alone is not sufficient since threads
3385fcfab6eSJohn Baldwin * might be committed to terminating while the PROC_LOCK is
3395fcfab6eSJohn Baldwin * dropped in either ptracestop() or while removing this thread
3405fcfab6eSJohn Baldwin * from the tidhash. Instead, the p_pendingexits field holds
3415fcfab6eSJohn Baldwin * the count of threads in either of those states and a thread
3425fcfab6eSJohn Baldwin * is considered the "last" thread if all of the other threads
3435fcfab6eSJohn Baldwin * in a process are already terminating.
3445fcfab6eSJohn Baldwin */
34589bb1cefSJeff Roberson PROC_LOCK(p);
3465fcfab6eSJohn Baldwin if (p->p_numthreads == p->p_pendingexits + 1) {
3475fcfab6eSJohn Baldwin /*
3485fcfab6eSJohn Baldwin * Ignore attempts to shut down last thread in the
3495fcfab6eSJohn Baldwin * proc. This will actually call _exit(2) in the
3505fcfab6eSJohn Baldwin * usermode trampoline when it returns.
3515fcfab6eSJohn Baldwin */
3525fcfab6eSJohn Baldwin PROC_UNLOCK(p);
3535fcfab6eSJohn Baldwin return (0);
3545fcfab6eSJohn Baldwin }
35558c77a9dSEdward Tomasz Napierala
35687a9b18dSKonstantin Belousov if (p->p_sysent->sv_ontdexit != NULL)
35787a9b18dSKonstantin Belousov p->p_sysent->sv_ontdexit(td);
35887a9b18dSKonstantin Belousov
3595fcfab6eSJohn Baldwin td->td_dbgflags |= TDB_EXIT;
360aae3547bSMateusz Guzik if (p->p_ptevents & PTRACE_LWP) {
361aae3547bSMateusz Guzik p->p_pendingexits++;
36282a4538fSEric Badger ptracestop(td, SIGTRAP, NULL);
3635fcfab6eSJohn Baldwin p->p_pendingexits--;
364aae3547bSMateusz Guzik }
365aae3547bSMateusz Guzik tidhash_remove(td);
3665fcfab6eSJohn Baldwin
3675fcfab6eSJohn Baldwin /*
3685fcfab6eSJohn Baldwin * The check above should prevent all other threads from this
3695fcfab6eSJohn Baldwin * process from exiting while the PROC_LOCK is dropped, so
3705fcfab6eSJohn Baldwin * there must be at least one other thread other than the
3715fcfab6eSJohn Baldwin * current thread.
3725fcfab6eSJohn Baldwin */
3735fcfab6eSJohn Baldwin KASSERT(p->p_numthreads > 1, ("too few threads"));
37447f6635cSEdward Tomasz Napierala racct_sub(p, RACCT_NTHR, 1);
3750d036d55SDavid Xu tdsigcleanup(td);
3768a894c1aSAlan Somers
3778a894c1aSAlan Somers #ifdef AUDIT
3788a894c1aSAlan Somers AUDIT_SYSCALL_EXIT(0, td);
3798a894c1aSAlan Somers #endif
3808a894c1aSAlan Somers
3810d036d55SDavid Xu PROC_SLOCK(p);
38271b7afb2SDavid Xu thread_stopped(p);
383ed062c8dSJulian Elischer thread_exit();
384ed062c8dSJulian Elischer /* NOTREACHED */
385ed062c8dSJulian Elischer }
38674d5b4afSKonstantin Belousov
38789bb1cefSJeff Roberson int
sys_thr_kill(struct thread * td,struct thr_kill_args * uap)3888451d0ddSKip Macy sys_thr_kill(struct thread *td, struct thr_kill_args *uap)
389cd28f17dSMarcel Moolenaar /* long id, int sig */
39089bb1cefSJeff Roberson {
3915f73a7ebSBruno Ducrot ksiginfo_t ksi;
39289bb1cefSJeff Roberson struct thread *ttd;
39389bb1cefSJeff Roberson struct proc *p;
39489bb1cefSJeff Roberson int error;
39589bb1cefSJeff Roberson
39689bb1cefSJeff Roberson p = td->td_proc;
3975f73a7ebSBruno Ducrot ksiginfo_init(&ksi);
3985f73a7ebSBruno Ducrot ksi.ksi_signo = uap->sig;
399baf28b69SDavid Xu ksi.ksi_code = SI_LWP;
4005f73a7ebSBruno Ducrot ksi.ksi_pid = p->p_pid;
4015f73a7ebSBruno Ducrot ksi.ksi_uid = td->td_ucred->cr_ruid;
4020a5cd498SDavid Xu if (uap->id == -1) {
4030a5cd498SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
40489bb1cefSJeff Roberson error = EINVAL;
4050a5cd498SDavid Xu } else {
4060a5cd498SDavid Xu error = ESRCH;
407cf7d9a8cSDavid Xu PROC_LOCK(p);
4080a5cd498SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) {
4090a5cd498SDavid Xu if (ttd != td) {
4100a5cd498SDavid Xu error = 0;
4110a5cd498SDavid Xu if (uap->sig == 0)
4120a5cd498SDavid Xu break;
413ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi);
4140a5cd498SDavid Xu }
4150a5cd498SDavid Xu }
416cf7d9a8cSDavid Xu PROC_UNLOCK(p);
4170a5cd498SDavid Xu }
4180a5cd498SDavid Xu } else {
419cf7d9a8cSDavid Xu error = 0;
420cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid);
4210a5cd498SDavid Xu if (ttd == NULL)
422cf7d9a8cSDavid Xu return (ESRCH);
423cf7d9a8cSDavid Xu if (uap->sig == 0)
4240a5cd498SDavid Xu ;
4250a5cd498SDavid Xu else if (!_SIG_VALID(uap->sig))
4260a5cd498SDavid Xu error = EINVAL;
4270a5cd498SDavid Xu else
428ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi);
429cf7d9a8cSDavid Xu PROC_UNLOCK(ttd->td_proc);
4300a5cd498SDavid Xu }
43189bb1cefSJeff Roberson return (error);
43289bb1cefSJeff Roberson }
4331713a516SMike Makonnen
4341713a516SMike Makonnen int
sys_thr_kill2(struct thread * td,struct thr_kill2_args * uap)4358451d0ddSKip Macy sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap)
4360b1f0611SDavid Xu /* pid_t pid, long id, int sig */
4370b1f0611SDavid Xu {
4385f73a7ebSBruno Ducrot ksiginfo_t ksi;
4390b1f0611SDavid Xu struct thread *ttd;
4400b1f0611SDavid Xu struct proc *p;
4410b1f0611SDavid Xu int error;
4420b1f0611SDavid Xu
44314961ba7SRobert Watson AUDIT_ARG_SIGNUM(uap->sig);
4440b1f0611SDavid Xu
4455f73a7ebSBruno Ducrot ksiginfo_init(&ksi);
4465f73a7ebSBruno Ducrot ksi.ksi_signo = uap->sig;
447baf28b69SDavid Xu ksi.ksi_code = SI_LWP;
4485f73a7ebSBruno Ducrot ksi.ksi_pid = td->td_proc->p_pid;
4495f73a7ebSBruno Ducrot ksi.ksi_uid = td->td_ucred->cr_ruid;
4500b1f0611SDavid Xu if (uap->id == -1) {
451cf7d9a8cSDavid Xu if ((p = pfind(uap->pid)) == NULL)
452cf7d9a8cSDavid Xu return (ESRCH);
453cf7d9a8cSDavid Xu AUDIT_ARG_PROCESS(p);
454cf7d9a8cSDavid Xu error = p_cansignal(td, p, uap->sig);
455cf7d9a8cSDavid Xu if (error) {
456cf7d9a8cSDavid Xu PROC_UNLOCK(p);
457cf7d9a8cSDavid Xu return (error);
458cf7d9a8cSDavid Xu }
4590b1f0611SDavid Xu if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
4600b1f0611SDavid Xu error = EINVAL;
4610b1f0611SDavid Xu } else {
4620b1f0611SDavid Xu error = ESRCH;
4630b1f0611SDavid Xu FOREACH_THREAD_IN_PROC(p, ttd) {
4640b1f0611SDavid Xu if (ttd != td) {
4650b1f0611SDavid Xu error = 0;
4660b1f0611SDavid Xu if (uap->sig == 0)
4670b1f0611SDavid Xu break;
468ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi);
4690b1f0611SDavid Xu }
4700b1f0611SDavid Xu }
4710b1f0611SDavid Xu }
472cf7d9a8cSDavid Xu PROC_UNLOCK(p);
4730b1f0611SDavid Xu } else {
474cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, uap->pid);
4750b1f0611SDavid Xu if (ttd == NULL)
476cf7d9a8cSDavid Xu return (ESRCH);
477cf7d9a8cSDavid Xu p = ttd->td_proc;
478cf7d9a8cSDavid Xu AUDIT_ARG_PROCESS(p);
479cf7d9a8cSDavid Xu error = p_cansignal(td, p, uap->sig);
480cf7d9a8cSDavid Xu if (uap->sig == 0)
4810b1f0611SDavid Xu ;
4820b1f0611SDavid Xu else if (!_SIG_VALID(uap->sig))
4830b1f0611SDavid Xu error = EINVAL;
4840b1f0611SDavid Xu else
485ad6eec7bSJohn Baldwin tdksignal(ttd, uap->sig, &ksi);
4860b1f0611SDavid Xu PROC_UNLOCK(p);
487cf7d9a8cSDavid Xu }
4880b1f0611SDavid Xu return (error);
4890b1f0611SDavid Xu }
4900b1f0611SDavid Xu
4910b1f0611SDavid Xu int
sys_thr_suspend(struct thread * td,struct thr_suspend_args * uap)4928451d0ddSKip Macy sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap)
4931713a516SMike Makonnen /* const struct timespec *timeout */
4941713a516SMike Makonnen {
495cda9a0d1SDavid Xu struct timespec ts, *tsp;
4961713a516SMike Makonnen int error;
4971713a516SMike Makonnen
498cda9a0d1SDavid Xu tsp = NULL;
4991713a516SMike Makonnen if (uap->timeout != NULL) {
5009a1d0cf6SPeter Holm error = umtx_copyin_timeout(uap->timeout, &ts);
5011713a516SMike Makonnen if (error != 0)
5021713a516SMike Makonnen return (error);
503cda9a0d1SDavid Xu tsp = &ts;
504cda9a0d1SDavid Xu }
505cda9a0d1SDavid Xu
506cda9a0d1SDavid Xu return (kern_thr_suspend(td, tsp));
507cda9a0d1SDavid Xu }
508cda9a0d1SDavid Xu
509cda9a0d1SDavid Xu int
kern_thr_suspend(struct thread * td,struct timespec * tsp)510cda9a0d1SDavid Xu kern_thr_suspend(struct thread *td, struct timespec *tsp)
511cda9a0d1SDavid Xu {
512cfca8a18SDavid Xu struct proc *p = td->td_proc;
513cda9a0d1SDavid Xu struct timeval tv;
5142961a782SDavid Xu int error = 0;
5152961a782SDavid Xu int timo = 0;
516cda9a0d1SDavid Xu
517745fbd3aSDavid Xu if (td->td_pflags & TDP_WAKEUP) {
518745fbd3aSDavid Xu td->td_pflags &= ~TDP_WAKEUP;
519745fbd3aSDavid Xu return (0);
520745fbd3aSDavid Xu }
521745fbd3aSDavid Xu
522cfca8a18SDavid Xu if (tsp != NULL) {
5232961a782SDavid Xu if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
5242961a782SDavid Xu error = EWOULDBLOCK;
5252961a782SDavid Xu else {
5262961a782SDavid Xu TIMESPEC_TO_TIMEVAL(&tv, tsp);
5272961a782SDavid Xu timo = tvtohz(&tv);
528cfca8a18SDavid Xu }
529cfca8a18SDavid Xu }
530cfca8a18SDavid Xu
531cfca8a18SDavid Xu PROC_LOCK(p);
532cfca8a18SDavid Xu if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0)
533cfca8a18SDavid Xu error = msleep((void *)td, &p->p_mtx,
5342961a782SDavid Xu PCATCH, "lthr", timo);
5352961a782SDavid Xu
536c1df5a1aSDavid Xu if (td->td_flags & TDF_THRWAKEUP) {
537982d11f8SJeff Roberson thread_lock(td);
5381713a516SMike Makonnen td->td_flags &= ~TDF_THRWAKEUP;
539982d11f8SJeff Roberson thread_unlock(td);
540cfca8a18SDavid Xu PROC_UNLOCK(p);
541c1df5a1aSDavid Xu return (0);
542c1df5a1aSDavid Xu }
543cfca8a18SDavid Xu PROC_UNLOCK(p);
544c1df5a1aSDavid Xu if (error == EWOULDBLOCK)
545c1df5a1aSDavid Xu error = ETIMEDOUT;
546c1df5a1aSDavid Xu else if (error == ERESTART) {
5472961a782SDavid Xu if (timo != 0)
548c1df5a1aSDavid Xu error = EINTR;
549c1df5a1aSDavid Xu }
550c1df5a1aSDavid Xu return (error);
5511713a516SMike Makonnen }
5521713a516SMike Makonnen
5531713a516SMike Makonnen int
sys_thr_wake(struct thread * td,struct thr_wake_args * uap)5548451d0ddSKip Macy sys_thr_wake(struct thread *td, struct thr_wake_args *uap)
555cd28f17dSMarcel Moolenaar /* long id */
5561713a516SMike Makonnen {
55744355392SDavid Xu struct proc *p;
558cd28f17dSMarcel Moolenaar struct thread *ttd;
5591713a516SMike Makonnen
560745fbd3aSDavid Xu if (uap->id == td->td_tid) {
561745fbd3aSDavid Xu td->td_pflags |= TDP_WAKEUP;
562745fbd3aSDavid Xu return (0);
563745fbd3aSDavid Xu }
564745fbd3aSDavid Xu
56544355392SDavid Xu p = td->td_proc;
566cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid);
567cf7d9a8cSDavid Xu if (ttd == NULL)
5681713a516SMike Makonnen return (ESRCH);
569982d11f8SJeff Roberson thread_lock(ttd);
570cd28f17dSMarcel Moolenaar ttd->td_flags |= TDF_THRWAKEUP;
571982d11f8SJeff Roberson thread_unlock(ttd);
572c1df5a1aSDavid Xu wakeup((void *)ttd);
57344355392SDavid Xu PROC_UNLOCK(p);
5741713a516SMike Makonnen return (0);
5751713a516SMike Makonnen }
5769e7d7224SDavid Xu
5779e7d7224SDavid Xu int
sys_thr_set_name(struct thread * td,struct thr_set_name_args * uap)5788451d0ddSKip Macy sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap)
5799e7d7224SDavid Xu {
580cf7d9a8cSDavid Xu struct proc *p;
5819e7d7224SDavid Xu char name[MAXCOMLEN + 1];
5829e7d7224SDavid Xu struct thread *ttd;
5839e7d7224SDavid Xu int error;
5849e7d7224SDavid Xu
5859e7d7224SDavid Xu error = 0;
5869e7d7224SDavid Xu name[0] = '\0';
5879e7d7224SDavid Xu if (uap->name != NULL) {
588ff07dd91SEric van Gyzen error = copyinstr(uap->name, name, sizeof(name), NULL);
589ff07dd91SEric van Gyzen if (error == ENAMETOOLONG) {
590ff07dd91SEric van Gyzen error = copyin(uap->name, name, sizeof(name) - 1);
591ff07dd91SEric van Gyzen name[sizeof(name) - 1] = '\0';
592ff07dd91SEric van Gyzen }
5939e7d7224SDavid Xu if (error)
5949e7d7224SDavid Xu return (error);
5959e7d7224SDavid Xu }
596cf7d9a8cSDavid Xu p = td->td_proc;
597cf7d9a8cSDavid Xu ttd = tdfind((lwpid_t)uap->id, p->p_pid);
598cf7d9a8cSDavid Xu if (ttd == NULL)
599cf7d9a8cSDavid Xu return (ESRCH);
6009e7d7224SDavid Xu strcpy(ttd->td_name, name);
601ebfaf69cSMatt Macy #ifdef HWPMC_HOOKS
602ebfaf69cSMatt Macy if (PMC_PROC_IS_USING_PMCS(p) || PMC_SYSTEM_SAMPLING_ACTIVE())
603ebfaf69cSMatt Macy PMC_CALL_HOOK_UNLOCKED(ttd, PMC_FN_THR_CREATE_LOG, NULL);
604ebfaf69cSMatt Macy #endif
60544ad5475SJohn Baldwin #ifdef KTR
60644ad5475SJohn Baldwin sched_clear_tdname(ttd);
60744ad5475SJohn Baldwin #endif
6089e7d7224SDavid Xu PROC_UNLOCK(p);
6099e7d7224SDavid Xu return (error);
6109e7d7224SDavid Xu }
61109baafb4SDmitry Chagin
61209baafb4SDmitry Chagin int
kern_thr_alloc(struct proc * p,int pages,struct thread ** ntd)61309baafb4SDmitry Chagin kern_thr_alloc(struct proc *p, int pages, struct thread **ntd)
61409baafb4SDmitry Chagin {
61509baafb4SDmitry Chagin
61609baafb4SDmitry Chagin /* Have race condition but it is cheap. */
61709baafb4SDmitry Chagin if (p->p_numthreads >= max_threads_per_proc) {
61809baafb4SDmitry Chagin ++max_threads_hits;
61909baafb4SDmitry Chagin return (EPROCLIM);
62009baafb4SDmitry Chagin }
62109baafb4SDmitry Chagin
62209baafb4SDmitry Chagin *ntd = thread_alloc(pages);
62309baafb4SDmitry Chagin if (*ntd == NULL)
62409baafb4SDmitry Chagin return (ENOMEM);
62509baafb4SDmitry Chagin
62609baafb4SDmitry Chagin return (0);
62709baafb4SDmitry Chagin }
628