xref: /netbsd-src/sys/kern/kern_kthread.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: kern_kthread.c,v 1.21 2008/02/14 14:26:57 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center, and by Andrew Doran.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: kern_kthread.c,v 1.21 2008/02/14 14:26:57 ad Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
47 #include <sys/proc.h>
48 #include <sys/sched.h>
49 #include <sys/kmem.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 /*
54  * note that stdarg.h and the ansi style va_start macro is used for both
55  * ansi and traditional c complers.
56  * XXX: this requires that stdarg.h define: va_alist and va_dcl
57  */
58 #include <machine/stdarg.h>
59 
60 /*
61  * Fork a kernel thread.  Any process can request this to be done.
62  */
63 int
64 kthread_create(pri_t pri, int flag, struct cpu_info *ci,
65 	       void (*func)(void *), void *arg,
66 	       lwp_t **lp, const char *fmt, ...)
67 {
68 	lwp_t *l;
69 	vaddr_t uaddr;
70 	bool inmem;
71 	int error;
72 	va_list ap;
73 
74 	inmem = uvm_uarea_alloc(&uaddr);
75 	if (uaddr == 0)
76 		return ENOMEM;
77 	error = lwp_create(&lwp0, &proc0, uaddr, inmem, LWP_DETACHED, NULL,
78 	    0, func, arg, &l, SCHED_FIFO);
79 	if (error) {
80 		uvm_uarea_free(uaddr, curcpu());
81 		return error;
82 	}
83 	uvm_lwp_hold(l);
84 	if (fmt != NULL) {
85 		l->l_name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
86 		if (l->l_name == NULL) {
87 			lwp_exit(l);
88 			return ENOMEM;
89 		}
90 		va_start(ap, fmt);
91 		vsnprintf(l->l_name, MAXCOMLEN, fmt, ap);
92 		va_end(ap);
93 	}
94 
95 	/*
96 	 * Set parameters.
97 	 */
98 	if ((flag & KTHREAD_INTR) != 0) {
99 		KASSERT((flag & KTHREAD_MPSAFE) != 0);
100 	}
101 
102 	if (pri == PRI_NONE) {
103 		/* Minimum kernel priority level. */
104 		pri = PRI_KTHREAD;
105 	}
106 	mutex_enter(&proc0.p_smutex);
107 	lwp_lock(l);
108 	l->l_priority = pri;
109 	if (ci != NULL) {
110 		if (ci != l->l_cpu) {
111 			lwp_unlock_to(l, ci->ci_schedstate.spc_mutex);
112 			lwp_lock(l);
113 		}
114 		l->l_flag |= LW_BOUND;
115 		l->l_cpu = ci;
116 	}
117 	if ((flag & KTHREAD_INTR) != 0)
118 		l->l_pflag |= LP_INTR;
119 	if ((flag & KTHREAD_MPSAFE) == 0)
120 		l->l_pflag &= ~LP_MPSAFE;
121 
122 	/*
123 	 * Set the new LWP running, unless the caller has requested
124 	 * otherwise.
125 	 */
126 	if ((flag & KTHREAD_IDLE) == 0) {
127 		l->l_stat = LSRUN;
128 		sched_enqueue(l, false);
129 		lwp_unlock(l);
130 	} else
131 		lwp_unlock_to(l, ci->ci_schedstate.spc_lwplock);
132 
133 	/*
134 	 * The LWP is not created suspended or stopped and cannot be set
135 	 * into those states later, so must be considered runnable.
136 	 */
137 	proc0.p_nrlwps++;
138 	mutex_exit(&proc0.p_smutex);
139 
140 	/* All done! */
141 	if (lp != NULL)
142 		*lp = l;
143 
144 	return (0);
145 }
146 
147 /*
148  * Cause a kernel thread to exit.  Assumes the exiting thread is the
149  * current context.
150  */
151 void
152 kthread_exit(int ecode)
153 {
154 	const char *name;
155 	lwp_t *l = curlwp;
156 
157 	/* We can't do much with the exit code, so just report it. */
158 	if (ecode != 0) {
159 		if ((name = l->l_name) == NULL)
160 			name = "unnamed";
161 		printf("WARNING: kthread `%s' (%d) exits with status %d\n",
162 		    name, l->l_lid, ecode);
163 	}
164 
165 	/* And exit.. */
166 	lwp_exit(l);
167 
168 	/*
169 	 * XXX Fool the compiler.  Making exit1() __noreturn__ is a can
170 	 * XXX of worms right now.
171 	 */
172 	for (;;)
173 		;
174 }
175 
176 /*
177  * Destroy an inactive kthread.  The kthread must be in the LSIDL state.
178  */
179 void
180 kthread_destroy(lwp_t *l)
181 {
182 
183 	KASSERT((l->l_flag & LW_SYSTEM) != 0);
184 	KASSERT(l->l_stat == LSIDL);
185 
186 	lwp_exit(l);
187 }
188