| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org> | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 24 | * SUCH DAMAGE. | |
| 25 | * | |
| 26 | * $FreeBSD: src/sys/kern/kern_kthread.c,v 1.5.2.3 2001/12/25 01:51:14 dillon Exp $ | |
| 379210cb | 27 | * $DragonFly: src/sys/kern/kern_kthread.c,v 1.13 2006/12/18 20:41:01 dillon Exp $ |
| 984263bc MD |
28 | */ |
| 29 | ||
| 30 | #include <sys/param.h> | |
| 31 | #include <sys/systm.h> | |
| 32 | #include <sys/proc.h> | |
| 33 | #include <sys/kthread.h> | |
| 34 | #include <sys/ptrace.h> | |
| 35 | #include <sys/resourcevar.h> | |
| 36 | #include <sys/signalvar.h> | |
| 37 | #include <sys/unistd.h> | |
| 38 | #include <sys/wait.h> | |
| 39 | ||
| 40 | #include <machine/stdarg.h> | |
| 41 | ||
| a3c18566 | 42 | static struct lwkt_token kpsus_token = LWKT_TOKEN_INITIALIZER(kpsus_token); |
| cd8ab232 MD |
43 | |
| 44 | ||
| 984263bc | 45 | /* |
| 496907b0 MD |
46 | * Create a kernel process/thread/whatever. It shares it's address space |
| 47 | * with proc0 - ie: kernel only. 5.x compatible. | |
| 48 | * | |
| cd8ab232 | 49 | * All kthreads are created as MPSAFE threads. |
| 496907b0 MD |
50 | */ |
| 51 | int | |
| 52 | kthread_create(void (*func)(void *), void *arg, | |
| be13c473 | 53 | struct thread **tdp, const char *fmt, ...) |
| 496907b0 MD |
54 | { |
| 55 | thread_t td; | |
| 56 | __va_list ap; | |
| 57 | ||
| fdce8919 | 58 | td = lwkt_alloc_thread(NULL, LWKT_THREAD_STACK, -1, TDF_VERBOSE); |
| 496907b0 MD |
59 | if (tdp) |
| 60 | *tdp = td; | |
| 61 | cpu_set_thread_handler(td, kthread_exit, func, arg); | |
| 496907b0 MD |
62 | |
| 63 | /* | |
| 64 | * Set up arg0 for 'ps' etc | |
| 65 | */ | |
| 66 | __va_start(ap, fmt); | |
| 379210cb | 67 | kvsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap); |
| 496907b0 MD |
68 | __va_end(ap); |
| 69 | ||
| 60055ede SG |
70 | td->td_ucred = crhold(proc0.p_ucred); |
| 71 | ||
| 496907b0 MD |
72 | /* |
| 73 | * Schedule the thread to run | |
| 74 | */ | |
| 75 | lwkt_schedule(td); | |
| 76 | return 0; | |
| 77 | } | |
| 78 | ||
| be13c473 MD |
79 | int |
| 80 | kthread_create_cpu(void (*func)(void *), void *arg, | |
| 81 | struct thread **tdp, int cpu, const char *fmt, ...) | |
| 82 | { | |
| 83 | thread_t td; | |
| 84 | __va_list ap; | |
| 85 | ||
| fdce8919 | 86 | td = lwkt_alloc_thread(NULL, LWKT_THREAD_STACK, cpu, TDF_VERBOSE); |
| be13c473 MD |
87 | if (tdp) |
| 88 | *tdp = td; | |
| 89 | cpu_set_thread_handler(td, kthread_exit, func, arg); | |
| be13c473 MD |
90 | |
| 91 | /* | |
| 92 | * Set up arg0 for 'ps' etc | |
| 93 | */ | |
| 94 | __va_start(ap, fmt); | |
| 95 | kvsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap); | |
| 96 | __va_end(ap); | |
| 97 | ||
| 98 | td->td_ucred = crhold(proc0.p_ucred); | |
| 99 | ||
| 100 | /* | |
| 101 | * Schedule the thread to run | |
| 102 | */ | |
| 103 | lwkt_schedule(td); | |
| 104 | return 0; | |
| 105 | } | |
| 106 | ||
| d2d8515b | 107 | #if 0 |
| 496907b0 MD |
108 | /* |
| 109 | * Same as kthread_create() but you can specify a custom stack size. | |
| 110 | */ | |
| 111 | int | |
| 112 | kthread_create_stk(void (*func)(void *), void *arg, | |
| be13c473 | 113 | struct thread **tdp, int stksize, const char *fmt, ...) |
| 496907b0 MD |
114 | { |
| 115 | thread_t td; | |
| 116 | __va_list ap; | |
| 117 | ||
| fdce8919 | 118 | td = lwkt_alloc_thread(NULL, stksize, -1, TDF_VERBOSE); |
| 496907b0 MD |
119 | if (tdp) |
| 120 | *tdp = td; | |
| 121 | cpu_set_thread_handler(td, kthread_exit, func, arg); | |
| cd8ab232 | 122 | |
| 496907b0 | 123 | __va_start(ap, fmt); |
| 379210cb | 124 | kvsnprintf(td->td_comm, sizeof(td->td_comm), fmt, ap); |
| 496907b0 MD |
125 | __va_end(ap); |
| 126 | ||
| 127 | lwkt_schedule(td); | |
| 128 | return 0; | |
| 129 | } | |
| d2d8515b | 130 | #endif |
| 496907b0 MD |
131 | |
| 132 | /* | |
| 133 | * Destroy an LWKT thread. Warning! This function is not called when | |
| 134 | * a process exits, cpu_proc_exit() directly calls cpu_thread_exit() and | |
| 135 | * uses a different reaping mechanism. | |
| 136 | * | |
| 137 | * XXX duplicates lwkt_exit() | |
| 138 | */ | |
| 139 | void | |
| 140 | kthread_exit(void) | |
| 141 | { | |
| 142 | lwkt_exit(); | |
| 143 | } | |
| 144 | ||
| 496907b0 | 145 | /* |
| 984263bc MD |
146 | * Start a kernel process. This is called after a fork() call in |
| 147 | * mi_startup() in the file kern/init_main.c. | |
| 148 | * | |
| 149 | * This function is used to start "internal" daemons and intended | |
| 150 | * to be called from SYSINIT(). | |
| cd8ab232 MD |
151 | * |
| 152 | * These threads are created MPSAFE. | |
| 984263bc MD |
153 | */ |
| 154 | void | |
| 77153250 | 155 | kproc_start(const void *udata) |
| 984263bc MD |
156 | { |
| 157 | const struct kproc_desc *kp = udata; | |
| 158 | int error; | |
| 159 | ||
| 160 | error = kthread_create((void (*)(void *))kp->func, NULL, | |
| be13c473 | 161 | kp->global_threadpp, kp->arg0); |
| 26a0694b | 162 | lwkt_setpri(*kp->global_threadpp, TDPRI_KERN_DAEMON); |
| 984263bc MD |
163 | if (error) |
| 164 | panic("kproc_start: %s: error %d", kp->arg0, error); | |
| 165 | } | |
| 166 | ||
| 167 | /* | |
| 984263bc MD |
168 | * Advise a kernel process to suspend (or resume) in its main loop. |
| 169 | * Participation is voluntary. | |
| 170 | */ | |
| 171 | int | |
| bc6dffab | 172 | suspend_kproc(struct thread *td, int timo) |
| 984263bc | 173 | { |
| 0cfcada1 | 174 | if (td->td_proc == NULL) { |
| cd8ab232 | 175 | lwkt_gettoken(&kpsus_token); |
| 4643740a MD |
176 | /* request thread pause */ |
| 177 | atomic_set_int(&td->td_mpflags, TDF_MP_STOPREQ); | |
| 0cfcada1 | 178 | wakeup(td); |
| 4643740a | 179 | while (td->td_mpflags & TDF_MP_STOPREQ) { |
| 377d4740 | 180 | int error = tsleep(td, 0, "suspkp", timo); |
| 0cfcada1 MD |
181 | if (error == EWOULDBLOCK) |
| 182 | break; | |
| 183 | } | |
| 4643740a | 184 | atomic_clear_int(&td->td_mpflags, TDF_MP_STOPREQ); |
| cd8ab232 | 185 | lwkt_reltoken(&kpsus_token); |
| 0cfcada1 MD |
186 | return(0); |
| 187 | } else { | |
| 188 | return(EINVAL); /* not a kernel thread */ | |
| 189 | } | |
| 984263bc MD |
190 | } |
| 191 | ||
| 192 | void | |
| 0cfcada1 | 193 | kproc_suspend_loop(void) |
| 984263bc | 194 | { |
| 0cfcada1 MD |
195 | struct thread *td = curthread; |
| 196 | ||
| 4643740a | 197 | if (td->td_mpflags & TDF_MP_STOPREQ) { |
| cd8ab232 | 198 | lwkt_gettoken(&kpsus_token); |
| 4643740a MD |
199 | atomic_clear_int(&td->td_mpflags, TDF_MP_STOPREQ); |
| 200 | while ((td->td_mpflags & TDF_MP_WAKEREQ) == 0) { | |
| 0cfcada1 | 201 | wakeup(td); |
| 377d4740 | 202 | tsleep(td, 0, "kpsusp", 0); |
| 0cfcada1 | 203 | } |
| 4643740a | 204 | atomic_clear_int(&td->td_mpflags, TDF_MP_WAKEREQ); |
| 0cfcada1 | 205 | wakeup(td); |
| cd8ab232 | 206 | lwkt_reltoken(&kpsus_token); |
| 984263bc MD |
207 | } |
| 208 | } | |
| 0cfcada1 | 209 |