Skip to content

Commit 3d372de

Browse files
author
gigi
committed
udpate
1 parent 0949450 commit 3d372de

2 files changed

Lines changed: 80 additions & 69 deletions

File tree

example/kernel_user_space_interfaces_example/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
obj-m += gnKernel.o
1+
#obj-m += gnKernel.o
22
#obj-m += udpRecvCallback.o
33
#obj-m += udpSend.o
44
#obj-m += ana_packet.o
@@ -12,7 +12,7 @@ obj-m += gnKernel.o
1212
#obj-m += cdev.o
1313
#obj-m += ioctl.o
1414
#obj-m += signal_kernel.o
15-
#obj-m += mmap_simple_kernel.o
15+
obj-m += mmap_simple_kernel.o
1616
#obj-m += sysctl.o
1717

1818
all:

example/kernel_user_space_interfaces_example/mmap_simple_kernel.c

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,116 +3,127 @@
33
#include <linux/init.h>
44
#include <linux/fs.h>
55
#include <linux/debugfs.h>
6+
#include <linux/slab.h>
67

78
#include <linux/mm.h> /* mmap related stuff */
89

9-
struct dentry *file1;
10+
#define NOPAGE_SIGBUS (NULL)
11+
12+
struct dentry* file1;
1013

1114
struct mmap_info {
12-
char *data; /* the data */
13-
int reference; /* how many times it is mmapped */
15+
char* data; /* the data */
16+
int reference; /* how many times it is mmapped */
1417
};
1518

1619

1720
/* keep track of how many times it is mmapped */
1821

19-
void mmap_open(struct vm_area_struct *vma)
22+
void mmap_open(struct vm_area_struct* vma)
2023
{
21-
struct mmap_info *info = (struct mmap_info *)vma->vm_private_data;
22-
info->reference++;
24+
struct mmap_info* info = (struct mmap_info*)vma->vm_private_data;
25+
info->reference++;
2326
}
2427

25-
void mmap_close(struct vm_area_struct *vma)
28+
void mmap_close(struct vm_area_struct* vma)
2629
{
27-
struct mmap_info *info = (struct mmap_info *)vma->vm_private_data;
28-
info->reference--;
30+
struct mmap_info* info = (struct mmap_info*)vma->vm_private_data;
31+
info->reference--;
2932
}
3033

3134
/* nopage is called the first time a memory area is accessed which is not in memory,
3235
* it does the actual mapping between kernel and user space memory
3336
*/
34-
struct page *mmap_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
37+
struct page* mmap_nopage(struct vm_area_struct* vma, unsigned long address,
38+
int* type)
3539
{
36-
struct page *page;
37-
struct mmap_info *info;
38-
/* is the address valid? */
39-
if (address > vma->vm_end) {
40-
printk("invalid address\n");
41-
return NOPAGE_SIGBUS;
42-
}
43-
/* the data is in vma->vm_private_data */
44-
info = (struct mmap_info *)vma->vm_private_data;
45-
if (!info->data) {
46-
printk("no data\n");
47-
return NULL;
48-
}
49-
50-
/* get the page */
51-
page = virt_to_page(info->data);
52-
53-
/* increment the reference count of this page */
54-
get_page(page);
55-
/* type is the page fault type */
56-
if (type)
57-
*type = VM_FAULT_MINOR;
58-
59-
return page;
40+
struct page* page;
41+
struct mmap_info* info;
42+
43+
/* is the address valid? */
44+
if (address > vma->vm_end) {
45+
printk("invalid address\n");
46+
return NOPAGE_SIGBUS;
47+
}
48+
49+
/* the data is in vma->vm_private_data */
50+
info = (struct mmap_info*)vma->vm_private_data;
51+
52+
if (!info->data) {
53+
printk("no data\n");
54+
return NULL;
55+
}
56+
57+
/* get the page */
58+
page = virt_to_page(info->data);
59+
60+
/* increment the reference count of this page */
61+
get_page(page);
62+
63+
/* type is the page fault type */
64+
if (type) {
65+
*type = VM_FAULT_MINOR;
66+
}
67+
68+
return page;
6069
}
6170

