/* * This program demonstrates MIPS cache virtual aliasing problem, * apply to cpus that use virtually indexed cache and each set of * cache has a size greater than PAGE_SIZE. * * If the machines failes the test, you will see a bunch of error emssage: * int at 568 is not 5 * .... */ #include #include #include #include #include #include #include #include int fid[2][2]; int fid_read, fid_write; #define PAGE_SIZE 4096 #define SUSPEND_CONTROL { int x; read(fid_read, &x, sizeof(x)); } #define RESUME_CONTROL { int x; write(fid_write, &x, sizeof(x)); } #define TRANSFER_CONTROL {int x; write(fid_write, &x, sizeof(x)); read(fid_read, &x, sizeof(x)); } static void my_sig_handler(int signum) { } void do_process_A() { int fd; int * base; int i; fid_write = fid[0][1]; fid_read = fid[1][0]; printf("A : open file"); fd=open("mmap-test", O_RDWR); assert(fd > 0); base=mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); printf(" map base is %p (@=%d)\n", base, *base); TRANSFER_CONTROL; printf("A: flash base page with 5\n"); for (i=0; i<1024; i++) base[i] = 5; TRANSFER_CONTROL; munmap(base, PAGE_SIZE); close(fd); exit(0); } void do_process_B() { int fd; int * base; int * base1; int i; fid_read = fid[0][0]; fid_write = fid[1][1]; SUSPEND_CONTROL; printf("B : open file"); fd=open("mmap-test", O_RDWR); assert(fd > 0); base1=mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, PAGE_SIZE); base=mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); printf(" map base is %p(@=%d)\n", base, *base); TRANSFER_CONTROL; printf("B : check page at base \n", *base); for (i=0; i<1024; i++) if (base[i] != 5) printf("int at %d is not 5\n", i); RESUME_CONTROL; munmap(base, PAGE_SIZE); munmap(base1, PAGE_SIZE); close(fd); exit(0); } main() { pipe(fid[0]); pipe(fid[1]); { /* create two page file with 0s */ int i; int zero=0; int fd; fd=open("mmap-test", O_CREAT | O_RDWR ); assert(fd > 0); for (i=0; i< 2*PAGE_SIZE; i++) assert(write(fd, &zero, 1)==1); assert(close(fd) == 0); } if (fork()) { do_process_B(); do_process_A(); assert(1 ==0); } if (fork()) { do_process_A(); assert(1 ==0); } wait(NULL); wait(NULL); }