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