blob: 176102eca994c66610f8c2672fbc6162a2e68779 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#define _GNU_SOURCE /* For CPU_ZERO etc. */
7
8#include <elf.h>
9#include <errno.h>
10#include <fcntl.h>
11#include <link.h>
12#include <sched.h>
David Brazdil0f672f62019-12-10 10:32:29 +000013#include <signal.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <stdio.h>
David Brazdil0f672f62019-12-10 10:32:29 +000015#include <stdlib.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <string.h>
David Brazdil0f672f62019-12-10 10:32:29 +000017#include <sys/ioctl.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018#include <sys/stat.h>
Olivier Deprez0e641232021-09-23 10:07:05 +020019#include <sys/sysinfo.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020#include <sys/types.h>
21#include <sys/utsname.h>
22#include <unistd.h>
David Brazdil0f672f62019-12-10 10:32:29 +000023#include <asm/unistd.h>
24#include <linux/limits.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025
26#include "utils.h"
27
28static char auxv[4096];
29
30int read_auxv(char *buf, ssize_t buf_size)
31{
32 ssize_t num;
33 int rc, fd;
34
35 fd = open("/proc/self/auxv", O_RDONLY);
36 if (fd == -1) {
37 perror("open");
38 return -errno;
39 }
40
41 num = read(fd, buf, buf_size);
42 if (num < 0) {
43 perror("read");
44 rc = -EIO;
45 goto out;
46 }
47
48 if (num > buf_size) {
49 printf("overflowed auxv buffer\n");
50 rc = -EOVERFLOW;
51 goto out;
52 }
53
54 rc = 0;
55out:
56 close(fd);
57 return rc;
58}
59
60void *find_auxv_entry(int type, char *auxv)
61{
62 ElfW(auxv_t) *p;
63
64 p = (ElfW(auxv_t) *)auxv;
65
66 while (p->a_type != AT_NULL) {
67 if (p->a_type == type)
68 return p;
69
70 p++;
71 }
72
73 return NULL;
74}
75
76void *get_auxv_entry(int type)
77{
78 ElfW(auxv_t) *p;
79
80 if (read_auxv(auxv, sizeof(auxv)))
81 return NULL;
82
83 p = find_auxv_entry(type, auxv);
84 if (p)
85 return (void *)p->a_un.a_val;
86
87 return NULL;
88}
89
90int pick_online_cpu(void)
91{
Olivier Deprez0e641232021-09-23 10:07:05 +020092 int ncpus, cpu = -1;
93 cpu_set_t *mask;
94 size_t size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095
Olivier Deprez0e641232021-09-23 10:07:05 +020096 ncpus = get_nprocs_conf();
97 size = CPU_ALLOC_SIZE(ncpus);
98 mask = CPU_ALLOC(ncpus);
99 if (!mask) {
100 perror("malloc");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101 return -1;
102 }
103
Olivier Deprez0e641232021-09-23 10:07:05 +0200104 CPU_ZERO_S(size, mask);
105
106 if (sched_getaffinity(0, size, mask)) {
107 perror("sched_getaffinity");
108 goto done;
109 }
110
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111 /* We prefer a primary thread, but skip 0 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200112 for (cpu = 8; cpu < ncpus; cpu += 8)
113 if (CPU_ISSET_S(cpu, size, mask))
114 goto done;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115
116 /* Search for anything, but in reverse */
Olivier Deprez0e641232021-09-23 10:07:05 +0200117 for (cpu = ncpus - 1; cpu >= 0; cpu--)
118 if (CPU_ISSET_S(cpu, size, mask))
119 goto done;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120
121 printf("No cpus in affinity mask?!\n");
Olivier Deprez0e641232021-09-23 10:07:05 +0200122
123done:
124 CPU_FREE(mask);
125 return cpu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126}
127
128bool is_ppc64le(void)
129{
130 struct utsname uts;
131 int rc;
132
133 errno = 0;
134 rc = uname(&uts);
135 if (rc) {
136 perror("uname");
137 return false;
138 }
139
140 return strcmp(uts.machine, "ppc64le") == 0;
141}
David Brazdil0f672f62019-12-10 10:32:29 +0000142
143int read_debugfs_file(char *debugfs_file, int *result)
144{
145 int rc = -1, fd;
146 char path[PATH_MAX];
147 char value[16];
148
149 strcpy(path, "/sys/kernel/debug/");
150 strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
151
152 if ((fd = open(path, O_RDONLY)) < 0)
153 return rc;
154
155 if ((rc = read(fd, value, sizeof(value))) < 0)
156 return rc;
157
158 value[15] = 0;
159 *result = atoi(value);
160 close(fd);
161
162 return 0;
163}
164
165int write_debugfs_file(char *debugfs_file, int result)
166{
167 int rc = -1, fd;
168 char path[PATH_MAX];
169 char value[16];
170
171 strcpy(path, "/sys/kernel/debug/");
172 strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
173
174 if ((fd = open(path, O_WRONLY)) < 0)
175 return rc;
176
177 snprintf(value, 16, "%d", result);
178
179 if ((rc = write(fd, value, strlen(value))) < 0)
180 return rc;
181
182 close(fd);
183
184 return 0;
185}
186
187static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
188 int cpu, int group_fd, unsigned long flags)
189{
190 return syscall(__NR_perf_event_open, hw_event, pid, cpu,
191 group_fd, flags);
192}
193
194static void perf_event_attr_init(struct perf_event_attr *event_attr,
195 unsigned int type,
196 unsigned long config)
197{
198 memset(event_attr, 0, sizeof(*event_attr));
199
200 event_attr->type = type;
201 event_attr->size = sizeof(struct perf_event_attr);
202 event_attr->config = config;
203 event_attr->read_format = PERF_FORMAT_GROUP;
204 event_attr->disabled = 1;
205 event_attr->exclude_kernel = 1;
206 event_attr->exclude_hv = 1;
207 event_attr->exclude_guest = 1;
208}
209
210int perf_event_open_counter(unsigned int type,
211 unsigned long config, int group_fd)
212{
213 int fd;
214 struct perf_event_attr event_attr;
215
216 perf_event_attr_init(&event_attr, type, config);
217
218 fd = perf_event_open(&event_attr, 0, -1, group_fd, 0);
219
220 if (fd < 0)
221 perror("perf_event_open() failed");
222
223 return fd;
224}
225
226int perf_event_enable(int fd)
227{
228 if (ioctl(fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
229 perror("error while enabling perf events");
230 return -1;
231 }
232
233 return 0;
234}
235
236int perf_event_disable(int fd)
237{
238 if (ioctl(fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP) == -1) {
239 perror("error disabling perf events");
240 return -1;
241 }
242
243 return 0;
244}
245
246int perf_event_reset(int fd)
247{
248 if (ioctl(fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
249 perror("error resetting perf events");
250 return -1;
251 }
252
253 return 0;
254}
255
256static void sigill_handler(int signr, siginfo_t *info, void *unused)
257{
258 static int warned = 0;
259 ucontext_t *ctx = (ucontext_t *)unused;
260 unsigned long *pc = &UCONTEXT_NIA(ctx);
261
262 /* mtspr 3,RS to check for move to DSCR below */
263 if ((*((unsigned int *)*pc) & 0xfc1fffff) == 0x7c0303a6) {
264 if (!warned++)
265 printf("WARNING: Skipping over dscr setup. Consider running 'ppc64_cpu --dscr=1' manually.\n");
266 *pc += 4;
267 } else {
268 printf("SIGILL at %p\n", pc);
269 abort();
270 }
271}
272
273void set_dscr(unsigned long val)
274{
275 static int init = 0;
276 struct sigaction sa;
277
278 if (!init) {
279 memset(&sa, 0, sizeof(sa));
280 sa.sa_sigaction = sigill_handler;
281 sa.sa_flags = SA_SIGINFO;
282 if (sigaction(SIGILL, &sa, NULL))
283 perror("sigill_handler");
284 init = 1;
285 }
286
287 asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR));
288}