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>
44 #include <sys/unistd.h>
46 #include <sys/sched.h>
47 #include <sys/tslog.h>
49 #include <vm/vm_extern.h>
51 #include <machine/stdarg.h>
54 * Start a kernel process. This is called after a fork() call in
55 * mi_startup() in the file kern/init_main.c.
57 * This function is used to start "internal" daemons and intended
58 * to be called from SYSINIT().
61 kproc_start(const void *udata)
63 const struct kproc_desc *kp = udata;
66 error = kproc_create((void (*)(void *))kp->func, NULL,
67 kp->global_procpp, 0, 0, "%s", kp->arg0);
69 panic("kproc_start: %s: error %d", kp->arg0, error);
73 * Create a kernel process/thread/whatever. It shares its address space
74 * with proc0 - ie: kernel only.
76 * func is the function to start.
77 * arg is the parameter to pass to function on first startup.
78 * newpp is the return value pointing to the thread's struct proc.
79 * flags are flags to fork1 (in unistd.h)
80 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
83 kproc_create(void (*func)(void *), void *arg,
84 struct proc **newpp, int flags, int pages, const char *fmt, ...)
93 panic("kproc_create called too soon");
95 bzero(&fr, sizeof(fr));
96 fr.fr_flags = RFMEM | RFFDG | RFPROC | RFSTOPPED | flags;
99 error = fork1(&thread0, &fr);
103 /* save a global descriptor, if desired */
107 /* this is a non-swapped system process */
109 td = FIRST_THREAD_IN_PROC(p2);
110 p2->p_flag |= P_SYSTEM | P_KPROC;
111 td->td_pflags |= TDP_KTHREAD;
112 mtx_lock(&p2->p_sigacts->ps_mtx);
113 p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
114 mtx_unlock(&p2->p_sigacts->ps_mtx);
117 /* set up arg0 for 'ps', et al */
119 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
121 /* set up arg0 for 'ps', et al */
123 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
126 sched_clear_tdname(td);
128 TSTHREAD(td, td->td_name);
130 if (PMC_SYSTEM_SAMPLING_ACTIVE()) {
131 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_PROC_CREATE_LOG, p2);
132 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
136 /* call the processes' main()... */
137 cpu_fork_kthread_handler(td, func, arg);
139 /* Avoid inheriting affinity from a random parent. */
140 cpuset_kernthread(td);
144 sched_user_prio(td, PUSER);
146 /* Delay putting it on the run queue until now. */
147 if (!(flags & RFSTOPPED))
148 sched_add(td, SRQ_BORING);
156 kproc_exit(int ecode)
165 * Reparent curthread from proc0 to init so that the zombie
168 sx_xlock(&proctree_lock);
170 proc_reparent(p, initproc, true);
172 sx_xunlock(&proctree_lock);
175 * Wakeup anyone waiting for us to exit.
184 * Advise a kernel process to suspend (or resume) in its main loop.
185 * Participation is voluntary.
188 kproc_suspend(struct proc *p, int timo)
191 * Make sure this is indeed a system process and we can safely
192 * use the p_siglist field.
195 if ((p->p_flag & P_KPROC) == 0) {
199 SIGADDSET(p->p_siglist, SIGSTOP);
201 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo);
205 kproc_resume(struct proc *p)
208 * Make sure this is indeed a system process and we can safely
209 * use the p_siglist field.
212 if ((p->p_flag & P_KPROC) == 0) {
216 SIGDELSET(p->p_siglist, SIGSTOP);
218 wakeup(&p->p_siglist);
223 kproc_suspend_check(struct proc *p)
226 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
227 wakeup(&p->p_siglist);
228 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
234 * Start a kernel thread.
236 * This function is used to start "internal" daemons and intended
237 * to be called from SYSINIT().
241 kthread_start(const void *udata)
243 const struct kthread_desc *kp = udata;
246 error = kthread_add((void (*)(void *))kp->func, NULL,
247 NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
249 panic("kthread_start: %s: error %d", kp->arg0, error);
253 * Create a kernel thread. It shares its address space
254 * with proc0 - ie: kernel only.
256 * func is the function to start.
257 * arg is the parameter to pass to function on first startup.
258 * newtdp is the return value pointing to the thread's struct thread.
259 * ** XXX fix this --> flags are flags to fork1 (in unistd.h)
260 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
263 kthread_add(void (*func)(void *), void *arg, struct proc *p,
264 struct thread **newtdp, int flags, int pages, const char *fmt, ...)
267 struct thread *newtd, *oldtd;
270 panic("kthread_add called too soon");
272 /* If no process supplied, put it on proc0 */
276 /* Initialize our new td */
277 newtd = thread_alloc(pages);
282 oldtd = FIRST_THREAD_IN_PROC(p);
284 bzero(&newtd->td_startzero,
285 __rangeof(struct thread, td_startzero, td_endzero));
286 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
287 __rangeof(struct thread, td_startcopy, td_endcopy));
289 /* set up arg0 for 'ps', et al */
291 vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap);
294 TSTHREAD(newtd, newtd->td_name);
296 newtd->td_proc = p; /* needed for cpu_copy_thread */
297 /* might be further optimized for kthread */
298 cpu_copy_thread(newtd, oldtd);
299 /* put the designated function(arg) as the resume context */
300 cpu_fork_kthread_handler(newtd, func, arg);
302 newtd->td_pflags |= TDP_KTHREAD;
303 thread_cow_get_proc(newtd, p);
305 /* this code almost the same as create_thread() in kern_thr.c */
306 p->p_flag |= P_HADTHREADS;
307 thread_link(newtd, p);
309 /* let the scheduler know about these things. */
310 sched_fork_thread(oldtd, newtd);
311 TD_SET_CAN_RUN(newtd);
312 thread_unlock(oldtd);
317 /* Avoid inheriting affinity from a random parent. */
318 cpuset_kernthread(newtd);
320 if (PMC_SYSTEM_SAMPLING_ACTIVE())
321 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
323 /* Delay putting it on the run queue until now. */
324 if (!(flags & RFSTOPPED)) {
326 sched_add(newtd, SRQ_BORING);
343 if (PMC_SYSTEM_SAMPLING_ACTIVE())
344 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
346 /* A module may be waiting for us to exit. */
350 * The last exiting thread in a kernel process must tear down
354 if (p->p_numthreads == 1) {
359 umtx_thread_exit(td);
366 * Advise a kernel process to suspend (or resume) in its main loop.
367 * Participation is voluntary.
370 kthread_suspend(struct thread *td, int timo)
377 * td_pflags should not be read by any thread other than
378 * curthread, but as long as this flag is invariant during the
379 * thread's lifetime, it is OK to check its state.
381 if ((td->td_pflags & TDP_KTHREAD) == 0)
385 * The caller of the primitive should have already checked that the
386 * thread is up and running, thus not being blocked by other
391 td->td_flags |= TDF_KTH_SUSP;
393 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
398 * Resume a thread previously put asleep with kthread_suspend().
401 kthread_resume(struct thread *td)
408 * td_pflags should not be read by any thread other than
409 * curthread, but as long as this flag is invariant during the
410 * thread's lifetime, it is OK to check its state.
412 if ((td->td_pflags & TDP_KTHREAD) == 0)
417 td->td_flags &= ~TDF_KTH_SUSP;
419 wakeup(&td->td_flags);
425 * Used by the thread to poll as to whether it should yield/sleep
426 * and notify the caller that is has happened.
429 kthread_suspend_check(void)
437 if ((td->td_pflags & TDP_KTHREAD) == 0)
438 panic("%s: curthread is not a valid kthread", __func__);
441 * Setting the TDF_KTH_SUSP flag is protected by process lock.
443 * Do an unlocked read first to avoid serializing with all other threads
444 * in the common case of not suspending.
446 if ((td->td_flags & TDF_KTH_SUSP) == 0)
449 while ((td->td_flags & TDF_KTH_SUSP) != 0) {
450 wakeup(&td->td_flags);
451 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
457 kproc_kthread_add(void (*func)(void *), void *arg,
458 struct proc **procptr, struct thread **tdptr,
459 int flags, int pages, const char *procname, const char *fmt, ...)
466 if (*procptr == NULL) {
467 error = kproc_create(func, arg,
468 procptr, flags, pages, "%s", procname);
471 td = FIRST_THREAD_IN_PROC(*procptr);
475 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
478 sched_clear_tdname(td);
483 vsnprintf(buf, sizeof(buf), fmt, ap);
485 error = kthread_add(func, arg, *procptr,
486 tdptr, flags, pages, "%s", buf);