kern_kthread.c revision 74927
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.
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/kern/kern_kthread.c 74927 2001-03-28 11:52:56Z jhb $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kthread.h>
32#include <sys/lock.h>
33#include <sys/mutex.h>
34#include <sys/proc.h>
35#include <sys/resourcevar.h>
36#include <sys/signalvar.h>
37#include <sys/sx.h>
38#include <sys/unistd.h>
39#include <sys/wait.h>
40
41#include <machine/stdarg.h>
42
43/*
44 * Start a kernel process.  This is called after a fork() call in
45 * mi_startup() in the file kern/init_main.c.
46 *
47 * This function is used to start "internal" daemons and intended
48 * to be called from SYSINIT().
49 */
50void
51kproc_start(udata)
52	const void *udata;
53{
54	const struct kproc_desc	*kp = udata;
55	int error;
56
57	error = kthread_create((void (*)(void *))kp->func, NULL,
58		    kp->global_procpp, 0, kp->arg0);
59	if (error)
60		panic("kproc_start: %s: error %d", kp->arg0, error);
61}
62
63/*
64 * Create a kernel process/thread/whatever.  It shares its address space
65 * with proc0 - ie: kernel only.
66 *
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.).
72 */
73int
74kthread_create(void (*func)(void *), void *arg,
75    struct proc **newpp, int flags, const char *fmt, ...)
76{
77	int error;
78	va_list ap;
79	struct proc *p2;
80
81	if (!proc0.p_stats /* || proc0.p_stats->p_start.tv_sec == 0 */)
82		panic("kthread_create called too soon");
83
84	error = fork1(&proc0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags, &p2);
85	if (error)
86		return error;
87
88	/* save a global descriptor, if desired */
89	if (newpp != NULL)
90		*newpp = p2;
91
92	/* this is a non-swapped system process */
93	PROC_LOCK(p2);
94	p2->p_flag |= P_SYSTEM | P_KTHREAD;
95	p2->p_procsig->ps_flag |= PS_NOCLDWAIT;
96	_PHOLD(p2);
97	PROC_UNLOCK(p2);
98
99	/* set up arg0 for 'ps', et al */
100	va_start(ap, fmt);
101	vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
102	va_end(ap);
103
104	/* call the processes' main()... */
105	cpu_set_fork_handler(p2, func, arg);
106
107	/* Delay putting it on the run queue until now. */
108	mtx_lock_spin(&sched_lock);
109	p2->p_sflag |= PS_INMEM;
110	if (!(flags & RFSTOPPED)) {
111		p2->p_stat = SRUN;
112		setrunqueue(p2);
113	}
114	mtx_unlock_spin(&sched_lock);
115
116	return 0;
117}
118
119void
120kthread_exit(int ecode)
121{
122
123	sx_xlock(&proctree_lock);
124	PROC_LOCK(curproc);
125	proc_reparent(curproc, initproc);
126	PROC_UNLOCK(curproc);
127	sx_xunlock(&proctree_lock);
128	exit1(curproc, W_EXITCODE(ecode, 0));
129}
130
131/*
132 * Advise a kernel process to suspend (or resume) in its main loop.
133 * Participation is voluntary.
134 */
135int
136kthread_suspend(struct proc *p, int timo)
137{
138	/*
139	 * Make sure this is indeed a system process and we can safely
140	 * use the p_siglist field.
141	 */
142	PROC_LOCK(p);
143	if ((p->p_flag & P_KTHREAD) == 0) {
144		PROC_UNLOCK(p);
145		return (EINVAL);
146	}
147	SIGADDSET(p->p_siglist, SIGSTOP);
148	return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkt", timo);
149}
150
151int
152kthread_resume(struct proc *p)
153{
154	/*
155	 * Make sure this is indeed a system process and we can safely
156	 * use the p_siglist field.
157	 */
158	PROC_LOCK(p);
159	if ((p->p_flag & P_KTHREAD) == 0) {
160		PROC_UNLOCK(p);
161		return (EINVAL);
162	}
163	SIGDELSET(p->p_siglist, SIGSTOP);
164	PROC_UNLOCK(p);
165	wakeup(&p->p_siglist);
166	return (0);
167}
168
169void
170kthread_suspend_check(struct proc *p)
171{
172	PROC_LOCK(p);
173	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
174		wakeup(&p->p_siglist);
175		msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "ktsusp", 0);
176	}
177	PROC_UNLOCK(p);
178}
179