蓝色天空

电子,图形,编程,游戏等分享和记录

0%

程序和内核交流的几种方式

这里写了一个示例,实现了程序和内核交流的几种方式。

主要做了这些事情:在内核中增加一个新的系统调用,专门申请一块物理内存;实现一个内核模块,映射物理内存到用户层。实现用户层应用程序调用新增的系统调用,传入物理内存的大小和要写入的文件路径等参数,并将指定内存中的内容回写到本地磁盘。

内核模块

内核模块主要实现了ioctl , read , write , mmap 等接口

内核模块代码

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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
chardevice.h
#ifndef CHARDEV_H
#define CHARDEV_H

#include <linux/ioctl.h>

#define MAX_SIZE 0x10000 /* max size (64kb) mmaped to userspace */
#define DEVICE_NAME "chardevice"
#define CLASS_NAME "ldj"
#define DEVICE_FILENAME "/dev/chardevice"
#define MAJOR_NUM 100

#define IOCTL_ALLOC_BUFFER _IOWR(MAJOR_NUM, 0, unsigned int)
#define IOCTL_SET_DATA _IOW(MAJOR_NUM, 1, char*)
#define IOCTL_GET_DATA _IOR(MAJOR_NUM, 2, char*)

#endif


chardevice.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
#include <asm/errno.h>
#include "chardevice.h"

static struct class* class;
static struct device* device;
static char *sh_mem = NULL;

//打开文件
static int chardevice_open(struct inode *inodep, struct file *filep)
{
try_module_get(THIS_MODULE);
pr_info("chardevice: Device opened\n");

return 0;
}

//关闭文件
static int chardevice_release(struct inode *inodep, struct file *filep)
{
pr_info("chardevice: Device closed\n");

module_put(THIS_MODULE);
return 0;
}

//读取文件
static ssize_t chardevice_read(struct file *filep, char *buffer, size_t len, loff_t *offset)
{
int ret;

if (len > MAX_SIZE) {
pr_info("read overflow!\n");
ret = -EFAULT;
goto out;
}

if (copy_to_user(buffer, sh_mem, len) == 0) {
pr_info("chardevice: copy %lu char to the user\n", len);
ret = len;
} else {
ret = -EFAULT;
}

out:
return ret;
}

//写入文件
static ssize_t chardevice_write(struct file *filep, const char *buffer, size_t len, loff_t *offset)
{
int ret;

if (copy_from_user(sh_mem, buffer, len)) {
pr_err("chardevice: write fault!\n");
ret = -EFAULT;
goto out;
}
pr_info("chardevice: copy %lu char from the user\n", len);
ret = len;

out:
return ret;
}

//映射文件
static int chardevice_mmap(struct file *filp, struct vm_area_struct *vma)
{
int ret = 0;
struct page *page = NULL;
unsigned long size = (unsigned long)(vma->vm_end - vma->vm_start);

if(!sh_mem){
return -ENOMEM;
}

if (size > MAX_SIZE) {
ret = -EINVAL;
goto out;
}

page = virt_to_page((unsigned long)sh_mem + (vma->vm_pgoff << PAGE_SHIFT));
ret = remap_pfn_range(vma, vma->vm_start, page_to_pfn(page), size, vma->vm_page_prot);
if (ret != 0) {
goto out;
}

out:
return ret;
}

//控制文件
static long chardevice_ioctl(struct file *file, /* ditto */
unsigned int ioctl_num, /* number and param for ioctl */
unsigned long ioctl_param)
{
int i;
long ret = 0;
char __user *up;
char *kp;

switch (ioctl_num) {
//申请指定大小的内存空间
case IOCTL_ALLOC_BUFFER:
if(sh_mem){
kvfree(sh_mem);
}

if(ioctl_param > MAX_SIZE){
pr_err("chardevice: alloc buffer size is over than max : %u", MAX_SIZE );
}

sh_mem = kvmalloc(ioctl_param, GFP_KERNEL);
if (sh_mem == NULL) {
pr_info("chardevice: kvmalloc fail \n");
ret = -ENOMEM;
}

break;

//将用户态数据存储到刚申请的内存空间中
case IOCTL_SET_DATA:
up = (char __user *)ioctl_param;
kp = sh_mem;

get_user(kp[0], up);
for (i = 0; kp && i < MAX_SIZE; i++){
get_user(kp[i], up++);
}

break;

//刚申请的内存空间中数据导出到用户态空间
case IOCTL_GET_DATA:
up = (char __user *)ioctl_param;
kp = sh_mem;

for (i = 0; kp && i < MAX_SIZE; i++){
put_user(kp[i], up++);
}

put_user('\0', up);
break;
}

return ret;
}

