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