]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/machine_routines_asm.s
f68b813768f7dee0ad029763b78baaa603fdf16c
[apple/xnu.git] / osfmk / i386 / machine_routines_asm.s
1 /*
2 * Copyright (c) 2000-2007 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 - %esp contains pointer to 64 bit structure.
42 **
43 ** Exit - 64 bit structure filled in.
44 **
45 */
46 ENTRY(ml_get_timebase)
47
48 movl S_ARG0, %ecx
49
50 rdtsc
51 lfence
52
53 movl %edx, 0(%ecx)
54 movl %eax, 4(%ecx)
55
56 ret
57
58 /*
59 * Convert between various timer units
60 *
61 * uint64_t tmrCvt(uint64_t time, uint64_t *conversion)
62 *
63 * This code converts 64-bit time units to other units.
64 * For example, the TSC is converted to HPET units.
65 *
66 * Time is a 64-bit integer that is some number of ticks.
67 * Conversion is 64-bit fixed point number which is composed
68 * of a 32 bit integer and a 32 bit fraction.
69 *
70 * The time ticks are multiplied by the conversion factor. The
71 * calculations are done as a 128-bit value but both the high
72 * and low words are dropped. The high word is overflow and the
73 * low word is the fraction part of the result.
74 *
75 * We return a 64-bit value.
76 *
77 * Note that we can use this function to multiply 2 conversion factors.
78 * We do this in order to calculate the multiplier used to convert
79 * directly between any two units.
80 *
81 */
82
83 .globl EXT(tmrCvt)
84 .align FALIGN
85
86 LEXT(tmrCvt)
87
88 pushl %ebp // Save a volatile
89 movl %esp,%ebp // Get the parameters - 8
90 pushl %ebx // Save a volatile
91 pushl %esi // Save a volatile
92 pushl %edi // Save a volatile
93
94 // %ebp + 8 - low-order ts
95 // %ebp + 12 - high-order ts
96 // %ebp + 16 - low-order cvt
97 // %ebp + 20 - high-order cvt
98
99 movl 8(%ebp),%eax // Get low-order ts
100 mull 16(%ebp) // Multiply by low-order conversion
101 movl %edx,%edi // Need to save only the high order part
102
103 movl 12(%ebp),%eax // Get the high-order ts
104 mull 16(%ebp) // Multiply by low-order conversion
105 addl %eax,%edi // Add in the overflow from the low x low calculation
106 adcl $0,%edx // Add in any overflow to high high part
107 movl %edx,%esi // Save high high part
108
109 // We now have the upper 64 bits of the 96 bit multiply of ts and the low half of cvt
110 // in %esi:%edi
111
112 movl 8(%ebp),%eax // Get low-order ts
113 mull 20(%ebp) // Multiply by high-order conversion
114 movl %eax,%ebx // Need to save the low order part
115 movl %edx,%ecx // Need to save the high order part
116
117 movl 12(%ebp),%eax // Get the high-order ts
118 mull 20(%ebp) // Multiply by high-order conversion
119
120 // Now have %ecx:%ebx as low part of high low and %edx:%eax as high part of high high
121 // We don't care about the highest word since it is overflow
122
123 addl %edi,%ebx // Add the low words
124 adcl %ecx,%esi // Add in the high plus carry from low
125 addl %eax,%esi // Add in the rest of the high
126
127 movl %ebx,%eax // Pass back low word
128 movl %esi,%edx // and the high word
129
130 popl %edi // Restore a volatile
131 popl %esi // Restore a volatile
132 popl %ebx // Restore a volatile
133 popl %ebp // Restore a volatile
134
135 ret // Leave...
136
137 .globl EXT(_rtc_nanotime_store)
138 .align FALIGN
139
140 LEXT(_rtc_nanotime_store)
141 push %ebp
142 movl %esp,%ebp
143 push %esi
144
145 mov 32(%ebp),%edx /* get ptr to rtc_nanotime_info */
146
147 movl RNT_GENERATION(%edx),%esi /* get current generation */
148 movl $0,RNT_GENERATION(%edx) /* flag data as being updated */
149
150 mov 8(%ebp),%eax
151 mov %eax,RNT_TSC_BASE(%edx)
152 mov 12(%ebp),%eax
153 mov %eax,RNT_TSC_BASE+4(%edx)
154
155 mov 24(%ebp),%eax
156 mov %eax,RNT_SCALE(%edx)
157
158 mov 28(%ebp),%eax
159 mov %eax,RNT_SHIFT(%edx)
160
161 mov 16(%ebp),%eax
162 mov %eax,RNT_NS_BASE(%edx)
163 mov 20(%ebp),%eax
164 mov %eax,RNT_NS_BASE+4(%edx)
165
166 incl %esi /* next generation */
167 jnz 1f
168 incl %esi /* skip 0, which is a flag */
169 1: movl %esi,RNT_GENERATION(%edx) /* update generation and make usable */
170
171 pop %esi
172 pop %ebp
173 ret
174
175
176 /* unint64_t _rtc_nanotime_read( rtc_nanotime_t *rntp, int slow );
177 *
178 * This is the same as the commpage nanotime routine, except that it uses the
179 * kernel internal "rtc_nanotime_info" data instead of the commpage data. The two copies
180 * of data (one in the kernel and one in user space) are kept in sync by rtc_clock_napped().
181 *
182 * Warning! There is another copy of this code in osfmk/i386/locore.s. The
183 * two versions must be kept in sync with each other!
184 *
185 * There are actually two versions of the algorithm, one each for "slow" and "fast"
186 * processors. The more common "fast" algorithm is:
187 *
188 * nanoseconds = (((rdtsc - rnt_tsc_base) * rnt_tsc_scale) / 2**32) - rnt_ns_base;
189 *
190 * Of course, the divide by 2**32 is a nop. rnt_tsc_scale is a constant computed during initialization:
191 *
192 * rnt_tsc_scale = (10e9 * 2**32) / tscFreq;
193 *
194 * The "slow" algorithm uses long division:
195 *
196 * nanoseconds = (((rdtsc - rnt_tsc_base) * 10e9) / tscFreq) - rnt_ns_base;
197 *
198 * Since this routine is not synchronized and can be called in any context,
199 * we use a generation count to guard against seeing partially updated data. In addition,
200 * the _rtc_nanotime_store() routine -- just above -- zeroes the generation before
201 * updating the data, and stores the nonzero generation only after all other data has been
202 * stored. Because IA32 guarantees that stores by one processor must be seen in order
203 * by another, we can avoid using a lock. We spin while the generation is zero.
204 *
205 * In accordance with the ABI, we return the 64-bit nanotime in %edx:%eax.
206 */
207
208 .globl EXT(_rtc_nanotime_read)
209 .align FALIGN
210 LEXT(_rtc_nanotime_read)
211 pushl %ebp
212 movl %esp,%ebp
213 pushl %esi
214 pushl %edi
215 pushl %ebx
216 movl 8(%ebp),%edi /* get ptr to rtc_nanotime_info */
217 movl 12(%ebp),%eax /* get "slow" flag */
218 testl %eax,%eax
219 jnz Lslow
220
221 /* Processor whose TSC frequency is faster than SLOW_TSC_THRESHOLD */
222 RTC_NANOTIME_READ_FAST()
223
224 popl %ebx
225 popl %edi
226 popl %esi
227 popl %ebp
228 ret
229
230 /* Processor whose TSC frequency is slower than or equal to SLOW_TSC_THRESHOLD */
231 Lslow:
232 movl RNT_GENERATION(%edi),%esi /* get generation (0 if being changed) */
233 testl %esi,%esi /* if being changed, loop until stable */
234 jz Lslow
235 pushl %esi /* save generation */
236 pushl RNT_SHIFT(%edi) /* save low 32 bits of tscFreq */
237
238 rdtsc /* get TSC in %edx:%eax */
239 subl RNT_TSC_BASE(%edi),%eax
240 sbbl RNT_TSC_BASE+4(%edi),%edx
241
242 /*
243 * Do the math to convert tsc ticks to nanoseconds. We first
244 * do long multiply of 1 billion times the tsc. Then we do
245 * long division by the tsc frequency
246 */
247 mov $1000000000, %ecx /* number of nanoseconds in a second */
248 mov %edx, %ebx
249 mul %ecx
250 mov %edx, %edi
251 mov %eax, %esi
252 mov %ebx, %eax
253 mul %ecx
254 add %edi, %eax
255 adc $0, %edx /* result in edx:eax:esi */
256 mov %eax, %edi
257 popl %ecx /* get low 32 tscFreq */
258 xor %eax, %eax
259 xchg %edx, %eax
260 div %ecx
261 xor %eax, %eax
262 mov %edi, %eax
263 div %ecx
264 mov %eax, %ebx
265 mov %esi, %eax
266 div %ecx
267 mov %ebx, %edx /* result in edx:eax */
268
269 movl 8(%ebp),%edi /* recover ptr to rtc_nanotime_info */
270 popl %esi /* recover generation */
271
272 addl RNT_NS_BASE(%edi),%eax
273 adcl RNT_NS_BASE+4(%edi),%edx
274
275 cmpl RNT_GENERATION(%edi),%esi /* have the parameters changed? */
276 jne Lslow /* yes, loop until stable */
277
278 pop %ebx
279 pop %edi
280 pop %esi
281 pop %ebp
282 ret /* result in edx:eax */
283