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