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