]> git.saurik.com Git - apple/libc.git/blame - i386/sys/mach_absolute_time_asm.s
Libc-825.40.1.tar.gz
[apple/libc.git] / i386 / sys / mach_absolute_time_asm.s
CommitLineData
1f2f436a
A
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>
1f2f436a 31
1f2f436a 32
a28bf75d
A
33/* return mach_absolute_time in %edx:%eax
34 *
35 * The algorithm we use is:
36 *
37 * ns = ((((rdtsc - rnt_tsc_base)<<rnt_shift)*rnt_tsc_scale) / 2**32) + rnt_ns_base;
38 *
39 * rnt_shift, a constant computed during initialization, is the smallest value for which:
40 *
41 * (tscFreq << rnt_shift) > SLOW_TSC_THRESHOLD
42 *
43 * Where SLOW_TSC_THRESHOLD is about 10e9. Since most processor's tscFreq is greater
44 * than 1GHz, rnt_shift is usually 0. rnt_tsc_scale is also a 32-bit constant:
45 *
46 * rnt_tsc_scale = (10e9 * 2**32) / (tscFreq << rnt_shift);
47 */
1f2f436a 48
a28bf75d
A
49 .globl _mach_absolute_time
50_mach_absolute_time:
1f2f436a
A
51 pushl %ebp
52 movl %esp,%ebp
53 pushl %esi
54 pushl %ebx
55
560:
57 movl _COMM_PAGE_NT_GENERATION,%esi /* get generation (0 if being changed) */
58 testl %esi,%esi /* if being updated, loop until stable */
59 jz 0b
60
61 lfence
62 rdtsc /* get TSC in %edx:%eax */
63 lfence
64
65 subl _COMM_PAGE_NT_TSC_BASE,%eax
66 sbbl _COMM_PAGE_NT_TSC_BASE+4,%edx
a28bf75d
A
67
68 /*
69 * Prior to supporting "slow" processors, xnu always set _NT_SHIFT to 32.
70 * Now it defaults to 0, unless the processor is slow. The shifts
71 * below implicitly mask the count down to 5 bits, handling either default.
72 */
73 movl _COMM_PAGE_NT_SHIFT,%ecx
74 shldl %cl,%eax,%edx /* shift %edx left, filling in from %eax */
75 shll %cl,%eax /* finish shifting %edx:%eax left by _COMM_PAGE_NT_SHIFT bits */
1f2f436a
A
76
77 movl _COMM_PAGE_NT_SCALE,%ecx
78
79 movl %edx,%ebx
80 mull %ecx
81 movl %ebx,%eax
82 movl %edx,%ebx
83 mull %ecx
84 addl %ebx,%eax
85 adcl $0,%edx
86
87 addl _COMM_PAGE_NT_NS_BASE,%eax
88 adcl _COMM_PAGE_NT_NS_BASE+4,%edx
89
90 cmpl _COMM_PAGE_NT_GENERATION,%esi /* have the parameters changed? */
91 jne 0b /* yes, loop until stable */
92
93 popl %ebx
94 popl %esi
95 popl %ebp
96 ret