utils.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <stdbool.h>
  10. #include <linux/auxvec.h>
  11. #include <linux/perf_event.h>
  12. #include <asm/cputable.h>
  13. #include "reg.h"
  14. /* Avoid headaches with PRI?64 - just use %ll? always */
  15. typedef unsigned long long u64;
  16. typedef signed long long s64;
  17. /* Just for familiarity */
  18. typedef uint32_t u32;
  19. typedef uint16_t u16;
  20. typedef uint8_t u8;
  21. void test_harness_set_timeout(uint64_t time);
  22. int test_harness(int (test_function)(void), char *name);
  23. int read_auxv(char *buf, ssize_t buf_size);
  24. void *find_auxv_entry(int type, char *auxv);
  25. void *get_auxv_entry(int type);
  26. int pick_online_cpu(void);
  27. int read_debugfs_file(char *debugfs_file, int *result);
  28. int write_debugfs_file(char *debugfs_file, int result);
  29. int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
  30. int perf_event_open_counter(unsigned int type,
  31. unsigned long config, int group_fd);
  32. int perf_event_enable(int fd);
  33. int perf_event_disable(int fd);
  34. int perf_event_reset(int fd);
  35. struct perf_event_read {
  36. __u64 nr;
  37. __u64 l1d_misses;
  38. };
  39. #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30)
  40. #include <unistd.h>
  41. #include <sys/syscall.h>
  42. static inline pid_t gettid(void)
  43. {
  44. return syscall(SYS_gettid);
  45. }
  46. #endif
  47. static inline bool have_hwcap(unsigned long ftr)
  48. {
  49. return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
  50. }
  51. #ifdef AT_HWCAP2
  52. static inline bool have_hwcap2(unsigned long ftr2)
  53. {
  54. return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
  55. }
  56. #else
  57. static inline bool have_hwcap2(unsigned long ftr2)
  58. {
  59. return false;
  60. }
  61. #endif
  62. static inline char *auxv_base_platform(void)
  63. {
  64. return ((char *)get_auxv_entry(AT_BASE_PLATFORM));
  65. }
  66. static inline char *auxv_platform(void)
  67. {
  68. return ((char *)get_auxv_entry(AT_PLATFORM));
  69. }
  70. bool is_ppc64le(void);
  71. int using_hash_mmu(bool *using_hash);
  72. /* Yes, this is evil */
  73. #define FAIL_IF(x) \
  74. do { \
  75. if ((x)) { \
  76. fprintf(stderr, \
  77. "[FAIL] Test FAILED on line %d\n", __LINE__); \
  78. return 1; \
  79. } \
  80. } while (0)
  81. #define FAIL_IF_EXIT(x) \
  82. do { \
  83. if ((x)) { \
  84. fprintf(stderr, \
  85. "[FAIL] Test FAILED on line %d\n", __LINE__); \
  86. _exit(1); \
  87. } \
  88. } while (0)
  89. /* The test harness uses this, yes it's gross */
  90. #define MAGIC_SKIP_RETURN_VALUE 99
  91. #define SKIP_IF(x) \
  92. do { \
  93. if ((x)) { \
  94. fprintf(stderr, \
  95. "[SKIP] Test skipped on line %d\n", __LINE__); \
  96. return MAGIC_SKIP_RETURN_VALUE; \
  97. } \
  98. } while (0)
  99. #define SKIP_IF_MSG(x, msg) \
  100. do { \
  101. if ((x)) { \
  102. fprintf(stderr, \
  103. "[SKIP] Test skipped on line %d: %s\n", \
  104. __LINE__, msg); \
  105. return MAGIC_SKIP_RETURN_VALUE; \
  106. } \
  107. } while (0)
  108. #define _str(s) #s
  109. #define str(s) _str(s)
  110. #define sigsafe_err(msg) ({ \
  111. ssize_t nbytes __attribute__((unused)); \
  112. nbytes = write(STDERR_FILENO, msg, strlen(msg)); })
  113. /* POWER9 feature */
  114. #ifndef PPC_FEATURE2_ARCH_3_00
  115. #define PPC_FEATURE2_ARCH_3_00 0x00800000
  116. #endif
  117. /* POWER10 feature */
  118. #ifndef PPC_FEATURE2_ARCH_3_1
  119. #define PPC_FEATURE2_ARCH_3_1 0x00040000
  120. #endif
  121. /* POWER10 features */
  122. #ifndef PPC_FEATURE2_MMA
  123. #define PPC_FEATURE2_MMA 0x00020000
  124. #endif
  125. #if defined(__powerpc64__)
  126. #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP]
  127. #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.gp_regs[PT_MSR]
  128. #elif defined(__powerpc__)
  129. #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
  130. #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_MSR]
  131. #else
  132. #error implement UCONTEXT_NIA
  133. #endif
  134. #endif /* _SELFTESTS_POWERPC_UTILS_H */