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