18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include <string.h>
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include "intel-pt-decoder/intel-pt-pkt-decoder.h"
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include "debug.h"
88c2ecf20Sopenharmony_ci#include "tests/tests.h"
98c2ecf20Sopenharmony_ci#include "arch-tests.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/**
128c2ecf20Sopenharmony_ci * struct test_data - Test data.
138c2ecf20Sopenharmony_ci * @len: number of bytes to decode
148c2ecf20Sopenharmony_ci * @bytes: bytes to decode
158c2ecf20Sopenharmony_ci * @ctx: packet context to decode
168c2ecf20Sopenharmony_ci * @packet: expected packet
178c2ecf20Sopenharmony_ci * @new_ctx: expected new packet context
188c2ecf20Sopenharmony_ci * @ctx_unchanged: the packet context must not change
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_cistruct test_data {
218c2ecf20Sopenharmony_ci	int len;
228c2ecf20Sopenharmony_ci	u8 bytes[INTEL_PT_PKT_MAX_SZ];
238c2ecf20Sopenharmony_ci	enum intel_pt_pkt_ctx ctx;
248c2ecf20Sopenharmony_ci	struct intel_pt_pkt packet;
258c2ecf20Sopenharmony_ci	enum intel_pt_pkt_ctx new_ctx;
268c2ecf20Sopenharmony_ci	int ctx_unchanged;
278c2ecf20Sopenharmony_ci} data[] = {
288c2ecf20Sopenharmony_ci	/* Padding Packet */
298c2ecf20Sopenharmony_ci	{1, {0}, 0, {INTEL_PT_PAD, 0, 0}, 0, 1 },
308c2ecf20Sopenharmony_ci	/* Short Taken/Not Taken Packet */
318c2ecf20Sopenharmony_ci	{1, {4}, 0, {INTEL_PT_TNT, 1, 0}, 0, 0 },
328c2ecf20Sopenharmony_ci	{1, {6}, 0, {INTEL_PT_TNT, 1, 0x20ULL << 58}, 0, 0 },
338c2ecf20Sopenharmony_ci	{1, {0x80}, 0, {INTEL_PT_TNT, 6, 0}, 0, 0 },
348c2ecf20Sopenharmony_ci	{1, {0xfe}, 0, {INTEL_PT_TNT, 6, 0x3fULL << 58}, 0, 0 },
358c2ecf20Sopenharmony_ci	/* Long Taken/Not Taken Packet */
368c2ecf20Sopenharmony_ci	{8, {0x02, 0xa3, 2}, 0, {INTEL_PT_TNT, 1, 0xa302ULL << 47}, 0, 0 },
378c2ecf20Sopenharmony_ci	{8, {0x02, 0xa3, 3}, 0, {INTEL_PT_TNT, 1, 0x1a302ULL << 47}, 0, 0 },
388c2ecf20Sopenharmony_ci	{8, {0x02, 0xa3, 0, 0, 0, 0, 0, 0x80}, 0, {INTEL_PT_TNT, 47, 0xa302ULL << 1}, 0, 0 },
398c2ecf20Sopenharmony_ci	{8, {0x02, 0xa3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 0, {INTEL_PT_TNT, 47, 0xffffffffffffa302ULL << 1}, 0, 0 },
408c2ecf20Sopenharmony_ci	/* Target IP Packet */
418c2ecf20Sopenharmony_ci	{1, {0x0d}, 0, {INTEL_PT_TIP, 0, 0}, 0, 0 },
428c2ecf20Sopenharmony_ci	{3, {0x2d, 1, 2}, 0, {INTEL_PT_TIP, 1, 0x201}, 0, 0 },
438c2ecf20Sopenharmony_ci	{5, {0x4d, 1, 2, 3, 4}, 0, {INTEL_PT_TIP, 2, 0x4030201}, 0, 0 },
448c2ecf20Sopenharmony_ci	{7, {0x6d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP, 3, 0x60504030201}, 0, 0 },
458c2ecf20Sopenharmony_ci	{7, {0x8d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP, 4, 0x60504030201}, 0, 0 },
468c2ecf20Sopenharmony_ci	{9, {0xcd, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP, 6, 0x807060504030201}, 0, 0 },
478c2ecf20Sopenharmony_ci	/* Packet Generation Enable */
488c2ecf20Sopenharmony_ci	{1, {0x11}, 0, {INTEL_PT_TIP_PGE, 0, 0}, 0, 0 },
498c2ecf20Sopenharmony_ci	{3, {0x31, 1, 2}, 0, {INTEL_PT_TIP_PGE, 1, 0x201}, 0, 0 },
508c2ecf20Sopenharmony_ci	{5, {0x51, 1, 2, 3, 4}, 0, {INTEL_PT_TIP_PGE, 2, 0x4030201}, 0, 0 },
518c2ecf20Sopenharmony_ci	{7, {0x71, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGE, 3, 0x60504030201}, 0, 0 },
528c2ecf20Sopenharmony_ci	{7, {0x91, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGE, 4, 0x60504030201}, 0, 0 },
538c2ecf20Sopenharmony_ci	{9, {0xd1, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP_PGE, 6, 0x807060504030201}, 0, 0 },
548c2ecf20Sopenharmony_ci	/* Packet Generation Disable */
558c2ecf20Sopenharmony_ci	{1, {0x01}, 0, {INTEL_PT_TIP_PGD, 0, 0}, 0, 0 },
568c2ecf20Sopenharmony_ci	{3, {0x21, 1, 2}, 0, {INTEL_PT_TIP_PGD, 1, 0x201}, 0, 0 },
578c2ecf20Sopenharmony_ci	{5, {0x41, 1, 2, 3, 4}, 0, {INTEL_PT_TIP_PGD, 2, 0x4030201}, 0, 0 },
588c2ecf20Sopenharmony_ci	{7, {0x61, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGD, 3, 0x60504030201}, 0, 0 },
598c2ecf20Sopenharmony_ci	{7, {0x81, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGD, 4, 0x60504030201}, 0, 0 },
608c2ecf20Sopenharmony_ci	{9, {0xc1, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP_PGD, 6, 0x807060504030201}, 0, 0 },
618c2ecf20Sopenharmony_ci	/* Flow Update Packet */
628c2ecf20Sopenharmony_ci	{1, {0x1d}, 0, {INTEL_PT_FUP, 0, 0}, 0, 0 },
638c2ecf20Sopenharmony_ci	{3, {0x3d, 1, 2}, 0, {INTEL_PT_FUP, 1, 0x201}, 0, 0 },
648c2ecf20Sopenharmony_ci	{5, {0x5d, 1, 2, 3, 4}, 0, {INTEL_PT_FUP, 2, 0x4030201}, 0, 0 },
658c2ecf20Sopenharmony_ci	{7, {0x7d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_FUP, 3, 0x60504030201}, 0, 0 },
668c2ecf20Sopenharmony_ci	{7, {0x9d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_FUP, 4, 0x60504030201}, 0, 0 },
678c2ecf20Sopenharmony_ci	{9, {0xdd, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_FUP, 6, 0x807060504030201}, 0, 0 },
688c2ecf20Sopenharmony_ci	/* Paging Information Packet */
698c2ecf20Sopenharmony_ci	{8, {0x02, 0x43, 2, 4, 6, 8, 10, 12}, 0, {INTEL_PT_PIP, 0, 0x60504030201}, 0, 0 },
708c2ecf20Sopenharmony_ci	{8, {0x02, 0x43, 3, 4, 6, 8, 10, 12}, 0, {INTEL_PT_PIP, 0, 0x60504030201 | (1ULL << 63)}, 0, 0 },
718c2ecf20Sopenharmony_ci	/* Mode Exec Packet */
728c2ecf20Sopenharmony_ci	{2, {0x99, 0x00}, 0, {INTEL_PT_MODE_EXEC, 0, 16}, 0, 0 },
738c2ecf20Sopenharmony_ci	{2, {0x99, 0x01}, 0, {INTEL_PT_MODE_EXEC, 0, 64}, 0, 0 },
748c2ecf20Sopenharmony_ci	{2, {0x99, 0x02}, 0, {INTEL_PT_MODE_EXEC, 0, 32}, 0, 0 },
758c2ecf20Sopenharmony_ci	/* Mode TSX Packet */
768c2ecf20Sopenharmony_ci	{2, {0x99, 0x20}, 0, {INTEL_PT_MODE_TSX, 0, 0}, 0, 0 },
778c2ecf20Sopenharmony_ci	{2, {0x99, 0x21}, 0, {INTEL_PT_MODE_TSX, 0, 1}, 0, 0 },
788c2ecf20Sopenharmony_ci	{2, {0x99, 0x22}, 0, {INTEL_PT_MODE_TSX, 0, 2}, 0, 0 },
798c2ecf20Sopenharmony_ci	/* Trace Stop Packet */
808c2ecf20Sopenharmony_ci	{2, {0x02, 0x83}, 0, {INTEL_PT_TRACESTOP, 0, 0}, 0, 0 },
818c2ecf20Sopenharmony_ci	/* Core:Bus Ratio Packet */
828c2ecf20Sopenharmony_ci	{4, {0x02, 0x03, 0x12, 0}, 0, {INTEL_PT_CBR, 0, 0x12}, 0, 1 },
838c2ecf20Sopenharmony_ci	/* Timestamp Counter Packet */
848c2ecf20Sopenharmony_ci	{8, {0x19, 1, 2, 3, 4, 5, 6, 7}, 0, {INTEL_PT_TSC, 0, 0x7060504030201}, 0, 1 },
858c2ecf20Sopenharmony_ci	/* Mini Time Counter Packet */
868c2ecf20Sopenharmony_ci	{2, {0x59, 0x12}, 0, {INTEL_PT_MTC, 0, 0x12}, 0, 1 },
878c2ecf20Sopenharmony_ci	/* TSC / MTC Alignment Packet */
888c2ecf20Sopenharmony_ci	{7, {0x02, 0x73}, 0, {INTEL_PT_TMA, 0, 0}, 0, 1 },
898c2ecf20Sopenharmony_ci	{7, {0x02, 0x73, 1, 2}, 0, {INTEL_PT_TMA, 0, 0x201}, 0, 1 },
908c2ecf20Sopenharmony_ci	{7, {0x02, 0x73, 0, 0, 0, 0xff, 1}, 0, {INTEL_PT_TMA, 0x1ff, 0}, 0, 1 },
918c2ecf20Sopenharmony_ci	{7, {0x02, 0x73, 0x80, 0xc0, 0, 0xff, 1}, 0, {INTEL_PT_TMA, 0x1ff, 0xc080}, 0, 1 },
928c2ecf20Sopenharmony_ci	/* Cycle Count Packet */
938c2ecf20Sopenharmony_ci	{1, {0x03}, 0, {INTEL_PT_CYC, 0, 0}, 0, 1 },
948c2ecf20Sopenharmony_ci	{1, {0x0b}, 0, {INTEL_PT_CYC, 0, 1}, 0, 1 },
958c2ecf20Sopenharmony_ci	{1, {0xfb}, 0, {INTEL_PT_CYC, 0, 0x1f}, 0, 1 },
968c2ecf20Sopenharmony_ci	{2, {0x07, 2}, 0, {INTEL_PT_CYC, 0, 0x20}, 0, 1 },
978c2ecf20Sopenharmony_ci	{2, {0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0xfff}, 0, 1 },
988c2ecf20Sopenharmony_ci	{3, {0x07, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x1000}, 0, 1 },
998c2ecf20Sopenharmony_ci	{3, {0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x7ffff}, 0, 1 },
1008c2ecf20Sopenharmony_ci	{4, {0x07, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x80000}, 0, 1 },
1018c2ecf20Sopenharmony_ci	{4, {0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x3ffffff}, 0, 1 },
1028c2ecf20Sopenharmony_ci	{5, {0x07, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x4000000}, 0, 1 },
1038c2ecf20Sopenharmony_ci	{5, {0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x1ffffffff}, 0, 1 },
1048c2ecf20Sopenharmony_ci	{6, {0x07, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x200000000}, 0, 1 },
1058c2ecf20Sopenharmony_ci	{6, {0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0xffffffffff}, 0, 1 },
1068c2ecf20Sopenharmony_ci	{7, {0x07, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x10000000000}, 0, 1 },
1078c2ecf20Sopenharmony_ci	{7, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x7fffffffffff}, 0, 1 },
1088c2ecf20Sopenharmony_ci	{8, {0x07, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x800000000000}, 0, 1 },
1098c2ecf20Sopenharmony_ci	{8, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x3fffffffffffff}, 0, 1 },
1108c2ecf20Sopenharmony_ci	{9, {0x07, 1, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x40000000000000}, 0, 1 },
1118c2ecf20Sopenharmony_ci	{9, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x1fffffffffffffff}, 0, 1 },
1128c2ecf20Sopenharmony_ci	{10, {0x07, 1, 1, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x2000000000000000}, 0, 1 },
1138c2ecf20Sopenharmony_ci	{10, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe}, 0, {INTEL_PT_CYC, 0, 0xffffffffffffffff}, 0, 1 },
1148c2ecf20Sopenharmony_ci	/* Virtual-Machine Control Structure Packet */
1158c2ecf20Sopenharmony_ci	{7, {0x02, 0xc8, 1, 2, 3, 4, 5}, 0, {INTEL_PT_VMCS, 5, 0x504030201}, 0, 0 },
1168c2ecf20Sopenharmony_ci	/* Overflow Packet */
1178c2ecf20Sopenharmony_ci	{2, {0x02, 0xf3}, 0, {INTEL_PT_OVF, 0, 0}, 0, 0 },
1188c2ecf20Sopenharmony_ci	{2, {0x02, 0xf3}, INTEL_PT_BLK_4_CTX, {INTEL_PT_OVF, 0, 0}, 0, 0 },
1198c2ecf20Sopenharmony_ci	{2, {0x02, 0xf3}, INTEL_PT_BLK_8_CTX, {INTEL_PT_OVF, 0, 0}, 0, 0 },
1208c2ecf20Sopenharmony_ci	/* Packet Stream Boundary*/
1218c2ecf20Sopenharmony_ci	{16, {0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82}, 0, {INTEL_PT_PSB, 0, 0}, 0, 0 },
1228c2ecf20Sopenharmony_ci	{16, {0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82}, INTEL_PT_BLK_4_CTX, {INTEL_PT_PSB, 0, 0}, 0, 0 },
1238c2ecf20Sopenharmony_ci	{16, {0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82, 0x02, 0x82}, INTEL_PT_BLK_8_CTX, {INTEL_PT_PSB, 0, 0}, 0, 0 },
1248c2ecf20Sopenharmony_ci	/* PSB End Packet */
1258c2ecf20Sopenharmony_ci	{2, {0x02, 0x23}, 0, {INTEL_PT_PSBEND, 0, 0}, 0, 0 },
1268c2ecf20Sopenharmony_ci	/* Maintenance Packet */
1278c2ecf20Sopenharmony_ci	{11, {0x02, 0xc3, 0x88, 1, 2, 3, 4, 5, 6, 7}, 0, {INTEL_PT_MNT, 0, 0x7060504030201}, 0, 1 },
1288c2ecf20Sopenharmony_ci	/* Write Data to PT Packet */
1298c2ecf20Sopenharmony_ci	{6, {0x02, 0x12, 1, 2, 3, 4}, 0, {INTEL_PT_PTWRITE, 0, 0x4030201}, 0, 0 },
1308c2ecf20Sopenharmony_ci	{10, {0x02, 0x32, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_PTWRITE, 1, 0x807060504030201}, 0, 0 },
1318c2ecf20Sopenharmony_ci	{6, {0x02, 0x92, 1, 2, 3, 4}, 0, {INTEL_PT_PTWRITE_IP, 0, 0x4030201}, 0, 0 },
1328c2ecf20Sopenharmony_ci	{10, {0x02, 0xb2, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_PTWRITE_IP, 1, 0x807060504030201}, 0, 0 },
1338c2ecf20Sopenharmony_ci	/* Execution Stop Packet */
1348c2ecf20Sopenharmony_ci	{2, {0x02, 0x62}, 0, {INTEL_PT_EXSTOP, 0, 0}, 0, 1 },
1358c2ecf20Sopenharmony_ci	{2, {0x02, 0xe2}, 0, {INTEL_PT_EXSTOP_IP, 0, 0}, 0, 1 },
1368c2ecf20Sopenharmony_ci	/* Monitor Wait Packet */
1378c2ecf20Sopenharmony_ci	{10, {0x02, 0xc2}, 0, {INTEL_PT_MWAIT, 0, 0}, 0, 0 },
1388c2ecf20Sopenharmony_ci	{10, {0x02, 0xc2, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_MWAIT, 0, 0x807060504030201}, 0, 0 },
1398c2ecf20Sopenharmony_ci	{10, {0x02, 0xc2, 0xff, 2, 3, 4, 7, 6, 7, 8}, 0, {INTEL_PT_MWAIT, 0, 0x8070607040302ff}, 0, 0 },
1408c2ecf20Sopenharmony_ci	/* Power Entry Packet */
1418c2ecf20Sopenharmony_ci	{4, {0x02, 0x22}, 0, {INTEL_PT_PWRE, 0, 0}, 0, 1 },
1428c2ecf20Sopenharmony_ci	{4, {0x02, 0x22, 1, 2}, 0, {INTEL_PT_PWRE, 0, 0x0201}, 0, 1 },
1438c2ecf20Sopenharmony_ci	{4, {0x02, 0x22, 0x80, 0x34}, 0, {INTEL_PT_PWRE, 0, 0x3480}, 0, 1 },
1448c2ecf20Sopenharmony_ci	{4, {0x02, 0x22, 0x00, 0x56}, 0, {INTEL_PT_PWRE, 0, 0x5600}, 0, 1 },
1458c2ecf20Sopenharmony_ci	/* Power Exit Packet */
1468c2ecf20Sopenharmony_ci	{7, {0x02, 0xa2}, 0, {INTEL_PT_PWRX, 0, 0}, 0, 1 },
1478c2ecf20Sopenharmony_ci	{7, {0x02, 0xa2, 1, 2, 3, 4, 5}, 0, {INTEL_PT_PWRX, 0, 0x504030201}, 0, 1 },
1488c2ecf20Sopenharmony_ci	{7, {0x02, 0xa2, 0xff, 0xff, 0xff, 0xff, 0xff}, 0, {INTEL_PT_PWRX, 0, 0xffffffffff}, 0, 1 },
1498c2ecf20Sopenharmony_ci	/* Block Begin Packet */
1508c2ecf20Sopenharmony_ci	{3, {0x02, 0x63, 0x00}, 0, {INTEL_PT_BBP, 0, 0}, INTEL_PT_BLK_8_CTX, 0 },
1518c2ecf20Sopenharmony_ci	{3, {0x02, 0x63, 0x80}, 0, {INTEL_PT_BBP, 1, 0}, INTEL_PT_BLK_4_CTX, 0 },
1528c2ecf20Sopenharmony_ci	{3, {0x02, 0x63, 0x1f}, 0, {INTEL_PT_BBP, 0, 0x1f}, INTEL_PT_BLK_8_CTX, 0 },
1538c2ecf20Sopenharmony_ci	{3, {0x02, 0x63, 0x9f}, 0, {INTEL_PT_BBP, 1, 0x1f}, INTEL_PT_BLK_4_CTX, 0 },
1548c2ecf20Sopenharmony_ci	/* 4-byte Block Item Packet */
1558c2ecf20Sopenharmony_ci	{5, {0x04}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0, 0}, INTEL_PT_BLK_4_CTX, 0 },
1568c2ecf20Sopenharmony_ci	{5, {0xfc}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0x1f, 0}, INTEL_PT_BLK_4_CTX, 0 },
1578c2ecf20Sopenharmony_ci	{5, {0x04, 1, 2, 3, 4}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0, 0x04030201}, INTEL_PT_BLK_4_CTX, 0 },
1588c2ecf20Sopenharmony_ci	{5, {0xfc, 1, 2, 3, 4}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0x1f, 0x04030201}, INTEL_PT_BLK_4_CTX, 0 },
1598c2ecf20Sopenharmony_ci	/* 8-byte Block Item Packet */
1608c2ecf20Sopenharmony_ci	{9, {0x04}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BIP, 0, 0}, INTEL_PT_BLK_8_CTX, 0 },
1618c2ecf20Sopenharmony_ci	{9, {0xfc}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BIP, 0x1f, 0}, INTEL_PT_BLK_8_CTX, 0 },
1628c2ecf20Sopenharmony_ci	{9, {0x04, 1, 2, 3, 4, 5, 6, 7, 8}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BIP, 0, 0x0807060504030201}, INTEL_PT_BLK_8_CTX, 0 },
1638c2ecf20Sopenharmony_ci	{9, {0xfc, 1, 2, 3, 4, 5, 6, 7, 8}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BIP, 0x1f, 0x0807060504030201}, INTEL_PT_BLK_8_CTX, 0 },
1648c2ecf20Sopenharmony_ci	/* Block End Packet */
1658c2ecf20Sopenharmony_ci	{2, {0x02, 0x33}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BEP, 0, 0}, 0, 0 },
1668c2ecf20Sopenharmony_ci	{2, {0x02, 0xb3}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BEP_IP, 0, 0}, 0, 0 },
1678c2ecf20Sopenharmony_ci	{2, {0x02, 0x33}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BEP, 0, 0}, 0, 0 },
1688c2ecf20Sopenharmony_ci	{2, {0x02, 0xb3}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BEP_IP, 0, 0}, 0, 0 },
1698c2ecf20Sopenharmony_ci	/* Terminator */
1708c2ecf20Sopenharmony_ci	{0, {0}, 0, {0, 0, 0}, 0, 0 },
1718c2ecf20Sopenharmony_ci};
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic int dump_packet(struct intel_pt_pkt *packet, u8 *bytes, int len)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	char desc[INTEL_PT_PKT_DESC_MAX];
1768c2ecf20Sopenharmony_ci	int ret, i;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++)
1798c2ecf20Sopenharmony_ci		pr_debug(" %02x", bytes[i]);
1808c2ecf20Sopenharmony_ci	for (; i < INTEL_PT_PKT_MAX_SZ; i++)
1818c2ecf20Sopenharmony_ci		pr_debug("   ");
1828c2ecf20Sopenharmony_ci	pr_debug("   ");
1838c2ecf20Sopenharmony_ci	ret = intel_pt_pkt_desc(packet, desc, INTEL_PT_PKT_DESC_MAX);
1848c2ecf20Sopenharmony_ci	if (ret < 0) {
1858c2ecf20Sopenharmony_ci		pr_debug("intel_pt_pkt_desc failed!\n");
1868c2ecf20Sopenharmony_ci		return TEST_FAIL;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	pr_debug("%s\n", desc);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	return TEST_OK;
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic void decoding_failed(struct test_data *d)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	pr_debug("Decoding failed!\n");
1968c2ecf20Sopenharmony_ci	pr_debug("Decoding:  ");
1978c2ecf20Sopenharmony_ci	dump_packet(&d->packet, d->bytes, d->len);
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int fail(struct test_data *d, struct intel_pt_pkt *packet, int len,
2018c2ecf20Sopenharmony_ci		enum intel_pt_pkt_ctx new_ctx)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	decoding_failed(d);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (len != d->len)
2068c2ecf20Sopenharmony_ci		pr_debug("Expected length: %d   Decoded length %d\n",
2078c2ecf20Sopenharmony_ci			 d->len, len);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	if (packet->type != d->packet.type)
2108c2ecf20Sopenharmony_ci		pr_debug("Expected type: %d   Decoded type %d\n",
2118c2ecf20Sopenharmony_ci			 d->packet.type, packet->type);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (packet->count != d->packet.count)
2148c2ecf20Sopenharmony_ci		pr_debug("Expected count: %d   Decoded count %d\n",
2158c2ecf20Sopenharmony_ci			 d->packet.count, packet->count);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	if (packet->payload != d->packet.payload)
2188c2ecf20Sopenharmony_ci		pr_debug("Expected payload: 0x%llx   Decoded payload 0x%llx\n",
2198c2ecf20Sopenharmony_ci			 (unsigned long long)d->packet.payload,
2208c2ecf20Sopenharmony_ci			 (unsigned long long)packet->payload);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	if (new_ctx != d->new_ctx)
2238c2ecf20Sopenharmony_ci		pr_debug("Expected packet context: %d   Decoded packet context %d\n",
2248c2ecf20Sopenharmony_ci			 d->new_ctx, new_ctx);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	return TEST_FAIL;
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic int test_ctx_unchanged(struct test_data *d, struct intel_pt_pkt *packet,
2308c2ecf20Sopenharmony_ci			      enum intel_pt_pkt_ctx ctx)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	enum intel_pt_pkt_ctx old_ctx = ctx;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	intel_pt_upd_pkt_ctx(packet, &ctx);
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	if (ctx != old_ctx) {
2378c2ecf20Sopenharmony_ci		decoding_failed(d);
2388c2ecf20Sopenharmony_ci		pr_debug("Packet context changed!\n");
2398c2ecf20Sopenharmony_ci		return TEST_FAIL;
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	return TEST_OK;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic int test_one(struct test_data *d)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct intel_pt_pkt packet;
2488c2ecf20Sopenharmony_ci	enum intel_pt_pkt_ctx ctx = d->ctx;
2498c2ecf20Sopenharmony_ci	int ret;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	memset(&packet, 0xff, sizeof(packet));
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/* Decode a packet */
2548c2ecf20Sopenharmony_ci	ret = intel_pt_get_packet(d->bytes, d->len, &packet, &ctx);
2558c2ecf20Sopenharmony_ci	if (ret < 0 || ret > INTEL_PT_PKT_MAX_SZ) {
2568c2ecf20Sopenharmony_ci		decoding_failed(d);
2578c2ecf20Sopenharmony_ci		pr_debug("intel_pt_get_packet returned %d\n", ret);
2588c2ecf20Sopenharmony_ci		return TEST_FAIL;
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* Some packets must always leave the packet context unchanged */
2628c2ecf20Sopenharmony_ci	if (d->ctx_unchanged) {
2638c2ecf20Sopenharmony_ci		int err;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci		err = test_ctx_unchanged(d, &packet, INTEL_PT_NO_CTX);
2668c2ecf20Sopenharmony_ci		if (err)
2678c2ecf20Sopenharmony_ci			return err;
2688c2ecf20Sopenharmony_ci		err = test_ctx_unchanged(d, &packet, INTEL_PT_BLK_4_CTX);
2698c2ecf20Sopenharmony_ci		if (err)
2708c2ecf20Sopenharmony_ci			return err;
2718c2ecf20Sopenharmony_ci		err = test_ctx_unchanged(d, &packet, INTEL_PT_BLK_8_CTX);
2728c2ecf20Sopenharmony_ci		if (err)
2738c2ecf20Sopenharmony_ci			return err;
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	/* Compare to the expected values */
2778c2ecf20Sopenharmony_ci	if (ret != d->len || packet.type != d->packet.type ||
2788c2ecf20Sopenharmony_ci	    packet.count != d->packet.count ||
2798c2ecf20Sopenharmony_ci	    packet.payload != d->packet.payload || ctx != d->new_ctx)
2808c2ecf20Sopenharmony_ci		return fail(d, &packet, ret, ctx);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	pr_debug("Decoded ok:");
2838c2ecf20Sopenharmony_ci	ret = dump_packet(&d->packet, d->bytes, d->len);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	return ret;
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci/*
2898c2ecf20Sopenharmony_ci * This test feeds byte sequences to the Intel PT packet decoder and checks the
2908c2ecf20Sopenharmony_ci * results. Changes to the packet context are also checked.
2918c2ecf20Sopenharmony_ci */
2928c2ecf20Sopenharmony_ciint test__intel_pt_pkt_decoder(struct test *test __maybe_unused, int subtest __maybe_unused)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct test_data *d = data;
2958c2ecf20Sopenharmony_ci	int ret;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	for (d = data; d->len; d++) {
2988c2ecf20Sopenharmony_ci		ret = test_one(d);
2998c2ecf20Sopenharmony_ci		if (ret)
3008c2ecf20Sopenharmony_ci			return ret;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	return TEST_OK;
3048c2ecf20Sopenharmony_ci}
305