简述:
linux内存空间分为用户空间和内核空间,应用程序是不能直接访问内核空间的数据。
mmap就是建立内核空间映射到用户空间虚拟地址上,之后,应用程序直接访问映射后虚拟地址,实际是在访问内核空间。
应用程序mmap的系统调用:
mmap声明的头文件:
#include <sys/mman.h>
如是ubuntu系统,可以在/usr/include/sys目录下查看
应用程序mmap声明:
void *mmap(void *__addr, size_t __len, int __port,int __flags,int __fd __off_t __offset);
__addr:用户空间的虚拟地址。
__len:要映射空间的大小(单位字节)
__port:期望的内存保护标志,不能与文件的打开模式冲突。
__flags:指定映射对象的类型,映射选项和映射页是否可以共享。
__fd:有效的文件描述。
__offset:表示被映射对象(即文件)从那里开始(偏移),通常都是用0。 该值大小为PAGE_SIZE的整数倍
当mmap系统调用,如果设备驱动支持mmap,驱动mmap接口将被调用。
取消映射用munmap。
应用程序的详细使用,参考下面博客:
Linux内存管理之mmap详解
Linux 内存映射函数 mmap()函数详解
驱动mmap:
(1)file_operations定义:
在include/linux/fs.h:
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
int (*show_fdinfo)(struct seq_file *m, struct file *f);
};
其中:
int (*mmap) (struct file *, struct vm_area_struct *);
就是驱动接口。
驱动实现mmap主要是调用:
int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
unsigned long pfn, unsigned long size, pgprot_t);
函数来映射,声明在include/linux/mm.h。
第一个参数:虚拟地址描述结构体(声明在include/linux/mm_types.h,起始mm.h中已经包含了它),一般是系统传递下来。
第二个参数:虚拟起始地址
第三个参数:物理地址
第四个参数:映射空间大小,单位字节
第五个参数:给新 VMA 要求的”protection”. 驱动直接使用 vma->vm_page_prot
返回值,成功返回0,否则返回-1;
用法例子:
#include <linux/slab.h>
#include <linux/mm.h>
static int simple_remap_mmap(struct file *filp, struct vm_area_struct *vma);
struct file_operations fo = {
...
.mmap = simple_remap_mmap,
...
};
static int simple_remap_mmap(struct file *filp, struct vm_area_struct *vma)
{
void *p;
p = kmalloc(200,GFP_KERNEL)
if(!p)
return -1;
if (remap_pfn_range(vma, vma->vm_start, virt_to_phys(p),
vma->vm_end - vma->vm_start,
vma->vm_page_prot))
return -EAGAIN;
return 0;
}
也可以直接定义数组:
#include <linux/slab.h>
#include <linux/mm.h>
static char ch[200];
static int simple_remap_mmap(struct file *filp, struct vm_area_struct *vma);
struct file_operations fo = {
...
.mmap = simple_remap_mmap,
...
};
static int simple_remap_mmap(struct file *filp, struct vm_area_struct *vma)
{
if (remap_pfn_range(vma, vma->vm_start, virt_to_phys(ch),
vma->vm_end - vma->vm_start,
vma->vm_page_prot))
return -EAGAIN;
return 0;
}
nopage方法可以替代remap_pfn_range,但是不建议,用的很少,linux版本的变化nopage这种方法也有很大变化,而remap_pfn_range是在linux各种版本基本用法都差不多。