8000 添加helper,处理mmap相关问题 · home-coder/ring-buffer@5a35b05 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a35b05

Browse files
committed
添加helper,处理mmap相关问题
1 parent fd84d2c commit 5a35b05

File tree

11 files changed

+109
-1
lines changed

11 files changed

+109
-1
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
export ARCH=arm
2+
CC = ~/basic/cross_compile/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-gcc
3+
#CC = gcc
14
CFLAGS = -O2 -Wall
25
INCLUDE = -I ./include
3-
CC = gcc
46

57
test:test.o kfifo.o
68
${CC} ${CFLAGS} test.o kfifo.o -o $@ ${INCLUDE} -pthread

helper/kernel/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export ARCH=arm
2+
export CROSS_COMPILE=/home/java/basic/cross_compile/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-
3+
LINUX_PATH :=/home/java/opt/hhzn/kernel_imx
4+
5+
obj-m += pages.o
6+
7+
all:
8+
make -C $(LINUX_PATH) M=`pwd` modules
9+
clean:
10+
make -C $(LINUX_PATH) M=`pwd` modules clean

helper/kernel/pages.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//#include <linux/config.h>
2+
#include <linux/module.h>
3+
#include <linux/kernel.h>
4+
#include <linux/mm.h>
5+
6+static unsigned long p = 0;
7+
8+
static int __init shao_init(void)
9+
{
10+
11+
//分配共享内存(一个页面)
12+
13+
p = __get_free_pages(GFP_KERNEL, 0);
14+
15+
SetPageReserved(virt_to_page(p));
16+
17+
printk("<1> p = 0x%08x\n", (unsigned int)p);
18+
19+
//在共享内存中写上一个字符串
20+
21+
strcpy((char *)p, "Hello world!\n");
22+
23+
return 0;
24+
25+
}
26+
27+
static void __exit shao_exit(void)
28+
{
29+
30+
ClearPageReserved(virt_to_page(p));
31+
32+
free_pages(p, 0);
33+
34+
}
35+
36+
MODULE_LICENSE("GPL");
37+
MODULE_AUTHOR("Kuanish");
38+
MODULE_DESCRIPTION("mmap test");
39+
module_init(shao_init);
40+
module_exit(shao_exit);

helper/linux/.mmap.c.swp

12 KB
Binary file not shown.

helper/linux/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export ARCH=arm
2+
CC = ~/basic/cross_compile/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-gcc
3+
#CC = gcc
4+
CFLAGS = -O2 -Wall --static
5+
INCLUDE = -I ./include
6+
7+
all:mmap hello
8+
9+
mmap:mmap.o
10+
${CC} ${CFLAGS} mmap.o -o $@
11+
rm -rf *.o
12+
hello:hello.o
13+
${CC} ${CFLAGS} hello.o -o $@
14+
15+
clean:
16+
rm -rf *.o mmap

helper/linux/hello

576 KB
Binary file not shown.

helper/linux/hello.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
printf("-----hello world----\n");
6+
return 0;
7+
}

helper/linux/hello.o

1.14 KB
Binary file not shown.

helper/linux/mmap

578 KB
Binary file not shown.

helper/linux/mmap.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <sys/mman.h>
2+
#include <sys/types.h>
3+
#include <sys/stat.h>
4+
#include <fcntl.h>
5+
#include <stdio.h>
6+
7+
#define PAGE_SIZE (4*1024)
8+
#define PAGE_OFFSET 0xc0000000
9+
#define KERNEL_VIRT_ADDR 0xd279c000 //此处地址即为内核模块打印的地址p,动态的不固定,需要自行修改
10+
11+
int main()
12+
{
13+
char *buf;
14+
int fd;
15+
unsigned long phy_addr;
16+
17+
fd = open("/dev/mem", O_RDWR);
18+
if (fd == -1)
19+
perror("open");
20+
21+
phy_addr = KERNEL_VIRT_ADDR - PAGE_OFFSET;
22+
buf = mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, phy_addr);
23+
if (buf == MAP_FAILED)
24+
perror("mmap");
25+
26+
puts(buf); //打印共享内存的内容
27+
28+
munmap(buf, PAGE_SIZE);
29+
close(fd);
30+
31+
return 0;
32+
33+
}

test

8.98 KB
Binary file not shown.

0 commit comments

Comments
 (0)
0