基于內存映射的設備驅動程序

基于內存映射的設備驅動程序

通過添加內核模塊實現一個基于內存映射的雜項設備驅動程序。


拓展:

該模塊只實現了顯示內存映射區域信息的功能,而且該信息是固定;

拓展部分實現了將當前進程在內存映射后的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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容