]> git.saurik.com Git - apple/xnu.git/blame - osfmk/x86_64/machine_routines_asm.s
xnu-1456.1.26.tar.gz
[apple/xnu.git] / osfmk / x86_64 / machine_routines_asm.s
CommitLineData
b0d623f7
A
1/*
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <i386/asm.h>
30#include <i386/rtclock.h>
31#include <i386/proc_reg.h>
32#include <i386/eflags.h>
33
34#include <i386/postcode.h>
35#include <i386/apic.h>
36#include <assym.s>
37
38/*
39** ml_get_timebase()
40**
41** Entry - %rdi contains pointer to 64 bit structure.
42**
43** Exit - 64 bit structure filled in.
44**
45*/
46ENTRY(ml_get_timebase)
47
48 lfence
49 rdtsc
50 lfence
51 shlq $32,%rdx
52 orq %rdx,%rax
53 movq %rax, (%rdi)
54
55 ret
56
57/*
58 * Convert between various timer units
59 *
60 * This code converts 64-bit time units to other units.
61 * For example, the TSC is converted to HPET units.
62 *
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.
66 *
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.
71 *
72 * We return a 64-bit value.
73 *
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.
77 *
78 * uint64_t tmrCvt(uint64_t time, // %rdi
79 * uint64_t conversion) // %rsi
80 *
81 */
82ENTRY(tmrCvt)
83 movq %rdi,%rax
84 mulq %rsi /* result is %rdx:%rax */
85 shrdq $32,%rdx,%rax /* %rdx:%rax >>= 32 */
86 ret
87
88
89/*
90 * void _rtc_nanotime_store(
91 * uint64_t tsc, // %rdi
92 * uint64_t nsec, // %rsi
93 * uint32_t scale, // %rdx
94 * uint32_t shift, // %rcx
95 * rtc_nanotime_t *dst); // %r8
96 */
97ENTRY(_rtc_nanotime_store)
98 movl RNT_GENERATION(%r8),%eax /* get current generation */
99 movl $0,RNT_GENERATION(%r8) /* flag data as being updated */
100 movq %rdi,RNT_TSC_BASE(%r8)
101 movq %rsi,RNT_NS_BASE(%r8)
102 movl %edx,RNT_SCALE(%r8)
103 movl %ecx,RNT_SHIFT(%r8)
104
105 incl %eax /* next generation */
106 jnz 1f
107 incl %eax /* skip 0, which is a flag */
1081: movl %eax,RNT_GENERATION(%r8) /* update generation */
109
110 ret
111
112/*
113 * unint64_t _rtc_nanotime_read(rtc_nanotime_t *rntp, int slow);
114 *
115 * This is the same as the commpage nanotime routine, except that it uses the
116 * kernel internal "rtc_nanotime_info" data instead of the commpage data.
117 * These two copies of data are kept in sync by rtc_clock_napped().
118 *
119 * Warning! There is another copy of this code in osfmk/x86_64/idt64.s.
120 * These are kept in sync by both using the RTC_NANOTIME_READ() macro.
121 *
122 * There are two versions of this algorithm, for "slow" and "fast" processors.
123 * The more common "fast" algorithm is:
124 *
125 * ns = (((rdtsc - rnt_tsc_base)*rnt_tsc_scale) / 2**32) + rnt_ns_base;
126 *
127 * Of course, the divide by 2**32 is a nop. rnt_tsc_scale is a constant
128 * computed during initialization:
129 *
130 * rnt_tsc_scale = (10e9 * 2**32) / tscFreq;
131 *
132 * The "slow" algorithm uses long division:
133 *
134 * ns = (((rdtsc - rnt_tsc_base) * 10e9) / tscFreq) + rnt_ns_base;
135 *
136 * Since this routine is not synchronized and can be called in any context,
137 * we use a generation count to guard against seeing partially updated data.
138 * In addition, the _rtc_nanotime_store() routine zeroes the generation before
139 * updating the data, and stores the nonzero generation only after all fields
140 * have been stored. Because IA32 guarantees that stores by one processor
141 * must be seen in order by another, we can avoid using a lock. We spin while
142 * the generation is zero.
143 *
144 * unint64_t _rtc_nanotime_read(
145 * rtc_nanotime_t *rntp, // %rdi
146 * int slow); // %rsi
147 *
148 */
149ENTRY(_rtc_nanotime_read)
150 test %rsi,%rsi
151 jnz Lslow
152
153 /*
154 * Processor whose TSC frequency is faster than SLOW_TSC_THRESHOLD
155 */
156 RTC_NANOTIME_READ_FAST()
157
158 ret
159
160 /*
161 * Processor whose TSC frequency is not faster than SLOW_TSC_THRESHOLD
162 * But K64 doesn't support this...
163 */
164Lslow:
165 lea 1f(%rip),%rdi
166 xorb %al,%al
167 call EXT(panic)
168 hlt
169 .data
1701: String "_rtc_nanotime_read() - slow algorithm not supported"
171