1*f7c14bbaSAndroid Build Coastguard Worker // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2*f7c14bbaSAndroid Build Coastguard Worker /* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
3*f7c14bbaSAndroid Build Coastguard Worker #include <linux/kernel.h>
4*f7c14bbaSAndroid Build Coastguard Worker #include <linux/filter.h>
5*f7c14bbaSAndroid Build Coastguard Worker #include "bpf.h"
6*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf.h"
7*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_common.h"
8*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_internal.h"
9*f7c14bbaSAndroid Build Coastguard Worker #include "str_error.h"
10*f7c14bbaSAndroid Build Coastguard Worker
ptr_to_u64(const void * ptr)11*f7c14bbaSAndroid Build Coastguard Worker static inline __u64 ptr_to_u64(const void *ptr)
12*f7c14bbaSAndroid Build Coastguard Worker {
13*f7c14bbaSAndroid Build Coastguard Worker return (__u64)(unsigned long)ptr;
14*f7c14bbaSAndroid Build Coastguard Worker }
15*f7c14bbaSAndroid Build Coastguard Worker
probe_fd(int fd)16*f7c14bbaSAndroid Build Coastguard Worker int probe_fd(int fd)
17*f7c14bbaSAndroid Build Coastguard Worker {
18*f7c14bbaSAndroid Build Coastguard Worker if (fd >= 0)
19*f7c14bbaSAndroid Build Coastguard Worker close(fd);
20*f7c14bbaSAndroid Build Coastguard Worker return fd >= 0;
21*f7c14bbaSAndroid Build Coastguard Worker }
22*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_prog_name(int token_fd)23*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_prog_name(int token_fd)
24*f7c14bbaSAndroid Build Coastguard Worker {
25*f7c14bbaSAndroid Build Coastguard Worker const size_t attr_sz = offsetofend(union bpf_attr, prog_name);
26*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
27*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_0, 0),
28*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
29*f7c14bbaSAndroid Build Coastguard Worker };
30*f7c14bbaSAndroid Build Coastguard Worker union bpf_attr attr;
31*f7c14bbaSAndroid Build Coastguard Worker int ret;
32*f7c14bbaSAndroid Build Coastguard Worker
33*f7c14bbaSAndroid Build Coastguard Worker memset(&attr, 0, attr_sz);
34*f7c14bbaSAndroid Build Coastguard Worker attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
35*f7c14bbaSAndroid Build Coastguard Worker attr.license = ptr_to_u64("GPL");
36*f7c14bbaSAndroid Build Coastguard Worker attr.insns = ptr_to_u64(insns);
37*f7c14bbaSAndroid Build Coastguard Worker attr.insn_cnt = (__u32)ARRAY_SIZE(insns);
38*f7c14bbaSAndroid Build Coastguard Worker attr.prog_token_fd = token_fd;
39*f7c14bbaSAndroid Build Coastguard Worker if (token_fd)
40*f7c14bbaSAndroid Build Coastguard Worker attr.prog_flags |= BPF_F_TOKEN_FD;
41*f7c14bbaSAndroid Build Coastguard Worker libbpf_strlcpy(attr.prog_name, "libbpf_nametest", sizeof(attr.prog_name));
42*f7c14bbaSAndroid Build Coastguard Worker
43*f7c14bbaSAndroid Build Coastguard Worker /* make sure loading with name works */
44*f7c14bbaSAndroid Build Coastguard Worker ret = sys_bpf_prog_load(&attr, attr_sz, PROG_LOAD_ATTEMPTS);
45*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(ret);
46*f7c14bbaSAndroid Build Coastguard Worker }
47*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_global_data(int token_fd)48*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_global_data(int token_fd)
49*f7c14bbaSAndroid Build Coastguard Worker {
50*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
51*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
52*f7c14bbaSAndroid Build Coastguard Worker BPF_LD_MAP_VALUE(BPF_REG_1, 0, 16),
53*f7c14bbaSAndroid Build Coastguard Worker BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 42),
54*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_0, 0),
55*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
56*f7c14bbaSAndroid Build Coastguard Worker };
57*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_map_create_opts, map_opts,
58*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
59*f7c14bbaSAndroid Build Coastguard Worker .map_flags = token_fd ? BPF_F_TOKEN_FD : 0,
60*f7c14bbaSAndroid Build Coastguard Worker );
61*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, prog_opts,
62*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
63*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
64*f7c14bbaSAndroid Build Coastguard Worker );
65*f7c14bbaSAndroid Build Coastguard Worker int ret, map, insn_cnt = ARRAY_SIZE(insns);
66*f7c14bbaSAndroid Build Coastguard Worker
67*f7c14bbaSAndroid Build Coastguard Worker map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_global", sizeof(int), 32, 1, &map_opts);
68*f7c14bbaSAndroid Build Coastguard Worker if (map < 0) {
69*f7c14bbaSAndroid Build Coastguard Worker ret = -errno;
70*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
71*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
72*f7c14bbaSAndroid Build Coastguard Worker __func__, cp, -ret);
73*f7c14bbaSAndroid Build Coastguard Worker return ret;
74*f7c14bbaSAndroid Build Coastguard Worker }
75*f7c14bbaSAndroid Build Coastguard Worker
76*f7c14bbaSAndroid Build Coastguard Worker insns[0].imm = map;
77*f7c14bbaSAndroid Build Coastguard Worker
78*f7c14bbaSAndroid Build Coastguard Worker ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &prog_opts);
79*f7c14bbaSAndroid Build Coastguard Worker close(map);
80*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(ret);
81*f7c14bbaSAndroid Build Coastguard Worker }
82*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf(int token_fd)83*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf(int token_fd)
84*f7c14bbaSAndroid Build Coastguard Worker {
85*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0int";
86*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
87*f7c14bbaSAndroid Build Coastguard Worker /* int */
88*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
89*f7c14bbaSAndroid Build Coastguard Worker };
90*f7c14bbaSAndroid Build Coastguard Worker
91*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
92*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
93*f7c14bbaSAndroid Build Coastguard Worker }
94*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf_func(int token_fd)95*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf_func(int token_fd)
96*f7c14bbaSAndroid Build Coastguard Worker {
97*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0int\0x\0a";
98*f7c14bbaSAndroid Build Coastguard Worker /* void x(int a) {} */
99*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
100*f7c14bbaSAndroid Build Coastguard Worker /* int */
101*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
102*f7c14bbaSAndroid Build Coastguard Worker /* FUNC_PROTO */ /* [2] */
103*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
104*f7c14bbaSAndroid Build Coastguard Worker BTF_PARAM_ENC(7, 1),
105*f7c14bbaSAndroid Build Coastguard Worker /* FUNC x */ /* [3] */
106*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
107*f7c14bbaSAndroid Build Coastguard Worker };
108*f7c14bbaSAndroid Build Coastguard Worker
109*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
110*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
111*f7c14bbaSAndroid Build Coastguard Worker }
112*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf_func_global(int token_fd)113*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf_func_global(int token_fd)
114*f7c14bbaSAndroid Build Coastguard Worker {
115*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0int\0x\0a";
116*f7c14bbaSAndroid Build Coastguard Worker /* static void x(int a) {} */
117*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
118*f7c14bbaSAndroid Build Coastguard Worker /* int */
119*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
120*f7c14bbaSAndroid Build Coastguard Worker /* FUNC_PROTO */ /* [2] */
121*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 0),
122*f7c14bbaSAndroid Build Coastguard Worker BTF_PARAM_ENC(7, 1),
123*f7c14bbaSAndroid Build Coastguard Worker /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */
124*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
125*f7c14bbaSAndroid Build Coastguard Worker };
126*f7c14bbaSAndroid Build Coastguard Worker
127*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
128*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
129*f7c14bbaSAndroid Build Coastguard Worker }
130*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf_datasec(int token_fd)131*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf_datasec(int token_fd)
132*f7c14bbaSAndroid Build Coastguard Worker {
133*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0x\0.data";
134*f7c14bbaSAndroid Build Coastguard Worker /* static int a; */
135*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
136*f7c14bbaSAndroid Build Coastguard Worker /* int */
137*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
138*f7c14bbaSAndroid Build Coastguard Worker /* VAR x */ /* [2] */
139*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
140*f7c14bbaSAndroid Build Coastguard Worker BTF_VAR_STATIC,
141*f7c14bbaSAndroid Build Coastguard Worker /* DATASEC val */ /* [3] */
142*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
143*f7c14bbaSAndroid Build Coastguard Worker BTF_VAR_SECINFO_ENC(2, 0, 4),
144*f7c14bbaSAndroid Build Coastguard Worker };
145*f7c14bbaSAndroid Build Coastguard Worker
146*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
147*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
148*f7c14bbaSAndroid Build Coastguard Worker }
149*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf_qmark_datasec(int token_fd)150*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf_qmark_datasec(int token_fd)
151*f7c14bbaSAndroid Build Coastguard Worker {
152*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0x\0?.data";
153*f7c14bbaSAndroid Build Coastguard Worker /* static int a; */
154*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
155*f7c14bbaSAndroid Build Coastguard Worker /* int */
156*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
157*f7c14bbaSAndroid Build Coastguard Worker /* VAR x */ /* [2] */
158*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
159*f7c14bbaSAndroid Build Coastguard Worker BTF_VAR_STATIC,
160*f7c14bbaSAndroid Build Coastguard Worker /* DATASEC ?.data */ /* [3] */
161*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
162*f7c14bbaSAndroid Build Coastguard Worker BTF_VAR_SECINFO_ENC(2, 0, 4),
163*f7c14bbaSAndroid Build Coastguard Worker };
164*f7c14bbaSAndroid Build Coastguard Worker
165*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
166*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
167*f7c14bbaSAndroid Build Coastguard Worker }
168*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf_float(int token_fd)169*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf_float(int token_fd)
170*f7c14bbaSAndroid Build Coastguard Worker {
171*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0float";
172*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
173*f7c14bbaSAndroid Build Coastguard Worker /* float */
174*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_FLOAT_ENC(1, 4),
175*f7c14bbaSAndroid Build Coastguard Worker };
176*f7c14bbaSAndroid Build Coastguard Worker
177*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
178*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
179*f7c14bbaSAndroid Build Coastguard Worker }
180*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf_decl_tag(int token_fd)181*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf_decl_tag(int token_fd)
182*f7c14bbaSAndroid Build Coastguard Worker {
183*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0tag";
184*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
185*f7c14bbaSAndroid Build Coastguard Worker /* int */
186*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
187*f7c14bbaSAndroid Build Coastguard Worker /* VAR x */ /* [2] */
188*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_VAR, 0, 0), 1),
189*f7c14bbaSAndroid Build Coastguard Worker BTF_VAR_STATIC,
190*f7c14bbaSAndroid Build Coastguard Worker /* attr */
191*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_DECL_TAG_ENC(1, 2, -1),
192*f7c14bbaSAndroid Build Coastguard Worker };
193*f7c14bbaSAndroid Build Coastguard Worker
194*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
195*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
196*f7c14bbaSAndroid Build Coastguard Worker }
197*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf_type_tag(int token_fd)198*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf_type_tag(int token_fd)
199*f7c14bbaSAndroid Build Coastguard Worker {
200*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0tag";
201*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
202*f7c14bbaSAndroid Build Coastguard Worker /* int */
203*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
204*f7c14bbaSAndroid Build Coastguard Worker /* attr */
205*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_TYPE_TAG_ENC(1, 1), /* [2] */
206*f7c14bbaSAndroid Build Coastguard Worker /* ptr */
207*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2), /* [3] */
208*f7c14bbaSAndroid Build Coastguard Worker };
209*f7c14bbaSAndroid Build Coastguard Worker
210*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
211*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
212*f7c14bbaSAndroid Build Coastguard Worker }
213*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_array_mmap(int token_fd)214*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_array_mmap(int token_fd)
215*f7c14bbaSAndroid Build Coastguard Worker {
216*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_map_create_opts, opts,
217*f7c14bbaSAndroid Build Coastguard Worker .map_flags = BPF_F_MMAPABLE | (token_fd ? BPF_F_TOKEN_FD : 0),
218*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
219*f7c14bbaSAndroid Build Coastguard Worker );
220*f7c14bbaSAndroid Build Coastguard Worker int fd;
221*f7c14bbaSAndroid Build Coastguard Worker
222*f7c14bbaSAndroid Build Coastguard Worker fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_mmap", sizeof(int), sizeof(int), 1, &opts);
223*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(fd);
224*f7c14bbaSAndroid Build Coastguard Worker }
225*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_exp_attach_type(int token_fd)226*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_exp_attach_type(int token_fd)
227*f7c14bbaSAndroid Build Coastguard Worker {
228*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, opts,
229*f7c14bbaSAndroid Build Coastguard Worker .expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE,
230*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
231*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
232*f7c14bbaSAndroid Build Coastguard Worker );
233*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
234*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_0, 0),
235*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
236*f7c14bbaSAndroid Build Coastguard Worker };
237*f7c14bbaSAndroid Build Coastguard Worker int fd, insn_cnt = ARRAY_SIZE(insns);
238*f7c14bbaSAndroid Build Coastguard Worker
239*f7c14bbaSAndroid Build Coastguard Worker /* use any valid combination of program type and (optional)
240*f7c14bbaSAndroid Build Coastguard Worker * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS)
241*f7c14bbaSAndroid Build Coastguard Worker * to see if kernel supports expected_attach_type field for
242*f7c14bbaSAndroid Build Coastguard Worker * BPF_PROG_LOAD command
243*f7c14bbaSAndroid Build Coastguard Worker */
244*f7c14bbaSAndroid Build Coastguard Worker fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", insns, insn_cnt, &opts);
245*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(fd);
246*f7c14bbaSAndroid Build Coastguard Worker }
247*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_probe_read_kernel(int token_fd)248*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_probe_read_kernel(int token_fd)
249*f7c14bbaSAndroid Build Coastguard Worker {
250*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, opts,
251*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
252*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
253*f7c14bbaSAndroid Build Coastguard Worker );
254*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
255*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), /* r1 = r10 (fp) */
256*f7c14bbaSAndroid Build Coastguard Worker BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */
257*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_2, 8), /* r2 = 8 */
258*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_3, 0), /* r3 = 0 */
259*f7c14bbaSAndroid Build Coastguard Worker BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
260*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
261*f7c14bbaSAndroid Build Coastguard Worker };
262*f7c14bbaSAndroid Build Coastguard Worker int fd, insn_cnt = ARRAY_SIZE(insns);
263*f7c14bbaSAndroid Build Coastguard Worker
264*f7c14bbaSAndroid Build Coastguard Worker fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
265*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(fd);
266*f7c14bbaSAndroid Build Coastguard Worker }
267*f7c14bbaSAndroid Build Coastguard Worker
probe_prog_bind_map(int token_fd)268*f7c14bbaSAndroid Build Coastguard Worker static int probe_prog_bind_map(int token_fd)
269*f7c14bbaSAndroid Build Coastguard Worker {
270*f7c14bbaSAndroid Build Coastguard Worker char *cp, errmsg[STRERR_BUFSIZE];
271*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
272*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_0, 0),
273*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
274*f7c14bbaSAndroid Build Coastguard Worker };
275*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_map_create_opts, map_opts,
276*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
277*f7c14bbaSAndroid Build Coastguard Worker .map_flags = token_fd ? BPF_F_TOKEN_FD : 0,
278*f7c14bbaSAndroid Build Coastguard Worker );
279*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, prog_opts,
280*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
281*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
282*f7c14bbaSAndroid Build Coastguard Worker );
283*f7c14bbaSAndroid Build Coastguard Worker int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
284*f7c14bbaSAndroid Build Coastguard Worker
285*f7c14bbaSAndroid Build Coastguard Worker map = bpf_map_create(BPF_MAP_TYPE_ARRAY, "libbpf_det_bind", sizeof(int), 32, 1, &map_opts);
286*f7c14bbaSAndroid Build Coastguard Worker if (map < 0) {
287*f7c14bbaSAndroid Build Coastguard Worker ret = -errno;
288*f7c14bbaSAndroid Build Coastguard Worker cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
289*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Error in %s():%s(%d). Couldn't create simple array map.\n",
290*f7c14bbaSAndroid Build Coastguard Worker __func__, cp, -ret);
291*f7c14bbaSAndroid Build Coastguard Worker return ret;
292*f7c14bbaSAndroid Build Coastguard Worker }
293*f7c14bbaSAndroid Build Coastguard Worker
294*f7c14bbaSAndroid Build Coastguard Worker prog = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, &prog_opts);
295*f7c14bbaSAndroid Build Coastguard Worker if (prog < 0) {
296*f7c14bbaSAndroid Build Coastguard Worker close(map);
297*f7c14bbaSAndroid Build Coastguard Worker return 0;
298*f7c14bbaSAndroid Build Coastguard Worker }
299*f7c14bbaSAndroid Build Coastguard Worker
300*f7c14bbaSAndroid Build Coastguard Worker ret = bpf_prog_bind_map(prog, map, NULL);
301*f7c14bbaSAndroid Build Coastguard Worker
302*f7c14bbaSAndroid Build Coastguard Worker close(map);
303*f7c14bbaSAndroid Build Coastguard Worker close(prog);
304*f7c14bbaSAndroid Build Coastguard Worker
305*f7c14bbaSAndroid Build Coastguard Worker return ret >= 0;
306*f7c14bbaSAndroid Build Coastguard Worker }
307*f7c14bbaSAndroid Build Coastguard Worker
probe_module_btf(int token_fd)308*f7c14bbaSAndroid Build Coastguard Worker static int probe_module_btf(int token_fd)
309*f7c14bbaSAndroid Build Coastguard Worker {
310*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0int";
311*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
312*f7c14bbaSAndroid Build Coastguard Worker /* int */
313*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),
314*f7c14bbaSAndroid Build Coastguard Worker };
315*f7c14bbaSAndroid Build Coastguard Worker struct bpf_btf_info info;
316*f7c14bbaSAndroid Build Coastguard Worker __u32 len = sizeof(info);
317*f7c14bbaSAndroid Build Coastguard Worker char name[16];
318*f7c14bbaSAndroid Build Coastguard Worker int fd, err;
319*f7c14bbaSAndroid Build Coastguard Worker
320*f7c14bbaSAndroid Build Coastguard Worker fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs), token_fd);
321*f7c14bbaSAndroid Build Coastguard Worker if (fd < 0)
322*f7c14bbaSAndroid Build Coastguard Worker return 0; /* BTF not supported at all */
323*f7c14bbaSAndroid Build Coastguard Worker
324*f7c14bbaSAndroid Build Coastguard Worker memset(&info, 0, sizeof(info));
325*f7c14bbaSAndroid Build Coastguard Worker info.name = ptr_to_u64(name);
326*f7c14bbaSAndroid Build Coastguard Worker info.name_len = sizeof(name);
327*f7c14bbaSAndroid Build Coastguard Worker
328*f7c14bbaSAndroid Build Coastguard Worker /* check that BPF_OBJ_GET_INFO_BY_FD supports specifying name pointer;
329*f7c14bbaSAndroid Build Coastguard Worker * kernel's module BTF support coincides with support for
330*f7c14bbaSAndroid Build Coastguard Worker * name/name_len fields in struct bpf_btf_info.
331*f7c14bbaSAndroid Build Coastguard Worker */
332*f7c14bbaSAndroid Build Coastguard Worker err = bpf_btf_get_info_by_fd(fd, &info, &len);
333*f7c14bbaSAndroid Build Coastguard Worker close(fd);
334*f7c14bbaSAndroid Build Coastguard Worker return !err;
335*f7c14bbaSAndroid Build Coastguard Worker }
336*f7c14bbaSAndroid Build Coastguard Worker
probe_perf_link(int token_fd)337*f7c14bbaSAndroid Build Coastguard Worker static int probe_perf_link(int token_fd)
338*f7c14bbaSAndroid Build Coastguard Worker {
339*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
340*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_0, 0),
341*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
342*f7c14bbaSAndroid Build Coastguard Worker };
343*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, opts,
344*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
345*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
346*f7c14bbaSAndroid Build Coastguard Worker );
347*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, link_fd, err;
348*f7c14bbaSAndroid Build Coastguard Worker
349*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL",
350*f7c14bbaSAndroid Build Coastguard Worker insns, ARRAY_SIZE(insns), &opts);
351*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0)
352*f7c14bbaSAndroid Build Coastguard Worker return -errno;
353*f7c14bbaSAndroid Build Coastguard Worker
354*f7c14bbaSAndroid Build Coastguard Worker /* use invalid perf_event FD to get EBADF, if link is supported;
355*f7c14bbaSAndroid Build Coastguard Worker * otherwise EINVAL should be returned
356*f7c14bbaSAndroid Build Coastguard Worker */
357*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, -1, BPF_PERF_EVENT, NULL);
358*f7c14bbaSAndroid Build Coastguard Worker err = -errno; /* close() can clobber errno */
359*f7c14bbaSAndroid Build Coastguard Worker
360*f7c14bbaSAndroid Build Coastguard Worker if (link_fd >= 0)
361*f7c14bbaSAndroid Build Coastguard Worker close(link_fd);
362*f7c14bbaSAndroid Build Coastguard Worker close(prog_fd);
363*f7c14bbaSAndroid Build Coastguard Worker
364*f7c14bbaSAndroid Build Coastguard Worker return link_fd < 0 && err == -EBADF;
365*f7c14bbaSAndroid Build Coastguard Worker }
366*f7c14bbaSAndroid Build Coastguard Worker
probe_uprobe_multi_link(int token_fd)367*f7c14bbaSAndroid Build Coastguard Worker static int probe_uprobe_multi_link(int token_fd)
368*f7c14bbaSAndroid Build Coastguard Worker {
369*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
370*f7c14bbaSAndroid Build Coastguard Worker .expected_attach_type = BPF_TRACE_UPROBE_MULTI,
371*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
372*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
373*f7c14bbaSAndroid Build Coastguard Worker );
374*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_link_create_opts, link_opts);
375*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
376*f7c14bbaSAndroid Build Coastguard Worker BPF_MOV64_IMM(BPF_REG_0, 0),
377*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
378*f7c14bbaSAndroid Build Coastguard Worker };
379*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, link_fd, err;
380*f7c14bbaSAndroid Build Coastguard Worker unsigned long offset = 0;
381*f7c14bbaSAndroid Build Coastguard Worker
382*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL",
383*f7c14bbaSAndroid Build Coastguard Worker insns, ARRAY_SIZE(insns), &load_opts);
384*f7c14bbaSAndroid Build Coastguard Worker if (prog_fd < 0)
385*f7c14bbaSAndroid Build Coastguard Worker return -errno;
386*f7c14bbaSAndroid Build Coastguard Worker
387*f7c14bbaSAndroid Build Coastguard Worker /* Creating uprobe in '/' binary should fail with -EBADF. */
388*f7c14bbaSAndroid Build Coastguard Worker link_opts.uprobe_multi.path = "/";
389*f7c14bbaSAndroid Build Coastguard Worker link_opts.uprobe_multi.offsets = &offset;
390*f7c14bbaSAndroid Build Coastguard Worker link_opts.uprobe_multi.cnt = 1;
391*f7c14bbaSAndroid Build Coastguard Worker
392*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts);
393*f7c14bbaSAndroid Build Coastguard Worker err = -errno; /* close() can clobber errno */
394*f7c14bbaSAndroid Build Coastguard Worker
395*f7c14bbaSAndroid Build Coastguard Worker if (link_fd >= 0 || err != -EBADF) {
396*f7c14bbaSAndroid Build Coastguard Worker close(link_fd);
397*f7c14bbaSAndroid Build Coastguard Worker close(prog_fd);
398*f7c14bbaSAndroid Build Coastguard Worker return 0;
399*f7c14bbaSAndroid Build Coastguard Worker }
400*f7c14bbaSAndroid Build Coastguard Worker
401*f7c14bbaSAndroid Build Coastguard Worker /* Initial multi-uprobe support in kernel didn't handle PID filtering
402*f7c14bbaSAndroid Build Coastguard Worker * correctly (it was doing thread filtering, not process filtering).
403*f7c14bbaSAndroid Build Coastguard Worker * So now we'll detect if PID filtering logic was fixed, and, if not,
404*f7c14bbaSAndroid Build Coastguard Worker * we'll pretend multi-uprobes are not supported, if not.
405*f7c14bbaSAndroid Build Coastguard Worker * Multi-uprobes are used in USDT attachment logic, and we need to be
406*f7c14bbaSAndroid Build Coastguard Worker * conservative here, because multi-uprobe selection happens early at
407*f7c14bbaSAndroid Build Coastguard Worker * load time, while the use of PID filtering is known late at
408*f7c14bbaSAndroid Build Coastguard Worker * attachment time, at which point it's too late to undo multi-uprobe
409*f7c14bbaSAndroid Build Coastguard Worker * selection.
410*f7c14bbaSAndroid Build Coastguard Worker *
411*f7c14bbaSAndroid Build Coastguard Worker * Creating uprobe with pid == -1 for (invalid) '/' binary will fail
412*f7c14bbaSAndroid Build Coastguard Worker * early with -EINVAL on kernels with fixed PID filtering logic;
413*f7c14bbaSAndroid Build Coastguard Worker * otherwise -ESRCH would be returned if passed correct binary path
414*f7c14bbaSAndroid Build Coastguard Worker * (but we'll just get -BADF, of course).
415*f7c14bbaSAndroid Build Coastguard Worker */
416*f7c14bbaSAndroid Build Coastguard Worker link_opts.uprobe_multi.pid = -1; /* invalid PID */
417*f7c14bbaSAndroid Build Coastguard Worker link_opts.uprobe_multi.path = "/"; /* invalid path */
418*f7c14bbaSAndroid Build Coastguard Worker link_opts.uprobe_multi.offsets = &offset;
419*f7c14bbaSAndroid Build Coastguard Worker link_opts.uprobe_multi.cnt = 1;
420*f7c14bbaSAndroid Build Coastguard Worker
421*f7c14bbaSAndroid Build Coastguard Worker link_fd = bpf_link_create(prog_fd, -1, BPF_TRACE_UPROBE_MULTI, &link_opts);
422*f7c14bbaSAndroid Build Coastguard Worker err = -errno; /* close() can clobber errno */
423*f7c14bbaSAndroid Build Coastguard Worker
424*f7c14bbaSAndroid Build Coastguard Worker if (link_fd >= 0)
425*f7c14bbaSAndroid Build Coastguard Worker close(link_fd);
426*f7c14bbaSAndroid Build Coastguard Worker close(prog_fd);
427*f7c14bbaSAndroid Build Coastguard Worker
428*f7c14bbaSAndroid Build Coastguard Worker return link_fd < 0 && err == -EINVAL;
429*f7c14bbaSAndroid Build Coastguard Worker }
430*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_bpf_cookie(int token_fd)431*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_bpf_cookie(int token_fd)
432*f7c14bbaSAndroid Build Coastguard Worker {
433*f7c14bbaSAndroid Build Coastguard Worker struct bpf_insn insns[] = {
434*f7c14bbaSAndroid Build Coastguard Worker BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_attach_cookie),
435*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
436*f7c14bbaSAndroid Build Coastguard Worker };
437*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, opts,
438*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
439*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
440*f7c14bbaSAndroid Build Coastguard Worker );
441*f7c14bbaSAndroid Build Coastguard Worker int ret, insn_cnt = ARRAY_SIZE(insns);
442*f7c14bbaSAndroid Build Coastguard Worker
443*f7c14bbaSAndroid Build Coastguard Worker ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
444*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(ret);
445*f7c14bbaSAndroid Build Coastguard Worker }
446*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_btf_enum64(int token_fd)447*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_btf_enum64(int token_fd)
448*f7c14bbaSAndroid Build Coastguard Worker {
449*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0enum64";
450*f7c14bbaSAndroid Build Coastguard Worker __u32 types[] = {
451*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(1, BTF_INFO_ENC(BTF_KIND_ENUM64, 0, 0), 8),
452*f7c14bbaSAndroid Build Coastguard Worker };
453*f7c14bbaSAndroid Build Coastguard Worker
454*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
455*f7c14bbaSAndroid Build Coastguard Worker strs, sizeof(strs), token_fd));
456*f7c14bbaSAndroid Build Coastguard Worker }
457*f7c14bbaSAndroid Build Coastguard Worker
probe_kern_arg_ctx_tag(int token_fd)458*f7c14bbaSAndroid Build Coastguard Worker static int probe_kern_arg_ctx_tag(int token_fd)
459*f7c14bbaSAndroid Build Coastguard Worker {
460*f7c14bbaSAndroid Build Coastguard Worker static const char strs[] = "\0a\0b\0arg:ctx\0";
461*f7c14bbaSAndroid Build Coastguard Worker const __u32 types[] = {
462*f7c14bbaSAndroid Build Coastguard Worker /* [1] INT */
463*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_INT_ENC(1 /* "a" */, BTF_INT_SIGNED, 0, 32, 4),
464*f7c14bbaSAndroid Build Coastguard Worker /* [2] PTR -> VOID */
465*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
466*f7c14bbaSAndroid Build Coastguard Worker /* [3] FUNC_PROTO `int(void *a)` */
467*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 1),
468*f7c14bbaSAndroid Build Coastguard Worker BTF_PARAM_ENC(1 /* "a" */, 2),
469*f7c14bbaSAndroid Build Coastguard Worker /* [4] FUNC 'a' -> FUNC_PROTO (main prog) */
470*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(1 /* "a" */, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 3),
471*f7c14bbaSAndroid Build Coastguard Worker /* [5] FUNC_PROTO `int(void *b __arg_ctx)` */
472*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FUNC_PROTO, 0, 1), 1),
473*f7c14bbaSAndroid Build Coastguard Worker BTF_PARAM_ENC(3 /* "b" */, 2),
474*f7c14bbaSAndroid Build Coastguard Worker /* [6] FUNC 'b' -> FUNC_PROTO (subprog) */
475*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_ENC(3 /* "b" */, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 5),
476*f7c14bbaSAndroid Build Coastguard Worker /* [7] DECL_TAG 'arg:ctx' -> func 'b' arg 'b' */
477*f7c14bbaSAndroid Build Coastguard Worker BTF_TYPE_DECL_TAG_ENC(5 /* "arg:ctx" */, 6, 0),
478*f7c14bbaSAndroid Build Coastguard Worker };
479*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_insn insns[] = {
480*f7c14bbaSAndroid Build Coastguard Worker /* main prog */
481*f7c14bbaSAndroid Build Coastguard Worker BPF_CALL_REL(+1),
482*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
483*f7c14bbaSAndroid Build Coastguard Worker /* global subprog */
484*f7c14bbaSAndroid Build Coastguard Worker BPF_EMIT_CALL(BPF_FUNC_get_func_ip), /* needs PTR_TO_CTX */
485*f7c14bbaSAndroid Build Coastguard Worker BPF_EXIT_INSN(),
486*f7c14bbaSAndroid Build Coastguard Worker };
487*f7c14bbaSAndroid Build Coastguard Worker const struct bpf_func_info_min func_infos[] = {
488*f7c14bbaSAndroid Build Coastguard Worker { 0, 4 }, /* main prog -> FUNC 'a' */
489*f7c14bbaSAndroid Build Coastguard Worker { 2, 6 }, /* subprog -> FUNC 'b' */
490*f7c14bbaSAndroid Build Coastguard Worker };
491*f7c14bbaSAndroid Build Coastguard Worker LIBBPF_OPTS(bpf_prog_load_opts, opts,
492*f7c14bbaSAndroid Build Coastguard Worker .token_fd = token_fd,
493*f7c14bbaSAndroid Build Coastguard Worker .prog_flags = token_fd ? BPF_F_TOKEN_FD : 0,
494*f7c14bbaSAndroid Build Coastguard Worker );
495*f7c14bbaSAndroid Build Coastguard Worker int prog_fd, btf_fd, insn_cnt = ARRAY_SIZE(insns);
496*f7c14bbaSAndroid Build Coastguard Worker
497*f7c14bbaSAndroid Build Coastguard Worker btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), strs, sizeof(strs), token_fd);
498*f7c14bbaSAndroid Build Coastguard Worker if (btf_fd < 0)
499*f7c14bbaSAndroid Build Coastguard Worker return 0;
500*f7c14bbaSAndroid Build Coastguard Worker
501*f7c14bbaSAndroid Build Coastguard Worker opts.prog_btf_fd = btf_fd;
502*f7c14bbaSAndroid Build Coastguard Worker opts.func_info = &func_infos;
503*f7c14bbaSAndroid Build Coastguard Worker opts.func_info_cnt = ARRAY_SIZE(func_infos);
504*f7c14bbaSAndroid Build Coastguard Worker opts.func_info_rec_size = sizeof(func_infos[0]);
505*f7c14bbaSAndroid Build Coastguard Worker
506*f7c14bbaSAndroid Build Coastguard Worker prog_fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, "det_arg_ctx",
507*f7c14bbaSAndroid Build Coastguard Worker "GPL", insns, insn_cnt, &opts);
508*f7c14bbaSAndroid Build Coastguard Worker close(btf_fd);
509*f7c14bbaSAndroid Build Coastguard Worker
510*f7c14bbaSAndroid Build Coastguard Worker return probe_fd(prog_fd);
511*f7c14bbaSAndroid Build Coastguard Worker }
512*f7c14bbaSAndroid Build Coastguard Worker
513*f7c14bbaSAndroid Build Coastguard Worker typedef int (*feature_probe_fn)(int /* token_fd */);
514*f7c14bbaSAndroid Build Coastguard Worker
515*f7c14bbaSAndroid Build Coastguard Worker static struct kern_feature_cache feature_cache;
516*f7c14bbaSAndroid Build Coastguard Worker
517*f7c14bbaSAndroid Build Coastguard Worker static struct kern_feature_desc {
518*f7c14bbaSAndroid Build Coastguard Worker const char *desc;
519*f7c14bbaSAndroid Build Coastguard Worker feature_probe_fn probe;
520*f7c14bbaSAndroid Build Coastguard Worker } feature_probes[__FEAT_CNT] = {
521*f7c14bbaSAndroid Build Coastguard Worker [FEAT_PROG_NAME] = {
522*f7c14bbaSAndroid Build Coastguard Worker "BPF program name", probe_kern_prog_name,
523*f7c14bbaSAndroid Build Coastguard Worker },
524*f7c14bbaSAndroid Build Coastguard Worker [FEAT_GLOBAL_DATA] = {
525*f7c14bbaSAndroid Build Coastguard Worker "global variables", probe_kern_global_data,
526*f7c14bbaSAndroid Build Coastguard Worker },
527*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF] = {
528*f7c14bbaSAndroid Build Coastguard Worker "minimal BTF", probe_kern_btf,
529*f7c14bbaSAndroid Build Coastguard Worker },
530*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF_FUNC] = {
531*f7c14bbaSAndroid Build Coastguard Worker "BTF functions", probe_kern_btf_func,
532*f7c14bbaSAndroid Build Coastguard Worker },
533*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF_GLOBAL_FUNC] = {
534*f7c14bbaSAndroid Build Coastguard Worker "BTF global function", probe_kern_btf_func_global,
535*f7c14bbaSAndroid Build Coastguard Worker },
536*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF_DATASEC] = {
537*f7c14bbaSAndroid Build Coastguard Worker "BTF data section and variable", probe_kern_btf_datasec,
538*f7c14bbaSAndroid Build Coastguard Worker },
539*f7c14bbaSAndroid Build Coastguard Worker [FEAT_ARRAY_MMAP] = {
540*f7c14bbaSAndroid Build Coastguard Worker "ARRAY map mmap()", probe_kern_array_mmap,
541*f7c14bbaSAndroid Build Coastguard Worker },
542*f7c14bbaSAndroid Build Coastguard Worker [FEAT_EXP_ATTACH_TYPE] = {
543*f7c14bbaSAndroid Build Coastguard Worker "BPF_PROG_LOAD expected_attach_type attribute",
544*f7c14bbaSAndroid Build Coastguard Worker probe_kern_exp_attach_type,
545*f7c14bbaSAndroid Build Coastguard Worker },
546*f7c14bbaSAndroid Build Coastguard Worker [FEAT_PROBE_READ_KERN] = {
547*f7c14bbaSAndroid Build Coastguard Worker "bpf_probe_read_kernel() helper", probe_kern_probe_read_kernel,
548*f7c14bbaSAndroid Build Coastguard Worker },
549*f7c14bbaSAndroid Build Coastguard Worker [FEAT_PROG_BIND_MAP] = {
550*f7c14bbaSAndroid Build Coastguard Worker "BPF_PROG_BIND_MAP support", probe_prog_bind_map,
551*f7c14bbaSAndroid Build Coastguard Worker },
552*f7c14bbaSAndroid Build Coastguard Worker [FEAT_MODULE_BTF] = {
553*f7c14bbaSAndroid Build Coastguard Worker "module BTF support", probe_module_btf,
554*f7c14bbaSAndroid Build Coastguard Worker },
555*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF_FLOAT] = {
556*f7c14bbaSAndroid Build Coastguard Worker "BTF_KIND_FLOAT support", probe_kern_btf_float,
557*f7c14bbaSAndroid Build Coastguard Worker },
558*f7c14bbaSAndroid Build Coastguard Worker [FEAT_PERF_LINK] = {
559*f7c14bbaSAndroid Build Coastguard Worker "BPF perf link support", probe_perf_link,
560*f7c14bbaSAndroid Build Coastguard Worker },
561*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF_DECL_TAG] = {
562*f7c14bbaSAndroid Build Coastguard Worker "BTF_KIND_DECL_TAG support", probe_kern_btf_decl_tag,
563*f7c14bbaSAndroid Build Coastguard Worker },
564*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF_TYPE_TAG] = {
565*f7c14bbaSAndroid Build Coastguard Worker "BTF_KIND_TYPE_TAG support", probe_kern_btf_type_tag,
566*f7c14bbaSAndroid Build Coastguard Worker },
567*f7c14bbaSAndroid Build Coastguard Worker [FEAT_MEMCG_ACCOUNT] = {
568*f7c14bbaSAndroid Build Coastguard Worker "memcg-based memory accounting", probe_memcg_account,
569*f7c14bbaSAndroid Build Coastguard Worker },
570*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BPF_COOKIE] = {
571*f7c14bbaSAndroid Build Coastguard Worker "BPF cookie support", probe_kern_bpf_cookie,
572*f7c14bbaSAndroid Build Coastguard Worker },
573*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF_ENUM64] = {
574*f7c14bbaSAndroid Build Coastguard Worker "BTF_KIND_ENUM64 support", probe_kern_btf_enum64,
575*f7c14bbaSAndroid Build Coastguard Worker },
576*f7c14bbaSAndroid Build Coastguard Worker [FEAT_SYSCALL_WRAPPER] = {
577*f7c14bbaSAndroid Build Coastguard Worker "Kernel using syscall wrapper", probe_kern_syscall_wrapper,
578*f7c14bbaSAndroid Build Coastguard Worker },
579*f7c14bbaSAndroid Build Coastguard Worker [FEAT_UPROBE_MULTI_LINK] = {
580*f7c14bbaSAndroid Build Coastguard Worker "BPF multi-uprobe link support", probe_uprobe_multi_link,
581*f7c14bbaSAndroid Build Coastguard Worker },
582*f7c14bbaSAndroid Build Coastguard Worker [FEAT_ARG_CTX_TAG] = {
583*f7c14bbaSAndroid Build Coastguard Worker "kernel-side __arg_ctx tag", probe_kern_arg_ctx_tag,
584*f7c14bbaSAndroid Build Coastguard Worker },
585*f7c14bbaSAndroid Build Coastguard Worker [FEAT_BTF_QMARK_DATASEC] = {
586*f7c14bbaSAndroid Build Coastguard Worker "BTF DATASEC names starting from '?'", probe_kern_btf_qmark_datasec,
587*f7c14bbaSAndroid Build Coastguard Worker },
588*f7c14bbaSAndroid Build Coastguard Worker };
589*f7c14bbaSAndroid Build Coastguard Worker
feat_supported(struct kern_feature_cache * cache,enum kern_feature_id feat_id)590*f7c14bbaSAndroid Build Coastguard Worker bool feat_supported(struct kern_feature_cache *cache, enum kern_feature_id feat_id)
591*f7c14bbaSAndroid Build Coastguard Worker {
592*f7c14bbaSAndroid Build Coastguard Worker struct kern_feature_desc *feat = &feature_probes[feat_id];
593*f7c14bbaSAndroid Build Coastguard Worker int ret;
594*f7c14bbaSAndroid Build Coastguard Worker
595*f7c14bbaSAndroid Build Coastguard Worker /* assume global feature cache, unless custom one is provided */
596*f7c14bbaSAndroid Build Coastguard Worker if (!cache)
597*f7c14bbaSAndroid Build Coastguard Worker cache = &feature_cache;
598*f7c14bbaSAndroid Build Coastguard Worker
599*f7c14bbaSAndroid Build Coastguard Worker if (READ_ONCE(cache->res[feat_id]) == FEAT_UNKNOWN) {
600*f7c14bbaSAndroid Build Coastguard Worker ret = feat->probe(cache->token_fd);
601*f7c14bbaSAndroid Build Coastguard Worker if (ret > 0) {
602*f7c14bbaSAndroid Build Coastguard Worker WRITE_ONCE(cache->res[feat_id], FEAT_SUPPORTED);
603*f7c14bbaSAndroid Build Coastguard Worker } else if (ret == 0) {
604*f7c14bbaSAndroid Build Coastguard Worker WRITE_ONCE(cache->res[feat_id], FEAT_MISSING);
605*f7c14bbaSAndroid Build Coastguard Worker } else {
606*f7c14bbaSAndroid Build Coastguard Worker pr_warn("Detection of kernel %s support failed: %d\n", feat->desc, ret);
607*f7c14bbaSAndroid Build Coastguard Worker WRITE_ONCE(cache->res[feat_id], FEAT_MISSING);
608*f7c14bbaSAndroid Build Coastguard Worker }
609*f7c14bbaSAndroid Build Coastguard Worker }
610*f7c14bbaSAndroid Build Coastguard Worker
611*f7c14bbaSAndroid Build Coastguard Worker return READ_ONCE(cache->res[feat_id]) == FEAT_SUPPORTED;
612*f7c14bbaSAndroid Build Coastguard Worker }
613