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