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