162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci#include <errno.h> 362306a36Sopenharmony_ci#include <string.h> 462306a36Sopenharmony_ci#include <regex.h> 562306a36Sopenharmony_ci#include <linux/zalloc.h> 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#include "perf_regs.h" 862306a36Sopenharmony_ci#include "../../../util/perf_regs.h" 962306a36Sopenharmony_ci#include "../../../util/debug.h" 1062306a36Sopenharmony_ci#include "../../../util/event.h" 1162306a36Sopenharmony_ci#include "../../../util/header.h" 1262306a36Sopenharmony_ci#include "../../../perf-sys.h" 1362306a36Sopenharmony_ci#include "utils_header.h" 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include <linux/kernel.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#define PVR_POWER9 0x004E 1862306a36Sopenharmony_ci#define PVR_POWER10 0x0080 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ciconst struct sample_reg sample_reg_masks[] = { 2162306a36Sopenharmony_ci SMPL_REG(r0, PERF_REG_POWERPC_R0), 2262306a36Sopenharmony_ci SMPL_REG(r1, PERF_REG_POWERPC_R1), 2362306a36Sopenharmony_ci SMPL_REG(r2, PERF_REG_POWERPC_R2), 2462306a36Sopenharmony_ci SMPL_REG(r3, PERF_REG_POWERPC_R3), 2562306a36Sopenharmony_ci SMPL_REG(r4, PERF_REG_POWERPC_R4), 2662306a36Sopenharmony_ci SMPL_REG(r5, PERF_REG_POWERPC_R5), 2762306a36Sopenharmony_ci SMPL_REG(r6, PERF_REG_POWERPC_R6), 2862306a36Sopenharmony_ci SMPL_REG(r7, PERF_REG_POWERPC_R7), 2962306a36Sopenharmony_ci SMPL_REG(r8, PERF_REG_POWERPC_R8), 3062306a36Sopenharmony_ci SMPL_REG(r9, PERF_REG_POWERPC_R9), 3162306a36Sopenharmony_ci SMPL_REG(r10, PERF_REG_POWERPC_R10), 3262306a36Sopenharmony_ci SMPL_REG(r11, PERF_REG_POWERPC_R11), 3362306a36Sopenharmony_ci SMPL_REG(r12, PERF_REG_POWERPC_R12), 3462306a36Sopenharmony_ci SMPL_REG(r13, PERF_REG_POWERPC_R13), 3562306a36Sopenharmony_ci SMPL_REG(r14, PERF_REG_POWERPC_R14), 3662306a36Sopenharmony_ci SMPL_REG(r15, PERF_REG_POWERPC_R15), 3762306a36Sopenharmony_ci SMPL_REG(r16, PERF_REG_POWERPC_R16), 3862306a36Sopenharmony_ci SMPL_REG(r17, PERF_REG_POWERPC_R17), 3962306a36Sopenharmony_ci SMPL_REG(r18, PERF_REG_POWERPC_R18), 4062306a36Sopenharmony_ci SMPL_REG(r19, PERF_REG_POWERPC_R19), 4162306a36Sopenharmony_ci SMPL_REG(r20, PERF_REG_POWERPC_R20), 4262306a36Sopenharmony_ci SMPL_REG(r21, PERF_REG_POWERPC_R21), 4362306a36Sopenharmony_ci SMPL_REG(r22, PERF_REG_POWERPC_R22), 4462306a36Sopenharmony_ci SMPL_REG(r23, PERF_REG_POWERPC_R23), 4562306a36Sopenharmony_ci SMPL_REG(r24, PERF_REG_POWERPC_R24), 4662306a36Sopenharmony_ci SMPL_REG(r25, PERF_REG_POWERPC_R25), 4762306a36Sopenharmony_ci SMPL_REG(r26, PERF_REG_POWERPC_R26), 4862306a36Sopenharmony_ci SMPL_REG(r27, PERF_REG_POWERPC_R27), 4962306a36Sopenharmony_ci SMPL_REG(r28, PERF_REG_POWERPC_R28), 5062306a36Sopenharmony_ci SMPL_REG(r29, PERF_REG_POWERPC_R29), 5162306a36Sopenharmony_ci SMPL_REG(r30, PERF_REG_POWERPC_R30), 5262306a36Sopenharmony_ci SMPL_REG(r31, PERF_REG_POWERPC_R31), 5362306a36Sopenharmony_ci SMPL_REG(nip, PERF_REG_POWERPC_NIP), 5462306a36Sopenharmony_ci SMPL_REG(msr, PERF_REG_POWERPC_MSR), 5562306a36Sopenharmony_ci SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3), 5662306a36Sopenharmony_ci SMPL_REG(ctr, PERF_REG_POWERPC_CTR), 5762306a36Sopenharmony_ci SMPL_REG(link, PERF_REG_POWERPC_LINK), 5862306a36Sopenharmony_ci SMPL_REG(xer, PERF_REG_POWERPC_XER), 5962306a36Sopenharmony_ci SMPL_REG(ccr, PERF_REG_POWERPC_CCR), 6062306a36Sopenharmony_ci SMPL_REG(softe, PERF_REG_POWERPC_SOFTE), 6162306a36Sopenharmony_ci SMPL_REG(trap, PERF_REG_POWERPC_TRAP), 6262306a36Sopenharmony_ci SMPL_REG(dar, PERF_REG_POWERPC_DAR), 6362306a36Sopenharmony_ci SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR), 6462306a36Sopenharmony_ci SMPL_REG(sier, PERF_REG_POWERPC_SIER), 6562306a36Sopenharmony_ci SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA), 6662306a36Sopenharmony_ci SMPL_REG(mmcr0, PERF_REG_POWERPC_MMCR0), 6762306a36Sopenharmony_ci SMPL_REG(mmcr1, PERF_REG_POWERPC_MMCR1), 6862306a36Sopenharmony_ci SMPL_REG(mmcr2, PERF_REG_POWERPC_MMCR2), 6962306a36Sopenharmony_ci SMPL_REG(mmcr3, PERF_REG_POWERPC_MMCR3), 7062306a36Sopenharmony_ci SMPL_REG(sier2, PERF_REG_POWERPC_SIER2), 7162306a36Sopenharmony_ci SMPL_REG(sier3, PERF_REG_POWERPC_SIER3), 7262306a36Sopenharmony_ci SMPL_REG(pmc1, PERF_REG_POWERPC_PMC1), 7362306a36Sopenharmony_ci SMPL_REG(pmc2, PERF_REG_POWERPC_PMC2), 7462306a36Sopenharmony_ci SMPL_REG(pmc3, PERF_REG_POWERPC_PMC3), 7562306a36Sopenharmony_ci SMPL_REG(pmc4, PERF_REG_POWERPC_PMC4), 7662306a36Sopenharmony_ci SMPL_REG(pmc5, PERF_REG_POWERPC_PMC5), 7762306a36Sopenharmony_ci SMPL_REG(pmc6, PERF_REG_POWERPC_PMC6), 7862306a36Sopenharmony_ci SMPL_REG(sdar, PERF_REG_POWERPC_SDAR), 7962306a36Sopenharmony_ci SMPL_REG(siar, PERF_REG_POWERPC_SIAR), 8062306a36Sopenharmony_ci SMPL_REG_END 8162306a36Sopenharmony_ci}; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci/* REG or %rREG */ 8462306a36Sopenharmony_ci#define SDT_OP_REGEX1 "^(%r)?([1-2]?[0-9]|3[0-1])$" 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci/* -NUM(REG) or NUM(REG) or -NUM(%rREG) or NUM(%rREG) */ 8762306a36Sopenharmony_ci#define SDT_OP_REGEX2 "^(\\-)?([0-9]+)\\((%r)?([1-2]?[0-9]|3[0-1])\\)$" 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_cistatic regex_t sdt_op_regex1, sdt_op_regex2; 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_cistatic int sdt_init_op_regex(void) 9262306a36Sopenharmony_ci{ 9362306a36Sopenharmony_ci static int initialized; 9462306a36Sopenharmony_ci int ret = 0; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci if (initialized) 9762306a36Sopenharmony_ci return 0; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci ret = regcomp(&sdt_op_regex1, SDT_OP_REGEX1, REG_EXTENDED); 10062306a36Sopenharmony_ci if (ret) 10162306a36Sopenharmony_ci goto error; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci ret = regcomp(&sdt_op_regex2, SDT_OP_REGEX2, REG_EXTENDED); 10462306a36Sopenharmony_ci if (ret) 10562306a36Sopenharmony_ci goto free_regex1; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci initialized = 1; 10862306a36Sopenharmony_ci return 0; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_cifree_regex1: 11162306a36Sopenharmony_ci regfree(&sdt_op_regex1); 11262306a36Sopenharmony_cierror: 11362306a36Sopenharmony_ci pr_debug4("Regex compilation error.\n"); 11462306a36Sopenharmony_ci return ret; 11562306a36Sopenharmony_ci} 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci/* 11862306a36Sopenharmony_ci * Parse OP and convert it into uprobe format, which is, +/-NUM(%gprREG). 11962306a36Sopenharmony_ci * Possible variants of OP are: 12062306a36Sopenharmony_ci * Format Example 12162306a36Sopenharmony_ci * ------------------------- 12262306a36Sopenharmony_ci * NUM(REG) 48(18) 12362306a36Sopenharmony_ci * -NUM(REG) -48(18) 12462306a36Sopenharmony_ci * NUM(%rREG) 48(%r18) 12562306a36Sopenharmony_ci * -NUM(%rREG) -48(%r18) 12662306a36Sopenharmony_ci * REG 18 12762306a36Sopenharmony_ci * %rREG %r18 12862306a36Sopenharmony_ci * iNUM i0 12962306a36Sopenharmony_ci * i-NUM i-1 13062306a36Sopenharmony_ci * 13162306a36Sopenharmony_ci * SDT marker arguments on Powerpc uses %rREG form with -mregnames flag 13262306a36Sopenharmony_ci * and REG form with -mno-regnames. Here REG is general purpose register, 13362306a36Sopenharmony_ci * which is in 0 to 31 range. 13462306a36Sopenharmony_ci */ 13562306a36Sopenharmony_ciint arch_sdt_arg_parse_op(char *old_op, char **new_op) 13662306a36Sopenharmony_ci{ 13762306a36Sopenharmony_ci int ret, new_len; 13862306a36Sopenharmony_ci regmatch_t rm[5]; 13962306a36Sopenharmony_ci char prefix; 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci /* Constant argument. Uprobe does not support it */ 14262306a36Sopenharmony_ci if (old_op[0] == 'i') { 14362306a36Sopenharmony_ci pr_debug4("Skipping unsupported SDT argument: %s\n", old_op); 14462306a36Sopenharmony_ci return SDT_ARG_SKIP; 14562306a36Sopenharmony_ci } 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci ret = sdt_init_op_regex(); 14862306a36Sopenharmony_ci if (ret < 0) 14962306a36Sopenharmony_ci return ret; 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci if (!regexec(&sdt_op_regex1, old_op, 3, rm, 0)) { 15262306a36Sopenharmony_ci /* REG or %rREG --> %gprREG */ 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci new_len = 5; /* % g p r NULL */ 15562306a36Sopenharmony_ci new_len += (int)(rm[2].rm_eo - rm[2].rm_so); 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci *new_op = zalloc(new_len); 15862306a36Sopenharmony_ci if (!*new_op) 15962306a36Sopenharmony_ci return -ENOMEM; 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci scnprintf(*new_op, new_len, "%%gpr%.*s", 16262306a36Sopenharmony_ci (int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so); 16362306a36Sopenharmony_ci } else if (!regexec(&sdt_op_regex2, old_op, 5, rm, 0)) { 16462306a36Sopenharmony_ci /* 16562306a36Sopenharmony_ci * -NUM(REG) or NUM(REG) or -NUM(%rREG) or NUM(%rREG) --> 16662306a36Sopenharmony_ci * +/-NUM(%gprREG) 16762306a36Sopenharmony_ci */ 16862306a36Sopenharmony_ci prefix = (rm[1].rm_so == -1) ? '+' : '-'; 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci new_len = 8; /* +/- ( % g p r ) NULL */ 17162306a36Sopenharmony_ci new_len += (int)(rm[2].rm_eo - rm[2].rm_so); 17262306a36Sopenharmony_ci new_len += (int)(rm[4].rm_eo - rm[4].rm_so); 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci *new_op = zalloc(new_len); 17562306a36Sopenharmony_ci if (!*new_op) 17662306a36Sopenharmony_ci return -ENOMEM; 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci scnprintf(*new_op, new_len, "%c%.*s(%%gpr%.*s)", prefix, 17962306a36Sopenharmony_ci (int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so, 18062306a36Sopenharmony_ci (int)(rm[4].rm_eo - rm[4].rm_so), old_op + rm[4].rm_so); 18162306a36Sopenharmony_ci } else { 18262306a36Sopenharmony_ci pr_debug4("Skipping unsupported SDT argument: %s\n", old_op); 18362306a36Sopenharmony_ci return SDT_ARG_SKIP; 18462306a36Sopenharmony_ci } 18562306a36Sopenharmony_ci 18662306a36Sopenharmony_ci return SDT_ARG_VALID; 18762306a36Sopenharmony_ci} 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ciuint64_t arch__intr_reg_mask(void) 19062306a36Sopenharmony_ci{ 19162306a36Sopenharmony_ci struct perf_event_attr attr = { 19262306a36Sopenharmony_ci .type = PERF_TYPE_HARDWARE, 19362306a36Sopenharmony_ci .config = PERF_COUNT_HW_CPU_CYCLES, 19462306a36Sopenharmony_ci .sample_type = PERF_SAMPLE_REGS_INTR, 19562306a36Sopenharmony_ci .precise_ip = 1, 19662306a36Sopenharmony_ci .disabled = 1, 19762306a36Sopenharmony_ci .exclude_kernel = 1, 19862306a36Sopenharmony_ci }; 19962306a36Sopenharmony_ci int fd; 20062306a36Sopenharmony_ci u32 version; 20162306a36Sopenharmony_ci u64 extended_mask = 0, mask = PERF_REGS_MASK; 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci /* 20462306a36Sopenharmony_ci * Get the PVR value to set the extended 20562306a36Sopenharmony_ci * mask specific to platform. 20662306a36Sopenharmony_ci */ 20762306a36Sopenharmony_ci version = (((mfspr(SPRN_PVR)) >> 16) & 0xFFFF); 20862306a36Sopenharmony_ci if (version == PVR_POWER9) 20962306a36Sopenharmony_ci extended_mask = PERF_REG_PMU_MASK_300; 21062306a36Sopenharmony_ci else if (version == PVR_POWER10) 21162306a36Sopenharmony_ci extended_mask = PERF_REG_PMU_MASK_31; 21262306a36Sopenharmony_ci else 21362306a36Sopenharmony_ci return mask; 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci attr.sample_regs_intr = extended_mask; 21662306a36Sopenharmony_ci attr.sample_period = 1; 21762306a36Sopenharmony_ci event_attr_init(&attr); 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ci /* 22062306a36Sopenharmony_ci * check if the pmu supports perf extended regs, before 22162306a36Sopenharmony_ci * returning the register mask to sample. 22262306a36Sopenharmony_ci */ 22362306a36Sopenharmony_ci fd = sys_perf_event_open(&attr, 0, -1, -1, 0); 22462306a36Sopenharmony_ci if (fd != -1) { 22562306a36Sopenharmony_ci close(fd); 22662306a36Sopenharmony_ci mask |= extended_mask; 22762306a36Sopenharmony_ci } 22862306a36Sopenharmony_ci return mask; 22962306a36Sopenharmony_ci} 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ciuint64_t arch__user_reg_mask(void) 23262306a36Sopenharmony_ci{ 23362306a36Sopenharmony_ci return PERF_REGS_MASK; 23462306a36Sopenharmony_ci} 235