2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/cpuset.h>
35 #include <sys/kthread.h>
37 #include <sys/mutex.h>
39 #include <sys/resourcevar.h>
40 #include <sys/rwlock.h>
41 #include <sys/signalvar.h>
42 #include <sys/sysent.h>
45 #include <sys/unistd.h>
47 #include <sys/sched.h>
48 #include <sys/tslog.h>
50 #include <vm/vm_extern.h>
52 #include <machine/stdarg.h>
55 * Start a kernel process. This is called after a fork() call in
56 * mi_startup() in the file kern/init_main.c.
58 * This function is used to start "internal" daemons and intended
59 * to be called from SYSINIT().
62 kproc_start(const void *udata)
64 const struct kproc_desc *kp = udata;
67 error = kproc_create((void (*)(void *))kp->func, NULL,
68 kp->global_procpp, 0, 0, "%s", kp->arg0);
70 panic("kproc_start: %s: error %d", kp->arg0, error);
74 * Create a kernel process/thread/whatever. It shares its address space
75 * with proc0 - ie: kernel only.
77 * func is the function to start.
78 * arg is the parameter to pass to function on first startup.
79 * newpp is the return value pointing to the thread's struct proc.
80 * flags are flags to fork1 (in unistd.h)
81 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
84 kproc_create(void (*func)(void *), void *arg,
85 struct proc **newpp, int flags, int pages, const char *fmt, ...)
94 panic("kproc_create called too soon");
96 bzero(&fr, sizeof(fr));
97 fr.fr_flags = RFMEM | RFFDG | RFPROC | RFSTOPPED | flags;
100 error = fork1(&thread0, &fr);
104 /* save a global descriptor, if desired */
108 /* this is a non-swapped system process */
110 td = FIRST_THREAD_IN_PROC(p2);
111 p2->p_flag |= P_SYSTEM | P_KPROC;
112 td->td_pflags |= TDP_KTHREAD;
113 mtx_lock(&p2->p_sigacts->ps_mtx);
114 p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
115 mtx_unlock(&p2->p_sigacts->ps_mtx);
118 /* set up arg0 for 'ps', et al */
120 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
122 /* set up arg0 for 'ps', et al */
124 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
127 sched_clear_tdname(td);
129 TSTHREAD(td, td->td_name);
131 if (PMC_SYSTEM_SAMPLING_ACTIVE()) {
132 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_PROC_CREATE_LOG, p2);
133 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
137 /* call the processes' main()... */
138 cpu_fork_kthread_handler(td, func, arg);
140 /* Avoid inheriting affinity from a random parent. */
141 cpuset_kernthread(td);
145 sched_user_prio(td, PUSER);
147 /* Delay putting it on the run queue until now. */
148 if (!(flags & RFSTOPPED))
149 sched_add(td, SRQ_BORING);
157 kproc_exit(int ecode)
166 * Reparent curthread from proc0 to init so that the zombie
169 sx_xlock(&proctree_lock);
171 proc_reparent(p, initproc, true);
173 sx_xunlock(&proctree_lock);
176 * Wakeup anyone waiting for us to exit.
185 * Advise a kernel process to suspend (or resume) in its main loop.
186 * Participation is voluntary.
189 kproc_suspend(struct proc *p, int timo)
192 * Make sure this is indeed a system process and we can safely
193 * use the p_siglist field.
196 if ((p->p_flag & P_KPROC) == 0) {
200 SIGADDSET(p->p_siglist, SIGSTOP);
202 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo);
206 kproc_resume(struct proc *p)
209 * Make sure this is indeed a system process and we can safely
210 * use the p_siglist field.
213 if ((p->p_flag & P_KPROC) == 0) {
217 SIGDELSET(p->p_siglist, SIGSTOP);
219 wakeup(&p->p_siglist);
224 kproc_suspend_check(struct proc *p)
227 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
228 wakeup(&p->p_siglist);
229 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
235 * Start a kernel thread.
237 * This function is used to start "internal" daemons and intended
238 * to be called from SYSINIT().
242 kthread_start(const void *udata)
244 const struct kthread_desc *kp = udata;
247 error = kthread_add((void (*)(void *))kp->func, NULL,
248 NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
250 panic("kthread_start: %s: error %d", kp->arg0, error);
254 * Create a kernel thread. It shares its address space
255 * with proc0 - ie: kernel only.
257 * func is the function to start.
258 * arg is the parameter to pass to function on first startup.
259 * newtdp is the return value pointing to the thread's struct thread.
260 * ** XXX fix this --> flags are flags to fork1 (in unistd.h)
261 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
264 kthread_add(void (*func)(void *), void *arg, struct proc *p,
265 struct thread **newtdp, int flags, int pages, const char *fmt, ...)
268 struct thread *newtd, *oldtd;
271 panic("kthread_add called too soon");
273 /* If no process supplied, put it on proc0 */
277 /* Initialize our new td */
278 newtd = thread_alloc(pages);
283 oldtd = FIRST_THREAD_IN_PROC(p);
285 bzero(&newtd->td_startzero,
286 __rangeof(struct thread, td_startzero, td_endzero));
287 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
288 __rangeof(struct thread, td_startcopy, td_endcopy));
290 /* set up arg0 for 'ps', et al */
292 vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap);
295 TSTHREAD(newtd, newtd->td_name);
297 newtd->td_proc = p; /* needed for cpu_copy_thread */
298 /* might be further optimized for kthread */
299 cpu_copy_thread(newtd, oldtd);
300 /* put the designated function(arg) as the resume context */
301 cpu_fork_kthread_handler(newtd, func, arg);
303 newtd->td_pflags |= TDP_KTHREAD;
304 thread_cow_get_proc(newtd, p);
306 /* this code almost the same as create_thread() in kern_thr.c */
307 p->p_flag |= P_HADTHREADS;
308 thread_link(newtd, p);
310 /* let the scheduler know about these things. */
311 sched_fork_thread(oldtd, newtd);
312 TD_SET_CAN_RUN(newtd);
313 thread_unlock(oldtd);
318 /* Avoid inheriting affinity from a random parent. */
319 cpuset_kernthread(newtd);
321 if (PMC_SYSTEM_SAMPLING_ACTIVE())
322 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
324 /* Delay putting it on the run queue until now. */
325 if (!(flags & RFSTOPPED)) {
327 sched_add(newtd, SRQ_BORING);
344 if (PMC_SYSTEM_SAMPLING_ACTIVE())
345 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
347 /* A module may be waiting for us to exit. */
351 * The last exiting thread in a kernel process must tear down
355 if (p->p_numthreads == 1) {
360 if (p->p_sysent->sv_ontdexit != NULL)
361 p->p_sysent->sv_ontdexit(td);
364 umtx_thread_exit(td);
371 * Advise a kernel process to suspend (or resume) in its main loop.
372 * Participation is voluntary.
375 kthread_suspend(struct thread *td, int timo)
382 * td_pflags should not be read by any thread other than
383 * curthread, but as long as this flag is invariant during the
384 * thread's lifetime, it is OK to check its state.
386 if ((td->td_pflags & TDP_KTHREAD) == 0)
390 * The caller of the primitive should have already checked that the
391 * thread is up and running, thus not being blocked by other
396 td->td_flags |= TDF_KTH_SUSP;
398 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
403 * Resume a thread previously put asleep with kthread_suspend().
406 kthread_resume(struct thread *td)
413 * td_pflags should not be read by any thread other than
414 * curthread, but as long as this flag is invariant during the
415 * thread's lifetime, it is OK to check its state.
417 if ((td->td_pflags & TDP_KTHREAD) == 0)
422 td->td_flags &= ~TDF_KTH_SUSP;
424 wakeup(&td->td_flags);
430 * Used by the thread to poll as to whether it should yield/sleep
431 * and notify the caller that is has happened.
434 kthread_suspend_check(void)
442 if ((td->td_pflags & TDP_KTHREAD) == 0)
443 panic("%s: curthread is not a valid kthread", __func__);
446 * Setting the TDF_KTH_SUSP flag is protected by process lock.
448 * Do an unlocked read first to avoid serializing with all other threads
449 * in the common case of not suspending.
451 if ((td->td_flags & TDF_KTH_SUSP) == 0)
454 while ((td->td_flags & TDF_KTH_SUSP) != 0) {
455 wakeup(&td->td_flags);
456 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
462 kproc_kthread_add(void (*func)(void *), void *arg,
463 struct proc **procptr, struct thread **tdptr,
464 int flags, int pages, const char *procname, const char *fmt, ...)
471 if (*procptr == NULL) {
472 error = kproc_create(func, arg,
473 procptr, flags, pages, "%s", procname);
476 td = FIRST_THREAD_IN_PROC(*procptr);
480 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
483 sched_clear_tdname(td);
488 vsnprintf(buf, sizeof(buf), fmt, ap);
490 error = kthread_add(func, arg, *procptr,
491 tdptr, flags, pages, "%s", buf);