0001
0002
0003
0004
0005
0006 #ifndef _SELFTESTS_POWERPC_UTILS_H
0007 #define _SELFTESTS_POWERPC_UTILS_H
0008
0009 #define __cacheline_aligned __attribute__((aligned(128)))
0010
0011 #include <stdint.h>
0012 #include <stdbool.h>
0013 #include <linux/auxvec.h>
0014 #include <linux/perf_event.h>
0015 #include <asm/cputable.h>
0016 #include "reg.h"
0017
0018
0019 typedef unsigned long long u64;
0020 typedef signed long long s64;
0021
0022
0023 typedef uint32_t u32;
0024 typedef uint16_t u16;
0025 typedef uint8_t u8;
0026
0027 void test_harness_set_timeout(uint64_t time);
0028 int test_harness(int (test_function)(void), char *name);
0029
0030 int read_auxv(char *buf, ssize_t buf_size);
0031 void *find_auxv_entry(int type, char *auxv);
0032 void *get_auxv_entry(int type);
0033
0034 int pick_online_cpu(void);
0035
0036 int read_debugfs_file(char *debugfs_file, int *result);
0037 int write_debugfs_file(char *debugfs_file, int result);
0038 int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
0039 int perf_event_open_counter(unsigned int type,
0040 unsigned long config, int group_fd);
0041 int perf_event_enable(int fd);
0042 int perf_event_disable(int fd);
0043 int perf_event_reset(int fd);
0044
0045 struct perf_event_read {
0046 __u64 nr;
0047 __u64 l1d_misses;
0048 };
0049
0050 #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30)
0051 #include <unistd.h>
0052 #include <sys/syscall.h>
0053
0054 static inline pid_t gettid(void)
0055 {
0056 return syscall(SYS_gettid);
0057 }
0058 #endif
0059
0060 static inline bool have_hwcap(unsigned long ftr)
0061 {
0062 return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr;
0063 }
0064
0065 #ifdef AT_HWCAP2
0066 static inline bool have_hwcap2(unsigned long ftr2)
0067 {
0068 return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2;
0069 }
0070 #else
0071 static inline bool have_hwcap2(unsigned long ftr2)
0072 {
0073 return false;
0074 }
0075 #endif
0076
0077 static inline char *auxv_base_platform(void)
0078 {
0079 return ((char *)get_auxv_entry(AT_BASE_PLATFORM));
0080 }
0081
0082 static inline char *auxv_platform(void)
0083 {
0084 return ((char *)get_auxv_entry(AT_PLATFORM));
0085 }
0086
0087 bool is_ppc64le(void);
0088 int using_hash_mmu(bool *using_hash);
0089
0090
0091 #define FAIL_IF(x) \
0092 do { \
0093 if ((x)) { \
0094 fprintf(stderr, \
0095 "[FAIL] Test FAILED on line %d\n", __LINE__); \
0096 return 1; \
0097 } \
0098 } while (0)
0099
0100 #define FAIL_IF_EXIT(x) \
0101 do { \
0102 if ((x)) { \
0103 fprintf(stderr, \
0104 "[FAIL] Test FAILED on line %d\n", __LINE__); \
0105 _exit(1); \
0106 } \
0107 } while (0)
0108
0109
0110 #define MAGIC_SKIP_RETURN_VALUE 99
0111
0112 #define SKIP_IF(x) \
0113 do { \
0114 if ((x)) { \
0115 fprintf(stderr, \
0116 "[SKIP] Test skipped on line %d\n", __LINE__); \
0117 return MAGIC_SKIP_RETURN_VALUE; \
0118 } \
0119 } while (0)
0120
0121 #define SKIP_IF_MSG(x, msg) \
0122 do { \
0123 if ((x)) { \
0124 fprintf(stderr, \
0125 "[SKIP] Test skipped on line %d: %s\n", \
0126 __LINE__, msg); \
0127 return MAGIC_SKIP_RETURN_VALUE; \
0128 } \
0129 } while (0)
0130
0131 #define _str(s) #s
0132 #define str(s) _str(s)
0133
0134 #define sigsafe_err(msg) ({ \
0135 ssize_t nbytes __attribute__((unused)); \
0136 nbytes = write(STDERR_FILENO, msg, strlen(msg)); })
0137
0138
0139 #ifndef PPC_FEATURE2_ARCH_3_00
0140 #define PPC_FEATURE2_ARCH_3_00 0x00800000
0141 #endif
0142
0143
0144 #ifndef PPC_FEATURE2_ARCH_3_1
0145 #define PPC_FEATURE2_ARCH_3_1 0x00040000
0146 #endif
0147
0148
0149 #ifndef PPC_FEATURE2_MMA
0150 #define PPC_FEATURE2_MMA 0x00020000
0151 #endif
0152
0153 #if defined(__powerpc64__)
0154 #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP]
0155 #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.gp_regs[PT_MSR]
0156 #elif defined(__powerpc__)
0157 #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP]
0158 #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_MSR]
0159 #else
0160 #error implement UCONTEXT_NIA
0161 #endif
0162
0163 #endif