xref: /freebsd/sys/kern/kern_thr.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
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 #ifdef COMPAT_IA32
57 
58 extern struct sysentvec ia32_freebsd_sysvec;
59 
60 static inline int
61 suword_lwpid(void *addr, lwpid_t lwpid)
62 {
63 	int error;
64 
65 	if (curproc->p_sysent != &ia32_freebsd_sysvec)
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 
78 static int create_thread(struct thread *td, mcontext_t *ctx,
79 			 void (*start_func)(void *), void *arg,
80 			 char *stack_base, size_t stack_size,
81 			 char *tls_base,
82 			 long *child_tid, long *parent_tid,
83 			 int flags, struct rtprio *rtp);
84 
85 /*
86  * System call interface.
87  */
88 int
89 thr_create(struct thread *td, struct thr_create_args *uap)
90     /* ucontext_t *ctx, long *id, int flags */
91 {
92 	ucontext_t ctx;
93 	int error;
94 
95 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
96 		return (error);
97 
98 	error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
99 		NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
100 	return (error);
101 }
102 
103 int
104 thr_new(struct thread *td, struct thr_new_args *uap)
105     /* struct thr_param * */
106 {
107 	struct thr_param param;
108 	int error;
109 
110 	if (uap->param_size < 0 || uap->param_size > sizeof(param))
111 		return (EINVAL);
112 	bzero(&param, sizeof(param));
113 	if ((error = copyin(uap->param, &param, uap->param_size)))
114 		return (error);
115 	return (kern_thr_new(td, &param));
116 }
117 
118 int
119 kern_thr_new(struct thread *td, struct thr_param *param)
120 {
121 	struct rtprio rtp, *rtpp;
122 	int error;
123 
124 	rtpp = NULL;
125 	if (param->rtp != 0) {
126 		error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
127 		rtpp = &rtp;
128 	}
129 	error = create_thread(td, NULL, param->start_func, param->arg,
130 		param->stack_base, param->stack_size, param->tls_base,
131 		param->child_tid, param->parent_tid, param->flags,
132 		rtpp);
133 	return (error);
134 }
135 
136 static int
137 create_thread(struct thread *td, mcontext_t *ctx,
138 	    void (*start_func)(void *), void *arg,
139 	    char *stack_base, size_t stack_size,
140 	    char *tls_base,
141 	    long *child_tid, long *parent_tid,
142 	    int flags, struct rtprio *rtp)
143 {
144 	stack_t stack;
145 	struct thread *newtd;
146 	struct proc *p;
147 	int error;
148 
149 	error = 0;
150 	p = td->td_proc;
151 
152 	/* Have race condition but it is cheap. */
153 	if (p->p_numthreads >= max_threads_per_proc)
154 		return (EPROCLIM);
155 
156 	if (rtp != NULL) {
157 		switch(rtp->type) {
158 		case RTP_PRIO_REALTIME:
159 		case RTP_PRIO_FIFO:
160 			/* Only root can set scheduler policy */
161 			if (priv_check(td, PRIV_SCHED_SETPOLICY) != 0)
162 				return (EPERM);
163 			if (rtp->prio > RTP_PRIO_MAX)
164 				return (EINVAL);
165 			break;
166 		case RTP_PRIO_NORMAL:
167 			rtp->prio = 0;
168 			break;
169 		default:
170 			return (EINVAL);
171 		}
172 	}
173 
174 	/* Initialize our td */
175 	newtd = thread_alloc();
176 
177 	/*
178 	 * Try the copyout as soon as we allocate the td so we don't
179 	 * have to tear things down in a failure case below.
180 	 * Here we copy out tid to two places, one for child and one
181 	 * for parent, because pthread can create a detached thread,
182 	 * if parent wants to safely access child tid, it has to provide
183 	 * its storage, because child thread may exit quickly and
184 	 * memory is freed before parent thread can access it.
185 	 */
186 	if ((child_tid != NULL &&
187 	    suword_lwpid(child_tid, newtd->td_tid)) ||
188 	    (parent_tid != NULL &&
189 	    suword_lwpid(parent_tid, newtd->td_tid))) {
190 		thread_free(newtd);
191 		return (EFAULT);
192 	}
193 
194 	bzero(&newtd->td_startzero,
195 	    __rangeof(struct thread, td_startzero, td_endzero));
196 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
197 	    __rangeof(struct thread, td_startcopy, td_endcopy));
198 	newtd->td_proc = td->td_proc;
199 	newtd->td_ucred = crhold(td->td_ucred);
200 
201 	cpu_set_upcall(newtd, td);
202 
203 	if (ctx != NULL) { /* old way to set user context */
204 		error = set_mcontext(newtd, ctx);
205 		if (error != 0) {
206 			thread_free(newtd);
207 			crfree(td->td_ucred);
208 			return (error);
209 		}
210 	} else {
211 		/* Set up our machine context. */
212 		stack.ss_sp = stack_base;
213 		stack.ss_size = stack_size;
214 		/* Set upcall address to user thread entry function. */
215 		cpu_set_upcall_kse(newtd, start_func, arg, &stack);
216 		/* Setup user TLS address and TLS pointer register. */
217 		error = cpu_set_user_tls(newtd, tls_base);
218 		if (error != 0) {
219 			thread_free(newtd);
220 			crfree(td->td_ucred);
221 			return (error);
222 		}
223 	}
224 
225 	PROC_LOCK(td->td_proc);
226 	td->td_proc->p_flag |= P_HADTHREADS;
227 	newtd->td_sigmask = td->td_sigmask;
228 	PROC_SLOCK(p);
229 	thread_link(newtd, p);
230 	thread_lock(td);
231 	/* let the scheduler know about these things. */
232 	sched_fork_thread(td, newtd);
233 	thread_unlock(td);
234 	PROC_SUNLOCK(p);
235 	PROC_UNLOCK(p);
236 	thread_lock(newtd);
237 	if (rtp != NULL) {
238 		if (!(td->td_pri_class == PRI_TIMESHARE &&
239 		      rtp->type == RTP_PRIO_NORMAL)) {
240 			rtp_to_pri(rtp, newtd);
241 			sched_prio(newtd, newtd->td_user_pri);
242 		} /* ignore timesharing class */
243 	}
244 	TD_SET_CAN_RUN(newtd);
245 	/* if ((flags & THR_SUSPENDED) == 0) */
246 		sched_add(newtd, SRQ_BORING);
247 	thread_unlock(newtd);
248 
249 	return (error);
250 }
251 
252 int
253 thr_self(struct thread *td, struct thr_self_args *uap)
254     /* long *id */
255 {
256 	int error;
257 
258 	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
259 	if (error == -1)
260 		return (EFAULT);
261 	return (0);
262 }
263 
264 int
265 thr_exit(struct thread *td, struct thr_exit_args *uap)
266     /* long *state */
267 {
268 	struct proc *p;
269 
270 	p = td->td_proc;
271 
272 	/* Signal userland that it can free the stack. */
273 	if ((void *)uap->state != NULL) {
274 		suword_lwpid(uap->state, 1);
275 		kern_umtx_wake(td, uap->state, INT_MAX);
276 	}
277 
278 	PROC_LOCK(p);
279 	sigqueue_flush(&td->td_sigqueue);
280 	PROC_SLOCK(p);
281 
282 	/*
283 	 * Shutting down last thread in the proc.  This will actually
284 	 * call exit() in the trampoline when it returns.
285 	 */
286 	if (p->p_numthreads != 1) {
287 		thread_stopped(p);
288 		thread_exit();
289 		/* NOTREACHED */
290 	}
291 	PROC_SUNLOCK(p);
292 	PROC_UNLOCK(p);
293 	return (0);
294 }
295 
296 int
297 thr_kill(struct thread *td, struct thr_kill_args *uap)
298     /* long id, int sig */
299 {
300 	struct thread *ttd;
301 	struct proc *p;
302 	int error;
303 
304 	p = td->td_proc;
305 	error = 0;
306 	PROC_LOCK(p);
307 	if (uap->id == -1) {
308 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
309 			error = EINVAL;
310 		} else {
311 			error = ESRCH;
312 			FOREACH_THREAD_IN_PROC(p, ttd) {
313 				if (ttd != td) {
314 					error = 0;
315 					if (uap->sig == 0)
316 						break;
317 					tdsignal(p, ttd, uap->sig, NULL);
318 				}
319 			}
320 		}
321 	} else {
322 		if (uap->id != td->td_tid)
323 			ttd = thread_find(p, uap->id);
324 		else
325 			ttd = td;
326 		if (ttd == NULL)
327 			error = ESRCH;
328 		else if (uap->sig == 0)
329 			;
330 		else if (!_SIG_VALID(uap->sig))
331 			error = EINVAL;
332 		else
333 			tdsignal(p, ttd, uap->sig, NULL);
334 	}
335 	PROC_UNLOCK(p);
336 	return (error);
337 }
338 
339 int
340 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
341 	/* const struct timespec *timeout */
342 {
343 	struct timespec ts, *tsp;
344 	int error;
345 
346 	error = 0;
347 	tsp = NULL;
348 	if (uap->timeout != NULL) {
349 		error = copyin((const void *)uap->timeout, (void *)&ts,
350 		    sizeof(struct timespec));
351 		if (error != 0)
352 			return (error);
353 		tsp = &ts;
354 	}
355 
356 	return (kern_thr_suspend(td, tsp));
357 }
358 
359 int
360 kern_thr_suspend(struct thread *td, struct timespec *tsp)
361 {
362 	struct timeval tv;
363 	int error = 0, hz = 0;
364 
365 	if (tsp != NULL) {
366 		if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
367 			return (EINVAL);
368 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
369 			return (ETIMEDOUT);
370 		TIMESPEC_TO_TIMEVAL(&tv, tsp);
371 		hz = tvtohz(&tv);
372 	}
373 
374 	if (td->td_pflags & TDP_WAKEUP) {
375 		td->td_pflags &= ~TDP_WAKEUP;
376 		return (0);
377 	}
378 
379 	PROC_LOCK(td->td_proc);
380 	if ((td->td_flags & TDF_THRWAKEUP) == 0)
381 		error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
382 		    hz);
383 	if (td->td_flags & TDF_THRWAKEUP) {
384 		thread_lock(td);
385 		td->td_flags &= ~TDF_THRWAKEUP;
386 		thread_unlock(td);
387 		PROC_UNLOCK(td->td_proc);
388 		return (0);
389 	}
390 	PROC_UNLOCK(td->td_proc);
391 	if (error == EWOULDBLOCK)
392 		error = ETIMEDOUT;
393 	else if (error == ERESTART) {
394 		if (hz != 0)
395 			error = EINTR;
396 	}
397 	return (error);
398 }
399 
400 int
401 thr_wake(struct thread *td, struct thr_wake_args *uap)
402 	/* long id */
403 {
404 	struct proc *p;
405 	struct thread *ttd;
406 
407 	if (uap->id == td->td_tid) {
408 		td->td_pflags |= TDP_WAKEUP;
409 		return (0);
410 	}
411 
412 	p = td->td_proc;
413 	PROC_LOCK(p);
414 	ttd = thread_find(p, uap->id);
415 	if (ttd == NULL) {
416 		PROC_UNLOCK(p);
417 		return (ESRCH);
418 	}
419 	thread_lock(ttd);
420 	ttd->td_flags |= TDF_THRWAKEUP;
421 	thread_unlock(ttd);
422 	wakeup((void *)ttd);
423 	PROC_UNLOCK(p);
424 	return (0);
425 }
426 
427 int
428 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
429 {
430 	struct proc *p = td->td_proc;
431 	char name[MAXCOMLEN + 1];
432 	struct thread *ttd;
433 	int error;
434 
435 	error = 0;
436 	name[0] = '\0';
437 	if (uap->name != NULL) {
438 		error = copyinstr(uap->name, name, sizeof(name),
439 			NULL);
440 		if (error)
441 			return (error);
442 	}
443 	PROC_LOCK(p);
444 	if (uap->id == td->td_tid)
445 		ttd = td;
446 	else
447 		ttd = thread_find(p, uap->id);
448 	if (ttd != NULL)
449 		strcpy(ttd->td_name, name);
450 	else
451 		error = ESRCH;
452 	PROC_UNLOCK(p);
453 	return (error);
454 }
455