1 #include <linux/slab.h>
2 #include <linux/types.h>
5 #include <linux/miscdevice.h>
6 #include <linux/module.h>
7 #include <linux/capability.h>
11 #include <xen/xenbus.h>
12 #include <xen/xenbus_dev.h>
13 #include <xen/grant_table.h>
14 #include <xen/events.h>
15 #include <asm/xen/hypervisor.h>
17 #include "xenbus_comms.h"
19 MODULE_LICENSE("GPL");
21 static int xenbus_backend_open(struct inode
*inode
, struct file
*filp
)
23 if (!capable(CAP_SYS_ADMIN
))
26 return nonseekable_open(inode
, filp
);
29 static long xenbus_alloc(domid_t domid
)
31 struct evtchn_alloc_unbound arg
;
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.
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.
49 gnttab_grant_foreign_access_ref(GNTTAB_RESERVED_XENSTORE
, domid
,
50 virt_to_mfn(xen_store_interface
), 0 /* writable */);
53 arg
.remote_dom
= domid
;
55 err
= HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound
, &arg
);
59 if (xen_store_evtchn
> 0)
62 xen_store_evtchn
= arg
.port
;
73 static long xenbus_backend_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
75 if (!capable(CAP_SYS_ADMIN
))
79 case IOCTL_XENBUS_BACKEND_EVTCHN
:
80 if (xen_store_evtchn
> 0)
81 return xen_store_evtchn
;
84 case IOCTL_XENBUS_BACKEND_SETUP
:
85 return xenbus_alloc(data
);
92 static int xenbus_backend_mmap(struct file
*file
, struct vm_area_struct
*vma
)
94 size_t size
= vma
->vm_end
- vma
->vm_start
;
96 if (!capable(CAP_SYS_ADMIN
))
99 if ((size
> PAGE_SIZE
) || (vma
->vm_pgoff
!= 0))
102 if (remap_pfn_range(vma
, vma
->vm_start
,
103 virt_to_pfn(xen_store_interface
),
104 size
, vma
->vm_page_prot
))
110 const struct file_operations xenbus_backend_fops
= {
111 .open
= xenbus_backend_open
,
112 .mmap
= xenbus_backend_mmap
,
113 .unlocked_ioctl
= xenbus_backend_ioctl
,
116 static struct miscdevice xenbus_backend_dev
= {
117 .minor
= MISC_DYNAMIC_MINOR
,
118 .name
= "xen/xenbus_backend",
119 .fops
= &xenbus_backend_fops
,
122 static int __init
xenbus_backend_init(void)
126 if (!xen_initial_domain())
129 err
= misc_register(&xenbus_backend_dev
);
131 printk(KERN_ERR
"Could not register xenbus backend device\n");
135 static void __exit
xenbus_backend_exit(void)
137 misc_deregister(&xenbus_backend_dev
);
140 module_init(xenbus_backend_init
);
141 module_exit(xenbus_backend_exit
);