1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3eda14cbcSMatt Macy  * All rights reserved.
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * Redistribution and use in source and binary forms, with or without
6eda14cbcSMatt Macy  * modification, are permitted provided that the following conditions
7eda14cbcSMatt Macy  * are met:
8eda14cbcSMatt Macy  * 1. Redistributions of source code must retain the above copyright
9eda14cbcSMatt Macy  *    notice, this list of conditions and the following disclaimer.
10eda14cbcSMatt Macy  * 2. Redistributions in binary form must reproduce the above copyright
11eda14cbcSMatt Macy  *    notice, this list of conditions and the following disclaimer in the
12eda14cbcSMatt Macy  *    documentation and/or other materials provided with the distribution.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15eda14cbcSMatt Macy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16eda14cbcSMatt Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17eda14cbcSMatt Macy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18eda14cbcSMatt Macy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19eda14cbcSMatt Macy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20eda14cbcSMatt Macy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21eda14cbcSMatt Macy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22eda14cbcSMatt Macy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23eda14cbcSMatt Macy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24eda14cbcSMatt Macy  * SUCH DAMAGE.
25eda14cbcSMatt Macy  *
26eda14cbcSMatt Macy  * $FreeBSD$
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #ifndef _OPENSOLARIS_SYS_PROC_H_
30eda14cbcSMatt Macy #define	_OPENSOLARIS_SYS_PROC_H_
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy #include <sys/param.h>
33eda14cbcSMatt Macy #include <sys/kthread.h>
34eda14cbcSMatt Macy #include_next <sys/proc.h>
35eda14cbcSMatt Macy #include <sys/stdint.h>
36eda14cbcSMatt Macy #include <sys/smp.h>
37eda14cbcSMatt Macy #include <sys/sched.h>
38eda14cbcSMatt Macy #include <sys/lock.h>
39eda14cbcSMatt Macy #include <sys/mutex.h>
40eda14cbcSMatt Macy #include <sys/unistd.h>
41eda14cbcSMatt Macy #include <sys/kmem.h>
42eda14cbcSMatt Macy #include <sys/malloc.h>
43eda14cbcSMatt Macy 
442fec3ae8SWarner Losh #ifdef _KERNEL
45eda14cbcSMatt Macy #define	CPU		curcpu
46eda14cbcSMatt Macy #define	minclsyspri	PRIBIO
47eda14cbcSMatt Macy #define	defclsyspri minclsyspri
48eda14cbcSMatt Macy #define	maxclsyspri	PVM
49eda14cbcSMatt Macy #define	max_ncpus	(mp_maxid + 1)
50eda14cbcSMatt Macy #define	boot_max_ncpus	(mp_maxid + 1)
51eda14cbcSMatt Macy 
52eda14cbcSMatt Macy #define	TS_RUN	0
53eda14cbcSMatt Macy 
54eda14cbcSMatt Macy #define	p0	proc0
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy #define	t_tid	td_tid
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy typedef	short		pri_t;
59eda14cbcSMatt Macy typedef	struct thread	_kthread;
60eda14cbcSMatt Macy typedef	struct thread	kthread_t;
61eda14cbcSMatt Macy typedef struct thread	*kthread_id_t;
62eda14cbcSMatt Macy typedef struct proc	proc_t;
63eda14cbcSMatt Macy 
64eda14cbcSMatt Macy extern proc_t *system_proc;
65eda14cbcSMatt Macy 
66eda14cbcSMatt Macy static __inline kthread_t *
do_thread_create(caddr_t stk,size_t stksize,void (* proc)(void *),void * arg,size_t len,proc_t * pp,int state,pri_t pri,const char * name)67eda14cbcSMatt Macy do_thread_create(caddr_t stk, size_t stksize, void (*proc)(void *), void *arg,
68eda14cbcSMatt Macy     size_t len, proc_t *pp, int state, pri_t pri, const char *name)
69eda14cbcSMatt Macy {
70eda14cbcSMatt Macy 	kthread_t *td = NULL;
71eda14cbcSMatt Macy 	proc_t **ppp;
72eda14cbcSMatt Macy 	int error;
73eda14cbcSMatt Macy 
74eda14cbcSMatt Macy 	/*
75eda14cbcSMatt Macy 	 * Be sure there are no surprises.
76eda14cbcSMatt Macy 	 */
77eda14cbcSMatt Macy 	ASSERT(stk == NULL);
78eda14cbcSMatt Macy 	ASSERT(len == 0);
79eda14cbcSMatt Macy 	ASSERT(state == TS_RUN);
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy 	if (pp == &p0)
82eda14cbcSMatt Macy 		ppp = &system_proc;
83eda14cbcSMatt Macy 	else
84eda14cbcSMatt Macy 		ppp = &pp;
85eda14cbcSMatt Macy 	error = kproc_kthread_add(proc, arg, ppp, &td, RFSTOPPED,
86eda14cbcSMatt Macy 	    stksize / PAGE_SIZE, "zfskern", "%s", name);
87eda14cbcSMatt Macy 	if (error == 0) {
88eda14cbcSMatt Macy 		thread_lock(td);
89eda14cbcSMatt Macy 		sched_prio(td, pri);
90eda14cbcSMatt Macy 		sched_add(td, SRQ_BORING);
91eda14cbcSMatt Macy #if __FreeBSD_version < 1300068
92eda14cbcSMatt Macy 		thread_unlock(td);
93eda14cbcSMatt Macy #endif
94eda14cbcSMatt Macy 	}
95eda14cbcSMatt Macy 	return (td);
96eda14cbcSMatt Macy }
97eda14cbcSMatt Macy 
98eda14cbcSMatt Macy #define	thread_create_named(name, stk, stksize, proc, arg, len,	\
99eda14cbcSMatt Macy     pp, state, pri) \
100eda14cbcSMatt Macy 	do_thread_create(stk, stksize, proc, arg, len, pp, state, pri, name)
101eda14cbcSMatt Macy #define	thread_create(stk, stksize, proc, arg, len, pp, state, pri) \
102eda14cbcSMatt Macy 	do_thread_create(stk, stksize, proc, arg, len, pp, state, pri, #proc)
103eda14cbcSMatt Macy #define	thread_exit()	kthread_exit()
104eda14cbcSMatt Macy 
105eda14cbcSMatt Macy int	uread(proc_t *, void *, size_t, uintptr_t);
106eda14cbcSMatt Macy int	uwrite(proc_t *, void *, size_t, uintptr_t);
107eda14cbcSMatt Macy 
108eda14cbcSMatt Macy static inline boolean_t
zfs_proc_is_caller(proc_t * p)109eda14cbcSMatt Macy zfs_proc_is_caller(proc_t *p)
110eda14cbcSMatt Macy {
111eda14cbcSMatt Macy 	return (p == curproc);
112eda14cbcSMatt Macy }
113eda14cbcSMatt Macy 
1142fec3ae8SWarner Losh #endif	/* _KERNEL */
115eda14cbcSMatt Macy #endif	/* _OPENSOLARIS_SYS_PROC_H_ */
116