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