在内核驱动程序中,为什么mmap不能在procfs中工作?

时间:2016-03-22 10:58:32

标签: linux-kernel linux-device-driver embedded-linux

我实现了mmap函数,并将其挂载到文件操作。 并在/ proc中创建一个文件。 但是当我insmod时,它会响应" mmap_example2:未知符号_page_cachable_default insmod:不能在模块中插入' mmap_example2.ko':未知符号,或未知参数"

当我从文件操作中删除mmap函数时,可以插入它。 所以我忽略了什么?如何让mmap在procfs中工作?

代码在

之下
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/vmalloc.h>

#define FILE_NAME "test"

typedef enum ALLOC_TYPE
{
    KMALLOC = 0, VMALLOC, MAX_ALLOC_TYPE,
} eAllocType;

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

static int file_open(struct inode *pInode, struct file *pFile)
{
    printk("%s\n", __FUNCTION__);
    return 0;
}

static int file_release(struct inode *pInode, struct file *pFile)
{
    printk("%s\n", __FUNCTION__);
    return 0;
}

static int file_mmap(struct file *pFile, struct vm_area_struct* pVMA)
{
    unsigned long page;
    unsigned char i;
    unsigned long start = (unsigned long)pVMA->vm_start;
    unsigned long size = (unsigned long)(pVMA->vm_end - pVMA->vm_start);

    page = virt_to_phys(buffer);

    if(remap_pfn_range(pVMA,start,page>>PAGE_SHIFT,size,PAGE_SHARED))
    return -1;

     for(i=0;i<10;i++)
         buffer[i] = array[i];
     return 0;
}

struct file_operations file_ops =
{
    .open       = file_open,
    .release    = file_release,
    .mmap       = file_mmap,
};

static int mmap_example2_init(void)
{
    struct proc_dir_entry* entry = NULL;
    printk("%s init\n", __FUNCTION__);
    if(!(entry = create_proc_entry(FILE_NAME,0666,NULL)))
    {
    printk("%s fail to create proc file\n",__FUNCTION__);
    return -EINVAL;
    }
    entry->proc_fops = &file_ops;
    buffer = kmalloc(10,GFP_KERNEL);
    if (!buffer)
    {
    printk("allocate mem error\n");
    return -1;
    }
    SetPageReserved(virt_to_page(buffer));
    return 0;
}

static void mmap_example2_exit(void)
{
    printk("%s exit\n", __FUNCTION__);
    remove_proc_entry(FILE_NAME,NULL);
    ClearPageReserved(virt_to_page(buffer));
    kfree(buffer);
}

module_init(mmap_example2_init);
module_exit(mmap_example2_exit);

1 个答案:

答案 0 :(得分:0)

要添加file_operations,请使用proc_create而不是create_proc_entry并将其传递给file_operation对象

static struct file_operations myops = 
{
    .read = myread,
    .mmap = mymmap,
};
static int simple_init(void)
{
    ent=proc_create("mytest",0660,NULL,&myops);
    printk(KERN_ALERT "hello, module %d...\n",irq);
    return 0;
}