1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2004
4*49cdfc7eSAndroid Build Coastguard Worker * Written by Robbie Williamson
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2014-2023
7*49cdfc7eSAndroid Build Coastguard Worker */
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * Verify that, a normal page cannot be mapped into a high memory region,
13*49cdfc7eSAndroid Build Coastguard Worker * and mmap() call fails with either ENOMEM or EINVAL errno.
14*49cdfc7eSAndroid Build Coastguard Worker */
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #ifdef __ia64__
19*49cdfc7eSAndroid Build Coastguard Worker # define HIGH_ADDR ((void *)(0xa000000000000000UL))
20*49cdfc7eSAndroid Build Coastguard Worker #else
21*49cdfc7eSAndroid Build Coastguard Worker # define HIGH_ADDR ((void *)(-page_size))
22*49cdfc7eSAndroid Build Coastguard Worker #endif
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE "mmapfile"
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker static long page_size;
27*49cdfc7eSAndroid Build Coastguard Worker static int fd;
28*49cdfc7eSAndroid Build Coastguard Worker
run(void)29*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker TESTPTR(mmap(HIGH_ADDR, page_size, PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0));
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET_PTR != MAP_FAILED) {
36*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "mmap() into high mem region succeeded unexpectedly");
37*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(TST_RET_PTR, page_size);
38*49cdfc7eSAndroid Build Coastguard Worker return;
39*49cdfc7eSAndroid Build Coastguard Worker }
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == ENOMEM || TST_ERR == EINVAL)
42*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TERRNO, "mmap() failed with expected errno");
43*49cdfc7eSAndroid Build Coastguard Worker else
44*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO, "mmap() failed with unexpected errno");
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker
setup(void)49*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker page_size = getpagesize();
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)54*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
57*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
61*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
62*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
63*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
64*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1
65*49cdfc7eSAndroid Build Coastguard Worker };
66