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