1 /*
2  * Check decoding of remap_file_pages syscall.
3  *
4  * Copyright (c) 2016-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 #include <asm/unistd.h>
33 
34 #ifdef __NR_remap_file_pages
35 
36 # include <stdio.h>
37 # include <stdint.h>
38 # include <unistd.h>
39 # include <linux/mman.h>
40 
41 static const char *errstr;
42 
43 static long
k_remap_file_pages(const kernel_ulong_t addr,const kernel_ulong_t size,const kernel_ulong_t prot,const kernel_ulong_t pgoff,const kernel_ulong_t flags)44 k_remap_file_pages(const kernel_ulong_t addr,
45 		   const kernel_ulong_t size,
46 		   const kernel_ulong_t prot,
47 		   const kernel_ulong_t pgoff,
48 		   const kernel_ulong_t flags)
49 {
50 	const long rc = syscall(__NR_remap_file_pages,
51 				addr, size, prot, pgoff, flags);
52 	errstr = sprintrc(rc);
53 	return rc;
54 }
55 
56 int
main(void)57 main(void)
58 {
59 	kernel_ulong_t addr = (kernel_ulong_t) 0xfacefeeddeadbeefULL;
60 	kernel_ulong_t size = (kernel_ulong_t) 0xdefaced1bad2f00dULL;
61 	kernel_ulong_t prot = PROT_READ|PROT_WRITE|PROT_EXEC;
62 	kernel_ulong_t pgoff = (kernel_ulong_t) 0xcaf3babebad4deedULL;
63 	kernel_ulong_t flags = MAP_PRIVATE|MAP_ANONYMOUS;
64 
65 	k_remap_file_pages(addr, size, prot, pgoff, flags);
66 	printf("remap_file_pages(%#jx, %ju, %s, %ju, %s) = %s\n",
67 	       (uintmax_t) addr, (uintmax_t) size,
68 	       "PROT_READ|PROT_WRITE|PROT_EXEC",
69 	       (uintmax_t) pgoff, "MAP_PRIVATE|MAP_ANONYMOUS", errstr);
70 
71 #ifdef MAP_HUGETLB
72 # ifndef MAP_HUGE_2MB
73 #  ifndef MAP_HUGE_SHIFT
74 #   define MAP_HUGE_SHIFT 26
75 #  endif
76 #  define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
77 # endif /* !MAP_HUGE_2MB */
78 	addr = (kernel_ulong_t) 0xfacefeeddeadf00dULL;
79 	size = (kernel_ulong_t) 0xdefaced1bad2beefULL;
80 	prot = (kernel_ulong_t) 0xdefaced00000000ULL | PROT_NONE;
81 	flags = MAP_TYPE | MAP_FIXED | MAP_NORESERVE | MAP_HUGETLB | MAP_HUGE_2MB;
82 
83 	k_remap_file_pages(addr, size, prot, pgoff, flags);
84 	printf("remap_file_pages(%#jx, %ju, %s, %ju"
85 /*
86  * HP PA-RISC is the only architecture that has MAP_TYPE defined to 0x3, which
87  * is also used for MAP_SHARED_VALIDATE since Linux commit v4.15-rc1~71^2^2~23.
88  */
89 # ifdef __hppa__
90 	       ", MAP_SHARED_VALIDATE"
91 # else
92 	       ", 0xf /* MAP_??? */"
93 # endif
94 	       "|MAP_FIXED|MAP_NORESERVE|MAP_HUGETLB|21<<MAP_HUGE_SHIFT)"
95 	       " = %s\n",
96 	       (uintmax_t) addr, (uintmax_t) size,
97 	       prot == PROT_NONE ? "PROT_NONE" :
98 				   "0xdefaced00000000 /* PROT_??? */",
99 	       (uintmax_t) pgoff, errstr);
100 #endif /* MAP_HUGETLB */
101 
102 	puts("+++ exited with 0 +++");
103 	return 0;
104 }
105 
106 #else
107 
108 SKIP_MAIN_UNDEFINED("__NR_remap_file_pages")
109 
110 #endif
111