utils.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2013, Michael Ellerman, IBM Corp.
  4. */
  5. #ifndef _SELFTESTS_POWERPC_UTILS_H
  6. #define _SELFTESTS_POWERPC_UTILS_H
  7. #define __cacheline_aligned __attribute__((aligned(128)))
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11. #include <sys/signal.h>
  12. #include <linux/auxvec.h>
  13. #include <linux/perf_event.h>
  14. #include <asm/cputable.h>
  15. #include "reg.h"
  16. #include <unistd.h>
  17. #ifndef ARRAY_SIZE
  18. # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  19. #endif
  20. /* Avoid headaches with PRI?64 - just use %ll? always */
  21. typedef unsigned long long u64;
  22. typedef signed long long s64;
  23. /* Just for familiarity */
  24. typedef uint32_t u32;
  25. typedef uint16_t u16;
  26. typedef uint8_t u8;
  27. void test_harness_set_timeout(uint64_t time);
  28. int test_harness(int (test_function)(void), const char *name);
  29. int read_auxv(char *buf, ssize_t buf_size);
  30. void *find_auxv_entry(int type, char *auxv);
  31. void *get_auxv_entry(int type);
  32. #define BIND_CPU_ANY (-1)
  33. int pick_online_cpu(void);
  34. int bind_to_cpu(int cpu);
  35. int parse_intmax(const char *buffer, size_t count, intmax_t *result, int base);
  36. int parse_uintmax(const char *buffer, size_t count, uintmax_t *result, int base);
  37. int parse_int(const char *buffer, size_t count, int *result, int base);
  38. int parse_uint(const char *buffer, size_t count, unsigned int *result, int base);
  39. int parse_long(const char *buffer, size_t count, long *result, int base);
  40. int parse_ulong(const char *buffer, size_t count, unsigned long *result, int base);
  41. int read_file(const char *path, char *buf, size_t count, size_t *len);
  42. int write_file(const char *path, const char *buf, size_t count);
  43. int read_file_alloc(const char *path, char **buf, size_t *len);
  44. int read_long(const char *path, long *result, int base);
  45. int write_long(const char *path, long result, int base);
  46. int read_ulong(const char *path, unsigned long *result, int base);
  47. int write_ulong(const char *path, unsigned long result, int base);
  48. int read_debugfs_file(const char *debugfs_file, char *buf, size_t count);
  49. int write_debugfs_file(const char *debugfs_file, const char *buf, size_t count);
  50. int read_debugfs_int(const char *debugfs_file, int *result);
  51. int write_debugfs_int(const char *debugfs_file, int result);
  52. int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
  53. int perf_event_open_counter(unsigned int type,
  54. unsigned long config, int group_fd);
  55. int perf_event_enable(int fd);
  56. int perf_event_disable(int fd);
  57. int perf_event_reset(int fd);
  58. struct perf_event_read {
  59. __u64 nr;
  60. __u64 l1d_misses;
  61. };
  62. #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30)
  63. #include <sys/syscall.h>
  64. static inline pid_t gettid(void)
  65. {
  66. return syscall(SYS_gettid);
  67. }
  68. #endif
  69. static inline bool have_hwcap(unsigned long ftr)
  70. {
  71. return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
  72. }
  73. #ifdef AT_HWCAP2
  74. static inline bool have_hwcap2(unsigned long ftr2)
  75. {
  76. return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
  77. }
  78. #else
  79. static inline bool have_hwcap2(unsigned long ftr2)
  80. {
  81. return false;
  82. }
  83. #endif
  84. static inline char *auxv_base_platform(void)
  85. {
  86. return ((char *)get_auxv_entry(AT_BASE_PLATFORM));
  87. }
  88. static inline char *auxv_platform(void)
  89. {
  90. return ((char *)get_auxv_entry(AT_PLATFORM));
  91. }
  92. bool is_ppc64le(void);
  93. int using_hash_mmu(bool *using_hash);
  94. struct sigaction push_signal_handler(int sig, void (*fn)(int, siginfo_t *, void *));
  95. struct sigaction pop_signal_handler(int sig, struct sigaction old_handler);
  96. /* Yes, this is evil */
  97. #define FAIL_IF(x) \
  98. do { \
  99. if ((x)) { \
  100. fprintf(stderr, \
  101. "[FAIL] Test FAILED on line %d\n", __LINE__); \
  102. return 1; \
  103. } \
  104. } while (0)
  105. #define FAIL_IF_MSG(x, msg) \
  106. do { \
  107. if ((x)) { \
  108. fprintf(stderr, \
  109. "[FAIL] Test FAILED on line %d: %s\n", \
  110. __LINE__, msg); \
  111. return 1; \
  112. } \
  113. } while (0)
  114. #define FAIL_IF_EXIT(x) \
  115. do { \
  116. if ((x)) { \
  117. fprintf(stderr, \
  118. "[FAIL] Test FAILED on line %d\n", __LINE__); \
  119. _exit(1); \
  120. } \
  121. } while (0)
  122. #define FAIL_IF_EXIT_MSG(x, msg) \
  123. do { \
  124. if ((x)) { \
  125. fprintf(stderr, \
  126. "[FAIL] Test FAILED on line %d: %s\n", \
  127. __LINE__, msg); \
  128. _exit(1); \
  129. } \
  130. } while (0)
  131. /* The test harness uses this, yes it's gross */
  132. #define MAGIC_SKIP_RETURN_VALUE 99
  133. #define SKIP_IF(x) \
  134. do { \
  135. if ((x)) { \
  136. fprintf(stderr, \
  137. "[SKIP] Test skipped on line %d\n", __LINE__); \
  138. return MAGIC_SKIP_RETURN_VALUE; \
  139. } \
  140. } while (0)
  141. #define SKIP_IF_MSG(x, msg) \
  142. do { \
  143. if ((x)) { \
  144. fprintf(stderr, \
  145. "[SKIP] Test skipped on line %d: %s\n", \
  146. __LINE__, msg); \
  147. return MAGIC_SKIP_RETURN_VALUE; \
  148. } \
  149. } while (0)
  150. #define _str(s) #s
  151. #define str(s) _str(s)
  152. #define sigsafe_err(msg) ({ \
  153. ssize_t nbytes __attribute__((unused)); \
  154. nbytes = write(STDERR_FILENO, msg, strlen(msg)); })
  155. /* POWER9 feature */
  156. #ifndef PPC_FEATURE2_ARCH_3_00
  157. #define PPC_FEATURE2_ARCH_3_00 0x00800000
  158. #endif
  159. /* POWER10 feature */
  160. #ifndef PPC_FEATURE2_ARCH_3_1
  161. #define PPC_FEATURE2_ARCH_3_1 0x00040000
  162. #endif
  163. /* POWER10 features */
  164. #ifndef PPC_FEATURE2_MMA
  165. #define PPC_FEATURE2_MMA 0x00020000
  166. #endif
  167. #if defined(__powerpc64__)
  168. #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP]
  169. #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.gp_regs[PT_MSR]
  170. #elif defined(__powerpc__)
  171. #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
  172. #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_MSR]
  173. #else
  174. #error implement UCONTEXT_NIA
  175. #endif
  176. #endif /* _SELFTESTS_POWERPC_UTILS_H */