Linux Audio

Check our new training course

Linux BSP development engineering services

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