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