]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/rtclock.h
xnu-1504.9.17.tar.gz
[apple/xnu.git] / osfmk / i386 / rtclock.h
CommitLineData
6601e61a 1/*
0b4c1975 2 * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
6601e61a 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6601e61a 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.
6601e61a 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
6601e61a
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.
6601e61a 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
6601e61a
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
0c530ab8
A
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
593a1d5f
A
43#ifndef ASSEMBLER
44typedef 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
7e4a7d39 56#if 0
0c530ab8 57#include <kern/etimer.h>
7e4a7d39 58#endif
0c530ab8 59
0c530ab8 60struct cpu_data;
6601e61a 61
b0d623f7
A
62extern uint64_t tsc_rebase_abs_time;
63
593a1d5f
A
64extern void _rtc_nanotime_store(
65 uint64_t tsc,
66 uint64_t nsec,
67 uint32_t scale,
68 uint32_t shift,
69 rtc_nanotime_t *dst);
70
0b4c1975
A
71extern void _rtc_nanotime_adjust(
72 uint64_t tsc_base_delta,
73 rtc_nanotime_t *dst);
74
593a1d5f
A
75extern uint64_t _rtc_nanotime_read(
76 rtc_nanotime_t *rntp,
77 int slow);
78
b0d623f7 79extern rtc_nanotime_t rtc_nanotime_info;
593a1d5f
A
80#endif
81
82#define SLOW_TSC_THRESHOLD 1000067800 /* TSC is too slow for regular nanotime() algorithm */
83
84#if defined(__i386__)
85/*
86 * Assembly snippet included in exception handlers and rtc_nanotime_read()
87 * %edi points to nanotime info struct
88 * %edx:%eax returns nanotime
89 */
90#define RTC_NANOTIME_READ_FAST() \
910: movl RNT_GENERATION(%edi),%esi /* being updated? */ ; \
92 testl %esi,%esi ; \
93 jz 0b /* wait until done */ ; \
c910b4d9 94 lfence ; \
593a1d5f
A
95 rdtsc ; \
96 lfence ; \
97 subl RNT_TSC_BASE(%edi),%eax ; \
98 sbbl RNT_TSC_BASE+4(%edi),%edx /* tsc - tsc_base */ ; \
99 movl RNT_SCALE(%edi),%ecx /* * scale factor */ ; \
100 movl %edx,%ebx ; \
101 mull %ecx ; \
102 movl %ebx,%eax ; \
103 movl %edx,%ebx ; \
104 mull %ecx ; \
105 addl %ebx,%eax ; \
106 adcl $0,%edx ; \
107 addl RNT_NS_BASE(%edi),%eax /* + ns_base */ ; \
108 adcl RNT_NS_BASE+4(%edi),%edx ; \
109 cmpl RNT_GENERATION(%edi),%esi /* check for update */ ; \
110 jne 0b /* do it all again */
111
112#elif defined(__x86_64__)
113
114/*
115 * Assembly snippet included in exception handlers and rtc_nanotime_read()
116 * %rdi points to nanotime info struct.
117 * %rax returns nanotime
118 */
119#define RTC_NANOTIME_READ_FAST() \
1200: movl RNT_GENERATION(%rdi),%esi ; \
121 test %esi,%esi /* info updating? */ ; \
122 jz 0b /* - wait if so */ ; \
c910b4d9 123 lfence ; \
593a1d5f
A
124 rdtsc ; \
125 lfence ; \
126 shlq $32,%rdx ; \
127 orq %rdx,%rax /* %rax := tsc */ ; \
128 subq RNT_TSC_BASE(%rdi),%rax /* tsc - tsc_base */ ; \
129 xorq %rcx,%rcx ; \
130 movl RNT_SCALE(%rdi),%ecx ; \
131 mulq %rcx /* delta * scale */ ; \
132 shrdq $32,%rdx,%rax /* %rdx:%rax >>= 32 */ ; \
133 addq RNT_NS_BASE(%rdi),%rax /* add ns_base */ ; \
134 cmpl RNT_GENERATION(%rdi),%esi /* repeat if changed */ ; \
135 jne 0b
136
137#endif
138
0c530ab8 139#endif /* _I386_RTCLOCK_H_ */