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