xref: /xnu-11417.121.6/tests/os_thread_self_restrict_pagers.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions #include <darwintest.h>
2*a1e26a70SApple OSS Distributions #include <darwintest_perf.h>
3*a1e26a70SApple OSS Distributions 
4*a1e26a70SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.vm"));
5*a1e26a70SApple OSS Distributions 
6*a1e26a70SApple OSS Distributions #include <machine/cpu_capabilities.h>
7*a1e26a70SApple OSS Distributions #include <sys/mman.h>
8*a1e26a70SApple OSS Distributions #include <errno.h>
9*a1e26a70SApple OSS Distributions #include <fcntl.h>
10*a1e26a70SApple OSS Distributions #include <stdint.h>
11*a1e26a70SApple OSS Distributions #include <libkern/OSCacheControl.h>
12*a1e26a70SApple OSS Distributions #include <unistd.h>
13*a1e26a70SApple OSS Distributions #include <signal.h>
14*a1e26a70SApple OSS Distributions #include <stdlib.h>
15*a1e26a70SApple OSS Distributions #include <sys/sysctl.h>
16*a1e26a70SApple OSS Distributions 
17*a1e26a70SApple OSS Distributions #include <mach/vm_param.h>
18*a1e26a70SApple OSS Distributions #include <pthread.h>
19*a1e26a70SApple OSS Distributions 
20*a1e26a70SApple OSS Distributions #include <os/thread_self_restrict.h>
21*a1e26a70SApple OSS Distributions 
22*a1e26a70SApple OSS Distributions #include <mach/mach.h>
23*a1e26a70SApple OSS Distributions #include <mach/mach_error.h>
24*a1e26a70SApple OSS Distributions #include <mach/mach_init.h>
25*a1e26a70SApple OSS Distributions #include <mach/mach_port.h>
26*a1e26a70SApple OSS Distributions #include <mach/mach_vm.h>
27*a1e26a70SApple OSS Distributions #include <mach/vm_map.h>
28*a1e26a70SApple OSS Distributions #include <mach/task.h>
29*a1e26a70SApple OSS Distributions 
30*a1e26a70SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
31*a1e26a70SApple OSS Distributions 
32*a1e26a70SApple OSS Distributions #if defined(__arm64__)
33*a1e26a70SApple OSS Distributions /* PAGE_SIZE on ARM64 is an expression derived from a non-const global variable */
34*a1e26a70SApple OSS Distributions #define PAD_SIZE        PAGE_MAX_SIZE
35*a1e26a70SApple OSS Distributions #else
36*a1e26a70SApple OSS Distributions #define PAD_SIZE        PAGE_MIN_SIZE
37*a1e26a70SApple OSS Distributions #endif
38*a1e26a70SApple OSS Distributions 
39*a1e26a70SApple OSS Distributions /* Enumerations */
40*a1e26a70SApple OSS Distributions typedef enum _access_type {
41*a1e26a70SApple OSS Distributions 	ACCESS_READ,
42*a1e26a70SApple OSS Distributions 	ACCESS_WRITE,
43*a1e26a70SApple OSS Distributions } access_type_t;
44*a1e26a70SApple OSS Distributions 
45*a1e26a70SApple OSS Distributions typedef enum _fault_strategy {
46*a1e26a70SApple OSS Distributions 	FAULT_STRAT_NONE,
47*a1e26a70SApple OSS Distributions 	FAULT_STRAT_RW_TPRO,
48*a1e26a70SApple OSS Distributions } fault_strategy_t;
49*a1e26a70SApple OSS Distributions 
50*a1e26a70SApple OSS Distributions /* Structures */
51*a1e26a70SApple OSS Distributions typedef struct {
52*a1e26a70SApple OSS Distributions 	uint64_t fault_count;
53*a1e26a70SApple OSS Distributions 	fault_strategy_t fault_strategy;
54*a1e26a70SApple OSS Distributions 	bool fault_expected;
55*a1e26a70SApple OSS Distributions } fault_state_t;
56*a1e26a70SApple OSS Distributions 
57*a1e26a70SApple OSS Distributions /* Globals */
58*a1e26a70SApple OSS Distributions static bool key_created = false;
59*a1e26a70SApple OSS Distributions static pthread_key_t fault_state_key;
60*a1e26a70SApple OSS Distributions 
61*a1e26a70SApple OSS Distributions /*
62*a1e26a70SApple OSS Distributions  * The pager will only map entries with TPRO if we need to perform fixups.
63*a1e26a70SApple OSS Distributions  * Otherwise it really is const. Ensure we forge a struct that will require
64*a1e26a70SApple OSS Distributions  * dynamic rebasing.
65*a1e26a70SApple OSS Distributions  */
66*a1e26a70SApple OSS Distributions typedef struct {
67*a1e26a70SApple OSS Distributions 	void *reloc;
68*a1e26a70SApple OSS Distributions 	uint32_t magic;
69*a1e26a70SApple OSS Distributions 	char bytes[PAD_SIZE - 12];
70*a1e26a70SApple OSS Distributions } const_page_t;
71*a1e26a70SApple OSS Distributions 
72*a1e26a70SApple OSS Distributions typedef struct {
73*a1e26a70SApple OSS Distributions 	const_page_t one;
74*a1e26a70SApple OSS Distributions 	const_page_t two;
75*a1e26a70SApple OSS Distributions 	char ro[PAD_SIZE];
76*a1e26a70SApple OSS Distributions } const_state_t;
77*a1e26a70SApple OSS Distributions 
78*a1e26a70SApple OSS Distributions #define MAGIC(state) (void *)&state->magic
79*a1e26a70SApple OSS Distributions 
80*a1e26a70SApple OSS Distributions /*
81*a1e26a70SApple OSS Distributions  * Force known data into our __DATA_CONST segment. The pager will be responsible
82*a1e26a70SApple OSS Distributions  * for handling the mapping of this.
83*a1e26a70SApple OSS Distributions  */
84*a1e26a70SApple OSS Distributions __attribute__((section("__DATA_CONST,__pager")))
85*a1e26a70SApple OSS Distributions __attribute__((aligned(PAD_SIZE)))
86*a1e26a70SApple OSS Distributions static const_state_t pager_state = {
87*a1e26a70SApple OSS Distributions 	.one.reloc = &pager_state,
88*a1e26a70SApple OSS Distributions 	.two.reloc = &pager_state,
89*a1e26a70SApple OSS Distributions 	.one.magic = 0x41414141,
90*a1e26a70SApple OSS Distributions 	.two.magic = 0x41414141,
91*a1e26a70SApple OSS Distributions 	.ro = "CCCC"
92*a1e26a70SApple OSS Distributions };
93*a1e26a70SApple OSS Distributions 
94*a1e26a70SApple OSS Distributions /* Allocate a fault_state_t, and associate it with the current thread. */
95*a1e26a70SApple OSS Distributions static fault_state_t *
fault_state_create(void)96*a1e26a70SApple OSS Distributions fault_state_create(void)
97*a1e26a70SApple OSS Distributions {
98*a1e26a70SApple OSS Distributions 	fault_state_t * fault_state = malloc(sizeof(fault_state_t));
99*a1e26a70SApple OSS Distributions 
100*a1e26a70SApple OSS Distributions 	if (fault_state) {
101*a1e26a70SApple OSS Distributions 		fault_state->fault_count = 0;
102*a1e26a70SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_NONE;
103*a1e26a70SApple OSS Distributions 		fault_state->fault_expected = false;
104*a1e26a70SApple OSS Distributions 
105*a1e26a70SApple OSS Distributions 		if (pthread_setspecific(fault_state_key, fault_state)) {
106*a1e26a70SApple OSS Distributions 			free(fault_state);
107*a1e26a70SApple OSS Distributions 			fault_state = NULL;
108*a1e26a70SApple OSS Distributions 		}
109*a1e26a70SApple OSS Distributions 	}
110*a1e26a70SApple OSS Distributions 
111*a1e26a70SApple OSS Distributions 	return fault_state;
112*a1e26a70SApple OSS Distributions }
113*a1e26a70SApple OSS Distributions 
114*a1e26a70SApple OSS Distributions /* Disassociate the given fault state from the current thread, and destroy it. */
115*a1e26a70SApple OSS Distributions static void
fault_state_destroy(void * fault_state)116*a1e26a70SApple OSS Distributions fault_state_destroy(void * fault_state)
117*a1e26a70SApple OSS Distributions {
118*a1e26a70SApple OSS Distributions 	if (fault_state == NULL) {
119*a1e26a70SApple OSS Distributions 		T_ASSERT_FAIL("Attempted to fault_state_destroy NULL");
120*a1e26a70SApple OSS Distributions 	}
121*a1e26a70SApple OSS Distributions 
122*a1e26a70SApple OSS Distributions 	free(fault_state);
123*a1e26a70SApple OSS Distributions }
124*a1e26a70SApple OSS Distributions 
125*a1e26a70SApple OSS Distributions /*
126*a1e26a70SApple OSS Distributions  * A signal handler that attempts to resolve anticipated faults through use of
127*a1e26a70SApple OSS Distributions  * the os_thread_self_restrict_rwx functions.
128*a1e26a70SApple OSS Distributions  */
129*a1e26a70SApple OSS Distributions static void
access_failed_handler(int signum)130*a1e26a70SApple OSS Distributions access_failed_handler(int signum)
131*a1e26a70SApple OSS Distributions {
132*a1e26a70SApple OSS Distributions 	fault_state_t * fault_state;
133*a1e26a70SApple OSS Distributions 
134*a1e26a70SApple OSS Distributions 	/* This handler should ONLY handle SIGBUS. */
135*a1e26a70SApple OSS Distributions 	if (signum != SIGBUS) {
136*a1e26a70SApple OSS Distributions 		T_ASSERT_FAIL("Unexpected signal sent to handler");
137*a1e26a70SApple OSS Distributions 	}
138*a1e26a70SApple OSS Distributions 
139*a1e26a70SApple OSS Distributions 	if (!(fault_state = pthread_getspecific(fault_state_key))) {
140*a1e26a70SApple OSS Distributions 		T_ASSERT_FAIL("Failed to retrieve fault state");
141*a1e26a70SApple OSS Distributions 	}
142*a1e26a70SApple OSS Distributions 
143*a1e26a70SApple OSS Distributions 	if (!(fault_state->fault_expected)) {
144*a1e26a70SApple OSS Distributions 		T_ASSERT_FAIL("Unexpected fault taken");
145*a1e26a70SApple OSS Distributions 	}
146*a1e26a70SApple OSS Distributions 
147*a1e26a70SApple OSS Distributions 	/* We should not see a second fault. */
148*a1e26a70SApple OSS Distributions 	fault_state->fault_expected = false;
149*a1e26a70SApple OSS Distributions 
150*a1e26a70SApple OSS Distributions 	switch (fault_state->fault_strategy) {
151*a1e26a70SApple OSS Distributions 	case FAULT_STRAT_NONE:
152*a1e26a70SApple OSS Distributions 		T_ASSERT_FAIL("No fault strategy");
153*a1e26a70SApple OSS Distributions 
154*a1e26a70SApple OSS Distributions 		/* Just in case we try to do something different. */
155*a1e26a70SApple OSS Distributions 		break;
156*a1e26a70SApple OSS Distributions 	case FAULT_STRAT_RW_TPRO:
157*a1e26a70SApple OSS Distributions 		os_thread_self_restrict_tpro_to_rw();
158*a1e26a70SApple OSS Distributions 		break;
159*a1e26a70SApple OSS Distributions 	}
160*a1e26a70SApple OSS Distributions 
161*a1e26a70SApple OSS Distributions 	fault_state->fault_count++;
162*a1e26a70SApple OSS Distributions }
163*a1e26a70SApple OSS Distributions 
164*a1e26a70SApple OSS Distributions /*
165*a1e26a70SApple OSS Distributions  * Attempt the specified access; if the access faults, this will return true;
166*a1e26a70SApple OSS Distributions  * otherwise, it will return false.
167*a1e26a70SApple OSS Distributions  */
168*a1e26a70SApple OSS Distributions static bool
does_access_fault(access_type_t access_type,void * addr,uint32_t value)169*a1e26a70SApple OSS Distributions does_access_fault(access_type_t access_type, void * addr, uint32_t value)
170*a1e26a70SApple OSS Distributions {
171*a1e26a70SApple OSS Distributions 	uint64_t old_fault_count;
172*a1e26a70SApple OSS Distributions 	uint64_t new_fault_count;
173*a1e26a70SApple OSS Distributions 
174*a1e26a70SApple OSS Distributions 	fault_state_t * fault_state;
175*a1e26a70SApple OSS Distributions 
176*a1e26a70SApple OSS Distributions 	struct sigaction old_action; /* Save area for any existing action. */
177*a1e26a70SApple OSS Distributions 	struct sigaction new_action; /* The action we wish to install for SIGBUS. */
178*a1e26a70SApple OSS Distributions 
179*a1e26a70SApple OSS Distributions 	bool retval = false;
180*a1e26a70SApple OSS Distributions 
181*a1e26a70SApple OSS Distributions 	new_action.sa_handler = access_failed_handler; /* A handler for write failures. */
182*a1e26a70SApple OSS Distributions 	new_action.sa_mask    = 0;                     /* Don't modify the mask. */
183*a1e26a70SApple OSS Distributions 	new_action.sa_flags   = 0;                     /* Flags?  Who needs those? */
184*a1e26a70SApple OSS Distributions 
185*a1e26a70SApple OSS Distributions 	if (addr == NULL) {
186*a1e26a70SApple OSS Distributions 		T_ASSERT_FAIL("Access attempted against NULL");
187*a1e26a70SApple OSS Distributions 	}
188*a1e26a70SApple OSS Distributions 
189*a1e26a70SApple OSS Distributions 	if (!(fault_state = pthread_getspecific(fault_state_key))) {
190*a1e26a70SApple OSS Distributions 		T_ASSERT_FAIL("Failed to retrieve fault state");
191*a1e26a70SApple OSS Distributions 	}
192*a1e26a70SApple OSS Distributions 
193*a1e26a70SApple OSS Distributions 	old_fault_count = fault_state->fault_count;
194*a1e26a70SApple OSS Distributions 
195*a1e26a70SApple OSS Distributions 	/* Install a handler so that we can catch SIGBUS. */
196*a1e26a70SApple OSS Distributions 	sigaction(SIGBUS, &new_action, &old_action);
197*a1e26a70SApple OSS Distributions 
198*a1e26a70SApple OSS Distributions 	/* Perform the requested operation. */
199*a1e26a70SApple OSS Distributions 	switch (access_type) {
200*a1e26a70SApple OSS Distributions 	case ACCESS_READ:
201*a1e26a70SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_RW_TPRO;
202*a1e26a70SApple OSS Distributions 		fault_state->fault_expected = true;
203*a1e26a70SApple OSS Distributions 
204*a1e26a70SApple OSS Distributions 		__sync_synchronize();
205*a1e26a70SApple OSS Distributions 
206*a1e26a70SApple OSS Distributions #if defined(__arm64__)
207*a1e26a70SApple OSS Distributions 		uint8_t a = *((volatile uint8_t *)addr);
208*a1e26a70SApple OSS Distributions #endif
209*a1e26a70SApple OSS Distributions 		__sync_synchronize();
210*a1e26a70SApple OSS Distributions 
211*a1e26a70SApple OSS Distributions 		fault_state->fault_expected = false;
212*a1e26a70SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_NONE;
213*a1e26a70SApple OSS Distributions 
214*a1e26a70SApple OSS Distributions 		break;
215*a1e26a70SApple OSS Distributions 
216*a1e26a70SApple OSS Distributions 	case ACCESS_WRITE:
217*a1e26a70SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_RW_TPRO;
218*a1e26a70SApple OSS Distributions 		fault_state->fault_expected = true;
219*a1e26a70SApple OSS Distributions 
220*a1e26a70SApple OSS Distributions 		__sync_synchronize();
221*a1e26a70SApple OSS Distributions 
222*a1e26a70SApple OSS Distributions 		*((volatile uint32_t *)addr) = value;
223*a1e26a70SApple OSS Distributions 
224*a1e26a70SApple OSS Distributions 		__sync_synchronize();
225*a1e26a70SApple OSS Distributions 
226*a1e26a70SApple OSS Distributions 		fault_state->fault_expected = false;
227*a1e26a70SApple OSS Distributions 		fault_state->fault_strategy = FAULT_STRAT_NONE;
228*a1e26a70SApple OSS Distributions 
229*a1e26a70SApple OSS Distributions 		break;
230*a1e26a70SApple OSS Distributions 	}
231*a1e26a70SApple OSS Distributions 
232*a1e26a70SApple OSS Distributions 	/* Restore the old SIGBUS handler. */
233*a1e26a70SApple OSS Distributions 	sigaction(SIGBUS, &old_action, NULL);
234*a1e26a70SApple OSS Distributions 
235*a1e26a70SApple OSS Distributions 	new_fault_count = fault_state->fault_count;
236*a1e26a70SApple OSS Distributions 
237*a1e26a70SApple OSS Distributions 	if (new_fault_count > old_fault_count) {
238*a1e26a70SApple OSS Distributions 		/* Indicate that we took a fault. */
239*a1e26a70SApple OSS Distributions 		retval = true;
240*a1e26a70SApple OSS Distributions 	}
241*a1e26a70SApple OSS Distributions 
242*a1e26a70SApple OSS Distributions 	return retval;
243*a1e26a70SApple OSS Distributions }
244*a1e26a70SApple OSS Distributions 
245*a1e26a70SApple OSS Distributions static bool
does_read_fault(void * addr)246*a1e26a70SApple OSS Distributions does_read_fault(void * addr)
247*a1e26a70SApple OSS Distributions {
248*a1e26a70SApple OSS Distributions 	return does_access_fault(ACCESS_READ, addr, 0);
249*a1e26a70SApple OSS Distributions }
250*a1e26a70SApple OSS Distributions 
251*a1e26a70SApple OSS Distributions static bool
does_write_fault(void * addr,uint32_t value)252*a1e26a70SApple OSS Distributions does_write_fault(void * addr, uint32_t value)
253*a1e26a70SApple OSS Distributions {
254*a1e26a70SApple OSS Distributions 	return does_access_fault(ACCESS_WRITE, addr, value);
255*a1e26a70SApple OSS Distributions }
256*a1e26a70SApple OSS Distributions 
257*a1e26a70SApple OSS Distributions static bool
has_pager_support(void)258*a1e26a70SApple OSS Distributions has_pager_support(void)
259*a1e26a70SApple OSS Distributions {
260*a1e26a70SApple OSS Distributions 	uint32_t enabled = false;
261*a1e26a70SApple OSS Distributions 	size_t output_size = sizeof(enabled);
262*a1e26a70SApple OSS Distributions 
263*a1e26a70SApple OSS Distributions 	(void)sysctlbyname("vm.pmap_tpro_pagers",
264*a1e26a70SApple OSS Distributions 	    &enabled, &output_size, NULL, 0);
265*a1e26a70SApple OSS Distributions 	return enabled;
266*a1e26a70SApple OSS Distributions }
267*a1e26a70SApple OSS Distributions 
268*a1e26a70SApple OSS Distributions static void
cleanup(void)269*a1e26a70SApple OSS Distributions cleanup(void)
270*a1e26a70SApple OSS Distributions {
271*a1e26a70SApple OSS Distributions 	fault_state_t * fault_state;
272*a1e26a70SApple OSS Distributions 
273*a1e26a70SApple OSS Distributions 	if (!(fault_state = pthread_getspecific(fault_state_key))) {
274*a1e26a70SApple OSS Distributions 		T_ASSERT_FAIL("Failed to retrieve fault state");
275*a1e26a70SApple OSS Distributions 
276*a1e26a70SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_setspecific(fault_state_key, NULL), "Remove fault_state");
277*a1e26a70SApple OSS Distributions 		fault_state_destroy(fault_state);
278*a1e26a70SApple OSS Distributions 	}
279*a1e26a70SApple OSS Distributions 
280*a1e26a70SApple OSS Distributions 	if (key_created) {
281*a1e26a70SApple OSS Distributions 		T_ASSERT_POSIX_ZERO(pthread_key_delete(fault_state_key), "Delete fault state key");
282*a1e26a70SApple OSS Distributions 	}
283*a1e26a70SApple OSS Distributions 
284*a1e26a70SApple OSS Distributions 	return;
285*a1e26a70SApple OSS Distributions }
286*a1e26a70SApple OSS Distributions 
287*a1e26a70SApple OSS Distributions static void
288*a1e26a70SApple OSS Distributions thread_self_restrict_test(void (^test)(void))
289*a1e26a70SApple OSS Distributions {
290*a1e26a70SApple OSS Distributions 	int err = 0;
291*a1e26a70SApple OSS Distributions 
292*a1e26a70SApple OSS Distributions 	T_SETUPBEGIN;
293*a1e26a70SApple OSS Distributions 	T_ATEND(cleanup);
294*a1e26a70SApple OSS Distributions 
295*a1e26a70SApple OSS Distributions 	/* Set up the necessary state for the test. */
296*a1e26a70SApple OSS Distributions 	err = pthread_key_create(&fault_state_key, fault_state_destroy);
297*a1e26a70SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(err, 0, "Create pthread key");
298*a1e26a70SApple OSS Distributions 	key_created = true;
299*a1e26a70SApple OSS Distributions 
300*a1e26a70SApple OSS Distributions 	T_ASSERT_NOTNULL(fault_state_create(), "Create fault state");
301*a1e26a70SApple OSS Distributions 	T_SETUPEND;
302*a1e26a70SApple OSS Distributions 
303*a1e26a70SApple OSS Distributions 	test();
304*a1e26a70SApple OSS Distributions }
305*a1e26a70SApple OSS Distributions 
306*a1e26a70SApple OSS Distributions static void
fork_child_test(const_page_t * state)307*a1e26a70SApple OSS Distributions fork_child_test(const_page_t *state)
308*a1e26a70SApple OSS Distributions {
309*a1e26a70SApple OSS Distributions 	pid_t pid;
310*a1e26a70SApple OSS Distributions 	int statloc;
311*a1e26a70SApple OSS Distributions 
312*a1e26a70SApple OSS Distributions 	pid = fork();
313*a1e26a70SApple OSS Distributions 	if (pid == 0) {
314*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(state->magic, 0x45454545, "Expected magic on fork");
315*a1e26a70SApple OSS Distributions 
316*a1e26a70SApple OSS Distributions 		os_thread_self_restrict_tpro_to_rw();
317*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write in child");
318*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(does_write_fault((void *)&state->bytes, 0x47474747), 0, "write to pager backed memory in child (no fault)");
319*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x46464646), 0, "write to pager backed memory in child (no fault)");
320*a1e26a70SApple OSS Distributions 		exit(0);
321*a1e26a70SApple OSS Distributions 	}
322*a1e26a70SApple OSS Distributions 
323*a1e26a70SApple OSS Distributions 	if (pid < 0) {
324*a1e26a70SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(pid, "fork");
325*a1e26a70SApple OSS Distributions 	}
326*a1e26a70SApple OSS Distributions 
327*a1e26a70SApple OSS Distributions 	waitpid(pid, &statloc, 0);
328*a1e26a70SApple OSS Distributions }
329*a1e26a70SApple OSS Distributions 
330*a1e26a70SApple OSS Distributions static void
pager_test(const_page_t * state)331*a1e26a70SApple OSS Distributions pager_test(const_page_t *state)
332*a1e26a70SApple OSS Distributions {
333*a1e26a70SApple OSS Distributions 	kern_return_t kr;
334*a1e26a70SApple OSS Distributions 	uint32_t pre;
335*a1e26a70SApple OSS Distributions 	vm_prot_t curprot, maxprot;
336*a1e26a70SApple OSS Distributions 	mach_vm_address_t addr = 0;
337*a1e26a70SApple OSS Distributions 	const_page_t *copy_state = NULL;
338*a1e26a70SApple OSS Distributions 	mach_port_t cow_port = MACH_PORT_NULL;
339*a1e26a70SApple OSS Distributions 	memory_object_size_t me_size = PAGE_SIZE;
340*a1e26a70SApple OSS Distributions 
341*a1e26a70SApple OSS Distributions 	/*
342*a1e26a70SApple OSS Distributions 	 * Validate our initial status quo. TPRO permissions should be RO,
343*a1e26a70SApple OSS Distributions 	 * so we should be able to read from our pager backed mapping but
344*a1e26a70SApple OSS Distributions 	 * should fault when trying to write to it.
345*a1e26a70SApple OSS Distributions 	 */
346*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only");
347*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_read_fault(MAGIC(state)), 0, "read from pager backed memory");
348*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x43434343), 1, "write to pager backed memory (detect fault)");
349*a1e26a70SApple OSS Distributions 
350*a1e26a70SApple OSS Distributions 	/*
351*a1e26a70SApple OSS Distributions 	 * Toggle permissions to RW and attempt a write. We should succeed.
352*a1e26a70SApple OSS Distributions 	 */
353*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
354*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write");
355*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x44444444), 0, "write to pager backed memory (no fault)");
356*a1e26a70SApple OSS Distributions 
357*a1e26a70SApple OSS Distributions 	/*
358*a1e26a70SApple OSS Distributions 	 * Toggle permissions to RO and attempt a write. We should detect
359*a1e26a70SApple OSS Distributions 	 * the fault
360*a1e26a70SApple OSS Distributions 	 */
361*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
362*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x45454545), 1, "write to pager backed memory (detect fault)");
363*a1e26a70SApple OSS Distributions 
364*a1e26a70SApple OSS Distributions 	/*
365*a1e26a70SApple OSS Distributions 	 * Fork a child process and ensure that writes into the pager backed
366*a1e26a70SApple OSS Distributions 	 * regions are not observed by the parent. They should now be COW.
367*a1e26a70SApple OSS Distributions 	 */
368*a1e26a70SApple OSS Distributions 	pre = state->magic;
369*a1e26a70SApple OSS Distributions 	fork_child_test(state);
370*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(pre, state->magic, "write from child should not be observed");
371*a1e26a70SApple OSS Distributions 
372*a1e26a70SApple OSS Distributions 	/*
373*a1e26a70SApple OSS Distributions 	 * Ensure that if we remap the target region in a shared manner that we
374*a1e26a70SApple OSS Distributions 	 * inherit TPRO. Remapping should be successful but we still rely on
375*a1e26a70SApple OSS Distributions 	 * TPRO permissions to toggle r--/rw-
376*a1e26a70SApple OSS Distributions 	 */
377*a1e26a70SApple OSS Distributions 	kr = mach_vm_remap(mach_task_self(),
378*a1e26a70SApple OSS Distributions 	    &addr,
379*a1e26a70SApple OSS Distributions 	    PAGE_SIZE,
380*a1e26a70SApple OSS Distributions 	    0,                /* mask */
381*a1e26a70SApple OSS Distributions 	    VM_FLAGS_ANYWHERE,
382*a1e26a70SApple OSS Distributions 	    mach_task_self(),
383*a1e26a70SApple OSS Distributions 	    (mach_vm_address_t)state,
384*a1e26a70SApple OSS Distributions 	    FALSE,                /* copy */
385*a1e26a70SApple OSS Distributions 	    &curprot,
386*a1e26a70SApple OSS Distributions 	    &maxprot,
387*a1e26a70SApple OSS Distributions 	    VM_INHERIT_DEFAULT);
388*a1e26a70SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_remap(SHARED)");
389*a1e26a70SApple OSS Distributions 	copy_state = (const_page_t *)addr;
390*a1e26a70SApple OSS Distributions 
391*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
392*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO configured as read-only");
393*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(curprot, VM_PROT_READ, "TPRO region should be VM_PROT_READ");
394*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 1, "write to remapped region (detect fault)");
395*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
396*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 0, "write to remapped region (no fault)");
397*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(0x46464646, state->magic, "write into copied region should be observed");
398*a1e26a70SApple OSS Distributions 
399*a1e26a70SApple OSS Distributions 	/*
400*a1e26a70SApple OSS Distributions 	 * Ensure that if we remap the region that we do not observe writes to
401*a1e26a70SApple OSS Distributions 	 * the new copy in __DATA_CONST itself.
402*a1e26a70SApple OSS Distributions 	 */
403*a1e26a70SApple OSS Distributions 	kr = mach_vm_remap(mach_task_self(),
404*a1e26a70SApple OSS Distributions 	    (mach_vm_address_t *)&copy_state,
405*a1e26a70SApple OSS Distributions 	    PAGE_SIZE,
406*a1e26a70SApple OSS Distributions 	    0,                /* mask */
407*a1e26a70SApple OSS Distributions 	    VM_FLAGS_ANYWHERE,
408*a1e26a70SApple OSS Distributions 	    mach_task_self(),
409*a1e26a70SApple OSS Distributions 	    (mach_vm_address_t)state,
410*a1e26a70SApple OSS Distributions 	    TRUE,                /* copy */
411*a1e26a70SApple OSS Distributions 	    &curprot,
412*a1e26a70SApple OSS Distributions 	    &maxprot,
413*a1e26a70SApple OSS Distributions 	    VM_INHERIT_DEFAULT);
414*a1e26a70SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_remap(COPY)");
415*a1e26a70SApple OSS Distributions 
416*a1e26a70SApple OSS Distributions 	/*
417*a1e26a70SApple OSS Distributions 	 * Toggle TPRO RW and write to the new copied region
418*a1e26a70SApple OSS Distributions 	 */
419*a1e26a70SApple OSS Distributions 	pre = state->magic;
420*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
421*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), true, "TPRO region configured as read-write");
422*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x46464646), 0, "write to pager backed memory (no fault)");
423*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed");
424*a1e26a70SApple OSS Distributions 
425*a1e26a70SApple OSS Distributions 	/*
426*a1e26a70SApple OSS Distributions 	 * Make a memory entry for our target region and attempt to map it in
427*a1e26a70SApple OSS Distributions 	 * in a shared fashion. We should succeed but it should transparently
428*a1e26a70SApple OSS Distributions 	 * copy the target VM object as extracting TPRO VM entries will fail.
429*a1e26a70SApple OSS Distributions 	 * Writes to the new region should therefore not be observed.
430*a1e26a70SApple OSS Distributions 	 */
431*a1e26a70SApple OSS Distributions 	me_size = PAGE_SIZE;
432*a1e26a70SApple OSS Distributions 	kr = mach_make_memory_entry_64(mach_task_self(),
433*a1e26a70SApple OSS Distributions 	    &me_size,
434*a1e26a70SApple OSS Distributions 	    (mach_vm_address_t)state,
435*a1e26a70SApple OSS Distributions 	    MAP_MEM_VM_SHARE | VM_PROT_READ | VM_PROT_WRITE,
436*a1e26a70SApple OSS Distributions 	    &cow_port,
437*a1e26a70SApple OSS Distributions 	    MACH_PORT_NULL);
438*a1e26a70SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kr, "mach_make_memory_entry_64(MAP_MEM_VM_SHARE)");
439*a1e26a70SApple OSS Distributions 
440*a1e26a70SApple OSS Distributions 	pre = state->magic;
441*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x48484849), 0, "write to mapped copy region (no fault)");
442*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed");
443*a1e26a70SApple OSS Distributions 
444*a1e26a70SApple OSS Distributions 	copy_state = NULL;
445*a1e26a70SApple OSS Distributions 	kr = mach_vm_map(mach_task_self(),
446*a1e26a70SApple OSS Distributions 	    (mach_vm_address_t *)&copy_state,
447*a1e26a70SApple OSS Distributions 	    PAGE_SIZE,
448*a1e26a70SApple OSS Distributions 	    0,              /* mask */
449*a1e26a70SApple OSS Distributions 	    VM_FLAGS_ANYWHERE,
450*a1e26a70SApple OSS Distributions 	    cow_port,
451*a1e26a70SApple OSS Distributions 	    0,              /* offset */
452*a1e26a70SApple OSS Distributions 	    TRUE,           /* copy */
453*a1e26a70SApple OSS Distributions 	    VM_PROT_READ | VM_PROT_WRITE,
454*a1e26a70SApple OSS Distributions 	    VM_PROT_READ | VM_PROT_WRITE,
455*a1e26a70SApple OSS Distributions 	    VM_INHERIT_DEFAULT);
456*a1e26a70SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kr, "mach_vm_map(cow_port)");
457*a1e26a70SApple OSS Distributions 
458*a1e26a70SApple OSS Distributions 	/*
459*a1e26a70SApple OSS Distributions 	 * Pages of the copy will no longer be mapped in as TPRO. Both
460*a1e26a70SApple OSS Distributions 	 * read/writes should work even with TPRO toggled RO.
461*a1e26a70SApple OSS Distributions 	 */
462*a1e26a70SApple OSS Distributions 	pre = state->magic;
463*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
464*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(copy_state), 0x48484848), 0, "write to mapped copy region (no fault)");
465*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(pre, state->magic, "write into copied region should not be observed");
466*a1e26a70SApple OSS Distributions 
467*a1e26a70SApple OSS Distributions 	/*
468*a1e26a70SApple OSS Distributions 	 * We've explored a number of ways to perform copies on the target
469*a1e26a70SApple OSS Distributions 	 * objects in __DATA_CONST. Our first target page (&pager_state.one)
470*a1e26a70SApple OSS Distributions 	 * should now be marked RO without TPRO permissions to handle any
471*a1e26a70SApple OSS Distributions 	 * incoming write faults. Write to it directly again to ensure we
472*a1e26a70SApple OSS Distributions 	 * fault back in with TPRO permissions.
473*a1e26a70SApple OSS Distributions 	 */
474*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
475*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x49494949), 1, "write to pager backed memory (detect fault)");
476*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
477*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x4a4a4a4a), 0, "write to pager backed memory (no fault)");
478*a1e26a70SApple OSS Distributions 
479*a1e26a70SApple OSS Distributions 	/*
480*a1e26a70SApple OSS Distributions 	 * Now we attempt to have the page paged out. On systems which support the
481*a1e26a70SApple OSS Distributions 	 * compressor, we'll get paged out/compressed. On fault we should
482*a1e26a70SApple OSS Distributions 	 * be pmapped back in with TPRO permissions.
483*a1e26a70SApple OSS Distributions 	 */
484*a1e26a70SApple OSS Distributions 	mach_vm_behavior_set(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE, VM_BEHAVIOR_PAGEOUT);
485*a1e26a70SApple OSS Distributions 
486*a1e26a70SApple OSS Distributions 	/*
487*a1e26a70SApple OSS Distributions 	 * Can verify in debugger at this point that page(s) have been
488*a1e26a70SApple OSS Distributions 	 * paged out. If compressor pager is available the page should
489*a1e26a70SApple OSS Distributions 	 * not be resident and compressor pager should be tied to the
490*a1e26a70SApple OSS Distributions 	 * top level VM object.
491*a1e26a70SApple OSS Distributions 	 */
492*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
493*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x49494949), 1, "write to pager backed memory after pageout (detect fault)");
494*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_rw();
495*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(does_write_fault(MAGIC(state), 0x4a4a4a4a), 0, "write to pager backed memory after pageout (no fault)");
496*a1e26a70SApple OSS Distributions 
497*a1e26a70SApple OSS Distributions 	/*
498*a1e26a70SApple OSS Distributions 	 * Try and reprotect the region. We should fail
499*a1e26a70SApple OSS Distributions 	 */
500*a1e26a70SApple OSS Distributions 	kr = vm_protect(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE, FALSE, VM_PROT_DEFAULT);
501*a1e26a70SApple OSS Distributions 	T_EXPECT_POSIX_ERROR(kr, KERN_PROTECTION_FAILURE, "vm_protect(RW) should fail");
502*a1e26a70SApple OSS Distributions 
503*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
504*a1e26a70SApple OSS Distributions }
505*a1e26a70SApple OSS Distributions 
506*a1e26a70SApple OSS Distributions static void
mmap_test(const_page_t * state)507*a1e26a70SApple OSS Distributions mmap_test(const_page_t *state)
508*a1e26a70SApple OSS Distributions {
509*a1e26a70SApple OSS Distributions 	void *mapping;
510*a1e26a70SApple OSS Distributions 
511*a1e26a70SApple OSS Distributions 	/*
512*a1e26a70SApple OSS Distributions 	 * Validate our initial status quo. TPRO permissions should be RO,
513*a1e26a70SApple OSS Distributions 	 * so we should be able to read from our pager backed mapping but
514*a1e26a70SApple OSS Distributions 	 * should fault when trying to write to it.
515*a1e26a70SApple OSS Distributions 	 */
516*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only");
517*a1e26a70SApple OSS Distributions 
518*a1e26a70SApple OSS Distributions 	/*
519*a1e26a70SApple OSS Distributions 	 * Attempt to mmap a fixed allocation over our TPRO region.
520*a1e26a70SApple OSS Distributions 	 * TPRO region should be permanent and should disallow being
521*a1e26a70SApple OSS Distributions 	 * overwritten.
522*a1e26a70SApple OSS Distributions 	 */
523*a1e26a70SApple OSS Distributions 	mapping = mmap(state, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
524*a1e26a70SApple OSS Distributions 	T_ASSERT_EQ(mapping, MAP_FAILED, "Map over TPRO range should fail");
525*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
526*a1e26a70SApple OSS Distributions }
527*a1e26a70SApple OSS Distributions 
528*a1e26a70SApple OSS Distributions static void
vm_allocate_test(const_page_t * state)529*a1e26a70SApple OSS Distributions vm_allocate_test(const_page_t *state)
530*a1e26a70SApple OSS Distributions {
531*a1e26a70SApple OSS Distributions 	kern_return_t kr;
532*a1e26a70SApple OSS Distributions 	mach_vm_address_t addr = (mach_vm_address_t)state;
533*a1e26a70SApple OSS Distributions 	vm_region_basic_info_data_64_t info;
534*a1e26a70SApple OSS Distributions 	mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64;
535*a1e26a70SApple OSS Distributions 	mach_vm_size_t size = PAGE_SIZE;
536*a1e26a70SApple OSS Distributions 	mach_port_t unused = MACH_PORT_NULL;
537*a1e26a70SApple OSS Distributions 
538*a1e26a70SApple OSS Distributions 	/*
539*a1e26a70SApple OSS Distributions 	 * Validate our initial status quo. TPRO permissions should be RO,
540*a1e26a70SApple OSS Distributions 	 * so we should be able to read from our pager backed mapping but
541*a1e26a70SApple OSS Distributions 	 * should fault when trying to write to it.
542*a1e26a70SApple OSS Distributions 	 */
543*a1e26a70SApple OSS Distributions 	T_EXPECT_EQ(os_thread_self_restrict_tpro_is_writable(), false, "TPRO region starts read-only");
544*a1e26a70SApple OSS Distributions 
545*a1e26a70SApple OSS Distributions 	/*
546*a1e26a70SApple OSS Distributions 	 * Deallocate the TPRO region. This should succeed but leave the region
547*a1e26a70SApple OSS Distributions 	 * intact with no permissions. Further allocations should not be able to
548*a1e26a70SApple OSS Distributions 	 * obtain the same address.
549*a1e26a70SApple OSS Distributions 	 */
550*a1e26a70SApple OSS Distributions 	kr = mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)state, PAGE_SIZE);
551*a1e26a70SApple OSS Distributions 	T_EXPECT_POSIX_ERROR(kr, KERN_SUCCESS, "vm_deallocate should succeed");
552*a1e26a70SApple OSS Distributions 
553*a1e26a70SApple OSS Distributions 	kr = mach_vm_allocate(mach_task_self(), (mach_vm_address_t *)&addr, PAGE_SIZE, VM_FLAGS_FIXED);
554*a1e26a70SApple OSS Distributions 	T_EXPECT_POSIX_ERROR(kr, KERN_NO_SPACE, "vm_allocate should fail with KERN_NO_SPACE");
555*a1e26a70SApple OSS Distributions 
556*a1e26a70SApple OSS Distributions 	/*
557*a1e26a70SApple OSS Distributions 	 * Lookup the target region and confirm that all permissions have been
558*a1e26a70SApple OSS Distributions 	 * removed.
559*a1e26a70SApple OSS Distributions 	 */
560*a1e26a70SApple OSS Distributions 	kr = mach_vm_region(mach_task_self(), &addr, &size, VM_REGION_BASIC_INFO_64, (vm_region_info_t)&info, &count, &unused);
561*a1e26a70SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_ERROR(kr, KERN_SUCCESS, "mach_vm_region should succeed");
562*a1e26a70SApple OSS Distributions 
563*a1e26a70SApple OSS Distributions 	T_ASSERT_EQ(info.protection, VM_PROT_NONE, "Entry should have no permissions");
564*a1e26a70SApple OSS Distributions 	T_ASSERT_EQ(info.max_protection, VM_PROT_NONE, "Entry should have no permissions");
565*a1e26a70SApple OSS Distributions 	os_thread_self_restrict_tpro_to_ro();
566*a1e26a70SApple OSS Distributions }
567*a1e26a70SApple OSS Distributions 
568*a1e26a70SApple OSS Distributions T_DECL(thread_self_restrict_pagers,
569*a1e26a70SApple OSS Distributions     "Verify that the TPRO pager interfaces work correctly", T_META_TAG_VM_PREFERRED)
570*a1e26a70SApple OSS Distributions {
571*a1e26a70SApple OSS Distributions #if __arm64__
572*a1e26a70SApple OSS Distributions 	/* Check to see that we support the necessary hardware features. */
573*a1e26a70SApple OSS Distributions 	if (!os_thread_self_restrict_tpro_is_supported() || !has_pager_support()) {
574*a1e26a70SApple OSS Distributions 		T_SKIP("no hardware TPRO support enabled on this system");
575*a1e26a70SApple OSS Distributions 	}
576*a1e26a70SApple OSS Distributions 
577*a1e26a70SApple OSS Distributions 	thread_self_restrict_test(^{
578*a1e26a70SApple OSS Distributions 		pager_test(&pager_state.one);
579*a1e26a70SApple OSS Distributions 
580*a1e26a70SApple OSS Distributions 		/*
581*a1e26a70SApple OSS Distributions 		 * Ensure that touching the second pager supported page exhibits
582*a1e26a70SApple OSS Distributions 		 * identical behaviour in order to validate the transitions between
583*a1e26a70SApple OSS Distributions 		 * VM entry & copy object chains.
584*a1e26a70SApple OSS Distributions 		 */
585*a1e26a70SApple OSS Distributions 		pager_test(&pager_state.two);
586*a1e26a70SApple OSS Distributions 
587*a1e26a70SApple OSS Distributions 		/*
588*a1e26a70SApple OSS Distributions 		 * Try and write to a normal __DATA_CONST page that isn't backed by
589*a1e26a70SApple OSS Distributions 		 * the dyld pager. The kernel will have mapped this directly but
590*a1e26a70SApple OSS Distributions 		 * should still maintain TPRO protection.
591*a1e26a70SApple OSS Distributions 		 */
592*a1e26a70SApple OSS Distributions 		os_thread_self_restrict_tpro_to_ro();
593*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(does_write_fault(&pager_state.ro[0], 0x41414141), 1, "write to __DATA_CONST should succeed (no fault)");
594*a1e26a70SApple OSS Distributions 		os_thread_self_restrict_tpro_to_rw();
595*a1e26a70SApple OSS Distributions 		T_EXPECT_EQ(does_write_fault(&pager_state.ro[0], 0x41414141), 0, "write to __DATA_CONST should fail (detect fault)");
596*a1e26a70SApple OSS Distributions 	});
597*a1e26a70SApple OSS Distributions #else
598*a1e26a70SApple OSS Distributions 	T_SKIP("thread_self_restrict_pagers not supported on this system");
599*a1e26a70SApple OSS Distributions #endif /* __arm64__ */
600*a1e26a70SApple OSS Distributions }
601*a1e26a70SApple OSS Distributions 
602*a1e26a70SApple OSS Distributions T_DECL(thread_self_restrict_tpro_permanent,
603*a1e26a70SApple OSS Distributions     "Verify that TPRO VM entries are permanent")
604*a1e26a70SApple OSS Distributions {
605*a1e26a70SApple OSS Distributions #if __arm64__
606*a1e26a70SApple OSS Distributions 	/* Check to see that we support the necessary hardware features. */
607*a1e26a70SApple OSS Distributions 	if (!os_thread_self_restrict_tpro_is_supported() || !has_pager_support()) {
608*a1e26a70SApple OSS Distributions 		T_SKIP("no hardware TPRO support enabled on this system");
609*a1e26a70SApple OSS Distributions 	}
610*a1e26a70SApple OSS Distributions 
611*a1e26a70SApple OSS Distributions 	thread_self_restrict_test(^{
612*a1e26a70SApple OSS Distributions 		mmap_test(&pager_state.one);
613*a1e26a70SApple OSS Distributions 		vm_allocate_test(&pager_state.two);
614*a1e26a70SApple OSS Distributions 	});
615*a1e26a70SApple OSS Distributions #else
616*a1e26a70SApple OSS Distributions 	T_SKIP("thread_self_restrict_tpro_permanent not supported on this system");
617*a1e26a70SApple OSS Distributions #endif /* __arm64__ */
618*a1e26a70SApple OSS Distributions }
619