1 /* $NetBSD: puffs_rumpglue.c,v 1.10 2009/10/14 17:29:19 pooka Exp $ */
4 * Copyright (c) 2008 Antti Kantee. All Rights Reserved.
6 * Development of this software was supported by the
7 * Research Foundation of Helsinki University of Technology
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: puffs_rumpglue.c,v 1.10 2009/10/14 17:29:19 pooka Exp $");
34 #include <sys/param.h>
37 #include <sys/filedesc.h>
38 #include <sys/kthread.h>
39 #include <sys/mount.h>
41 #include <dev/putter/putter.h>
42 #include <dev/putter/putter_sys.h>
44 #include <rump/rump.h>
45 #include <rump/rumpuser.h>
47 #include "rump_vfs_private.h"
49 void putterattach(void); /* XXX: from autoconf */
50 dev_type_open(puttercdopen
);
58 #define BUFSIZE (64*1024)
62 * Read requests from /dev/puffs and forward them to comfd
64 * XXX: the init detection is really sucky, but let's not
65 * waste too much energy for a better one here
70 struct ptargs
*pap
= arg
;
77 buf
= kmem_alloc(BUFSIZE
, KM_SLEEP
);
81 kpause(NULL
, 0, hz
/4, NULL
);
87 fp
= fd_getfile(pap
->fpfd
);
88 error
= dofileread(pap
->fpfd
, fp
, buf
, BUFSIZE
,
91 if (error
== ENOENT
&& inited
== 0)
95 panic("fileread failed: %d", error
);
100 n
= rumpuser_write(pap
->comfd
, buf
, rv
, &error
);
102 panic("fileread failed: %d", error
);
104 panic("fileread failed: closed");
112 /* Read requests from comfd and proxy them to /dev/puffs */
114 writethread(void *arg
)
116 struct ptargs
*pap
= arg
;
118 struct putter_hdr
*phdr
;
125 buf
= kmem_alloc(BUFSIZE
, KM_SLEEP
);
126 phdr
= (struct putter_hdr
*)buf
;
132 * Need to write everything to the "kernel" in one chunk,
133 * so make sure we have it here.
136 toread
= sizeof(struct putter_hdr
);
138 n
= rumpuser_read(pap
->comfd
, buf
+off
, toread
, &error
);
142 panic("rumpuser_read %zd %d", n
, error
);
145 if (off
>= sizeof(struct putter_hdr
))
146 toread
= phdr
->pth_framelen
- off
;
148 toread
= off
- sizeof(struct putter_hdr
);
153 fp
= fd_getfile(pap
->fpfd
);
154 error
= dofilewrite(pap
->fpfd
, fp
, buf
, phdr
->pth_framelen
,
158 KASSERT(rv
== phdr
->pth_framelen
);
166 rump_syspuffs_glueinit(int fd
, int *newfd
)
171 if ((rv
= rump_init()) != 0)
175 rv
= puttercdopen(makedev(178, 0), 0, 0, curlwp
);
176 if (rv
&& rv
!= EMOVEFD
)
179 pap
= kmem_alloc(sizeof(struct ptargs
), KM_SLEEP
);
181 pap
->fpfd
= curlwp
->l_dupfd
;
182 pap
->fdp
= curlwp
->l_proc
->p_fd
;
184 kthread_create(PRI_NONE
, 0, NULL
, readthread
, pap
, NULL
, "rputter");
185 kthread_create(PRI_NONE
, 0, NULL
, writethread
, pap
, NULL
, "wputter");
187 *newfd
= curlwp
->l_dupfd
;