kern_kthread.c revision 86292
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 86292 2001-11-12 08:42:20Z dillon $ 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, 5879886Skris kp->global_procpp, 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, 7565557Sjasone struct proc **newpp, int flags, const char *fmt, ...) 7648391Speter{ 7748391Speter int error; 7848391Speter va_list ap; 7948391Speter struct proc *p2; 8048391Speter 8165557Sjasone if (!proc0.p_stats /* || proc0.p_stats->p_start.tv_sec == 0 */) 8265557Sjasone panic("kthread_create called too soon"); 8365557Sjasone 8483366Sjulian error = fork1(thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags, &p2); 8548391Speter if (error) 8648391Speter return error; 8748391Speter 8848391Speter /* save a global descriptor, if desired */ 8948391Speter if (newpp != NULL) 9048391Speter *newpp = p2; 9148391Speter 9248391Speter /* this is a non-swapped system process */ 9371559Sjhb PROC_LOCK(p2); 9471559Sjhb p2->p_flag |= P_SYSTEM | P_KTHREAD; 9552140Sluoqi p2->p_procsig->ps_flag |= PS_NOCLDWAIT; 9673911Sjhb _PHOLD(p2); 9771559Sjhb PROC_UNLOCK(p2); 9848391Speter 9948391Speter /* set up arg0 for 'ps', et al */ 10048391Speter va_start(ap, fmt); 10148391Speter vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap); 10248391Speter va_end(ap); 10348391Speter 10448391Speter /* call the processes' main()... */ 10583366Sjulian cpu_set_fork_handler(&p2->p_thread, func, arg); /* XXXKSE */ 10648391Speter 10769657Sjhb /* Delay putting it on the run queue until now. */ 10872200Sbmilekic mtx_lock_spin(&sched_lock); 10971559Sjhb p2->p_sflag |= PS_INMEM; 11069657Sjhb if (!(flags & RFSTOPPED)) { 11169657Sjhb p2->p_stat = SRUN; 11283366Sjulian setrunqueue(&p2->p_thread); /* XXXKSE */ 11369657Sjhb } 11472200Sbmilekic mtx_unlock_spin(&sched_lock); 11569657Sjhb 11648391Speter return 0; 11748391Speter} 11848391Speter 11948391Spetervoid 12048391Speterkthread_exit(int ecode) 12148391Speter{ 12286292Sdillon struct proc *p = curproc; 12370317Sjake 12474927Sjhb sx_xlock(&proctree_lock); 12586292Sdillon PROC_LOCK(p); 12686292Sdillon proc_reparent(p, initproc); 12786292Sdillon PROC_UNLOCK(p); 12874927Sjhb sx_xunlock(&proctree_lock); 12983366Sjulian exit1(curthread, W_EXITCODE(ecode, 0)); 13048391Speter} 13148391Speter 13255539Sluoqi/* 13355539Sluoqi * Advise a kernel process to suspend (or resume) in its main loop. 13455539Sluoqi * Participation is voluntary. 13555539Sluoqi */ 13655539Sluoqiint 13770063Sjhbkthread_suspend(struct proc *p, int timo) 13855539Sluoqi{ 13955539Sluoqi /* 14055539Sluoqi * Make sure this is indeed a system process and we can safely 14155539Sluoqi * use the p_siglist field. 14255539Sluoqi */ 14373911Sjhb PROC_LOCK(p); 14473911Sjhb if ((p->p_flag & P_KTHREAD) == 0) { 14573911Sjhb PROC_UNLOCK(p); 14655539Sluoqi return (EINVAL); 14773911Sjhb } 14855539Sluoqi SIGADDSET(p->p_siglist, SIGSTOP); 14973911Sjhb return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkt", timo); 15055539Sluoqi} 15155539Sluoqi 15255539Sluoqiint 15370063Sjhbkthread_resume(struct proc *p) 15455539Sluoqi{ 15555539Sluoqi /* 15655539Sluoqi * Make sure this is indeed a system process and we can safely 15755539Sluoqi * use the p_siglist field. 15855539Sluoqi */ 15973911Sjhb PROC_LOCK(p); 16073911Sjhb if ((p->p_flag & P_KTHREAD) == 0) { 16173911Sjhb PROC_UNLOCK(p); 16255539Sluoqi return (EINVAL); 16373911Sjhb } 16455539Sluoqi SIGDELSET(p->p_siglist, SIGSTOP); 16573911Sjhb PROC_UNLOCK(p); 16673911Sjhb wakeup(&p->p_siglist); 16755539Sluoqi return (0); 16855539Sluoqi} 16955539Sluoqi 17055539Sluoqivoid 17170063Sjhbkthread_suspend_check(struct proc *p) 17255539Sluoqi{ 17373911Sjhb PROC_LOCK(p); 17455539Sluoqi while (SIGISMEMBER(p->p_siglist, SIGSTOP)) { 17573911Sjhb wakeup(&p->p_siglist); 17673911Sjhb msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "ktsusp", 0); 17755539Sluoqi } 17873911Sjhb PROC_UNLOCK(p); 17955539Sluoqi} 180