用C代码在linux中添加mtrr条目

发布于 2024-12-07 21:38:19 字数 1423 浏览 2 评论 0原文

我试图向 MTRR 添加一个条目以将内存区域标记为写入组合,但内核不接受我的调用。它返回 EINVAL 错误号。可能是什么问题?我已经尝试了一切,但没有运气。以下是代码和运行后的输出:

   #define NUM_ELTS        (1024*64)

    struct mtrr_sentry sentry;

    void register_wc(uint *addr);

    void register_wc(uint *addr) {
        int fd,ret;
        int aux1,aux2;
        int page_size;

        sentry.base=(ulong) addr;
        sentry.size=NUM_ELTS;
        sentry.type=MTRR_TYPE_WRCOMB;

        page_size=getpagesize();
        aux1=sentry.base & (page_size - 1);
        aux2=sentry.size & (page_size - 1);

        printf("aux1=%d, aux2=%d, base=%d, size=%d, type=%d\n",aux1,aux2,sentry.base,sentry.size,sentry.type);

        fd=open("/proc/mtrr",O_WRONLY); if (fd==-1) { perror("open()"); exit(2); }
        printf("fd=%d\n",fd);
        ret=ioctl(fd,MTRRIOC_ADD_ENTRY,&sentry); if (ret==-1) { perror("ioctl()"); exit(3); }
        sleep(10);
        close(fd);
    }
int main(int argc, char **argv) {
    ulong size;
    uint *data;

    size=sizeof(uint)*NUM_ELTS;

    data=(uint*) memalign(4096,size);   if (!data) { exit(1); }
    printf("data address is %d, PAGE_SIZE=%d\n",data,getpagesize());
    register_wc(data);
}

程序产生的输出是:

data address is -1420279808, PAGE_SIZE=4096
aux1=0, aux2=0, base=-1420279808, size=65536, type=1
fd=3
ioctl(): Invalid argument

代码(几乎)从 /usr/src/linux/Documentation/x86/mtrr.txt 复制

I am trying to add an entry to MTRR to mark a memory region as write combining, but the kernel does not accepts my call. It returns EINVAL errno. What could be the problem? I have tried everything but have no luck. Here is the code and the output after running it:

   #define NUM_ELTS        (1024*64)

    struct mtrr_sentry sentry;

    void register_wc(uint *addr);

    void register_wc(uint *addr) {
        int fd,ret;
        int aux1,aux2;
        int page_size;

        sentry.base=(ulong) addr;
        sentry.size=NUM_ELTS;
        sentry.type=MTRR_TYPE_WRCOMB;

        page_size=getpagesize();
        aux1=sentry.base & (page_size - 1);
        aux2=sentry.size & (page_size - 1);

        printf("aux1=%d, aux2=%d, base=%d, size=%d, type=%d\n",aux1,aux2,sentry.base,sentry.size,sentry.type);

        fd=open("/proc/mtrr",O_WRONLY); if (fd==-1) { perror("open()"); exit(2); }
        printf("fd=%d\n",fd);
        ret=ioctl(fd,MTRRIOC_ADD_ENTRY,&sentry); if (ret==-1) { perror("ioctl()"); exit(3); }
        sleep(10);
        close(fd);
    }
int main(int argc, char **argv) {
    ulong size;
    uint *data;

    size=sizeof(uint)*NUM_ELTS;

    data=(uint*) memalign(4096,size);   if (!data) { exit(1); }
    printf("data address is %d, PAGE_SIZE=%d\n",data,getpagesize());
    register_wc(data);
}

The output produced by the program is:

data address is -1420279808, PAGE_SIZE=4096
aux1=0, aux2=0, base=-1420279808, size=65536, type=1
fd=3
ioctl(): Invalid argument

The code is copied (almost) from /usr/src/linux/Documentation/x86/mtrr.txt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-12-14 21:38:19

您传递给 MTRRIOC_ADD_ENTRY 的基地址必须是物理地址。看起来您正在将逻辑地址传递给刚刚分配的内存块,这没有任何意义。 MTRR 用于控制对内存映射硬件的访问,而不是对 RAM 的访问。

The base address you pass to MTRRIOC_ADD_ENTRY must be a physical address. It looks like you're passing in a logical address to a block of memory you just allocated, which doesn't make any sense. MTRRs are used to control access to memory-mapped hardware, not to RAM.

玻璃人 2024-12-14 21:38:19

@duskwulf 说的话。

您尚未定义

struct mtrr_sentry sentry

,成员也没有地址。

编辑:我想你想添加 mtrr_sentry 的定义。

#include <asm/mtrr.h>

What @duskwulf said.

You haven't defined

struct mtrr_sentry sentry

and the members don't have addresses.

EDIT: I think you want to add the definition of mtrr_sentry.

#include <asm/mtrr.h>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文