用C代码在linux中添加mtrr条目
我试图向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您传递给
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.@duskwulf 说的话。
您尚未定义
,成员也没有地址。
编辑:我想你想添加 mtrr_sentry 的定义。
What @duskwulf said.
You haven't defined
and the members don't have addresses.
EDIT: I think you want to add the definition of mtrr_sentry.