/
drv.c
146 lines (119 loc) · 3.22 KB
/
drv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/export.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/highmem.h>
MODULE_LICENSE("GPL");
static int major;
static int minor;
static struct class *cl;
static struct cdev my_cdev;
void simple_vma_open(struct vm_area_struct *vma)
{
printk(KERN_NOTICE "Simple VMA open, virt 0x%lx, phys 0x%lx\n",
vma->vm_start, vma->vm_pgoff << PAGE_SHIFT);
}
void simple_vma_close(struct vm_area_struct *vma)
{
printk(KERN_NOTICE "Simple VMA close, virt 0x%lx, phys 0x%lx\n",
vma->vm_start, vma->vm_pgoff << PAGE_SHIFT);
}
static struct vm_operations_struct my_mem_vm_ops = {
.open = simple_vma_open,
.close = simple_vma_close,
};
static int open_mem(struct inode *inode, struct file *filp)
{
printk("%s %d\n", __FUNCTION__, __LINE__);
return 0;
}
static ssize_t read_mem(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
unsigned long offset = *ppos;
printk("buf:%p count:%d ppos:%p p:%lx\n", buf, count, ppos, offset);
*ppos += count;
return 0;
}
static int mmap_mem(struct file *file, struct vm_area_struct *vma)
{
size_t size = vma->vm_end - vma->vm_start;
char *ptr_page;
int i;
struct page *mmap_page;
mmap_page = alloc_pages(GFP_KERNEL, get_order(size));
printk(KERN_NOTICE "mmap:pgoff=%x size=%d pfn=%x\n",
(int)vma->vm_pgoff, size, (int)page_to_pfn(mmap_page));
/* Remap-pfn-range will mark the range VM_IO */
if (remap_pfn_range(vma,
vma->vm_start,
page_to_pfn(mmap_page),
size,
vma->vm_page_prot)) {
return -EAGAIN;
}
ptr_page = kmap_atomic(mmap_page);
for (i = 0; i < 100; i++)
ptr_page[i] = (char)vma->vm_pgoff;
kunmap_atomic(ptr_page);
SetPageDirty(mmap_page); /* only the first page */
flush_dcache_page(mmap_page);
vma->vm_ops = &my_mem_vm_ops;
vma->vm_ops->open(vma);
return 0;
}
static const struct file_operations my_mem_fops = {
/* .llseek = memory_lseek, */
.read = read_mem,
/* .write = write_mem, */
.mmap = mmap_mem,
.open = open_mem,
/* .get_unmapped_area = get_unmapped_area_mem, */
};
static int __init my_mem_init(void)
{
int ret;
dev_t dev;
ret = alloc_chrdev_region(&dev, minor, 1, "my_mem");
if (ret < 0) {
printk(KERN_CRIT "fail to allocate chrdev region\n");
return ret;
}
major = MAJOR(dev);
printk("major=%d minor=%d\n", major, minor);
cl = class_create(THIS_MODULE, "my_mem_class");
if (IS_ERR(cl)) {
printk(KERN_CRIT "fail to class_create\n");
unregister_chrdev_region(dev, 1);
return -1;
}
cdev_init(&my_cdev, &my_mem_fops);
if (cdev_add(&my_cdev, dev, 1) == -1) {
printk(KERN_CRIT "fail to cdev_add\n");
class_destroy(cl);
unregister_chrdev_region(dev, 1);
return -1;
}
if (device_create(cl, NULL, MKDEV(major, minor), NULL, "my_mem0") == NULL) {
printk(KERN_CRIT " fail to create device\n");
class_destroy(cl);
unregister_chrdev_region(MKDEV(major, minor), 1);
cdev_del(&my_cdev);
return -1;
}
return 0;
}
static void __exit my_mem_exit(void)
{
dev_t dev = MKDEV(major, minor);
device_destroy(cl, dev);
cdev_del(&my_cdev);
class_destroy(cl);
unregister_chrdev_region(dev, 1);
}
module_init(my_mem_init);
module_exit(my_mem_exit);