hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
....@@ -1,16 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * intel_pt_decoder.c: Intel Processor Trace support
34 * Copyright (c) 2013-2014, Intel Corporation.
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms and conditions of the GNU General Public License,
7
- * version 2, as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope it will be useful, but WITHOUT
10
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
- * more details.
13
- *
145 */
156
167 #ifndef _GNU_SOURCE
....@@ -23,15 +14,22 @@
2314 #include <stdint.h>
2415 #include <inttypes.h>
2516 #include <linux/compiler.h>
17
+#include <linux/string.h>
18
+#include <linux/zalloc.h>
2619
27
-#include "../cache.h"
28
-#include "../util.h"
2920 #include "../auxtrace.h"
3021
3122 #include "intel-pt-insn-decoder.h"
3223 #include "intel-pt-pkt-decoder.h"
3324 #include "intel-pt-decoder.h"
3425 #include "intel-pt-log.h"
26
+
27
+#define BITULL(x) (1ULL << (x))
28
+
29
+/* IA32_RTIT_CTL MSR bits */
30
+#define INTEL_PT_CYC_ENABLE BITULL(1)
31
+#define INTEL_PT_CYC_THRESHOLD (BITULL(22) | BITULL(21) | BITULL(20) | BITULL(19))
32
+#define INTEL_PT_CYC_THRESHOLD_SHIFT 19
3533
3634 #define INTEL_PT_BLK_SIZE 1024
3735
....@@ -64,6 +62,7 @@
6462 INTEL_PT_STATE_TIP_PGD,
6563 INTEL_PT_STATE_FUP,
6664 INTEL_PT_STATE_FUP_NO_TIP,
65
+ INTEL_PT_STATE_RESAMPLE,
6766 };
6867
6968 static inline bool intel_pt_sample_time(enum intel_pt_pkt_state pkt_state)
....@@ -74,6 +73,7 @@
7473 case INTEL_PT_STATE_ERR_RESYNC:
7574 case INTEL_PT_STATE_IN_SYNC:
7675 case INTEL_PT_STATE_TNT_CONT:
76
+ case INTEL_PT_STATE_RESAMPLE:
7777 return true;
7878 case INTEL_PT_STATE_TNT:
7979 case INTEL_PT_STATE_TIP:
....@@ -104,6 +104,7 @@
104104 uint64_t *insn_cnt_ptr, uint64_t *ip, uint64_t to_ip,
105105 uint64_t max_insn_cnt, void *data);
106106 bool (*pgd_ip)(uint64_t ip, void *data);
107
+ int (*lookahead)(void *data, intel_pt_lookahead_cb_t cb, void *cb_data);
107108 void *data;
108109 struct intel_pt_state state;
109110 const unsigned char *buf;
....@@ -116,6 +117,10 @@
116117 bool have_cyc;
117118 bool fixup_last_mtc;
118119 bool have_last_ip;
120
+ bool in_psb;
121
+ bool hop;
122
+ bool hop_psb_fup;
123
+ bool leap;
119124 enum intel_pt_param_flags flags;
120125 uint64_t pos;
121126 uint64_t last_ip;
....@@ -124,6 +129,7 @@
124129 uint64_t timestamp;
125130 uint64_t tsc_timestamp;
126131 uint64_t ref_timestamp;
132
+ uint64_t buf_timestamp;
127133 uint64_t sample_timestamp;
128134 uint64_t ret_addr;
129135 uint64_t ctc_timestamp;
....@@ -139,6 +145,10 @@
139145 int mtc_shift;
140146 struct intel_pt_stack stack;
141147 enum intel_pt_pkt_state pkt_state;
148
+ enum intel_pt_pkt_ctx pkt_ctx;
149
+ enum intel_pt_pkt_ctx prev_pkt_ctx;
150
+ enum intel_pt_blk_type blk_type;
151
+ int blk_type_pos;
142152 struct intel_pt_pkt packet;
143153 struct intel_pt_pkt tnt;
144154 int pkt_step;
....@@ -160,6 +170,13 @@
160170 uint64_t period_mask;
161171 uint64_t period_ticks;
162172 uint64_t last_masked_timestamp;
173
+ uint64_t tot_cyc_cnt;
174
+ uint64_t sample_tot_cyc_cnt;
175
+ uint64_t base_cyc_cnt;
176
+ uint64_t cyc_cnt_timestamp;
177
+ uint64_t ctl;
178
+ uint64_t cyc_threshold;
179
+ double tsc_to_cyc;
163180 bool continuous_period;
164181 bool overflow;
165182 bool set_fup_tx_flags;
....@@ -167,6 +184,8 @@
167184 bool set_fup_mwait;
168185 bool set_fup_pwre;
169186 bool set_fup_exstop;
187
+ bool set_fup_bep;
188
+ bool sample_cyc;
170189 unsigned int fup_tx_flags;
171190 unsigned int tx_flags;
172191 uint64_t fup_ptw_payload;
....@@ -192,6 +211,14 @@
192211 x >>= 1;
193212
194213 return x << i;
214
+}
215
+
216
+static uint64_t intel_pt_cyc_threshold(uint64_t ctl)
217
+{
218
+ if (!(ctl & INTEL_PT_CYC_ENABLE))
219
+ return 0;
220
+
221
+ return (ctl & INTEL_PT_CYC_THRESHOLD) >> INTEL_PT_CYC_THRESHOLD_SHIFT;
195222 }
196223
197224 static void intel_pt_setup_period(struct intel_pt_decoder *decoder)
....@@ -226,17 +253,23 @@
226253 decoder->get_trace = params->get_trace;
227254 decoder->walk_insn = params->walk_insn;
228255 decoder->pgd_ip = params->pgd_ip;
256
+ decoder->lookahead = params->lookahead;
229257 decoder->data = params->data;
230258 decoder->return_compression = params->return_compression;
231259 decoder->branch_enable = params->branch_enable;
260
+ decoder->hop = params->quick >= 1;
261
+ decoder->leap = params->quick >= 2;
232262
233263 decoder->flags = params->flags;
234264
265
+ decoder->ctl = params->ctl;
235266 decoder->period = params->period;
236267 decoder->period_type = params->period_type;
237268
238269 decoder->max_non_turbo_ratio = params->max_non_turbo_ratio;
239270 decoder->max_non_turbo_ratio_fp = params->max_non_turbo_ratio;
271
+
272
+ decoder->cyc_threshold = intel_pt_cyc_threshold(decoder->ctl);
240273
241274 intel_pt_setup_period(decoder);
242275
....@@ -268,6 +301,9 @@
268301 intel_pt_log("timestamp: tsc_ctc_ratio_d %u\n", decoder->tsc_ctc_ratio_d);
269302 intel_pt_log("timestamp: tsc_ctc_mult %u\n", decoder->tsc_ctc_mult);
270303 intel_pt_log("timestamp: tsc_slip %#x\n", decoder->tsc_slip);
304
+
305
+ if (decoder->hop)
306
+ intel_pt_log("Hop mode: decoding FUP and TIPs, but not TNT\n");
271307
272308 return decoder;
273309 }
....@@ -479,7 +515,21 @@
479515 return -EBADMSG;
480516 }
481517
482
-static int intel_pt_get_data(struct intel_pt_decoder *decoder)
518
+static inline void intel_pt_update_sample_time(struct intel_pt_decoder *decoder)
519
+{
520
+ decoder->sample_timestamp = decoder->timestamp;
521
+ decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
522
+}
523
+
524
+static void intel_pt_reposition(struct intel_pt_decoder *decoder)
525
+{
526
+ decoder->ip = 0;
527
+ decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
528
+ decoder->timestamp = 0;
529
+ decoder->have_tma = false;
530
+}
531
+
532
+static int intel_pt_get_data(struct intel_pt_decoder *decoder, bool reposition)
483533 {
484534 struct intel_pt_buffer buffer = { .buf = 0, };
485535 int ret;
....@@ -496,12 +546,10 @@
496546 intel_pt_log("No more data\n");
497547 return -ENODATA;
498548 }
499
- if (!buffer.consecutive) {
500
- decoder->ip = 0;
501
- decoder->pkt_state = INTEL_PT_STATE_NO_PSB;
549
+ decoder->buf_timestamp = buffer.ref_timestamp;
550
+ if (!buffer.consecutive || reposition) {
551
+ intel_pt_reposition(decoder);
502552 decoder->ref_timestamp = buffer.ref_timestamp;
503
- decoder->timestamp = 0;
504
- decoder->have_tma = false;
505553 decoder->state.trace_nr = buffer.trace_nr;
506554 intel_pt_log("Reference timestamp 0x%" PRIx64 "\n",
507555 decoder->ref_timestamp);
....@@ -511,10 +559,11 @@
511559 return 0;
512560 }
513561
514
-static int intel_pt_get_next_data(struct intel_pt_decoder *decoder)
562
+static int intel_pt_get_next_data(struct intel_pt_decoder *decoder,
563
+ bool reposition)
515564 {
516565 if (!decoder->next_buf)
517
- return intel_pt_get_data(decoder);
566
+ return intel_pt_get_data(decoder, reposition);
518567
519568 decoder->buf = decoder->next_buf;
520569 decoder->len = decoder->next_len;
....@@ -533,7 +582,7 @@
533582 len = decoder->len;
534583 memcpy(buf, decoder->buf, len);
535584
536
- ret = intel_pt_get_data(decoder);
585
+ ret = intel_pt_get_data(decoder, false);
537586 if (ret) {
538587 decoder->pos += old_len;
539588 return ret < 0 ? ret : -EINVAL;
....@@ -545,7 +594,8 @@
545594 memcpy(buf + len, decoder->buf, n);
546595 len += n;
547596
548
- ret = intel_pt_get_packet(buf, len, &decoder->packet);
597
+ decoder->prev_pkt_ctx = decoder->pkt_ctx;
598
+ ret = intel_pt_get_packet(buf, len, &decoder->packet, &decoder->pkt_ctx);
549599 if (ret < (int)old_len) {
550600 decoder->next_buf = decoder->buf;
551601 decoder->next_len = decoder->len;
....@@ -580,6 +630,7 @@
580630 {
581631 struct intel_pt_pkt_info pkt_info;
582632 const unsigned char *buf = decoder->buf;
633
+ enum intel_pt_pkt_ctx pkt_ctx = decoder->pkt_ctx;
583634 size_t len = decoder->len;
584635 int ret;
585636
....@@ -598,7 +649,8 @@
598649 if (!len)
599650 return INTEL_PT_NEED_MORE_BYTES;
600651
601
- ret = intel_pt_get_packet(buf, len, &pkt_info.packet);
652
+ ret = intel_pt_get_packet(buf, len, &pkt_info.packet,
653
+ &pkt_ctx);
602654 if (!ret)
603655 return INTEL_PT_NEED_MORE_BYTES;
604656 if (ret < 0)
....@@ -673,6 +725,10 @@
673725 case INTEL_PT_MNT:
674726 case INTEL_PT_PTWRITE:
675727 case INTEL_PT_PTWRITE_IP:
728
+ case INTEL_PT_BBP:
729
+ case INTEL_PT_BIP:
730
+ case INTEL_PT_BEP:
731
+ case INTEL_PT_BEP_IP:
676732 return 0;
677733
678734 case INTEL_PT_MTC:
....@@ -859,14 +915,15 @@
859915 decoder->len -= decoder->pkt_step;
860916
861917 if (!decoder->len) {
862
- ret = intel_pt_get_next_data(decoder);
918
+ ret = intel_pt_get_next_data(decoder, false);
863919 if (ret)
864920 return ret;
865921 }
866922
923
+ decoder->prev_pkt_ctx = decoder->pkt_ctx;
867924 ret = intel_pt_get_packet(decoder->buf, decoder->len,
868
- &decoder->packet);
869
- if (ret == INTEL_PT_NEED_MORE_BYTES &&
925
+ &decoder->packet, &decoder->pkt_ctx);
926
+ if (ret == INTEL_PT_NEED_MORE_BYTES && BITS_PER_LONG == 32 &&
870927 decoder->len < INTEL_PT_PKT_MAX_SZ && !decoder->next_buf) {
871928 ret = intel_pt_get_split_packet(decoder);
872929 if (ret < 0)
....@@ -1057,53 +1114,69 @@
10571114
10581115 static bool intel_pt_fup_event(struct intel_pt_decoder *decoder)
10591116 {
1117
+ enum intel_pt_sample_type type = decoder->state.type;
10601118 bool ret = false;
1119
+
1120
+ decoder->state.type &= ~INTEL_PT_BRANCH;
10611121
10621122 if (decoder->set_fup_tx_flags) {
10631123 decoder->set_fup_tx_flags = false;
10641124 decoder->tx_flags = decoder->fup_tx_flags;
1065
- decoder->state.type = INTEL_PT_TRANSACTION;
1125
+ decoder->state.type |= INTEL_PT_TRANSACTION;
10661126 if (decoder->fup_tx_flags & INTEL_PT_ABORT_TX)
10671127 decoder->state.type |= INTEL_PT_BRANCH;
1068
- decoder->state.from_ip = decoder->ip;
1069
- decoder->state.to_ip = 0;
10701128 decoder->state.flags = decoder->fup_tx_flags;
1071
- return true;
1129
+ ret = true;
10721130 }
10731131 if (decoder->set_fup_ptw) {
10741132 decoder->set_fup_ptw = false;
1075
- decoder->state.type = INTEL_PT_PTW;
1133
+ decoder->state.type |= INTEL_PT_PTW;
10761134 decoder->state.flags |= INTEL_PT_FUP_IP;
1077
- decoder->state.from_ip = decoder->ip;
1078
- decoder->state.to_ip = 0;
10791135 decoder->state.ptw_payload = decoder->fup_ptw_payload;
1080
- return true;
1136
+ ret = true;
10811137 }
10821138 if (decoder->set_fup_mwait) {
10831139 decoder->set_fup_mwait = false;
1084
- decoder->state.type = INTEL_PT_MWAIT_OP;
1085
- decoder->state.from_ip = decoder->ip;
1086
- decoder->state.to_ip = 0;
1140
+ decoder->state.type |= INTEL_PT_MWAIT_OP;
10871141 decoder->state.mwait_payload = decoder->fup_mwait_payload;
10881142 ret = true;
10891143 }
10901144 if (decoder->set_fup_pwre) {
10911145 decoder->set_fup_pwre = false;
10921146 decoder->state.type |= INTEL_PT_PWR_ENTRY;
1093
- decoder->state.type &= ~INTEL_PT_BRANCH;
1094
- decoder->state.from_ip = decoder->ip;
1095
- decoder->state.to_ip = 0;
10961147 decoder->state.pwre_payload = decoder->fup_pwre_payload;
10971148 ret = true;
10981149 }
10991150 if (decoder->set_fup_exstop) {
11001151 decoder->set_fup_exstop = false;
11011152 decoder->state.type |= INTEL_PT_EX_STOP;
1102
- decoder->state.type &= ~INTEL_PT_BRANCH;
11031153 decoder->state.flags |= INTEL_PT_FUP_IP;
1154
+ ret = true;
1155
+ }
1156
+ if (decoder->set_fup_bep) {
1157
+ decoder->set_fup_bep = false;
1158
+ decoder->state.type |= INTEL_PT_BLK_ITEMS;
1159
+ ret = true;
1160
+ }
1161
+ if (decoder->overflow) {
1162
+ decoder->overflow = false;
1163
+ if (!ret && !decoder->pge) {
1164
+ if (decoder->hop) {
1165
+ decoder->state.type = 0;
1166
+ decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
1167
+ }
1168
+ decoder->pge = true;
1169
+ decoder->state.type |= INTEL_PT_BRANCH | INTEL_PT_TRACE_BEGIN;
1170
+ decoder->state.from_ip = 0;
1171
+ decoder->state.to_ip = decoder->ip;
1172
+ return true;
1173
+ }
1174
+ }
1175
+ if (ret) {
11041176 decoder->state.from_ip = decoder->ip;
11051177 decoder->state.to_ip = 0;
1106
- ret = true;
1178
+ } else {
1179
+ decoder->state.type = type;
11071180 }
11081181 return ret;
11091182 }
....@@ -1176,7 +1249,7 @@
11761249 decoder->pge = false;
11771250 decoder->continuous_period = false;
11781251 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1179
- decoder->state.to_ip = 0;
1252
+ decoder->state.type |= INTEL_PT_TRACE_END;
11801253 return 0;
11811254 }
11821255 if (err == INTEL_PT_RETURN)
....@@ -1190,9 +1263,13 @@
11901263 decoder->continuous_period = false;
11911264 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
11921265 decoder->state.from_ip = decoder->ip;
1193
- decoder->state.to_ip = 0;
1194
- if (decoder->packet.count != 0)
1266
+ if (decoder->packet.count == 0) {
1267
+ decoder->state.to_ip = 0;
1268
+ } else {
1269
+ decoder->state.to_ip = decoder->last_ip;
11951270 decoder->ip = decoder->last_ip;
1271
+ }
1272
+ decoder->state.type |= INTEL_PT_TRACE_END;
11961273 } else {
11971274 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
11981275 decoder->state.from_ip = decoder->ip;
....@@ -1219,7 +1296,8 @@
12191296 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
12201297 decoder->ip = to_ip;
12211298 decoder->state.from_ip = decoder->ip;
1222
- decoder->state.to_ip = 0;
1299
+ decoder->state.to_ip = to_ip;
1300
+ decoder->state.type |= INTEL_PT_TRACE_END;
12231301 return 0;
12241302 }
12251303 intel_pt_log_at("ERROR: Conditional branch when expecting indirect branch",
....@@ -1317,10 +1395,10 @@
13171395 decoder->ip += intel_pt_insn.length;
13181396 return 0;
13191397 }
1398
+ decoder->sample_cyc = false;
13201399 decoder->ip += intel_pt_insn.length;
13211400 if (!decoder->tnt.count) {
1322
- decoder->sample_timestamp = decoder->timestamp;
1323
- decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
1401
+ intel_pt_update_sample_time(decoder);
13241402 return -EAGAIN;
13251403 }
13261404 decoder->tnt.payload <<= 1;
....@@ -1354,6 +1432,21 @@
13541432 return 0;
13551433 }
13561434
1435
+static uint64_t intel_pt_8b_tsc(uint64_t timestamp, uint64_t ref_timestamp)
1436
+{
1437
+ timestamp |= (ref_timestamp & (0xffULL << 56));
1438
+
1439
+ if (timestamp < ref_timestamp) {
1440
+ if (ref_timestamp - timestamp > (1ULL << 55))
1441
+ timestamp += (1ULL << 56);
1442
+ } else {
1443
+ if (timestamp - ref_timestamp > (1ULL << 55))
1444
+ timestamp -= (1ULL << 56);
1445
+ }
1446
+
1447
+ return timestamp;
1448
+}
1449
+
13571450 static void intel_pt_calc_tsc_timestamp(struct intel_pt_decoder *decoder)
13581451 {
13591452 uint64_t timestamp;
....@@ -1361,15 +1454,8 @@
13611454 decoder->have_tma = false;
13621455
13631456 if (decoder->ref_timestamp) {
1364
- timestamp = decoder->packet.payload |
1365
- (decoder->ref_timestamp & (0xffULL << 56));
1366
- if (timestamp < decoder->ref_timestamp) {
1367
- if (decoder->ref_timestamp - timestamp > (1ULL << 55))
1368
- timestamp += (1ULL << 56);
1369
- } else {
1370
- if (timestamp - decoder->ref_timestamp > (1ULL << 55))
1371
- timestamp -= (1ULL << 56);
1372
- }
1457
+ timestamp = intel_pt_8b_tsc(decoder->packet.payload,
1458
+ decoder->ref_timestamp);
13731459 decoder->tsc_timestamp = timestamp;
13741460 decoder->timestamp = timestamp;
13751461 decoder->ref_timestamp = 0;
....@@ -1408,9 +1494,54 @@
14081494 intel_pt_log("ERROR: Buffer overflow\n");
14091495 intel_pt_clear_tx_flags(decoder);
14101496 decoder->timestamp_insn_cnt = 0;
1411
- decoder->pkt_state = INTEL_PT_STATE_ERR_RESYNC;
1497
+ decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1498
+ decoder->state.from_ip = decoder->ip;
1499
+ decoder->ip = 0;
1500
+ decoder->pge = false;
1501
+ decoder->set_fup_tx_flags = false;
1502
+ decoder->set_fup_ptw = false;
1503
+ decoder->set_fup_mwait = false;
1504
+ decoder->set_fup_pwre = false;
1505
+ decoder->set_fup_exstop = false;
1506
+ decoder->set_fup_bep = false;
14121507 decoder->overflow = true;
14131508 return -EOVERFLOW;
1509
+}
1510
+
1511
+static inline void intel_pt_mtc_cyc_cnt_pge(struct intel_pt_decoder *decoder)
1512
+{
1513
+ if (decoder->have_cyc)
1514
+ return;
1515
+
1516
+ decoder->cyc_cnt_timestamp = decoder->timestamp;
1517
+ decoder->base_cyc_cnt = decoder->tot_cyc_cnt;
1518
+}
1519
+
1520
+static inline void intel_pt_mtc_cyc_cnt_cbr(struct intel_pt_decoder *decoder)
1521
+{
1522
+ decoder->tsc_to_cyc = decoder->cbr / decoder->max_non_turbo_ratio_fp;
1523
+
1524
+ if (decoder->pge)
1525
+ intel_pt_mtc_cyc_cnt_pge(decoder);
1526
+}
1527
+
1528
+static inline void intel_pt_mtc_cyc_cnt_upd(struct intel_pt_decoder *decoder)
1529
+{
1530
+ uint64_t tot_cyc_cnt, tsc_delta;
1531
+
1532
+ if (decoder->have_cyc)
1533
+ return;
1534
+
1535
+ decoder->sample_cyc = true;
1536
+
1537
+ if (!decoder->pge || decoder->timestamp <= decoder->cyc_cnt_timestamp)
1538
+ return;
1539
+
1540
+ tsc_delta = decoder->timestamp - decoder->cyc_cnt_timestamp;
1541
+ tot_cyc_cnt = tsc_delta * decoder->tsc_to_cyc + decoder->base_cyc_cnt;
1542
+
1543
+ if (tot_cyc_cnt > decoder->tot_cyc_cnt)
1544
+ decoder->tot_cyc_cnt = tot_cyc_cnt;
14141545 }
14151546
14161547 static void intel_pt_calc_tma(struct intel_pt_decoder *decoder)
....@@ -1421,6 +1552,11 @@
14211552
14221553 if (!decoder->tsc_ctc_ratio_d)
14231554 return;
1555
+
1556
+ if (decoder->pge && !decoder->in_psb)
1557
+ intel_pt_mtc_cyc_cnt_pge(decoder);
1558
+ else
1559
+ intel_pt_mtc_cyc_cnt_upd(decoder);
14241560
14251561 decoder->last_mtc = (ctc >> decoder->mtc_shift) & 0xff;
14261562 decoder->ctc_timestamp = decoder->tsc_timestamp - fc;
....@@ -1477,6 +1613,8 @@
14771613 else
14781614 decoder->timestamp = timestamp;
14791615
1616
+ intel_pt_mtc_cyc_cnt_upd(decoder);
1617
+
14801618 decoder->timestamp_insn_cnt = 0;
14811619 decoder->last_mtc = mtc;
14821620
....@@ -1486,6 +1624,8 @@
14861624 decoder->have_calc_cyc_to_tsc = false;
14871625 intel_pt_calc_cyc_to_tsc(decoder, true);
14881626 }
1627
+
1628
+ intel_pt_log_to("Setting timestamp", decoder->timestamp);
14891629 }
14901630
14911631 static void intel_pt_calc_cbr(struct intel_pt_decoder *decoder)
....@@ -1499,6 +1639,10 @@
14991639
15001640 decoder->cbr = cbr;
15011641 decoder->cbr_cyc_to_tsc = decoder->max_non_turbo_ratio_fp / cbr;
1642
+ decoder->cyc_ref_timestamp = decoder->timestamp;
1643
+ decoder->cycle_cnt = 0;
1644
+
1645
+ intel_pt_mtc_cyc_cnt_cbr(decoder);
15021646 }
15031647
15041648 static void intel_pt_calc_cyc_timestamp(struct intel_pt_decoder *decoder)
....@@ -1508,6 +1652,9 @@
15081652 decoder->have_cyc = true;
15091653
15101654 decoder->cycle_cnt += decoder->packet.payload;
1655
+ if (decoder->pge)
1656
+ decoder->tot_cyc_cnt += decoder->packet.payload;
1657
+ decoder->sample_cyc = true;
15111658
15121659 if (!decoder->cyc_ref_timestamp)
15131660 return;
....@@ -1526,6 +1673,48 @@
15261673 decoder->timestamp = timestamp;
15271674
15281675 decoder->timestamp_insn_cnt = 0;
1676
+
1677
+ intel_pt_log_to("Setting timestamp", decoder->timestamp);
1678
+}
1679
+
1680
+static void intel_pt_bbp(struct intel_pt_decoder *decoder)
1681
+{
1682
+ if (decoder->prev_pkt_ctx == INTEL_PT_NO_CTX) {
1683
+ memset(decoder->state.items.mask, 0, sizeof(decoder->state.items.mask));
1684
+ decoder->state.items.is_32_bit = false;
1685
+ }
1686
+ decoder->blk_type = decoder->packet.payload;
1687
+ decoder->blk_type_pos = intel_pt_blk_type_pos(decoder->blk_type);
1688
+ if (decoder->blk_type == INTEL_PT_GP_REGS)
1689
+ decoder->state.items.is_32_bit = decoder->packet.count;
1690
+ if (decoder->blk_type_pos < 0) {
1691
+ intel_pt_log("WARNING: Unknown block type %u\n",
1692
+ decoder->blk_type);
1693
+ } else if (decoder->state.items.mask[decoder->blk_type_pos]) {
1694
+ intel_pt_log("WARNING: Duplicate block type %u\n",
1695
+ decoder->blk_type);
1696
+ }
1697
+}
1698
+
1699
+static void intel_pt_bip(struct intel_pt_decoder *decoder)
1700
+{
1701
+ uint32_t id = decoder->packet.count;
1702
+ uint32_t bit = 1 << id;
1703
+ int pos = decoder->blk_type_pos;
1704
+
1705
+ if (pos < 0 || id >= INTEL_PT_BLK_ITEM_ID_CNT) {
1706
+ intel_pt_log("WARNING: Unknown block item %u type %d\n",
1707
+ id, decoder->blk_type);
1708
+ return;
1709
+ }
1710
+
1711
+ if (decoder->state.items.mask[pos] & bit) {
1712
+ intel_pt_log("WARNING: Duplicate block item %u type %d\n",
1713
+ id, decoder->blk_type);
1714
+ }
1715
+
1716
+ decoder->state.items.mask[pos] |= bit;
1717
+ decoder->state.items.val[pos][id] = decoder->packet.payload;
15291718 }
15301719
15311720 /* Walk PSB+ packets when already in sync. */
....@@ -1533,14 +1722,17 @@
15331722 {
15341723 int err;
15351724
1725
+ decoder->in_psb = true;
1726
+
15361727 while (1) {
15371728 err = intel_pt_get_next_packet(decoder);
15381729 if (err)
1539
- return err;
1730
+ goto out;
15401731
15411732 switch (decoder->packet.type) {
15421733 case INTEL_PT_PSBEND:
1543
- return 0;
1734
+ err = 0;
1735
+ goto out;
15441736
15451737 case INTEL_PT_TIP_PGD:
15461738 case INTEL_PT_TIP_PGE:
....@@ -1556,12 +1748,18 @@
15561748 case INTEL_PT_MWAIT:
15571749 case INTEL_PT_PWRE:
15581750 case INTEL_PT_PWRX:
1751
+ case INTEL_PT_BBP:
1752
+ case INTEL_PT_BIP:
1753
+ case INTEL_PT_BEP:
1754
+ case INTEL_PT_BEP_IP:
15591755 decoder->have_tma = false;
15601756 intel_pt_log("ERROR: Unexpected packet\n");
1561
- return -EAGAIN;
1757
+ err = -EAGAIN;
1758
+ goto out;
15621759
15631760 case INTEL_PT_OVF:
1564
- return intel_pt_overflow(decoder);
1761
+ err = intel_pt_overflow(decoder);
1762
+ goto out;
15651763
15661764 case INTEL_PT_TSC:
15671765 intel_pt_calc_tsc_timestamp(decoder);
....@@ -1585,8 +1783,14 @@
15851783
15861784 case INTEL_PT_FUP:
15871785 decoder->pge = true;
1588
- if (decoder->packet.count)
1786
+ if (decoder->packet.count) {
15891787 intel_pt_set_last_ip(decoder);
1788
+ if (decoder->hop) {
1789
+ /* Act on FUP at PSBEND */
1790
+ decoder->ip = decoder->last_ip;
1791
+ decoder->hop_psb_fup = true;
1792
+ }
1793
+ }
15901794 break;
15911795
15921796 case INTEL_PT_MODE_TSX:
....@@ -1610,6 +1814,10 @@
16101814 break;
16111815 }
16121816 }
1817
+out:
1818
+ decoder->in_psb = false;
1819
+
1820
+ return err;
16131821 }
16141822
16151823 static int intel_pt_walk_fup_tip(struct intel_pt_decoder *decoder)
....@@ -1646,6 +1854,10 @@
16461854 case INTEL_PT_MWAIT:
16471855 case INTEL_PT_PWRE:
16481856 case INTEL_PT_PWRX:
1857
+ case INTEL_PT_BBP:
1858
+ case INTEL_PT_BIP:
1859
+ case INTEL_PT_BEP:
1860
+ case INTEL_PT_BEP_IP:
16491861 intel_pt_log("ERROR: Missing TIP after FUP\n");
16501862 decoder->pkt_state = INTEL_PT_STATE_ERR3;
16511863 decoder->pkt_step = 0;
....@@ -1660,14 +1872,15 @@
16601872
16611873 case INTEL_PT_TIP_PGD:
16621874 decoder->state.from_ip = decoder->ip;
1663
- decoder->state.to_ip = 0;
1664
- if (decoder->packet.count != 0) {
1875
+ if (decoder->packet.count == 0) {
1876
+ decoder->state.to_ip = 0;
1877
+ } else {
16651878 intel_pt_set_ip(decoder);
1666
- intel_pt_log("Omitting PGD ip " x64_fmt "\n",
1667
- decoder->ip);
1879
+ decoder->state.to_ip = decoder->ip;
16681880 }
16691881 decoder->pge = false;
16701882 decoder->continuous_period = false;
1883
+ decoder->state.type |= INTEL_PT_TRACE_END;
16711884 return 0;
16721885
16731886 case INTEL_PT_TIP_PGE:
....@@ -1681,6 +1894,8 @@
16811894 intel_pt_set_ip(decoder);
16821895 decoder->state.to_ip = decoder->ip;
16831896 }
1897
+ decoder->state.type |= INTEL_PT_TRACE_BEGIN;
1898
+ intel_pt_mtc_cyc_cnt_pge(decoder);
16841899 return 0;
16851900
16861901 case INTEL_PT_TIP:
....@@ -1722,8 +1937,136 @@
17221937 }
17231938 }
17241939
1940
+static int intel_pt_resample(struct intel_pt_decoder *decoder)
1941
+{
1942
+ decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
1943
+ decoder->state.type = INTEL_PT_INSTRUCTION;
1944
+ decoder->state.from_ip = decoder->ip;
1945
+ decoder->state.to_ip = 0;
1946
+ return 0;
1947
+}
1948
+
1949
+#define HOP_PROCESS 0
1950
+#define HOP_IGNORE 1
1951
+#define HOP_RETURN 2
1952
+#define HOP_AGAIN 3
1953
+
1954
+static int intel_pt_scan_for_psb(struct intel_pt_decoder *decoder);
1955
+
1956
+/* Hop mode: Ignore TNT, do not walk code, but get ip from FUPs and TIPs */
1957
+static int intel_pt_hop_trace(struct intel_pt_decoder *decoder, bool *no_tip, int *err)
1958
+{
1959
+ *err = 0;
1960
+
1961
+ /* Leap from PSB to PSB, getting ip from FUP within PSB+ */
1962
+ if (decoder->leap && !decoder->in_psb && decoder->packet.type != INTEL_PT_PSB) {
1963
+ *err = intel_pt_scan_for_psb(decoder);
1964
+ if (*err)
1965
+ return HOP_RETURN;
1966
+ }
1967
+
1968
+ switch (decoder->packet.type) {
1969
+ case INTEL_PT_TNT:
1970
+ return HOP_IGNORE;
1971
+
1972
+ case INTEL_PT_TIP_PGD:
1973
+ decoder->pge = false;
1974
+ if (!decoder->packet.count)
1975
+ return HOP_IGNORE;
1976
+ intel_pt_set_ip(decoder);
1977
+ decoder->state.type |= INTEL_PT_TRACE_END;
1978
+ decoder->state.from_ip = 0;
1979
+ decoder->state.to_ip = decoder->ip;
1980
+ return HOP_RETURN;
1981
+
1982
+ case INTEL_PT_TIP:
1983
+ if (!decoder->packet.count)
1984
+ return HOP_IGNORE;
1985
+ intel_pt_set_ip(decoder);
1986
+ decoder->state.type = INTEL_PT_INSTRUCTION;
1987
+ decoder->state.from_ip = decoder->ip;
1988
+ decoder->state.to_ip = 0;
1989
+ return HOP_RETURN;
1990
+
1991
+ case INTEL_PT_FUP:
1992
+ if (!decoder->packet.count)
1993
+ return HOP_IGNORE;
1994
+ intel_pt_set_ip(decoder);
1995
+ if (decoder->set_fup_mwait || decoder->set_fup_pwre)
1996
+ *no_tip = true;
1997
+ if (!decoder->branch_enable || !decoder->pge)
1998
+ *no_tip = true;
1999
+ if (*no_tip) {
2000
+ decoder->state.type = INTEL_PT_INSTRUCTION;
2001
+ decoder->state.from_ip = decoder->ip;
2002
+ decoder->state.to_ip = 0;
2003
+ intel_pt_fup_event(decoder);
2004
+ return HOP_RETURN;
2005
+ }
2006
+ intel_pt_fup_event(decoder);
2007
+ decoder->state.type |= INTEL_PT_INSTRUCTION | INTEL_PT_BRANCH;
2008
+ *err = intel_pt_walk_fup_tip(decoder);
2009
+ if (!*err && decoder->state.to_ip)
2010
+ decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
2011
+ return HOP_RETURN;
2012
+
2013
+ case INTEL_PT_PSB:
2014
+ decoder->last_ip = 0;
2015
+ decoder->have_last_ip = true;
2016
+ decoder->hop_psb_fup = false;
2017
+ *err = intel_pt_walk_psbend(decoder);
2018
+ if (*err == -EAGAIN)
2019
+ return HOP_AGAIN;
2020
+ if (*err)
2021
+ return HOP_RETURN;
2022
+ if (decoder->hop_psb_fup) {
2023
+ decoder->hop_psb_fup = false;
2024
+ decoder->state.type = INTEL_PT_INSTRUCTION;
2025
+ decoder->state.from_ip = decoder->ip;
2026
+ decoder->state.to_ip = 0;
2027
+ return HOP_RETURN;
2028
+ }
2029
+ if (decoder->cbr != decoder->cbr_seen) {
2030
+ decoder->state.type = 0;
2031
+ return HOP_RETURN;
2032
+ }
2033
+ return HOP_IGNORE;
2034
+
2035
+ case INTEL_PT_BAD:
2036
+ case INTEL_PT_PAD:
2037
+ case INTEL_PT_TIP_PGE:
2038
+ case INTEL_PT_TSC:
2039
+ case INTEL_PT_TMA:
2040
+ case INTEL_PT_MODE_EXEC:
2041
+ case INTEL_PT_MODE_TSX:
2042
+ case INTEL_PT_MTC:
2043
+ case INTEL_PT_CYC:
2044
+ case INTEL_PT_VMCS:
2045
+ case INTEL_PT_PSBEND:
2046
+ case INTEL_PT_CBR:
2047
+ case INTEL_PT_TRACESTOP:
2048
+ case INTEL_PT_PIP:
2049
+ case INTEL_PT_OVF:
2050
+ case INTEL_PT_MNT:
2051
+ case INTEL_PT_PTWRITE:
2052
+ case INTEL_PT_PTWRITE_IP:
2053
+ case INTEL_PT_EXSTOP:
2054
+ case INTEL_PT_EXSTOP_IP:
2055
+ case INTEL_PT_MWAIT:
2056
+ case INTEL_PT_PWRE:
2057
+ case INTEL_PT_PWRX:
2058
+ case INTEL_PT_BBP:
2059
+ case INTEL_PT_BIP:
2060
+ case INTEL_PT_BEP:
2061
+ case INTEL_PT_BEP_IP:
2062
+ default:
2063
+ return HOP_PROCESS;
2064
+ }
2065
+}
2066
+
17252067 static int intel_pt_walk_trace(struct intel_pt_decoder *decoder)
17262068 {
2069
+ int last_packet_type = INTEL_PT_PAD;
17272070 bool no_tip = false;
17282071 int err;
17292072
....@@ -1732,6 +2075,26 @@
17322075 if (err)
17332076 return err;
17342077 next:
2078
+ err = 0;
2079
+ if (decoder->cyc_threshold) {
2080
+ if (decoder->sample_cyc && last_packet_type != INTEL_PT_CYC)
2081
+ decoder->sample_cyc = false;
2082
+ last_packet_type = decoder->packet.type;
2083
+ }
2084
+
2085
+ if (decoder->hop) {
2086
+ switch (intel_pt_hop_trace(decoder, &no_tip, &err)) {
2087
+ case HOP_IGNORE:
2088
+ continue;
2089
+ case HOP_RETURN:
2090
+ return err;
2091
+ case HOP_AGAIN:
2092
+ goto next;
2093
+ default:
2094
+ break;
2095
+ }
2096
+ }
2097
+
17352098 switch (decoder->packet.type) {
17362099 case INTEL_PT_TNT:
17372100 if (!decoder->packet.count)
....@@ -1751,6 +2114,8 @@
17512114
17522115 case INTEL_PT_TIP_PGE: {
17532116 decoder->pge = true;
2117
+ decoder->overflow = false;
2118
+ intel_pt_mtc_cyc_cnt_pge(decoder);
17542119 if (decoder->packet.count == 0) {
17552120 intel_pt_log_at("Skipping zero TIP.PGE",
17562121 decoder->pos);
....@@ -1759,6 +2124,13 @@
17592124 intel_pt_set_ip(decoder);
17602125 decoder->state.from_ip = 0;
17612126 decoder->state.to_ip = decoder->ip;
2127
+ decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2128
+ /*
2129
+ * In hop mode, resample to get the to_ip as an
2130
+ * "instruction" sample.
2131
+ */
2132
+ if (decoder->hop)
2133
+ decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
17622134 return 0;
17632135 }
17642136
....@@ -1779,7 +2151,7 @@
17792151 break;
17802152 }
17812153 intel_pt_set_last_ip(decoder);
1782
- if (!decoder->branch_enable) {
2154
+ if (!decoder->branch_enable || !decoder->pge) {
17832155 decoder->ip = decoder->last_ip;
17842156 if (intel_pt_fup_event(decoder))
17852157 return 0;
....@@ -1817,6 +2189,15 @@
18172189 goto next;
18182190 if (err)
18192191 return err;
2192
+ /*
2193
+ * PSB+ CBR will not have changed but cater for the
2194
+ * possibility of another CBR change that gets caught up
2195
+ * in the PSB+.
2196
+ */
2197
+ if (decoder->cbr != decoder->cbr_seen) {
2198
+ decoder->state.type = 0;
2199
+ return 0;
2200
+ }
18202201 break;
18212202
18222203 case INTEL_PT_PIP:
....@@ -1857,14 +2238,8 @@
18572238
18582239 case INTEL_PT_CBR:
18592240 intel_pt_calc_cbr(decoder);
1860
- if (!decoder->branch_enable &&
1861
- decoder->cbr != decoder->cbr_seen) {
1862
- decoder->cbr_seen = decoder->cbr;
1863
- decoder->state.type = INTEL_PT_CBR_CHG;
1864
- decoder->state.from_ip = decoder->ip;
1865
- decoder->state.to_ip = 0;
1866
- decoder->state.cbr_payload =
1867
- decoder->packet.payload;
2241
+ if (decoder->cbr != decoder->cbr_seen) {
2242
+ decoder->state.type = 0;
18682243 return 0;
18692244 }
18702245 break;
....@@ -1875,7 +2250,7 @@
18752250
18762251 case INTEL_PT_MODE_TSX:
18772252 /* MODE_TSX need not be followed by FUP */
1878
- if (!decoder->pge) {
2253
+ if (!decoder->pge || decoder->in_psb) {
18792254 intel_pt_update_in_tx(decoder);
18802255 break;
18812256 }
....@@ -1958,6 +2333,33 @@
19582333 decoder->state.pwrx_payload = decoder->packet.payload;
19592334 return 0;
19602335
2336
+ case INTEL_PT_BBP:
2337
+ intel_pt_bbp(decoder);
2338
+ break;
2339
+
2340
+ case INTEL_PT_BIP:
2341
+ intel_pt_bip(decoder);
2342
+ break;
2343
+
2344
+ case INTEL_PT_BEP:
2345
+ decoder->state.type = INTEL_PT_BLK_ITEMS;
2346
+ decoder->state.from_ip = decoder->ip;
2347
+ decoder->state.to_ip = 0;
2348
+ return 0;
2349
+
2350
+ case INTEL_PT_BEP_IP:
2351
+ err = intel_pt_get_next_packet(decoder);
2352
+ if (err)
2353
+ return err;
2354
+ if (decoder->packet.type == INTEL_PT_FUP) {
2355
+ decoder->set_fup_bep = true;
2356
+ no_tip = true;
2357
+ } else {
2358
+ intel_pt_log_at("ERROR: Missing FUP after BEP",
2359
+ decoder->pos);
2360
+ }
2361
+ goto next;
2362
+
19612363 default:
19622364 return intel_pt_bug(decoder);
19632365 }
....@@ -1976,10 +2378,12 @@
19762378 {
19772379 int err;
19782380
2381
+ decoder->in_psb = true;
2382
+
19792383 while (1) {
19802384 err = intel_pt_get_next_packet(decoder);
19812385 if (err)
1982
- return err;
2386
+ goto out;
19832387
19842388 switch (decoder->packet.type) {
19852389 case INTEL_PT_TIP_PGD:
....@@ -1994,8 +2398,13 @@
19942398 case INTEL_PT_MWAIT:
19952399 case INTEL_PT_PWRE:
19962400 case INTEL_PT_PWRX:
2401
+ case INTEL_PT_BBP:
2402
+ case INTEL_PT_BIP:
2403
+ case INTEL_PT_BEP:
2404
+ case INTEL_PT_BEP_IP:
19972405 intel_pt_log("ERROR: Unexpected packet\n");
1998
- return -ENOENT;
2406
+ err = -ENOENT;
2407
+ goto out;
19992408
20002409 case INTEL_PT_FUP:
20012410 decoder->pge = true;
....@@ -2054,16 +2463,20 @@
20542463 decoder->pkt_state = INTEL_PT_STATE_ERR4;
20552464 else
20562465 decoder->pkt_state = INTEL_PT_STATE_ERR3;
2057
- return -ENOENT;
2466
+ err = -ENOENT;
2467
+ goto out;
20582468
20592469 case INTEL_PT_BAD: /* Does not happen */
2060
- return intel_pt_bug(decoder);
2470
+ err = intel_pt_bug(decoder);
2471
+ goto out;
20612472
20622473 case INTEL_PT_OVF:
2063
- return intel_pt_overflow(decoder);
2474
+ err = intel_pt_overflow(decoder);
2475
+ goto out;
20642476
20652477 case INTEL_PT_PSBEND:
2066
- return 0;
2478
+ err = 0;
2479
+ goto out;
20672480
20682481 case INTEL_PT_PSB:
20692482 case INTEL_PT_VMCS:
....@@ -2073,6 +2486,10 @@
20732486 break;
20742487 }
20752488 }
2489
+out:
2490
+ decoder->in_psb = false;
2491
+
2492
+ return err;
20762493 }
20772494
20782495 static int intel_pt_walk_to_ip(struct intel_pt_decoder *decoder)
....@@ -2087,15 +2504,31 @@
20872504 switch (decoder->packet.type) {
20882505 case INTEL_PT_TIP_PGD:
20892506 decoder->continuous_period = false;
2090
- __fallthrough;
2091
- case INTEL_PT_TIP_PGE:
2092
- case INTEL_PT_TIP:
2093
- decoder->pge = decoder->packet.type != INTEL_PT_TIP_PGD;
2507
+ decoder->pge = false;
20942508 if (intel_pt_have_ip(decoder))
20952509 intel_pt_set_ip(decoder);
2096
- if (decoder->ip)
2097
- return 0;
2098
- break;
2510
+ if (!decoder->ip)
2511
+ break;
2512
+ decoder->state.type |= INTEL_PT_TRACE_END;
2513
+ return 0;
2514
+
2515
+ case INTEL_PT_TIP_PGE:
2516
+ decoder->pge = true;
2517
+ intel_pt_mtc_cyc_cnt_pge(decoder);
2518
+ if (intel_pt_have_ip(decoder))
2519
+ intel_pt_set_ip(decoder);
2520
+ if (!decoder->ip)
2521
+ break;
2522
+ decoder->state.type |= INTEL_PT_TRACE_BEGIN;
2523
+ return 0;
2524
+
2525
+ case INTEL_PT_TIP:
2526
+ decoder->pge = true;
2527
+ if (intel_pt_have_ip(decoder))
2528
+ intel_pt_set_ip(decoder);
2529
+ if (!decoder->ip)
2530
+ break;
2531
+ return 0;
20992532
21002533 case INTEL_PT_FUP:
21012534 if (intel_pt_have_ip(decoder))
....@@ -2175,6 +2608,10 @@
21752608 case INTEL_PT_MWAIT:
21762609 case INTEL_PT_PWRE:
21772610 case INTEL_PT_PWRX:
2611
+ case INTEL_PT_BBP:
2612
+ case INTEL_PT_BIP:
2613
+ case INTEL_PT_BEP:
2614
+ case INTEL_PT_BEP_IP:
21782615 default:
21792616 break;
21802617 }
....@@ -2190,10 +2627,11 @@
21902627 decoder->set_fup_mwait = false;
21912628 decoder->set_fup_pwre = false;
21922629 decoder->set_fup_exstop = false;
2630
+ decoder->set_fup_bep = false;
2631
+ decoder->overflow = false;
21932632
21942633 if (!decoder->branch_enable) {
21952634 decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2196
- decoder->overflow = false;
21972635 decoder->state.type = 0; /* Do not have a sample */
21982636 return 0;
21992637 }
....@@ -2203,8 +2641,11 @@
22032641 if (err)
22042642 return err;
22052643
2206
- decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2207
- decoder->overflow = false;
2644
+ /* In hop mode, resample to get the to_ip as an "instruction" sample */
2645
+ if (decoder->hop)
2646
+ decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
2647
+ else
2648
+ decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
22082649
22092650 decoder->state.from_ip = 0;
22102651 decoder->state.to_ip = decoder->ip;
....@@ -2247,7 +2688,7 @@
22472688 decoder->pos += decoder->len;
22482689 decoder->len = 0;
22492690
2250
- ret = intel_pt_get_next_data(decoder);
2691
+ ret = intel_pt_get_next_data(decoder, false);
22512692 if (ret)
22522693 return ret;
22532694
....@@ -2273,7 +2714,7 @@
22732714 intel_pt_log("Scanning for PSB\n");
22742715 while (1) {
22752716 if (!decoder->len) {
2276
- ret = intel_pt_get_next_data(decoder);
2717
+ ret = intel_pt_get_next_data(decoder, false);
22772718 if (ret)
22782719 return ret;
22792720 }
....@@ -2311,12 +2752,13 @@
23112752 decoder->ip = 0;
23122753 intel_pt_clear_stack(&decoder->stack);
23132754
2755
+leap:
23142756 err = intel_pt_scan_for_psb(decoder);
23152757 if (err)
23162758 return err;
23172759
23182760 decoder->have_last_ip = true;
2319
- decoder->pkt_state = INTEL_PT_STATE_NO_IP;
2761
+ decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
23202762
23212763 err = intel_pt_walk_psb(decoder);
23222764 if (err)
....@@ -2324,7 +2766,20 @@
23242766
23252767 if (decoder->ip) {
23262768 decoder->state.type = 0; /* Do not have a sample */
2327
- decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2769
+ /*
2770
+ * In hop mode, resample to get the PSB FUP ip as an
2771
+ * "instruction" sample.
2772
+ */
2773
+ if (decoder->hop)
2774
+ decoder->pkt_state = INTEL_PT_STATE_RESAMPLE;
2775
+ else
2776
+ decoder->pkt_state = INTEL_PT_STATE_IN_SYNC;
2777
+ } else if (decoder->leap) {
2778
+ /*
2779
+ * In leap mode, only PSB+ is decoded, so keeping leaping to the
2780
+ * next PSB until there is an ip.
2781
+ */
2782
+ goto leap;
23282783 } else {
23292784 return intel_pt_sync_ip(decoder);
23302785 }
....@@ -2388,6 +2843,9 @@
23882843 if (err == -EAGAIN)
23892844 err = intel_pt_walk_trace(decoder);
23902845 break;
2846
+ case INTEL_PT_STATE_RESAMPLE:
2847
+ err = intel_pt_resample(decoder);
2848
+ break;
23912849 default:
23922850 err = intel_pt_bug(decoder);
23932851 break;
....@@ -2396,26 +2854,43 @@
23962854
23972855 if (err) {
23982856 decoder->state.err = intel_pt_ext_err(err);
2399
- decoder->state.from_ip = decoder->ip;
2400
- decoder->sample_timestamp = decoder->timestamp;
2401
- decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2857
+ if (err != -EOVERFLOW)
2858
+ decoder->state.from_ip = decoder->ip;
2859
+ intel_pt_update_sample_time(decoder);
2860
+ decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
24022861 } else {
24032862 decoder->state.err = 0;
2404
- if (decoder->cbr != decoder->cbr_seen && decoder->state.type) {
2863
+ if (decoder->cbr != decoder->cbr_seen) {
24052864 decoder->cbr_seen = decoder->cbr;
2865
+ if (!decoder->state.type) {
2866
+ decoder->state.from_ip = decoder->ip;
2867
+ decoder->state.to_ip = 0;
2868
+ }
24062869 decoder->state.type |= INTEL_PT_CBR_CHG;
24072870 decoder->state.cbr_payload = decoder->cbr_payload;
2871
+ decoder->state.cbr = decoder->cbr;
24082872 }
24092873 if (intel_pt_sample_time(decoder->pkt_state)) {
2410
- decoder->sample_timestamp = decoder->timestamp;
2411
- decoder->sample_insn_cnt = decoder->timestamp_insn_cnt;
2874
+ intel_pt_update_sample_time(decoder);
2875
+ if (decoder->sample_cyc) {
2876
+ decoder->sample_tot_cyc_cnt = decoder->tot_cyc_cnt;
2877
+ decoder->state.flags |= INTEL_PT_SAMPLE_IPC;
2878
+ decoder->sample_cyc = false;
2879
+ }
24122880 }
2881
+ /*
2882
+ * When using only TSC/MTC to compute cycles, IPC can be
2883
+ * sampled as soon as the cycle count changes.
2884
+ */
2885
+ if (!decoder->have_cyc)
2886
+ decoder->state.flags |= INTEL_PT_SAMPLE_IPC;
24132887 }
24142888
24152889 decoder->state.timestamp = decoder->sample_timestamp;
24162890 decoder->state.est_timestamp = intel_pt_est_timestamp(decoder);
24172891 decoder->state.cr3 = decoder->cr3;
24182892 decoder->state.tot_insn_cnt = decoder->tot_insn_cnt;
2893
+ decoder->state.tot_cyc_cnt = decoder->sample_tot_cyc_cnt;
24192894
24202895 return &decoder->state;
24212896 }
....@@ -2519,11 +2994,12 @@
25192994 static bool intel_pt_next_tsc(unsigned char *buf, size_t len, uint64_t *tsc,
25202995 size_t *rem)
25212996 {
2997
+ enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
25222998 struct intel_pt_pkt packet;
25232999 int ret;
25243000
25253001 while (len) {
2526
- ret = intel_pt_get_packet(buf, len, &packet);
3002
+ ret = intel_pt_get_packet(buf, len, &packet, &ctx);
25273003 if (ret <= 0)
25283004 return false;
25293005 if (packet.type == INTEL_PT_TSC) {
....@@ -2725,3 +3201,131 @@
27253201 return buf_b; /* No overlap */
27263202 }
27273203 }
3204
+
3205
+/**
3206
+ * struct fast_forward_data - data used by intel_pt_ff_cb().
3207
+ * @timestamp: timestamp to fast forward towards
3208
+ * @buf_timestamp: buffer timestamp of last buffer with trace data earlier than
3209
+ * the fast forward timestamp.
3210
+ */
3211
+struct fast_forward_data {
3212
+ uint64_t timestamp;
3213
+ uint64_t buf_timestamp;
3214
+};
3215
+
3216
+/**
3217
+ * intel_pt_ff_cb - fast forward lookahead callback.
3218
+ * @buffer: Intel PT trace buffer
3219
+ * @data: opaque pointer to fast forward data (struct fast_forward_data)
3220
+ *
3221
+ * Determine if @buffer trace is past the fast forward timestamp.
3222
+ *
3223
+ * Return: 1 (stop lookahead) if @buffer trace is past the fast forward
3224
+ * timestamp, and 0 otherwise.
3225
+ */
3226
+static int intel_pt_ff_cb(struct intel_pt_buffer *buffer, void *data)
3227
+{
3228
+ struct fast_forward_data *d = data;
3229
+ unsigned char *buf;
3230
+ uint64_t tsc;
3231
+ size_t rem;
3232
+ size_t len;
3233
+
3234
+ buf = (unsigned char *)buffer->buf;
3235
+ len = buffer->len;
3236
+
3237
+ if (!intel_pt_next_psb(&buf, &len) ||
3238
+ !intel_pt_next_tsc(buf, len, &tsc, &rem))
3239
+ return 0;
3240
+
3241
+ tsc = intel_pt_8b_tsc(tsc, buffer->ref_timestamp);
3242
+
3243
+ intel_pt_log("Buffer 1st timestamp " x64_fmt " ref timestamp " x64_fmt "\n",
3244
+ tsc, buffer->ref_timestamp);
3245
+
3246
+ /*
3247
+ * If the buffer contains a timestamp earlier that the fast forward
3248
+ * timestamp, then record it, else stop.
3249
+ */
3250
+ if (tsc < d->timestamp)
3251
+ d->buf_timestamp = buffer->ref_timestamp;
3252
+ else
3253
+ return 1;
3254
+
3255
+ return 0;
3256
+}
3257
+
3258
+/**
3259
+ * intel_pt_fast_forward - reposition decoder forwards.
3260
+ * @decoder: Intel PT decoder
3261
+ * @timestamp: timestamp to fast forward towards
3262
+ *
3263
+ * Reposition decoder at the last PSB with a timestamp earlier than @timestamp.
3264
+ *
3265
+ * Return: 0 on success or negative error code on failure.
3266
+ */
3267
+int intel_pt_fast_forward(struct intel_pt_decoder *decoder, uint64_t timestamp)
3268
+{
3269
+ struct fast_forward_data d = { .timestamp = timestamp };
3270
+ unsigned char *buf;
3271
+ size_t len;
3272
+ int err;
3273
+
3274
+ intel_pt_log("Fast forward towards timestamp " x64_fmt "\n", timestamp);
3275
+
3276
+ /* Find buffer timestamp of buffer to fast forward to */
3277
+ err = decoder->lookahead(decoder->data, intel_pt_ff_cb, &d);
3278
+ if (err < 0)
3279
+ return err;
3280
+
3281
+ /* Walk to buffer with same buffer timestamp */
3282
+ if (d.buf_timestamp) {
3283
+ do {
3284
+ decoder->pos += decoder->len;
3285
+ decoder->len = 0;
3286
+ err = intel_pt_get_next_data(decoder, true);
3287
+ /* -ENOLINK means non-consecutive trace */
3288
+ if (err && err != -ENOLINK)
3289
+ return err;
3290
+ } while (decoder->buf_timestamp != d.buf_timestamp);
3291
+ }
3292
+
3293
+ if (!decoder->buf)
3294
+ return 0;
3295
+
3296
+ buf = (unsigned char *)decoder->buf;
3297
+ len = decoder->len;
3298
+
3299
+ if (!intel_pt_next_psb(&buf, &len))
3300
+ return 0;
3301
+
3302
+ /*
3303
+ * Walk PSBs while the PSB timestamp is less than the fast forward
3304
+ * timestamp.
3305
+ */
3306
+ do {
3307
+ uint64_t tsc;
3308
+ size_t rem;
3309
+
3310
+ if (!intel_pt_next_tsc(buf, len, &tsc, &rem))
3311
+ break;
3312
+ tsc = intel_pt_8b_tsc(tsc, decoder->buf_timestamp);
3313
+ /*
3314
+ * A TSC packet can slip past MTC packets but, after fast
3315
+ * forward, decoding starts at the TSC timestamp. That means
3316
+ * the timestamps may not be exactly the same as the timestamps
3317
+ * that would have been decoded without fast forward.
3318
+ */
3319
+ if (tsc < timestamp) {
3320
+ intel_pt_log("Fast forward to next PSB timestamp " x64_fmt "\n", tsc);
3321
+ decoder->pos += decoder->len - len;
3322
+ decoder->buf = buf;
3323
+ decoder->len = len;
3324
+ intel_pt_reposition(decoder);
3325
+ } else {
3326
+ break;
3327
+ }
3328
+ } while (intel_pt_step_psb(&buf, &len));
3329
+
3330
+ return 0;
3331
+}