xref: /freebsd/sys/kern/kern_thr.c (revision 7bd6fde3)
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/sysent.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/signalvar.h>
47 #include <sys/ucontext.h>
48 #include <sys/thr.h>
49 #include <sys/rtprio.h>
50 #include <sys/umtx.h>
51 #include <sys/limits.h>
52 
53 #include <machine/frame.h>
54 
55 #ifdef COMPAT_IA32
56 
57 extern struct sysentvec ia32_freebsd_sysvec;
58 
59 static inline int
60 suword_lwpid(void *addr, lwpid_t lwpid)
61 {
62 	int error;
63 
64 	if (curproc->p_sysent != &ia32_freebsd_sysvec)
65 		error = suword(addr, lwpid);
66 	else
67 		error = suword32(addr, lwpid);
68 	return (error);
69 }
70 
71 #else
72 #define suword_lwpid	suword
73 #endif
74 
75 extern int max_threads_per_proc;
76 
77 static int create_thread(struct thread *td, mcontext_t *ctx,
78 			 void (*start_func)(void *), void *arg,
79 			 char *stack_base, size_t stack_size,
80 			 char *tls_base,
81 			 long *child_tid, long *parent_tid,
82 			 int flags, struct rtprio *rtp);
83 
84 /*
85  * System call interface.
86  */
87 int
88 thr_create(struct thread *td, struct thr_create_args *uap)
89     /* ucontext_t *ctx, long *id, int flags */
90 {
91 	ucontext_t ctx;
92 	int error;
93 
94 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
95 		return (error);
96 
97 	error = create_thread(td, &ctx.uc_mcontext, NULL, NULL,
98 		NULL, 0, NULL, uap->id, NULL, uap->flags, NULL);
99 	return (error);
100 }
101 
102 int
103 thr_new(struct thread *td, struct thr_new_args *uap)
104     /* struct thr_param * */
105 {
106 	struct thr_param param;
107 	int error;
108 
109 	if (uap->param_size < 0 || uap->param_size > sizeof(param))
110 		return (EINVAL);
111 	bzero(&param, sizeof(param));
112 	if ((error = copyin(uap->param, &param, uap->param_size)))
113 		return (error);
114 	return (kern_thr_new(td, &param));
115 }
116 
117 int
118 kern_thr_new(struct thread *td, struct thr_param *param)
119 {
120 	struct rtprio rtp, *rtpp;
121 	int error;
122 
123 	rtpp = NULL;
124 	if (param->rtp != 0) {
125 		error = copyin(param->rtp, &rtp, sizeof(struct rtprio));
126 		rtpp = &rtp;
127 	}
128 	error = create_thread(td, NULL, param->start_func, param->arg,
129 		param->stack_base, param->stack_size, param->tls_base,
130 		param->child_tid, param->parent_tid, param->flags,
131 		rtpp);
132 	return (error);
133 }
134 
135 static int
136 create_thread(struct thread *td, mcontext_t *ctx,
137 	    void (*start_func)(void *), void *arg,
138 	    char *stack_base, size_t stack_size,
139 	    char *tls_base,
140 	    long *child_tid, long *parent_tid,
141 	    int flags, struct rtprio *rtp)
142 {
143 	stack_t stack;
144 	struct thread *newtd;
145 	struct proc *p;
146 	long id;
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 	id = newtd->td_tid;
187 	if ((child_tid != NULL &&
188 	    suword_lwpid(child_tid, newtd->td_tid)) ||
189 	    (parent_tid != NULL &&
190 	    suword_lwpid(parent_tid, newtd->td_tid))) {
191 		thread_free(newtd);
192 		return (EFAULT);
193 	}
194 
195 	bzero(&newtd->td_startzero,
196 	    __rangeof(struct thread, td_startzero, td_endzero));
197 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
198 	    __rangeof(struct thread, td_startcopy, td_endcopy));
199 	newtd->td_proc = td->td_proc;
200 	newtd->td_ucred = crhold(td->td_ucred);
201 
202 	cpu_set_upcall(newtd, td);
203 
204 	if (ctx != NULL) { /* old way to set user context */
205 		error = set_mcontext(newtd, ctx);
206 		if (error != 0) {
207 			thread_free(newtd);
208 			crfree(td->td_ucred);
209 			return (error);
210 		}
211 	} else {
212 		/* Set up our machine context. */
213 		stack.ss_sp = stack_base;
214 		stack.ss_size = stack_size;
215 		/* Set upcall address to user thread entry function. */
216 		cpu_set_upcall_kse(newtd, start_func, arg, &stack);
217 		/* Setup user TLS address and TLS pointer register. */
218 		error = cpu_set_user_tls(newtd, tls_base);
219 		if (error != 0) {
220 			thread_free(newtd);
221 			crfree(td->td_ucred);
222 			return (error);
223 		}
224 	}
225 
226 	PROC_LOCK(td->td_proc);
227 	td->td_proc->p_flag |= P_HADTHREADS;
228 	newtd->td_sigmask = td->td_sigmask;
229 	mtx_lock_spin(&sched_lock);
230 	thread_link(newtd, p);
231 	PROC_UNLOCK(p);
232 
233 	/* let the scheduler know about these things. */
234 	sched_fork_thread(td, newtd);
235 	if (rtp != NULL) {
236 		if (!(td->td_pri_class == PRI_TIMESHARE &&
237 		      rtp->type == RTP_PRIO_NORMAL)) {
238 			rtp_to_pri(rtp, newtd);
239 			sched_prio(newtd, newtd->td_user_pri);
240 		} /* ignore timesharing class */
241 	}
242 	TD_SET_CAN_RUN(newtd);
243 	/* if ((flags & THR_SUSPENDED) == 0) */
244 		sched_add(newtd, SRQ_BORING);
245 	mtx_unlock_spin(&sched_lock);
246 
247 	return (error);
248 }
249 
250 int
251 thr_self(struct thread *td, struct thr_self_args *uap)
252     /* long *id */
253 {
254 	int error;
255 
256 	error = suword_lwpid(uap->id, (unsigned)td->td_tid);
257 	if (error == -1)
258 		return (EFAULT);
259 	return (0);
260 }
261 
262 int
263 thr_exit(struct thread *td, struct thr_exit_args *uap)
264     /* long *state */
265 {
266 	struct proc *p;
267 
268 	p = td->td_proc;
269 
270 	/* Signal userland that it can free the stack. */
271 	if ((void *)uap->state != NULL) {
272 		suword_lwpid(uap->state, 1);
273 		kern_umtx_wake(td, uap->state, INT_MAX);
274 	}
275 
276 	PROC_LOCK(p);
277 	sigqueue_flush(&td->td_sigqueue);
278 	mtx_lock_spin(&sched_lock);
279 
280 	/*
281 	 * Shutting down last thread in the proc.  This will actually
282 	 * call exit() in the trampoline when it returns.
283 	 */
284 	if (p->p_numthreads != 1) {
285 		thread_stopped(p);
286 		thread_exit();
287 		/* NOTREACHED */
288 	}
289 	mtx_unlock_spin(&sched_lock);
290 	PROC_UNLOCK(p);
291 	return (0);
292 }
293 
294 int
295 thr_kill(struct thread *td, struct thr_kill_args *uap)
296     /* long id, int sig */
297 {
298 	struct thread *ttd;
299 	struct proc *p;
300 	int error;
301 
302 	p = td->td_proc;
303 	error = 0;
304 	PROC_LOCK(p);
305 	if (uap->id == -1) {
306 		if (uap->sig != 0 && !_SIG_VALID(uap->sig)) {
307 			error = EINVAL;
308 		} else {
309 			error = ESRCH;
310 			FOREACH_THREAD_IN_PROC(p, ttd) {
311 				if (ttd != td) {
312 					error = 0;
313 					if (uap->sig == 0)
314 						break;
315 					tdsignal(p, ttd, uap->sig, NULL);
316 				}
317 			}
318 		}
319 	} else {
320 		if (uap->id != td->td_tid)
321 			ttd = thread_find(p, uap->id);
322 		else
323 			ttd = td;
324 		if (ttd == NULL)
325 			error = ESRCH;
326 		else if (uap->sig == 0)
327 			;
328 		else if (!_SIG_VALID(uap->sig))
329 			error = EINVAL;
330 		else
331 			tdsignal(p, ttd, uap->sig, NULL);
332 	}
333 	PROC_UNLOCK(p);
334 	return (error);
335 }
336 
337 int
338 thr_suspend(struct thread *td, struct thr_suspend_args *uap)
339 	/* const struct timespec *timeout */
340 {
341 	struct timespec ts, *tsp;
342 	int error;
343 
344 	error = 0;
345 	tsp = NULL;
346 	if (uap->timeout != NULL) {
347 		error = copyin((const void *)uap->timeout, (void *)&ts,
348 		    sizeof(struct timespec));
349 		if (error != 0)
350 			return (error);
351 		tsp = &ts;
352 	}
353 
354 	return (kern_thr_suspend(td, tsp));
355 }
356 
357 int
358 kern_thr_suspend(struct thread *td, struct timespec *tsp)
359 {
360 	struct timeval tv;
361 	int error = 0, hz = 0;
362 
363 	if (tsp != NULL) {
364 		if (tsp->tv_nsec < 0 || tsp->tv_nsec > 1000000000)
365 			return (EINVAL);
366 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
367 			return (ETIMEDOUT);
368 		TIMESPEC_TO_TIMEVAL(&tv, tsp);
369 		hz = tvtohz(&tv);
370 	}
371 
372 	if (td->td_pflags & TDP_WAKEUP) {
373 		td->td_pflags &= ~TDP_WAKEUP;
374 		return (0);
375 	}
376 
377 	PROC_LOCK(td->td_proc);
378 	if ((td->td_flags & TDF_THRWAKEUP) == 0)
379 		error = msleep((void *)td, &td->td_proc->p_mtx, PCATCH, "lthr",
380 		    hz);
381 	if (td->td_flags & TDF_THRWAKEUP) {
382 		mtx_lock_spin(&sched_lock);
383 		td->td_flags &= ~TDF_THRWAKEUP;
384 		mtx_unlock_spin(&sched_lock);
385 		PROC_UNLOCK(td->td_proc);
386 		return (0);
387 	}
388 	PROC_UNLOCK(td->td_proc);
389 	if (error == EWOULDBLOCK)
390 		error = ETIMEDOUT;
391 	else if (error == ERESTART) {
392 		if (hz != 0)
393 			error = EINTR;
394 	}
395 	return (error);
396 }
397 
398 int
399 thr_wake(struct thread *td, struct thr_wake_args *uap)
400 	/* long id */
401 {
402 	struct proc *p;
403 	struct thread *ttd;
404 
405 	if (uap->id == td->td_tid) {
406 		td->td_pflags |= TDP_WAKEUP;
407 		return (0);
408 	}
409 
410 	p = td->td_proc;
411 	PROC_LOCK(p);
412 	ttd = thread_find(p, uap->id);
413 	if (ttd == NULL) {
414 		PROC_UNLOCK(p);
415 		return (ESRCH);
416 	}
417 	mtx_lock_spin(&sched_lock);
418 	ttd->td_flags |= TDF_THRWAKEUP;
419 	mtx_unlock_spin(&sched_lock);
420 	wakeup((void *)ttd);
421 	PROC_UNLOCK(p);
422 	return (0);
423 }
424 
425 int
426 thr_set_name(struct thread *td, struct thr_set_name_args *uap)
427 {
428 	struct proc *p = td->td_proc;
429 	char name[MAXCOMLEN + 1];
430 	struct thread *ttd;
431 	int error;
432 
433 	error = 0;
434 	name[0] = '\0';
435 	if (uap->name != NULL) {
436 		error = copyinstr(uap->name, name, sizeof(name),
437 			NULL);
438 		if (error)
439 			return (error);
440 	}
441 	PROC_LOCK(p);
442 	if (uap->id == td->td_tid)
443 		ttd = td;
444 	else
445 		ttd = thread_find(p, uap->id);
446 	if (ttd != NULL)
447 		strcpy(ttd->td_name, name);
448 	else
449 		error = ESRCH;
450 	PROC_UNLOCK(p);
451 	return (error);
452 }
453