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