1 | /* $Id: time-r0drv-linux.c 83484 2020-03-30 14:37:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
32 | #include "the-linux-kernel.h"
|
---|
33 | #include "internal/iprt.h"
|
---|
34 | #include <iprt/time.h>
|
---|
35 | #include <iprt/asm.h>
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
40 | {
|
---|
41 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
|
---|
42 | /*
|
---|
43 | * Starting with kernel version 5.6-rc3 only 64-bit time interfaces
|
---|
44 | * are allowed in the kernel.
|
---|
45 | */
|
---|
46 | uint64_t u64;
|
---|
47 | struct timespec64 Ts = { 0, 0 };
|
---|
48 |
|
---|
49 | ktime_get_ts64(&Ts);
|
---|
50 | u64 = Ts.tv_sec * RT_NS_1SEC_64 + Ts.tv_nsec;
|
---|
51 | return u64;
|
---|
52 |
|
---|
53 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16) /* This must match timer-r0drv-linux.c! */
|
---|
54 | /*
|
---|
55 | * Use ktime_get_ts, this is also what clock_gettime(CLOCK_MONOTONIC,) is using.
|
---|
56 | */
|
---|
57 | uint64_t u64;
|
---|
58 | struct timespec Ts = { 0, 0 };
|
---|
59 | ktime_get_ts(&Ts);
|
---|
60 | u64 = Ts.tv_sec * RT_NS_1SEC_64 + Ts.tv_nsec;
|
---|
61 | return u64;
|
---|
62 |
|
---|
63 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 60)
|
---|
64 | /*
|
---|
65 | * Seems there is no way of getting to the exact source of
|
---|
66 | * sys_clock_gettime(CLOCK_MONOTONIC, &ts) here, I think. But
|
---|
67 | * 64-bit jiffies adjusted for the initial value should be pretty
|
---|
68 | * much the same I hope.
|
---|
69 | */
|
---|
70 | uint64_t u64 = get_jiffies_64();
|
---|
71 | # ifdef INITIAL_JIFFIES
|
---|
72 | u64 += INITIAL_JIFFIES;
|
---|
73 | # endif
|
---|
74 | u64 *= TICK_NSEC;
|
---|
75 | return u64;
|
---|
76 |
|
---|
77 | #else /* < 2.5.60 */
|
---|
78 | # if BITS_PER_LONG >= 64
|
---|
79 | /*
|
---|
80 | * This is the same as above, except that there is no get_jiffies_64()
|
---|
81 | * here and we rely on long, and therefor jiffies, being 64-bit instead.
|
---|
82 | */
|
---|
83 | uint64_t u64 = jiffies;
|
---|
84 | # ifdef INITIAL_JIFFIES
|
---|
85 | u64 += INITIAL_JIFFIES;
|
---|
86 | # endif
|
---|
87 | u64 *= TICK_NSEC;
|
---|
88 | return u64;
|
---|
89 |
|
---|
90 | # else /* 32 bit jiffies */
|
---|
91 | /*
|
---|
92 | * We'll have to try track jiffy rollovers here or we'll be
|
---|
93 | * in trouble every time it flips.
|
---|
94 | *
|
---|
95 | * The high dword of the s_u64Last is the rollover count, the
|
---|
96 | * low dword is the previous jiffies. Updating is done by
|
---|
97 | * atomic compare & exchange of course.
|
---|
98 | */
|
---|
99 | static uint64_t volatile s_u64Last = 0;
|
---|
100 | uint64_t u64;
|
---|
101 |
|
---|
102 | for (;;)
|
---|
103 | {
|
---|
104 | uint64_t u64NewLast;
|
---|
105 | int32_t iDelta;
|
---|
106 | uint32_t cRollovers;
|
---|
107 | uint32_t u32LastJiffies;
|
---|
108 |
|
---|
109 | /* sample the values */
|
---|
110 | unsigned long ulNow = jiffies;
|
---|
111 | uint64_t u64Last = s_u64Last;
|
---|
112 | if (ulNow != jiffies)
|
---|
113 | continue; /* try again */
|
---|
114 | # ifdef INITIAL_JIFFIES
|
---|
115 | ulNow += INITIAL_JIFFIES;
|
---|
116 | # endif
|
---|
117 |
|
---|
118 | u32LastJiffies = (uint32_t)u64Last;
|
---|
119 | cRollovers = u64Last >> 32;
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Check for rollover and update the static last value.
|
---|
123 | *
|
---|
124 | * We have to make sure we update it successfully to rule out
|
---|
125 | * an underrun because of racing someone.
|
---|
126 | */
|
---|
127 | iDelta = ulNow - u32LastJiffies;
|
---|
128 | if (iDelta < 0)
|
---|
129 | {
|
---|
130 | cRollovers++;
|
---|
131 | u64NewLast = RT_MAKE_U64(ulNow, cRollovers);
|
---|
132 | if (!ASMAtomicCmpXchgU64(&s_u64Last, u64NewLast, u64Last))
|
---|
133 | continue; /* race, try again */
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | u64NewLast = RT_MAKE_U64(ulNow, cRollovers);
|
---|
138 | ASMAtomicCmpXchgU64(&s_u64Last, u64NewLast, u64Last);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /* calculate the return value */
|
---|
142 | u64 = ulNow;
|
---|
143 | u64 *= TICK_NSEC;
|
---|
144 | u64 += cRollovers * (_4G * TICK_NSEC);
|
---|
145 | break;
|
---|
146 | }
|
---|
147 |
|
---|
148 | return u64;
|
---|
149 | # endif /* 32 bit jiffies */
|
---|
150 | #endif /* < 2.5.60 */
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | RTDECL(uint64_t) RTTimeNanoTS(void)
|
---|
155 | {
|
---|
156 | return rtTimeGetSystemNanoTS();
|
---|
157 | }
|
---|
158 | RT_EXPORT_SYMBOL(RTTimeNanoTS);
|
---|
159 |
|
---|
160 |
|
---|
161 | RTDECL(uint64_t) RTTimeMilliTS(void)
|
---|
162 | {
|
---|
163 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
164 | }
|
---|
165 | RT_EXPORT_SYMBOL(RTTimeMilliTS);
|
---|
166 |
|
---|
167 |
|
---|
168 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
169 | {
|
---|
170 | return rtTimeGetSystemNanoTS();
|
---|
171 | }
|
---|
172 | RT_EXPORT_SYMBOL(RTTimeSystemNanoTS);
|
---|
173 |
|
---|
174 |
|
---|
175 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
176 | {
|
---|
177 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
178 | }
|
---|
179 | RT_EXPORT_SYMBOL(RTTimeSystemMilliTS);
|
---|
180 |
|
---|
181 |
|
---|
182 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
183 | {
|
---|
184 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
185 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
|
---|
186 | /* On Linux 4.20, time.h includes time64.h and we have to use 64-bit times. */
|
---|
187 | # ifdef _LINUX_TIME64_H
|
---|
188 | struct timespec64 Ts;
|
---|
189 | ktime_get_real_ts64(&Ts);
|
---|
190 | # else
|
---|
191 | struct timespec Ts;
|
---|
192 | ktime_get_real_ts(&Ts);
|
---|
193 | # endif
|
---|
194 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
195 | # ifdef _LINUX_TIME64_H
|
---|
196 | return RTTimeSpecSetTimespec64(pTime, &Ts);
|
---|
197 | # else
|
---|
198 | return RTTimeSpecSetTimespec(pTime, &Ts);
|
---|
199 | # endif
|
---|
200 | #else /* < 2.6.16 */
|
---|
201 | struct timeval Tv;
|
---|
202 | do_gettimeofday(&Tv);
|
---|
203 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
204 | return RTTimeSpecSetTimeval(pTime, &Tv);
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 | RT_EXPORT_SYMBOL(RTTimeNow);
|
---|
208 |
|
---|