18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <errno.h> 38c2ecf20Sopenharmony_ci#include <string.h> 48c2ecf20Sopenharmony_ci#include <regex.h> 58c2ecf20Sopenharmony_ci#include <linux/zalloc.h> 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include "../../../util/perf_regs.h" 88c2ecf20Sopenharmony_ci#include "../../../util/debug.h" 98c2ecf20Sopenharmony_ci#include "../../../util/event.h" 108c2ecf20Sopenharmony_ci#include "../../../util/header.h" 118c2ecf20Sopenharmony_ci#include "../../../perf-sys.h" 128c2ecf20Sopenharmony_ci#include "utils_header.h" 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define PVR_POWER9 0x004E 178c2ecf20Sopenharmony_ci#define PVR_POWER10 0x0080 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciconst struct sample_reg sample_reg_masks[] = { 208c2ecf20Sopenharmony_ci SMPL_REG(r0, PERF_REG_POWERPC_R0), 218c2ecf20Sopenharmony_ci SMPL_REG(r1, PERF_REG_POWERPC_R1), 228c2ecf20Sopenharmony_ci SMPL_REG(r2, PERF_REG_POWERPC_R2), 238c2ecf20Sopenharmony_ci SMPL_REG(r3, PERF_REG_POWERPC_R3), 248c2ecf20Sopenharmony_ci SMPL_REG(r4, PERF_REG_POWERPC_R4), 258c2ecf20Sopenharmony_ci SMPL_REG(r5, PERF_REG_POWERPC_R5), 268c2ecf20Sopenharmony_ci SMPL_REG(r6, PERF_REG_POWERPC_R6), 278c2ecf20Sopenharmony_ci SMPL_REG(r7, PERF_REG_POWERPC_R7), 288c2ecf20Sopenharmony_ci SMPL_REG(r8, PERF_REG_POWERPC_R8), 298c2ecf20Sopenharmony_ci SMPL_REG(r9, PERF_REG_POWERPC_R9), 308c2ecf20Sopenharmony_ci SMPL_REG(r10, PERF_REG_POWERPC_R10), 318c2ecf20Sopenharmony_ci SMPL_REG(r11, PERF_REG_POWERPC_R11), 328c2ecf20Sopenharmony_ci SMPL_REG(r12, PERF_REG_POWERPC_R12), 338c2ecf20Sopenharmony_ci SMPL_REG(r13, PERF_REG_POWERPC_R13), 348c2ecf20Sopenharmony_ci SMPL_REG(r14, PERF_REG_POWERPC_R14), 358c2ecf20Sopenharmony_ci SMPL_REG(r15, PERF_REG_POWERPC_R15), 368c2ecf20Sopenharmony_ci SMPL_REG(r16, PERF_REG_POWERPC_R16), 378c2ecf20Sopenharmony_ci SMPL_REG(r17, PERF_REG_POWERPC_R17), 388c2ecf20Sopenharmony_ci SMPL_REG(r18, PERF_REG_POWERPC_R18), 398c2ecf20Sopenharmony_ci SMPL_REG(r19, PERF_REG_POWERPC_R19), 408c2ecf20Sopenharmony_ci SMPL_REG(r20, PERF_REG_POWERPC_R20), 418c2ecf20Sopenharmony_ci SMPL_REG(r21, PERF_REG_POWERPC_R21), 428c2ecf20Sopenharmony_ci SMPL_REG(r22, PERF_REG_POWERPC_R22), 438c2ecf20Sopenharmony_ci SMPL_REG(r23, PERF_REG_POWERPC_R23), 448c2ecf20Sopenharmony_ci SMPL_REG(r24, PERF_REG_POWERPC_R24), 458c2ecf20Sopenharmony_ci SMPL_REG(r25, PERF_REG_POWERPC_R25), 468c2ecf20Sopenharmony_ci SMPL_REG(r26, PERF_REG_POWERPC_R26), 478c2ecf20Sopenharmony_ci SMPL_REG(r27, PERF_REG_POWERPC_R27), 488c2ecf20Sopenharmony_ci SMPL_REG(r28, PERF_REG_POWERPC_R28), 498c2ecf20Sopenharmony_ci SMPL_REG(r29, PERF_REG_POWERPC_R29), 508c2ecf20Sopenharmony_ci SMPL_REG(r30, PERF_REG_POWERPC_R30), 518c2ecf20Sopenharmony_ci SMPL_REG(r31, PERF_REG_POWERPC_R31), 528c2ecf20Sopenharmony_ci SMPL_REG(nip, PERF_REG_POWERPC_NIP), 538c2ecf20Sopenharmony_ci SMPL_REG(msr, PERF_REG_POWERPC_MSR), 548c2ecf20Sopenharmony_ci SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3), 558c2ecf20Sopenharmony_ci SMPL_REG(ctr, PERF_REG_POWERPC_CTR), 568c2ecf20Sopenharmony_ci SMPL_REG(link, PERF_REG_POWERPC_LINK), 578c2ecf20Sopenharmony_ci SMPL_REG(xer, PERF_REG_POWERPC_XER), 588c2ecf20Sopenharmony_ci SMPL_REG(ccr, PERF_REG_POWERPC_CCR), 598c2ecf20Sopenharmony_ci SMPL_REG(softe, PERF_REG_POWERPC_SOFTE), 608c2ecf20Sopenharmony_ci SMPL_REG(trap, PERF_REG_POWERPC_TRAP), 618c2ecf20Sopenharmony_ci SMPL_REG(dar, PERF_REG_POWERPC_DAR), 628c2ecf20Sopenharmony_ci SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR), 638c2ecf20Sopenharmony_ci SMPL_REG(sier, PERF_REG_POWERPC_SIER), 648c2ecf20Sopenharmony_ci SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA), 658c2ecf20Sopenharmony_ci SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0), 668c2ecf20Sopenharmony_ci SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1), 678c2ecf20Sopenharmony_ci SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2), 688c2ecf20Sopenharmony_ci SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3), 698c2ecf20Sopenharmony_ci SMPL_REG(sier2, PERF_REG_POWERPC_SIER2), 708c2ecf20Sopenharmony_ci SMPL_REG(sier3, PERF_REG_POWERPC_SIER3), 718c2ecf20Sopenharmony_ci SMPL_REG_END 728c2ecf20Sopenharmony_ci}; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci/* REG or %rREG */ 758c2ecf20Sopenharmony_ci#define SDT_OP_REGEX1 "^(%r)?([1-2]?[0-9]|3[0-1])$" 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/* -NUM(REG) or NUM(REG) or -NUM(%rREG) or NUM(%rREG) */ 788c2ecf20Sopenharmony_ci#define SDT_OP_REGEX2 "^(\\-)?([0-9]+)\\((%r)?([1-2]?[0-9]|3[0-1])\\)$" 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic regex_t sdt_op_regex1, sdt_op_regex2; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic int sdt_init_op_regex(void) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci static int initialized; 858c2ecf20Sopenharmony_ci int ret = 0; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci if (initialized) 888c2ecf20Sopenharmony_ci return 0; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci ret = regcomp(&sdt_op_regex1, SDT_OP_REGEX1, REG_EXTENDED); 918c2ecf20Sopenharmony_ci if (ret) 928c2ecf20Sopenharmony_ci goto error; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci ret = regcomp(&sdt_op_regex2, SDT_OP_REGEX2, REG_EXTENDED); 958c2ecf20Sopenharmony_ci if (ret) 968c2ecf20Sopenharmony_ci goto free_regex1; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci initialized = 1; 998c2ecf20Sopenharmony_ci return 0; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cifree_regex1: 1028c2ecf20Sopenharmony_ci regfree(&sdt_op_regex1); 1038c2ecf20Sopenharmony_cierror: 1048c2ecf20Sopenharmony_ci pr_debug4("Regex compilation error.\n"); 1058c2ecf20Sopenharmony_ci return ret; 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci/* 1098c2ecf20Sopenharmony_ci * Parse OP and convert it into uprobe format, which is, +/-NUM(%gprREG). 1108c2ecf20Sopenharmony_ci * Possible variants of OP are: 1118c2ecf20Sopenharmony_ci * Format Example 1128c2ecf20Sopenharmony_ci * ------------------------- 1138c2ecf20Sopenharmony_ci * NUM(REG) 48(18) 1148c2ecf20Sopenharmony_ci * -NUM(REG) -48(18) 1158c2ecf20Sopenharmony_ci * NUM(%rREG) 48(%r18) 1168c2ecf20Sopenharmony_ci * -NUM(%rREG) -48(%r18) 1178c2ecf20Sopenharmony_ci * REG 18 1188c2ecf20Sopenharmony_ci * %rREG %r18 1198c2ecf20Sopenharmony_ci * iNUM i0 1208c2ecf20Sopenharmony_ci * i-NUM i-1 1218c2ecf20Sopenharmony_ci * 1228c2ecf20Sopenharmony_ci * SDT marker arguments on Powerpc uses %rREG form with -mregnames flag 1238c2ecf20Sopenharmony_ci * and REG form with -mno-regnames. Here REG is general purpose register, 1248c2ecf20Sopenharmony_ci * which is in 0 to 31 range. 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_ciint arch_sdt_arg_parse_op(char *old_op, char **new_op) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci int ret, new_len; 1298c2ecf20Sopenharmony_ci regmatch_t rm[5]; 1308c2ecf20Sopenharmony_ci char prefix; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci /* Constant argument. Uprobe does not support it */ 1338c2ecf20Sopenharmony_ci if (old_op[0] == 'i') { 1348c2ecf20Sopenharmony_ci pr_debug4("Skipping unsupported SDT argument: %s\n", old_op); 1358c2ecf20Sopenharmony_ci return SDT_ARG_SKIP; 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci ret = sdt_init_op_regex(); 1398c2ecf20Sopenharmony_ci if (ret < 0) 1408c2ecf20Sopenharmony_ci return ret; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci if (!regexec(&sdt_op_regex1, old_op, 3, rm, 0)) { 1438c2ecf20Sopenharmony_ci /* REG or %rREG --> %gprREG */ 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci new_len = 5; /* % g p r NULL */ 1468c2ecf20Sopenharmony_ci new_len += (int)(rm[2].rm_eo - rm[2].rm_so); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci *new_op = zalloc(new_len); 1498c2ecf20Sopenharmony_ci if (!*new_op) 1508c2ecf20Sopenharmony_ci return -ENOMEM; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci scnprintf(*new_op, new_len, "%%gpr%.*s", 1538c2ecf20Sopenharmony_ci (int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so); 1548c2ecf20Sopenharmony_ci } else if (!regexec(&sdt_op_regex2, old_op, 5, rm, 0)) { 1558c2ecf20Sopenharmony_ci /* 1568c2ecf20Sopenharmony_ci * -NUM(REG) or NUM(REG) or -NUM(%rREG) or NUM(%rREG) --> 1578c2ecf20Sopenharmony_ci * +/-NUM(%gprREG) 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_ci prefix = (rm[1].rm_so == -1) ? '+' : '-'; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci new_len = 8; /* +/- ( % g p r ) NULL */ 1628c2ecf20Sopenharmony_ci new_len += (int)(rm[2].rm_eo - rm[2].rm_so); 1638c2ecf20Sopenharmony_ci new_len += (int)(rm[4].rm_eo - rm[4].rm_so); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci *new_op = zalloc(new_len); 1668c2ecf20Sopenharmony_ci if (!*new_op) 1678c2ecf20Sopenharmony_ci return -ENOMEM; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci scnprintf(*new_op, new_len, "%c%.*s(%%gpr%.*s)", prefix, 1708c2ecf20Sopenharmony_ci (int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so, 1718c2ecf20Sopenharmony_ci (int)(rm[4].rm_eo - rm[4].rm_so), old_op + rm[4].rm_so); 1728c2ecf20Sopenharmony_ci } else { 1738c2ecf20Sopenharmony_ci pr_debug4("Skipping unsupported SDT argument: %s\n", old_op); 1748c2ecf20Sopenharmony_ci return SDT_ARG_SKIP; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci return SDT_ARG_VALID; 1788c2ecf20Sopenharmony_ci} 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ciuint64_t arch__intr_reg_mask(void) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci struct perf_event_attr attr = { 1838c2ecf20Sopenharmony_ci .type = PERF_TYPE_HARDWARE, 1848c2ecf20Sopenharmony_ci .config = PERF_COUNT_HW_CPU_CYCLES, 1858c2ecf20Sopenharmony_ci .sample_type = PERF_SAMPLE_REGS_INTR, 1868c2ecf20Sopenharmony_ci .precise_ip = 1, 1878c2ecf20Sopenharmony_ci .disabled = 1, 1888c2ecf20Sopenharmony_ci .exclude_kernel = 1, 1898c2ecf20Sopenharmony_ci }; 1908c2ecf20Sopenharmony_ci int fd; 1918c2ecf20Sopenharmony_ci u32 version; 1928c2ecf20Sopenharmony_ci u64 extended_mask = 0, mask = PERF_REGS_MASK; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci /* 1958c2ecf20Sopenharmony_ci * Get the PVR value to set the extended 1968c2ecf20Sopenharmony_ci * mask specific to platform. 1978c2ecf20Sopenharmony_ci */ 1988c2ecf20Sopenharmony_ci version = (((mfspr(SPRN_PVR)) >> 16) & 0xFFFF); 1998c2ecf20Sopenharmony_ci if (version == PVR_POWER9) 2008c2ecf20Sopenharmony_ci extended_mask = PERF_REG_PMU_MASK_300; 2018c2ecf20Sopenharmony_ci else if (version == PVR_POWER10) 2028c2ecf20Sopenharmony_ci extended_mask = PERF_REG_PMU_MASK_31; 2038c2ecf20Sopenharmony_ci else 2048c2ecf20Sopenharmony_ci return mask; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci attr.sample_regs_intr = extended_mask; 2078c2ecf20Sopenharmony_ci attr.sample_period = 1; 2088c2ecf20Sopenharmony_ci event_attr_init(&attr); 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci /* 2118c2ecf20Sopenharmony_ci * check if the pmu supports perf extended regs, before 2128c2ecf20Sopenharmony_ci * returning the register mask to sample. 2138c2ecf20Sopenharmony_ci */ 2148c2ecf20Sopenharmony_ci fd = sys_perf_event_open(&attr, 0, -1, -1, 0); 2158c2ecf20Sopenharmony_ci if (fd != -1) { 2168c2ecf20Sopenharmony_ci close(fd); 2178c2ecf20Sopenharmony_ci mask |= extended_mask; 2188c2ecf20Sopenharmony_ci } 2198c2ecf20Sopenharmony_ci return mask; 2208c2ecf20Sopenharmony_ci} 221