xref: /freebsd/sys/kern/kern_thr.c (revision 683697624683d36244876eba620c7a98f419f843)
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$
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 
49 void
50 thr_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 #if 0
105 	td->td_proc = NULL;
106 #endif
107 	td->td_ksegrp = NULL;
108 	td->td_last_kse = NULL;
109 	thread_stash(td);
110 
111 #if defined(__i386__) || defined(__sparc64__)
112 	cpu_throw(td, choosethread());
113 #else
114 	cpu_throw();
115 #endif
116 }
117 
118 #define	RANGEOF(type, start, end) (offsetof(type, end) - offsetof(type, start))
119 
120 /*
121  * System call interface.
122  */
123 int
124 thr_create(struct thread *td, struct thr_create_args *uap)
125     /* ucontext_t *ctx, thr_id_t *id, int flags */
126 {
127 	struct kse *ke0;
128 	struct thread *td0;
129 	ucontext_t ctx;
130 	int error;
131 
132 	if ((error = copyin(uap->ctx, &ctx, sizeof(ctx))))
133 		return (error);
134 
135 	/* Initialize our td. */
136 	td0 = thread_alloc();
137 
138 	/*
139 	 * Try the copyout as soon as we allocate the td so we don't have to
140 	 * tear things down in a failure case below.
141 	 */
142 	if ((error = copyout(&td0, uap->id, sizeof(thr_id_t)))) {
143 		thread_free(td0);
144 		return (error);
145 	}
146 
147 	bzero(&td0->td_startzero,
148 	    (unsigned)RANGEOF(struct thread, td_startzero, td_endzero));
149 	bcopy(&td->td_startcopy, &td0->td_startcopy,
150 	    (unsigned) RANGEOF(struct thread, td_startcopy, td_endcopy));
151 
152 	td0->td_proc = td->td_proc;
153 	td0->td_sigmask = td->td_sigmask;
154 	bcopy(td->td_frame, td0->td_frame, sizeof(struct trapframe));
155 	td0->td_ucred = crhold(td->td_ucred);
156 
157 	/* Initialize our kse structure. */
158 	ke0 = kse_alloc();
159 	bzero(&ke0->ke_startzero,
160 	    RANGEOF(struct kse, ke_startzero, ke_endzero));
161 
162 	/* Set up our machine context. */
163 	cpu_set_upcall(td0, td->td_pcb);
164 	error = set_mcontext(td0, &ctx.uc_mcontext);
165 	if (error != 0) {
166 		kse_free(ke0);
167 		thread_free(td0);
168 		goto out;
169 	}
170 
171 	/* Link the thread and kse into the ksegrp and make it runnable. */
172 	mtx_lock_spin(&sched_lock);
173 
174 	thread_link(td0, td->td_ksegrp);
175 	kse_link(ke0, td->td_ksegrp);
176 
177 	/* Bind this thread and kse together. */
178 	td0->td_kse = ke0;
179 	ke0->ke_thread = td0;
180 
181 	TD_SET_CAN_RUN(td0);
182 	if ((uap->flags & THR_SUSPENDED) == 0)
183 		setrunqueue(td0);
184 
185 	mtx_unlock_spin(&sched_lock);
186 
187 out:
188 	return (error);
189 }
190 
191 int
192 thr_self(struct thread *td, struct thr_self_args *uap)
193     /* thr_id_t *id */
194 {
195 	int error;
196 
197 	if ((error = copyout(&td, uap->id, sizeof(thr_id_t))))
198 		return (error);
199 
200 	return (0);
201 }
202 
203 int
204 thr_exit(struct thread *td, struct thr_exit_args *uap)
205     /* NULL */
206 {
207 	struct proc *p;
208 
209 	p = td->td_proc;
210 
211 	PROC_LOCK(p);
212 	mtx_lock_spin(&sched_lock);
213 
214 	/*
215 	 * This unlocks proc and doesn't return unless this is the last
216 	 * thread.
217 	 */
218 	thr_exit1();
219 	mtx_unlock_spin(&sched_lock);
220 
221 	return (0);
222 }
223 
224 int
225 thr_kill(struct thread *td, struct thr_kill_args *uap)
226     /* thr_id_t id, int sig */
227 {
228 	struct thread *ttd;
229 	struct proc *p;
230 	int error;
231 
232 	p = td->td_proc;
233 	error = 0;
234 
235 	PROC_LOCK(p);
236 
237 	FOREACH_THREAD_IN_PROC(p, ttd)
238 		if (ttd == uap->id)
239 			break;
240 
241 	if (ttd == NULL) {
242 		error = ESRCH;
243 		goto out;
244 	}
245 
246 	if (uap->sig == 0)
247 		goto out;
248 
249 	if (!_SIG_VALID(uap->sig)) {
250 		error = EINVAL;
251 		goto out;
252 	}
253 
254 	/*
255 	 * We need a way to force this to go into this thread's siglist.
256 	 * Until then blocked signals will go to the proc.
257 	 */
258 	tdsignal(ttd, uap->sig);
259 out:
260 	PROC_UNLOCK(p);
261 
262 	return (error);
263 }
264