kern_kthread.c revision 1.16.6.1 1 1.16.6.1 ad /* $NetBSD: kern_kthread.c,v 1.16.6.1 2007/04/09 22:10:02 ad Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.16.6.1 ad * Copyright (c) 1998, 1999, 2007 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.16.6.1 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 * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.12 lukem
40 1.12 lukem #include <sys/cdefs.h>
41 1.16.6.1 ad __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.16.6.1 2007/04/09 22:10:02 ad Exp $");
42 1.1 thorpej
43 1.1 thorpej #include <sys/param.h>
44 1.1 thorpej #include <sys/systm.h>
45 1.1 thorpej #include <sys/kernel.h>
46 1.1 thorpej #include <sys/kthread.h>
47 1.1 thorpej #include <sys/proc.h>
48 1.1 thorpej #include <sys/wait.h>
49 1.2 thorpej #include <sys/malloc.h>
50 1.2 thorpej #include <sys/queue.h>
51 1.1 thorpej
52 1.16.6.1 ad #include <uvm/uvm_extern.h>
53 1.16.6.1 ad
54 1.1 thorpej /*
55 1.1 thorpej * note that stdarg.h and the ansi style va_start macro is used for both
56 1.1 thorpej * ansi and traditional c complers.
57 1.1 thorpej * XXX: this requires that stdarg.h define: va_alist and va_dcl
58 1.1 thorpej */
59 1.1 thorpej #include <machine/stdarg.h>
60 1.1 thorpej
61 1.16.6.1 ad bool kthread_create_now;
62 1.8 thorpej
63 1.1 thorpej /*
64 1.1 thorpej * Fork a kernel thread. Any process can request this to be done.
65 1.1 thorpej * The VM space and limits, etc. will be shared with proc0.
66 1.1 thorpej */
67 1.1 thorpej int
68 1.16.6.1 ad kthread_create1(pri_t pri, bool mpsafe, void (*func)(void *), void *arg,
69 1.16.6.1 ad struct lwp **lp, const char *fmt, ...)
70 1.1 thorpej {
71 1.16.6.1 ad struct lwp *l;
72 1.16.6.1 ad vaddr_t uaddr;
73 1.16.6.1 ad bool inmem;
74 1.1 thorpej int error;
75 1.1 thorpej va_list ap;
76 1.1 thorpej
77 1.16.6.1 ad inmem = uvm_uarea_alloc(&uaddr);
78 1.16.6.1 ad if (uaddr == 0)
79 1.16.6.1 ad return ENOMEM;
80 1.16.6.1 ad error = newlwp(&lwp0, &proc0, uaddr, inmem, 0, NULL, 0, func, arg, &l);
81 1.16.6.1 ad if (error) {
82 1.16.6.1 ad /* XXX uvm_uarea_free(uaddr); */
83 1.16.6.1 ad return error;
84 1.16.6.1 ad }
85 1.16.6.1 ad
86 1.16.6.1 ad /* Set parameters. */
87 1.16.6.1 ad if (pri == PRI_NONE) {
88 1.16.6.1 ad /* Minimum kernel priority level. */
89 1.16.6.1 ad pri = PUSER - 1;
90 1.16.6.1 ad }
91 1.16.6.1 ad if (mpsafe)
92 1.16.6.1 ad l->l_pflag |= LP_MPSAFE;
93 1.16.6.1 ad if (fmt != NULL) {
94 1.16.6.1 ad va_start(ap, fmt);
95 1.16.6.1 ad vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
96 1.16.6.1 ad va_end(ap);
97 1.16.6.1 ad }
98 1.16.6.1 ad
99 1.16.6.1 ad /* Set the new LWP running. */
100 1.16.6.1 ad mutex_enter(&proc0.p_smutex);
101 1.16.6.1 ad lwp_lock(l);
102 1.16.6.1 ad l->l_usrpri = pri;
103 1.16.6.1 ad l->l_priority = pri;
104 1.16.6.1 ad l->l_stat = LSRUN;
105 1.16.6.1 ad proc0.p_nrlwps++;
106 1.16.6.1 ad setrunqueue(l);
107 1.16.6.1 ad lwp_unlock(l);
108 1.16.6.1 ad mutex_exit(&proc0.p_smutex);
109 1.1 thorpej
110 1.1 thorpej /* All done! */
111 1.16.6.1 ad if (lp != NULL)
112 1.16.6.1 ad *lp = l;
113 1.16.6.1 ad
114 1.1 thorpej return (0);
115 1.1 thorpej }
116 1.1 thorpej
117 1.1 thorpej /*
118 1.1 thorpej * Cause a kernel thread to exit. Assumes the exiting thread is the
119 1.1 thorpej * current context.
120 1.1 thorpej */
121 1.1 thorpej void
122 1.11 thorpej kthread_exit(int ecode)
123 1.1 thorpej {
124 1.16.6.1 ad struct lwp *l = curlwp;
125 1.1 thorpej
126 1.16.6.1 ad /* We can't do much with the exit code, so just report it. */
127 1.1 thorpej if (ecode != 0)
128 1.16.6.1 ad printf("WARNING: kthread `%s' (%d) exits with status %d\n",
129 1.16.6.1 ad l->l_name, l->l_lid, ecode);
130 1.1 thorpej
131 1.16 ad /* Acquire the sched state mutex. exit1() will release it. */
132 1.16.6.1 ad lwp_exit(l);
133 1.1 thorpej
134 1.1 thorpej /*
135 1.1 thorpej * XXX Fool the compiler. Making exit1() __noreturn__ is a can
136 1.1 thorpej * XXX of worms right now.
137 1.1 thorpej */
138 1.1 thorpej for (;;);
139 1.2 thorpej }
140 1.2 thorpej
141 1.2 thorpej struct kthread_q {
142 1.2 thorpej SIMPLEQ_ENTRY(kthread_q) kq_q;
143 1.11 thorpej void (*kq_func)(void *);
144 1.2 thorpej void *kq_arg;
145 1.2 thorpej };
146 1.2 thorpej
147 1.2 thorpej SIMPLEQ_HEAD(, kthread_q) kthread_q = SIMPLEQ_HEAD_INITIALIZER(kthread_q);
148 1.2 thorpej
149 1.2 thorpej /*
150 1.2 thorpej * Defer the creation of a kernel thread. Once the standard kernel threads
151 1.2 thorpej * and processes have been created, this queue will be run to callback to
152 1.2 thorpej * the caller to create threads for e.g. file systems and device drivers.
153 1.2 thorpej */
154 1.2 thorpej void
155 1.11 thorpej kthread_create(void (*func)(void *), void *arg)
156 1.2 thorpej {
157 1.2 thorpej struct kthread_q *kq;
158 1.2 thorpej
159 1.8 thorpej if (kthread_create_now) {
160 1.8 thorpej (*func)(arg);
161 1.8 thorpej return;
162 1.8 thorpej }
163 1.8 thorpej
164 1.2 thorpej kq = malloc(sizeof(*kq), M_TEMP, M_NOWAIT);
165 1.2 thorpej if (kq == NULL)
166 1.2 thorpej panic("unable to allocate kthread_q");
167 1.2 thorpej memset(kq, 0, sizeof(*kq));
168 1.2 thorpej
169 1.2 thorpej kq->kq_func = func;
170 1.2 thorpej kq->kq_arg = arg;
171 1.2 thorpej
172 1.2 thorpej SIMPLEQ_INSERT_TAIL(&kthread_q, kq, kq_q);
173 1.2 thorpej }
174 1.2 thorpej
175 1.2 thorpej void
176 1.11 thorpej kthread_run_deferred_queue(void)
177 1.2 thorpej {
178 1.2 thorpej struct kthread_q *kq;
179 1.8 thorpej
180 1.8 thorpej /* No longer need to defer kthread creation. */
181 1.16.6.1 ad kthread_create_now = true;
182 1.2 thorpej
183 1.2 thorpej while ((kq = SIMPLEQ_FIRST(&kthread_q)) != NULL) {
184 1.13 lukem SIMPLEQ_REMOVE_HEAD(&kthread_q, kq_q);
185 1.2 thorpej (*kq->kq_func)(kq->kq_arg);
186 1.2 thorpej free(kq, M_TEMP);
187 1.2 thorpej }
188 1.1 thorpej }
189