1 /*
2 *
3 * Copyright (c) National ICT Australia, 2006
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /* NICTA */
21 /* 13/02/2006 Implemented carl.vanschaik at nicta.com.au */
22
23 /* mem03.c */
24 /*
25 * NAME
26 * mem03.c
27 *
28 * DESCRIPTION
29 * - create two files, write known data to the files.
30 * - mmap the files, verify data
31 * - unmap files
32 * - remmap files, swap virtual addresses ie: file1 at file2's address, etc
33 *
34 * REASONING
35 * - If the kernel fails to correctly flush the TLB entry, the second mmap
36 * will not show the correct data.
37 *
38 *
39 * RESTRICTIONS
40 * None
41 */
42
43 #include <stdio.h>
44 #include <signal.h>
45 #include <stdlib.h>
46 #include "test.h"
47 #include <unistd.h>
48 #include <errno.h>
49 #include <time.h>
50 #include <string.h>
51 #include <sys/types.h>
52 #include <sys/stat.h> /* definitions for open() */
53 #include <sys/mman.h> /* definitions for mmap() */
54 #include <fcntl.h> /* definition of open() */
55 #include <sys/user.h>
56
57 #define FAILED (-1) /* return status for all funcs indicating failure */
58 #define SUCCESS 0 /* return status for all routines indicating success */
59
60 static void setup();
61 static void cleanup();
62
63 char *TCID = "mem03";
64 int TST_TOTAL = 1;
65
66 int f1 = -1, f2 = -1;
67 char *mm1 = NULL, *mm2 = NULL;
68
69 /*--------------------------------------------------------------------*/
main(void)70 int main(void)
71 {
72 char tmp1[] = "./tmp.file.1";
73 char tmp2[] = "./tmp.file.2";
74
75 char str1[] = "testing 123";
76 char str2[] = "my test mem";
77
78 setup();
79
80 if ((f1 = open(tmp1, O_RDWR | O_CREAT, S_IREAD | S_IWRITE)) == -1)
81 tst_brkm(TFAIL, cleanup, "failed to open/create file %s", tmp1);
82
83 if ((f2 = open(tmp2, O_RDWR | O_CREAT, S_IREAD | S_IWRITE)) == -1)
84 tst_brkm(TFAIL, cleanup, "failed to open/create file %s", tmp2);
85
86 write(f1, str1, strlen(str1));
87 write(f2, str2, strlen(str2));
88
89 {
90 char *save_mm1, *save_mm2;
91
92 mm1 = mmap(0, 64, PROT_READ, MAP_PRIVATE, f1, 0);
93 mm2 = mmap(0, 64, PROT_READ, MAP_PRIVATE, f2, 0);
94
95 if ((mm1 == (void *)-1) || (mm2 == (void *)-1))
96 tst_brkm(TFAIL, cleanup, "mmap failed");
97
98 save_mm1 = mm1;
99 save_mm2 = mm2;
100
101 if (strncmp(str1, mm1, strlen(str1)))
102 tst_brkm(TFAIL, cleanup, "failed on compare %s", tmp1);
103
104 if (strncmp(str2, mm2, strlen(str2)))
105 tst_brkm(TFAIL, cleanup, "failed on compare %s", tmp2);
106
107 munmap(mm1, 64);
108 munmap(mm2, 64);
109
110 mm1 = mmap(save_mm2, 64, PROT_READ, MAP_PRIVATE, f1, 0);
111 mm2 = mmap(save_mm1, 64, PROT_READ, MAP_PRIVATE, f2, 0);
112
113 if ((mm1 == (void *)-1) || (mm2 == (void *)-1))
114 tst_brkm(TFAIL, cleanup, "second mmap failed");
115
116 if (mm1 != save_mm2) {
117 printf("mmap not using same address\n");
118
119 }
120
121 if (mm2 != save_mm1) {
122 printf("mmap not using same address\n");
123
124 }
125
126 if (strncmp(str1, mm1, strlen(str1)))
127 tst_brkm(TFAIL, cleanup, "failed on compare %s", tmp1);
128
129 if (strncmp(str2, mm2, strlen(str2)))
130 tst_brkm(TFAIL, cleanup, "failed on compare %s", tmp2);
131
132 munmap(mm1, 64);
133 munmap(mm2, 64);
134 }
135
136 tst_resm(TPASS, "%s memory test succeeded", TCID);
137
138 /* clean up and exit */
139 cleanup();
140
141 tst_exit();
142 }
143
144 /*
145 * setup() - performs all ONE TIME setup for this test
146 */
setup(void)147 void setup(void)
148 {
149 /*
150 * Create a temporary directory and cd into it.
151 */
152 tst_tmpdir();
153 }
154
155 /*
156 * cleanup() - performs all the ONE TIME cleanup for this test at completion
157 * or premature exit.
158 */
cleanup(void)159 void cleanup(void)
160 {
161 if (mm1)
162 munmap(mm1, 64);
163 if (mm2)
164 munmap(mm2, 64);
165
166 if (f1 != -1)
167 close(f1);
168 if (f2 != -1)
169 close(f2);
170
171 tst_rmdir();
172
173 }
174