kern_kthread.c revision 69657
148391Speter/*
248391Speter * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
348391Speter * All rights reserved.
448391Speter *
548391Speter * Redistribution and use in source and binary forms, with or without
648391Speter * modification, are permitted provided that the following conditions
748391Speter * are met:
848391Speter * 1. Redistributions of source code must retain the above copyright
948391Speter *    notice, this list of conditions and the following disclaimer.
1048391Speter * 2. Redistributions in binary form must reproduce the above copyright
1148391Speter *    notice, this list of conditions and the following disclaimer in the
1248391Speter *    documentation and/or other materials provided with the distribution.
1348391Speter *
1448391Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1548391Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1648391Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1748391Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1848391Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1948391Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2048391Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2148391Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2248391Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2348391Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2448391Speter * SUCH DAMAGE.
2548391Speter *
2648391Speter * $FreeBSD: head/sys/kern/kern_kthread.c 69657 2000-12-06 03:45:15Z jhb $
2748391Speter */
2848391Speter
2948391Speter#include <sys/param.h>
3048391Speter#include <sys/systm.h>
3148391Speter#include <sys/proc.h>
3248391Speter#include <sys/kthread.h>
3355722Simp#include <sys/resourcevar.h>
3455539Sluoqi#include <sys/signalvar.h>
3548391Speter#include <sys/unistd.h>
3648391Speter#include <sys/wait.h>
3748391Speter
3848391Speter#include <machine/stdarg.h>
3948391Speter
4048391Speter/*
4148391Speter * Start a kernel process.  This is called after a fork() call in
4248391Speter * mi_startup() in the file kern/init_main.c.
4348391Speter *
4448391Speter * This function is used to start "internal" daemons and intended
4548391Speter * to be called from SYSINIT().
4648391Speter */
4748391Spetervoid
4848391Speterkproc_start(udata)
4948391Speter	const void *udata;
5048391Speter{
5148391Speter	const struct kproc_desc	*kp = udata;
5248391Speter	int error;
5348391Speter
5448391Speter	error = kthread_create((void (*)(void *))kp->func, NULL,
5565557Sjasone		    kp->global_procpp, 0, kp->arg0);
5648391Speter	if (error)
5748391Speter		panic("kproc_start: %s: error %d", kp->arg0, error);
5848391Speter}
5948391Speter
6048391Speter/*
6165557Sjasone * Create a kernel process/thread/whatever.  It shares its address space
6248391Speter * with proc0 - ie: kernel only.
6365557Sjasone *
6465557Sjasone * func is the function to start.
6565557Sjasone * arg is the parameter to pass to function on first startup.
6665557Sjasone * newpp is the return value pointing to the thread's struct proc.
6765557Sjasone * flags are flags to fork1 (in unistd.h)
6865557Sjasone * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
6948391Speter */
7048391Speterint
7148391Speterkthread_create(void (*func)(void *), void *arg,
7265557Sjasone    struct proc **newpp, int flags, const char *fmt, ...)
7348391Speter{
7448391Speter	int error;
7548391Speter	va_list ap;
7648391Speter	struct proc *p2;
7748391Speter
7865557Sjasone	if (!proc0.p_stats /* || proc0.p_stats->p_start.tv_sec == 0 */)
7965557Sjasone		panic("kthread_create called too soon");
8065557Sjasone
8169657Sjhb	error = fork1(&proc0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags, &p2);
8248391Speter	if (error)
8348391Speter		return error;
8448391Speter
8548391Speter	/* save a global descriptor, if desired */
8648391Speter	if (newpp != NULL)
8748391Speter		*newpp = p2;
8848391Speter
8948391Speter	/* this is a non-swapped system process */
9052140Sluoqi	p2->p_flag |= P_INMEM | P_SYSTEM;
9152140Sluoqi	p2->p_procsig->ps_flag |= PS_NOCLDWAIT;
9248391Speter	PHOLD(p2);
9348391Speter
9448391Speter	/* set up arg0 for 'ps', et al */
9548391Speter	va_start(ap, fmt);
9648391Speter	vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
9748391Speter	va_end(ap);
9848391Speter
9948391Speter	/* call the processes' main()... */
10048391Speter	cpu_set_fork_handler(p2, func, arg);
10148391Speter
10269657Sjhb	/* Delay putting it on the run queue until now. */
10369657Sjhb	if (!(flags & RFSTOPPED)) {
10469657Sjhb		mtx_enter(&sched_lock, MTX_SPIN);
10569657Sjhb		p2->p_stat = SRUN;
10669657Sjhb		setrunqueue(p2);
10769657Sjhb		mtx_exit(&sched_lock, MTX_SPIN);
10869657Sjhb	}
10969657Sjhb
11048391Speter	return 0;
11148391Speter}
11248391Speter
11348391Spetervoid
11448391Speterkthread_exit(int ecode)
11548391Speter{
11667322Sjhb	proc_reparent(curproc, initproc);
11748391Speter	exit1(curproc, W_EXITCODE(ecode, 0));
11848391Speter}
11948391Speter
12055539Sluoqi/*
12155539Sluoqi * Advise a kernel process to suspend (or resume) in its main loop.
12255539Sluoqi * Participation is voluntary.
12355539Sluoqi */
12455539Sluoqiint
12555539Sluoqisuspend_kproc(struct proc *p, int timo)
12655539Sluoqi{
12755539Sluoqi	/*
12855539Sluoqi	 * Make sure this is indeed a system process and we can safely
12955539Sluoqi	 * use the p_siglist field.
13055539Sluoqi	 */
13155539Sluoqi	if ((p->p_flag & P_SYSTEM) == 0)
13255539Sluoqi		return (EINVAL);
13355539Sluoqi	SIGADDSET(p->p_siglist, SIGSTOP);
13455539Sluoqi	return tsleep((caddr_t)&p->p_siglist, PPAUSE, "suspkp", timo);
13555539Sluoqi}
13655539Sluoqi
13755539Sluoqiint
13855539Sluoqiresume_kproc(struct proc *p)
13955539Sluoqi{
14055539Sluoqi	/*
14155539Sluoqi	 * Make sure this is indeed a system process and we can safely
14255539Sluoqi	 * use the p_siglist field.
14355539Sluoqi	 */
14455539Sluoqi	if ((p->p_flag & P_SYSTEM) == 0)
14555539Sluoqi		return (EINVAL);
14655539Sluoqi	SIGDELSET(p->p_siglist, SIGSTOP);
14755539Sluoqi	wakeup((caddr_t)&p->p_siglist);
14855539Sluoqi	return (0);
14955539Sluoqi}
15055539Sluoqi
15155539Sluoqivoid
15255539Sluoqikproc_suspend_loop(struct proc *p)
15355539Sluoqi{
15455539Sluoqi	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
15555539Sluoqi		wakeup((caddr_t)&p->p_siglist);
15655539Sluoqi		tsleep((caddr_t)&p->p_siglist, PPAUSE, "kpsusp", 0);
15755539Sluoqi	}
15855539Sluoqi}
159