span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }.cm-searching {background: #ffa; background: rgba(255, 255, 0, .4);}.cm-force-border { padding-right: .1px; }@media print { .CodeMirror div.CodeMirror-cursors {visibility: hidden;}}.cm-tab-wrap-hack:after { content: ""; }span.CodeMirror-selectedtext { background: none; }.CodeMirror-activeline-background, .CodeMirror-selected {transition: visibility 0ms 100ms;}.CodeMirror-blur .CodeMirror-activeline-background, .CodeMirror-blur .CodeMirror-selected {visibility:hidden;}.CodeMirror-blur .CodeMirror-matchingbracket {color:inherit !important;outline:none !important;text-decoration:none !important;}
-->
li {list-style-type:decimal;}ol.wiz-list-level2 > li {list-style-type:lower-latin;}ol.wiz-list-level3 > li {list-style-type:lower-roman;}blockquote {padding:0 12px;padding:0 0.75rem;}blockquote > :first-child {margin-top:0;}blockquote > :last-child {margin-bottom:0;}img {border:0;max-width:100%;height:auto !important;margin:2px 0;}table {border-collapse:collapse;border:1px solid #bbbbbb;}td, th {padding:4px 8px;border-collapse:collapse;border:1px solid #bbbbbb;min-height:28px;word-break:break-all;box-sizing: border-box;}.wiz-hide {display:none !important;}
-->
作者
平台
参考
概述
/**
* remap_pfn_range - remap kernel memory to userspace
* @vma: user vma to map to
* @addr: target user address to start at
* @pfn: physical address of kernel memory
* @size: size of map area
* @prot: page protection flags for this mapping
*
* Note: this is only safe if the mm semaphore is held when called.
*/
int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
unsigned long pfn, unsigned long size, pgprot_t prot);
正文
一、驱动程序
static int __init remap_pfn_init(void)
{
int ret = ; kbuff = kzalloc(BUF_SIZE, GFP_KERNEL); // 这里的BUF_SIZE是128KB
if (!kbuff) {
ret = -ENOMEM;
goto err;
} ret = misc_register(&remap_pfn_misc); // 注册一个misc设备
if (unlikely(ret)) {
pr_err("failed to register misc device!\n");
goto err;
} return ; err:
return ret;
}
第11行注册了一个misc设备,相关信息如下:
static struct miscdevice remap_pfn_misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = "remap_pfn",
.fops = &remap_pfn_fops,
};
这样加载驱动后会在/dev下生成一个名为remap_pfn的节点,用户程序可以通过这个节点跟驱动通信。其中remap_pfn_fops的定义如下:
static const struct file_operations remap_pfn_fops = {
.owner = THIS_MODULE,
.open = remap_pfn_open,
.mmap = remap_pfn_mmap,
};
第3行的open函数这里没有做什么实际的工作,只是打印一些log,比如将进程的内存布局信息输出
static int remap_pfn_open(struct inode *inode, struct file *file)
{
struct mm_struct *mm = current->mm; printk("client: %s (%d)\n", current->comm, current->pid);
printk("code section: [0x%lx 0x%lx]\n", mm->start_code, mm->end_code);
printk("data section: [0x%lx 0x%lx]\n", mm->start_data, mm->end_data);
printk("brk section: s: 0x%lx, c: 0x%lx\n", mm->start_brk, mm->brk);
printk("mmap section: s: 0x%lx\n", mm->mmap_base);
printk("stack section: s: 0x%lx\n", mm->start_stack);
printk("arg section: [0x%lx 0x%lx]\n", mm->arg_start, mm->arg_end);
printk("env section: [0x%lx 0x%lx]\n", mm->env_start, mm->env_end); return ;
}
static int remap_pfn_mmap(struct file *file, struct vm_area_struct *vma)
{
unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
unsigned long pfn_start = (virt_to_phys(kbuff) >> PAGE_SHIFT) + vma->vm_pgoff;
unsigned long virt_start = (unsigned long)kbuff + offset;
unsigned long size = vma->vm_end - vma->vm_start;
int ret = ; printk("phy: 0x%lx, offset: 0x%lx, size: 0x%lx\n", pfn_start << PAGE_SHIFT, offset, size); ret = remap_pfn_range(vma, vma->vm_start, pfn_start, size, vma->vm_page_prot);
if (ret)
printk("%s: remap_pfn_range failed at [0x%lx 0x%lx]\n",
__func__, vma->vm_start, vma->vm_end);
else
printk("%s: map 0x%lx to 0x%lx, size: 0x%lx\n", __func__, virt_start,
vma->vm_start, size); return ret;
}
第3行的vma_pgoff表示的是该vma表示的区间在缓冲区中的偏移地址,单位是页。这个值是用户调用mmap时传入的最后一个参数,不过用户空间的offset的单位是字节(当然必须是页对齐),进入内核后,内核会将该值右移PAGE_SHIFT(12),也就是转换为以页为单位。因为要在第9行打印这个编译地址,所以这里将其再左移PAGE_SHIFT,然后赋值给offset。
二、用户测试程序
#define PAGE_SIZE (4*1024)
#define BUF_SIZE (16*PAGE_SIZE)
#define OFFSET (0) int main(int argc, const char *argv[])
{
int fd;
char *addr = NULL; fd = open("/dev/remap_pfn", O_RDWR); addr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, OFFSET); sprintf(addr, "I am %s\n", argv[]); while()
sleep();
return ;
}
第10和第12行,打开设备节点,然后从内核空间映射64KB的内存到用户空间,首地址存放在addr中,由于后面既要写入也要共享,所以设置了对应的flags。这里指定的offset是0,即映射前64KB。
#define PAGE_SIZE (4*1024)
#define BUF_SIZE (16*PAGE_SIZE)
#define OFFSET (0) int main(int argc, const char *argv[])
{
int fd;
char *addr = NULL; fd = open("/dev/remap_pfn", O_RDWR); addr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, OFFSET); printf("%s", addr); while()
sleep(); return ;
}
user_2跟user_1实现一般一样,不同之处是将addr指向的虚拟地址空间的内容打印出来。
#define PAGE_SIZE (4*1024)
#define BUF_SIZE (16*PAGE_SIZE)
#define OFFSET (16*PAGE_SIZE) int main(int argc, const char *argv[])
{
int fd;
char *addr = NULL; fd = open("/dev/remap_pfn", O_RDWR); addr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, OFFSET); sprintf(addr, "I am %s\n", argv[]); while()
sleep();
return ;
}
第12行的OFFSET设置的是64KB,表示将内核缓冲区的后64KB映射到用户空间
#define PAGE_SIZE (4*1024)
#define BUF_SIZE (16*PAGE_SIZE)
#define OFFSET (16*PAGE_SIZE) int main(int argc, const char *argv[])
{
int fd;
char *addr = NULL; fd = open("/dev/remap_pfn", O_RDWR); addr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, OFFSET); printf("%s", addr); while()
sleep();
return ;
}
#define PAGE_SIZE (4*1024)
#define BUF_SIZE (32*PAGE_SIZE)
#define OFFSET (0) int main(int argc, const char *argv[])
{
int fd;
char *addr = NULL;
int *brk; fd = open("/dev/remap_pfn", O_RDWR); addr = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, );
memset(addr, 0x0, BUF_SIZE); printf("Clear Finished\n"); while()
sleep();
return ;
}
三、测试
1、内核空间的虚拟内存布局
[ 0.000000] Virtual kernel memory layout:
[ 0.000000] vector : 0xffff0000 - 0xffff1000 ( kB)
[ 0.000000] fixmap : 0xffc00000 - 0xfff00000 ( kB)
[ 0.000000] vmalloc : 0xf0800000 - 0xff800000 ( MB)
[ 0.000000] lowmem : 0xc0000000 - 0xf0000000 ( MB)
[ 0.000000] pkmap : 0xbfe00000 - 0xc0000000 ( MB)
[ 0.000000] modules : 0xbf000000 - 0xbfe00000 ( MB)
[ 0.000000] .text : 0xc0008000 - 0xc0800000 ( kB)
[ 0.000000] .init : 0xc0b00000 - 0xc0c00000 ( kB)
[ 0.000000] .data : 0xc0c00000 - 0xc0c7696c ( kB)
[ 0.000000] .bss : 0xc0c78000 - 0xc0cc9b8c ( kB)
2、用户虚拟地址空间的布局
3、user_1和user_2
[root@vexpress mnt]# ./user_1
可以看到如下内核log:
[ 2494.835749] client: user_1 ()
[ 2494.835918] code section: [0x8000 0x87f4]
[ 2494.836047] data section: [0x107f4 0x1092c]
[ 2494.836165] brk section: s: 0x11000, c: 0x11000
[ 2494.836307] mmap section: s: 0xb6f17000
[ 2494.836441] stack section: s: 0xbe909e20
[ 2494.836569] arg section: [0xbe909f23 0xbe909f2c]
[ 2494.836689] env section: [0xbe909f2c 0xbe909ff3]
[ 2494.836943] phy: 0x8eb60000, offset: 0x0, size: 0x10000
[ 2494.837176] remap_pfn_mmap: map 0xeeb60000 to 0xb6d75000, size: 0x10000
进程号是870,可以分别用下面的查看一下该进程的地址空间的map信息:
[root@vexpress mnt]# cat /proc//maps
- r-xp : /mnt/user_1
- rw-p : /mnt/user_1
b6d75000-b6d85000 rw-s : /dev/remap_pfn
b6d85000-b6eb8000 r-xp b3: /lib/libc-2.18.so
b6eb8000-b6ebf000 ---p b3: /lib/libc-2.18.so
b6ebf000-b6ec1000 r--p b3: /lib/libc-2.18.so
b6ec1000-b6ec2000 rw-p b3: /lib/libc-2.18.so
b6ec2000-b6ec5000 rw-p :
b6ec5000-b6ee6000 r-xp b3: /lib/libgcc_s.so.
b6ee6000-b6eed000 ---p b3: /lib/libgcc_s.so.
b6eed000-b6eee000 rw-p b3: /lib/libgcc_s.so.
b6eee000-b6f0e000 r-xp b3: /lib/ld-2.18.so
b6f13000-b6f15000 rw-p :
b6f15000-b6f16000 r--p 0001f000 b3: /lib/ld-2.18.so
b6f16000-b6f17000 rw-p b3: /lib/ld-2.18.so
be8e9000-be90a000 rw-p : [stack]
bed1c000-bed1d000 r-xp : [sigpage]
bed1d000-bed1e000 r--p : [vvar]
bed1e000-bed1f000 r-xp : [vdso]
ffff0000-ffff1000 r-xp : [vectors]
上面的每一行都可以表示一个vma的映射信息,其中第4行是需要关心的:
b6d75000-b6d85000 rw-s : /dev/remap_pfn
含义:
[root@vexpress mnt]# pmap -x
: {no such process} ./user_1
Address Kbytes PSS Dirty Swap Mode Mapping
r-xp /mnt/user_1
rw-p /mnt/user_1
b6d75000 rw-s /dev/remap_pfn
b6d85000 r-xp /lib/libc-2.18.so
b6eb8000 ---p /lib/libc-2.18.so
b6ebf000 r--p /lib/libc-2.18.so
b6ec1000 rw-p /lib/libc-2.18.so
b6ec2000 rw-p [ anon ]
b6ec5000 r-xp /lib/libgcc_s.so.
b6ee6000 ---p /lib/libgcc_s.so.
b6eed000 rw-p /lib/libgcc_s.so.
b6eee000 r-xp /lib/ld-2.18.so
b6f13000 rw-p [ anon ]
b6f15000 r--p /lib/ld-2.18.so
b6f16000 rw-p /lib/ld-2.18.so
be8e9000 rw-p [stack]
bed1c000 r-xp [sigpage]
bed1d000 r--p [vvar]
bed1e000 r-xp [vdso]
ffff0000 r-xp [vectors]
-------- ------ ------ ------ ------
total
[root@vexpress mnt]# ./user_2
I am ./user_1
可以看到user_1写入的信息,下面是内核log以及虚拟地址空间映射信息:
[ 2545.832903] client: user_2 ()
[ 2545.833087] code section: [0x8000 0x87e0]
[ 2545.833178] data section: [0x107e0 0x10918]
[ 2545.833262] brk section: s: 0x11000, c: 0x11000
[ 2545.833346] mmap section: s: 0xb6fb5000
[ 2545.833423] stack section: s: 0xbea0ee20
[ 2545.833499] arg section: [0xbea0ef23 0xbea0ef2c]
[ 2545.833590] env section: [0xbea0ef2c 0xbea0eff3]
[ 2545.833761] phy: 0x8eb60000, offset: 0x0, size: 0x10000
[ 2545.833900] remap_pfn_mmap: map 0xeeb60000 to 0xb6e13000, size: 0x10000 [root@vexpress mnt]# cat /proc//maps
- r-xp : /mnt/user_2
- rw-p : /mnt/user_2
b6e13000-b6e23000 rw-s : /dev/remap_pfn
b6e23000-b6f56000 r-xp b3: /lib/libc-2.18.so
b6f56000-b6f5d000 ---p b3: /lib/libc-2.18.so
b6f5d000-b6f5f000 r--p b3: /lib/libc-2.18.so
b6f5f000-b6f60000 rw-p b3: /lib/libc-2.18.so
b6f60000-b6f63000 rw-p :
b6f63000-b6f84000 r-xp b3: /lib/libgcc_s.so.
b6f84000-b6f8b000 ---p b3: /lib/libgcc_s.so.
b6f8b000-b6f8c000 rw-p b3: /lib/libgcc_s.so.
b6f8c000-b6fac000 r-xp b3: /lib/ld-2.18.so
b6fb0000-b6fb3000 rw-p :
b6fb3000-b6fb4000 r--p 0001f000 b3: /lib/ld-2.18.so
b6fb4000-b6fb5000 rw-p b3: /lib/ld-2.18.so
be9ee000-bea0f000 rw-p : [stack]
beedf000-beee0000 r-xp : [sigpage]
beee0000-beee1000 r--p : [vvar]
beee1000-beee2000 r-xp : [vdso]
ffff0000-ffff1000 r-xp : [vectors]
上面的的log信息可以查看: https://github.com/pengdonglin137/remap_pfn_demo/blob/master/log/user2
4、user_3和user_4
[ 4938.000918] client: user_3 ()
[ 4938.001117] code section: [0x8000 0x87f4]
[ 4938.001205] data section: [0x107f4 0x1092c]
[ 4938.001281] brk section: s: 0x11000, c: 0x11000
[ 4938.001410] mmap section: s: 0xb6ff1000
[ 4938.001485] stack section: s: 0xbea10e20
[ 4938.001549] arg section: [0xbea10f23 0xbea10f2c]
[ 4938.001606] env section: [0xbea10f2c 0xbea10ff3]
[ 4938.001793] phy: 0x8eb70000, offset: 0x10000, size: 0x10000
[ 4938.001996] remap_pfn_mmap: map 0xeeb70000 to 0xb6e4f000, size: 0x10000 [root@vexpress mnt]#
[root@vexpress mnt]# cat /proc//maps
- r-xp : /mnt/user_3
- rw-p : /mnt/user_3
b6e4f000-b6e5f000 rw-s : /dev/remap_pfn
b6e5f000-b6f92000 r-xp b3: /lib/libc-2.18.so
b6f92000-b6f99000 ---p b3: /lib/libc-2.18.so
b6f99000-b6f9b000 r--p b3: /lib/libc-2.18.so
b6f9b000-b6f9c000 rw-p b3: /lib/libc-2.18.so
b6f9c000-b6f9f000 rw-p :
b6f9f000-b6fc0000 r-xp b3: /lib/libgcc_s.so.
b6fc0000-b6fc7000 ---p b3: /lib/libgcc_s.so.
b6fc7000-b6fc8000 rw-p b3: /lib/libgcc_s.so.
b6fc8000-b6fe8000 r-xp b3: /lib/ld-2.18.so
b6fed000-b6fef000 rw-p :
b6fef000-b6ff0000 r--p 0001f000 b3: /lib/ld-2.18.so
b6ff0000-b6ff1000 rw-p b3: /lib/ld-2.18.so
be9f0000-bea11000 rw-p : [stack]
bebe9000-bebea000 r-xp : [sigpage]
bebea000-bebeb000 r--p : [vvar]
bebeb000-bebec000 r-xp : [vdso]
ffff0000-ffff1000 r-xp : [vectors]
需要关注的是第16行,其中的"00010000"表示offset,大小是64KB,也就是vma->vm_pgoff的值。