您的位置:首页 > 运维架构 > Linux

利用mmap /dev/mem 读写Linux内存

2016-07-01 10:54 543 查看
使用 hexedit /dev/mem 可以显示所有物理内存中的信息。 运用mmap将/dev/mem map出来,然后直接对其读写可以实现用户空间的内核操作。

以下是我写的一个sample

[cpp]
view plain
copy

#include<stdio.h>  
#include<unistd.h>  
#include<sys/mman.h>  
#include<sys/types.h>  
#include<sys/stat.h>  
#include<fcntl.h>  
  
int main()  
{  
    unsigned char * map_base;  
    FILE *f;  
    int n, fd;  
  
    fd = open("/dev/mem", O_RDWR|O_SYNC);  
    if (fd == -1)  
    {  
        return (-1);  
    }  
  
    map_base = mmap(NULL, 0xff, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x20000);  
  
    if (map_base == 0)  
    {  
        printf("NULL pointer!\n");  
    }  
    else  
    {  
        printf("Successfull!\n");  
    }  
  
    unsigned long addr;  
    unsigned char content;  
  
    int i = 0;  
    for (;i < 0xff; ++i)  
    {  
        addr = (unsigned long)(map_base + i);  
        content = map_base[i];  
        printf("address: 0x%lx   content 0x%x\t\t", addr, (unsigned int)content);  
  
        map_base[i] = (unsigned char)i;  
        content = map_base[i];  
        printf("updated address: 0x%lx   content 0x%x\n", addr, (unsigned int)content);  
    }  
  
    close(fd);  
  
    munmap(map_base, 0xff);  
  
    return (1);  
}  

上面的例子将起始地址0x20000(物理地址), 长度为0xff映射出来。 然后就可以像普通数组一样操作内存。
下面是输出结果

address: 0x7f3f95391000   content 0x0           updated address: 0x7f3f95391000   content 0x0

address: 0x7f3f95391001   content 0x0           updated address: 0x7f3f95391001   content 0x1

address: 0x7f3f95391002   content 0x0           updated address: 0x7f3f95391002   content 0x2

address: 0x7f3f95391003   content 0x0           updated address: 0x7f3f95391003   content 0x3

address: 0x7f3f95391004   content 0x0           updated address: 0x7f3f95391004   content 0x4

。。。

我的测试机器是64位机。 该例子将物理地址0x20000映射到了虚拟地址0x7f3f95391000。

首先将当前地址下的内容输出, 然后写入新值。

可以通过 hexedit /dev/mem 验证新值已经写入。

如果想在用户态处理kernel分配的地址可以这么做。 首先用virt_addr = get_free_pages(GFP_KERNEL, order)分配内存,通过phy_addr = __pa(virt_addr)得到物理地址,然后在用户态将/dev/mem用mmap 映射出来, offset就是phy_addr, length设为 2^order。 此时就可以在用户态读写内核分配的内存了。

注:该操作需要有root权限。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: