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