kern_kthread.c (fb7c88e695d54999e2cf5363ca5cbebd91ecc63c) kern_kthread.c (9c8b8baa38c9a8135d7602f127cb0c735010837d)
1/*-
1/*
2 * Copyright (c) 1999 Peter Wemm <peter@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, this list of conditions and the following disclaimer.

--- 7 unchanged lines hidden (view full) ---

17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
2 * Copyright (c) 1999 Peter Wemm <peter@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, this list of conditions and the following disclaimer.

--- 7 unchanged lines hidden (view full) ---

17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
25 */
26
27 */
28
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/systm.h>
29#include <sys/param.h>
30#include <sys/systm.h>
32#include <sys/kthread.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
31#include <sys/kernel.h>
35#include <sys/proc.h>
32#include <sys/proc.h>
36#include <sys/resourcevar.h>
37#include <sys/signalvar.h>
38#include <sys/sx.h>
33#include <sys/kthread.h>
39#include <sys/unistd.h>
40#include <sys/wait.h>
34#include <sys/unistd.h>
35#include <sys/wait.h>
41#include <sys/sched.h>
42#include <vm/vm.h>
43#include <vm/vm_extern.h>
44
36
37#include <machine/cpu.h>
45#include <machine/stdarg.h>
46
47/*
48 * Start a kernel process. This is called after a fork() call in
49 * mi_startup() in the file kern/init_main.c.
50 *
51 * This function is used to start "internal" daemons and intended
52 * to be called from SYSINIT().
53 */
54void
55kproc_start(udata)
56 const void *udata;
57{
58 const struct kproc_desc *kp = udata;
59 int error;
60
38#include <machine/stdarg.h>
39
40/*
41 * Start a kernel process. This is called after a fork() call in
42 * mi_startup() in the file kern/init_main.c.
43 *
44 * This function is used to start "internal" daemons and intended
45 * to be called from SYSINIT().
46 */
47void
48kproc_start(udata)
49 const void *udata;
50{
51 const struct kproc_desc *kp = udata;
52 int error;
53
61 error = kproc_create((void (*)(void *))kp->func, NULL,
62 kp->global_procpp, 0, 0, "%s", kp->arg0);
54 error = kthread_create((void (*)(void *))kp->func, NULL,
55 kp->global_procpp, kp->arg0);
63 if (error)
64 panic("kproc_start: %s: error %d", kp->arg0, error);
65}
66
67/*
56 if (error)
57 panic("kproc_start: %s: error %d", kp->arg0, error);
58}
59
60/*
68 * Create a kernel process/thread/whatever. It shares its address space
61 * Create a kernel process/thread/whatever. It shares it's address space
69 * with proc0 - ie: kernel only.
62 * with proc0 - ie: kernel only.
70 *
71 * func is the function to start.
72 * arg is the parameter to pass to function on first startup.
73 * newpp is the return value pointing to the thread's struct proc.
74 * flags are flags to fork1 (in unistd.h)
75 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
76 */
77int
63 */
64int
78kproc_create(void (*func)(void *), void *arg,
79 struct proc **newpp, int flags, int pages, const char *fmt, ...)
65kthread_create(void (*func)(void *), void *arg,
66 struct proc **newpp, const char *fmt, ...)
80{
81 int error;
82 va_list ap;
67{
68 int error;
69 va_list ap;
83 struct thread *td;
84 struct proc *p2;
85
70 struct proc *p2;
71
86 if (!proc0.p_stats)
87 panic("kproc_create called too soon");
88
89 error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
90 pages, &p2);
72 error = fork1(&proc0, RFMEM | RFFDG | RFPROC, &p2);
91 if (error)
92 return error;
93
94 /* save a global descriptor, if desired */
95 if (newpp != NULL)
96 *newpp = p2;
97
98 /* this is a non-swapped system process */
73 if (error)
74 return error;
75
76 /* save a global descriptor, if desired */
77 if (newpp != NULL)
78 *newpp = p2;
79
80 /* this is a non-swapped system process */
99 PROC_LOCK(p2);
100 td = FIRST_THREAD_IN_PROC(p2);
101 p2->p_flag |= P_SYSTEM | P_KTHREAD;
102 td->td_pflags |= TDP_KTHREAD;
103 mtx_lock(&p2->p_sigacts->ps_mtx);
104 p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
105 mtx_unlock(&p2->p_sigacts->ps_mtx);
106 PROC_UNLOCK(p2);
81 p2->p_flag |= P_INMEM | P_SYSTEM | P_NOCLDWAIT;
82 PHOLD(p2);
107
108 /* set up arg0 for 'ps', et al */
109 va_start(ap, fmt);
110 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
111 va_end(ap);
83
84 /* set up arg0 for 'ps', et al */
85 va_start(ap, fmt);
86 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
87 va_end(ap);
112 /* set up arg0 for 'ps', et al */
113 va_start(ap, fmt);
114 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
115 va_end(ap);
116
117 /* call the processes' main()... */
88
89 /* call the processes' main()... */
118 cpu_set_fork_handler(td, func, arg);
119 TD_SET_CAN_RUN(td);
90 cpu_set_fork_handler(p2, func, arg);
120
91
121 /* Delay putting it on the run queue until now. */
122 if (!(flags & RFSTOPPED)) {
123 thread_lock(td);
124 sched_add(td, SRQ_BORING);
125 thread_unlock(td);
126 }
127
128 return 0;
129}
130
131void
92 return 0;
93}
94
95void
132kproc_exit(int ecode)
96kthread_exit(int ecode)
133{
97{
134 struct thread *td;
135 struct proc *p;
136
137 td = curthread;
138 p = td->td_proc;
139
140 /*
141 * Reparent curthread from proc0 to init so that the zombie
142 * is harvested.
143 */
144 sx_xlock(&proctree_lock);
145 PROC_LOCK(p);
146 proc_reparent(p, initproc);
147 PROC_UNLOCK(p);
148 sx_xunlock(&proctree_lock);
149
150 /*
151 * Wakeup anyone waiting for us to exit.
152 */
153 wakeup(p);
154
155 /* Buh-bye! */
156 exit1(td, W_EXITCODE(ecode, 0));
98 exit1(curproc, W_EXITCODE(ecode, 0));
157}
158
99}
100
159/*
160 * Advise a kernel process to suspend (or resume) in its main loop.
161 * Participation is voluntary.
162 */
163int
164kproc_suspend(struct proc *p, int timo)
165{
166 /*
167 * Make sure this is indeed a system process and we can safely
168 * use the p_siglist field.
169 */
170 PROC_LOCK(p);
171 if ((p->p_flag & P_KTHREAD) == 0) {
172 PROC_UNLOCK(p);
173 return (EINVAL);
174 }
175 SIGADDSET(p->p_siglist, SIGSTOP);
176 wakeup(p);
177 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo);
178}
179
180int
181kproc_resume(struct proc *p)
182{
183 /*
184 * Make sure this is indeed a system process and we can safely
185 * use the p_siglist field.
186 */
187 PROC_LOCK(p);
188 if ((p->p_flag & P_KTHREAD) == 0) {
189 PROC_UNLOCK(p);
190 return (EINVAL);
191 }
192 SIGDELSET(p->p_siglist, SIGSTOP);
193 PROC_UNLOCK(p);
194 wakeup(&p->p_siglist);
195 return (0);
196}
197
198void
199kproc_suspend_check(struct proc *p)
200{
201 PROC_LOCK(p);
202 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
203 wakeup(&p->p_siglist);
204 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
205 }
206 PROC_UNLOCK(p);
207}
208
209
210/*
211 * Start a kernel thread.
212 *
213 * This function is used to start "internal" daemons and intended
214 * to be called from SYSINIT().
215 */
216
217void
218kthread_start(udata)
219 const void *udata;
220{
221 const struct kthread_desc *kp = udata;
222 int error;
223
224 error = kthread_add((void (*)(void *))kp->func, NULL,
225 NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
226 if (error)
227 panic("kthread_start: %s: error %d", kp->arg0, error);
228}
229
230/*
231 * Create a kernel thread. It shares its address space
232 * with proc0 - ie: kernel only.
233 *
234 * func is the function to start.
235 * arg is the parameter to pass to function on first startup.
236 * newtdp is the return value pointing to the thread's struct thread.
237 * ** XXX fix this --> flags are flags to fork1 (in unistd.h)
238 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
239 */
240int
241kthread_add(void (*func)(void *), void *arg, struct proc *p,
242 struct thread **newtdp, int flags, int pages, const char *fmt, ...)
243{
244 va_list ap;
245 struct thread *newtd, *oldtd;
246
247 if (!proc0.p_stats)
248 panic("kthread_add called too soon");
249
250 /* If no process supplied, put it on proc0 */
251 if (p == NULL) {
252 p = &proc0;
253 oldtd = &thread0;
254 } else {
255 oldtd = FIRST_THREAD_IN_PROC(p);
256 }
257
258 /* Initialize our new td */
259 newtd = thread_alloc(pages);
260 if (newtd == NULL)
261 return (ENOMEM);
262
263 bzero(&newtd->td_startzero,
264 __rangeof(struct thread, td_startzero, td_endzero));
265/* XXX check if we should zero. */
266 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
267 __rangeof(struct thread, td_startcopy, td_endcopy));
268
269 /* set up arg0 for 'ps', et al */
270 va_start(ap, fmt);
271 vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap);
272 va_end(ap);
273
274 newtd->td_proc = p; /* needed for cpu_set_upcall */
275
276 /* XXX optimise this probably? */
277 /* On x86 (and probably the others too) it is way too full of junk */
278 /* Needs a better name */
279 cpu_set_upcall(newtd, oldtd);
280 /* put the designated function(arg) as the resume context */
281 cpu_set_fork_handler(newtd, func, arg);
282
283 newtd->td_pflags |= TDP_KTHREAD;
284 newtd->td_ucred = crhold(p->p_ucred);
285
286 /* this code almost the same as create_thread() in kern_thr.c */
287 PROC_LOCK(p);
288 p->p_flag |= P_HADTHREADS;
289 newtd->td_sigmask = oldtd->td_sigmask; /* XXX dubious */
290 thread_link(newtd, p);
291 thread_lock(oldtd);
292 /* let the scheduler know about these things. */
293 sched_fork_thread(oldtd, newtd);
294 TD_SET_CAN_RUN(newtd);
295 thread_unlock(oldtd);
296 PROC_UNLOCK(p);
297
298
299 /* Delay putting it on the run queue until now. */
300 if (!(flags & RFSTOPPED)) {
301 thread_lock(newtd);
302 sched_add(newtd, SRQ_BORING);
303 thread_unlock(newtd);
304 }
305 if (newtdp)
306 *newtdp = newtd;
307 return 0;
308}
309
310void
311kthread_exit(void)
312{
313 struct proc *p;
314
315 p = curthread->td_proc;
316
317 /* A module may be waiting for us to exit. */
318 wakeup(curthread);
319 PROC_LOCK(p);
320 if (p->p_numthreads == 1) {
321 PROC_UNLOCK(p);
322 kproc_exit(0);
323
324 /* NOTREACHED. */
325 }
326 PROC_SLOCK(p);
327 thread_exit();
328}
329
330/*
331 * Advise a kernel process to suspend (or resume) in its main loop.
332 * Participation is voluntary.
333 */
334int
335kthread_suspend(struct thread *td, int timo)
336{
337 struct proc *p;
338
339 p = td->td_proc;
340
341 /*
342 * td_pflags should not be read by any thread other than
343 * curthread, but as long as this flag is invariant during the
344 * thread's lifetime, it is OK to check its state.
345 */
346 if ((td->td_pflags & TDP_KTHREAD) == 0)
347 return (EINVAL);
348
349 /*
350 * The caller of the primitive should have already checked that the
351 * thread is up and running, thus not being blocked by other
352 * conditions.
353 */
354 PROC_LOCK(p);
355 thread_lock(td);
356 td->td_flags |= TDF_KTH_SUSP;
357 thread_unlock(td);
358 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
359 timo));
360}
361
362/*
363 * Resume a thread previously put asleep with kthread_suspend().
364 */
365int
366kthread_resume(struct thread *td)
367{
368 struct proc *p;
369
370 p = td->td_proc;
371
372 /*
373 * td_pflags should not be read by any thread other than
374 * curthread, but as long as this flag is invariant during the
375 * thread's lifetime, it is OK to check its state.
376 */
377 if ((td->td_pflags & TDP_KTHREAD) == 0)
378 return (EINVAL);
379
380 PROC_LOCK(p);
381 thread_lock(td);
382 td->td_flags &= ~TDF_KTH_SUSP;
383 thread_unlock(td);
384 wakeup(&td->td_flags);
385 PROC_UNLOCK(p);
386 return (0);
387}
388
389/*
390 * Used by the thread to poll as to whether it should yield/sleep
391 * and notify the caller that is has happened.
392 */
393void
394kthread_suspend_check()
395{
396 struct proc *p;
397 struct thread *td;
398
399 td = curthread;
400 p = td->td_proc;
401
402 if ((td->td_pflags & TDP_KTHREAD) == 0)
403 panic("%s: curthread is not a valid kthread", __func__);
404
405 /*
406 * As long as the double-lock protection is used when accessing the
407 * TDF_KTH_SUSP flag, synchronizing the read operation via proc mutex
408 * is fine.
409 */
410 PROC_LOCK(p);
411 while (td->td_flags & TDF_KTH_SUSP) {
412 wakeup(&td->td_flags);
413 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
414 }
415 PROC_UNLOCK(p);
416}
417
418int
419kproc_kthread_add(void (*func)(void *), void *arg,
420 struct proc **procptr, struct thread **tdptr,
421 int flags, int pages, char * procname, const char *fmt, ...)
422{
423 int error;
424 va_list ap;
425 char buf[100];
426 struct thread *td;
427
428 if (*procptr == 0) {
429 error = kproc_create(func, arg,
430 procptr, flags, pages, "%s", procname);
431 if (error)
432 return (error);
433 td = FIRST_THREAD_IN_PROC(*procptr);
434 if (tdptr)
435 *tdptr = td;
436 va_start(ap, fmt);
437 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
438 va_end(ap);
439 return (0);
440 }
441 va_start(ap, fmt);
442 vsnprintf(buf, sizeof(buf), fmt, ap);
443 va_end(ap);
444 error = kthread_add(func, arg, *procptr,
445 tdptr, flags, pages, "%s", buf);
446 return (error);
447}