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

时间:2011-10-04 01:01:27

标签: c linux memory assembly kernel

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

   #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复制

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

@duskwulf说的话。

您尚未定义

struct mtrr_sentry sentry

并且成员没有地址。

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

#include <asm/mtrr.h>