]>
Commit | Line | Data |
---|---|---|
283c0972 JP |
1 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
2 | ||
e9f0fec3 BB |
3 | #include <linux/slab.h> |
4 | #include <linux/types.h> | |
5 | #include <linux/mm.h> | |
6 | #include <linux/fs.h> | |
7 | #include <linux/miscdevice.h> | |
ab1241a1 | 8 | #include <linux/init.h> |
e9f0fec3 BB |
9 | #include <linux/capability.h> |
10 | ||
01464a60 | 11 | #include <xen/xen.h> |
e9f0fec3 | 12 | #include <xen/page.h> |
d2fb4c51 | 13 | #include <xen/xenbus.h> |
e9f0fec3 | 14 | #include <xen/xenbus_dev.h> |
d2fb4c51 DDG |
15 | #include <xen/grant_table.h> |
16 | #include <xen/events.h> | |
17 | #include <asm/xen/hypervisor.h> | |
e9f0fec3 | 18 | |
332f791d | 19 | #include "xenbus.h" |
e9f0fec3 | 20 | |
e9f0fec3 BB |
21 | static int xenbus_backend_open(struct inode *inode, struct file *filp) |
22 | { | |
23 | if (!capable(CAP_SYS_ADMIN)) | |
24 | return -EPERM; | |
25 | ||
26 | return nonseekable_open(inode, filp); | |
27 | } | |
28 | ||
d2fb4c51 DDG |
29 | static long xenbus_alloc(domid_t domid) |
30 | { | |
31 | struct evtchn_alloc_unbound arg; | |
32 | int err = -EEXIST; | |
33 | ||
34 | xs_suspend(); | |
35 | ||
36 | /* If xenstored_ready is nonzero, that means we have already talked to | |
37 | * xenstore and set up watches. These watches will be restored by | |
38 | * xs_resume, but that requires communication over the port established | |
39 | * below that is not visible to anyone until the ioctl returns. | |
40 | * | |
41 | * This can be resolved by splitting the ioctl into two parts | |
42 | * (postponing the resume until xenstored is active) but this is | |
43 | * unnecessarily complex for the intended use where xenstored is only | |
44 | * started once - so return -EEXIST if it's already running. | |
45 | */ | |
46 | if (xenstored_ready) | |
47 | goto out_err; | |
48 | ||
49 | gnttab_grant_foreign_access_ref(GNTTAB_RESERVED_XENSTORE, domid, | |
0df4f266 | 50 | virt_to_gfn(xen_store_interface), 0 /* writable */); |
d2fb4c51 DDG |
51 | |
52 | arg.dom = DOMID_SELF; | |
53 | arg.remote_dom = domid; | |
54 | ||
55 | err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &arg); | |
56 | if (err) | |
57 | goto out_err; | |
58 | ||
59 | if (xen_store_evtchn > 0) | |
60 | xb_deinit_comms(); | |
61 | ||
62 | xen_store_evtchn = arg.port; | |
63 | ||
64 | xs_resume(); | |
65 | ||
66 | return arg.port; | |
67 | ||
68 | out_err: | |
69 | xs_suspend_cancel(); | |
70 | return err; | |
71 | } | |
72 | ||
3d645b02 LN |
73 | static long xenbus_backend_ioctl(struct file *file, unsigned int cmd, |
74 | unsigned long data) | |
e9f0fec3 BB |
75 | { |
76 | if (!capable(CAP_SYS_ADMIN)) | |
77 | return -EPERM; | |
78 | ||
79 | switch (cmd) { | |
d7e50750 LN |
80 | case IOCTL_XENBUS_BACKEND_EVTCHN: |
81 | if (xen_store_evtchn > 0) | |
82 | return xen_store_evtchn; | |
83 | return -ENODEV; | |
84 | case IOCTL_XENBUS_BACKEND_SETUP: | |
85 | return xenbus_alloc(data); | |
86 | default: | |
87 | return -ENOTTY; | |
e9f0fec3 BB |
88 | } |
89 | } | |
90 | ||
91 | static int xenbus_backend_mmap(struct file *file, struct vm_area_struct *vma) | |
92 | { | |
93 | size_t size = vma->vm_end - vma->vm_start; | |
94 | ||
95 | if (!capable(CAP_SYS_ADMIN)) | |
96 | return -EPERM; | |
97 | ||
98 | if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0)) | |
99 | return -EINVAL; | |
100 | ||
101 | if (remap_pfn_range(vma, vma->vm_start, | |
102 | virt_to_pfn(xen_store_interface), | |
103 | size, vma->vm_page_prot)) | |
104 | return -EAGAIN; | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
b8b0f559 | 109 | static const struct file_operations xenbus_backend_fops = { |
e9f0fec3 BB |
110 | .open = xenbus_backend_open, |
111 | .mmap = xenbus_backend_mmap, | |
112 | .unlocked_ioctl = xenbus_backend_ioctl, | |
113 | }; | |
114 | ||
115 | static struct miscdevice xenbus_backend_dev = { | |
116 | .minor = MISC_DYNAMIC_MINOR, | |
117 | .name = "xen/xenbus_backend", | |
118 | .fops = &xenbus_backend_fops, | |
119 | }; | |
120 | ||
121 | static int __init xenbus_backend_init(void) | |
122 | { | |
123 | int err; | |
124 | ||
125 | if (!xen_initial_domain()) | |
126 | return -ENODEV; | |
127 | ||
128 | err = misc_register(&xenbus_backend_dev); | |
129 | if (err) | |
283c0972 | 130 | pr_err("Could not register xenbus backend device\n"); |
e9f0fec3 BB |
131 | return err; |
132 | } | |
ab1241a1 | 133 | device_initcall(xenbus_backend_init); |