我们可以在同一个/dev文件上两个MMAP吗 [英] Can we TWO MMAP on same /dev file

查看:131
本文介绍了我们可以在同一个/dev文件上两个MMAP吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我已经构建了一个字符驱动程序,其中使用dma_alloc_coherent()

现在,我正在传递这些BUFFER的物理地址[src_ptr&使用ioctl()作为source_offsetdest_offset到用户空间.

Now i am passing the PHYSICAL address of these BUFFER [src_ptr & dest_ptr] to user space using ioctl() as source_offset and dest_offset .

在用户空间中,此函数集用作mmap调用的偏移量. 因此,在同一个/dev文件中,说/dev/250我正在进行两个MMAP调用

In user space this offeset is used as an offset for mmap call. So on same /dev file say /dev/250 i am making TWO MMAP call

usr_src_ptr= mmap(0,page_size, PROT_READ|PROT_WRITE, 
                               MAP_SHARED,dev_FD,
                               src_offset );    

if (usr_src_ptr == MAP_FAILED){
printf("USR[UPP]:SOURCE MMAP FAiled \n\n");
    close(dev_FD);
    exit(-1);   
}else{
    printf("USR[UPP]:SOURCE MMAP is %X..\n",usr_src_ptr);
}

usr_dest_ptr= mmap(0,page_size, PROT_READ|PROT_WRITE,MAP_SHARED,
                                dev_FD,dest_offset );       
if (usr_dest_ptr == MAP_FAILED){
     printf("USR[UPP]:DEST MMAP FAiled \n\n");
    close(dev_FD);
    exit(-1);   
}else{
    printf("USR[UPP]:DEST MMAP is %X..\n",usr_dest_ptr);
}

  • 我正在用户空间&中的user_src_ptr中写入0x77.印刷 用户空间中的user_src_ptr和内核空间中的dest_src_ptr. 我同时获得了用户空间和内核空间的正确数据

  • I am writing 0x77 in user_src_ptr in user space & printing user_src_ptr in user space and dest_src_ptr in KERNEL space . I get correct data for both user and kernel space

    我正在内核空间&中的dest_ptr中写入0x55.打印dest_ptr 在内核空间中,usr_dest_ptr在用户空间中. 现在对于这种情况,我得到了正确的数据,例如0X55在内核缓冲区中 dest_ptr,但目标的用户空间缓冲区始终 返回0x77.即我写入usr_src_ptr的数据.

    I am writng 0x55 in dest_ptr in kernel space & printing dest_ptr in kernel space and usr_dest_ptr in user space. Now for this case I get the correct data e.g. 0X55 in kernel buffer dest_ptr, but the user space buffer of destination always return 0x77 . i.e the data which I have written into usr_src_ptr.

    任何人都可以让我知道我们是否可以在不同偏移量的同一个文件上执行两个mmap()操作吗?

    Can any one please let me know if we can do two mmap() operations on same file at different OFFSET?

    代替:

    if ((ret = remap_pfn_range(vma,vma->vm_start,
                              (virt_to_phys((void *)src_ptr) >> PAGE_SHIFT),
                              size,vma->vm_page_prot)) < 0)  return ret;  
    

    正确的是:

    if ((ret = remap_pfn_range(vma,vma->vm_start,
                               vma->vm_pgoff,
                               size,vma->vm_page_prot)) < 0)
    

    感谢BЈовић的输入....

    Thanks BЈовић for your input ....

    推荐答案

    是的,正如您已经在示例中发现的那样,您可以在同一文件中以相同的偏移量对同一个文件执行mmap(). mmap(2)的手册页中没有提到偏移量限制(除非它有设为sysconf(_SC_PAGE_SIZE)返回的页面大小的倍数,当然,它不应超出文件范围.

    Yes, as you already found out with your example, you can do mmap() on the same file with different offsets in the same process. The man page for mmap(2) says nothing about the offset limits (except for it has to be a multiple of the page size as returned by sysconf(_SC_PAGE_SIZE), and off course it shouldn't go beyond the file).

    只有一个限制:当mmap()-ing在一个过程中使用相同的文件,则必须使用相同的文件ID.您是在示例中执行此操作的,这就是为什么它可以正常工作的原因.

    There is only one limit : when mmap()-ing the same file in one process, you have to use the same file ID. You did this in your example, and that's is why it works fine.

    这篇关于我们可以在同一个/dev文件上两个MMAP吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