static const struct file_operations chardevice_fops = {
.open = chardevice_open,
.read = chardevice_read,
.write = chardevice_write,
.release = chardevice_release,
.mmap = chardevice_mmap,
.unlocked_ioctl = chardevice_ioctl,
.owner = THIS_MODULE,
};

static int __init chardevice_init(void)
{
int ret = 0;
pr_info("chardevice: =========================== \n");
pr_info("chardevice: registing! \n");

ret = register_chrdev(MAJOR_NUM, DEVICE_NAME, &chardevice_fops);

if (ret < 0) {
pr_info("chardevice: fail to register_chrdev! \n");
goto out;
}

class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(class)){
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
pr_info("mchar: failed to register device class");
ret = PTR_ERR(class);
goto out;
}

device = device_create(class, NULL, MKDEV(MAJOR_NUM, 0), NULL, DEVICE_NAME);
if (IS_ERR(device)) {
class_destroy(class);
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
ret = PTR_ERR(device);
goto out;
}


pr_info("chardevice: sprintf \n");
out:
return ret;
}

static void __exit chardevice_exit(void)
{
device_destroy(class, MKDEV(MAJOR_NUM, 0));
class_unregister(class);
class_destroy(class);
unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
kvfree(sh_mem);

pr_info("chardevice: unregistered! \n");
}

module_init(chardevice_init);
module_exit(chardevice_exit);
MODULE_LICENSE("GPL");

Makefile

obj-m += chardevice.o

PWD := $(CURDIR)

all:
make modules M=$(PWD) -C /lib/modules/$(shell uname -r)/build

clean:
make clean M=$(PWD) -C /lib/modules/$(shell uname -r)/build

内核模块编译和运行

内核模块编译

1
make -j`nproc`

内核模块运行

1
sudo insmod ./chardevice.ko

用户态程序

用户态程序代码

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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
test.c
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include "chardevice.h"

#define FILE_FILENAME "a.txt"

int ioctl_alloc_buffer(unsigned int len)
{
int fd_device, ret;

fd_device = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
if(fd_device < 0) {
printf("ioctl_alloc_buffer : fail to open %s \n", DEVICE_FILENAME);
return -1;
}

ret = ioctl(fd_device, IOCTL_ALLOC_BUFFER, len);

if (ret < 0) {
printf("ioctl_alloc_buffer : failed , ret = %d\n", ret);
}

printf("ioctl_alloc_buffer : success !\n");

close(fd_device);
return ret;
}

int ioctl_set_data(char *data)
{
int fd_device, ret;
char *p = NULL;
char buff[MAX_SIZE];

fd_device = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
if(fd_device < 0) {
printf("ioctl_set_data : fail to open %s \n", DEVICE_FILENAME);
return -1;
}

ret = ioctl(fd_device, IOCTL_SET_DATA, data);

if (ret < 0) {
printf("ioctl_set_data : failed , ret = %d\n", ret);
}

printf("ioctl_set_data : data = %s \n", data);

close(fd_device);
return ret;
}

int ioctl_get_data()
{
int fd_device, ret;
char *p = NULL;
char buff[MAX_SIZE];

fd_device = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
if(fd_device < 0) {
printf("ioctl_get_data : fail to open %s \n", DEVICE_FILENAME);
return -1;
}

ret = ioctl(fd_device, IOCTL_GET_DATA, buff);

if (ret < 0) {
printf("ioctl_get_data : failed , ret = %d\n", ret);
}

printf("ioctl_get_data : data = %s \n", buff);

close(fd_device);
return ret;
}

int file_mmap_write(char *data, unsigned int data_len)
{
int fd_device, ret;
char *p = NULL;
char buff[MAX_SIZE];

if(data_len > MAX_SIZE){
printf("file_mmap_write : error! mmap size over than %u \n", MAX_SIZE);
return -1;
}

fd_device = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
if(fd_device < 0) {
printf("file_mmap_write : fail to open %s \n", DEVICE_FILENAME);
return -1;
}

p = (char*)mmap(0, data_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd_device, 0);
printf("file_mmap_write : writing txt to driver , data = %s \n", data);
sprintf(p, "%s", data);

munmap(p, data_len);
close(fd_device);
}

