1139804Simp/*-
248391Speter * Copyright (c) 1999 Peter Wemm <peter@FreeBSD.org>
348391Speter * All rights reserved.
448391Speter *
548391Speter * Redistribution and use in source and binary forms, with or without
648391Speter * modification, are permitted provided that the following conditions
748391Speter * are met:
848391Speter * 1. Redistributions of source code must retain the above copyright
948391Speter *    notice, this list of conditions and the following disclaimer.
1048391Speter * 2. Redistributions in binary form must reproduce the above copyright
1148391Speter *    notice, this list of conditions and the following disclaimer in the
1248391Speter *    documentation and/or other materials provided with the distribution.
1348391Speter *
1448391Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1548391Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1648391Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1748391Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1848391Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1948391Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2048391Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2148391Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2248391Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2348391Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2448391Speter * SUCH DAMAGE.
2548391Speter */
2648391Speter
27116182Sobrien#include <sys/cdefs.h>
28116182Sobrien__FBSDID("$FreeBSD$");
29116182Sobrien
3048391Speter#include <sys/param.h>
3148391Speter#include <sys/systm.h>
32230984Srstone#include <sys/cpuset.h>
3348391Speter#include <sys/kthread.h>
3470317Sjake#include <sys/lock.h>
3574927Sjhb#include <sys/mutex.h>
3674927Sjhb#include <sys/proc.h>
3755722Simp#include <sys/resourcevar.h>
38214238Sdavidxu#include <sys/rwlock.h>
3955539Sluoqi#include <sys/signalvar.h>
4074927Sjhb#include <sys/sx.h>
4148391Speter#include <sys/unistd.h>
4248391Speter#include <sys/wait.h>
43166188Sjeff#include <sys/sched.h>
44173004Sjulian#include <vm/vm.h>
45173004Sjulian#include <vm/vm_extern.h>
4648391Speter
4748391Speter#include <machine/stdarg.h>
4848391Speter
4948391Speter/*
5048391Speter * Start a kernel process.  This is called after a fork() call in
5148391Speter * mi_startup() in the file kern/init_main.c.
5248391Speter *
5348391Speter * This function is used to start "internal" daemons and intended
5448391Speter * to be called from SYSINIT().
5548391Speter */
5648391Spetervoid
5748391Speterkproc_start(udata)
5848391Speter	const void *udata;
5948391Speter{
6048391Speter	const struct kproc_desc	*kp = udata;
6148391Speter	int error;
6248391Speter
63172836Sjulian	error = kproc_create((void (*)(void *))kp->func, NULL,
64104354Sscottl		    kp->global_procpp, 0, 0, "%s", kp->arg0);
6548391Speter	if (error)
6648391Speter		panic("kproc_start: %s: error %d", kp->arg0, error);
6748391Speter}
6848391Speter
6948391Speter/*
7065557Sjasone * Create a kernel process/thread/whatever.  It shares its address space
7148391Speter * with proc0 - ie: kernel only.
7265557Sjasone *
7365557Sjasone * func is the function to start.
7465557Sjasone * arg is the parameter to pass to function on first startup.
7565557Sjasone * newpp is the return value pointing to the thread's struct proc.
7665557Sjasone * flags are flags to fork1 (in unistd.h)
7765557Sjasone * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.).
7848391Speter */
7948391Speterint
80172836Sjuliankproc_create(void (*func)(void *), void *arg,
81104354Sscottl    struct proc **newpp, int flags, int pages, const char *fmt, ...)
8248391Speter{
8348391Speter	int error;
8448391Speter	va_list ap;
85103216Sjulian	struct thread *td;
8648391Speter	struct proc *p2;
8748391Speter
88114434Sdes	if (!proc0.p_stats)
89172836Sjulian		panic("kproc_create called too soon");
9065557Sjasone
9190375Speter	error = fork1(&thread0, RFMEM | RFFDG | RFPROC | RFSTOPPED | flags,
92224987Sjonathan	    pages, &p2, NULL, 0);
9348391Speter	if (error)
9448391Speter		return error;
9548391Speter
9648391Speter	/* save a global descriptor, if desired */
9748391Speter	if (newpp != NULL)
9848391Speter		*newpp = p2;
9948391Speter
10048391Speter	/* this is a non-swapped system process */
10171559Sjhb	PROC_LOCK(p2);
102173004Sjulian	td = FIRST_THREAD_IN_PROC(p2);
10371559Sjhb	p2->p_flag |= P_SYSTEM | P_KTHREAD;
104173004Sjulian	td->td_pflags |= TDP_KTHREAD;
105114983Sjhb	mtx_lock(&p2->p_sigacts->ps_mtx);
106114983Sjhb	p2->p_sigacts->ps_flag |= PS_NOCLDWAIT;
107114983Sjhb	mtx_unlock(&p2->p_sigacts->ps_mtx);
10871559Sjhb	PROC_UNLOCK(p2);
10948391Speter
11048391Speter	/* set up arg0 for 'ps', et al */
11148391Speter	va_start(ap, fmt);
11248391Speter	vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap);
11348391Speter	va_end(ap);
114173004Sjulian	/* set up arg0 for 'ps', et al */
115173004Sjulian	va_start(ap, fmt);
116173004Sjulian	vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
117173004Sjulian	va_end(ap);
118232700Sjhb#ifdef KTR
119232700Sjhb	sched_clear_tdname(td);
120232700Sjhb#endif
12148391Speter
12248391Speter	/* call the processes' main()... */
123103216Sjulian	cpu_set_fork_handler(td, func, arg);
124230984Srstone
125230984Srstone	/* Avoid inheriting affinity from a random parent. */
126230984Srstone	cpuset_setthread(td->td_tid, cpuset_root);
127217079Sjhb	thread_lock(td);
128103216Sjulian	TD_SET_CAN_RUN(td);
129217079Sjhb	sched_prio(td, PVM);
130217079Sjhb	sched_user_prio(td, PUSER);
13148391Speter
13269657Sjhb	/* Delay putting it on the run queue until now. */
133217079Sjhb	if (!(flags & RFSTOPPED))
134166188Sjeff		sched_add(td, SRQ_BORING);
135217079Sjhb	thread_unlock(td);
13669657Sjhb
13748391Speter	return 0;
13848391Speter}
13948391Speter
14048391Spetervoid
141172836Sjuliankproc_exit(int ecode)
14248391Speter{
14386293Speter	struct thread *td;
14486293Speter	struct proc *p;
14570317Sjake
14686293Speter	td = curthread;
14786293Speter	p = td->td_proc;
148155400Sjhb
149155400Sjhb	/*
150155400Sjhb	 * Reparent curthread from proc0 to init so that the zombie
151155400Sjhb	 * is harvested.
152155400Sjhb	 */
15374927Sjhb	sx_xlock(&proctree_lock);
15486292Sdillon	PROC_LOCK(p);
15586292Sdillon	proc_reparent(p, initproc);
15686292Sdillon	PROC_UNLOCK(p);
15774927Sjhb	sx_xunlock(&proctree_lock);
158155400Sjhb
159155400Sjhb	/*
160155400Sjhb	 * Wakeup anyone waiting for us to exit.
161155400Sjhb	 */
162155400Sjhb	wakeup(p);
163155400Sjhb
164155400Sjhb	/* Buh-bye! */
16586293Speter	exit1(td, W_EXITCODE(ecode, 0));
16648391Speter}
16748391Speter
16855539Sluoqi/*
16955539Sluoqi * Advise a kernel process to suspend (or resume) in its main loop.
17055539Sluoqi * Participation is voluntary.
17155539Sluoqi */
17255539Sluoqiint
173172836Sjuliankproc_suspend(struct proc *p, int timo)
17455539Sluoqi{
17555539Sluoqi	/*
17655539Sluoqi	 * Make sure this is indeed a system process and we can safely
177104306Sjmallett	 * use the p_siglist field.
17855539Sluoqi	 */
17973911Sjhb	PROC_LOCK(p);
18073911Sjhb	if ((p->p_flag & P_KTHREAD) == 0) {
18173911Sjhb		PROC_UNLOCK(p);
18255539Sluoqi		return (EINVAL);
18373911Sjhb	}
184104306Sjmallett	SIGADDSET(p->p_siglist, SIGSTOP);
18588160Speter	wakeup(p);
186173004Sjulian	return msleep(&p->p_siglist, &p->p_mtx, PPAUSE | PDROP, "suspkp", timo);
18755539Sluoqi}
18855539Sluoqi
18955539Sluoqiint
190172836Sjuliankproc_resume(struct proc *p)
19155539Sluoqi{
19255539Sluoqi	/*
19355539Sluoqi	 * Make sure this is indeed a system process and we can safely
19455539Sluoqi	 * use the p_siglist field.
19555539Sluoqi	 */
19673911Sjhb	PROC_LOCK(p);
19773911Sjhb	if ((p->p_flag & P_KTHREAD) == 0) {
19873911Sjhb		PROC_UNLOCK(p);
19955539Sluoqi		return (EINVAL);
20073911Sjhb	}
201104306Sjmallett	SIGDELSET(p->p_siglist, SIGSTOP);
20273911Sjhb	PROC_UNLOCK(p);
203104306Sjmallett	wakeup(&p->p_siglist);
20455539Sluoqi	return (0);
20555539Sluoqi}
20655539Sluoqi
20755539Sluoqivoid
208172836Sjuliankproc_suspend_check(struct proc *p)
20955539Sluoqi{
21073911Sjhb	PROC_LOCK(p);
211104306Sjmallett	while (SIGISMEMBER(p->p_siglist, SIGSTOP)) {
212104306Sjmallett		wakeup(&p->p_siglist);
213173004Sjulian		msleep(&p->p_siglist, &p->p_mtx, PPAUSE, "kpsusp", 0);
21455539Sluoqi	}
21573911Sjhb	PROC_UNLOCK(p);
21655539Sluoqi}
217173004Sjulian
218173004Sjulian
219173004Sjulian/*
220173004Sjulian * Start a kernel thread.
221173004Sjulian *
222173004Sjulian * This function is used to start "internal" daemons and intended
223173004Sjulian * to be called from SYSINIT().
224173004Sjulian */
225173004Sjulian
226173004Sjulianvoid
227173004Sjuliankthread_start(udata)
228173004Sjulian	const void *udata;
229173004Sjulian{
230173004Sjulian	const struct kthread_desc	*kp = udata;
231173004Sjulian	int error;
232173004Sjulian
233173004Sjulian	error = kthread_add((void (*)(void *))kp->func, NULL,
234173004Sjulian		    NULL, kp->global_threadpp, 0, 0, "%s", kp->arg0);
235173004Sjulian	if (error)
236173004Sjulian		panic("kthread_start: %s: error %d", kp->arg0, error);
237173004Sjulian}
238173004Sjulian
239173004Sjulian/*
240173004Sjulian * Create a kernel thread.  It shares its address space
241173004Sjulian * with proc0 - ie: kernel only.
242173004Sjulian *
243173004Sjulian * func is the function to start.
244173004Sjulian * arg is the parameter to pass to function on first startup.
245173004Sjulian * newtdp is the return value pointing to the thread's struct thread.
246173004Sjulian *  ** XXX fix this --> flags are flags to fork1 (in unistd.h)
247173004Sjulian * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.).
248173004Sjulian */
249173004Sjulianint
250173004Sjuliankthread_add(void (*func)(void *), void *arg, struct proc *p,
251173004Sjulian    struct thread **newtdp, int flags, int pages, const char *fmt, ...)
252173004Sjulian{
253173004Sjulian	va_list ap;
254173004Sjulian	struct thread *newtd, *oldtd;
255173004Sjulian
256173004Sjulian	if (!proc0.p_stats)
257173004Sjulian		panic("kthread_add called too soon");
258173004Sjulian
259173052Sjulian	/* If no process supplied, put it on proc0 */
260254457Sbryanv	if (p == NULL)
261173004Sjulian		p = &proc0;
262173004Sjulian
263173052Sjulian	/* Initialize our new td  */
264196730Skib	newtd = thread_alloc(pages);
265173004Sjulian	if (newtd == NULL)
266173004Sjulian		return (ENOMEM);
267173004Sjulian
268254457Sbryanv	PROC_LOCK(p);
269254457Sbryanv	oldtd = FIRST_THREAD_IN_PROC(p);
270254457Sbryanv
271173004Sjulian	bzero(&newtd->td_startzero,
272173004Sjulian	    __rangeof(struct thread, td_startzero, td_endzero));
273173004Sjulian	bcopy(&oldtd->td_startcopy, &newtd->td_startcopy,
274173004Sjulian	    __rangeof(struct thread, td_startcopy, td_endcopy));
275173004Sjulian
276173004Sjulian	/* set up arg0 for 'ps', et al */
277173004Sjulian	va_start(ap, fmt);
278173004Sjulian	vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap);
279173004Sjulian	va_end(ap);
280173004Sjulian
281173004Sjulian	newtd->td_proc = p;  /* needed for cpu_set_upcall */
282173004Sjulian
283173004Sjulian	/* XXX optimise this probably? */
284173004Sjulian	/* On x86 (and probably the others too) it is way too full of junk */
285173004Sjulian	/* Needs a better name */
286173004Sjulian	cpu_set_upcall(newtd, oldtd);
287173004Sjulian	/* put the designated function(arg) as the resume context */
288173004Sjulian	cpu_set_fork_handler(newtd, func, arg);
289173004Sjulian
290173004Sjulian	newtd->td_pflags |= TDP_KTHREAD;
291173004Sjulian	newtd->td_ucred = crhold(p->p_ucred);
292173004Sjulian
293173004Sjulian	/* this code almost the same as create_thread() in kern_thr.c */
294173004Sjulian	p->p_flag |= P_HADTHREADS;
295173004Sjulian	thread_link(newtd, p);
296173004Sjulian	thread_lock(oldtd);
297173004Sjulian	/* let the scheduler know about these things. */
298173004Sjulian	sched_fork_thread(oldtd, newtd);
299173004Sjulian	TD_SET_CAN_RUN(newtd);
300173004Sjulian	thread_unlock(oldtd);
301173004Sjulian	PROC_UNLOCK(p);
302173004Sjulian
303213642Sdavidxu	tidhash_add(newtd);
304173004Sjulian
305230984Srstone	/* Avoid inheriting affinity from a random parent. */
306230984Srstone	cpuset_setthread(newtd->td_tid, cpuset_root);
307230984Srstone
308173004Sjulian	/* Delay putting it on the run queue until now. */
309173004Sjulian	if (!(flags & RFSTOPPED)) {
310173004Sjulian		thread_lock(newtd);
311173004Sjulian		sched_add(newtd, SRQ_BORING);
312173004Sjulian		thread_unlock(newtd);
313173004Sjulian	}
314173004Sjulian	if (newtdp)
315173004Sjulian		*newtdp = newtd;
316173004Sjulian	return 0;
317173004Sjulian}
318173004Sjulian
319173004Sjulianvoid
320173031Sjuliankthread_exit(void)
321173004Sjulian{
322173658Sjhb	struct proc *p;
323173658Sjhb
324204087Sattilio	p = curthread->td_proc;
325204087Sattilio
326173658Sjhb	/* A module may be waiting for us to exit. */
327173052Sjulian	wakeup(curthread);
328216921Sjhb
329216921Sjhb	/*
330216921Sjhb	 * The last exiting thread in a kernel process must tear down
331216921Sjhb	 * the whole process.
332216921Sjhb	 */
333214238Sdavidxu	rw_wlock(&tidhash_lock);
334204087Sattilio	PROC_LOCK(p);
335204089Sattilio	if (p->p_numthreads == 1) {
336204087Sattilio		PROC_UNLOCK(p);
337214238Sdavidxu		rw_wunlock(&tidhash_lock);
338204087Sattilio		kproc_exit(0);
339204087Sattilio	}
340214238Sdavidxu	LIST_REMOVE(curthread, td_hash);
341214238Sdavidxu	rw_wunlock(&tidhash_lock);
342173658Sjhb	PROC_SLOCK(p);
343173004Sjulian	thread_exit();
344173004Sjulian}
345173004Sjulian
346173004Sjulian/*
347173004Sjulian * Advise a kernel process to suspend (or resume) in its main loop.
348173004Sjulian * Participation is voluntary.
349173004Sjulian */
350173004Sjulianint
351173004Sjuliankthread_suspend(struct thread *td, int timo)
352173004Sjulian{
353202933Sattilio	struct proc *p;
354202933Sattilio
355202933Sattilio	p = td->td_proc;
356202933Sattilio
357202933Sattilio	/*
358204088Sattilio	 * td_pflags should not be read by any thread other than
359202933Sattilio	 * curthread, but as long as this flag is invariant during the
360204088Sattilio	 * thread's lifetime, it is OK to check its state.
361202933Sattilio	 */
362202933Sattilio	if ((td->td_pflags & TDP_KTHREAD) == 0)
363173004Sjulian		return (EINVAL);
364202933Sattilio
365202933Sattilio	/*
366202933Sattilio	 * The caller of the primitive should have already checked that the
367202933Sattilio	 * thread is up and running, thus not being blocked by other
368202933Sattilio	 * conditions.
369202933Sattilio	 */
370202933Sattilio	PROC_LOCK(p);
371173004Sjulian	thread_lock(td);
372173004Sjulian	td->td_flags |= TDF_KTH_SUSP;
373173004Sjulian	thread_unlock(td);
374202933Sattilio	return (msleep(&td->td_flags, &p->p_mtx, PPAUSE | PDROP, "suspkt",
375202933Sattilio	    timo));
376173004Sjulian}
377173004Sjulian
378173004Sjulian/*
379202933Sattilio * Resume a thread previously put asleep with kthread_suspend().
380173004Sjulian */
381173004Sjulianint
382173004Sjuliankthread_resume(struct thread *td)
383173004Sjulian{
384202933Sattilio	struct proc *p;
385202933Sattilio
386202933Sattilio	p = td->td_proc;
387202933Sattilio
388202933Sattilio	/*
389204088Sattilio	 * td_pflags should not be read by any thread other than
390202933Sattilio	 * curthread, but as long as this flag is invariant during the
391204088Sattilio	 * thread's lifetime, it is OK to check its state.
392202933Sattilio	 */
393202933Sattilio	if ((td->td_pflags & TDP_KTHREAD) == 0)
394173004Sjulian		return (EINVAL);
395202933Sattilio
396202933Sattilio	PROC_LOCK(p);
397173004Sjulian	thread_lock(td);
398173004Sjulian	td->td_flags &= ~TDF_KTH_SUSP;
399173004Sjulian	thread_unlock(td);
400202933Sattilio	wakeup(&td->td_flags);
401202933Sattilio	PROC_UNLOCK(p);
402173004Sjulian	return (0);
403173004Sjulian}
404173004Sjulian
405173004Sjulian/*
406173004Sjulian * Used by the thread to poll as to whether it should yield/sleep
407173004Sjulian * and notify the caller that is has happened.
408173004Sjulian */
409173004Sjulianvoid
410202933Sattiliokthread_suspend_check()
411173004Sjulian{
412202933Sattilio	struct proc *p;
413202933Sattilio	struct thread *td;
414202933Sattilio
415202933Sattilio	td = curthread;
416202933Sattilio	p = td->td_proc;
417202933Sattilio
418202933Sattilio	if ((td->td_pflags & TDP_KTHREAD) == 0)
419202933Sattilio		panic("%s: curthread is not a valid kthread", __func__);
420202933Sattilio
421202933Sattilio	/*
422202933Sattilio	 * As long as the double-lock protection is used when accessing the
423202933Sattilio	 * TDF_KTH_SUSP flag, synchronizing the read operation via proc mutex
424202933Sattilio	 * is fine.
425202933Sattilio	 */
426202933Sattilio	PROC_LOCK(p);
427173004Sjulian	while (td->td_flags & TDF_KTH_SUSP) {
428173004Sjulian		wakeup(&td->td_flags);
429202933Sattilio		msleep(&td->td_flags, &p->p_mtx, PPAUSE, "ktsusp", 0);
430173004Sjulian	}
431202933Sattilio	PROC_UNLOCK(p);
432173004Sjulian}
433173004Sjulian
434173004Sjulianint
435173004Sjuliankproc_kthread_add(void (*func)(void *), void *arg,
436173004Sjulian            struct proc **procptr, struct thread **tdptr,
437208390Sjhb            int flags, int pages, const char *procname, const char *fmt, ...)
438173004Sjulian{
439173004Sjulian	int error;
440173004Sjulian	va_list ap;
441173004Sjulian	char buf[100];
442173004Sjulian	struct thread *td;
443173004Sjulian
444173004Sjulian	if (*procptr == 0) {
445173004Sjulian		error = kproc_create(func, arg,
446173004Sjulian		    	procptr, flags, pages, "%s", procname);
447173004Sjulian		if (error)
448173004Sjulian			return (error);
449173004Sjulian		td = FIRST_THREAD_IN_PROC(*procptr);
450178682Sjulian		if (tdptr)
451178682Sjulian			*tdptr = td;
452173004Sjulian		va_start(ap, fmt);
453173004Sjulian		vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap);
454173004Sjulian		va_end(ap);
455232700Sjhb#ifdef KTR
456232700Sjhb		sched_clear_tdname(td);
457232700Sjhb#endif
458173004Sjulian		return (0);
459173004Sjulian	}
460173004Sjulian	va_start(ap, fmt);
461173004Sjulian	vsnprintf(buf, sizeof(buf), fmt, ap);
462173004Sjulian	va_end(ap);
463173004Sjulian	error = kthread_add(func, arg, *procptr,
464173004Sjulian		    tdptr, flags, pages, "%s", buf);
465173004Sjulian	return (error);
466173004Sjulian}
467