1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Traceprobe fetch helper inlines 4 */ 5 6 static nokprobe_inline void 7 fetch_store_raw(unsigned long val, struct fetch_insn *code, void *buf) 8 { 9 switch (code->size) { 10 case 1: 11 *(u8 *)buf = (u8)val; 12 break; 13 case 2: 14 *(u16 *)buf = (u16)val; 15 break; 16 case 4: 17 *(u32 *)buf = (u32)val; 18 break; 19 case 8: 20 //TBD: 32bit signed 21 *(u64 *)buf = (u64)val; 22 break; 23 default: 24 *(unsigned long *)buf = val; 25 } 26 } 27 28 static nokprobe_inline void 29 fetch_apply_bitfield(struct fetch_insn *code, void *buf) 30 { 31 switch (code->basesize) { 32 case 1: 33 *(u8 *)buf <<= code->lshift; 34 *(u8 *)buf >>= code->rshift; 35 break; 36 case 2: 37 *(u16 *)buf <<= code->lshift; 38 *(u16 *)buf >>= code->rshift; 39 break; 40 case 4: 41 *(u32 *)buf <<= code->lshift; 42 *(u32 *)buf >>= code->rshift; 43 break; 44 case 8: 45 *(u64 *)buf <<= code->lshift; 46 *(u64 *)buf >>= code->rshift; 47 break; 48 } 49 } 50 51 /* 52 * These functions must be defined for each callsite. 53 * Return consumed dynamic data size (>= 0), or error (< 0). 54 * If dest is NULL, don't store result and return required dynamic data size. 55 */ 56 static int 57 process_fetch_insn(struct fetch_insn *code, void *rec, void *edata, 58 void *dest, void *base); 59 static nokprobe_inline int fetch_store_strlen(unsigned long addr); 60 static nokprobe_inline int 61 fetch_store_string(unsigned long addr, void *dest, void *base); 62 static nokprobe_inline int fetch_store_strlen_user(unsigned long addr); 63 static nokprobe_inline int 64 fetch_store_string_user(unsigned long addr, void *dest, void *base); 65 static nokprobe_inline int 66 probe_mem_read(void *dest, void *src, size_t size); 67 static nokprobe_inline int 68 probe_mem_read_user(void *dest, void *src, size_t size); 69 70 static nokprobe_inline int 71 fetch_store_symstrlen(unsigned long addr) 72 { 73 char namebuf[KSYM_SYMBOL_LEN]; 74 int ret; 75 76 ret = sprint_symbol(namebuf, addr); 77 if (ret < 0) 78 return 0; 79 80 return ret + 1; 81 } 82 83 /* 84 * Fetch a null-terminated symbol string + offset. Caller MUST set *(u32 *)buf 85 * with max length and relative data location. 86 */ 87 static nokprobe_inline int 88 fetch_store_symstring(unsigned long addr, void *dest, void *base) 89 { 90 char namebuf[KSYM_SYMBOL_LEN]; 91 int maxlen = get_loc_len(*(u32 *)dest); 92 void *__dest; 93 int len; 94 95 if (unlikely(!maxlen)) 96 return -ENOMEM; 97 98 __dest = get_loc_data(dest, base); 99 len = sprint_symbol(namebuf, addr); 100 if (len < 0) { 101 *(u32 *)dest = make_data_loc(0, __dest - base); 102 return 0; 103 } 104 105 len = min(len, maxlen - 1); 106 memcpy(__dest, namebuf, len); 107 ((char *)__dest)[len] = '\0'; 108 *(u32 *)dest = make_data_loc(len + 1, __dest - base); 109 110 return len + 1; 111 } 112 113 /* common part of process_fetch_insn*/ 114 static nokprobe_inline int 115 process_common_fetch_insn(struct fetch_insn *code, unsigned long *val) 116 { 117 switch (code->op) { 118 case FETCH_OP_IMM: 119 *val = code->immediate; 120 break; 121 case FETCH_OP_COMM: 122 *val = (unsigned long)current->comm; 123 break; 124 case FETCH_OP_IMMSTR: 125 *val = (unsigned long)code->data; 126 break; 127 case FETCH_OP_CURRENT: 128 *val = (unsigned long)current; 129 break; 130 default: 131 return -EILSEQ; 132 } 133 return 0; 134 } 135 136 /* From the 2nd stage, routine is same */ 137 static nokprobe_inline int 138 process_fetch_insn_bottom(struct fetch_insn *code, unsigned long val, 139 void *dest, void *base) 140 { 141 struct fetch_insn *s3 = NULL; 142 int total = 0, ret = 0, i = 0; 143 u32 loc = 0; 144 unsigned long lval, llval = val; 145 146 stage2: 147 /* 2nd stage: dereference memory if needed */ 148 do { 149 lval = val; 150 switch (code->op) { 151 case FETCH_OP_DEREF: 152 ret = probe_mem_read(&val, (void *)val + code->offset, 153 sizeof(val)); 154 break; 155 case FETCH_OP_UDEREF: 156 ret = probe_mem_read_user(&val, 157 (void *)val + code->offset, sizeof(val)); 158 break; 159 case FETCH_OP_CPU_PTR: 160 val = (unsigned long)this_cpu_ptr((void __percpu *)val); 161 ret = 0; 162 break; 163 default: 164 lval = llval; 165 goto out; 166 } 167 if (ret) 168 return ret; 169 llval = lval; 170 code++; 171 } while (1); 172 out: 173 174 s3 = code; 175 stage3: 176 /* 3rd stage: store value to buffer */ 177 if (unlikely(!dest)) { 178 switch (code->op) { 179 case FETCH_OP_ST_STRING: 180 ret = fetch_store_strlen(val + code->offset); 181 code++; 182 goto array; 183 case FETCH_OP_ST_USTRING: 184 ret = fetch_store_strlen_user(val + code->offset); 185 code++; 186 goto array; 187 case FETCH_OP_ST_SYMSTR: 188 ret = fetch_store_symstrlen(val + code->offset); 189 code++; 190 goto array; 191 default: 192 return -EILSEQ; 193 } 194 } 195 196 switch (code->op) { 197 case FETCH_OP_ST_RAW: 198 fetch_store_raw(val, code, dest); 199 break; 200 case FETCH_OP_ST_MEM: 201 probe_mem_read(dest, (void *)val + code->offset, code->size); 202 break; 203 case FETCH_OP_ST_UMEM: 204 probe_mem_read_user(dest, (void *)val + code->offset, code->size); 205 break; 206 case FETCH_OP_ST_STRING: 207 loc = *(u32 *)dest; 208 ret = fetch_store_string(val + code->offset, dest, base); 209 break; 210 case FETCH_OP_ST_USTRING: 211 loc = *(u32 *)dest; 212 ret = fetch_store_string_user(val + code->offset, dest, base); 213 break; 214 case FETCH_OP_ST_SYMSTR: 215 loc = *(u32 *)dest; 216 ret = fetch_store_symstring(val + code->offset, dest, base); 217 break; 218 default: 219 return -EILSEQ; 220 } 221 code++; 222 223 /* 4th stage: modify stored value if needed */ 224 if (code->op == FETCH_OP_MOD_BF) { 225 fetch_apply_bitfield(code, dest); 226 code++; 227 } 228 229 array: 230 /* the last stage: Loop on array */ 231 if (code->op == FETCH_OP_LP_ARRAY) { 232 if (ret < 0) 233 ret = 0; 234 total += ret; 235 if (++i < code->param) { 236 code = s3; 237 if (s3->op != FETCH_OP_ST_STRING && 238 s3->op != FETCH_OP_ST_USTRING) { 239 dest += s3->size; 240 val += s3->size; 241 goto stage3; 242 } 243 code--; 244 val = lval + sizeof(char *); 245 if (dest) { 246 dest += sizeof(u32); 247 *(u32 *)dest = update_data_loc(loc, ret); 248 } 249 goto stage2; 250 } 251 code++; 252 ret = total; 253 } 254 255 return code->op == FETCH_OP_END ? ret : -EILSEQ; 256 } 257 258 /* Sum up total data length for dynamic arrays (strings) */ 259 static nokprobe_inline int 260 __get_data_size(struct trace_probe *tp, void *regs, void *edata) 261 { 262 struct probe_arg *arg; 263 int i, len, ret = 0; 264 265 for (i = 0; i < tp->nr_args; i++) { 266 arg = tp->args + i; 267 if (unlikely(arg->dynamic)) { 268 len = process_fetch_insn(arg->code, regs, edata, NULL, NULL); 269 if (len > 0) 270 ret += len; 271 } 272 } 273 274 return ret; 275 } 276 277 /* Store the value of each argument */ 278 static nokprobe_inline void 279 store_trace_args(void *data, struct trace_probe *tp, void *rec, void *edata, 280 int header_size, int maxlen) 281 { 282 struct probe_arg *arg; 283 void *base = data - header_size; 284 void *dyndata = data + tp->size; 285 u32 *dl; /* Data location */ 286 int ret, i; 287 288 for (i = 0; i < tp->nr_args; i++) { 289 arg = tp->args + i; 290 dl = data + arg->offset; 291 /* Point the dynamic data area if needed */ 292 if (unlikely(arg->dynamic)) 293 *dl = make_data_loc(maxlen, dyndata - base); 294 ret = process_fetch_insn(arg->code, rec, edata, dl, base); 295 if (arg->dynamic && likely(ret > 0)) { 296 dyndata += ret; 297 maxlen -= ret; 298 } 299 } 300 } 301