1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2004
3f08c3bdfSopenharmony_ci *  Written by Robbie Williamson
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci * This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci * (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci * the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci * along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * Test Description: Test that a normal page cannot be mapped into a high
22f08c3bdfSopenharmony_ci * memory region.
23f08c3bdfSopenharmony_ci */
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#include <sys/types.h>
26f08c3bdfSopenharmony_ci#include <sys/mman.h>
27f08c3bdfSopenharmony_ci#include <sys/mount.h>
28f08c3bdfSopenharmony_ci#include <sys/stat.h>
29f08c3bdfSopenharmony_ci#include <errno.h>
30f08c3bdfSopenharmony_ci#include <fcntl.h>
31f08c3bdfSopenharmony_ci#include <signal.h>
32f08c3bdfSopenharmony_ci#include <stdint.h>
33f08c3bdfSopenharmony_ci#include <stdio.h>
34f08c3bdfSopenharmony_ci#include <stdlib.h>
35f08c3bdfSopenharmony_ci#include <string.h>
36f08c3bdfSopenharmony_ci#include <unistd.h>
37f08c3bdfSopenharmony_ci#include "test.h"
38f08c3bdfSopenharmony_ci#include "safe_macros.h"
39f08c3bdfSopenharmony_ci#include "lapi/abisize.h"
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cichar *TCID = "mmap15";
42f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci#ifdef __ia64__
45f08c3bdfSopenharmony_ci# define HIGH_ADDR (void *)(0xa000000000000000UL)
46f08c3bdfSopenharmony_ci#else
47f08c3bdfSopenharmony_ci# define HIGH_ADDR (void *)(-page_size)
48f08c3bdfSopenharmony_ci#endif
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic long page_size;
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void setup(void);
53f08c3bdfSopenharmony_cistatic void cleanup(void);
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ciint main(int ac, char **av)
56f08c3bdfSopenharmony_ci{
57f08c3bdfSopenharmony_ci	int lc, fd;
58f08c3bdfSopenharmony_ci	void *addr;
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci#ifdef TST_ABI32
61f08c3bdfSopenharmony_ci	tst_brkm(TCONF, NULL, "This test is only for 64bit");
62f08c3bdfSopenharmony_ci#endif
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	setup();
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
69f08c3bdfSopenharmony_ci		tst_count = 0;
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci		fd = SAFE_OPEN(cleanup, "testfile", O_RDWR | O_CREAT, 0666);
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci		/* Attempt to mmap into highmem addr, should get ENOMEM */
74f08c3bdfSopenharmony_ci		addr = mmap(HIGH_ADDR, page_size, PROT_READ,
75f08c3bdfSopenharmony_ci			    MAP_SHARED | MAP_FIXED, fd, 0);
76f08c3bdfSopenharmony_ci		if (addr != MAP_FAILED) {
77f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "mmap into high region "
78f08c3bdfSopenharmony_ci				 "succeeded unexpectedly");
79f08c3bdfSopenharmony_ci			munmap(addr, page_size);
80f08c3bdfSopenharmony_ci			close(fd);
81f08c3bdfSopenharmony_ci			continue;
82f08c3bdfSopenharmony_ci		}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci		if (errno != ENOMEM && errno != EINVAL) {
85f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TERRNO, "mmap into high region "
86f08c3bdfSopenharmony_ci				 "failed unexpectedly");
87f08c3bdfSopenharmony_ci		} else {
88f08c3bdfSopenharmony_ci			tst_resm(TPASS | TERRNO, "mmap into high region "
89f08c3bdfSopenharmony_ci				 "failed as expected");
90f08c3bdfSopenharmony_ci		}
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci		SAFE_CLOSE(cleanup, fd);
93f08c3bdfSopenharmony_ci	}
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	cleanup();
96f08c3bdfSopenharmony_ci	tst_exit();
97f08c3bdfSopenharmony_ci}
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_cistatic void setup(void)
100f08c3bdfSopenharmony_ci{
101f08c3bdfSopenharmony_ci	tst_require_root();
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	tst_tmpdir();
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	page_size = getpagesize();
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	TEST_PAUSE;
108f08c3bdfSopenharmony_ci}
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_cistatic void cleanup(void)
111f08c3bdfSopenharmony_ci{
112f08c3bdfSopenharmony_ci	tst_rmdir();
113f08c3bdfSopenharmony_ci}
114