hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/tools/testing/selftests/powerpc/utils.c
....@@ -1,6 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
3
- * Licensed under GPLv2.
44 */
55
66 #define _GNU_SOURCE /* For CPU_ZERO etc. */
....@@ -11,12 +11,16 @@
1111 #include <link.h>
1212 #include <sched.h>
1313 #include <stdio.h>
14
+#include <stdlib.h>
1415 #include <string.h>
16
+#include <sys/ioctl.h>
1517 #include <sys/stat.h>
1618 #include <sys/sysinfo.h>
1719 #include <sys/types.h>
1820 #include <sys/utsname.h>
1921 #include <unistd.h>
22
+#include <asm/unistd.h>
23
+#include <linux/limits.h>
2024
2125 #include "utils.h"
2226
....@@ -134,3 +138,166 @@
134138
135139 return strcmp(uts.machine, "ppc64le") == 0;
136140 }
141
+
142
+int read_sysfs_file(char *fpath, char *result, size_t result_size)
143
+{
144
+ char path[PATH_MAX] = "/sys/";
145
+ int rc = -1, fd;
146
+
147
+ strncat(path, fpath, PATH_MAX - strlen(path) - 1);
148
+
149
+ if ((fd = open(path, O_RDONLY)) < 0)
150
+ return rc;
151
+
152
+ rc = read(fd, result, result_size);
153
+
154
+ close(fd);
155
+
156
+ if (rc < 0)
157
+ return rc;
158
+
159
+ return 0;
160
+}
161
+
162
+int read_debugfs_file(char *debugfs_file, int *result)
163
+{
164
+ int rc = -1, fd;
165
+ char path[PATH_MAX];
166
+ char value[16];
167
+
168
+ strcpy(path, "/sys/kernel/debug/");
169
+ strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
170
+
171
+ if ((fd = open(path, O_RDONLY)) < 0)
172
+ return rc;
173
+
174
+ if ((rc = read(fd, value, sizeof(value))) < 0)
175
+ return rc;
176
+
177
+ value[15] = 0;
178
+ *result = atoi(value);
179
+ close(fd);
180
+
181
+ return 0;
182
+}
183
+
184
+int write_debugfs_file(char *debugfs_file, int result)
185
+{
186
+ int rc = -1, fd;
187
+ char path[PATH_MAX];
188
+ char value[16];
189
+
190
+ strcpy(path, "/sys/kernel/debug/");
191
+ strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
192
+
193
+ if ((fd = open(path, O_WRONLY)) < 0)
194
+ return rc;
195
+
196
+ snprintf(value, 16, "%d", result);
197
+
198
+ if ((rc = write(fd, value, strlen(value))) < 0)
199
+ return rc;
200
+
201
+ close(fd);
202
+
203
+ return 0;
204
+}
205
+
206
+static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
207
+ int cpu, int group_fd, unsigned long flags)
208
+{
209
+ return syscall(__NR_perf_event_open, hw_event, pid, cpu,
210
+ group_fd, flags);
211
+}
212
+
213
+static void perf_event_attr_init(struct perf_event_attr *event_attr,
214
+ unsigned int type,
215
+ unsigned long config)
216
+{
217
+ memset(event_attr, 0, sizeof(*event_attr));
218
+
219
+ event_attr->type = type;
220
+ event_attr->size = sizeof(struct perf_event_attr);
221
+ event_attr->config = config;
222
+ event_attr->read_format = PERF_FORMAT_GROUP;
223
+ event_attr->disabled = 1;
224
+ event_attr->exclude_kernel = 1;
225
+ event_attr->exclude_hv = 1;
226
+ event_attr->exclude_guest = 1;
227
+}
228
+
229
+int perf_event_open_counter(unsigned int type,
230
+ unsigned long config, int group_fd)
231
+{
232
+ int fd;
233
+ struct perf_event_attr event_attr;
234
+
235
+ perf_event_attr_init(&event_attr, type, config);
236
+
237
+ fd = perf_event_open(&event_attr, 0, -1, group_fd, 0);
238
+
239
+ if (fd < 0)
240
+ perror("perf_event_open() failed");
241
+
242
+ return fd;
243
+}
244
+
245
+int perf_event_enable(int fd)
246
+{
247
+ if (ioctl(fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
248
+ perror("error while enabling perf events");
249
+ return -1;
250
+ }
251
+
252
+ return 0;
253
+}
254
+
255
+int perf_event_disable(int fd)
256
+{
257
+ if (ioctl(fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP) == -1) {
258
+ perror("error disabling perf events");
259
+ return -1;
260
+ }
261
+
262
+ return 0;
263
+}
264
+
265
+int perf_event_reset(int fd)
266
+{
267
+ if (ioctl(fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
268
+ perror("error resetting perf events");
269
+ return -1;
270
+ }
271
+
272
+ return 0;
273
+}
274
+
275
+int using_hash_mmu(bool *using_hash)
276
+{
277
+ char line[128];
278
+ FILE *f;
279
+ int rc;
280
+
281
+ f = fopen("/proc/cpuinfo", "r");
282
+ FAIL_IF(!f);
283
+
284
+ rc = 0;
285
+ while (fgets(line, sizeof(line), f) != NULL) {
286
+ if (!strcmp(line, "MMU : Hash\n") ||
287
+ !strcmp(line, "platform : Cell\n") ||
288
+ !strcmp(line, "platform : PowerMac\n")) {
289
+ *using_hash = true;
290
+ goto out;
291
+ }
292
+
293
+ if (strcmp(line, "MMU : Radix\n") == 0) {
294
+ *using_hash = false;
295
+ goto out;
296
+ }
297
+ }
298
+
299
+ rc = -1;
300
+out:
301
+ fclose(f);
302
+ return rc;
303
+}