]> git.saurik.com Git - apple/libc.git/blob - i386/sys/mach_absolute_time_asm.s
Libc-825.25.tar.gz
[apple/libc.git] / i386 / sys / mach_absolute_time_asm.s
1 /*
2 * Copyright (c) 2003-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 <sys/appleapiopts.h>
30 #include <machine/cpu_capabilities.h>
31 #include <platfunc.h>
32
33 #if defined(VARIANT_DYLD)
34 /* For dyld, we need to decide upon call whether to jump to fast or slow */
35 .globl _mach_absolute_time
36 .align 2, 0x90
37 _mach_absolute_time:
38 movl _COMM_PAGE_CPU_CAPABILITIES, %eax
39 andl $(kSlow), %eax
40 jnz PLATFUNC_VARIANT_NAME(mach_absolute_time, slow)
41 jmp PLATFUNC_VARIANT_NAME(mach_absolute_time, fast)
42 #endif
43
44 /* return mach_absolute_time in %edx:%eax */
45
46 PLATFUNC_FUNCTION_START(mach_absolute_time, fast, 32, 4)
47 .private_extern _mach_absolute_time_direct
48 _mach_absolute_time_direct:
49 pushl %ebp
50 movl %esp,%ebp
51 pushl %esi
52 pushl %ebx
53
54 0:
55 movl _COMM_PAGE_NT_GENERATION,%esi /* get generation (0 if being changed) */
56 testl %esi,%esi /* if being updated, loop until stable */
57 jz 0b
58
59 lfence
60 rdtsc /* get TSC in %edx:%eax */
61 lfence
62
63 subl _COMM_PAGE_NT_TSC_BASE,%eax
64 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
65
66 movl _COMM_PAGE_NT_SCALE,%ecx
67
68 movl %edx,%ebx
69 mull %ecx
70 movl %ebx,%eax
71 movl %edx,%ebx
72 mull %ecx
73 addl %ebx,%eax
74 adcl $0,%edx
75
76 addl _COMM_PAGE_NT_NS_BASE,%eax
77 adcl _COMM_PAGE_NT_NS_BASE+4,%edx
78
79 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
80 jne 0b /* yes, loop until stable */
81
82 popl %ebx
83 popl %esi
84 popl %ebp
85 ret
86 PLATFUNC_DESCRIPTOR(mach_absolute_time,fast,0,kSlow)
87
88
89 /* mach_absolute_time routine for machines slower than ~1Gz (SLOW_TSC_THRESHOLD) */
90 PLATFUNC_FUNCTION_START(mach_absolute_time, slow, 32, 4)
91 push %ebp
92 mov %esp,%ebp
93 push %esi
94 push %edi
95 push %ebx
96
97 0:
98 movl _COMM_PAGE_NT_GENERATION,%esi
99 testl %esi,%esi /* if generation is 0, data being changed */
100 jz 0b /* so loop until stable */
101
102 lfence
103 rdtsc /* get TSC in %edx:%eax */
104 lfence
105 subl _COMM_PAGE_NT_TSC_BASE,%eax
106 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
107
108 pushl %esi /* save generation */
109 /*
110 * Do the math to convert tsc ticks to nanoseconds. We first
111 * do long multiply of 1 billion times the tsc. Then we do
112 * long division by the tsc frequency
113 */
114 mov $1000000000, %ecx /* number of nanoseconds in a second */
115 mov %edx, %ebx
116 mul %ecx
117 mov %edx, %edi
118 mov %eax, %esi
119 mov %ebx, %eax
120 mul %ecx
121 add %edi, %eax
122 adc $0, %edx /* result in edx:eax:esi */
123 mov %eax, %edi
124 mov _COMM_PAGE_NT_SHIFT,%ecx /* overloaded as the low 32 tscFreq */
125 xor %eax, %eax
126 xchg %edx, %eax
127 div %ecx
128 xor %eax, %eax
129 mov %edi, %eax
130 div %ecx
131 mov %eax, %ebx
132 mov %esi, %eax
133 div %ecx
134 mov %ebx, %edx /* result in edx:eax */
135 popl %esi /* recover generation */
136
137 add _COMM_PAGE_NT_NS_BASE,%eax
138 adc _COMM_PAGE_NT_NS_BASE+4,%edx
139
140 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
141 jne 0b /* yes, loop until stable */
142
143 pop %ebx
144 pop %edi
145 pop %esi
146 pop %ebp
147 ret /* result in edx:eax */
148 PLATFUNC_DESCRIPTOR(mach_absolute_time,slow,kSlow,0)