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