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