2 * Copyright (c) 2003-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Here's what to do if you want to add a new routine to the comm page:
32 * 1. Add a definition for it's address in osfmk/i386/cpu_capabilities.h,
33 * being careful to reserve room for future expansion.
35 * 2. Write one or more versions of the routine, each with it's own
36 * commpage_descriptor. The tricky part is getting the "special",
37 * "musthave", and "canthave" fields right, so that exactly one
38 * version of the routine is selected for every machine.
39 * The source files should be in osfmk/i386/commpage/.
41 * 3. Add a ptr to your new commpage_descriptor(s) in the "routines"
42 * array in osfmk/i386/commpage/commpage_asm.s. There are two
43 * arrays, one for the 32-bit and one for the 64-bit commpage.
45 * 4. Write the code in Libc to use the new routine.
48 #include <mach/mach_types.h>
49 #include <mach/machine.h>
50 #include <mach/vm_map.h>
51 #include <mach/mach_vm.h>
52 #include <mach/machine.h>
53 #include <i386/cpuid.h>
55 #include <i386/rtclock_protos.h>
56 #include <i386/cpu_data.h>
57 #include <i386/machine_routines.h>
58 #include <i386/misc_protos.h>
59 #include <i386/cpuid.h>
60 #include <machine/cpu_capabilities.h>
61 #include <machine/commpage.h>
62 #include <machine/pmap.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_map.h>
65 #include <stdatomic.h>
67 #include <ipc/ipc_port.h>
69 #include <kern/page_decrypt.h>
70 #include <kern/processor.h>
72 #include <sys/kdebug.h>
75 #include <atm/atm_internal.h>
78 /* the lists of commpage routines are in commpage_asm.s */
79 extern commpage_descriptor
* commpage_32_routines
[];
80 extern commpage_descriptor
* commpage_64_routines
[];
82 extern vm_map_t commpage32_map
; // the shared submap, set up in vm init
83 extern vm_map_t commpage64_map
; // the shared submap, set up in vm init
84 extern vm_map_t commpage_text32_map
; // the shared submap, set up in vm init
85 extern vm_map_t commpage_text64_map
; // the shared submap, set up in vm init
88 char *commPagePtr32
= NULL
; // virtual addr in kernel map of 32-bit commpage
89 char *commPagePtr64
= NULL
; // ...and of 64-bit commpage
90 char *commPageTextPtr32
= NULL
; // virtual addr in kernel map of 32-bit commpage
91 char *commPageTextPtr64
= NULL
; // ...and of 64-bit commpage
93 uint64_t _cpu_capabilities
= 0; // define the capability vector
95 typedef uint32_t commpage_address_t
;
97 static commpage_address_t next
; // next available address in comm page
99 static char *commPagePtr
; // virtual addr in kernel map of commpage we are working on
100 static commpage_address_t commPageBaseOffset
; // subtract from 32-bit runtime address to get offset in virtual commpage in kernel map
102 static commpage_time_data
*time_data32
= NULL
;
103 static commpage_time_data
*time_data64
= NULL
;
104 static new_commpage_timeofday_data_t
*gtod_time_data32
= NULL
;
105 static new_commpage_timeofday_data_t
*gtod_time_data64
= NULL
;
108 decl_simple_lock_data(static,commpage_active_cpus_lock
);
110 /* Allocate the commpage and add to the shared submap created by vm:
111 * 1. allocate a page in the kernel map (RW)
113 * 3. make a memory entry out of it
114 * 4. map that entry into the shared comm region map (R-only)
119 vm_map_t submap
, // commpage32_map or commpage_map64
120 size_t area_used
, // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
123 vm_offset_t kernel_addr
= 0; // address of commpage in kernel map
124 vm_offset_t zero
= 0;
125 vm_size_t size
= area_used
; // size actually populated
126 vm_map_entry_t entry
;
131 panic("commpage submap is null");
133 if ((kr
= vm_map_kernel(kernel_map
,
138 VM_KERN_MEMORY_OSFMK
,
145 panic("cannot allocate commpage %d", kr
);
147 if ((kr
= vm_map_wire_kernel(kernel_map
,
149 kernel_addr
+area_used
,
150 VM_PROT_DEFAULT
, VM_KERN_MEMORY_OSFMK
,
152 panic("cannot wire commpage: %d", kr
);
155 * Now that the object is created and wired into the kernel map, mark it so that no delay
156 * copy-on-write will ever be performed on it as a result of mapping it into user-space.
157 * If such a delayed copy ever occurred, we could remove the kernel's wired mapping - and
158 * that would be a real disaster.
160 * JMM - What we really need is a way to create it like this in the first place.
162 if (!(kr
= vm_map_lookup_entry( kernel_map
, vm_map_trunc_page(kernel_addr
, VM_MAP_PAGE_MASK(kernel_map
)), &entry
) || entry
->is_sub_map
))
163 panic("cannot find commpage entry %d", kr
);
164 VME_OBJECT(entry
)->copy_strategy
= MEMORY_OBJECT_COPY_NONE
;
166 if ((kr
= mach_make_memory_entry( kernel_map
, // target map
168 kernel_addr
, // offset (address in kernel map)
169 uperm
, // protections as specified
170 &handle
, // this is the object handle we get
171 NULL
))) // parent_entry (what is this?)
172 panic("cannot make entry for commpage %d", kr
);
174 if ((kr
= vm_map_64_kernel( submap
, // target map (shared submap)
175 &zero
, // address (map into 1st page in submap)
178 VM_FLAGS_FIXED
, // flags (it must be 1st page in submap)
180 handle
, // port is the memory entry we just made
181 0, // offset (map 1st page in memory entry)
183 uperm
, // cur_protection (R-only in user map)
184 uperm
, // max_protection
185 VM_INHERIT_SHARE
))) // inheritance
186 panic("cannot map commpage %d", kr
);
188 ipc_port_release(handle
);
189 /* Make the kernel mapping non-executable. This cannot be done
190 * at the time of map entry creation as mach_make_memory_entry
191 * cannot handle disjoint permissions at this time.
193 kr
= vm_protect(kernel_map
, kernel_addr
, area_used
, FALSE
, VM_PROT_READ
| VM_PROT_WRITE
);
194 assert (kr
== KERN_SUCCESS
);
196 return (void*)(intptr_t)kernel_addr
; // return address in kernel map
199 /* Get address (in kernel map) of a commpage field. */
203 commpage_address_t addr_at_runtime
)
205 return (void*) ((uintptr_t)commPagePtr
+ (addr_at_runtime
- commPageBaseOffset
));
208 /* Determine number of CPUs on this system. We cannot rely on
209 * machine_info.max_cpus this early in the boot.
212 commpage_cpus( void )
216 cpus
= ml_get_max_cpus(); // NB: this call can block
219 panic("commpage cpus==0");
226 /* Initialize kernel version of _cpu_capabilities vector (used by KEXTs.) */
229 commpage_init_cpu_capabilities( void )
233 ml_cpu_info_t cpu_info
;
236 ml_cpu_get_info(&cpu_info
);
238 switch (cpu_info
.vector_unit
) {
249 bits
|= kHasSupplementalSSE3
;
265 switch (cpu_info
.cache_line_size
) {
278 cpus
= commpage_cpus(); // how many CPUs do we have
280 bits
|= (cpus
<< kNumCPUsShift
);
282 bits
|= kFastThreadLocalStorage
; // we use %gs for TLS
284 #define setif(_bits, _bit, _condition) \
285 if (_condition) _bits |= _bit
287 setif(bits
, kUP
, cpus
== 1);
288 setif(bits
, k64Bit
, cpu_mode_is64bit());
289 setif(bits
, kSlow
, tscFreq
<= SLOW_TSC_THRESHOLD
);
291 setif(bits
, kHasAES
, cpuid_features() &
293 setif(bits
, kHasF16C
, cpuid_features() &
295 setif(bits
, kHasRDRAND
, cpuid_features() &
296 CPUID_FEATURE_RDRAND
);
297 setif(bits
, kHasFMA
, cpuid_features() &
300 setif(bits
, kHasBMI1
, cpuid_leaf7_features() &
301 CPUID_LEAF7_FEATURE_BMI1
);
302 setif(bits
, kHasBMI2
, cpuid_leaf7_features() &
303 CPUID_LEAF7_FEATURE_BMI2
);
304 setif(bits
, kHasRTM
, cpuid_leaf7_features() &
305 CPUID_LEAF7_FEATURE_RTM
);
306 setif(bits
, kHasHLE
, cpuid_leaf7_features() &
307 CPUID_LEAF7_FEATURE_HLE
);
308 setif(bits
, kHasAVX2_0
, cpuid_leaf7_features() &
309 CPUID_LEAF7_FEATURE_AVX2
);
310 setif(bits
, kHasRDSEED
, cpuid_features() &
311 CPUID_LEAF7_FEATURE_RDSEED
);
312 setif(bits
, kHasADX
, cpuid_features() &
313 CPUID_LEAF7_FEATURE_ADX
);
315 #if 0 /* The kernel doesn't support MPX or SGX */
316 setif(bits
, kHasMPX
, cpuid_leaf7_features() &
317 CPUID_LEAF7_FEATURE_MPX
);
318 setif(bits
, kHasSGX
, cpuid_leaf7_features() &
319 CPUID_LEAF7_FEATURE_SGX
);
322 #if !defined(RC_HIDE_XNU_J137)
323 if (ml_fpu_avx512_enabled()) {
324 setif(bits
, kHasAVX512F
, cpuid_leaf7_features() &
325 CPUID_LEAF7_FEATURE_AVX512F
);
326 setif(bits
, kHasAVX512CD
, cpuid_leaf7_features() &
327 CPUID_LEAF7_FEATURE_AVX512CD
);
328 setif(bits
, kHasAVX512DQ
, cpuid_leaf7_features() &
329 CPUID_LEAF7_FEATURE_AVX512DQ
);
330 setif(bits
, kHasAVX512BW
, cpuid_leaf7_features() &
331 CPUID_LEAF7_FEATURE_AVX512BW
);
332 setif(bits
, kHasAVX512VL
, cpuid_leaf7_features() &
333 CPUID_LEAF7_FEATURE_AVX512VL
);
334 setif(bits
, kHasAVX512IFMA
, cpuid_leaf7_features() &
335 CPUID_LEAF7_FEATURE_AVX512IFMA
);
336 setif(bits
, kHasAVX512VBMI
, cpuid_leaf7_features() &
337 CPUID_LEAF7_FEATURE_AVX512VBMI
);
340 #endif /* not RC_HIDE_XNU_J137 */
341 uint64_t misc_enable
= rdmsr64(MSR_IA32_MISC_ENABLE
);
342 setif(bits
, kHasENFSTRG
, (misc_enable
& 1ULL) &&
343 (cpuid_leaf7_features() &
344 CPUID_LEAF7_FEATURE_ERMS
));
346 _cpu_capabilities
= bits
; // set kernel version for use by drivers etc
349 /* initialize the approx_time_supported flag and set the approx time to 0.
350 * Called during initial commpage population.
353 commpage_mach_approximate_time_init(void)
355 char *cp
= commPagePtr32
;
358 #ifdef CONFIG_MACH_APPROXIMATE_TIME
364 cp
+= (_COMM_PAGE_APPROX_TIME_SUPPORTED
- _COMM_PAGE32_BASE_ADDRESS
);
365 *(boolean_t
*)cp
= supported
;
370 cp
+= (_COMM_PAGE_APPROX_TIME_SUPPORTED
- _COMM_PAGE32_START_ADDRESS
);
371 *(boolean_t
*)cp
= supported
;
373 commpage_update_mach_approximate_time(0);
377 commpage_mach_continuous_time_init(void)
379 commpage_update_mach_continuous_time(0);
383 commpage_boottime_init(void)
386 clock_usec_t microsecs
;
387 clock_get_boottime_microtime(&secs
, µsecs
);
388 commpage_update_boottime(secs
* USEC_PER_SEC
+ microsecs
);
392 _get_cpu_capabilities(void)
394 return _cpu_capabilities
;
397 /* Copy data into commpage. */
401 commpage_address_t address
,
405 void *dest
= commpage_addr_of(address
);
408 panic("commpage overlap at address 0x%p, 0x%x < 0x%x", dest
, address
, next
);
410 bcopy(source
,dest
,length
);
412 next
= address
+ length
;
415 /* Copy a routine into comm page if it matches running machine.
418 commpage_stuff_routine(
419 commpage_descriptor
*rd
)
421 commpage_stuff(rd
->commpage_address
,rd
->code_address
,rd
->code_length
);
424 /* Fill in the 32- or 64-bit commpage. Called once for each.
428 commpage_populate_one(
429 vm_map_t submap
, // commpage32_map or compage64_map
430 char ** kernAddressPtr
, // &commPagePtr32 or &commPagePtr64
431 size_t area_used
, // _COMM_PAGE32_AREA_USED or _COMM_PAGE64_AREA_USED
432 commpage_address_t base_offset
, // will become commPageBaseOffset
433 commpage_time_data
** time_data
, // &time_data32 or &time_data64
434 new_commpage_timeofday_data_t
** gtod_time_data
, // >od_time_data32 or >od_time_data64
435 const char* signature
, // "commpage 32-bit" or "commpage 64-bit"
443 short version
= _COMM_PAGE_THIS_VERSION
;
446 commPagePtr
= (char *)commpage_allocate( submap
, (vm_size_t
) area_used
, uperm
);
447 *kernAddressPtr
= commPagePtr
; // save address either in commPagePtr32 or 64
448 commPageBaseOffset
= base_offset
;
450 *time_data
= commpage_addr_of( _COMM_PAGE_TIME_DATA_START
);
451 *gtod_time_data
= commpage_addr_of( _COMM_PAGE_NEWTIMEOFDAY_DATA
);
453 /* Stuff in the constants. We move things into the comm page in strictly
454 * ascending order, so we can check for overlap and panic if so.
455 * Note: the 32-bit cpu_capabilities vector is retained in addition to
456 * the expanded 64-bit vector.
458 commpage_stuff(_COMM_PAGE_SIGNATURE
,signature
,(int)MIN(_COMM_PAGE_SIGNATURELEN
, strlen(signature
)));
459 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES64
,&_cpu_capabilities
,sizeof(_cpu_capabilities
));
460 commpage_stuff(_COMM_PAGE_VERSION
,&version
,sizeof(short));
461 commpage_stuff(_COMM_PAGE_CPU_CAPABILITIES
,&_cpu_capabilities
,sizeof(uint32_t));
464 if (_cpu_capabilities
& kCache64
)
466 else if (_cpu_capabilities
& kCache128
)
468 commpage_stuff(_COMM_PAGE_CACHE_LINESIZE
,&c2
,2);
471 commpage_stuff(_COMM_PAGE_SPIN_COUNT
,&c4
,4);
473 /* machine_info valid after ml_get_max_cpus() */
474 c1
= machine_info
.physical_cpu_max
;
475 commpage_stuff(_COMM_PAGE_PHYSICAL_CPUS
,&c1
,1);
476 c1
= machine_info
.logical_cpu_max
;
477 commpage_stuff(_COMM_PAGE_LOGICAL_CPUS
,&c1
,1);
479 c8
= ml_cpu_cache_size(0);
480 commpage_stuff(_COMM_PAGE_MEMORY_SIZE
, &c8
, 8);
482 cfamily
= cpuid_info()->cpuid_cpufamily
;
483 commpage_stuff(_COMM_PAGE_CPUFAMILY
, &cfamily
, 4);
485 if (next
> _COMM_PAGE_END
)
486 panic("commpage overflow: next = 0x%08x, commPagePtr = 0x%p", next
, commPagePtr
);
491 /* Fill in commpages: called once, during kernel initialization, from the
492 * startup thread before user-mode code is running.
494 * See the top of this file for a list of what you have to do to add
495 * a new routine to the commpage.
499 commpage_populate( void )
501 commpage_init_cpu_capabilities();
503 commpage_populate_one( commpage32_map
,
505 _COMM_PAGE32_AREA_USED
,
506 _COMM_PAGE32_BASE_ADDRESS
,
512 pmap_commpage32_init((vm_offset_t
) commPagePtr32
, _COMM_PAGE32_BASE_ADDRESS
,
513 _COMM_PAGE32_AREA_USED
/INTEL_PGBYTES
);
515 time_data64
= time_data32
; /* if no 64-bit commpage, point to 32-bit */
516 gtod_time_data64
= gtod_time_data32
;
518 if (_cpu_capabilities
& k64Bit
) {
519 commpage_populate_one( commpage64_map
,
521 _COMM_PAGE64_AREA_USED
,
522 _COMM_PAGE32_START_ADDRESS
, /* commpage address are relative to 32-bit commpage placement */
528 pmap_commpage64_init((vm_offset_t
) commPagePtr64
, _COMM_PAGE64_BASE_ADDRESS
,
529 _COMM_PAGE64_AREA_USED
/INTEL_PGBYTES
);
533 simple_lock_init(&commpage_active_cpus_lock
, 0);
535 commpage_update_active_cpus();
536 commpage_mach_approximate_time_init();
537 commpage_mach_continuous_time_init();
538 commpage_boottime_init();
539 rtc_nanotime_init_commpage();
540 commpage_update_kdebug_state();
542 commpage_update_atm_diagnostic_config(atm_get_diagnostic_config());
546 /* Fill in the common routines during kernel initialization.
547 * This is called before user-mode code is running.
549 void commpage_text_populate( void ){
550 commpage_descriptor
**rd
;
553 commPagePtr
= (char *) commpage_allocate(commpage_text32_map
, (vm_size_t
) _COMM_PAGE_TEXT_AREA_USED
, VM_PROT_READ
| VM_PROT_EXECUTE
);
554 commPageTextPtr32
= commPagePtr
;
556 char *cptr
= commPagePtr
;
558 for(; i
< _COMM_PAGE_TEXT_AREA_USED
; i
++){
562 commPageBaseOffset
= _COMM_PAGE_TEXT_START
;
563 for (rd
= commpage_32_routines
; *rd
!= NULL
; rd
++) {
564 commpage_stuff_routine(*rd
);
568 pmap_commpage32_init((vm_offset_t
) commPageTextPtr32
, _COMM_PAGE_TEXT_START
,
569 _COMM_PAGE_TEXT_AREA_USED
/INTEL_PGBYTES
);
572 if (_cpu_capabilities
& k64Bit
) {
574 commPagePtr
= (char *) commpage_allocate(commpage_text64_map
, (vm_size_t
) _COMM_PAGE_TEXT_AREA_USED
, VM_PROT_READ
| VM_PROT_EXECUTE
);
575 commPageTextPtr64
= commPagePtr
;
578 for(i
=0; i
<_COMM_PAGE_TEXT_AREA_USED
; i
++){
582 for (rd
= commpage_64_routines
; *rd
!=NULL
; rd
++) {
583 commpage_stuff_routine(*rd
);
587 pmap_commpage64_init((vm_offset_t
) commPageTextPtr64
, _COMM_PAGE_TEXT_START
,
588 _COMM_PAGE_TEXT_AREA_USED
/INTEL_PGBYTES
);
592 if (next
> _COMM_PAGE_TEXT_END
)
593 panic("commpage text overflow: next=0x%08x, commPagePtr=%p", next
, commPagePtr
);
597 /* Update commpage nanotime information.
599 * This routine must be serialized by some external means, ie a lock.
603 commpage_set_nanotime(
609 commpage_time_data
*p32
= time_data32
;
610 commpage_time_data
*p64
= time_data64
;
611 static uint32_t generation
= 0;
614 if (p32
== NULL
) /* have commpages been allocated yet? */
617 if ( generation
!= p32
->nt_generation
)
618 panic("nanotime trouble 1"); /* possibly not serialized */
619 if ( ns_base
< p32
->nt_ns_base
)
620 panic("nanotime trouble 2");
621 if ((shift
!= 0) && ((_cpu_capabilities
& kSlow
)==0) )
622 panic("nanotime trouble 3");
624 next_gen
= ++generation
;
626 next_gen
= ++generation
;
628 p32
->nt_generation
= 0; /* mark invalid, so commpage won't try to use it */
629 p64
->nt_generation
= 0;
631 p32
->nt_tsc_base
= tsc_base
;
632 p64
->nt_tsc_base
= tsc_base
;
634 p32
->nt_ns_base
= ns_base
;
635 p64
->nt_ns_base
= ns_base
;
637 p32
->nt_scale
= scale
;
638 p64
->nt_scale
= scale
;
640 p32
->nt_shift
= shift
;
641 p64
->nt_shift
= shift
;
643 p32
->nt_generation
= next_gen
; /* mark data as valid */
644 p64
->nt_generation
= next_gen
;
647 /* Update commpage gettimeofday() information. As with nanotime(), we interleave
648 * updates to the 32- and 64-bit commpage, in order to keep time more nearly in sync
649 * between the two environments.
651 * This routine must be serializeed by some external means, ie a lock.
655 commpage_set_timestamp(
660 uint64_t tick_per_sec
)
662 new_commpage_timeofday_data_t
*p32
= gtod_time_data32
;
663 new_commpage_timeofday_data_t
*p64
= gtod_time_data64
;
665 p32
->TimeStamp_tick
= 0x0ULL
;
666 p64
->TimeStamp_tick
= 0x0ULL
;
668 p32
->TimeStamp_sec
= sec
;
669 p64
->TimeStamp_sec
= sec
;
671 p32
->TimeStamp_frac
= frac
;
672 p64
->TimeStamp_frac
= frac
;
674 p32
->Ticks_scale
= scale
;
675 p64
->Ticks_scale
= scale
;
677 p32
->Ticks_per_sec
= tick_per_sec
;
678 p64
->Ticks_per_sec
= tick_per_sec
;
680 p32
->TimeStamp_tick
= abstime
;
681 p64
->TimeStamp_tick
= abstime
;
684 /* Update _COMM_PAGE_MEMORY_PRESSURE. Called periodically from vm's compute_memory_pressure() */
687 commpage_set_memory_pressure(
688 unsigned int pressure
)
695 cp
+= (_COMM_PAGE_MEMORY_PRESSURE
- _COMM_PAGE32_BASE_ADDRESS
);
696 ip
= (uint32_t*) (void *) cp
;
697 *ip
= (uint32_t) pressure
;
702 cp
+= (_COMM_PAGE_MEMORY_PRESSURE
- _COMM_PAGE32_START_ADDRESS
);
703 ip
= (uint32_t*) (void *) cp
;
704 *ip
= (uint32_t) pressure
;
710 /* Update _COMM_PAGE_SPIN_COUNT. We might want to reduce when running on a battery, etc. */
713 commpage_set_spin_count(
719 if (count
== 0) /* we test for 0 after decrement, not before */
724 cp
+= (_COMM_PAGE_SPIN_COUNT
- _COMM_PAGE32_BASE_ADDRESS
);
725 ip
= (uint32_t*) (void *) cp
;
726 *ip
= (uint32_t) count
;
731 cp
+= (_COMM_PAGE_SPIN_COUNT
- _COMM_PAGE32_START_ADDRESS
);
732 ip
= (uint32_t*) (void *) cp
;
733 *ip
= (uint32_t) count
;
738 /* Updated every time a logical CPU goes offline/online */
740 commpage_update_active_cpus(void)
743 volatile uint8_t *ip
;
745 /* At least 32-bit commpage must be initialized */
749 simple_lock(&commpage_active_cpus_lock
);
752 cp
+= (_COMM_PAGE_ACTIVE_CPUS
- _COMM_PAGE32_BASE_ADDRESS
);
753 ip
= (volatile uint8_t*) cp
;
754 *ip
= (uint8_t) processor_avail_count
;
758 cp
+= (_COMM_PAGE_ACTIVE_CPUS
- _COMM_PAGE32_START_ADDRESS
);
759 ip
= (volatile uint8_t*) cp
;
760 *ip
= (uint8_t) processor_avail_count
;
763 simple_unlock(&commpage_active_cpus_lock
);
767 * Update the commpage with current kdebug state. This currently has bits for
768 * global trace state, and typefilter enablement. It is likely additional state
769 * will be tracked in the future.
771 * INVARIANT: This value will always be 0 if global tracing is disabled. This
772 * allows simple guard tests of "if (*_COMM_PAGE_KDEBUG_ENABLE) { ... }"
775 commpage_update_kdebug_state(void)
777 volatile uint32_t *saved_data_ptr
;
782 cp
+= (_COMM_PAGE_KDEBUG_ENABLE
- _COMM_PAGE32_BASE_ADDRESS
);
783 saved_data_ptr
= (volatile uint32_t *)cp
;
784 *saved_data_ptr
= kdebug_commpage_state();
789 cp
+= (_COMM_PAGE_KDEBUG_ENABLE
- _COMM_PAGE32_START_ADDRESS
);
790 saved_data_ptr
= (volatile uint32_t *)cp
;
791 *saved_data_ptr
= kdebug_commpage_state();
795 /* Ditto for atm_diagnostic_config */
797 commpage_update_atm_diagnostic_config(uint32_t diagnostic_config
)
799 volatile uint32_t *saved_data_ptr
;
804 cp
+= (_COMM_PAGE_ATM_DIAGNOSTIC_CONFIG
- _COMM_PAGE32_BASE_ADDRESS
);
805 saved_data_ptr
= (volatile uint32_t *)cp
;
806 *saved_data_ptr
= diagnostic_config
;
811 cp
+= (_COMM_PAGE_ATM_DIAGNOSTIC_CONFIG
- _COMM_PAGE32_START_ADDRESS
);
812 saved_data_ptr
= (volatile uint32_t *)cp
;
813 *saved_data_ptr
= diagnostic_config
;
818 * update the commpage data for last known value of mach_absolute_time()
822 commpage_update_mach_approximate_time(uint64_t abstime
)
824 #ifdef CONFIG_MACH_APPROXIMATE_TIME
830 cp
+= (_COMM_PAGE_APPROX_TIME
- _COMM_PAGE32_BASE_ADDRESS
);
831 saved_data
= atomic_load_explicit((_Atomic
uint64_t *)(uintptr_t)cp
, memory_order_relaxed
);
832 if (saved_data
< abstime
) {
833 /* ignoring the success/fail return value assuming that
834 * if the value has been updated since we last read it,
835 * "someone" has a newer timestamp than us and ours is
837 atomic_compare_exchange_strong_explicit((_Atomic
uint64_t *)(uintptr_t)cp
,
838 &saved_data
, abstime
, memory_order_relaxed
, memory_order_relaxed
);
843 cp
+= (_COMM_PAGE_APPROX_TIME
- _COMM_PAGE32_START_ADDRESS
);
844 saved_data
= atomic_load_explicit((_Atomic
uint64_t *)(uintptr_t)cp
, memory_order_relaxed
);
845 if (saved_data
< abstime
) {
846 /* ignoring the success/fail return value assuming that
847 * if the value has been updated since we last read it,
848 * "someone" has a newer timestamp than us and ours is
850 atomic_compare_exchange_strong_explicit((_Atomic
uint64_t *)(uintptr_t)cp
,
851 &saved_data
, abstime
, memory_order_relaxed
, memory_order_relaxed
);
855 #pragma unused (abstime)
860 commpage_update_mach_continuous_time(uint64_t sleeptime
)
865 cp
+= (_COMM_PAGE_CONT_TIMEBASE
- _COMM_PAGE32_START_ADDRESS
);
866 *(uint64_t *)cp
= sleeptime
;
871 cp
+= (_COMM_PAGE_CONT_TIMEBASE
- _COMM_PAGE32_START_ADDRESS
);
872 *(uint64_t *)cp
= sleeptime
;
877 commpage_update_boottime(uint64_t boottime
)
882 cp
+= (_COMM_PAGE_BOOTTIME_USEC
- _COMM_PAGE32_START_ADDRESS
);
883 *(uint64_t *)cp
= boottime
;
888 cp
+= (_COMM_PAGE_BOOTTIME_USEC
- _COMM_PAGE32_START_ADDRESS
);
889 *(uint64_t *)cp
= boottime
;
894 extern user32_addr_t commpage_text32_location
;
895 extern user64_addr_t commpage_text64_location
;
897 /* Check to see if a given address is in the Preemption Free Zone (PFZ) */
900 commpage_is_in_pfz32(uint32_t addr32
)
902 if ( (addr32
>= (commpage_text32_location
+ _COMM_TEXT_PFZ_START_OFFSET
))
903 && (addr32
< (commpage_text32_location
+_COMM_TEXT_PFZ_END_OFFSET
))) {
911 commpage_is_in_pfz64(addr64_t addr64
)
913 if ( (addr64
>= (commpage_text64_location
+ _COMM_TEXT_PFZ_START_OFFSET
))
914 && (addr64
< (commpage_text64_location
+ _COMM_TEXT_PFZ_END_OFFSET
))) {