]>
git.saurik.com Git - apple/xnu.git/blob - libsyscall/wrappers/mach_continuous_time.c
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/types.h>
26 #include <machine/cpu_capabilities.h>
27 #include <mach/mach_time.h>
29 __attribute__((visibility("hidden")))
31 _mach_continuous_time_base(void)
33 #if !defined(__x86_64__) && !defined(__arm64__)
34 // Deal with the lack of 64-bit loads on arm32 (see mach_approximate_time.s)
36 volatile uint64_t *base_ptr
= (volatile uint64_t*)_COMM_PAGE_CONT_TIMEBASE
;
37 uint64_t read1
, read2
;
40 __asm__
volatile("lfence" ::: "memory");
42 #error "unsupported arch"
46 if(__builtin_expect((read1
== read2
), 1))
50 return *(volatile uint64_t*)_COMM_PAGE_CONT_TIMEBASE
;
55 __attribute__((visibility("hidden")))
57 _mach_continuous_time(uint64_t* absolute_time
, uint64_t* cont_time
)
59 volatile uint64_t *base_ptr
= (volatile uint64_t*)_COMM_PAGE_CONT_TIMEBASE
;
60 volatile uint64_t read1
, read2
;
61 volatile uint64_t absolute
;
65 absolute
= mach_absolute_time();
67 } while (__builtin_expect((read1
!= read2
), 0));
69 if (absolute_time
) *absolute_time
= absolute
;
70 if (cont_time
) *cont_time
= absolute
+ read1
;
76 mach_continuous_time(void)
79 _mach_continuous_time(NULL
, &cont_time
);
84 mach_continuous_approximate_time(void)
87 * No retry loop here because if we use a slightly too old timebase that's
88 * okay, we are approximate time anyway.
90 volatile register uint64_t time_base
= _mach_continuous_time_base();
91 return time_base
+ mach_approximate_time();