歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux設備驅動remap_pfn_range() 和remap_page_range()

LDD3的開發環境推薦的是2.6.10,安裝了RHEL4-update4,其內核版本為2.6.9.42,編譯mmap的程序時報錯:Unknown symbol remap_pfn_range

在網上查了下,應將上述報錯的函數改為remap_page_range,並且remap_page_range不再推薦使用了,兩個內核函數第二個參數定義不相同:

int remap_page_range(struct vm_area_struct *vma,
            unsigned long from, 
            unsigned long phys_addr, //
            unsigned long size, 
            pgprot_t prot)
			

int remap_pfn_range(struct vm_area_struct *vma,
            unsigned long addr,
            unsigned long pfn,		//注意和上述的不同
            unsigned long size,
            pgprot_t prot)
直接看下面的一段程序,在注釋中已經將兩個函數的使用區分出來了。使用前,先mknod /dev/mymap c 60 0

#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.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>

#define DEVICE_NAME "mymap"

static unsigned char array[10]={0,1,2,3,4,5,6,7,8,9};
static unsigned char *buffer;

static int my_open(struct inode *inode, struct file *file)
{
    return 0;
}

static int my_map(struct file *filp, struct vm_area_struct *vma)
{    
    unsigned long page;
    unsigned char i;
    unsigned long start = (unsigned long)vma->vm_start;
    //unsigned long end =  (unsigned long)vma->vm_end;
    unsigned long size = (unsigned long)(vma->vm_end - vma->vm_start);

    //得到物理地址
    page = virt_to_phys(buffer);    
    //將用戶空間的一個vma虛擬內存區映射到以page開始的一段連續物理頁面上
    //if(remap_pfn_range(vma,start,page>>PAGE_SHIFT,size,PAGE_SHARED))//第三個參數是頁幀號,由物理地址右移PAGE_SHIFT得到
	if(remap_page_range(vma,start,page,size,PAGE_SHARED))//第三個參數是頁幀號,由物理地址右移PAGE_SHIFT得到	
        return -1;

    //往該內存寫10字節數據
    for(i=0;i<10;i++)
        buffer[i] = array[i];
    
    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);
	
	
	ret = register_chrdev(60, DEVICE_NAME, &dev_fops);
    if (ret < 0) 
	{
            printk("<1> memory: can't obtain major number %d\n", 60);
            return ret;
    }

	
    //內存分配
    buffer = (unsigned char *)kmalloc(PAGE_SIZE,GFP_KERNEL);
    //將該段內存設置為保留
    SetPageReserved(virt_to_page(buffer));

    return ret;
}

static void __exit dev_exit(void)
{
    //注銷設備
    //misc_deregister(&misc);
	 unregister_chrdev(60, DEVICE_NAME);
	 
	 
    //清除保留
    ClearPageReserved(virt_to_page(buffer));
    //釋放內存
    kfree(buffer);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("Dual BSD/GPL");
應用程序:

#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 fd;
    int i;
    unsigned char *p_map;
    
    //打開設備
    fd = open("/dev/mymap",O_RDWR);
    if(fd < 0)
    {
        printf("open fail\n");
        exit(1);
    }

    //內存映射
    p_map = (unsigned char *)mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0);
    if(p_map == MAP_FAILED)
    {
        printf("mmap fail\n");
        goto here;
    }

    //打印映射後的內存中的前10個字節內容
    for(i=0;i<10;i++)
        printf("%d\n",p_map[i]);
    

here:
    munmap(p_map, PAGE_SIZE);
    return 0;
}
makefile文件:

obj-m := testdrv.o
PWD       := $(shell pwd)
all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
	rm -rf *.o *~ core .*.cmd *.mod.c ./tmp_version

Copyright © Linux教程網 All Rights Reserved