1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017 Intel Corporation. */
11 #include <sys/types.h>
14 #define MiB(a) ((a)*1024*1024)
16 void err_exit(char *op)
18 fprintf(stderr, "%s: %s\n", op, strerror(errno));
22 int main(int argc, char *argv[])
24 volatile int a __attribute__((__unused__));
25 char *buffer = "HELLO WORLD!";
30 printf("Usage: %s <pmem file>\n", basename(argv[0]));
34 fd = open(argv[1], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
39 * This allows us to map a huge zero page, and we do it at a non-zero
40 * offset for a little additional testing.
43 ftruncate(fd, MiB(4));
45 data = mmap(NULL, MiB(2), PROT_READ, MAP_SHARED, fd, MiB(2));
46 if (data == MAP_FAILED)
50 * This faults in a 2MiB zero page to satisfy the read.
51 * 'a' is volatile so this read doesn't get optimized out.
55 pwrite(fd, buffer, strlen(buffer), MiB(2));
58 * Try and use the mmap to read back the data we just wrote with
59 * pwrite(). If the kernel bug is present the mapping from the 2MiB
60 * zero page will still be intact, and we'll read back zeros instead.
62 if (strncmp(buffer, data, strlen(buffer))) {
63 fprintf(stderr, "strncmp mismatch: '%s' vs '%s'\n", buffer,
68 err = munmap(data, MiB(2));