1 | /* $Id: time-r0drv-linux.c 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | /* Make sure we have the setting functions we need for RTTimeNow: */
|
---|
35 | #if RTLNX_VER_MAX(2,6,16)
|
---|
36 | # define RTTIME_INCL_TIMEVAL
|
---|
37 | #elif RTLNX_VER_MAX(3,17,0)
|
---|
38 | # define RTTIME_INCL_TIMESPEC
|
---|
39 | #endif
|
---|
40 | #include <iprt/time.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
46 | {
|
---|
47 | #if RTLNX_VER_MIN(5,6,0)
|
---|
48 | /*
|
---|
49 | * Starting with kernel version 5.6-rc3 only 64-bit time interfaces
|
---|
50 | * are allowed in the kernel.
|
---|
51 | */
|
---|
52 | uint64_t u64;
|
---|
53 | struct timespec64 Ts = { 0, 0 };
|
---|
54 |
|
---|
55 | ktime_get_ts64(&Ts);
|
---|
56 | u64 = Ts.tv_sec * RT_NS_1SEC_64 + Ts.tv_nsec;
|
---|
57 | return u64;
|
---|
58 |
|
---|
59 | #elif RTLNX_VER_MIN(2,6,16) /* This must match timer-r0drv-linux.c! */
|
---|
60 | /*
|
---|
61 | * Use ktime_get_ts, this is also what clock_gettime(CLOCK_MONOTONIC,) is using.
|
---|
62 | */
|
---|
63 | uint64_t u64;
|
---|
64 | struct timespec Ts = { 0, 0 };
|
---|
65 | ktime_get_ts(&Ts);
|
---|
66 | u64 = Ts.tv_sec * RT_NS_1SEC_64 + Ts.tv_nsec;
|
---|
67 | return u64;
|
---|
68 |
|
---|
69 | #elif RTLNX_VER_MIN(2,5,60)
|
---|
70 | /*
|
---|
71 | * Seems there is no way of getting to the exact source of
|
---|
72 | * sys_clock_gettime(CLOCK_MONOTONIC, &ts) here, I think. But
|
---|
73 | * 64-bit jiffies adjusted for the initial value should be pretty
|
---|
74 | * much the same I hope.
|
---|
75 | */
|
---|
76 | uint64_t u64 = get_jiffies_64();
|
---|
77 | # ifdef INITIAL_JIFFIES
|
---|
78 | u64 += INITIAL_JIFFIES;
|
---|
79 | # endif
|
---|
80 | u64 *= TICK_NSEC;
|
---|
81 | return u64;
|
---|
82 |
|
---|
83 | #else /* < 2.5.60 */
|
---|
84 | # if BITS_PER_LONG >= 64
|
---|
85 | /*
|
---|
86 | * This is the same as above, except that there is no get_jiffies_64()
|
---|
87 | * here and we rely on long, and therefor jiffies, being 64-bit instead.
|
---|
88 | */
|
---|
89 | uint64_t u64 = jiffies;
|
---|
90 | # ifdef INITIAL_JIFFIES
|
---|
91 | u64 += INITIAL_JIFFIES;
|
---|
92 | # endif
|
---|
93 | u64 *= TICK_NSEC;
|
---|
94 | return u64;
|
---|
95 |
|
---|
96 | # else /* 32 bit jiffies */
|
---|
97 | /*
|
---|
98 | * We'll have to try track jiffy rollovers here or we'll be
|
---|
99 | * in trouble every time it flips.
|
---|
100 | *
|
---|
101 | * The high dword of the s_u64Last is the rollover count, the
|
---|
102 | * low dword is the previous jiffies. Updating is done by
|
---|
103 | * atomic compare & exchange of course.
|
---|
104 | */
|
---|
105 | static uint64_t volatile s_u64Last = 0;
|
---|
106 | uint64_t u64;
|
---|
107 |
|
---|
108 | for (;;)
|
---|
109 | {
|
---|
110 | uint64_t u64NewLast;
|
---|
111 | int32_t iDelta;
|
---|
112 | uint32_t cRollovers;
|
---|
113 | uint32_t u32LastJiffies;
|
---|
114 |
|
---|
115 | /* sample the values */
|
---|
116 | unsigned long ulNow = jiffies;
|
---|
117 | uint64_t u64Last = s_u64Last;
|
---|
118 | if (ulNow != jiffies)
|
---|
119 | continue; /* try again */
|
---|
120 | # ifdef INITIAL_JIFFIES
|
---|
121 | ulNow += INITIAL_JIFFIES;
|
---|
122 | # endif
|
---|
123 |
|
---|
124 | u32LastJiffies = (uint32_t)u64Last;
|
---|
125 | cRollovers = u64Last >> 32;
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Check for rollover and update the static last value.
|
---|
129 | *
|
---|
130 | * We have to make sure we update it successfully to rule out
|
---|
131 | * an underrun because of racing someone.
|
---|
132 | */
|
---|
133 | iDelta = ulNow - u32LastJiffies;
|
---|
134 | if (iDelta < 0)
|
---|
135 | {
|
---|
136 | cRollovers++;
|
---|
137 | u64NewLast = RT_MAKE_U64(ulNow, cRollovers);
|
---|
138 | if (!ASMAtomicCmpXchgU64(&s_u64Last, u64NewLast, u64Last))
|
---|
139 | continue; /* race, try again */
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | u64NewLast = RT_MAKE_U64(ulNow, cRollovers);
|
---|
144 | ASMAtomicCmpXchgU64(&s_u64Last, u64NewLast, u64Last);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /* calculate the return value */
|
---|
148 | u64 = ulNow;
|
---|
149 | u64 *= TICK_NSEC;
|
---|
150 | u64 += cRollovers * (_4G * TICK_NSEC);
|
---|
151 | break;
|
---|
152 | }
|
---|
153 |
|
---|
154 | return u64;
|
---|
155 | # endif /* 32 bit jiffies */
|
---|
156 | #endif /* < 2.5.60 */
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | RTDECL(uint64_t) RTTimeNanoTS(void)
|
---|
161 | {
|
---|
162 | return rtTimeGetSystemNanoTS();
|
---|
163 | }
|
---|
164 | RT_EXPORT_SYMBOL(RTTimeNanoTS);
|
---|
165 |
|
---|
166 |
|
---|
167 | RTDECL(uint64_t) RTTimeMilliTS(void)
|
---|
168 | {
|
---|
169 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
170 | }
|
---|
171 | RT_EXPORT_SYMBOL(RTTimeMilliTS);
|
---|
172 |
|
---|
173 |
|
---|
174 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
175 | {
|
---|
176 | return rtTimeGetSystemNanoTS();
|
---|
177 | }
|
---|
178 | RT_EXPORT_SYMBOL(RTTimeSystemNanoTS);
|
---|
179 |
|
---|
180 |
|
---|
181 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
182 | {
|
---|
183 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
184 | }
|
---|
185 | RT_EXPORT_SYMBOL(RTTimeSystemMilliTS);
|
---|
186 |
|
---|
187 |
|
---|
188 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
189 | {
|
---|
190 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
191 | #if RTLNX_VER_MIN(3,17,0)
|
---|
192 | struct timespec64 Ts;
|
---|
193 | ktime_get_real_ts64(&Ts); /* ktime_get_real_ts64 was added as a macro in 3.17, function since 4.18. */
|
---|
194 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
195 | return RTTimeSpecSetTimespec64(pTime, &Ts);
|
---|
196 |
|
---|
197 | #elif RTLNX_VER_MIN(2,6,16)
|
---|
198 | struct timespec Ts;
|
---|
199 | ktime_get_real_ts(&Ts); /* ktime_get_real_ts was removed in Linux 4.20. */
|
---|
200 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
201 | return RTTimeSpecSetTimespec(pTime, &Ts);
|
---|
202 |
|
---|
203 | #else /* < 2.6.16 */
|
---|
204 | struct timeval Tv;
|
---|
205 | do_gettimeofday(&Tv);
|
---|
206 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
207 | return RTTimeSpecSetTimeval(pTime, &Tv);
|
---|
208 | #endif
|
---|
209 | }
|
---|
210 | RT_EXPORT_SYMBOL(RTTimeNow);
|
---|
211 |
|
---|