kern_kthread.c revision 1.29 1 1.29 haad /* $NetBSD: kern_kthread.c,v 1.29 2010/05/12 15:53:20 haad Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.25 ad * Copyright (c) 1998, 1999, 2007, 2009 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.17 ad * NASA Ames Research Center, and by Andrew Doran.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej *
20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.1 thorpej */
32 1.12 lukem
33 1.12 lukem #include <sys/cdefs.h>
34 1.29 haad __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.29 2010/05/12 15:53:20 haad Exp $");
35 1.1 thorpej
36 1.1 thorpej #include <sys/param.h>
37 1.1 thorpej #include <sys/systm.h>
38 1.1 thorpej #include <sys/kernel.h>
39 1.1 thorpej #include <sys/kthread.h>
40 1.1 thorpej #include <sys/proc.h>
41 1.17 ad #include <sys/sched.h>
42 1.17 ad #include <sys/kmem.h>
43 1.17 ad
44 1.17 ad #include <uvm/uvm_extern.h>
45 1.1 thorpej
46 1.1 thorpej /*
47 1.1 thorpej * note that stdarg.h and the ansi style va_start macro is used for both
48 1.1 thorpej * ansi and traditional c complers.
49 1.1 thorpej * XXX: this requires that stdarg.h define: va_alist and va_dcl
50 1.1 thorpej */
51 1.1 thorpej #include <machine/stdarg.h>
52 1.1 thorpej
53 1.1 thorpej /*
54 1.1 thorpej * Fork a kernel thread. Any process can request this to be done.
55 1.28 haad *
56 1.28 haad * With joinable kthreads KTHREAD_JOINABLE flag this should be known.
57 1.29 haad * 1. If you specify KTHREAD_JOINABLE, you must call kthread_join() to reap
58 1.29 haad * the thread. It will not be automatically reaped by the system.
59 1.29 haad * 2. For any given call to kthread_create(KTHREAD_JOINABLE), you may call
60 1.29 haad * kthread_join() only once on the returned lwp_t *.
61 1.1 thorpej */
62 1.1 thorpej int
63 1.17 ad kthread_create(pri_t pri, int flag, struct cpu_info *ci,
64 1.17 ad void (*func)(void *), void *arg,
65 1.17 ad lwp_t **lp, const char *fmt, ...)
66 1.1 thorpej {
67 1.17 ad lwp_t *l;
68 1.17 ad vaddr_t uaddr;
69 1.28 haad int error, lc, lwp_flags;
70 1.1 thorpej va_list ap;
71 1.1 thorpej
72 1.28 haad lwp_flags = LWP_DETACHED;
73 1.28 haad
74 1.27 rmind uaddr = uvm_uarea_alloc();
75 1.27 rmind if (uaddr == 0) {
76 1.17 ad return ENOMEM;
77 1.27 rmind }
78 1.26 agc if ((flag & KTHREAD_TS) != 0) {
79 1.25 ad lc = SCHED_OTHER;
80 1.25 ad } else {
81 1.25 ad lc = SCHED_RR;
82 1.25 ad }
83 1.28 haad
84 1.28 haad if ((flag & KTHREAD_JOINABLE) != 0) {
85 1.28 haad lwp_flags &= ~LWP_DETACHED;
86 1.28 haad }
87 1.28 haad
88 1.28 haad error = lwp_create(&lwp0, &proc0, uaddr, lwp_flags, NULL,
89 1.25 ad 0, func, arg, &l, lc);
90 1.17 ad if (error) {
91 1.27 rmind uvm_uarea_free(uaddr);
92 1.17 ad return error;
93 1.17 ad }
94 1.17 ad if (fmt != NULL) {
95 1.17 ad l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
96 1.17 ad if (l->l_name == NULL) {
97 1.28 haad kthread_destroy(l);
98 1.17 ad return ENOMEM;
99 1.17 ad }
100 1.17 ad va_start(ap, fmt);
101 1.17 ad vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
102 1.17 ad va_end(ap);
103 1.17 ad }
104 1.17 ad
105 1.17 ad /*
106 1.17 ad * Set parameters.
107 1.17 ad */
108 1.17 ad if ((flag & KTHREAD_INTR) != 0) {
109 1.17 ad KASSERT((flag & KTHREAD_MPSAFE) != 0);
110 1.17 ad }
111 1.17 ad
112 1.28 haad /* Joinable kthread can't be NULL. */
113 1.28 haad if ((flag & KTHREAD_JOINABLE) != 0) {
114 1.28 haad KASSERT(l != NULL);
115 1.28 haad }
116 1.28 haad
117 1.17 ad if (pri == PRI_NONE) {
118 1.26 agc if ((flag & KTHREAD_TS) != 0) {
119 1.25 ad /* Maximum user priority level. */
120 1.25 ad pri = MAXPRI_USER;
121 1.25 ad } else {
122 1.25 ad /* Minimum kernel priority level. */
123 1.25 ad pri = PRI_KTHREAD;
124 1.25 ad }
125 1.17 ad }
126 1.23 ad mutex_enter(proc0.p_lock);
127 1.19 ad lwp_lock(l);
128 1.17 ad l->l_priority = pri;
129 1.17 ad if (ci != NULL) {
130 1.17 ad if (ci != l->l_cpu) {
131 1.17 ad lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
132 1.17 ad lwp_lock(l);
133 1.17 ad }
134 1.22 ad l->l_pflag |= LP_BOUND;
135 1.17 ad l->l_cpu = ci;
136 1.17 ad }
137 1.17 ad if ((flag & KTHREAD_INTR) != 0)
138 1.19 ad l->l_pflag |= LP_INTR;
139 1.20 ad if ((flag & KTHREAD_MPSAFE) == 0)
140 1.20 ad l->l_pflag &= ~LP_MPSAFE;
141 1.17 ad
142 1.17 ad /*
143 1.17 ad * Set the new LWP running, unless the caller has requested
144 1.17 ad * otherwise.
145 1.17 ad */
146 1.17 ad if ((flag & KTHREAD_IDLE) == 0) {
147 1.17 ad l->l_stat = LSRUN;
148 1.17 ad sched_enqueue(l, false);
149 1.19 ad lwp_unlock(l);
150 1.19 ad } else
151 1.21 ad lwp_unlock_to(l, ci->ci_schedstate.spc_lwplock);
152 1.17 ad
153 1.17 ad /*
154 1.17 ad * The LWP is not created suspended or stopped and cannot be set
155 1.17 ad * into those states later, so must be considered runnable.
156 1.17 ad */
157 1.17 ad proc0.p_nrlwps++;
158 1.23 ad mutex_exit(proc0.p_lock);
159 1.1 thorpej
160 1.1 thorpej /* All done! */
161 1.17 ad if (lp != NULL)
162 1.17 ad *lp = l;
163 1.17 ad
164 1.1 thorpej return (0);
165 1.1 thorpej }
166 1.1 thorpej
167 1.1 thorpej /*
168 1.1 thorpej * Cause a kernel thread to exit. Assumes the exiting thread is the
169 1.1 thorpej * current context.
170 1.1 thorpej */
171 1.1 thorpej void
172 1.11 thorpej kthread_exit(int ecode)
173 1.1 thorpej {
174 1.18 ad const char *name;
175 1.17 ad lwp_t *l = curlwp;
176 1.1 thorpej
177 1.17 ad /* We can't do much with the exit code, so just report it. */
178 1.18 ad if (ecode != 0) {
179 1.18 ad if ((name = l->l_name) == NULL)
180 1.18 ad name = "unnamed";
181 1.17 ad printf("WARNING: kthread `%s' (%d) exits with status %d\n",
182 1.18 ad name, l->l_lid, ecode);
183 1.18 ad }
184 1.1 thorpej
185 1.17 ad /* And exit.. */
186 1.17 ad lwp_exit(l);
187 1.1 thorpej
188 1.1 thorpej /*
189 1.1 thorpej * XXX Fool the compiler. Making exit1() __noreturn__ is a can
190 1.1 thorpej * XXX of worms right now.
191 1.1 thorpej */
192 1.17 ad for (;;)
193 1.17 ad ;
194 1.2 thorpej }
195 1.2 thorpej
196 1.2 thorpej /*
197 1.17 ad * Destroy an inactive kthread. The kthread must be in the LSIDL state.
198 1.2 thorpej */
199 1.2 thorpej void
200 1.17 ad kthread_destroy(lwp_t *l)
201 1.2 thorpej {
202 1.28 haad proc_t *p;
203 1.28 haad
204 1.17 ad KASSERT((l->l_flag & LW_SYSTEM) != 0);
205 1.17 ad KASSERT(l->l_stat == LSIDL);
206 1.2 thorpej
207 1.28 haad p = l->l_proc;
208 1.28 haad
209 1.28 haad /* Add LRP_DETACHED flag because we can have joinable kthread now. */
210 1.28 haad mutex_enter(p->p_lock);
211 1.28 haad l->l_prflag |= LPR_DETACHED;
212 1.28 haad mutex_exit(p->p_lock);
213 1.28 haad
214 1.17 ad lwp_exit(l);
215 1.1 thorpej }
216 1.28 haad
217 1.28 haad /*
218 1.28 haad * Wait for a kthread to exit, as pthread_join().
219 1.28 haad */
220 1.28 haad int
221 1.28 haad kthread_join(lwp_t *l)
222 1.28 haad {
223 1.28 haad lwpid_t departed;
224 1.28 haad proc_t *p;
225 1.28 haad int error;
226 1.28 haad
227 1.28 haad KASSERT((l->l_flag & LW_SYSTEM) != 0);
228 1.28 haad KASSERT((l->l_prflag & LPR_DETACHED) == 0);
229 1.28 haad
230 1.28 haad p = l->l_proc;
231 1.28 haad
232 1.28 haad mutex_enter(p->p_lock);
233 1.28 haad error = lwp_wait1(curlwp, l->l_lid, &departed, LWPWAIT_EXITCONTROL);
234 1.28 haad mutex_exit(p->p_lock);
235 1.28 haad
236 1.28 haad return error;
237 1.28 haad }
238