xref: /xnu-11417.121.6/osfmk/kern/exclaves_sensor.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1 /*
2  * Copyright (c) 2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <stdint.h>
30 #include <mach/exclaves.h>
31 #include <mach/kern_return.h>
32 
33 #include "exclaves_boot.h"
34 #include "exclaves_debug.h"
35 #include "exclaves_resource.h"
36 #include "exclaves_sensor.h"
37 
38 #if CONFIG_EXCLAVES
39 
40 #include <kern/locks.h>
41 #include <kern/thread_call.h>
42 
43 #include "kern/exclaves.tightbeam.h"
44 
45 /* -------------------------------------------------------------------------- */
46 #pragma mark EIC
47 
48 #define EXCLAVES_EIC "com.apple.service.ExclaveIndicatorController"
49 
50 /* The minimum time a sensor is on. */
51 #define EXCLAVES_EIC_MIN_SENSOR_TIME (3100 * NSEC_PER_MSEC) /* 3.1 seconds */
52 
53 /* Default to 30Hz */
54 static uint64_t exclaves_display_healthcheck_rate_hz = 30;
55 
56 static exclaveindicatorcontroller_sensorrequest_s eic_client;
57 
58 static inline __unused exclaveindicatorcontroller_sensortype_s
sensor_type_to_eic_sensortype(exclaves_sensor_type_t type)59 sensor_type_to_eic_sensortype(exclaves_sensor_type_t type)
60 {
61 	assert3u(type, >, 0);
62 	assert3u(type, <=, EXCLAVES_SENSOR_MAX);
63 
64 	switch (type) {
65 	case EXCLAVES_SENSOR_CAM:
66 		return EXCLAVEINDICATORCONTROLLER_SENSORTYPE_SENSOR_CAM;
67 	case EXCLAVES_SENSOR_MIC:
68 		return EXCLAVEINDICATORCONTROLLER_SENSORTYPE_SENSOR_MIC;
69 	case EXCLAVES_SENSOR_CAM_ALT_FACEID:
70 		return EXCLAVEINDICATORCONTROLLER_SENSORTYPE_SENSOR_CAM_ALT_FACEID;
71 	case EXCLAVES_SENSOR_CAM_ALT_FACEID_DELAYED:
72 		return EXCLAVEINDICATORCONTROLLER_SENSORTYPE_SENSOR_CAM_ALT_FACEID_DELAYED;
73 	default:
74 		panic("unknown sensor type");
75 	}
76 }
77 
78 static inline exclaves_sensor_status_t
eic_sensorstatus_to_sensor_status(exclaveindicatorcontroller_sensorstatusresponse_s status)79 eic_sensorstatus_to_sensor_status(exclaveindicatorcontroller_sensorstatusresponse_s status)
80 {
81 	assert3u(status, >, 0);
82 	assert3u(status, <=, EXCLAVEINDICATORCONTROLLER_SENSORSTATUSRESPONSE_SENSOR_PENDING);
83 
84 	switch (status) {
85 	case EXCLAVEINDICATORCONTROLLER_SENSORSTATUSRESPONSE_SENSOR_ALLOWED:
86 		return EXCLAVES_SENSOR_STATUS_ALLOWED;
87 	case EXCLAVEINDICATORCONTROLLER_SENSORSTATUSRESPONSE_SENSOR_DENIED:
88 		return EXCLAVES_SENSOR_STATUS_DENIED;
89 	case EXCLAVEINDICATORCONTROLLER_SENSORSTATUSRESPONSE_SENSOR_CONTROL:
90 		return EXCLAVES_SENSOR_STATUS_CONTROL;
91 	case EXCLAVEINDICATORCONTROLLER_SENSORSTATUSRESPONSE_SENSOR_PENDING:
92 		return EXCLAVES_SENSOR_STATUS_PENDING;
93 	default:
94 		panic("unknown sensor status");
95 	}
96 }
97 
98 static kern_return_t
exclaves_eic_init(void)99 exclaves_eic_init(void)
100 {
101 	exclaves_id_t eic_id = exclaves_service_lookup(EXCLAVES_DOMAIN_KERNEL,
102 	    EXCLAVES_EIC);
103 
104 	if (eic_id == EXCLAVES_INVALID_ID) {
105 		exclaves_requirement_assert(EXCLAVES_R_EIC,
106 		    "exclaves indicator controller not found");
107 		return KERN_SUCCESS;
108 	}
109 
110 	tb_endpoint_t ep = tb_endpoint_create_with_value(
111 		TB_TRANSPORT_TYPE_XNU, eic_id, TB_ENDPOINT_OPTIONS_NONE);
112 
113 	tb_error_t ret =
114 	    exclaveindicatorcontroller_sensorrequest__init(&eic_client, ep);
115 
116 	return ret == TB_ERROR_SUCCESS ? KERN_SUCCESS : KERN_FAILURE;
117 }
118 
119 static kern_return_t
exclaves_eic_tick_rate(uint64_t rate_hz)120 exclaves_eic_tick_rate(uint64_t rate_hz)
121 {
122 	exclaveindicatorcontroller_indicatorrefreshrate_s rate;
123 
124 	/* Round up to nearest supported value. */
125 	switch (rate_hz) {
126 	case 0 ... 30:
127 		exclaves_display_healthcheck_rate_hz = 30;
128 		rate.tag = EXCLAVEINDICATORCONTROLLER_INDICATORREFRESHRATE__HZ_30;
129 		break;
130 	case 31 ... 60:
131 		exclaves_display_healthcheck_rate_hz = 60;
132 		rate.tag = EXCLAVEINDICATORCONTROLLER_INDICATORREFRESHRATE__HZ_60;
133 		break;
134 	default:
135 		exclaves_display_healthcheck_rate_hz = 120;
136 		rate.tag = EXCLAVEINDICATORCONTROLLER_INDICATORREFRESHRATE__HZ_120;
137 		break;
138 	}
139 
140 	tb_error_t ret = exclaveindicatorcontroller_sensorrequest_setindicatorrefreshrate(
141 		&eic_client, &rate, ^(__unused exclaveindicatorcontroller_requesterror_s result) {});
142 
143 	return ret == TB_ERROR_SUCCESS ? KERN_SUCCESS : KERN_FAILURE;
144 }
145 
146 static kern_return_t
exclaves_eic_sensor_start(exclaves_sensor_type_t __unused sensor_type,__assert_only uint64_t flags,exclaves_sensor_status_t * status)147 exclaves_eic_sensor_start(exclaves_sensor_type_t __unused sensor_type,
148     __assert_only uint64_t flags, exclaves_sensor_status_t *status)
149 {
150 	assert3p(status, !=, NULL);
151 	assert3u(flags, ==, 0);
152 
153 	*status = EXCLAVES_SENSOR_STATUS_ALLOWED;
154 	return KERN_SUCCESS;
155 }
156 
157 static kern_return_t
exclaves_eic_sensor_stop(exclaves_sensor_type_t __unused sensor_type)158 exclaves_eic_sensor_stop(exclaves_sensor_type_t __unused sensor_type)
159 {
160 	return KERN_SUCCESS;
161 }
162 
163 static kern_return_t
exclaves_eic_sensor_status(exclaves_sensor_type_t __unused sensor_type,__assert_only uint64_t flags,exclaves_sensor_status_t * status)164 exclaves_eic_sensor_status(exclaves_sensor_type_t __unused sensor_type,
165     __assert_only uint64_t flags, exclaves_sensor_status_t *status)
166 {
167 	assert3p(status, !=, NULL);
168 	assert3u(flags, ==, 0);
169 
170 	*status = EXCLAVES_SENSOR_STATUS_ALLOWED;
171 	return KERN_SUCCESS;
172 }
173 
174 /*
175  * It is intentional to keep "buffer" untyped here as it avoids xnu having to
176  * understand what those IDs are at all. They are simply passed through from the
177  * resource table as-is.
178  */
179 static kern_return_t
exclaves_eic_sensor_copy(uint32_t buffer,uint64_t size1,uint64_t offset1,uint64_t size2,uint64_t offset2,exclaves_sensor_status_t * status)180 exclaves_eic_sensor_copy(uint32_t buffer, uint64_t size1, uint64_t offset1,
181     uint64_t size2, uint64_t offset2, exclaves_sensor_status_t *status)
182 {
183 	assert3u(size1, >, 0);
184 	assert3p(status, !=, NULL);
185 
186 	tb_error_t ret = exclaveindicatorcontroller_sensorrequest_copybuffer(
187 		&eic_client, buffer, offset1, size1, offset2, size2,
188 		^(exclaveindicatorcontroller_sensorstatusresponse_s result) {
189 		*status = eic_sensorstatus_to_sensor_status(result);
190 	});
191 
192 	return ret == TB_ERROR_SUCCESS ? KERN_SUCCESS : KERN_FAILURE;
193 }
194 
195 static bool
exclaves_sensor_tick(void)196 exclaves_sensor_tick(void)
197 {
198 	__block bool again = true;
199 	__unused tb_error_t ret = exclaveindicatorcontroller_sensorrequest_tick(
200 		&eic_client, ^(bool result) {
201 		again = result;
202 	});
203 	assert3u(ret, ==, TB_ERROR_SUCCESS);
204 
205 	return again;
206 }
207 
208 /* -------------------------------------------------------------------------- */
209 #pragma mark sensor
210 
211 static LCK_GRP_DECLARE(sensor_lck_grp, "exclaves_sensor");
212 
213 typedef struct {
214 	/*
215 	 * Count of how many times sensor_start has been called on this sensor
216 	 * without a corresponding sensor_stop.
217 	 */
218 	uint64_t s_startcount;
219 
220 	/* Last start time. */
221 	uint64_t s_start_abs;
222 
223 	/* Last stop time. */
224 	uint64_t s_stop_abs;
225 
226 	/* mutex to protect updates to the above */
227 	lck_mtx_t s_mutex;
228 
229 	/* Keep track of whether this sensor was initialised or not. */
230 	bool s_initialised;
231 } exclaves_sensor_t;
232 
233 /**
234  * A reverse lookup table for the sensor resources,
235  * as the kpi uses sensor ids directly to access the same resources */
236 static exclaves_sensor_t sensors[EXCLAVES_SENSOR_MAX];
237 
238 /*
239  * A thread call used to periodically call "status" on any open sensors.
240  */
241 static thread_call_t sensor_healthcheck_tcall = NULL;
242 
243 static inline bool
valid_sensor(exclaves_sensor_type_t sensor_type)244 valid_sensor(exclaves_sensor_type_t sensor_type)
245 {
246 	switch (sensor_type) {
247 	case EXCLAVES_SENSOR_CAM:
248 	case EXCLAVES_SENSOR_MIC:
249 	case EXCLAVES_SENSOR_CAM_ALT_FACEID:
250 	case EXCLAVES_SENSOR_CAM_ALT_FACEID_DELAYED:
251 		return true;
252 	default:
253 		return false;
254 	}
255 }
256 
257 static inline exclaves_sensor_t *
sensor_type_to_sensor(exclaves_sensor_type_t sensor_type)258 sensor_type_to_sensor(exclaves_sensor_type_t sensor_type)
259 {
260 	assert(valid_sensor(sensor_type));
261 	return &sensors[sensor_type - 1];
262 }
263 
264 /* Calculate the next healthcheck time. */
265 static void
healthcheck_deadline(uint64_t * deadline,uint64_t * leeway)266 healthcheck_deadline(uint64_t *deadline, uint64_t *leeway)
267 {
268 	const uint32_t interval =
269 	    NSEC_PER_SEC / exclaves_display_healthcheck_rate_hz;
270 	clock_interval_to_deadline(interval, 1, deadline);
271 	nanoseconds_to_absolutetime(interval / 2, leeway);
272 }
273 
274 /*
275  * Called from the threadcall to call into exclaves with a status command for
276  * every started sensor. Re-arms itself so it runs at a frequency set by the
277  * display healthcheck rate. Exits when there are no longer any started sensors.
278  * A sensor has a minimum on-time. For stopped sensors, call back into exclaves
279  * until this minimum time has been reached.
280  */
281 static void
exclaves_sensor_healthcheck(__unused void * param0,__unused void * param1)282 exclaves_sensor_healthcheck(__unused void *param0, __unused void *param1)
283 {
284 	uint64_t hc_leeway, hc_deadline;
285 
286 	/*
287 	 * Calculate the next deadline up-front so the overhead of calling into
288 	 * exclaves doesn't add to the period.
289 	 */
290 	healthcheck_deadline(&hc_deadline, &hc_leeway);
291 
292 	if (exclaves_sensor_tick()) {
293 		thread_call_enter_delayed_with_leeway(sensor_healthcheck_tcall,
294 		    NULL, hc_deadline, hc_leeway, THREAD_CALL_DELAY_LEEWAY);
295 	}
296 }
297 
298 static kern_return_t
exclaves_sensor_init(void)299 exclaves_sensor_init(void)
300 {
301 	kern_return_t kr = exclaves_eic_init();
302 	if (kr != KERN_SUCCESS) {
303 		return kr;
304 	}
305 
306 	for (uint32_t i = 1; i <= EXCLAVES_SENSOR_MAX; i++) {
307 		exclaves_sensor_t *sensor = sensor_type_to_sensor(i);
308 
309 		lck_mtx_init(&sensor->s_mutex, &sensor_lck_grp, NULL);
310 
311 		sensor->s_startcount = 0;
312 		sensor->s_initialised = true;
313 	}
314 
315 	sensor_healthcheck_tcall =
316 	    thread_call_allocate_with_priority(exclaves_sensor_healthcheck,
317 	    NULL, THREAD_CALL_PRIORITY_KERNEL);
318 
319 	return KERN_SUCCESS;
320 }
321 EXCLAVES_BOOT_TASK(exclaves_sensor_init, EXCLAVES_BOOT_RANK_ANY);
322 
323 kern_return_t
exclaves_sensor_start(exclaves_sensor_type_t sensor_type,uint64_t flags,exclaves_sensor_status_t * status)324 exclaves_sensor_start(exclaves_sensor_type_t sensor_type, uint64_t flags,
325     exclaves_sensor_status_t *status)
326 {
327 	if (!valid_sensor(sensor_type)) {
328 		return KERN_INVALID_ARGUMENT;
329 	}
330 
331 	exclaves_sensor_t *sensor = sensor_type_to_sensor(sensor_type);
332 	if (!sensor->s_initialised) {
333 		return KERN_FAILURE;
334 	}
335 
336 	lck_mtx_lock(&sensor->s_mutex);
337 	kern_return_t kr;
338 
339 	if (sensor->s_startcount == UINT64_MAX) {
340 		lck_mtx_unlock(&sensor->s_mutex);
341 		return KERN_INVALID_ARGUMENT;
342 	}
343 
344 	if (sensor->s_startcount > 0) {
345 		kr = exclaves_eic_sensor_status(sensor_type, flags, status);
346 		if (kr == KERN_SUCCESS) {
347 			sensor->s_startcount += 1;
348 		}
349 		lck_mtx_unlock(&sensor->s_mutex);
350 		return kr;
351 	}
352 
353 	// call start iff startcount is 0
354 	kr = exclaves_eic_sensor_start(sensor_type, flags, status);
355 	if (kr != KERN_SUCCESS) {
356 		lck_mtx_unlock(&sensor->s_mutex);
357 		return kr;
358 	}
359 
360 	sensor->s_start_abs = mach_absolute_time();
361 	sensor->s_startcount += 1;
362 
363 	lck_mtx_unlock(&sensor->s_mutex);
364 
365 	/* Kick off the periodic status check. */
366 	(void)thread_call_enter(sensor_healthcheck_tcall);
367 
368 	return KERN_SUCCESS;
369 }
370 
371 kern_return_t
exclaves_sensor_stop(exclaves_sensor_type_t sensor_type,uint64_t flags,exclaves_sensor_status_t * status)372 exclaves_sensor_stop(exclaves_sensor_type_t sensor_type, uint64_t flags,
373     exclaves_sensor_status_t *status)
374 {
375 	if (!valid_sensor(sensor_type)) {
376 		return KERN_INVALID_ARGUMENT;
377 	}
378 
379 	exclaves_sensor_t *sensor = sensor_type_to_sensor(sensor_type);
380 	if (!sensor->s_initialised) {
381 		return KERN_FAILURE;
382 	}
383 
384 	kern_return_t kr;
385 
386 	lck_mtx_lock(&sensor->s_mutex);
387 
388 	if (sensor->s_startcount == 0) {
389 		lck_mtx_unlock(&sensor->s_mutex);
390 		return KERN_INVALID_ARGUMENT;
391 	}
392 
393 	if (sensor->s_startcount > 1) {
394 		kr = exclaves_eic_sensor_status(sensor_type, flags, status);
395 		if (kr == KERN_SUCCESS) {
396 			sensor->s_startcount -= 1;
397 		}
398 		lck_mtx_unlock(&sensor->s_mutex);
399 		return kr;
400 	}
401 
402 	// call stop iff startcount is going to go to 0
403 	kr = exclaves_eic_sensor_stop(sensor_type);
404 	if (kr != KERN_SUCCESS) {
405 		lck_mtx_unlock(&sensor->s_mutex);
406 		return kr;
407 	}
408 
409 	sensor->s_stop_abs = mach_absolute_time();
410 	sensor->s_startcount = 0;
411 
412 	kr = exclaves_eic_sensor_status(sensor_type, flags, status);
413 
414 	lck_mtx_unlock(&sensor->s_mutex);
415 
416 	return kr;
417 }
418 
419 kern_return_t
exclaves_sensor_status(exclaves_sensor_type_t sensor_type,uint64_t flags,exclaves_sensor_status_t * status)420 exclaves_sensor_status(exclaves_sensor_type_t sensor_type, uint64_t flags,
421     exclaves_sensor_status_t *status)
422 {
423 	if (!valid_sensor(sensor_type)) {
424 		return KERN_INVALID_ARGUMENT;
425 	}
426 
427 	exclaves_sensor_t *sensor = sensor_type_to_sensor(sensor_type);
428 	if (!sensor->s_initialised) {
429 		return KERN_FAILURE;
430 	}
431 
432 	return exclaves_eic_sensor_status(sensor_type, flags, status);
433 }
434 
435 kern_return_t
exclaves_sensor_tick_rate(uint64_t rate_hz)436 exclaves_sensor_tick_rate(uint64_t rate_hz)
437 {
438 	/*
439 	 * Make sure that the initialisation has taken place before calling into
440 	 * the EIC. Any sensor is sufficient.
441 	 */
442 	exclaves_sensor_t *sensor = sensor_type_to_sensor(EXCLAVES_SENSOR_CAM);
443 	if (!sensor->s_initialised) {
444 		return KERN_FAILURE;
445 	}
446 
447 	return exclaves_eic_tick_rate(rate_hz);
448 }
449 
450 kern_return_t
exclaves_display_healthcheck_rate(uint64_t __unused ns)451 exclaves_display_healthcheck_rate(uint64_t __unused ns)
452 {
453 	/* Deprecated, no longer does anything */
454 	return KERN_SUCCESS;
455 }
456 
457 kern_return_t
exclaves_sensor_copy(uint32_t buffer,uint64_t size1,uint64_t offset1,uint64_t size2,uint64_t offset2,exclaves_sensor_status_t * status)458 exclaves_sensor_copy(uint32_t buffer, uint64_t size1, uint64_t offset1,
459     uint64_t size2, uint64_t offset2, exclaves_sensor_status_t *status)
460 {
461 	/*
462 	 * Make sure that the initialisation has taken place before calling into
463 	 * the EIC. Any sensor is sufficient.
464 	 */
465 	exclaves_sensor_t *sensor = sensor_type_to_sensor(EXCLAVES_SENSOR_CAM);
466 	if (!sensor->s_initialised) {
467 		return KERN_FAILURE;
468 	}
469 
470 
471 	return exclaves_eic_sensor_copy(buffer, size1, offset1, size2, offset2,
472 	           status);
473 }
474 
475 kern_return_t
exclaves_indicator_min_on_time_deadlines(struct exclaves_indicator_deadlines * deadlines)476 exclaves_indicator_min_on_time_deadlines(struct exclaves_indicator_deadlines *deadlines)
477 {
478 	assert(deadlines);
479 
480 	//For now, only one version is supported. Return an error if libsyscall sends us any other versions
481 	if (deadlines->version != 1) {
482 		return KERN_INVALID_ARGUMENT;
483 	}
484 
485 	// Make sure that the initialisation has taken place before calling into
486 	// the EIC. Any sensor is sufficient.
487 	exclaves_sensor_t *sensor = sensor_type_to_sensor(EXCLAVES_SENSOR_CAM);
488 	if (!sensor->s_initialised) {
489 		return KERN_FAILURE;
490 	}
491 
492 	tb_error_t ret = exclaveindicatorcontroller_sensorrequest_getmotstate(
493 		&eic_client, ^(exclaveindicatorcontroller_motstate_s result) {
494 		deadlines->camera_indicator = result.deadlinecil;
495 		deadlines->mic_indicator = result.deadlinemil;
496 		deadlines->faceid_indicator = result.deadlinefid;
497 	});
498 
499 	return ret == TB_ERROR_SUCCESS ? KERN_SUCCESS : KERN_FAILURE;
500 }
501 
502 
503 
504 #else /* CONFIG_EXCLAVES */
505 
506 kern_return_t
exclaves_display_healthcheck_rate(__unused uint64_t ns)507 exclaves_display_healthcheck_rate(__unused uint64_t ns)
508 {
509 	return KERN_NOT_SUPPORTED;
510 }
511 
512 kern_return_t
exclaves_sensor_tick_rate(uint64_t __unused rate_hz)513 exclaves_sensor_tick_rate(uint64_t __unused rate_hz)
514 {
515 	return KERN_NOT_SUPPORTED;
516 }
517 
518 #endif /* CONFIG_EXCLAVES */
519