1 /*- 2 * Copyright (c) 2003, Jeffrey Roberson <jeff@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 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. 14 * 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. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_compat.h" 31 #include "opt_posix.h" 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/priv.h> 37 #include <sys/proc.h> 38 #include <sys/posix4.h> 39 #include <sys/resourcevar.h> 40 #include <sys/sched.h> 41 #include <sys/sysctl.h> 42 #include <sys/smp.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> 49 #include <sys/thr.h> 50 #include <sys/rtprio.h> 51 #include <sys/umtx.h> 52 #include <sys/limits.h> 53 54 #include <machine/frame.h> 55 56 #include <security/audit/audit.h> 57 58 #ifdef COMPAT_IA32 59 60 static inline int 61 suword_lwpid(void *addr, lwpid_t lwpid) 62 { 63 int error; 64 65 if (SV_CURPROC_FLAG(SV_LP64)) 66 error = suword(addr, lwpid); 67 else 68 error = suword32(addr, lwpid); 69 return (error); 70 } 71 72 #else 73 #define suword_lwpid suword 74 #endif 75 76 extern int max_threads_per_proc; 77 extern int max_threads_hits; 78 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, 82 char *tls_base, 83 long *child_tid, long *parent_tid, 84 int flags, struct rtprio *rtp); 85 86 /* 87 * System call interface. 88 */ 89 int 90 thr_create(struct thread *td, struct thr_create_args *uap) 91 /* ucontext_t *ctx, long *id, int flags */ 92 { 93 ucontext_t ctx; 94 int error; 95 96 if ((error = copyin(uap->ctx, &ctx, sizeof(ctx)))) 97 return (error); 98 99 error = create_thread(td, &ctx.uc_mcontext, NULL, NULL, 100 NULL, 0, NULL, uap->id, NULL, uap->flags, NULL); 101 return (error); 102 } 103 104 int 105 thr_new(struct thread *td, struct thr_new_args *uap) 106 /* struct thr_param * */ 107 { 108 struct thr_param param; 109 int error; 110 111 if (uap->param_size < 0 || uap->param_size > sizeof(param)) 112 return (EINVAL); 113 bzero(¶m, sizeof(param)); 114 if ((error = copyin(uap->param, ¶m, uap->param_size))) 115 return (error); 116 return (kern_thr_new(td, ¶m)); 117 } 118 119 int 120 kern_thr_new(struct thread *td, struct thr_param *param) 121 { 122 struct rtprio rtp, *rtpp; 123 int error; 124 125 rtpp = NULL; 126 if (param->rtp != 0) { 127 error = copyin(param->rtp, &rtp, sizeof(struct rtprio)); 128 if (error) 129 return (error); 130 rtpp = &rtp; 131 } 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, 135 rtpp); 136 return (error); 137 } 138 139 static int 140 create_thread(struct thread *td, mcontext_t *ctx, 141 void (*start_func)(void *), void *arg, 142 char *stack_base, size_t stack_size, 143 char *tls_base, 144 long *child_tid, long *parent_tid, 145 int flags, struct rtprio *rtp) 146 { 147 stack_t stack; 148 struct thread *newtd; 149 struct proc *p; 150 int error; 151 152 error = 0; 153 p = td->td_proc; 154 155 /* Have race condition but it is cheap. */ 156 if (p->p_numthreads >= max_threads_per_proc) { 157 ++max_threads_hits; 158 return (EPROCLIM); 159 } 160 161 if (rtp != NULL) { 162 switch(rtp->type) { 163 case RTP_PRIO_REALTIME: 164 case RTP_PRIO_FIFO: 165 /* Only root can set scheduler policy */ 166 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0) 167 return (EPERM); 168 if (rtp->prio > RTP_PRIO_MAX) 169 return (EINVAL); 170 break; 171 case RTP_PRIO_NORMAL: 172 rtp->prio = 0; 173 break; 174 default: 175 return (EINVAL); 176 } 177 } 178 179 /* Initialize our td */ 180 newtd = thread_alloc(); 181 if (newtd == NULL) 182 return (ENOMEM); 183 184 /* 185 * Try the copyout as soon as we allocate the td so we don't 186 * have to tear things down in a failure case below. 187 * Here we copy out tid to two places, one for child and one 188 * for parent, because pthread can create a detached thread, 189 * if parent wants to safely access child tid, it has to provide 190 * its storage, because child thread may exit quickly and 191 * memory is freed before parent thread can access it. 192 */ 193 if ((child_tid != NULL && 194 suword_lwpid(child_tid, newtd->td_tid)) || 195 (parent_tid != NULL && 196 suword_lwpid(parent_tid, newtd->td_tid))) { 197 thread_free(newtd); 198 return (EFAULT); 199 } 200 201 bzero(&newtd->td_startzero, 202 __rangeof(struct thread, td_startzero, td_endzero)); 203 bcopy(&td->td_startcopy, &newtd->td_startcopy, 204 __rangeof(struct thread, td_startcopy, td_endcopy)); 205 newtd->td_proc = td->td_proc; 206 newtd->td_ucred = crhold(td->td_ucred); 207 208 cpu_set_upcall(newtd, td); 209 210 if (ctx != NULL) { /* old way to set user context */ 211 error = set_mcontext(newtd, ctx); 212 if (error != 0) { 213 thread_free(newtd); 214 crfree(td->td_ucred); 215 return (error); 216 } 217 } else { 218 /* Set up our machine context. */ 219 stack.ss_sp = stack_base; 220 stack.ss_size = stack_size; 221 /* Set upcall address to user thread entry function. */ 222 cpu_set_upcall_kse(newtd, start_func, arg, &stack); 223 /* Setup user TLS address and TLS pointer register. */ 224 error = cpu_set_user_tls(newtd, tls_base); 225 if (error != 0) { 226 thread_free(newtd); 227 crfree(td->td_ucred); 228 return (error); 229 } 230 } 231 232 PROC_LOCK(td->td_proc); 233 td->td_proc->p_flag |= P_HADTHREADS; 234 newtd->td_sigmask = td->td_sigmask; 235 thread_link(newtd, p); 236 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name)); 237 thread_lock(td); 238 /* let the scheduler know about these things. */ 239 sched_fork_thread(td, newtd); 240 thread_unlock(td); 241 if (P_SHOULDSTOP(p)) 242 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 243 PROC_UNLOCK(p); 244 thread_lock(newtd); 245 if (rtp != NULL) { 246 if (!(td->td_pri_class == PRI_TIMESHARE && 247 rtp->type == RTP_PRIO_NORMAL)) { 248 rtp_to_pri(rtp, newtd); 249 sched_prio(newtd, newtd->td_user_pri); 250 } /* ignore timesharing class */ 251 } 252 TD_SET_CAN_RUN(newtd); 253 sched_add(newtd, SRQ_BORING); 254 thread_unlock(newtd); 255 256 return (error); 257 } 258 259 int 260 thr_self(struct thread *td, struct thr_self_args *uap) 261 /* long *id */ 262 { 263 int error; 264 265 error = suword_lwpid(uap->id, (unsigned)td->td_tid); 266 if (error == -1) 267 return (EFAULT); 268 return (0); 269 } 270 271 int 272 thr_exit(struct thread *td, struct thr_exit_args *uap) 273 /* long *state */ 274 { 275 struct proc *p; 276 277 p = td->td_proc; 278 279 /* Signal userland that it can free the stack. */ 280 if ((void *)uap->state != NULL) { 281 suword_lwpid(uap->state, 1); 282 kern_umtx_wake(td, uap->state, INT_MAX, 0); 283 } 284 285 PROC_LOCK(p); 286 sigqueue_flush(&td->td_sigqueue); 287 PROC_SLOCK(p); 288 289 /* 290 * Shutting down last thread in the proc. This will actually 291 * call exit() in the trampoline when it returns. 292 */ 293 if (p->p_numthreads != 1) { 294 thread_stopped(p); 295 thread_exit(); 296 /* NOTREACHED */ 297 } 298 PROC_SUNLOCK(p); 299 PROC_UNLOCK(p); 300 return (0); 301 } 302 303 int 304 thr_kill(struct thread *td, struct thr_kill_args *uap) 305 /* long id, int sig */ 306 { 307 struct thread *ttd; 308 struct proc *p; 309 int error; 310 311 p = td->td_proc; 312 error = 0; 313 PROC_LOCK(p); 314 if (uap->id == -1) { 315 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 316 error = EINVAL; 317 } else { 318 error = ESRCH; 319 FOREACH_THREAD_IN_PROC(p, ttd) { 320 if (ttd != td) { 321 error = 0; 322 if (uap->sig == 0) 323 break; 324 tdsignal(p, ttd, uap->sig, NULL); 325 } 326 } 327 } 328 } else { 329 if (uap->id != td->td_tid) 330 ttd = thread_find(p, uap->id); 331 else 332 ttd = td; 333 if (ttd == NULL) 334 error = ESRCH; 335 else if (uap->sig == 0) 336 ; 337 else if (!_SIG_VALID(uap->sig)) 338 error = EINVAL; 339 else 340 tdsignal(p, ttd, uap->sig, NULL); 341 } 342 PROC_UNLOCK(p); 343 return (error); 344 } 345 346 int 347 thr_kill2(struct thread *td, struct thr_kill2_args *uap) 348 /* pid_t pid, long id, int sig */ 349 { 350 struct thread *ttd; 351 struct proc *p; 352 int error; 353 354 AUDIT_ARG(signum, uap->sig); 355 356 if (uap->pid == td->td_proc->p_pid) { 357 p = td->td_proc; 358 PROC_LOCK(p); 359 } else if ((p = pfind(uap->pid)) == NULL) { 360 return (ESRCH); 361 } 362 AUDIT_ARG(process, p); 363 364 error = p_cansignal(td, p, uap->sig); 365 if (error == 0) { 366 if (uap->id == -1) { 367 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) { 368 error = EINVAL; 369 } else { 370 error = ESRCH; 371 FOREACH_THREAD_IN_PROC(p, ttd) { 372 if (ttd != td) { 373 error = 0; 374 if (uap->sig == 0) 375 break; 376 tdsignal(p, ttd, uap->sig, NULL); 377 } 378 } 379 } 380 } else { 381 if (uap->id != td->td_tid) 382 ttd = thread_find(p, uap->id); 383 else 384 ttd = td; 385 if (ttd == NULL) 386 error = ESRCH; 387 else if (uap->sig == 0) 388 ; 389 else if (!_SIG_VALID(uap->sig)) 390 error = EINVAL; 391 else 392 tdsignal(p, ttd, uap->sig, NULL); 393 } 394 } 395 PROC_UNLOCK(p); 396 return (error); 397 } 398 399 int 400 thr_suspend(struct thread *td, struct thr_suspend_args *uap) 401 /* const struct timespec *timeout */ 402 { 403 struct timespec ts, *tsp; 404 int error; 405 406 error = 0; 407 tsp = NULL; 408 if (uap->timeout != NULL) { 409 error = copyin((const void *)uap->timeout, (void *)&ts, 410 sizeof(struct timespec)); 411 if (error != 0) 412 return (error); 413 tsp = &ts; 414 } 415 416 return (kern_thr_suspend(td, tsp)); 417 } 418 419 int 420 kern_thr_suspend(struct thread *td, struct timespec *tsp) 421 { 422 struct timeval tv; 423 int error = 0, hz = 0; 424 425 if (tsp != NULL) { 426 if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000) 427 return (EINVAL); 428 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 429 return (ETIMEDOUT); 430 TIMESPEC_TO_TIMEVAL(&tv, tsp); 431 hz = tvtohz(&tv); 432 } 433 434 if (td->td_pflags & TDP_WAKEUP) { 435 td->td_pflags &= ~TDP_WAKEUP; 436 return (0); 437 } 438 439 PROC_LOCK(td->td_proc); 440 if ((td->td_flags & TDF_THRWAKEUP) == 0) 441 error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr", 442 hz); 443 if (td->td_flags & TDF_THRWAKEUP) { 444 thread_lock(td); 445 td->td_flags &= ~TDF_THRWAKEUP; 446 thread_unlock(td); 447 PROC_UNLOCK(td->td_proc); 448 return (0); 449 } 450 PROC_UNLOCK(td->td_proc); 451 if (error == EWOULDBLOCK) 452 error = ETIMEDOUT; 453 else if (error == ERESTART) { 454 if (hz != 0) 455 error = EINTR; 456 } 457 return (error); 458 } 459 460 int 461 thr_wake(struct thread *td, struct thr_wake_args *uap) 462 /* long id */ 463 { 464 struct proc *p; 465 struct thread *ttd; 466 467 if (uap->id == td->td_tid) { 468 td->td_pflags |= TDP_WAKEUP; 469 return (0); 470 } 471 472 p = td->td_proc; 473 PROC_LOCK(p); 474 ttd = thread_find(p, uap->id); 475 if (ttd == NULL) { 476 PROC_UNLOCK(p); 477 return (ESRCH); 478 } 479 thread_lock(ttd); 480 ttd->td_flags |= TDF_THRWAKEUP; 481 thread_unlock(ttd); 482 wakeup((void *)ttd); 483 PROC_UNLOCK(p); 484 return (0); 485 } 486 487 int 488 thr_set_name(struct thread *td, struct thr_set_name_args *uap) 489 { 490 struct proc *p = td->td_proc; 491 char name[MAXCOMLEN + 1]; 492 struct thread *ttd; 493 int error; 494 495 error = 0; 496 name[0] = '\0'; 497 if (uap->name != NULL) { 498 error = copyinstr(uap->name, name, sizeof(name), 499 NULL); 500 if (error) 501 return (error); 502 } 503 PROC_LOCK(p); 504 if (uap->id == td->td_tid) 505 ttd = td; 506 else 507 ttd = thread_find(p, uap->id); 508 if (ttd != NULL) 509 strcpy(ttd->td_name, name); 510 else 511 error = ESRCH; 512 PROC_UNLOCK(p); 513 return (error); 514 } 515