xref: /xnu-8792.61.2/libsyscall/wrappers/mach_bridge_remote_time.c (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1*42e22086SApple OSS Distributions /*
2*42e22086SApple OSS Distributions  * Copyright (c) 2018 Apple Inc. All rights reserved.
3*42e22086SApple OSS Distributions  *
4*42e22086SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*42e22086SApple OSS Distributions  *
6*42e22086SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*42e22086SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*42e22086SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*42e22086SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*42e22086SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*42e22086SApple OSS Distributions  * file.
12*42e22086SApple OSS Distributions  *
13*42e22086SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*42e22086SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*42e22086SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*42e22086SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*42e22086SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*42e22086SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*42e22086SApple OSS Distributions  * limitations under the License.
20*42e22086SApple OSS Distributions  *
21*42e22086SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*42e22086SApple OSS Distributions  */
23*42e22086SApple OSS Distributions #include <sys/types.h>
24*42e22086SApple OSS Distributions #include <machine/cpu_capabilities.h>
25*42e22086SApple OSS Distributions #include <kern/remote_time.h>
26*42e22086SApple OSS Distributions #include <mach/mach_time.h>
27*42e22086SApple OSS Distributions #include "strings.h"
28*42e22086SApple OSS Distributions #include <TargetConditionals.h>
29*42e22086SApple OSS Distributions 
30*42e22086SApple OSS Distributions #define BT_RESET_SENTINEL_TS  (~3ULL) /* from machine/machine_remote_time.h */
31*42e22086SApple OSS Distributions 
32*42e22086SApple OSS Distributions extern uint64_t __mach_bridge_remote_time(uint64_t local_time);
33*42e22086SApple OSS Distributions 
34*42e22086SApple OSS Distributions #if TARGET_OS_BRIDGE && defined(__arm64__)
35*42e22086SApple OSS Distributions static uint64_t
absolutetime_to_nanoseconds(uint64_t abs_time)36*42e22086SApple OSS Distributions absolutetime_to_nanoseconds(uint64_t abs_time)
37*42e22086SApple OSS Distributions {
38*42e22086SApple OSS Distributions 	mach_timebase_info_data_t info;
39*42e22086SApple OSS Distributions 	mach_timebase_info(&info);
40*42e22086SApple OSS Distributions 	uint64_t time_in_ns = (uint64_t)(((double)info.numer / (double)info.denom) * abs_time);
41*42e22086SApple OSS Distributions 
42*42e22086SApple OSS Distributions 	return time_in_ns;
43*42e22086SApple OSS Distributions }
44*42e22086SApple OSS Distributions 
45*42e22086SApple OSS Distributions uint64_t
mach_bridge_remote_time(__unused uint64_t local_time)46*42e22086SApple OSS Distributions mach_bridge_remote_time(__unused uint64_t local_time)
47*42e22086SApple OSS Distributions {
48*42e22086SApple OSS Distributions 	uint64_t remote_time = 0;
49*42e22086SApple OSS Distributions 	uint64_t local_time_ns = 0;
50*42e22086SApple OSS Distributions 	uint64_t now = 0;
51*42e22086SApple OSS Distributions 	struct bt_params params = {};
52*42e22086SApple OSS Distributions 
53*42e22086SApple OSS Distributions 	COMM_PAGE_SLOT_TYPE(struct bt_params) commpage_bt_params_p =
54*42e22086SApple OSS Distributions 	    COMM_PAGE_SLOT(struct bt_params, REMOTETIME_PARAMS);
55*42e22086SApple OSS Distributions 	volatile uint64_t *base_local_ts_p = &commpage_bt_params_p->base_local_ts;
56*42e22086SApple OSS Distributions 	volatile uint64_t *base_remote_ts_p = &commpage_bt_params_p->base_remote_ts;
57*42e22086SApple OSS Distributions 	volatile double *rate_p = &commpage_bt_params_p->rate;
58*42e22086SApple OSS Distributions 
59*42e22086SApple OSS Distributions 	do {
60*42e22086SApple OSS Distributions 		params.base_local_ts = *base_local_ts_p;
61*42e22086SApple OSS Distributions 		if (*base_local_ts_p == BT_RESET_SENTINEL_TS) {
62*42e22086SApple OSS Distributions 			return 0;
63*42e22086SApple OSS Distributions 		}
64*42e22086SApple OSS Distributions 		/*
65*42e22086SApple OSS Distributions 		 * This call contains an instruction barrier that ensures the second read of
66*42e22086SApple OSS Distributions 		 * base_local_ts is not speculated above the first read of base_local_ts.
67*42e22086SApple OSS Distributions 		 */
68*42e22086SApple OSS Distributions 		now = mach_absolute_time();
69*42e22086SApple OSS Distributions 		params.base_remote_ts = *base_remote_ts_p;
70*42e22086SApple OSS Distributions 		params.rate = *rate_p;
71*42e22086SApple OSS Distributions 		/*
72*42e22086SApple OSS Distributions 		 * This barrier prevents the second read of base_local_ts from being reordered
73*42e22086SApple OSS Distributions 		 * w.r.t the reads of other values in bt_params.
74*42e22086SApple OSS Distributions 		 */
75*42e22086SApple OSS Distributions 		__asm__ volatile ("dmb ishld" ::: "memory");
76*42e22086SApple OSS Distributions 	} while (params.base_local_ts && (params.base_local_ts != commpage_bt_params_p->base_local_ts));
77*42e22086SApple OSS Distributions 
78*42e22086SApple OSS Distributions 	if (!local_time) {
79*42e22086SApple OSS Distributions 		local_time = now;
80*42e22086SApple OSS Distributions 	}
81*42e22086SApple OSS Distributions 	local_time_ns = absolutetime_to_nanoseconds(local_time);
82*42e22086SApple OSS Distributions 	if (local_time_ns < params.base_local_ts) {
83*42e22086SApple OSS Distributions 		remote_time = __mach_bridge_remote_time(local_time);
84*42e22086SApple OSS Distributions 	} else {
85*42e22086SApple OSS Distributions 		remote_time = mach_bridge_compute_timestamp(local_time_ns, &params);
86*42e22086SApple OSS Distributions 	}
87*42e22086SApple OSS Distributions 	return remote_time;
88*42e22086SApple OSS Distributions }
89*42e22086SApple OSS Distributions #endif /* TARGET_OS_BRIDGE && defined(__arm64__) */
90