libipt, time: don't store MTC offset

At TMA, we store the CTC bits below the MTC range in time->mtc_offset and the
CTC bits in the MTC range in time->ctc.

The latter is used to compute last_ctc at the first MTC after TMA.
The former is subtracted from the CTC delta between last and current CTC.

We might as well add the former to last_ctc before computing the CTC delta.
This allows us to merge time->mtc_offset into time->ctc.

Change-Id: Ib01b89a8ffa4796975fb7f26c8182a3d42096fcc
Signed-off-by: Markus Metzger <markus.t.metzger@intel.com>
diff --git a/libipt/internal/include/pt_time.h b/libipt/internal/include/pt_time.h
index 099f5be..9b59ec7 100644
--- a/libipt/internal/include/pt_time.h
+++ b/libipt/internal/include/pt_time.h
@@ -50,9 +50,6 @@
 	/* The estimated Fast Counter. */
 	uint64_t fc;
 
-	/* The number of CTC ticks into the current MTC period (from TMA). */
-	uint32_t mtc_offset;
-
 	/* The adjusted last CTC value (from MTC and TMA). */
 	uint32_t ctc;
 
diff --git a/libipt/src/pt_time.c b/libipt/src/pt_time.c
index 6bf4bd9..6066e22 100644
--- a/libipt/src/pt_time.c
+++ b/libipt/src/pt_time.c
@@ -168,7 +168,7 @@
 		       const struct pt_packet_tma *packet,
 		       const struct pt_config *config)
 {
-	uint32_t ctc, mtc_mask, mtc_offset, mtc_freq, mtc_hi;
+	uint32_t ctc, mtc_freq, mtc_hi, ctc_mask;
 	uint64_t fc;
 
 	if (!time || !packet || !config)
@@ -190,17 +190,16 @@
 	fc = packet->fc;
 
 	mtc_freq = config->mtc_freq;
-	mtc_mask = (1u << mtc_freq) - 1u;
-	mtc_offset = ctc & mtc_mask;
 	mtc_hi = mtc_freq + pt_pl_mtc_bit_size;
 
-	/* Mask out the MTC offset and the high order bits. */
-	ctc &= pt_pl_mtc_mask << mtc_freq;
+	/* A mask for the relevant CTC bits ignoring high-order bits that are
+	 * not provided by MTC.
+	 */
+	ctc_mask = (1u << mtc_hi) - 1u;
 
 	time->have_tma = 1;
 	time->base -= fc;
 	time->fc += fc;
-	time->mtc_offset = mtc_offset;
 
 	/* If the MTC frequency is low enough that TMA provides the full CTC
 	 * value, we can use the TMA as an MTC.
@@ -220,7 +219,7 @@
 		time->have_mtc = 1;
 
 	/* In both cases, we store the TMA's CTC bits until the next MTC. */
-	time->ctc = time->ctc_cyc = ctc;
+	time->ctc = time->ctc_cyc = ctc & ctc_mask;
 
 	return 0;
 }
@@ -229,7 +228,7 @@
 		       const struct pt_packet_mtc *packet,
 		       const struct pt_config *config)
 {
-	uint32_t last_ctc, mtc_offset, ctc, ctc_delta;
+	uint32_t last_ctc, ctc, ctc_delta;
 	uint64_t tsc, base;
 	uint8_t mtc_freq;
 	int errcode, have_tsc, have_tma, have_mtc;
@@ -252,7 +251,6 @@
 
 	base = time->base;
 	last_ctc = time->ctc;
-	mtc_offset = time->mtc_offset;
 	mtc_freq = config->mtc_freq;
 
 	ctc = packet->ctc << mtc_freq;
@@ -264,7 +262,6 @@
 	/* Prepare for the next packet in case we error out below. */
 	time->have_mtc = 1;
 	time->fc = 0ull;
-	time->mtc_offset = 0;
 	time->ctc = ctc;
 
 	/* We recover from previous CYC losses. */
@@ -324,14 +321,6 @@
 		return errcode;
 	}
 
-	/* We don't want a wrap-around here.  Something must be wrong. */
-	if (ctc_delta < mtc_offset) {
-		time->lost_mtc += 1;
-		return -pte_bad_packet;
-	}
-
-	ctc_delta -= mtc_offset;
-
 	errcode = pt_time_ctc_fc(&tsc, ctc_delta, config);
 	if (errcode < 0)
 		return errcode;