xref: /xnu-8796.141.3/tests/jumbo_va_spaces_common.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <errno.h>
2*1b191cb5SApple OSS Distributions #include <unistd.h>
3*1b191cb5SApple OSS Distributions #include <sys/mman.h>
4*1b191cb5SApple OSS Distributions 
5*1b191cb5SApple OSS Distributions #include <darwintest.h>
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions #include "jumbo_va_spaces_common.h"
8*1b191cb5SApple OSS Distributions 
9*1b191cb5SApple OSS Distributions #define GB (1ULL * 1024 * 1024 * 1024)
10*1b191cb5SApple OSS Distributions 
11*1b191cb5SApple OSS Distributions /*
12*1b191cb5SApple OSS Distributions  * This test expects the entitlement to be the enabling factor for a process to
13*1b191cb5SApple OSS Distributions  * allocate at least this many GB of VA space. i.e. with the entitlement, n GB
14*1b191cb5SApple OSS Distributions  * must be allocatable; whereas without it, it must be less.
15*1b191cb5SApple OSS Distributions  * This value was determined experimentally to fit on applicable devices and to
16*1b191cb5SApple OSS Distributions  * be clearly distinguishable from the default VA limit.
17*1b191cb5SApple OSS Distributions  */
18*1b191cb5SApple OSS Distributions #define ALLOC_TEST_GB 53
19*1b191cb5SApple OSS Distributions 
20*1b191cb5SApple OSS Distributions void
verify_jumbo_va(bool entitled)21*1b191cb5SApple OSS Distributions verify_jumbo_va(bool entitled)
22*1b191cb5SApple OSS Distributions {
23*1b191cb5SApple OSS Distributions 	T_LOG("Attemping to allocate VA space in 1 GB chunks.");
24*1b191cb5SApple OSS Distributions 	void *res;
25*1b191cb5SApple OSS Distributions 	int i;
26*1b191cb5SApple OSS Distributions 
27*1b191cb5SApple OSS Distributions 	for (i = 0; i < (ALLOC_TEST_GB * 2); i++) {
28*1b191cb5SApple OSS Distributions 		res = mmap(NULL, 1 * GB, PROT_NONE, MAP_PRIVATE | MAP_ANON, 0, 0);
29*1b191cb5SApple OSS Distributions 		if (res == MAP_FAILED) {
30*1b191cb5SApple OSS Distributions 			if (errno != ENOMEM) {
31*1b191cb5SApple OSS Distributions 				T_WITH_ERRNO;
32*1b191cb5SApple OSS Distributions 				T_LOG("mmap failed: stopped at %d of %d GB allocated", i, ALLOC_TEST_GB);
33*1b191cb5SApple OSS Distributions 			}
34*1b191cb5SApple OSS Distributions 			break;
35*1b191cb5SApple OSS Distributions 		} else {
36*1b191cb5SApple OSS Distributions 			T_LOG("%d: %p\n", i, res);
37*1b191cb5SApple OSS Distributions 		}
38*1b191cb5SApple OSS Distributions 	}
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions 	if (entitled) {
41*1b191cb5SApple OSS Distributions 		T_EXPECT_GE_INT(i, ALLOC_TEST_GB, "Allocate at least %d GB of VA space", ALLOC_TEST_GB);
42*1b191cb5SApple OSS Distributions 	} else {
43*1b191cb5SApple OSS Distributions 		T_EXPECT_LT_INT(i, ALLOC_TEST_GB, "Not permitted to allocate %d GB of VA space", ALLOC_TEST_GB);
44*1b191cb5SApple OSS Distributions 	}
45*1b191cb5SApple OSS Distributions }
46