atomics.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/bpf.h>
  3. #include <bpf/bpf_helpers.h>
  4. #include <bpf/bpf_tracing.h>
  5. #include <stdbool.h>
  6. #ifdef ENABLE_ATOMICS_TESTS
  7. bool skip_tests __attribute((__section__(".data"))) = false;
  8. #else
  9. bool skip_tests = true;
  10. #endif
  11. __u32 pid = 0;
  12. __u64 add64_value = 1;
  13. __u64 add64_result = 0;
  14. __u32 add32_value = 1;
  15. __u32 add32_result = 0;
  16. __u64 add_stack_value_copy = 0;
  17. __u64 add_stack_result = 0;
  18. __u64 add_noreturn_value = 1;
  19. SEC("raw_tp/sys_enter")
  20. int add(const void *ctx)
  21. {
  22. if (pid != (bpf_get_current_pid_tgid() >> 32))
  23. return 0;
  24. #ifdef ENABLE_ATOMICS_TESTS
  25. __u64 add_stack_value = 1;
  26. add64_result = __sync_fetch_and_add(&add64_value, 2);
  27. add32_result = __sync_fetch_and_add(&add32_value, 2);
  28. add_stack_result = __sync_fetch_and_add(&add_stack_value, 2);
  29. add_stack_value_copy = add_stack_value;
  30. __sync_fetch_and_add(&add_noreturn_value, 2);
  31. #endif
  32. return 0;
  33. }
  34. __s64 sub64_value = 1;
  35. __s64 sub64_result = 0;
  36. __s32 sub32_value = 1;
  37. __s32 sub32_result = 0;
  38. __s64 sub_stack_value_copy = 0;
  39. __s64 sub_stack_result = 0;
  40. __s64 sub_noreturn_value = 1;
  41. SEC("raw_tp/sys_enter")
  42. int sub(const void *ctx)
  43. {
  44. if (pid != (bpf_get_current_pid_tgid() >> 32))
  45. return 0;
  46. #ifdef ENABLE_ATOMICS_TESTS
  47. __u64 sub_stack_value = 1;
  48. sub64_result = __sync_fetch_and_sub(&sub64_value, 2);
  49. sub32_result = __sync_fetch_and_sub(&sub32_value, 2);
  50. sub_stack_result = __sync_fetch_and_sub(&sub_stack_value, 2);
  51. sub_stack_value_copy = sub_stack_value;
  52. __sync_fetch_and_sub(&sub_noreturn_value, 2);
  53. #endif
  54. return 0;
  55. }
  56. __u64 and64_value = (0x110ull << 32);
  57. __u64 and64_result = 0;
  58. __u32 and32_value = 0x110;
  59. __u32 and32_result = 0;
  60. __u64 and_noreturn_value = (0x110ull << 32);
  61. SEC("raw_tp/sys_enter")
  62. int and(const void *ctx)
  63. {
  64. if (pid != (bpf_get_current_pid_tgid() >> 32))
  65. return 0;
  66. #ifdef ENABLE_ATOMICS_TESTS
  67. and64_result = __sync_fetch_and_and(&and64_value, 0x011ull << 32);
  68. and32_result = __sync_fetch_and_and(&and32_value, 0x011);
  69. __sync_fetch_and_and(&and_noreturn_value, 0x011ull << 32);
  70. #endif
  71. return 0;
  72. }
  73. __u64 or64_value = (0x110ull << 32);
  74. __u64 or64_result = 0;
  75. __u32 or32_value = 0x110;
  76. __u32 or32_result = 0;
  77. __u64 or_noreturn_value = (0x110ull << 32);
  78. SEC("raw_tp/sys_enter")
  79. int or(const void *ctx)
  80. {
  81. if (pid != (bpf_get_current_pid_tgid() >> 32))
  82. return 0;
  83. #ifdef ENABLE_ATOMICS_TESTS
  84. or64_result = __sync_fetch_and_or(&or64_value, 0x011ull << 32);
  85. or32_result = __sync_fetch_and_or(&or32_value, 0x011);
  86. __sync_fetch_and_or(&or_noreturn_value, 0x011ull << 32);
  87. #endif
  88. return 0;
  89. }
  90. __u64 xor64_value = (0x110ull << 32);
  91. __u64 xor64_result = 0;
  92. __u32 xor32_value = 0x110;
  93. __u32 xor32_result = 0;
  94. __u64 xor_noreturn_value = (0x110ull << 32);
  95. SEC("raw_tp/sys_enter")
  96. int xor(const void *ctx)
  97. {
  98. if (pid != (bpf_get_current_pid_tgid() >> 32))
  99. return 0;
  100. #ifdef ENABLE_ATOMICS_TESTS
  101. xor64_result = __sync_fetch_and_xor(&xor64_value, 0x011ull << 32);
  102. xor32_result = __sync_fetch_and_xor(&xor32_value, 0x011);
  103. __sync_fetch_and_xor(&xor_noreturn_value, 0x011ull << 32);
  104. #endif
  105. return 0;
  106. }
  107. __u64 cmpxchg64_value = 1;
  108. __u64 cmpxchg64_result_fail = 0;
  109. __u64 cmpxchg64_result_succeed = 0;
  110. __u32 cmpxchg32_value = 1;
  111. __u32 cmpxchg32_result_fail = 0;
  112. __u32 cmpxchg32_result_succeed = 0;
  113. SEC("raw_tp/sys_enter")
  114. int cmpxchg(const void *ctx)
  115. {
  116. if (pid != (bpf_get_current_pid_tgid() >> 32))
  117. return 0;
  118. #ifdef ENABLE_ATOMICS_TESTS
  119. cmpxchg64_result_fail = __sync_val_compare_and_swap(&cmpxchg64_value, 0, 3);
  120. cmpxchg64_result_succeed = __sync_val_compare_and_swap(&cmpxchg64_value, 1, 2);
  121. cmpxchg32_result_fail = __sync_val_compare_and_swap(&cmpxchg32_value, 0, 3);
  122. cmpxchg32_result_succeed = __sync_val_compare_and_swap(&cmpxchg32_value, 1, 2);
  123. #endif
  124. return 0;
  125. }
  126. __u64 xchg64_value = 1;
  127. __u64 xchg64_result = 0;
  128. __u32 xchg32_value = 1;
  129. __u32 xchg32_result = 0;
  130. SEC("raw_tp/sys_enter")
  131. int xchg(const void *ctx)
  132. {
  133. if (pid != (bpf_get_current_pid_tgid() >> 32))
  134. return 0;
  135. #ifdef ENABLE_ATOMICS_TESTS
  136. __u64 val64 = 2;
  137. __u32 val32 = 2;
  138. xchg64_result = __sync_lock_test_and_set(&xchg64_value, val64);
  139. xchg32_result = __sync_lock_test_and_set(&xchg32_value, val32);
  140. #endif
  141. return 0;
  142. }