2 * Copyright (c) 2007 Apple Inc. All rights reserved.
3 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. The rights granted to you under the License
11 * may not be used to create, or enable the creation or redistribution of,
12 * unlawful or unlicensed copies of an Apple operating system, or to
13 * circumvent, violate, or enable the circumvention or violation of, any
14 * terms of an Apple operating system software license agreement.
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this file.
19 * The Original Code and all software distributed under the License are
20 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24 * Please see the License for the specific language governing rights and
25 * limitations under the License.
27 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 * @APPLE_FREE_COPYRIGHT@
36 * File: arm/commpage/commpage.c
37 * Purpose: Set up and export a RO/RW page
39 #include <libkern/section_keywords.h>
40 #include <mach/mach_types.h>
41 #include <mach/machine.h>
42 #include <mach/vm_map.h>
43 #include <machine/cpu_capabilities.h>
44 #include <machine/commpage.h>
45 #include <machine/pmap.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_protos.h>
49 #include <ipc/ipc_port.h>
50 #include <arm/cpuid.h> /* for cpuid_info() & cache_info() */
51 #include <arm/rtclock.h>
52 #include <libkern/OSAtomic.h>
53 #include <stdatomic.h>
54 #include <kern/remote_time.h>
55 #include <machine/machine_remote_time.h>
57 #include <sys/kdebug.h>
60 #include <atm/atm_internal.h>
63 static void commpage_init_cpu_capabilities( void );
64 static int commpage_cpus( void );
66 SECURITY_READ_ONLY_LATE(vm_address_t
) commPagePtr
= 0;
67 SECURITY_READ_ONLY_LATE(vm_address_t
) sharedpage_rw_addr
= 0;
68 SECURITY_READ_ONLY_LATE(uint32_t) _cpu_capabilities
= 0;
70 /* For sysctl access from BSD side */
71 extern int gARMv81Atomics
;
72 extern int gARMv8Crc32
;
81 sharedpage_rw_addr
= pmap_create_sharedpage();
82 commPagePtr
= (vm_address_t
)_COMM_PAGE_BASE_ADDRESS
;
84 *((uint16_t*)(_COMM_PAGE_VERSION
+ _COMM_PAGE_RW_OFFSET
)) = (uint16_t) _COMM_PAGE_THIS_VERSION
;
86 commpage_init_cpu_capabilities();
87 commpage_set_timestamp(0, 0, 0, 0, 0);
89 if (_cpu_capabilities
& kCache32
) {
91 } else if (_cpu_capabilities
& kCache64
) {
93 } else if (_cpu_capabilities
& kCache128
) {
99 *((uint16_t*)(_COMM_PAGE_CACHE_LINESIZE
+ _COMM_PAGE_RW_OFFSET
)) = c2
;
100 *((uint32_t*)(_COMM_PAGE_SPIN_COUNT
+ _COMM_PAGE_RW_OFFSET
)) = 1;
102 commpage_update_active_cpus();
103 cpufamily
= cpuid_get_cpufamily();
105 /* machine_info valid after ml_get_max_cpus() */
106 *((uint8_t*)(_COMM_PAGE_PHYSICAL_CPUS
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) machine_info
.physical_cpu_max
;
107 *((uint8_t*)(_COMM_PAGE_LOGICAL_CPUS
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) machine_info
.logical_cpu_max
;
108 *((uint64_t*)(_COMM_PAGE_MEMORY_SIZE
+ _COMM_PAGE_RW_OFFSET
)) = machine_info
.max_mem
;
109 *((uint32_t*)(_COMM_PAGE_CPUFAMILY
+ _COMM_PAGE_RW_OFFSET
)) = (uint32_t)cpufamily
;
110 *((uint32_t*)(_COMM_PAGE_DEV_FIRM
+ _COMM_PAGE_RW_OFFSET
)) = (uint32_t)PE_i_can_has_debugger(NULL
);
111 *((uint8_t*)(_COMM_PAGE_USER_TIMEBASE
+ _COMM_PAGE_RW_OFFSET
)) = user_timebase_allowed();
112 *((uint8_t*)(_COMM_PAGE_CONT_HWCLOCK
+ _COMM_PAGE_RW_OFFSET
)) = user_cont_hwclock_allowed();
113 *((uint8_t*)(_COMM_PAGE_KERNEL_PAGE_SHIFT
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) page_shift
;
116 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_32
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) page_shift_user32
;
117 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_64
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) SIXTEENK_PAGE_SHIFT
;
118 #elif (__ARM_ARCH_7K__ >= 2) && defined(PLATFORM_WatchOS)
119 /* enforce 16KB alignment for watch targets with new ABI */
120 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_32
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) SIXTEENK_PAGE_SHIFT
;
121 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_64
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) SIXTEENK_PAGE_SHIFT
;
122 #else /* __arm64__ */
123 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_32
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) PAGE_SHIFT
;
124 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_64
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) PAGE_SHIFT
;
125 #endif /* __arm64__ */
127 commpage_update_timebase();
128 commpage_update_mach_continuous_time(0);
131 clock_usec_t microsecs
;
132 clock_get_boottime_microtime(&secs
, µsecs
);
133 commpage_update_boottime(secs
* USEC_PER_SEC
+ microsecs
);
136 * set commpage approximate time to zero for initialization.
137 * scheduler shall populate correct value before running user thread
139 *((uint64_t *)(_COMM_PAGE_APPROX_TIME
+ _COMM_PAGE_RW_OFFSET
)) = 0;
140 #ifdef CONFIG_MACH_APPROXIMATE_TIME
141 *((uint8_t *)(_COMM_PAGE_APPROX_TIME_SUPPORTED
+ _COMM_PAGE_RW_OFFSET
)) = 1;
143 *((uint8_t *)(_COMM_PAGE_APPROX_TIME_SUPPORTED
+ _COMM_PAGE_RW_OFFSET
)) = 0;
146 commpage_update_kdebug_state();
149 commpage_update_atm_diagnostic_config(atm_get_diagnostic_config());
153 *((uint64_t*)(_COMM_PAGE_REMOTETIME_PARAMS
+ _COMM_PAGE_RW_OFFSET
)) = BT_RESET_SENTINEL_TS
;
157 uint64_t m
; // magic number
158 int32_t a
; // add indicator
159 int32_t s
; // shift amount
163 commpage_set_timestamp(
168 uint64_t tick_per_sec
)
170 new_commpage_timeofday_data_t
*commpage_timeofday_datap
;
172 if (commPagePtr
== 0) {
176 commpage_timeofday_datap
= (new_commpage_timeofday_data_t
*)(_COMM_PAGE_NEWTIMEOFDAY_DATA
+ _COMM_PAGE_RW_OFFSET
);
178 commpage_timeofday_datap
->TimeStamp_tick
= 0x0ULL
;
180 #if (__ARM_ARCH__ >= 7)
181 __asm__
volatile ("dmb ish");
183 commpage_timeofday_datap
->TimeStamp_sec
= secs
;
184 commpage_timeofday_datap
->TimeStamp_frac
= frac
;
185 commpage_timeofday_datap
->Ticks_scale
= scale
;
186 commpage_timeofday_datap
->Ticks_per_sec
= tick_per_sec
;
188 #if (__ARM_ARCH__ >= 7)
189 __asm__
volatile ("dmb ish");
191 commpage_timeofday_datap
->TimeStamp_tick
= tbr
;
195 * Update _COMM_PAGE_MEMORY_PRESSURE. Called periodically from vm's compute_memory_pressure()
199 commpage_set_memory_pressure(
200 unsigned int pressure
)
202 if (commPagePtr
== 0) {
205 *((uint32_t *)(_COMM_PAGE_MEMORY_PRESSURE
+ _COMM_PAGE_RW_OFFSET
)) = pressure
;
209 * Update _COMM_PAGE_SPIN_COUNT. We might want to reduce when running on a battery, etc.
213 commpage_set_spin_count(
216 if (count
== 0) { /* we test for 0 after decrement, not before */
220 if (commPagePtr
== 0) {
223 *((uint32_t *)(_COMM_PAGE_SPIN_COUNT
+ _COMM_PAGE_RW_OFFSET
)) = count
;
227 * Determine number of CPUs on this system.
230 commpage_cpus( void )
234 cpus
= ml_get_max_cpus(); // NB: this call can block
237 panic("commpage cpus==0");
247 _get_commpage_priv_address(void)
249 return sharedpage_rw_addr
;
253 * Initialize _cpu_capabilities vector
256 commpage_init_cpu_capabilities( void )
260 ml_cpu_info_t cpu_info
;
263 ml_cpu_get_info(&cpu_info
);
265 switch (cpu_info
.cache_line_size
) {
278 cpus
= commpage_cpus();
284 bits
|= (cpus
<< kNumCPUsShift
);
286 bits
|= kFastThreadLocalStorage
; // TPIDRURO for TLS
290 arm_mvfp_info_t
*mvfp_info
= arm_mvfp_info();
291 if (mvfp_info
->neon
) {
294 if (mvfp_info
->neon_hpfp
) {
295 bits
|= kHasNeonHPFP
;
297 if (mvfp_info
->neon_fp16
) {
298 bits
|= kHasNeonFP16
;
301 #if defined(__arm64__)
304 #if __ARM_ENABLE_WFE_
306 if (arm64_wfe_allowed()) {
313 #if __ARM_V8_CRYPTO_EXTENSIONS__
314 bits
|= kHasARMv8Crypto
;
317 uint64_t isar0
= __builtin_arm_rsr64("ID_AA64ISAR0_EL1");
318 if ((isar0
& ID_AA64ISAR0_EL1_ATOMIC_MASK
) == ID_AA64ISAR0_EL1_ATOMIC_8_1
) {
319 bits
|= kHasARMv81Atomics
;
322 if ((isar0
& ID_AA64ISAR0_EL1_CRC32_MASK
) == ID_AA64ISAR0_EL1_CRC32_EN
) {
323 bits
|= kHasARMv8Crc32
;
327 _cpu_capabilities
= bits
;
329 *((uint32_t *)(_COMM_PAGE_CPU_CAPABILITIES
+ _COMM_PAGE_RW_OFFSET
)) = _cpu_capabilities
;
333 * Updated every time a logical CPU goes offline/online
336 commpage_update_active_cpus(void)
341 *((uint8_t *)(_COMM_PAGE_ACTIVE_CPUS
+ _COMM_PAGE_RW_OFFSET
)) = processor_avail_count
;
345 * Update the commpage bits for mach_absolute_time and mach_continuous_time (for userspace)
348 commpage_update_timebase(void)
351 *((uint64_t*)(_COMM_PAGE_TIMEBASE_OFFSET
+ _COMM_PAGE_RW_OFFSET
)) = rtclock_base_abstime
;
356 * Update the commpage with current kdebug state. This currently has bits for
357 * global trace state, and typefilter enablement. It is likely additional state
358 * will be tracked in the future.
360 * INVARIANT: This value will always be 0 if global tracing is disabled. This
361 * allows simple guard tests of "if (*_COMM_PAGE_KDEBUG_ENABLE) { ... }"
364 commpage_update_kdebug_state(void)
367 *((volatile uint32_t*)(_COMM_PAGE_KDEBUG_ENABLE
+ _COMM_PAGE_RW_OFFSET
)) = kdebug_commpage_state();
371 /* Ditto for atm_diagnostic_config */
373 commpage_update_atm_diagnostic_config(uint32_t diagnostic_config
)
376 *((volatile uint32_t*)(_COMM_PAGE_ATM_DIAGNOSTIC_CONFIG
+ _COMM_PAGE_RW_OFFSET
)) = diagnostic_config
;
381 * Update the commpage data with the state of multiuser mode for
382 * this device. Allowing various services in userspace to avoid
383 * IPC in the (more common) non-multiuser environment.
386 commpage_update_multiuser_config(uint32_t multiuser_config
)
389 *((volatile uint32_t *)(_COMM_PAGE_MULTIUSER_CONFIG
+ _COMM_PAGE_RW_OFFSET
)) = multiuser_config
;
394 * update the commpage data for
395 * last known value of mach_absolute_time()
399 commpage_update_mach_approximate_time(uint64_t abstime
)
401 #ifdef CONFIG_MACH_APPROXIMATE_TIME
402 uintptr_t approx_time_base
= (uintptr_t)(_COMM_PAGE_APPROX_TIME
+ _COMM_PAGE_RW_OFFSET
);
406 saved_data
= atomic_load_explicit((_Atomic
uint64_t *)approx_time_base
,
407 memory_order_relaxed
);
408 if (saved_data
< abstime
) {
409 /* ignoring the success/fail return value assuming that
410 * if the value has been updated since we last read it,
411 * "someone" has a newer timestamp than us and ours is
413 atomic_compare_exchange_strong_explicit((_Atomic
uint64_t *)approx_time_base
,
414 &saved_data
, abstime
, memory_order_relaxed
, memory_order_relaxed
);
418 #pragma unused (abstime)
423 * update the commpage data's total system sleep time for
424 * userspace call to mach_continuous_time()
427 commpage_update_mach_continuous_time(uint64_t sleeptime
)
431 *((uint64_t *)(_COMM_PAGE_CONT_TIMEBASE
+ _COMM_PAGE_RW_OFFSET
)) = sleeptime
;
433 uint64_t *c_time_base
= (uint64_t *)(_COMM_PAGE_CONT_TIMEBASE
+ _COMM_PAGE_RW_OFFSET
);
437 } while (!OSCompareAndSwap64(old
, sleeptime
, c_time_base
));
438 #endif /* __arm64__ */
443 * update the commpage's value for the boot time
446 commpage_update_boottime(uint64_t value
)
450 *((uint64_t *)(_COMM_PAGE_BOOTTIME_USEC
+ _COMM_PAGE_RW_OFFSET
)) = value
;
452 uint64_t *cp
= (uint64_t *)(_COMM_PAGE_BOOTTIME_USEC
+ _COMM_PAGE_RW_OFFSET
);
456 } while (!OSCompareAndSwap64(old_value
, value
, cp
));
457 #endif /* __arm64__ */
462 * set the commpage's remote time params for
463 * userspace call to mach_bridge_remote_time()
466 commpage_set_remotetime_params(double rate
, uint64_t base_local_ts
, uint64_t base_remote_ts
)
470 struct bt_params
*paramsp
= (struct bt_params
*)(_COMM_PAGE_REMOTETIME_PARAMS
+ _COMM_PAGE_RW_OFFSET
);
471 paramsp
->base_local_ts
= 0;
472 __asm__
volatile ("dmb ish" ::: "memory");
473 paramsp
->rate
= rate
;
474 paramsp
->base_remote_ts
= base_remote_ts
;
475 __asm__
volatile ("dmb ish" ::: "memory");
476 paramsp
->base_local_ts
= base_local_ts
; //This will act as a generation count
480 (void)base_remote_ts
;
481 #endif /* __arm64__ */
487 * After this counter has incremented, all running CPUs are guaranteed to
488 * have quiesced, i.e. executed serially dependent memory barriers.
489 * This is only tracked for CPUs running in userspace, therefore only useful
490 * outside the kernel.
492 * Note that you can't know which side of those barriers your read was from,
493 * so you have to observe 2 increments in order to ensure that you saw a
494 * serially dependent barrier chain across all running CPUs.
497 commpage_increment_cpu_quiescent_counter(void)
505 _Atomic
uint64_t *sched_gen
= (_Atomic
uint64_t *)(_COMM_PAGE_CPU_QUIESCENT_COUNTER
+
506 _COMM_PAGE_RW_OFFSET
);
508 * On 32bit architectures, double-wide atomic load or stores are a CAS,
509 * so the atomic increment is the most efficient way to increment the
512 * On 64bit architectures however, because the update is synchronized by
513 * the cpu mask, relaxed loads and stores is more efficient.
516 old_gen
= atomic_load_explicit(sched_gen
, memory_order_relaxed
);
517 atomic_store_explicit(sched_gen
, old_gen
+ 1, memory_order_relaxed
);
519 old_gen
= atomic_fetch_add_explicit(sched_gen
, 1, memory_order_relaxed
);