Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / testing / selftests / x86 / test_vdso.c
blob2352590117042ebf79ddd946a00d8c49d38d786e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ldt_gdt.c - Test cases for LDT and GDT access
4 * Copyright (c) 2011-2015 Andrew Lutomirski
5 */
7 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <sys/time.h>
11 #include <time.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <sys/syscall.h>
15 #include <dlfcn.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <sched.h>
19 #include <stdbool.h>
21 #ifndef SYS_getcpu
22 # ifdef __x86_64__
23 # define SYS_getcpu 309
24 # else
25 # define SYS_getcpu 318
26 # endif
27 #endif
29 /* max length of lines in /proc/self/maps - anything longer is skipped here */
30 #define MAPS_LINE_LEN 128
32 int nerrs = 0;
34 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
36 getcpu_t vgetcpu;
37 getcpu_t vdso_getcpu;
39 static void *vsyscall_getcpu(void)
41 #ifdef __x86_64__
42 FILE *maps;
43 char line[MAPS_LINE_LEN];
44 bool found = false;
46 maps = fopen("/proc/self/maps", "r");
47 if (!maps) /* might still be present, but ignore it here, as we test vDSO not vsyscall */
48 return NULL;
50 while (fgets(line, MAPS_LINE_LEN, maps)) {
51 char r, x;
52 void *start, *end;
53 char name[MAPS_LINE_LEN];
55 /* sscanf() is safe here as strlen(name) >= strlen(line) */
56 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
57 &start, &end, &r, &x, name) != 5)
58 continue;
60 if (strcmp(name, "[vsyscall]"))
61 continue;
63 /* assume entries are OK, as we test vDSO here not vsyscall */
64 found = true;
65 break;
68 fclose(maps);
70 if (!found) {
71 printf("Warning: failed to find vsyscall getcpu\n");
72 return NULL;
74 return (void *) (0xffffffffff600800);
75 #else
76 return NULL;
77 #endif
81 static void fill_function_pointers()
83 void *vdso = dlopen("linux-vdso.so.1",
84 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
85 if (!vdso)
86 vdso = dlopen("linux-gate.so.1",
87 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
88 if (!vdso) {
89 printf("[WARN]\tfailed to find vDSO\n");
90 return;
93 vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
94 if (!vdso_getcpu)
95 printf("Warning: failed to find getcpu in vDSO\n");
97 vgetcpu = (getcpu_t) vsyscall_getcpu();
100 static long sys_getcpu(unsigned * cpu, unsigned * node,
101 void* cache)
103 return syscall(__NR_getcpu, cpu, node, cache);
106 static void test_getcpu(void)
108 printf("[RUN]\tTesting getcpu...\n");
110 for (int cpu = 0; ; cpu++) {
111 cpu_set_t cpuset;
112 CPU_ZERO(&cpuset);
113 CPU_SET(cpu, &cpuset);
114 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
115 return;
117 unsigned cpu_sys, cpu_vdso, cpu_vsys,
118 node_sys, node_vdso, node_vsys;
119 long ret_sys, ret_vdso = 1, ret_vsys = 1;
120 unsigned node;
122 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
123 if (vdso_getcpu)
124 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
125 if (vgetcpu)
126 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
128 if (!ret_sys)
129 node = node_sys;
130 else if (!ret_vdso)
131 node = node_vdso;
132 else if (!ret_vsys)
133 node = node_vsys;
135 bool ok = true;
136 if (!ret_sys && (cpu_sys != cpu || node_sys != node))
137 ok = false;
138 if (!ret_vdso && (cpu_vdso != cpu || node_vdso != node))
139 ok = false;
140 if (!ret_vsys && (cpu_vsys != cpu || node_vsys != node))
141 ok = false;
143 printf("[%s]\tCPU %u:", ok ? "OK" : "FAIL", cpu);
144 if (!ret_sys)
145 printf(" syscall: cpu %u, node %u", cpu_sys, node_sys);
146 if (!ret_vdso)
147 printf(" vdso: cpu %u, node %u", cpu_vdso, node_vdso);
148 if (!ret_vsys)
149 printf(" vsyscall: cpu %u, node %u", cpu_vsys,
150 node_vsys);
151 printf("\n");
153 if (!ok)
154 nerrs++;
158 int main(int argc, char **argv)
160 fill_function_pointers();
162 test_getcpu();
164 return nerrs ? 1 : 0;