]> git.saurik.com Git - apple/xnu.git/blame - osfmk/x86_64/machine_routines_asm.s
xnu-1699.22.73.tar.gz
[apple/xnu.git] / osfmk / x86_64 / machine_routines_asm.s
CommitLineData
b0d623f7 1/*
0b4c1975 2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
b0d623f7
A
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>
6d2010ae 30#include <i386/rtclock_asm.h>
b0d623f7
A
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
6d2010ae 88 /*
0b4c1975
A
89 * void _rtc_nanotime_adjust(
90 * uint64_t tsc_base_delta, // %rdi
91 * rtc_nanotime_t *dst); // %rsi
92 */
93ENTRY(_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)
97
98 incl %eax /* next generation */
99 jnz 1f
100 incl %eax /* skip 0, which is a flag */
1011: movl %eax,RNT_GENERATION(%rsi) /* update generation */
102
103 ret
104
b0d623f7
A
105/*
106 * unint64_t _rtc_nanotime_read(rtc_nanotime_t *rntp, int slow);
107 *
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().
111 *
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.
114 *
115 * There are two versions of this algorithm, for "slow" and "fast" processors.
116 * The more common "fast" algorithm is:
117 *
118 * ns = (((rdtsc - rnt_tsc_base)*rnt_tsc_scale) / 2**32) + rnt_ns_base;
119 *
120 * Of course, the divide by 2**32 is a nop. rnt_tsc_scale is a constant
121 * computed during initialization:
122 *
123 * rnt_tsc_scale = (10e9 * 2**32) / tscFreq;
124 *
125 * The "slow" algorithm uses long division:
126 *
127 * ns = (((rdtsc - rnt_tsc_base) * 10e9) / tscFreq) + rnt_ns_base;
128 *
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.
136 *
137 * unint64_t _rtc_nanotime_read(
138 * rtc_nanotime_t *rntp, // %rdi
139 * int slow); // %rsi
140 *
141 */
142ENTRY(_rtc_nanotime_read)
143 test %rsi,%rsi
144 jnz Lslow
145
146 /*
147 * Processor whose TSC frequency is faster than SLOW_TSC_THRESHOLD
148 */
6d2010ae 149 PAL_RTC_NANOTIME_READ_FAST()
b0d623f7
A
150
151 ret
152
153 /*
154 * Processor whose TSC frequency is not faster than SLOW_TSC_THRESHOLD
155 * But K64 doesn't support this...
156 */
157Lslow:
158 lea 1f(%rip),%rdi
159 xorb %al,%al
160 call EXT(panic)
161 hlt
162 .data
1631: String "_rtc_nanotime_read() - slow algorithm not supported"
164
6d2010ae
A
165
166Entry(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)
175