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