1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (c) 2020 Facebook */ 3 #ifndef _BPF_TESTMOD_H 4 #define _BPF_TESTMOD_H 5 6 #include <linux/types.h> 7 8 struct task_struct; 9 struct cgroup; 10 11 struct bpf_testmod_test_read_ctx { 12 char *buf; 13 loff_t off; 14 size_t len; 15 }; 16 17 struct bpf_testmod_test_write_ctx { 18 char *buf; 19 loff_t off; 20 size_t len; 21 }; 22 23 struct bpf_testmod_test_writable_ctx { 24 bool early_ret; 25 int val; 26 }; 27 28 /* BPF iter that returns *value* *n* times in a row */ 29 struct bpf_iter_testmod_seq { 30 s64 value; 31 int cnt; 32 }; 33 34 struct bpf_testmod_ops { 35 int (*test_1)(void); 36 void (*test_2)(int a, int b); 37 /* Used to test nullable arguments. */ 38 int (*test_maybe_null)(int dummy, struct task_struct *task); 39 int (*unsupported_ops)(void); 40 /* Used to test ref_acquired arguments. */ 41 int (*test_refcounted)(int dummy, struct task_struct *task); 42 /* Used to test checking of __ref arguments when it not the first argument. */ 43 int (*test_refcounted_multi)(int dummy, struct task_struct *task, 44 struct task_struct *task2); 45 /* Used to test returning referenced kptr. */ 46 struct task_struct *(*test_return_ref_kptr)(int dummy, struct task_struct *task, 47 struct cgroup *cgrp); 48 49 /* Used to test indirect struct_ops trampoline with stack-passed arguments. */ 50 int (*test_trampoline_stack_args)(int arg1, int arg2, int arg3, 51 int arg4, int arg5, int arg6, 52 int arg7, int arg8, int arg9); 53 54 /* The following fields are used to test shadow copies. */ 55 char onebyte; 56 struct { 57 int a; 58 int b; 59 } unsupported; 60 int data; 61 62 /* The following pointers are used to test the maps having multiple 63 * pages of trampolines. 64 */ 65 int (*tramp_1)(int value); 66 int (*tramp_2)(int value); 67 int (*tramp_3)(int value); 68 int (*tramp_4)(int value); 69 int (*tramp_5)(int value); 70 int (*tramp_6)(int value); 71 int (*tramp_7)(int value); 72 int (*tramp_8)(int value); 73 int (*tramp_9)(int value); 74 int (*tramp_10)(int value); 75 int (*tramp_11)(int value); 76 int (*tramp_12)(int value); 77 int (*tramp_13)(int value); 78 int (*tramp_14)(int value); 79 int (*tramp_15)(int value); 80 int (*tramp_16)(int value); 81 int (*tramp_17)(int value); 82 int (*tramp_18)(int value); 83 int (*tramp_19)(int value); 84 int (*tramp_20)(int value); 85 int (*tramp_21)(int value); 86 int (*tramp_22)(int value); 87 int (*tramp_23)(int value); 88 int (*tramp_24)(int value); 89 int (*tramp_25)(int value); 90 int (*tramp_26)(int value); 91 int (*tramp_27)(int value); 92 int (*tramp_28)(int value); 93 int (*tramp_29)(int value); 94 int (*tramp_30)(int value); 95 int (*tramp_31)(int value); 96 int (*tramp_32)(int value); 97 int (*tramp_33)(int value); 98 int (*tramp_34)(int value); 99 int (*tramp_35)(int value); 100 int (*tramp_36)(int value); 101 int (*tramp_37)(int value); 102 int (*tramp_38)(int value); 103 int (*tramp_39)(int value); 104 int (*tramp_40)(int value); 105 }; 106 107 struct bpf_testmod_ops2 { 108 int (*test_1)(void); 109 }; 110 111 /* 16 bytes, so it takes two argument slots when passed by value */ 112 struct bpf_testmod_arena_pair { 113 u64 a; 114 u64 b; 115 }; 116 117 /* 118 * The qualifiers are BTF type tags for the verifier. The module gets __arena 119 * and __nullable from bpf_compiler_types.h. A BPF program gets __arena from 120 * bpf_arena_common.h, and the same definition here when it includes this 121 * header first, and needs __nullable and __percpu empty. User space needs all 122 * of them empty. 123 */ 124 #ifdef __KERNEL__ 125 #include <linux/bpf_compiler_types.h> 126 #endif 127 #if defined(__BPF__) && !defined(__arena) 128 #if defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM) 129 #define __arena __attribute__((address_space(1))) __attribute__((btf_type_tag("arena"))) 130 #else 131 #define __arena __attribute__((btf_type_tag("arena"))) 132 #endif 133 #endif 134 #ifndef __arena 135 #define __arena 136 #endif 137 #ifndef __nullable 138 #define __nullable 139 #endif 140 #ifndef __percpu 141 #define __percpu 142 #endif 143 144 struct bpf_testmod_pcpu { 145 int val; 146 }; 147 148 struct bpf_testmod_arena_nested { 149 u64 __arena *ptr; 150 }; 151 152 union bpf_testmod_arena_union { 153 u64 __arena *ptr; 154 u64 raw; 155 }; 156 157 /* Arguments of test_arena_member(), the pointers hold kernel addresses of arena memory */ 158 struct bpf_testmod_arena_member_args { 159 /* the value the caller passed, never NULL */ 160 u64 __arena *ptr; 161 /* always NULL */ 162 u64 __arena __nullable *null_ptr; 163 /* the same value again, behind a nested struct */ 164 struct bpf_testmod_arena_nested nested; 165 /* the same value again, but a load from a union is rejected */ 166 union bpf_testmod_arena_union u; 167 /* a scalar, loaded by the same insn as @ptr in a verifier test */ 168 u64 cookie; 169 /* a bad pointer to a struct like this one, a load through it faults */ 170 struct bpf_testmod_arena_member_args *faulting; 171 }; 172 173 /* Arguments of test_nullable(), trusted through the __safe_trusted alias in the module */ 174 struct bpf_testmod_nullable_args { 175 /* never NULL */ 176 struct task_struct *task; 177 /* NULL on the first call, set on the second */ 178 struct task_struct __nullable *task_or_null; 179 /* NULL on the first call, a per-CPU variable on the second */ 180 struct bpf_testmod_pcpu __percpu __nullable *pcpu_or_null; 181 /* NULL on the first call, set on the second */ 182 int __nullable *int_or_null; 183 /* this struct again, through a plain pointer, which the verifier probes */ 184 struct bpf_testmod_nullable_args *self; 185 }; 186 187 struct bpf_testmod_ops3 { 188 int (*test_1)(void); 189 int (*test_2)(void); 190 /* Used to test arena pointer arguments. */ 191 int (*test_arena)(u64 *ptr); 192 int (*test_arena_nullable)(u64 *ptr); 193 /* enough leading args to force @ptr onto the stack on x86 and arm64 */ 194 int (*test_arena_stack)(u64 a, u64 b, u64 c, u64 d, u64 e, u64 f, 195 u64 g, u64 h, u64 *ptr); 196 /* a multi-slot leading arg, so @ptr is not at the slot its arg index suggests */ 197 int (*test_arena_multislot)(struct bpf_testmod_arena_pair p, u64 *ptr); 198 /* Used to test __arena pointer members. */ 199 int (*test_arena_member)(struct bpf_testmod_arena_member_args *args); 200 /* Used to test __nullable pointer members. */ 201 int (*test_nullable)(struct bpf_testmod_nullable_args *args); 202 }; 203 204 struct st_ops_args { 205 u64 a; 206 }; 207 208 struct bpf_testmod_st_ops { 209 int (*test_prologue)(struct st_ops_args *args); 210 int (*test_epilogue)(struct st_ops_args *args); 211 int (*test_pro_epilogue)(struct st_ops_args *args); 212 struct module *owner; 213 }; 214 215 struct bpf_testmod_multi_st_ops { 216 int (*test_1)(struct st_ops_args *args); 217 struct hlist_node node; 218 int id; 219 }; 220 221 #endif /* _BPF_TESTMOD_H */ 222