Home | History | Annotate | Line # | Download | only in kern
kern_kthread.c revision 1.26
      1  1.26      agc /*	$NetBSD: kern_kthread.c,v 1.26 2009/01/30 04:09:35 agc Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*-
      4  1.25       ad  * Copyright (c) 1998, 1999, 2007, 2009 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.17       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  *
     20   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1  thorpej  */
     32  1.12    lukem 
     33  1.12    lukem #include <sys/cdefs.h>
     34  1.26      agc __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.26 2009/01/30 04:09:35 agc Exp $");
     35   1.1  thorpej 
     36   1.1  thorpej #include <sys/param.h>
     37   1.1  thorpej #include <sys/systm.h>
     38   1.1  thorpej #include <sys/kernel.h>
     39   1.1  thorpej #include <sys/kthread.h>
     40   1.1  thorpej #include <sys/proc.h>
     41  1.17       ad #include <sys/sched.h>
     42  1.17       ad #include <sys/kmem.h>
     43  1.17       ad 
     44  1.17       ad #include <uvm/uvm_extern.h>
     45   1.1  thorpej 
     46   1.1  thorpej /*
     47   1.1  thorpej  * note that stdarg.h and the ansi style va_start macro is used for both
     48   1.1  thorpej  * ansi and traditional c complers.
     49   1.1  thorpej  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     50   1.1  thorpej  */
     51   1.1  thorpej #include <machine/stdarg.h>
     52   1.1  thorpej 
     53   1.1  thorpej /*
     54   1.1  thorpej  * Fork a kernel thread.  Any process can request this to be done.
     55   1.1  thorpej  */
     56   1.1  thorpej int
     57  1.17       ad kthread_create(pri_t pri, int flag, struct cpu_info *ci,
     58  1.17       ad 	       void (*func)(void *), void *arg,
     59  1.17       ad 	       lwp_t **lp, const char *fmt, ...)
     60   1.1  thorpej {
     61  1.17       ad 	lwp_t *l;
     62  1.17       ad 	vaddr_t uaddr;
     63  1.17       ad 	bool inmem;
     64   1.1  thorpej 	int error;
     65   1.1  thorpej 	va_list ap;
     66  1.25       ad 	int lc;
     67   1.1  thorpej 
     68  1.17       ad 	inmem = uvm_uarea_alloc(&uaddr);
     69  1.17       ad 	if (uaddr == 0)
     70  1.17       ad 		return ENOMEM;
     71  1.26      agc 	if ((flag & KTHREAD_TS) != 0) {
     72  1.25       ad 		lc = SCHED_OTHER;
     73  1.25       ad 	} else {
     74  1.25       ad 		lc = SCHED_RR;
     75  1.25       ad 	}
     76  1.19       ad 	error = lwp_create(&lwp0, &proc0, uaddr, inmem, LWP_DETACHED, NULL,
     77  1.25       ad 	    0, func, arg, &l, lc);
     78  1.17       ad 	if (error) {
     79  1.19       ad 		uvm_uarea_free(uaddr, curcpu());
     80  1.17       ad 		return error;
     81  1.17       ad 	}
     82  1.17       ad 	uvm_lwp_hold(l);
     83  1.17       ad 	if (fmt != NULL) {
     84  1.17       ad 		l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
     85  1.17       ad 		if (l->l_name == NULL) {
     86  1.17       ad 			lwp_exit(l);
     87  1.17       ad 			return ENOMEM;
     88  1.17       ad 		}
     89  1.17       ad 		va_start(ap, fmt);
     90  1.17       ad 		vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
     91  1.17       ad 		va_end(ap);
     92  1.17       ad 	}
     93  1.17       ad 
     94  1.17       ad 	/*
     95  1.17       ad 	 * Set parameters.
     96  1.17       ad 	 */
     97  1.17       ad 	if ((flag & KTHREAD_INTR) != 0) {
     98  1.17       ad 		KASSERT((flag & KTHREAD_MPSAFE) != 0);
     99  1.17       ad 	}
    100  1.17       ad 
    101  1.17       ad 	if (pri == PRI_NONE) {
    102  1.26      agc 		if ((flag & KTHREAD_TS) != 0) {
    103  1.25       ad 			/* Maximum user priority level. */
    104  1.25       ad 			pri = MAXPRI_USER;
    105  1.25       ad 		} else {
    106  1.25       ad 			/* Minimum kernel priority level. */
    107  1.25       ad 			pri = PRI_KTHREAD;
    108  1.25       ad 		}
    109  1.17       ad 	}
    110  1.23       ad 	mutex_enter(proc0.p_lock);
    111  1.19       ad 	lwp_lock(l);
    112  1.17       ad 	l->l_priority = pri;
    113  1.17       ad 	if (ci != NULL) {
    114  1.17       ad 		if (ci != l->l_cpu) {
    115  1.17       ad 			lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
    116  1.17       ad 			lwp_lock(l);
    117  1.17       ad 		}
    118  1.22       ad 		l->l_pflag |= LP_BOUND;
    119  1.17       ad 		l->l_cpu = ci;
    120  1.17       ad 	}
    121  1.17       ad 	if ((flag & KTHREAD_INTR) != 0)
    122  1.19       ad 		l->l_pflag |= LP_INTR;
    123  1.20       ad 	if ((flag & KTHREAD_MPSAFE) == 0)
    124  1.20       ad 		l->l_pflag &= ~LP_MPSAFE;
    125  1.17       ad 
    126  1.17       ad 	/*
    127  1.17       ad 	 * Set the new LWP running, unless the caller has requested
    128  1.17       ad 	 * otherwise.
    129  1.17       ad 	 */
    130  1.17       ad 	if ((flag & KTHREAD_IDLE) == 0) {
    131  1.17       ad 		l->l_stat = LSRUN;
    132  1.17       ad 		sched_enqueue(l, false);
    133  1.19       ad 		lwp_unlock(l);
    134  1.19       ad 	} else
    135  1.21       ad 		lwp_unlock_to(l, ci->ci_schedstate.spc_lwplock);
    136  1.17       ad 
    137  1.17       ad 	/*
    138  1.17       ad 	 * The LWP is not created suspended or stopped and cannot be set
    139  1.17       ad 	 * into those states later, so must be considered runnable.
    140  1.17       ad 	 */
    141  1.17       ad 	proc0.p_nrlwps++;
    142  1.23       ad 	mutex_exit(proc0.p_lock);
    143   1.1  thorpej 
    144   1.1  thorpej 	/* All done! */
    145  1.17       ad 	if (lp != NULL)
    146  1.17       ad 		*lp = l;
    147  1.17       ad 
    148   1.1  thorpej 	return (0);
    149   1.1  thorpej }
    150   1.1  thorpej 
    151   1.1  thorpej /*
    152   1.1  thorpej  * Cause a kernel thread to exit.  Assumes the exiting thread is the
    153   1.1  thorpej  * current context.
    154   1.1  thorpej  */
    155   1.1  thorpej void
    156  1.11  thorpej kthread_exit(int ecode)
    157   1.1  thorpej {
    158  1.18       ad 	const char *name;
    159  1.17       ad 	lwp_t *l = curlwp;
    160   1.1  thorpej 
    161  1.17       ad 	/* We can't do much with the exit code, so just report it. */
    162  1.18       ad 	if (ecode != 0) {
    163  1.18       ad 		if ((name = l->l_name) == NULL)
    164  1.18       ad 			name = "unnamed";
    165  1.17       ad 		printf("WARNING: kthread `%s' (%d) exits with status %d\n",
    166  1.18       ad 		    name, l->l_lid, ecode);
    167  1.18       ad 	}
    168   1.1  thorpej 
    169  1.17       ad 	/* And exit.. */
    170  1.17       ad 	lwp_exit(l);
    171   1.1  thorpej 
    172   1.1  thorpej 	/*
    173   1.1  thorpej 	 * XXX Fool the compiler.  Making exit1() __noreturn__ is a can
    174   1.1  thorpej 	 * XXX of worms right now.
    175   1.1  thorpej 	 */
    176  1.17       ad 	for (;;)
    177  1.17       ad 		;
    178   1.2  thorpej }
    179   1.2  thorpej 
    180   1.2  thorpej /*
    181  1.17       ad  * Destroy an inactive kthread.  The kthread must be in the LSIDL state.
    182   1.2  thorpej  */
    183   1.2  thorpej void
    184  1.17       ad kthread_destroy(lwp_t *l)
    185   1.2  thorpej {
    186   1.8  thorpej 
    187  1.17       ad 	KASSERT((l->l_flag & LW_SYSTEM) != 0);
    188  1.17       ad 	KASSERT(l->l_stat == LSIDL);
    189   1.2  thorpej 
    190  1.17       ad 	lwp_exit(l);
    191   1.1  thorpej }
    192