1 /* $NetBSD: sys_lwp.c,v 1.61 2017/06/01 02:45:13 chs Exp $ */ 2 3 /*- 4 * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Nathan J. Williams, and Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Lightweight process (LWP) system calls. See kern_lwp.c for a description 34 * of LWPs. 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.61 2017/06/01 02:45:13 chs Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/pool.h> 43 #include <sys/proc.h> 44 #include <sys/types.h> 45 #include <sys/syscallargs.h> 46 #include <sys/kauth.h> 47 #include <sys/kmem.h> 48 #include <sys/sleepq.h> 49 #include <sys/lwpctl.h> 50 #include <sys/cpu.h> 51 52 #include <uvm/uvm_extern.h> 53 54 #define LWP_UNPARK_MAX 1024 55 56 static syncobj_t lwp_park_sobj = { 57 SOBJ_SLEEPQ_LIFO, 58 sleepq_unsleep, 59 sleepq_changepri, 60 sleepq_lendpri, 61 syncobj_noowner, 62 }; 63 64 static sleeptab_t lwp_park_tab; 65 66 void 67 lwp_sys_init(void) 68 { 69 sleeptab_init(&lwp_park_tab); 70 } 71 72 int 73 do_lwp_create(lwp_t *l, void *arg, u_long flags, lwpid_t *new_lwp, 74 const sigset_t *sigmask, const stack_t *sigstk) 75 { 76 struct proc *p = l->l_proc; 77 struct lwp *l2; 78 struct schedstate_percpu *spc; 79 vaddr_t uaddr; 80 int error; 81 82 /* XXX check against resource limits */ 83 84 uaddr = uvm_uarea_alloc(); 85 if (__predict_false(uaddr == 0)) 86 return ENOMEM; 87 88 error = lwp_create(l, p, uaddr, flags & LWP_DETACHED, NULL, 0, 89 p->p_emul->e_startlwp, arg, &l2, l->l_class, sigmask, &SS_INIT); 90 if (__predict_false(error)) { 91 uvm_uarea_free(uaddr); 92 return error; 93 } 94 95 *new_lwp = l2->l_lid; 96 97 /* 98 * Set the new LWP running, unless the caller has requested that 99 * it be created in suspended state. If the process is stopping, 100 * then the LWP is created stopped. 101 */ 102 mutex_enter(p->p_lock); 103 lwp_lock(l2); 104 spc = &l2->l_cpu->ci_schedstate; 105 if ((flags & LWP_SUSPENDED) == 0 && 106 (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) { 107 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) { 108 KASSERT(l2->l_wchan == NULL); 109 l2->l_stat = LSSTOP; 110 p->p_nrlwps--; 111 lwp_unlock_to(l2, spc->spc_lwplock); 112 } else { 113 KASSERT(lwp_locked(l2, spc->spc_mutex)); 114 l2->l_stat = LSRUN; 115 sched_enqueue(l2, false); 116 lwp_unlock(l2); 117 } 118 } else { 119 l2->l_stat = LSSUSPENDED; 120 p->p_nrlwps--; 121 lwp_unlock_to(l2, spc->spc_lwplock); 122 } 123 mutex_exit(p->p_lock); 124 125 return 0; 126 } 127 128 int 129 sys__lwp_create(struct lwp *l, const struct sys__lwp_create_args *uap, 130 register_t *retval) 131 { 132 /* { 133 syscallarg(const ucontext_t *) ucp; 134 syscallarg(u_long) flags; 135 syscallarg(lwpid_t *) new_lwp; 136 } */ 137 struct proc *p = l->l_proc; 138 ucontext_t *newuc; 139 lwpid_t lid; 140 int error; 141 142 newuc = kmem_alloc(sizeof(ucontext_t), KM_SLEEP); 143 error = copyin(SCARG(uap, ucp), newuc, p->p_emul->e_ucsize); 144 if (error) 145 goto fail; 146 147 /* validate the ucontext */ 148 if ((newuc->uc_flags & _UC_CPU) == 0) { 149 error = EINVAL; 150 goto fail; 151 } 152 error = cpu_mcontext_validate(l, &newuc->uc_mcontext); 153 if (error) 154 goto fail; 155 156 const sigset_t *sigmask = newuc->uc_flags & _UC_SIGMASK ? 157 &newuc->uc_sigmask : &l->l_sigmask; 158 error = do_lwp_create(l, newuc, SCARG(uap, flags), &lid, sigmask, 159 &SS_INIT); 160 if (error) 161 goto fail; 162 163 /* 164 * do not free ucontext in case of an error here, 165 * the lwp will actually run and access it 166 */ 167 return copyout(&lid, SCARG(uap, new_lwp), sizeof(lid)); 168 169 fail: 170 kmem_free(newuc, sizeof(ucontext_t)); 171 return error; 172 } 173 174 int 175 sys__lwp_exit(struct lwp *l, const void *v, register_t *retval) 176 { 177 178 lwp_exit(l); 179 return 0; 180 } 181 182 int 183 sys__lwp_self(struct lwp *l, const void *v, register_t *retval) 184 { 185 186 *retval = l->l_lid; 187 return 0; 188 } 189 190 int 191 sys__lwp_getprivate(struct lwp *l, const void *v, register_t *retval) 192 { 193 194 *retval = (uintptr_t)l->l_private; 195 return 0; 196 } 197 198 int 199 sys__lwp_setprivate(struct lwp *l, const struct sys__lwp_setprivate_args *uap, 200 register_t *retval) 201 { 202 /* { 203 syscallarg(void *) ptr; 204 } */ 205 206 return lwp_setprivate(l, SCARG(uap, ptr)); 207 } 208 209 int 210 sys__lwp_suspend(struct lwp *l, const struct sys__lwp_suspend_args *uap, 211 register_t *retval) 212 { 213 /* { 214 syscallarg(lwpid_t) target; 215 } */ 216 struct proc *p = l->l_proc; 217 struct lwp *t; 218 int error; 219 220 mutex_enter(p->p_lock); 221 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) { 222 mutex_exit(p->p_lock); 223 return ESRCH; 224 } 225 226 /* 227 * Check for deadlock, which is only possible when we're suspending 228 * ourself. XXX There is a short race here, as p_nrlwps is only 229 * incremented when an LWP suspends itself on the kernel/user 230 * boundary. It's still possible to kill -9 the process so we 231 * don't bother checking further. 232 */ 233 lwp_lock(t); 234 if ((t == l && p->p_nrlwps == 1) || 235 (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) { 236 lwp_unlock(t); 237 mutex_exit(p->p_lock); 238 return EDEADLK; 239 } 240 241 /* 242 * Suspend the LWP. XXX If it's on a different CPU, we should wait 243 * for it to be preempted, where it will put itself to sleep. 244 * 245 * Suspension of the current LWP will happen on return to userspace. 246 */ 247 error = lwp_suspend(l, t); 248 if (error) { 249 mutex_exit(p->p_lock); 250 return error; 251 } 252 253 /* 254 * Wait for: 255 * o process exiting 256 * o target LWP suspended 257 * o target LWP not suspended and L_WSUSPEND clear 258 * o target LWP exited 259 */ 260 for (;;) { 261 error = cv_wait_sig(&p->p_lwpcv, p->p_lock); 262 if (error) { 263 error = ERESTART; 264 break; 265 } 266 if (lwp_find(p, SCARG(uap, target)) == NULL) { 267 error = ESRCH; 268 break; 269 } 270 if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) { 271 error = ERESTART; 272 break; 273 } 274 if (t->l_stat == LSSUSPENDED || 275 (t->l_flag & LW_WSUSPEND) == 0) 276 break; 277 } 278 mutex_exit(p->p_lock); 279 280 return error; 281 } 282 283 int 284 sys__lwp_continue(struct lwp *l, const struct sys__lwp_continue_args *uap, 285 register_t *retval) 286 { 287 /* { 288 syscallarg(lwpid_t) target; 289 } */ 290 int error; 291 struct proc *p = l->l_proc; 292 struct lwp *t; 293 294 error = 0; 295 296 mutex_enter(p->p_lock); 297 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) { 298 mutex_exit(p->p_lock); 299 return ESRCH; 300 } 301 302 lwp_lock(t); 303 lwp_continue(t); 304 mutex_exit(p->p_lock); 305 306 return error; 307 } 308 309 int 310 sys__lwp_wakeup(struct lwp *l, const struct sys__lwp_wakeup_args *uap, 311 register_t *retval) 312 { 313 /* { 314 syscallarg(lwpid_t) target; 315 } */ 316 struct lwp *t; 317 struct proc *p; 318 int error; 319 320 p = l->l_proc; 321 mutex_enter(p->p_lock); 322 323 if ((t = lwp_find(p, SCARG(uap, target))) == NULL) { 324 mutex_exit(p->p_lock); 325 return ESRCH; 326 } 327 328 lwp_lock(t); 329 t->l_flag |= (LW_CANCELLED | LW_UNPARKED); 330 331 if (t->l_stat != LSSLEEP) { 332 lwp_unlock(t); 333 error = ENODEV; 334 } else if ((t->l_flag & LW_SINTR) == 0) { 335 lwp_unlock(t); 336 error = EBUSY; 337 } else { 338 /* Wake it up. lwp_unsleep() will release the LWP lock. */ 339 lwp_unsleep(t, true); 340 error = 0; 341 } 342 343 mutex_exit(p->p_lock); 344 345 return error; 346 } 347 348 int 349 sys__lwp_wait(struct lwp *l, const struct sys__lwp_wait_args *uap, 350 register_t *retval) 351 { 352 /* { 353 syscallarg(lwpid_t) wait_for; 354 syscallarg(lwpid_t *) departed; 355 } */ 356 struct proc *p = l->l_proc; 357 int error; 358 lwpid_t dep; 359 360 mutex_enter(p->p_lock); 361 error = lwp_wait(l, SCARG(uap, wait_for), &dep, false); 362 mutex_exit(p->p_lock); 363 364 if (!error && SCARG(uap, departed)) { 365 error = copyout(&dep, SCARG(uap, departed), sizeof(dep)); 366 } 367 368 return error; 369 } 370 371 int 372 sys__lwp_kill(struct lwp *l, const struct sys__lwp_kill_args *uap, 373 register_t *retval) 374 { 375 /* { 376 syscallarg(lwpid_t) target; 377 syscallarg(int) signo; 378 } */ 379 struct proc *p = l->l_proc; 380 struct lwp *t; 381 ksiginfo_t ksi; 382 int signo = SCARG(uap, signo); 383 int error = 0; 384 385 if ((u_int)signo >= NSIG) 386 return EINVAL; 387 388 KSI_INIT(&ksi); 389 ksi.ksi_signo = signo; 390 ksi.ksi_code = SI_LWP; 391 ksi.ksi_pid = p->p_pid; 392 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred); 393 ksi.ksi_lid = SCARG(uap, target); 394 395 mutex_enter(proc_lock); 396 mutex_enter(p->p_lock); 397 if ((t = lwp_find(p, ksi.ksi_lid)) == NULL) 398 error = ESRCH; 399 else if (signo != 0) 400 kpsignal2(p, &ksi); 401 mutex_exit(p->p_lock); 402 mutex_exit(proc_lock); 403 404 return error; 405 } 406 407 int 408 sys__lwp_detach(struct lwp *l, const struct sys__lwp_detach_args *uap, 409 register_t *retval) 410 { 411 /* { 412 syscallarg(lwpid_t) target; 413 } */ 414 struct proc *p; 415 struct lwp *t; 416 lwpid_t target; 417 int error; 418 419 target = SCARG(uap, target); 420 p = l->l_proc; 421 422 mutex_enter(p->p_lock); 423 424 if (l->l_lid == target) 425 t = l; 426 else { 427 /* 428 * We can't use lwp_find() here because the target might 429 * be a zombie. 430 */ 431 LIST_FOREACH(t, &p->p_lwps, l_sibling) 432 if (t->l_lid == target) 433 break; 434 } 435 436 /* 437 * If the LWP is already detached, there's nothing to do. 438 * If it's a zombie, we need to clean up after it. LSZOMB 439 * is visible with the proc mutex held. 440 * 441 * After we have detached or released the LWP, kick any 442 * other LWPs that may be sitting in _lwp_wait(), waiting 443 * for the target LWP to exit. 444 */ 445 if (t != NULL && t->l_stat != LSIDL) { 446 if ((t->l_prflag & LPR_DETACHED) == 0) { 447 p->p_ndlwps++; 448 t->l_prflag |= LPR_DETACHED; 449 if (t->l_stat == LSZOMB) { 450 /* Releases proc mutex. */ 451 lwp_free(t, false, false); 452 return 0; 453 } 454 error = 0; 455 456 /* 457 * Have any LWPs sleeping in lwp_wait() recheck 458 * for deadlock. 459 */ 460 cv_broadcast(&p->p_lwpcv); 461 } else 462 error = EINVAL; 463 } else 464 error = ESRCH; 465 466 mutex_exit(p->p_lock); 467 468 return error; 469 } 470 471 static inline wchan_t 472 lwp_park_wchan(struct proc *p, const void *hint) 473 { 474 475 return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint); 476 } 477 478 int 479 lwp_unpark(lwpid_t target, const void *hint) 480 { 481 sleepq_t *sq; 482 wchan_t wchan; 483 kmutex_t *mp; 484 proc_t *p; 485 lwp_t *t; 486 487 /* 488 * Easy case: search for the LWP on the sleep queue. If 489 * it's parked, remove it from the queue and set running. 490 */ 491 p = curproc; 492 wchan = lwp_park_wchan(p, hint); 493 sq = sleeptab_lookup(&lwp_park_tab, wchan, &mp); 494 495 TAILQ_FOREACH(t, sq, l_sleepchain) 496 if (t->l_proc == p && t->l_lid == target) 497 break; 498 499 if (__predict_true(t != NULL)) { 500 sleepq_remove(sq, t); 501 mutex_spin_exit(mp); 502 return 0; 503 } 504 505 /* 506 * The LWP hasn't parked yet. Take the hit and mark the 507 * operation as pending. 508 */ 509 mutex_spin_exit(mp); 510 511 mutex_enter(p->p_lock); 512 if ((t = lwp_find(p, target)) == NULL) { 513 mutex_exit(p->p_lock); 514 return ESRCH; 515 } 516 517 /* 518 * It may not have parked yet, we may have raced, or it 519 * is parked on a different user sync object. 520 */ 521 lwp_lock(t); 522 if (t->l_syncobj == &lwp_park_sobj) { 523 /* Releases the LWP lock. */ 524 lwp_unsleep(t, true); 525 } else { 526 /* 527 * Set the operation pending. The next call to _lwp_park 528 * will return early. 529 */ 530 t->l_flag |= LW_UNPARKED; 531 lwp_unlock(t); 532 } 533 534 mutex_exit(p->p_lock); 535 return 0; 536 } 537 538 int 539 lwp_park(clockid_t clock_id, int flags, struct timespec *ts, const void *hint) 540 { 541 sleepq_t *sq; 542 kmutex_t *mp; 543 wchan_t wchan; 544 int timo, error; 545 lwp_t *l; 546 547 if (ts != NULL) { 548 if ((error = ts2timo(clock_id, flags, ts, &timo, NULL)) != 0) 549 return error; 550 KASSERT(timo != 0); 551 } else { 552 timo = 0; 553 } 554 555 /* Find and lock the sleep queue. */ 556 l = curlwp; 557 wchan = lwp_park_wchan(l->l_proc, hint); 558 sq = sleeptab_lookup(&lwp_park_tab, wchan, &mp); 559 560 /* 561 * Before going the full route and blocking, check to see if an 562 * unpark op is pending. 563 */ 564 lwp_lock(l); 565 if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) { 566 l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED); 567 lwp_unlock(l); 568 mutex_spin_exit(mp); 569 return EALREADY; 570 } 571 lwp_unlock_to(l, mp); 572 l->l_biglocks = 0; 573 sleepq_enqueue(sq, wchan, "parked", &lwp_park_sobj); 574 error = sleepq_block(timo, true); 575 switch (error) { 576 case EWOULDBLOCK: 577 error = ETIMEDOUT; 578 break; 579 case ERESTART: 580 error = EINTR; 581 break; 582 default: 583 /* nothing */ 584 break; 585 } 586 return error; 587 } 588 589 /* 590 * 'park' an LWP waiting on a user-level synchronisation object. The LWP 591 * will remain parked until another LWP in the same process calls in and 592 * requests that it be unparked. 593 */ 594 int 595 sys____lwp_park60(struct lwp *l, const struct sys____lwp_park60_args *uap, 596 register_t *retval) 597 { 598 /* { 599 syscallarg(clockid_t) clock_id; 600 syscallarg(int) flags; 601 syscallarg(const struct timespec *) ts; 602 syscallarg(lwpid_t) unpark; 603 syscallarg(const void *) hint; 604 syscallarg(const void *) unparkhint; 605 } */ 606 struct timespec ts, *tsp; 607 int error; 608 609 if (SCARG(uap, ts) == NULL) 610 tsp = NULL; 611 else { 612 error = copyin(SCARG(uap, ts), &ts, sizeof(ts)); 613 if (error != 0) 614 return error; 615 tsp = &ts; 616 } 617 618 if (SCARG(uap, unpark) != 0) { 619 error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint)); 620 if (error != 0) 621 return error; 622 } 623 624 return lwp_park(SCARG(uap, clock_id), SCARG(uap, flags), tsp, 625 SCARG(uap, hint)); 626 } 627 628 int 629 sys__lwp_unpark(struct lwp *l, const struct sys__lwp_unpark_args *uap, 630 register_t *retval) 631 { 632 /* { 633 syscallarg(lwpid_t) target; 634 syscallarg(const void *) hint; 635 } */ 636 637 return lwp_unpark(SCARG(uap, target), SCARG(uap, hint)); 638 } 639 640 int 641 sys__lwp_unpark_all(struct lwp *l, const struct sys__lwp_unpark_all_args *uap, 642 register_t *retval) 643 { 644 /* { 645 syscallarg(const lwpid_t *) targets; 646 syscallarg(size_t) ntargets; 647 syscallarg(const void *) hint; 648 } */ 649 struct proc *p; 650 struct lwp *t; 651 sleepq_t *sq; 652 wchan_t wchan; 653 lwpid_t targets[32], *tp, *tpp, *tmax, target; 654 int error; 655 kmutex_t *mp; 656 u_int ntargets; 657 size_t sz; 658 659 p = l->l_proc; 660 ntargets = SCARG(uap, ntargets); 661 662 if (SCARG(uap, targets) == NULL) { 663 /* 664 * Let the caller know how much we are willing to do, and 665 * let it unpark the LWPs in blocks. 666 */ 667 *retval = LWP_UNPARK_MAX; 668 return 0; 669 } 670 if (ntargets > LWP_UNPARK_MAX || ntargets == 0) 671 return EINVAL; 672 673 /* 674 * Copy in the target array. If it's a small number of LWPs, then 675 * place the numbers on the stack. 676 */ 677 sz = sizeof(target) * ntargets; 678 if (sz <= sizeof(targets)) 679 tp = targets; 680 else 681 tp = kmem_alloc(sz, KM_SLEEP); 682 error = copyin(SCARG(uap, targets), tp, sz); 683 if (error != 0) { 684 if (tp != targets) { 685 kmem_free(tp, sz); 686 } 687 return error; 688 } 689 690 wchan = lwp_park_wchan(p, SCARG(uap, hint)); 691 sq = sleeptab_lookup(&lwp_park_tab, wchan, &mp); 692 693 for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) { 694 target = *tpp; 695 696 /* 697 * Easy case: search for the LWP on the sleep queue. If 698 * it's parked, remove it from the queue and set running. 699 */ 700 TAILQ_FOREACH(t, sq, l_sleepchain) 701 if (t->l_proc == p && t->l_lid == target) 702 break; 703 704 if (t != NULL) { 705 sleepq_remove(sq, t); 706 continue; 707 } 708 709 /* 710 * The LWP hasn't parked yet. Take the hit and 711 * mark the operation as pending. 712 */ 713 mutex_spin_exit(mp); 714 mutex_enter(p->p_lock); 715 if ((t = lwp_find(p, target)) == NULL) { 716 mutex_exit(p->p_lock); 717 mutex_spin_enter(mp); 718 continue; 719 } 720 lwp_lock(t); 721 722 /* 723 * It may not have parked yet, we may have raced, or 724 * it is parked on a different user sync object. 725 */ 726 if (t->l_syncobj == &lwp_park_sobj) { 727 /* Releases the LWP lock. */ 728 lwp_unsleep(t, true); 729 } else { 730 /* 731 * Set the operation pending. The next call to 732 * _lwp_park will return early. 733 */ 734 t->l_flag |= LW_UNPARKED; 735 lwp_unlock(t); 736 } 737 738 mutex_exit(p->p_lock); 739 mutex_spin_enter(mp); 740 } 741 742 mutex_spin_exit(mp); 743 if (tp != targets) 744 kmem_free(tp, sz); 745 746 return 0; 747 } 748 749 int 750 sys__lwp_setname(struct lwp *l, const struct sys__lwp_setname_args *uap, 751 register_t *retval) 752 { 753 /* { 754 syscallarg(lwpid_t) target; 755 syscallarg(const char *) name; 756 } */ 757 char *name, *oname; 758 lwpid_t target; 759 proc_t *p; 760 lwp_t *t; 761 int error; 762 763 if ((target = SCARG(uap, target)) == 0) 764 target = l->l_lid; 765 766 name = kmem_alloc(MAXCOMLEN, KM_SLEEP); 767 error = copyinstr(SCARG(uap, name), name, MAXCOMLEN, NULL); 768 switch (error) { 769 case ENAMETOOLONG: 770 case 0: 771 name[MAXCOMLEN - 1] = '\0'; 772 break; 773 default: 774 kmem_free(name, MAXCOMLEN); 775 return error; 776 } 777 778 p = curproc; 779 mutex_enter(p->p_lock); 780 if ((t = lwp_find(p, target)) == NULL) { 781 mutex_exit(p->p_lock); 782 kmem_free(name, MAXCOMLEN); 783 return ESRCH; 784 } 785 lwp_lock(t); 786 oname = t->l_name; 787 t->l_name = name; 788 lwp_unlock(t); 789 mutex_exit(p->p_lock); 790 791 if (oname != NULL) 792 kmem_free(oname, MAXCOMLEN); 793 794 return 0; 795 } 796 797 int 798 sys__lwp_getname(struct lwp *l, const struct sys__lwp_getname_args *uap, 799 register_t *retval) 800 { 801 /* { 802 syscallarg(lwpid_t) target; 803 syscallarg(char *) name; 804 syscallarg(size_t) len; 805 } */ 806 char name[MAXCOMLEN]; 807 lwpid_t target; 808 proc_t *p; 809 lwp_t *t; 810 811 if ((target = SCARG(uap, target)) == 0) 812 target = l->l_lid; 813 814 p = curproc; 815 mutex_enter(p->p_lock); 816 if ((t = lwp_find(p, target)) == NULL) { 817 mutex_exit(p->p_lock); 818 return ESRCH; 819 } 820 lwp_lock(t); 821 if (t->l_name == NULL) 822 name[0] = '\0'; 823 else 824 strlcpy(name, t->l_name, sizeof(name)); 825 lwp_unlock(t); 826 mutex_exit(p->p_lock); 827 828 return copyoutstr(name, SCARG(uap, name), SCARG(uap, len), NULL); 829 } 830 831 int 832 sys__lwp_ctl(struct lwp *l, const struct sys__lwp_ctl_args *uap, 833 register_t *retval) 834 { 835 /* { 836 syscallarg(int) features; 837 syscallarg(struct lwpctl **) address; 838 } */ 839 int error, features; 840 vaddr_t vaddr; 841 842 features = SCARG(uap, features); 843 features &= ~(LWPCTL_FEATURE_CURCPU | LWPCTL_FEATURE_PCTR); 844 if (features != 0) 845 return ENODEV; 846 if ((error = lwp_ctl_alloc(&vaddr)) != 0) 847 return error; 848 return copyout(&vaddr, SCARG(uap, address), sizeof(void *)); 849 } 850