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>
31 #include <machine/commpage.h>
43 COMMPAGE_DESCRIPTOR(mach_absolute_time,_COMM_PAGE_ABSOLUTE_TIME,0,0)
46 /* return nanotime in %edx:%eax */
55 movl _COMM_PAGE_NT_GENERATION,%esi /* get generation (0 if being changed) */
56 testl %esi,%esi /* if being updated, loop until stable */
59 rdtsc /* get TSC in %edx:%eax */
60 subl _COMM_PAGE_NT_TSC_BASE,%eax
61 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
63 movl _COMM_PAGE_NT_SCALE,%ecx
73 addl _COMM_PAGE_NT_NS_BASE,%eax
74 adcl _COMM_PAGE_NT_NS_BASE+4,%edx
76 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
77 jne 0b /* yes, loop until stable */
84 COMMPAGE_DESCRIPTOR(nanotime,_COMM_PAGE_NANOTIME,0,kSlow)
87 /* nanotime routine for machines slower than ~1Gz (SLOW_TSC_THRESHOLD) */
96 movl _COMM_PAGE_NT_GENERATION,%esi
97 testl %esi,%esi /* if generation is 0, data being changed */
98 jz 0b /* so loop until stable */
100 rdtsc /* get TSC in %edx:%eax */
101 subl _COMM_PAGE_NT_TSC_BASE,%eax
102 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
104 pushl %esi /* save generation */
106 * Do the math to convert tsc ticks to nanoseconds. We first
107 * do long multiply of 1 billion times the tsc. Then we do
108 * long division by the tsc frequency
110 mov $1000000000, %ecx /* number of nanoseconds in a second */
118 adc $0, %edx /* result in edx:eax:esi */
120 mov _COMM_PAGE_NT_SHIFT,%ecx /* overloaded as the low 32 tscFreq */
130 mov %ebx, %edx /* result in edx:eax */
131 popl %esi /* recover generation */
133 add _COMM_PAGE_NT_NS_BASE,%eax
134 adc _COMM_PAGE_NT_NS_BASE+4,%edx
136 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
137 jne 0b /* yes, loop until stable */
143 ret /* result in edx:eax */
145 COMMPAGE_DESCRIPTOR(nanotime_slow,_COMM_PAGE_NANOTIME,kSlow,0)
148 /* The 64-bit version. We return the 64-bit nanotime in %rax,
149 * and by convention we must preserve %r9, %r10, and %r11.
154 Lnanotime_64: // NB: must preserve r9, r10, and r11
155 pushq %rbp // set up a frame for backtraces
157 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_TIME_DATA_START),%rsi
159 movl _NT_GENERATION(%rsi),%r8d // get generation
160 testl %r8d,%r8d // if 0, data is being changed...
161 jz 1b // ...so loop until stable
162 rdtsc // edx:eax := tsc
163 shlq $32,%rdx // rax := ((edx << 32) | eax), ie 64-bit tsc
165 subq _NT_TSC_BASE(%rsi), %rax // rax := (tsc - base_tsc)
166 movl _NT_SCALE(%rsi),%ecx
167 mulq %rcx // rdx:rax := (tsc - base_tsc) * scale
168 shrdq $32,%rdx,%rax // _COMM_PAGE_NT_SHIFT is always 32
169 addq _NT_NS_BASE(%rsi),%rax // (((tsc - base_tsc) * scale) >> 32) + ns_base
171 cmpl _NT_GENERATION(%rsi),%r8d // did the data change during computation?
176 COMMPAGE_DESCRIPTOR(nanotime_64,_COMM_PAGE_NANOTIME,0,kSlow)