xref: /freebsd/tests/sys/vm/mmap_map_32bit_helper.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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 #define MAP_32BIT_MAX_ADDR      ((vm_offset_t)1 << 31)
15 
16 int
17 main(void)
18 {
19 	size_t pagesize;
20 	void *s32;
21 	int fd;
22 
23 	if ((pagesize = getpagesize()) <= 0)
24 		err(1, "getpagesize");
25 
26 	fd = open("/dev/zero", O_RDONLY);
27 	if (fd <= 0)
28 		err(1, "open failed");
29 
30 	s32 = mmap(NULL, pagesize, PROT_READ, MAP_32BIT | MAP_PRIVATE, fd, 0);
31 	if (s32 == MAP_FAILED)
32 		err(1, "mmap MAP_32BIT | MAP_PRIVATE failed");
33 	if (((vm_offset_t)s32 + pagesize) > MAP_32BIT_MAX_ADDR)
34 		errx(1, "mmap invalid result %p", s32);
35 
36 	close(fd);
37 	if (munmap(s32, pagesize) != 0)
38 		err(1, "munmap failed");
39 
40 	s32 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
41 	    MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
42 	if (s32 == MAP_FAILED)
43 		err(1, "mmap MAP_32BIT | MAP_ANONYMOUS | MAP_PRIVATE failed");
44 	if (((vm_offset_t)s32 + pagesize) > MAP_32BIT_MAX_ADDR)
45 		errx(1, "mmap invalid result %p", s32);
46 
47 	if (munmap(s32, pagesize) != 0)
48 		err(1, "munmap failed");
49 	exit(0);
50 }
51