kern_kthread.c revision 48391
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 48391 1999-07-01 13:21:46Z peter $ 2748391Speter */ 2848391Speter 2948391Speter#include <sys/param.h> 3048391Speter#include <sys/systm.h> 3148391Speter#include <sys/kernel.h> 3248391Speter#include <sys/proc.h> 3348391Speter#include <sys/kthread.h> 3448391Speter#include <sys/unistd.h> 3548391Speter#include <sys/wait.h> 3648391Speter 3748391Speter#include <machine/cpu.h> 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 7248391Speter error = fork1(&proc0, RFMEM | RFFDG | RFPROC, &p2); 7348391Speter if (error) 7448391Speter return error; 7548391Speter 7648391Speter /* save a global descriptor, if desired */ 7748391Speter if (newpp != NULL) 7848391Speter *newpp = p2; 7948391Speter 8048391Speter /* this is a non-swapped system process */ 8148391Speter p2->p_flag |= P_INMEM | P_SYSTEM | P_NOCLDWAIT; 8248391Speter PHOLD(p2); 8348391Speter 8448391Speter /* set up arg0 for 'ps', et al */ 8548391Speter va_start(ap, fmt); 8648391Speter vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap); 8748391Speter va_end(ap); 8848391Speter 8948391Speter /* call the processes' main()... */ 9048391Speter cpu_set_fork_handler(p2, func, arg); 9148391Speter 9248391Speter return 0; 9348391Speter} 9448391Speter 9548391Spetervoid 9648391Speterkthread_exit(int ecode) 9748391Speter{ 9848391Speter exit1(curproc, W_EXITCODE(ecode, 0)); 9948391Speter} 10048391Speter 101