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;
98 fr.fr_flags2 = FR2_KPROC;
101 error = fork1(&thread0, &fr);
105 /* save a global descriptor, if desired */
109 /* set up arg0 for 'ps', et al */
111 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
113 td = FIRST_THREAD_IN_PROC(p2);
115 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
118 sched_clear_tdname(td);
120 TSTHREAD(td, td->td_name);
122 if (PMC_SYSTEM_SAMPLING_ACTIVE()) {
123 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_PROC_CREATE_LOG, p2);
124 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
128 /* call the processes' main()... */
129 cpu_fork_kthread_handler(td, func, arg);
131 /* Avoid inheriting affinity from a random parent. */
132 cpuset_kernthread(td);
136 sched_user_prio(td, PUSER);
138 /* Delay putting it on the run queue until now. */
139 if (!(flags & RFSTOPPED))
140 sched_add(td, SRQ_BORING);
148 kproc_exit(int ecode)
157 * Reparent curthread from proc0 to init so that the zombie
160 sx_xlock(&proctree_lock);
162 proc_reparent(p, initproc, true);
164 sx_xunlock(&proctree_lock);
167 * Wakeup anyone waiting for us to exit.
176 * Advise a kernel process to suspend (or resume) in its main loop.
177 * Participation is voluntary.
180 kproc_suspend(struct proc *p, int timo)
183 * Make sure this is indeed a system process and we can safely
184 * use the p_siglist field.
187 if ((p->p_flag & P_KPROC) == 0) {
191 SIGADDSET(p->p_siglist, SIGSTOP);
193 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo);
197 kproc_resume(struct proc *p)
200 * Make sure this is indeed a system process and we can safely
201 * use the p_siglist field.
204 if ((p->p_flag & P_KPROC) == 0) {
208 SIGDELSET(p->p_siglist, SIGSTOP);
210 wakeup(&p->p_siglist);
215 kproc_suspend_check(struct proc *p)
218 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
219 wakeup(&p->p_siglist);
220 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
226 * Start a kernel thread.
228 * This function is used to start "internal" daemons and intended
229 * to be called from SYSINIT().
233 kthread_start(const void *udata)
235 const struct kthread_desc *kp = udata;
238 error = kthread_add((void (*)(void *))kp->func, NULL,
239 NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
241 panic("kthread_start: %s: error %d", kp->arg0, error);
245 * Create a kernel thread. It shares its address space
246 * with proc0 - ie: kernel only.
248 * func is the function to start.
249 * arg is the parameter to pass to function on first startup.
250 * newtdp is the return value pointing to the thread's struct thread.
251 * ** XXX fix this --> flags are flags to fork1 (in unistd.h)
252 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
255 kthread_add(void (*func)(void *), void *arg, struct proc *p,
256 struct thread **newtdp, int flags, int pages, const char *fmt, ...)
259 struct thread *newtd, *oldtd;
262 panic("kthread_add called too soon");
264 /* If no process supplied, put it on proc0 */
268 /* Initialize our new td */
269 newtd = thread_alloc(pages);
274 oldtd = FIRST_THREAD_IN_PROC(p);
276 bzero(&newtd->td_startzero,
277 __rangeof(struct thread, td_startzero, td_endzero));
278 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
279 __rangeof(struct thread, td_startcopy, td_endcopy));
281 /* set up arg0 for 'ps', et al */
283 vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap);
286 TSTHREAD(newtd, newtd->td_name);
288 newtd->td_proc = p; /* needed for cpu_copy_thread */
289 newtd->td_pflags |= TDP_KTHREAD;
291 /* might be further optimized for kthread */
292 cpu_copy_thread(newtd, oldtd);
294 /* put the designated function(arg) as the resume context */
295 cpu_fork_kthread_handler(newtd, func, arg);
297 thread_cow_get_proc(newtd, p);
299 /* this code almost the same as create_thread() in kern_thr.c */
300 p->p_flag |= P_HADTHREADS;
301 thread_link(newtd, p);
303 /* let the scheduler know about these things. */
304 sched_fork_thread(oldtd, newtd);
305 TD_SET_CAN_RUN(newtd);
306 thread_unlock(oldtd);
311 /* Avoid inheriting affinity from a random parent. */
312 cpuset_kernthread(newtd);
314 if (PMC_SYSTEM_SAMPLING_ACTIVE())
315 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
317 /* Delay putting it on the run queue until now. */
318 if (!(flags & RFSTOPPED)) {
320 sched_add(newtd, SRQ_BORING);
337 if (PMC_SYSTEM_SAMPLING_ACTIVE())
338 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
340 /* A module may be waiting for us to exit. */
344 * The last exiting thread in a kernel process must tear down
348 if (p->p_numthreads == 1) {
353 if (p->p_sysent->sv_ontdexit != NULL)
354 p->p_sysent->sv_ontdexit(td);
357 umtx_thread_exit(td);
364 * Advise a kernel process to suspend (or resume) in its main loop.
365 * Participation is voluntary.
368 kthread_suspend(struct thread *td, int timo)
375 * td_pflags should not be read by any thread other than
376 * curthread, but as long as this flag is invariant during the
377 * thread's lifetime, it is OK to check its state.
379 if ((td->td_pflags & TDP_KTHREAD) == 0)
383 * The caller of the primitive should have already checked that the
384 * thread is up and running, thus not being blocked by other
389 td->td_flags |= TDF_KTH_SUSP;
391 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
396 * Resume a thread previously put asleep with kthread_suspend().
399 kthread_resume(struct thread *td)
406 * td_pflags should not be read by any thread other than
407 * curthread, but as long as this flag is invariant during the
408 * thread's lifetime, it is OK to check its state.
410 if ((td->td_pflags & TDP_KTHREAD) == 0)
415 td->td_flags &= ~TDF_KTH_SUSP;
417 wakeup(&td->td_flags);
423 * Used by the thread to poll as to whether it should yield/sleep
424 * and notify the caller that is has happened.
427 kthread_suspend_check(void)
435 if ((td->td_pflags & TDP_KTHREAD) == 0)
436 panic("%s: curthread is not a valid kthread", __func__);
439 * Setting the TDF_KTH_SUSP flag is protected by process lock.
441 * Do an unlocked read first to avoid serializing with all other threads
442 * in the common case of not suspending.
444 if ((td->td_flags & TDF_KTH_SUSP) == 0)
447 while ((td->td_flags & TDF_KTH_SUSP) != 0) {
448 wakeup(&td->td_flags);
449 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
455 kproc_kthread_add(void (*func)(void *), void *arg,
456 struct proc **procptr, struct thread **tdptr,
457 int flags, int pages, const char *procname, const char *fmt, ...)
464 if (*procptr == NULL) {
465 error = kproc_create(func, arg,
466 procptr, flags, pages, "%s", procname);
469 td = FIRST_THREAD_IN_PROC(*procptr);
473 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
476 sched_clear_tdname(td);
481 vsnprintf(buf, sizeof(buf), fmt, ap);
483 error = kthread_add(func, arg, *procptr,
484 tdptr, flags, pages, "%s", buf);