1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
4 */
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>
13 #include <signal.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/ioctl.h>
18 #include <sys/stat.h>
19 #include <sys/sysinfo.h>
20 #include <sys/types.h>
21 #include <sys/utsname.h>
22 #include <unistd.h>
23 #include <asm/unistd.h>
24 #include <linux/limits.h>
25
26 #include "utils.h"
27
28 static char auxv[4096];
29
read_auxv(char * buf,ssize_t buf_size)30 int 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;
55 out:
56 close(fd);
57 return rc;
58 }
59
find_auxv_entry(int type,char * auxv)60 void *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
get_auxv_entry(int type)76 void *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
pick_online_cpu(void)90 int pick_online_cpu(void)
91 {
92 int ncpus, cpu = -1;
93 cpu_set_t *mask;
94 size_t size;
95
96 ncpus = get_nprocs_conf();
97 size = CPU_ALLOC_SIZE(ncpus);
98 mask = CPU_ALLOC(ncpus);
99 if (!mask) {
100 perror("malloc");
101 return -1;
102 }
103
104 CPU_ZERO_S(size, mask);
105
106 if (sched_getaffinity(0, size, mask)) {
107 perror("sched_getaffinity");
108 goto done;
109 }
110
111 /* We prefer a primary thread, but skip 0 */
112 for (cpu = 8; cpu < ncpus; cpu += 8)
113 if (CPU_ISSET_S(cpu, size, mask))
114 goto done;
115
116 /* Search for anything, but in reverse */
117 for (cpu = ncpus - 1; cpu >= 0; cpu--)
118 if (CPU_ISSET_S(cpu, size, mask))
119 goto done;
120
121 printf("No cpus in affinity mask?!\n");
122
123 done:
124 CPU_FREE(mask);
125 return cpu;
126 }
127
is_ppc64le(void)128 bool 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 }
142
read_debugfs_file(char * debugfs_file,int * result)143 int 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
write_debugfs_file(char * debugfs_file,int result)165 int 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
perf_event_open(struct perf_event_attr * hw_event,pid_t pid,int cpu,int group_fd,unsigned long flags)187 static 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
perf_event_attr_init(struct perf_event_attr * event_attr,unsigned int type,unsigned long config)194 static 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
perf_event_open_counter(unsigned int type,unsigned long config,int group_fd)210 int 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
perf_event_enable(int fd)226 int 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
perf_event_disable(int fd)236 int 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
perf_event_reset(int fd)246 int 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
sigill_handler(int signr,siginfo_t * info,void * unused)256 static 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
set_dscr(unsigned long val)273 void 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 }
289