Home | History | Annotate | Line # | Download | only in libsyspuffs
      1 /*	$NetBSD: puffs_rumpglue.c,v 1.16 2016/01/26 23:12:17 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.16 2016/01/26 23:12:17 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.h>
     42 #include <dev/putter/putter_sys.h>
     43 
     44 #include <rump-sys/vfs.h>
     45 
     46 #include <rump/rump.h>
     47 #include <rump/rumpuser.h>
     48 
     49 void putterattach(void); /* XXX: from autoconf */
     50 dev_type_open(puttercdopen);
     51 
     52 struct ptargs {
     53 	int comfd;
     54 	int fpfd;
     55 	struct filedesc *fdp;
     56 };
     57 
     58 #define BUFSIZE (64*1024)
     59 extern int hz;
     60 
     61 /*
     62  * Read requests from /dev/puffs and forward them to comfd
     63  *
     64  * XXX: the init detection is really sucky, but let's not
     65  * waste too much energy for a better one here
     66  */
     67 static void
     68 readthread(void *arg)
     69 {
     70 	struct ptargs *pap = arg;
     71 	struct file *fp;
     72 	register_t rv;
     73 	char *buf;
     74 	off_t off;
     75 	int error, inited;
     76 
     77 	buf = kmem_alloc(BUFSIZE, KM_SLEEP);
     78 	inited = 0;
     79 
     80  retry:
     81 	kpause(NULL, 0, hz/4, NULL);
     82 
     83 	for (;;) {
     84 		size_t n;
     85 
     86 		off = 0;
     87 		fp = fd_getfile(pap->fpfd);
     88 		if (fp == NULL)
     89 			error = EINVAL;
     90 		else
     91 			error = dofileread(pap->fpfd, fp, buf, BUFSIZE,
     92 			    &off, 0, &rv);
     93 		if (error) {
     94 			if (error == ENOENT && inited == 0)
     95 				goto retry;
     96 			if (error == ENXIO)
     97 				break;
     98 			panic("fileread failed: %d", error);
     99 		}
    100 		inited = 1;
    101 
    102 		while (rv) {
    103 			struct rumpuser_iovec iov;
    104 
    105 			iov.iov_base = buf;
    106 			iov.iov_len = rv;
    107 
    108 			error = rumpuser_iovwrite(pap->comfd, &iov, 1,
    109 			    RUMPUSER_IOV_NOSEEK, &n);
    110 			if (error)
    111 				panic("fileread failed: %d", error);
    112 			if (n == 0)
    113 				panic("fileread failed: closed");
    114 			rv -= n;
    115 		}
    116 	}
    117 
    118 	kthread_exit(0);
    119 }
    120 
    121 /* Read requests from comfd and proxy them to /dev/puffs */
    122 static void
    123 writethread(void *arg)
    124 {
    125 	struct ptargs *pap = arg;
    126 	struct file *fp;
    127 	struct putter_hdr *phdr;
    128 	register_t rv;
    129 	char *buf;
    130 	off_t off;
    131 	size_t toread;
    132 	int error;
    133 
    134 	buf = kmem_alloc(BUFSIZE, KM_SLEEP);
    135 	phdr = (struct putter_hdr *)buf;
    136 
    137 	for (;;) {
    138 		size_t n;
    139 
    140 		/*
    141 		 * Need to write everything to the "kernel" in one chunk,
    142 		 * so make sure we have it here.
    143 		 */
    144 		off = 0;
    145 		toread = sizeof(struct putter_hdr);
    146 		do {
    147 			struct rumpuser_iovec iov;
    148 
    149 			iov.iov_base = buf+off;
    150 			iov.iov_len = toread;
    151 			error = rumpuser_iovread(pap->comfd, &iov, 1,
    152 			    RUMPUSER_IOV_NOSEEK, &n);
    153 			if (error)
    154 				panic("rumpuser_read %zd %d", n, error);
    155 			if (n == 0)
    156 				goto out;
    157 			off += n;
    158 			if (off >= sizeof(struct putter_hdr))
    159 				toread = phdr->pth_framelen - off;
    160 			else
    161 				toread = off - sizeof(struct putter_hdr);
    162 		} while (toread);
    163 
    164 		off = 0;
    165 		rv = 0;
    166 		fp = fd_getfile(pap->fpfd);
    167 		if (fp == NULL)
    168 			error = EINVAL;
    169 		else
    170 			error = dofilewrite(pap->fpfd, fp, buf,
    171 			    phdr->pth_framelen, &off, 0, &rv);
    172 		if (error == ENXIO)
    173 			goto out;
    174 		KASSERT(rv == phdr->pth_framelen);
    175 	}
    176  out:
    177 
    178 	kthread_exit(0);
    179 }
    180 
    181 int
    182 rump_syspuffs_glueinit(int fd, int *newfd)
    183 {
    184 	struct ptargs *pap;
    185 	int rv;
    186 
    187 	if ((rv = rump_init()) != 0)
    188 		return rv;
    189 
    190 	putterattach();
    191 	rv = puttercdopen(makedev(178, 0), 0, 0, curlwp);
    192 	if (rv && rv != EMOVEFD)
    193 		return rv;
    194 
    195 	pap = kmem_alloc(sizeof(struct ptargs), KM_SLEEP);
    196 	pap->comfd = fd;
    197 	pap->fpfd = curlwp->l_dupfd;
    198 	pap->fdp = curlwp->l_proc->p_fd;
    199 
    200 	rv = kthread_create(PRI_NONE, 0, NULL, readthread, pap, NULL,
    201 	    "rputter");
    202 	if (rv)
    203 		return rv;
    204 
    205 	rv = kthread_create(PRI_NONE, 0, NULL, writethread, pap, NULL,
    206 	    "wputter");
    207 	if (rv)
    208 		return rv;
    209 
    210 	*newfd = curlwp->l_dupfd;
    211 	return 0;
    212 }
    213