kern_kthread.c revision 114983
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 114983 2003-05-13 20:36:02Z jhb $
2748391Speter */
2848391Speter
2948391Speter#include <sys/param.h>
3048391Speter#include <sys/systm.h>
3148391Speter#include <sys/kthread.h>
3270317Sjake#include <sys/lock.h>
3374927Sjhb#include <sys/mutex.h>
3474927Sjhb#include <sys/proc.h>
3555722Simp#include <sys/resourcevar.h>
3655539Sluoqi#include <sys/signalvar.h>
3774927Sjhb#include <sys/sx.h>
3848391Speter#include <sys/unistd.h>
3948391Speter#include <sys/wait.h>
4048391Speter
4148391Speter#include <machine/stdarg.h>
4248391Speter
4348391Speter/*
4448391Speter * Start a kernel process.  This is called after a fork() call in
4548391Speter * mi_startup() in the file kern/init_main.c.
4648391Speter *
4748391Speter * This function is used to start "internal" daemons and intended
4848391Speter * to be called from SYSINIT().
4948391Speter */
5048391Spetervoid
5148391Speterkproc_start(udata)
5248391Speter	const void *udata;
5348391Speter{
5448391Speter	const struct kproc_desc	*kp = udata;
5548391Speter	int error;
5648391Speter
5748391Speter	error = kthread_create((void (*)(void *))kp->func, NULL,
58104354Sscottl		    kp->global_procpp, 0, 0, "%s", kp->arg0);
5948391Speter	if (error)
6048391Speter		panic("kproc_start: %s: error %d", kp->arg0, error);
6148391Speter}
6248391Speter
6348391Speter/*
6465557Sjasone * Create a kernel process/thread/whatever.  It shares its address space
6548391Speter * with proc0 - ie: kernel only.
6665557Sjasone *
6765557Sjasone * func is the function to start.
6865557Sjasone * arg is the parameter to pass to function on first startup.
6965557Sjasone * newpp is the return value pointing to the thread's struct proc.
7065557Sjasone * flags are flags to fork1 (in unistd.h)
7165557Sjasone * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
7248391Speter */
7348391Speterint
7448391Speterkthread_create(void (*func)(void *), void *arg,
75104354Sscottl    struct proc **newpp, int flags, int pages, const char *fmt, ...)
7648391Speter{
7748391Speter	int error;
7848391Speter	va_list ap;
79103216Sjulian	struct thread *td;
8048391Speter	struct proc *p2;
8148391Speter
82114434Sdes	if (!proc0.p_stats)
8365557Sjasone		panic("kthread_create called too soon");
8465557Sjasone
8590375Speter	error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
86104354Sscottl	    pages, &p2);
8748391Speter	if (error)
8848391Speter		return error;
8948391Speter
9048391Speter	/* save a global descriptor, if desired */
9148391Speter	if (newpp != NULL)
9248391Speter		*newpp = p2;
9348391Speter
9448391Speter	/* this is a non-swapped system process */
9571559Sjhb	PROC_LOCK(p2);
9671559Sjhb	p2->p_flag |= P_SYSTEM | P_KTHREAD;
97114983Sjhb	mtx_lock(&p2->p_sigacts->ps_mtx);
98114983Sjhb	p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
99114983Sjhb	mtx_unlock(&p2->p_sigacts->ps_mtx);
10073911Sjhb	_PHOLD(p2);
10171559Sjhb	PROC_UNLOCK(p2);
10248391Speter
10348391Speter	/* set up arg0 for 'ps', et al */
10448391Speter	va_start(ap, fmt);
10548391Speter	vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
10648391Speter	va_end(ap);
10748391Speter
10848391Speter	/* call the processes' main()... */
109103216Sjulian	td = FIRST_THREAD_IN_PROC(p2);
110103216Sjulian	cpu_set_fork_handler(td, func, arg);
111103216Sjulian	TD_SET_CAN_RUN(td);
11248391Speter
11369657Sjhb	/* Delay putting it on the run queue until now. */
11469657Sjhb	if (!(flags & RFSTOPPED)) {
115113631Sjhb		mtx_lock_spin(&sched_lock);
116103216Sjulian		setrunqueue(td);
117113631Sjhb		mtx_unlock_spin(&sched_lock);
11869657Sjhb	}
11969657Sjhb
12048391Speter	return 0;
12148391Speter}
12248391Speter
12348391Spetervoid
12448391Speterkthread_exit(int ecode)
12548391Speter{
12686293Speter	struct thread *td;
12786293Speter	struct proc *p;
12870317Sjake
12986293Speter	td = curthread;
13086293Speter	p = td->td_proc;
13174927Sjhb	sx_xlock(&proctree_lock);
13286292Sdillon	PROC_LOCK(p);
13386292Sdillon	proc_reparent(p, initproc);
13486292Sdillon	PROC_UNLOCK(p);
13574927Sjhb	sx_xunlock(&proctree_lock);
13686293Speter	exit1(td, W_EXITCODE(ecode, 0));
13748391Speter}
13848391Speter
13955539Sluoqi/*
14055539Sluoqi * Advise a kernel process to suspend (or resume) in its main loop.
14155539Sluoqi * Participation is voluntary.
14255539Sluoqi */
14355539Sluoqiint
14470063Sjhbkthread_suspend(struct proc *p, int timo)
14555539Sluoqi{
14655539Sluoqi	/*
14755539Sluoqi	 * Make sure this is indeed a system process and we can safely
148104306Sjmallett	 * use the p_siglist field.
14955539Sluoqi	 */
15073911Sjhb	PROC_LOCK(p);
15173911Sjhb	if ((p->p_flag & P_KTHREAD) == 0) {
15273911Sjhb		PROC_UNLOCK(p);
15355539Sluoqi		return (EINVAL);
15473911Sjhb	}
155104306Sjmallett	SIGADDSET(p->p_siglist, SIGSTOP);
15688160Speter	wakeup(p);
157104306Sjmallett	return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkt", timo);
15855539Sluoqi}
15955539Sluoqi
16055539Sluoqiint
16170063Sjhbkthread_resume(struct proc *p)
16255539Sluoqi{
16355539Sluoqi	/*
16455539Sluoqi	 * Make sure this is indeed a system process and we can safely
16555539Sluoqi	 * use the p_siglist field.
16655539Sluoqi	 */
16773911Sjhb	PROC_LOCK(p);
16873911Sjhb	if ((p->p_flag & P_KTHREAD) == 0) {
16973911Sjhb		PROC_UNLOCK(p);
17055539Sluoqi		return (EINVAL);
17173911Sjhb	}
172104306Sjmallett	SIGDELSET(p->p_siglist, SIGSTOP);
17373911Sjhb	PROC_UNLOCK(p);
174104306Sjmallett	wakeup(&p->p_siglist);
17555539Sluoqi	return (0);
17655539Sluoqi}
17755539Sluoqi
17855539Sluoqivoid
17970063Sjhbkthread_suspend_check(struct proc *p)
18055539Sluoqi{
18173911Sjhb	PROC_LOCK(p);
182104306Sjmallett	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
183104306Sjmallett		wakeup(&p->p_siglist);
184104306Sjmallett		msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "ktsusp", 0);
18555539Sluoqi	}
18673911Sjhb	PROC_UNLOCK(p);
18755539Sluoqi}
188