1 | /* $Id: time-r0drv-nt.cpp 52822 2014-09-23 10:25:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Time, Ring-0 Driver, Nt.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2014 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-nt-kernel.h"
|
---|
33 | #include <iprt/time.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
37 | {
|
---|
38 | /*
|
---|
39 | * Note! The time source we use here must be exactly the same as in
|
---|
40 | * the ring-3 code!
|
---|
41 | *
|
---|
42 | * Using interrupt time is the simplest and requires the least calculation.
|
---|
43 | * It is also accounting for suspended time. Unfortuantely, there is no
|
---|
44 | * ring-3 for reading it... but that won't stop us.
|
---|
45 | *
|
---|
46 | * Using the tick count is problematic in ring-3 on older windows version
|
---|
47 | * as we can only get the 32-bit tick value, i.e. we'll roll over sooner or
|
---|
48 | * later.
|
---|
49 | */
|
---|
50 | #if 1
|
---|
51 | /* Interrupt time. (NT4 doesn't have an API for it.) */
|
---|
52 | # ifndef IPRT_TARGET_NT4
|
---|
53 | ULONGLONG InterruptTime = KeQueryInterruptTime();
|
---|
54 | return (uint64_t)InterruptTime * 100; /* The value is in 100ns, convert to ns units. */
|
---|
55 | # else
|
---|
56 | LARGE_INTEGER InterruptTime;
|
---|
57 | do
|
---|
58 | {
|
---|
59 | InterruptTime.HighPart = ((KUSER_SHARED_DATA volatile *)SharedUserData)->InterruptTime.High1Time;
|
---|
60 | InterruptTime.LowPart = ((KUSER_SHARED_DATA volatile *)SharedUserData)->InterruptTime.LowPart;
|
---|
61 | } while (((KUSER_SHARED_DATA volatile *)SharedUserData)->InterruptTime.High2Time != InterruptTime.HighPart);
|
---|
62 |
|
---|
63 | return (uint64_t)InterruptTime.QuadPart * 100;
|
---|
64 | # endif
|
---|
65 | #else
|
---|
66 | /* Tick Count (NT4 SP1 has these APIs, haven't got SP0 to check). */
|
---|
67 | LARGE_INTEGER Tick;
|
---|
68 | KeQueryTickCount(&Tick);
|
---|
69 | return (uint64_t)Tick.QuadPart * KeQueryTimeIncrement() * 100;
|
---|
70 | #endif
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | RTDECL(uint64_t) RTTimeNanoTS(void)
|
---|
75 | {
|
---|
76 | return rtTimeGetSystemNanoTS();
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | RTDECL(uint64_t) RTTimeMilliTS(void)
|
---|
81 | {
|
---|
82 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
87 | {
|
---|
88 | return rtTimeGetSystemNanoTS();
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
93 | {
|
---|
94 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
99 | {
|
---|
100 | LARGE_INTEGER SystemTime;
|
---|
101 | #ifndef IPRT_TARGET_NT4
|
---|
102 | KeQuerySystemTime(&SystemTime);
|
---|
103 | #else
|
---|
104 | do
|
---|
105 | {
|
---|
106 | SystemTime.HighPart = ((KUSER_SHARED_DATA volatile *)SharedUserData)->SystemTime.High1Time;
|
---|
107 | SystemTime.LowPart = ((KUSER_SHARED_DATA volatile *)SharedUserData)->SystemTime.LowPart;
|
---|
108 | } while (((KUSER_SHARED_DATA volatile *)SharedUserData)->SystemTime.High2Time != SystemTime.HighPart);
|
---|
109 | #endif
|
---|
110 | return RTTimeSpecSetNtTime(pTime, SystemTime.QuadPart);
|
---|
111 | }
|
---|
112 |
|
---|