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