当前位置: 技术问答>linux和unix
mmap函数如何使用?
来源: 互联网 发布时间:2016-04-28
本文导语: 内核层: unsigned int map_start; int DEMO_mmap(struct file *filp,struct vm_area_struct *vma) { vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); vma->vm_pgoff = ( (u32)map_start >> PAGE_SHIFT); ...
内核层:
unsigned int map_start;
int DEMO_mmap(struct file *filp,struct vm_area_struct *vma)
{
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
vma->vm_pgoff = ( (u32)map_start >> PAGE_SHIFT);
if (remap_pfn_range(vma,vma->vm_start,vma->vm_pgoff,vma->vm_end-vma->vm_start,vma->vm_page_prot))
return -EAGAIN;
return 0;
}
struct file_operations DEMO_fops = {
.owner = THIS_MODULE,
.llseek = DEMO_llseek,
.read = DEMO_read,
.write = DEMO_write,
.ioctl = DEMO_ioctl,
.open = DEMO_open,
.release = DEMO_release,
.mmap = DEMO_mmap,
};
应运层:
unsigned char *start;
unsigned char *buf="genghaifei";
fd=open("/dev/camera",O_RDWR);
if(fd==-1)
{
perror("error openn");
exit(-1);
}
start=(unsigned char*)mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
memcpy(start,buf,10);
请问这样是不是把buf的内容copy到内核空间了?如果是的话,copy到内核的那个位置处?内核里面的map_start到底有什么用?谢谢!
unsigned int map_start;
int DEMO_mmap(struct file *filp,struct vm_area_struct *vma)
{
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
vma->vm_pgoff = ( (u32)map_start >> PAGE_SHIFT);
if (remap_pfn_range(vma,vma->vm_start,vma->vm_pgoff,vma->vm_end-vma->vm_start,vma->vm_page_prot))
return -EAGAIN;
return 0;
}
struct file_operations DEMO_fops = {
.owner = THIS_MODULE,
.llseek = DEMO_llseek,
.read = DEMO_read,
.write = DEMO_write,
.ioctl = DEMO_ioctl,
.open = DEMO_open,
.release = DEMO_release,
.mmap = DEMO_mmap,
};
应运层:
unsigned char *start;
unsigned char *buf="genghaifei";
fd=open("/dev/camera",O_RDWR);
if(fd==-1)
{
perror("error openn");
exit(-1);
}
start=(unsigned char*)mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
memcpy(start,buf,10);
请问这样是不是把buf的内容copy到内核空间了?如果是的话,copy到内核的那个位置处?内核里面的map_start到底有什么用?谢谢!
|
map_start 是一个物理地址。本意就是把buf的内容写到了这个物理地址所在的地方了。
|
dma_alloc_coherent用来分配一段连续的DMA内存,返回的物理地址存在map_start,函数返回虚拟地址。
读数据是通过虚拟地址来读的。
读数据是通过虚拟地址来读的。