2 * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kthread.h>
33 #include <sys/mutex.h>
35 #include <sys/resourcevar.h>
36 #include <sys/signalvar.h>
38 #include <sys/unistd.h>
41 #include <machine/stdarg.h>
44 * Start a kernel process. This is called after a fork() call in
45 * mi_startup() in the file kern/init_main.c.
47 * This function is used to start "internal" daemons and intended
48 * to be called from SYSINIT().
54 const struct kproc_desc *kp = udata;
57 error = kthread_create((void (*)(void *))kp->func, NULL,
58 kp->global_procpp, 0, 0, "%s", kp->arg0);
60 panic("kproc_start: %s: error %d", kp->arg0, error);
64 * Create a kernel process/thread/whatever. It shares its address space
65 * with proc0 - ie: kernel only.
67 * func is the function to start.
68 * arg is the parameter to pass to function on first startup.
69 * newpp is the return value pointing to the thread's struct proc.
70 * flags are flags to fork1 (in unistd.h)
71 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
74 kthread_create(void (*func)(void *), void *arg,
75 struct proc **newpp, int flags, int pages, const char *fmt, ...)
82 if (!proc0.p_stats /* || proc0.p_stats->p_start.tv_sec == 0 */)
83 panic("kthread_create called too soon");
85 error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
90 /* save a global descriptor, if desired */
94 /* this is a non-swapped system process */
96 p2->p_flag |= P_SYSTEM | P_KTHREAD;
97 p2->p_procsig->ps_flag |= PS_NOCLDWAIT;
101 /* set up arg0 for 'ps', et al */
103 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
106 /* call the processes' main()... */
107 td = FIRST_THREAD_IN_PROC(p2);
108 cpu_set_fork_handler(td, func, arg);
111 /* Delay putting it on the run queue until now. */
112 mtx_lock_spin(&sched_lock);
113 p2->p_sflag |= PS_INMEM;
114 if (!(flags & RFSTOPPED)) {
117 mtx_unlock_spin(&sched_lock);
123 kthread_exit(int ecode)
130 sx_xlock(&proctree_lock);
132 proc_reparent(p, initproc);
134 sx_xunlock(&proctree_lock);
135 exit1(td, W_EXITCODE(ecode, 0));
139 * Advise a kernel process to suspend (or resume) in its main loop.
140 * Participation is voluntary.
143 kthread_suspend(struct proc *p, int timo)
146 * Make sure this is indeed a system process and we can safely
147 * use the p_siglist field.
150 if ((p->p_flag & P_KTHREAD) == 0) {
154 SIGADDSET(p->p_siglist, SIGSTOP);
156 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkt", timo);
160 kthread_resume(struct proc *p)
163 * Make sure this is indeed a system process and we can safely
164 * use the p_siglist field.
167 if ((p->p_flag & P_KTHREAD) == 0) {
171 SIGDELSET(p->p_siglist, SIGSTOP);
173 wakeup(&p->p_siglist);
178 kthread_suspend_check(struct proc *p)
181 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
182 wakeup(&p->p_siglist);
183 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "ktsusp", 0);