-
Notifications
You must be signed in to change notification settings - Fork 128
/
xendev_component.c
171 lines (143 loc) · 4.34 KB
/
xendev_component.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* rump_dev_xen.c
*
* Machinery for setting up the contents of /dev/xen* in a rumpkernel.
*
* Copyright (c) 2014 Citrix
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this source file (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: $");
#include "rumpxen_xendev.h"
#include "rump_private.h"
#include "rump_vfs_private.h"
#include <sys/vfs_syscalls.h>
#include <sys/dirent.h>
#include <miscfs/kernfs/kernfs.h>
char *xbd_strdup(const char *s)
{
char *r;
size_t l = strlen(s) + 1;
r = xbd_malloc(l);
if (!r)
return r;
memcpy(r, s, l);
return r;
}
#define DEV_XEN "/dev/xen"
static const struct xen_dev_info {
const char *path;
void (*xd_init)(void);
int (*xd_open)(struct file *fp, void **fdata_r);
const struct fileops *fo;
} devs[] = {
#define XDEV(cmin, path, init_fn, component) \
[cmin] = { path, init_fn, component##_dev_open, \
&component##_dev_fileops }
XDEV(0, DEV_XEN "/xenbus", NULL, xenbus),
XDEV(1, "/dev/xenevt", xenevt_dev_init, xenevt),
#undef XDEV
};
#define NUM_DEV_INFOS (sizeof(devs)/sizeof(devs[0]))
static int
xen_dev_open(dev_t dev, int flags, int mode, struct lwp *l)
{
const struct xen_dev_info *xdinfo;
int fd, err;
struct file *fp;
void *fdata;
DPRINTF(("xen devsw: opening minor=%lu\n", (unsigned long)minor(dev)));
if (minor(dev) < 0 || minor(dev) >= NUM_DEV_INFOS)
return ENODEV;
xdinfo = &devs[minor(dev)];
if (!xdinfo->xd_open)
return ENODEV;
err = fd_allocfile(&fp, &fd);
if (err)
return err;
DPRINTF(("%s: opening...\n", xdinfo->path));
err = xdinfo->xd_open(fp, &fdata);
if (err) {
fd_abort(curproc, fp, fd);
return err;
}
DPRINTF(("%s: opened, fd_clone\n", xdinfo->path));
return fd_clone(fp, fd, flags, xdinfo->fo, fdata);
}
static const struct cdevsw xen_dev_cdevsw = {
.d_open = xen_dev_open,
.d_close = nullclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = noioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_flag = D_OTHER
};
#define DIR_MODE (S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
kernfs_parentdir_t *kernxen_pkt;
static void xenkernfs_init(void)
{
kernfs_entry_t *dkt;
KERNFS_ALLOCENTRY(dkt, M_TEMP, M_WAITOK);
KERNFS_INITENTRY(dkt, DT_DIR, "xen", NULL, KFSsubdir, VDIR, DIR_MODE);
kernfs_addentry(NULL, dkt);
kernxen_pkt = KERNFS_ENTOPARENTDIR(dkt);
}
RUMP_COMPONENT(RUMP_COMPONENT_DEV)
{
devmajor_t bmaj, cmaj;
devminor_t cmin;
int err;
const struct xen_dev_info *xdinfo;
DPRINTF(("xen devsw: attaching\n"));
err = do_sys_mkdir(DEV_XEN, 0755, UIO_SYSSPACE);
if (err && err != EEXIST)
panic("xen devsw: mkdir " DEV_XEN " failed: %d", err);
bmaj = cmaj = NODEVMAJOR;
err = devsw_attach("xen", NULL, &bmaj, &xen_dev_cdevsw, &cmaj);
if (err)
panic("xen devsw: attach failed: %d", err);
for (cmin = 0; cmin < NUM_DEV_INFOS; cmin++) {
xdinfo = &devs[cmin];
err = rump_vfs_makeonedevnode(S_IFCHR, xdinfo->path, cmaj, cmin);
if (err)
panic("%s: cannot create device node: %d",
xdinfo->path, err);
if (xdinfo->xd_init)
xdinfo->xd_init();
DPRINTF(("%s: created, %lu.%lu\n",
xdinfo->path, (unsigned long)cmaj, (unsigned long)cmin));
}
xenkernfs_init();
xenprivcmd_init();
}
/*
* Local variables:
* c-file-style: "linux"
* indent-tabs-mode: t
* c-indent-level: 8
* c-basic-offset: 8
* tab-width: 8
* End:
*/