2 * Copyright (c) 2000-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 #include <i386/rtclock_asm.h>
31 #include <i386/proc_reg.h>
32 #include <i386/eflags.h>
34 #include <i386/postcode.h>
35 #include <i386/apic.h>
41 ** Entry - %rdi contains pointer to 64 bit structure.
43 ** Exit - 64 bit structure filled in.
46 ENTRY(ml_get_timebase)
58 * Convert between various timer units
60 * This code converts 64-bit time units to other units.
61 * For example, the TSC is converted to HPET units.
63 * Time is a 64-bit integer that is some number of ticks.
64 * Conversion is 64-bit fixed point number which is composed
65 * of a 32 bit integer and a 32 bit fraction.
67 * The time ticks are multiplied by the conversion factor. The
68 * calculations are done as a 128-bit value but both the high
69 * and low words are dropped. The high word is overflow and the
70 * low word is the fraction part of the result.
72 * We return a 64-bit value.
74 * Note that we can use this function to multiply 2 conversion factors.
75 * We do this in order to calculate the multiplier used to convert
76 * directly between any two units.
78 * uint64_t tmrCvt(uint64_t time, // %rdi
79 * uint64_t conversion) // %rsi
84 mulq %rsi /* result is %rdx:%rax */
85 shrdq $32,%rdx,%rax /* %rdx:%rax >>= 32 */
89 * void _rtc_nanotime_adjust(
90 * uint64_t tsc_base_delta, // %rdi
91 * rtc_nanotime_t *dst); // %rsi
93 ENTRY(_rtc_nanotime_adjust)
94 movl RNT_GENERATION(%rsi),%eax /* get current generation */
95 movl $0,RNT_GENERATION(%rsi) /* flag data as being updated */
96 addq %rdi,RNT_TSC_BASE(%rsi)
98 incl %eax /* next generation */
100 incl %eax /* skip 0, which is a flag */
101 1: movl %eax,RNT_GENERATION(%rsi) /* update generation */
106 * unint64_t _rtc_nanotime_read(rtc_nanotime_t *rntp, int slow);
108 * This is the same as the commpage nanotime routine, except that it uses the
109 * kernel internal "rtc_nanotime_info" data instead of the commpage data.
110 * These two copies of data are kept in sync by rtc_clock_napped().
112 * Warning! There is another copy of this code in osfmk/x86_64/idt64.s.
113 * These are kept in sync by both using the RTC_NANOTIME_READ() macro.
115 * There are two versions of this algorithm, for "slow" and "fast" processors.
116 * The more common "fast" algorithm is:
118 * ns = (((rdtsc - rnt_tsc_base)*rnt_tsc_scale) / 2**32) + rnt_ns_base;
120 * Of course, the divide by 2**32 is a nop. rnt_tsc_scale is a constant
121 * computed during initialization:
123 * rnt_tsc_scale = (10e9 * 2**32) / tscFreq;
125 * The "slow" algorithm uses long division:
127 * ns = (((rdtsc - rnt_tsc_base) * 10e9) / tscFreq) + rnt_ns_base;
129 * Since this routine is not synchronized and can be called in any context,
130 * we use a generation count to guard against seeing partially updated data.
131 * In addition, the _rtc_nanotime_store() routine zeroes the generation before
132 * updating the data, and stores the nonzero generation only after all fields
133 * have been stored. Because IA32 guarantees that stores by one processor
134 * must be seen in order by another, we can avoid using a lock. We spin while
135 * the generation is zero.
137 * unint64_t _rtc_nanotime_read(
138 * rtc_nanotime_t *rntp, // %rdi
142 ENTRY(_rtc_nanotime_read)
147 * Processor whose TSC frequency is faster than SLOW_TSC_THRESHOLD
149 PAL_RTC_NANOTIME_READ_FAST()
154 * Processor whose TSC frequency is not faster than SLOW_TSC_THRESHOLD
155 * But K64 doesn't support this...
163 1: String "_rtc_nanotime_read() - slow algorithm not supported"
166 Entry(call_continuation)
167 movq %rdi,%rcx /* get continuation */
168 movq %rsi,%rdi /* continuation param */
169 movq %rdx,%rsi /* wait result */
170 movq %gs:CPU_KERNEL_STACK,%rsp /* set the stack */
171 xorq %rbp,%rbp /* zero frame pointer */
172 call *%rcx /* call continuation */
173 movq %gs:CPU_ACTIVE_THREAD,%rdi
174 call EXT(thread_terminate)