Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * bpf-prologue.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2015 He Kuang <hekuang@huawei.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2015 Huawei Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "bpf-loader.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "bpf-prologue.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "probe-finder.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <dwarf-regs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/filter.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define BPF_REG_SIZE		8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define JMP_TO_ERROR_CODE	-1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define JMP_TO_SUCCESS_CODE	-2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define JMP_TO_USER_CODE	-3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct bpf_insn_pos {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct bpf_insn *begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct bpf_insn *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct bpf_insn *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) pos_get_cnt(struct bpf_insn_pos *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return pos->pos - pos->begin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) append_insn(struct bpf_insn new_insn, struct bpf_insn_pos *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (!pos->pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (pos->pos + 1 >= pos->end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		pr_err("bpf prologue: prologue too long\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		pos->pos = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	*(pos->pos)++ = new_insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) check_pos(struct bpf_insn_pos *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!pos->pos || pos->pos >= pos->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * Convert type string (u8/u16/u32/u64/s8/s16/s32/s64 ..., see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * Documentation/trace/kprobetrace.rst) to size field of BPF_LDX_MEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * instruction (BPF_{B,H,W,DW}).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) argtype_to_ldx_size(const char *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int arg_size = type ? atoi(&type[1]) : 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	switch (arg_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return BPF_B;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return BPF_H;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	case 32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return BPF_W;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	case 64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return BPF_DW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) insn_sz_to_str(int insn_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	switch (insn_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	case BPF_B:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		return "BPF_B";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	case BPF_H:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return "BPF_H";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	case BPF_W:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		return "BPF_W";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	case BPF_DW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return "BPF_DW";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return "UNKNOWN";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* Give it a shorter name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define ins(i, p) append_insn((i), (p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * Give a register name (in 'reg'), generate instruction to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * load register into an eBPF register rd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  *   'ldd target_reg, offset(ctx_reg)', where:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * ctx_reg is pre initialized to pointer of 'struct pt_regs'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) gen_ldx_reg_from_ctx(struct bpf_insn_pos *pos, int ctx_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		     const char *reg, int target_reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int offset = regs_query_register_offset(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (offset < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		pr_err("bpf: prologue: failed to get register %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		       reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	ins(BPF_LDX_MEM(BPF_DW, target_reg, ctx_reg, offset), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return check_pos(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * Generate a BPF_FUNC_probe_read function call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  * src_base_addr_reg is a register holding base address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * dst_addr_reg is a register holding dest address (on stack),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * result is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  *  *[dst_addr_reg] = *([src_base_addr_reg] + offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * Arguments of BPF_FUNC_probe_read:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  *     ARG1: ptr to stack (dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  *     ARG2: size (8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  *     ARG3: unsafe ptr (src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) gen_read_mem(struct bpf_insn_pos *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	     int src_base_addr_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	     int dst_addr_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	     long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	     int probeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* mov arg3, src_base_addr_reg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (src_base_addr_reg != BPF_REG_ARG3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		ins(BPF_MOV64_REG(BPF_REG_ARG3, src_base_addr_reg), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	/* add arg3, #offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		ins(BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG3, offset), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	/* mov arg2, #reg_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	ins(BPF_ALU64_IMM(BPF_MOV, BPF_REG_ARG2, BPF_REG_SIZE), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	/* mov arg1, dst_addr_reg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (dst_addr_reg != BPF_REG_ARG1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		ins(BPF_MOV64_REG(BPF_REG_ARG1, dst_addr_reg), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	/* Call probe_read  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	ins(BPF_EMIT_CALL(probeid), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	 * Error processing: if read fail, goto error code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * will be relocated. Target should be the start of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 * error processing code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	ins(BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, JMP_TO_ERROR_CODE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	    pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return check_pos(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * Each arg should be bare register. Fetch and save them into argument
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * registers (r3 - r5).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * BPF_REG_1 should have been initialized with pointer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * 'struct pt_regs'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) gen_prologue_fastpath(struct bpf_insn_pos *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		      struct probe_trace_arg *args, int nargs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	for (i = 0; i < nargs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		err = gen_ldx_reg_from_ctx(pos, BPF_REG_1, args[i].value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 					   BPF_PROLOGUE_START_ARG_REG + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return check_pos(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  * Slow path:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  *   At least one argument has the form of 'offset($rx)'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * Following code first stores them into stack, then loads all of then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * to r2 - r5.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * Before final loading, the final result should be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * low address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  * BPF_REG_FP - 24  ARG3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * BPF_REG_FP - 16  ARG2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * BPF_REG_FP - 8   ARG1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  * BPF_REG_FP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * high address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  * For each argument (described as: offn(...off2(off1(reg)))),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * generates following code:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  *  r7 <- fp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  *  r7 <- r7 - stack_offset  // Ideal code should initialize r7 using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  *                           // fp before generating args. However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  *                           // eBPF won't regard r7 as stack pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  *                           // if it is generated by minus 8 from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  *                           // another stack pointer except fp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  *                           // This is why we have to set r7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  *                           // to fp for each variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  *  r3 <- value of 'reg'-> generated using gen_ldx_reg_from_ctx()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  *  (r7) <- r3       // skip following instructions for bare reg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  *  r3 <- r3 + off1  . // skip if off1 == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  *  r2 <- 8           \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  *  r1 <- r7           |-> generated by gen_read_mem()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  *  call probe_read    /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  *  jnei r0, 0, err  ./
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  *  r3 <- (r7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  *  r3 <- r3 + off2  . // skip if off2 == 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  *  r2 <- 8           \  // r2 may be broken by probe_read, so set again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  *  r1 <- r7           |-> generated by gen_read_mem()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  *  call probe_read    /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  *  jnei r0, 0, err  ./
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  *  ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) gen_prologue_slowpath(struct bpf_insn_pos *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		      struct probe_trace_arg *args, int nargs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	int err, i, probeid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	for (i = 0; i < nargs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		struct probe_trace_arg *arg = &args[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		const char *reg = arg->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		struct probe_trace_arg_ref *ref = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		int stack_offset = (i + 1) * -8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		pr_debug("prologue: fetch arg %d, base reg is %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			 i, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		/* value of base register is stored into ARG3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		err = gen_ldx_reg_from_ctx(pos, BPF_REG_CTX, reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 					   BPF_REG_ARG3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			pr_err("prologue: failed to get offset of register %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			       reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		/* Make r7 the stack pointer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		ins(BPF_MOV64_REG(BPF_REG_7, BPF_REG_FP), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		/* r7 += -8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		ins(BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, stack_offset), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		 * Store r3 (base register) onto stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		 * Ensure fp[offset] is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		 * fp is the only valid base register when storing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		 * into stack. We are not allowed to use r7 as base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		 * register here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		ins(BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_ARG3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				stack_offset), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		ref = arg->ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		probeid = BPF_FUNC_probe_read_kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		while (ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			pr_debug("prologue: arg %d: offset %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				 i, ref->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			if (ref->user_access)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 				probeid = BPF_FUNC_probe_read_user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 			err = gen_read_mem(pos, BPF_REG_3, BPF_REG_7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 					   ref->offset, probeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				pr_err("prologue: failed to generate probe_read function call\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 				goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			ref = ref->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			 * Load previous result into ARG3. Use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			 * BPF_REG_FP instead of r7 because verifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			 * allows FP based addressing only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			if (ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 				ins(BPF_LDX_MEM(BPF_DW, BPF_REG_ARG3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 						BPF_REG_FP, stack_offset), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	/* Final pass: read to registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	for (i = 0; i < nargs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		int insn_sz = (args[i].ref) ? argtype_to_ldx_size(args[i].type) : BPF_DW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		pr_debug("prologue: load arg %d, insn_sz is %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			 i, insn_sz_to_str(insn_sz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		ins(BPF_LDX_MEM(insn_sz, BPF_PROLOGUE_START_ARG_REG + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 				BPF_REG_FP, -BPF_REG_SIZE * (i + 1)), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	ins(BPF_JMP_IMM(BPF_JA, BPF_REG_0, 0, JMP_TO_SUCCESS_CODE), pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	return check_pos(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) prologue_relocate(struct bpf_insn_pos *pos, struct bpf_insn *error_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		  struct bpf_insn *success_code, struct bpf_insn *user_code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct bpf_insn *insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (check_pos(pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	for (insn = pos->begin; insn < pos->pos; insn++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		struct bpf_insn *target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		u8 class = BPF_CLASS(insn->code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		u8 opcode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (class != BPF_JMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		opcode = BPF_OP(insn->code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (opcode == BPF_CALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		switch (insn->off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		case JMP_TO_ERROR_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			target = error_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		case JMP_TO_SUCCESS_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			target = success_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		case JMP_TO_USER_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			target = user_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			pr_err("bpf prologue: internal error: relocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			return -BPF_LOADER_ERRNO__PROLOGUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		insn->off = target - (insn + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int bpf__gen_prologue(struct probe_trace_arg *args, int nargs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		      struct bpf_insn *new_prog, size_t *new_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		      size_t cnt_space)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	struct bpf_insn *success_code = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct bpf_insn *error_code = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	struct bpf_insn *user_code = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct bpf_insn_pos pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	bool fastpath = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	int err = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (!new_prog || !new_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (cnt_space > BPF_MAXINSNS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		cnt_space = BPF_MAXINSNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	pos.begin = new_prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	pos.end = new_prog + cnt_space;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	pos.pos = new_prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (!nargs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		    &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (check_pos(&pos))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		*new_cnt = pos_get_cnt(&pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (nargs > BPF_PROLOGUE_MAX_ARGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		pr_warning("bpf: prologue: %d arguments are dropped\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 			   nargs - BPF_PROLOGUE_MAX_ARGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		nargs = BPF_PROLOGUE_MAX_ARGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	/* First pass: validation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	for (i = 0; i < nargs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		struct probe_trace_arg_ref *ref = args[i].ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		if (args[i].value[0] == '@') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			/* TODO: fetch global variable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			pr_err("bpf: prologue: global %s%+ld not support\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 				args[i].value, ref ? ref->offset : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			return -ENOTSUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		while (ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			/* fastpath is true if all args has ref == NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			fastpath = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			 * Instruction encodes immediate value using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			 * s32, ref->offset is long. On systems which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			 * can't fill long in s32, refuse to process if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			 * ref->offset too large (or small).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) #ifdef __LP64__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) #define OFFSET_MAX	((1LL << 31) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) #define OFFSET_MIN	((1LL << 31) * -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			if (ref->offset > OFFSET_MAX ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 					ref->offset < OFFSET_MIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 				pr_err("bpf: prologue: offset out of bound: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 				       ref->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 				return -BPF_LOADER_ERRNO__PROLOGUEOOB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			ref = ref->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	pr_debug("prologue: pass validation\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (fastpath) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		/* If all variables are registers... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		pr_debug("prologue: fast path\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		err = gen_prologue_fastpath(&pos, args, nargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		pr_debug("prologue: slow path\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		/* Initialization: move ctx to a callee saved register. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		ins(BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1), &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		err = gen_prologue_slowpath(&pos, args, nargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		 * start of ERROR_CODE (only slow pass needs error code)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		 *   mov r2 <- 1  // r2 is error number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		 *   mov r3 <- 0  // r3, r4... should be touched or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		 *                // verifier would complain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		 *   mov r4 <- 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		 *   ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		 *   goto usercode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		error_code = pos.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		    &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		for (i = 0; i < nargs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			ins(BPF_ALU64_IMM(BPF_MOV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 					  BPF_PROLOGUE_START_ARG_REG + i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 					  0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			    &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		ins(BPF_JMP_IMM(BPF_JA, BPF_REG_0, 0, JMP_TO_USER_CODE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 				&pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	 * start of SUCCESS_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	 *   mov r2 <- 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	 *   goto usercode  // skip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	success_code = pos.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 0), &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	 * start of USER_CODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	 *   Restore ctx to r1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	user_code = pos.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (!fastpath) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		 * Only slow path needs restoring of ctx. In fast path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		 * register are loaded directly from r1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		ins(BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX), &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		err = prologue_relocate(&pos, error_code, success_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 					user_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	err = check_pos(&pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	*new_cnt = pos_get_cnt(&pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }