1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
|
/*
* Copyright (c) 2016-2019, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "pt_block_decoder.h"
#include "pt_block_cache.h"
#include "pt_section.h"
#include "pt_image.h"
#include "pt_insn.h"
#include "pt_config.h"
#include "pt_asid.h"
#include "pt_compiler.h"
#include "intel-pt.h"
#include <string.h>
#include <stdlib.h>
static int pt_blk_proceed_trailing_event(struct pt_block_decoder *,
struct pt_block *);
static int pt_blk_status(const struct pt_block_decoder *decoder, int flags)
{
int status;
if (!decoder)
return -pte_internal;
status = decoder->status;
/* Indicate whether tracing is disabled or enabled.
*
* This duplicates the indication in struct pt_insn and covers the case
* where we indicate the status after synchronizing.
*/
if (!decoder->enabled)
flags |= pts_ip_suppressed;
/* Forward end-of-trace indications.
*
* Postpone it as long as we're still processing events, though.
*/
if ((status & pts_eos) && !decoder->process_event)
flags |= pts_eos;
return flags;
}
static void pt_blk_reset(struct pt_block_decoder *decoder)
{
if (!decoder)
return;
decoder->mode = ptem_unknown;
decoder->ip = 0ull;
decoder->status = 0;
decoder->enabled = 0;
decoder->process_event = 0;
decoder->speculative = 0;
decoder->process_insn = 0;
decoder->bound_paging = 0;
decoder->bound_vmcs = 0;
decoder->bound_ptwrite = 0;
memset(&decoder->event, 0, sizeof(decoder->event));
pt_retstack_init(&decoder->retstack);
pt_asid_init(&decoder->asid);
}
/* Initialize the query decoder flags based on our flags. */
static int pt_blk_init_qry_flags(struct pt_conf_flags *qflags,
const struct pt_conf_flags *flags)
{
if (!qflags || !flags)
return -pte_internal;
memset(qflags, 0, sizeof(*qflags));
qflags->variant.query.keep_tcal_on_ovf =
flags->variant.block.keep_tcal_on_ovf;
return 0;
}
int pt_blk_decoder_init(struct pt_block_decoder *decoder,
const struct pt_config *uconfig)
{
struct pt_config config;
int errcode;
if (!decoder)
return -pte_internal;
errcode = pt_config_from_user(&config, uconfig);
if (errcode < 0)
return errcode;
/* The user supplied decoder flags. */
decoder->flags = config.flags;
/* Set the flags we need for the query decoder we use. */
errcode = pt_blk_init_qry_flags(&config.flags, &decoder->flags);
if (errcode < 0)
return errcode;
errcode = pt_qry_decoder_init(&decoder->query, &config);
if (errcode < 0)
return errcode;
pt_image_init(&decoder->default_image, NULL);
decoder->image = &decoder->default_image;
errcode = pt_msec_cache_init(&decoder->scache);
if (errcode < 0)
return errcode;
pt_blk_reset(decoder);
return 0;
}
void pt_blk_decoder_fini(struct pt_block_decoder *decoder)
{
if (!decoder)
return;
pt_msec_cache_fini(&decoder->scache);
pt_image_fini(&decoder->default_image);
pt_qry_decoder_fini(&decoder->query);
}
struct pt_block_decoder *
pt_blk_alloc_decoder(const struct pt_config *config)
{
struct pt_block_decoder *decoder;
int errcode;
decoder = malloc(sizeof(*decoder));
if (!decoder)
return NULL;
errcode = pt_blk_decoder_init(decoder, config);
if (errcode < 0) {
free(decoder);
return NULL;
}
return decoder;
}
void pt_blk_free_decoder(struct pt_block_decoder *decoder)
{
if (!decoder)
return;
pt_blk_decoder_fini(decoder);
free(decoder);
}
/* Maybe synthesize a tick event.
*
* If we're not already processing events, check the current time against the
* last event's time. If it changed, synthesize a tick event with the new time.
*
* Returns zero if no tick event has been created.
* Returns a positive integer if a tick event has been created.
* Returns a negative error code otherwise.
*/
static int pt_blk_tick(struct pt_block_decoder *decoder, uint64_t ip)
{
struct pt_event *ev;
uint64_t tsc;
uint32_t lost_mtc, lost_cyc;
int errcode;
if (!decoder)
return -pte_internal;
/* We're not generating tick events if tracing is disabled. */
if (!decoder->enabled)
return -pte_internal;
/* Events already provide a timestamp so there is no need to synthesize
* an artificial tick event. There's no room, either, since this would
* overwrite the in-progress event.
*
* In rare cases where we need to proceed to an event location using
* trace this may cause us to miss a timing update if the event is not
* forwarded to the user.
*
* The only case I can come up with at the moment is a MODE.EXEC binding
* to the TIP IP of a far branch.
*/
if (decoder->process_event)
return 0;
errcode = pt_qry_time(&decoder->query, &tsc, &lost_mtc, &lost_cyc);
if (errcode < 0) {
/* If we don't have wall-clock time, we use relative time. */
if (errcode != -pte_no_time)
return errcode;
}
ev = &decoder->event;
/* We're done if time has not changed since the last event. */
if (tsc == ev->tsc)
return 0;
/* Time has changed so we create a new tick event. */
memset(ev, 0, sizeof(*ev));
ev->type = ptev_tick;
ev->variant.tick.ip = ip;
/* Indicate if we have wall-clock time or only relative time. */
if (errcode != -pte_no_time)
ev->has_tsc = 1;
ev->tsc = tsc;
ev->lost_mtc = lost_mtc;
ev->lost_cyc = lost_cyc;
/* We now have an event to process. */
decoder->process_event = 1;
return 1;
}
/* Query an indirect branch.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_indirect_branch(struct pt_block_decoder *decoder,
uint64_t *ip)
{
uint64_t evip;
int status, errcode;
if (!decoder)
return -pte_internal;
evip = decoder->ip;
status = pt_qry_indirect_branch(&decoder->query, ip);
if (status < 0)
return status;
if (decoder->flags.variant.block.enable_tick_events) {
errcode = pt_blk_tick(decoder, evip);
if (errcode < 0)
return errcode;
}
return status;
}
/* Query a conditional branch.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_cond_branch(struct pt_block_decoder *decoder, int *taken)
{
int status, errcode;
if (!decoder)
return -pte_internal;
status = pt_qry_cond_branch(&decoder->query, taken);
if (status < 0)
return status;
if (decoder->flags.variant.block.enable_tick_events) {
errcode = pt_blk_tick(decoder, decoder->ip);
if (errcode < 0)
return errcode;
}
return status;
}
static int pt_blk_start(struct pt_block_decoder *decoder, int status)
{
if (!decoder)
return -pte_internal;
if (status < 0)
return status;
decoder->status = status;
if (!(status & pts_ip_suppressed))
decoder->enabled = 1;
/* We will always have an event.
*
* If we synchronized onto an empty PSB+, tracing is disabled and we'll
* process events until the enabled event.
*
* If tracing is enabled, PSB+ must at least provide the execution mode,
* which we're going to forward to the user.
*/
return pt_blk_proceed_trailing_event(decoder, NULL);
}
static int pt_blk_sync_reset(struct pt_block_decoder *decoder)
{
if (!decoder)
return -pte_internal;
pt_blk_reset(decoder);
return 0;
}
int pt_blk_sync_forward(struct pt_block_decoder *decoder)
{
int errcode, status;
if (!decoder)
return -pte_invalid;
errcode = pt_blk_sync_reset(decoder);
if (errcode < 0)
return errcode;
status = pt_qry_sync_forward(&decoder->query, &decoder->ip);
return pt_blk_start(decoder, status);
}
int pt_blk_sync_backward(struct pt_block_decoder *decoder)
{
int errcode, status;
if (!decoder)
return -pte_invalid;
errcode = pt_blk_sync_reset(decoder);
if (errcode < 0)
return errcode;
status = pt_qry_sync_backward(&decoder->query, &decoder->ip);
return pt_blk_start(decoder, status);
}
int pt_blk_sync_set(struct pt_block_decoder *decoder, uint64_t offset)
{
int errcode, status;
if (!decoder)
return -pte_invalid;
errcode = pt_blk_sync_reset(decoder);
if (errcode < 0)
return errcode;
status = pt_qry_sync_set(&decoder->query, &decoder->ip, offset);
return pt_blk_start(decoder, status);
}
int pt_blk_get_offset(const struct pt_block_decoder *decoder, uint64_t *offset)
{
if (!decoder)
return -pte_invalid;
return pt_qry_get_offset(&decoder->query, offset);
}
int pt_blk_get_sync_offset(const struct pt_block_decoder *decoder,
uint64_t *offset)
{
if (!decoder)
return -pte_invalid;
return pt_qry_get_sync_offset(&decoder->query, offset);
}
struct pt_image *pt_blk_get_image(struct pt_block_decoder *decoder)
{
if (!decoder)
return NULL;
return decoder->image;
}
int pt_blk_set_image(struct pt_block_decoder *decoder, struct pt_image *image)
{
if (!decoder)
return -pte_invalid;
if (!image)
image = &decoder->default_image;
decoder->image = image;
return 0;
}
const struct pt_config *
pt_blk_get_config(const struct pt_block_decoder *decoder)
{
if (!decoder)
return NULL;
return pt_qry_get_config(&decoder->query);
}
int pt_blk_time(struct pt_block_decoder *decoder, uint64_t *time,
uint32_t *lost_mtc, uint32_t *lost_cyc)
{
if (!decoder || !time)
return -pte_invalid;
return pt_qry_time(&decoder->query, time, lost_mtc, lost_cyc);
}
int pt_blk_core_bus_ratio(struct pt_block_decoder *decoder, uint32_t *cbr)
{
if (!decoder || !cbr)
return -pte_invalid;
return pt_qry_core_bus_ratio(&decoder->query, cbr);
}
int pt_blk_asid(const struct pt_block_decoder *decoder, struct pt_asid *asid,
size_t size)
{
if (!decoder || !asid)
return -pte_invalid;
return pt_asid_to_user(asid, &decoder->asid, size);
}
/* Fetch the next pending event.
*
* Checks for pending events. If an event is pending, fetches it (if not
* already in process).
*
* Returns zero if no event is pending.
* Returns a positive integer if an event is pending or in process.
* Returns a negative error code otherwise.
*/
static inline int pt_blk_fetch_event(struct pt_block_decoder *decoder)
{
int status;
if (!decoder)
return -pte_internal;
if (decoder->process_event)
return 1;
if (!(decoder->status & pts_event_pending))
return 0;
status = pt_qry_event(&decoder->query, &decoder->event,
sizeof(decoder->event));
if (status < 0)
return status;
decoder->process_event = 1;
decoder->status = status;
return 1;
}
static inline int pt_blk_block_is_empty(const struct pt_block *block)
{
if (!block)
return 1;
return !block->ninsn;
}
static inline int block_to_user(struct pt_block *ublock, size_t size,
const struct pt_block *block)
{
if (!ublock || !block)
return -pte_internal;
if (ublock == block)
return 0;
/* Zero out any unknown bytes. */
if (sizeof(*block) < size) {
memset(ublock + sizeof(*block), 0, size - sizeof(*block));
size = sizeof(*block);
}
memcpy(ublock, block, size);
return 0;
}
static int pt_insn_false(const struct pt_insn *insn,
const struct pt_insn_ext *iext)
{
(void) insn;
(void) iext;
return 0;
}
/* Determine the next IP using trace.
*
* Tries to determine the IP of the next instruction using trace and provides it
* in @pip.
*
* Not requiring trace to determine the IP is treated as an internal error.
*
* Does not update the return compression stack for indirect calls. This is
* expected to have been done, already, when trying to determine the next IP
* without using trace.
*
* Does not update @decoder->status. The caller is expected to do that.
*
* Returns a non-negative pt_status_flag bit-vector on success, a negative error
* code otherwise.
* Returns -pte_internal if @pip, @decoder, @insn, or @iext are NULL.
* Returns -pte_internal if no trace is required.
*/
static int pt_blk_next_ip(uint64_t *pip, struct pt_block_decoder *decoder,
const struct pt_insn *insn,
const struct pt_insn_ext *iext)
{
int status, errcode;
if (!pip || !decoder || !insn || !iext)
return -pte_internal;
/* We handle non-taken conditional branches, and compressed returns
* directly in the switch.
*
* All kinds of branches are handled below the switch.
*/
switch (insn->iclass) {
case ptic_cond_jump: {
uint64_t ip;
int taken;
status = pt_blk_cond_branch(decoder, &taken);
if (status < 0)
return status;
ip = insn->ip + insn->size;
if (taken)
ip += (uint64_t) (int64_t)
iext->variant.branch.displacement;
*pip = ip;
return status;
}
case ptic_return: {
int taken;
/* Check for a compressed return. */
status = pt_blk_cond_branch(decoder, &taken);
if (status < 0) {
if (status != -pte_bad_query)
return status;
break;
}
/* A compressed return is indicated by a taken conditional
* branch.
*/
if (!taken)
return -pte_bad_retcomp;
errcode = pt_retstack_pop(&decoder->retstack, pip);
if (errcode < 0)
return errcode;
return status;
}
case ptic_jump:
case ptic_call:
/* A direct jump or call wouldn't require trace. */
if (iext->variant.branch.is_direct)
return -pte_internal;
break;
case ptic_far_call:
case ptic_far_return:
case ptic_far_jump:
break;
case ptic_ptwrite:
case ptic_other:
return -pte_internal;
case ptic_error:
return -pte_bad_insn;
}
/* Process an indirect branch.
*
* This covers indirect jumps and calls, non-compressed returns, and all
* flavors of far transfers.
*/
return pt_blk_indirect_branch(decoder, pip);
}
/* Proceed to the next IP using trace.
*
* We failed to proceed without trace. This ends the current block. Now use
* trace to do one final step to determine the start IP of the next block.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_proceed_with_trace(struct pt_block_decoder *decoder,
const struct pt_insn *insn,
const struct pt_insn_ext *iext)
{
int status;
if (!decoder)
return -pte_internal;
status = pt_blk_next_ip(&decoder->ip, decoder, insn, iext);
if (status < 0)
return status;
/* Preserve the query decoder's response which indicates upcoming
* events.
*/
decoder->status = status;
/* We do need an IP in order to proceed. */
if (status & pts_ip_suppressed)
return -pte_noip;
return 0;
}
/* Decode one instruction in a known section.
*
* Decode the instruction at @insn->ip in @msec assuming execution mode
* @insn->mode.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_decode_in_section(struct pt_insn *insn,
struct pt_insn_ext *iext,
const struct pt_mapped_section *msec)
{
int status;
if (!insn || !iext)
return -pte_internal;
/* We know that @ip is contained in @section.
*
* Note that we need to translate @ip into a section offset.
*/
status = pt_msec_read(msec, insn->raw, sizeof(insn->raw), insn->ip);
if (status < 0)
return status;
/* We initialize @insn->size to the maximal possible size. It will be
* set to the actual size during instruction decode.
*/
insn->size = (uint8_t) status;
return pt_ild_decode(insn, iext);
}
/* Update the return-address stack if @insn is a near call.
*
* Returns zero on success, a negative error code otherwise.
*/
static inline int pt_blk_log_call(struct pt_block_decoder *decoder,
const struct pt_insn *insn,
const struct pt_insn_ext *iext)
{
if (!decoder || !insn || !iext)
return -pte_internal;
if (insn->iclass != ptic_call)
return 0;
/* Ignore direct calls to the next instruction that are used for
* position independent code.
*/
if (iext->variant.branch.is_direct &&
!iext->variant.branch.displacement)
return 0;
return pt_retstack_push(&decoder->retstack, insn->ip + insn->size);
}
/* Proceed by one instruction.
*
* Tries to decode the instruction at @decoder->ip and, on success, adds it to
* @block and provides it in @pinsn and @piext.
*
* The instruction will not be added if:
*
* - the memory could not be read: return error
* - it could not be decoded: return error
* - @block is already full: return zero
* - @block would switch sections: return zero
*
* Returns a positive integer if the instruction was added.
* Returns zero if the instruction didn't fit into @block.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_one_insn(struct pt_block_decoder *decoder,
struct pt_block *block,
struct pt_insn *pinsn,
struct pt_insn_ext *piext)
{
struct pt_insn_ext iext;
struct pt_insn insn;
uint16_t ninsn;
int status;
if (!decoder || !block || !pinsn || !piext)
return -pte_internal;
/* There's nothing to do if there is no room in @block. */
ninsn = block->ninsn + 1;
if (!ninsn)
return 0;
/* The truncated instruction must be last. */
if (block->truncated)
return 0;
memset(&insn, 0, sizeof(insn));
memset(&iext, 0, sizeof(iext));
insn.mode = decoder->mode;
insn.ip = decoder->ip;
status = pt_insn_decode(&insn, &iext, decoder->image, &decoder->asid);
if (status < 0)
return status;
/* We do not switch sections inside a block. */
if (insn.isid != block->isid) {
if (!pt_blk_block_is_empty(block))
return 0;
block->isid = insn.isid;
}
/* If we couldn't read @insn's memory in one chunk from @insn.isid, we
* provide the memory in @block.
*/
if (insn.truncated) {
memcpy(block->raw, insn.raw, insn.size);
block->size = insn.size;
block->truncated = 1;
}
/* Log calls' return addresses for return compression. */
status = pt_blk_log_call(decoder, &insn, &iext);
if (status < 0)
return status;
/* We have a new instruction. */
block->iclass = insn.iclass;
block->end_ip = insn.ip;
block->ninsn = ninsn;
*pinsn = insn;
*piext = iext;
return 1;
}
/* Proceed to a particular type of instruction without using trace.
*
* Proceed until we reach an instruction for which @predicate returns a positive
* integer or until:
*
* - @predicate returns an error: return error
* - @block is full: return zero
* - @block would switch sections: return zero
* - we would need trace: return -pte_bad_query
*
* Provide the last instruction that was reached in @insn and @iext.
*
* Update @decoder->ip to point to the last IP that was reached. If we fail due
* to lack of trace or if we reach a desired instruction, this is @insn->ip;
* otherwise this is the next instruction's IP.
*
* Returns a positive integer if a suitable instruction was reached.
* Returns zero if no such instruction was reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_to_insn(struct pt_block_decoder *decoder,
struct pt_block *block,
struct pt_insn *insn,
struct pt_insn_ext *iext,
int (*predicate)(const struct pt_insn *,
const struct pt_insn_ext *))
{
int status;
if (!decoder || !insn || !predicate)
return -pte_internal;
for (;;) {
status = pt_blk_proceed_one_insn(decoder, block, insn, iext);
if (status <= 0)
return status;
/* We're done if this instruction matches the spec (positive
* status) or we run into an error (negative status).
*/
status = predicate(insn, iext);
if (status != 0)
return status;
/* Let's see if we can proceed to the next IP without trace. */
status = pt_insn_next_ip(&decoder->ip, insn, iext);
if (status < 0)
return status;
/* End the block if the user asked us to.
*
* We only need to take care about direct near branches.
* Indirect and far branches require trace and will naturally
* end a block.
*/
if ((decoder->flags.variant.block.end_on_call &&
(insn->iclass == ptic_call)) ||
(decoder->flags.variant.block.end_on_jump &&
(insn->iclass == ptic_jump)))
return 0;
}
}
/* Proceed to a particular IP without using trace.
*
* Proceed until we reach @ip or until:
*
* - @block is full: return zero
* - @block would switch sections: return zero
* - we would need trace: return -pte_bad_query
*
* Provide the last instruction that was reached in @insn and @iext. If we
* reached @ip, this is the instruction preceding it.
*
* Update @decoder->ip to point to the last IP that was reached. If we fail due
* to lack of trace, this is @insn->ip; otherwise this is the next instruction's
* IP.
*
* Returns a positive integer if @ip was reached.
* Returns zero if no such instruction was reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_to_ip(struct pt_block_decoder *decoder,
struct pt_block *block, struct pt_insn *insn,
struct pt_insn_ext *iext, uint64_t ip)
{
int status;
if (!decoder || !insn)
return -pte_internal;
for (;;) {
/* We're done when we reach @ip. We may not even have to decode
* a single instruction in some cases.
*/
if (decoder->ip == ip)
return 1;
status = pt_blk_proceed_one_insn(decoder, block, insn, iext);
if (status <= 0)
return status;
/* Let's see if we can proceed to the next IP without trace. */
status = pt_insn_next_ip(&decoder->ip, insn, iext);
if (status < 0)
return status;
/* End the block if the user asked us to.
*
* We only need to take care about direct near branches.
* Indirect and far branches require trace and will naturally
* end a block.
*
* The call at the end of the block may have reached @ip; make
* sure to indicate that.
*/
if ((decoder->flags.variant.block.end_on_call &&
(insn->iclass == ptic_call)) ||
(decoder->flags.variant.block.end_on_jump &&
(insn->iclass == ptic_jump))) {
return (decoder->ip == ip ? 1 : 0);
}
}
}
/* Proceed to a particular IP with trace, if necessary.
*
* Proceed until we reach @ip or until:
*
* - @block is full: return zero
* - @block would switch sections: return zero
* - we need trace: return zero
*
* Update @decoder->ip to point to the last IP that was reached.
*
* A return of zero ends @block.
*
* Returns a positive integer if @ip was reached.
* Returns zero if no such instruction was reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_to_ip_with_trace(struct pt_block_decoder *decoder,
struct pt_block *block,
uint64_t ip)
{
struct pt_insn_ext iext;
struct pt_insn insn;
int status;
/* Try to reach @ip without trace.
*
* We're also OK if @block overflowed or we switched sections and we
* have to try again in the next iteration.
*/
status = pt_blk_proceed_to_ip(decoder, block, &insn, &iext, ip);
if (status != -pte_bad_query)
return status;
/* Needing trace is not an error. We use trace to determine the next
* start IP and end the block.
*/
return pt_blk_proceed_with_trace(decoder, &insn, &iext);
}
static int pt_insn_skl014(const struct pt_insn *insn,
const struct pt_insn_ext *iext)
{
if (!insn || !iext)
return 0;
switch (insn->iclass) {
default:
return 0;
case ptic_call:
case ptic_jump:
return iext->variant.branch.is_direct;
case ptic_other:
return pt_insn_changes_cr3(insn, iext);
}
}
/* Proceed to the location of a synchronous disabled event with suppressed IP
* considering SKL014.
*
* We have a (synchronous) disabled event pending. Proceed to the event
* location and indicate whether we were able to reach it.
*
* With SKL014 a TIP.PGD with suppressed IP may also be generated by a direct
* unconditional branch that clears FilterEn by jumping out of a filter region
* or into a TraceStop region. Use the filter configuration to determine the
* exact branch the event binds to.
*
* The last instruction that was reached is stored in @insn/@iext.
*
* Returns a positive integer if the event location was reached.
* Returns zero if the event location was not reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_skl014(struct pt_block_decoder *decoder,
struct pt_block *block, struct pt_insn *insn,
struct pt_insn_ext *iext)
{
const struct pt_conf_addr_filter *addr_filter;
int status;
if (!decoder || !block || !insn || !iext)
return -pte_internal;
addr_filter = &decoder->query.config.addr_filter;
for (;;) {
uint64_t ip;
status = pt_blk_proceed_to_insn(decoder, block, insn, iext,
pt_insn_skl014);
if (status <= 0)
break;
/* The erratum doesn't apply if we can bind the event to a
* CR3-changing instruction.
*/
if (pt_insn_changes_cr3(insn, iext))
break;
/* Check the filter against the branch target. */
status = pt_insn_next_ip(&ip, insn, iext);
if (status < 0)
break;
status = pt_filter_addr_check(addr_filter, ip);
if (status <= 0) {
/* We need to flip the indication.
*
* We reached the event location when @ip lies inside a
* tracing-disabled region.
*/
if (!status)
status = 1;
break;
}
/* This is not the correct instruction. Proceed past it and try
* again.
*/
decoder->ip = ip;
/* End the block if the user asked us to.
*
* We only need to take care about direct near branches.
* Indirect and far branches require trace and will naturally
* end a block.
*/
if ((decoder->flags.variant.block.end_on_call &&
(insn->iclass == ptic_call)) ||
(decoder->flags.variant.block.end_on_jump &&
(insn->iclass == ptic_jump)))
break;
}
return status;
}
/* Proceed to the event location for a disabled event.
*
* We have a (synchronous) disabled event pending. Proceed to the event
* location and indicate whether we were able to reach it.
*
* The last instruction that was reached is stored in @insn/@iext.
*
* Returns a positive integer if the event location was reached.
* Returns zero if the event location was not reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_to_disabled(struct pt_block_decoder *decoder,
struct pt_block *block,
struct pt_insn *insn,
struct pt_insn_ext *iext,
const struct pt_event *ev)
{
if (!decoder || !block || !ev)
return -pte_internal;
if (ev->ip_suppressed) {
/* Due to SKL014 the TIP.PGD payload may be suppressed also for
* direct branches.
*
* If we don't have a filter configuration we assume that no
* address filters were used and the erratum does not apply.
*
* We might otherwise disable tracing too early.
*/
if (decoder->query.config.addr_filter.config.addr_cfg &&
decoder->query.config.errata.skl014)
return pt_blk_proceed_skl014(decoder, block, insn,
iext);
/* A synchronous disabled event also binds to far branches and
* CPL-changing instructions. Both would require trace,
* however, and are thus implicitly handled by erroring out.
*
* The would-require-trace error is handled by our caller.
*/
return pt_blk_proceed_to_insn(decoder, block, insn, iext,
pt_insn_changes_cr3);
} else
return pt_blk_proceed_to_ip(decoder, block, insn, iext,
ev->variant.disabled.ip);
}
/* Set the expected resume address for a synchronous disable.
*
* On a synchronous disable, @decoder->ip still points to the instruction to
* which the event bound. That's not where we expect tracing to resume.
*
* For calls, a fair assumption is that tracing resumes after returning from the
* called function. For other types of instructions, we simply don't know.
*
* Returns zero on success, a negative pt_error_code otherwise.
*/
static int pt_blk_set_disable_resume_ip(struct pt_block_decoder *decoder,
const struct pt_insn *insn)
{
if (!decoder || !insn)
return -pte_internal;
switch (insn->iclass) {
case ptic_call:
case ptic_far_call:
decoder->ip = insn->ip + insn->size;
break;
default:
decoder->ip = 0ull;
break;
}
return 0;
}
/* Proceed to the event location for an async paging event.
*
* We have an async paging event pending. Proceed to the event location and
* indicate whether we were able to reach it. Needing trace in order to proceed
* is not an error in this case but ends the block.
*
* Returns a positive integer if the event location was reached.
* Returns zero if the event location was not reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_to_async_paging(struct pt_block_decoder *decoder,
struct pt_block *block,
const struct pt_event *ev)
{
int status;
if (!decoder || !ev)
return -pte_internal;
/* Apply the event immediately if we don't have an IP. */
if (ev->ip_suppressed)
return 1;
status = pt_blk_proceed_to_ip_with_trace(decoder, block,
ev->variant.async_paging.ip);
if (status < 0)
return status;
/* We may have reached the IP. */
return (decoder->ip == ev->variant.async_paging.ip ? 1 : 0);
}
/* Proceed to the event location for an async vmcs event.
*
* We have an async vmcs event pending. Proceed to the event location and
* indicate whether we were able to reach it. Needing trace in order to proceed
* is not an error in this case but ends the block.
*
* Returns a positive integer if the event location was reached.
* Returns zero if the event location was not reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_to_async_vmcs(struct pt_block_decoder *decoder,
struct pt_block *block,
const struct pt_event *ev)
{
int status;
if (!decoder || !ev)
return -pte_internal;
/* Apply the event immediately if we don't have an IP. */
if (ev->ip_suppressed)
return 1;
status = pt_blk_proceed_to_ip_with_trace(decoder, block,
ev->variant.async_vmcs.ip);
if (status < 0)
return status;
/* We may have reached the IP. */
return (decoder->ip == ev->variant.async_vmcs.ip ? 1 : 0);
}
/* Proceed to the event location for an exec mode event.
*
* We have an exec mode event pending. Proceed to the event location and
* indicate whether we were able to reach it. Needing trace in order to proceed
* is not an error in this case but ends the block.
*
* Returns a positive integer if the event location was reached.
* Returns zero if the event location was not reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_to_exec_mode(struct pt_block_decoder *decoder,
struct pt_block *block,
const struct pt_event *ev)
{
int status;
if (!decoder || !ev)
return -pte_internal;
/* Apply the event immediately if we don't have an IP. */
if (ev->ip_suppressed)
return 1;
status = pt_blk_proceed_to_ip_with_trace(decoder, block,
ev->variant.exec_mode.ip);
if (status < 0)
return status;
/* We may have reached the IP. */
return (decoder->ip == ev->variant.exec_mode.ip ? 1 : 0);
}
/* Proceed to the event location for a ptwrite event.
*
* We have a ptwrite event pending. Proceed to the event location and indicate
* whether we were able to reach it.
*
* In case of the event binding to a ptwrite instruction, we pass beyond that
* instruction and update the event to provide the instruction's IP.
*
* In the case of the event binding to an IP provided in the event, we move
* beyond the instruction at that IP.
*
* Returns a positive integer if the event location was reached.
* Returns zero if the event location was not reached.
* Returns a negative error code otherwise.
*/
static int pt_blk_proceed_to_ptwrite(struct pt_block_decoder *decoder,
struct pt_block *block,
struct pt_insn *insn,
struct pt_insn_ext *iext,
struct pt_event *ev)
{
int status;
if (!insn || !ev)
return -pte_internal;
/* If we don't have an IP, the event binds to the next PTWRITE
* instruction.
*
* If we have an IP it still binds to the next PTWRITE instruction but
* now the IP tells us where that instruction is. This makes most sense
* when tracing is disabled and we don't have any other means of finding
* the PTWRITE instruction. We nevertheless distinguish the two cases,
* here.
*
* In both cases, we move beyond the PTWRITE instruction, so it will be
* the last instruction in the current block and @decoder->ip will point
* to the instruction following it.
*/
if (ev->ip_suppressed) {
status = pt_blk_proceed_to_insn(decoder, block, insn, iext,
pt_insn_is_ptwrite);
if (status <= 0)
return status;
/* We now know the IP of the PTWRITE instruction corresponding
* to this event. Fill it in to make it more convenient for the
* user to process the event.
*/
ev->variant.ptwrite.ip = insn->ip;
ev->ip_suppressed = 0;
} else {
status = pt_blk_proceed_to_ip(decoder, block, insn, iext,
ev->variant.ptwrite.ip);
if (status <= 0)
return status;
/* We reached the PTWRITE instruction and @decoder->ip points to
* it; @insn/@iext still contain the preceding instruction.
*
* Proceed beyond the PTWRITE to account for it. Note that we
* may still overflow the block, which would cause us to
* postpone both instruction and event to the next block.
*/
status = pt_blk_proceed_one_insn(decoder, block, insn, iext);
if (status <= 0)
return status;
}
return 1;
}
/* Try to work around erratum SKD022.
*
* If we get an asynchronous disable on VMLAUNCH or VMRESUME, the FUP that
* caused the disable to be asynchronous might have been bogous.
*
* Returns a positive integer if the erratum has been handled.
* Returns zero if the erratum does not apply.
* Returns a negative error code otherwise.
*/
static int pt_blk_handle_erratum_skd022(struct pt_block_decoder *decoder,
struct pt_event *ev)
{
struct pt_insn_ext iext;
struct pt_insn insn;
int errcode;
if (!decoder || !ev)
return -pte_internal;
insn.mode = decoder->mode;
insn.ip = ev->variant.async_disabled.at;
errcode = pt_insn_decode(&insn, &iext, decoder->image, &decoder->asid);
if (errcode < 0)
return 0;
switch (iext.iclass) {
default:
/* The erratum does not apply. */
return 0;
case PTI_INST_VMLAUNCH:
case PTI_INST_VMRESUME:
/* The erratum may apply. We can't be sure without a lot more
* analysis. Let's assume it does.
*
* We turn the async disable into a sync disable. Our caller
* will restart event processing.
*/
ev->type = ptev_disabled;
ev->variant.disabled.ip = ev->variant.async_disabled.ip;
return 1;
}
}
/* Postpone proceeding past @insn/@iext and indicate a pending event.
*
* There may be further events pending on @insn/@iext. Postpone proceeding past
* @insn/@iext until we processed all events that bind to it.
*
* Returns a non-negative pt_status_flag bit-vector indicating a pending event
* on success, a negative pt_error_code otherwise.
*/
static int pt_blk_postpone_insn(struct pt_block_decoder *decoder,
const struct pt_insn *insn,
const struct pt_insn_ext *iext)
{
if (!decoder || !insn || !iext)
return -pte_internal;
/* Only one can be active. */
if (decoder->process_insn)
return -pte_internal;
decoder->process_insn = 1;
decoder->insn = *insn;
decoder->iext = *iext;
return pt_blk_status(decoder, pts_event_pending);
}
/* Remove any postponed instruction from @decoder.
*
* Returns zero on success, a negative pt_error_code otherwise.
*/
static int pt_blk_clear_postponed_insn(struct pt_block_decoder *decoder)
{
if (!decoder)
return -pte_internal;
decoder->process_insn = 0;
decoder->bound_paging = 0;
decoder->bound_vmcs = 0;
decoder->bound_ptwrite = 0;
return 0;
}
/* Proceed past a postponed instruction.
*
* If an instruction has been postponed in @decoder, proceed past it.
*
* Returns zero on success, a negative pt_error_code otherwise.
*/
static int pt_blk_proceed_postponed_insn(struct pt_block_decoder *decoder)
{
int status;
if (!decoder)
return -pte_internal;
/* There's nothing to do if we have no postponed instruction. */
if (!decoder->process_insn)
return 0;
/* There's nothing to do if tracing got disabled. */
if (!decoder->enabled)
return pt_blk_clear_postponed_insn(decoder);
status = pt_insn_next_ip(&decoder->ip, &decoder->insn, &decoder->iext);
if (status < 0) {
if (status != -pte_bad_query)
return status;
status = pt_blk_proceed_with_trace(decoder, &decoder->insn,
&decoder->iext);
if (status < 0)
return status;
}
return pt_blk_clear_postponed_insn(decoder);
}
/* Proceed to the next event.
*
* We have an event pending. Proceed to the event location and indicate the
* event to the user.
*
* On our way to the event location we may also be forced to postpone the event
* to the next block, e.g. if we overflow the number of instructions in the
* block or if we need trace in order to reach the event location.
*
* If we're not able to reach the event location, we return zero. This is what
* pt_blk_status() would return since:
*
* - we suppress pts_eos as long as we're processing events
* - we do not set pts_ip_suppressed since tracing must be enabled
*
* Returns a non-negative pt_status_flag bit-vector on success, a negative error
* code otherwise.
*/
static int pt_blk_proceed_event(struct pt_block_decoder *decoder,
struct pt_block *block)
{
struct pt_insn_ext iext;
struct pt_insn insn;
struct pt_event *ev;
int status;
if (!decoder || !decoder->process_event || !block)
return -pte_internal;
ev = &decoder->event;
switch (ev->type) {
case ptev_enabled:
break;
case ptev_disabled:
status = pt_blk_proceed_to_disabled(decoder, block, &insn,
&iext, ev);
if (status <= 0) {
/* A synchronous disable event also binds to the next
* indirect or conditional branch, i.e. to any branch
* that would have required trace.
*/
if (status != -pte_bad_query)
return status;
status = pt_blk_set_disable_resume_ip(decoder, &insn);
if (status < 0)
return status;
}
break;
case ptev_async_disabled:
status = pt_blk_proceed_to_ip(decoder, block, &insn, &iext,
ev->variant.async_disabled.at);
if (status <= 0)
return status;
if (decoder->query.config.errata.skd022) {
status = pt_blk_handle_erratum_skd022(decoder, ev);
if (status != 0) {
if (status < 0)
return status;
/* If the erratum hits, we modify the event.
* Try again.
*/
return pt_blk_proceed_event(decoder, block);
}
}
break;
case ptev_async_branch:
status = pt_blk_proceed_to_ip(decoder, block, &insn, &iext,
ev->variant.async_branch.from);
if (status <= 0)
return status;
break;
case ptev_paging:
if (!decoder->enabled)
break;
status = pt_blk_proceed_to_insn(decoder, block, &insn, &iext,
pt_insn_binds_to_pip);
if (status <= 0)
return status;
/* We bound a paging event. Make sure we do not bind further
* paging events to this instruction.
*/
decoder->bound_paging = 1;
return pt_blk_postpone_insn(decoder, &insn, &iext);
case ptev_async_paging:
status = pt_blk_proceed_to_async_paging(decoder, block, ev);
if (status <= 0)
return status;
break;
case ptev_vmcs:
if (!decoder->enabled)
break;
status = pt_blk_proceed_to_insn(decoder, block, &insn, &iext,
pt_insn_binds_to_vmcs);
if (status <= 0)
return status;
/* We bound a vmcs event. Make sure we do not bind further vmcs
* events to this instruction.
*/
decoder->bound_vmcs = 1;
return pt_blk_postpone_insn(decoder, &insn, &iext);
case ptev_async_vmcs:
status = pt_blk_proceed_to_async_vmcs(decoder, block, ev);
if (status <= 0)
return status;
break;
case ptev_overflow:
break;
case ptev_exec_mode:
status = pt_blk_proceed_to_exec_mode(decoder, block, ev);
if (status <= 0)
return status;
break;
case ptev_tsx:
if (ev->ip_suppressed)
break;
status = pt_blk_proceed_to_ip(decoder, block, &insn, &iext,
ev->variant.tsx.ip);
if (status <= 0)
return status;
break;
case ptev_stop:
break;
case ptev_exstop:
if (!decoder->enabled || ev->ip_suppressed)
break;
status = pt_blk_proceed_to_ip(decoder, block, &insn, &iext,
ev->variant.exstop.ip);
if (status <= 0)
return status;
break;
case ptev_mwait:
if (!decoder->enabled || ev->ip_suppressed)
break;
status = pt_blk_proceed_to_ip(decoder, block, &insn, &iext,
ev->variant.mwait.ip);
if (status <= 0)
return status;
break;
case ptev_pwre:
case ptev_pwrx:
break;
case ptev_ptwrite:
if (!decoder->enabled)
break;
status = pt_blk_proceed_to_ptwrite(decoder, block, &insn,
&iext, ev);
if (status <= 0)
return status;
/* We bound a ptwrite event. Make sure we do not bind further
* ptwrite events to this instruction.
*/
decoder->bound_ptwrite = 1;
return pt_blk_postpone_insn(decoder, &insn, &iext);
case ptev_tick:
case ptev_cbr:
case ptev_mnt:
break;
}
return pt_blk_status(decoder, pts_event_pending);
}
/* Proceed to the next decision point without using the block cache.
*
* Tracing is enabled and we don't have an event pending. Proceed as far as
* we get without trace. Stop when we either:
*
* - need trace in order to continue
* - overflow the max number of instructions in a block
*
* We actually proceed one instruction further to get the start IP for the next
* block. This only updates @decoder's internal state, though.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_proceed_no_event_uncached(struct pt_block_decoder *decoder,
struct pt_block *block)
{
struct pt_insn_ext iext;
struct pt_insn insn;
int status;
if (!decoder || !block)
return -pte_internal;
/* This is overly conservative, really. We shouldn't get a bad-query
* status unless we decoded at least one instruction successfully.
*/
memset(&insn, 0, sizeof(insn));
memset(&iext, 0, sizeof(iext));
/* Proceed as far as we get without trace. */
status = pt_blk_proceed_to_insn(decoder, block, &insn, &iext,
pt_insn_false);
if (status < 0) {
if (status != -pte_bad_query)
return status;
return pt_blk_proceed_with_trace(decoder, &insn, &iext);
}
return 0;
}
/* Check if @ip is contained in @section loaded at @laddr.
*
* Returns non-zero if it is.
* Returns zero if it isn't or of @section is NULL.
*/
static inline int pt_blk_is_in_section(const struct pt_mapped_section *msec,
uint64_t ip)
{
uint64_t begin, end;
begin = pt_msec_begin(msec);
end = pt_msec_end(msec);
return (begin <= ip && ip < end);
}
/* Insert a trampoline block cache entry.
*
* Add a trampoline block cache entry at @ip to continue at @nip, where @nip
* must be the next instruction after @ip.
*
* Both @ip and @nip must be section-relative
*
* Returns zero on success, a negative error code otherwise.
*/
static inline int pt_blk_add_trampoline(struct pt_block_cache *bcache,
uint64_t ip, uint64_t nip,
enum pt_exec_mode mode)
{
struct pt_bcache_entry bce;
int64_t disp;
/* The displacement from @ip to @nip for the trampoline. */
disp = (int64_t) (nip - ip);
memset(&bce, 0, sizeof(bce));
bce.displacement = (int32_t) disp;
bce.ninsn = 1;
bce.mode = mode;
bce.qualifier = ptbq_again;
/* If we can't reach @nip without overflowing the displacement field, we
* have to stop and re-decode the instruction at @ip.
*/
if ((int64_t) bce.displacement != disp) {
memset(&bce, 0, sizeof(bce));
bce.ninsn = 1;
bce.mode = mode;
bce.qualifier = ptbq_decode;
}
return pt_bcache_add(bcache, ip, bce);
}
/* Insert a decode block cache entry.
*
* Add a decode block cache entry at @ioff.
*
* Returns zero on success, a negative error code otherwise.
*/
static inline int pt_blk_add_decode(struct pt_block_cache *bcache,
uint64_t ioff, enum pt_exec_mode mode)
{
struct pt_bcache_entry bce;
memset(&bce, 0, sizeof(bce));
bce.ninsn = 1;
bce.mode = mode;
bce.qualifier = ptbq_decode;
return pt_bcache_add(bcache, ioff, bce);
}
enum {
/* The maximum number of steps when filling the block cache. */
bcache_fill_steps = 0x400
};
/* Proceed to the next instruction and fill the block cache for @decoder->ip.
*
* Tracing is enabled and we don't have an event pending. The current IP is not
* yet cached.
*
* Proceed one instruction without using the block cache, then try to proceed
* further using the block cache.
*
* On our way back, add a block cache entry for the IP before proceeding. Note
* that the recursion is bounded by @steps and ultimately by the maximum number
* of instructions in a block.
*
* Returns zero on success, a negative error code otherwise.
*/
static int
pt_blk_proceed_no_event_fill_cache(struct pt_block_decoder *decoder,
struct pt_block *block,
struct pt_block_cache *bcache,
const struct pt_mapped_section *msec,
size_t steps)
{
struct pt_bcache_entry bce;
struct pt_insn_ext iext;
struct pt_insn insn;
uint64_t nip, dip, ioff, noff;
int64_t disp;
int status;
if (!decoder || !steps)
return -pte_internal;
/* Proceed one instruction by decoding and examining it.
*
* Note that we also return on a status of zero that indicates that the
* instruction didn't fit into @block.
*/
status = pt_blk_proceed_one_insn(decoder, block, &insn, &iext);
if (status <= 0)
return status;
ioff = pt_msec_unmap(msec, insn.ip);
/* Let's see if we can proceed to the next IP without trace.
*
* If we can't, this is certainly a decision point.
*/
status = pt_insn_next_ip(&decoder->ip, &insn, &iext);
if (status < 0) {
if (status != -pte_bad_query)
return status;
memset(&bce, 0, sizeof(bce));
bce.ninsn = 1;
bce.mode = insn.mode;
bce.isize = insn.size;
/* Clear the instruction size in case of overflows. */
if ((uint8_t) bce.isize != insn.size)
bce.isize = 0;
switch (insn.iclass) {
case ptic_ptwrite:
case ptic_error:
case ptic_other:
return -pte_internal;
case ptic_jump:
/* A direct jump doesn't require trace. */
if (iext.variant.branch.is_direct)
return -pte_internal;
bce.qualifier = ptbq_indirect;
break;
case ptic_call:
/* A direct call doesn't require trace. */
if (iext.variant.branch.is_direct)
return -pte_internal;
bce.qualifier = ptbq_ind_call;
break;
case ptic_return:
bce.qualifier = ptbq_return;
break;
case ptic_cond_jump:
bce.qualifier = ptbq_cond;
break;
case ptic_far_call:
case ptic_far_return:
case ptic_far_jump:
bce.qualifier = ptbq_indirect;
break;
}
/* If the block was truncated, we have to decode its last
* instruction each time.
*
* We could have skipped the above switch and size assignment in
* this case but this is already a slow and hopefully infrequent
* path.
*/
if (block->truncated)
bce.qualifier = ptbq_decode;
status = pt_bcache_add(bcache, ioff, bce);
if (status < 0)
return status;
return pt_blk_proceed_with_trace(decoder, &insn, &iext);
}
/* The next instruction's IP. */
nip = decoder->ip;
noff = pt_msec_unmap(msec, nip);
/* Even if we were able to proceed without trace, we might have to stop
* here for various reasons:
*
* - at near direct calls to update the return-address stack
*
* We are forced to re-decode @insn to get the branch displacement.
*
* Even though it is constant, we don't cache it to avoid increasing
* the size of a cache entry. Note that the displacement field is
* zero for this entry and we might be tempted to use it - but other
* entries that point to this decision point will have non-zero
* displacement.
*
* We could proceed after a near direct call but we migh as well
* postpone it to the next iteration. Make sure to end the block if
* @decoder->flags.variant.block.end_on_call is set, though.
*
* - at near direct backwards jumps to detect section splits
*
* In case the current section is split underneath us, we must take
* care to detect that split.
*
* There is one corner case where the split is in the middle of a
* linear sequence of instructions that branches back into the
* originating section.
*
* Calls, indirect branches, and far branches are already covered
* since they either require trace or already require us to stop
* (i.e. near direct calls) for other reasons. That leaves near
* direct backward jumps.
*
* Instead of the decode stop at the jump instruction we're using we
* could have made sure that other block cache entries that extend
* this one insert a trampoline to the jump's entry. This would
* have been a bit more complicated.
*
* - if we switched sections
*
* This ends a block just like a branch that requires trace.
*
* We need to re-decode @insn in order to determine the start IP of
* the next block.
*
* - if the block is truncated
*
* We need to read the last instruction's memory from multiple
* sections and provide it to the user.
*
* We could still use the block cache but then we'd have to handle
* this case for each qualifier. Truncation is hopefully rare and
* having to read the memory for the instruction from multiple
* sections is already slow. Let's rather keep things simple and
* route it through the decode flow, where we already have
* everything in place.
*/
switch (insn.iclass) {
case ptic_call:
return pt_blk_add_decode(bcache, ioff, insn.mode);
case ptic_jump:
/* An indirect branch requires trace and should have been
* handled above.
*/
if (!iext.variant.branch.is_direct)
return -pte_internal;
if (iext.variant.branch.displacement < 0 ||
decoder->flags.variant.block.end_on_jump)
return pt_blk_add_decode(bcache, ioff, insn.mode);
fallthrough;
default:
if (!pt_blk_is_in_section(msec, nip) || block->truncated)
return pt_blk_add_decode(bcache, ioff, insn.mode);
break;
}
/* We proceeded one instruction. Let's see if we have a cache entry for
* the next instruction.
*/
status = pt_bcache_lookup(&bce, bcache, noff);
if (status < 0)
return status;
/* If we don't have a valid cache entry, yet, fill the cache some more.
*
* On our way back, we add a cache entry for this instruction based on
* the cache entry of the succeeding instruction.
*/
if (!pt_bce_is_valid(bce)) {
/* If we exceeded the maximum number of allowed steps, we insert
* a trampoline to the next instruction.
*
* The next time we encounter the same code, we will use the
* trampoline to jump directly to where we left off this time
* and continue from there.
*/
steps -= 1;
if (!steps)
return pt_blk_add_trampoline(bcache, ioff, noff,
insn.mode);
status = pt_blk_proceed_no_event_fill_cache(decoder, block,
bcache, msec,
steps);
if (status < 0)
return status;
/* Let's see if we have more luck this time. */
status = pt_bcache_lookup(&bce, bcache, noff);
if (status < 0)
return status;
/* If we still don't have a valid cache entry, we're done. Most
* likely, @block overflowed and we couldn't proceed past the
* next instruction.
*/
if (!pt_bce_is_valid(bce))
return 0;
}
/* We must not have switched execution modes.
*
* This would require an event and we're on the no-event flow.
*/
if (pt_bce_exec_mode(bce) != insn.mode)
return -pte_internal;
/* The decision point IP and the displacement from @insn.ip. */
dip = nip + (uint64_t) (int64_t) bce.displacement;
disp = (int64_t) (dip - insn.ip);
/* We may have switched sections if the section was split. See
* pt_blk_proceed_no_event_cached() for a more elaborate comment.
*
* We're not adding a block cache entry since this won't apply to the
* original section which may be shared with other decoders.
*
* We will instead take the slow path until the end of the section.
*/
if (!pt_blk_is_in_section(msec, dip))
return 0;
/* Let's try to reach @nip's decision point from @insn.ip.
*
* There are two fields that may overflow: @bce.ninsn and
* @bce.displacement.
*/
bce.ninsn += 1;
bce.displacement = (int32_t) disp;
/* If none of them overflowed, we're done.
*
* If one or both overflowed, let's try to insert a trampoline, i.e. we
* try to reach @dip via a ptbq_again entry to @nip.
*/
if (!bce.ninsn || ((int64_t) bce.displacement != disp))
return pt_blk_add_trampoline(bcache, ioff, noff, insn.mode);
/* We're done. Add the cache entry.
*
* There's a chance that other decoders updated the cache entry in the
* meantime. They should have come to the same conclusion as we,
* though, and the cache entries should be identical.
*
* Cache updates are atomic so even if the two versions were not
* identical, we wouldn't care because they are both correct.
*/
return pt_bcache_add(bcache, ioff, bce);
}
/* Proceed at a potentially truncated instruction.
*
* We were not able to decode the instruction at @decoder->ip in @decoder's
* cached section. This is typically caused by not having enough bytes.
*
* Try to decode the instruction again using the entire image. If this succeeds
* we expect to end up with an instruction that was truncated in the section it
* started. We provide the full instruction in this case and end the block.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_proceed_truncated(struct pt_block_decoder *decoder,
struct pt_block *block)
{
struct pt_insn_ext iext;
struct pt_insn insn;
int errcode;
if (!decoder || !block)
return -pte_internal;
memset(&iext, 0, sizeof(iext));
memset(&insn, 0, sizeof(insn));
insn.mode = decoder->mode;
insn.ip = decoder->ip;
errcode = pt_insn_decode(&insn, &iext, decoder->image, &decoder->asid);
if (errcode < 0)
return errcode;
/* We shouldn't use this function if the instruction isn't truncated. */
if (!insn.truncated)
return -pte_internal;
/* Provide the instruction in the block. This ends the block. */
memcpy(block->raw, insn.raw, insn.size);
block->iclass = insn.iclass;
block->size = insn.size;
block->truncated = 1;
/* Log calls' return addresses for return compression. */
errcode = pt_blk_log_call(decoder, &insn, &iext);
if (errcode < 0)
return errcode;
/* Let's see if we can proceed to the next IP without trace.
*
* The truncated instruction ends the block but we still need to get the
* next block's start IP.
*/
errcode = pt_insn_next_ip(&decoder->ip, &insn, &iext);
if (errcode < 0) {
if (errcode != -pte_bad_query)
return errcode;
return pt_blk_proceed_with_trace(decoder, &insn, &iext);
}
return 0;
}
/* Proceed to the next decision point using the block cache.
*
* Tracing is enabled and we don't have an event pending. We already set
* @block's isid. All reads are done within @msec as we're not switching
* sections between blocks.
*
* Proceed as far as we get without trace. Stop when we either:
*
* - need trace in order to continue
* - overflow the max number of instructions in a block
*
* We actually proceed one instruction further to get the start IP for the next
* block. This only updates @decoder's internal state, though.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_proceed_no_event_cached(struct pt_block_decoder *decoder,
struct pt_block *block,
struct pt_block_cache *bcache,
const struct pt_mapped_section *msec)
{
struct pt_bcache_entry bce;
uint16_t binsn, ninsn;
uint64_t offset, nip;
int status;
if (!decoder || !block)
return -pte_internal;
offset = pt_msec_unmap(msec, decoder->ip);
status = pt_bcache_lookup(&bce, bcache, offset);
if (status < 0)
return status;
/* If we don't find a valid cache entry, fill the cache. */
if (!pt_bce_is_valid(bce))
return pt_blk_proceed_no_event_fill_cache(decoder, block,
bcache, msec,
bcache_fill_steps);
/* If we switched sections, the origianl section must have been split
* underneath us. A split preserves the block cache of the original
* section.
*
* Crossing sections requires ending the block so we can indicate the
* proper isid for the entire block.
*
* Plus there's the chance that the new section that caused the original
* section to split changed instructions.
*
* This check will also cover changes to a linear sequence of code we
* would otherwise have jumped over as long as the start and end are in
* different sub-sections.
*
* Since we stop on every (backwards) branch (through an artificial stop
* in the case of a near direct backward branch) we will detect all
* section splits.
*
* Switch to the slow path until we reach the end of this section.
*/
nip = decoder->ip + (uint64_t) (int64_t) bce.displacement;
if (!pt_blk_is_in_section(msec, nip))
return pt_blk_proceed_no_event_uncached(decoder, block);
/* We have a valid cache entry. Let's first check if the way to the
* decision point still fits into @block.
*
* If it doesn't, we end the block without filling it as much as we
* could since this would require us to switch to the slow path.
*
* On the next iteration, we will start with an empty block, which is
* guaranteed to have enough room for at least one block cache entry.
*/
binsn = block->ninsn;
ninsn = binsn + (uint16_t) bce.ninsn;
if (ninsn < binsn)
return 0;
/* Jump ahead to the decision point and proceed from there.
*
* We're not switching execution modes so even if @block already has an
* execution mode, it will be the one we're going to set.
*/
decoder->ip = nip;
/* We don't know the instruction class so we should be setting it to
* ptic_error. Since we will be able to fill it back in later in most
* cases, we move the clearing to the switch cases that don't.
*/
block->end_ip = nip;
block->ninsn = ninsn;
block->mode = pt_bce_exec_mode(bce);
switch (pt_bce_qualifier(bce)) {
case ptbq_again:
/* We're not able to reach the actual decision point due to
* overflows so we inserted a trampoline.
*
* We don't know the instruction and it is not guaranteed that
* we will proceed further (e.g. if @block overflowed). Let's
* clear any previously stored instruction class which has
* become invalid when we updated @block->ninsn.
*/
block->iclass = ptic_error;
return pt_blk_proceed_no_event_cached(decoder, block, bcache,
msec);
case ptbq_cond:
/* We're at a conditional branch. */
block->iclass = ptic_cond_jump;
/* Let's first check whether we know the size of the
* instruction. If we do, we might get away without decoding
* the instruction.
*
* If we don't know the size we might as well do the full decode
* and proceed-with-trace flow we do for ptbq_decode.
*/
if (bce.isize) {
uint64_t ip;
int taken;
/* If the branch is not taken, we don't need to decode
* the instruction at @decoder->ip.
*
* If it is taken, we have to implement everything here.
* We can't use the normal decode and proceed-with-trace
* flow since we already consumed the TNT bit.
*/
status = pt_blk_cond_branch(decoder, &taken);
if (status < 0)
return status;
/* Preserve the query decoder's response which indicates
* upcoming events.
*/
decoder->status = status;
ip = decoder->ip;
if (taken) {
struct pt_insn_ext iext;
struct pt_insn insn;
memset(&iext, 0, sizeof(iext));
memset(&insn, 0, sizeof(insn));
insn.mode = pt_bce_exec_mode(bce);
insn.ip = ip;
status = pt_blk_decode_in_section(&insn, &iext,
msec);
if (status < 0)
return status;
ip += (uint64_t) (int64_t)
iext.variant.branch.displacement;
}
decoder->ip = ip + bce.isize;
break;
}
fallthrough;
case ptbq_decode: {
struct pt_insn_ext iext;
struct pt_insn insn;
/* We need to decode the instruction at @decoder->ip and decide
* what to do based on that.
*
* We already accounted for the instruction so we can't just
* call pt_blk_proceed_one_insn().
*/
memset(&iext, 0, sizeof(iext));
memset(&insn, 0, sizeof(insn));
insn.mode = pt_bce_exec_mode(bce);
insn.ip = decoder->ip;
status = pt_blk_decode_in_section(&insn, &iext, msec);
if (status < 0) {
if (status != -pte_bad_insn)
return status;
return pt_blk_proceed_truncated(decoder, block);
}
/* We just decoded @insn so we know the instruction class. */
block->iclass = insn.iclass;
/* Log calls' return addresses for return compression. */
status = pt_blk_log_call(decoder, &insn, &iext);
if (status < 0)
return status;
/* Let's see if we can proceed to the next IP without trace.
*
* Note that we also stop due to displacement overflows or to
* maintain the return-address stack for near direct calls.
*/
status = pt_insn_next_ip(&decoder->ip, &insn, &iext);
if (status < 0) {
if (status != -pte_bad_query)
return status;
/* We can't, so let's proceed with trace, which
* completes the block.
*/
return pt_blk_proceed_with_trace(decoder, &insn, &iext);
}
/* End the block if the user asked us to.
*
* We only need to take care about direct near branches.
* Indirect and far branches require trace and will naturally
* end a block.
*/
if ((decoder->flags.variant.block.end_on_call &&
(insn.iclass == ptic_call)) ||
(decoder->flags.variant.block.end_on_jump &&
(insn.iclass == ptic_jump)))
break;
/* If we can proceed without trace and we stay in @msec we may
* proceed further.
*
* We're done if we switch sections, though.
*/
if (!pt_blk_is_in_section(msec, decoder->ip))
break;
return pt_blk_proceed_no_event_cached(decoder, block, bcache,
msec);
}
case ptbq_ind_call: {
uint64_t ip;
/* We're at a near indirect call. */
block->iclass = ptic_call;
/* We need to update the return-address stack and query the
* destination IP.
*/
ip = decoder->ip;
/* If we already know the size of the instruction, we don't need
* to re-decode it.
*/
if (bce.isize)
ip += bce.isize;
else {
struct pt_insn_ext iext;
struct pt_insn insn;
memset(&iext, 0, sizeof(iext));
memset(&insn, 0, sizeof(insn));
insn.mode = pt_bce_exec_mode(bce);
insn.ip = ip;
status = pt_blk_decode_in_section(&insn, &iext, msec);
if (status < 0)
return status;
ip += insn.size;
}
status = pt_retstack_push(&decoder->retstack, ip);
if (status < 0)
return status;
status = pt_blk_indirect_branch(decoder, &decoder->ip);
if (status < 0)
return status;
/* Preserve the query decoder's response which indicates
* upcoming events.
*/
decoder->status = status;
break;
}
case ptbq_return: {
int taken;
/* We're at a near return. */
block->iclass = ptic_return;
/* Check for a compressed return. */
status = pt_blk_cond_branch(decoder, &taken);
if (status < 0) {
if (status != -pte_bad_query)
return status;
/* The return is not compressed. We need another query
* to determine the destination IP.
*/
status = pt_blk_indirect_branch(decoder, &decoder->ip);
if (status < 0)
return status;
/* Preserve the query decoder's response which indicates
* upcoming events.
*/
decoder->status = status;
break;
}
/* Preserve the query decoder's response which indicates
* upcoming events.
*/
decoder->status = status;
/* A compressed return is indicated by a taken conditional
* branch.
*/
if (!taken)
return -pte_bad_retcomp;
return pt_retstack_pop(&decoder->retstack, &decoder->ip);
}
case ptbq_indirect:
/* We're at an indirect jump or far transfer.
*
* We don't know the exact instruction class and there's no
* reason to decode the instruction for any other purpose.
*
* Indicate that we don't know the instruction class and leave
* it to our caller to decode the instruction if needed.
*/
block->iclass = ptic_error;
/* This is neither a near call nor return so we don't need to
* touch the return-address stack.
*
* Just query the destination IP.
*/
status = pt_blk_indirect_branch(decoder, &decoder->ip);
if (status < 0)
return status;
/* Preserve the query decoder's response which indicates
* upcoming events.
*/
decoder->status = status;
break;
}
return 0;
}
static int pt_blk_msec_fill(struct pt_block_decoder *decoder,
const struct pt_mapped_section **pmsec)
{
const struct pt_mapped_section *msec;
struct pt_section *section;
int isid, errcode;
if (!decoder || !pmsec)
return -pte_internal;
isid = pt_msec_cache_fill(&decoder->scache, &msec, decoder->image,
&decoder->asid, decoder->ip);
if (isid < 0)
return isid;
section = pt_msec_section(msec);
if (!section)
return -pte_internal;
*pmsec = msec;
errcode = pt_section_request_bcache(section);
if (errcode < 0)
return errcode;
return isid;
}
static inline int pt_blk_msec_lookup(struct pt_block_decoder *decoder,
const struct pt_mapped_section **pmsec)
{
int isid;
if (!decoder)
return -pte_internal;
isid = pt_msec_cache_read(&decoder->scache, pmsec, decoder->image,
decoder->ip);
if (isid < 0) {
if (isid != -pte_nomap)
return isid;
return pt_blk_msec_fill(decoder, pmsec);
}
return isid;
}
/* Proceed to the next decision point - try using the cache.
*
* Tracing is enabled and we don't have an event pending. Proceed as far as
* we get without trace. Stop when we either:
*
* - need trace in order to continue
* - overflow the max number of instructions in a block
*
* We actually proceed one instruction further to get the start IP for the next
* block. This only updates @decoder's internal state, though.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_proceed_no_event(struct pt_block_decoder *decoder,
struct pt_block *block)
{
const struct pt_mapped_section *msec;
struct pt_block_cache *bcache;
struct pt_section *section;
int isid;
if (!decoder || !block)
return -pte_internal;
isid = pt_blk_msec_lookup(decoder, &msec);
if (isid < 0) {
if (isid != -pte_nomap)
return isid;
/* Even if there is no such section in the image, we may still
* read the memory via the callback function.
*/
return pt_blk_proceed_no_event_uncached(decoder, block);
}
/* We do not switch sections inside a block. */
if (isid != block->isid) {
if (!pt_blk_block_is_empty(block))
return 0;
block->isid = isid;
}
section = pt_msec_section(msec);
if (!section)
return -pte_internal;
bcache = pt_section_bcache(section);
if (!bcache)
return pt_blk_proceed_no_event_uncached(decoder, block);
return pt_blk_proceed_no_event_cached(decoder, block, bcache, msec);
}
/* Proceed to the next event or decision point.
*
* Returns a non-negative pt_status_flag bit-vector on success, a negative error
* code otherwise.
*/
static int pt_blk_proceed(struct pt_block_decoder *decoder,
struct pt_block *block)
{
int status;
status = pt_blk_fetch_event(decoder);
if (status != 0) {
if (status < 0)
return status;
return pt_blk_proceed_event(decoder, block);
}
/* If tracing is disabled we should either be out of trace or we should
* have taken the event flow above.
*/
if (!decoder->enabled) {
if (decoder->status & pts_eos)
return -pte_eos;
return -pte_no_enable;
}
status = pt_blk_proceed_no_event(decoder, block);
if (status < 0)
return status;
return pt_blk_proceed_trailing_event(decoder, block);
}
enum {
/* The maximum number of steps to take when determining whether the
* event location can be reached.
*/
bdm64_max_steps = 0x100
};
/* Try to work around erratum BDM64.
*
* If we got a transaction abort immediately following a branch that produced
* trace, the trace for that branch might have been corrupted.
*
* Returns a positive integer if the erratum was handled.
* Returns zero if the erratum does not seem to apply.
* Returns a negative error code otherwise.
*/
static int pt_blk_handle_erratum_bdm64(struct pt_block_decoder *decoder,
const struct pt_block *block,
const struct pt_event *ev)
{
struct pt_insn_ext iext;
struct pt_insn insn;
int status;
if (!decoder || !block || !ev)
return -pte_internal;
/* This only affects aborts. */
if (!ev->variant.tsx.aborted)
return 0;
/* This only affects branches that require trace.
*
* If the erratum hits, that branch ended the current block and brought
* us to the trailing event flow.
*/
if (pt_blk_block_is_empty(block))
return 0;
insn.mode = block->mode;
insn.ip = block->end_ip;
status = pt_insn_decode(&insn, &iext, decoder->image, &decoder->asid);
if (status < 0)
return 0;
if (!pt_insn_is_branch(&insn, &iext))
return 0;
/* Let's check if we can reach the event location from here.
*
* If we can, let's assume the erratum did not hit. We might still be
* wrong but we're not able to tell.
*/
status = pt_insn_range_is_contiguous(decoder->ip, ev->variant.tsx.ip,
decoder->mode, decoder->image,
&decoder->asid, bdm64_max_steps);
if (status > 0)
return status;
/* We can't reach the event location. This could either mean that we
* stopped too early (and status is zero) or that the erratum hit.
*
* We assume the latter and pretend that the previous branch brought us
* to the event location, instead.
*/
decoder->ip = ev->variant.tsx.ip;
return 1;
}
/* Check whether a trailing TSX event should be postponed.
*
* This involves handling erratum BDM64.
*
* Returns a positive integer if the event is to be postponed.
* Returns zero if the event should be processed.
* Returns a negative error code otherwise.
*/
static inline int pt_blk_postpone_trailing_tsx(struct pt_block_decoder *decoder,
struct pt_block *block,
const struct pt_event *ev)
{
int status;
if (!decoder || !ev)
return -pte_internal;
if (ev->ip_suppressed)
return 0;
if (block && decoder->query.config.errata.bdm64) {
status = pt_blk_handle_erratum_bdm64(decoder, block, ev);
if (status < 0)
return 1;
}
if (decoder->ip != ev->variant.tsx.ip)
return 1;
return 0;
}
/* Proceed with events that bind to the current decoder IP.
*
* This function is used in the following scenarios:
*
* - we just synchronized onto the trace stream
* - we ended a block and proceeded to the next IP
* - we processed an event that was indicated by this function
*
* Check if there is an event at the current IP that needs to be indicated to
* the user.
*
* Returns a non-negative pt_status_flag bit-vector on success, a negative error
* code otherwise.
*/
static int pt_blk_proceed_trailing_event(struct pt_block_decoder *decoder,
struct pt_block *block)
{
struct pt_event *ev;
int status;
if (!decoder)
return -pte_internal;
status = pt_blk_fetch_event(decoder);
if (status <= 0) {
if (status < 0)
return status;
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
return pt_blk_status(decoder, 0);
}
ev = &decoder->event;
switch (ev->type) {
case ptev_disabled:
/* Synchronous disable events are normally indicated on the
* event flow.
*/
if (!decoder->process_insn)
break;
/* A sync disable may bind to a CR3 changing instruction. */
if (ev->ip_suppressed &&
pt_insn_changes_cr3(&decoder->insn, &decoder->iext))
return pt_blk_status(decoder, pts_event_pending);
/* Or it binds to the next branch that would require trace.
*
* Try to complete processing the current instruction by
* proceeding past it. If that fails because it would require
* trace, we can apply the disabled event.
*/
status = pt_insn_next_ip(&decoder->ip, &decoder->insn,
&decoder->iext);
if (status < 0) {
if (status != -pte_bad_query)
return status;
status = pt_blk_set_disable_resume_ip(decoder,
&decoder->insn);
if (status < 0)
return status;
return pt_blk_status(decoder, pts_event_pending);
}
/* We proceeded past the current instruction. */
status = pt_blk_clear_postponed_insn(decoder);
if (status < 0)
return status;
/* This might have brought us to the disable IP. */
if (!ev->ip_suppressed &&
decoder->ip == ev->variant.disabled.ip)
return pt_blk_status(decoder, pts_event_pending);
break;
case ptev_enabled:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
return pt_blk_status(decoder, pts_event_pending);
case ptev_async_disabled:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
if (decoder->ip != ev->variant.async_disabled.at)
break;
if (decoder->query.config.errata.skd022) {
status = pt_blk_handle_erratum_skd022(decoder, ev);
if (status != 0) {
if (status < 0)
return status;
/* If the erratum applies, the event is modified
* to a synchronous disable event that will be
* processed on the next pt_blk_proceed_event()
* call. We're done.
*/
break;
}
}
return pt_blk_status(decoder, pts_event_pending);
case ptev_async_branch:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
if (decoder->ip != ev->variant.async_branch.from)
break;
return pt_blk_status(decoder, pts_event_pending);
case ptev_paging:
/* We apply the event immediately if we're not tracing. */
if (!decoder->enabled)
return pt_blk_status(decoder, pts_event_pending);
/* Synchronous paging events are normally indicated on the event
* flow, unless they bind to the same instruction as a previous
* event.
*
* We bind at most one paging event to an instruction, though.
*/
if (!decoder->process_insn || decoder->bound_paging)
break;
/* We're done if we're not binding to the currently postponed
* instruction. We will process the event on the normal event
* flow in the next iteration.
*/
if (!pt_insn_binds_to_pip(&decoder->insn, &decoder->iext))
break;
/* We bound a paging event. Make sure we do not bind further
* paging events to this instruction.
*/
decoder->bound_paging = 1;
return pt_blk_status(decoder, pts_event_pending);
case ptev_async_paging:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
if (!ev->ip_suppressed &&
decoder->ip != ev->variant.async_paging.ip)
break;
return pt_blk_status(decoder, pts_event_pending);
case ptev_vmcs:
/* We apply the event immediately if we're not tracing. */
if (!decoder->enabled)
return pt_blk_status(decoder, pts_event_pending);
/* Synchronous vmcs events are normally indicated on the event
* flow, unless they bind to the same instruction as a previous
* event.
*
* We bind at most one vmcs event to an instruction, though.
*/
if (!decoder->process_insn || decoder->bound_vmcs)
break;
/* We're done if we're not binding to the currently postponed
* instruction. We will process the event on the normal event
* flow in the next iteration.
*/
if (!pt_insn_binds_to_vmcs(&decoder->insn, &decoder->iext))
break;
/* We bound a vmcs event. Make sure we do not bind further vmcs
* events to this instruction.
*/
decoder->bound_vmcs = 1;
return pt_blk_status(decoder, pts_event_pending);
case ptev_async_vmcs:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
if (!ev->ip_suppressed &&
decoder->ip != ev->variant.async_vmcs.ip)
break;
return pt_blk_status(decoder, pts_event_pending);
case ptev_overflow:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
return pt_blk_status(decoder, pts_event_pending);
case ptev_exec_mode:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
if (!ev->ip_suppressed &&
decoder->ip != ev->variant.exec_mode.ip)
break;
return pt_blk_status(decoder, pts_event_pending);
case ptev_tsx:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
status = pt_blk_postpone_trailing_tsx(decoder, block, ev);
if (status != 0) {
if (status < 0)
return status;
break;
}
return pt_blk_status(decoder, pts_event_pending);
case ptev_stop:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
return pt_blk_status(decoder, pts_event_pending);
case ptev_exstop:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
if (!ev->ip_suppressed && decoder->enabled &&
decoder->ip != ev->variant.exstop.ip)
break;
return pt_blk_status(decoder, pts_event_pending);
case ptev_mwait:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
if (!ev->ip_suppressed && decoder->enabled &&
decoder->ip != ev->variant.mwait.ip)
break;
return pt_blk_status(decoder, pts_event_pending);
case ptev_pwre:
case ptev_pwrx:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
return pt_blk_status(decoder, pts_event_pending);
case ptev_ptwrite:
/* We apply the event immediately if we're not tracing. */
if (!decoder->enabled)
return pt_blk_status(decoder, pts_event_pending);
/* Ptwrite events are normally indicated on the event flow,
* unless they bind to the same instruction as a previous event.
*
* We bind at most one ptwrite event to an instruction, though.
*/
if (!decoder->process_insn || decoder->bound_ptwrite)
break;
/* We're done if we're not binding to the currently postponed
* instruction. We will process the event on the normal event
* flow in the next iteration.
*/
if (!ev->ip_suppressed ||
!pt_insn_is_ptwrite(&decoder->insn, &decoder->iext))
break;
/* We bound a ptwrite event. Make sure we do not bind further
* ptwrite events to this instruction.
*/
decoder->bound_ptwrite = 1;
return pt_blk_status(decoder, pts_event_pending);
case ptev_tick:
case ptev_cbr:
case ptev_mnt:
/* This event does not bind to an instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
return pt_blk_status(decoder, pts_event_pending);
}
/* No further events. Proceed past any postponed instruction. */
status = pt_blk_proceed_postponed_insn(decoder);
if (status < 0)
return status;
return pt_blk_status(decoder, 0);
}
int pt_blk_next(struct pt_block_decoder *decoder, struct pt_block *ublock,
size_t size)
{
struct pt_block block, *pblock;
int errcode, status;
if (!decoder || !ublock)
return -pte_invalid;
pblock = size == sizeof(block) ? ublock : █
/* Zero-initialize the block in case of error returns. */
memset(pblock, 0, sizeof(*pblock));
/* Fill in a few things from the current decode state.
*
* This reflects the state of the last pt_blk_next() or pt_blk_start()
* call. Note that, unless we stop with tracing disabled, we proceed
* already to the start IP of the next block.
*
* Some of the state may later be overwritten as we process events.
*/
pblock->ip = decoder->ip;
pblock->mode = decoder->mode;
if (decoder->speculative)
pblock->speculative = 1;
/* Proceed one block. */
status = pt_blk_proceed(decoder, pblock);
errcode = block_to_user(ublock, size, pblock);
if (errcode < 0)
return errcode;
return status;
}
/* Process an enabled event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_enabled(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
if (!decoder || !ev)
return -pte_internal;
/* This event can't be a status update. */
if (ev->status_update)
return -pte_bad_context;
/* We must have an IP in order to start decoding. */
if (ev->ip_suppressed)
return -pte_noip;
/* We must currently be disabled. */
if (decoder->enabled)
return -pte_bad_context;
decoder->ip = ev->variant.enabled.ip;
decoder->enabled = 1;
decoder->process_event = 0;
return 0;
}
/* Process a disabled event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_disabled(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
if (!decoder || !ev)
return -pte_internal;
/* This event can't be a status update. */
if (ev->status_update)
return -pte_bad_context;
/* We must currently be enabled. */
if (!decoder->enabled)
return -pte_bad_context;
/* We preserve @decoder->ip. This is where we expect tracing to resume
* and we'll indicate that on the subsequent enabled event if tracing
* actually does resume from there.
*/
decoder->enabled = 0;
decoder->process_event = 0;
return 0;
}
/* Process an asynchronous branch event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_async_branch(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
if (!decoder || !ev)
return -pte_internal;
/* This event can't be a status update. */
if (ev->status_update)
return -pte_bad_context;
/* We must currently be enabled. */
if (!decoder->enabled)
return -pte_bad_context;
/* Jump to the branch destination. We will continue from there in the
* next iteration.
*/
decoder->ip = ev->variant.async_branch.to;
decoder->process_event = 0;
return 0;
}
/* Process a paging event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_paging(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
uint64_t cr3;
int errcode;
if (!decoder || !ev)
return -pte_internal;
cr3 = ev->variant.paging.cr3;
if (decoder->asid.cr3 != cr3) {
errcode = pt_msec_cache_invalidate(&decoder->scache);
if (errcode < 0)
return errcode;
decoder->asid.cr3 = cr3;
}
decoder->process_event = 0;
return 0;
}
/* Process a vmcs event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_vmcs(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
uint64_t vmcs;
int errcode;
if (!decoder || !ev)
return -pte_internal;
vmcs = ev->variant.vmcs.base;
if (decoder->asid.vmcs != vmcs) {
errcode = pt_msec_cache_invalidate(&decoder->scache);
if (errcode < 0)
return errcode;
decoder->asid.vmcs = vmcs;
}
decoder->process_event = 0;
return 0;
}
/* Process an overflow event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_overflow(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
if (!decoder || !ev)
return -pte_internal;
/* This event can't be a status update. */
if (ev->status_update)
return -pte_bad_context;
/* If the IP is suppressed, the overflow resolved while tracing was
* disabled. Otherwise it resolved while tracing was enabled.
*/
if (ev->ip_suppressed) {
/* Tracing is disabled. It doesn't make sense to preserve the
* previous IP. This will just be misleading. Even if tracing
* had been disabled before, as well, we might have missed the
* re-enable in the overflow.
*/
decoder->enabled = 0;
decoder->ip = 0ull;
} else {
/* Tracing is enabled and we're at the IP at which the overflow
* resolved.
*/
decoder->enabled = 1;
decoder->ip = ev->variant.overflow.ip;
}
/* We don't know the TSX state. Let's assume we execute normally.
*
* We also don't know the execution mode. Let's keep what we have
* in case we don't get an update before we have to decode the next
* instruction.
*/
decoder->speculative = 0;
decoder->process_event = 0;
return 0;
}
/* Process an exec mode event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_exec_mode(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
enum pt_exec_mode mode;
if (!decoder || !ev)
return -pte_internal;
/* Use status update events to diagnose inconsistencies. */
mode = ev->variant.exec_mode.mode;
if (ev->status_update && decoder->enabled &&
decoder->mode != ptem_unknown && decoder->mode != mode)
return -pte_bad_status_update;
decoder->mode = mode;
decoder->process_event = 0;
return 0;
}
/* Process a tsx event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_tsx(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
if (!decoder || !ev)
return -pte_internal;
decoder->speculative = ev->variant.tsx.speculative;
decoder->process_event = 0;
return 0;
}
/* Process a stop event.
*
* Returns zero on success, a negative error code otherwise.
*/
static int pt_blk_process_stop(struct pt_block_decoder *decoder,
const struct pt_event *ev)
{
if (!decoder || !ev)
return -pte_internal;
/* This event can't be a status update. */
if (ev->status_update)
return -pte_bad_context;
/* Tracing is always disabled before it is stopped. */
if (decoder->enabled)
return -pte_bad_context;
decoder->process_event = 0;
return 0;
}
int pt_blk_event(struct pt_block_decoder *decoder, struct pt_event *uevent,
size_t size)
{
struct pt_event *ev;
int status;
if (!decoder || !uevent)
return -pte_invalid;
/* We must currently process an event. */
if (!decoder->process_event)
return -pte_bad_query;
ev = &decoder->event;
switch (ev->type) {
case ptev_enabled:
/* Indicate that tracing resumes from the IP at which tracing
* had been disabled before (with some special treatment for
* calls).
*/
if (ev->variant.enabled.ip == decoder->ip)
ev->variant.enabled.resumed = 1;
status = pt_blk_process_enabled(decoder, ev);
if (status < 0)
return status;
break;
case ptev_async_disabled:
if (decoder->ip != ev->variant.async_disabled.at)
return -pte_bad_query;
fallthrough;
case ptev_disabled:
status = pt_blk_process_disabled(decoder, ev);
if (status < 0)
return status;
break;
case ptev_async_branch:
if (decoder->ip != ev->variant.async_branch.from)
return -pte_bad_query;
status = pt_blk_process_async_branch(decoder, ev);
if (status < 0)
return status;
break;
case ptev_async_paging:
if (!ev->ip_suppressed &&
decoder->ip != ev->variant.async_paging.ip)
return -pte_bad_query;
fallthrough;
case ptev_paging:
status = pt_blk_process_paging(decoder, ev);
if (status < 0)
return status;
break;
case ptev_async_vmcs:
if (!ev->ip_suppressed &&
decoder->ip != ev->variant.async_vmcs.ip)
return -pte_bad_query;
fallthrough;
case ptev_vmcs:
status = pt_blk_process_vmcs(decoder, ev);
if (status < 0)
return status;
break;
case ptev_overflow:
status = pt_blk_process_overflow(decoder, ev);
if (status < 0)
return status;
break;
case ptev_exec_mode:
if (!ev->ip_suppressed &&
decoder->ip != ev->variant.exec_mode.ip)
return -pte_bad_query;
status = pt_blk_process_exec_mode(decoder, ev);
if (status < 0)
return status;
break;
case ptev_tsx:
if (!ev->ip_suppressed && decoder->ip != ev->variant.tsx.ip)
return -pte_bad_query;
status = pt_blk_process_tsx(decoder, ev);
if (status < 0)
return status;
break;
case ptev_stop:
status = pt_blk_process_stop(decoder, ev);
if (status < 0)
return status;
break;
case ptev_exstop:
if (!ev->ip_suppressed && decoder->enabled &&
decoder->ip != ev->variant.exstop.ip)
return -pte_bad_query;
decoder->process_event = 0;
break;
case ptev_mwait:
if (!ev->ip_suppressed && decoder->enabled &&
decoder->ip != ev->variant.mwait.ip)
return -pte_bad_query;
decoder->process_event = 0;
break;
case ptev_pwre:
case ptev_pwrx:
case ptev_ptwrite:
case ptev_tick:
case ptev_cbr:
case ptev_mnt:
decoder->process_event = 0;
break;
}
/* Copy the event to the user. Make sure we're not writing beyond the
* memory provided by the user.
*
* We might truncate details of an event but only for those events the
* user can't know about, anyway.
*/
if (sizeof(*ev) < size)
size = sizeof(*ev);
memcpy(uevent, ev, size);
/* Indicate further events. */
return pt_blk_proceed_trailing_event(decoder, NULL);
}
|