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 int pick_online_cpu(void); 35 36 int parse_intmax(const char *buffer, size_t count, intmax_t *result, int base); 37 int parse_uintmax(const char *buffer, size_t count, uintmax_t *result, int base); 38 int parse_int(const char *buffer, size_t count, int *result, int base); 39 int parse_uint(const char *buffer, size_t count, unsigned int *result, int base); 40 int parse_long(const char *buffer, size_t count, long *result, int base); 41 int parse_ulong(const char *buffer, size_t count, unsigned long *result, int base); 42 43 int read_file(const char *path, char *buf, size_t count, size_t *len); 44 int write_file(const char *path, const char *buf, size_t count); 45 int read_long(const char *path, long *result, int base); 46 int write_long(const char *path, long result, int base); 47 int read_ulong(const char *path, unsigned long *result, int base); 48 int write_ulong(const char *path, unsigned long result, int base); 49 int read_debugfs_file(const char *debugfs_file, char *buf, size_t count); 50 int write_debugfs_file(const char *debugfs_file, const char *buf, size_t count); 51 int read_debugfs_int(const char *debugfs_file, int *result); 52 int write_debugfs_int(const char *debugfs_file, int result); 53 int read_sysfs_file(char *debugfs_file, char *result, size_t result_size); 54 int perf_event_open_counter(unsigned int type, 55 unsigned long config, int group_fd); 56 int perf_event_enable(int fd); 57 int perf_event_disable(int fd); 58 int perf_event_reset(int fd); 59 60 struct perf_event_read { 61 __u64 nr; 62 __u64 l1d_misses; 63 }; 64 65 #if !defined(__GLIBC_PREREQ) || !__GLIBC_PREREQ(2, 30) 66 #include <unistd.h> 67 #include <sys/syscall.h> 68 69 static inline pid_t gettid(void) 70 { 71 return syscall(SYS_gettid); 72 } 73 #endif 74 75 static inline bool have_hwcap(unsigned long ftr) 76 { 77 return ((unsigned long)get_auxv_entry(AT_HWCAP) & ftr) == ftr; 78 } 79 80 #ifdef AT_HWCAP2 81 static inline bool have_hwcap2(unsigned long ftr2) 82 { 83 return ((unsigned long)get_auxv_entry(AT_HWCAP2) & ftr2) == ftr2; 84 } 85 #else 86 static inline bool have_hwcap2(unsigned long ftr2) 87 { 88 return false; 89 } 90 #endif 91 92 static inline char *auxv_base_platform(void) 93 { 94 return ((char *)get_auxv_entry(AT_BASE_PLATFORM)); 95 } 96 97 static inline char *auxv_platform(void) 98 { 99 return ((char *)get_auxv_entry(AT_PLATFORM)); 100 } 101 102 bool is_ppc64le(void); 103 int using_hash_mmu(bool *using_hash); 104 105 /* Yes, this is evil */ 106 #define FAIL_IF(x) \ 107 do { \ 108 if ((x)) { \ 109 fprintf(stderr, \ 110 "[FAIL] Test FAILED on line %d\n", __LINE__); \ 111 return 1; \ 112 } \ 113 } while (0) 114 115 #define FAIL_IF_EXIT(x) \ 116 do { \ 117 if ((x)) { \ 118 fprintf(stderr, \ 119 "[FAIL] Test FAILED on line %d\n", __LINE__); \ 120 _exit(1); \ 121 } \ 122 } while (0) 123 124 /* The test harness uses this, yes it's gross */ 125 #define MAGIC_SKIP_RETURN_VALUE 99 126 127 #define SKIP_IF(x) \ 128 do { \ 129 if ((x)) { \ 130 fprintf(stderr, \ 131 "[SKIP] Test skipped on line %d\n", __LINE__); \ 132 return MAGIC_SKIP_RETURN_VALUE; \ 133 } \ 134 } while (0) 135 136 #define SKIP_IF_MSG(x, msg) \ 137 do { \ 138 if ((x)) { \ 139 fprintf(stderr, \ 140 "[SKIP] Test skipped on line %d: %s\n", \ 141 __LINE__, msg); \ 142 return MAGIC_SKIP_RETURN_VALUE; \ 143 } \ 144 } while (0) 145 146 #define _str(s) #s 147 #define str(s) _str(s) 148 149 #define sigsafe_err(msg) ({ \ 150 ssize_t nbytes __attribute__((unused)); \ 151 nbytes = write(STDERR_FILENO, msg, strlen(msg)); }) 152 153 /* POWER9 feature */ 154 #ifndef PPC_FEATURE2_ARCH_3_00 155 #define PPC_FEATURE2_ARCH_3_00 0x00800000 156 #endif 157 158 /* POWER10 feature */ 159 #ifndef PPC_FEATURE2_ARCH_3_1 160 #define PPC_FEATURE2_ARCH_3_1 0x00040000 161 #endif 162 163 /* POWER10 features */ 164 #ifndef PPC_FEATURE2_MMA 165 #define PPC_FEATURE2_MMA 0x00020000 166 #endif 167 168 #if defined(__powerpc64__) 169 #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP] 170 #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.gp_regs[PT_MSR] 171 #elif defined(__powerpc__) 172 #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP] 173 #define UCONTEXT_MSR(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_MSR] 174 #else 175 #error implement UCONTEXT_NIA 176 #endif 177 178 #endif /* _SELFTESTS_POWERPC_UTILS_H */ 179