mmap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Remember: mmap, like most fops, does not work with debugfs as of 4.9! https://patchwork.kernel.org/patch/9252557/
  3. Adapted from:
  4. https://coherentmusings.wordpress.com/2014/06/10/implementing-mmap-for-transferring-data-from-user-space-to-kernel-space/
  5. Related:
  6. - https://stackoverflow.com/questions/10760479/mmap-kernel-buffer-to-user-space/10770582#10770582
  7. - https://stackoverflow.com/questions/1623008/allocating-memory-for-user-space-from-kernel-thread
  8. - https://stackoverflow.com/questions/6967933/mmap-mapping-in-user-space-a-kernel-buffer-allocated-with-kmalloc
  9. - https://github.com/jeremytrimble/ezdma
  10. - https://github.com/simonjhall/dma
  11. - https://github.com/ikwzm/udmabuf
  12. */
  13. #include <linux/fs.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h> /* min */
  16. #include <linux/mm.h>
  17. #include <linux/module.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  20. #include <linux/slab.h>
  21. static const char *filename = "lkmc_mmap";
  22. enum { BUFFER_SIZE = 4 };
  23. struct mmap_info {
  24. char *data;
  25. };
  26. /* After unmap. */
  27. static void vm_close(struct vm_area_struct *vma)
  28. {
  29. pr_info("vm_close\n");
  30. }
  31. /* First page access. */
  32. int (*fault)(struct vm_fault *vmf);
  33. static int vm_fault(struct vm_fault *vmf)
  34. {
  35. struct page *page;
  36. struct mmap_info *info;
  37. pr_info("vm_fault\n");
  38. info = (struct mmap_info *)vmf->vma->vm_private_data;
  39. if (info->data) {
  40. page = virt_to_page(info->data);
  41. get_page(page);
  42. vmf->page = page;
  43. }
  44. return 0;
  45. }
  46. /* Aftr mmap. TODO vs mmap, when can this happen at a different time than mmap? */
  47. static void vm_open(struct vm_area_struct *vma)
  48. {
  49. pr_info("vm_open\n");
  50. }
  51. static struct vm_operations_struct vm_ops =
  52. {
  53. .close = vm_close,
  54. .fault = vm_fault,
  55. .open = vm_open,
  56. };
  57. static int mmap(struct file *filp, struct vm_area_struct *vma)
  58. {
  59. pr_info("mmap\n");
  60. vma->vm_ops = &vm_ops;
  61. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  62. vma->vm_private_data = filp->private_data;
  63. vm_open(vma);
  64. return 0;
  65. }
  66. static int open(struct inode *inode, struct file *filp)
  67. {
  68. struct mmap_info *info;
  69. pr_info("open\n");
  70. info = kmalloc(sizeof(struct mmap_info), GFP_KERNEL);
  71. pr_info("virt_to_phys = 0x%llx\n", (unsigned long long)virt_to_phys((void *)info));
  72. info->data = (char *)get_zeroed_page(GFP_KERNEL);
  73. memcpy(info->data, "asdf", BUFFER_SIZE);
  74. filp->private_data = info;
  75. return 0;
  76. }
  77. static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  78. {
  79. struct mmap_info *info;
  80. int ret;
  81. pr_info("read\n");
  82. info = filp->private_data;
  83. ret = min(len, (size_t)BUFFER_SIZE);
  84. if (copy_to_user(buf, info->data, ret)) {
  85. ret = -EFAULT;
  86. }
  87. return ret;
  88. }
  89. static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  90. {
  91. struct mmap_info *info;
  92. pr_info("write\n");
  93. info = filp->private_data;
  94. if (copy_from_user(info->data, buf, min(len, (size_t)BUFFER_SIZE))) {
  95. return -EFAULT;
  96. } else {
  97. return len;
  98. }
  99. }
  100. static int release(struct inode *inode, struct file *filp)
  101. {
  102. struct mmap_info *info;
  103. pr_info("release\n");
  104. info = filp->private_data;
  105. free_page((unsigned long)info->data);
  106. kfree(info);
  107. filp->private_data = NULL;
  108. return 0;
  109. }
  110. static const struct file_operations fops = {
  111. .mmap = mmap,
  112. .open = open,
  113. .release = release,
  114. .read = read,
  115. .write = write,
  116. };
  117. static int myinit(void)
  118. {
  119. proc_create(filename, 0, NULL, &fops);
  120. return 0;
  121. }
  122. static void myexit(void)
  123. {
  124. remove_proc_entry(filename, NULL);
  125. }
  126. module_init(myinit)
  127. module_exit(myexit)
  128. MODULE_LICENSE("GPL");