Home | History | Annotate | Line # | Download | only in kern
kern_kthread.c revision 1.16.6.2
      1  1.16.6.2       ad /*	$NetBSD: kern_kthread.c,v 1.16.6.2 2007/04/10 12:07:13 ad Exp $	*/
      2       1.1  thorpej 
      3       1.1  thorpej /*-
      4  1.16.6.1       ad  * Copyright (c) 1998, 1999, 2007 The NetBSD Foundation, Inc.
      5       1.1  thorpej  * All rights reserved.
      6       1.1  thorpej  *
      7       1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.16.6.1       ad  * NASA Ames Research Center, and by Andrew Doran.
     10       1.1  thorpej  *
     11       1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     12       1.1  thorpej  * modification, are permitted provided that the following conditions
     13       1.1  thorpej  * are met:
     14       1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     15       1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     16       1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18       1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     19       1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     20       1.1  thorpej  *    must display the following acknowledgement:
     21       1.1  thorpej  *	This product includes software developed by the NetBSD
     22       1.1  thorpej  *	Foundation, Inc. and its contributors.
     23       1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1  thorpej  *    contributors may be used to endorse or promote products derived
     25       1.1  thorpej  *    from this software without specific prior written permission.
     26       1.1  thorpej  *
     27       1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1  thorpej  */
     39      1.12    lukem 
     40      1.12    lukem #include <sys/cdefs.h>
     41  1.16.6.2       ad __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.16.6.2 2007/04/10 12:07:13 ad Exp $");
     42       1.1  thorpej 
     43       1.1  thorpej #include <sys/param.h>
     44       1.1  thorpej #include <sys/systm.h>
     45       1.1  thorpej #include <sys/kernel.h>
     46       1.1  thorpej #include <sys/kthread.h>
     47       1.1  thorpej #include <sys/proc.h>
     48       1.1  thorpej #include <sys/wait.h>
     49       1.2  thorpej #include <sys/malloc.h>
     50       1.2  thorpej #include <sys/queue.h>
     51       1.1  thorpej 
     52  1.16.6.1       ad #include <uvm/uvm_extern.h>
     53  1.16.6.1       ad 
     54       1.1  thorpej /*
     55       1.1  thorpej  * note that stdarg.h and the ansi style va_start macro is used for both
     56       1.1  thorpej  * ansi and traditional c complers.
     57       1.1  thorpej  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     58       1.1  thorpej  */
     59       1.1  thorpej #include <machine/stdarg.h>
     60       1.1  thorpej 
     61       1.1  thorpej /*
     62       1.1  thorpej  * Fork a kernel thread.  Any process can request this to be done.
     63       1.1  thorpej  */
     64       1.1  thorpej int
     65  1.16.6.2       ad kthread_create(pri_t pri, bool mpsafe, void (*func)(void *), void *arg,
     66  1.16.6.2       ad 	       struct lwp **lp, const char *fmt, ...)
     67       1.1  thorpej {
     68  1.16.6.1       ad 	struct lwp *l;
     69  1.16.6.1       ad 	vaddr_t uaddr;
     70  1.16.6.1       ad 	bool inmem;
     71       1.1  thorpej 	int error;
     72       1.1  thorpej 	va_list ap;
     73       1.1  thorpej 
     74  1.16.6.1       ad 	inmem = uvm_uarea_alloc(&uaddr);
     75  1.16.6.1       ad 	if (uaddr == 0)
     76  1.16.6.1       ad 		return ENOMEM;
     77  1.16.6.2       ad 	error = newlwp(&lwp0, &proc0, uaddr, inmem, LWP_DETACHED, NULL, 0,
     78  1.16.6.2       ad 	    func, arg, &l);
     79  1.16.6.1       ad 	if (error) {
     80  1.16.6.1       ad 		/* XXX uvm_uarea_free(uaddr); */
     81  1.16.6.1       ad 		return error;
     82  1.16.6.1       ad 	}
     83  1.16.6.1       ad 
     84  1.16.6.1       ad 	/* Set parameters. */
     85  1.16.6.1       ad 	if (pri == PRI_NONE) {
     86  1.16.6.1       ad 		/* Minimum kernel priority level. */
     87  1.16.6.1       ad 		pri = PUSER - 1;
     88  1.16.6.1       ad 	}
     89  1.16.6.1       ad 	if (mpsafe)
     90  1.16.6.1       ad 		l->l_pflag |= LP_MPSAFE;
     91  1.16.6.1       ad 	if (fmt != NULL) {
     92  1.16.6.1       ad 		va_start(ap, fmt);
     93  1.16.6.1       ad 		vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
     94  1.16.6.1       ad 		va_end(ap);
     95  1.16.6.1       ad 	}
     96  1.16.6.1       ad 
     97  1.16.6.1       ad 	/* Set the new LWP running. */
     98  1.16.6.1       ad 	mutex_enter(&proc0.p_smutex);
     99  1.16.6.1       ad 	lwp_lock(l);
    100  1.16.6.1       ad 	l->l_usrpri = pri;
    101  1.16.6.1       ad 	l->l_priority = pri;
    102  1.16.6.1       ad 	l->l_stat = LSRUN;
    103  1.16.6.1       ad 	proc0.p_nrlwps++;
    104  1.16.6.1       ad 	setrunqueue(l);
    105  1.16.6.1       ad 	lwp_unlock(l);
    106  1.16.6.1       ad 	mutex_exit(&proc0.p_smutex);
    107       1.1  thorpej 
    108       1.1  thorpej 	/* All done! */
    109  1.16.6.1       ad 	if (lp != NULL)
    110  1.16.6.1       ad 		*lp = l;
    111  1.16.6.1       ad 
    112       1.1  thorpej 	return (0);
    113       1.1  thorpej }
    114       1.1  thorpej 
    115       1.1  thorpej /*
    116       1.1  thorpej  * Cause a kernel thread to exit.  Assumes the exiting thread is the
    117       1.1  thorpej  * current context.
    118       1.1  thorpej  */
    119       1.1  thorpej void
    120      1.11  thorpej kthread_exit(int ecode)
    121       1.1  thorpej {
    122  1.16.6.1       ad 	struct lwp *l = curlwp;
    123       1.1  thorpej 
    124  1.16.6.1       ad 	/* We can't do much with the exit code, so just report it. */
    125       1.1  thorpej 	if (ecode != 0)
    126  1.16.6.1       ad 		printf("WARNING: kthread `%s' (%d) exits with status %d\n",
    127  1.16.6.1       ad 		    l->l_name, l->l_lid, ecode);
    128       1.1  thorpej 
    129  1.16.6.2       ad 	/* And exit.. */
    130  1.16.6.1       ad 	lwp_exit(l);
    131       1.1  thorpej 
    132       1.1  thorpej 	/*
    133       1.1  thorpej 	 * XXX Fool the compiler.  Making exit1() __noreturn__ is a can
    134       1.1  thorpej 	 * XXX of worms right now.
    135       1.1  thorpej 	 */
    136  1.16.6.2       ad 	for (;;)
    137  1.16.6.2       ad 		;
    138       1.1  thorpej }
    139