-
Notifications
You must be signed in to change notification settings - Fork 1
/
drv_vma_fault.c
198 lines (162 loc) · 4.24 KB
/
drv_vma_fault.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#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;
#define ALLOC_PAGE_COUNT 16
static struct page *data_pages[ALLOC_PAGE_COUNT];
void simple_vma_open(struct vm_area_struct *vma)
{
printk(KERN_NOTICE "Simple VMA open, virt 0x%lx\n",
vma->vm_start);
}
void simple_vma_close(struct vm_area_struct *vma)
{
printk(KERN_NOTICE "Simple VMA close, virt 0x%lx\n",
vma->vm_start);
}
const char *simple_vma_name(struct vm_area_struct *vma)
{
return "SIMPLE_VMA_NAME";
}
int simple_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
size_t size = vma->vm_end - vma->vm_start;
int i;
int ret;
struct page *map_page;
char *ptr;
printk(KERN_NOTICE "vmf:fault=%p flag=%x offset=%lx\n",
vmf->virtual_address,
vmf->flags,
vmf->pgoff);
printk(KERN_NOTICE "vma:start=0x%lx size=%x pgoff=%lx\n",
vma->vm_start,
(int)size,
vma->vm_pgoff);
vma->vm_flags |= VM_MIXEDMAP;
map_page = data_pages[vmf->pgoff];
ret = vm_insert_mixed(vma,
(unsigned long)vmf->virtual_address,
page_to_pfn(map_page));
printk(KERN_NOTICE "ret=%d pfn=%x vaddr=0x%lx cnt=%d\n",
ret,
(int)page_to_pfn(map_page),
(unsigned long)vmf->virtual_address,
page_count(map_page));
/*
* Example of the "Direct I/O"
*/
ptr = kmap_atomic(map_page);
for (i = 0; i < 100; i++) {
ptr[i] = (unsigned char)vmf->pgoff;
}
kunmap_atomic(ptr);
SetPageDirty(map_page);
return VM_FAULT_NOPAGE;
}
static struct vm_operations_struct my_mem_vm_ops = {
.open = simple_vma_open,
.close = simple_vma_close,
.name = simple_vma_name,
.fault = simple_vma_fault,
};
static int open_mem(struct inode *inode, struct file *filp)
{
int i;
for (i = 0; i < ALLOC_PAGE_COUNT; i++) {
data_pages[i] = alloc_page(GFP_KERNEL);
if (data_pages[i])
printk(KERN_NOTICE "alloc: pfn=%x %d\n",
(int)page_to_pfn(data_pages[i]),
page_count(data_pages[i]));
else
printk(KERN_NOTICE "fail to alloc pages\n");
}
return 0;
}
static int release_mem(struct inode *inode, struct file *filp)
{
int i;
for (i= 0; i < ALLOC_PAGE_COUNT; i++) {
printk(KERN_NOTICE "free-page:%x %d\n",
(int)page_to_pfn(data_pages[i]), page_count(data_pages[i]));
__free_page(data_pages[i]);
}
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)
{
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,
.release = release_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);