kern_kthread.c revision 70317
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 70317 2000-12-23 19:43:10Z jake $ 2748391Speter */ 2848391Speter 2948391Speter#include <sys/param.h> 3048391Speter#include <sys/systm.h> 3148391Speter#include <sys/proc.h> 3248391Speter#include <sys/kthread.h> 3370317Sjake#include <sys/lock.h> 3455722Simp#include <sys/resourcevar.h> 3555539Sluoqi#include <sys/signalvar.h> 3648391Speter#include <sys/unistd.h> 3748391Speter#include <sys/wait.h> 3848391Speter 3948391Speter#include <machine/stdarg.h> 4048391Speter 4148391Speter/* 4248391Speter * Start a kernel process. This is called after a fork() call in 4348391Speter * mi_startup() in the file kern/init_main.c. 4448391Speter * 4548391Speter * This function is used to start "internal" daemons and intended 4648391Speter * to be called from SYSINIT(). 4748391Speter */ 4848391Spetervoid 4948391Speterkproc_start(udata) 5048391Speter const void *udata; 5148391Speter{ 5248391Speter const struct kproc_desc *kp = udata; 5348391Speter int error; 5448391Speter 5548391Speter error = kthread_create((void (*)(void *))kp->func, NULL, 5665557Sjasone kp->global_procpp, 0, kp->arg0); 5748391Speter if (error) 5848391Speter panic("kproc_start: %s: error %d", kp->arg0, error); 5948391Speter} 6048391Speter 6148391Speter/* 6265557Sjasone * Create a kernel process/thread/whatever. It shares its address space 6348391Speter * with proc0 - ie: kernel only. 6465557Sjasone * 6565557Sjasone * func is the function to start. 6665557Sjasone * arg is the parameter to pass to function on first startup. 6765557Sjasone * newpp is the return value pointing to the thread's struct proc. 6865557Sjasone * flags are flags to fork1 (in unistd.h) 6965557Sjasone * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.). 7048391Speter */ 7148391Speterint 7248391Speterkthread_create(void (*func)(void *), void *arg, 7365557Sjasone struct proc **newpp, int flags, const char *fmt, ...) 7448391Speter{ 7548391Speter int error; 7648391Speter va_list ap; 7748391Speter struct proc *p2; 7848391Speter 7965557Sjasone if (!proc0.p_stats /* || proc0.p_stats->p_start.tv_sec == 0 */) 8065557Sjasone panic("kthread_create called too soon"); 8165557Sjasone 8269657Sjhb error = fork1(&proc0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags, &p2); 8348391Speter if (error) 8448391Speter return error; 8548391Speter 8648391Speter /* save a global descriptor, if desired */ 8748391Speter if (newpp != NULL) 8848391Speter *newpp = p2; 8948391Speter 9048391Speter /* this is a non-swapped system process */ 9152140Sluoqi p2->p_flag |= P_INMEM | P_SYSTEM; 9252140Sluoqi p2->p_procsig->ps_flag |= PS_NOCLDWAIT; 9348391Speter PHOLD(p2); 9448391Speter 9548391Speter /* set up arg0 for 'ps', et al */ 9648391Speter va_start(ap, fmt); 9748391Speter vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap); 9848391Speter va_end(ap); 9948391Speter 10048391Speter /* call the processes' main()... */ 10148391Speter cpu_set_fork_handler(p2, func, arg); 10248391Speter 10369657Sjhb /* Delay putting it on the run queue until now. */ 10469657Sjhb if (!(flags & RFSTOPPED)) { 10569657Sjhb mtx_enter(&sched_lock, MTX_SPIN); 10669657Sjhb p2->p_stat = SRUN; 10769657Sjhb setrunqueue(p2); 10869657Sjhb mtx_exit(&sched_lock, MTX_SPIN); 10969657Sjhb } 11069657Sjhb 11148391Speter return 0; 11248391Speter} 11348391Speter 11448391Spetervoid 11548391Speterkthread_exit(int ecode) 11648391Speter{ 11770317Sjake 11870317Sjake PROCTREE_LOCK(PT_EXCLUSIVE); 11967322Sjhb proc_reparent(curproc, initproc); 12070317Sjake PROCTREE_LOCK(PT_RELEASE); 12148391Speter exit1(curproc, W_EXITCODE(ecode, 0)); 12248391Speter} 12348391Speter 12455539Sluoqi/* 12555539Sluoqi * Advise a kernel process to suspend (or resume) in its main loop. 12655539Sluoqi * Participation is voluntary. 12755539Sluoqi */ 12855539Sluoqiint 12970063Sjhbkthread_suspend(struct proc *p, int timo) 13055539Sluoqi{ 13155539Sluoqi /* 13255539Sluoqi * Make sure this is indeed a system process and we can safely 13355539Sluoqi * use the p_siglist field. 13455539Sluoqi */ 13555539Sluoqi if ((p->p_flag & P_SYSTEM) == 0) 13655539Sluoqi return (EINVAL); 13755539Sluoqi SIGADDSET(p->p_siglist, SIGSTOP); 13870063Sjhb return tsleep((caddr_t)&p->p_siglist, PPAUSE, "suspkt", timo); 13955539Sluoqi} 14055539Sluoqi 14155539Sluoqiint 14270063Sjhbkthread_resume(struct proc *p) 14355539Sluoqi{ 14455539Sluoqi /* 14555539Sluoqi * Make sure this is indeed a system process and we can safely 14655539Sluoqi * use the p_siglist field. 14755539Sluoqi */ 14855539Sluoqi if ((p->p_flag & P_SYSTEM) == 0) 14955539Sluoqi return (EINVAL); 15055539Sluoqi SIGDELSET(p->p_siglist, SIGSTOP); 15155539Sluoqi wakeup((caddr_t)&p->p_siglist); 15255539Sluoqi return (0); 15355539Sluoqi} 15455539Sluoqi 15555539Sluoqivoid 15670063Sjhbkthread_suspend_check(struct proc *p) 15755539Sluoqi{ 15855539Sluoqi while (SIGISMEMBER(p->p_siglist, SIGSTOP)) { 15955539Sluoqi wakeup((caddr_t)&p->p_siglist); 16070063Sjhb tsleep((caddr_t)&p->p_siglist, PPAUSE, "ktsusp", 0); 16155539Sluoqi } 16255539Sluoqi} 163