add mtrr entry in linux with C code
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
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.
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>
精彩评论