]>
Commit | Line | Data |
---|---|---|
de355530 | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2003-2007 Apple Inc. All rights reserved. |
de355530 | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
de355530 | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
de355530 | 27 | */ |
de355530 | 28 | |
55e303ae A |
29 | #include <sys/appleapiopts.h> |
30 | #include <machine/cpu_capabilities.h> | |
31 | #include <machine/commpage.h> | |
91447636 A |
32 | #include <i386/asm.h> |
33 | ||
34 | #include <assym.s> | |
55e303ae A |
35 | |
36 | .text | |
37 | .align 2, 0x90 | |
38 | ||
39 | Lmach_absolute_time: | |
40 | int $0x3 | |
41 | ret | |
42 | ||
0c530ab8 | 43 | COMMPAGE_DESCRIPTOR(mach_absolute_time,_COMM_PAGE_ABSOLUTE_TIME,0,0) |
5d5c5d0d | 44 | |
0c530ab8 A |
45 | |
46 | /* return nanotime in %edx:%eax */ | |
2d21ac55 | 47 | |
6601e61a | 48 | Lnanotime: |
2d21ac55 A |
49 | pushl %ebp |
50 | movl %esp,%ebp | |
51 | pushl %esi | |
52 | pushl %ebx | |
0c530ab8 A |
53 | |
54 | 0: | |
2d21ac55 A |
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 | |
0c530ab8 | 58 | |
2d21ac55 A |
59 | rdtsc /* get TSC in %edx:%eax */ |
60 | subl _COMM_PAGE_NT_TSC_BASE,%eax | |
61 | sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx | |
0c530ab8 | 62 | |
2d21ac55 | 63 | movl _COMM_PAGE_NT_SCALE,%ecx |
0c530ab8 | 64 | |
2d21ac55 | 65 | movl %edx,%ebx |
0c530ab8 | 66 | mull %ecx |
2d21ac55 A |
67 | movl %ebx,%eax |
68 | movl %edx,%ebx | |
0c530ab8 | 69 | mull %ecx |
2d21ac55 A |
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 */ | |
0c530ab8 A |
132 | |
133 | add _COMM_PAGE_NT_NS_BASE,%eax | |
134 | adc _COMM_PAGE_NT_NS_BASE+4,%edx | |
135 | ||
2d21ac55 A |
136 | cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */ |
137 | jne 0b /* yes, loop until stable */ | |
0c530ab8 A |
138 | |
139 | pop %ebx | |
140 | pop %edi | |
141 | pop %esi | |
142 | pop %ebp | |
2d21ac55 | 143 | ret /* result in edx:eax */ |
89b3af67 | 144 | |
2d21ac55 | 145 | COMMPAGE_DESCRIPTOR(nanotime_slow,_COMM_PAGE_NANOTIME,kSlow,0) |
4452a7af | 146 | |
0c530ab8 A |
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 | |
2d21ac55 A |
154 | Lnanotime_64: // NB: must preserve r9, r10, and r11 |
155 | pushq %rbp // set up a frame for backtraces | |
0c530ab8 | 156 | movq %rsp,%rbp |
2d21ac55 | 157 | movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_TIME_DATA_START),%rsi |
91447636 | 158 | 1: |
2d21ac55 A |
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 | |
0c530ab8 | 164 | orq %rdx,%rax |
2d21ac55 | 165 | subq _NT_TSC_BASE(%rsi), %rax // rax := (tsc - base_tsc) |
0c530ab8 | 166 | movl _NT_SCALE(%rsi),%ecx |
2d21ac55 A |
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 | |
0c530ab8 | 170 | |
2d21ac55 | 171 | cmpl _NT_GENERATION(%rsi),%r8d // did the data change during computation? |
91447636 | 172 | jne 1b |
0c530ab8 | 173 | popq %rbp |
91447636 A |
174 | ret |
175 | ||
2d21ac55 | 176 | COMMPAGE_DESCRIPTOR(nanotime_64,_COMM_PAGE_NANOTIME,0,kSlow) |