1 /*
2 * Check decoding of remap_file_pages syscall.
3 *
4 * Copyright (c) 2016-2017 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2016-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include "scno.h"
13
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <unistd.h>
17 #include <linux/mman.h>
18
19 static const char *errstr;
20
21 static long
22 k_remap_file_pages(const kernel_ulong_t addr,
23 const kernel_ulong_t size,
24 const kernel_ulong_t prot,
25 const kernel_ulong_t pgoff,
26 const kernel_ulong_t flags)
27 {
28 const long rc = syscall(__NR_remap_file_pages,
29 addr, size, prot, pgoff, flags);
30 errstr = sprintrc(rc);
31 return rc;
32 }
33
34 int
35 main(void)
36 {
37 kernel_ulong_t addr = (kernel_ulong_t) 0xfacefeeddeadbeefULL;
38 kernel_ulong_t size = (kernel_ulong_t) 0xdefaced1bad2f00dULL;
39 kernel_ulong_t prot = PROT_READ|PROT_WRITE|PROT_EXEC;
40 kernel_ulong_t pgoff = (kernel_ulong_t) 0xcaf3babebad4deedULL;
41 kernel_ulong_t flags = MAP_PRIVATE|MAP_ANONYMOUS;
42 #define prot1_str "PROT_READ|PROT_WRITE|PROT_EXEC"
43 #define flags1_str "MAP_PRIVATE|MAP_ANONYMOUS"
44
45 k_remap_file_pages(addr, size, prot, pgoff, flags);
46 #if XLAT_RAW
47 printf("remap_file_pages(%#jx, %ju, %#jx, %ju, %#jx) = %s\n",
48 (uintmax_t) addr, (uintmax_t) size, (uintmax_t) prot,
49 (uintmax_t) pgoff, (uintmax_t) flags, errstr);
50 #elif XLAT_VERBOSE
51 printf("remap_file_pages(%#jx, %ju, %#jx /* %s */, %ju, %#jx /* %s */)"
52 " = %s\n",
53 (uintmax_t) addr, (uintmax_t) size, (uintmax_t) prot, prot1_str,
54 (uintmax_t) pgoff, (uintmax_t) flags,
55 flags1_str, errstr);
56 #else /* XLAT_ABBREV */
57 printf("remap_file_pages(%#jx, %ju, %s, %ju, %s) = %s\n",
58 (uintmax_t) addr, (uintmax_t) size, prot1_str,
59 (uintmax_t) pgoff, flags1_str, errstr);
60 #endif
61
62 #ifdef MAP_HUGETLB
63 # ifndef MAP_HUGE_2MB
64 # ifndef MAP_HUGE_SHIFT
65 # define MAP_HUGE_SHIFT 26
66 # endif
67 # define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
68 # endif /* !MAP_HUGE_2MB */
69 addr = (kernel_ulong_t) 0xfacefeeddeadf00dULL;
70 size = (kernel_ulong_t) 0xdefaced1bad2beefULL;
71 prot = (kernel_ulong_t) 0xdefaced00000000ULL | PROT_NONE;
72 flags = MAP_TYPE | MAP_FIXED | MAP_NORESERVE | MAP_HUGETLB | MAP_HUGE_2MB;
73
74 k_remap_file_pages(addr, size, prot, pgoff, flags);
75
76 /*
77 * HP PA-RISC is the only architecture that has MAP_TYPE defined to something
78 * different. For example, before commit v4.17-rc1~146^2~9 it was defined to
79 * 0x3 which is also used for MAP_SHARED_VALIDATE since Linux commit
80 * v4.15-rc1~71^2^2~23.
81 */
82 # ifdef __hppa__
83 # if MAP_TYPE == 0x03
84 # define MAP_TYPE_str "MAP_SHARED_VALIDATE"
85 # else
86 # define MAP_TYPE_str "0x2b /* MAP_??? */"
87 # endif
88 # else
89 # define MAP_TYPE_str "0xf /* MAP_??? */"
90 # endif
91 # define flags2_str \
92 MAP_TYPE_str "|MAP_FIXED|MAP_NORESERVE|MAP_HUGETLB|21<<MAP_HUGE_SHIFT"
93
94 # if XLAT_RAW
95 printf("remap_file_pages(%#jx, %ju, %#jx, %ju, %#jx) = %s\n",
96 (uintmax_t) addr, (uintmax_t) size, (uintmax_t) prot,
97 (uintmax_t) pgoff, (uintmax_t) flags, errstr);
98 # elif XLAT_VERBOSE
99 printf("remap_file_pages(%#jx, %ju, %#jx /* %s */, %ju, %#jx /* %s */)"
100 " = %s\n",
101 (uintmax_t) addr, (uintmax_t) size, (uintmax_t) prot,
102 prot == PROT_NONE ? "PROT_NONE" : "PROT_???",
103 (uintmax_t) pgoff, (uintmax_t) flags, flags2_str, errstr);
104 # else /* XLAT_ABBREV */
105 printf("remap_file_pages(%#jx, %ju, %s, %ju, %s) = %s\n",
106 (uintmax_t) addr, (uintmax_t) size,
107 prot == PROT_NONE ? "PROT_NONE" :
108 "0xdefaced00000000 /* PROT_??? */",
109 (uintmax_t) pgoff, flags2_str, errstr);
110 # endif
111 #endif /* MAP_HUGETLB */
112
113 puts("+++ exited with 0 +++");
114 return 0;
115 }