xref: /xnu-12377.81.4/tests/pmap_fault_on_commpage.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions #include <darwintest.h>
2*043036a2SApple OSS Distributions #include <machine/cpu_capabilities.h>
3*043036a2SApple OSS Distributions #include "test_utils.h"
4*043036a2SApple OSS Distributions 
5*043036a2SApple OSS Distributions #include <stdlib.h>
6*043036a2SApple OSS Distributions #include <signal.h>
7*043036a2SApple OSS Distributions 
8*043036a2SApple OSS Distributions T_GLOBAL_META(
9*043036a2SApple OSS Distributions 	T_META_NAMESPACE("xnu.arm"),
10*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
11*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("arm"),
12*043036a2SApple OSS Distributions 	T_META_ENABLED(TARGET_CPU_ARM64),
13*043036a2SApple OSS Distributions 	T_META_OWNER("xi_han"),
14*043036a2SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
15*043036a2SApple OSS Distributions 	XNU_T_META_SOC_SPECIFIC
16*043036a2SApple OSS Distributions 	);
17*043036a2SApple OSS Distributions 
18*043036a2SApple OSS Distributions #if defined(__LP64__)
19*043036a2SApple OSS Distributions #define SIGNAL_EXPECTED        SIGBUS
20*043036a2SApple OSS Distributions #define SIGNAL_EXPECTED_STR    "SIGBUS"
21*043036a2SApple OSS Distributions #else
22*043036a2SApple OSS Distributions /* On arm64_32, _COMM_PAGE_START_ADDRESS is out of normal VA range, so a SIGSEGV is expected if there's a fault. */
23*043036a2SApple OSS Distributions #define SIGNAL_EXPECTED        SIGSEGV
24*043036a2SApple OSS Distributions #define SIGNAL_EXPECTED_STR    "SIGSEGV"
25*043036a2SApple OSS Distributions #endif
26*043036a2SApple OSS Distributions 
27*043036a2SApple OSS Distributions #define TEST_STATE_TESTING_NONE              0
28*043036a2SApple OSS Distributions #define TEST_STATE_TESTING_READ              1
29*043036a2SApple OSS Distributions #define TEST_STATE_TESTING_WRITE             2
30*043036a2SApple OSS Distributions static volatile sig_atomic_t test_state;
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions static void
test_handler(int signum)33*043036a2SApple OSS Distributions test_handler(int signum)
34*043036a2SApple OSS Distributions {
35*043036a2SApple OSS Distributions 	T_ASSERT_EQ(signum, SIGNAL_EXPECTED, "received signal");
36*043036a2SApple OSS Distributions 
37*043036a2SApple OSS Distributions 	if (test_state == TEST_STATE_TESTING_READ) {
38*043036a2SApple OSS Distributions 		T_FAIL("read access triggered a %s", SIGNAL_EXPECTED_STR);
39*043036a2SApple OSS Distributions 	} else if (test_state == TEST_STATE_TESTING_WRITE) {
40*043036a2SApple OSS Distributions 		T_PASS("write access triggered a %s", SIGNAL_EXPECTED_STR);
41*043036a2SApple OSS Distributions 		exit(EXIT_SUCCESS);
42*043036a2SApple OSS Distributions 	} else {
43*043036a2SApple OSS Distributions 		T_FAIL("unexpected %s in test state %u", SIGNAL_EXPECTED_STR, (unsigned int)test_state);
44*043036a2SApple OSS Distributions 	}
45*043036a2SApple OSS Distributions }
46*043036a2SApple OSS Distributions 
47*043036a2SApple OSS Distributions T_DECL(pmap_commpage_access_test,
48*043036a2SApple OSS Distributions     "Verify system behavior on user access to the commpage", T_META_TAG_VM_NOT_PREFERRED)
49*043036a2SApple OSS Distributions {
50*043036a2SApple OSS Distributions 	test_state = TEST_STATE_TESTING_NONE;
51*043036a2SApple OSS Distributions 
52*043036a2SApple OSS Distributions 	struct sigaction sa;
53*043036a2SApple OSS Distributions 	sa.sa_handler = test_handler;
54*043036a2SApple OSS Distributions 	sa.sa_mask = 0;
55*043036a2SApple OSS Distributions 	sa.sa_flags = 0;
56*043036a2SApple OSS Distributions 	sigaction(SIGNAL_EXPECTED, &sa, NULL);
57*043036a2SApple OSS Distributions 
58*043036a2SApple OSS Distributions 	test_state = TEST_STATE_TESTING_READ;
59*043036a2SApple OSS Distributions 	*(volatile uint32_t *)_COMM_PAGE_START_ADDRESS;
60*043036a2SApple OSS Distributions 
61*043036a2SApple OSS Distributions 	T_PASS("read access must not trigger a %s", SIGNAL_EXPECTED_STR);
62*043036a2SApple OSS Distributions 
63*043036a2SApple OSS Distributions 	test_state = TEST_STATE_TESTING_WRITE;
64*043036a2SApple OSS Distributions 	*(volatile uint32_t *)_COMM_PAGE_START_ADDRESS = 0;
65*043036a2SApple OSS Distributions 
66*043036a2SApple OSS Distributions 	T_FAIL("write access must trigger a %s", SIGNAL_EXPECTED_STR);
67*043036a2SApple OSS Distributions }
68