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>
12 #include "bpf-loader.h"
13 #include "bpf-prologue.h"
14 #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
,
148 /* mov arg3, src_base_addr_reg */
149 if (src_base_addr_reg
!= BPF_REG_ARG3
)
150 ins(BPF_MOV64_REG(BPF_REG_ARG3
, src_base_addr_reg
), pos
);
151 /* add arg3, #offset */
153 ins(BPF_ALU64_IMM(BPF_ADD
, BPF_REG_ARG3
, offset
), pos
);
155 /* mov arg2, #reg_size */
156 ins(BPF_ALU64_IMM(BPF_MOV
, BPF_REG_ARG2
, BPF_REG_SIZE
), pos
);
158 /* mov arg1, dst_addr_reg */
159 if (dst_addr_reg
!= BPF_REG_ARG1
)
160 ins(BPF_MOV64_REG(BPF_REG_ARG1
, dst_addr_reg
), pos
);
162 /* Call probe_read */
163 ins(BPF_EMIT_CALL(probeid
), pos
);
165 * Error processing: if read fail, goto error code,
166 * will be relocated. Target should be the start of
167 * error processing code.
169 ins(BPF_JMP_IMM(BPF_JNE
, BPF_REG_0
, 0, JMP_TO_ERROR_CODE
),
172 return check_pos(pos
);
176 * Each arg should be bare register. Fetch and save them into argument
177 * registers (r3 - r5).
179 * BPF_REG_1 should have been initialized with pointer to
183 gen_prologue_fastpath(struct bpf_insn_pos
*pos
,
184 struct probe_trace_arg
*args
, int nargs
)
188 for (i
= 0; i
< nargs
; i
++) {
189 err
= gen_ldx_reg_from_ctx(pos
, BPF_REG_1
, args
[i
].value
,
190 BPF_PROLOGUE_START_ARG_REG
+ i
);
195 return check_pos(pos
);
202 * At least one argument has the form of 'offset($rx)'.
204 * Following code first stores them into stack, then loads all of then
206 * Before final loading, the final result should be:
209 * BPF_REG_FP - 24 ARG3
210 * BPF_REG_FP - 16 ARG2
211 * BPF_REG_FP - 8 ARG1
215 * For each argument (described as: offn(...off2(off1(reg)))),
216 * generates following code:
219 * r7 <- r7 - stack_offset // Ideal code should initialize r7 using
220 * // fp before generating args. However,
221 * // eBPF won't regard r7 as stack pointer
222 * // if it is generated by minus 8 from
223 * // another stack pointer except fp.
224 * // This is why we have to set r7
225 * // to fp for each variable.
226 * r3 <- value of 'reg'-> generated using gen_ldx_reg_from_ctx()
227 * (r7) <- r3 // skip following instructions for bare reg
228 * r3 <- r3 + off1 . // skip if off1 == 0
230 * r1 <- r7 |-> generated by gen_read_mem()
234 * r3 <- r3 + off2 . // skip if off2 == 0
235 * r2 <- 8 \ // r2 may be broken by probe_read, so set again
236 * r1 <- r7 |-> generated by gen_read_mem()
242 gen_prologue_slowpath(struct bpf_insn_pos
*pos
,
243 struct probe_trace_arg
*args
, int nargs
)
247 for (i
= 0; i
< nargs
; i
++) {
248 struct probe_trace_arg
*arg
= &args
[i
];
249 const char *reg
= arg
->value
;
250 struct probe_trace_arg_ref
*ref
= NULL
;
251 int stack_offset
= (i
+ 1) * -8;
253 pr_debug("prologue: fetch arg %d, base reg is %s\n",
256 /* value of base register is stored into ARG3 */
257 err
= gen_ldx_reg_from_ctx(pos
, BPF_REG_CTX
, reg
,
260 pr_err("prologue: failed to get offset of register %s\n",
265 /* Make r7 the stack pointer. */
266 ins(BPF_MOV64_REG(BPF_REG_7
, BPF_REG_FP
), pos
);
268 ins(BPF_ALU64_IMM(BPF_ADD
, BPF_REG_7
, stack_offset
), pos
);
270 * Store r3 (base register) onto stack
271 * Ensure fp[offset] is set.
272 * fp is the only valid base register when storing
273 * into stack. We are not allowed to use r7 as base
276 ins(BPF_STX_MEM(BPF_DW
, BPF_REG_FP
, BPF_REG_ARG3
,
280 probeid
= BPF_FUNC_probe_read_kernel
;
282 pr_debug("prologue: arg %d: offset %ld\n",
285 if (ref
->user_access
)
286 probeid
= BPF_FUNC_probe_read_user
;
288 err
= gen_read_mem(pos
, BPF_REG_3
, BPF_REG_7
,
289 ref
->offset
, probeid
);
291 pr_err("prologue: failed to generate probe_read function call\n");
297 * Load previous result into ARG3. Use
298 * BPF_REG_FP instead of r7 because verifier
299 * allows FP based addressing only.
302 ins(BPF_LDX_MEM(BPF_DW
, BPF_REG_ARG3
,
303 BPF_REG_FP
, stack_offset
), pos
);
307 /* Final pass: read to registers */
308 for (i
= 0; i
< nargs
; i
++) {
309 int insn_sz
= (args
[i
].ref
) ? argtype_to_ldx_size(args
[i
].type
) : BPF_DW
;
311 pr_debug("prologue: load arg %d, insn_sz is %s\n",
312 i
, insn_sz_to_str(insn_sz
));
313 ins(BPF_LDX_MEM(insn_sz
, BPF_PROLOGUE_START_ARG_REG
+ i
,
314 BPF_REG_FP
, -BPF_REG_SIZE
* (i
+ 1)), pos
);
317 ins(BPF_JMP_IMM(BPF_JA
, BPF_REG_0
, 0, JMP_TO_SUCCESS_CODE
), pos
);
319 return check_pos(pos
);
325 prologue_relocate(struct bpf_insn_pos
*pos
, struct bpf_insn
*error_code
,
326 struct bpf_insn
*success_code
, struct bpf_insn
*user_code
)
328 struct bpf_insn
*insn
;
331 return -BPF_LOADER_ERRNO__PROLOGUE2BIG
;
333 for (insn
= pos
->begin
; insn
< pos
->pos
; insn
++) {
334 struct bpf_insn
*target
;
335 u8
class = BPF_CLASS(insn
->code
);
338 if (class != BPF_JMP
)
340 opcode
= BPF_OP(insn
->code
);
341 if (opcode
== BPF_CALL
)
345 case JMP_TO_ERROR_CODE
:
348 case JMP_TO_SUCCESS_CODE
:
349 target
= success_code
;
351 case JMP_TO_USER_CODE
:
355 pr_err("bpf prologue: internal error: relocation failed\n");
356 return -BPF_LOADER_ERRNO__PROLOGUE
;
359 insn
->off
= target
- (insn
+ 1);
364 int bpf__gen_prologue(struct probe_trace_arg
*args
, int nargs
,
365 struct bpf_insn
*new_prog
, size_t *new_cnt
,
368 struct bpf_insn
*success_code
= NULL
;
369 struct bpf_insn
*error_code
= NULL
;
370 struct bpf_insn
*user_code
= NULL
;
371 struct bpf_insn_pos pos
;
372 bool fastpath
= true;
375 if (!new_prog
|| !new_cnt
)
378 if (cnt_space
> BPF_MAXINSNS
)
379 cnt_space
= BPF_MAXINSNS
;
381 pos
.begin
= new_prog
;
382 pos
.end
= new_prog
+ cnt_space
;
386 ins(BPF_ALU64_IMM(BPF_MOV
, BPF_PROLOGUE_FETCH_RESULT_REG
, 0),
392 *new_cnt
= pos_get_cnt(&pos
);
396 if (nargs
> BPF_PROLOGUE_MAX_ARGS
) {
397 pr_warning("bpf: prologue: %d arguments are dropped\n",
398 nargs
- BPF_PROLOGUE_MAX_ARGS
);
399 nargs
= BPF_PROLOGUE_MAX_ARGS
;
402 /* First pass: validation */
403 for (i
= 0; i
< nargs
; i
++) {
404 struct probe_trace_arg_ref
*ref
= args
[i
].ref
;
406 if (args
[i
].value
[0] == '@') {
407 /* TODO: fetch global variable */
408 pr_err("bpf: prologue: global %s%+ld not support\n",
409 args
[i
].value
, ref
? ref
->offset
: 0);
414 /* fastpath is true if all args has ref == NULL */
418 * Instruction encodes immediate value using
419 * s32, ref->offset is long. On systems which
420 * can't fill long in s32, refuse to process if
421 * ref->offset too large (or small).
424 #define OFFSET_MAX ((1LL << 31) - 1)
425 #define OFFSET_MIN ((1LL << 31) * -1)
426 if (ref
->offset
> OFFSET_MAX
||
427 ref
->offset
< OFFSET_MIN
) {
428 pr_err("bpf: prologue: offset out of bound: %ld\n",
430 return -BPF_LOADER_ERRNO__PROLOGUEOOB
;
436 pr_debug("prologue: pass validation\n");
439 /* If all variables are registers... */
440 pr_debug("prologue: fast path\n");
441 err
= gen_prologue_fastpath(&pos
, args
, nargs
);
445 pr_debug("prologue: slow path\n");
447 /* Initialization: move ctx to a callee saved register. */
448 ins(BPF_MOV64_REG(BPF_REG_CTX
, BPF_REG_ARG1
), &pos
);
450 err
= gen_prologue_slowpath(&pos
, args
, nargs
);
454 * start of ERROR_CODE (only slow pass needs error code)
455 * mov r2 <- 1 // r2 is error number
456 * mov r3 <- 0 // r3, r4... should be touched or
457 * // verifier would complain
462 error_code
= pos
.pos
;
463 ins(BPF_ALU64_IMM(BPF_MOV
, BPF_PROLOGUE_FETCH_RESULT_REG
, 1),
466 for (i
= 0; i
< nargs
; i
++)
467 ins(BPF_ALU64_IMM(BPF_MOV
,
468 BPF_PROLOGUE_START_ARG_REG
+ i
,
471 ins(BPF_JMP_IMM(BPF_JA
, BPF_REG_0
, 0, JMP_TO_USER_CODE
),
476 * start of SUCCESS_CODE:
478 * goto usercode // skip
480 success_code
= pos
.pos
;
481 ins(BPF_ALU64_IMM(BPF_MOV
, BPF_PROLOGUE_FETCH_RESULT_REG
, 0), &pos
);
484 * start of USER_CODE:
490 * Only slow path needs restoring of ctx. In fast path,
491 * register are loaded directly from r1.
493 ins(BPF_MOV64_REG(BPF_REG_ARG1
, BPF_REG_CTX
), &pos
);
494 err
= prologue_relocate(&pos
, error_code
, success_code
,
500 err
= check_pos(&pos
);
504 *new_cnt
= pos_get_cnt(&pos
);