puffs_rumpglue.c revision 1.12 1 /* $NetBSD: puffs_rumpglue.c,v 1.12 2013/04/29 20:08:48 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.12 2013/04/29 20:08:48 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 #include "rump_vfs_private.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 ssize_t n;
85
86 off = 0;
87 fp = fd_getfile(pap->fpfd);
88 error = dofileread(pap->fpfd, fp, buf, BUFSIZE,
89 &off, 0, &rv);
90 if (error) {
91 if (error == ENOENT && inited == 0)
92 goto retry;
93 if (error == ENXIO)
94 break;
95 panic("fileread failed: %d", error);
96 }
97 inited = 1;
98
99 while (rv) {
100 struct rumpuser_iovec iov;
101
102 iov.iov_base = buf;
103 iov.iov_len = rv;
104
105 n = rumpuser_iovwrite(pap->comfd, &iov, 1,
106 RUMPUSER_IOV_NOSEEK, &error);
107 if (n == -1)
108 panic("fileread failed: %d", error);
109 if (n == 0)
110 panic("fileread failed: closed");
111 rv -= n;
112 }
113 }
114
115 kthread_exit(0);
116 }
117
118 /* Read requests from comfd and proxy them to /dev/puffs */
119 static void
120 writethread(void *arg)
121 {
122 struct ptargs *pap = arg;
123 struct file *fp;
124 struct putter_hdr *phdr;
125 register_t rv;
126 char *buf;
127 off_t off;
128 size_t toread;
129 int error;
130
131 buf = kmem_alloc(BUFSIZE, KM_SLEEP);
132 phdr = (struct putter_hdr *)buf;
133
134 for (;;) {
135 ssize_t n;
136
137 /*
138 * Need to write everything to the "kernel" in one chunk,
139 * so make sure we have it here.
140 */
141 off = 0;
142 toread = sizeof(struct putter_hdr);
143 do {
144 struct rumpuser_iovec iov;
145
146 iov.iov_base = buf+off;
147 iov.iov_len = toread;
148 n = rumpuser_iovread(pap->comfd, &iov, 1,
149 RUMPUSER_IOV_NOSEEK, &error);
150 if (n <= 0) {
151 if (n == 0)
152 goto out;
153 panic("rumpuser_read %zd %d", n, error);
154 }
155 off += n;
156 if (off >= sizeof(struct putter_hdr))
157 toread = phdr->pth_framelen - off;
158 else
159 toread = off - sizeof(struct putter_hdr);
160 } while (toread);
161
162 off = 0;
163 rv = 0;
164 fp = fd_getfile(pap->fpfd);
165 error = dofilewrite(pap->fpfd, fp, buf, phdr->pth_framelen,
166 &off, 0, &rv);
167 if (error == ENXIO)
168 goto out;
169 KASSERT(rv == phdr->pth_framelen);
170 }
171 out:
172
173 kthread_exit(0);
174 }
175
176 int
177 rump_syspuffs_glueinit(int fd, int *newfd)
178 {
179 struct ptargs *pap;
180 int rv;
181
182 if ((rv = rump_init()) != 0)
183 return rv;
184
185 putterattach();
186 rv = puttercdopen(makedev(178, 0), 0, 0, curlwp);
187 if (rv && rv != EMOVEFD)
188 return rv;
189
190 pap = kmem_alloc(sizeof(struct ptargs), KM_SLEEP);
191 pap->comfd = fd;
192 pap->fpfd = curlwp->l_dupfd;
193 pap->fdp = curlwp->l_proc->p_fd;
194
195 kthread_create(PRI_NONE, 0, NULL, readthread, pap, NULL, "rputter");
196 kthread_create(PRI_NONE, 0, NULL, writethread, pap, NULL, "wputter");
197
198 *newfd = curlwp->l_dupfd;
199 return 0;
200 }
201