thermal: fix Mediatek thermal controller build
[linux/fpc-iii.git] / samples / bpf / bpf_helpers.h
blob7904a2a493de43f69c0cf868d78c38d8509790af
1 #ifndef __BPF_HELPERS_H
2 #define __BPF_HELPERS_H
4 /* helper macro to place programs, maps, license in
5 * different sections in elf_bpf file. Section names
6 * are interpreted by elf_bpf loader
7 */
8 #define SEC(NAME) __attribute__((section(NAME), used))
10 /* helper functions called from eBPF programs written in C */
11 static void *(*bpf_map_lookup_elem)(void *map, void *key) =
12 (void *) BPF_FUNC_map_lookup_elem;
13 static int (*bpf_map_update_elem)(void *map, void *key, void *value,
14 unsigned long long flags) =
15 (void *) BPF_FUNC_map_update_elem;
16 static int (*bpf_map_delete_elem)(void *map, void *key) =
17 (void *) BPF_FUNC_map_delete_elem;
18 static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) =
19 (void *) BPF_FUNC_probe_read;
20 static unsigned long long (*bpf_ktime_get_ns)(void) =
21 (void *) BPF_FUNC_ktime_get_ns;
22 static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) =
23 (void *) BPF_FUNC_trace_printk;
24 static void (*bpf_tail_call)(void *ctx, void *map, int index) =
25 (void *) BPF_FUNC_tail_call;
26 static unsigned long long (*bpf_get_smp_processor_id)(void) =
27 (void *) BPF_FUNC_get_smp_processor_id;
28 static unsigned long long (*bpf_get_current_pid_tgid)(void) =
29 (void *) BPF_FUNC_get_current_pid_tgid;
30 static unsigned long long (*bpf_get_current_uid_gid)(void) =
31 (void *) BPF_FUNC_get_current_uid_gid;
32 static int (*bpf_get_current_comm)(void *buf, int buf_size) =
33 (void *) BPF_FUNC_get_current_comm;
34 static int (*bpf_perf_event_read)(void *map, int index) =
35 (void *) BPF_FUNC_perf_event_read;
36 static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) =
37 (void *) BPF_FUNC_clone_redirect;
38 static int (*bpf_redirect)(int ifindex, int flags) =
39 (void *) BPF_FUNC_redirect;
40 static int (*bpf_perf_event_output)(void *ctx, void *map, int index, void *data, int size) =
41 (void *) BPF_FUNC_perf_event_output;
42 static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
43 (void *) BPF_FUNC_get_stackid;
45 /* llvm builtin functions that eBPF C program may use to
46 * emit BPF_LD_ABS and BPF_LD_IND instructions
48 struct sk_buff;
49 unsigned long long load_byte(void *skb,
50 unsigned long long off) asm("llvm.bpf.load.byte");
51 unsigned long long load_half(void *skb,
52 unsigned long long off) asm("llvm.bpf.load.half");
53 unsigned long long load_word(void *skb,
54 unsigned long long off) asm("llvm.bpf.load.word");
56 /* a helper structure used by eBPF C program
57 * to describe map attributes to elf_bpf loader
59 struct bpf_map_def {
60 unsigned int type;
61 unsigned int key_size;
62 unsigned int value_size;
63 unsigned int max_entries;
64 unsigned int map_flags;
67 static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
68 (void *) BPF_FUNC_skb_store_bytes;
69 static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
70 (void *) BPF_FUNC_l3_csum_replace;
71 static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
72 (void *) BPF_FUNC_l4_csum_replace;
74 #if defined(__x86_64__)
76 #define PT_REGS_PARM1(x) ((x)->di)
77 #define PT_REGS_PARM2(x) ((x)->si)
78 #define PT_REGS_PARM3(x) ((x)->dx)
79 #define PT_REGS_PARM4(x) ((x)->cx)
80 #define PT_REGS_PARM5(x) ((x)->r8)
81 #define PT_REGS_RET(x) ((x)->sp)
82 #define PT_REGS_FP(x) ((x)->bp)
83 #define PT_REGS_RC(x) ((x)->ax)
84 #define PT_REGS_SP(x) ((x)->sp)
85 #define PT_REGS_IP(x) ((x)->ip)
87 #elif defined(__s390x__)
89 #define PT_REGS_PARM1(x) ((x)->gprs[2])
90 #define PT_REGS_PARM2(x) ((x)->gprs[3])
91 #define PT_REGS_PARM3(x) ((x)->gprs[4])
92 #define PT_REGS_PARM4(x) ((x)->gprs[5])
93 #define PT_REGS_PARM5(x) ((x)->gprs[6])
94 #define PT_REGS_RET(x) ((x)->gprs[14])
95 #define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */
96 #define PT_REGS_RC(x) ((x)->gprs[2])
97 #define PT_REGS_SP(x) ((x)->gprs[15])
98 #define PT_REGS_IP(x) ((x)->ip)
100 #elif defined(__aarch64__)
102 #define PT_REGS_PARM1(x) ((x)->regs[0])
103 #define PT_REGS_PARM2(x) ((x)->regs[1])
104 #define PT_REGS_PARM3(x) ((x)->regs[2])
105 #define PT_REGS_PARM4(x) ((x)->regs[3])
106 #define PT_REGS_PARM5(x) ((x)->regs[4])
107 #define PT_REGS_RET(x) ((x)->regs[30])
108 #define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */
109 #define PT_REGS_RC(x) ((x)->regs[0])
110 #define PT_REGS_SP(x) ((x)->sp)
111 #define PT_REGS_IP(x) ((x)->pc)
113 #elif defined(__powerpc__)
115 #define PT_REGS_PARM1(x) ((x)->gpr[3])
116 #define PT_REGS_PARM2(x) ((x)->gpr[4])
117 #define PT_REGS_PARM3(x) ((x)->gpr[5])
118 #define PT_REGS_PARM4(x) ((x)->gpr[6])
119 #define PT_REGS_PARM5(x) ((x)->gpr[7])
120 #define PT_REGS_RC(x) ((x)->gpr[3])
121 #define PT_REGS_SP(x) ((x)->sp)
122 #define PT_REGS_IP(x) ((x)->nip)
124 #endif
126 #ifdef __powerpc__
127 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; })
128 #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP
129 #else
130 #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \
131 bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); })
132 #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \
133 bpf_probe_read(&(ip), sizeof(ip), \
134 (void *)(PT_REGS_FP(ctx) + sizeof(ip))); })
135 #endif
137 #endif