xref: /freebsd/tests/sys/vm/mmap_map_32bit_helper.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*-
2  * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #include <sys/mman.h>
8 
9 #include <err.h>
10 #include <fcntl.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
14 _Static_assert(sizeof(vm_offset_t) >= 8, "Test is not intended for ILP32");
15 #define MAP_32BIT_MAX_ADDR      ((vm_offset_t)1 << 31)
16 
17 int
18 main(void)
19 {
20 	size_t pagesize;
21 	void *s32;
22 	int fd;
23 
24 	if ((pagesize = getpagesize()) <= 0)
25 		err(1, "getpagesize");
26 
27 	fd = open("/dev/zero", O_RDONLY);
28 	if (fd <= 0)
29 		err(1, "open failed");
30 
31 	s32 = mmap(NULL, pagesize, PROT_READ, MAP_32BIT | MAP_PRIVATE, fd, 0);
32 	if (s32 == MAP_FAILED)
33 		err(1, "mmap MAP_32BIT | MAP_PRIVATE failed");
34 	if (((vm_offset_t)s32 + pagesize) > MAP_32BIT_MAX_ADDR)
35 		errx(1, "mmap invalid result %p", s32);
36 
37 	close(fd);
38 	if (munmap(s32, pagesize) != 0)
39 		err(1, "munmap failed");
40 
41 	s32 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
42 	    MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
43 	if (s32 == MAP_FAILED)
44 		err(1, "mmap MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE failed");
45 	if (((vm_offset_t)s32 + pagesize) > MAP_32BIT_MAX_ADDR)
46 		errx(1, "mmap invalid result %p", s32);
47 
48 	if (munmap(s32, pagesize) != 0)
49 		err(1, "munmap failed");
50 	exit(0);
51 }
52