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