xref: /OK3568_Linux_fs/kernel/tools/perf/arch/x86/tests/intel-pt-pkt-decoder-test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #include <string.h>
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include "intel-pt-decoder/intel-pt-pkt-decoder.h"
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include "debug.h"
8*4882a593Smuzhiyun #include "tests/tests.h"
9*4882a593Smuzhiyun #include "arch-tests.h"
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /**
12*4882a593Smuzhiyun  * struct test_data - Test data.
13*4882a593Smuzhiyun  * @len: number of bytes to decode
14*4882a593Smuzhiyun  * @bytes: bytes to decode
15*4882a593Smuzhiyun  * @ctx: packet context to decode
16*4882a593Smuzhiyun  * @packet: expected packet
17*4882a593Smuzhiyun  * @new_ctx: expected new packet context
18*4882a593Smuzhiyun  * @ctx_unchanged: the packet context must not change
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun struct test_data {
21*4882a593Smuzhiyun 	int len;
22*4882a593Smuzhiyun 	u8 bytes[INTEL_PT_PKT_MAX_SZ];
23*4882a593Smuzhiyun 	enum intel_pt_pkt_ctx ctx;
24*4882a593Smuzhiyun 	struct intel_pt_pkt packet;
25*4882a593Smuzhiyun 	enum intel_pt_pkt_ctx new_ctx;
26*4882a593Smuzhiyun 	int ctx_unchanged;
27*4882a593Smuzhiyun } data[] = {
28*4882a593Smuzhiyun 	/* Padding Packet */
29*4882a593Smuzhiyun 	{1, {0}, 0, {INTEL_PT_PAD, 0, 0}, 0, 1 },
30*4882a593Smuzhiyun 	/* Short Taken/Not Taken Packet */
31*4882a593Smuzhiyun 	{1, {4}, 0, {INTEL_PT_TNT, 1, 0}, 0, 0 },
32*4882a593Smuzhiyun 	{1, {6}, 0, {INTEL_PT_TNT, 1, 0x20ULL << 58}, 0, 0 },
33*4882a593Smuzhiyun 	{1, {0x80}, 0, {INTEL_PT_TNT, 6, 0}, 0, 0 },
34*4882a593Smuzhiyun 	{1, {0xfe}, 0, {INTEL_PT_TNT, 6, 0x3fULL << 58}, 0, 0 },
35*4882a593Smuzhiyun 	/* Long Taken/Not Taken Packet */
36*4882a593Smuzhiyun 	{8, {0x02, 0xa3, 2}, 0, {INTEL_PT_TNT, 1, 0xa302ULL << 47}, 0, 0 },
37*4882a593Smuzhiyun 	{8, {0x02, 0xa3, 3}, 0, {INTEL_PT_TNT, 1, 0x1a302ULL << 47}, 0, 0 },
38*4882a593Smuzhiyun 	{8, {0x02, 0xa3, 0, 0, 0, 0, 0, 0x80}, 0, {INTEL_PT_TNT, 47, 0xa302ULL << 1}, 0, 0 },
39*4882a593Smuzhiyun 	{8, {0x02, 0xa3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 0, {INTEL_PT_TNT, 47, 0xffffffffffffa302ULL << 1}, 0, 0 },
40*4882a593Smuzhiyun 	/* Target IP Packet */
41*4882a593Smuzhiyun 	{1, {0x0d}, 0, {INTEL_PT_TIP, 0, 0}, 0, 0 },
42*4882a593Smuzhiyun 	{3, {0x2d, 1, 2}, 0, {INTEL_PT_TIP, 1, 0x201}, 0, 0 },
43*4882a593Smuzhiyun 	{5, {0x4d, 1, 2, 3, 4}, 0, {INTEL_PT_TIP, 2, 0x4030201}, 0, 0 },
44*4882a593Smuzhiyun 	{7, {0x6d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP, 3, 0x60504030201}, 0, 0 },
45*4882a593Smuzhiyun 	{7, {0x8d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP, 4, 0x60504030201}, 0, 0 },
46*4882a593Smuzhiyun 	{9, {0xcd, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP, 6, 0x807060504030201}, 0, 0 },
47*4882a593Smuzhiyun 	/* Packet Generation Enable */
48*4882a593Smuzhiyun 	{1, {0x11}, 0, {INTEL_PT_TIP_PGE, 0, 0}, 0, 0 },
49*4882a593Smuzhiyun 	{3, {0x31, 1, 2}, 0, {INTEL_PT_TIP_PGE, 1, 0x201}, 0, 0 },
50*4882a593Smuzhiyun 	{5, {0x51, 1, 2, 3, 4}, 0, {INTEL_PT_TIP_PGE, 2, 0x4030201}, 0, 0 },
51*4882a593Smuzhiyun 	{7, {0x71, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGE, 3, 0x60504030201}, 0, 0 },
52*4882a593Smuzhiyun 	{7, {0x91, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGE, 4, 0x60504030201}, 0, 0 },
53*4882a593Smuzhiyun 	{9, {0xd1, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP_PGE, 6, 0x807060504030201}, 0, 0 },
54*4882a593Smuzhiyun 	/* Packet Generation Disable */
55*4882a593Smuzhiyun 	{1, {0x01}, 0, {INTEL_PT_TIP_PGD, 0, 0}, 0, 0 },
56*4882a593Smuzhiyun 	{3, {0x21, 1, 2}, 0, {INTEL_PT_TIP_PGD, 1, 0x201}, 0, 0 },
57*4882a593Smuzhiyun 	{5, {0x41, 1, 2, 3, 4}, 0, {INTEL_PT_TIP_PGD, 2, 0x4030201}, 0, 0 },
58*4882a593Smuzhiyun 	{7, {0x61, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGD, 3, 0x60504030201}, 0, 0 },
59*4882a593Smuzhiyun 	{7, {0x81, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGD, 4, 0x60504030201}, 0, 0 },
60*4882a593Smuzhiyun 	{9, {0xc1, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP_PGD, 6, 0x807060504030201}, 0, 0 },
61*4882a593Smuzhiyun 	/* Flow Update Packet */
62*4882a593Smuzhiyun 	{1, {0x1d}, 0, {INTEL_PT_FUP, 0, 0}, 0, 0 },
63*4882a593Smuzhiyun 	{3, {0x3d, 1, 2}, 0, {INTEL_PT_FUP, 1, 0x201}, 0, 0 },
64*4882a593Smuzhiyun 	{5, {0x5d, 1, 2, 3, 4}, 0, {INTEL_PT_FUP, 2, 0x4030201}, 0, 0 },
65*4882a593Smuzhiyun 	{7, {0x7d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_FUP, 3, 0x60504030201}, 0, 0 },
66*4882a593Smuzhiyun 	{7, {0x9d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_FUP, 4, 0x60504030201}, 0, 0 },
67*4882a593Smuzhiyun 	{9, {0xdd, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_FUP, 6, 0x807060504030201}, 0, 0 },
68*4882a593Smuzhiyun 	/* Paging Information Packet */
69*4882a593Smuzhiyun 	{8, {0x02, 0x43, 2, 4, 6, 8, 10, 12}, 0, {INTEL_PT_PIP, 0, 0x60504030201}, 0, 0 },
70*4882a593Smuzhiyun 	{8, {0x02, 0x43, 3, 4, 6, 8, 10, 12}, 0, {INTEL_PT_PIP, 0, 0x60504030201 | (1ULL << 63)}, 0, 0 },
71*4882a593Smuzhiyun 	/* Mode Exec Packet */
72*4882a593Smuzhiyun 	{2, {0x99, 0x00}, 0, {INTEL_PT_MODE_EXEC, 0, 16}, 0, 0 },
73*4882a593Smuzhiyun 	{2, {0x99, 0x01}, 0, {INTEL_PT_MODE_EXEC, 0, 64}, 0, 0 },
74*4882a593Smuzhiyun 	{2, {0x99, 0x02}, 0, {INTEL_PT_MODE_EXEC, 0, 32}, 0, 0 },
75*4882a593Smuzhiyun 	/* Mode TSX Packet */
76*4882a593Smuzhiyun 	{2, {0x99, 0x20}, 0, {INTEL_PT_MODE_TSX, 0, 0}, 0, 0 },
77*4882a593Smuzhiyun 	{2, {0x99, 0x21}, 0, {INTEL_PT_MODE_TSX, 0, 1}, 0, 0 },
78*4882a593Smuzhiyun 	{2, {0x99, 0x22}, 0, {INTEL_PT_MODE_TSX, 0, 2}, 0, 0 },
79*4882a593Smuzhiyun 	/* Trace Stop Packet */
80*4882a593Smuzhiyun 	{2, {0x02, 0x83}, 0, {INTEL_PT_TRACESTOP, 0, 0}, 0, 0 },
81*4882a593Smuzhiyun 	/* Core:Bus Ratio Packet */
82*4882a593Smuzhiyun 	{4, {0x02, 0x03, 0x12, 0}, 0, {INTEL_PT_CBR, 0, 0x12}, 0, 1 },
83*4882a593Smuzhiyun 	/* Timestamp Counter Packet */
84*4882a593Smuzhiyun 	{8, {0x19, 1, 2, 3, 4, 5, 6, 7}, 0, {INTEL_PT_TSC, 0, 0x7060504030201}, 0, 1 },
85*4882a593Smuzhiyun 	/* Mini Time Counter Packet */
86*4882a593Smuzhiyun 	{2, {0x59, 0x12}, 0, {INTEL_PT_MTC, 0, 0x12}, 0, 1 },
87*4882a593Smuzhiyun 	/* TSC / MTC Alignment Packet */
88*4882a593Smuzhiyun 	{7, {0x02, 0x73}, 0, {INTEL_PT_TMA, 0, 0}, 0, 1 },
89*4882a593Smuzhiyun 	{7, {0x02, 0x73, 1, 2}, 0, {INTEL_PT_TMA, 0, 0x201}, 0, 1 },
90*4882a593Smuzhiyun 	{7, {0x02, 0x73, 0, 0, 0, 0xff, 1}, 0, {INTEL_PT_TMA, 0x1ff, 0}, 0, 1 },
91*4882a593Smuzhiyun 	{7, {0x02, 0x73, 0x80, 0xc0, 0, 0xff, 1}, 0, {INTEL_PT_TMA, 0x1ff, 0xc080}, 0, 1 },
92*4882a593Smuzhiyun 	/* Cycle Count Packet */
93*4882a593Smuzhiyun 	{1, {0x03}, 0, {INTEL_PT_CYC, 0, 0}, 0, 1 },
94*4882a593Smuzhiyun 	{1, {0x0b}, 0, {INTEL_PT_CYC, 0, 1}, 0, 1 },
95*4882a593Smuzhiyun 	{1, {0xfb}, 0, {INTEL_PT_CYC, 0, 0x1f}, 0, 1 },
96*4882a593Smuzhiyun 	{2, {0x07, 2}, 0, {INTEL_PT_CYC, 0, 0x20}, 0, 1 },
97*4882a593Smuzhiyun 	{2, {0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0xfff}, 0, 1 },
98*4882a593Smuzhiyun 	{3, {0x07, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x1000}, 0, 1 },
99*4882a593Smuzhiyun 	{3, {0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x7ffff}, 0, 1 },
100*4882a593Smuzhiyun 	{4, {0x07, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x80000}, 0, 1 },
101*4882a593Smuzhiyun 	{4, {0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x3ffffff}, 0, 1 },
102*4882a593Smuzhiyun 	{5, {0x07, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x4000000}, 0, 1 },
103*4882a593Smuzhiyun 	{5, {0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x1ffffffff}, 0, 1 },
104*4882a593Smuzhiyun 	{6, {0x07, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x200000000}, 0, 1 },
105*4882a593Smuzhiyun 	{6, {0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0xffffffffff}, 0, 1 },
106*4882a593Smuzhiyun 	{7, {0x07, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x10000000000}, 0, 1 },
107*4882a593Smuzhiyun 	{7, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x7fffffffffff}, 0, 1 },
108*4882a593Smuzhiyun 	{8, {0x07, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x800000000000}, 0, 1 },
109*4882a593Smuzhiyun 	{8, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x3fffffffffffff}, 0, 1 },
110*4882a593Smuzhiyun 	{9, {0x07, 1, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x40000000000000}, 0, 1 },
111*4882a593Smuzhiyun 	{9, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x1fffffffffffffff}, 0, 1 },
112*4882a593Smuzhiyun 	{10, {0x07, 1, 1, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x2000000000000000}, 0, 1 },
113*4882a593Smuzhiyun 	{10, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe}, 0, {INTEL_PT_CYC, 0, 0xffffffffffffffff}, 0, 1 },
114*4882a593Smuzhiyun 	/* Virtual-Machine Control Structure Packet */
115*4882a593Smuzhiyun 	{7, {0x02, 0xc8, 1, 2, 3, 4, 5}, 0, {INTEL_PT_VMCS, 5, 0x504030201}, 0, 0 },
116*4882a593Smuzhiyun 	/* Overflow Packet */
117*4882a593Smuzhiyun 	{2, {0x02, 0xf3}, 0, {INTEL_PT_OVF, 0, 0}, 0, 0 },
118*4882a593Smuzhiyun 	{2, {0x02, 0xf3}, INTEL_PT_BLK_4_CTX, {INTEL_PT_OVF, 0, 0}, 0, 0 },
119*4882a593Smuzhiyun 	{2, {0x02, 0xf3}, INTEL_PT_BLK_8_CTX, {INTEL_PT_OVF, 0, 0}, 0, 0 },
120*4882a593Smuzhiyun 	/* Packet Stream Boundary*/
121*4882a593Smuzhiyun 	{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 },
122*4882a593Smuzhiyun 	{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 },
123*4882a593Smuzhiyun 	{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 },
124*4882a593Smuzhiyun 	/* PSB End Packet */
125*4882a593Smuzhiyun 	{2, {0x02, 0x23}, 0, {INTEL_PT_PSBEND, 0, 0}, 0, 0 },
126*4882a593Smuzhiyun 	/* Maintenance Packet */
127*4882a593Smuzhiyun 	{11, {0x02, 0xc3, 0x88, 1, 2, 3, 4, 5, 6, 7}, 0, {INTEL_PT_MNT, 0, 0x7060504030201}, 0, 1 },
128*4882a593Smuzhiyun 	/* Write Data to PT Packet */
129*4882a593Smuzhiyun 	{6, {0x02, 0x12, 1, 2, 3, 4}, 0, {INTEL_PT_PTWRITE, 0, 0x4030201}, 0, 0 },
130*4882a593Smuzhiyun 	{10, {0x02, 0x32, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_PTWRITE, 1, 0x807060504030201}, 0, 0 },
131*4882a593Smuzhiyun 	{6, {0x02, 0x92, 1, 2, 3, 4}, 0, {INTEL_PT_PTWRITE_IP, 0, 0x4030201}, 0, 0 },
132*4882a593Smuzhiyun 	{10, {0x02, 0xb2, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_PTWRITE_IP, 1, 0x807060504030201}, 0, 0 },
133*4882a593Smuzhiyun 	/* Execution Stop Packet */
134*4882a593Smuzhiyun 	{2, {0x02, 0x62}, 0, {INTEL_PT_EXSTOP, 0, 0}, 0, 1 },
135*4882a593Smuzhiyun 	{2, {0x02, 0xe2}, 0, {INTEL_PT_EXSTOP_IP, 0, 0}, 0, 1 },
136*4882a593Smuzhiyun 	/* Monitor Wait Packet */
137*4882a593Smuzhiyun 	{10, {0x02, 0xc2}, 0, {INTEL_PT_MWAIT, 0, 0}, 0, 0 },
138*4882a593Smuzhiyun 	{10, {0x02, 0xc2, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_MWAIT, 0, 0x807060504030201}, 0, 0 },
139*4882a593Smuzhiyun 	{10, {0x02, 0xc2, 0xff, 2, 3, 4, 7, 6, 7, 8}, 0, {INTEL_PT_MWAIT, 0, 0x8070607040302ff}, 0, 0 },
140*4882a593Smuzhiyun 	/* Power Entry Packet */
141*4882a593Smuzhiyun 	{4, {0x02, 0x22}, 0, {INTEL_PT_PWRE, 0, 0}, 0, 1 },
142*4882a593Smuzhiyun 	{4, {0x02, 0x22, 1, 2}, 0, {INTEL_PT_PWRE, 0, 0x0201}, 0, 1 },
143*4882a593Smuzhiyun 	{4, {0x02, 0x22, 0x80, 0x34}, 0, {INTEL_PT_PWRE, 0, 0x3480}, 0, 1 },
144*4882a593Smuzhiyun 	{4, {0x02, 0x22, 0x00, 0x56}, 0, {INTEL_PT_PWRE, 0, 0x5600}, 0, 1 },
145*4882a593Smuzhiyun 	/* Power Exit Packet */
146*4882a593Smuzhiyun 	{7, {0x02, 0xa2}, 0, {INTEL_PT_PWRX, 0, 0}, 0, 1 },
147*4882a593Smuzhiyun 	{7, {0x02, 0xa2, 1, 2, 3, 4, 5}, 0, {INTEL_PT_PWRX, 0, 0x504030201}, 0, 1 },
148*4882a593Smuzhiyun 	{7, {0x02, 0xa2, 0xff, 0xff, 0xff, 0xff, 0xff}, 0, {INTEL_PT_PWRX, 0, 0xffffffffff}, 0, 1 },
149*4882a593Smuzhiyun 	/* Block Begin Packet */
150*4882a593Smuzhiyun 	{3, {0x02, 0x63, 0x00}, 0, {INTEL_PT_BBP, 0, 0}, INTEL_PT_BLK_8_CTX, 0 },
151*4882a593Smuzhiyun 	{3, {0x02, 0x63, 0x80}, 0, {INTEL_PT_BBP, 1, 0}, INTEL_PT_BLK_4_CTX, 0 },
152*4882a593Smuzhiyun 	{3, {0x02, 0x63, 0x1f}, 0, {INTEL_PT_BBP, 0, 0x1f}, INTEL_PT_BLK_8_CTX, 0 },
153*4882a593Smuzhiyun 	{3, {0x02, 0x63, 0x9f}, 0, {INTEL_PT_BBP, 1, 0x1f}, INTEL_PT_BLK_4_CTX, 0 },
154*4882a593Smuzhiyun 	/* 4-byte Block Item Packet */
155*4882a593Smuzhiyun 	{5, {0x04}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0, 0}, INTEL_PT_BLK_4_CTX, 0 },
156*4882a593Smuzhiyun 	{5, {0xfc}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0x1f, 0}, INTEL_PT_BLK_4_CTX, 0 },
157*4882a593Smuzhiyun 	{5, {0x04, 1, 2, 3, 4}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0, 0x04030201}, INTEL_PT_BLK_4_CTX, 0 },
158*4882a593Smuzhiyun 	{5, {0xfc, 1, 2, 3, 4}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0x1f, 0x04030201}, INTEL_PT_BLK_4_CTX, 0 },
159*4882a593Smuzhiyun 	/* 8-byte Block Item Packet */
160*4882a593Smuzhiyun 	{9, {0x04}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BIP, 0, 0}, INTEL_PT_BLK_8_CTX, 0 },
161*4882a593Smuzhiyun 	{9, {0xfc}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BIP, 0x1f, 0}, INTEL_PT_BLK_8_CTX, 0 },
162*4882a593Smuzhiyun 	{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 },
163*4882a593Smuzhiyun 	{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 },
164*4882a593Smuzhiyun 	/* Block End Packet */
165*4882a593Smuzhiyun 	{2, {0x02, 0x33}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BEP, 0, 0}, 0, 0 },
166*4882a593Smuzhiyun 	{2, {0x02, 0xb3}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BEP_IP, 0, 0}, 0, 0 },
167*4882a593Smuzhiyun 	{2, {0x02, 0x33}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BEP, 0, 0}, 0, 0 },
168*4882a593Smuzhiyun 	{2, {0x02, 0xb3}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BEP_IP, 0, 0}, 0, 0 },
169*4882a593Smuzhiyun 	/* Terminator */
170*4882a593Smuzhiyun 	{0, {0}, 0, {0, 0, 0}, 0, 0 },
171*4882a593Smuzhiyun };
172*4882a593Smuzhiyun 
dump_packet(struct intel_pt_pkt * packet,u8 * bytes,int len)173*4882a593Smuzhiyun static int dump_packet(struct intel_pt_pkt *packet, u8 *bytes, int len)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	char desc[INTEL_PT_PKT_DESC_MAX];
176*4882a593Smuzhiyun 	int ret, i;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	for (i = 0; i < len; i++)
179*4882a593Smuzhiyun 		pr_debug(" %02x", bytes[i]);
180*4882a593Smuzhiyun 	for (; i < INTEL_PT_PKT_MAX_SZ; i++)
181*4882a593Smuzhiyun 		pr_debug("   ");
182*4882a593Smuzhiyun 	pr_debug("   ");
183*4882a593Smuzhiyun 	ret = intel_pt_pkt_desc(packet, desc, INTEL_PT_PKT_DESC_MAX);
184*4882a593Smuzhiyun 	if (ret < 0) {
185*4882a593Smuzhiyun 		pr_debug("intel_pt_pkt_desc failed!\n");
186*4882a593Smuzhiyun 		return TEST_FAIL;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 	pr_debug("%s\n", desc);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	return TEST_OK;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
decoding_failed(struct test_data * d)193*4882a593Smuzhiyun static void decoding_failed(struct test_data *d)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	pr_debug("Decoding failed!\n");
196*4882a593Smuzhiyun 	pr_debug("Decoding:  ");
197*4882a593Smuzhiyun 	dump_packet(&d->packet, d->bytes, d->len);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
fail(struct test_data * d,struct intel_pt_pkt * packet,int len,enum intel_pt_pkt_ctx new_ctx)200*4882a593Smuzhiyun static int fail(struct test_data *d, struct intel_pt_pkt *packet, int len,
201*4882a593Smuzhiyun 		enum intel_pt_pkt_ctx new_ctx)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	decoding_failed(d);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	if (len != d->len)
206*4882a593Smuzhiyun 		pr_debug("Expected length: %d   Decoded length %d\n",
207*4882a593Smuzhiyun 			 d->len, len);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	if (packet->type != d->packet.type)
210*4882a593Smuzhiyun 		pr_debug("Expected type: %d   Decoded type %d\n",
211*4882a593Smuzhiyun 			 d->packet.type, packet->type);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	if (packet->count != d->packet.count)
214*4882a593Smuzhiyun 		pr_debug("Expected count: %d   Decoded count %d\n",
215*4882a593Smuzhiyun 			 d->packet.count, packet->count);
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if (packet->payload != d->packet.payload)
218*4882a593Smuzhiyun 		pr_debug("Expected payload: 0x%llx   Decoded payload 0x%llx\n",
219*4882a593Smuzhiyun 			 (unsigned long long)d->packet.payload,
220*4882a593Smuzhiyun 			 (unsigned long long)packet->payload);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	if (new_ctx != d->new_ctx)
223*4882a593Smuzhiyun 		pr_debug("Expected packet context: %d   Decoded packet context %d\n",
224*4882a593Smuzhiyun 			 d->new_ctx, new_ctx);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	return TEST_FAIL;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
test_ctx_unchanged(struct test_data * d,struct intel_pt_pkt * packet,enum intel_pt_pkt_ctx ctx)229*4882a593Smuzhiyun static int test_ctx_unchanged(struct test_data *d, struct intel_pt_pkt *packet,
230*4882a593Smuzhiyun 			      enum intel_pt_pkt_ctx ctx)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	enum intel_pt_pkt_ctx old_ctx = ctx;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	intel_pt_upd_pkt_ctx(packet, &ctx);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (ctx != old_ctx) {
237*4882a593Smuzhiyun 		decoding_failed(d);
238*4882a593Smuzhiyun 		pr_debug("Packet context changed!\n");
239*4882a593Smuzhiyun 		return TEST_FAIL;
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	return TEST_OK;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
test_one(struct test_data * d)245*4882a593Smuzhiyun static int test_one(struct test_data *d)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	struct intel_pt_pkt packet;
248*4882a593Smuzhiyun 	enum intel_pt_pkt_ctx ctx = d->ctx;
249*4882a593Smuzhiyun 	int ret;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	memset(&packet, 0xff, sizeof(packet));
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	/* Decode a packet */
254*4882a593Smuzhiyun 	ret = intel_pt_get_packet(d->bytes, d->len, &packet, &ctx);
255*4882a593Smuzhiyun 	if (ret < 0 || ret > INTEL_PT_PKT_MAX_SZ) {
256*4882a593Smuzhiyun 		decoding_failed(d);
257*4882a593Smuzhiyun 		pr_debug("intel_pt_get_packet returned %d\n", ret);
258*4882a593Smuzhiyun 		return TEST_FAIL;
259*4882a593Smuzhiyun 	}
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/* Some packets must always leave the packet context unchanged */
262*4882a593Smuzhiyun 	if (d->ctx_unchanged) {
263*4882a593Smuzhiyun 		int err;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 		err = test_ctx_unchanged(d, &packet, INTEL_PT_NO_CTX);
266*4882a593Smuzhiyun 		if (err)
267*4882a593Smuzhiyun 			return err;
268*4882a593Smuzhiyun 		err = test_ctx_unchanged(d, &packet, INTEL_PT_BLK_4_CTX);
269*4882a593Smuzhiyun 		if (err)
270*4882a593Smuzhiyun 			return err;
271*4882a593Smuzhiyun 		err = test_ctx_unchanged(d, &packet, INTEL_PT_BLK_8_CTX);
272*4882a593Smuzhiyun 		if (err)
273*4882a593Smuzhiyun 			return err;
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	/* Compare to the expected values */
277*4882a593Smuzhiyun 	if (ret != d->len || packet.type != d->packet.type ||
278*4882a593Smuzhiyun 	    packet.count != d->packet.count ||
279*4882a593Smuzhiyun 	    packet.payload != d->packet.payload || ctx != d->new_ctx)
280*4882a593Smuzhiyun 		return fail(d, &packet, ret, ctx);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	pr_debug("Decoded ok:");
283*4882a593Smuzhiyun 	ret = dump_packet(&d->packet, d->bytes, d->len);
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	return ret;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /*
289*4882a593Smuzhiyun  * This test feeds byte sequences to the Intel PT packet decoder and checks the
290*4882a593Smuzhiyun  * results. Changes to the packet context are also checked.
291*4882a593Smuzhiyun  */
test__intel_pt_pkt_decoder(struct test * test __maybe_unused,int subtest __maybe_unused)292*4882a593Smuzhiyun int test__intel_pt_pkt_decoder(struct test *test __maybe_unused, int subtest __maybe_unused)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	struct test_data *d = data;
295*4882a593Smuzhiyun 	int ret;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	for (d = data; d->len; d++) {
298*4882a593Smuzhiyun 		ret = test_one(d);
299*4882a593Smuzhiyun 		if (ret)
300*4882a593Smuzhiyun 			return ret;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	return TEST_OK;
304*4882a593Smuzhiyun }
305