1 /* $OpenBSD: kern_kthread.c,v 1.31 2011/11/09 20:57:38 guenther Exp $ */ 2 /* $NetBSD: kern_kthread.c,v 1.3 1998/12/22 21:21:36 kleink Exp $ */ 3 4 /*- 5 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/kthread.h> 38 #include <sys/proc.h> 39 #include <sys/wait.h> 40 #include <sys/malloc.h> 41 #include <sys/queue.h> 42 43 #include <machine/cpu.h> 44 45 /* 46 * note that stdarg.h and the ansi style va_start macro is used for both 47 * ansi and traditional c compilers. 48 * XXX: this requires that stdarg.h define: va_alist and va_dcl 49 */ 50 #include <sys/stdarg.h> 51 52 int kthread_create_now; 53 54 /* 55 * Fork a kernel thread. Any process can request this to be done. 56 * The VM space and limits, etc. will be shared with proc0. 57 */ 58 int 59 kthread_create(void (*func)(void *), void *arg, 60 struct proc **newpp, const char *fmt, ...) 61 { 62 struct proc *p; 63 int error; 64 va_list ap; 65 66 /* 67 * First, create the new process. Share the memory, file 68 * descriptors and don't leave the exit status around for the 69 * parent to wait for. 70 */ 71 error = fork1(&proc0, 0, FORK_SHAREVM|FORK_SHAREFILES|FORK_NOZOMBIE| 72 FORK_SIGHAND, NULL, 0, func, arg, NULL, &p); 73 if (error) 74 return (error); 75 76 /* 77 * Mark it as a system process. 78 */ 79 atomic_setbits_int(&p->p_flag, P_SYSTEM); 80 81 /* Name it as specified. */ 82 va_start(ap, fmt); 83 vsnprintf(p->p_comm, sizeof p->p_comm, fmt, ap); 84 va_end(ap); 85 86 /* All done! */ 87 if (newpp != NULL) 88 *newpp = p; 89 return (0); 90 } 91 92 /* 93 * Cause a kernel thread to exit. Assumes the exiting thread is the 94 * current context. 95 */ 96 void 97 kthread_exit(int ecode) 98 { 99 100 /* 101 * XXX What do we do with the exit code? Should we even bother 102 * XXX with it? The parent (proc0) isn't going to do much with 103 * XXX it. 104 */ 105 if (ecode != 0) 106 printf("WARNING: thread `%s' (%d) exits with status %d\n", 107 curproc->p_comm, curproc->p_pid, ecode); 108 109 exit1(curproc, W_EXITCODE(ecode, 0), EXIT_NORMAL); 110 111 /* 112 * XXX Fool the compiler. Making exit1() __dead is a can 113 * XXX of worms right now. 114 */ 115 for (;;); 116 } 117 118 struct kthread_q { 119 SIMPLEQ_ENTRY(kthread_q) kq_q; 120 void (*kq_func)(void *); 121 void *kq_arg; 122 }; 123 124 SIMPLEQ_HEAD(, kthread_q) kthread_q = SIMPLEQ_HEAD_INITIALIZER(kthread_q); 125 126 /* 127 * Defer the creation of a kernel thread. Once the standard kernel threads 128 * and processes have been created, this queue will be run to callback to 129 * the caller to create threads for e.g. file systems and device drivers. 130 */ 131 void 132 kthread_create_deferred(void (*func)(void *), void *arg) 133 { 134 struct kthread_q *kq; 135 136 if (kthread_create_now) { 137 (*func)(arg); 138 return; 139 } 140 141 kq = malloc(sizeof *kq, M_TEMP, M_NOWAIT|M_ZERO); 142 if (kq == NULL) 143 panic("unable to allocate kthread_q"); 144 145 kq->kq_func = func; 146 kq->kq_arg = arg; 147 148 SIMPLEQ_INSERT_TAIL(&kthread_q, kq, kq_q); 149 } 150 151 void 152 kthread_run_deferred_queue(void) 153 { 154 struct kthread_q *kq; 155 156 /* No longer need to defer kthread creation. */ 157 kthread_create_now = 1; 158 159 while ((kq = SIMPLEQ_FIRST(&kthread_q)) != NULL) { 160 SIMPLEQ_REMOVE_HEAD(&kthread_q, kq_q); 161 (*kq->kq_func)(kq->kq_arg); 162 free(kq, M_TEMP); 163 } 164 } 165