2012年3月25日 Sunday
1、内核版本号2.6.33.20
2、内存映射中mmap()的参数传递
3、驱动模块源文件proc_dev.c
- #include <linux/init.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/errno.h>
- #include <linux/types.h>
- #include <linux/fcntl.h>
- #include <linux/vmalloc.h>
- #include <asm/uaccess.h>
- #include <asm/io.h>
- #include <asm/page.h>
- #include <linux/mm.h>
- #define MMAPCALL_DEV_NAME "mmapcall"
- #define MMAPCALL_DEV_MAJOR 240
- int mmapcall_open(struct inode *inode,struct file *filp)
- {
- return 0;
- }
- int mmapcall_release(struct inode *inode,struct file *filp)
- {
- return 0;
- }
- int mmapcall_mmap(struct file *filp,struct vm_area_struct *vma)
- {
- printk("vm_pgoff [%08X]\n",vma->vm_pgoff<<PAGE_SHIFT);
- printk("vm_start [%08X]\n",vma->vm_start);
- printk("vm_end [%08X]\n",vma->vm_end);
- printk("vm_flags [%08X]\n",vma->vm_flags);
-
- return -EAGAIN;
- }
- struct file_operations mmapcall_fops={
- .owner=THIS_MODULE,
- .open=mmapcall_open,
- .release=mmapcall_release,
- .mmap=mmapcall_mmap,
- };
- int mmapcall_init(void)
- {
- int result;
-
- result=register_chrdev(MMAPCALL_DEV_MAJOR,MMAPCALL_DEV_NAME,&mmapcall_fops);
-
- if(result<0){
- return result;
- }
- return 0;
- }
- void mmapcall_exit(void)
- {
- unregister_chrdev(MMAPCALL_DEV_MAJOR,MMAPCALL_DEV_NAME);
- }
- module_init(mmapcall_init);
- module_exit(mmapcall_exit);
- MODULE_LICENSE("Dual BSD/GPL");
4、Makefile
- obj-m += mmapcall_dev.o
- CURRENT_PATH := $(shell pwd)
- LINUX_KERNEL := $(shell uname -r)
- LINUX_KERNEL_PATH := /usr/src/linux-$(LINUX_KERNEL)
- all:
- make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
- clean:
- #make -C $(LINUX_KERNEL_PAHT) M=$(CURRENT_PATH) clean
- rm *.mod.*
- rm *.order
- rm *.o
- rm *.symvers
5、应用程序源文件call_dev.c
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/ioctl.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #define DEVICE_FILENAME "/dev/mmapcall"
- #define MMAP_SIZE 0x1000
- int main()
- {
- int dev;
- int loop;
- char *ptrdata;
-
- dev=open(DEVICE_FILENAME,O_RDWR|O_NDELAY);
-
- if(dev>=0){
- ptrdata=(char*)mmap(0x12345000,
- MMAP_SIZE,
- PROT_READ|PROT_WRITE,
- MAP_SHARED,
- dev,
- 0x87654000);
- munmap(ptrdata,MMAP_SIZE);
-
- close(dev);
- }
- return 0;
-
- }
6、实施方法及运行结果
- ~/mmap_params# mknod /dev/mmapcall c 240 0
- ~/mmap_params# ls -la /dev/mmapcall | grep mmapcall
- crw-r--r-- 1 root root 240, 0 2012-03-25 12:38 /dev/mmapcall
- ~/mmap_params# make
- ~/mmap_params# gcc mmapcall_app.c -o mmapcall_app
- ~/mmap_params# insmod mmapcall_dev.ko
- ~/mmap_params# ./mmapcall_app
- ~/mmap_params# dmesg
- [ 8354.352485] vm_pgoff [87654000]
- [ 8354.352497] vm_start [12345000]
- [ 8354.352500] vm_end [12346000]
- [ 8354.352502] vm_flags [000000FB]
7、移除模块及设备文件
- ~/mmap_params# rm /dev/mmapcall
- ~/mmap_params# rmmod mmapcall_dev
阅读(1636) | 评论(0) | 转发(0) |