diff --git a/src/tpm2/Clock.c b/src/tpm2/Clock.c index 7e14dbd2..f8d86921 100644 --- a/src/tpm2/Clock.c +++ b/src/tpm2/Clock.c @@ -65,41 +65,6 @@ #include #include -#include "Tpm.h" -#include "Platform_fp.h" - -#ifdef TPM_WINDOWS - -/* Windows returns result in units of CLOCKS_PER_SEC. seconds = clock() / CLOCKS_PER_SEC */ - -uint64_t tpmclock(void) -{ - clock_t clocktime = clock(); - uint64_t tpmtime = ((uint64_t)clocktime * 1000) / (uint64_t)CLOCKS_PER_SEC; - return tpmtime; -} - -#endif - -#ifdef TPM_POSIX - - -#include - -/* return time in milliseconds */ - -uint64_t tpmclock(void) -{ - struct timeval tval; - uint64_t tpmtime; - - gettimeofday(&tval, NULL); /* get the time */ - tpmtime = ((uint64_t)tval.tv_sec * 1000) + ((uint64_t)tval.tv_usec / 1000); - return tpmtime; -} - -#endif - /* C.3 Clock.c */ /* C.3.1. Introduction */ /* This file contains the routines that are used by the simulator to mimic a hardware clock on a @@ -110,6 +75,27 @@ uint64_t tpmclock(void) #include "Platform_fp.h" #include "TpmFail_fp.h" #include + +/* ClockGetTime -- get time given a specified clock type */ +uint64_t +ClockGetTime( + clockid_t clk_id + ) +{ + uint64_t time; +#ifdef TPM_WINDOWS +#error Not supported for TPM_WINDOWS +#else + struct timespec systime; + + clock_gettime(clk_id, &systime); + time = (uint64_t)systime.tv_sec * 1000 + (systime.tv_nsec / 1000000); +#endif + + return time; +} + + /* C.3.3. Simulator Functions */ /* C.3.3.1. Introduction */ /* This set of functions is intended to be called by the simulator environment in order to simulate @@ -123,7 +109,7 @@ _plat__TimerReset( void ) { - s_realTimePrevious = (clock_t) tpmclock(); /* kgold, FIXME, something wrong here */ + s_realTimePrevious = (clock_t) ClockGetTime(CLOCK_REALTIME); /* kgold, FIXME, something wrong here */ s_tpmTime = 0; s_adjustRate = CLOCK_NOMINAL; s_timerReset = TRUE; @@ -173,7 +159,7 @@ _plat__TimerRead( // Save the value previously read from the system clock timeDiff = s_realTimePrevious; // update with the current value of the system clock - s_realTimePrevious = tpmclock(); + s_realTimePrevious = ClockGetTime(CLOCK_REALTIME); // In the place below when we "put back" the unused part of the timeDiff // it is possible that we can put back more than we take out. That is, we could // take out 1000 mSec, rate adjust it and put back 1001 mS. This means that diff --git a/src/tpm2/NVMarshal.c b/src/tpm2/NVMarshal.c index 8e71077e..68065b87 100644 --- a/src/tpm2/NVMarshal.c +++ b/src/tpm2/NVMarshal.c @@ -2904,7 +2904,7 @@ VolatileState_Marshal(BYTE **buffer, INT32 *size) written += BOOL_Marshal(&s_timerStopped, buffer, size); written += UINT32_Marshal(&s_adjustRate, buffer, size); - tmp_uint64 = tpmclock(); + tmp_uint64 = ClockGetTime(CLOCK_REALTIME); written += UINT64_Marshal(&tmp_uint64, buffer, size); written += BLOCK_SKIP_WRITE_PUSH(TRUE, buffer, size); /* v3 */ @@ -3367,7 +3367,7 @@ skip_hardware_clock: INT64 timediff; rc = UINT64_Unmarshal(&backthen, buffer, size); - now = tpmclock(); + now = ClockGetTime(CLOCK_REALTIME); timediff = now - backthen; g_time += timediff; diff --git a/src/tpm2/Platform_fp.h b/src/tpm2/Platform_fp.h index bc8d4b4f..2f4c21f7 100644 --- a/src/tpm2/Platform_fp.h +++ b/src/tpm2/Platform_fp.h @@ -377,7 +377,8 @@ _plat__GetUnique( unsigned char *b // output buffer ); -/* libtpms: tpmclock() needs to be public */ -uint64_t tpmclock(void); +/* libtpms: ClockGetTime() needs to be public */ +#include +uint64_t ClockGetTime(clockid_t clk_id); #endif // _PLATFORM_FP_H_