2 * Small test program to verify simulated mmap behaviour.
4 * When running qemu-linux-user with the -p flag, you may need to tell
5 * this test program about the pagesize because getpagesize() will not reflect
6 * the -p choice. Simply pass one argument beeing the pagesize.
8 * Copyright (c) 2007 AXIS Communications AB
9 * Written by Edgar E. Iglesias.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
37 #define fail_unless(x) \
41 fprintf (stderr, "FAILED at %s:%d\n", __FILE__, __LINE__); \
42 exit (EXIT_FAILURE); \
46 unsigned char *dummybuf
;
47 static unsigned int pagesize
;
48 static unsigned int pagemask
;
52 void check_aligned_anonymous_unfixed_mmaps(void)
62 fprintf (stderr
, "%s", __func__
);
63 for (i
= 0; i
< 0x1fff; i
++)
67 len
= pagesize
+ (pagesize
* i
& 7);
68 p1
= mmap(NULL
, len
, PROT_READ
,
69 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
70 p2
= mmap(NULL
, len
, PROT_READ
,
71 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
72 p3
= mmap(NULL
, len
, PROT_READ
,
73 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
74 p4
= mmap(NULL
, len
, PROT_READ
,
75 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
76 p5
= mmap(NULL
, len
, PROT_READ
,
77 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
79 /* Make sure we get pages aligned with the pagesize. The
80 target expects this. */
81 fail_unless (p1
!= MAP_FAILED
);
82 fail_unless (p2
!= MAP_FAILED
);
83 fail_unless (p3
!= MAP_FAILED
);
84 fail_unless (p4
!= MAP_FAILED
);
85 fail_unless (p5
!= MAP_FAILED
);
87 D(printf ("p=%x\n", p
));
88 fail_unless ((p
& pagemask
) == 0);
90 fail_unless ((p
& pagemask
) == 0);
92 fail_unless ((p
& pagemask
) == 0);
94 fail_unless ((p
& pagemask
) == 0);
96 fail_unless ((p
& pagemask
) == 0);
98 /* Make sure we can read from the entire area. */
99 memcpy (dummybuf
, p1
, pagesize
);
100 memcpy (dummybuf
, p2
, pagesize
);
101 memcpy (dummybuf
, p3
, pagesize
);
102 memcpy (dummybuf
, p4
, pagesize
);
103 memcpy (dummybuf
, p5
, pagesize
);
111 fprintf (stderr
, " passed\n");
114 void check_large_anonymous_unfixed_mmap(void)
120 fprintf (stderr
, "%s", __func__
);
123 p1
= mmap(NULL
, len
, PROT_READ
,
124 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
126 /* Make sure we get pages aligned with the pagesize. The
127 target expects this. */
128 fail_unless (p1
!= MAP_FAILED
);
130 fail_unless ((p
& pagemask
) == 0);
132 /* Make sure we can read from the entire area. */
133 memcpy (dummybuf
, p1
, pagesize
);
135 fprintf (stderr
, " passed\n");
138 void check_aligned_anonymous_unfixed_colliding_mmaps(void)
146 fprintf (stderr
, "%s", __func__
);
147 for (i
= 0; i
< 0x2fff; i
++)
150 p1
= mmap(NULL
, pagesize
, PROT_READ
,
151 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
152 fail_unless (p1
!= MAP_FAILED
);
154 fail_unless ((p
& pagemask
) == 0);
155 memcpy (dummybuf
, p1
, pagesize
);
157 p2
= mmap(NULL
, pagesize
, PROT_READ
,
158 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
159 fail_unless (p2
!= MAP_FAILED
);
161 fail_unless ((p
& pagemask
) == 0);
162 memcpy (dummybuf
, p2
, pagesize
);
165 munmap (p1
, pagesize
);
167 p3
= mmap(NULL
, nlen
, PROT_READ
,
168 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
170 /* Check if the mmaped areas collide. */
175 memcpy (dummybuf
, p3
, pagesize
);
177 /* Make sure we get pages aligned with the pagesize. The
178 target expects this. */
179 fail_unless (p3
!= MAP_FAILED
);
181 fail_unless ((p
& pagemask
) == 0);
182 munmap (p2
, pagesize
);
185 fprintf (stderr
, " passed\n");
188 void check_aligned_anonymous_fixed_mmaps(void)
195 /* Find a suitable address to start with. */
196 addr
= mmap(NULL
, pagesize
* 40, PROT_READ
| PROT_WRITE
,
197 MAP_PRIVATE
| MAP_ANONYMOUS
,
199 fprintf (stderr
, "%s addr=%p", __func__
, addr
);
200 fail_unless (addr
!= MAP_FAILED
);
202 for (i
= 0; i
< 40; i
++)
204 /* Create submaps within our unfixed map. */
205 p1
= mmap(addr
, pagesize
, PROT_READ
,
206 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
208 /* Make sure we get pages aligned with the pagesize.
209 The target expects this. */
211 fail_unless (p1
== addr
);
212 fail_unless ((p
& pagemask
) == 0);
213 memcpy (dummybuf
, p1
, pagesize
);
214 munmap (p1
, pagesize
);
217 fprintf (stderr
, " passed\n");
220 void check_aligned_anonymous_fixed_mmaps_collide_with_host(void)
227 /* Find a suitable address to start with. Right were the x86 hosts
229 addr
= ((void *)0x80000000);
230 fprintf (stderr
, "%s addr=%p", __func__
, addr
);
231 fprintf (stderr
, "FIXME: QEMU fails to track pages used by the host.");
233 for (i
= 0; i
< 20; i
++)
235 /* Create submaps within our unfixed map. */
236 p1
= mmap(addr
, pagesize
, PROT_READ
| PROT_WRITE
,
237 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
239 /* Make sure we get pages aligned with the pagesize.
240 The target expects this. */
242 fail_unless (p1
== addr
);
243 fail_unless ((p
& pagemask
) == 0);
244 memcpy (p1
, dummybuf
, pagesize
);
245 munmap (p1
, pagesize
);
248 fprintf (stderr
, " passed\n");
251 void check_file_unfixed_mmaps(void)
253 unsigned int *p1
, *p2
, *p3
;
257 fprintf (stderr
, "%s", __func__
);
258 for (i
= 0; i
< 0x10; i
++)
263 p1
= mmap(NULL
, len
, PROT_READ
,
266 p2
= mmap(NULL
, len
, PROT_READ
,
269 p3
= mmap(NULL
, len
, PROT_READ
,
271 test_fd
, pagesize
* 2);
273 fail_unless (p1
!= MAP_FAILED
);
274 fail_unless (p2
!= MAP_FAILED
);
275 fail_unless (p3
!= MAP_FAILED
);
277 /* Make sure we get pages aligned with the pagesize. The
278 target expects this. */
280 fail_unless ((p
& pagemask
) == 0);
282 fail_unless ((p
& pagemask
) == 0);
284 fail_unless ((p
& pagemask
) == 0);
286 /* Verify that the file maps was made correctly. */
287 D(printf ("p1=%d p2=%d p3=%d\n", *p1
, *p2
, *p3
));
288 fail_unless (*p1
== 0);
289 fail_unless (*p2
== (pagesize
/ sizeof *p2
));
290 fail_unless (*p3
== ((pagesize
* 2) / sizeof *p3
));
292 memcpy (dummybuf
, p1
, pagesize
);
293 memcpy (dummybuf
, p2
, pagesize
);
294 memcpy (dummybuf
, p3
, pagesize
);
299 fprintf (stderr
, " passed\n");
302 void check_file_unfixed_eof_mmaps(void)
309 fprintf (stderr
, "%s", __func__
);
310 for (i
= 0; i
< 0x10; i
++)
312 p1
= mmap(NULL
, pagesize
, PROT_READ
,
315 (test_fsize
- sizeof *p1
) & ~pagemask
);
317 fail_unless (p1
!= MAP_FAILED
);
319 /* Make sure we get pages aligned with the pagesize. The
320 target expects this. */
322 fail_unless ((p
& pagemask
) == 0);
323 /* Verify that the file maps was made correctly. */
324 fail_unless (p1
[(test_fsize
& pagemask
) / sizeof *p1
- 1]
325 == ((test_fsize
- sizeof *p1
) / sizeof *p1
));
327 /* Verify that the end of page is accessable and zeroed. */
329 fail_unless (cp
[pagesize
- 4] == 0);
330 munmap (p1
, pagesize
);
332 fprintf (stderr
, " passed\n");
335 void check_file_fixed_eof_mmaps(void)
343 /* Find a suitable address to start with. */
344 addr
= mmap(NULL
, pagesize
* 44, PROT_READ
,
345 MAP_PRIVATE
| MAP_ANONYMOUS
,
348 fprintf (stderr
, "%s addr=%p", __func__
, (void *)addr
);
349 fail_unless (addr
!= MAP_FAILED
);
351 for (i
= 0; i
< 0x10; i
++)
353 /* Create submaps within our unfixed map. */
354 p1
= mmap(addr
, pagesize
, PROT_READ
,
355 MAP_PRIVATE
| MAP_FIXED
,
357 (test_fsize
- sizeof *p1
) & ~pagemask
);
359 fail_unless (p1
!= MAP_FAILED
);
361 /* Make sure we get pages aligned with the pagesize. The
362 target expects this. */
364 fail_unless ((p
& pagemask
) == 0);
366 /* Verify that the file maps was made correctly. */
367 fail_unless (p1
[(test_fsize
& pagemask
) / sizeof *p1
- 1]
368 == ((test_fsize
- sizeof *p1
) / sizeof *p1
));
370 /* Verify that the end of page is accessable and zeroed. */
372 fail_unless (cp
[pagesize
- 4] == 0);
373 munmap (p1
, pagesize
);
376 fprintf (stderr
, " passed\n");
379 void check_file_fixed_mmaps(void)
382 unsigned int *p1
, *p2
, *p3
, *p4
;
385 /* Find a suitable address to start with. */
386 addr
= mmap(NULL
, pagesize
* 40 * 4, PROT_READ
,
387 MAP_PRIVATE
| MAP_ANONYMOUS
,
389 fprintf (stderr
, "%s addr=%p", __func__
, (void *)addr
);
390 fail_unless (addr
!= MAP_FAILED
);
392 for (i
= 0; i
< 40; i
++)
394 p1
= mmap(addr
, pagesize
, PROT_READ
,
395 MAP_PRIVATE
| MAP_FIXED
,
397 p2
= mmap(addr
+ pagesize
, pagesize
, PROT_READ
,
398 MAP_PRIVATE
| MAP_FIXED
,
400 p3
= mmap(addr
+ pagesize
* 2, pagesize
, PROT_READ
,
401 MAP_PRIVATE
| MAP_FIXED
,
402 test_fd
, pagesize
* 2);
403 p4
= mmap(addr
+ pagesize
* 3, pagesize
, PROT_READ
,
404 MAP_PRIVATE
| MAP_FIXED
,
405 test_fd
, pagesize
* 3);
407 /* Make sure we get pages aligned with the pagesize.
408 The target expects this. */
409 fail_unless (p1
== (void *)addr
);
410 fail_unless (p2
== (void *)addr
+ pagesize
);
411 fail_unless (p3
== (void *)addr
+ pagesize
* 2);
412 fail_unless (p4
== (void *)addr
+ pagesize
* 3);
414 /* Verify that the file maps was made correctly. */
415 fail_unless (*p1
== 0);
416 fail_unless (*p2
== (pagesize
/ sizeof *p2
));
417 fail_unless (*p3
== ((pagesize
* 2) / sizeof *p3
));
418 fail_unless (*p4
== ((pagesize
* 3) / sizeof *p4
));
420 memcpy (dummybuf
, p1
, pagesize
);
421 memcpy (dummybuf
, p2
, pagesize
);
422 memcpy (dummybuf
, p3
, pagesize
);
423 memcpy (dummybuf
, p4
, pagesize
);
425 munmap (p1
, pagesize
);
426 munmap (p2
, pagesize
);
427 munmap (p3
, pagesize
);
428 munmap (p4
, pagesize
);
429 addr
+= pagesize
* 4;
431 fprintf (stderr
, " passed\n");
434 int main(int argc
, char **argv
)
436 char tempname
[] = "/tmp/.cmmapXXXXXX";
439 /* Trust the first argument, otherwise probe the system for our
442 pagesize
= strtoul(argv
[1], NULL
, 0);
444 pagesize
= sysconf(_SC_PAGESIZE
);
446 /* Assume pagesize is a power of two. */
447 pagemask
= pagesize
- 1;
448 dummybuf
= malloc (pagesize
);
449 printf ("pagesize=%u pagemask=%x\n", pagesize
, pagemask
);
451 test_fd
= mkstemp(tempname
);
454 /* Fill the file with int's counting from zero and up. */
455 for (i
= 0; i
< (pagesize
* 4) / sizeof i
; i
++)
456 write (test_fd
, &i
, sizeof i
);
457 /* Append a few extra writes to make the file end at non
459 write (test_fd
, &i
, sizeof i
); i
++;
460 write (test_fd
, &i
, sizeof i
); i
++;
461 write (test_fd
, &i
, sizeof i
); i
++;
463 test_fsize
= lseek(test_fd
, 0, SEEK_CUR
);
466 check_aligned_anonymous_unfixed_mmaps();
467 check_aligned_anonymous_unfixed_colliding_mmaps();
468 check_aligned_anonymous_fixed_mmaps();
469 check_file_unfixed_mmaps();
470 check_file_fixed_mmaps();
471 check_file_fixed_eof_mmaps();
472 check_file_unfixed_eof_mmaps();
474 /* Fails at the moment. */
475 /* check_aligned_anonymous_fixed_mmaps_collide_with_host(); */