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