]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/rtclock.h
ec3e922d83384dafb17d6c7d00c6a0a785a54c6e
[apple/xnu.git] / osfmk / i386 / rtclock.h
1 /*
2 * Copyright (c) 2004-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 * @OSF_COPYRIGHT@
30 */
31 /*
32 * @APPLE_FREE_COPYRIGHT@
33 */
34 /*
35 * File: rtclock.h
36 * Purpose: Routines for handling the machine dependent
37 * real-time clock.
38 */
39
40 #ifndef _I386_RTCLOCK_H_
41 #define _I386_RTCLOCK_H_
42
43 #ifndef ASSEMBLER
44 typedef struct rtc_nanotime {
45 uint64_t tsc_base; /* timestamp */
46 uint64_t ns_base; /* nanoseconds */
47 uint32_t scale; /* tsc -> nanosec multiplier */
48 uint32_t shift; /* tsc -> nanosec shift/div */
49 /* shift is overloaded with
50 * lower 32bits of tsc_freq
51 * on slower machines (SLOW_TSC_THRESHOLD) */
52 uint32_t generation; /* 0 == being updated */
53 uint32_t spare1;
54 } rtc_nanotime_t;
55
56 #include <kern/etimer.h>
57
58 struct cpu_data;
59
60 extern uint64_t tsc_rebase_abs_time;
61
62 extern void _rtc_nanotime_store(
63 uint64_t tsc,
64 uint64_t nsec,
65 uint32_t scale,
66 uint32_t shift,
67 rtc_nanotime_t *dst);
68
69 extern uint64_t _rtc_nanotime_read(
70 rtc_nanotime_t *rntp,
71 int slow);
72
73 extern rtc_nanotime_t rtc_nanotime_info;
74 #endif
75
76 #define SLOW_TSC_THRESHOLD 1000067800 /* TSC is too slow for regular nanotime() algorithm */
77
78 #if defined(__i386__)
79 /*
80 * Assembly snippet included in exception handlers and rtc_nanotime_read()
81 * %edi points to nanotime info struct
82 * %edx:%eax returns nanotime
83 */
84 #define RTC_NANOTIME_READ_FAST() \
85 0: movl RNT_GENERATION(%edi),%esi /* being updated? */ ; \
86 testl %esi,%esi ; \
87 jz 0b /* wait until done */ ; \
88 lfence ; \
89 rdtsc ; \
90 lfence ; \
91 subl RNT_TSC_BASE(%edi),%eax ; \
92 sbbl RNT_TSC_BASE+4(%edi),%edx /* tsc - tsc_base */ ; \
93 movl RNT_SCALE(%edi),%ecx /* * scale factor */ ; \
94 movl %edx,%ebx ; \
95 mull %ecx ; \
96 movl %ebx,%eax ; \
97 movl %edx,%ebx ; \
98 mull %ecx ; \
99 addl %ebx,%eax ; \
100 adcl $0,%edx ; \
101 addl RNT_NS_BASE(%edi),%eax /* + ns_base */ ; \
102 adcl RNT_NS_BASE+4(%edi),%edx ; \
103 cmpl RNT_GENERATION(%edi),%esi /* check for update */ ; \
104 jne 0b /* do it all again */
105
106 #elif defined(__x86_64__)
107
108 /*
109 * Assembly snippet included in exception handlers and rtc_nanotime_read()
110 * %rdi points to nanotime info struct.
111 * %rax returns nanotime
112 */
113 #define RTC_NANOTIME_READ_FAST() \
114 0: movl RNT_GENERATION(%rdi),%esi ; \
115 test %esi,%esi /* info updating? */ ; \
116 jz 0b /* - wait if so */ ; \
117 lfence ; \
118 rdtsc ; \
119 lfence ; \
120 shlq $32,%rdx ; \
121 orq %rdx,%rax /* %rax := tsc */ ; \
122 subq RNT_TSC_BASE(%rdi),%rax /* tsc - tsc_base */ ; \
123 xorq %rcx,%rcx ; \
124 movl RNT_SCALE(%rdi),%ecx ; \
125 mulq %rcx /* delta * scale */ ; \
126 shrdq $32,%rdx,%rax /* %rdx:%rax >>= 32 */ ; \
127 addq RNT_NS_BASE(%rdi),%rax /* add ns_base */ ; \
128 cmpl RNT_GENERATION(%rdi),%esi /* repeat if changed */ ; \
129 jne 0b
130
131 #endif
132
133 #endif /* _I386_RTCLOCK_H_ */