1 | /*- |
---|
2 | * Copyright (c) 2009 Simon L. Nielsen <simon@FreeBSD.org>, |
---|
3 | * Bjoern A. Zeeb <bz@FreeBSD.org> |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * |
---|
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
24 | * SUCH DAMAGE. |
---|
25 | * |
---|
26 | * $FreeBSD$ |
---|
27 | */ |
---|
28 | |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | #include "config.h" |
---|
31 | #endif |
---|
32 | |
---|
33 | #define CONFIGURE_INIT |
---|
34 | #include "system.h" |
---|
35 | #include "pritime.h" |
---|
36 | #include "test_helper.h" |
---|
37 | #include "test_driver.h" |
---|
38 | |
---|
39 | #include <sys/mman.h> |
---|
40 | |
---|
41 | #include <errno.h> |
---|
42 | #include <fcntl.h> |
---|
43 | #include <string.h> |
---|
44 | |
---|
45 | const char rtems_test_name[] = "PSX MMAP01"; |
---|
46 | const char test_driver_name[] = "/dev/test_driver"; |
---|
47 | |
---|
48 | static const struct { |
---|
49 | void *addr; |
---|
50 | int ok; |
---|
51 | } map_at_zero_tests[] = { |
---|
52 | { (void *)0, 0 }, |
---|
53 | { (void *)1, 0 }, |
---|
54 | { (void *)(PAGE_SIZE - 1), 0 }, |
---|
55 | { (void *)PAGE_SIZE, 1 }, |
---|
56 | { (void *)-1, 0 }, |
---|
57 | { (void *)(-PAGE_SIZE), 0 }, |
---|
58 | { (void *)(-1 - PAGE_SIZE), 0 }, |
---|
59 | { (void *)(-1 - PAGE_SIZE - 1), 0 }, |
---|
60 | { (void *)(0x1000 * PAGE_SIZE), 1 }, |
---|
61 | }; |
---|
62 | |
---|
63 | #define MAP_AT_ZERO_NITEMS (sizeof(map_at_zero_tests) / sizeof(map_at_zero_tests[0])) |
---|
64 | |
---|
65 | static void* checked_mmap( |
---|
66 | int prot, |
---|
67 | int flags, |
---|
68 | int fd, |
---|
69 | int error, |
---|
70 | const char *msg |
---|
71 | ) |
---|
72 | { |
---|
73 | void *p; |
---|
74 | int pagesize, err; |
---|
75 | |
---|
76 | rtems_test_assert((pagesize = getpagesize()) > 0); |
---|
77 | p = mmap(NULL, pagesize, prot, flags, fd, 0); |
---|
78 | if (p == MAP_FAILED) { |
---|
79 | if (error == 0) |
---|
80 | mmap_test_assert(0, "%s failed with errno %d\n", msg, errno); |
---|
81 | else { |
---|
82 | err = errno; |
---|
83 | mmap_test_assert_equal(error, err, |
---|
84 | "%s failed with wrong errno %d (expected %d)\n", msg, |
---|
85 | errno, error); |
---|
86 | } |
---|
87 | } else { |
---|
88 | mmap_test_check(error == 0, "%s succeeded\n", msg); |
---|
89 | munmap(p, pagesize); |
---|
90 | } |
---|
91 | return p; |
---|
92 | } |
---|
93 | |
---|
94 | static void mmap_map_at_zero( void ) |
---|
95 | { |
---|
96 | void *p; |
---|
97 | unsigned int i; |
---|
98 | |
---|
99 | for (i = 0; i < MAP_AT_ZERO_NITEMS; i++) { |
---|
100 | p = mmap((void *)map_at_zero_tests[i].addr, PAGE_SIZE, |
---|
101 | PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED | MAP_PRIVATE, |
---|
102 | -1, 0); |
---|
103 | if (p == MAP_FAILED) { |
---|
104 | mmap_test_assert( map_at_zero_tests[i].ok == 0, |
---|
105 | "mmap(%08lX, ...) failed\n", (long)map_at_zero_tests[i].addr ); |
---|
106 | } else { |
---|
107 | mmap_test_check( map_at_zero_tests[i].ok == 1, |
---|
108 | "mmap(%p, ...) succeeded: p=%p\n", |
---|
109 | map_at_zero_tests[i].addr, p ); |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | static void mmap_bad_arguments( void ) |
---|
115 | { |
---|
116 | int devfd, pagesize, shmfd, zerofd; |
---|
117 | void* p; |
---|
118 | |
---|
119 | rtems_test_assert((pagesize = getpagesize()) > 0); |
---|
120 | rtems_test_assert((devfd = open(&test_driver_name[0], O_RDONLY)) >= 0); |
---|
121 | rtems_test_assert((shmfd = shm_open("/shm", O_CREAT | O_RDWR, 0644)) >= 0); |
---|
122 | rtems_test_assert(ftruncate(shmfd, pagesize) == 0); |
---|
123 | rtems_test_assert((zerofd = open("/dev/zero", O_RDONLY)) >= 0); |
---|
124 | |
---|
125 | /* |
---|
126 | * These should normally work on FREEBSD. Test cases below that fail are |
---|
127 | * due to unsupported features in RTEMS. |
---|
128 | */ |
---|
129 | checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON, -1, 0, |
---|
130 | "simple MAP_ANON"); |
---|
131 | checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0, |
---|
132 | "simple shm fd shared"); |
---|
133 | checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, shmfd, 0, |
---|
134 | "simple shm fd private"); |
---|
135 | /* RTEMS cannot protect against writes so this will fail */ |
---|
136 | checked_mmap(PROT_READ, MAP_SHARED, zerofd, ENOTSUP, |
---|
137 | "simple /dev/zero shared"); |
---|
138 | /* |
---|
139 | * Repeat with no write protection. Will fail because of unimplemented |
---|
140 | * mmap handler in /dev/zero. |
---|
141 | */ |
---|
142 | checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, zerofd, ENOTSUP, |
---|
143 | "simple /dev/zero shared"); |
---|
144 | /* RTEMS /dev/zero is a character device so this will fail */ |
---|
145 | checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, zerofd, EINVAL, |
---|
146 | "simple /dev/zero private"); |
---|
147 | /* RTEMS cannot protect against writes so this will fail */ |
---|
148 | checked_mmap(PROT_READ, MAP_SHARED, devfd, ENOTSUP, |
---|
149 | "simple test driver shared"); |
---|
150 | /* |
---|
151 | * Repeat with no write protection. Should fail because of unimplemented |
---|
152 | * mmap handler in /dev/null. |
---|
153 | */ |
---|
154 | p = checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, devfd, 0, |
---|
155 | "simple test driver shared"); |
---|
156 | rtems_test_assert(p == &test_data[0]); |
---|
157 | |
---|
158 | /* Extra PROT flags. */ |
---|
159 | checked_mmap(PROT_READ | PROT_WRITE | 0x100000, MAP_ANON, -1, EINVAL, |
---|
160 | "MAP_ANON with extra PROT flags"); |
---|
161 | checked_mmap(0xffff, MAP_SHARED, shmfd, EINVAL, |
---|
162 | "shm fd with garbage PROT"); |
---|
163 | |
---|
164 | /* Undefined flag. */ |
---|
165 | checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_RESERVED0080, -1, |
---|
166 | EINVAL, "Undefined flag"); |
---|
167 | |
---|
168 | /* Both MAP_SHARED and MAP_PRIVATE */ |
---|
169 | checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | |
---|
170 | MAP_SHARED, -1, EINVAL, "MAP_ANON with both SHARED and PRIVATE"); |
---|
171 | checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_SHARED, shmfd, |
---|
172 | EINVAL, "shm fd with both SHARED and PRIVATE"); |
---|
173 | |
---|
174 | /* At least one of MAP_SHARED or MAP_PRIVATE without ANON */ |
---|
175 | checked_mmap(PROT_READ | PROT_WRITE, 0, shmfd, EINVAL, |
---|
176 | "shm fd without sharing flag"); |
---|
177 | |
---|
178 | /* MAP_ANON with sharing flag. Will fail on RTEMS*/ |
---|
179 | checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, EINVAL, |
---|
180 | "shared MAP_ANON"); |
---|
181 | /* MAP_ANON with private flag*/ |
---|
182 | checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0, |
---|
183 | "private MAP_ANON"); |
---|
184 | |
---|
185 | /* MAP_ANON should require an fd of -1. */ |
---|
186 | checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, EINVAL, |
---|
187 | "MAP_ANON with fd != -1"); |
---|
188 | |
---|
189 | /* |
---|
190 | * Writable MAP_SHARED should fail on read-only descriptors. Will fail |
---|
191 | * on RTEMS because of unimplemented mmap handler in /dev/null and the fact |
---|
192 | * that there is no read only protection. |
---|
193 | */ |
---|
194 | checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, zerofd, ENOTSUP, |
---|
195 | "MAP_SHARED of read-only /dev/zero"); |
---|
196 | |
---|
197 | /* |
---|
198 | * Character devices other than /dev/zero do not support private |
---|
199 | * mappings. RTEMS cannot protect against writes so this will fail with |
---|
200 | * ENOTSUP |
---|
201 | */ |
---|
202 | checked_mmap(PROT_READ, MAP_PRIVATE, devfd, ENOTSUP, |
---|
203 | "MAP_PRIVATE of test driver"); |
---|
204 | |
---|
205 | /* |
---|
206 | * Repeat with no write protection. |
---|
207 | */ |
---|
208 | checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, devfd, EINVAL, |
---|
209 | "MAP_PRIVATE of test driver"); |
---|
210 | |
---|
211 | close(devfd); |
---|
212 | close(shmfd); |
---|
213 | close(zerofd); |
---|
214 | } |
---|
215 | |
---|
216 | static void mmap_dev_zero_private( void ) |
---|
217 | { |
---|
218 | char *p1, *p2, *p3; |
---|
219 | int fd, pagesize; |
---|
220 | /*int i*/ |
---|
221 | |
---|
222 | rtems_test_assert((pagesize = getpagesize()) > 0); |
---|
223 | rtems_test_assert((fd = open("/dev/zero", O_RDONLY)) >= 0); |
---|
224 | |
---|
225 | /* This should not return MAP_FAILED but does on RTEMS */ |
---|
226 | p1 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
---|
227 | /*rtems_test_assert(p1 != MAP_FAILED);*/ |
---|
228 | rtems_test_assert(p1 == MAP_FAILED); |
---|
229 | |
---|
230 | /* This should not return MAP_FAILED but does on RTEMS */ |
---|
231 | p2 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
---|
232 | /*rtems_test_assert(p2 != MAP_FAILED);*/ |
---|
233 | rtems_test_assert(p2 == MAP_FAILED); |
---|
234 | |
---|
235 | /* These tests can not run due to failures above */ |
---|
236 | /*for (i = 0; i < pagesize; i++) |
---|
237 | mmap_test_assert_equal(0, p1[i], "byte at p1[%d] is %x", i, p1[i]); |
---|
238 | |
---|
239 | rtems_test_assert(memcmp(p1, p2, pagesize) == 0); |
---|
240 | |
---|
241 | p1[0] = 1; |
---|
242 | |
---|
243 | rtems_test_assert(p2[0] == 0); |
---|
244 | |
---|
245 | p2[0] = 2; |
---|
246 | |
---|
247 | rtems_test_assert(p1[0] == 1);*/ |
---|
248 | |
---|
249 | /* This should not return MAP_FAILED but does on RTEMS */ |
---|
250 | p3 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
---|
251 | /*rtems_test_assert(p3 != MAP_FAILED);*/ |
---|
252 | rtems_test_assert(p3 == MAP_FAILED); |
---|
253 | |
---|
254 | /*rtems_test_assert(p3[0] == 0); |
---|
255 | |
---|
256 | munmap(p1, pagesize); |
---|
257 | munmap(p2, pagesize); |
---|
258 | munmap(p3, pagesize);*/ |
---|
259 | close(fd); |
---|
260 | } |
---|
261 | |
---|
262 | static void mmap_dev_zero_shared( void ) |
---|
263 | { |
---|
264 | char *p1, *p2, *p3; |
---|
265 | int fd, pagesize; |
---|
266 | /*int i*/ |
---|
267 | |
---|
268 | rtems_test_assert((pagesize = getpagesize()) > 0); |
---|
269 | rtems_test_assert((fd = open("/dev/zero", O_RDWR)) >= 0); |
---|
270 | |
---|
271 | /* This should not return MAP_FAILED but does on RTEMS */ |
---|
272 | p1 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
---|
273 | /*rtems_test_assert(p1 != MAP_FAILED);*/ |
---|
274 | rtems_test_assert(p1 == MAP_FAILED); |
---|
275 | |
---|
276 | /* This should not return MAP_FAILED but does on RTEMS */ |
---|
277 | p2 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
---|
278 | /*rtems_test_assert(p2 != MAP_FAILED);*/ |
---|
279 | rtems_test_assert(p2 == MAP_FAILED); |
---|
280 | |
---|
281 | /* These tests can not run due to failures above */ |
---|
282 | /*for (i = 0; i < pagesize; i++) |
---|
283 | mmap_test_assert_equal(0, p1[i], "byte at p1[%d] is %x", i, p1[i]); |
---|
284 | |
---|
285 | rtems_test_assert(memcmp(p1, p2, pagesize) == 0); |
---|
286 | |
---|
287 | p1[0] = 1; |
---|
288 | |
---|
289 | rtems_test_assert(p2[0] == 0); |
---|
290 | |
---|
291 | p2[0] = 2; |
---|
292 | |
---|
293 | rtems_test_assert(p1[0] == 1);*/ |
---|
294 | |
---|
295 | p3 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
---|
296 | /*rtems_test_assert(p3 != MAP_FAILED);*/ |
---|
297 | rtems_test_assert(p3 == MAP_FAILED); |
---|
298 | |
---|
299 | /*rtems_test_assert(p3[0] == 0); |
---|
300 | |
---|
301 | munmap(p1, pagesize); |
---|
302 | munmap(p2, pagesize); |
---|
303 | munmap(p3, pagesize);*/ |
---|
304 | close(fd); |
---|
305 | } |
---|
306 | |
---|
307 | void *POSIX_Init( |
---|
308 | void *argument |
---|
309 | ) |
---|
310 | { |
---|
311 | int rv; |
---|
312 | |
---|
313 | TEST_BEGIN(); |
---|
314 | |
---|
315 | rv = IMFS_make_generic_node( |
---|
316 | &test_driver_name[0], |
---|
317 | S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, |
---|
318 | &node_control, |
---|
319 | NULL |
---|
320 | ); |
---|
321 | rtems_test_assert(rv == 0); |
---|
322 | |
---|
323 | puts( "Init: mmap - map at zero" ); |
---|
324 | mmap_map_at_zero(); |
---|
325 | puts( "Init: mmap - bad arguments" ); |
---|
326 | mmap_bad_arguments(); |
---|
327 | /* |
---|
328 | * The following test does not work on RTEMS because /dev/zero is |
---|
329 | * implemented as a character device which does not support MAP_PRIVATE. |
---|
330 | */ |
---|
331 | puts( "Init: mmap - /dev/zero private" ); |
---|
332 | mmap_dev_zero_private(); |
---|
333 | /* |
---|
334 | * The following test does not work on RTEMS because /dev/zero does not |
---|
335 | * implemented the mmap handler. |
---|
336 | */ |
---|
337 | puts( "Init: mmap - /dev/zero shared" ); |
---|
338 | mmap_dev_zero_shared(); |
---|
339 | |
---|
340 | TEST_END(); |
---|
341 | |
---|
342 | rtems_test_exit(0); |
---|
343 | return 0; |
---|
344 | } |
---|