FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_thr.c
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: releng/8.2/sys/kern/kern_thr.c 213362 2010-10-02 17:41:47Z kib $");
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_FREEBSD32
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 p = td->td_proc;
153
154 /* Have race condition but it is cheap. */
155 if (p->p_numthreads >= max_threads_per_proc) {
156 ++max_threads_hits;
157 return (EPROCLIM);
158 }
159
160 if (rtp != NULL) {
161 switch(rtp->type) {
162 case RTP_PRIO_REALTIME:
163 case RTP_PRIO_FIFO:
164 /* Only root can set scheduler policy */
165 if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
166 return (EPERM);
167 if (rtp->prio > RTP_PRIO_MAX)
168 return (EINVAL);
169 break;
170 case RTP_PRIO_NORMAL:
171 rtp->prio = 0;
172 break;
173 default:
174 return (EINVAL);
175 }
176 }
177
178 /* Initialize our td */
179 newtd = thread_alloc(0);
180 if (newtd == NULL)
181 return (ENOMEM);
182
183 /*
184 * Try the copyout as soon as we allocate the td so we don't
185 * have to tear things down in a failure case below.
186 * Here we copy out tid to two places, one for child and one
187 * for parent, because pthread can create a detached thread,
188 * if parent wants to safely access child tid, it has to provide
189 * its storage, because child thread may exit quickly and
190 * memory is freed before parent thread can access it.
191 */
192 if ((child_tid != NULL &&
193 suword_lwpid(child_tid, newtd->td_tid)) ||
194 (parent_tid != NULL &&
195 suword_lwpid(parent_tid, newtd->td_tid))) {
196 thread_free(newtd);
197 return (EFAULT);
198 }
199
200 bzero(&newtd->td_startzero,
201 __rangeof(struct thread, td_startzero, td_endzero));
202 bzero(&newtd->td_rux, sizeof(newtd->td_rux));
203 newtd->td_map_def_user = NULL;
204 bcopy(&td->td_startcopy, &newtd->td_startcopy,
205 __rangeof(struct thread, td_startcopy, td_endcopy));
206 newtd->td_proc = td->td_proc;
207 newtd->td_ucred = crhold(td->td_ucred);
208
209 cpu_set_upcall(newtd, td);
210
211 if (ctx != NULL) { /* old way to set user context */
212 error = set_mcontext(newtd, ctx);
213 if (error != 0) {
214 thread_free(newtd);
215 crfree(td->td_ucred);
216 return (error);
217 }
218 } else {
219 /* Set up our machine context. */
220 stack.ss_sp = stack_base;
221 stack.ss_size = stack_size;
222 /* Set upcall address to user thread entry function. */
223 cpu_set_upcall_kse(newtd, start_func, arg, &stack);
224 /* Setup user TLS address and TLS pointer register. */
225 error = cpu_set_user_tls(newtd, tls_base);
226 if (error != 0) {
227 thread_free(newtd);
228 crfree(td->td_ucred);
229 return (error);
230 }
231 }
232
233 PROC_LOCK(td->td_proc);
234 td->td_proc->p_flag |= P_HADTHREADS;
235 newtd->td_sigmask = td->td_sigmask;
236 thread_link(newtd, p);
237 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
238 thread_lock(td);
239 /* let the scheduler know about these things. */
240 sched_fork_thread(td, newtd);
241 thread_unlock(td);
242 if (P_SHOULDSTOP(p))
243 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
244 PROC_UNLOCK(p);
245 thread_lock(newtd);
246 if (rtp != NULL) {
247 if (!(td->td_pri_class == PRI_TIMESHARE &&
248 rtp->type == RTP_PRIO_NORMAL)) {
249 rtp_to_pri(rtp, newtd);
250 sched_prio(newtd, newtd->td_user_pri);
251 } /* ignore timesharing class */
252 }
253 TD_SET_CAN_RUN(newtd);
254 sched_add(newtd, SRQ_BORING);
255 thread_unlock(newtd);
256
257 return (0);
258 }
259
260 int
261 thr_self(struct thread *td, struct thr_self_args *uap)
262 /* long *id */
263 {
264 int error;
265
266 error = suword_lwpid(uap->id, (unsigned)td->td_tid);
267 if (error == -1)
268 return (EFAULT);
269 return (0);
270 }
271
272 int
273 thr_exit(struct thread *td, struct thr_exit_args *uap)
274 /* long *state */
275 {
276 struct proc *p;
277
278 p = td->td_proc;
279
280 /* Signal userland that it can free the stack. */
281 if ((void *)uap->state != NULL) {
282 suword_lwpid(uap->state, 1);
283 kern_umtx_wake(td, uap->state, INT_MAX, 0);
284 }
285
286 PROC_LOCK(p);
287 tdsigcleanup(td);
288 PROC_SLOCK(p);
289
290 /*
291 * Shutting down last thread in the proc. This will actually
292 * call exit() in the trampoline when it returns.
293 */
294 if (p->p_numthreads != 1) {
295 thread_stopped(p);
296 thread_exit();
297 /* NOTREACHED */
298 }
299 PROC_SUNLOCK(p);
300 PROC_UNLOCK(p);
301 return (0);
302 }
303
304 int
305 thr_kill(struct thread *td, struct thr_kill_args *uap)
306 /* long id, int sig */
307 {
308 ksiginfo_t ksi;
309 struct thread *ttd;
310 struct proc *p;
311 int error;
312
313 p = td->td_proc;
314 error = 0;
315 ksiginfo_init(&ksi);
316 ksi.ksi_signo = uap->sig;
317 ksi.ksi_code = SI_USER;
318 ksi.ksi_pid = p->p_pid;
319 ksi.ksi_uid = td->td_ucred->cr_ruid;
320 PROC_LOCK(p);
321 if (uap->id == -1) {
322 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
323 error = EINVAL;
324 } else {
325 error = ESRCH;
326 FOREACH_THREAD_IN_PROC(p, ttd) {
327 if (ttd != td) {
328 error = 0;
329 if (uap->sig == 0)
330 break;
331 tdsignal(p, ttd, uap->sig, &ksi);
332 }
333 }
334 }
335 } else {
336 if (uap->id != td->td_tid)
337 ttd = thread_find(p, uap->id);
338 else
339 ttd = td;
340 if (ttd == NULL)
341 error = ESRCH;
342 else if (uap->sig == 0)
343 ;
344 else if (!_SIG_VALID(uap->sig))
345 error = EINVAL;
346 else
347 tdsignal(p, ttd, uap->sig, &ksi);
348 }
349 PROC_UNLOCK(p);
350 return (error);
351 }
352
353 int
354 thr_kill2(struct thread *td, struct thr_kill2_args *uap)
355 /* pid_t pid, long id, int sig */
356 {
357 ksiginfo_t ksi;
358 struct thread *ttd;
359 struct proc *p;
360 int error;
361
362 AUDIT_ARG_SIGNUM(uap->sig);
363
364 if (uap->pid == td->td_proc->p_pid) {
365 p = td->td_proc;
366 PROC_LOCK(p);
367 } else if ((p = pfind(uap->pid)) == NULL) {
368 return (ESRCH);
369 }
370 AUDIT_ARG_PROCESS(p);
371
372 error = p_cansignal(td, p, uap->sig);
373 if (error == 0) {
374 ksiginfo_init(&ksi);
375 ksi.ksi_signo = uap->sig;
376 ksi.ksi_code = SI_USER;
377 ksi.ksi_pid = td->td_proc->p_pid;
378 ksi.ksi_uid = td->td_ucred->cr_ruid;
379 if (uap->id == -1) {
380 if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
381 error = EINVAL;
382 } else {
383 error = ESRCH;
384 FOREACH_THREAD_IN_PROC(p, ttd) {
385 if (ttd != td) {
386 error = 0;
387 if (uap->sig == 0)
388 break;
389 tdsignal(p, ttd, uap->sig,
390 &ksi);
391 }
392 }
393 }
394 } else {
395 if (uap->id != td->td_tid)
396 ttd = thread_find(p, uap->id);
397 else
398 ttd = td;
399 if (ttd == NULL)
400 error = ESRCH;
401 else if (uap->sig == 0)
402 ;
403 else if (!_SIG_VALID(uap->sig))
404 error = EINVAL;
405 else
406 tdsignal(p, ttd, uap->sig, &ksi);
407 }
408 }
409 PROC_UNLOCK(p);
410 return (error);
411 }
412
413 int
414 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
415 /* const struct timespec *timeout */
416 {
417 struct timespec ts, *tsp;
418 int error;
419
420 tsp = NULL;
421 if (uap->timeout != NULL) {
422 error = copyin((const void *)uap->timeout, (void *)&ts,
423 sizeof(struct timespec));
424 if (error != 0)
425 return (error);
426 tsp = &ts;
427 }
428
429 return (kern_thr_suspend(td, tsp));
430 }
431
432 int
433 kern_thr_suspend(struct thread *td, struct timespec *tsp)
434 {
435 struct timeval tv;
436 int error = 0, hz = 0;
437
438 if (tsp != NULL) {
439 if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
440 return (EINVAL);
441 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
442 return (ETIMEDOUT);
443 TIMESPEC_TO_TIMEVAL(&tv, tsp);
444 hz = tvtohz(&tv);
445 }
446
447 if (td->td_pflags & TDP_WAKEUP) {
448 td->td_pflags &= ~TDP_WAKEUP;
449 return (0);
450 }
451
452 PROC_LOCK(td->td_proc);
453 if ((td->td_flags & TDF_THRWAKEUP) == 0)
454 error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
455 hz);
456 if (td->td_flags & TDF_THRWAKEUP) {
457 thread_lock(td);
458 td->td_flags &= ~TDF_THRWAKEUP;
459 thread_unlock(td);
460 PROC_UNLOCK(td->td_proc);
461 return (0);
462 }
463 PROC_UNLOCK(td->td_proc);
464 if (error == EWOULDBLOCK)
465 error = ETIMEDOUT;
466 else if (error == ERESTART) {
467 if (hz != 0)
468 error = EINTR;
469 }
470 return (error);
471 }
472
473 int
474 thr_wake(struct thread *td, struct thr_wake_args *uap)
475 /* long id */
476 {
477 struct proc *p;
478 struct thread *ttd;
479
480 if (uap->id == td->td_tid) {
481 td->td_pflags |= TDP_WAKEUP;
482 return (0);
483 }
484
485 p = td->td_proc;
486 PROC_LOCK(p);
487 ttd = thread_find(p, uap->id);
488 if (ttd == NULL) {
489 PROC_UNLOCK(p);
490 return (ESRCH);
491 }
492 thread_lock(ttd);
493 ttd->td_flags |= TDF_THRWAKEUP;
494 thread_unlock(ttd);
495 wakeup((void *)ttd);
496 PROC_UNLOCK(p);
497 return (0);
498 }
499
500 int
501 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
502 {
503 struct proc *p = td->td_proc;
504 char name[MAXCOMLEN + 1];
505 struct thread *ttd;
506 int error;
507
508 error = 0;
509 name[0] = '\0';
510 if (uap->name != NULL) {
511 error = copyinstr(uap->name, name, sizeof(name),
512 NULL);
513 if (error)
514 return (error);
515 }
516 PROC_LOCK(p);
517 if (uap->id == td->td_tid)
518 ttd = td;
519 else
520 ttd = thread_find(p, uap->id);
521 if (ttd != NULL)
522 strcpy(ttd->td_name, name);
523 else
524 error = ESRCH;
525 PROC_UNLOCK(p);
526 return (error);
527 }
Cache object: 4bf6eba24394d5a2af79ee3bd47f0710
|