]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/commpage/commpage_mach_absolute_time.s
3ea04c5f1f170f7f9d96d39e7f23f06ee4da2f2d
[apple/xnu.git] / osfmk / i386 / commpage / commpage_mach_absolute_time.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 <machine/commpage.h>
32 #include <i386/asm.h>
33
34 #include <assym.s>
35
36 .text
37 .align 2, 0x90
38
39 Lmach_absolute_time:
40 int $0x3
41 ret
42
43 COMMPAGE_DESCRIPTOR(mach_absolute_time,_COMM_PAGE_ABSOLUTE_TIME,0,0)
44
45
46 /* return nanotime in %edx:%eax */
47
48 Lnanotime:
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 rdtsc /* get TSC in %edx:%eax */
60 subl _COMM_PAGE_NT_TSC_BASE,%eax
61 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
62
63 movl _COMM_PAGE_NT_SCALE,%ecx
64
65 movl %edx,%ebx
66 mull %ecx
67 movl %ebx,%eax
68 movl %edx,%ebx
69 mull %ecx
70 addl %ebx,%eax
71 adcl $0,%edx
72
73 addl _COMM_PAGE_NT_NS_BASE,%eax
74 adcl _COMM_PAGE_NT_NS_BASE+4,%edx
75
76 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
77 jne 0b /* yes, loop until stable */
78
79 popl %ebx
80 popl %esi
81 popl %ebp
82 ret
83
84 COMMPAGE_DESCRIPTOR(nanotime,_COMM_PAGE_NANOTIME,0,kSlow)
85
86
87 /* nanotime routine for machines slower than ~1Gz (SLOW_TSC_THRESHOLD) */
88 Lnanotime_slow:
89 push %ebp
90 mov %esp,%ebp
91 push %esi
92 push %edi
93 push %ebx
94
95 0:
96 movl _COMM_PAGE_NT_GENERATION,%esi
97 testl %esi,%esi /* if generation is 0, data being changed */
98 jz 0b /* so loop until stable */
99
100 rdtsc /* get TSC in %edx:%eax */
101 subl _COMM_PAGE_NT_TSC_BASE,%eax
102 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
103
104 pushl %esi /* save generation */
105 /*
106 * Do the math to convert tsc ticks to nanoseconds. We first
107 * do long multiply of 1 billion times the tsc. Then we do
108 * long division by the tsc frequency
109 */
110 mov $1000000000, %ecx /* number of nanoseconds in a second */
111 mov %edx, %ebx
112 mul %ecx
113 mov %edx, %edi
114 mov %eax, %esi
115 mov %ebx, %eax
116 mul %ecx
117 add %edi, %eax
118 adc $0, %edx /* result in edx:eax:esi */
119 mov %eax, %edi
120 mov _COMM_PAGE_NT_SHIFT,%ecx /* overloaded as the low 32 tscFreq */
121 xor %eax, %eax
122 xchg %edx, %eax
123 div %ecx
124 xor %eax, %eax
125 mov %edi, %eax
126 div %ecx
127 mov %eax, %ebx
128 mov %esi, %eax
129 div %ecx
130 mov %ebx, %edx /* result in edx:eax */
131 popl %esi /* recover generation */
132
133 add _COMM_PAGE_NT_NS_BASE,%eax
134 adc _COMM_PAGE_NT_NS_BASE+4,%edx
135
136 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
137 jne 0b /* yes, loop until stable */
138
139 pop %ebx
140 pop %edi
141 pop %esi
142 pop %ebp
143 ret /* result in edx:eax */
144
145 COMMPAGE_DESCRIPTOR(nanotime_slow,_COMM_PAGE_NANOTIME,kSlow,0)
146
147
148 /* The 64-bit version. We return the 64-bit nanotime in %rax,
149 * and by convention we must preserve %r9, %r10, and %r11.
150 */
151 .text
152 .align 2
153 .code64
154 Lnanotime_64: // NB: must preserve r9, r10, and r11
155 pushq %rbp // set up a frame for backtraces
156 movq %rsp,%rbp
157 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_TIME_DATA_START),%rsi
158 1:
159 movl _NT_GENERATION(%rsi),%r8d // get generation
160 testl %r8d,%r8d // if 0, data is being changed...
161 jz 1b // ...so loop until stable
162 rdtsc // edx:eax := tsc
163 shlq $32,%rdx // rax := ((edx << 32) | eax), ie 64-bit tsc
164 orq %rdx,%rax
165 subq _NT_TSC_BASE(%rsi), %rax // rax := (tsc - base_tsc)
166 movl _NT_SCALE(%rsi),%ecx
167 mulq %rcx // rdx:rax := (tsc - base_tsc) * scale
168 shrdq $32,%rdx,%rax // _COMM_PAGE_NT_SHIFT is always 32
169 addq _NT_NS_BASE(%rsi),%rax // (((tsc - base_tsc) * scale) >> 32) + ns_base
170
171 cmpl _NT_GENERATION(%rsi),%r8d // did the data change during computation?
172 jne 1b
173 popq %rbp
174 ret
175
176 COMMPAGE_DESCRIPTOR(nanotime_64,_COMM_PAGE_NANOTIME,0,kSlow)