#include #include #include #include #define PAGE_SIZE 4096 #define MAGIC 0x79128347ul int main(void) { void *p; volatile unsigned long *m; int ret; /* Map a frame for the test. */ m = p = mmap(NULL, PAGE_SIZE, PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); if (p == MAP_FAILED) { perror("mmap"); exit(1); } /* Write to the frame. */ *m = MAGIC; /* Set PROT_NONE on the mapping. */ ret = mprotect(p, PAGE_SIZE, PROT_NONE); if (ret < 0) { perror("mprotect"); exit(1); } /* * Set PROT_READ (or any other non-PROT_NONE protection) on the * mapping. * * On a buggy system pte_mknonnuma() will clear _PAGE_PROTNONE (== * _PAGE_NUMA) and set _PAGE_PRESENT /without/ doing a PFN to MFN * conversion. * * The resulting mapping will be to the wrong MFN. */ ret = mprotect(p, PAGE_SIZE, PROT_READ); if (ret < 0) { perror("mprotect"); exit(1); } /* Check we're still mapping the original frame. */ if (*m != MAGIC) { printf("FAIL (0x%lx != 0x%lx)\n", *m, MAGIC); ret = 1; } /* * On a buggy system, the munmap() will usually trigger a "Bad * page" error since the mapping isn't for the original page. */ munmap(p, PAGE_SIZE); return ret; }