2 * Copyright (c) 2003-2007 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@
29 #include <sys/appleapiopts.h>
30 #include <machine/cpu_capabilities.h>
33 #if defined(VARIANT_DYLD)
34 /* For dyld, we need to decide upon call whether to jump to fast or slow */
35 .globl _mach_absolute_time
38 movl _COMM_PAGE_CPU_CAPABILITIES, %eax
40 jnz PLATFUNC_VARIANT_NAME(mach_absolute_time, slow)
41 jmp PLATFUNC_VARIANT_NAME(mach_absolute_time, fast)
44 /* return mach_absolute_time in %edx:%eax */
46 PLATFUNC_FUNCTION_START(mach_absolute_time, fast, 32, 4)
47 .private_extern _mach_absolute_time_direct
48 _mach_absolute_time_direct:
55 movl _COMM_PAGE_NT_GENERATION,%esi /* get generation (0 if being changed) */
56 testl %esi,%esi /* if being updated, loop until stable */
60 rdtsc /* get TSC in %edx:%eax */
63 subl _COMM_PAGE_NT_TSC_BASE,%eax
64 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
66 movl _COMM_PAGE_NT_SCALE,%ecx
76 addl _COMM_PAGE_NT_NS_BASE,%eax
77 adcl _COMM_PAGE_NT_NS_BASE+4,%edx
79 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
80 jne 0b /* yes, loop until stable */
86 PLATFUNC_DESCRIPTOR(mach_absolute_time,fast,0,kSlow)
89 /* mach_absolute_time routine for machines slower than ~1Gz (SLOW_TSC_THRESHOLD) */
90 PLATFUNC_FUNCTION_START(mach_absolute_time, slow, 32, 4)
98 movl _COMM_PAGE_NT_GENERATION,%esi
99 testl %esi,%esi /* if generation is 0, data being changed */
100 jz 0b /* so loop until stable */
103 rdtsc /* get TSC in %edx:%eax */
105 subl _COMM_PAGE_NT_TSC_BASE,%eax
106 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
108 pushl %esi /* save generation */
110 * Do the math to convert tsc ticks to nanoseconds. We first
111 * do long multiply of 1 billion times the tsc. Then we do
112 * long division by the tsc frequency
114 mov $1000000000, %ecx /* number of nanoseconds in a second */
122 adc $0, %edx /* result in edx:eax:esi */
124 mov _COMM_PAGE_NT_SHIFT,%ecx /* overloaded as the low 32 tscFreq */
134 mov %ebx, %edx /* result in edx:eax */
135 popl %esi /* recover generation */
137 add _COMM_PAGE_NT_NS_BASE,%eax
138 adc _COMM_PAGE_NT_NS_BASE+4,%edx
140 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
141 jne 0b /* yes, loop until stable */
147 ret /* result in edx:eax */
148 PLATFUNC_DESCRIPTOR(mach_absolute_time,slow,kSlow,0)