xenstored.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include <xen/page.h>
  7. #include <xen/xenbus.h>
  8. #include "xenfs.h"
  9. static ssize_t xsd_read(struct file *file, char __user *buf,
  10. size_t size, loff_t *off)
  11. {
  12. const char *str = (const char *)file->private_data;
  13. return simple_read_from_buffer(buf, size, off, str, strlen(str));
  14. }
  15. static int xsd_release(struct inode *inode, struct file *file)
  16. {
  17. kfree(file->private_data);
  18. return 0;
  19. }
  20. static int xsd_kva_open(struct inode *inode, struct file *file)
  21. {
  22. file->private_data = (void *)kasprintf(GFP_KERNEL, "0x%p",
  23. xen_store_interface);
  24. if (!file->private_data)
  25. return -ENOMEM;
  26. return 0;
  27. }
  28. static int xsd_kva_mmap(struct file *file, struct vm_area_struct *vma)
  29. {
  30. size_t size = vma->vm_end - vma->vm_start;
  31. if ((size > PAGE_SIZE) || (vma->vm_pgoff != 0))
  32. return -EINVAL;
  33. if (remap_pfn_range(vma, vma->vm_start,
  34. virt_to_pfn(xen_store_interface),
  35. size, vma->vm_page_prot))
  36. return -EAGAIN;
  37. return 0;
  38. }
  39. const struct file_operations xsd_kva_file_ops = {
  40. .open = xsd_kva_open,
  41. .mmap = xsd_kva_mmap,
  42. .read = xsd_read,
  43. .release = xsd_release,
  44. };
  45. static int xsd_port_open(struct inode *inode, struct file *file)
  46. {
  47. file->private_data = (void *)kasprintf(GFP_KERNEL, "%d",
  48. xen_store_evtchn);
  49. if (!file->private_data)
  50. return -ENOMEM;
  51. return 0;
  52. }
  53. const struct file_operations xsd_port_file_ops = {
  54. .open = xsd_port_open,
  55. .read = xsd_read,
  56. .release = xsd_release,
  57. };