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/config.h>
46 #include <machine/pmap.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_map.h>
49 #include <vm/vm_protos.h>
50 #include <ipc/ipc_port.h>
51 #include <arm/cpuid.h> /* for cpuid_info() & cache_info() */
52 #include <arm/rtclock.h>
53 #include <libkern/OSAtomic.h>
54 #include <stdatomic.h>
55 #include <kern/remote_time.h>
56 #include <machine/machine_remote_time.h>
58 #include <sys/kdebug.h>
61 #include <atm/atm_internal.h>
64 static void commpage_init_cpu_capabilities( void );
65 static int commpage_cpus( void );
67 SECURITY_READ_ONLY_LATE(vm_address_t
) commPagePtr
= 0;
68 SECURITY_READ_ONLY_LATE(vm_address_t
) sharedpage_rw_addr
= 0;
69 SECURITY_READ_ONLY_LATE(uint32_t) _cpu_capabilities
= 0;
71 /* For sysctl access from BSD side */
72 extern int gARMv81Atomics
;
73 extern int gARMv8Crc32
;
74 extern int gARMv82FHM
;
83 sharedpage_rw_addr
= pmap_create_sharedpage();
84 commPagePtr
= (vm_address_t
)_COMM_PAGE_BASE_ADDRESS
;
87 bcopy(_COMM_PAGE64_SIGNATURE_STRING
, (void *)(_COMM_PAGE_SIGNATURE
+ _COMM_PAGE_RW_OFFSET
),
88 MIN(_COMM_PAGE_SIGNATURELEN
, strlen(_COMM_PAGE64_SIGNATURE_STRING
)));
90 bcopy(_COMM_PAGE32_SIGNATURE_STRING
, (void *)(_COMM_PAGE_SIGNATURE
+ _COMM_PAGE_RW_OFFSET
),
91 MIN(_COMM_PAGE_SIGNATURELEN
, strlen(_COMM_PAGE32_SIGNATURE_STRING
)));
94 *((uint16_t*)(_COMM_PAGE_VERSION
+ _COMM_PAGE_RW_OFFSET
)) = (uint16_t) _COMM_PAGE_THIS_VERSION
;
96 commpage_init_cpu_capabilities();
97 commpage_set_timestamp(0, 0, 0, 0, 0);
99 if (_cpu_capabilities
& kCache32
) {
101 } else if (_cpu_capabilities
& kCache64
) {
103 } else if (_cpu_capabilities
& kCache128
) {
109 *((uint16_t*)(_COMM_PAGE_CACHE_LINESIZE
+ _COMM_PAGE_RW_OFFSET
)) = c2
;
110 *((uint32_t*)(_COMM_PAGE_SPIN_COUNT
+ _COMM_PAGE_RW_OFFSET
)) = 1;
112 commpage_update_active_cpus();
113 cpufamily
= cpuid_get_cpufamily();
115 /* machine_info valid after ml_get_max_cpus() */
116 *((uint8_t*)(_COMM_PAGE_PHYSICAL_CPUS
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) machine_info
.physical_cpu_max
;
117 *((uint8_t*)(_COMM_PAGE_LOGICAL_CPUS
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) machine_info
.logical_cpu_max
;
118 *((uint64_t*)(_COMM_PAGE_MEMORY_SIZE
+ _COMM_PAGE_RW_OFFSET
)) = machine_info
.max_mem
;
119 *((uint32_t*)(_COMM_PAGE_CPUFAMILY
+ _COMM_PAGE_RW_OFFSET
)) = (uint32_t)cpufamily
;
120 *((uint32_t*)(_COMM_PAGE_DEV_FIRM
+ _COMM_PAGE_RW_OFFSET
)) = (uint32_t)PE_i_can_has_debugger(NULL
);
121 *((uint8_t*)(_COMM_PAGE_USER_TIMEBASE
+ _COMM_PAGE_RW_OFFSET
)) = user_timebase_type();
122 *((uint8_t*)(_COMM_PAGE_CONT_HWCLOCK
+ _COMM_PAGE_RW_OFFSET
)) = user_cont_hwclock_allowed();
123 *((uint8_t*)(_COMM_PAGE_KERNEL_PAGE_SHIFT
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) page_shift
;
126 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_32
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) page_shift_user32
;
127 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_64
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) SIXTEENK_PAGE_SHIFT
;
128 #elif (__ARM_ARCH_7K__ >= 2)
129 /* enforce 16KB alignment for watch targets with new ABI */
130 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_32
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) SIXTEENK_PAGE_SHIFT
;
131 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_64
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) SIXTEENK_PAGE_SHIFT
;
132 #else /* __arm64__ */
133 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_32
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) PAGE_SHIFT
;
134 *((uint8_t*)(_COMM_PAGE_USER_PAGE_SHIFT_64
+ _COMM_PAGE_RW_OFFSET
)) = (uint8_t) PAGE_SHIFT
;
135 #endif /* __arm64__ */
137 commpage_update_timebase();
138 commpage_update_mach_continuous_time(0);
141 clock_usec_t microsecs
;
142 clock_get_boottime_microtime(&secs
, µsecs
);
143 commpage_update_boottime(secs
* USEC_PER_SEC
+ microsecs
);
146 * set commpage approximate time to zero for initialization.
147 * scheduler shall populate correct value before running user thread
149 *((uint64_t *)(_COMM_PAGE_APPROX_TIME
+ _COMM_PAGE_RW_OFFSET
)) = 0;
150 #ifdef CONFIG_MACH_APPROXIMATE_TIME
151 *((uint8_t *)(_COMM_PAGE_APPROX_TIME_SUPPORTED
+ _COMM_PAGE_RW_OFFSET
)) = 1;
153 *((uint8_t *)(_COMM_PAGE_APPROX_TIME_SUPPORTED
+ _COMM_PAGE_RW_OFFSET
)) = 0;
156 commpage_update_kdebug_state();
159 commpage_update_atm_diagnostic_config(atm_get_diagnostic_config());
163 *((uint64_t*)(_COMM_PAGE_REMOTETIME_PARAMS
+ _COMM_PAGE_RW_OFFSET
)) = BT_RESET_SENTINEL_TS
;
167 uint64_t m
; // magic number
168 int32_t a
; // add indicator
169 int32_t s
; // shift amount
173 commpage_set_timestamp(
178 uint64_t tick_per_sec
)
180 new_commpage_timeofday_data_t
*commpage_timeofday_datap
;
182 if (commPagePtr
== 0) {
186 commpage_timeofday_datap
= (new_commpage_timeofday_data_t
*)(_COMM_PAGE_NEWTIMEOFDAY_DATA
+ _COMM_PAGE_RW_OFFSET
);
188 commpage_timeofday_datap
->TimeStamp_tick
= 0x0ULL
;
190 #if (__ARM_ARCH__ >= 7)
191 __asm__
volatile ("dmb ish");
193 commpage_timeofday_datap
->TimeStamp_sec
= secs
;
194 commpage_timeofday_datap
->TimeStamp_frac
= frac
;
195 commpage_timeofday_datap
->Ticks_scale
= scale
;
196 commpage_timeofday_datap
->Ticks_per_sec
= tick_per_sec
;
198 #if (__ARM_ARCH__ >= 7)
199 __asm__
volatile ("dmb ish");
201 commpage_timeofday_datap
->TimeStamp_tick
= tbr
;
205 * Update _COMM_PAGE_MEMORY_PRESSURE. Called periodically from vm's compute_memory_pressure()
209 commpage_set_memory_pressure(
210 unsigned int pressure
)
212 if (commPagePtr
== 0) {
215 *((uint32_t *)(_COMM_PAGE_MEMORY_PRESSURE
+ _COMM_PAGE_RW_OFFSET
)) = pressure
;
219 * Update _COMM_PAGE_SPIN_COUNT. We might want to reduce when running on a battery, etc.
223 commpage_set_spin_count(
226 if (count
== 0) { /* we test for 0 after decrement, not before */
230 if (commPagePtr
== 0) {
233 *((uint32_t *)(_COMM_PAGE_SPIN_COUNT
+ _COMM_PAGE_RW_OFFSET
)) = count
;
237 * Determine number of CPUs on this system.
240 commpage_cpus( void )
244 cpus
= ml_get_max_cpus(); // NB: this call can block
247 panic("commpage cpus==0");
257 _get_cpu_capabilities(void)
259 return _cpu_capabilities
;
263 _get_commpage_priv_address(void)
265 return sharedpage_rw_addr
;
269 * Initialize _cpu_capabilities vector
272 commpage_init_cpu_capabilities( void )
276 ml_cpu_info_t cpu_info
;
279 ml_cpu_get_info(&cpu_info
);
281 switch (cpu_info
.cache_line_size
) {
294 cpus
= commpage_cpus();
300 bits
|= (cpus
<< kNumCPUsShift
);
302 bits
|= kFastThreadLocalStorage
; // TPIDRURO for TLS
306 arm_mvfp_info_t
*mvfp_info
= arm_mvfp_info();
307 if (mvfp_info
->neon
) {
310 if (mvfp_info
->neon_hpfp
) {
311 bits
|= kHasNeonHPFP
;
313 if (mvfp_info
->neon_fp16
) {
314 bits
|= kHasNeonFP16
;
317 #if defined(__arm64__)
320 #if __ARM_ENABLE_WFE_
322 if (arm64_wfe_allowed()) {
329 #if __ARM_V8_CRYPTO_EXTENSIONS__
330 bits
|= kHasARMv8Crypto
;
333 uint64_t isar0
= __builtin_arm_rsr64("ID_AA64ISAR0_EL1");
334 if ((isar0
& ID_AA64ISAR0_EL1_ATOMIC_MASK
) == ID_AA64ISAR0_EL1_ATOMIC_8_1
) {
335 bits
|= kHasARMv81Atomics
;
338 if ((isar0
& ID_AA64ISAR0_EL1_CRC32_MASK
) == ID_AA64ISAR0_EL1_CRC32_EN
) {
339 bits
|= kHasARMv8Crc32
;
342 if ((isar0
& ID_AA64ISAR0_EL1_FHM_MASK
) >= ID_AA64ISAR0_EL1_FHM_8_2
) {
343 bits
|= kHasARMv82FHM
;
351 _cpu_capabilities
= bits
;
353 *((uint32_t *)(_COMM_PAGE_CPU_CAPABILITIES
+ _COMM_PAGE_RW_OFFSET
)) = _cpu_capabilities
;
357 * Updated every time a logical CPU goes offline/online
360 commpage_update_active_cpus(void)
365 *((uint8_t *)(_COMM_PAGE_ACTIVE_CPUS
+ _COMM_PAGE_RW_OFFSET
)) = processor_avail_count
;
369 * Update the commpage bits for mach_absolute_time and mach_continuous_time (for userspace)
372 commpage_update_timebase(void)
375 *((uint64_t*)(_COMM_PAGE_TIMEBASE_OFFSET
+ _COMM_PAGE_RW_OFFSET
)) = rtclock_base_abstime
;
380 * Update the commpage with current kdebug state. This currently has bits for
381 * global trace state, and typefilter enablement. It is likely additional state
382 * will be tracked in the future.
384 * INVARIANT: This value will always be 0 if global tracing is disabled. This
385 * allows simple guard tests of "if (*_COMM_PAGE_KDEBUG_ENABLE) { ... }"
388 commpage_update_kdebug_state(void)
391 *((volatile uint32_t*)(_COMM_PAGE_KDEBUG_ENABLE
+ _COMM_PAGE_RW_OFFSET
)) = kdebug_commpage_state();
395 /* Ditto for atm_diagnostic_config */
397 commpage_update_atm_diagnostic_config(uint32_t diagnostic_config
)
400 *((volatile uint32_t*)(_COMM_PAGE_ATM_DIAGNOSTIC_CONFIG
+ _COMM_PAGE_RW_OFFSET
)) = diagnostic_config
;
405 * Update the commpage data with the state of multiuser mode for
406 * this device. Allowing various services in userspace to avoid
407 * IPC in the (more common) non-multiuser environment.
410 commpage_update_multiuser_config(uint32_t multiuser_config
)
413 *((volatile uint32_t *)(_COMM_PAGE_MULTIUSER_CONFIG
+ _COMM_PAGE_RW_OFFSET
)) = multiuser_config
;
418 * update the commpage data for
419 * last known value of mach_absolute_time()
423 commpage_update_mach_approximate_time(uint64_t abstime
)
425 #ifdef CONFIG_MACH_APPROXIMATE_TIME
426 uintptr_t approx_time_base
= (uintptr_t)(_COMM_PAGE_APPROX_TIME
+ _COMM_PAGE_RW_OFFSET
);
430 saved_data
= atomic_load_explicit((_Atomic
uint64_t *)approx_time_base
,
431 memory_order_relaxed
);
432 if (saved_data
< abstime
) {
433 /* ignoring the success/fail return value assuming that
434 * if the value has been updated since we last read it,
435 * "someone" has a newer timestamp than us and ours is
437 atomic_compare_exchange_strong_explicit((_Atomic
uint64_t *)approx_time_base
,
438 &saved_data
, abstime
, memory_order_relaxed
, memory_order_relaxed
);
442 #pragma unused (abstime)
447 * update the commpage data's total system sleep time for
448 * userspace call to mach_continuous_time()
451 commpage_update_mach_continuous_time(uint64_t sleeptime
)
455 *((uint64_t *)(_COMM_PAGE_CONT_TIMEBASE
+ _COMM_PAGE_RW_OFFSET
)) = sleeptime
;
457 uint64_t *c_time_base
= (uint64_t *)(_COMM_PAGE_CONT_TIMEBASE
+ _COMM_PAGE_RW_OFFSET
);
461 } while (!OSCompareAndSwap64(old
, sleeptime
, c_time_base
));
462 #endif /* __arm64__ */
467 * update the commpage's value for the boot time
470 commpage_update_boottime(uint64_t value
)
474 *((uint64_t *)(_COMM_PAGE_BOOTTIME_USEC
+ _COMM_PAGE_RW_OFFSET
)) = value
;
476 uint64_t *cp
= (uint64_t *)(_COMM_PAGE_BOOTTIME_USEC
+ _COMM_PAGE_RW_OFFSET
);
480 } while (!OSCompareAndSwap64(old_value
, value
, cp
));
481 #endif /* __arm64__ */
486 * set the commpage's remote time params for
487 * userspace call to mach_bridge_remote_time()
490 commpage_set_remotetime_params(double rate
, uint64_t base_local_ts
, uint64_t base_remote_ts
)
494 struct bt_params
*paramsp
= (struct bt_params
*)(_COMM_PAGE_REMOTETIME_PARAMS
+ _COMM_PAGE_RW_OFFSET
);
495 paramsp
->base_local_ts
= 0;
496 __asm__
volatile ("dmb ish" ::: "memory");
497 paramsp
->rate
= rate
;
498 paramsp
->base_remote_ts
= base_remote_ts
;
499 __asm__
volatile ("dmb ish" ::: "memory");
500 paramsp
->base_local_ts
= base_local_ts
; //This will act as a generation count
504 (void)base_remote_ts
;
505 #endif /* __arm64__ */
511 * After this counter has incremented, all running CPUs are guaranteed to
512 * have quiesced, i.e. executed serially dependent memory barriers.
513 * This is only tracked for CPUs running in userspace, therefore only useful
514 * outside the kernel.
516 * Note that you can't know which side of those barriers your read was from,
517 * so you have to observe 2 increments in order to ensure that you saw a
518 * serially dependent barrier chain across all running CPUs.
521 commpage_increment_cpu_quiescent_counter(void)
529 _Atomic
uint64_t *sched_gen
= (_Atomic
uint64_t *)(_COMM_PAGE_CPU_QUIESCENT_COUNTER
+
530 _COMM_PAGE_RW_OFFSET
);
532 * On 32bit architectures, double-wide atomic load or stores are a CAS,
533 * so the atomic increment is the most efficient way to increment the
536 * On 64bit architectures however, because the update is synchronized by
537 * the cpu mask, relaxed loads and stores is more efficient.
540 old_gen
= os_atomic_load(sched_gen
, relaxed
);
541 os_atomic_store(sched_gen
, old_gen
+ 1, relaxed
);
543 old_gen
= atomic_fetch_add_explicit(sched_gen
, 1, memory_order_relaxed
);
549 * update the commpage with if dtrace user land probes are enabled
552 commpage_update_dof(boolean_t enabled
)
555 *((uint8_t*)(_COMM_PAGE_DTRACE_DOF_ENABLED
+ _COMM_PAGE_RW_OFFSET
)) = (enabled
? 1 : 0);
562 * update the dyld global config flags
565 commpage_update_dyld_flags(uint64_t value
)
567 *((uint64_t*)(_COMM_PAGE_DYLD_SYSTEM_FLAGS
+ _COMM_PAGE_RW_OFFSET
)) = value
;