2 * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
41 #include <sys/unistd.h>
43 #include <sys/sched.h>
45 #include <vm/vm_extern.h>
47 #include <machine/stdarg.h>
50 * Start a kernel process. This is called after a fork() call in
51 * mi_startup() in the file kern/init_main.c.
53 * This function is used to start "internal" daemons and intended
54 * to be called from SYSINIT().
60 const struct kproc_desc *kp = udata;
63 error = kproc_create((void (*)(void *))kp->func, NULL,
64 kp->global_procpp, 0, 0, "%s", kp->arg0);
66 panic("kproc_start: %s: error %d", kp->arg0, error);
70 * Create a kernel process/thread/whatever. It shares its address space
71 * with proc0 - ie: kernel only.
73 * func is the function to start.
74 * arg is the parameter to pass to function on first startup.
75 * newpp is the return value pointing to the thread's struct proc.
76 * flags are flags to fork1 (in unistd.h)
77 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
80 kproc_create(void (*func)(void *), void *arg,
81 struct proc **newpp, int flags, int pages, const char *fmt, ...)
89 panic("kproc_create called too soon");
91 error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
96 /* save a global descriptor, if desired */
100 /* this is a non-swapped system process */
102 td = FIRST_THREAD_IN_PROC(p2);
103 p2->p_flag |= P_SYSTEM | P_KTHREAD;
104 td->td_pflags |= TDP_KTHREAD;
105 mtx_lock(&p2->p_sigacts->ps_mtx);
106 p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
107 mtx_unlock(&p2->p_sigacts->ps_mtx);
110 /* set up arg0 for 'ps', et al */
112 vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
114 /* set up arg0 for 'ps', et al */
116 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
119 sched_clear_tdname(td);
122 /* call the processes' main()... */
123 cpu_set_fork_handler(td, func, arg);
125 /* Avoid inheriting affinity from a random parent. */
126 cpuset_setthread(td->td_tid, cpuset_root);
130 sched_user_prio(td, PUSER);
132 /* Delay putting it on the run queue until now. */
133 if (!(flags & RFSTOPPED))
134 sched_add(td, SRQ_BORING);
141 kproc_exit(int ecode)
150 * Reparent curthread from proc0 to init so that the zombie
153 sx_xlock(&proctree_lock);
155 proc_reparent(p, initproc);
157 sx_xunlock(&proctree_lock);
160 * Wakeup anyone waiting for us to exit.
165 exit1(td, W_EXITCODE(ecode, 0));
169 * Advise a kernel process to suspend (or resume) in its main loop.
170 * Participation is voluntary.
173 kproc_suspend(struct proc *p, int timo)
176 * Make sure this is indeed a system process and we can safely
177 * use the p_siglist field.
180 if ((p->p_flag & P_KTHREAD) == 0) {
184 SIGADDSET(p->p_siglist, SIGSTOP);
186 return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo);
190 kproc_resume(struct proc *p)
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_KTHREAD) == 0) {
201 SIGDELSET(p->p_siglist, SIGSTOP);
203 wakeup(&p->p_siglist);
208 kproc_suspend_check(struct proc *p)
211 while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
212 wakeup(&p->p_siglist);
213 msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
220 * Start a kernel thread.
222 * This function is used to start "internal" daemons and intended
223 * to be called from SYSINIT().
230 const struct kthread_desc *kp = udata;
233 error = kthread_add((void (*)(void *))kp->func, NULL,
234 NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
236 panic("kthread_start: %s: error %d", kp->arg0, error);
240 * Create a kernel thread. It shares its address space
241 * with proc0 - ie: kernel only.
243 * func is the function to start.
244 * arg is the parameter to pass to function on first startup.
245 * newtdp is the return value pointing to the thread's struct thread.
246 * ** XXX fix this --> flags are flags to fork1 (in unistd.h)
247 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
250 kthread_add(void (*func)(void *), void *arg, struct proc *p,
251 struct thread **newtdp, int flags, int pages, const char *fmt, ...)
254 struct thread *newtd, *oldtd;
257 panic("kthread_add called too soon");
259 /* If no process supplied, put it on proc0 */
264 oldtd = FIRST_THREAD_IN_PROC(p);
267 /* Initialize our new td */
268 newtd = thread_alloc(pages);
272 bzero(&newtd->td_startzero,
273 __rangeof(struct thread, td_startzero, td_endzero));
274 /* XXX check if we should zero. */
275 bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
276 __rangeof(struct thread, td_startcopy, td_endcopy));
278 /* set up arg0 for 'ps', et al */
280 vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap);
283 newtd->td_proc = p; /* needed for cpu_set_upcall */
285 /* XXX optimise this probably? */
286 /* On x86 (and probably the others too) it is way too full of junk */
287 /* Needs a better name */
288 cpu_set_upcall(newtd, oldtd);
289 /* put the designated function(arg) as the resume context */
290 cpu_set_fork_handler(newtd, func, arg);
292 newtd->td_pflags |= TDP_KTHREAD;
293 newtd->td_ucred = crhold(p->p_ucred);
295 /* this code almost the same as create_thread() in kern_thr.c */
297 p->p_flag |= P_HADTHREADS;
298 newtd->td_sigmask = oldtd->td_sigmask; /* XXX dubious */
299 thread_link(newtd, p);
301 /* let the scheduler know about these things. */
302 sched_fork_thread(oldtd, newtd);
303 TD_SET_CAN_RUN(newtd);
304 thread_unlock(oldtd);
309 /* Avoid inheriting affinity from a random parent. */
310 cpuset_setthread(newtd->td_tid, cpuset_root);
312 /* Delay putting it on the run queue until now. */
313 if (!(flags & RFSTOPPED)) {
315 sched_add(newtd, SRQ_BORING);
316 thread_unlock(newtd);
328 p = curthread->td_proc;
330 /* A module may be waiting for us to exit. */
334 * The last exiting thread in a kernel process must tear down
337 rw_wlock(&tidhash_lock);
339 if (p->p_numthreads == 1) {
341 rw_wunlock(&tidhash_lock);
344 LIST_REMOVE(curthread, td_hash);
345 rw_wunlock(&tidhash_lock);
351 * Advise a kernel process to suspend (or resume) in its main loop.
352 * Participation is voluntary.
355 kthread_suspend(struct thread *td, int timo)
362 * td_pflags should not be read by any thread other than
363 * curthread, but as long as this flag is invariant during the
364 * thread's lifetime, it is OK to check its state.
366 if ((td->td_pflags & TDP_KTHREAD) == 0)
370 * The caller of the primitive should have already checked that the
371 * thread is up and running, thus not being blocked by other
376 td->td_flags |= TDF_KTH_SUSP;
378 return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
383 * Resume a thread previously put asleep with kthread_suspend().
386 kthread_resume(struct thread *td)
393 * td_pflags should not be read by any thread other than
394 * curthread, but as long as this flag is invariant during the
395 * thread's lifetime, it is OK to check its state.
397 if ((td->td_pflags & TDP_KTHREAD) == 0)
402 td->td_flags &= ~TDF_KTH_SUSP;
404 wakeup(&td->td_flags);
410 * Used by the thread to poll as to whether it should yield/sleep
411 * and notify the caller that is has happened.
414 kthread_suspend_check()
422 if ((td->td_pflags & TDP_KTHREAD) == 0)
423 panic("%s: curthread is not a valid kthread", __func__);
426 * As long as the double-lock protection is used when accessing the
427 * TDF_KTH_SUSP flag, synchronizing the read operation via proc mutex
431 while (td->td_flags & TDF_KTH_SUSP) {
432 wakeup(&td->td_flags);
433 msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
439 kproc_kthread_add(void (*func)(void *), void *arg,
440 struct proc **procptr, struct thread **tdptr,
441 int flags, int pages, const char *procname, const char *fmt, ...)
449 error = kproc_create(func, arg,
450 procptr, flags, pages, "%s", procname);
453 td = FIRST_THREAD_IN_PROC(*procptr);
457 vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
460 sched_clear_tdname(td);
465 vsnprintf(buf, sizeof(buf), fmt, ap);
467 error = kthread_add(func, arg, *procptr,
468 tdptr, flags, pages, "%s", buf);