Home | History | Annotate | Line # | Download | only in kern
kern_kthread.c revision 1.24.10.2.4.1
      1  1.24.10.2.4.1     matt /*	$NetBSD: kern_kthread.c,v 1.24.10.2.4.1 2011/12/27 16:35:13 matt Exp $	*/
      2            1.1  thorpej 
      3            1.1  thorpej /*-
      4      1.24.10.1      snj  * 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.24.10.2.4.1     matt __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.24.10.2.4.1 2011/12/27 16:35:13 matt 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.24.10.2.4.1     matt #include <sys/mutex.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 #include <machine/stdarg.h>
     46            1.1  thorpej 
     47  1.24.10.2.4.1     matt static lwp_t *		kthread_jtarget;
     48  1.24.10.2.4.1     matt static kmutex_t		kthread_lock;
     49  1.24.10.2.4.1     matt static kcondvar_t	kthread_cv;
     50  1.24.10.2.4.1     matt 
     51  1.24.10.2.4.1     matt void
     52  1.24.10.2.4.1     matt kthread_sysinit(void)
     53  1.24.10.2.4.1     matt {
     54  1.24.10.2.4.1     matt 
     55  1.24.10.2.4.1     matt 	mutex_init(&kthread_lock, MUTEX_DEFAULT, IPL_NONE);
     56  1.24.10.2.4.1     matt 	cv_init(&kthread_cv, "kthrwait");
     57  1.24.10.2.4.1     matt 	kthread_jtarget = NULL;
     58  1.24.10.2.4.1     matt }
     59  1.24.10.2.4.1     matt 
     60            1.1  thorpej /*
     61  1.24.10.2.4.1     matt  * kthread_create: create a kernel thread, that is, system-only LWP.
     62            1.1  thorpej  */
     63            1.1  thorpej int
     64           1.17       ad kthread_create(pri_t pri, int flag, struct cpu_info *ci,
     65  1.24.10.2.4.1     matt     void (*func)(void *), void *arg, lwp_t **lp, const char *fmt, ...)
     66            1.1  thorpej {
     67           1.17       ad 	lwp_t *l;
     68           1.17       ad 	vaddr_t uaddr;
     69           1.17       ad 	bool inmem;
     70  1.24.10.2.4.1     matt 	int error, lc;
     71            1.1  thorpej 	va_list ap;
     72  1.24.10.2.4.1     matt 
     73  1.24.10.2.4.1     matt 	KASSERT((flag & KTHREAD_INTR) == 0 || (flag & KTHREAD_MPSAFE) != 0);
     74            1.1  thorpej 
     75           1.17       ad 	inmem = uvm_uarea_alloc(&uaddr);
     76           1.17       ad 	if (uaddr == 0)
     77           1.17       ad 		return ENOMEM;
     78      1.24.10.2      snj 	if ((flag & KTHREAD_TS) != 0) {
     79      1.24.10.1      snj 		lc = SCHED_OTHER;
     80      1.24.10.1      snj 	} else {
     81      1.24.10.1      snj 		lc = SCHED_RR;
     82      1.24.10.1      snj 	}
     83           1.19       ad 	error = lwp_create(&lwp0, &proc0, uaddr, inmem, LWP_DETACHED, NULL,
     84      1.24.10.1      snj 	    0, func, arg, &l, lc);
     85           1.17       ad 	if (error) {
     86           1.19       ad 		uvm_uarea_free(uaddr, curcpu());
     87           1.17       ad 		return error;
     88           1.17       ad 	}
     89           1.17       ad 	uvm_lwp_hold(l);
     90           1.17       ad 	if (fmt != NULL) {
     91           1.17       ad 		l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
     92           1.17       ad 		if (l->l_name == NULL) {
     93  1.24.10.2.4.1     matt 			kthread_destroy(l);
     94           1.17       ad 			return ENOMEM;
     95           1.17       ad 		}
     96           1.17       ad 		va_start(ap, fmt);
     97           1.17       ad 		vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
     98           1.17       ad 		va_end(ap);
     99           1.17       ad 	}
    100           1.17       ad 
    101           1.17       ad 	/*
    102           1.17       ad 	 * Set parameters.
    103           1.17       ad 	 */
    104           1.17       ad 	if (pri == PRI_NONE) {
    105      1.24.10.2      snj 		if ((flag & KTHREAD_TS) != 0) {
    106      1.24.10.1      snj 			/* Maximum user priority level. */
    107      1.24.10.1      snj 			pri = MAXPRI_USER;
    108      1.24.10.1      snj 		} else {
    109      1.24.10.1      snj 			/* Minimum kernel priority level. */
    110      1.24.10.1      snj 			pri = PRI_KTHREAD;
    111      1.24.10.1      snj 		}
    112           1.17       ad 	}
    113           1.23       ad 	mutex_enter(proc0.p_lock);
    114           1.19       ad 	lwp_lock(l);
    115           1.17       ad 	l->l_priority = pri;
    116           1.17       ad 	if (ci != NULL) {
    117           1.17       ad 		if (ci != l->l_cpu) {
    118           1.17       ad 			lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
    119           1.17       ad 			lwp_lock(l);
    120           1.17       ad 		}
    121           1.22       ad 		l->l_pflag |= LP_BOUND;
    122           1.17       ad 		l->l_cpu = ci;
    123           1.17       ad 	}
    124  1.24.10.2.4.1     matt 
    125  1.24.10.2.4.1     matt 	if ((flag & KTHREAD_JOINABLE) != 0) {
    126  1.24.10.2.4.1     matt 		KASSERT(lp != NULL);
    127  1.24.10.2.4.1     matt 		l->l_pflag |= LP_JOINABLE;
    128  1.24.10.2.4.1     matt 	}
    129  1.24.10.2.4.1     matt 	if ((flag & KTHREAD_INTR) != 0) {
    130           1.19       ad 		l->l_pflag |= LP_INTR;
    131  1.24.10.2.4.1     matt 	}
    132  1.24.10.2.4.1     matt 	if ((flag & KTHREAD_MPSAFE) == 0) {
    133           1.20       ad 		l->l_pflag &= ~LP_MPSAFE;
    134  1.24.10.2.4.1     matt 	}
    135           1.17       ad 
    136           1.17       ad 	/*
    137           1.17       ad 	 * Set the new LWP running, unless the caller has requested
    138           1.17       ad 	 * otherwise.
    139           1.17       ad 	 */
    140           1.17       ad 	if ((flag & KTHREAD_IDLE) == 0) {
    141           1.17       ad 		l->l_stat = LSRUN;
    142           1.17       ad 		sched_enqueue(l, false);
    143           1.19       ad 		lwp_unlock(l);
    144           1.19       ad 	} else
    145           1.21       ad 		lwp_unlock_to(l, ci->ci_schedstate.spc_lwplock);
    146           1.17       ad 
    147           1.17       ad 	/*
    148           1.17       ad 	 * The LWP is not created suspended or stopped and cannot be set
    149           1.17       ad 	 * into those states later, so must be considered runnable.
    150           1.17       ad 	 */
    151           1.17       ad 	proc0.p_nrlwps++;
    152           1.23       ad 	mutex_exit(proc0.p_lock);
    153            1.1  thorpej 
    154            1.1  thorpej 	/* All done! */
    155  1.24.10.2.4.1     matt 	if (lp != NULL) {
    156           1.17       ad 		*lp = l;
    157  1.24.10.2.4.1     matt 	}
    158  1.24.10.2.4.1     matt 	return 0;
    159            1.1  thorpej }
    160            1.1  thorpej 
    161            1.1  thorpej /*
    162            1.1  thorpej  * Cause a kernel thread to exit.  Assumes the exiting thread is the
    163            1.1  thorpej  * current context.
    164            1.1  thorpej  */
    165            1.1  thorpej void
    166           1.11  thorpej kthread_exit(int ecode)
    167            1.1  thorpej {
    168           1.18       ad 	const char *name;
    169           1.17       ad 	lwp_t *l = curlwp;
    170            1.1  thorpej 
    171           1.17       ad 	/* We can't do much with the exit code, so just report it. */
    172           1.18       ad 	if (ecode != 0) {
    173           1.18       ad 		if ((name = l->l_name) == NULL)
    174           1.18       ad 			name = "unnamed";
    175           1.17       ad 		printf("WARNING: kthread `%s' (%d) exits with status %d\n",
    176           1.18       ad 		    name, l->l_lid, ecode);
    177           1.18       ad 	}
    178            1.1  thorpej 
    179  1.24.10.2.4.1     matt 	/* Barrier for joining. */
    180  1.24.10.2.4.1     matt 	if (l->l_pflag & LP_JOINABLE) {
    181  1.24.10.2.4.1     matt 		mutex_enter(&kthread_lock);
    182  1.24.10.2.4.1     matt 		while (kthread_jtarget != l) {
    183  1.24.10.2.4.1     matt 			cv_wait(&kthread_cv, &kthread_lock);
    184  1.24.10.2.4.1     matt 		}
    185  1.24.10.2.4.1     matt 		kthread_jtarget = NULL;
    186  1.24.10.2.4.1     matt 		cv_broadcast(&kthread_cv);
    187  1.24.10.2.4.1     matt 		mutex_exit(&kthread_lock);
    188  1.24.10.2.4.1     matt 	}
    189  1.24.10.2.4.1     matt 
    190           1.17       ad 	/* And exit.. */
    191           1.17       ad 	lwp_exit(l);
    192  1.24.10.2.4.1     matt 	KASSERT(false);
    193            1.2  thorpej }
    194            1.2  thorpej 
    195            1.2  thorpej /*
    196           1.17       ad  * Destroy an inactive kthread.  The kthread must be in the LSIDL state.
    197            1.2  thorpej  */
    198            1.2  thorpej void
    199           1.17       ad kthread_destroy(lwp_t *l)
    200            1.2  thorpej {
    201            1.8  thorpej 
    202           1.17       ad 	KASSERT((l->l_flag & LW_SYSTEM) != 0);
    203           1.17       ad 	KASSERT(l->l_stat == LSIDL);
    204            1.2  thorpej 
    205           1.17       ad 	lwp_exit(l);
    206            1.1  thorpej }
    207  1.24.10.2.4.1     matt 
    208  1.24.10.2.4.1     matt /*
    209  1.24.10.2.4.1     matt  * Wait for a kthread to exit, as pthread_join().
    210  1.24.10.2.4.1     matt  */
    211  1.24.10.2.4.1     matt int
    212  1.24.10.2.4.1     matt kthread_join(lwp_t *l)
    213  1.24.10.2.4.1     matt {
    214  1.24.10.2.4.1     matt 
    215  1.24.10.2.4.1     matt 	KASSERT((l->l_flag & LW_SYSTEM) != 0);
    216  1.24.10.2.4.1     matt 
    217  1.24.10.2.4.1     matt 	/*
    218  1.24.10.2.4.1     matt 	 * - Wait if some other thread has occupied the target.
    219  1.24.10.2.4.1     matt 	 * - Speicfy our kthread as a target and notify it.
    220  1.24.10.2.4.1     matt 	 * - Wait for the target kthread to notify us.
    221  1.24.10.2.4.1     matt 	 */
    222  1.24.10.2.4.1     matt 	mutex_enter(&kthread_lock);
    223  1.24.10.2.4.1     matt 	while (kthread_jtarget) {
    224  1.24.10.2.4.1     matt 		cv_wait(&kthread_cv, &kthread_lock);
    225  1.24.10.2.4.1     matt 	}
    226  1.24.10.2.4.1     matt 	kthread_jtarget = l;
    227  1.24.10.2.4.1     matt 	cv_broadcast(&kthread_cv);
    228  1.24.10.2.4.1     matt 	while (kthread_jtarget == l) {
    229  1.24.10.2.4.1     matt 		cv_wait(&kthread_cv, &kthread_lock);
    230  1.24.10.2.4.1     matt 	}
    231  1.24.10.2.4.1     matt 	mutex_exit(&kthread_lock);
    232  1.24.10.2.4.1     matt 
    233  1.24.10.2.4.1     matt 	return 0;
    234  1.24.10.2.4.1     matt }
    235