int file_mmap_read(unsigned int data_len)
{
int fd_device, ret;
char *p = NULL;
char buff[MAX_SIZE];

if(data_len > MAX_SIZE){
printf("file_mmap_write : error! mmap size over than %u \n", MAX_SIZE);
return -1;
}

fd_device = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
if(fd_device < 0) {
printf("file_mmap_write : fail to open %s \n", DEVICE_FILENAME);
return -1;
}

p = (char*)mmap(0, data_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd_device, 0);
printf("file_mmap_read : reading txt from driver , data = %s \n", p);

munmap(p, data_len);
close(fd_device);
}

int file_write(char *data)
{
int fd_device, ret;

fd_device = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
if(fd_device < 0) {
printf("file_write : fail to open %s \n", DEVICE_FILENAME);
return -1;
}

ret = write(fd_device, data, MAX_SIZE);
if(fd_device < 0) {
printf("file_write : fail to write %s \n", DEVICE_FILENAME);
return -1;
}

printf("file_write : writing txt to driver , data = %s \n", data);

close(fd_device);
}

int file_read()
{
int fd_device, ret;
char buff[MAX_SIZE];

fd_device = open(DEVICE_FILENAME, O_RDWR|O_NDELAY);
if(fd_device < 0) {
printf("file_read : fail to open %s \n", DEVICE_FILENAME);
return -1;
}

ret = read(fd_device, buff, MAX_SIZE);
printf("file_read : reading txt from driver , data = %s \n", buff);

close(fd_device);
}

int test()
{
int fd_file, ret;
char buff[MAX_SIZE];

//从 a.txt 读取数据存到 buff 中
fd_file = open(FILE_FILENAME, O_RDWR|O_NDELAY);
if(fd_file < 0) {
printf("fail to open %s \n", FILE_FILENAME);
return -1;
}

ret = read(fd_file, buff, MAX_SIZE);
if(ret < 0){
printf("fail to read %s \n", FILE_FILENAME);
return ret;
}
else if(ret == 0)
{
printf("%s is empty \n", FILE_FILENAME);
}
else
{
printf("a.txt content = %s \n", buff);
}

//ioctl 测试

//请求驱动申请一块 MAX_SIZE 大小的内核内存
ioctl_alloc_buffer(MAX_SIZE);

//向内核内存存入 buff 中的数据
ioctl_set_data(buff);

//从内核内存获取数据并打印出来
ioctl_get_data();

//mmap 测试

//mmap 内核内存到用户态内存,并存入 buff 中的数据
file_mmap_write(buff, MAX_SIZE);

//mmap 内核内存到用户态内存,获取数据并打印出来
file_mmap_read(MAX_SIZE);

//read write 测试
//写 buff 到驱动驱动申请的内核内存
file_write(buff);

//读内核内存并打印出来
file_read();

}

extern char *optarg;

int main(int argc, char *argv[])
{
int fd_file, ret;
unsigned int buff_size;
char buff[MAX_SIZE], *file_path;
char optStr[] = "l:f:h";
int c;

//解析命令行参数
while ((c = getopt(argc, argv, optStr)) != -1) {
switch (c) {
case 'l':
printf("set buffer size = %s\n", optarg);
buff_size = atol(optarg);
break;
case 'f':
file_path = optarg;
printf("select file path = %s\n", file_path);
break;
case 'h':
printf("test -l <set_buffer_size> -f <file_to_copy>\n");
break;
}
}

//请求驱动申请一块 MAX_SIZE 大小的内核内存
if(buff_size <= 0){
printf("test : the size of buff is invalied , buff_size = %u \n", buff_size);
return -1;
}
ioctl_alloc_buffer(buff_size);

//从 a.txt 读取数据存到 buff 中
fd_file = open(file_path, O_RDWR|O_NDELAY);
if(fd_file < 0) {
printf("fail to open %s \n", file_path);
return -1;
}

ret = read(fd_file, buff, buff_size);
if(ret < 0){
printf("fail to read %s \n", file_path);
return ret;
}
else if(ret == 0)
{
printf("%s is empty \n", file_path);
}
else
{
printf("%s content = %s \n", file_path, buff);
}

file_mmap_write(buff, buff_size);

file_mmap_read(buff_size);

return 0;
}

用户态程序代码编译和运行

用户态程序代码编译

1
gcc test.c -o test

用户态程序代码运行

1
2
cat  "春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。" > a.txt
sudo ./test -l 65536 -f a.txt

用户态程序代码运行结果

1
2
3
4
5
6
set buffer size = 65536
select file path = a.txt
ioctl_alloc_buffer : success !
a.txt content = 春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。
file_mmap_write : writing txt to driver , data = 春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。
file_mmap_read : reading txt from driver , data = 春眠不觉晓,处处闻啼鸟。夜来风雨声,花落知多少。