1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2015 He Kuang <hekuang@huawei.com>
6 * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
7 * Copyright (C) 2015 Huawei Inc.
10 #include <bpf/libbpf.h>
13 #include "bpf-loader.h"
14 #include "bpf-prologue.h"
15 #include "probe-finder.h"
17 #include <dwarf-regs.h>
18 #include <linux/filter.h>
20 #define BPF_REG_SIZE 8
22 #define JMP_TO_ERROR_CODE -1
23 #define JMP_TO_SUCCESS_CODE -2
24 #define JMP_TO_USER_CODE -3
27 struct bpf_insn *begin;
33 pos_get_cnt(struct bpf_insn_pos *pos)
35 return pos->pos - pos->begin;
39 append_insn(struct bpf_insn new_insn, struct bpf_insn_pos *pos)
42 return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
44 if (pos->pos + 1 >= pos->end) {
45 pr_err("bpf prologue: prologue too long\n");
47 return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
50 *(pos->pos)++ = new_insn;
55 check_pos(struct bpf_insn_pos *pos)
57 if (!pos->pos || pos->pos >= pos->end)
58 return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
63 * Convert type string (u8/u16/u32/u64/s8/s16/s32/s64 ..., see
64 * Documentation/trace/kprobetrace.rst) to size field of BPF_LDX_MEM
65 * instruction (BPF_{B,H,W,DW}).
68 argtype_to_ldx_size(const char *type)
70 int arg_size = type ? atoi(&type[1]) : 64;
86 insn_sz_to_str(int insn_sz)
102 /* Give it a shorter name */
103 #define ins(i, p) append_insn((i), (p))
106 * Give a register name (in 'reg'), generate instruction to
107 * load register into an eBPF register rd:
108 * 'ldd target_reg, offset(ctx_reg)', where:
109 * ctx_reg is pre initialized to pointer of 'struct pt_regs'.
112 gen_ldx_reg_from_ctx(struct bpf_insn_pos *pos, int ctx_reg,
113 const char *reg, int target_reg)
115 int offset = regs_query_register_offset(reg);
118 pr_err("bpf: prologue: failed to get register %s\n",
122 ins(BPF_LDX_MEM(BPF_DW, target_reg, ctx_reg, offset), pos);
124 return check_pos(pos);
128 * Generate a BPF_FUNC_probe_read function call.
130 * src_base_addr_reg is a register holding base address,
131 * dst_addr_reg is a register holding dest address (on stack),
134 * *[dst_addr_reg] = *([src_base_addr_reg] + offset)
136 * Arguments of BPF_FUNC_probe_read:
137 * ARG1: ptr to stack (dest)
139 * ARG3: unsafe ptr (src)
142 gen_read_mem(struct bpf_insn_pos *pos,
143 int src_base_addr_reg,
147 /* mov arg3, src_base_addr_reg */
148 if (src_base_addr_reg != BPF_REG_ARG3)
149 ins(BPF_MOV64_REG(BPF_REG_ARG3, src_base_addr_reg), pos);
150 /* add arg3, #offset */
152 ins(BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG3, offset), pos);
154 /* mov arg2, #reg_size */
155 ins(BPF_ALU64_IMM(BPF_MOV, BPF_REG_ARG2, BPF_REG_SIZE), pos);
157 /* mov arg1, dst_addr_reg */
158 if (dst_addr_reg != BPF_REG_ARG1)
159 ins(BPF_MOV64_REG(BPF_REG_ARG1, dst_addr_reg), pos);
161 /* Call probe_read */
162 ins(BPF_EMIT_CALL(BPF_FUNC_probe_read), pos);
164 * Error processing: if read fail, goto error code,
165 * will be relocated. Target should be the start of
166 * error processing code.
168 ins(BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, JMP_TO_ERROR_CODE),
171 return check_pos(pos);
175 * Each arg should be bare register. Fetch and save them into argument
176 * registers (r3 - r5).
178 * BPF_REG_1 should have been initialized with pointer to
182 gen_prologue_fastpath(struct bpf_insn_pos *pos,
183 struct probe_trace_arg *args, int nargs)
187 for (i = 0; i < nargs; i++) {
188 err = gen_ldx_reg_from_ctx(pos, BPF_REG_1, args[i].value,
189 BPF_PROLOGUE_START_ARG_REG + i);
194 return check_pos(pos);
201 * At least one argument has the form of 'offset($rx)'.
203 * Following code first stores them into stack, then loads all of then
205 * Before final loading, the final result should be:
208 * BPF_REG_FP - 24 ARG3
209 * BPF_REG_FP - 16 ARG2
210 * BPF_REG_FP - 8 ARG1
214 * For each argument (described as: offn(...off2(off1(reg)))),
215 * generates following code:
218 * r7 <- r7 - stack_offset // Ideal code should initialize r7 using
219 * // fp before generating args. However,
220 * // eBPF won't regard r7 as stack pointer
221 * // if it is generated by minus 8 from
222 * // another stack pointer except fp.
223 * // This is why we have to set r7
224 * // to fp for each variable.
225 * r3 <- value of 'reg'-> generated using gen_ldx_reg_from_ctx()
226 * (r7) <- r3 // skip following instructions for bare reg
227 * r3 <- r3 + off1 . // skip if off1 == 0
229 * r1 <- r7 |-> generated by gen_read_mem()
233 * r3 <- r3 + off2 . // skip if off2 == 0
234 * r2 <- 8 \ // r2 may be broken by probe_read, so set again
235 * r1 <- r7 |-> generated by gen_read_mem()
241 gen_prologue_slowpath(struct bpf_insn_pos *pos,
242 struct probe_trace_arg *args, int nargs)
246 for (i = 0; i < nargs; i++) {
247 struct probe_trace_arg *arg = &args[i];
248 const char *reg = arg->value;
249 struct probe_trace_arg_ref *ref = NULL;
250 int stack_offset = (i + 1) * -8;
252 pr_debug("prologue: fetch arg %d, base reg is %s\n",
255 /* value of base register is stored into ARG3 */
256 err = gen_ldx_reg_from_ctx(pos, BPF_REG_CTX, reg,
259 pr_err("prologue: failed to get offset of register %s\n",
264 /* Make r7 the stack pointer. */
265 ins(BPF_MOV64_REG(BPF_REG_7, BPF_REG_FP), pos);
267 ins(BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, stack_offset), pos);
269 * Store r3 (base register) onto stack
270 * Ensure fp[offset] is set.
271 * fp is the only valid base register when storing
272 * into stack. We are not allowed to use r7 as base
275 ins(BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_ARG3,
280 pr_debug("prologue: arg %d: offset %ld\n",
282 err = gen_read_mem(pos, BPF_REG_3, BPF_REG_7,
285 pr_err("prologue: failed to generate probe_read function call\n");
291 * Load previous result into ARG3. Use
292 * BPF_REG_FP instead of r7 because verifier
293 * allows FP based addressing only.
296 ins(BPF_LDX_MEM(BPF_DW, BPF_REG_ARG3,
297 BPF_REG_FP, stack_offset), pos);
301 /* Final pass: read to registers */
302 for (i = 0; i < nargs; i++) {
303 int insn_sz = (args[i].ref) ? argtype_to_ldx_size(args[i].type) : BPF_DW;
305 pr_debug("prologue: load arg %d, insn_sz is %s\n",
306 i, insn_sz_to_str(insn_sz));
307 ins(BPF_LDX_MEM(insn_sz, BPF_PROLOGUE_START_ARG_REG + i,
308 BPF_REG_FP, -BPF_REG_SIZE * (i + 1)), pos);
311 ins(BPF_JMP_IMM(BPF_JA, BPF_REG_0, 0, JMP_TO_SUCCESS_CODE), pos);
313 return check_pos(pos);
319 prologue_relocate(struct bpf_insn_pos *pos, struct bpf_insn *error_code,
320 struct bpf_insn *success_code, struct bpf_insn *user_code)
322 struct bpf_insn *insn;
325 return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
327 for (insn = pos->begin; insn < pos->pos; insn++) {
328 struct bpf_insn *target;
329 u8 class = BPF_CLASS(insn->code);
332 if (class != BPF_JMP)
334 opcode = BPF_OP(insn->code);
335 if (opcode == BPF_CALL)
339 case JMP_TO_ERROR_CODE:
342 case JMP_TO_SUCCESS_CODE:
343 target = success_code;
345 case JMP_TO_USER_CODE:
349 pr_err("bpf prologue: internal error: relocation failed\n");
350 return -BPF_LOADER_ERRNO__PROLOGUE;
353 insn->off = target - (insn + 1);
358 int bpf__gen_prologue(struct probe_trace_arg *args, int nargs,
359 struct bpf_insn *new_prog, size_t *new_cnt,
362 struct bpf_insn *success_code = NULL;
363 struct bpf_insn *error_code = NULL;
364 struct bpf_insn *user_code = NULL;
365 struct bpf_insn_pos pos;
366 bool fastpath = true;
369 if (!new_prog || !new_cnt)
372 if (cnt_space > BPF_MAXINSNS)
373 cnt_space = BPF_MAXINSNS;
375 pos.begin = new_prog;
376 pos.end = new_prog + cnt_space;
380 ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 0),
386 *new_cnt = pos_get_cnt(&pos);
390 if (nargs > BPF_PROLOGUE_MAX_ARGS) {
391 pr_warning("bpf: prologue: %d arguments are dropped\n",
392 nargs - BPF_PROLOGUE_MAX_ARGS);
393 nargs = BPF_PROLOGUE_MAX_ARGS;
396 /* First pass: validation */
397 for (i = 0; i < nargs; i++) {
398 struct probe_trace_arg_ref *ref = args[i].ref;
400 if (args[i].value[0] == '@') {
401 /* TODO: fetch global variable */
402 pr_err("bpf: prologue: global %s%+ld not support\n",
403 args[i].value, ref ? ref->offset : 0);
408 /* fastpath is true if all args has ref == NULL */
412 * Instruction encodes immediate value using
413 * s32, ref->offset is long. On systems which
414 * can't fill long in s32, refuse to process if
415 * ref->offset too large (or small).
418 #define OFFSET_MAX ((1LL << 31) - 1)
419 #define OFFSET_MIN ((1LL << 31) * -1)
420 if (ref->offset > OFFSET_MAX ||
421 ref->offset < OFFSET_MIN) {
422 pr_err("bpf: prologue: offset out of bound: %ld\n",
424 return -BPF_LOADER_ERRNO__PROLOGUEOOB;
430 pr_debug("prologue: pass validation\n");
433 /* If all variables are registers... */
434 pr_debug("prologue: fast path\n");
435 err = gen_prologue_fastpath(&pos, args, nargs);
439 pr_debug("prologue: slow path\n");
441 /* Initialization: move ctx to a callee saved register. */
442 ins(BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1), &pos);
444 err = gen_prologue_slowpath(&pos, args, nargs);
448 * start of ERROR_CODE (only slow pass needs error code)
449 * mov r2 <- 1 // r2 is error number
450 * mov r3 <- 0 // r3, r4... should be touched or
451 * // verifier would complain
456 error_code = pos.pos;
457 ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 1),
460 for (i = 0; i < nargs; i++)
461 ins(BPF_ALU64_IMM(BPF_MOV,
462 BPF_PROLOGUE_START_ARG_REG + i,
465 ins(BPF_JMP_IMM(BPF_JA, BPF_REG_0, 0, JMP_TO_USER_CODE),
470 * start of SUCCESS_CODE:
472 * goto usercode // skip
474 success_code = pos.pos;
475 ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 0), &pos);
478 * start of USER_CODE:
484 * Only slow path needs restoring of ctx. In fast path,
485 * register are loaded directly from r1.
487 ins(BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX), &pos);
488 err = prologue_relocate(&pos, error_code, success_code,
494 err = check_pos(&pos);
498 *new_cnt = pos_get_cnt(&pos);