forked from martinezjavier/ldd3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.c
224 lines (184 loc) · 5.29 KB
/
simple.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Simple - REALLY simple memory mapping demonstration.
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*
* $Id: simple.c,v 1.12 2005/01/31 16:15:31 rubini Exp $
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/mm.h>
#include <linux/kdev_t.h>
#include <asm/page.h>
#include <linux/cdev.h>
#include <linux/version.h>
#include <linux/device.h>
static int simple_major = 0;
module_param(simple_major, int, 0);
MODULE_AUTHOR("Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");
/*
* Closing is just as simpler.
*/
static int simple_release(struct inode *inode, struct file *filp)
{
return 0;
}
/*
* Common VMA ops.
*/
void simple_vma_open(struct vm_area_struct *vma)
{
printk(KERN_NOTICE "Simple VMA open, virt %lx, phys %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.\n");
}
/*
* The remap_pfn_range version of mmap. This one is heavily borrowed
* from drivers/char/mem.c.
*/
static struct vm_operations_struct simple_remap_vm_ops = {
.open = simple_vma_open,
.close = simple_vma_close,
};
static int simple_remap_mmap(struct file *filp, struct vm_area_struct *vma)
{
if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
vma->vm_end - vma->vm_start,
vma->vm_page_prot))
return -EAGAIN;
vma->vm_ops = &simple_remap_vm_ops;
simple_vma_open(vma);
return 0;
}
/*
* The nopage version.
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,17,0)
typedef int vm_fault_t;
#endif
static vm_fault_t simple_vma_nopage(struct vm_fault *vmf)
{
struct page *pageptr;
struct vm_area_struct *vma = vmf->vma;
unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
unsigned long physaddr = (unsigned long) vmf->address - vma->vm_start + offset;
unsigned long pageframe = physaddr >> PAGE_SHIFT;
// Eventually remove these printks
printk (KERN_NOTICE "---- Nopage, off %lx phys %lx\n", offset, physaddr);
printk (KERN_NOTICE "VA is %p\n", __va (physaddr));
printk (KERN_NOTICE "Page at %p\n", virt_to_page (__va (physaddr)));
if (!pfn_valid(pageframe))
return VM_FAULT_SIGBUS;
pageptr = pfn_to_page(pageframe);
printk (KERN_NOTICE "page->index = %ld mapping %p\n", pageptr->index, pageptr->mapping);
printk (KERN_NOTICE "Page frame %ld\n", pageframe);
get_page(pageptr);
vmf->page = pageptr;
return 0;
}
static struct vm_operations_struct simple_nopage_vm_ops = {
.open = simple_vma_open,
.close = simple_vma_close,
.fault = simple_vma_nopage,
};
static int simple_nopage_mmap(struct file *filp, struct vm_area_struct *vma)
{
vma->vm_ops = &simple_nopage_vm_ops;
simple_vma_open(vma);
return 0;
}
/*
* Set up the cdev structure for a device.
*/
static void simple_setup_cdev(struct cdev *dev, int minor,
struct file_operations *fops)
{
int err, devno = MKDEV(simple_major, minor);
cdev_init(dev, fops);
dev->owner = THIS_MODULE;
err = cdev_add (dev, devno, 1);
/* Fail gracefully if need be */
if (err)
printk (KERN_NOTICE "Error %d adding simple%d", err, minor);
}
/*
* Our various sub-devices.
*/
/* Device 0 uses remap_pfn_range */
static struct file_operations simple_remap_ops = {
.owner = THIS_MODULE,
.open = simple_open,
.release = simple_release,
.mmap = simple_remap_mmap,
};
/* Device 1 uses nopage */
static struct file_operations simple_nopage_ops = {
.owner = THIS_MODULE,
.open = simple_open,
.release = simple_release,
.mmap = simple_nopage_mmap,
};
#define MAX_SIMPLE_DEV 2
#if 0
static struct file_operations *simple_fops[MAX_SIMPLE_DEV] = {
&simple_remap_ops,
&simple_nopage_ops,
};
#endif
/*
* We export two simple devices. There's no need for us to maintain any
* special housekeeping info, so we just deal with raw cdevs.
*/
static struct cdev SimpleDevs[MAX_SIMPLE_DEV];
/*
* Module housekeeping.
*/
static int simple_init(void)
{
int result;
dev_t dev = MKDEV(simple_major, 0);
/* Figure out our device number. */
if (simple_major)
result = register_chrdev_region(dev, 2, "simple");
else {
result = alloc_chrdev_region(&dev, 0, 2, "simple");
simple_major = MAJOR(dev);
}
if (result < 0) {
printk(KERN_WARNING "simple: unable to get major %d\n", simple_major);
return result;
}
if (simple_major == 0)
simple_major = result;
/* Now set up two cdevs. */
simple_setup_cdev(SimpleDevs, 0, &simple_remap_ops);
simple_setup_cdev(SimpleDevs + 1, 1, &simple_nopage_ops);
return 0;
}
static void simple_cleanup(void)
{
cdev_del(SimpleDevs);
cdev_del(SimpleDevs + 1);
unregister_chrdev_region(MKDEV(simple_major, 0), 2);
}
module_init(simple_init);
module_exit(simple_cleanup);