2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/cpuset.h>
32 #include <sys/kthread.h>
34 #include <sys/mutex.h>
36 #include <sys/resourcevar.h>
37 #include <sys/rwlock.h>
38 #include <sys/signalvar.h>
39 #include <sys/sysent.h>
41 #include <sys/umtxvar.h>
42 #include <sys/unistd.h>
44 #include <sys/sched.h>
45 #include <sys/tslog.h>
47 #include <vm/vm_extern.h>
49 #include <machine/stdarg.h>
52 * Start a kernel process. This is called after a fork() call in
53 * mi_startup() in the file kern/init_main.c.
55 * This function is used to start "internal" daemons and intended
56 * to be called from SYSINIT().
59 kproc_start(const void *udata)
61 const struct kproc_desc *kp = udata;
64 error = kproc_create((void (*)(void *))kp->func, NULL,
65 kp->global_procpp, 0, 0, "%s", kp->arg0);
67 panic("kproc_start: %s: error %d", kp->arg0, error);
71 * Create a kernel process/thread/whatever. It shares its address space
72 * with proc0 - ie: kernel only.
74 * func is the function to start.
75 * arg is the parameter to pass to function on first startup.
76 * newpp is the return value pointing to the thread's struct proc.
77 * flags are flags to fork1 (in unistd.h)
78 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
81 kproc_create1(void (*func)(void *), void *arg,
82 struct proc **newpp, int flags, int pages, const char *tdname)
90 panic("kproc_create called too soon");
92 bzero(&fr, sizeof(fr));
93 fr.fr_flags = RFMEM | RFFDG | RFPROC | RFSTOPPED | flags;
94 fr.fr_flags2 = FR2_KPROC;
97 error = fork1(&thread0, &fr);
101 /* save a global descriptor, if desired */
105 /* set up arg0 for 'ps', et al */
106 strcpy(p2->p_comm, tdname);
107 td = FIRST_THREAD_IN_PROC(p2);
108 strcpy(td->td_name, tdname);
110 sched_clear_tdname(td);
112 TSTHREAD(td, td->td_name);
114 if (PMC_SYSTEM_SAMPLING_ACTIVE()) {
115 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_PROC_CREATE_LOG, p2);
116 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
120 /* call the processes' main()... */
121 cpu_fork_kthread_handler(td, func, arg);
123 /* Avoid inheriting affinity from a random parent. */
124 cpuset_kernthread(td);
128 sched_user_prio(td, PUSER);
130 /* Delay putting it on the run queue until now. */
131 if ((flags & RFSTOPPED) == 0)
132 sched_add(td, SRQ_BORING);
140 kproc_create(void (*func)(void *), void *arg,
141 struct proc **newpp, int flags, int pages, const char *fmt, ...)
145 char tdname[MAXCOMLEN + 1];
148 vsnprintf(tdname, sizeof(tdname), fmt, ap);
151 error = kproc_create1(func, arg, newpp, flags, pages, tdname);
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,
207 kproc_resume(struct proc *p)
210 * Make sure this is indeed a system process and we can safely
211 * use the p_siglist field.
214 if ((p->p_flag & P_KPROC) == 0) {
218 SIGDELSET(p->p_siglist, SIGSTOP);
220 wakeup(&p->p_siglist);
225 kproc_suspend_check(struct proc *p)
228 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
229 wakeup(&p->p_siglist);
230 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
236 * Start a kernel thread.
238 * This function is used to start "internal" daemons and intended
239 * to be called from SYSINIT().
243 kthread_start(const void *udata)
245 const struct kthread_desc *kp = udata;
248 error = kthread_add((void (*)(void *))kp->func, NULL,
249 NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
251 panic("kthread_start: %s: error %d", kp->arg0, error);
255 * Create a kernel thread. It shares its address space
256 * with proc0 - ie: kernel only.
258 * func is the function to start.
259 * arg is the parameter to pass to function on first startup.
260 * newtdp is the return value pointing to the thread's struct thread.
261 * ** XXX fix this --> flags are flags to fork1 (in unistd.h)
262 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
265 kthread_add1(void (*func)(void *), void *arg, struct proc *p,
266 struct thread **newtdp, int flags, int pages, const char *tdname)
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 if (p->p_state == PRS_ZOMBIE || (p->p_flag2 & P2_WEXIT) != 0) {
287 oldtd = FIRST_THREAD_IN_PROC(p);
289 bzero(&newtd->td_startzero,
290 __rangeof(struct thread, td_startzero, td_endzero));
291 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
292 __rangeof(struct thread, td_startcopy, td_endcopy));
294 /* set up arg0 for 'ps', et al */
295 strcpy(newtd->td_name, tdname);
297 TSTHREAD(newtd, newtd->td_name);
299 newtd->td_proc = p; /* needed for cpu_copy_thread */
300 newtd->td_pflags |= TDP_KTHREAD;
302 /* might be further optimized for kthread */
303 cpu_copy_thread(newtd, oldtd);
305 /* put the designated function(arg) as the resume context */
306 cpu_fork_kthread_handler(newtd, func, arg);
308 thread_cow_get_proc(newtd, p);
310 /* This code is similar to thread_create() in kern_thr.c. */
311 p->p_flag |= P_HADTHREADS;
312 thread_link(newtd, p);
314 /* let the scheduler know about these things. */
315 sched_fork_thread(oldtd, newtd);
316 TD_SET_CAN_RUN(newtd);
317 thread_unlock(oldtd);
322 /* Avoid inheriting affinity from a random parent. */
323 cpuset_kernthread(newtd);
325 if (PMC_SYSTEM_SAMPLING_ACTIVE())
326 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
328 /* Delay putting it on the run queue until now. */
329 if ((flags & RFSTOPPED) == 0) {
331 sched_add(newtd, SRQ_BORING);
339 kthread_add(void (*func)(void *), void *arg, struct proc *p,
340 struct thread **newtdp, int flags, int pages, const char *fmt, ...)
344 char tdname[MAXCOMLEN + 1];
347 vsnprintf(tdname, sizeof(tdname), fmt, ap);
350 error = kthread_add1(func, arg, p, newtdp, flags, pages, tdname);
365 if (PMC_SYSTEM_SAMPLING_ACTIVE())
366 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
368 /* A module may be waiting for us to exit. */
372 * The last exiting thread in a kernel process must tear down
376 if (p->p_numthreads == 1) {
381 if (p->p_sysent->sv_ontdexit != NULL)
382 p->p_sysent->sv_ontdexit(td);
385 umtx_thread_exit(td);
392 * Advise a kernel process to suspend (or resume) in its main loop.
393 * Participation is voluntary.
396 kthread_suspend(struct thread *td, int timo)
403 * td_pflags should not be read by any thread other than
404 * curthread, but as long as this flag is invariant during the
405 * thread's lifetime, it is OK to check its state.
407 if ((td->td_pflags & TDP_KTHREAD) == 0)
411 * The caller of the primitive should have already checked that the
412 * thread is up and running, thus not being blocked by other
417 td->td_flags |= TDF_KTH_SUSP;
419 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
424 * Resume a thread previously put asleep with kthread_suspend().
427 kthread_resume(struct thread *td)
434 * td_pflags should not be read by any thread other than
435 * curthread, but as long as this flag is invariant during the
436 * thread's lifetime, it is OK to check its state.
438 if ((td->td_pflags & TDP_KTHREAD) == 0)
443 td->td_flags &= ~TDF_KTH_SUSP;
445 wakeup(&td->td_flags);
451 * Used by the thread to poll as to whether it should yield/sleep
452 * and notify the caller that is has happened.
455 kthread_suspend_check(void)
463 if ((td->td_pflags & TDP_KTHREAD) == 0)
464 panic("%s: curthread is not a valid kthread", __func__);
467 * Setting the TDF_KTH_SUSP flag is protected by process lock.
469 * Do an unlocked read first to avoid serializing with all other threads
470 * in the common case of not suspending.
472 if ((td->td_flags & TDF_KTH_SUSP) == 0)
475 while ((td->td_flags & TDF_KTH_SUSP) != 0) {
476 wakeup(&td->td_flags);
477 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
483 kproc_kthread_add(void (*func)(void *), void *arg,
484 struct proc **procptr, struct thread **tdptr,
485 int flags, int pages, const char *procname, const char *fmt, ...)
492 if (*procptr == NULL) {
493 error = kproc_create(func, arg,
494 procptr, flags, pages, "%s", procname);
497 td = FIRST_THREAD_IN_PROC(*procptr);
501 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
504 sched_clear_tdname(td);
509 vsnprintf(buf, sizeof(buf), fmt, ap);
511 error = kthread_add(func, arg, *procptr,
512 tdptr, flags, pages, "%s", buf);