robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2004 |
| 3 | * Written by Robbie Williamson |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 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. |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 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. |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 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 |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 21 | * Test Description: Test that a normal page cannot be mapped into a high |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 22 | * memory region. |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/mount.h> |
| 28 | #include <sys/stat.h> |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 29 | #include <errno.h> |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 31 | #include <signal.h> |
| 32 | #include <stdint.h> |
Caspar Zhang | 79667fa | 2012-03-12 14:41:46 +0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 37 | #include "test.h" |
Wanlong Gao | 56d368c | 2012-02-10 11:16:22 +0800 | [diff] [blame] | 38 | #include "safe_macros.h" |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 39 | |
Cyril Hrubis | 3ccc48d | 2014-06-17 14:42:40 +0200 | [diff] [blame] | 40 | char *TCID = "mmap15"; |
Wanlong Gao | 386dfe2 | 2012-02-06 11:16:25 +0800 | [diff] [blame] | 41 | int TST_TOTAL = 1; |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 42 | |
Jan Stancek | 254cba6 | 2014-07-22 17:57:09 +0200 | [diff] [blame] | 43 | #define HIGH_ADDR (void *)(1L << 53) |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 44 | |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 45 | static long map_sz; |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 46 | |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 47 | static void setup(void); |
| 48 | static void cleanup(void); |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 49 | |
Wanlong Gao | 386dfe2 | 2012-02-06 11:16:25 +0800 | [diff] [blame] | 50 | int main(int ac, char **av) |
vapier | 541da0c | 2006-02-11 04:54:12 +0000 | [diff] [blame] | 51 | { |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 52 | int lc, fd; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 53 | const char *msg; |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 54 | void *addr; |
Garrett Cooper | 71543af | 2011-04-21 20:24:03 -0700 | [diff] [blame] | 55 | |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 56 | #if __WORDSIZE == 32 |
Garrett Cooper | 71543af | 2011-04-21 20:24:03 -0700 | [diff] [blame] | 57 | tst_brkm(TCONF, NULL, "This test is only for 64bit"); |
| 58 | #endif |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 59 | |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 60 | msg = parse_opts(ac, av, NULL, NULL); |
Wanlong Gao | c4dd870 | 2012-02-08 17:13:52 +0800 | [diff] [blame] | 61 | if (msg) |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 62 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 63 | |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 64 | setup(); |
| 65 | |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 66 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 67 | tst_count = 0; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 68 | |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 69 | fd = SAFE_OPEN(cleanup, "testfile", O_RDWR | O_CREAT, 0666); |
| 70 | |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 71 | /* Attempt to mmap into highmem addr, should get ENOMEM */ |
| 72 | addr = mmap(HIGH_ADDR, map_sz, PROT_READ, |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 73 | MAP_SHARED | MAP_FIXED, fd, 0); |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 74 | if (addr != MAP_FAILED) { |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 75 | tst_resm(TFAIL, "mmap into high region " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 76 | "succeeded unexpectedly"); |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 77 | munmap(addr, map_sz); |
| 78 | close(fd); |
| 79 | continue; |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 80 | } |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 81 | |
| 82 | if (errno != ENOMEM) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | tst_resm(TFAIL | TERRNO, "mmap into high region " |
| 84 | "failed unexpectedly - expect " |
| 85 | "errno=ENOMEM, got"); |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 86 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | tst_resm(TPASS | TERRNO, "mmap into high region " |
| 88 | "failed as expected"); |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | SAFE_CLOSE(cleanup, fd); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 92 | } |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 93 | |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 94 | cleanup(); |
Garrett Cooper | b0a973a | 2011-01-21 01:15:32 -0800 | [diff] [blame] | 95 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 96 | } |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 97 | |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 98 | static void setup(void) |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 99 | { |
Wanlong Gao | c4dd870 | 2012-02-08 17:13:52 +0800 | [diff] [blame] | 100 | tst_require_root(NULL); |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 101 | |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 102 | tst_tmpdir(); |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 103 | |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 104 | map_sz = getpagesize(); |
Caspar Zhang | 879ae7b | 2012-03-13 18:24:50 +0800 | [diff] [blame] | 105 | |
| 106 | TEST_PAUSE; |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 109 | static void cleanup(void) |
robbiew | bc8d45a | 2004-04-20 21:43:50 +0000 | [diff] [blame] | 110 | { |
Cyril Hrubis | 049bed7 | 2014-06-17 14:32:29 +0200 | [diff] [blame] | 111 | tst_rmdir(); |
Wanlong Gao | 386dfe2 | 2012-02-06 11:16:25 +0800 | [diff] [blame] | 112 | } |