基于内存映射的设备驱动程序
通过添加内核模块实现一个基于内存映射的杂项设备驱动程序。
拓展:
该模块只实现了显示内存映射区域信息的功能,而且该信息是固定;
拓展部分实现了将当前进程在内存映射后的vma区域的信息显示出来。
编译时:
$make
$sudo insmod miscdev_map.ko
$sudo chmod a+rw /dev/mymap
$gcc -o miscdev_maptest miscdev_maptest.c
$./miscdev_maptest
//miscdev_map.c
// 通过内存空间映射,实现设备驱动程序与用户程序的通信
// 注意: 杂项设备不需要再显示地使用mknod创建设备节点
//测试使用miscdev_maptest.c程序
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/gpio.h>
#include <linux/module.h> // for init_module()
#include <linux/proc_fs.h> // for create_proc_read_entry()
#include <linux/seq_file.h> // for sequence files
#include <linux/mm.h> // for 'struct vm_area_struct'
#include <linux/sched.h> // for 'struct task_struct'
#include <linux/mount.h>
#include <linux/dcache.h>
#include <linux/string.h>
#define DEVICE_NAME "mymap"
#define DEVICE_INFO "I am the devive mymap, this is my test output"
static unsigned char *buffer;
static int my_open(struct inode *inode, struct file *file)
{
//return seq_open(file, &my_seq_fops); //没什么用
return 0;
}
static int my_map(struct file *filp, struct vm_area_struct *vma)
{
unsigned long start = (unsigned long)vma->vm_start;
unsigned long size = (unsigned long)(vma->vm_end - vma->vm_start);
vma->vm_flags |= VM_IO;
vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP); //VM_RESERVED
//得到物理地址
page = virt_to_phys(buffer);
//将用户空间的一个vma虚拟内存区映射到以page开始的一段连续物理页面上
if(remap_pfn_range(vma,start,page>>PAGE_SHIFT,size,PAGE_SHARED))//第三个参数是页帧号,由物理地址右移PAGE_SHIFT得到
return -1;
//往该内存写数据
sprintf(buffer, "%s,vm_start=%08lX,vm_end=%08lX,%c%c%c%c", DEVICE_INFO, vma->vm_start, vma->vm_end ,(( vma->vm_flags & VM_READ ) ? 'r' : '-'),(( vma->vm_flags & VM_WRITE ) ? 'w' : '-'),(( vma->vm_flags & VM_EXEC ) ? 'x' : '-'),(( vma->vm_flags & VM_SHARED ) ? 's' : 'p'));
return 0;
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open = my_open,
.mmap = my_map,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
ret = misc_register(&misc); //注册混杂设备
buffer = (unsigned char *)kmalloc(PAGE_SIZE,GFP_KERNEL);//内存分配
SetPageReserved(virt_to_page(buffer));//将该段内存设置为保留
return ret;
}
static void __exit dev_exit(void)
{
//注销设备
misc_deregister(&misc);
//清除保留
ClearPageReserved(virt_to_page(buffer));
//释放内存
kfree(buffer);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("LKN@SCUT");
/*miscdev_maptest.c
本程序用于测试开发的miscdev_map内核模块,必须在miscdev_map内核模块编译、添加并修改设备权限后运行。。
程序编译命令: $ gcc -o miscdev_maptest miscdev_maptest.c
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#define PAGE_SIZE 4096
int main(int argc , char *argv[])
{
int fd1;
unsigned char *p_map;
//打开设备
fd1 = open("/dev/mymap",O_RDWR);
if(fd1 < 0)
{
printf("open fail\n");
exit(1);
}
//内存映射
p_map = (unsigned char *)mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,fd1, 0);
if(p_map == MAP_FAILED)
{
printf("mmap fail\n");
goto out;
}
//打印映射后的内存中的内容
printf("%s\n",p_map);
//sleep(7);
out:
munmap(p_map, PAGE_SIZE);
return 0;
}
/*Makefile*/
ifneq ($(KERNELRELEASE),)
obj-m := miscdev_map.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers
endif