xref: /xnu-11417.101.15/tests/cpu_counters/cpc_security_tests.c (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1*e3723e1fSApple OSS Distributions // Copyright 2023 (c) Apple Inc.  All rights reserved.
2*e3723e1fSApple OSS Distributions 
3*e3723e1fSApple OSS Distributions #include <darwintest.h>
4*e3723e1fSApple OSS Distributions #include <darwintest_utils.h>
5*e3723e1fSApple OSS Distributions #include <dirent.h>
6*e3723e1fSApple OSS Distributions #include <kperf/kpc.h>
7*e3723e1fSApple OSS Distributions #include <kperfdata/kpep.h>
8*e3723e1fSApple OSS Distributions #include <stdarg.h>
9*e3723e1fSApple OSS Distributions #include <stdbool.h>
10*e3723e1fSApple OSS Distributions #include <string.h>
11*e3723e1fSApple OSS Distributions #include <sys/guarded.h>
12*e3723e1fSApple OSS Distributions #include <sys/ioctl.h>
13*e3723e1fSApple OSS Distributions #include <sys/monotonic.h>
14*e3723e1fSApple OSS Distributions 
15*e3723e1fSApple OSS Distributions #include "test_utils.h"
16*e3723e1fSApple OSS Distributions 
17*e3723e1fSApple OSS Distributions #if __arm64__
18*e3723e1fSApple OSS Distributions #define HAS_CPC_SECURITY true
19*e3723e1fSApple OSS Distributions #else // __arm64__
20*e3723e1fSApple OSS Distributions #define HAS_CPC_SECURITY false
21*e3723e1fSApple OSS Distributions #endif // !__arm64__
22*e3723e1fSApple OSS Distributions 
23*e3723e1fSApple OSS Distributions #define _T_META_REQUIRES_CPC_SUPPORT \
24*e3723e1fSApple OSS Distributions 	T_META_REQUIRES_SYSCTL_EQ("kern.monotonic.supported", 1)
25*e3723e1fSApple OSS Distributions 
26*e3723e1fSApple OSS Distributions T_GLOBAL_META(
27*e3723e1fSApple OSS Distributions 	T_META_NAMESPACE("xnu.cpc"),
28*e3723e1fSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
29*e3723e1fSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("cpu counters"),
30*e3723e1fSApple OSS Distributions 	T_META_OWNER("mwidmann"),
31*e3723e1fSApple OSS Distributions 	T_META_CHECK_LEAKS(false),
32*e3723e1fSApple OSS Distributions 	T_META_ASROOT(true),
33*e3723e1fSApple OSS Distributions 	XNU_T_META_SOC_SPECIFIC,
34*e3723e1fSApple OSS Distributions 	T_META_ENABLED(HAS_CPC_SECURITY),
35*e3723e1fSApple OSS Distributions 	_T_META_REQUIRES_CPC_SUPPORT);
36*e3723e1fSApple OSS Distributions 
37*e3723e1fSApple OSS Distributions // Several of these tests have two variants to support running on development and release kernels.
38*e3723e1fSApple OSS Distributions // Tests prefixed with `secure_` put the development kernel into a secure CPC mode while tests prefixed with `release_` can run on the RELEASE build variant.
39*e3723e1fSApple OSS Distributions 
40*e3723e1fSApple OSS Distributions // Metadata for running on a development kernel in CPC secure mode.
41*e3723e1fSApple OSS Distributions #define _T_META_CPC_SECURE_ON_DEV T_META_SYSCTL_INT("kern.cpc.secure=1"), XNU_T_META_REQUIRES_DEVELOPMENT_KERNEL
42*e3723e1fSApple OSS Distributions 
43*e3723e1fSApple OSS Distributions static void
_assert_kpep_ok(int kpep_err,const char * fmt,...)44*e3723e1fSApple OSS Distributions _assert_kpep_ok(int kpep_err, const char *fmt, ...)
45*e3723e1fSApple OSS Distributions {
46*e3723e1fSApple OSS Distributions 	char msg[1024] = "";
47*e3723e1fSApple OSS Distributions 	va_list args;
48*e3723e1fSApple OSS Distributions 	va_start(args, fmt);
49*e3723e1fSApple OSS Distributions 	vsnprintf(msg, sizeof(msg), fmt, args);
50*e3723e1fSApple OSS Distributions 	va_end(args);
51*e3723e1fSApple OSS Distributions 	T_QUIET;
52*e3723e1fSApple OSS Distributions 	T_ASSERT_EQ(kpep_err, KPEP_ERR_NONE, "%s: %s", msg, kpep_strerror(kpep_err));
53*e3723e1fSApple OSS Distributions }
54*e3723e1fSApple OSS Distributions 
55*e3723e1fSApple OSS Distributions static void
_skip_for_db(const char * kind,int kpep_err)56*e3723e1fSApple OSS Distributions _skip_for_db(const char *kind, int kpep_err)
57*e3723e1fSApple OSS Distributions {
58*e3723e1fSApple OSS Distributions 	const char * const public_kpep_path = "/usr/share/kpep";
59*e3723e1fSApple OSS Distributions 	const char * const internal_kpep_path = "/usr/local/share/kpep";
60*e3723e1fSApple OSS Distributions 	const char * const paths[2] = { public_kpep_path, internal_kpep_path, };
61*e3723e1fSApple OSS Distributions 	for (int i = 0; i < 2; i++) {
62*e3723e1fSApple OSS Distributions 		const char * const path = paths[i];
63*e3723e1fSApple OSS Distributions 		T_LOG("contents of %s:", path);
64*e3723e1fSApple OSS Distributions 		DIR *dir = opendir(path);
65*e3723e1fSApple OSS Distributions 		if (dir) {
66*e3723e1fSApple OSS Distributions 			struct dirent *entry = NULL;
67*e3723e1fSApple OSS Distributions 			while ((entry = readdir(dir)) != NULL) {
68*e3723e1fSApple OSS Distributions 				T_LOG("    %s", entry->d_name);
69*e3723e1fSApple OSS Distributions 			}
70*e3723e1fSApple OSS Distributions 			(void)closedir(dir);
71*e3723e1fSApple OSS Distributions 		} else {
72*e3723e1fSApple OSS Distributions 			T_LOG("failed to open directory: %s", strerror(errno));
73*e3723e1fSApple OSS Distributions 		}
74*e3723e1fSApple OSS Distributions 	}
75*e3723e1fSApple OSS Distributions 	int cpu_family = 0;
76*e3723e1fSApple OSS Distributions 	size_t family_size = sizeof(cpu_family);
77*e3723e1fSApple OSS Distributions 	int ret = sysctlbyname("hw.cpufamily", &cpu_family, &family_size, NULL, 0);
78*e3723e1fSApple OSS Distributions 	if (ret != 0) {
79*e3723e1fSApple OSS Distributions 		T_LOG("HW CPU family: 0x%8x", cpu_family);
80*e3723e1fSApple OSS Distributions 	} else {
81*e3723e1fSApple OSS Distributions 		T_LOG("failed to get hw.cpufamily: %s", strerror(errno));
82*e3723e1fSApple OSS Distributions 	}
83*e3723e1fSApple OSS Distributions 	T_SKIP("cannot open %s event database: %s", kind, kpep_strerror(kpep_err));
84*e3723e1fSApple OSS Distributions }
85*e3723e1fSApple OSS Distributions 
86*e3723e1fSApple OSS Distributions // Check that a secure kernel disallows restricted events.
87*e3723e1fSApple OSS Distributions 
88*e3723e1fSApple OSS Distributions static void
check_secure_cpmu(void)89*e3723e1fSApple OSS Distributions check_secure_cpmu(void)
90*e3723e1fSApple OSS Distributions {
91*e3723e1fSApple OSS Distributions 	kpep_db_t public_db = NULL;
92*e3723e1fSApple OSS Distributions 	int ret = kpep_db_createx(NULL, KPEP_DB_FLAG_PUBLIC_ONLY, &public_db);
93*e3723e1fSApple OSS Distributions 	if (ret != KPEP_ERR_NONE) {
94*e3723e1fSApple OSS Distributions 		_skip_for_db("public", ret);
95*e3723e1fSApple OSS Distributions 	}
96*e3723e1fSApple OSS Distributions 	kpep_db_t internal_db = NULL;
97*e3723e1fSApple OSS Distributions 	ret = kpep_db_createx(NULL, KPEP_DB_FLAG_INTERNAL_ONLY, &internal_db);
98*e3723e1fSApple OSS Distributions 	if (ret != KPEP_ERR_NONE) {
99*e3723e1fSApple OSS Distributions 		_skip_for_db("internal", ret);
100*e3723e1fSApple OSS Distributions 	}
101*e3723e1fSApple OSS Distributions 	const char *na = NULL;
102*e3723e1fSApple OSS Distributions 	kpep_db_name(public_db, &na);
103*e3723e1fSApple OSS Distributions 
104*e3723e1fSApple OSS Distributions 	size_t internal_event_count = 0;
105*e3723e1fSApple OSS Distributions 	ret = kpep_db_events_count(internal_db, &internal_event_count);
106*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "getting internal event count");
107*e3723e1fSApple OSS Distributions 
108*e3723e1fSApple OSS Distributions 	kpep_event_t *internal_events = calloc(internal_event_count,
109*e3723e1fSApple OSS Distributions 	    sizeof(internal_events[0]));
110*e3723e1fSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO;
111*e3723e1fSApple OSS Distributions 	T_ASSERT_NOTNULL(internal_events, "allocate space for internal events");
112*e3723e1fSApple OSS Distributions 
113*e3723e1fSApple OSS Distributions 	ret = kpep_db_events(internal_db, internal_events,
114*e3723e1fSApple OSS Distributions 	    internal_event_count * sizeof(internal_events[0]));
115*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "getting internal events");
116*e3723e1fSApple OSS Distributions 
117*e3723e1fSApple OSS Distributions 	kpep_config_t config = NULL;
118*e3723e1fSApple OSS Distributions 	ret = kpep_config_create(internal_db, &config);
119*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "creating event configuration");
120*e3723e1fSApple OSS Distributions 	ret = kpep_config_force_counters(config);
121*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "forcing counters with configuration");
122*e3723e1fSApple OSS Distributions 
123*e3723e1fSApple OSS Distributions 	size_t kpc_count_allocated = 0;
124*e3723e1fSApple OSS Distributions 	uint64_t *kpc_configs = NULL;
125*e3723e1fSApple OSS Distributions 	size_t *kpc_map = NULL;
126*e3723e1fSApple OSS Distributions 
127*e3723e1fSApple OSS Distributions 	unsigned int tested = 0;
128*e3723e1fSApple OSS Distributions 	unsigned int filtered = 0;
129*e3723e1fSApple OSS Distributions 	unsigned int public_tested = 0;
130*e3723e1fSApple OSS Distributions 	for (size_t i = 0; i < internal_event_count; i++) {
131*e3723e1fSApple OSS Distributions 		kpep_event_t event = internal_events[i];
132*e3723e1fSApple OSS Distributions 		const char *name = NULL;
133*e3723e1fSApple OSS Distributions 		ret = kpep_event_alias(event, &name);
134*e3723e1fSApple OSS Distributions 		if (!name) {
135*e3723e1fSApple OSS Distributions 			ret = kpep_event_name(event, &name);
136*e3723e1fSApple OSS Distributions 		}
137*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting event name");
138*e3723e1fSApple OSS Distributions 		if (strncmp(name, "FIXED", strlen("FIXED")) == 0) {
139*e3723e1fSApple OSS Distributions 			T_LOG("skipping non-configurable %s event", name);
140*e3723e1fSApple OSS Distributions 			continue;
141*e3723e1fSApple OSS Distributions 		}
142*e3723e1fSApple OSS Distributions 		bool empty_event = strcmp(name, "NO_EVNT") == 0;
143*e3723e1fSApple OSS Distributions 		if (empty_event) {
144*e3723e1fSApple OSS Distributions 			continue;
145*e3723e1fSApple OSS Distributions 		}
146*e3723e1fSApple OSS Distributions 
147*e3723e1fSApple OSS Distributions 		kpep_event_t public_event = NULL;
148*e3723e1fSApple OSS Distributions 		ret = kpep_db_event(public_db, name, &public_event);
149*e3723e1fSApple OSS Distributions 		bool internal_only = ret == KPEP_ERR_EVENT_NOT_FOUND;
150*e3723e1fSApple OSS Distributions 		ret = kpep_config_add_event(config, &event, 0, NULL);
151*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "adding event %s to configuration", name);
152*e3723e1fSApple OSS Distributions 
153*e3723e1fSApple OSS Distributions 		size_t kpc_count = 0;
154*e3723e1fSApple OSS Distributions 		ret = kpep_config_kpc_count(config, &kpc_count);
155*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting KPC count");
156*e3723e1fSApple OSS Distributions 		if (kpc_count == 0) {
157*e3723e1fSApple OSS Distributions 			T_LOG("skipping event %s with no configuration", name);
158*e3723e1fSApple OSS Distributions 			ret = kpep_config_remove_event(config, 0);
159*e3723e1fSApple OSS Distributions 			_assert_kpep_ok(ret, "removing event %s from configuration", name);
160*e3723e1fSApple OSS Distributions 			continue;
161*e3723e1fSApple OSS Distributions 		}
162*e3723e1fSApple OSS Distributions 		if (kpc_count > kpc_count_allocated) {
163*e3723e1fSApple OSS Distributions 			free(kpc_configs);
164*e3723e1fSApple OSS Distributions 			kpc_configs = calloc(kpc_count, sizeof(kpc_config_t));
165*e3723e1fSApple OSS Distributions 			T_QUIET; T_WITH_ERRNO;
166*e3723e1fSApple OSS Distributions 			T_ASSERT_NOTNULL(kpc_configs, "allocate space for KPC configs");
167*e3723e1fSApple OSS Distributions 
168*e3723e1fSApple OSS Distributions 			free(kpc_map);
169*e3723e1fSApple OSS Distributions 			kpc_map = calloc(kpc_count, sizeof(size_t));
170*e3723e1fSApple OSS Distributions 			T_QUIET; T_WITH_ERRNO;
171*e3723e1fSApple OSS Distributions 			T_ASSERT_NOTNULL(kpc_map, "allocate space for KPC map");
172*e3723e1fSApple OSS Distributions 
173*e3723e1fSApple OSS Distributions 			kpc_count_allocated = kpc_count;
174*e3723e1fSApple OSS Distributions 		}
175*e3723e1fSApple OSS Distributions 
176*e3723e1fSApple OSS Distributions 		ret = kpep_config_kpc_map(config, kpc_map, kpc_count * sizeof(size_t));
177*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting KPC map for event %s", name);
178*e3723e1fSApple OSS Distributions 		ret = kpep_config_kpc(config, (uint8_t *)kpc_configs, kpc_count * sizeof(kpc_config_t));
179*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting KPC configs for event %s", name);
180*e3723e1fSApple OSS Distributions 		kpc_config_t kpc_config = kpc_configs[kpc_map[0]];
181*e3723e1fSApple OSS Distributions 
182*e3723e1fSApple OSS Distributions 		ret = kpep_config_apply(config);
183*e3723e1fSApple OSS Distributions 		bool not_permitted = ret == KPEP_ERR_ERRNO && errno == EPERM;
184*e3723e1fSApple OSS Distributions 		if (not_permitted) {
185*e3723e1fSApple OSS Distributions 			if (!internal_only) {
186*e3723e1fSApple OSS Distributions 				T_LOG("failed to configure public event %s (0x%04llx)", name, kpc_config);
187*e3723e1fSApple OSS Distributions 			}
188*e3723e1fSApple OSS Distributions 			filtered++;
189*e3723e1fSApple OSS Distributions 		} else if (internal_only) {
190*e3723e1fSApple OSS Distributions 			T_FAIL("configured internal-only event %s (0x%04llx) with secure CPC", name, kpc_config);
191*e3723e1fSApple OSS Distributions 		} else {
192*e3723e1fSApple OSS Distributions 			public_tested++;
193*e3723e1fSApple OSS Distributions 		}
194*e3723e1fSApple OSS Distributions 		ret = kpep_config_remove_event(config, 0);
195*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "removing event %s from configuration", name);
196*e3723e1fSApple OSS Distributions 		tested++;
197*e3723e1fSApple OSS Distributions 	}
198*e3723e1fSApple OSS Distributions 
199*e3723e1fSApple OSS Distributions 	T_LOG("tested %u internal/public events", tested);
200*e3723e1fSApple OSS Distributions 	T_LOG("correctly permitted to configure %u public events", public_tested);
201*e3723e1fSApple OSS Distributions 	T_LOG("correctly not permitted to configure %u internal-only events",
202*e3723e1fSApple OSS Distributions 	    filtered);
203*e3723e1fSApple OSS Distributions 	kpep_config_free(config);
204*e3723e1fSApple OSS Distributions 	kpep_db_free(public_db);
205*e3723e1fSApple OSS Distributions 	kpep_db_free(internal_db);
206*e3723e1fSApple OSS Distributions 	free(kpc_configs);
207*e3723e1fSApple OSS Distributions 	free(kpc_map);
208*e3723e1fSApple OSS Distributions }
209*e3723e1fSApple OSS Distributions 
210*e3723e1fSApple OSS Distributions T_DECL(secure_cpmu_event_restrictions, "secured CPMU should be restricted to known events",
211*e3723e1fSApple OSS Distributions     _T_META_CPC_SECURE_ON_DEV, T_META_TAG_VM_NOT_ELIGIBLE)
212*e3723e1fSApple OSS Distributions {
213*e3723e1fSApple OSS Distributions 	check_secure_cpmu();
214*e3723e1fSApple OSS Distributions }
215*e3723e1fSApple OSS Distributions 
216*e3723e1fSApple OSS Distributions T_DECL(release_cpmu_event_restrictions, "release CPMU should be restricted to known events",
217*e3723e1fSApple OSS Distributions     XNU_T_META_REQUIRES_RELEASE_KERNEL, T_META_TAG_VM_NOT_ELIGIBLE)
218*e3723e1fSApple OSS Distributions {
219*e3723e1fSApple OSS Distributions 	check_secure_cpmu();
220*e3723e1fSApple OSS Distributions }
221*e3723e1fSApple OSS Distributions 
222*e3723e1fSApple OSS Distributions #define UNCORE_DEV_PATH "/dev/monotonic/uncore"
223*e3723e1fSApple OSS Distributions #define UPMU_REF_CYCLES 0x02
224*e3723e1fSApple OSS Distributions 
225*e3723e1fSApple OSS Distributions static void
check_secure_upmu(void)226*e3723e1fSApple OSS Distributions check_secure_upmu(void)
227*e3723e1fSApple OSS Distributions {
228*e3723e1fSApple OSS Distributions 	guardid_t guard;
229*e3723e1fSApple OSS Distributions 	int fd;
230*e3723e1fSApple OSS Distributions 
231*e3723e1fSApple OSS Distributions 	guard = 0xa5adcafe;
232*e3723e1fSApple OSS Distributions 
233*e3723e1fSApple OSS Distributions 	T_SETUPBEGIN;
234*e3723e1fSApple OSS Distributions 
235*e3723e1fSApple OSS Distributions 	fd = guarded_open_np(UNCORE_DEV_PATH, &guard,
236*e3723e1fSApple OSS Distributions 	    GUARD_CLOSE | GUARD_DUP | GUARD_WRITE, O_CLOEXEC | O_EXCL);
237*e3723e1fSApple OSS Distributions 	if (fd < 0 && errno == ENOENT) {
238*e3723e1fSApple OSS Distributions 		T_SKIP("uncore counters are unsupported");
239*e3723e1fSApple OSS Distributions 	}
240*e3723e1fSApple OSS Distributions 
241*e3723e1fSApple OSS Distributions 	union monotonic_ctl_add add_ctl = {
242*e3723e1fSApple OSS Distributions 		.in.config.event = UPMU_REF_CYCLES,
243*e3723e1fSApple OSS Distributions 		.in.config.allowed_ctr_mask = 0xffff,
244*e3723e1fSApple OSS Distributions 	};
245*e3723e1fSApple OSS Distributions 
246*e3723e1fSApple OSS Distributions 	T_SETUPEND;
247*e3723e1fSApple OSS Distributions 
248*e3723e1fSApple OSS Distributions 	int ret = ioctl(fd, MT_IOC_ADD, &add_ctl);
249*e3723e1fSApple OSS Distributions 	T_EXPECT_POSIX_FAILURE(ret, EPERM,
250*e3723e1fSApple OSS Distributions 	    "should not be allowed to count any events on UPMU");
251*e3723e1fSApple OSS Distributions }
252*e3723e1fSApple OSS Distributions 
253*e3723e1fSApple OSS Distributions T_DECL(secure_upmu_event_restrictions, "secured UPMU should be restricted to no events",
254*e3723e1fSApple OSS Distributions     _T_META_CPC_SECURE_ON_DEV, T_META_TAG_VM_NOT_ELIGIBLE)
255*e3723e1fSApple OSS Distributions {
256*e3723e1fSApple OSS Distributions 	check_secure_upmu();
257*e3723e1fSApple OSS Distributions }
258*e3723e1fSApple OSS Distributions 
259*e3723e1fSApple OSS Distributions T_DECL(release_upmu_event_restrictions, "release UPMU should be restricted to no events",
260*e3723e1fSApple OSS Distributions     XNU_T_META_REQUIRES_RELEASE_KERNEL, T_META_TAG_VM_NOT_ELIGIBLE)
261*e3723e1fSApple OSS Distributions {
262*e3723e1fSApple OSS Distributions 	check_secure_upmu();
263*e3723e1fSApple OSS Distributions }
264*e3723e1fSApple OSS Distributions 
265*e3723e1fSApple OSS Distributions // Check that events which are exposed publicly are allowed to be configured.
266*e3723e1fSApple OSS Distributions 
267*e3723e1fSApple OSS Distributions static void
check_event_coverage(kpep_db_flags_t flag,const char * kind)268*e3723e1fSApple OSS Distributions check_event_coverage(kpep_db_flags_t flag, const char *kind)
269*e3723e1fSApple OSS Distributions {
270*e3723e1fSApple OSS Distributions 	kpep_db_t db = NULL;
271*e3723e1fSApple OSS Distributions 	int ret = kpep_db_createx(NULL, flag, &db);
272*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "creating %s event database", kind);
273*e3723e1fSApple OSS Distributions 
274*e3723e1fSApple OSS Distributions 	size_t event_count = 0;
275*e3723e1fSApple OSS Distributions 	ret = kpep_db_events_count(db, &event_count);
276*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "getting %s event count", kind);
277*e3723e1fSApple OSS Distributions 
278*e3723e1fSApple OSS Distributions 	kpep_event_t *events = calloc(event_count, sizeof(events[0]));
279*e3723e1fSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO;
280*e3723e1fSApple OSS Distributions 	T_ASSERT_NOTNULL(events, "allocate space for events");
281*e3723e1fSApple OSS Distributions 
282*e3723e1fSApple OSS Distributions 	ret = kpep_db_events(db, events, event_count * sizeof(events[0]));
283*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "getting public events");
284*e3723e1fSApple OSS Distributions 
285*e3723e1fSApple OSS Distributions 	kpep_config_t config = NULL;
286*e3723e1fSApple OSS Distributions 	ret = kpep_config_create(db, &config);
287*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "creating event configuration");
288*e3723e1fSApple OSS Distributions 	ret = kpep_config_force_counters(config);
289*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "forcing counters with configuration");
290*e3723e1fSApple OSS Distributions 
291*e3723e1fSApple OSS Distributions 	size_t kpc_count_allocated = 0;
292*e3723e1fSApple OSS Distributions 	uint64_t *kpc_configs = NULL;
293*e3723e1fSApple OSS Distributions 	size_t *kpc_map = NULL;
294*e3723e1fSApple OSS Distributions 
295*e3723e1fSApple OSS Distributions 	unsigned int tested = 0;
296*e3723e1fSApple OSS Distributions 	for (size_t i = 0; i < event_count; i++) {
297*e3723e1fSApple OSS Distributions 		kpep_event_t event = events[i];
298*e3723e1fSApple OSS Distributions 		const char *name = NULL;
299*e3723e1fSApple OSS Distributions 		ret = kpep_event_name(event, &name);
300*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting event name");
301*e3723e1fSApple OSS Distributions 		if (strncmp(name, "FIXED", strlen("FIXED")) == 0) {
302*e3723e1fSApple OSS Distributions 			T_LOG("skipping non-configurable %s event", name);
303*e3723e1fSApple OSS Distributions 			continue;
304*e3723e1fSApple OSS Distributions 		}
305*e3723e1fSApple OSS Distributions 
306*e3723e1fSApple OSS Distributions 		ret = kpep_config_add_event(config, &event, 0, NULL);
307*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "adding event %s to configuration", name);
308*e3723e1fSApple OSS Distributions 
309*e3723e1fSApple OSS Distributions 		size_t kpc_count = 0;
310*e3723e1fSApple OSS Distributions 		ret = kpep_config_kpc_count(config, &kpc_count);
311*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting KPC count");
312*e3723e1fSApple OSS Distributions 		if (kpc_count > kpc_count_allocated) {
313*e3723e1fSApple OSS Distributions 			free(kpc_configs);
314*e3723e1fSApple OSS Distributions 			kpc_configs = calloc(kpc_count, sizeof(kpc_config_t));
315*e3723e1fSApple OSS Distributions 			T_QUIET; T_WITH_ERRNO;
316*e3723e1fSApple OSS Distributions 			T_ASSERT_NOTNULL(kpc_configs, "allocate space for KPC configs");
317*e3723e1fSApple OSS Distributions 			kpc_count_allocated = kpc_count;
318*e3723e1fSApple OSS Distributions 
319*e3723e1fSApple OSS Distributions 			free(kpc_map);
320*e3723e1fSApple OSS Distributions 			kpc_map = calloc(kpc_count, sizeof(size_t));
321*e3723e1fSApple OSS Distributions 			T_QUIET; T_WITH_ERRNO;
322*e3723e1fSApple OSS Distributions 			T_ASSERT_NOTNULL(kpc_map, "allocate space for KPC map");
323*e3723e1fSApple OSS Distributions 		}
324*e3723e1fSApple OSS Distributions 
325*e3723e1fSApple OSS Distributions 		ret = kpep_config_kpc_map(config, kpc_map, kpc_count * sizeof(size_t));
326*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting KPC map for event %s", name);
327*e3723e1fSApple OSS Distributions 		ret = kpep_config_kpc(config, (uint8_t *)kpc_configs, kpc_count * sizeof(kpc_config_t));
328*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting KPC configs for event %s", name);
329*e3723e1fSApple OSS Distributions 		kpc_config_t kpc_config = kpc_configs[kpc_map[0]];
330*e3723e1fSApple OSS Distributions 
331*e3723e1fSApple OSS Distributions 		ret = kpep_config_apply(config);
332*e3723e1fSApple OSS Distributions 		if (ret == KPEP_ERR_ERRNO && errno == EPERM) {
333*e3723e1fSApple OSS Distributions 			T_FAIL("failed to configure %s event %s (0x%04llx) with secure CPC", kind, name, kpc_config);
334*e3723e1fSApple OSS Distributions 		} else {
335*e3723e1fSApple OSS Distributions 			_assert_kpep_ok(ret, "applying configuration with event %s (0x%04llx)", name, kpc_config);
336*e3723e1fSApple OSS Distributions 		}
337*e3723e1fSApple OSS Distributions 		ret = kpep_config_remove_event(config, 0);
338*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "removing event %s from configuration", name);
339*e3723e1fSApple OSS Distributions 		tested++;
340*e3723e1fSApple OSS Distributions 	}
341*e3723e1fSApple OSS Distributions 
342*e3723e1fSApple OSS Distributions 	T_LOG("attempted to configure %u %s events", tested, kind);
343*e3723e1fSApple OSS Distributions 	kpep_config_free(config);
344*e3723e1fSApple OSS Distributions 	kpep_db_free(db);
345*e3723e1fSApple OSS Distributions 	free(kpc_configs);
346*e3723e1fSApple OSS Distributions 	free(kpc_map);
347*e3723e1fSApple OSS Distributions }
348*e3723e1fSApple OSS Distributions 
349*e3723e1fSApple OSS Distributions T_DECL(secure_public_event_coverage, "all public events in kpep should be allowed",
350*e3723e1fSApple OSS Distributions     _T_META_CPC_SECURE_ON_DEV, T_META_TAG_VM_NOT_ELIGIBLE)
351*e3723e1fSApple OSS Distributions {
352*e3723e1fSApple OSS Distributions 	check_event_coverage(KPEP_DB_FLAG_PUBLIC_ONLY, "public");
353*e3723e1fSApple OSS Distributions }
354*e3723e1fSApple OSS Distributions 
355*e3723e1fSApple OSS Distributions T_DECL(release_public_event_coverage, "all public events in kpep should be allowed",
356*e3723e1fSApple OSS Distributions     XNU_T_META_REQUIRES_RELEASE_KERNEL, T_META_TAG_VM_NOT_ELIGIBLE)
357*e3723e1fSApple OSS Distributions {
358*e3723e1fSApple OSS Distributions 	check_event_coverage(KPEP_DB_FLAG_PUBLIC_ONLY, "public");
359*e3723e1fSApple OSS Distributions }
360*e3723e1fSApple OSS Distributions 
361*e3723e1fSApple OSS Distributions // Check for internal development behaviors.
362*e3723e1fSApple OSS Distributions T_DECL(insecure_cpmu_unrestricted, "insecure CPMU should be unrestricted",
363*e3723e1fSApple OSS Distributions     XNU_T_META_REQUIRES_DEVELOPMENT_KERNEL, T_META_SYSCTL_INT("kern.cpc.secure=0"), T_META_TAG_VM_NOT_ELIGIBLE)
364*e3723e1fSApple OSS Distributions {
365*e3723e1fSApple OSS Distributions 	check_event_coverage(KPEP_DB_FLAG_INTERNAL_ONLY, "internal");
366*e3723e1fSApple OSS Distributions }
367*e3723e1fSApple OSS Distributions 
368*e3723e1fSApple OSS Distributions T_DECL(secure_kpc_counting_system, "kpc should not allow counting the kernel when secure",
369*e3723e1fSApple OSS Distributions     _T_META_CPC_SECURE_ON_DEV)
370*e3723e1fSApple OSS Distributions {
371*e3723e1fSApple OSS Distributions 	kpep_db_t db = NULL;
372*e3723e1fSApple OSS Distributions 	int ret = kpep_db_createx(NULL, KPEP_DB_FLAG_PUBLIC_ONLY, &db);
373*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "creating public event database");
374*e3723e1fSApple OSS Distributions 
375*e3723e1fSApple OSS Distributions 	size_t event_count = 0;
376*e3723e1fSApple OSS Distributions 	ret = kpep_db_events_count(db, &event_count);
377*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "getting public event count");
378*e3723e1fSApple OSS Distributions 
379*e3723e1fSApple OSS Distributions 	kpep_event_t *events = calloc(event_count, sizeof(events[0]));
380*e3723e1fSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO;
381*e3723e1fSApple OSS Distributions 	T_ASSERT_NOTNULL(events, "allocate space for events");
382*e3723e1fSApple OSS Distributions 
383*e3723e1fSApple OSS Distributions 	ret = kpep_db_events(db, events, event_count * sizeof(events[0]));
384*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "getting public events");
385*e3723e1fSApple OSS Distributions 
386*e3723e1fSApple OSS Distributions 	kpep_config_t config = NULL;
387*e3723e1fSApple OSS Distributions 	ret = kpep_config_create(db, &config);
388*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "creating event configuration");
389*e3723e1fSApple OSS Distributions 	ret = kpep_config_force_counters(config);
390*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "forcing counters with configuration");
391*e3723e1fSApple OSS Distributions 
392*e3723e1fSApple OSS Distributions 
393*e3723e1fSApple OSS Distributions 	kpep_event_t event = NULL;
394*e3723e1fSApple OSS Distributions 	const char *name = NULL;
395*e3723e1fSApple OSS Distributions 	for (size_t i = 0; i < event_count; i++) {
396*e3723e1fSApple OSS Distributions 		event = events[i];
397*e3723e1fSApple OSS Distributions 		ret = kpep_event_name(event, &name);
398*e3723e1fSApple OSS Distributions 		_assert_kpep_ok(ret, "getting event name");
399*e3723e1fSApple OSS Distributions 		if (strncmp(name, "FIXED", strlen("FIXED")) != 0) {
400*e3723e1fSApple OSS Distributions 			break;
401*e3723e1fSApple OSS Distributions 		}
402*e3723e1fSApple OSS Distributions 		T_LOG("skipping non-configurable %s event", name);
403*e3723e1fSApple OSS Distributions 	}
404*e3723e1fSApple OSS Distributions 
405*e3723e1fSApple OSS Distributions 	ret = kpep_config_add_event(config, &event, KPEP_EVENT_FLAG_KERNEL, NULL);
406*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "adding event %s to configuration", name);
407*e3723e1fSApple OSS Distributions 
408*e3723e1fSApple OSS Distributions 	ret = kpep_config_apply(config);
409*e3723e1fSApple OSS Distributions 	_assert_kpep_ok(ret, "applying configuration with event %s", name);
410*e3723e1fSApple OSS Distributions 
411*e3723e1fSApple OSS Distributions 	uint32_t config_count = kpc_get_config_count(KPC_CLASS_CONFIGURABLE_MASK);
412*e3723e1fSApple OSS Distributions 	uint64_t *configs = calloc(config_count, sizeof(configs[0]));
413*e3723e1fSApple OSS Distributions 	T_QUIET;
414*e3723e1fSApple OSS Distributions 	T_ASSERT_NOTNULL(configs, "allocated %u * %zu", config_count, sizeof(configs[0]));
415*e3723e1fSApple OSS Distributions 	ret = kpc_get_config(KPC_CLASS_CONFIGURABLE_MASK, configs);
416*e3723e1fSApple OSS Distributions 	for (uint32_t i = 0; i < config_count; i++) {
417*e3723e1fSApple OSS Distributions 		if ((configs[i] & 0x40000)) {
418*e3723e1fSApple OSS Distributions 			T_FAIL("found configurable counter %u with configuration 0x%llx", i, configs[i]);
419*e3723e1fSApple OSS Distributions 		}
420*e3723e1fSApple OSS Distributions 	}
421*e3723e1fSApple OSS Distributions 	T_LOG("checked %d events for EL2 counting", config_count);
422*e3723e1fSApple OSS Distributions 
423*e3723e1fSApple OSS Distributions 	kpep_config_free(config);
424*e3723e1fSApple OSS Distributions 	kpep_db_free(db);
425*e3723e1fSApple OSS Distributions }
426