xref: /openbmc/linux/drivers/xen/xenfs/xenstored.c (revision 565d76cb)
1 #include <linux/slab.h>
2 #include <linux/types.h>
3 #include <linux/mm.h>
4 #include <linux/fs.h>
5 
6 #include <xen/page.h>
7 
8 #include "xenfs.h"
9 #include "../xenbus/xenbus_comms.h"
10 
11 static ssize_t xsd_read(struct file *file, char __user *buf,
12 			    size_t size, loff_t *off)
13 {
14 	const char *str = (const char *)file->private_data;
15 	return simple_read_from_buffer(buf, size, off, str, strlen(str));
16 }
17 
18 static int xsd_release(struct inode *inode, struct file *file)
19 {
20 	kfree(file->private_data);
21 	return 0;
22 }
23 
24 static int xsd_kva_open(struct inode *inode, struct file *file)
25 {
26 	file->private_data = (void *)kasprintf(GFP_KERNEL, "0x%p",
27 					       xen_store_interface);
28 	if (!file->private_data)
29 		return -ENOMEM;
30 	return 0;
31 }
32 
33 static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
34 {
35 	size_t size = vma->vm_end - vma->vm_start;
36 
37 	if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
38 		return -EINVAL;
39 
40 	if (remap_pfn_range(vma, vma->vm_start,
41 			    virt_to_pfn(xen_store_interface),
42 			    size, vma->vm_page_prot))
43 		return -EAGAIN;
44 
45 	return 0;
46 }
47 
48 const struct file_operations xsd_kva_file_ops = {
49 	.open = xsd_kva_open,
50 	.mmap = xsd_kva_mmap,
51 	.read = xsd_read,
52 	.release = xsd_release,
53 };
54 
55 static int xsd_port_open(struct inode *inode, struct file *file)
56 {
57 	file->private_data = (void *)kasprintf(GFP_KERNEL, "%d",
58 					       xen_store_evtchn);
59 	if (!file->private_data)
60 		return -ENOMEM;
61 	return 0;
62 }
63 
64 const struct file_operations xsd_port_file_ops = {
65 	.open = xsd_port_open,
66 	.read = xsd_read,
67 	.release = xsd_release,
68 };
69