mmap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* https://github.com/cirosantilli/linux-kernel-module-cheat#mmap */
  2. #include <linux/fs.h>
  3. #include <linux/init.h>
  4. #include <linux/kernel.h> /* min */
  5. #include <linux/mm.h>
  6. #include <linux/module.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/uaccess.h> /* copy_from_user, copy_to_user */
  9. #include <linux/slab.h>
  10. static const char *filename = "lkmc_mmap";
  11. enum { BUFFER_SIZE = 4 };
  12. struct mmap_info {
  13. char *data;
  14. };
  15. /* After unmap. */
  16. static void vm_close(struct vm_area_struct *vma)
  17. {
  18. pr_info("vm_close\n");
  19. }
  20. /* First page access. */
  21. static vm_fault_t vm_fault(struct vm_fault *vmf)
  22. {
  23. struct page *page;
  24. struct mmap_info *info;
  25. pr_info("vm_fault\n");
  26. info = (struct mmap_info *)vmf->vma->vm_private_data;
  27. if (info->data) {
  28. page = virt_to_page(info->data);
  29. get_page(page);
  30. vmf->page = page;
  31. }
  32. return 0;
  33. }
  34. /* Aftr mmap. TODO vs mmap, when can this happen at a different time than mmap? */
  35. static void vm_open(struct vm_area_struct *vma)
  36. {
  37. pr_info("vm_open\n");
  38. }
  39. static struct vm_operations_struct vm_ops =
  40. {
  41. .close = vm_close,
  42. .fault = vm_fault,
  43. .open = vm_open,
  44. };
  45. static int mmap(struct file *filp, struct vm_area_struct *vma)
  46. {
  47. pr_info("mmap\n");
  48. vma->vm_ops = &vm_ops;
  49. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  50. vma->vm_private_data = filp->private_data;
  51. vm_open(vma);
  52. return 0;
  53. }
  54. static int open(struct inode *inode, struct file *filp)
  55. {
  56. struct mmap_info *info;
  57. pr_info("open\n");
  58. info = kmalloc(sizeof(struct mmap_info), GFP_KERNEL);
  59. pr_info("virt_to_phys = 0x%llx\n", (unsigned long long)virt_to_phys((void *)info));
  60. info->data = (char *)get_zeroed_page(GFP_KERNEL);
  61. memcpy(info->data, "asdf", BUFFER_SIZE);
  62. filp->private_data = info;
  63. return 0;
  64. }
  65. static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  66. {
  67. struct mmap_info *info;
  68. int ret;
  69. pr_info("read\n");
  70. info = filp->private_data;
  71. ret = min(len, (size_t)BUFFER_SIZE);
  72. if (copy_to_user(buf, info->data, ret)) {
  73. ret = -EFAULT;
  74. }
  75. return ret;
  76. }
  77. static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  78. {
  79. struct mmap_info *info;
  80. pr_info("write\n");
  81. info = filp->private_data;
  82. if (copy_from_user(info->data, buf, min(len, (size_t)BUFFER_SIZE))) {
  83. return -EFAULT;
  84. } else {
  85. return len;
  86. }
  87. }
  88. static int release(struct inode *inode, struct file *filp)
  89. {
  90. struct mmap_info *info;
  91. pr_info("release\n");
  92. info = filp->private_data;
  93. free_page((unsigned long)info->data);
  94. kfree(info);
  95. filp->private_data = NULL;
  96. return 0;
  97. }
  98. static const struct file_operations fops = {
  99. .mmap = mmap,
  100. .open = open,
  101. .release = release,
  102. .read = read,
  103. .write = write,
  104. };
  105. static int myinit(void)
  106. {
  107. proc_create(filename, 0, NULL, &fops);
  108. return 0;
  109. }
  110. static void myexit(void)
  111. {
  112. remove_proc_entry(filename, NULL);
  113. }
  114. module_init(myinit)
  115. module_exit(myexit)
  116. MODULE_LICENSE("GPL");