kern_thr.c revision 112993
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 * $FreeBSD: head/sys/kern/kern_thr.c 112993 2003-04-02 23:53:30Z peter $
27 *
28 */
29
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/lock.h>
33#include <sys/mutex.h>
34#include <sys/proc.h>
35#include <sys/resourcevar.h>
36#include <sys/sysent.h>
37#include <sys/systm.h>
38#include <sys/sysproto.h>
39#include <sys/signalvar.h>
40#include <sys/ucontext.h>
41#include <sys/thr.h>
42
43#include <machine/frame.h>
44
45/*
46 * Back end support functions.
47 */
48
49void
50thr_exit1(void)
51{
52	struct ksegrp *kg;
53	struct thread *td;
54	struct kse *ke;
55	struct proc *p;
56
57	td = curthread;
58	p = td->td_proc;
59	kg = td->td_ksegrp;
60	ke = td->td_kse;
61
62	mtx_assert(&sched_lock, MA_OWNED);
63	PROC_LOCK_ASSERT(p, MA_OWNED);
64	KASSERT(!mtx_owned(&Giant), ("dying thread owns giant"));
65
66	/*
67	 * Shutting down last thread in the proc.  This will actually
68	 * call exit() in the trampoline when it returns.
69	 */
70	if (p->p_numthreads == 1) {
71		PROC_UNLOCK(p);
72		return;
73	}
74
75	/*
76	 * XXX Undelivered process wide signals should be reposted to the
77	 * proc.
78	 */
79
80	/* Clean up cpu resources. */
81	cpu_thread_exit(td);
82
83	/* XXX make thread_unlink() */
84	TAILQ_REMOVE(&p->p_threads, td, td_plist);
85	p->p_numthreads--;
86	TAILQ_REMOVE(&kg->kg_threads, td, td_kglist);
87	kg->kg_numthreads--;
88
89	ke->ke_state = KES_UNQUEUED;
90	ke->ke_thread = NULL;
91	kse_unlink(ke);
92
93	/*
94	 * If we were stopped while waiting for all threads to exit and this
95	 * is the last thread wakeup the exiting thread.
96	 */
97	if (P_SHOULDSTOP(p) == P_STOPPED_SINGLE)
98		if (p->p_numthreads == 1)
99			thread_unsuspend_one(p->p_singlethread);
100
101	PROC_UNLOCK(p);
102	td->td_kse = NULL;
103	td->td_state = TDS_INACTIVE;
104	td->td_proc = NULL;
105	td->td_ksegrp = NULL;
106	td->td_last_kse = NULL;
107	thread_stash(td);
108
109#if defined(__i386__) || defined(__sparc64__)
110	cpu_throw(td, choosethread());
111#else
112	cpu_throw();
113#endif
114}
115
116#define	RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
117
118/*
119 * System call interface.
120 */
121int
122thr_create(struct thread *td, struct thr_create_args *uap)
123    /* ucontext_t *ctx, thr_id_t *id, int flags */
124{
125	struct kse *ke0;
126	struct thread *td0;
127	ucontext_t ctx;
128	int error;
129
130	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
131		return (error);
132
133	/* Initialize our td. */
134	td0 = thread_alloc();
135
136	/*
137	 * Try the copyout as soon as we allocate the td so we don't have to
138	 * tear things down in a failure case below.
139	 */
140	if ((error = copyout(&td0, uap->id, sizeof(thr_id_t)))) {
141		thread_free(td0);
142		return (error);
143	}
144
145	bzero(&td0->td_startzero,
146	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
147	bcopy(&td->td_startcopy, &td0->td_startcopy,
148	    (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
149
150	td0->td_proc = td->td_proc;
151	td0->td_sigmask = td->td_sigmask;
152	bcopy(td->td_frame, td0->td_frame, sizeof(struct trapframe));
153	td0->td_ucred = crhold(td->td_ucred);
154
155	/* Initialize our kse structure. */
156	ke0 = kse_alloc();
157	bzero(&ke0->ke_startzero,
158	    RANGEOF(struct kse, ke_startzero, ke_endzero));
159
160	/* Set up our machine context. */
161	cpu_set_upcall(td0, td->td_pcb);
162	error = set_mcontext(td0, &ctx.uc_mcontext);
163	if (error != 0) {
164		kse_free(ke0);
165		thread_free(td0);
166		goto out;
167	}
168
169	/* Link the thread and kse into the ksegrp and make it runnable. */
170	mtx_lock_spin(&sched_lock);
171
172	thread_link(td0, td->td_ksegrp);
173	kse_link(ke0, td->td_ksegrp);
174
175	/* Bind this thread and kse together. */
176	td0->td_kse = ke0;
177	ke0->ke_thread = td0;
178
179	TD_SET_CAN_RUN(td0);
180	if ((uap->flags & THR_SUSPENDED) == 0)
181		setrunqueue(td0);
182
183	mtx_unlock_spin(&sched_lock);
184
185out:
186	return (error);
187}
188
189int
190thr_self(struct thread *td, struct thr_self_args *uap)
191    /* thr_id_t *id */
192{
193	int error;
194
195	if ((error = copyout(&td, uap->id, sizeof(thr_id_t))))
196		return (error);
197
198	return (0);
199}
200
201int
202thr_exit(struct thread *td, struct thr_exit_args *uap)
203    /* NULL */
204{
205	struct proc *p;
206
207	p = td->td_proc;
208
209	PROC_LOCK(p);
210	mtx_lock_spin(&sched_lock);
211
212	/*
213	 * This unlocks proc and doesn't return unless this is the last
214	 * thread.
215	 */
216	thr_exit1();
217	mtx_unlock_spin(&sched_lock);
218
219	return (0);
220}
221
222int
223thr_kill(struct thread *td, struct thr_kill_args *uap)
224    /* thr_id_t id, int sig */
225{
226	struct thread *ttd;
227	struct proc *p;
228	int error;
229
230	p = td->td_proc;
231	error = 0;
232
233	PROC_LOCK(p);
234
235	FOREACH_THREAD_IN_PROC(p, ttd)
236		if (ttd == uap->id)
237			break;
238
239	if (ttd == NULL) {
240		error = ESRCH;
241		goto out;
242	}
243
244	if (uap->sig == 0)
245		goto out;
246
247	if (!_SIG_VALID(uap->sig)) {
248		error = EINVAL;
249		goto out;
250	}
251
252	/*
253	 * We need a way to force this to go into this thread's siglist.
254	 * Until then blocked signals will go to the proc.
255	 */
256	tdsignal(ttd, uap->sig);
257out:
258	PROC_UNLOCK(p);
259
260	return (error);
261}
262