1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_compat.h" 33 #include "opt_posix.h" 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/priv.h> 39 #include <sys/proc.h> 40 #include <sys/posix4.h> 41 #include <sys/ptrace.h> 42 #include <sys/racct.h> 43 #include <sys/resourcevar.h> 44 #include <sys/rwlock.h> 45 #include <sys/sched.h> 46 #include <sys/sysctl.h> 47 #include <sys/smp.h> 48 #include <sys/syscallsubr.h> 49 #include <sys/sysent.h> 50 #include <sys/systm.h> 51 #include <sys/sysproto.h> 52 #include <sys/signalvar.h> 53 #include <sys/sysctl.h> 54 #include <sys/ucontext.h> 55 #include <sys/thr.h> 56 #include <sys/rtprio.h> 57 #include <sys/umtx.h> 58 #include <sys/limits.h> 59 60 #include <machine/frame.h> 61 62 #include <security/audit/audit.h> 63 64 static SYSCTL_NODE(_kern, OID_AUTO, threads, CTLFLAG_RW, 0, 65 "thread allocation"); 66 67 static int max_threads_per_proc = 1500; 68 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_per_proc, CTLFLAG_RW, 69 &max_threads_per_proc, 0, "Limit on threads per proc"); 70 71 static int max_threads_hits; 72 SYSCTL_INT(_kern_threads, OID_AUTO, max_threads_hits, CTLFLAG_RD, 73 &max_threads_hits, 0, "kern.threads.max_threads_per_proc hit count"); 74 75 #ifdef COMPAT_FREEBSD32 76 77 static inline int 78 suword_lwpid(void *addr, lwpid_t lwpid) 79 { 80 int error; 81 82 if (SV_CURPROC_FLAG(SV_LP64)) 83 error = suword(addr, lwpid); 84 else 85 error = suword32(addr, lwpid); 86 return (error); 87 } 88 89 #else 90 #define suword_lwpid suword 91 #endif 92 93 /* 94 * System call interface. 95 */ 96 97 struct thr_create_initthr_args { 98 ucontext_t ctx; 99 long *tid; 100 }; 101 102 static int 103 thr_create_initthr(struct thread *td, void *thunk) 104 { 105 struct thr_create_initthr_args *args; 106 107 /* Copy out the child tid. */ 108 args = thunk; 109 if (args->tid != NULL && suword_lwpid(args->tid, td->td_tid)) 110 return (EFAULT); 111 112 return (set_mcontext(td, &args->ctx.uc_mcontext)); 113 } 114 115 int 116 sys_thr_create(struct thread *td, struct thr_create_args *uap) 117 /* ucontext_t *ctx, long *id, int flags */ 118 { 119 struct thr_create_initthr_args args; 120 int error; 121 122 if ((error = copyin(uap->ctx, &args.ctx, sizeof(args.ctx)))) 123 return (error); 124 args.tid = uap->id; 125 return (thread_create(td, NULL, thr_create_initthr, &args)); 126 } 127 128 int 129 sys_thr_new(struct thread *td, struct thr_new_args *uap) 130 /* struct thr_param * */ 131 { 132 struct thr_param param; 133 int error; 134 135 if (uap->param_size < 0 || uap->param_size > sizeof(param)) 136 return (EINVAL); 137 bzero(¶m, sizeof(param)); 138 if ((error = copyin(uap->param, ¶m, uap->param_size))) 139 return (error); 140 return (kern_thr_new(td, ¶m)); 141 } 142 143 static int 144 thr_new_initthr(struct thread *td, void *thunk) 145 { 146 stack_t stack; 147 struct thr_param *param; 148 149 /* 150 * Here we copy out tid to two places, one for child and one 151 * for parent, because pthread can create a detached thread, 152 * if parent wants to safely access child tid, it has to provide 153 * its storage, because child thread may exit quickly and 154 * memory is freed before parent thread can access it. 155 */ 156 param = thunk; 157 if ((param->child_tid != NULL && 158 suword_lwpid(param->child_tid, td->td_tid)) || 159 (param->parent_tid != NULL && 160 suword_lwpid(param->parent_tid, td->td_tid))) 161 return (EFAULT); 162 163 /* Set up our machine context. */ 164 stack.ss_sp = param->stack_base; 165 stack.ss_size = param->stack_size; 166 /* Set upcall address to user thread entry function. */ 167 cpu_set_upcall(td, param->start_func, param->arg, &stack); 168 /* Setup user TLS address and TLS pointer register. */ 169 return (cpu_set_user_tls(td, param->tls_base)); 170 } 171 172 int 173 kern_thr_new(struct thread *td, struct thr_param *param) 174 { 175 struct rtprio rtp, *rtpp; 176 int error; 177 178 rtpp = NULL; 179 if (param->rtp != 0) { 180 error = copyin(param->rtp, &rtp, sizeof(struct rtprio)); 181 if (error) 182 return (error); 183 rtpp = &rtp; 184 } 185 return (thread_create(td, rtpp, thr_new_initthr, param)); 186 } 187 188 int 189 thread_create(struct thread *td, struct rtprio *rtp, 190 int (*initialize_thread)(struct thread *, void *), void *thunk) 191 { 192 struct thread *newtd; 193 struct proc *p; 194 int error; 195 196 p = td->td_proc; 197 198 if (rtp != NULL) { 199 switch(rtp->type) { 200 case RTP_PRIO_REALTIME: 201 case RTP_PRIO_FIFO: 202 /* Only root can set scheduler policy */ 203 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0) 204 return (EPERM); 205 if (rtp->prio > RTP_PRIO_MAX) 206 return (EINVAL); 207 break; 208 case RTP_PRIO_NORMAL: 209 rtp->prio = 0; 210 break; 211 default: 212 return (EINVAL); 213 } 214 } 215 216 #ifdef RACCT 217 if (racct_enable) { 218 PROC_LOCK(p); 219 error = racct_add(p, RACCT_NTHR, 1); 220 PROC_UNLOCK(p); 221 if (error != 0) 222 return (EPROCLIM); 223 } 224 #endif 225 226 /* Initialize our td */ 227 error = kern_thr_alloc(p, 0, &newtd); 228 if (error) 229 goto fail; 230 231 cpu_copy_thread(newtd, td); 232 233 bzero(&newtd->td_startzero, 234 __rangeof(struct thread, td_startzero, td_endzero)); 235 bcopy(&td->td_startcopy, &newtd->td_startcopy, 236 __rangeof(struct thread, td_startcopy, td_endcopy)); 237 newtd->td_proc = td->td_proc; 238 newtd->td_rb_list = newtd->td_rbp_list = newtd->td_rb_inact = 0; 239 thread_cow_get(newtd, td); 240 241 error = initialize_thread(newtd, thunk); 242 if (error != 0) { 243 thread_cow_free(newtd); 244 thread_free(newtd); 245 goto fail; 246 } 247 248 PROC_LOCK(p); 249 p->p_flag |= P_HADTHREADS; 250 thread_link(newtd, p); 251 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name)); 252 thread_lock(td); 253 /* let the scheduler know about these things. */ 254 sched_fork_thread(td, newtd); 255 thread_unlock(td); 256 if (P_SHOULDSTOP(p)) 257 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 258 if (p->p_ptevents & PTRACE_LWP) 259 newtd->td_dbgflags |= TDB_BORN; 260 261 PROC_UNLOCK(p); 262 263 tidhash_add(newtd); 264 265 thread_lock(newtd); 266 if (rtp != NULL) { 267 if (!(td->td_pri_class == PRI_TIMESHARE && 268 rtp->type == RTP_PRIO_NORMAL)) { 269 rtp_to_pri(rtp, newtd); 270 sched_prio(newtd, newtd->td_user_pri); 271 } /* ignore timesharing class */ 272 } 273 TD_SET_CAN_RUN(newtd); 274 sched_add(newtd, SRQ_BORING); 275 thread_unlock(newtd); 276 277 return (0); 278 279 fail: 280 #ifdef RACCT 281 if (racct_enable) { 282 PROC_LOCK(p); 283 racct_sub(p, RACCT_NTHR, 1); 284 PROC_UNLOCK(p); 285 } 286 #endif 287 return (error); 288 } 289 290 int 291 sys_thr_self(struct thread *td, struct thr_self_args *uap) 292 /* long *id */ 293 { 294 int error; 295 296 error = suword_lwpid(uap->id, (unsigned)td->td_tid); 297 if (error == -1) 298 return (EFAULT); 299 return (0); 300 } 301 302 int 303 sys_thr_exit(struct thread *td, struct thr_exit_args *uap) 304 /* long *state */ 305 { 306 307 umtx_thread_exit(td); 308 309 /* Signal userland that it can free the stack. */ 310 if ((void *)uap->state != NULL) { 311 suword_lwpid(uap->state, 1); 312 kern_umtx_wake(td, uap->state, INT_MAX, 0); 313 } 314 315 return (kern_thr_exit(td)); 316 } 317 318 int 319 kern_thr_exit(struct thread *td) 320 { 321 struct proc *p; 322 323 p = td->td_proc; 324 325 /* 326 * If all of the threads in a process call this routine to 327 * exit (e.g. all threads call pthread_exit()), exactly one 328 * thread should return to the caller to terminate the process 329 * instead of the thread. 330 * 331 * Checking p_numthreads alone is not sufficient since threads 332 * might be committed to terminating while the PROC_LOCK is 333 * dropped in either ptracestop() or while removing this thread 334 * from the tidhash. Instead, the p_pendingexits field holds 335 * the count of threads in either of those states and a thread 336 * is considered the "last" thread if all of the other threads 337 * in a process are already terminating. 338 */ 339 PROC_LOCK(p); 340 if (p->p_numthreads == p->p_pendingexits + 1) { 341 /* 342 * Ignore attempts to shut down last thread in the 343 * proc. This will actually call _exit(2) in the 344 * usermode trampoline when it returns. 345 */ 346 PROC_UNLOCK(p); 347 return (0); 348 } 349 350 p->p_pendingexits++; 351 td->td_dbgflags |= TDB_EXIT; 352 if (p->p_ptevents & PTRACE_LWP) 353 ptracestop(td, SIGTRAP, NULL); 354 PROC_UNLOCK(p); 355 tidhash_remove(td); 356 PROC_LOCK(p); 357 p->p_pendingexits--; 358 359 /* 360 * The check above should prevent all other threads from this 361 * process from exiting while the PROC_LOCK is dropped, so 362 * there must be at least one other thread other than the 363 * current thread. 364 */ 365 KASSERT(p->p_numthreads > 1, ("too few threads")); 366 racct_sub(p, RACCT_NTHR, 1); 367 tdsigcleanup(td); 368 PROC_SLOCK(p); 369 thread_stopped(p); 370 thread_exit(); 371 /* NOTREACHED */ 372 } 373 374 int 375 sys_thr_kill(struct thread *td, struct thr_kill_args *uap) 376 /* long id, int sig */ 377 { 378 ksiginfo_t ksi; 379 struct thread *ttd; 380 struct proc *p; 381 int error; 382 383 p = td->td_proc; 384 ksiginfo_init(&ksi); 385 ksi.ksi_signo = uap->sig; 386 ksi.ksi_code = SI_LWP; 387 ksi.ksi_pid = p->p_pid; 388 ksi.ksi_uid = td->td_ucred->cr_ruid; 389 if (uap->id == -1) { 390 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 391 error = EINVAL; 392 } else { 393 error = ESRCH; 394 PROC_LOCK(p); 395 FOREACH_THREAD_IN_PROC(p, ttd) { 396 if (ttd != td) { 397 error = 0; 398 if (uap->sig == 0) 399 break; 400 tdksignal(ttd, uap->sig, &ksi); 401 } 402 } 403 PROC_UNLOCK(p); 404 } 405 } else { 406 error = 0; 407 ttd = tdfind((lwpid_t)uap->id, p->p_pid); 408 if (ttd == NULL) 409 return (ESRCH); 410 if (uap->sig == 0) 411 ; 412 else if (!_SIG_VALID(uap->sig)) 413 error = EINVAL; 414 else 415 tdksignal(ttd, uap->sig, &ksi); 416 PROC_UNLOCK(ttd->td_proc); 417 } 418 return (error); 419 } 420 421 int 422 sys_thr_kill2(struct thread *td, struct thr_kill2_args *uap) 423 /* pid_t pid, long id, int sig */ 424 { 425 ksiginfo_t ksi; 426 struct thread *ttd; 427 struct proc *p; 428 int error; 429 430 AUDIT_ARG_SIGNUM(uap->sig); 431 432 ksiginfo_init(&ksi); 433 ksi.ksi_signo = uap->sig; 434 ksi.ksi_code = SI_LWP; 435 ksi.ksi_pid = td->td_proc->p_pid; 436 ksi.ksi_uid = td->td_ucred->cr_ruid; 437 if (uap->id == -1) { 438 if ((p = pfind(uap->pid)) == NULL) 439 return (ESRCH); 440 AUDIT_ARG_PROCESS(p); 441 error = p_cansignal(td, p, uap->sig); 442 if (error) { 443 PROC_UNLOCK(p); 444 return (error); 445 } 446 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 447 error = EINVAL; 448 } else { 449 error = ESRCH; 450 FOREACH_THREAD_IN_PROC(p, ttd) { 451 if (ttd != td) { 452 error = 0; 453 if (uap->sig == 0) 454 break; 455 tdksignal(ttd, uap->sig, &ksi); 456 } 457 } 458 } 459 PROC_UNLOCK(p); 460 } else { 461 ttd = tdfind((lwpid_t)uap->id, uap->pid); 462 if (ttd == NULL) 463 return (ESRCH); 464 p = ttd->td_proc; 465 AUDIT_ARG_PROCESS(p); 466 error = p_cansignal(td, p, uap->sig); 467 if (uap->sig == 0) 468 ; 469 else if (!_SIG_VALID(uap->sig)) 470 error = EINVAL; 471 else 472 tdksignal(ttd, uap->sig, &ksi); 473 PROC_UNLOCK(p); 474 } 475 return (error); 476 } 477 478 int 479 sys_thr_suspend(struct thread *td, struct thr_suspend_args *uap) 480 /* const struct timespec *timeout */ 481 { 482 struct timespec ts, *tsp; 483 int error; 484 485 tsp = NULL; 486 if (uap->timeout != NULL) { 487 error = umtx_copyin_timeout(uap->timeout, &ts); 488 if (error != 0) 489 return (error); 490 tsp = &ts; 491 } 492 493 return (kern_thr_suspend(td, tsp)); 494 } 495 496 int 497 kern_thr_suspend(struct thread *td, struct timespec *tsp) 498 { 499 struct proc *p = td->td_proc; 500 struct timeval tv; 501 int error = 0; 502 int timo = 0; 503 504 if (td->td_pflags & TDP_WAKEUP) { 505 td->td_pflags &= ~TDP_WAKEUP; 506 return (0); 507 } 508 509 if (tsp != NULL) { 510 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 511 error = EWOULDBLOCK; 512 else { 513 TIMESPEC_TO_TIMEVAL(&tv, tsp); 514 timo = tvtohz(&tv); 515 } 516 } 517 518 PROC_LOCK(p); 519 if (error == 0 && (td->td_flags & TDF_THRWAKEUP) == 0) 520 error = msleep((void *)td, &p->p_mtx, 521 PCATCH, "lthr", timo); 522 523 if (td->td_flags & TDF_THRWAKEUP) { 524 thread_lock(td); 525 td->td_flags &= ~TDF_THRWAKEUP; 526 thread_unlock(td); 527 PROC_UNLOCK(p); 528 return (0); 529 } 530 PROC_UNLOCK(p); 531 if (error == EWOULDBLOCK) 532 error = ETIMEDOUT; 533 else if (error == ERESTART) { 534 if (timo != 0) 535 error = EINTR; 536 } 537 return (error); 538 } 539 540 int 541 sys_thr_wake(struct thread *td, struct thr_wake_args *uap) 542 /* long id */ 543 { 544 struct proc *p; 545 struct thread *ttd; 546 547 if (uap->id == td->td_tid) { 548 td->td_pflags |= TDP_WAKEUP; 549 return (0); 550 } 551 552 p = td->td_proc; 553 ttd = tdfind((lwpid_t)uap->id, p->p_pid); 554 if (ttd == NULL) 555 return (ESRCH); 556 thread_lock(ttd); 557 ttd->td_flags |= TDF_THRWAKEUP; 558 thread_unlock(ttd); 559 wakeup((void *)ttd); 560 PROC_UNLOCK(p); 561 return (0); 562 } 563 564 int 565 sys_thr_set_name(struct thread *td, struct thr_set_name_args *uap) 566 { 567 struct proc *p; 568 char name[MAXCOMLEN + 1]; 569 struct thread *ttd; 570 int error; 571 572 error = 0; 573 name[0] = '\0'; 574 if (uap->name != NULL) { 575 error = copyinstr(uap->name, name, sizeof(name), NULL); 576 if (error == ENAMETOOLONG) { 577 error = copyin(uap->name, name, sizeof(name) - 1); 578 name[sizeof(name) - 1] = '\0'; 579 } 580 if (error) 581 return (error); 582 } 583 p = td->td_proc; 584 ttd = tdfind((lwpid_t)uap->id, p->p_pid); 585 if (ttd == NULL) 586 return (ESRCH); 587 strcpy(ttd->td_name, name); 588 #ifdef KTR 589 sched_clear_tdname(ttd); 590 #endif 591 PROC_UNLOCK(p); 592 return (error); 593 } 594 595 int 596 kern_thr_alloc(struct proc *p, int pages, struct thread **ntd) 597 { 598 599 /* Have race condition but it is cheap. */ 600 if (p->p_numthreads >= max_threads_per_proc) { 601 ++max_threads_hits; 602 return (EPROCLIM); 603 } 604 605 *ntd = thread_alloc(pages); 606 if (*ntd == NULL) 607 return (ENOMEM); 608 609 return (0); 610 } 611