xref: /netbsd-src/sys/rump/librump/rumpkern/emul.c (revision 1423e65b26cfa3a30fb7aaea6d57132588c43746)
1 /*	$NetBSD: emul.c,v 1.144 2010/06/23 08:36:03 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.144 2010/06/23 08:36:03 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 vaddr_t
162 calc_cache_size(struct vm_map *map, int pct, int va_pct)
163 {
164 	paddr_t t;
165 
166 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
167 	if ((vaddr_t)t != t) {
168 		panic("%s: needs tweak", __func__);
169 	}
170 	return t;
171 }
172 
173 void
174 assert_sleepable(void)
175 {
176 
177 	/* always sleepable, although we should improve this */
178 }
179 
180 void
181 module_init_md(void)
182 {
183 
184 	/*
185 	 * Nothing for now.  However, we should load the librump
186 	 * symbol table.
187 	 */
188 }
189 
190 /* us and them, after all we're only ordinary seconds */
191 static void
192 rump_delay(unsigned int us)
193 {
194 	uint64_t sec, nsec;
195 	int error;
196 
197 	sec = us / 1000000;
198 	nsec = (us % 1000000) * 1000;
199 
200 	if (__predict_false(sec != 0))
201 		printf("WARNING: over 1s delay\n");
202 
203 	rumpuser_nanosleep(&sec, &nsec, &error);
204 }
205 void (*delay_func)(unsigned int) = rump_delay;
206 
207 /*
208  * Provide weak aliases for tty routines used by printf.
209  * They will be used unless the rumpkern_tty component is present.
210  */
211 
212 int rump_ttycheckoutq(struct tty *, int);
213 int
214 rump_ttycheckoutq(struct tty *tp, int wait)
215 {
216 
217 	return 1;
218 }
219 __weak_alias(ttycheckoutq,rump_ttycheckoutq);
220 
221 int rump_tputchar(int, int, struct tty *);
222 int
223 rump_tputchar(int c, int flags, struct tty *tp)
224 {
225 
226 	cnputc(c);
227 	return 0;
228 }
229 __weak_alias(tputchar,rump_tputchar);
230 
231 void
232 cnputc(int c)
233 {
234 	int error;
235 
236 	rumpuser_putchar(c, &error);
237 }
238 
239 void
240 cnflush(void)
241 {
242 
243 	/* done */
244 }
245 
246 void
247 cpu_reboot(int howto, char *bootstr)
248 {
249 
250 	rump_reboot(howto);
251 
252 	/* this function is __dead, we must exit */
253 	rumpuser_exit(0);
254 }
255 
256 #ifdef __HAVE_SYSCALL_INTERN
257 void
258 syscall_intern(struct proc *p)
259 {
260 
261 	/* no you don't */
262 }
263 #endif
264 
265 void
266 xc_send_ipi(struct cpu_info *ci)
267 {
268 
269 	/* I'll think about the implementation if this is ever used */
270 	panic("not implemented");
271 }
272