18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
28c2ecf20Sopenharmony_ci/* Copyright (C) 2016-2018 Netronome Systems, Inc. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/bpf.h>
58c2ecf20Sopenharmony_ci#include <linux/bpf_verifier.h>
68c2ecf20Sopenharmony_ci#include <linux/kernel.h>
78c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
88c2ecf20Sopenharmony_ci#include <linux/pkt_cls.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "../nfp_app.h"
118c2ecf20Sopenharmony_ci#include "../nfp_main.h"
128c2ecf20Sopenharmony_ci#include "../nfp_net.h"
138c2ecf20Sopenharmony_ci#include "fw.h"
148c2ecf20Sopenharmony_ci#include "main.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define pr_vlog(env, fmt, ...)	\
178c2ecf20Sopenharmony_ci	bpf_verifier_log_write(env, "[nfp] " fmt, ##__VA_ARGS__)
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct nfp_insn_meta *
208c2ecf20Sopenharmony_cinfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
218c2ecf20Sopenharmony_ci		  unsigned int insn_idx)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci	unsigned int forward, backward, i;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	backward = meta->n - insn_idx;
268c2ecf20Sopenharmony_ci	forward = insn_idx - meta->n;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if (min(forward, backward) > nfp_prog->n_insns - insn_idx - 1) {
298c2ecf20Sopenharmony_ci		backward = nfp_prog->n_insns - insn_idx - 1;
308c2ecf20Sopenharmony_ci		meta = nfp_prog_last_meta(nfp_prog);
318c2ecf20Sopenharmony_ci	}
328c2ecf20Sopenharmony_ci	if (min(forward, backward) > insn_idx && backward > insn_idx) {
338c2ecf20Sopenharmony_ci		forward = insn_idx;
348c2ecf20Sopenharmony_ci		meta = nfp_prog_first_meta(nfp_prog);
358c2ecf20Sopenharmony_ci	}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (forward < backward)
388c2ecf20Sopenharmony_ci		for (i = 0; i < forward; i++)
398c2ecf20Sopenharmony_ci			meta = nfp_meta_next(meta);
408c2ecf20Sopenharmony_ci	else
418c2ecf20Sopenharmony_ci		for (i = 0; i < backward; i++)
428c2ecf20Sopenharmony_ci			meta = nfp_meta_prev(meta);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return meta;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic void
488c2ecf20Sopenharmony_cinfp_record_adjust_head(struct nfp_app_bpf *bpf, struct nfp_prog *nfp_prog,
498c2ecf20Sopenharmony_ci		       struct nfp_insn_meta *meta,
508c2ecf20Sopenharmony_ci		       const struct bpf_reg_state *reg2)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	unsigned int location =	UINT_MAX;
538c2ecf20Sopenharmony_ci	int imm;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* Datapath usually can give us guarantees on how much adjust head
568c2ecf20Sopenharmony_ci	 * can be done without the need for any checks.  Optimize the simple
578c2ecf20Sopenharmony_ci	 * case where there is only one adjust head by a constant.
588c2ecf20Sopenharmony_ci	 */
598c2ecf20Sopenharmony_ci	if (reg2->type != SCALAR_VALUE || !tnum_is_const(reg2->var_off))
608c2ecf20Sopenharmony_ci		goto exit_set_location;
618c2ecf20Sopenharmony_ci	imm = reg2->var_off.value;
628c2ecf20Sopenharmony_ci	/* Translator will skip all checks, we need to guarantee min pkt len */
638c2ecf20Sopenharmony_ci	if (imm > ETH_ZLEN - ETH_HLEN)
648c2ecf20Sopenharmony_ci		goto exit_set_location;
658c2ecf20Sopenharmony_ci	if (imm > (int)bpf->adjust_head.guaranteed_add ||
668c2ecf20Sopenharmony_ci	    imm < -bpf->adjust_head.guaranteed_sub)
678c2ecf20Sopenharmony_ci		goto exit_set_location;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if (nfp_prog->adjust_head_location) {
708c2ecf20Sopenharmony_ci		/* Only one call per program allowed */
718c2ecf20Sopenharmony_ci		if (nfp_prog->adjust_head_location != meta->n)
728c2ecf20Sopenharmony_ci			goto exit_set_location;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci		if (meta->arg2.reg.var_off.value != imm)
758c2ecf20Sopenharmony_ci			goto exit_set_location;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	location = meta->n;
798c2ecf20Sopenharmony_ciexit_set_location:
808c2ecf20Sopenharmony_ci	nfp_prog->adjust_head_location = location;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic bool nfp_bpf_map_update_value_ok(struct bpf_verifier_env *env)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	const struct bpf_reg_state *reg1 = cur_regs(env) + BPF_REG_1;
868c2ecf20Sopenharmony_ci	const struct bpf_reg_state *reg3 = cur_regs(env) + BPF_REG_3;
878c2ecf20Sopenharmony_ci	struct bpf_offloaded_map *offmap;
888c2ecf20Sopenharmony_ci	struct bpf_func_state *state;
898c2ecf20Sopenharmony_ci	struct nfp_bpf_map *nfp_map;
908c2ecf20Sopenharmony_ci	int off, i;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	state = env->cur_state->frame[reg3->frameno];
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* We need to record each time update happens with non-zero words,
958c2ecf20Sopenharmony_ci	 * in case such word is used in atomic operations.
968c2ecf20Sopenharmony_ci	 * Implicitly depend on nfp_bpf_stack_arg_ok(reg3) being run before.
978c2ecf20Sopenharmony_ci	 */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	offmap = map_to_offmap(reg1->map_ptr);
1008c2ecf20Sopenharmony_ci	nfp_map = offmap->dev_priv;
1018c2ecf20Sopenharmony_ci	off = reg3->off + reg3->var_off.value;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	for (i = 0; i < offmap->map.value_size; i++) {
1048c2ecf20Sopenharmony_ci		struct bpf_stack_state *stack_entry;
1058c2ecf20Sopenharmony_ci		unsigned int soff;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		soff = -(off + i) - 1;
1088c2ecf20Sopenharmony_ci		stack_entry = &state->stack[soff / BPF_REG_SIZE];
1098c2ecf20Sopenharmony_ci		if (stack_entry->slot_type[soff % BPF_REG_SIZE] == STACK_ZERO)
1108c2ecf20Sopenharmony_ci			continue;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		if (nfp_map->use_map[i / 4].type == NFP_MAP_USE_ATOMIC_CNT) {
1138c2ecf20Sopenharmony_ci			pr_vlog(env, "value at offset %d/%d may be non-zero, bpf_map_update_elem() is required to initialize atomic counters to zero to avoid offload endian issues\n",
1148c2ecf20Sopenharmony_ci				i, soff);
1158c2ecf20Sopenharmony_ci			return false;
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci		nfp_map->use_map[i / 4].non_zero_update = 1;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return true;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic int
1248c2ecf20Sopenharmony_cinfp_bpf_stack_arg_ok(const char *fname, struct bpf_verifier_env *env,
1258c2ecf20Sopenharmony_ci		     const struct bpf_reg_state *reg,
1268c2ecf20Sopenharmony_ci		     struct nfp_bpf_reg_state *old_arg)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	s64 off, old_off;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (reg->type != PTR_TO_STACK) {
1318c2ecf20Sopenharmony_ci		pr_vlog(env, "%s: unsupported ptr type %d\n",
1328c2ecf20Sopenharmony_ci			fname, reg->type);
1338c2ecf20Sopenharmony_ci		return false;
1348c2ecf20Sopenharmony_ci	}
1358c2ecf20Sopenharmony_ci	if (!tnum_is_const(reg->var_off)) {
1368c2ecf20Sopenharmony_ci		pr_vlog(env, "%s: variable pointer\n", fname);
1378c2ecf20Sopenharmony_ci		return false;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	off = reg->var_off.value + reg->off;
1418c2ecf20Sopenharmony_ci	if (-off % 4) {
1428c2ecf20Sopenharmony_ci		pr_vlog(env, "%s: unaligned stack pointer %lld\n", fname, -off);
1438c2ecf20Sopenharmony_ci		return false;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/* Rest of the checks is only if we re-parse the same insn */
1478c2ecf20Sopenharmony_ci	if (!old_arg)
1488c2ecf20Sopenharmony_ci		return true;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	old_off = old_arg->reg.var_off.value + old_arg->reg.off;
1518c2ecf20Sopenharmony_ci	old_arg->var_off |= off != old_off;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return true;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic bool
1578c2ecf20Sopenharmony_cinfp_bpf_map_call_ok(const char *fname, struct bpf_verifier_env *env,
1588c2ecf20Sopenharmony_ci		    struct nfp_insn_meta *meta,
1598c2ecf20Sopenharmony_ci		    u32 helper_tgt, const struct bpf_reg_state *reg1)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	if (!helper_tgt) {
1628c2ecf20Sopenharmony_ci		pr_vlog(env, "%s: not supported by FW\n", fname);
1638c2ecf20Sopenharmony_ci		return false;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return true;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic int
1708c2ecf20Sopenharmony_cinfp_bpf_check_helper_call(struct nfp_prog *nfp_prog,
1718c2ecf20Sopenharmony_ci			  struct bpf_verifier_env *env,
1728c2ecf20Sopenharmony_ci			  struct nfp_insn_meta *meta)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	const struct bpf_reg_state *reg1 = cur_regs(env) + BPF_REG_1;
1758c2ecf20Sopenharmony_ci	const struct bpf_reg_state *reg2 = cur_regs(env) + BPF_REG_2;
1768c2ecf20Sopenharmony_ci	const struct bpf_reg_state *reg3 = cur_regs(env) + BPF_REG_3;
1778c2ecf20Sopenharmony_ci	struct nfp_app_bpf *bpf = nfp_prog->bpf;
1788c2ecf20Sopenharmony_ci	u32 func_id = meta->insn.imm;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	switch (func_id) {
1818c2ecf20Sopenharmony_ci	case BPF_FUNC_xdp_adjust_head:
1828c2ecf20Sopenharmony_ci		if (!bpf->adjust_head.off_max) {
1838c2ecf20Sopenharmony_ci			pr_vlog(env, "adjust_head not supported by FW\n");
1848c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
1858c2ecf20Sopenharmony_ci		}
1868c2ecf20Sopenharmony_ci		if (!(bpf->adjust_head.flags & NFP_BPF_ADJUST_HEAD_NO_META)) {
1878c2ecf20Sopenharmony_ci			pr_vlog(env, "adjust_head: FW requires shifting metadata, not supported by the driver\n");
1888c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
1898c2ecf20Sopenharmony_ci		}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci		nfp_record_adjust_head(bpf, nfp_prog, meta, reg2);
1928c2ecf20Sopenharmony_ci		break;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	case BPF_FUNC_xdp_adjust_tail:
1958c2ecf20Sopenharmony_ci		if (!bpf->adjust_tail) {
1968c2ecf20Sopenharmony_ci			pr_vlog(env, "adjust_tail not supported by FW\n");
1978c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
1988c2ecf20Sopenharmony_ci		}
1998c2ecf20Sopenharmony_ci		break;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	case BPF_FUNC_map_lookup_elem:
2028c2ecf20Sopenharmony_ci		if (!nfp_bpf_map_call_ok("map_lookup", env, meta,
2038c2ecf20Sopenharmony_ci					 bpf->helpers.map_lookup, reg1) ||
2048c2ecf20Sopenharmony_ci		    !nfp_bpf_stack_arg_ok("map_lookup", env, reg2,
2058c2ecf20Sopenharmony_ci					  meta->func_id ? &meta->arg2 : NULL))
2068c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
2078c2ecf20Sopenharmony_ci		break;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	case BPF_FUNC_map_update_elem:
2108c2ecf20Sopenharmony_ci		if (!nfp_bpf_map_call_ok("map_update", env, meta,
2118c2ecf20Sopenharmony_ci					 bpf->helpers.map_update, reg1) ||
2128c2ecf20Sopenharmony_ci		    !nfp_bpf_stack_arg_ok("map_update", env, reg2,
2138c2ecf20Sopenharmony_ci					  meta->func_id ? &meta->arg2 : NULL) ||
2148c2ecf20Sopenharmony_ci		    !nfp_bpf_stack_arg_ok("map_update", env, reg3, NULL) ||
2158c2ecf20Sopenharmony_ci		    !nfp_bpf_map_update_value_ok(env))
2168c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
2178c2ecf20Sopenharmony_ci		break;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	case BPF_FUNC_map_delete_elem:
2208c2ecf20Sopenharmony_ci		if (!nfp_bpf_map_call_ok("map_delete", env, meta,
2218c2ecf20Sopenharmony_ci					 bpf->helpers.map_delete, reg1) ||
2228c2ecf20Sopenharmony_ci		    !nfp_bpf_stack_arg_ok("map_delete", env, reg2,
2238c2ecf20Sopenharmony_ci					  meta->func_id ? &meta->arg2 : NULL))
2248c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
2258c2ecf20Sopenharmony_ci		break;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	case BPF_FUNC_get_prandom_u32:
2288c2ecf20Sopenharmony_ci		if (bpf->pseudo_random)
2298c2ecf20Sopenharmony_ci			break;
2308c2ecf20Sopenharmony_ci		pr_vlog(env, "bpf_get_prandom_u32(): FW doesn't support random number generation\n");
2318c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	case BPF_FUNC_perf_event_output:
2348c2ecf20Sopenharmony_ci		BUILD_BUG_ON(NFP_BPF_SCALAR_VALUE != SCALAR_VALUE ||
2358c2ecf20Sopenharmony_ci			     NFP_BPF_MAP_VALUE != PTR_TO_MAP_VALUE ||
2368c2ecf20Sopenharmony_ci			     NFP_BPF_STACK != PTR_TO_STACK ||
2378c2ecf20Sopenharmony_ci			     NFP_BPF_PACKET_DATA != PTR_TO_PACKET);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci		if (!bpf->helpers.perf_event_output) {
2408c2ecf20Sopenharmony_ci			pr_vlog(env, "event_output: not supported by FW\n");
2418c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
2428c2ecf20Sopenharmony_ci		}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci		/* Force current CPU to make sure we can report the event
2458c2ecf20Sopenharmony_ci		 * wherever we get the control message from FW.
2468c2ecf20Sopenharmony_ci		 */
2478c2ecf20Sopenharmony_ci		if (reg3->var_off.mask & BPF_F_INDEX_MASK ||
2488c2ecf20Sopenharmony_ci		    (reg3->var_off.value & BPF_F_INDEX_MASK) !=
2498c2ecf20Sopenharmony_ci		    BPF_F_CURRENT_CPU) {
2508c2ecf20Sopenharmony_ci			char tn_buf[48];
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci			tnum_strn(tn_buf, sizeof(tn_buf), reg3->var_off);
2538c2ecf20Sopenharmony_ci			pr_vlog(env, "event_output: must use BPF_F_CURRENT_CPU, var_off: %s\n",
2548c2ecf20Sopenharmony_ci				tn_buf);
2558c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
2568c2ecf20Sopenharmony_ci		}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		/* Save space in meta, we don't care about arguments other
2598c2ecf20Sopenharmony_ci		 * than 4th meta, shove it into arg1.
2608c2ecf20Sopenharmony_ci		 */
2618c2ecf20Sopenharmony_ci		reg1 = cur_regs(env) + BPF_REG_4;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci		if (reg1->type != SCALAR_VALUE /* NULL ptr */ &&
2648c2ecf20Sopenharmony_ci		    reg1->type != PTR_TO_STACK &&
2658c2ecf20Sopenharmony_ci		    reg1->type != PTR_TO_MAP_VALUE &&
2668c2ecf20Sopenharmony_ci		    reg1->type != PTR_TO_PACKET) {
2678c2ecf20Sopenharmony_ci			pr_vlog(env, "event_output: unsupported ptr type: %d\n",
2688c2ecf20Sopenharmony_ci				reg1->type);
2698c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
2708c2ecf20Sopenharmony_ci		}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		if (reg1->type == PTR_TO_STACK &&
2738c2ecf20Sopenharmony_ci		    !nfp_bpf_stack_arg_ok("event_output", env, reg1, NULL))
2748c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci		/* Warn user that on offload NFP may return success even if map
2778c2ecf20Sopenharmony_ci		 * is not going to accept the event, since the event output is
2788c2ecf20Sopenharmony_ci		 * fully async and device won't know the state of the map.
2798c2ecf20Sopenharmony_ci		 * There is also FW limitation on the event length.
2808c2ecf20Sopenharmony_ci		 *
2818c2ecf20Sopenharmony_ci		 * Lost events will not show up on the perf ring, driver
2828c2ecf20Sopenharmony_ci		 * won't see them at all.  Events may also get reordered.
2838c2ecf20Sopenharmony_ci		 */
2848c2ecf20Sopenharmony_ci		dev_warn_once(&nfp_prog->bpf->app->pf->pdev->dev,
2858c2ecf20Sopenharmony_ci			      "bpf: note: return codes and behavior of bpf_event_output() helper differs for offloaded programs!\n");
2868c2ecf20Sopenharmony_ci		pr_vlog(env, "warning: return codes and behavior of event_output helper differ for offload!\n");
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		if (!meta->func_id)
2898c2ecf20Sopenharmony_ci			break;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci		if (reg1->type != meta->arg1.type) {
2928c2ecf20Sopenharmony_ci			pr_vlog(env, "event_output: ptr type changed: %d %d\n",
2938c2ecf20Sopenharmony_ci				meta->arg1.type, reg1->type);
2948c2ecf20Sopenharmony_ci			return -EINVAL;
2958c2ecf20Sopenharmony_ci		}
2968c2ecf20Sopenharmony_ci		break;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	default:
2998c2ecf20Sopenharmony_ci		pr_vlog(env, "unsupported function id: %d\n", func_id);
3008c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	meta->func_id = func_id;
3048c2ecf20Sopenharmony_ci	meta->arg1 = *reg1;
3058c2ecf20Sopenharmony_ci	meta->arg2.reg = *reg2;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	return 0;
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_cistatic int
3118c2ecf20Sopenharmony_cinfp_bpf_check_exit(struct nfp_prog *nfp_prog,
3128c2ecf20Sopenharmony_ci		   struct bpf_verifier_env *env)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	const struct bpf_reg_state *reg0 = cur_regs(env) + BPF_REG_0;
3158c2ecf20Sopenharmony_ci	u64 imm;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	if (nfp_prog->type == BPF_PROG_TYPE_XDP)
3188c2ecf20Sopenharmony_ci		return 0;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (!(reg0->type == SCALAR_VALUE && tnum_is_const(reg0->var_off))) {
3218c2ecf20Sopenharmony_ci		char tn_buf[48];
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci		tnum_strn(tn_buf, sizeof(tn_buf), reg0->var_off);
3248c2ecf20Sopenharmony_ci		pr_vlog(env, "unsupported exit state: %d, var_off: %s\n",
3258c2ecf20Sopenharmony_ci			reg0->type, tn_buf);
3268c2ecf20Sopenharmony_ci		return -EINVAL;
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	imm = reg0->var_off.value;
3308c2ecf20Sopenharmony_ci	if (nfp_prog->type == BPF_PROG_TYPE_SCHED_CLS &&
3318c2ecf20Sopenharmony_ci	    imm <= TC_ACT_REDIRECT &&
3328c2ecf20Sopenharmony_ci	    imm != TC_ACT_SHOT && imm != TC_ACT_STOLEN &&
3338c2ecf20Sopenharmony_ci	    imm != TC_ACT_QUEUED) {
3348c2ecf20Sopenharmony_ci		pr_vlog(env, "unsupported exit state: %d, imm: %llx\n",
3358c2ecf20Sopenharmony_ci			reg0->type, imm);
3368c2ecf20Sopenharmony_ci		return -EINVAL;
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	return 0;
3408c2ecf20Sopenharmony_ci}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistatic int
3438c2ecf20Sopenharmony_cinfp_bpf_check_stack_access(struct nfp_prog *nfp_prog,
3448c2ecf20Sopenharmony_ci			   struct nfp_insn_meta *meta,
3458c2ecf20Sopenharmony_ci			   const struct bpf_reg_state *reg,
3468c2ecf20Sopenharmony_ci			   struct bpf_verifier_env *env)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	s32 old_off, new_off;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (reg->frameno != env->cur_state->curframe)
3518c2ecf20Sopenharmony_ci		meta->flags |= FLAG_INSN_PTR_CALLER_STACK_FRAME;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	if (!tnum_is_const(reg->var_off)) {
3548c2ecf20Sopenharmony_ci		pr_vlog(env, "variable ptr stack access\n");
3558c2ecf20Sopenharmony_ci		return -EINVAL;
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (meta->ptr.type == NOT_INIT)
3598c2ecf20Sopenharmony_ci		return 0;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	old_off = meta->ptr.off + meta->ptr.var_off.value;
3628c2ecf20Sopenharmony_ci	new_off = reg->off + reg->var_off.value;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	meta->ptr_not_const |= old_off != new_off;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	if (!meta->ptr_not_const)
3678c2ecf20Sopenharmony_ci		return 0;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	if (old_off % 4 == new_off % 4)
3708c2ecf20Sopenharmony_ci		return 0;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	pr_vlog(env, "stack access changed location was:%d is:%d\n",
3738c2ecf20Sopenharmony_ci		old_off, new_off);
3748c2ecf20Sopenharmony_ci	return -EINVAL;
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic const char *nfp_bpf_map_use_name(enum nfp_bpf_map_use use)
3788c2ecf20Sopenharmony_ci{
3798c2ecf20Sopenharmony_ci	static const char * const names[] = {
3808c2ecf20Sopenharmony_ci		[NFP_MAP_UNUSED]	= "unused",
3818c2ecf20Sopenharmony_ci		[NFP_MAP_USE_READ]	= "read",
3828c2ecf20Sopenharmony_ci		[NFP_MAP_USE_WRITE]	= "write",
3838c2ecf20Sopenharmony_ci		[NFP_MAP_USE_ATOMIC_CNT] = "atomic",
3848c2ecf20Sopenharmony_ci	};
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	if (use >= ARRAY_SIZE(names) || !names[use])
3878c2ecf20Sopenharmony_ci		return "unknown";
3888c2ecf20Sopenharmony_ci	return names[use];
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic int
3928c2ecf20Sopenharmony_cinfp_bpf_map_mark_used_one(struct bpf_verifier_env *env,
3938c2ecf20Sopenharmony_ci			  struct nfp_bpf_map *nfp_map,
3948c2ecf20Sopenharmony_ci			  unsigned int off, enum nfp_bpf_map_use use)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	if (nfp_map->use_map[off / 4].type != NFP_MAP_UNUSED &&
3978c2ecf20Sopenharmony_ci	    nfp_map->use_map[off / 4].type != use) {
3988c2ecf20Sopenharmony_ci		pr_vlog(env, "map value use type conflict %s vs %s off: %u\n",
3998c2ecf20Sopenharmony_ci			nfp_bpf_map_use_name(nfp_map->use_map[off / 4].type),
4008c2ecf20Sopenharmony_ci			nfp_bpf_map_use_name(use), off);
4018c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4028c2ecf20Sopenharmony_ci	}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	if (nfp_map->use_map[off / 4].non_zero_update &&
4058c2ecf20Sopenharmony_ci	    use == NFP_MAP_USE_ATOMIC_CNT) {
4068c2ecf20Sopenharmony_ci		pr_vlog(env, "atomic counter in map value may already be initialized to non-zero value off: %u\n",
4078c2ecf20Sopenharmony_ci			off);
4088c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4098c2ecf20Sopenharmony_ci	}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	nfp_map->use_map[off / 4].type = use;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	return 0;
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic int
4178c2ecf20Sopenharmony_cinfp_bpf_map_mark_used(struct bpf_verifier_env *env, struct nfp_insn_meta *meta,
4188c2ecf20Sopenharmony_ci		      const struct bpf_reg_state *reg,
4198c2ecf20Sopenharmony_ci		      enum nfp_bpf_map_use use)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	struct bpf_offloaded_map *offmap;
4228c2ecf20Sopenharmony_ci	struct nfp_bpf_map *nfp_map;
4238c2ecf20Sopenharmony_ci	unsigned int size, off;
4248c2ecf20Sopenharmony_ci	int i, err;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	if (!tnum_is_const(reg->var_off)) {
4278c2ecf20Sopenharmony_ci		pr_vlog(env, "map value offset is variable\n");
4288c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
4298c2ecf20Sopenharmony_ci	}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	off = reg->var_off.value + meta->insn.off + reg->off;
4328c2ecf20Sopenharmony_ci	size = BPF_LDST_BYTES(&meta->insn);
4338c2ecf20Sopenharmony_ci	offmap = map_to_offmap(reg->map_ptr);
4348c2ecf20Sopenharmony_ci	nfp_map = offmap->dev_priv;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	if (off + size > offmap->map.value_size) {
4378c2ecf20Sopenharmony_ci		pr_vlog(env, "map value access out-of-bounds\n");
4388c2ecf20Sopenharmony_ci		return -EINVAL;
4398c2ecf20Sopenharmony_ci	}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	for (i = 0; i < size; i += 4 - (off + i) % 4) {
4428c2ecf20Sopenharmony_ci		err = nfp_bpf_map_mark_used_one(env, nfp_map, off + i, use);
4438c2ecf20Sopenharmony_ci		if (err)
4448c2ecf20Sopenharmony_ci			return err;
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	return 0;
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic int
4518c2ecf20Sopenharmony_cinfp_bpf_check_ptr(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
4528c2ecf20Sopenharmony_ci		  struct bpf_verifier_env *env, u8 reg_no)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	const struct bpf_reg_state *reg = cur_regs(env) + reg_no;
4558c2ecf20Sopenharmony_ci	int err;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if (reg->type != PTR_TO_CTX &&
4588c2ecf20Sopenharmony_ci	    reg->type != PTR_TO_STACK &&
4598c2ecf20Sopenharmony_ci	    reg->type != PTR_TO_MAP_VALUE &&
4608c2ecf20Sopenharmony_ci	    reg->type != PTR_TO_PACKET) {
4618c2ecf20Sopenharmony_ci		pr_vlog(env, "unsupported ptr type: %d\n", reg->type);
4628c2ecf20Sopenharmony_ci		return -EINVAL;
4638c2ecf20Sopenharmony_ci	}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	if (reg->type == PTR_TO_STACK) {
4668c2ecf20Sopenharmony_ci		err = nfp_bpf_check_stack_access(nfp_prog, meta, reg, env);
4678c2ecf20Sopenharmony_ci		if (err)
4688c2ecf20Sopenharmony_ci			return err;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	if (reg->type == PTR_TO_MAP_VALUE) {
4728c2ecf20Sopenharmony_ci		if (is_mbpf_load(meta)) {
4738c2ecf20Sopenharmony_ci			err = nfp_bpf_map_mark_used(env, meta, reg,
4748c2ecf20Sopenharmony_ci						    NFP_MAP_USE_READ);
4758c2ecf20Sopenharmony_ci			if (err)
4768c2ecf20Sopenharmony_ci				return err;
4778c2ecf20Sopenharmony_ci		}
4788c2ecf20Sopenharmony_ci		if (is_mbpf_store(meta)) {
4798c2ecf20Sopenharmony_ci			pr_vlog(env, "map writes not supported\n");
4808c2ecf20Sopenharmony_ci			return -EOPNOTSUPP;
4818c2ecf20Sopenharmony_ci		}
4828c2ecf20Sopenharmony_ci		if (is_mbpf_xadd(meta)) {
4838c2ecf20Sopenharmony_ci			err = nfp_bpf_map_mark_used(env, meta, reg,
4848c2ecf20Sopenharmony_ci						    NFP_MAP_USE_ATOMIC_CNT);
4858c2ecf20Sopenharmony_ci			if (err)
4868c2ecf20Sopenharmony_ci				return err;
4878c2ecf20Sopenharmony_ci		}
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (meta->ptr.type != NOT_INIT && meta->ptr.type != reg->type) {
4918c2ecf20Sopenharmony_ci		pr_vlog(env, "ptr type changed for instruction %d -> %d\n",
4928c2ecf20Sopenharmony_ci			meta->ptr.type, reg->type);
4938c2ecf20Sopenharmony_ci		return -EINVAL;
4948c2ecf20Sopenharmony_ci	}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	meta->ptr = *reg;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	return 0;
4998c2ecf20Sopenharmony_ci}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_cistatic int
5028c2ecf20Sopenharmony_cinfp_bpf_check_store(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
5038c2ecf20Sopenharmony_ci		    struct bpf_verifier_env *env)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	const struct bpf_reg_state *reg = cur_regs(env) + meta->insn.dst_reg;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	if (reg->type == PTR_TO_CTX) {
5088c2ecf20Sopenharmony_ci		if (nfp_prog->type == BPF_PROG_TYPE_XDP) {
5098c2ecf20Sopenharmony_ci			/* XDP ctx accesses must be 4B in size */
5108c2ecf20Sopenharmony_ci			switch (meta->insn.off) {
5118c2ecf20Sopenharmony_ci			case offsetof(struct xdp_md, rx_queue_index):
5128c2ecf20Sopenharmony_ci				if (nfp_prog->bpf->queue_select)
5138c2ecf20Sopenharmony_ci					goto exit_check_ptr;
5148c2ecf20Sopenharmony_ci				pr_vlog(env, "queue selection not supported by FW\n");
5158c2ecf20Sopenharmony_ci				return -EOPNOTSUPP;
5168c2ecf20Sopenharmony_ci			}
5178c2ecf20Sopenharmony_ci		}
5188c2ecf20Sopenharmony_ci		pr_vlog(env, "unsupported store to context field\n");
5198c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ciexit_check_ptr:
5228c2ecf20Sopenharmony_ci	return nfp_bpf_check_ptr(nfp_prog, meta, env, meta->insn.dst_reg);
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_cistatic int
5268c2ecf20Sopenharmony_cinfp_bpf_check_xadd(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
5278c2ecf20Sopenharmony_ci		   struct bpf_verifier_env *env)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	const struct bpf_reg_state *sreg = cur_regs(env) + meta->insn.src_reg;
5308c2ecf20Sopenharmony_ci	const struct bpf_reg_state *dreg = cur_regs(env) + meta->insn.dst_reg;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	if (dreg->type != PTR_TO_MAP_VALUE) {
5338c2ecf20Sopenharmony_ci		pr_vlog(env, "atomic add not to a map value pointer: %d\n",
5348c2ecf20Sopenharmony_ci			dreg->type);
5358c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci	if (sreg->type != SCALAR_VALUE) {
5388c2ecf20Sopenharmony_ci		pr_vlog(env, "atomic add not of a scalar: %d\n", sreg->type);
5398c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
5408c2ecf20Sopenharmony_ci	}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	meta->xadd_over_16bit |=
5438c2ecf20Sopenharmony_ci		sreg->var_off.value > 0xffff || sreg->var_off.mask > 0xffff;
5448c2ecf20Sopenharmony_ci	meta->xadd_maybe_16bit |=
5458c2ecf20Sopenharmony_ci		(sreg->var_off.value & ~sreg->var_off.mask) <= 0xffff;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	return nfp_bpf_check_ptr(nfp_prog, meta, env, meta->insn.dst_reg);
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic int
5518c2ecf20Sopenharmony_cinfp_bpf_check_alu(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
5528c2ecf20Sopenharmony_ci		  struct bpf_verifier_env *env)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	const struct bpf_reg_state *sreg =
5558c2ecf20Sopenharmony_ci		cur_regs(env) + meta->insn.src_reg;
5568c2ecf20Sopenharmony_ci	const struct bpf_reg_state *dreg =
5578c2ecf20Sopenharmony_ci		cur_regs(env) + meta->insn.dst_reg;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	meta->umin_src = min(meta->umin_src, sreg->umin_value);
5608c2ecf20Sopenharmony_ci	meta->umax_src = max(meta->umax_src, sreg->umax_value);
5618c2ecf20Sopenharmony_ci	meta->umin_dst = min(meta->umin_dst, dreg->umin_value);
5628c2ecf20Sopenharmony_ci	meta->umax_dst = max(meta->umax_dst, dreg->umax_value);
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/* NFP supports u16 and u32 multiplication.
5658c2ecf20Sopenharmony_ci	 *
5668c2ecf20Sopenharmony_ci	 * For ALU64, if either operand is beyond u32's value range, we reject
5678c2ecf20Sopenharmony_ci	 * it. One thing to note, if the source operand is BPF_K, then we need
5688c2ecf20Sopenharmony_ci	 * to check "imm" field directly, and we'd reject it if it is negative.
5698c2ecf20Sopenharmony_ci	 * Because for ALU64, "imm" (with s32 type) is expected to be sign
5708c2ecf20Sopenharmony_ci	 * extended to s64 which NFP mul doesn't support.
5718c2ecf20Sopenharmony_ci	 *
5728c2ecf20Sopenharmony_ci	 * For ALU32, it is fine for "imm" be negative though, because the
5738c2ecf20Sopenharmony_ci	 * result is 32-bits and there is no difference on the low halve of
5748c2ecf20Sopenharmony_ci	 * the result for signed/unsigned mul, so we will get correct result.
5758c2ecf20Sopenharmony_ci	 */
5768c2ecf20Sopenharmony_ci	if (is_mbpf_mul(meta)) {
5778c2ecf20Sopenharmony_ci		if (meta->umax_dst > U32_MAX) {
5788c2ecf20Sopenharmony_ci			pr_vlog(env, "multiplier is not within u32 value range\n");
5798c2ecf20Sopenharmony_ci			return -EINVAL;
5808c2ecf20Sopenharmony_ci		}
5818c2ecf20Sopenharmony_ci		if (mbpf_src(meta) == BPF_X && meta->umax_src > U32_MAX) {
5828c2ecf20Sopenharmony_ci			pr_vlog(env, "multiplicand is not within u32 value range\n");
5838c2ecf20Sopenharmony_ci			return -EINVAL;
5848c2ecf20Sopenharmony_ci		}
5858c2ecf20Sopenharmony_ci		if (mbpf_class(meta) == BPF_ALU64 &&
5868c2ecf20Sopenharmony_ci		    mbpf_src(meta) == BPF_K && meta->insn.imm < 0) {
5878c2ecf20Sopenharmony_ci			pr_vlog(env, "sign extended multiplicand won't be within u32 value range\n");
5888c2ecf20Sopenharmony_ci			return -EINVAL;
5898c2ecf20Sopenharmony_ci		}
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	/* NFP doesn't have divide instructions, we support divide by constant
5938c2ecf20Sopenharmony_ci	 * through reciprocal multiplication. Given NFP support multiplication
5948c2ecf20Sopenharmony_ci	 * no bigger than u32, we'd require divisor and dividend no bigger than
5958c2ecf20Sopenharmony_ci	 * that as well.
5968c2ecf20Sopenharmony_ci	 *
5978c2ecf20Sopenharmony_ci	 * Also eBPF doesn't support signed divide and has enforced this on C
5988c2ecf20Sopenharmony_ci	 * language level by failing compilation. However LLVM assembler hasn't
5998c2ecf20Sopenharmony_ci	 * enforced this, so it is possible for negative constant to leak in as
6008c2ecf20Sopenharmony_ci	 * a BPF_K operand through assembly code, we reject such cases as well.
6018c2ecf20Sopenharmony_ci	 */
6028c2ecf20Sopenharmony_ci	if (is_mbpf_div(meta)) {
6038c2ecf20Sopenharmony_ci		if (meta->umax_dst > U32_MAX) {
6048c2ecf20Sopenharmony_ci			pr_vlog(env, "dividend is not within u32 value range\n");
6058c2ecf20Sopenharmony_ci			return -EINVAL;
6068c2ecf20Sopenharmony_ci		}
6078c2ecf20Sopenharmony_ci		if (mbpf_src(meta) == BPF_X) {
6088c2ecf20Sopenharmony_ci			if (meta->umin_src != meta->umax_src) {
6098c2ecf20Sopenharmony_ci				pr_vlog(env, "divisor is not constant\n");
6108c2ecf20Sopenharmony_ci				return -EINVAL;
6118c2ecf20Sopenharmony_ci			}
6128c2ecf20Sopenharmony_ci			if (meta->umax_src > U32_MAX) {
6138c2ecf20Sopenharmony_ci				pr_vlog(env, "divisor is not within u32 value range\n");
6148c2ecf20Sopenharmony_ci				return -EINVAL;
6158c2ecf20Sopenharmony_ci			}
6168c2ecf20Sopenharmony_ci		}
6178c2ecf20Sopenharmony_ci		if (mbpf_src(meta) == BPF_K && meta->insn.imm < 0) {
6188c2ecf20Sopenharmony_ci			pr_vlog(env, "divide by negative constant is not supported\n");
6198c2ecf20Sopenharmony_ci			return -EINVAL;
6208c2ecf20Sopenharmony_ci		}
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	return 0;
6248c2ecf20Sopenharmony_ci}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ciint nfp_verify_insn(struct bpf_verifier_env *env, int insn_idx,
6278c2ecf20Sopenharmony_ci		    int prev_insn_idx)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	struct nfp_prog *nfp_prog = env->prog->aux->offload->dev_priv;
6308c2ecf20Sopenharmony_ci	struct nfp_insn_meta *meta = nfp_prog->verifier_meta;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	meta = nfp_bpf_goto_meta(nfp_prog, meta, insn_idx);
6338c2ecf20Sopenharmony_ci	nfp_prog->verifier_meta = meta;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (!nfp_bpf_supported_opcode(meta->insn.code)) {
6368c2ecf20Sopenharmony_ci		pr_vlog(env, "instruction %#02x not supported\n",
6378c2ecf20Sopenharmony_ci			meta->insn.code);
6388c2ecf20Sopenharmony_ci		return -EINVAL;
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	if (meta->insn.src_reg >= MAX_BPF_REG ||
6428c2ecf20Sopenharmony_ci	    meta->insn.dst_reg >= MAX_BPF_REG) {
6438c2ecf20Sopenharmony_ci		pr_vlog(env, "program uses extended registers - jit hardening?\n");
6448c2ecf20Sopenharmony_ci		return -EINVAL;
6458c2ecf20Sopenharmony_ci	}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	if (is_mbpf_helper_call(meta))
6488c2ecf20Sopenharmony_ci		return nfp_bpf_check_helper_call(nfp_prog, env, meta);
6498c2ecf20Sopenharmony_ci	if (meta->insn.code == (BPF_JMP | BPF_EXIT))
6508c2ecf20Sopenharmony_ci		return nfp_bpf_check_exit(nfp_prog, env);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	if (is_mbpf_load(meta))
6538c2ecf20Sopenharmony_ci		return nfp_bpf_check_ptr(nfp_prog, meta, env,
6548c2ecf20Sopenharmony_ci					 meta->insn.src_reg);
6558c2ecf20Sopenharmony_ci	if (is_mbpf_store(meta))
6568c2ecf20Sopenharmony_ci		return nfp_bpf_check_store(nfp_prog, meta, env);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	if (is_mbpf_xadd(meta))
6598c2ecf20Sopenharmony_ci		return nfp_bpf_check_xadd(nfp_prog, meta, env);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	if (is_mbpf_alu(meta))
6628c2ecf20Sopenharmony_ci		return nfp_bpf_check_alu(nfp_prog, meta, env);
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	return 0;
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_cistatic int
6688c2ecf20Sopenharmony_cinfp_assign_subprog_idx_and_regs(struct bpf_verifier_env *env,
6698c2ecf20Sopenharmony_ci				struct nfp_prog *nfp_prog)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	struct nfp_insn_meta *meta;
6728c2ecf20Sopenharmony_ci	int index = 0;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	list_for_each_entry(meta, &nfp_prog->insns, l) {
6758c2ecf20Sopenharmony_ci		if (nfp_is_subprog_start(meta))
6768c2ecf20Sopenharmony_ci			index++;
6778c2ecf20Sopenharmony_ci		meta->subprog_idx = index;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		if (meta->insn.dst_reg >= BPF_REG_6 &&
6808c2ecf20Sopenharmony_ci		    meta->insn.dst_reg <= BPF_REG_9)
6818c2ecf20Sopenharmony_ci			nfp_prog->subprog[index].needs_reg_push = 1;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	if (index + 1 != nfp_prog->subprog_cnt) {
6858c2ecf20Sopenharmony_ci		pr_vlog(env, "BUG: number of processed BPF functions is not consistent (processed %d, expected %d)\n",
6868c2ecf20Sopenharmony_ci			index + 1, nfp_prog->subprog_cnt);
6878c2ecf20Sopenharmony_ci		return -EFAULT;
6888c2ecf20Sopenharmony_ci	}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	return 0;
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_cistatic unsigned int nfp_bpf_get_stack_usage(struct nfp_prog *nfp_prog)
6948c2ecf20Sopenharmony_ci{
6958c2ecf20Sopenharmony_ci	struct nfp_insn_meta *meta = nfp_prog_first_meta(nfp_prog);
6968c2ecf20Sopenharmony_ci	unsigned int max_depth = 0, depth = 0, frame = 0;
6978c2ecf20Sopenharmony_ci	struct nfp_insn_meta *ret_insn[MAX_CALL_FRAMES];
6988c2ecf20Sopenharmony_ci	unsigned short frame_depths[MAX_CALL_FRAMES];
6998c2ecf20Sopenharmony_ci	unsigned short ret_prog[MAX_CALL_FRAMES];
7008c2ecf20Sopenharmony_ci	unsigned short idx = meta->subprog_idx;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	/* Inspired from check_max_stack_depth() from kernel verifier.
7038c2ecf20Sopenharmony_ci	 * Starting from main subprogram, walk all instructions and recursively
7048c2ecf20Sopenharmony_ci	 * walk all callees that given subprogram can call. Since recursion is
7058c2ecf20Sopenharmony_ci	 * prevented by the kernel verifier, this algorithm only needs a local
7068c2ecf20Sopenharmony_ci	 * stack of MAX_CALL_FRAMES to remember callsites.
7078c2ecf20Sopenharmony_ci	 */
7088c2ecf20Sopenharmony_ciprocess_subprog:
7098c2ecf20Sopenharmony_ci	frame_depths[frame] = nfp_prog->subprog[idx].stack_depth;
7108c2ecf20Sopenharmony_ci	frame_depths[frame] = round_up(frame_depths[frame], STACK_FRAME_ALIGN);
7118c2ecf20Sopenharmony_ci	depth += frame_depths[frame];
7128c2ecf20Sopenharmony_ci	max_depth = max(max_depth, depth);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cicontinue_subprog:
7158c2ecf20Sopenharmony_ci	for (; meta != nfp_prog_last_meta(nfp_prog) && meta->subprog_idx == idx;
7168c2ecf20Sopenharmony_ci	     meta = nfp_meta_next(meta)) {
7178c2ecf20Sopenharmony_ci		if (!is_mbpf_pseudo_call(meta))
7188c2ecf20Sopenharmony_ci			continue;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci		/* We found a call to a subprogram. Remember instruction to
7218c2ecf20Sopenharmony_ci		 * return to and subprog id.
7228c2ecf20Sopenharmony_ci		 */
7238c2ecf20Sopenharmony_ci		ret_insn[frame] = nfp_meta_next(meta);
7248c2ecf20Sopenharmony_ci		ret_prog[frame] = idx;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci		/* Find the callee and start processing it. */
7278c2ecf20Sopenharmony_ci		meta = nfp_bpf_goto_meta(nfp_prog, meta,
7288c2ecf20Sopenharmony_ci					 meta->n + 1 + meta->insn.imm);
7298c2ecf20Sopenharmony_ci		idx = meta->subprog_idx;
7308c2ecf20Sopenharmony_ci		frame++;
7318c2ecf20Sopenharmony_ci		goto process_subprog;
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci	/* End of for() loop means the last instruction of the subprog was
7348c2ecf20Sopenharmony_ci	 * reached. If we popped all stack frames, return; otherwise, go on
7358c2ecf20Sopenharmony_ci	 * processing remaining instructions from the caller.
7368c2ecf20Sopenharmony_ci	 */
7378c2ecf20Sopenharmony_ci	if (frame == 0)
7388c2ecf20Sopenharmony_ci		return max_depth;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	depth -= frame_depths[frame];
7418c2ecf20Sopenharmony_ci	frame--;
7428c2ecf20Sopenharmony_ci	meta = ret_insn[frame];
7438c2ecf20Sopenharmony_ci	idx = ret_prog[frame];
7448c2ecf20Sopenharmony_ci	goto continue_subprog;
7458c2ecf20Sopenharmony_ci}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_cistatic void nfp_bpf_insn_flag_zext(struct nfp_prog *nfp_prog,
7488c2ecf20Sopenharmony_ci				   struct bpf_insn_aux_data *aux)
7498c2ecf20Sopenharmony_ci{
7508c2ecf20Sopenharmony_ci	struct nfp_insn_meta *meta;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	list_for_each_entry(meta, &nfp_prog->insns, l) {
7538c2ecf20Sopenharmony_ci		if (aux[meta->n].zext_dst)
7548c2ecf20Sopenharmony_ci			meta->flags |= FLAG_INSN_DO_ZEXT;
7558c2ecf20Sopenharmony_ci	}
7568c2ecf20Sopenharmony_ci}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ciint nfp_bpf_finalize(struct bpf_verifier_env *env)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	struct bpf_subprog_info *info;
7618c2ecf20Sopenharmony_ci	struct nfp_prog *nfp_prog;
7628c2ecf20Sopenharmony_ci	unsigned int max_stack;
7638c2ecf20Sopenharmony_ci	struct nfp_net *nn;
7648c2ecf20Sopenharmony_ci	int i;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	nfp_prog = env->prog->aux->offload->dev_priv;
7678c2ecf20Sopenharmony_ci	nfp_prog->subprog_cnt = env->subprog_cnt;
7688c2ecf20Sopenharmony_ci	nfp_prog->subprog = kcalloc(nfp_prog->subprog_cnt,
7698c2ecf20Sopenharmony_ci				    sizeof(nfp_prog->subprog[0]), GFP_KERNEL);
7708c2ecf20Sopenharmony_ci	if (!nfp_prog->subprog)
7718c2ecf20Sopenharmony_ci		return -ENOMEM;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	nfp_assign_subprog_idx_and_regs(env, nfp_prog);
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	info = env->subprog_info;
7768c2ecf20Sopenharmony_ci	for (i = 0; i < nfp_prog->subprog_cnt; i++) {
7778c2ecf20Sopenharmony_ci		nfp_prog->subprog[i].stack_depth = info[i].stack_depth;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci		if (i == 0)
7808c2ecf20Sopenharmony_ci			continue;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci		/* Account for size of return address. */
7838c2ecf20Sopenharmony_ci		nfp_prog->subprog[i].stack_depth += REG_WIDTH;
7848c2ecf20Sopenharmony_ci		/* Account for size of saved registers, if necessary. */
7858c2ecf20Sopenharmony_ci		if (nfp_prog->subprog[i].needs_reg_push)
7868c2ecf20Sopenharmony_ci			nfp_prog->subprog[i].stack_depth += BPF_REG_SIZE * 4;
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	nn = netdev_priv(env->prog->aux->offload->netdev);
7908c2ecf20Sopenharmony_ci	max_stack = nn_readb(nn, NFP_NET_CFG_BPF_STACK_SZ) * 64;
7918c2ecf20Sopenharmony_ci	nfp_prog->stack_size = nfp_bpf_get_stack_usage(nfp_prog);
7928c2ecf20Sopenharmony_ci	if (nfp_prog->stack_size > max_stack) {
7938c2ecf20Sopenharmony_ci		pr_vlog(env, "stack too large: program %dB > FW stack %dB\n",
7948c2ecf20Sopenharmony_ci			nfp_prog->stack_size, max_stack);
7958c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
7968c2ecf20Sopenharmony_ci	}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	nfp_bpf_insn_flag_zext(nfp_prog, env->insn_aux_data);
7998c2ecf20Sopenharmony_ci	return 0;
8008c2ecf20Sopenharmony_ci}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ciint nfp_bpf_opt_replace_insn(struct bpf_verifier_env *env, u32 off,
8038c2ecf20Sopenharmony_ci			     struct bpf_insn *insn)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	struct nfp_prog *nfp_prog = env->prog->aux->offload->dev_priv;
8068c2ecf20Sopenharmony_ci	struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8078c2ecf20Sopenharmony_ci	struct nfp_insn_meta *meta = nfp_prog->verifier_meta;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	meta = nfp_bpf_goto_meta(nfp_prog, meta, aux_data[off].orig_idx);
8108c2ecf20Sopenharmony_ci	nfp_prog->verifier_meta = meta;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	/* conditional jump to jump conversion */
8138c2ecf20Sopenharmony_ci	if (is_mbpf_cond_jump(meta) &&
8148c2ecf20Sopenharmony_ci	    insn->code == (BPF_JMP | BPF_JA | BPF_K)) {
8158c2ecf20Sopenharmony_ci		unsigned int tgt_off;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci		tgt_off = off + insn->off + 1;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci		if (!insn->off) {
8208c2ecf20Sopenharmony_ci			meta->jmp_dst = list_next_entry(meta, l);
8218c2ecf20Sopenharmony_ci			meta->jump_neg_op = false;
8228c2ecf20Sopenharmony_ci		} else if (meta->jmp_dst->n != aux_data[tgt_off].orig_idx) {
8238c2ecf20Sopenharmony_ci			pr_vlog(env, "branch hard wire at %d changes target %d -> %d\n",
8248c2ecf20Sopenharmony_ci				off, meta->jmp_dst->n,
8258c2ecf20Sopenharmony_ci				aux_data[tgt_off].orig_idx);
8268c2ecf20Sopenharmony_ci			return -EINVAL;
8278c2ecf20Sopenharmony_ci		}
8288c2ecf20Sopenharmony_ci		return 0;
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	pr_vlog(env, "unsupported instruction replacement %hhx -> %hhx\n",
8328c2ecf20Sopenharmony_ci		meta->insn.code, insn->code);
8338c2ecf20Sopenharmony_ci	return -EINVAL;
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ciint nfp_bpf_opt_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	struct nfp_prog *nfp_prog = env->prog->aux->offload->dev_priv;
8398c2ecf20Sopenharmony_ci	struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8408c2ecf20Sopenharmony_ci	struct nfp_insn_meta *meta = nfp_prog->verifier_meta;
8418c2ecf20Sopenharmony_ci	unsigned int i;
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	meta = nfp_bpf_goto_meta(nfp_prog, meta, aux_data[off].orig_idx);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	for (i = 0; i < cnt; i++) {
8468c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(&meta->l == &nfp_prog->insns))
8478c2ecf20Sopenharmony_ci			return -EINVAL;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci		/* doesn't count if it already has the flag */
8508c2ecf20Sopenharmony_ci		if (meta->flags & FLAG_INSN_SKIP_VERIFIER_OPT)
8518c2ecf20Sopenharmony_ci			i--;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci		meta->flags |= FLAG_INSN_SKIP_VERIFIER_OPT;
8548c2ecf20Sopenharmony_ci		meta = list_next_entry(meta, l);
8558c2ecf20Sopenharmony_ci	}
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	return 0;
8588c2ecf20Sopenharmony_ci}
859