Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.121.2.1
      1 /*	$NetBSD: emul.c,v 1.121.2.1 2010/04/30 14:44:29 uebayasi 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.121.2.1 2010/04/30 14:44:29 uebayasi 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/filedesc.h>
     46 #include <sys/cpu.h>
     47 #include <sys/kmem.h>
     48 #include <sys/poll.h>
     49 #include <sys/timetc.h>
     50 #include <sys/tprintf.h>
     51 #include <sys/module.h>
     52 #include <sys/tty.h>
     53 #include <sys/reboot.h>
     54 #include <sys/syscallvar.h>
     55 #include <sys/xcall.h>
     56 
     57 #include <dev/cons.h>
     58 
     59 #include <rump/rumpuser.h>
     60 
     61 #include <uvm/uvm_map.h>
     62 
     63 #include "rump_private.h"
     64 
     65 kmutex_t *proc_lock;
     66 struct lwp lwp0;
     67 struct vnode *rootvp;
     68 dev_t rootdev = NODEV;
     69 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
     70 const int schedppq = 1;
     71 int hardclock_ticks;
     72 bool mp_online = false;
     73 struct timeval boottime;
     74 int cold = 1;
     75 int boothowto = AB_SILENT;
     76 struct tty *constty;
     77 
     78 const struct bdevsw *bdevsw0[1];
     79 const struct bdevsw **bdevsw = bdevsw0;
     80 const int sys_cdevsws = 0;
     81 int max_cdevsws = 1;
     82 
     83 const struct cdevsw *cdevsw0[1];
     84 const struct cdevsw **cdevsw = cdevsw0;
     85 const int sys_bdevsws = 0;
     86 int max_bdevsws = 1;
     87 
     88 struct devsw_conv devsw_conv0;
     89 struct devsw_conv *devsw_conv = &devsw_conv0;
     90 int max_devsw_convs = 0;
     91 int mem_no = 2;
     92 
     93 struct device *booted_device;
     94 struct device *booted_wedge;
     95 int booted_partition;
     96 
     97 /* XXX: unused */
     98 kmutex_t tty_lock;
     99 krwlock_t exec_lock;
    100 
    101 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
    102 
    103 /* sparc doesn't sport constant page size */
    104 #ifdef __sparc__
    105 int nbpg = 4096;
    106 #endif
    107 
    108 struct loadavg averunnable = {
    109 	{ 0 * FSCALE,
    110 	  1 * FSCALE,
    111 	  11 * FSCALE, },
    112 	FSCALE,
    113 };
    114 
    115 struct emul emul_netbsd = {
    116 	.e_name = "netbsd-rump",
    117 	.e_sysent = rump_sysent,
    118 	.e_vm_default_addr = uvm_default_mapaddr,
    119 };
    120 
    121 struct proc *
    122 p_find(pid_t pid, uint flags)
    123 {
    124 
    125 	panic("%s: not implemented", __func__);
    126 }
    127 
    128 struct pgrp *
    129 pg_find(pid_t pid, uint flags)
    130 {
    131 
    132 	panic("%s: not implemented", __func__);
    133 }
    134 
    135 int
    136 pgid_in_session(struct proc *p, pid_t pg_id)
    137 {
    138 
    139 	panic("%s: not implemented", __func__);
    140 }
    141 
    142 int
    143 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    144 {
    145 	extern int hz;
    146 	int rv, error;
    147 	uint64_t sec, nsec;
    148 
    149 	if (mtx)
    150 		mutex_exit(mtx);
    151 
    152 	sec = timeo / hz;
    153 	nsec = (timeo % hz) * (1000000000 / hz);
    154 	rv = rumpuser_nanosleep(&sec, &nsec, &error);
    155 
    156 	if (mtx)
    157 		mutex_enter(mtx);
    158 
    159 	if (rv)
    160 		return error;
    161 
    162 	return 0;
    163 }
    164 
    165 void
    166 lwp_unsleep(lwp_t *l, bool cleanup)
    167 {
    168 
    169 	KASSERT(mutex_owned(l->l_mutex));
    170 
    171 	(*l->l_syncobj->sobj_unsleep)(l, cleanup);
    172 }
    173 
    174 vaddr_t
    175 calc_cache_size(struct vm_map *map, int pct, int va_pct)
    176 {
    177 	paddr_t t;
    178 
    179 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
    180 	if ((vaddr_t)t != t) {
    181 		panic("%s: needs tweak", __func__);
    182 	}
    183 	return t;
    184 }
    185 
    186 void
    187 assert_sleepable(void)
    188 {
    189 
    190 	/* always sleepable, although we should improve this */
    191 }
    192 
    193 int
    194 proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
    195 {
    196 
    197 	panic("%s: not implemented", __func__);
    198 }
    199 
    200 void
    201 proc_crmod_enter(void)
    202 {
    203 
    204 	panic("%s: not implemented", __func__);
    205 }
    206 
    207 void
    208 proc_crmod_leave(kauth_cred_t c1, kauth_cred_t c2, bool sugid)
    209 {
    210 
    211 	panic("%s: not implemented", __func__);
    212 }
    213 
    214 void
    215 module_init_md(void)
    216 {
    217 
    218 	/*
    219 	 * Nothing for now.  However, we should load the librump
    220 	 * symbol table.
    221 	 */
    222 }
    223 
    224 /* us and them, after all we're only ordinary seconds */
    225 static void
    226 rump_delay(unsigned int us)
    227 {
    228 	uint64_t sec, nsec;
    229 	int error;
    230 
    231 	sec = us / 1000000;
    232 	nsec = (us % 1000000) * 1000;
    233 
    234 	if (__predict_false(sec != 0))
    235 		printf("WARNING: over 1s delay\n");
    236 
    237 	rumpuser_nanosleep(&sec, &nsec, &error);
    238 }
    239 void (*delay_func)(unsigned int) = rump_delay;
    240 
    241 void
    242 proc_sesshold(struct session *ss)
    243 {
    244 
    245 	panic("proc_sesshold() impossible, session %p", ss);
    246 }
    247 
    248 void
    249 proc_sessrele(struct session *ss)
    250 {
    251 
    252 	panic("proc_sessrele() impossible, session %p", ss);
    253 }
    254 
    255 int
    256 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
    257 {
    258 
    259 	/* XXX */
    260 	*vm = p->p_vmspace;
    261 	return 0;
    262 }
    263 
    264 int
    265 ttycheckoutq(struct tty *tp, int wait)
    266 {
    267 
    268 	return 1;
    269 }
    270 
    271 void
    272 cnputc(int c)
    273 {
    274 	int error;
    275 
    276 	rumpuser_putchar(c, &error);
    277 }
    278 
    279 void
    280 cnflush(void)
    281 {
    282 
    283 	/* done */
    284 }
    285 
    286 int
    287 tputchar(int c, int flags, struct tty *tp)
    288 {
    289 
    290 	cnputc(c);
    291 	return 0;
    292 }
    293 
    294 void
    295 cpu_reboot(int howto, char *bootstr)
    296 {
    297 
    298 	rump_reboot(howto);
    299 
    300 	/* this function is __dead, we must exit */
    301 	rumpuser_exit(0);
    302 }
    303