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