xref: /netbsd-src/sys/rump/librump/rumpkern/emul.c (revision f2af7cc18768e2ca30fbc8508a6ee20d421e72de)
1 /*	$NetBSD: emul.c,v 1.145 2010/09/01 19:37:58 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
5  *
6  * Development of this software was supported by Google Summer of Code.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.145 2010/09/01 19:37:58 pooka Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/null.h>
35 #include <sys/vnode.h>
36 #include <sys/stat.h>
37 #include <sys/select.h>
38 #include <sys/syslog.h>
39 #include <sys/namei.h>
40 #include <sys/kauth.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/queue.h>
44 #include <sys/file.h>
45 #include <sys/cpu.h>
46 #include <sys/kmem.h>
47 #include <sys/poll.h>
48 #include <sys/timetc.h>
49 #include <sys/tprintf.h>
50 #include <sys/module.h>
51 #include <sys/tty.h>
52 #include <sys/reboot.h>
53 #include <sys/syscallvar.h>
54 #include <sys/xcall.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 struct lwp lwp0;
65 struct vnode *rootvp;
66 dev_t rootdev = NODEV;
67 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
68 int nkmempages = 256*256/2; /* from le chapeau */
69 const int schedppq = 1;
70 int hardclock_ticks;
71 bool mp_online = false;
72 struct timeval boottime;
73 int cold = 1;
74 int boothowto = AB_SILENT;
75 struct tty *constty;
76 
77 const struct bdevsw *bdevsw0[255];
78 const struct bdevsw **bdevsw = bdevsw0;
79 const int sys_cdevsws = 255;
80 int max_cdevsws = 255;
81 
82 const struct cdevsw *cdevsw0[255];
83 const struct cdevsw **cdevsw = cdevsw0;
84 const int sys_bdevsws = 255;
85 int max_bdevsws = 255;
86 
87 int mem_no = 2;
88 
89 struct device *booted_device;
90 struct device *booted_wedge;
91 int booted_partition;
92 
93 /* XXX: unused */
94 kmutex_t tty_lock;
95 krwlock_t exec_lock;
96 
97 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
98 
99 /* sparc doesn't sport constant page size, pretend we have 4k pages */
100 #ifdef __sparc__
101 int nbpg = 4096;
102 int pgofset = 4096-1;
103 int pgshift = 12;
104 #endif
105 
106 /* sun3 is sun3 with broken kernel modules */
107 #ifdef sun3
108 char KERNBASE[1]; /* this is completely random ... */
109 #endif
110 
111 struct loadavg averunnable = {
112 	{ 0 * FSCALE,
113 	  1 * FSCALE,
114 	  11 * FSCALE, },
115 	FSCALE,
116 };
117 
118 struct emul emul_netbsd = {
119 	.e_name = "netbsd-rump",
120 	.e_sysent = rump_sysent,
121 	.e_vm_default_addr = uvm_default_mapaddr,
122 #ifdef __HAVE_SYSCALL_INTERN
123 	.e_syscall_intern = syscall_intern,
124 #endif
125 };
126 
127 u_int nprocs = 1;
128 
129 int
130 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
131 {
132 	extern int hz;
133 	int rv, error;
134 	uint64_t sec, nsec;
135 
136 	if (mtx)
137 		mutex_exit(mtx);
138 
139 	sec = timeo / hz;
140 	nsec = (timeo % hz) * (1000000000 / hz);
141 	rv = rumpuser_nanosleep(&sec, &nsec, &error);
142 
143 	if (mtx)
144 		mutex_enter(mtx);
145 
146 	if (rv)
147 		return error;
148 
149 	return 0;
150 }
151 
152 void
153 lwp_unsleep(lwp_t *l, bool cleanup)
154 {
155 
156 	KASSERT(mutex_owned(l->l_mutex));
157 
158 	(*l->l_syncobj->sobj_unsleep)(l, cleanup);
159 }
160 
161 void
162 lwp_update_creds(struct lwp *l)
163 {
164 	struct proc *p;
165 	kauth_cred_t oldcred;
166 
167 	p = l->l_proc;
168 	oldcred = l->l_cred;
169 	l->l_prflag &= ~LPR_CRMOD;
170 
171 	mutex_enter(p->p_lock);
172 	kauth_cred_hold(p->p_cred);
173 	l->l_cred = p->p_cred;
174 	mutex_exit(p->p_lock);
175 
176 	if (oldcred != NULL)
177 		kauth_cred_free(oldcred);
178 }
179 
180 vaddr_t
181 calc_cache_size(struct vm_map *map, int pct, int va_pct)
182 {
183 	paddr_t t;
184 
185 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
186 	if ((vaddr_t)t != t) {
187 		panic("%s: needs tweak", __func__);
188 	}
189 	return t;
190 }
191 
192 void
193 assert_sleepable(void)
194 {
195 
196 	/* always sleepable, although we should improve this */
197 }
198 
199 void
200 module_init_md(void)
201 {
202 
203 	/*
204 	 * Nothing for now.  However, we should load the librump
205 	 * symbol table.
206 	 */
207 }
208 
209 /* us and them, after all we're only ordinary seconds */
210 static void
211 rump_delay(unsigned int us)
212 {
213 	uint64_t sec, nsec;
214 	int error;
215 
216 	sec = us / 1000000;
217 	nsec = (us % 1000000) * 1000;
218 
219 	if (__predict_false(sec != 0))
220 		printf("WARNING: over 1s delay\n");
221 
222 	rumpuser_nanosleep(&sec, &nsec, &error);
223 }
224 void (*delay_func)(unsigned int) = rump_delay;
225 
226 /*
227  * Provide weak aliases for tty routines used by printf.
228  * They will be used unless the rumpkern_tty component is present.
229  */
230 
231 int rump_ttycheckoutq(struct tty *, int);
232 int
233 rump_ttycheckoutq(struct tty *tp, int wait)
234 {
235 
236 	return 1;
237 }
238 __weak_alias(ttycheckoutq,rump_ttycheckoutq);
239 
240 int rump_tputchar(int, int, struct tty *);
241 int
242 rump_tputchar(int c, int flags, struct tty *tp)
243 {
244 
245 	cnputc(c);
246 	return 0;
247 }
248 __weak_alias(tputchar,rump_tputchar);
249 
250 void
251 cnputc(int c)
252 {
253 	int error;
254 
255 	rumpuser_putchar(c, &error);
256 }
257 
258 void
259 cnflush(void)
260 {
261 
262 	/* done */
263 }
264 
265 void
266 cpu_reboot(int howto, char *bootstr)
267 {
268 
269 	rump_reboot(howto);
270 
271 	/* this function is __dead, we must exit */
272 	rumpuser_exit(0);
273 }
274 
275 #ifdef __HAVE_SYSCALL_INTERN
276 void
277 syscall_intern(struct proc *p)
278 {
279 
280 	/* no you don't */
281 }
282 #endif
283 
284 void
285 xc_send_ipi(struct cpu_info *ci)
286 {
287 
288 	/* I'll think about the implementation if this is ever used */
289 	panic("not implemented");
290 }
291