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/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/cpuset.h>
33 #include <sys/kthread.h>
35 #include <sys/mutex.h>
37 #include <sys/resourcevar.h>
38 #include <sys/rwlock.h>
39 #include <sys/signalvar.h>
40 #include <sys/sysent.h>
42 #include <sys/umtxvar.h>
43 #include <sys/unistd.h>
45 #include <sys/sched.h>
46 #include <sys/tslog.h>
48 #include <vm/vm_extern.h>
50 #include <machine/stdarg.h>
53 * Start a kernel process. This is called after a fork() call in
54 * mi_startup() in the file kern/init_main.c.
56 * This function is used to start "internal" daemons and intended
57 * to be called from SYSINIT().
60 kproc_start(const void *udata)
62 const struct kproc_desc *kp = udata;
65 error = kproc_create((void (*)(void *))kp->func, NULL,
66 kp->global_procpp, 0, 0, "%s", kp->arg0);
68 panic("kproc_start: %s: error %d", kp->arg0, error);
72 * Create a kernel process/thread/whatever. It shares its address space
73 * with proc0 - ie: kernel only.
75 * func is the function to start.
76 * arg is the parameter to pass to function on first startup.
77 * newpp is the return value pointing to the thread's struct proc.
78 * flags are flags to fork1 (in unistd.h)
79 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
82 kproc_create1(void (*func)(void *), void *arg,
83 struct proc **newpp, int flags, int pages, const char *tdname)
91 panic("kproc_create called too soon");
93 bzero(&fr, sizeof(fr));
94 fr.fr_flags = RFMEM | RFFDG | RFPROC | RFSTOPPED | flags;
95 fr.fr_flags2 = FR2_KPROC;
98 error = fork1(&thread0, &fr);
102 /* save a global descriptor, if desired */
106 /* set up arg0 for 'ps', et al */
107 strcpy(p2->p_comm, tdname);
108 td = FIRST_THREAD_IN_PROC(p2);
109 strcpy(td->td_name, tdname);
111 sched_clear_tdname(td);
113 TSTHREAD(td, td->td_name);
115 if (PMC_SYSTEM_SAMPLING_ACTIVE()) {
116 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_PROC_CREATE_LOG, p2);
117 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
121 /* call the processes' main()... */
122 cpu_fork_kthread_handler(td, func, arg);
124 /* Avoid inheriting affinity from a random parent. */
125 cpuset_kernthread(td);
129 sched_user_prio(td, PUSER);
131 /* Delay putting it on the run queue until now. */
132 if ((flags & RFSTOPPED) == 0)
133 sched_add(td, SRQ_BORING);
141 kproc_create(void (*func)(void *), void *arg,
142 struct proc **newpp, int flags, int pages, const char *fmt, ...)
146 char tdname[MAXCOMLEN + 1];
149 vsnprintf(tdname, sizeof(tdname), fmt, ap);
152 error = kproc_create1(func, arg, newpp, flags, pages, tdname);
158 kproc_exit(int ecode)
167 * Reparent curthread from proc0 to init so that the zombie
170 sx_xlock(&proctree_lock);
172 proc_reparent(p, initproc, true);
174 sx_xunlock(&proctree_lock);
177 * Wakeup anyone waiting for us to exit.
186 * Advise a kernel process to suspend (or resume) in its main loop.
187 * Participation is voluntary.
190 kproc_suspend(struct proc *p, int timo)
193 * Make sure this is indeed a system process and we can safely
194 * use the p_siglist field.
197 if ((p->p_flag & P_KPROC) == 0) {
201 SIGADDSET(p->p_siglist, SIGSTOP);
203 return (msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP,
208 kproc_resume(struct proc *p)
211 * Make sure this is indeed a system process and we can safely
212 * use the p_siglist field.
215 if ((p->p_flag & P_KPROC) == 0) {
219 SIGDELSET(p->p_siglist, SIGSTOP);
221 wakeup(&p->p_siglist);
226 kproc_suspend_check(struct proc *p)
229 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
230 wakeup(&p->p_siglist);
231 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
237 * Start a kernel thread.
239 * This function is used to start "internal" daemons and intended
240 * to be called from SYSINIT().
244 kthread_start(const void *udata)
246 const struct kthread_desc *kp = udata;
249 error = kthread_add((void (*)(void *))kp->func, NULL,
250 NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
252 panic("kthread_start: %s: error %d", kp->arg0, error);
256 * Create a kernel thread. It shares its address space
257 * with proc0 - ie: kernel only.
259 * func is the function to start.
260 * arg is the parameter to pass to function on first startup.
261 * newtdp is the return value pointing to the thread's struct thread.
262 * ** XXX fix this --> flags are flags to fork1 (in unistd.h)
263 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
266 kthread_add1(void (*func)(void *), void *arg, struct proc *p,
267 struct thread **newtdp, int flags, int pages, const char *tdname)
269 struct thread *newtd, *oldtd;
272 panic("kthread_add called too soon");
274 /* If no process supplied, put it on proc0 */
278 /* Initialize our new td */
279 newtd = thread_alloc(pages);
284 if (p->p_state == PRS_ZOMBIE || (p->p_flag2 & P2_WEXIT) != 0) {
288 oldtd = FIRST_THREAD_IN_PROC(p);
290 bzero(&newtd->td_startzero,
291 __rangeof(struct thread, td_startzero, td_endzero));
292 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
293 __rangeof(struct thread, td_startcopy, td_endcopy));
295 /* set up arg0 for 'ps', et al */
296 strcpy(newtd->td_name, tdname);
298 TSTHREAD(newtd, newtd->td_name);
300 newtd->td_proc = p; /* needed for cpu_copy_thread */
301 newtd->td_pflags |= TDP_KTHREAD;
303 /* might be further optimized for kthread */
304 cpu_copy_thread(newtd, oldtd);
306 /* put the designated function(arg) as the resume context */
307 cpu_fork_kthread_handler(newtd, func, arg);
309 thread_cow_get_proc(newtd, p);
311 /* This code is similar to thread_create() in kern_thr.c. */
312 p->p_flag |= P_HADTHREADS;
313 thread_link(newtd, p);
315 /* let the scheduler know about these things. */
316 sched_fork_thread(oldtd, newtd);
317 TD_SET_CAN_RUN(newtd);
318 thread_unlock(oldtd);
323 /* Avoid inheriting affinity from a random parent. */
324 cpuset_kernthread(newtd);
326 if (PMC_SYSTEM_SAMPLING_ACTIVE())
327 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
329 /* Delay putting it on the run queue until now. */
330 if ((flags & RFSTOPPED) == 0) {
332 sched_add(newtd, SRQ_BORING);
340 kthread_add(void (*func)(void *), void *arg, struct proc *p,
341 struct thread **newtdp, int flags, int pages, const char *fmt, ...)
345 char tdname[MAXCOMLEN + 1];
348 vsnprintf(tdname, sizeof(tdname), fmt, ap);
351 error = kthread_add1(func, arg, p, newtdp, flags, pages, tdname);
366 if (PMC_SYSTEM_SAMPLING_ACTIVE())
367 PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
369 /* A module may be waiting for us to exit. */
373 * The last exiting thread in a kernel process must tear down
377 if (p->p_numthreads == 1) {
382 if (p->p_sysent->sv_ontdexit != NULL)
383 p->p_sysent->sv_ontdexit(td);
386 umtx_thread_exit(td);
393 * Advise a kernel process to suspend (or resume) in its main loop.
394 * Participation is voluntary.
397 kthread_suspend(struct thread *td, int timo)
404 * td_pflags should not be read by any thread other than
405 * curthread, but as long as this flag is invariant during the
406 * thread's lifetime, it is OK to check its state.
408 if ((td->td_pflags & TDP_KTHREAD) == 0)
412 * The caller of the primitive should have already checked that the
413 * thread is up and running, thus not being blocked by other
418 td->td_flags |= TDF_KTH_SUSP;
420 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
425 * Resume a thread previously put asleep with kthread_suspend().
428 kthread_resume(struct thread *td)
435 * td_pflags should not be read by any thread other than
436 * curthread, but as long as this flag is invariant during the
437 * thread's lifetime, it is OK to check its state.
439 if ((td->td_pflags & TDP_KTHREAD) == 0)
444 td->td_flags &= ~TDF_KTH_SUSP;
446 wakeup(&td->td_flags);
452 * Used by the thread to poll as to whether it should yield/sleep
453 * and notify the caller that is has happened.
456 kthread_suspend_check(void)
464 if ((td->td_pflags & TDP_KTHREAD) == 0)
465 panic("%s: curthread is not a valid kthread", __func__);
468 * Setting the TDF_KTH_SUSP flag is protected by process lock.
470 * Do an unlocked read first to avoid serializing with all other threads
471 * in the common case of not suspending.
473 if ((td->td_flags & TDF_KTH_SUSP) == 0)
476 while ((td->td_flags & TDF_KTH_SUSP) != 0) {
477 wakeup(&td->td_flags);
478 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
484 kproc_kthread_add(void (*func)(void *), void *arg,
485 struct proc **procptr, struct thread **tdptr,
486 int flags, int pages, const char *procname, const char *fmt, ...)
493 if (*procptr == NULL) {
494 error = kproc_create(func, arg,
495 procptr, flags, pages, "%s", procname);
498 td = FIRST_THREAD_IN_PROC(*procptr);
502 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
505 sched_clear_tdname(td);
510 vsnprintf(buf, sizeof(buf), fmt, ap);
512 error = kthread_add(func, arg, *procptr,
513 tdptr, flags, pages, "%s", buf);