Linux中的直接内存访问 [英] Direct Memory Access in Linux

查看:157
本文介绍了Linux中的直接内存访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试直接访问嵌入式Linux项目的物理内存,但我不知道如何最好地指定内存供我使用。

I'm trying to access physical memory directly for an embedded Linux project, but I'm not sure how I can best designate memory for my use.

如果我定期启动我的设备,并访问/ dev / mem,我可以轻松地读取和写入任何地方我想要的。但是,在这里,我正在访问可以轻松分配给任何进程的内存;我不想做的事情

If I boot my device regularly, and access /dev/mem, I can easily read and write to just about anywhere I want. However, in this, I'm accessing memory that can easily be allocated to any process; which I don't want to do

我的/ dev / mem的代码是(所有错误检查等)删除):

My code for /dev/mem is (all error checking, etc. removed):

mem_fd = open("/dev/mem", O_RDWR));
mem_p = malloc(SIZE + (PAGE_SIZE - 1));
if ((unsigned long) mem_p % PAGE_SIZE) {
    mem_p += PAGE_SIZE - ((unsigned long) mem_p % PAGE_SIZE);
}
mem_p = (unsigned char *) mmap(mem_p, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, mem_fd, BASE_ADDRESS);

这个工作。不过,我想使用没有其他人会碰的记忆。我尝试通过使用mem = XXXm引导来限制内核看到的内存量,然后将BASE_ADDRESS设置为高于(但低于物理内存)的内容,但它似乎不是一直访问相同的内存。

And this works. However, I'd like to be using memory that no one else will touch. I've tried limiting the amount of memory that the kernel sees by booting with mem=XXXm, and then setting BASE_ADDRESS to something above that (but below the physical memory), but it doesn't seem to be accessing the same memory consistently.

根据我在网上看到的内容,我怀疑我可能需要一个内核模块(这是OK),它使用ioremap()或remap_pfn_range() )但是我绝对不知道怎么样任何人都可以帮忙吗?

Based on what I've seen online, I suspect I may need a kernel module (which is OK) which uses either ioremap() or remap_pfn_range() (or both???), but I have absolutely no idea how; can anyone help?

编辑:
我想要的是一种总是访问同一物理内存(比如说1.5MB值)的方式,并设置内存让内核不会将其分配给任何其他进程。

What I want is a way to always access the same physical memory (say, 1.5MB worth), and set that memory aside so that the kernel will not allocate it to any other process.

我正在尝试重现我们在其他操作系统中使用的系统(没有内存管理),因此我可以通过链接器在内存中分配一个空间,并使用像

I'm trying to reproduce a system we had in other OSes (with no memory management) whereby I could allocate a space in memory via the linker, and access it using something like

*(unsigned char *)0x12345678

EDIT2:
我想我应该提供一些更多的细节。该内存空间将用于RAM缓冲区,用于嵌入式应用程序的高性能日志记录解决方案。在我们拥有的系统中,在软重启期间,没有什么可以清除或扰乱物理内存。因此,如果我写一个物理地址X,并重新启动系统,重新启动后仍然设置相同的位。这已经在运行VxWorks的完全相同的硬件上进行了测试(该逻辑在Nucleus RTOS和OS20在不同的平台上也很好地工作,FWIW)。我的想法是通过直接寻址物理内存在Linux中尝试相同的东西;因此,每次启动时都需要相同的地址。

I guess I should provide some more detail. This memory space will be used for a RAM buffer for a high performance logging solution for an embedded application. In the systems we have, there's nothing that clears or scrambles physical memory during a soft reboot. Thus, if I write a bit to a physical address X, and reboot the system, the same bit will still be set after the reboot. This has been tested on the exact same hardware running VxWorks (this logic also works nicely in Nucleus RTOS and OS20 on different platforms, FWIW). My idea was to try the same thing in Linux by addressing physical memory directly; therefore, it's essential that I get the same addresses each boot.

我应该明白这是针对内核2.6.12和更新版本。

I should probably clarify that this is for kernel 2.6.12 and newer.

EDIT3:
这是我的代码,首先是内核模块,然后是用户空间应用程序。

