Imported Upstream version 20091031
[ltp-debian.git] / testcases / kernel / mem / hugetlb / hugemmap / hugemmap03.c
bloba07b9aa0ea8ab3fd378fb71951734c61543f6f5b
1 /*
3 * Copyright (c) International Business Machines Corp., 2004
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Test Name: hugemmap03
23 * Test Description: Test that a normal page cannot be mapped into a high
24 * memory region.
26 * Usage: <for command-line>
27 * hugemmap03 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
28 * where, -c n : Run n copies concurrently.
29 * -f : Turn off functionality Testing.
30 * -i n : Execute test n times.
31 * -I x : Execute test for x seconds.
32 * -P x : Pause for x seconds between iterations.
33 * -t : Turn on syscall timing.
35 * HISTORY
36 * 04/2004 Written by Robbie Williamson
38 * RESTRICTIONS:
39 * Must be compiled in 64-bit mode.
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <stdint.h>
50 #include <sys/stat.h>
51 #include <sys/mman.h>
52 #include <sys/types.h>
54 #include "test.h"
55 #include "usctest.h"
57 #define PAGE_SIZE ((1UL) << 12) /* Normal page size */
58 #define HIGH_ADDR (void *)(0x1000000000000)
60 char* TEMPFILE="mmapfile";
62 char *TCID="hugemmap03"; /* Test program identifier. */
63 int TST_TOTAL=1; /* Total number of test cases. */
64 extern int Tst_count; /* Test Case counter for tst_* routines */
65 unsigned long *addr; /* addr of memory mapped region */
66 int fildes; /* file descriptor for tempfile */
67 char *Hopt; /* location of hugetlbfs */
69 void setup(); /* Main setup function of test */
70 void cleanup(); /* cleanup function for the test */
72 void help()
74 printf(" -H /.. Location of hugetlbfs, i.e. -H /var/hugetlbfs \n");
77 int
78 main(int ac, char **av)
80 #if __WORDSIZE==32 /* 32-bit compiled */
81 tst_resm(TCONF,"This test is only for 64bit");
82 tst_exit();
83 /*NOTREACHED*/
84 return 1;
85 #else /* 64-bit compiled */
86 int lc; /* loop counter */
87 char *msg; /* message returned from parse_opts */
88 int Hflag=0; /* binary flag: opt or not */
90 option_t options[] = {
91 { "H:", &Hflag, &Hopt }, /* Required for location of hugetlbfs */
92 { NULL, NULL, NULL } /* NULL required to end array */
95 /* Parse standard options given to run the test. */
96 msg = parse_opts(ac, av, options, &help);
97 if (msg != (char *) NULL) {
98 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s, use -help", msg);
99 tst_exit();
102 if (Hflag == 0) {
103 tst_brkm(TBROK, NULL, "-H option is REQUIRED for this test, use -h for options help");
104 tst_exit();
107 /* Perform global setup for test */
108 setup();
110 /* Check looping state if -i option given */
111 for (lc = 0; TEST_LOOPING(lc); lc++) {
113 /* Creat a temporary file used for huge mapping */
114 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
115 tst_brkm(TFAIL, cleanup,
116 "open() on %s Failed, errno=%d : %s",
117 TEMPFILE, errno, strerror(errno));
120 /* Reset Tst_count in case we are looping. */
121 Tst_count=0;
123 /* Attempt to mmap using normal pages and a high memory address */
124 errno = 0;
125 addr = mmap(HIGH_ADDR, PAGE_SIZE, PROT_READ,
126 MAP_SHARED | MAP_FIXED, fildes, 0);
127 if (addr != MAP_FAILED) {
128 tst_resm(TFAIL, "Normal mmap() into high region unexpectedly succeeded on %s, errno=%d : %s",
129 TEMPFILE, errno, strerror(errno));
130 continue;
131 } else {
132 tst_resm(TPASS, "Normal mmap() into high region failed correctly");
133 break;
136 close(fildes);
137 } /* End for TEST_LOOPING */
139 /* Call cleanup() to undo setup done for the test. */
140 cleanup();
142 /*NOTREACHED*/
143 return 1;
144 #endif
145 } /* End main */
148 * setup() - performs all ONE TIME setup for this test.
150 * Get system page size, allocate and initialize the string dummy.
151 * Initialize addr such that it is more than one page below the break
152 * address of the process, and initialize one page region from addr
153 * with char 'A'.
154 * Creat a temporary directory and a file under it.
155 * Write some known data into file and get the size of the file.
157 void
158 setup()
160 char mypid[40];
162 sprintf(mypid,"/%d",getpid());
163 TEMPFILE=strcat(mypid,TEMPFILE);
164 TEMPFILE=strcat(Hopt,TEMPFILE);
166 /* capture signals */
167 tst_sig(FORK, DEF_HANDLER, cleanup);
169 /* Pause if that option was specified */
170 TEST_PAUSE;
176 * cleanup() - performs all ONE TIME cleanup for this test at
177 * completion or premature exit.
178 * Remove the temporary directory created.
180 void
181 cleanup()
184 * print timing stats if that option was specified.
186 TEST_CLEANUP;
188 unlink(TEMPFILE);
190 /* exit with return code appropriate for results */
191 tst_exit();