kern_kthread.c revision 55722
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 55722 2000-01-10 08:00:58Z imp $
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,
5548391Speter		    kp->global_procpp, kp->arg0);
5648391Speter	if (error)
5748391Speter		panic("kproc_start: %s: error %d", kp->arg0, error);
5848391Speter}
5948391Speter
6048391Speter/*
6148391Speter * Create a kernel process/thread/whatever.  It shares it's address space
6248391Speter * with proc0 - ie: kernel only.
6348391Speter */
6448391Speterint
6548391Speterkthread_create(void (*func)(void *), void *arg,
6648391Speter    struct proc **newpp, const char *fmt, ...)
6748391Speter{
6848391Speter	int error;
6948391Speter	va_list ap;
7048391Speter	struct proc *p2;
7148391Speter
7255722Simp	if (!proc0.p_stats || proc0.p_stats->p_start.tv_sec == 0) {
7355722Simp		panic("kthread_create called too soon");
7455722Simp	}
7555722Simp
7648391Speter	error = fork1(&proc0, RFMEM | RFFDG | RFPROC, &p2);
7748391Speter	if (error)
7848391Speter		return error;
7948391Speter
8048391Speter	/* save a global descriptor, if desired */
8148391Speter	if (newpp != NULL)
8248391Speter		*newpp = p2;
8348391Speter
8448391Speter	/* this is a non-swapped system process */
8552140Sluoqi	p2->p_flag |= P_INMEM | P_SYSTEM;
8652140Sluoqi	p2->p_procsig->ps_flag |= PS_NOCLDWAIT;
8748391Speter	PHOLD(p2);
8848391Speter
8948391Speter	/* set up arg0 for 'ps', et al */
9048391Speter	va_start(ap, fmt);
9148391Speter	vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
9248391Speter	va_end(ap);
9348391Speter
9448391Speter	/* call the processes' main()... */
9548391Speter	cpu_set_fork_handler(p2, func, arg);
9648391Speter
9748391Speter	return 0;
9848391Speter}
9948391Speter
10048391Spetervoid
10148391Speterkthread_exit(int ecode)
10248391Speter{
10348391Speter	exit1(curproc, W_EXITCODE(ecode, 0));
10448391Speter}
10548391Speter
10655539Sluoqi/*
10755539Sluoqi * Advise a kernel process to suspend (or resume) in its main loop.
10855539Sluoqi * Participation is voluntary.
10955539Sluoqi */
11055539Sluoqiint
11155539Sluoqisuspend_kproc(struct proc *p, int timo)
11255539Sluoqi{
11355539Sluoqi	/*
11455539Sluoqi	 * Make sure this is indeed a system process and we can safely
11555539Sluoqi	 * use the p_siglist field.
11655539Sluoqi	 */
11755539Sluoqi	if ((p->p_flag & P_SYSTEM) == 0)
11855539Sluoqi		return (EINVAL);
11955539Sluoqi	SIGADDSET(p->p_siglist, SIGSTOP);
12055539Sluoqi	return tsleep((caddr_t)&p->p_siglist, PPAUSE, "suspkp", timo);
12155539Sluoqi}
12255539Sluoqi
12355539Sluoqiint
12455539Sluoqiresume_kproc(struct proc *p)
12555539Sluoqi{
12655539Sluoqi	/*
12755539Sluoqi	 * Make sure this is indeed a system process and we can safely
12855539Sluoqi	 * use the p_siglist field.
12955539Sluoqi	 */
13055539Sluoqi	if ((p->p_flag & P_SYSTEM) == 0)
13155539Sluoqi		return (EINVAL);
13255539Sluoqi	SIGDELSET(p->p_siglist, SIGSTOP);
13355539Sluoqi	wakeup((caddr_t)&p->p_siglist);
13455539Sluoqi	return (0);
13555539Sluoqi}
13655539Sluoqi
13755539Sluoqivoid
13855539Sluoqikproc_suspend_loop(struct proc *p)
13955539Sluoqi{
14055539Sluoqi	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
14155539Sluoqi		wakeup((caddr_t)&p->p_siglist);
14255539Sluoqi		tsleep((caddr_t)&p->p_siglist, PPAUSE, "kpsusp", 0);
14355539Sluoqi	}
14455539Sluoqi}
145