Here's my code, first for the kernel module, then for the userspace application.

要使用它,我启动mem = 95m,然后insmod foo-module.ko,然后mknod mknod / dev / foo c 32 0,然后运行foo-user,它死亡。在gdb下运行显示它在分配中死亡,尽管在gdb中,我无法取消引用我从mmap获取的地址(尽管printf可以)

To use it, I boot with mem=95m, then insmod foo-module.ko, then mknod mknod /dev/foo c 32 0, then run foo-user , where it dies. Running under gdb shows that it dies at the assignment, although within gdb, I cannot dereference the address I get from mmap (although printf can)

foo-module.c

foo-module.c

#include <linux/module.h>
#include <linux/config.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <asm/io.h>

#define VERSION_STR "1.0.0"
#define FOO_BUFFER_SIZE (1u*1024u*1024u)
#define FOO_BUFFER_OFFSET (95u*1024u*1024u)
#define FOO_MAJOR 32
#define FOO_NAME "foo"

static const char *foo_version = "@(#) foo Support version " VERSION_STR " " __DATE__ " " __TIME__;

static void    *pt = NULL;

static int      foo_release(struct inode *inode, struct file *file);
static int      foo_open(struct inode *inode, struct file *file);
static int      foo_mmap(struct file *filp, struct vm_area_struct *vma);

struct file_operations foo_fops = {
    .owner = THIS_MODULE,
    .llseek = NULL,
    .read = NULL,
    .write = NULL,
    .readdir = NULL,
    .poll = NULL,
    .ioctl = NULL,
    .mmap = foo_mmap,
    .open = foo_open,
    .flush = NULL,
    .release = foo_release,
    .fsync = NULL,
    .fasync = NULL,
    .lock = NULL,
    .readv = NULL,
    .writev = NULL,
};

static int __init foo_init(void)
{
    int             i;
    printk(KERN_NOTICE "Loading foo support module\n");
    printk(KERN_INFO "Version %s\n", foo_version);
    printk(KERN_INFO "Preparing device /dev/foo\n");
    i = register_chrdev(FOO_MAJOR, FOO_NAME, &foo_fops);
    if (i != 0) {
        return -EIO;
        printk(KERN_ERR "Device couldn't be registered!");
    }
    printk(KERN_NOTICE "Device ready.\n");
    printk(KERN_NOTICE "Make sure to run mknod /dev/foo c %d 0\n", FOO_MAJOR);
    printk(KERN_INFO "Allocating memory\n");
    pt = ioremap(FOO_BUFFER_OFFSET, FOO_BUFFER_SIZE);
    if (pt == NULL) {
        printk(KERN_ERR "Unable to remap memory\n");
        return 1;
    }
    printk(KERN_INFO "ioremap returned %p\n", pt);
    return 0;
}
static void __exit foo_exit(void)
{
    printk(KERN_NOTICE "Unloading foo support module\n");
    unregister_chrdev(FOO_MAJOR, FOO_NAME);
    if (pt != NULL) {
        printk(KERN_INFO "Unmapping memory at %p\n", pt);
        iounmap(pt);
    } else {
        printk(KERN_WARNING "No memory to unmap!\n");
    }
    return;
}
static int foo_open(struct inode *inode, struct file *file)
{
    printk("foo_open\n");
    return 0;
}
static int foo_release(struct inode *inode, struct file *file)
{
    printk("foo_release\n");
    return 0;
}
static int foo_mmap(struct file *filp, struct vm_area_struct *vma)
{
    int             ret;
    if (pt == NULL) {
        printk(KERN_ERR "Memory not mapped!\n");
        return -EAGAIN;
    }
    if ((vma->vm_end - vma->vm_start) != FOO_BUFFER_SIZE) {
        printk(KERN_ERR "Error: sizes don't match (buffer size = %d, requested size = %lu)\n", FOO_BUFFER_SIZE, vma->vm_end - vma->vm_start);
        return -EAGAIN;
    }
    ret = remap_pfn_range(vma, vma->vm_start, (unsigned long) pt, vma->vm_end - vma->vm_start, PAGE_SHARED);
    if (ret != 0) {
        printk(KERN_ERR "Error in calling remap_pfn_range: returned %d\n", ret);
        return -EAGAIN;
    }
    return 0;
}
module_init(foo_init);
module_exit(foo_exit);
MODULE_AUTHOR("Mike Miller");
MODULE_LICENSE("NONE");
MODULE_VERSION(VERSION_STR);
MODULE_DESCRIPTION("Provides support for foo to access direct memory");