6271
struct vm_operations_struct mmap_vm_ops = {
63-
.open = mmap_open,
64-
.close = mmap_close,
65-
.nopage = mmap_nopage,
72+
.open = mmap_open,
73+
.close = mmap_close,
74+
//.nopage = mmap_nopage,
6675
};
6776

68-
int my_mmap(struct file *filp, struct vm_area_struct *vma)
77+
int my_mmap(struct file* filp, struct vm_area_struct* vma)
6978
{
70-
vma->vm_ops = &mmap_vm_ops;
71-
vma->vm_flags |= VM_RESERVED;
72-
/* assign the file private data to the vm private data */
73-
vma->vm_private_data = filp->private_data;
74-
mmap_open(vma);
75-
return 0;
79+
vma->vm_ops = &mmap_vm_ops;
80+
vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
81+
/* assign the file private data to the vm private data */
82+
vma->vm_private_data = filp->private_data;
83+
mmap_open(vma);
84+
return 0;
7685
}
7786

78-
int my_close(struct inode *inode, struct file *filp)
87+
int my_close(struct inode* inode, struct file* filp)
7988
{
80-
struct mmap_info *info = filp->private_data;
81-
/* obtain new memory */
82-
free_page((unsigned long)info->data);
83-
kfree(info);
84-
filp->private_data = NULL;
85-
return 0;
89+
struct mmap_info* info = filp->private_data;
90+
/* obtain new memory */
91+
free_page((unsigned long)info->data);
92+
kfree(info);
93+
filp->private_data = NULL;
94+
return 0;
8695
}
8796

88-
int my_open(struct inode *inode, struct file *filp)
97+
int my_open(struct inode* inode, struct file* filp)
8998
{
90-
struct mmap_info *info = kmalloc(sizeof(struct mmap_info), GFP_KERNEL);
91-
/* obtain new memory */
92-
info->data = (char *)get_zeroed_page(GFP_KERNEL);
93-
memcpy(info->data, "hello from kernel this is file: ", 32);
94-
memcpy(info->data + 32, filp->f_dentry->d_name.name, strlen(filp->f_dentry->d_name.name));
95-
/* assign this info struct to the file */
96-
filp->private_data = info;
97-
return 0;
99+
struct mmap_info* info = kmalloc(sizeof(struct mmap_info), GFP_KERNEL);
100+
/* obtain new memory */
101+
info->data = (char*)get_zeroed_page(GFP_KERNEL);
102+
memcpy(info->data, "hello from kernel this is file: ", 32);
103+
memcpy(info->data + 32, filp->f_path.dentry->d_name.name,
104+
strlen(filp->f_path.dentry->d_name.name));
105+
/* assign this info struct to the file */
106+
filp->private_data = info;
107+
return 0;
98108
}
99109

100110
static const struct file_operations my_fops = {
101-
.open = my_open,
102-
.release = my_close,
103-
.mmap = my_mmap,
111+
.open = my_open,
112+
.release = my_close,
113+
.mmap = my_mmap,
104114
};
105115

106116
static int __init mmapexample_module_init(void)
107117
{
108-
file1 = debugfs_create_file("mmap_example", 0644, NULL, NULL, &my_fops);
109-
return 0;
118+
printk("mmapexample_module_init\n");
119+
file1 = debugfs_create_file("mmap_example", 0644, NULL, NULL, &my_fops);
120+
return 0;
110121
}
111122

112123
static void __exit mmapexample_module_exit(void)
113124
{
114-
debugfs_remove(file1);
115-
125+
printk("mmapexample_module_exit\n");
126+
debugfs_remove(file1);
116127
}
117128

118129
module_init(mmapexample_module_init);

0 commit comments

Comments
 (0)