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>
34 /* return mach_absolute_time in %edx:%eax
36 * The algorithm we use is:
38 * ns = ((((rdtsc - rnt_tsc_base)<<rnt_shift)*rnt_tsc_scale) / 2**32) + rnt_ns_base;
40 * rnt_shift, a constant computed during initialization, is the smallest value for which:
42 * (tscFreq << rnt_shift) > SLOW_TSC_THRESHOLD
44 * Where SLOW_TSC_THRESHOLD is about 10e9. Since most processor's tscFreq is greater
45 * than 1GHz, rnt_shift is usually 0. rnt_tsc_scale is also a 32-bit constant:
47 * rnt_tsc_scale = (10e9 * 2**32) / (tscFreq << rnt_shift);
50 .globl _mach_absolute_time
58 movl _COMM_PAGE_NT_GENERATION,%esi /* get generation (0 if being changed) */
59 testl %esi,%esi /* if being updated, loop until stable */
63 rdtsc /* get TSC in %edx:%eax */
66 subl _COMM_PAGE_NT_TSC_BASE,%eax
67 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
70 * Prior to supporting "slow" processors, xnu always set _NT_SHIFT to 32.
71 * Now it defaults to 0, unless the processor is slow. The shifts
72 * below implicitly mask the count down to 5 bits, handling either default.
74 movl _COMM_PAGE_NT_SHIFT,%ecx
75 shldl %cl,%eax,%edx /* shift %edx left, filling in from %eax */
76 shll %cl,%eax /* finish shifting %edx:%eax left by _COMM_PAGE_NT_SHIFT bits */
78 movl _COMM_PAGE_NT_SCALE,%ecx
88 addl _COMM_PAGE_NT_NS_BASE,%eax
89 adcl _COMM_PAGE_NT_NS_BASE+4,%edx
91 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
92 jne 0b /* yes, loop until stable */
99 #elif defined(__x86_64__)
102 * 64-bit version _mach_absolute_time. We return the 64-bit nanotime in %rax.
104 * The algorithm we use is:
106 * ns = ((((rdtsc - rnt_tsc_base)<<rnt_shift)*rnt_tsc_scale) / 2**32) + rnt_ns_base;
108 * rnt_shift, a constant computed during initialization, is the smallest value for which:
110 * tscFreq << rnt_shift) > SLOW_TSC_THRESHOLD
112 * Where SLOW_TSC_THRESHOLD is about 10e9. Since most processor's tscFreqs are greater
113 * than 1GHz, rnt_shift is usually 0. rnt_tsc_scale is also a 32-bit constant:
115 * rnt_tsc_scale = (10e9 * 2**32) / (tscFreq << rnt_shift);
118 .globl _mach_absolute_time
120 pushq %rbp // set up a frame for backtraces
122 movq $(_COMM_PAGE_TIME_DATA_START),%rsi
124 movl _NT_GENERATION(%rsi),%r8d // get generation
125 testl %r8d,%r8d // if 0, data is being changed...
126 jz 1b // ...so loop until stable
128 rdtsc // edx:eax := tsc
130 shlq $32,%rdx // rax := ((edx << 32) | eax), ie 64-bit tsc
134 * Prior to supporting "slow" processors, xnu always set _NT_SHIFT to 32.
135 * Now it defaults to 0, unless the processor is slow. In order to maintain
136 * compatibility with both old and new versions of xnu, we mask the shift
137 * down to 0x1F, which maps the old default (32) into the new default (0).
139 movl _NT_SHIFT(%rsi),%ecx
140 andl $0x1F,%ecx // *** remove this line once 10.9 is GM ***
141 subq _NT_TSC_BASE(%rsi), %rax // rax := (tsc - base_tsc)
142 shlq %cl,%rax // rax := (tsc - base_tsc) << NT_SHIFT
143 movl _NT_SCALE(%rsi),%ecx
144 mulq %rcx // rdx:rax := ((tsc - base_tsc)<<shift) * scale
145 shrdq $32,%rdx,%rax // divide by 2**32
146 addq _NT_NS_BASE(%rsi),%rax // (((tsc - base_tsc) * scale) >> 32) + ns_base
148 cmpl _NT_GENERATION(%rsi),%r8d // did the data change during computation?
154 #error Unsupported architecture