/* * test program to demonstrate possible cache aliasing problem with O_DRECT * option on IDE files. * * Problem exists on NEC rochopper boards with vr5432/vr5500 CPUs. However * it did not show up with vr4131 cpu and toshiba CPUs, which is unexpected. */ #define _GNU_SOURCE #include #include #include #include #include #include #define IOSIZE 32768 void *aligned_alloc(int size, int align) { void *p = malloc(size + align); return (void*) (((unsigned)p + align-1) / align * align); } void check_buffer(char *p, int round) { int *q= (int*)p; int intsize = IOSIZE / 4; int i; int base=round*intsize; for (i=0; i< intsize; i++, q++) if (*q != base+i) printf("error at (%d, %d): got %d, expect %d\n", round, i, *q, base+i); } void dcp(int sfd) { int zfd; int r, w; char *p; int round=0; #if 0 zfd = open("/dev/zero", O_RDWR); p = mmap(NULL, IOSIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, zfd, 0); close(zfd); #endif p = aligned_alloc(IOSIZE, 4096); printf("buffer alloced/mapped to memory area: %x\n", p); while (1) { memset(p, 0, IOSIZE); r = read(sfd, p, IOSIZE); if (r <= 0) break; check_buffer(p, round); round++; } } int main(int argc, char *argv[]) { int sfd; int ret; sfd = open(argv[1], O_RDONLY | O_DIRECT); printf ("sfd ret = %d\n", sfd); dcp(sfd); return 0; }