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