Home | History | Annotate | Line # | Download | only in libsyspuffs
puffs_rumpglue.c revision 1.1
      1 /*	$NetBSD: puffs_rumpglue.c,v 1.1 2008/01/02 18:15:13 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Research Foundation of Helsinki University of Technology
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: puffs_rumpglue.c,v 1.1 2008/01/02 18:15:13 pooka Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/conf.h>
     36 #include <sys/file.h>
     37 #include <sys/filedesc.h>
     38 #include <sys/kthread.h>
     39 #include <sys/mount.h>
     40 
     41 #include <dev/putter/putter_sys.h>
     42 
     43 #include "rump.h"
     44 #include "rumpuser.h"
     45 
     46 #include "puffs_rumpglue.h"
     47 
     48 int
     49 dounmount(struct mount *mp, int flags, struct lwp *l)
     50 {
     51 
     52 	VFS_UNMOUNT(mp, MNT_FORCE);
     53 	panic("control fd is dead");
     54 }
     55 
     56 void putterattach(void); /* XXX: from autoconf */
     57 dev_type_open(puttercdopen);
     58 
     59 struct ptargs {
     60 	int comfd;
     61 	int fpfd;
     62 	struct filedesc *fdp;
     63 };
     64 
     65 #define BUFSIZE (64*1024)
     66 extern int hz;
     67 
     68 /*
     69  * Read requests from /dev/puffs and forward them to comfd
     70  *
     71  * XXX: the init detection is really sucky, but let's not
     72  * waste too much energy for a better one here
     73  */
     74 static void
     75 readthread(void *arg)
     76 {
     77 	struct ptargs *pap = arg;
     78 	struct file *fp;
     79 	register_t rv;
     80 	char *buf;
     81 	off_t off;
     82 	int error, inited;
     83 
     84 	buf = kmem_alloc(BUFSIZE, KM_SLEEP);
     85 	inited = 0;
     86 
     87  retry:
     88 	kpause(NULL, 0, hz/4, NULL);
     89 
     90 	for (;;) {
     91 		ssize_t n;
     92 
     93 		off = 0;
     94 		fp = fd_getfile(pap->fdp, pap->fpfd);
     95 		FILE_USE(fp);
     96 		error = dofileread(pap->fpfd, fp, buf, BUFSIZE,
     97 		    &off, 0, &rv);
     98 		if (error) {
     99 			if (error == ENOENT && inited == 0)
    100 				goto retry;
    101 			if (error == ENXIO)
    102 				kthread_exit(0);
    103 			panic("fileread failed: %d", error);
    104 		}
    105 		inited = 1;
    106 
    107 		while (rv) {
    108 			n = rumpuser_write(pap->comfd, buf, rv, &error);
    109 			if (n == -1)
    110 				panic("fileread failed: %d", error);
    111 			if (n == 0)
    112 				panic("fileread failed: closed");
    113 			rv -= n;
    114 		}
    115 	}
    116 }
    117 
    118 /* Read requests from comfd and proxy them to /dev/puffs */
    119 static void
    120 writethread(void *arg)
    121 {
    122 	struct ptargs *pap = arg;
    123 	struct file *fp;
    124 	register_t rv;
    125 	char *buf;
    126 	off_t off;
    127 	int error;
    128 
    129 	buf = kmem_alloc(BUFSIZE, KM_SLEEP);
    130 
    131 	for (;;) {
    132 		ssize_t n;
    133 
    134 		n = rumpuser_read(pap->comfd, buf, BUFSIZE, &error);
    135 		if (n <= 0)
    136 			panic("rumpuser_read %zd %d", n, error);
    137 
    138 		off = 0;
    139 		fp = fd_getfile(pap->fdp, pap->fpfd);
    140 		FILE_USE(fp);
    141 		error = dofilewrite(pap->fpfd, fp, buf, n,
    142 		    &off, 0, &rv);
    143 		if (error == ENXIO)
    144 			kthread_exit(0);
    145 		KASSERT(rv == n);
    146 	}
    147 }
    148 
    149 int
    150 puffs_rumpglue_init(int fd, int *newfd)
    151 {
    152 	struct ptargs *pap;
    153 	int rv;
    154 
    155 	rump_init();
    156 	putterattach();
    157 	rv = puttercdopen(makedev(178, 0), 0, 0, curlwp);
    158 	if (rv && rv != EMOVEFD)
    159 		return rv;
    160 
    161 	pap = kmem_alloc(sizeof(struct ptargs), KM_SLEEP);
    162 	pap->comfd = fd;
    163 	pap->fpfd = curlwp->l_dupfd;
    164 	pap->fdp = curlwp->l_proc->p_fd;
    165 
    166 	kthread_create(PRI_NONE, 0, NULL, readthread, pap, NULL, "rputter");
    167 	kthread_create(PRI_NONE, 0, NULL, writethread, pap, NULL, "wputter");
    168 
    169 	*newfd = curlwp->l_dupfd;
    170 	return 0;
    171 }
    172