1 /*	$NetBSD: kern.h,v 1.3 2016/02/08 18:18:19 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _SYS_RUMP_PRIVATE_H_
29 #define _SYS_RUMP_PRIVATE_H_
30 
31 #include <sys/param.h>
32 #include <sys/cpu.h>
33 #include <sys/device.h>
34 #include <sys/lwp.h>
35 #include <sys/proc.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 
39 #include <uvm/uvm.h>
40 #include <uvm/uvm_object.h>
41 #include <uvm/uvm_page.h>
42 
43 #include <rump/rump.h>
44 #include <rump/rumpuser.h>
45 
46 #include <rump-sys/kern_if.h>
47 
48 extern struct rumpuser_mtx *rump_giantlock;
49 
50 extern int rump_threads;
51 extern struct device rump_rootdev;
52 
53 extern struct sysent rump_sysent[];
54 
55 enum rump_component_type {
56 	RUMP_COMPONENT_DEV,
57 		RUMP_COMPONENT_DEV_AFTERMAINBUS,
58 	RUMP_COMPONENT_NET,
59 		RUMP_COMPONENT_NET_ROUTE,
60 		RUMP_COMPONENT_NET_IF,
61 		RUMP_COMPONENT_NET_IFCFG,
62 	RUMP_COMPONENT_VFS,
63 	RUMP_COMPONENT_KERN,
64 		RUMP_COMPONENT_KERN_VFS,
65 	RUMP_COMPONENT_POSTINIT,
66 	RUMP_COMPONENT_SYSCALL,
67 
68 	RUMP__FACTION_DEV,
69 	RUMP__FACTION_VFS,
70 	RUMP__FACTION_NET,
71 
72 	RUMP_COMPONENT_MAX,
73 };
74 struct rump_component {
75 	enum rump_component_type rc_type;
76 	void (*rc_init)(void);
77 	LIST_ENTRY(rump_component) rc_entries;
78 };
79 
80 /*
81  * If RUMP_USE_CTOR is defined, we use __attribute__((constructor)) to
82  * determine which components are present when rump_init() is called.
83  * Otherwise, we use link sets and the __start/__stop symbols generated
84  * by the toolchain.
85  */
86 
87 #ifdef RUMP_USE_CTOR
88 #define _RUMP_COMPONENT_REGISTER(type)					\
89 static void rumpcomp_ctor##type(void) __attribute__((constructor));	\
90 static void rumpcomp_ctor##type(void)					\
91 {									\
92 	rump_component_load(&rumpcomp##type);				\
93 }									\
94 static void rumpcomp_dtor##type(void) __attribute__((destructor));	\
95 static void rumpcomp_dtor##type(void)					\
96 {									\
97 	rump_component_unload(&rumpcomp##type);				\
98 }
99 
100 #else /* RUMP_USE_CTOR */
101 
102 #define _RUMP_COMPONENT_REGISTER(type)					\
103 __link_set_add_rodata(rump_components, rumpcomp##type);
104 #endif /* RUMP_USE_CTOR */
105 
106 #define RUMP_COMPONENT(type)				\
107 static void rumpcompinit##type(void);			\
108 static struct rump_component rumpcomp##type = {	\
109 	.rc_type = type,				\
110 	.rc_init = rumpcompinit##type,			\
111 };							\
112 _RUMP_COMPONENT_REGISTER(type)				\
113 static void rumpcompinit##type(void)
114 
115 #define FLAWLESSCALL(call)						\
116 do {									\
117 	int att_error;							\
118 	if ((att_error = call) != 0)					\
119 		panic("\"%s\" failed", #call);				\
120 } while (/*CONSTCOND*/0)
121 
122 #define RUMPMEM_UNLIMITED ((unsigned long)-1)
123 extern unsigned long rump_physmemlimit;
124 
125 extern struct vmspace *rump_vmspace_local;
126 extern struct pmap rump_pmap_local;
127 #define RUMP_LOCALPROC_P(p) \
128     (p->p_vmspace == vmspace_kernel() || p->p_vmspace == rump_vmspace_local)
129 
130 /* vm bundle for remote clients.  the last member is the hypercall cookie */
131 struct rump_spctl {
132 	struct vmspace spctl_vm;
133 	void *spctl;
134 };
135 #define RUMP_SPVM2CTL(vm) (((struct rump_spctl *)vm)->spctl)
136 
137 void		rump_component_load(const struct rump_component *);
138 void		rump_component_unload(struct rump_component *);
139 void		rump_component_init(enum rump_component_type);
140 int		rump_component_count(enum rump_component_type);
141 
142 typedef void	(*rump_proc_vfs_init_fn)(struct proc *);
143 typedef void	(*rump_proc_vfs_release_fn)(struct proc *);
144 extern rump_proc_vfs_init_fn rump_proc_vfs_init;
145 extern rump_proc_vfs_release_fn rump_proc_vfs_release;
146 
147 extern struct cpu_info rump_bootcpu;
148 
149 extern bool rump_ttycomponent;
150 
151 struct lwp *	rump__lwproc_alloclwp(struct proc *);
152 void		rump__lwproc_lwphold(void);
153 void		rump__lwproc_lwprele(void);
154 
155 void	rump_cpus_bootstrap(int *);
156 void	rump_biglock_init(void);
157 void	rump_scheduler_init(int);
158 void	rump_schedule(void);
159 void	rump_unschedule(void);
160 void 	rump_schedule_cpu(struct lwp *);
161 void 	rump_schedule_cpu_interlock(struct lwp *, void *);
162 void	rump_unschedule_cpu(struct lwp *);
163 void	rump_unschedule_cpu_interlock(struct lwp *, void *);
164 void	rump_unschedule_cpu1(struct lwp *, void *);
165 int	rump_syscall(int, void *, size_t, register_t *);
166 
167 struct rump_onesyscall {
168 	int ros_num;
169 	sy_call_t *ros_handler;
170 };
171 void	rump_syscall_boot_establish(const struct rump_onesyscall *, size_t);
172 
173 void	rump_schedlock_cv_wait(struct rumpuser_cv *);
174 int	rump_schedlock_cv_timedwait(struct rumpuser_cv *,
175 				    const struct timespec *);
176 
177 void	rump_user_schedule(int, void *);
178 void	rump_user_unschedule(int, int *, void *);
179 
180 void	rump_cpu_attach(struct cpu_info *);
181 
182 struct clockframe *rump_cpu_makeclockframe(void);
183 
184 void	rump_kernel_bigwrap(int *);
185 void	rump_kernel_bigunwrap(int);
186 
187 void	rump_tsleep_init(void);
188 
189 void	rump_intr_init(int);
190 void	rump_softint_run(struct cpu_info *);
191 
192 void	*rump_hypermalloc(size_t, int, bool, const char *);
193 void	rump_hyperfree(void *, size_t);
194 
195 void	rump_xc_highpri(struct cpu_info *);
196 
197 void	rump_thread_init(void);
198 void	rump_thread_allow(struct lwp *);
199 
200 void	rump_consdev_init(void);
201 
202 void	rump_hyperentropy_init(void);
203 
204 void	rump_lwproc_init(void);
205 void	rump_lwproc_curlwp_set(struct lwp *);
206 void	rump_lwproc_curlwp_clear(struct lwp *);
207 int	rump_lwproc_rfork_vmspace(struct vmspace *, int);
208 
209 /*
210  * sysproxy is an optional component.  The interfaces with "hyp"
211  * in the name come into the rump kernel from the client or sysproxy
212  * stub, the rest go out of the rump kernel.
213  */
214 struct rump_sysproxy_ops {
215 	int (*rspo_copyin)(void *, const void *, void *, size_t);
216 	int (*rspo_copyinstr)(void *, const void *, void *, size_t *);
217 	int (*rspo_copyout)(void *, const void *, void *, size_t);
218 	int (*rspo_copyoutstr)(void *, const void *, void *, size_t *);
219 	int (*rspo_anonmmap)(void *, size_t, void **);
220 	int (*rspo_raise)(void *, int);
221 	void (*rspo_fini)(void *);
222 
223 	pid_t (*rspo_hyp_getpid)(void);
224 	int (*rspo_hyp_syscall)(int, void *, long *);
225 	int (*rspo_hyp_rfork)(void *, int, const char *);
226 	void (*rspo_hyp_lwpexit)(void);
227 	void (*rspo_hyp_execnotify)(const char *);
228 };
229 extern struct rump_sysproxy_ops rump_sysproxy_ops;
230 #define rump_sysproxy_copyin(arg, raddr, laddr, len)			\
231 	rump_sysproxy_ops.rspo_copyin(arg, raddr, laddr, len)
232 #define rump_sysproxy_copyinstr(arg, raddr, laddr, lenp)		\
233 	rump_sysproxy_ops.rspo_copyinstr(arg, raddr, laddr, lenp)
234 #define rump_sysproxy_copyout(arg, laddr, raddr, len)			\
235 	rump_sysproxy_ops.rspo_copyout(arg, laddr, raddr, len)
236 #define rump_sysproxy_copyoutstr(arg, laddr, raddr, lenp)		\
237 	rump_sysproxy_ops.rspo_copyoutstr(arg, laddr, raddr, lenp)
238 #define rump_sysproxy_anonmmap(arg, howmuch, addr)			\
239 	rump_sysproxy_ops.rspo_anonmmap(arg, howmuch, addr)
240 #define rump_sysproxy_raise(arg, signo)					\
241 	rump_sysproxy_ops.rspo_raise(arg, signo)
242 #define rump_sysproxy_fini(arg)						\
243 	rump_sysproxy_ops.rspo_fini(arg)
244 #define rump_sysproxy_hyp_getpid()					\
245 	rump_sysproxy_ops.rspo_hyp_getpid()
246 #define rump_sysproxy_hyp_syscall(num, arg, retval)			\
247 	rump_sysproxy_ops.rspo_hyp_syscall(num, arg, retval)
248 #define rump_sysproxy_hyp_rfork(priv, flag, comm)			\
249 	rump_sysproxy_ops.rspo_hyp_rfork(priv, flag, comm)
250 #define rump_sysproxy_hyp_lwpexit()					\
251 	rump_sysproxy_ops.rspo_hyp_lwpexit()
252 #define rump_sysproxy_hyp_execnotify(comm)				\
253 	rump_sysproxy_ops.rspo_hyp_execnotify(comm)
254 
255 #endif /* _SYS_RUMP_PRIVATE_H_ */
256