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