]> git.proxmox.com Git - libtpms.git/commitdiff
tpm2: Reimplement tpmClock and call it ClockGetTime
authorStefan Berger <stefanb@linux.vnet.ibm.com>
Thu, 12 Jul 2018 18:08:33 +0000 (14:08 -0400)
committerStefan Berger <stefanb@us.ibm.com>
Thu, 3 Jan 2019 18:15:44 +0000 (13:15 -0500)
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
src/tpm2/Clock.c
src/tpm2/NVMarshal.c
src/tpm2/Platform_fp.h

index 7e14dbd22aac916b257a816e679dc57ead620480..f8d86921de5fd32141b00d78eecd315690f5ddc0 100644 (file)
 #include <stdio.h>
 #include <time.h>
 
-#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 <sys/time.h>
-
-/* 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 <assert.h>
+
+/* 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
index 8e71077e39437649f91c7fbb6d7d0c6e54f9f5cf..68065b870f13fa8ddb08e363da843c7cbd886cab 100644 (file)
@@ -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;
index bc8d4b4ff8394c5c7435be94d641ba35b8a66a7d..2f4c21f77d76d674ad6eba85ed0034730bc930f4 100644 (file)
@@ -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 <time.h>
+uint64_t ClockGetTime(clockid_t clk_id);
 
 #endif  // _PLATFORM_FP_H_