]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - ubuntu/vbox/r0drv/linux/time-r0drv-linux.c
1 /* $Id: time-r0drv-linux.c $ */
3 * IPRT - Time, Ring-0 Driver, Linux.
7 * Copyright (C) 2006-2016 Oracle Corporation
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.
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.
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.
28 /*********************************************************************************************************************************
30 *********************************************************************************************************************************/
31 #define LOG_GROUP RTLOGGROUP_TIME
32 #include "the-linux-kernel.h"
33 #include "internal/iprt.h"
34 #include <iprt/time.h>
39 DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
41 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16) /* This must match timer-r0drv-linux.c! */
43 * Use ktime_get_ts, this is also what clock_gettime(CLOCK_MONOTONIC,) is using.
48 u64
= Ts
.tv_sec
* RT_NS_1SEC_64
+ Ts
.tv_nsec
;
51 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 60)
53 * Seems there is no way of getting to the exact source of
54 * sys_clock_gettime(CLOCK_MONOTONIC, &ts) here, I think. But
55 * 64-bit jiffies adjusted for the initial value should be pretty
56 * much the same I hope.
58 uint64_t u64
= get_jiffies_64();
59 # ifdef INITIAL_JIFFIES
60 u64
+= INITIAL_JIFFIES
;
66 # if BITS_PER_LONG >= 64
68 * This is the same as above, except that there is no get_jiffies_64()
69 * here and we rely on long, and therefor jiffies, being 64-bit instead.
71 uint64_t u64
= jiffies
;
72 # ifdef INITIAL_JIFFIES
73 u64
+= INITIAL_JIFFIES
;
78 # else /* 32 bit jiffies */
80 * We'll have to try track jiffy rollovers here or we'll be
81 * in trouble every time it flips.
83 * The high dword of the s_u64Last is the rollover count, the
84 * low dword is the previous jiffies. Updating is done by
85 * atomic compare & exchange of course.
87 static uint64_t volatile s_u64Last
= 0;
95 uint32_t u32LastJiffies
;
97 /* sample the values */
98 unsigned long ulNow
= jiffies
;
99 uint64_t u64Last
= s_u64Last
;
100 if (ulNow
!= jiffies
)
101 continue; /* try again */
102 # ifdef INITIAL_JIFFIES
103 ulNow
+= INITIAL_JIFFIES
;
106 u32LastJiffies
= (uint32_t)u64Last
;
107 cRollovers
= u64Last
>> 32;
110 * Check for rollover and update the static last value.
112 * We have to make sure we update it successfully to rule out
113 * an underrun because of racing someone.
115 iDelta
= ulNow
- u32LastJiffies
;
119 u64NewLast
= RT_MAKE_U64(ulNow
, cRollovers
);
120 if (!ASMAtomicCmpXchgU64(&s_u64Last
, u64NewLast
, u64Last
))
121 continue; /* race, try again */
125 u64NewLast
= RT_MAKE_U64(ulNow
, cRollovers
);
126 ASMAtomicCmpXchgU64(&s_u64Last
, u64NewLast
, u64Last
);
129 /* calculate the return value */
132 u64
+= cRollovers
* (_4G
* TICK_NSEC
);
137 # endif /* 32 bit jiffies */
138 #endif /* < 2.5.60 */
142 RTDECL(uint64_t) RTTimeNanoTS(void)
144 return rtTimeGetSystemNanoTS();
146 RT_EXPORT_SYMBOL(RTTimeNanoTS
);
149 RTDECL(uint64_t) RTTimeMilliTS(void)
151 return rtTimeGetSystemNanoTS() / RT_NS_1MS
;
153 RT_EXPORT_SYMBOL(RTTimeMilliTS
);
156 RTDECL(uint64_t) RTTimeSystemNanoTS(void)
158 return rtTimeGetSystemNanoTS();
160 RT_EXPORT_SYMBOL(RTTimeSystemNanoTS
);
163 RTDECL(uint64_t) RTTimeSystemMilliTS(void)
165 return rtTimeGetSystemNanoTS() / RT_NS_1MS
;
167 RT_EXPORT_SYMBOL(RTTimeSystemMilliTS
);
170 RTDECL(PRTTIMESPEC
) RTTimeNow(PRTTIMESPEC pTime
)
172 IPRT_LINUX_SAVE_EFL_AC();
173 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 16)
175 ktime_get_real_ts(&Ts
);
176 IPRT_LINUX_RESTORE_EFL_AC();
177 return RTTimeSpecSetTimespec(pTime
, &Ts
);
181 do_gettimeofday(&Tv
);
182 IPRT_LINUX_RESTORE_EFL_AC();
183 return RTTimeSpecSetTimeval(pTime
, &Tv
);
186 RT_EXPORT_SYMBOL(RTTimeNow
);