8000 添加X86下测试mmap,修改kernel, Makefile, test等 · home-coder/ring-buffer@ff389ec · GitHub
[go: up one dir, main page]

Skip to content

Commit ff389ec

Browse files
author
jiangxiujie
committed
添加X86下测试mmap,修改kernel, Makefile, test等
1 parent 14c5b91 commit ff389ec

File tree

3 files changed

+72
-21
lines changed

3 files changed

+72
-21
lines changed

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
#ARCH=arm
22
#与硬件相关,需要联调
3-
ARCH=x86
3+
#ARCH=x86org
4+
ARCH=x86mmap
5+
46
#$(info, $(ARCH))//内核 androidmk使用
57
ifeq ($(ARCH), arm)
68
CC = ~/basic/cross_compile/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-gcc
79
CFLAGS = -O2 -Wall --static
8-
else
10+
endif
11+
12+
ifeq ($(ARCH), x86mmap)
13+
CC = gcc
14+
CFLAGS = -O2 -Wall --static
15+
endif
16+
17+
ifeq ($(ARCH), x86org)
918
CC = gcc
1019
CFLAGS = -DORG_TEST -O2 -Wall --static
1120
endif

helper/kernel/pages.c

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
1-
//#include <linux/config.h>
21
#include <linux/module.h>
32
#include <linux/kernel.h>
43
#include <linux/mm.h>
54
#include <linux/delay.h>
5+
#include <linux/sched.h>//wake_up_process()
6+
#include <linux/kthread.h>//kthread_ceate(), kthread_run()
67

78
static unsigned long p = 0;
89
static unsigned long pp = 0;
910
int count = 0;
1011
char buf[32];
12+
static struct task_struct *mmap_task = NULL;
1113

12-
static int put_pages(void *p)
14+
/*循环的写数据*/
15+
static int put_pages(void *param)
1316
{
14-
while (1) {
15-
msleep(1);
16-
memset(buf, 0, 32);
17-
sprintf(buf, "hello %d", count++ % 1000);
18-
strcpy((char *)p, buf);
17+
while (!kthread_should_stop()) { //不要用while 1, 因为那样rmmod会出错
18+
msleep(1);
19+
memset(buf, 0, 32);
20+
sprintf(buf, "hello %d", count++ % 1000);
21+
if ((void *)p == NULL) {
22+
printk("kerr, ---->p is null\n");
23+
return 0;
24+
}
25+
26+
printk("put mmap -->%d\n", count);
27+
strcpy((char *)p, buf);
1928
}
29+
2030
return 0;
2131
}
2232

23-
static int __init shao_init(void)
33+
static int creat_work(void)
2434
{
25-
//分配共享内存(一个页面)
35+
int err;
36+
37+
mmap_task = kthread_run(put_pages, NULL, "mmap_task");
38+
if (IS_ERR(mmap_task)) {
39+
printk("create new kernel thread failed\n");
40+
err = PTR_ERR(mmap_task);
41+
mmap_task = NULL;
42+
return err;
43+
}
44+
45+
return 0;
46+
}
2647

48+
static int __init kfifo_mmap_init(void)
49+
{
50+
//分配共享内存(一个页面)
2751
p = __get_free_pages(GFP_KERNEL, 0);
2852

2953
SetPageReserved(virt_to_page(p));
@@ -32,21 +56,27 @@ static int __init shao_init(void)
3256
printk("<1> pp = 0x%lx\n", pp);
3357

3458
//在共享内存中写上一个字符串
35-
kernel_thread(put_pages, (void *)p, 0);
59+
// kernel_thread(put_pages, (void *)p, 0);
60+
61+
creat_work();
62+
3663
return 0;
3764
}
3865

39-
static void __exit shao_exit(void)
66+
static void __exit kfifo_mmap_exit(void)
4067
{
68+
printk("----module exit----\n");
69+
if (mmap_task) {
70+
kthread_stop(mmap_task);
71+
mmap_task = NULL;
72+
}
4173

4274
ClearPageReserved(virt_to_page(p));
43-
4475
free_pages(p, 0);
45-
4676
}
4777

4878
MODULE_LICENSE("GPL");
49-
MODULE_AUTHOR("Kuanish");
79+
MODULE_AUTHOR("home-coder");
5080
MODULE_DESCRIPTION("mmap test");
51-
module_init(shao_init);
52-
module_exit(shao_exit);
81+
module_init(kfifo_mmap_init);
82+
module_exit(kfifo_mmap_exit);

test.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,18 @@ void thread_writer(void *param)
8585
// usleep(100);
8686
}
8787
}
88+
8889
#else
8990
void thread_writer(void *param)
9091
{
9192
#define PAGE_SIZE (4*1024)
92-
#define PAGE_OFFSET 0xc0000000 //32位的偏移3G, 但是我在内核求得用户空间地址了,这个变量不需要了
93+
//#define PAGE_OFFSET 0xc0000000 //32位的偏移3G, 但是我在内核求得用户空间地址了,这个变量不需要了
9394
#define KERNEL_VIRT_ADDR 0x22049000 //此处地址即为内核模块打印的地址p,动态的不固定,需要自行修改
9495
unsigned char *buffer;
9596
int fd;
9697
unsigned long phy_addr;
9798
struct ll_param *p = (struct ll_param *)param;
99+
unsigned int klen = 0;
98100

99101
fd = open("/dev/mem", O_RDWR);
100102
if (fd == -1)
@@ -105,8 +107,18 @@ void thread_writer(void *param)
105107
if (buffer == MAP_FAILED)
106108
perror("mmap");
107109
while (1) {
108-
kfifo_put(p->fifo, buffer, 32); //strlen((char *)buffer)
109-
bzero(buffer, 32);
110+
pthread_mutex_lock(&qlock);
111+
112+
if (buffer[0] != '\0') {
113+
if (klen >= FIFO_LENGTH) {
114+
pthread_cond_wait(&q_not_full, &qlock);
115+
}
116+
kfifo_put(p->fifo, buffer, 32); //strlen((char *)buffer)
117+
bzero(buffer, 32);
118+
}
119+
120+
pthread_mutex_unlock(&qlock);
121+
pthread_cond_signal(&q_not_empty);
110122
usleep(10);
111123
}
112124

0 commit comments

Comments
 (0)
0