foo-user.c

foo-user.c

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>

int main(void)
{
    int             fd;
    char           *mptr;
    fd = open("/dev/foo", O_RDWR | O_SYNC);
    if (fd == -1) {
        printf("open error...\n");
        return 1;
    }
    mptr = mmap(0, 1 * 1024 * 1024, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 4096);
    printf("On start, mptr points to 0x%lX.\n",(unsigned long) mptr);
    printf("mptr points to 0x%lX. *mptr = 0x%X\n", (unsigned long) mptr, *mptr);
    mptr[0] = 'a';
    mptr[1] = 'b';
    printf("mptr points to 0x%lX. *mptr = 0x%X\n", (unsigned long) mptr, *mptr);
    close(fd);
    return 0;
}


推荐答案

我想你可以找到很多关于kmalloc + mmap部分的文档。
但是,我不确定你可以连续地将这么多的内存公平,并且总是在同一个地方。当然,如果一切都一样,那么你可能会得到一个不变的地址。但是,每次更改内核代码时,都会得到一个不同的地址,所以我不会使用kmalloc解决方案。

I think you can find a lot of documentation about the kmalloc + mmap part. However, I am not sure that you can kmalloc so much memory in a contiguous way, and have it always at the same place. Sure, if everything is always the same, then you might get a constant address. However, each time you change the kernel code, you will get a different address, so I would not go with the kmalloc solution.

我想你应该保留一些内存启动时间,即保留一些物理内存,使内核不被触摸。那么你可以ioremap这个内存,这将给你
a内核虚拟地址,然后你可以mmap它,并写一个漂亮的设备驱动程序。

I think you should reserve some memory at boot time, ie reserve some physical memory so that is is not touched by the kernel. Then you can ioremap this memory which will give you a kernel virtual address, and then you can mmap it and write a nice device driver.

这带我们回来以PDF格式 linux设备驱动程序。看一下第15章,它是在第443页上描述这种技术

This take us back to linux device drivers in PDF format. Have a look at chapter 15, it is describing this technique on page 443

编辑:ioremap和mmap。
我认为这可能比两步更容易调试:首先得到ioremap
,并使用字符设备操作(即读/写)进行测试。一旦你知道你可以安全地使用读/写访问整个ioremapped内存,那么你尝试mmap整个ioremapped范围。

Edit : ioremap and mmap. I think this might be easier to debug doing things in two step : first get the ioremap right, and test it using a character device operation, ie read/write. Once you know you can safely have access to the whole ioremapped memory using read / write, then you try to mmap the whole ioremapped range.

如果你有麻烦可能要发表另一个问题关于mmaping

And if you get in trouble may be post another question about mmaping

编辑:remap_pfn_range
ioremap返回一个virtual_adress,您必须将其转换为pfn以进行remap_pfn_ranges。
现在,我不明白什么是pfn(页框号),但我想你可以得到一个调用

Edit : remap_pfn_range ioremap returns a virtual_adress, which you must convert to a pfn for remap_pfn_ranges. Now, I don't understand exactly what a pfn (Page Frame Number) is, but I think you can get one calling

virt_to_phys(pt) >> PAGE_SHIFT

这可能不是正确的方式(tm),但您应该尝试

This probably is not the Right Way (tm) to do it, but you should try it

您还应检查FOO_MEM_OFFSET是您的RAM块的物理地址。即使在mmu发生任何事情之前,您的内存也可以在处理器的内存映射中为0。

You should also check that FOO_MEM_OFFSET is the physical address of your RAM block. Ie before anything happens with the mmu, your memory is available at 0 in the memory map of your processor.

这篇关于Linux中的直接内存访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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