kern_kthread.c revision 71559
1/* 2 * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/kern/kern_kthread.c 71559 2001-01-24 10:47:50Z jhb $ 27 */ 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/proc.h> 32#include <sys/kthread.h> 33#include <sys/lock.h> 34#include <sys/resourcevar.h> 35#include <sys/signalvar.h> 36#include <sys/unistd.h> 37#include <sys/wait.h> 38 39#include <machine/stdarg.h> 40 41/* 42 * Start a kernel process. This is called after a fork() call in 43 * mi_startup() in the file kern/init_main.c. 44 * 45 * This function is used to start "internal" daemons and intended 46 * to be called from SYSINIT(). 47 */ 48void 49kproc_start(udata) 50 const void *udata; 51{ 52 const struct kproc_desc *kp = udata; 53 int error; 54 55 error = kthread_create((void (*)(void *))kp->func, NULL, 56 kp->global_procpp, 0, kp->arg0); 57 if (error) 58 panic("kproc_start: %s: error %d", kp->arg0, error); 59} 60 61/* 62 * Create a kernel process/thread/whatever. It shares its address space 63 * with proc0 - ie: kernel only. 64 * 65 * func is the function to start. 66 * arg is the parameter to pass to function on first startup. 67 * newpp is the return value pointing to the thread's struct proc. 68 * flags are flags to fork1 (in unistd.h) 69 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.). 70 */ 71int 72kthread_create(void (*func)(void *), void *arg, 73 struct proc **newpp, int flags, const char *fmt, ...) 74{ 75 int error; 76 va_list ap; 77 struct proc *p2; 78 79 if (!proc0.p_stats /* || proc0.p_stats->p_start.tv_sec == 0 */) 80 panic("kthread_create called too soon"); 81 82 error = fork1(&proc0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags, &p2); 83 if (error) 84 return error; 85 86 /* save a global descriptor, if desired */ 87 if (newpp != NULL) 88 *newpp = p2; 89 90 /* this is a non-swapped system process */ 91 PROC_LOCK(p2); 92 p2->p_flag |= P_SYSTEM | P_KTHREAD; 93 p2->p_procsig->ps_flag |= PS_NOCLDWAIT; 94 PROC_UNLOCK(p2); 95 PHOLD(p2); 96 97 /* set up arg0 for 'ps', et al */ 98 va_start(ap, fmt); 99 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap); 100 va_end(ap); 101 102 /* call the processes' main()... */ 103 cpu_set_fork_handler(p2, func, arg); 104 105 /* Delay putting it on the run queue until now. */ 106 mtx_enter(&sched_lock, MTX_SPIN); 107 p2->p_sflag |= PS_INMEM; 108 if (!(flags & RFSTOPPED)) { 109 p2->p_stat = SRUN; 110 setrunqueue(p2); 111 } 112 mtx_exit(&sched_lock, MTX_SPIN); 113 114 return 0; 115} 116 117void 118kthread_exit(int ecode) 119{ 120 121 PROCTREE_LOCK(PT_EXCLUSIVE); 122 proc_reparent(curproc, initproc); 123 PROCTREE_LOCK(PT_RELEASE); 124 exit1(curproc, W_EXITCODE(ecode, 0)); 125} 126 127/* 128 * Advise a kernel process to suspend (or resume) in its main loop. 129 * Participation is voluntary. 130 */ 131int 132kthread_suspend(struct proc *p, int timo) 133{ 134 /* 135 * Make sure this is indeed a system process and we can safely 136 * use the p_siglist field. 137 */ 138 if ((p->p_flag & P_SYSTEM) == 0) 139 return (EINVAL); 140 SIGADDSET(p->p_siglist, SIGSTOP); 141 return tsleep((caddr_t)&p->p_siglist, PPAUSE, "suspkt", timo); 142} 143 144int 145kthread_resume(struct proc *p) 146{ 147 /* 148 * Make sure this is indeed a system process and we can safely 149 * use the p_siglist field. 150 */ 151 if ((p->p_flag & P_SYSTEM) == 0) 152 return (EINVAL); 153 SIGDELSET(p->p_siglist, SIGSTOP); 154 wakeup((caddr_t)&p->p_siglist); 155 return (0); 156} 157 158void 159kthread_suspend_check(struct proc *p) 160{ 161 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) { 162 wakeup((caddr_t)&p->p_siglist); 163 tsleep((caddr_t)&p->p_siglist, PPAUSE, "ktsusp", 0); 164 } 165} 166