Home | History | Annotate | Line # | Download | only in libsyspuffs
puffs_rumpglue.c revision 1.4
      1 /*	$NetBSD: puffs_rumpglue.c,v 1.4 2008/07/29 13:17:43 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.4 2008/07/29 13:17:43 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/rump.h>
     44 #include <rump/rumpuser.h>
     45 
     46 #include "puffs_rumpglue.h"
     47 
     48 void putterattach(void); /* XXX: from autoconf */
     49 dev_type_open(puttercdopen);
     50 
     51 struct ptargs {
     52 	int comfd;
     53 	int fpfd;
     54 	struct filedesc *fdp;
     55 };
     56 
     57 #define BUFSIZE (64*1024)
     58 extern int hz;
     59 
     60 /*
     61  * Read requests from /dev/puffs and forward them to comfd
     62  *
     63  * XXX: the init detection is really sucky, but let's not
     64  * waste too much energy for a better one here
     65  */
     66 static void
     67 readthread(void *arg)
     68 {
     69 	struct ptargs *pap = arg;
     70 	struct file *fp;
     71 	register_t rv;
     72 	char *buf;
     73 	off_t off;
     74 	int error, inited;
     75 
     76 	buf = kmem_alloc(BUFSIZE, KM_SLEEP);
     77 	inited = 0;
     78 
     79  retry:
     80 	kpause(NULL, 0, hz/4, NULL);
     81 
     82 	for (;;) {
     83 		ssize_t n;
     84 
     85 		off = 0;
     86 		fp = fd_getfile(pap->fpfd);
     87 		error = dofileread(pap->fpfd, fp, buf, BUFSIZE,
     88 		    &off, 0, &rv);
     89 		if (error) {
     90 			if (error == ENOENT && inited == 0)
     91 				goto retry;
     92 			if (error == ENXIO)
     93 				kthread_exit(0);
     94 			panic("fileread failed: %d", error);
     95 		}
     96 		inited = 1;
     97 
     98 		while (rv) {
     99 			n = rumpuser_write(pap->comfd, buf, rv, &error);
    100 			if (n == -1)
    101 				panic("fileread failed: %d", error);
    102 			if (n == 0)
    103 				panic("fileread failed: closed");
    104 			rv -= n;
    105 		}
    106 	}
    107 }
    108 
    109 /* Read requests from comfd and proxy them to /dev/puffs */
    110 static void
    111 writethread(void *arg)
    112 {
    113 	struct ptargs *pap = arg;
    114 	struct file *fp;
    115 	register_t rv;
    116 	char *buf;
    117 	off_t off;
    118 	int error;
    119 
    120 	buf = kmem_alloc(BUFSIZE, KM_SLEEP);
    121 
    122 	for (;;) {
    123 		ssize_t n;
    124 
    125 		n = rumpuser_read(pap->comfd, buf, BUFSIZE, &error);
    126 		if (n <= 0)
    127 			panic("rumpuser_read %zd %d", n, error);
    128 
    129 		off = 0;
    130 		fp = fd_getfile(pap->fpfd);
    131 		error = dofilewrite(pap->fpfd, fp, buf, n,
    132 		    &off, 0, &rv);
    133 		if (error == ENXIO)
    134 			kthread_exit(0);
    135 		KASSERT(rv == n);
    136 	}
    137 }
    138 
    139 int
    140 puffs_rumpglue_init(int fd, int *newfd)
    141 {
    142 	struct ptargs *pap;
    143 	int rv;
    144 
    145 	rump_init();
    146 	putterattach();
    147 	rv = puttercdopen(makedev(178, 0), 0, 0, curlwp);
    148 	if (rv && rv != EMOVEFD)
    149 		return rv;
    150 
    151 	pap = kmem_alloc(sizeof(struct ptargs), KM_SLEEP);
    152 	pap->comfd = fd;
    153 	pap->fpfd = curlwp->l_dupfd;
    154 	pap->fdp = curlwp->l_proc->p_fd;
    155 
    156 	kthread_create(PRI_NONE, 0, NULL, readthread, pap, NULL, "rputter");
    157 	kthread_create(PRI_NONE, 0, NULL, writethread, pap, NULL, "wputter");
    158 
    159 	*newfd = curlwp->l_dupfd;
    160 	return 0;
    161 }
    162