Path: blob/master/tools/perf/arch/x86/tests/intel-pt-test.c
26288 views
// SPDX-License-Identifier: GPL-2.012#include <linux/compiler.h>3#include <linux/bits.h>4#include <string.h>5#include <cpuid.h>6#include <sched.h>78#include "intel-pt-decoder/intel-pt-pkt-decoder.h"910#include "debug.h"11#include "tests/tests.h"12#include "arch-tests.h"13#include "cpumap.h"1415/**16* struct test_data - Test data.17* @len: number of bytes to decode18* @bytes: bytes to decode19* @ctx: packet context to decode20* @packet: expected packet21* @new_ctx: expected new packet context22* @ctx_unchanged: the packet context must not change23*/24static const struct test_data {25int len;26u8 bytes[INTEL_PT_PKT_MAX_SZ];27enum intel_pt_pkt_ctx ctx;28struct intel_pt_pkt packet;29enum intel_pt_pkt_ctx new_ctx;30int ctx_unchanged;31} data[] = {32/* Padding Packet */33{1, {0}, 0, {INTEL_PT_PAD, 0, 0}, 0, 1 },34/* Short Taken/Not Taken Packet */35{1, {4}, 0, {INTEL_PT_TNT, 1, 0}, 0, 0 },36{1, {6}, 0, {INTEL_PT_TNT, 1, 0x20ULL << 58}, 0, 0 },37{1, {0x80}, 0, {INTEL_PT_TNT, 6, 0}, 0, 0 },38{1, {0xfe}, 0, {INTEL_PT_TNT, 6, 0x3fULL << 58}, 0, 0 },39/* Long Taken/Not Taken Packet */40{8, {0x02, 0xa3, 2}, 0, {INTEL_PT_TNT, 1, 0xa302ULL << 47}, 0, 0 },41{8, {0x02, 0xa3, 3}, 0, {INTEL_PT_TNT, 1, 0x1a302ULL << 47}, 0, 0 },42{8, {0x02, 0xa3, 0, 0, 0, 0, 0, 0x80}, 0, {INTEL_PT_TNT, 47, 0xa302ULL << 1}, 0, 0 },43{8, {0x02, 0xa3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 0, {INTEL_PT_TNT, 47, 0xffffffffffffa302ULL << 1}, 0, 0 },44/* Target IP Packet */45{1, {0x0d}, 0, {INTEL_PT_TIP, 0, 0}, 0, 0 },46{3, {0x2d, 1, 2}, 0, {INTEL_PT_TIP, 1, 0x201}, 0, 0 },47{5, {0x4d, 1, 2, 3, 4}, 0, {INTEL_PT_TIP, 2, 0x4030201}, 0, 0 },48{7, {0x6d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP, 3, 0x60504030201}, 0, 0 },49{7, {0x8d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP, 4, 0x60504030201}, 0, 0 },50{9, {0xcd, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP, 6, 0x807060504030201}, 0, 0 },51/* Packet Generation Enable */52{1, {0x11}, 0, {INTEL_PT_TIP_PGE, 0, 0}, 0, 0 },53{3, {0x31, 1, 2}, 0, {INTEL_PT_TIP_PGE, 1, 0x201}, 0, 0 },54{5, {0x51, 1, 2, 3, 4}, 0, {INTEL_PT_TIP_PGE, 2, 0x4030201}, 0, 0 },55{7, {0x71, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGE, 3, 0x60504030201}, 0, 0 },56{7, {0x91, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGE, 4, 0x60504030201}, 0, 0 },57{9, {0xd1, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP_PGE, 6, 0x807060504030201}, 0, 0 },58/* Packet Generation Disable */59{1, {0x01}, 0, {INTEL_PT_TIP_PGD, 0, 0}, 0, 0 },60{3, {0x21, 1, 2}, 0, {INTEL_PT_TIP_PGD, 1, 0x201}, 0, 0 },61{5, {0x41, 1, 2, 3, 4}, 0, {INTEL_PT_TIP_PGD, 2, 0x4030201}, 0, 0 },62{7, {0x61, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGD, 3, 0x60504030201}, 0, 0 },63{7, {0x81, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_TIP_PGD, 4, 0x60504030201}, 0, 0 },64{9, {0xc1, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_TIP_PGD, 6, 0x807060504030201}, 0, 0 },65/* Flow Update Packet */66{1, {0x1d}, 0, {INTEL_PT_FUP, 0, 0}, 0, 0 },67{3, {0x3d, 1, 2}, 0, {INTEL_PT_FUP, 1, 0x201}, 0, 0 },68{5, {0x5d, 1, 2, 3, 4}, 0, {INTEL_PT_FUP, 2, 0x4030201}, 0, 0 },69{7, {0x7d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_FUP, 3, 0x60504030201}, 0, 0 },70{7, {0x9d, 1, 2, 3, 4, 5, 6}, 0, {INTEL_PT_FUP, 4, 0x60504030201}, 0, 0 },71{9, {0xdd, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_FUP, 6, 0x807060504030201}, 0, 0 },72/* Paging Information Packet */73{8, {0x02, 0x43, 2, 4, 6, 8, 10, 12}, 0, {INTEL_PT_PIP, 0, 0xC0A08060402}, 0, 0 },74{8, {0x02, 0x43, 3, 4, 6, 8, 10, 12}, 0, {INTEL_PT_PIP, 0, 0xC0A08060403}, 0, 0 },75/* Mode Exec Packet */76{2, {0x99, 0x00}, 0, {INTEL_PT_MODE_EXEC, 0, 16}, 0, 0 },77{2, {0x99, 0x01}, 0, {INTEL_PT_MODE_EXEC, 1, 64}, 0, 0 },78{2, {0x99, 0x02}, 0, {INTEL_PT_MODE_EXEC, 2, 32}, 0, 0 },79{2, {0x99, 0x04}, 0, {INTEL_PT_MODE_EXEC, 4, 16}, 0, 0 },80{2, {0x99, 0x05}, 0, {INTEL_PT_MODE_EXEC, 5, 64}, 0, 0 },81{2, {0x99, 0x06}, 0, {INTEL_PT_MODE_EXEC, 6, 32}, 0, 0 },82/* Mode TSX Packet */83{2, {0x99, 0x20}, 0, {INTEL_PT_MODE_TSX, 0, 0}, 0, 0 },84{2, {0x99, 0x21}, 0, {INTEL_PT_MODE_TSX, 0, 1}, 0, 0 },85{2, {0x99, 0x22}, 0, {INTEL_PT_MODE_TSX, 0, 2}, 0, 0 },86/* Trace Stop Packet */87{2, {0x02, 0x83}, 0, {INTEL_PT_TRACESTOP, 0, 0}, 0, 0 },88/* Core:Bus Ratio Packet */89{4, {0x02, 0x03, 0x12, 0}, 0, {INTEL_PT_CBR, 0, 0x12}, 0, 1 },90/* Timestamp Counter Packet */91{8, {0x19, 1, 2, 3, 4, 5, 6, 7}, 0, {INTEL_PT_TSC, 0, 0x7060504030201}, 0, 1 },92/* Mini Time Counter Packet */93{2, {0x59, 0x12}, 0, {INTEL_PT_MTC, 0, 0x12}, 0, 1 },94/* TSC / MTC Alignment Packet */95{7, {0x02, 0x73}, 0, {INTEL_PT_TMA, 0, 0}, 0, 1 },96{7, {0x02, 0x73, 1, 2}, 0, {INTEL_PT_TMA, 0, 0x201}, 0, 1 },97{7, {0x02, 0x73, 0, 0, 0, 0xff, 1}, 0, {INTEL_PT_TMA, 0x1ff, 0}, 0, 1 },98{7, {0x02, 0x73, 0x80, 0xc0, 0, 0xff, 1}, 0, {INTEL_PT_TMA, 0x1ff, 0xc080}, 0, 1 },99/* Cycle Count Packet */100{1, {0x03}, 0, {INTEL_PT_CYC, 0, 0}, 0, 1 },101{1, {0x0b}, 0, {INTEL_PT_CYC, 0, 1}, 0, 1 },102{1, {0xfb}, 0, {INTEL_PT_CYC, 0, 0x1f}, 0, 1 },103{2, {0x07, 2}, 0, {INTEL_PT_CYC, 0, 0x20}, 0, 1 },104{2, {0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0xfff}, 0, 1 },105{3, {0x07, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x1000}, 0, 1 },106{3, {0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x7ffff}, 0, 1 },107{4, {0x07, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x80000}, 0, 1 },108{4, {0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x3ffffff}, 0, 1 },109{5, {0x07, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x4000000}, 0, 1 },110{5, {0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x1ffffffff}, 0, 1 },111{6, {0x07, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x200000000}, 0, 1 },112{6, {0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0xffffffffff}, 0, 1 },113{7, {0x07, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x10000000000}, 0, 1 },114{7, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x7fffffffffff}, 0, 1 },115{8, {0x07, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x800000000000}, 0, 1 },116{8, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x3fffffffffffff}, 0, 1 },117{9, {0x07, 1, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x40000000000000}, 0, 1 },118{9, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}, 0, {INTEL_PT_CYC, 0, 0x1fffffffffffffff}, 0, 1 },119{10, {0x07, 1, 1, 1, 1, 1, 1, 1, 1, 2}, 0, {INTEL_PT_CYC, 0, 0x2000000000000000}, 0, 1 },120{10, {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe}, 0, {INTEL_PT_CYC, 0, 0xffffffffffffffff}, 0, 1 },121/* Virtual-Machine Control Structure Packet */122{7, {0x02, 0xc8, 1, 2, 3, 4, 5}, 0, {INTEL_PT_VMCS, 5, 0x504030201}, 0, 0 },123/* Overflow Packet */124{2, {0x02, 0xf3}, 0, {INTEL_PT_OVF, 0, 0}, 0, 0 },125{2, {0x02, 0xf3}, INTEL_PT_BLK_4_CTX, {INTEL_PT_OVF, 0, 0}, 0, 0 },126{2, {0x02, 0xf3}, INTEL_PT_BLK_8_CTX, {INTEL_PT_OVF, 0, 0}, 0, 0 },127/* Packet Stream Boundary*/128{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 },129{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 },130{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 },131/* PSB End Packet */132{2, {0x02, 0x23}, 0, {INTEL_PT_PSBEND, 0, 0}, 0, 0 },133/* Maintenance Packet */134{11, {0x02, 0xc3, 0x88, 1, 2, 3, 4, 5, 6, 7}, 0, {INTEL_PT_MNT, 0, 0x7060504030201}, 0, 1 },135/* Write Data to PT Packet */136{6, {0x02, 0x12, 1, 2, 3, 4}, 0, {INTEL_PT_PTWRITE, 0, 0x4030201}, 0, 0 },137{10, {0x02, 0x32, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_PTWRITE, 1, 0x807060504030201}, 0, 0 },138{6, {0x02, 0x92, 1, 2, 3, 4}, 0, {INTEL_PT_PTWRITE_IP, 0, 0x4030201}, 0, 0 },139{10, {0x02, 0xb2, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_PTWRITE_IP, 1, 0x807060504030201}, 0, 0 },140/* Execution Stop Packet */141{2, {0x02, 0x62}, 0, {INTEL_PT_EXSTOP, 0, 0}, 0, 1 },142{2, {0x02, 0xe2}, 0, {INTEL_PT_EXSTOP_IP, 0, 0}, 0, 1 },143/* Monitor Wait Packet */144{10, {0x02, 0xc2}, 0, {INTEL_PT_MWAIT, 0, 0}, 0, 0 },145{10, {0x02, 0xc2, 1, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_MWAIT, 0, 0x807060504030201}, 0, 0 },146{10, {0x02, 0xc2, 0xff, 2, 3, 4, 7, 6, 7, 8}, 0, {INTEL_PT_MWAIT, 0, 0x8070607040302ff}, 0, 0 },147/* Power Entry Packet */148{4, {0x02, 0x22}, 0, {INTEL_PT_PWRE, 0, 0}, 0, 1 },149{4, {0x02, 0x22, 1, 2}, 0, {INTEL_PT_PWRE, 0, 0x0201}, 0, 1 },150{4, {0x02, 0x22, 0x80, 0x34}, 0, {INTEL_PT_PWRE, 0, 0x3480}, 0, 1 },151{4, {0x02, 0x22, 0x00, 0x56}, 0, {INTEL_PT_PWRE, 0, 0x5600}, 0, 1 },152/* Power Exit Packet */153{7, {0x02, 0xa2}, 0, {INTEL_PT_PWRX, 0, 0}, 0, 1 },154{7, {0x02, 0xa2, 1, 2, 3, 4, 5}, 0, {INTEL_PT_PWRX, 0, 0x504030201}, 0, 1 },155{7, {0x02, 0xa2, 0xff, 0xff, 0xff, 0xff, 0xff}, 0, {INTEL_PT_PWRX, 0, 0xffffffffff}, 0, 1 },156/* Block Begin Packet */157{3, {0x02, 0x63, 0x00}, 0, {INTEL_PT_BBP, 0, 0}, INTEL_PT_BLK_8_CTX, 0 },158{3, {0x02, 0x63, 0x80}, 0, {INTEL_PT_BBP, 1, 0}, INTEL_PT_BLK_4_CTX, 0 },159{3, {0x02, 0x63, 0x1f}, 0, {INTEL_PT_BBP, 0, 0x1f}, INTEL_PT_BLK_8_CTX, 0 },160{3, {0x02, 0x63, 0x9f}, 0, {INTEL_PT_BBP, 1, 0x1f}, INTEL_PT_BLK_4_CTX, 0 },161/* 4-byte Block Item Packet */162{5, {0x04}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0, 0}, INTEL_PT_BLK_4_CTX, 0 },163{5, {0xfc}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0x1f, 0}, INTEL_PT_BLK_4_CTX, 0 },164{5, {0x04, 1, 2, 3, 4}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0, 0x04030201}, INTEL_PT_BLK_4_CTX, 0 },165{5, {0xfc, 1, 2, 3, 4}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BIP, 0x1f, 0x04030201}, INTEL_PT_BLK_4_CTX, 0 },166/* 8-byte Block Item Packet */167{9, {0x04}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BIP, 0, 0}, INTEL_PT_BLK_8_CTX, 0 },168{9, {0xfc}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BIP, 0x1f, 0}, INTEL_PT_BLK_8_CTX, 0 },169{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 },170{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 },171/* Block End Packet */172{2, {0x02, 0x33}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BEP, 0, 0}, 0, 0 },173{2, {0x02, 0xb3}, INTEL_PT_BLK_4_CTX, {INTEL_PT_BEP_IP, 0, 0}, 0, 0 },174{2, {0x02, 0x33}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BEP, 0, 0}, 0, 0 },175{2, {0x02, 0xb3}, INTEL_PT_BLK_8_CTX, {INTEL_PT_BEP_IP, 0, 0}, 0, 0 },176/* Control Flow Event Packet */177{4, {0x02, 0x13, 0x01, 0x03}, 0, {INTEL_PT_CFE, 1, 3}, 0, 0 },178{4, {0x02, 0x13, 0x81, 0x03}, 0, {INTEL_PT_CFE_IP, 1, 3}, 0, 0 },179{4, {0x02, 0x13, 0x1f, 0x00}, 0, {INTEL_PT_CFE, 0x1f, 0}, 0, 0 },180{4, {0x02, 0x13, 0x9f, 0xff}, 0, {INTEL_PT_CFE_IP, 0x1f, 0xff}, 0, 0 },181/* */182{11, {0x02, 0x53, 0x09, 1, 2, 3, 4, 5, 6, 7}, 0, {INTEL_PT_EVD, 0x09, 0x7060504030201}, 0, 0 },183{11, {0x02, 0x53, 0x3f, 2, 3, 4, 5, 6, 7, 8}, 0, {INTEL_PT_EVD, 0x3f, 0x8070605040302}, 0, 0 },184/* Terminator */185{0, {0}, 0, {0, 0, 0}, 0, 0 },186};187188static int dump_packet(const struct intel_pt_pkt *packet, const u8 *bytes, int len)189{190char desc[INTEL_PT_PKT_DESC_MAX];191int ret, i;192193for (i = 0; i < len; i++)194pr_debug(" %02x", bytes[i]);195for (; i < INTEL_PT_PKT_MAX_SZ; i++)196pr_debug(" ");197pr_debug(" ");198ret = intel_pt_pkt_desc(packet, desc, INTEL_PT_PKT_DESC_MAX);199if (ret < 0) {200pr_debug("intel_pt_pkt_desc failed!\n");201return TEST_FAIL;202}203pr_debug("%s\n", desc);204205return TEST_OK;206}207208static void decoding_failed(const struct test_data *d)209{210pr_debug("Decoding failed!\n");211pr_debug("Decoding: ");212dump_packet(&d->packet, d->bytes, d->len);213}214215static int fail(const struct test_data *d, struct intel_pt_pkt *packet, int len,216enum intel_pt_pkt_ctx new_ctx)217{218decoding_failed(d);219220if (len != d->len)221pr_debug("Expected length: %d Decoded length %d\n",222d->len, len);223224if (packet->type != d->packet.type)225pr_debug("Expected type: %d Decoded type %d\n",226d->packet.type, packet->type);227228if (packet->count != d->packet.count)229pr_debug("Expected count: %d Decoded count %d\n",230d->packet.count, packet->count);231232if (packet->payload != d->packet.payload)233pr_debug("Expected payload: 0x%llx Decoded payload 0x%llx\n",234(unsigned long long)d->packet.payload,235(unsigned long long)packet->payload);236237if (new_ctx != d->new_ctx)238pr_debug("Expected packet context: %d Decoded packet context %d\n",239d->new_ctx, new_ctx);240241return TEST_FAIL;242}243244static int test_ctx_unchanged(const struct test_data *d, struct intel_pt_pkt *packet,245enum intel_pt_pkt_ctx ctx)246{247enum intel_pt_pkt_ctx old_ctx = ctx;248249intel_pt_upd_pkt_ctx(packet, &ctx);250251if (ctx != old_ctx) {252decoding_failed(d);253pr_debug("Packet context changed!\n");254return TEST_FAIL;255}256257return TEST_OK;258}259260static int test_one(const struct test_data *d)261{262struct intel_pt_pkt packet;263enum intel_pt_pkt_ctx ctx = d->ctx;264int ret;265266memset(&packet, 0xff, sizeof(packet));267268/* Decode a packet */269ret = intel_pt_get_packet(d->bytes, d->len, &packet, &ctx);270if (ret < 0 || ret > INTEL_PT_PKT_MAX_SZ) {271decoding_failed(d);272pr_debug("intel_pt_get_packet returned %d\n", ret);273return TEST_FAIL;274}275276/* Some packets must always leave the packet context unchanged */277if (d->ctx_unchanged) {278int err;279280err = test_ctx_unchanged(d, &packet, INTEL_PT_NO_CTX);281if (err)282return err;283err = test_ctx_unchanged(d, &packet, INTEL_PT_BLK_4_CTX);284if (err)285return err;286err = test_ctx_unchanged(d, &packet, INTEL_PT_BLK_8_CTX);287if (err)288return err;289}290291/* Compare to the expected values */292if (ret != d->len || packet.type != d->packet.type ||293packet.count != d->packet.count ||294packet.payload != d->packet.payload || ctx != d->new_ctx)295return fail(d, &packet, ret, ctx);296297pr_debug("Decoded ok:");298ret = dump_packet(&d->packet, d->bytes, d->len);299300return ret;301}302303/*304* This test feeds byte sequences to the Intel PT packet decoder and checks the305* results. Changes to the packet context are also checked.306*/307int test__intel_pt_pkt_decoder(struct test_suite *test __maybe_unused, int subtest __maybe_unused)308{309const struct test_data *d = data;310int ret;311312for (d = data; d->len; d++) {313ret = test_one(d);314if (ret)315return ret;316}317318return TEST_OK;319}320321static int setaffinity(int cpu)322{323cpu_set_t cpu_set;324325CPU_ZERO(&cpu_set);326CPU_SET(cpu, &cpu_set);327if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set)) {328pr_debug("sched_setaffinity() failed for CPU %d\n", cpu);329return -1;330}331return 0;332}333334#define INTEL_PT_ADDR_FILT_CNT_MASK GENMASK(2, 0)335#define INTEL_PT_SUBLEAF_CNT 2336#define CPUID_REG_CNT 4337338struct cpuid_result {339union {340struct {341unsigned int eax;342unsigned int ebx;343unsigned int ecx;344unsigned int edx;345};346unsigned int reg[CPUID_REG_CNT];347};348};349350struct pt_caps {351struct cpuid_result subleaf[INTEL_PT_SUBLEAF_CNT];352};353354static int get_pt_caps(int cpu, struct pt_caps *caps)355{356struct cpuid_result r;357int i;358359if (setaffinity(cpu))360return -1;361362memset(caps, 0, sizeof(*caps));363364for (i = 0; i < INTEL_PT_SUBLEAF_CNT; i++) {365__get_cpuid_count(20, i, &r.eax, &r.ebx, &r.ecx, &r.edx);366pr_debug("CPU %d CPUID leaf 20 subleaf %d\n", cpu, i);367pr_debug("eax = 0x%08x\n", r.eax);368pr_debug("ebx = 0x%08x\n", r.ebx);369pr_debug("ecx = 0x%08x\n", r.ecx);370pr_debug("edx = 0x%08x\n", r.edx);371caps->subleaf[i] = r;372}373374return 0;375}376377static bool is_hybrid(void)378{379unsigned int eax, ebx, ecx, edx = 0;380bool result;381382__get_cpuid_count(7, 0, &eax, &ebx, &ecx, &edx);383result = edx & BIT(15);384pr_debug("Is %shybrid : CPUID leaf 7 subleaf 0 edx %#x (bit-15 indicates hybrid)\n",385result ? "" : "not ", edx);386return result;387}388389static int compare_caps(int cpu, struct pt_caps *caps, struct pt_caps *caps0)390{391struct pt_caps mask = { /* Mask of bits to check*/392.subleaf = {393[0] = {394.ebx = GENMASK(8, 0),395.ecx = GENMASK(3, 0),396},397[1] = {398.eax = GENMASK(31, 16),399.ebx = GENMASK(31, 0),400}401}402};403unsigned int m, reg, reg0;404int ret = 0;405int i, j;406407for (i = 0; i < INTEL_PT_SUBLEAF_CNT; i++) {408for (j = 0; j < CPUID_REG_CNT; j++) {409m = mask.subleaf[i].reg[j];410reg = m & caps->subleaf[i].reg[j];411reg0 = m & caps0->subleaf[i].reg[j];412if ((reg & reg0) != reg0) {413pr_debug("CPU %d subleaf %d reg %d FAIL %#x vs %#x\n",414cpu, i, j, reg, reg0);415ret = -1;416}417}418}419420m = INTEL_PT_ADDR_FILT_CNT_MASK;421reg = m & caps->subleaf[1].eax;422reg0 = m & caps0->subleaf[1].eax;423if (reg < reg0) {424pr_debug("CPU %d subleaf 1 reg 0 FAIL address filter count %#x vs %#x\n",425cpu, reg, reg0);426ret = -1;427}428429if (!ret)430pr_debug("CPU %d OK\n", cpu);431432return ret;433}434435int test__intel_pt_hybrid_compat(struct test_suite *test, int subtest)436{437int max_cpu = cpu__max_cpu().cpu;438struct pt_caps last_caps;439struct pt_caps caps0;440int ret = TEST_OK;441int cpu;442443if (!is_hybrid()) {444test->test_cases[subtest].skip_reason = "not hybrid";445return TEST_SKIP;446}447448if (get_pt_caps(0, &caps0))449return TEST_FAIL;450451for (cpu = 1, last_caps = caps0; cpu < max_cpu; cpu++) {452struct pt_caps caps;453454if (get_pt_caps(cpu, &caps)) {455pr_debug("CPU %d not found\n", cpu);456continue;457}458if (!memcmp(&caps, &last_caps, sizeof(caps))) {459pr_debug("CPU %d same caps as previous CPU\n", cpu);460continue;461}462if (compare_caps(cpu, &caps, &caps0))463ret = TEST_FAIL;464last_caps = caps;465}466467return ret;468}469470471