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