blob: 079ffc65c9253f551191bf8fd8cd822550faf6ec [file] [log] [blame]
robbiewbc8d45a2004-04-20 21:43:50 +00001/*
Cyril Hrubis049bed72014-06-17 14:32:29 +02002 * Copyright (c) International Business Machines Corp., 2004
3 * Written by Robbie Williamson
robbiewbc8d45a2004-04-20 21:43:50 +00004 *
Cyril Hrubis049bed72014-06-17 14:32:29 +02005 * 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.
robbiewbc8d45a2004-04-20 21:43:50 +00009 *
Cyril Hrubis049bed72014-06-17 14:32:29 +020010 * 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.
robbiewbc8d45a2004-04-20 21:43:50 +000014 *
Cyril Hrubis049bed72014-06-17 14:32:29 +020015 * 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
robbiewbc8d45a2004-04-20 21:43:50 +000018 */
19
20/*
subrata_modak4bb656a2009-02-26 12:02:09 +000021 * Test Description: Test that a normal page cannot be mapped into a high
robbiewbc8d45a2004-04-20 21:43:50 +000022 * memory region.
robbiewbc8d45a2004-04-20 21:43:50 +000023 */
24
Caspar Zhang79667fa2012-03-12 14:41:46 +080025#include <sys/types.h>
26#include <sys/mman.h>
27#include <sys/mount.h>
28#include <sys/stat.h>
robbiewbc8d45a2004-04-20 21:43:50 +000029#include <errno.h>
robbiewbc8d45a2004-04-20 21:43:50 +000030#include <fcntl.h>
robbiewbc8d45a2004-04-20 21:43:50 +000031#include <signal.h>
32#include <stdint.h>
Caspar Zhang79667fa2012-03-12 14:41:46 +080033#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
robbiewbc8d45a2004-04-20 21:43:50 +000037#include "test.h"
Wanlong Gao56d368c2012-02-10 11:16:22 +080038#include "safe_macros.h"
robbiewbc8d45a2004-04-20 21:43:50 +000039
Cyril Hrubis3ccc48d2014-06-17 14:42:40 +020040char *TCID = "mmap15";
Wanlong Gao386dfe22012-02-06 11:16:25 +080041int TST_TOTAL = 1;
Caspar Zhang879ae7b2012-03-13 18:24:50 +080042
Jan Stancek254cba62014-07-22 17:57:09 +020043#define HIGH_ADDR (void *)(1L << 53)
Caspar Zhang879ae7b2012-03-13 18:24:50 +080044
Caspar Zhang879ae7b2012-03-13 18:24:50 +080045static long map_sz;
Caspar Zhang879ae7b2012-03-13 18:24:50 +080046
Cyril Hrubis049bed72014-06-17 14:32:29 +020047static void setup(void);
48static void cleanup(void);
robbiewbc8d45a2004-04-20 21:43:50 +000049
Wanlong Gao386dfe22012-02-06 11:16:25 +080050int main(int ac, char **av)
vapier541da0c2006-02-11 04:54:12 +000051{
Cyril Hrubis049bed72014-06-17 14:32:29 +020052 int lc, fd;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020053 const char *msg;
Cyril Hrubis049bed72014-06-17 14:32:29 +020054 void *addr;
Garrett Cooper71543af2011-04-21 20:24:03 -070055
Caspar Zhang879ae7b2012-03-13 18:24:50 +080056#if __WORDSIZE == 32
Garrett Cooper71543af2011-04-21 20:24:03 -070057 tst_brkm(TCONF, NULL, "This test is only for 64bit");
58#endif
robbiewbc8d45a2004-04-20 21:43:50 +000059
Cyril Hrubis049bed72014-06-17 14:32:29 +020060 msg = parse_opts(ac, av, NULL, NULL);
Wanlong Gaoc4dd8702012-02-08 17:13:52 +080061 if (msg)
Caspar Zhang879ae7b2012-03-13 18:24:50 +080062 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewbc8d45a2004-04-20 21:43:50 +000063
robbiewbc8d45a2004-04-20 21:43:50 +000064 setup();
65
robbiewbc8d45a2004-04-20 21:43:50 +000066 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080067 tst_count = 0;
Garrett Cooper2c282152010-12-16 00:55:50 -080068
Cyril Hrubis049bed72014-06-17 14:32:29 +020069 fd = SAFE_OPEN(cleanup, "testfile", O_RDWR | O_CREAT, 0666);
70
Caspar Zhang879ae7b2012-03-13 18:24:50 +080071 /* Attempt to mmap into highmem addr, should get ENOMEM */
72 addr = mmap(HIGH_ADDR, map_sz, PROT_READ,
Cyril Hrubis049bed72014-06-17 14:32:29 +020073 MAP_SHARED | MAP_FIXED, fd, 0);
robbiewbc8d45a2004-04-20 21:43:50 +000074 if (addr != MAP_FAILED) {
Caspar Zhang879ae7b2012-03-13 18:24:50 +080075 tst_resm(TFAIL, "mmap into high region "
Wanlong Gao354ebb42012-12-07 10:10:04 +080076 "succeeded unexpectedly");
Cyril Hrubis049bed72014-06-17 14:32:29 +020077 munmap(addr, map_sz);
78 close(fd);
79 continue;
robbiewbc8d45a2004-04-20 21:43:50 +000080 }
Cyril Hrubis049bed72014-06-17 14:32:29 +020081
82 if (errno != ENOMEM) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 tst_resm(TFAIL | TERRNO, "mmap into high region "
84 "failed unexpectedly - expect "
85 "errno=ENOMEM, got");
Cyril Hrubis049bed72014-06-17 14:32:29 +020086 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 tst_resm(TPASS | TERRNO, "mmap into high region "
88 "failed as expected");
Cyril Hrubis049bed72014-06-17 14:32:29 +020089 }
90
91 SAFE_CLOSE(cleanup, fd);
Garrett Cooper2c282152010-12-16 00:55:50 -080092 }
Cyril Hrubis049bed72014-06-17 14:32:29 +020093
robbiewbc8d45a2004-04-20 21:43:50 +000094 cleanup();
Garrett Cooperb0a973a2011-01-21 01:15:32 -080095 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080096}
robbiewbc8d45a2004-04-20 21:43:50 +000097
Cyril Hrubis049bed72014-06-17 14:32:29 +020098static void setup(void)
robbiewbc8d45a2004-04-20 21:43:50 +000099{
Wanlong Gaoc4dd8702012-02-08 17:13:52 +0800100 tst_require_root(NULL);
Caspar Zhang879ae7b2012-03-13 18:24:50 +0800101
Cyril Hrubis049bed72014-06-17 14:32:29 +0200102 tst_tmpdir();
Caspar Zhang879ae7b2012-03-13 18:24:50 +0800103
Cyril Hrubis049bed72014-06-17 14:32:29 +0200104 map_sz = getpagesize();
Caspar Zhang879ae7b2012-03-13 18:24:50 +0800105
106 TEST_PAUSE;
robbiewbc8d45a2004-04-20 21:43:50 +0000107}
108
Cyril Hrubis049bed72014-06-17 14:32:29 +0200109static void cleanup(void)
robbiewbc8d45a2004-04-20 21:43:50 +0000110{
Cyril Hrubis049bed72014-06-17 14:32:29 +0200111 tst_rmdir();
Wanlong Gao386dfe22012-02-06 11:16:25 +0800112}