selftests/bpf: fix bpf_get_current_task
[linux-2.6-block.git] / tools / testing / selftests / bpf / bpf_helpers.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __BPF_HELPERS_H
3 #define __BPF_HELPERS_H
4
5 /* helper macro to place programs, maps, license in
6  * different sections in elf_bpf file. Section names
7  * are interpreted by elf_bpf loader
8  */
9 #define SEC(NAME) __attribute__((section(NAME), used))
10
11 /* helper functions called from eBPF programs written in C */
12 static void *(*bpf_map_lookup_elem)(void *map, const void *key) =
13         (void *) BPF_FUNC_map_lookup_elem;
14 static int (*bpf_map_update_elem)(void *map, const void *key, const void *value,
15                                   unsigned long long flags) =
16         (void *) BPF_FUNC_map_update_elem;
17 static int (*bpf_map_delete_elem)(void *map, const void *key) =
18         (void *) BPF_FUNC_map_delete_elem;
19 static int (*bpf_map_push_elem)(void *map, const void *value,
20                                 unsigned long long flags) =
21         (void *) BPF_FUNC_map_push_elem;
22 static int (*bpf_map_pop_elem)(void *map, void *value) =
23         (void *) BPF_FUNC_map_pop_elem;
24 static int (*bpf_map_peek_elem)(void *map, void *value) =
25         (void *) BPF_FUNC_map_peek_elem;
26 static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
27         (void *) BPF_FUNC_probe_read;
28 static unsigned long long (*bpf_ktime_get_ns)(void) =
29         (void *) BPF_FUNC_ktime_get_ns;
30 static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
31         (void *) BPF_FUNC_trace_printk;
32 static void (*bpf_tail_call)(void *ctx, void *map, int index) =
33         (void *) BPF_FUNC_tail_call;
34 static unsigned long long (*bpf_get_smp_processor_id)(void) =
35         (void *) BPF_FUNC_get_smp_processor_id;
36 static unsigned long long (*bpf_get_current_pid_tgid)(void) =
37         (void *) BPF_FUNC_get_current_pid_tgid;
38 static unsigned long long (*bpf_get_current_uid_gid)(void) =
39         (void *) BPF_FUNC_get_current_uid_gid;
40 static int (*bpf_get_current_comm)(void *buf, int buf_size) =
41         (void *) BPF_FUNC_get_current_comm;
42 static unsigned long long (*bpf_perf_event_read)(void *map,
43                                                  unsigned long long flags) =
44         (void *) BPF_FUNC_perf_event_read;
45 static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
46         (void *) BPF_FUNC_clone_redirect;
47 static int (*bpf_redirect)(int ifindex, int flags) =
48         (void *) BPF_FUNC_redirect;
49 static int (*bpf_redirect_map)(void *map, int key, int flags) =
50         (void *) BPF_FUNC_redirect_map;
51 static int (*bpf_perf_event_output)(void *ctx, void *map,
52                                     unsigned long long flags, void *data,
53                                     int size) =
54         (void *) BPF_FUNC_perf_event_output;
55 static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
56         (void *) BPF_FUNC_get_stackid;
57 static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
58         (void *) BPF_FUNC_probe_write_user;
59 static int (*bpf_current_task_under_cgroup)(void *map, int index) =
60         (void *) BPF_FUNC_current_task_under_cgroup;
61 static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) =
62         (void *) BPF_FUNC_skb_get_tunnel_key;
63 static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) =
64         (void *) BPF_FUNC_skb_set_tunnel_key;
65 static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) =
66         (void *) BPF_FUNC_skb_get_tunnel_opt;
67 static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) =
68         (void *) BPF_FUNC_skb_set_tunnel_opt;
69 static unsigned long long (*bpf_get_prandom_u32)(void) =
70         (void *) BPF_FUNC_get_prandom_u32;
71 static int (*bpf_xdp_adjust_head)(void *ctx, int offset) =
72         (void *) BPF_FUNC_xdp_adjust_head;
73 static int (*bpf_xdp_adjust_meta)(void *ctx, int offset) =
74         (void *) BPF_FUNC_xdp_adjust_meta;
75 static int (*bpf_get_socket_cookie)(void *ctx) =
76         (void *) BPF_FUNC_get_socket_cookie;
77 static int (*bpf_setsockopt)(void *ctx, int level, int optname, void *optval,
78                              int optlen) =
79         (void *) BPF_FUNC_setsockopt;
80 static int (*bpf_getsockopt)(void *ctx, int level, int optname, void *optval,
81                              int optlen) =
82         (void *) BPF_FUNC_getsockopt;
83 static int (*bpf_sock_ops_cb_flags_set)(void *ctx, int flags) =
84         (void *) BPF_FUNC_sock_ops_cb_flags_set;
85 static int (*bpf_sk_redirect_map)(void *ctx, void *map, int key, int flags) =
86         (void *) BPF_FUNC_sk_redirect_map;
87 static int (*bpf_sk_redirect_hash)(void *ctx, void *map, void *key, int flags) =
88         (void *) BPF_FUNC_sk_redirect_hash;
89 static int (*bpf_sock_map_update)(void *map, void *key, void *value,
90                                   unsigned long long flags) =
91         (void *) BPF_FUNC_sock_map_update;
92 static int (*bpf_sock_hash_update)(void *map, void *key, void *value,
93                                    unsigned long long flags) =
94         (void *) BPF_FUNC_sock_hash_update;
95 static int (*bpf_perf_event_read_value)(void *map, unsigned long long flags,
96                                         void *buf, unsigned int buf_size) =
97         (void *) BPF_FUNC_perf_event_read_value;
98 static int (*bpf_perf_prog_read_value)(void *ctx, void *buf,
99                                        unsigned int buf_size) =
100         (void *) BPF_FUNC_perf_prog_read_value;
101 static int (*bpf_override_return)(void *ctx, unsigned long rc) =
102         (void *) BPF_FUNC_override_return;
103 static int (*bpf_msg_redirect_map)(void *ctx, void *map, int key, int flags) =
104         (void *) BPF_FUNC_msg_redirect_map;
105 static int (*bpf_msg_redirect_hash)(void *ctx,
106                                     void *map, void *key, int flags) =
107         (void *) BPF_FUNC_msg_redirect_hash;
108 static int (*bpf_msg_apply_bytes)(void *ctx, int len) =
109         (void *) BPF_FUNC_msg_apply_bytes;
110 static int (*bpf_msg_cork_bytes)(void *ctx, int len) =
111         (void *) BPF_FUNC_msg_cork_bytes;
112 static int (*bpf_msg_pull_data)(void *ctx, int start, int end, int flags) =
113         (void *) BPF_FUNC_msg_pull_data;
114 static int (*bpf_msg_push_data)(void *ctx, int start, int end, int flags) =
115         (void *) BPF_FUNC_msg_push_data;
116 static int (*bpf_msg_pop_data)(void *ctx, int start, int cut, int flags) =
117         (void *) BPF_FUNC_msg_pop_data;
118 static int (*bpf_bind)(void *ctx, void *addr, int addr_len) =
119         (void *) BPF_FUNC_bind;
120 static int (*bpf_xdp_adjust_tail)(void *ctx, int offset) =
121         (void *) BPF_FUNC_xdp_adjust_tail;
122 static int (*bpf_skb_get_xfrm_state)(void *ctx, int index, void *state,
123                                      int size, int flags) =
124         (void *) BPF_FUNC_skb_get_xfrm_state;
125 static int (*bpf_sk_select_reuseport)(void *ctx, void *map, void *key, __u32 flags) =
126         (void *) BPF_FUNC_sk_select_reuseport;
127 static int (*bpf_get_stack)(void *ctx, void *buf, int size, int flags) =
128         (void *) BPF_FUNC_get_stack;
129 static int (*bpf_fib_lookup)(void *ctx, struct bpf_fib_lookup *params,
130                              int plen, __u32 flags) =
131         (void *) BPF_FUNC_fib_lookup;
132 static int (*bpf_lwt_push_encap)(void *ctx, unsigned int type, void *hdr,
133                                  unsigned int len) =
134         (void *) BPF_FUNC_lwt_push_encap;
135 static int (*bpf_lwt_seg6_store_bytes)(void *ctx, unsigned int offset,
136                                        void *from, unsigned int len) =
137         (void *) BPF_FUNC_lwt_seg6_store_bytes;
138 static int (*bpf_lwt_seg6_action)(void *ctx, unsigned int action, void *param,
139                                   unsigned int param_len) =
140         (void *) BPF_FUNC_lwt_seg6_action;
141 static int (*bpf_lwt_seg6_adjust_srh)(void *ctx, unsigned int offset,
142                                       unsigned int len) =
143         (void *) BPF_FUNC_lwt_seg6_adjust_srh;
144 static int (*bpf_rc_repeat)(void *ctx) =
145         (void *) BPF_FUNC_rc_repeat;
146 static int (*bpf_rc_keydown)(void *ctx, unsigned int protocol,
147                              unsigned long long scancode, unsigned int toggle) =
148         (void *) BPF_FUNC_rc_keydown;
149 static unsigned long long (*bpf_get_current_cgroup_id)(void) =
150         (void *) BPF_FUNC_get_current_cgroup_id;
151 static void *(*bpf_get_local_storage)(void *map, unsigned long long flags) =
152         (void *) BPF_FUNC_get_local_storage;
153 static unsigned long long (*bpf_skb_cgroup_id)(void *ctx) =
154         (void *) BPF_FUNC_skb_cgroup_id;
155 static unsigned long long (*bpf_skb_ancestor_cgroup_id)(void *ctx, int level) =
156         (void *) BPF_FUNC_skb_ancestor_cgroup_id;
157 static struct bpf_sock *(*bpf_sk_lookup_tcp)(void *ctx,
158                                              struct bpf_sock_tuple *tuple,
159                                              int size, unsigned long long netns_id,
160                                              unsigned long long flags) =
161         (void *) BPF_FUNC_sk_lookup_tcp;
162 static struct bpf_sock *(*bpf_skc_lookup_tcp)(void *ctx,
163                                              struct bpf_sock_tuple *tuple,
164                                              int size, unsigned long long netns_id,
165                                              unsigned long long flags) =
166         (void *) BPF_FUNC_skc_lookup_tcp;
167 static struct bpf_sock *(*bpf_sk_lookup_udp)(void *ctx,
168                                              struct bpf_sock_tuple *tuple,
169                                              int size, unsigned long long netns_id,
170                                              unsigned long long flags) =
171         (void *) BPF_FUNC_sk_lookup_udp;
172 static int (*bpf_sk_release)(struct bpf_sock *sk) =
173         (void *) BPF_FUNC_sk_release;
174 static int (*bpf_skb_vlan_push)(void *ctx, __be16 vlan_proto, __u16 vlan_tci) =
175         (void *) BPF_FUNC_skb_vlan_push;
176 static int (*bpf_skb_vlan_pop)(void *ctx) =
177         (void *) BPF_FUNC_skb_vlan_pop;
178 static int (*bpf_rc_pointer_rel)(void *ctx, int rel_x, int rel_y) =
179         (void *) BPF_FUNC_rc_pointer_rel;
180 static void (*bpf_spin_lock)(struct bpf_spin_lock *lock) =
181         (void *) BPF_FUNC_spin_lock;
182 static void (*bpf_spin_unlock)(struct bpf_spin_lock *lock) =
183         (void *) BPF_FUNC_spin_unlock;
184 static struct bpf_sock *(*bpf_sk_fullsock)(struct bpf_sock *sk) =
185         (void *) BPF_FUNC_sk_fullsock;
186 static struct bpf_tcp_sock *(*bpf_tcp_sock)(struct bpf_sock *sk) =
187         (void *) BPF_FUNC_tcp_sock;
188 static struct bpf_sock *(*bpf_get_listener_sock)(struct bpf_sock *sk) =
189         (void *) BPF_FUNC_get_listener_sock;
190 static int (*bpf_skb_ecn_set_ce)(void *ctx) =
191         (void *) BPF_FUNC_skb_ecn_set_ce;
192 static int (*bpf_tcp_check_syncookie)(struct bpf_sock *sk,
193             void *ip, int ip_len, void *tcp, int tcp_len) =
194         (void *) BPF_FUNC_tcp_check_syncookie;
195 static int (*bpf_sysctl_get_name)(void *ctx, char *buf,
196                                   unsigned long long buf_len,
197                                   unsigned long long flags) =
198         (void *) BPF_FUNC_sysctl_get_name;
199 static int (*bpf_sysctl_get_current_value)(void *ctx, char *buf,
200                                            unsigned long long buf_len) =
201         (void *) BPF_FUNC_sysctl_get_current_value;
202 static int (*bpf_sysctl_get_new_value)(void *ctx, char *buf,
203                                        unsigned long long buf_len) =
204         (void *) BPF_FUNC_sysctl_get_new_value;
205 static int (*bpf_sysctl_set_new_value)(void *ctx, const char *buf,
206                                        unsigned long long buf_len) =
207         (void *) BPF_FUNC_sysctl_set_new_value;
208 static int (*bpf_strtol)(const char *buf, unsigned long long buf_len,
209                          unsigned long long flags, long *res) =
210         (void *) BPF_FUNC_strtol;
211 static int (*bpf_strtoul)(const char *buf, unsigned long long buf_len,
212                           unsigned long long flags, unsigned long *res) =
213         (void *) BPF_FUNC_strtoul;
214 static void *(*bpf_sk_storage_get)(void *map, struct bpf_sock *sk,
215                                    void *value, __u64 flags) =
216         (void *) BPF_FUNC_sk_storage_get;
217 static int (*bpf_sk_storage_delete)(void *map, struct bpf_sock *sk) =
218         (void *)BPF_FUNC_sk_storage_delete;
219
220 /* llvm builtin functions that eBPF C program may use to
221  * emit BPF_LD_ABS and BPF_LD_IND instructions
222  */
223 struct sk_buff;
224 unsigned long long load_byte(void *skb,
225                              unsigned long long off) asm("llvm.bpf.load.byte");
226 unsigned long long load_half(void *skb,
227                              unsigned long long off) asm("llvm.bpf.load.half");
228 unsigned long long load_word(void *skb,
229                              unsigned long long off) asm("llvm.bpf.load.word");
230
231 /* a helper structure used by eBPF C program
232  * to describe map attributes to elf_bpf loader
233  */
234 struct bpf_map_def {
235         unsigned int type;
236         unsigned int key_size;
237         unsigned int value_size;
238         unsigned int max_entries;
239         unsigned int map_flags;
240         unsigned int inner_map_idx;
241         unsigned int numa_node;
242 };
243
244 #define BPF_ANNOTATE_KV_PAIR(name, type_key, type_val)          \
245         struct ____btf_map_##name {                             \
246                 type_key key;                                   \
247                 type_val value;                                 \
248         };                                                      \
249         struct ____btf_map_##name                               \
250         __attribute__ ((section(".maps." #name), used))         \
251                 ____btf_map_##name = { }
252
253 static int (*bpf_skb_load_bytes)(void *ctx, int off, void *to, int len) =
254         (void *) BPF_FUNC_skb_load_bytes;
255 static int (*bpf_skb_load_bytes_relative)(void *ctx, int off, void *to, int len, __u32 start_header) =
256         (void *) BPF_FUNC_skb_load_bytes_relative;
257 static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
258         (void *) BPF_FUNC_skb_store_bytes;
259 static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
260         (void *) BPF_FUNC_l3_csum_replace;
261 static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
262         (void *) BPF_FUNC_l4_csum_replace;
263 static int (*bpf_csum_diff)(void *from, int from_size, void *to, int to_size, int seed) =
264         (void *) BPF_FUNC_csum_diff;
265 static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) =
266         (void *) BPF_FUNC_skb_under_cgroup;
267 static int (*bpf_skb_change_head)(void *, int len, int flags) =
268         (void *) BPF_FUNC_skb_change_head;
269 static int (*bpf_skb_pull_data)(void *, int len) =
270         (void *) BPF_FUNC_skb_pull_data;
271 static unsigned int (*bpf_get_cgroup_classid)(void *ctx) =
272         (void *) BPF_FUNC_get_cgroup_classid;
273 static unsigned int (*bpf_get_route_realm)(void *ctx) =
274         (void *) BPF_FUNC_get_route_realm;
275 static int (*bpf_skb_change_proto)(void *ctx, __be16 proto, __u64 flags) =
276         (void *) BPF_FUNC_skb_change_proto;
277 static int (*bpf_skb_change_type)(void *ctx, __u32 type) =
278         (void *) BPF_FUNC_skb_change_type;
279 static unsigned int (*bpf_get_hash_recalc)(void *ctx) =
280         (void *) BPF_FUNC_get_hash_recalc;
281 static unsigned long long (*bpf_get_current_task)(void) =
282         (void *) BPF_FUNC_get_current_task;
283 static int (*bpf_skb_change_tail)(void *ctx, __u32 len, __u64 flags) =
284         (void *) BPF_FUNC_skb_change_tail;
285 static long long (*bpf_csum_update)(void *ctx, __u32 csum) =
286         (void *) BPF_FUNC_csum_update;
287 static void (*bpf_set_hash_invalid)(void *ctx) =
288         (void *) BPF_FUNC_set_hash_invalid;
289 static int (*bpf_get_numa_node_id)(void) =
290         (void *) BPF_FUNC_get_numa_node_id;
291 static int (*bpf_probe_read_str)(void *ctx, __u32 size,
292                                  const void *unsafe_ptr) =
293         (void *) BPF_FUNC_probe_read_str;
294 static unsigned int (*bpf_get_socket_uid)(void *ctx) =
295         (void *) BPF_FUNC_get_socket_uid;
296 static unsigned int (*bpf_set_hash)(void *ctx, __u32 hash) =
297         (void *) BPF_FUNC_set_hash;
298 static int (*bpf_skb_adjust_room)(void *ctx, __s32 len_diff, __u32 mode,
299                                   unsigned long long flags) =
300         (void *) BPF_FUNC_skb_adjust_room;
301
302 /* Scan the ARCH passed in from ARCH env variable (see Makefile) */
303 #if defined(__TARGET_ARCH_x86)
304         #define bpf_target_x86
305         #define bpf_target_defined
306 #elif defined(__TARGET_ARCH_s930x)
307         #define bpf_target_s930x
308         #define bpf_target_defined
309 #elif defined(__TARGET_ARCH_arm)
310         #define bpf_target_arm
311         #define bpf_target_defined
312 #elif defined(__TARGET_ARCH_arm64)
313         #define bpf_target_arm64
314         #define bpf_target_defined
315 #elif defined(__TARGET_ARCH_mips)
316         #define bpf_target_mips
317         #define bpf_target_defined
318 #elif defined(__TARGET_ARCH_powerpc)
319         #define bpf_target_powerpc
320         #define bpf_target_defined
321 #elif defined(__TARGET_ARCH_sparc)
322         #define bpf_target_sparc
323         #define bpf_target_defined
324 #else
325         #undef bpf_target_defined
326 #endif
327
328 /* Fall back to what the compiler says */
329 #ifndef bpf_target_defined
330 #if defined(__x86_64__)
331         #define bpf_target_x86
332 #elif defined(__s390x__)
333         #define bpf_target_s930x
334 #elif defined(__arm__)
335         #define bpf_target_arm
336 #elif defined(__aarch64__)
337         #define bpf_target_arm64
338 #elif defined(__mips__)
339         #define bpf_target_mips
340 #elif defined(__powerpc__)
341         #define bpf_target_powerpc
342 #elif defined(__sparc__)
343         #define bpf_target_sparc
344 #endif
345 #endif
346
347 #if defined(bpf_target_x86)
348
349 #define PT_REGS_PARM1(x) ((x)->di)
350 #define PT_REGS_PARM2(x) ((x)->si)
351 #define PT_REGS_PARM3(x) ((x)->dx)
352 #define PT_REGS_PARM4(x) ((x)->cx)
353 #define PT_REGS_PARM5(x) ((x)->r8)
354 #define PT_REGS_RET(x) ((x)->sp)
355 #define PT_REGS_FP(x) ((x)->bp)
356 #define PT_REGS_RC(x) ((x)->ax)
357 #define PT_REGS_SP(x) ((x)->sp)
358 #define PT_REGS_IP(x) ((x)->ip)
359
360 #elif defined(bpf_target_s390x)
361
362 #define PT_REGS_PARM1(x) ((x)->gprs[2])
363 #define PT_REGS_PARM2(x) ((x)->gprs[3])
364 #define PT_REGS_PARM3(x) ((x)->gprs[4])
365 #define PT_REGS_PARM4(x) ((x)->gprs[5])
366 #define PT_REGS_PARM5(x) ((x)->gprs[6])
367 #define PT_REGS_RET(x) ((x)->gprs[14])
368 #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
369 #define PT_REGS_RC(x) ((x)->gprs[2])
370 #define PT_REGS_SP(x) ((x)->gprs[15])
371 #define PT_REGS_IP(x) ((x)->psw.addr)
372
373 #elif defined(bpf_target_arm)
374
375 #define PT_REGS_PARM1(x) ((x)->uregs[0])
376 #define PT_REGS_PARM2(x) ((x)->uregs[1])
377 #define PT_REGS_PARM3(x) ((x)->uregs[2])
378 #define PT_REGS_PARM4(x) ((x)->uregs[3])
379 #define PT_REGS_PARM5(x) ((x)->uregs[4])
380 #define PT_REGS_RET(x) ((x)->uregs[14])
381 #define PT_REGS_FP(x) ((x)->uregs[11]) /* Works only with CONFIG_FRAME_POINTER */
382 #define PT_REGS_RC(x) ((x)->uregs[0])
383 #define PT_REGS_SP(x) ((x)->uregs[13])
384 #define PT_REGS_IP(x) ((x)->uregs[12])
385
386 #elif defined(bpf_target_arm64)
387
388 #define PT_REGS_PARM1(x) ((x)->regs[0])
389 #define PT_REGS_PARM2(x) ((x)->regs[1])
390 #define PT_REGS_PARM3(x) ((x)->regs[2])
391 #define PT_REGS_PARM4(x) ((x)->regs[3])
392 #define PT_REGS_PARM5(x) ((x)->regs[4])
393 #define PT_REGS_RET(x) ((x)->regs[30])
394 #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
395 #define PT_REGS_RC(x) ((x)->regs[0])
396 #define PT_REGS_SP(x) ((x)->sp)
397 #define PT_REGS_IP(x) ((x)->pc)
398
399 #elif defined(bpf_target_mips)
400
401 #define PT_REGS_PARM1(x) ((x)->regs[4])
402 #define PT_REGS_PARM2(x) ((x)->regs[5])
403 #define PT_REGS_PARM3(x) ((x)->regs[6])
404 #define PT_REGS_PARM4(x) ((x)->regs[7])
405 #define PT_REGS_PARM5(x) ((x)->regs[8])
406 #define PT_REGS_RET(x) ((x)->regs[31])
407 #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */
408 #define PT_REGS_RC(x) ((x)->regs[1])
409 #define PT_REGS_SP(x) ((x)->regs[29])
410 #define PT_REGS_IP(x) ((x)->cp0_epc)
411
412 #elif defined(bpf_target_powerpc)
413
414 #define PT_REGS_PARM1(x) ((x)->gpr[3])
415 #define PT_REGS_PARM2(x) ((x)->gpr[4])
416 #define PT_REGS_PARM3(x) ((x)->gpr[5])
417 #define PT_REGS_PARM4(x) ((x)->gpr[6])
418 #define PT_REGS_PARM5(x) ((x)->gpr[7])
419 #define PT_REGS_RC(x) ((x)->gpr[3])
420 #define PT_REGS_SP(x) ((x)->sp)
421 #define PT_REGS_IP(x) ((x)->nip)
422
423 #elif defined(bpf_target_sparc)
424
425 #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0])
426 #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1])
427 #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2])
428 #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3])
429 #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4])
430 #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7])
431 #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0])
432 #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP])
433
434 /* Should this also be a bpf_target check for the sparc case? */
435 #if defined(__arch64__)
436 #define PT_REGS_IP(x) ((x)->tpc)
437 #else
438 #define PT_REGS_IP(x) ((x)->pc)
439 #endif
440
441 #endif
442
443 #ifdef bpf_target_powerpc
444 #define BPF_KPROBE_READ_RET_IP(ip, ctx)         ({ (ip) = (ctx)->link; })
445 #define BPF_KRETPROBE_READ_RET_IP               BPF_KPROBE_READ_RET_IP
446 #elif bpf_target_sparc
447 #define BPF_KPROBE_READ_RET_IP(ip, ctx)         ({ (ip) = PT_REGS_RET(ctx); })
448 #define BPF_KRETPROBE_READ_RET_IP               BPF_KPROBE_READ_RET_IP
449 #else
450 #define BPF_KPROBE_READ_RET_IP(ip, ctx)         ({                              \
451                 bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
452 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx)      ({                              \
453                 bpf_probe_read(&(ip), sizeof(ip),                               \
454                                 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
455 #endif
456
457 #endif