diff --git a/kernel_module/debugfs.c b/kernel_module/debugfs.c index bc3bc37..9345d23 100644 --- a/kernel_module/debugfs.c +++ b/kernel_module/debugfs.c @@ -6,6 +6,11 @@ Usage: /debugfs.sh Requires `CONFIG_DEBUG_FS=y`. + +Only the more basic fops can be implemented in debugfs, e.g. mmap is never called: + +- https://patchwork.kernel.org/patch/9252557/ +- https://github.com/torvalds/linux/blob/v4.9/fs/debugfs/file.c#L212 */ #include diff --git a/kernel_module/mmap.c b/kernel_module/mmap.c index 1fb3b6a..7c34e07 100644 --- a/kernel_module/mmap.c +++ b/kernel_module/mmap.c @@ -1,4 +1,6 @@ -/* TODO neither works, mmap never gets called. */ +/* +Remember: mmap does not work with debugfs as of 4.9! +*/ #if 0 @@ -122,7 +124,9 @@ module_init(myinit); module_exit(myexit); MODULE_LICENSE("GPL"); -#endif + +/*minimized debugfs*/ + #include #include @@ -133,8 +137,9 @@ MODULE_LICENSE("GPL"); static struct dentry *debugfs_file; -static int map_mmap(struct file *filp, struct vm_area_struct *vma) +static int mmap(struct file *filp, struct vm_area_struct *vma) { + pr_info("mmap\n"); if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, vma->vm_end - vma->vm_start, vma->vm_page_prot)) { return -EAGAIN; @@ -146,7 +151,7 @@ struct file_operations fops = { .owner = THIS_MODULE, .open = nonseekable_open, - .mmap = map_mmap + .mmap = mmap }; static int myinit(void) @@ -163,3 +168,90 @@ static void myexit(void) module_init(myinit); module_exit(myexit); MODULE_LICENSE("GPL"); + +/*working chardev*/ + +#include +#include +#include +#include +#include +#include + +#define NAME "lkmc_mmap" + +static int major; + +static int mmap(struct file *filp, struct vm_area_struct *vma) +{ + pr_info("mmap\n"); + if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, + vma->vm_end - vma->vm_start, vma->vm_page_prot)) { + return -EAGAIN; + } + return 0; +} + +struct file_operations fops = +{ + .owner = THIS_MODULE, + .open = nonseekable_open, + .mmap = mmap +}; + +static int myinit(void) +{ + major = register_chrdev(0, NAME, &fops); + return 0; +} + +static void myexit(void) +{ + unregister_chrdev(major, NAME); +} + +module_init(myinit) +module_exit(myexit) +MODULE_LICENSE("GPL"); + +#endif + +/* proc attempt */ + +#include +#include +#include +#include +#include +#include +#include + +static const char *filename = "lkmc_procfs"; + +static int mmap(struct file *filp, struct vm_area_struct *vma) +{ + pr_info("mmap\n"); + return 0; +} + +struct file_operations fops = +{ + .owner = THIS_MODULE, + .open = nonseekable_open, + .mmap = mmap +}; + +static int myinit(void) +{ + proc_create(filename, 0, NULL, &fops); + return 0; +} + +static void myexit(void) +{ + remove_proc_entry(filename, NULL); +} + +module_init(myinit) +module_exit(myexit) +MODULE_LICENSE("GPL"); diff --git a/kernel_module/user/mmap.c b/kernel_module/user/mmap.c index 901b0f9..b864479 100644 --- a/kernel_module/user/mmap.c +++ b/kernel_module/user/mmap.c @@ -16,6 +16,7 @@ int main(int argc, char **argv) printf("Usage: %s \n", argv[0]); return EXIT_FAILURE; } + printf("open pathname = %s\n", argv[1]); fd = open(argv[1], O_RDWR | O_SYNC); if (fd < 0) { perror("open");