1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) National ICT Australia, 2006
4 * Author: carl.vanschaik at nicta.com.au
5 */
6
7 /*\
8 * [Description]
9 *
10 * If the kernel fails to correctly flush the TLB entry, the second mmap
11 * will not show the correct data.
12 *
13 * [Algorithm]
14 * - create two files, write known data to the files
15 * - mmap the files, verify data
16 * - unmap files
17 * - remmap files, swap virtual addresses
18 * - check wheather if the memory content is correct
19 */
20
21 #include <string.h>
22 #include "tst_test.h"
23
24 #define LEN 64
25
26 static int f1 = -1, f2 = -1;
27 static char *mm1 = NULL, *mm2 = NULL;
28
29 static const char tmp1[] = "testfile1";
30 static const char tmp2[] = "testfile2";
31
32 static const char str1[] = "testing 123";
33 static const char str2[] = "my test mem";
34
run(void)35 static void run(void)
36 {
37
38 char *save_mm1, *save_mm2;
39
40 mm1 = SAFE_MMAP(0, LEN, PROT_READ, MAP_PRIVATE, f1, 0);
41 mm2 = SAFE_MMAP(0, LEN, PROT_READ, MAP_PRIVATE, f2, 0);
42
43 save_mm1 = mm1;
44 save_mm2 = mm2;
45
46 if (strncmp(str1, mm1, strlen(str1)))
47 tst_brk(TFAIL, "failed on compare %s", tmp1);
48
49 if (strncmp(str2, mm2, strlen(str2)))
50 tst_brk(TFAIL, "failed on compare %s", tmp2);
51
52 SAFE_MUNMAP(mm1, LEN);
53 SAFE_MUNMAP(mm2, LEN);
54
55 mm1 = SAFE_MMAP(save_mm2, LEN, PROT_READ, MAP_PRIVATE, f1, 0);
56 mm2 = SAFE_MMAP(save_mm1, LEN, PROT_READ, MAP_PRIVATE, f2, 0);
57
58 if (mm1 != save_mm2 || mm2 != save_mm1)
59 tst_res(TINFO, "mmap not using same address");
60
61 if (strncmp(str1, mm1, strlen(str1)))
62 tst_brk(TFAIL, "failed on compare %s", tmp1);
63
64 if (strncmp(str2, mm2, strlen(str2)))
65 tst_brk(TFAIL, "failed on compare %s", tmp2);
66
67 tst_res(TPASS, "memory test succeeded");
68 }
69
setup(void)70 static void setup(void)
71 {
72 f1 = SAFE_OPEN(tmp1, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
73 f2 = SAFE_OPEN(tmp2, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
74
75 SAFE_WRITE(SAFE_WRITE_ALL, f1, str1, strlen(str1));
76 SAFE_WRITE(SAFE_WRITE_ALL, f2, str2, strlen(str2));
77 }
78
cleanup(void)79 static void cleanup(void)
80 {
81 if (mm1)
82 SAFE_MUNMAP(mm1, LEN);
83
84 if (mm2)
85 SAFE_MUNMAP(mm2, LEN);
86
87 if (f1 != -1)
88 SAFE_CLOSE(f1);
89 if (f2 != -1)
90 SAFE_CLOSE(f2);
91 }
92
93 static struct tst_test test = {
94 .needs_tmpdir = 1,
95 .test_all = run,
96 .setup = setup,
97 .cleanup = cleanup,
98 };
99
100