xref: /netbsd-src/sys/rump/librump/rumpkern/emul.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: emul.c,v 1.160 2013/12/16 15:36:29 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 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.160 2013/12/16 15:36:29 pooka Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/null.h>
33 #include <sys/vnode.h>
34 #include <sys/stat.h>
35 #include <sys/select.h>
36 #include <sys/syslog.h>
37 #include <sys/namei.h>
38 #include <sys/kauth.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41 #include <sys/queue.h>
42 #include <sys/file.h>
43 #include <sys/cpu.h>
44 #include <sys/kmem.h>
45 #include <sys/poll.h>
46 #include <sys/timetc.h>
47 #include <sys/tprintf.h>
48 #include <sys/module.h>
49 #include <sys/tty.h>
50 #include <sys/reboot.h>
51 #include <sys/syscall.h>
52 #include <sys/syscallvar.h>
53 #include <sys/xcall.h>
54 #include <sys/sleepq.h>
55 
56 #include <dev/cons.h>
57 
58 #include <rump/rumpuser.h>
59 
60 #include <uvm/uvm_map.h>
61 
62 #include "rump_private.h"
63 
64 /*
65  * physmem is largely unused (except for nmbcluster calculations),
66  * so pick a default value which suits ZFS.  if an application wants
67  * a very small memory footprint, it can still adjust this before
68  * calling rump_init()
69  */
70 #define PHYSMEM 512*256
71 int physmem = PHYSMEM;
72 int nkmempages = PHYSMEM/2; /* from le chapeau */
73 #undef PHYSMEM
74 
75 struct lwp lwp0;
76 struct vnode *rootvp;
77 dev_t rootdev = NODEV;
78 
79 const int schedppq = 1;
80 int hardclock_ticks;
81 bool mp_online = false;
82 struct timeval boottime;
83 int cold = 1;
84 int boothowto = AB_SILENT;
85 struct tty *constty;
86 
87 const struct bdevsw *bdevsw0[255];
88 const struct bdevsw **bdevsw = bdevsw0;
89 const int sys_cdevsws = 255;
90 int max_cdevsws = 255;
91 
92 const struct cdevsw *cdevsw0[255];
93 const struct cdevsw **cdevsw = cdevsw0;
94 const int sys_bdevsws = 255;
95 int max_bdevsws = 255;
96 
97 int mem_no = 2;
98 
99 device_t booted_device;
100 device_t booted_wedge;
101 int booted_partition;
102 
103 /* XXX: unused */
104 kmutex_t tty_lock;
105 krwlock_t exec_lock;
106 
107 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
108 
109 /* sparc doesn't sport constant page size, pretend we have 4k pages */
110 #ifdef __sparc__
111 int nbpg = 4096;
112 int pgofset = 4096-1;
113 int pgshift = 12;
114 #endif
115 
116 /* on sun3 VM_MAX_ADDRESS is a const variable */
117 /* XXX: should be moved into rump.c and initialize for sun3 and sun3x? */
118 #ifdef sun3
119 const vaddr_t kernbase = KERNBASE3;
120 #endif
121 
122 struct loadavg averunnable = {
123 	{ 0 * FSCALE,
124 	  1 * FSCALE,
125 	  11 * FSCALE, },
126 	FSCALE,
127 };
128 
129 struct emul emul_netbsd = {
130 	.e_name = "netbsd-rump",
131 	.e_sysent = rump_sysent,
132 #ifndef __HAVE_MINIMAL_EMUL
133 	.e_nsysent = SYS_NSYSENT,
134 #endif
135 	.e_vm_default_addr = uvm_default_mapaddr,
136 #ifdef __HAVE_SYSCALL_INTERN
137 	.e_syscall_intern = syscall_intern,
138 #endif
139 };
140 
141 u_int nprocs = 1;
142 
143 int
144 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
145 {
146 	extern int hz;
147 	int rv;
148 	uint64_t sec, nsec;
149 
150 	if (mtx)
151 		mutex_exit(mtx);
152 
153 	sec = timeo / hz;
154 	nsec = (timeo % hz) * (1000000000 / hz);
155 	rv = rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, sec, nsec);
156 	KASSERT(rv == 0);
157 
158 	if (mtx)
159 		mutex_enter(mtx);
160 
161 	return 0;
162 }
163 
164 void
165 lwp_unsleep(lwp_t *l, bool cleanup)
166 {
167 
168 	KASSERT(mutex_owned(l->l_mutex));
169 
170 	(*l->l_syncobj->sobj_unsleep)(l, cleanup);
171 }
172 
173 void
174 lwp_update_creds(struct lwp *l)
175 {
176 	struct proc *p;
177 	kauth_cred_t oldcred;
178 
179 	p = l->l_proc;
180 	oldcred = l->l_cred;
181 	l->l_prflag &= ~LPR_CRMOD;
182 
183 	mutex_enter(p->p_lock);
184 	kauth_cred_hold(p->p_cred);
185 	l->l_cred = p->p_cred;
186 	mutex_exit(p->p_lock);
187 
188 	if (oldcred != NULL)
189 		kauth_cred_free(oldcred);
190 }
191 
192 vaddr_t
193 calc_cache_size(vsize_t vasz, int pct, int va_pct)
194 {
195 	paddr_t t;
196 
197 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
198 	if ((vaddr_t)t != t) {
199 		panic("%s: needs tweak", __func__);
200 	}
201 	return t;
202 }
203 
204 void
205 assert_sleepable(void)
206 {
207 
208 	/* always sleepable, although we should improve this */
209 }
210 
211 void
212 module_init_md(void)
213 {
214 
215 	/*
216 	 * Nothing for now.  However, we should load the librump
217 	 * symbol table.
218 	 */
219 }
220 
221 /* us and them, after all we're only ordinary seconds */
222 static void
223 rump_delay(unsigned int us)
224 {
225 	uint64_t sec, nsec;
226 
227 	sec = us / 1000000;
228 	nsec = (us % 1000000) * 1000;
229 
230 	if (__predict_false(sec != 0))
231 		printf("WARNING: over 1s delay\n");
232 
233 	rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, sec, nsec);
234 }
235 void (*delay_func)(unsigned int) = rump_delay;
236 
237 /*
238  * Provide weak aliases for tty routines used by printf.
239  * They will be used unless the rumpkern_tty component is present.
240  */
241 
242 int rump_ttycheckoutq(struct tty *, int);
243 int
244 rump_ttycheckoutq(struct tty *tp, int wait)
245 {
246 
247 	return 1;
248 }
249 __weak_alias(ttycheckoutq,rump_ttycheckoutq);
250 
251 int rump_tputchar(int, int, struct tty *);
252 int
253 rump_tputchar(int c, int flags, struct tty *tp)
254 {
255 
256 	cnputc(c);
257 	return 0;
258 }
259 __weak_alias(tputchar,rump_tputchar);
260 
261 void
262 cnputc(int c)
263 {
264 
265 	rumpuser_putchar(c);
266 }
267 
268 void
269 cnflush(void)
270 {
271 
272 	/* done */
273 }
274 
275 #ifdef __HAVE_SYSCALL_INTERN
276 void
277 syscall_intern(struct proc *p)
278 {
279 
280 	p->p_emuldata = NULL;
281 }
282 #endif
283 
284 #ifdef LOCKDEBUG
285 void
286 turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
287 {
288 
289 	/* nada */
290 }
291 #endif
292