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/kthread.h>
34 #include <sys/mutex.h>
36 #include <sys/resourcevar.h>
37 #include <sys/signalvar.h>
39 #include <sys/unistd.h>
41 #include <sys/sched.h>
43 #include <vm/vm_extern.h>
45 #include <machine/stdarg.h>
48 * Start a kernel process. This is called after a fork() call in
49 * mi_startup() in the file kern/init_main.c.
51 * This function is used to start "internal" daemons and intended
52 * to be called from SYSINIT().
58 const struct kproc_desc
*kp
= udata
;
61 error
= kproc_create((void (*)(void *))kp
->func
, NULL
,
62 kp
->global_procpp
, 0, 0, "%s", kp
->arg0
);
64 panic("kproc_start: %s: error %d", kp
->arg0
, error
);
68 * Create a kernel process/thread/whatever. It shares its address space
69 * with proc0 - ie: kernel only.
71 * func is the function to start.
72 * arg is the parameter to pass to function on first startup.
73 * newpp is the return value pointing to the thread's struct proc.
74 * flags are flags to fork1 (in unistd.h)
75 * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
78 kproc_create(void (*func
)(void *), void *arg
,
79 struct proc
**newpp
, int flags
, int pages
, const char *fmt
, ...)
87 panic("kproc_create called too soon");
89 error
= fork1(&thread0
, RFMEM
| RFFDG
| RFPROC
| RFSTOPPED
| flags
,
94 /* save a global descriptor, if desired */
98 /* this is a non-swapped system process */
100 td
= FIRST_THREAD_IN_PROC(p2
);
101 p2
->p_flag
|= P_SYSTEM
| P_KTHREAD
;
102 td
->td_pflags
|= TDP_KTHREAD
;
103 mtx_lock(&p2
->p_sigacts
->ps_mtx
);
104 p2
->p_sigacts
->ps_flag
|= PS_NOCLDWAIT
;
105 mtx_unlock(&p2
->p_sigacts
->ps_mtx
);
108 /* set up arg0 for 'ps', et al */
110 vsnprintf(p2
->p_comm
, sizeof(p2
->p_comm
), fmt
, ap
);
112 /* set up arg0 for 'ps', et al */
114 vsnprintf(td
->td_name
, sizeof(td
->td_name
), fmt
, ap
);
117 /* call the processes' main()... */
118 cpu_set_fork_handler(td
, func
, arg
);
121 /* Delay putting it on the run queue until now. */
122 if (!(flags
& RFSTOPPED
)) {
124 sched_add(td
, SRQ_BORING
);
132 kproc_exit(int ecode
)
141 * Reparent curthread from proc0 to init so that the zombie
144 sx_xlock(&proctree_lock
);
146 proc_reparent(p
, initproc
);
148 sx_xunlock(&proctree_lock
);
151 * Wakeup anyone waiting for us to exit.
156 exit1(td
, W_EXITCODE(ecode
, 0));
160 * Advise a kernel process to suspend (or resume) in its main loop.
161 * Participation is voluntary.
164 kproc_suspend(struct proc
*p
, int timo
)
167 * Make sure this is indeed a system process and we can safely
168 * use the p_siglist field.
171 if ((p
->p_flag
& P_KTHREAD
) == 0) {
175 SIGADDSET(p
->p_siglist
, SIGSTOP
);
177 return msleep(&p
->p_siglist
, &p
->p_mtx
, PPAUSE
| PDROP
, "suspkp", timo
);
181 kproc_resume(struct proc
*p
)
184 * Make sure this is indeed a system process and we can safely
185 * use the p_siglist field.
188 if ((p
->p_flag
& P_KTHREAD
) == 0) {
192 SIGDELSET(p
->p_siglist
, SIGSTOP
);
194 wakeup(&p
->p_siglist
);
199 kproc_suspend_check(struct proc
*p
)
202 while (SIGISMEMBER(p
->p_siglist
, SIGSTOP
)) {
203 wakeup(&p
->p_siglist
);
204 msleep(&p
->p_siglist
, &p
->p_mtx
, PPAUSE
, "kpsusp", 0);
211 * Start a kernel thread.
213 * This function is used to start "internal" daemons and intended
214 * to be called from SYSINIT().
221 const struct kthread_desc
*kp
= udata
;
224 error
= kthread_add((void (*)(void *))kp
->func
, NULL
,
225 NULL
, kp
->global_threadpp
, 0, 0, "%s", kp
->arg0
);
227 panic("kthread_start: %s: error %d", kp
->arg0
, error
);
231 * Create a kernel thread. It shares its address space
232 * with proc0 - ie: kernel only.
234 * func is the function to start.
235 * arg is the parameter to pass to function on first startup.
236 * newtdp is the return value pointing to the thread's struct thread.
237 * ** XXX fix this --> flags are flags to fork1 (in unistd.h)
238 * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
241 kthread_add(void (*func
)(void *), void *arg
, struct proc
*p
,
242 struct thread
**newtdp
, int flags
, int pages
, const char *fmt
, ...)
245 struct thread
*newtd
, *oldtd
;
248 panic("kthread_add called too soon");
250 /* If no process supplied, put it on proc0 */
255 oldtd
= FIRST_THREAD_IN_PROC(p
);
258 /* Initialize our new td */
259 newtd
= thread_alloc();
263 bzero(&newtd
->td_startzero
,
264 __rangeof(struct thread
, td_startzero
, td_endzero
));
265 /* XXX check if we should zero. */
266 bcopy(&oldtd
->td_startcopy
, &newtd
->td_startcopy
,
267 __rangeof(struct thread
, td_startcopy
, td_endcopy
));
269 /* set up arg0 for 'ps', et al */
271 vsnprintf(newtd
->td_name
, sizeof(newtd
->td_name
), fmt
, ap
);
274 newtd
->td_proc
= p
; /* needed for cpu_set_upcall */
276 /* XXX optimise this probably? */
277 /* On x86 (and probably the others too) it is way too full of junk */
278 /* Needs a better name */
279 cpu_set_upcall(newtd
, oldtd
);
280 /* put the designated function(arg) as the resume context */
281 cpu_set_fork_handler(newtd
, func
, arg
);
283 newtd
->td_pflags
|= TDP_KTHREAD
;
284 newtd
->td_ucred
= crhold(p
->p_ucred
);
285 /* Allocate and switch to an alternate kstack if specified. */
287 vm_thread_new_altkstack(newtd
, pages
);
289 /* this code almost the same as create_thread() in kern_thr.c */
291 p
->p_flag
|= P_HADTHREADS
;
292 newtd
->td_sigmask
= oldtd
->td_sigmask
; /* XXX dubious */
293 thread_link(newtd
, p
);
295 /* let the scheduler know about these things. */
296 sched_fork_thread(oldtd
, newtd
);
297 TD_SET_CAN_RUN(newtd
);
298 thread_unlock(oldtd
);
302 /* Delay putting it on the run queue until now. */
303 if (!(flags
& RFSTOPPED
)) {
305 sched_add(newtd
, SRQ_BORING
);
306 thread_unlock(newtd
);
318 /* A module may be waiting for us to exit. */
322 * We could rely on thread_exit to call exit1() but
323 * there is extra work that needs to be done
325 if (curthread
->td_proc
->p_numthreads
== 1)
326 kproc_exit(0); /* never returns */
328 p
= curthread
->td_proc
;
335 * Advise a kernel process to suspend (or resume) in its main loop.
336 * Participation is voluntary.
339 kthread_suspend(struct thread
*td
, int timo
)
341 if ((td
->td_pflags
& TDP_KTHREAD
) == 0) {
345 td
->td_flags
|= TDF_KTH_SUSP
;
348 * If it's stopped for some other reason,
349 * kick it to notice our request
350 * or we'll end up timing out
352 wakeup(td
); /* traditional place for kernel threads to sleep on */ /* XXX ?? */
353 return (tsleep(&td
->td_flags
, PPAUSE
| PDROP
, "suspkt", timo
));
357 * let the kthread it can keep going again.
360 kthread_resume(struct thread
*td
)
362 if ((td
->td_pflags
& TDP_KTHREAD
) == 0) {
366 td
->td_flags
&= ~TDF_KTH_SUSP
;
368 wakeup(&td
->td_name
);
373 * Used by the thread to poll as to whether it should yield/sleep
374 * and notify the caller that is has happened.
377 kthread_suspend_check(struct thread
*td
)
379 while (td
->td_flags
& TDF_KTH_SUSP
) {
381 * let the caller know we got the message then sleep
383 wakeup(&td
->td_flags
);
384 tsleep(&td
->td_name
, PPAUSE
, "ktsusp", 0);
389 kproc_kthread_add(void (*func
)(void *), void *arg
,
390 struct proc
**procptr
, struct thread
**tdptr
,
391 int flags
, int pages
, char * procname
, const char *fmt
, ...)
399 error
= kproc_create(func
, arg
,
400 procptr
, flags
, pages
, "%s", procname
);
403 td
= FIRST_THREAD_IN_PROC(*procptr
);
407 vsnprintf(td
->td_name
, sizeof(td
->td_name
), fmt
, ap
);
412 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
414 error
= kthread_add(func
, arg
, *procptr
,
415 tdptr
, flags
, pages
, "%s", buf
);