Debian Codegraph @

simulator/ms-tpm-20-ref/Samples/Google/Clock.c

raw
1 /* Microsoft Reference Implementation for TPM 2.0
2 *
3 * The copyright in this software is being made available under the BSD
4 * License, included below. This software may be subject to other third party
5 * and contributor rights, including patent rights, and no such rights are
6 * granted under this license.
7 *
8 * Copyright (c) Microsoft Corporation
9 *
10 * All rights reserved.
11 *
12 * BSD License
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are met:
16 *
17 * Redistributions of source code must retain the above copyright notice, this
18 * list of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimer in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS
25 * IS"" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 //** Description
37 //
38 // This file contains the routines that are used by the simulator to mimic
39 // a hardware clock on a TPM.
40 //
41 // In this implementation, all the time values are measured in millisecond.
42 // However, the precision of the clock functions may be implementation
43 // dependent.
44
45 #ifdef _WIN32
46 #include <sys/types.h>
47 #include <sys/timeb.h>
48 #else
49 #include <time.h>
50 #endif
51
52 #include "PlatformData.h"
53 #include "Platform_fp.h"
54
55 unsigned int s_adjustRate;
56 bool s_timerReset;
57
58 clock64_t s_realTimePrevious;
59 clock64_t s_tpmTime;
60 clock64_t s_lastSystemTime;
61 clock64_t s_lastReportedTime;
62
63 void _plat__TimerReset() {
64 s_lastSystemTime = 0;
65 s_tpmTime = 0;
66 s_adjustRate = CLOCK_NOMINAL;
67 s_timerReset = true;
68 return;
69 }
70
71 static clock64_t _plat__RealTime() {
72 #ifdef _WIN32 // On Windows we might be using msvcrt, which only has _ftime.
73 struct _timeb sysTime;
74 _ftime_s(&sysTime);
75 return (clock64_t)(sysTime.time) * 1000 + sysTime.millitm;
76 #else
77 struct timespec systime;
78 clock_gettime(CLOCK_MONOTONIC, &systime);
79 return (clock64_t)systime.tv_sec * 1000 + (systime.tv_nsec / 1000000);
80 #endif
81 }
82
83 uint64_t _plat__TimerRead() {
84 clock64_t timeDiff;
85 clock64_t adjustedTimeDiff;
86 clock64_t timeNow;
87 clock64_t readjustedTimeDiff;
88
89 // This produces a timeNow that is basically locked to the system clock.
90 timeNow = _plat__RealTime();
91
92 // if this hasn't been initialized, initialize it
93 if (s_lastSystemTime == 0) {
94 s_lastSystemTime = timeNow;
95 s_lastReportedTime = 0;
96 s_realTimePrevious = 0;
97 }
98 // The system time can bounce around and that's OK as long as we don't allow
99 // time to go backwards. When the time does appear to go backwards, set
100 // lastSystemTime to be the new value and then update the reported time.
101 if (timeNow < s_lastReportedTime) s_lastSystemTime = timeNow;
102 s_lastReportedTime = s_lastReportedTime + timeNow - s_lastSystemTime;
103 s_lastSystemTime = timeNow;
104 timeNow = s_lastReportedTime;
105
106 // The code above produces a timeNow that is similar to the value returned
107 // by Clock(). The difference is that timeNow does not max out, and it is
108 // at a ms. rate rather than at a CLOCKS_PER_SEC rate. The code below
109 // uses that value and does the rate adjustment on the time value.
110 // If there is no difference in time, then skip all the computations
111 if (s_realTimePrevious >= timeNow) return s_tpmTime;
112 // Compute the amount of time since the last update of the system clock
113 timeDiff = timeNow - s_realTimePrevious;
114
115 // Do the time rate adjustment and conversion from CLOCKS_PER_SEC to mSec
116 adjustedTimeDiff = (timeDiff * CLOCK_NOMINAL) / ((uint64_t)s_adjustRate);
117
118 // update the TPM time with the adjusted timeDiff
119 s_tpmTime += (clock64_t)adjustedTimeDiff;
120
121 // Might have some rounding error that would loose CLOCKS. See what is not
122 // being used. As mentioned above, this could result in putting back more than
123 // is taken out. Here, we are trying to recreate timeDiff.
124 readjustedTimeDiff =
125 (adjustedTimeDiff * (uint64_t)s_adjustRate) / CLOCK_NOMINAL;
126
127 // adjusted is now converted back to being the amount we should advance the
128 // previous sampled time. It should always be less than or equal to timeDiff.
129 // That is, we could not have use more time than we started with.
130 s_realTimePrevious = s_realTimePrevious + readjustedTimeDiff;
131
132 return s_tpmTime;
133 }
134
135 bool _plat__TimerWasReset() {
136 bool retVal = s_timerReset;
137 s_timerReset = false;
138 return retVal;
139 }
140
141 void _plat__ClockAdjustRate(int adjust) {
142 // We expect the caller should only use a fixed set of constant values to
143 // adjust the rate
144 switch (adjust) {
145 case CLOCK_ADJUST_COARSE:
146 s_adjustRate += CLOCK_ADJUST_COARSE;
147 break;
148 case -CLOCK_ADJUST_COARSE:
149 s_adjustRate -= CLOCK_ADJUST_COARSE;
150 break;
151 case CLOCK_ADJUST_MEDIUM:
152 s_adjustRate += CLOCK_ADJUST_MEDIUM;
153 break;
154 case -CLOCK_ADJUST_MEDIUM:
155 s_adjustRate -= CLOCK_ADJUST_MEDIUM;
156 break;
157 case CLOCK_ADJUST_FINE:
158 s_adjustRate += CLOCK_ADJUST_FINE;
159 break;
160 case -CLOCK_ADJUST_FINE:
161 s_adjustRate -= CLOCK_ADJUST_FINE;
162 break;
163 default:
164 // ignore any other values;
165 break;
166 }
167
168 if (s_adjustRate > (CLOCK_NOMINAL + CLOCK_ADJUST_LIMIT))
169 s_adjustRate = CLOCK_NOMINAL + CLOCK_ADJUST_LIMIT;
170 if (s_adjustRate < (CLOCK_NOMINAL - CLOCK_ADJUST_LIMIT))
171 s_adjustRate = CLOCK_NOMINAL - CLOCK_ADJUST_LIMIT;
172
173 return;
174 }