Using the sample code from 12.6. Creating MTRRs from a C programme using ioctl()’s

I can create a write-back type memory range. However, I was wondering if anyone can tell me how can I use that?

I tried to use mmap to bind the virtual address of a structure with the physical base address of the MTRR region. That however doesn’t seem to do anything. I could not find any suitable article/topic regarding this. I guess I do not have a clue where to start. Any help?

Here is the code where I created an MTRR region and use mmap to bind the virtual address of my structure with the physical address of the write-back memory region,

// setup mtrr
int fd_mtrr;
struct mtrr_sentry sentry;
sentry.base = 0x06b100000;
sentry.size = 0x40000;
sentry.type = 6; // "write-back"
if ( ( fd_mtrr = open ("/proc/mtrr", O_WRONLY, 0) ) == -1 ){
    if (errno == ENOENT){
        fputs ("/proc/mtrr not found: not supported or you don't have a PPro?\n", stderr);
        exit (3);
    fprintf (stderr, "Error opening /proc/mtrr\t%s\n", ERRSTRING);
    exit (4);
if (ioctl (fd_mtrr, MTRRIOC_ADD_ENTRY, &sentry) == -1){
    fprintf (stderr, "Error doing ioctl(2) on /dev/mtrr\t%s\n", ERRSTRING);
    exit (5);
fprintf (stderr, "MTRR created \n");

// mmap structure addres with mtrr
int mm, *map;
mm = open("/dev/mem", O_RDWR);
map =  mmap(&env, sizeof(env), PROT_READ | PROT_WRITE, MAP_PRIVATE, mm, sentry.base);
if(map == MAP_FAILED){
    printf("Mapping Failed\n");
    return 1;
fprintf (stderr, "mapping MTRR with env is done \n");

My goal:

I'm trying to assign a predefined static structure to a write-back memory type region. While reading about MTRR'sI came to know that PAT can be also used to achieve the same goal, however, I did not find any suitable example on how to use PAT to set up a write-back memory type.

My ultimate goal is to put that aforementioned structure into the L1d cache. From the intel documentation, I know that any attempt to read/write 1byte into write-back memory type actually put the whole 64-byte into L1d cache. Therefore, After successful binding of the virtual address (address of the structure I’m trying to put into L1d) to the write-back type physical address, If I write 1 byte to the virtual address, I’m actually writing 1byte into the Physical address. And because the type of the physical address is write-back type, 64-byte will be transferred into the L1d cache. Please correct me if the understanding is incorrect.

Previously I have also tried to use __builtin_prefetch() with the following code.

// p is the address of a static structure & num is the size of the whole structure
void fillL1(unsigned char *p, int num){
    int i;
    unsigned char *buf = p;
        buf += 64;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.