1 /*
2  * Copyright (c) International Business Machines  Corp., 2004
3  *  Written by Robbie Williamson
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 /*
21  * Test Description: Test that a normal page cannot be mapped into a high
22  * memory region.
23  */
24 
25 #include <sys/types.h>
26 #include <sys/mman.h>
27 #include <sys/mount.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include "test.h"
38 #include "safe_macros.h"
39 
40 char *TCID = "mmap15";
41 int TST_TOTAL = 1;
42 
43 #ifdef __ia64__
44 # define HIGH_ADDR (void *)(0xa000000000000000UL)
45 #else
46 # define HIGH_ADDR (void *)(-page_size)
47 #endif
48 
49 static long page_size;
50 
51 static void setup(void);
52 static void cleanup(void);
53 
54 int main(int ac, char **av)
55 {
56 	int lc, fd;
57 	void *addr;
58 
59 #if __WORDSIZE == 32
60 	tst_brkm(TCONF, NULL, "This test is only for 64bit");
61 #endif
62 
63 	tst_parse_opts(ac, av, NULL, NULL);
64 
65 	setup();
66 
67 	for (lc = 0; TEST_LOOPING(lc); lc++) {
68 		tst_count = 0;
69 
70 		fd = SAFE_OPEN(cleanup, "testfile", O_RDWR | O_CREAT, 0666);
71 
72 		/* Attempt to mmap into highmem addr, should get ENOMEM */
73 		addr = mmap(HIGH_ADDR, page_size, PROT_READ,
74 			    MAP_SHARED | MAP_FIXED, fd, 0);
75 		if (addr != MAP_FAILED) {
76 			tst_resm(TFAIL, "mmap into high region "
77 				 "succeeded unexpectedly");
78 			munmap(addr, page_size);
79 			close(fd);
80 			continue;
81 		}
82 
83 		if (errno != ENOMEM) {
84 			tst_resm(TFAIL | TERRNO, "mmap into high region "
85 				 "failed unexpectedly - expect "
86 				 "errno=ENOMEM, got");
87 		} else {
88 			tst_resm(TPASS | TERRNO, "mmap into high region "
89 				 "failed as expected");
90 		}
91 
92 		SAFE_CLOSE(cleanup, fd);
93 	}
94 
95 	cleanup();
96 	tst_exit();
97 }
98 
99 static void setup(void)
100 {
101 	tst_require_root();
102 
103 	tst_tmpdir();
104 
105 	page_size = getpagesize();
106 
107 	TEST_PAUSE;
108 }
109 
110 static void cleanup(void)
111 {
112 	tst_rmdir();
113 }
114