0001
0002 #include <linux/bpf.h>
0003 #include <bpf/bpf_helpers.h>
0004 #include <bpf/bpf_tracing.h>
0005 #include <stdbool.h>
0006
0007 #ifdef ENABLE_ATOMICS_TESTS
0008 bool skip_tests __attribute((__section__(".data"))) = false;
0009 #else
0010 bool skip_tests = true;
0011 #endif
0012
0013 __u32 pid = 0;
0014
0015 __u64 add64_value = 1;
0016 __u64 add64_result = 0;
0017 __u32 add32_value = 1;
0018 __u32 add32_result = 0;
0019 __u64 add_stack_value_copy = 0;
0020 __u64 add_stack_result = 0;
0021 __u64 add_noreturn_value = 1;
0022
0023 SEC("raw_tp/sys_enter")
0024 int add(const void *ctx)
0025 {
0026 if (pid != (bpf_get_current_pid_tgid() >> 32))
0027 return 0;
0028 #ifdef ENABLE_ATOMICS_TESTS
0029 __u64 add_stack_value = 1;
0030
0031 add64_result = __sync_fetch_and_add(&add64_value, 2);
0032 add32_result = __sync_fetch_and_add(&add32_value, 2);
0033 add_stack_result = __sync_fetch_and_add(&add_stack_value, 2);
0034 add_stack_value_copy = add_stack_value;
0035 __sync_fetch_and_add(&add_noreturn_value, 2);
0036 #endif
0037
0038 return 0;
0039 }
0040
0041 __s64 sub64_value = 1;
0042 __s64 sub64_result = 0;
0043 __s32 sub32_value = 1;
0044 __s32 sub32_result = 0;
0045 __s64 sub_stack_value_copy = 0;
0046 __s64 sub_stack_result = 0;
0047 __s64 sub_noreturn_value = 1;
0048
0049 SEC("raw_tp/sys_enter")
0050 int sub(const void *ctx)
0051 {
0052 if (pid != (bpf_get_current_pid_tgid() >> 32))
0053 return 0;
0054 #ifdef ENABLE_ATOMICS_TESTS
0055 __u64 sub_stack_value = 1;
0056
0057 sub64_result = __sync_fetch_and_sub(&sub64_value, 2);
0058 sub32_result = __sync_fetch_and_sub(&sub32_value, 2);
0059 sub_stack_result = __sync_fetch_and_sub(&sub_stack_value, 2);
0060 sub_stack_value_copy = sub_stack_value;
0061 __sync_fetch_and_sub(&sub_noreturn_value, 2);
0062 #endif
0063
0064 return 0;
0065 }
0066
0067 __u64 and64_value = (0x110ull << 32);
0068 __u64 and64_result = 0;
0069 __u32 and32_value = 0x110;
0070 __u32 and32_result = 0;
0071 __u64 and_noreturn_value = (0x110ull << 32);
0072
0073 SEC("raw_tp/sys_enter")
0074 int and(const void *ctx)
0075 {
0076 if (pid != (bpf_get_current_pid_tgid() >> 32))
0077 return 0;
0078 #ifdef ENABLE_ATOMICS_TESTS
0079
0080 and64_result = __sync_fetch_and_and(&and64_value, 0x011ull << 32);
0081 and32_result = __sync_fetch_and_and(&and32_value, 0x011);
0082 __sync_fetch_and_and(&and_noreturn_value, 0x011ull << 32);
0083 #endif
0084
0085 return 0;
0086 }
0087
0088 __u64 or64_value = (0x110ull << 32);
0089 __u64 or64_result = 0;
0090 __u32 or32_value = 0x110;
0091 __u32 or32_result = 0;
0092 __u64 or_noreturn_value = (0x110ull << 32);
0093
0094 SEC("raw_tp/sys_enter")
0095 int or(const void *ctx)
0096 {
0097 if (pid != (bpf_get_current_pid_tgid() >> 32))
0098 return 0;
0099 #ifdef ENABLE_ATOMICS_TESTS
0100 or64_result = __sync_fetch_and_or(&or64_value, 0x011ull << 32);
0101 or32_result = __sync_fetch_and_or(&or32_value, 0x011);
0102 __sync_fetch_and_or(&or_noreturn_value, 0x011ull << 32);
0103 #endif
0104
0105 return 0;
0106 }
0107
0108 __u64 xor64_value = (0x110ull << 32);
0109 __u64 xor64_result = 0;
0110 __u32 xor32_value = 0x110;
0111 __u32 xor32_result = 0;
0112 __u64 xor_noreturn_value = (0x110ull << 32);
0113
0114 SEC("raw_tp/sys_enter")
0115 int xor(const void *ctx)
0116 {
0117 if (pid != (bpf_get_current_pid_tgid() >> 32))
0118 return 0;
0119 #ifdef ENABLE_ATOMICS_TESTS
0120 xor64_result = __sync_fetch_and_xor(&xor64_value, 0x011ull << 32);
0121 xor32_result = __sync_fetch_and_xor(&xor32_value, 0x011);
0122 __sync_fetch_and_xor(&xor_noreturn_value, 0x011ull << 32);
0123 #endif
0124
0125 return 0;
0126 }
0127
0128 __u64 cmpxchg64_value = 1;
0129 __u64 cmpxchg64_result_fail = 0;
0130 __u64 cmpxchg64_result_succeed = 0;
0131 __u32 cmpxchg32_value = 1;
0132 __u32 cmpxchg32_result_fail = 0;
0133 __u32 cmpxchg32_result_succeed = 0;
0134
0135 SEC("raw_tp/sys_enter")
0136 int cmpxchg(const void *ctx)
0137 {
0138 if (pid != (bpf_get_current_pid_tgid() >> 32))
0139 return 0;
0140 #ifdef ENABLE_ATOMICS_TESTS
0141 cmpxchg64_result_fail = __sync_val_compare_and_swap(&cmpxchg64_value, 0, 3);
0142 cmpxchg64_result_succeed = __sync_val_compare_and_swap(&cmpxchg64_value, 1, 2);
0143
0144 cmpxchg32_result_fail = __sync_val_compare_and_swap(&cmpxchg32_value, 0, 3);
0145 cmpxchg32_result_succeed = __sync_val_compare_and_swap(&cmpxchg32_value, 1, 2);
0146 #endif
0147
0148 return 0;
0149 }
0150
0151 __u64 xchg64_value = 1;
0152 __u64 xchg64_result = 0;
0153 __u32 xchg32_value = 1;
0154 __u32 xchg32_result = 0;
0155
0156 SEC("raw_tp/sys_enter")
0157 int xchg(const void *ctx)
0158 {
0159 if (pid != (bpf_get_current_pid_tgid() >> 32))
0160 return 0;
0161 #ifdef ENABLE_ATOMICS_TESTS
0162 __u64 val64 = 2;
0163 __u32 val32 = 2;
0164
0165 xchg64_result = __sync_lock_test_and_set(&xchg64_value, val64);
0166 xchg32_result = __sync_lock_test_and_set(&xchg32_value, val32);
0167 #endif
0168
0169 return 0;
0170 }