Rename misnamed BACK_CHAIN_OFFSET to LR_OFFSET
[qemu-kvm/fedora.git] / tests / test-mmap.c
blobced483fddc9cfddedfe372d4dd77b2a58ceda932
1 /*
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include <sys/mman.h>
34 #define D(x)
36 #define fail_unless(x) \
37 do \
38 { \
39 if (!(x)) { \
40 fprintf (stderr, "FAILED at %s:%d\n", __FILE__, __LINE__); \
41 exit (EXIT_FAILURE); \
42 } \
43 } while (0);
45 unsigned char *dummybuf;
46 static unsigned int pagesize;
47 static unsigned int pagemask;
48 int test_fd;
49 size_t test_fsize;
51 void check_aligned_anonymous_unfixed_mmaps(void)
53 void *p1;
54 void *p2;
55 void *p3;
56 void *p4;
57 void *p5;
58 uintptr_t p;
59 int i;
61 fprintf (stderr, "%s", __func__);
62 for (i = 0; i < 0x1fff; i++)
64 size_t len;
66 len = pagesize + (pagesize * i & 7);
67 p1 = mmap(NULL, len, PROT_READ,
68 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
69 p2 = mmap(NULL, len, PROT_READ,
70 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
71 p3 = mmap(NULL, len, PROT_READ,
72 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
73 p4 = mmap(NULL, len, PROT_READ,
74 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
75 p5 = mmap(NULL, len, PROT_READ,
76 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
78 /* Make sure we get pages aligned with the pagesize. The
79 target expects this. */
80 fail_unless (p1 != MAP_FAILED);
81 fail_unless (p2 != MAP_FAILED);
82 fail_unless (p3 != MAP_FAILED);
83 fail_unless (p4 != MAP_FAILED);
84 fail_unless (p5 != MAP_FAILED);
85 p = (uintptr_t) p1;
86 D(printf ("p=%x\n", p));
87 fail_unless ((p & pagemask) == 0);
88 p = (uintptr_t) p2;
89 fail_unless ((p & pagemask) == 0);
90 p = (uintptr_t) p3;
91 fail_unless ((p & pagemask) == 0);
92 p = (uintptr_t) p4;
93 fail_unless ((p & pagemask) == 0);
94 p = (uintptr_t) p5;
95 fail_unless ((p & pagemask) == 0);
97 /* Make sure we can read from the entire area. */
98 memcpy (dummybuf, p1, pagesize);
99 memcpy (dummybuf, p2, pagesize);
100 memcpy (dummybuf, p3, pagesize);
101 memcpy (dummybuf, p4, pagesize);
102 memcpy (dummybuf, p5, pagesize);
104 munmap (p1, len);
105 munmap (p2, len);
106 munmap (p3, len);
107 munmap (p4, len);
108 munmap (p5, len);
110 fprintf (stderr, " passed\n");
113 void check_large_anonymous_unfixed_mmap(void)
115 void *p1;
116 uintptr_t p;
117 size_t len;
119 fprintf (stderr, "%s", __func__);
121 len = 0x02000000;
122 p1 = mmap(NULL, len, PROT_READ,
123 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
125 /* Make sure we get pages aligned with the pagesize. The
126 target expects this. */
127 fail_unless (p1 != MAP_FAILED);
128 p = (uintptr_t) p1;
129 fail_unless ((p & pagemask) == 0);
131 /* Make sure we can read from the entire area. */
132 memcpy (dummybuf, p1, pagesize);
133 munmap (p1, len);
134 fprintf (stderr, " passed\n");
137 void check_aligned_anonymous_unfixed_colliding_mmaps(void)
139 char *p1;
140 char *p2;
141 char *p3;
142 uintptr_t p;
143 int i;
145 fprintf (stderr, "%s", __func__);
146 for (i = 0; i < 0x2fff; i++)
148 int nlen;
149 p1 = mmap(NULL, pagesize, PROT_READ,
150 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
151 fail_unless (p1 != MAP_FAILED);
152 p = (uintptr_t) p1;
153 fail_unless ((p & pagemask) == 0);
154 memcpy (dummybuf, p1, pagesize);
156 p2 = mmap(NULL, pagesize, PROT_READ,
157 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
158 fail_unless (p2 != MAP_FAILED);
159 p = (uintptr_t) p2;
160 fail_unless ((p & pagemask) == 0);
161 memcpy (dummybuf, p2, pagesize);
164 munmap (p1, pagesize);
165 nlen = pagesize * 8;
166 p3 = mmap(NULL, nlen, PROT_READ,
167 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
169 /* Check if the mmaped areas collide. */
170 if (p3 < p2
171 && (p3 + nlen) > p2)
172 fail_unless (0);
174 memcpy (dummybuf, p3, pagesize);
176 /* Make sure we get pages aligned with the pagesize. The
177 target expects this. */
178 fail_unless (p3 != MAP_FAILED);
179 p = (uintptr_t) p3;
180 fail_unless ((p & pagemask) == 0);
181 munmap (p2, pagesize);
182 munmap (p3, nlen);
184 fprintf (stderr, " passed\n");
187 void check_aligned_anonymous_fixed_mmaps(void)
189 char *addr;
190 void *p1;
191 uintptr_t p;
192 int i;
194 /* Find a suitable address to start with. */
195 addr = mmap(NULL, pagesize * 40, PROT_READ | PROT_WRITE,
196 MAP_PRIVATE | MAP_ANONYMOUS,
197 -1, 0);
198 fprintf (stderr, "%s addr=%p", __func__, addr);
199 fail_unless (addr != MAP_FAILED);
201 for (i = 0; i < 40; i++)
203 /* Create submaps within our unfixed map. */
204 p1 = mmap(addr, pagesize, PROT_READ,
205 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
206 -1, 0);
207 /* Make sure we get pages aligned with the pagesize.
208 The target expects this. */
209 p = (uintptr_t) p1;
210 fail_unless (p1 == addr);
211 fail_unless ((p & pagemask) == 0);
212 memcpy (dummybuf, p1, pagesize);
213 munmap (p1, pagesize);
214 addr += pagesize;
216 fprintf (stderr, " passed\n");
219 void check_aligned_anonymous_fixed_mmaps_collide_with_host(void)
221 char *addr;
222 void *p1;
223 uintptr_t p;
224 int i;
226 /* Find a suitable address to start with. Right were the x86 hosts
227 stack is. */
228 addr = ((void *)0x80000000);
229 fprintf (stderr, "%s addr=%p", __func__, addr);
230 fprintf (stderr, "FIXME: QEMU fails to track pages used by the host.");
232 for (i = 0; i < 20; i++)
234 /* Create submaps within our unfixed map. */
235 p1 = mmap(addr, pagesize, PROT_READ | PROT_WRITE,
236 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
237 -1, 0);
238 /* Make sure we get pages aligned with the pagesize.
239 The target expects this. */
240 p = (uintptr_t) p1;
241 fail_unless (p1 == addr);
242 fail_unless ((p & pagemask) == 0);
243 memcpy (p1, dummybuf, pagesize);
244 munmap (p1, pagesize);
245 addr += pagesize;
247 fprintf (stderr, " passed\n");
250 void check_file_unfixed_mmaps(void)
252 unsigned int *p1, *p2, *p3;
253 uintptr_t p;
254 int i;
256 fprintf (stderr, "%s", __func__);
257 for (i = 0; i < 0x10; i++)
259 size_t len;
261 len = pagesize;
262 p1 = mmap(NULL, len, PROT_READ,
263 MAP_PRIVATE,
264 test_fd, 0);
265 p2 = mmap(NULL, len, PROT_READ,
266 MAP_PRIVATE,
267 test_fd, pagesize);
268 p3 = mmap(NULL, len, PROT_READ,
269 MAP_PRIVATE,
270 test_fd, pagesize * 2);
272 fail_unless (p1 != MAP_FAILED);
273 fail_unless (p2 != MAP_FAILED);
274 fail_unless (p3 != MAP_FAILED);
276 /* Make sure we get pages aligned with the pagesize. The
277 target expects this. */
278 p = (uintptr_t) p1;
279 fail_unless ((p & pagemask) == 0);
280 p = (uintptr_t) p2;
281 fail_unless ((p & pagemask) == 0);
282 p = (uintptr_t) p3;
283 fail_unless ((p & pagemask) == 0);
285 /* Verify that the file maps was made correctly. */
286 D(printf ("p1=%d p2=%d p3=%d\n", *p1, *p2, *p3));
287 fail_unless (*p1 == 0);
288 fail_unless (*p2 == (pagesize / sizeof *p2));
289 fail_unless (*p3 == ((pagesize * 2) / sizeof *p3));
291 memcpy (dummybuf, p1, pagesize);
292 memcpy (dummybuf, p2, pagesize);
293 memcpy (dummybuf, p3, pagesize);
294 munmap (p1, len);
295 munmap (p2, len);
296 munmap (p3, len);
298 fprintf (stderr, " passed\n");
301 void check_file_unfixed_eof_mmaps(void)
303 char *cp;
304 unsigned int *p1;
305 uintptr_t p;
306 int i;
308 fprintf (stderr, "%s", __func__);
309 for (i = 0; i < 0x10; i++)
311 p1 = mmap(NULL, pagesize, PROT_READ,
312 MAP_PRIVATE,
313 test_fd,
314 (test_fsize - sizeof *p1) & ~pagemask);
316 fail_unless (p1 != MAP_FAILED);
318 /* Make sure we get pages aligned with the pagesize. The
319 target expects this. */
320 p = (uintptr_t) p1;
321 fail_unless ((p & pagemask) == 0);
322 /* Verify that the file maps was made correctly. */
323 fail_unless (p1[(test_fsize & pagemask) / sizeof *p1 - 1]
324 == ((test_fsize - sizeof *p1) / sizeof *p1));
326 /* Verify that the end of page is accessable and zeroed. */
327 cp = (void *) p1;
328 fail_unless (cp[pagesize - 4] == 0);
329 munmap (p1, pagesize);
331 fprintf (stderr, " passed\n");
334 void check_file_fixed_eof_mmaps(void)
336 char *addr;
337 char *cp;
338 unsigned int *p1;
339 uintptr_t p;
340 int i;
342 /* Find a suitable address to start with. */
343 addr = mmap(NULL, pagesize * 44, PROT_READ,
344 MAP_PRIVATE | MAP_ANONYMOUS,
345 -1, 0);
347 fprintf (stderr, "%s addr=%p", __func__, (void *)addr);
348 fail_unless (addr != MAP_FAILED);
350 for (i = 0; i < 0x10; i++)
352 /* Create submaps within our unfixed map. */
353 p1 = mmap(addr, pagesize, PROT_READ,
354 MAP_PRIVATE | MAP_FIXED,
355 test_fd,
356 (test_fsize - sizeof *p1) & ~pagemask);
358 fail_unless (p1 != MAP_FAILED);
360 /* Make sure we get pages aligned with the pagesize. The
361 target expects this. */
362 p = (uintptr_t) p1;
363 fail_unless ((p & pagemask) == 0);
365 /* Verify that the file maps was made correctly. */
366 fail_unless (p1[(test_fsize & pagemask) / sizeof *p1 - 1]
367 == ((test_fsize - sizeof *p1) / sizeof *p1));
369 /* Verify that the end of page is accessable and zeroed. */
370 cp = (void *)p1;
371 fail_unless (cp[pagesize - 4] == 0);
372 munmap (p1, pagesize);
373 addr += pagesize;
375 fprintf (stderr, " passed\n");
378 void check_file_fixed_mmaps(void)
380 unsigned char *addr;
381 unsigned int *p1, *p2, *p3, *p4;
382 int i;
384 /* Find a suitable address to start with. */
385 addr = mmap(NULL, pagesize * 40 * 4, PROT_READ,
386 MAP_PRIVATE | MAP_ANONYMOUS,
387 -1, 0);
388 fprintf (stderr, "%s addr=%p", __func__, (void *)addr);
389 fail_unless (addr != MAP_FAILED);
391 for (i = 0; i < 40; i++)
393 p1 = mmap(addr, pagesize, PROT_READ,
394 MAP_PRIVATE | MAP_FIXED,
395 test_fd, 0);
396 p2 = mmap(addr + pagesize, pagesize, PROT_READ,
397 MAP_PRIVATE | MAP_FIXED,
398 test_fd, pagesize);
399 p3 = mmap(addr + pagesize * 2, pagesize, PROT_READ,
400 MAP_PRIVATE | MAP_FIXED,
401 test_fd, pagesize * 2);
402 p4 = mmap(addr + pagesize * 3, pagesize, PROT_READ,
403 MAP_PRIVATE | MAP_FIXED,
404 test_fd, pagesize * 3);
406 /* Make sure we get pages aligned with the pagesize.
407 The target expects this. */
408 fail_unless (p1 == (void *)addr);
409 fail_unless (p2 == (void *)addr + pagesize);
410 fail_unless (p3 == (void *)addr + pagesize * 2);
411 fail_unless (p4 == (void *)addr + pagesize * 3);
413 /* Verify that the file maps was made correctly. */
414 fail_unless (*p1 == 0);
415 fail_unless (*p2 == (pagesize / sizeof *p2));
416 fail_unless (*p3 == ((pagesize * 2) / sizeof *p3));
417 fail_unless (*p4 == ((pagesize * 3) / sizeof *p4));
419 memcpy (dummybuf, p1, pagesize);
420 memcpy (dummybuf, p2, pagesize);
421 memcpy (dummybuf, p3, pagesize);
422 memcpy (dummybuf, p4, pagesize);
424 munmap (p1, pagesize);
425 munmap (p2, pagesize);
426 munmap (p3, pagesize);
427 munmap (p4, pagesize);
428 addr += pagesize * 4;
430 fprintf (stderr, " passed\n");
433 int main(int argc, char **argv)
435 char tempname[] = "/tmp/.cmmapXXXXXX";
436 unsigned int i;
438 /* Trust the first argument, otherwise probe the system for our
439 pagesize. */
440 if (argc > 1)
441 pagesize = strtoul(argv[1], NULL, 0);
442 else
443 pagesize = sysconf(_SC_PAGESIZE);
445 /* Assume pagesize is a power of two. */
446 pagemask = pagesize - 1;
447 dummybuf = malloc (pagesize);
448 printf ("pagesize=%u pagemask=%x\n", pagesize, pagemask);
450 test_fd = mkstemp(tempname);
451 unlink(tempname);
453 /* Fill the file with int's counting from zero and up. */
454 for (i = 0; i < (pagesize * 4) / sizeof i; i++)
455 write (test_fd, &i, sizeof i);
456 /* Append a few extra writes to make the file end at non
457 page boundary. */
458 write (test_fd, &i, sizeof i); i++;
459 write (test_fd, &i, sizeof i); i++;
460 write (test_fd, &i, sizeof i); i++;
462 test_fsize = lseek(test_fd, 0, SEEK_CUR);
464 /* Run the tests. */
465 check_aligned_anonymous_unfixed_mmaps();
466 check_aligned_anonymous_unfixed_colliding_mmaps();
467 check_aligned_anonymous_fixed_mmaps();
468 check_file_unfixed_mmaps();
469 check_file_fixed_mmaps();
470 check_file_fixed_eof_mmaps();
471 check_file_unfixed_eof_mmaps();
473 /* Fails at the moment. */
474 /* check_aligned_anonymous_fixed_mmaps_collide_with_host(); */
476 return EXIT_SUCCESS;