blob: a2dd02847acdc4ebd9d3fa957961222a33106083 [file] [log] [blame]
vapierb56735e2006-08-21 07:05:41 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
vapierb56735e2006-08-21 07:05:41 +000018 */
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
vapierb56735e2006-08-21 07:05:41 +000043#include <stdio.h>
44#include <signal.h>
45#include <stdlib.h>
46#include "test.h"
vapierb56735e2006-08-21 07:05:41 +000047#include <unistd.h>
48#include <errno.h>
49#include <time.h>
50#include <string.h>
51#include <sys/types.h>
subrata_modak56207ce2009-03-23 13:35:39 +000052#include <sys/stat.h> /* definitions for open() */
53#include <sys/mman.h> /* definitions for mmap() */
54#include <fcntl.h> /* definition of open() */
vapierb56735e2006-08-21 07:05:41 +000055#include <sys/user.h>
56
subrata_modak56207ce2009-03-23 13:35:39 +000057#define FAILED (-1) /* return status for all funcs indicating failure */
58#define SUCCESS 0 /* return status for all routines indicating success */
vapierb56735e2006-08-21 07:05:41 +000059
subrata_modak8c5ab482008-02-27 14:31:11 +000060static void setup();
61static void cleanup();
62
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020063char *TCID = "mem03";
64int TST_TOTAL = 1;
vapierb56735e2006-08-21 07:05:41 +000065
subrata_modak56207ce2009-03-23 13:35:39 +000066int f1 = -1, f2 = -1;
67char *mm1 = NULL, *mm2 = NULL;
vapierb56735e2006-08-21 07:05:41 +000068
69/*--------------------------------------------------------------------*/
Mike Frysingerc57fba52014-04-09 18:56:30 -040070int main(void)
vapierb56735e2006-08-21 07:05:41 +000071{
vapierb56735e2006-08-21 07:05:41 +000072 char tmp1[] = "./tmp.file.1";
73 char tmp2[] = "./tmp.file.2";
74
75 char str1[] = "testing 123";
76 char str2[] = "my test mem";
77
subrata_modak8c5ab482008-02-27 14:31:11 +000078 setup();
vapierb56735e2006-08-21 07:05:41 +000079
subrata_modak56207ce2009-03-23 13:35:39 +000080 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);
subrata_modak8c5ab482008-02-27 14:31:11 +000082
subrata_modak56207ce2009-03-23 13:35:39 +000083 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);
vapierb56735e2006-08-21 07:05:41 +000085
86 write(f1, str1, strlen(str1));
87 write(f2, str2, strlen(str2));
88
89 {
vapierb56735e2006-08-21 07:05:41 +000090 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
subrata_modak56207ce2009-03-23 13:35:39 +000095 if ((mm1 == (void *)-1) || (mm2 == (void *)-1))
subrata_modak8c5ab482008-02-27 14:31:11 +000096 tst_brkm(TFAIL, cleanup, "mmap failed");
vapierb56735e2006-08-21 07:05:41 +000097
98 save_mm1 = mm1;
99 save_mm2 = mm2;
100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 if (strncmp(str1, mm1, strlen(str1)))
102 tst_brkm(TFAIL, cleanup, "failed on compare %s", tmp1);
vapierb56735e2006-08-21 07:05:41 +0000103
subrata_modak56207ce2009-03-23 13:35:39 +0000104 if (strncmp(str2, mm2, strlen(str2)))
105 tst_brkm(TFAIL, cleanup, "failed on compare %s", tmp2);
vapierb56735e2006-08-21 07:05:41 +0000106
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
subrata_modak56207ce2009-03-23 13:35:39 +0000113 if ((mm1 == (void *)-1) || (mm2 == (void *)-1))
subrata_modak8c5ab482008-02-27 14:31:11 +0000114 tst_brkm(TFAIL, cleanup, "second mmap failed");
vapierb56735e2006-08-21 07:05:41 +0000115
subrata_modak56207ce2009-03-23 13:35:39 +0000116 if (mm1 != save_mm2) {
vapierb56735e2006-08-21 07:05:41 +0000117 printf("mmap not using same address\n");
Garrett Cooper2c282152010-12-16 00:55:50 -0800118
vapierb56735e2006-08-21 07:05:41 +0000119 }
120
subrata_modak56207ce2009-03-23 13:35:39 +0000121 if (mm2 != save_mm1) {
vapierb56735e2006-08-21 07:05:41 +0000122 printf("mmap not using same address\n");
Garrett Cooper2c282152010-12-16 00:55:50 -0800123
vapierb56735e2006-08-21 07:05:41 +0000124 }
125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 if (strncmp(str1, mm1, strlen(str1)))
127 tst_brkm(TFAIL, cleanup, "failed on compare %s", tmp1);
vapierb56735e2006-08-21 07:05:41 +0000128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 if (strncmp(str2, mm2, strlen(str2)))
130 tst_brkm(TFAIL, cleanup, "failed on compare %s", tmp2);
131
vapierb56735e2006-08-21 07:05:41 +0000132 munmap(mm1, 64);
133 munmap(mm2, 64);
134 }
135
subrata_modak56207ce2009-03-23 13:35:39 +0000136 tst_resm(TPASS, "%s memory test succeeded", TCID);
subrata_modak8c5ab482008-02-27 14:31:11 +0000137
138 /* clean up and exit */
139 cleanup();
140
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800141 tst_exit();
vapierb56735e2006-08-21 07:05:41 +0000142}
143
subrata_modak8c5ab482008-02-27 14:31:11 +0000144/*
145 * setup() - performs all ONE TIME setup for this test
146 */
subrata_modak56207ce2009-03-23 13:35:39 +0000147void setup(void)
subrata_modak8c5ab482008-02-27 14:31:11 +0000148{
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 */
subrata_modak56207ce2009-03-23 13:35:39 +0000159void cleanup(void)
subrata_modak8c5ab482008-02-27 14:31:11 +0000160{
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
subrata_modak8c5ab482008-02-27 14:31:11 +0000171 tst_rmdir();
172
Chris Dearmanec6edca2012-10-17 19:54:01 -0700173}