1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
6 #define _GNU_SOURCE /* For CPU_ZERO etc. */
17 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/utsname.h>
22 #include <asm/unistd.h>
23 #include <linux/limits.h>
27 static char auxv[4096];
29 int read_auxv(char *buf, ssize_t buf_size)
34 fd = open("/proc/self/auxv", O_RDONLY);
40 num = read(fd, buf, buf_size);
48 printf("overflowed auxv buffer\n");
59 void *find_auxv_entry(int type, char *auxv)
63 p = (ElfW(auxv_t) *)auxv;
65 while (p->a_type != AT_NULL) {
66 if (p->a_type == type)
75 void *get_auxv_entry(int type)
79 if (read_auxv(auxv, sizeof(auxv)))
82 p = find_auxv_entry(type, auxv);
84 return (void *)p->a_un.a_val;
89 int pick_online_cpu(void)
96 if (sched_getaffinity(0, sizeof(mask), &mask)) {
97 perror("sched_getaffinity");
101 /* We prefer a primary thread, but skip 0 */
102 for (cpu = 8; cpu < CPU_SETSIZE; cpu += 8)
103 if (CPU_ISSET(cpu, &mask))
106 /* Search for anything, but in reverse */
107 for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--)
108 if (CPU_ISSET(cpu, &mask))
111 printf("No cpus in affinity mask?!\n");
115 bool is_ppc64le(void)
127 return strcmp(uts.machine, "ppc64le") == 0;
130 int read_debugfs_file(char *debugfs_file, int *result)
136 strcpy(path, "/sys/kernel/debug/");
137 strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
139 if ((fd = open(path, O_RDONLY)) < 0)
142 if ((rc = read(fd, value, sizeof(value))) < 0)
146 *result = atoi(value);
152 int write_debugfs_file(char *debugfs_file, int result)
158 strcpy(path, "/sys/kernel/debug/");
159 strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
161 if ((fd = open(path, O_WRONLY)) < 0)
164 snprintf(value, 16, "%d", result);
166 if ((rc = write(fd, value, strlen(value))) < 0)
174 static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
175 int cpu, int group_fd, unsigned long flags)
177 return syscall(__NR_perf_event_open, hw_event, pid, cpu,
181 static void perf_event_attr_init(struct perf_event_attr *event_attr,
183 unsigned long config)
185 memset(event_attr, 0, sizeof(*event_attr));
187 event_attr->type = type;
188 event_attr->size = sizeof(struct perf_event_attr);
189 event_attr->config = config;
190 event_attr->read_format = PERF_FORMAT_GROUP;
191 event_attr->disabled = 1;
192 event_attr->exclude_kernel = 1;
193 event_attr->exclude_hv = 1;
194 event_attr->exclude_guest = 1;
197 int perf_event_open_counter(unsigned int type,
198 unsigned long config, int group_fd)
201 struct perf_event_attr event_attr;
203 perf_event_attr_init(&event_attr, type, config);
205 fd = perf_event_open(&event_attr, 0, -1, group_fd, 0);
208 perror("perf_event_open() failed");
213 int perf_event_enable(int fd)
215 if (ioctl(fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
216 perror("error while enabling perf events");
223 int perf_event_disable(int fd)
225 if (ioctl(fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP) == -1) {
226 perror("error disabling perf events");
233 int perf_event_reset(int fd)
235 if (ioctl(fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
236 perror("error resetting perf events");
243 static void sigill_handler(int signr, siginfo_t *info, void *unused)
245 static int warned = 0;
246 ucontext_t *ctx = (ucontext_t *)unused;
247 unsigned long *pc = &UCONTEXT_NIA(ctx);
249 /* mtspr 3,RS to check for move to DSCR below */
250 if ((*((unsigned int *)*pc) & 0xfc1fffff) == 0x7c0303a6) {
252 printf("WARNING: Skipping over dscr setup. Consider running 'ppc64_cpu --dscr=1' manually.\n");
255 printf("SIGILL at %p\n", pc);
260 void set_dscr(unsigned long val)
266 memset(&sa, 0, sizeof(sa));
267 sa.sa_sigaction = sigill_handler;
268 sa.sa_flags = SA_SIGINFO;
269 if (sigaction(SIGILL, &sa, NULL))
270 perror("sigill_handler");
274 asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR));