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