1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c)      2016 Red Hat, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 #include <asm/unistd.h>
31 
32 #if defined __NR_userfaultfd && defined HAVE_LINUX_USERFAULTFD_H
33 
34 # include <fcntl.h>
35 # include <inttypes.h>
36 # include <stdint.h>
37 # include <stdio.h>
38 # include <string.h>
39 # include <unistd.h>
40 
41 # include <sys/ioctl.h>
42 # include <sys/mman.h>
43 # include <linux/ioctl.h>
44 # include <linux/userfaultfd.h>
45 
46 int
main(void)47 main(void)
48 {
49 	int rc;
50 	int fd = syscall(__NR_userfaultfd, O_NONBLOCK);
51 	size_t pagesize = getpagesize();
52 
53 	if (fd < 0)
54 		perror_msg_and_skip("userfaultfd");
55 
56 	/* ---- API ---- */
57 	struct uffdio_api *api_struct = tail_alloc(sizeof(*api_struct));
58 
59 	/* With a bad fd */
60 	memset(api_struct, 0, sizeof(*api_struct));
61 	rc = ioctl(-1, UFFDIO_API, api_struct);
62 	printf("ioctl(-1, UFFDIO_API, {api=0, features=0}) = %d %s (%m)\n",
63 	       rc, errno2name());
64 	/* With a bad pointer */
65 	rc = ioctl(fd, UFFDIO_API, NULL);
66 	printf("ioctl(%d, UFFDIO_API, NULL) = %d %s (%m)\n",
67 		fd, rc, errno2name());
68 	/* Normal call */
69 	api_struct->api = UFFD_API;
70 	api_struct->features = 0;
71 	rc = ioctl(fd, UFFDIO_API, api_struct);
72 	printf("ioctl(%d, UFFDIO_API, {api=0xaa, features=0, "
73 	       "features.out=%#" PRIx64 ", " "ioctls=1<<_UFFDIO_REGISTER|"
74 	       "1<<_UFFDIO_UNREGISTER|1<<_UFFDIO_API",
75 	       fd, (uint64_t)api_struct->features);
76 	api_struct->ioctls &= ~(1ull<<_UFFDIO_REGISTER|
77 				1ull<<_UFFDIO_UNREGISTER|
78 				1ull<<_UFFDIO_API);
79 	if (api_struct->ioctls)
80 		printf("|%#" PRIx64, (uint64_t)api_struct->ioctls);
81 	printf("}) = %d\n", rc);
82 
83 	/* For the rest of the tests we need some anonymous memory */
84 	void *area1 = mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
85 			   MAP_PRIVATE|MAP_ANONYMOUS,
86 			   -1, 0);
87 	if (area1 == MAP_FAILED)
88 		perror_msg_and_fail("mmap area1");
89 	void *area2 = mmap(NULL, pagesize, PROT_READ|PROT_WRITE,
90 			   MAP_PRIVATE|MAP_ANONYMOUS,
91 			   -1, 0);
92 	if (area2 == MAP_FAILED)
93 		perror_msg_and_fail("mmap area2");
94 	madvise(area2, pagesize, MADV_DONTNEED);
95 	*(char *)area1 = 42;
96 
97 	/* ---- REGISTER ---- */
98 	struct uffdio_register *register_struct =
99 					 tail_alloc(sizeof(*register_struct));
100 	memset(register_struct, 0, sizeof(*register_struct));
101 
102 	rc = ioctl(-1, UFFDIO_REGISTER, register_struct);
103 	printf("ioctl(-1, UFFDIO_REGISTER, {range={start=0, len=0}, "
104 	       "mode=0}) = %d %s (%m)\n", rc, errno2name());
105 
106 	rc = ioctl(fd, UFFDIO_REGISTER, NULL);
107 	printf("ioctl(%d, UFFDIO_REGISTER, NULL) = %d %s (%m)\n",
108 	       fd, rc, errno2name());
109 
110 	register_struct->range.start = (uint64_t)(uintptr_t)area2;
111 	register_struct->range.len = pagesize;
112 	register_struct->mode = UFFDIO_REGISTER_MODE_MISSING;
113 	rc = ioctl(fd, UFFDIO_REGISTER, register_struct);
114 	printf("ioctl(%d, UFFDIO_REGISTER, {range={start=%p, len=%#zx}, "
115 	       "mode=UFFDIO_REGISTER_MODE_MISSING, ioctls="
116 	       "1<<_UFFDIO_WAKE|1<<_UFFDIO_COPY|1<<_UFFDIO_ZEROPAGE",
117 	       fd, area2, pagesize);
118 	register_struct->ioctls &= ~(1ull<<_UFFDIO_WAKE|
119 				    1ull<<_UFFDIO_COPY|
120 				    1ull<<_UFFDIO_ZEROPAGE);
121 	if (register_struct->ioctls)
122 		printf("|%#" PRIx64, (uint64_t)register_struct->ioctls);
123 	printf("}) = %d\n", rc);
124 
125 	/* With area2 registered we can now do the atomic copies onto it
126 	 * but be careful not to access it in any other way otherwise
127 	 * userfaultfd will cause us to stall.
128 	 */
129 	/* ---- COPY ---- */
130 	struct uffdio_copy *copy_struct = tail_alloc(sizeof(*copy_struct));
131 
132 	memset(copy_struct, 0, sizeof(*copy_struct));
133 	rc = ioctl(-1, UFFDIO_COPY, copy_struct);
134 	printf("ioctl(-1, UFFDIO_COPY, {dst=0, src=0, len=0, mode=0"
135 	       "}) = %d %s (%m)\n", rc, errno2name());
136 
137 	rc = ioctl(fd, UFFDIO_COPY, NULL);
138 	printf("ioctl(%d, UFFDIO_COPY, NULL) = %d %s (%m)\n",
139 	       fd, rc, errno2name());
140 
141 	copy_struct->dst = (uint64_t)(uintptr_t)area2;
142 	copy_struct->src = (uint64_t)(uintptr_t)area1;
143 	copy_struct->len = pagesize;
144 	copy_struct->mode = UFFDIO_COPY_MODE_DONTWAKE;
145 	rc = ioctl(fd, UFFDIO_COPY, copy_struct);
146 	printf("ioctl(%d, UFFDIO_COPY, {dst=%p, src=%p, len=%#zx,"
147 	       " mode=UFFDIO_COPY_MODE_DONTWAKE, copy=%#zx}) = %d\n",
148 	       fd, area2, area1, pagesize, pagesize, rc);
149 
150 	/* ---- ZEROPAGE ---- */
151 	struct uffdio_zeropage *zero_struct = tail_alloc(sizeof(*zero_struct));
152 	madvise(area2, pagesize, MADV_DONTNEED);
153 
154 	memset(zero_struct, 0, sizeof(*zero_struct));
155 	rc = ioctl(-1, UFFDIO_ZEROPAGE, zero_struct);
156 	printf("ioctl(-1, UFFDIO_ZEROPAGE, {range={start=0, len=0}, mode=0"
157 	       "}) = %d %s (%m)\n", rc, errno2name());
158 
159 	rc = ioctl(fd, UFFDIO_ZEROPAGE, NULL);
160 	printf("ioctl(%d, UFFDIO_ZEROPAGE, NULL) = %d %s (%m)\n",
161 	       fd, rc, errno2name());
162 
163 	zero_struct->range.start = (uint64_t)(uintptr_t)area2;
164 	zero_struct->range.len = pagesize;
165 	zero_struct->mode = UFFDIO_ZEROPAGE_MODE_DONTWAKE;
166 	rc = ioctl(fd, UFFDIO_ZEROPAGE, zero_struct);
167 	printf("ioctl(%d, UFFDIO_ZEROPAGE, {range={start=%p, len=%#zx},"
168 	       " mode=UFFDIO_ZEROPAGE_MODE_DONTWAKE, zeropage=%#zx}) = %d\n",
169 	       fd, area2, pagesize, pagesize, rc);
170 
171 	/* ---- WAKE ---- */
172 	struct uffdio_range *range_struct = tail_alloc(sizeof(*range_struct));
173 	memset(range_struct, 0, sizeof(*range_struct));
174 
175 	rc = ioctl(-1, UFFDIO_WAKE, range_struct);
176 	printf("ioctl(-1, UFFDIO_WAKE, {start=0, len=0}) = %d %s (%m)\n",
177 	       rc, errno2name());
178 
179 	rc = ioctl(fd, UFFDIO_WAKE, NULL);
180 	printf("ioctl(%d, UFFDIO_WAKE, NULL) = %d %s (%m)\n",
181 	       fd, rc, errno2name());
182 
183 	range_struct->start = (uint64_t)(uintptr_t)area2;
184 	range_struct->len = pagesize;
185 	rc = ioctl(fd, UFFDIO_WAKE, range_struct);
186 	printf("ioctl(%d, UFFDIO_WAKE, {start=%p, len=%#zx}) = %d\n",
187 	       fd, area2, pagesize, rc);
188 
189 	/* ---- UNREGISTER ---- */
190 	memset(range_struct, 0, sizeof(*range_struct));
191 
192 	rc = ioctl(-1, UFFDIO_UNREGISTER, range_struct);
193 	printf("ioctl(-1, UFFDIO_UNREGISTER, {start=0, len=0}) = %d %s (%m)\n",
194 	       rc, errno2name());
195 
196 	rc = ioctl(fd, UFFDIO_UNREGISTER, NULL);
197 	printf("ioctl(%d, UFFDIO_UNREGISTER, NULL) = %d %s (%m)\n",
198 	       fd, rc, errno2name());
199 
200 	range_struct->start = (uint64_t)(uintptr_t)area2;
201 	range_struct->len = pagesize;
202 	rc = ioctl(fd, UFFDIO_UNREGISTER, range_struct);
203 	printf("ioctl(%d, UFFDIO_UNREGISTER, {start=%p, len=%#zx}) = %d\n",
204 	       fd, area2, pagesize, rc);
205 	puts("+++ exited with 0 +++");
206 	return 0;
207 }
208 
209 #else
210 
211 SKIP_MAIN_UNDEFINED("__NR_userfaultfd && HAVE_LINUX_USERFAULTFD_H")
212 
213 #endif
214