Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / powerpc / utils.c
blobaa8fc1e6365bf8e047b3cf4d49a6eded6e5837fd
1 /*
2 * Copyright 2013-2015, Michael Ellerman, IBM Corp.
3 * Licensed under GPLv2.
4 */
6 #define _GNU_SOURCE /* For CPU_ZERO etc. */
8 #include <elf.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <link.h>
12 #include <sched.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/utsname.h>
18 #include <unistd.h>
20 #include "utils.h"
22 static char auxv[4096];
24 int read_auxv(char *buf, ssize_t buf_size)
26 ssize_t num;
27 int rc, fd;
29 fd = open("/proc/self/auxv", O_RDONLY);
30 if (fd == -1) {
31 perror("open");
32 return -errno;
35 num = read(fd, buf, buf_size);
36 if (num < 0) {
37 perror("read");
38 rc = -EIO;
39 goto out;
42 if (num > buf_size) {
43 printf("overflowed auxv buffer\n");
44 rc = -EOVERFLOW;
45 goto out;
48 rc = 0;
49 out:
50 close(fd);
51 return rc;
54 void *find_auxv_entry(int type, char *auxv)
56 ElfW(auxv_t) *p;
58 p = (ElfW(auxv_t) *)auxv;
60 while (p->a_type != AT_NULL) {
61 if (p->a_type == type)
62 return p;
64 p++;
67 return NULL;
70 void *get_auxv_entry(int type)
72 ElfW(auxv_t) *p;
74 if (read_auxv(auxv, sizeof(auxv)))
75 return NULL;
77 p = find_auxv_entry(type, auxv);
78 if (p)
79 return (void *)p->a_un.a_val;
81 return NULL;
84 int pick_online_cpu(void)
86 cpu_set_t mask;
87 int cpu;
89 CPU_ZERO(&mask);
91 if (sched_getaffinity(0, sizeof(mask), &mask)) {
92 perror("sched_getaffinity");
93 return -1;
96 /* We prefer a primary thread, but skip 0 */
97 for (cpu = 8; cpu < CPU_SETSIZE; cpu += 8)
98 if (CPU_ISSET(cpu, &mask))
99 return cpu;
101 /* Search for anything, but in reverse */
102 for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--)
103 if (CPU_ISSET(cpu, &mask))
104 return cpu;
106 printf("No cpus in affinity mask?!\n");
107 return -1;
110 bool is_ppc64le(void)
112 struct utsname uts;
113 int rc;
115 errno = 0;
116 rc = uname(&uts);
117 if (rc) {
118 perror("uname");
119 return false;
122 return strcmp(uts.machine, "ppc64le") == 0;