1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <string.h> 4 #include <regex.h> 5 #include <linux/zalloc.h> 6 7 #include "../../perf.h" 8 #include "../../util/perf_regs.h" 9 #include "../../util/debug.h" 10 11 #include <linux/kernel.h> 12 13 const struct sample_reg sample_reg_masks[] = { 14 SMPL_REG(r0, PERF_REG_POWERPC_R0), 15 SMPL_REG(r1, PERF_REG_POWERPC_R1), 16 SMPL_REG(r2, PERF_REG_POWERPC_R2), 17 SMPL_REG(r3, PERF_REG_POWERPC_R3), 18 SMPL_REG(r4, PERF_REG_POWERPC_R4), 19 SMPL_REG(r5, PERF_REG_POWERPC_R5), 20 SMPL_REG(r6, PERF_REG_POWERPC_R6), 21 SMPL_REG(r7, PERF_REG_POWERPC_R7), 22 SMPL_REG(r8, PERF_REG_POWERPC_R8), 23 SMPL_REG(r9, PERF_REG_POWERPC_R9), 24 SMPL_REG(r10, PERF_REG_POWERPC_R10), 25 SMPL_REG(r11, PERF_REG_POWERPC_R11), 26 SMPL_REG(r12, PERF_REG_POWERPC_R12), 27 SMPL_REG(r13, PERF_REG_POWERPC_R13), 28 SMPL_REG(r14, PERF_REG_POWERPC_R14), 29 SMPL_REG(r15, PERF_REG_POWERPC_R15), 30 SMPL_REG(r16, PERF_REG_POWERPC_R16), 31 SMPL_REG(r17, PERF_REG_POWERPC_R17), 32 SMPL_REG(r18, PERF_REG_POWERPC_R18), 33 SMPL_REG(r19, PERF_REG_POWERPC_R19), 34 SMPL_REG(r20, PERF_REG_POWERPC_R20), 35 SMPL_REG(r21, PERF_REG_POWERPC_R21), 36 SMPL_REG(r22, PERF_REG_POWERPC_R22), 37 SMPL_REG(r23, PERF_REG_POWERPC_R23), 38 SMPL_REG(r24, PERF_REG_POWERPC_R24), 39 SMPL_REG(r25, PERF_REG_POWERPC_R25), 40 SMPL_REG(r26, PERF_REG_POWERPC_R26), 41 SMPL_REG(r27, PERF_REG_POWERPC_R27), 42 SMPL_REG(r28, PERF_REG_POWERPC_R28), 43 SMPL_REG(r29, PERF_REG_POWERPC_R29), 44 SMPL_REG(r30, PERF_REG_POWERPC_R30), 45 SMPL_REG(r31, PERF_REG_POWERPC_R31), 46 SMPL_REG(nip, PERF_REG_POWERPC_NIP), 47 SMPL_REG(msr, PERF_REG_POWERPC_MSR), 48 SMPL_REG(orig_r3, PERF_REG_POWERPC_ORIG_R3), 49 SMPL_REG(ctr, PERF_REG_POWERPC_CTR), 50 SMPL_REG(link, PERF_REG_POWERPC_LINK), 51 SMPL_REG(xer, PERF_REG_POWERPC_XER), 52 SMPL_REG(ccr, PERF_REG_POWERPC_CCR), 53 SMPL_REG(softe, PERF_REG_POWERPC_SOFTE), 54 SMPL_REG(trap, PERF_REG_POWERPC_TRAP), 55 SMPL_REG(dar, PERF_REG_POWERPC_DAR), 56 SMPL_REG(dsisr, PERF_REG_POWERPC_DSISR), 57 SMPL_REG(sier, PERF_REG_POWERPC_SIER), 58 SMPL_REG(mmcra, PERF_REG_POWERPC_MMCRA), 59 SMPL_REG_END 60 }; 61 62 /* REG or %rREG */ 63 #define SDT_OP_REGEX1 "^(%r)?([1-2]?[0-9]|3[0-1])$" 64 65 /* -NUM(REG) or NUM(REG) or -NUM(%rREG) or NUM(%rREG) */ 66 #define SDT_OP_REGEX2 "^(\\-)?([0-9]+)\\((%r)?([1-2]?[0-9]|3[0-1])\\)$" 67 68 static regex_t sdt_op_regex1, sdt_op_regex2; 69 70 static int sdt_init_op_regex(void) 71 { 72 static int initialized; 73 int ret = 0; 74 75 if (initialized) 76 return 0; 77 78 ret = regcomp(&sdt_op_regex1, SDT_OP_REGEX1, REG_EXTENDED); 79 if (ret) 80 goto error; 81 82 ret = regcomp(&sdt_op_regex2, SDT_OP_REGEX2, REG_EXTENDED); 83 if (ret) 84 goto free_regex1; 85 86 initialized = 1; 87 return 0; 88 89 free_regex1: 90 regfree(&sdt_op_regex1); 91 error: 92 pr_debug4("Regex compilation error.\n"); 93 return ret; 94 } 95 96 /* 97 * Parse OP and convert it into uprobe format, which is, +/-NUM(%gprREG). 98 * Possible variants of OP are: 99 * Format Example 100 * ------------------------- 101 * NUM(REG) 48(18) 102 * -NUM(REG) -48(18) 103 * NUM(%rREG) 48(%r18) 104 * -NUM(%rREG) -48(%r18) 105 * REG 18 106 * %rREG %r18 107 * iNUM i0 108 * i-NUM i-1 109 * 110 * SDT marker arguments on Powerpc uses %rREG form with -mregnames flag 111 * and REG form with -mno-regnames. Here REG is general purpose register, 112 * which is in 0 to 31 range. 113 */ 114 int arch_sdt_arg_parse_op(char *old_op, char **new_op) 115 { 116 int ret, new_len; 117 regmatch_t rm[5]; 118 char prefix; 119 120 /* Constant argument. Uprobe does not support it */ 121 if (old_op[0] == 'i') { 122 pr_debug4("Skipping unsupported SDT argument: %s\n", old_op); 123 return SDT_ARG_SKIP; 124 } 125 126 ret = sdt_init_op_regex(); 127 if (ret < 0) 128 return ret; 129 130 if (!regexec(&sdt_op_regex1, old_op, 3, rm, 0)) { 131 /* REG or %rREG --> %gprREG */ 132 133 new_len = 5; /* % g p r NULL */ 134 new_len += (int)(rm[2].rm_eo - rm[2].rm_so); 135 136 *new_op = zalloc(new_len); 137 if (!*new_op) 138 return -ENOMEM; 139 140 scnprintf(*new_op, new_len, "%%gpr%.*s", 141 (int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so); 142 } else if (!regexec(&sdt_op_regex2, old_op, 5, rm, 0)) { 143 /* 144 * -NUM(REG) or NUM(REG) or -NUM(%rREG) or NUM(%rREG) --> 145 * +/-NUM(%gprREG) 146 */ 147 prefix = (rm[1].rm_so == -1) ? '+' : '-'; 148 149 new_len = 8; /* +/- ( % g p r ) NULL */ 150 new_len += (int)(rm[2].rm_eo - rm[2].rm_so); 151 new_len += (int)(rm[4].rm_eo - rm[4].rm_so); 152 153 *new_op = zalloc(new_len); 154 if (!*new_op) 155 return -ENOMEM; 156 157 scnprintf(*new_op, new_len, "%c%.*s(%%gpr%.*s)", prefix, 158 (int)(rm[2].rm_eo - rm[2].rm_so), old_op + rm[2].rm_so, 159 (int)(rm[4].rm_eo - rm[4].rm_so), old_op + rm[4].rm_so); 160 } else { 161 pr_debug4("Skipping unsupported SDT argument: %s\n", old_op); 162 return SDT_ARG_SKIP; 163 } 164 165 return SDT_ARG_VALID; 166 } 167