/* this program checks cache consistency for a virtually indexed direct-mapped cache larger than the page size */ #include #include #include #include #define PAGE 8192 main(int argc, char *argv[]) { char name[]="/tmp/mapcheckXXXXXX"; void *area1, *area2; long res,res2; int fd=mkstemp(name); if (fd==-1) { fprintf(stderr,"could not open file"); exit(1); } unlink(name); if (ftruncate(fd,PAGE)) { perror("ftruncate"); exit(1); } /* map a file */ if ((area1=mmap(0,PAGE,PROT_READ|PROT_WRITE,MAP_FILE|MAP_SHARED,fd,0))==(void *)-1) { perror("map1"); exit(1); } /* map same file at an address that differs by an odd number of pages, such that it does not map to the same cache entry in a two-page cache */ if ((area2=mmap(area1+3*PAGE,PAGE,PROT_READ|PROT_WRITE,MAP_FILE|MAP_SHARED|MAP_FIXED,fd,0))==(void *)-1) { perror("map2"); exit(1); } res2=*(long *)area1+*(long *)area2; /* warm up cache */ *(long *)area1=42; /* write word */ asm("mb"); *(long *)area2=23; /* overwrite the same physical word at a different virtual address */ asm("mb"); res=*(long *)area1; /* read overwritten word from first virtual address */ printf("area1=0x%lx, area2=0x%lx, Result=%d\n",(long)area1, (long) area2, res); if (res==23) printf("ok\n"); else printf("cache not consistent\n"); sprintf(area1,"%d",res2); /* use res2, so the compiler does not optimize it (and the cache warming code) away */ return 0; }