159fe41b5SDave Marchevsky // SPDX-License-Identifier: GPL-2.0 259fe41b5SDave Marchevsky /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ 359fe41b5SDave Marchevsky #include <vmlinux.h> 459fe41b5SDave Marchevsky #include <bpf/bpf_tracing.h> 559fe41b5SDave Marchevsky #include <bpf/bpf_helpers.h> 659fe41b5SDave Marchevsky #include "../bpf_testmod/bpf_testmod_kfunc.h" 7*03b77e17SAlexei Starovoitov 859fe41b5SDave Marchevsky static struct prog_test_ref_kfunc __kptr *v; 959fe41b5SDave Marchevsky long total_sum = -1; 1059fe41b5SDave Marchevsky 1159fe41b5SDave Marchevsky SEC("tc") test_jit_probe_mem(struct __sk_buff * ctx)1259fe41b5SDave Marchevskyint test_jit_probe_mem(struct __sk_buff *ctx) 1359fe41b5SDave Marchevsky { 1459fe41b5SDave Marchevsky struct prog_test_ref_kfunc *p; 1559fe41b5SDave Marchevsky unsigned long zero = 0, sum; 1659fe41b5SDave Marchevsky 1759fe41b5SDave Marchevsky p = bpf_kfunc_call_test_acquire(&zero); 1859fe41b5SDave Marchevsky if (!p) 1959fe41b5SDave Marchevsky return 1; 2059fe41b5SDave Marchevsky 2159fe41b5SDave Marchevsky p = bpf_kptr_xchg(&v, p); 2259fe41b5SDave Marchevsky if (p) 2359fe41b5SDave Marchevsky goto release_out; 2459fe41b5SDave Marchevsky 2559fe41b5SDave Marchevsky /* Direct map value access of kptr, should be PTR_UNTRUSTED */ 2659fe41b5SDave Marchevsky p = v; 2759fe41b5SDave Marchevsky if (!p) 2859fe41b5SDave Marchevsky return 1; 2959fe41b5SDave Marchevsky 3059fe41b5SDave Marchevsky asm volatile ( 3159fe41b5SDave Marchevsky "r9 = %[p];" 3259fe41b5SDave Marchevsky "%[sum] = 0;" 3359fe41b5SDave Marchevsky 3459fe41b5SDave Marchevsky /* r8 = p->a */ 3559fe41b5SDave Marchevsky "r8 = *(u32 *)(r9 + 0);" 3659fe41b5SDave Marchevsky "%[sum] += r8;" 3759fe41b5SDave Marchevsky 3859fe41b5SDave Marchevsky /* r8 = p->b */ 3959fe41b5SDave Marchevsky "r8 = *(u32 *)(r9 + 4);" 4059fe41b5SDave Marchevsky "%[sum] += r8;" 4159fe41b5SDave Marchevsky 4259fe41b5SDave Marchevsky "r9 += 8;" 4359fe41b5SDave Marchevsky /* r9 = p->a */ 4459fe41b5SDave Marchevsky "r9 = *(u32 *)(r9 - 8);" 4559fe41b5SDave Marchevsky "%[sum] += r9;" 4659fe41b5SDave Marchevsky 4759fe41b5SDave Marchevsky : [sum] "=r"(sum) 4859fe41b5SDave Marchevsky : [p] "r"(p) 4959fe41b5SDave Marchevsky : "r8", "r9" 5059fe41b5SDave Marchevsky ); 5159fe41b5SDave Marchevsky 5259fe41b5SDave Marchevsky total_sum = sum; 5359fe41b5SDave Marchevsky return 0; 5459fe41b5SDave Marchevsky release_out: 5559fe41b5SDave Marchevsky bpf_kfunc_call_test_release(p); 5659fe41b5SDave Marchevsky return 1; 5759fe41b5SDave Marchevsky } 5859fe41b5SDave Marchevsky 5959fe41b5SDave Marchevsky char _license[] SEC("license") = "GPL"; 6059fe41b5SDave Marchevsky