1 /* SPDX-License-Identifier: GPL-2.0 */
9 #include <sys/syscall.h>
15 #include <sys/ucontext.h>
30 # define SYS_getcpu 309
32 # define SYS_getcpu 318
36 /* max length of lines in /proc/self/maps - anything longer is skipped here */
37 #define MAPS_LINE_LEN 128
39 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
43 memset(&sa, 0, sizeof(sa));
44 sa.sa_sigaction = handler;
45 sa.sa_flags = SA_SIGINFO | flags;
46 sigemptyset(&sa.sa_mask);
47 if (sigaction(sig, &sa, 0))
51 /* vsyscalls and vDSO */
52 bool should_read_vsyscall = false;
54 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
55 gtod_t vgtod = (gtod_t)VSYS(0xffffffffff600000);
58 typedef int (*vgettime_t)(clockid_t, struct timespec *);
59 vgettime_t vdso_gettime;
61 typedef long (*time_func_t)(time_t *t);
62 time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400);
63 time_func_t vdso_time;
65 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
66 getcpu_t vgetcpu = (getcpu_t)VSYS(0xffffffffff600800);
69 static void init_vdso(void)
71 void *vdso = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
73 vdso = dlopen("linux-gate.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
75 printf("[WARN]\tfailed to find vDSO\n");
79 vdso_gtod = (gtod_t)dlsym(vdso, "__vdso_gettimeofday");
81 printf("[WARN]\tfailed to find gettimeofday in vDSO\n");
83 vdso_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
85 printf("[WARN]\tfailed to find clock_gettime in vDSO\n");
87 vdso_time = (time_func_t)dlsym(vdso, "__vdso_time");
89 printf("[WARN]\tfailed to find time in vDSO\n");
91 vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
93 /* getcpu() was never wired up in the 32-bit vDSO. */
94 printf("[%s]\tfailed to find getcpu in vDSO\n",
95 sizeof(long) == 8 ? "WARN" : "NOTE");
99 static int init_vsys(void)
104 char line[MAPS_LINE_LEN];
107 maps = fopen("/proc/self/maps", "r");
109 printf("[WARN]\tCould not open /proc/self/maps -- assuming vsyscall is r-x\n");
110 should_read_vsyscall = true;
114 while (fgets(line, MAPS_LINE_LEN, maps)) {
117 char name[MAPS_LINE_LEN];
119 /* sscanf() is safe here as strlen(name) >= strlen(line) */
120 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
121 &start, &end, &r, &x, name) != 5)
124 if (strcmp(name, "[vsyscall]"))
127 printf("\tvsyscall map: %s", line);
129 if (start != (void *)0xffffffffff600000 ||
130 end != (void *)0xffffffffff601000) {
131 printf("[FAIL]\taddress range is nonsense\n");
135 printf("\tvsyscall permissions are %c-%c\n", r, x);
136 should_read_vsyscall = (r == 'r');
150 printf("\tno vsyscall map in /proc/self/maps\n");
151 should_read_vsyscall = false;
164 static inline long sys_gtod(struct timeval *tv, struct timezone *tz)
166 return syscall(SYS_gettimeofday, tv, tz);
169 static inline int sys_clock_gettime(clockid_t id, struct timespec *ts)
171 return syscall(SYS_clock_gettime, id, ts);
174 static inline long sys_time(time_t *t)
176 return syscall(SYS_time, t);
179 static inline long sys_getcpu(unsigned * cpu, unsigned * node,
182 return syscall(SYS_getcpu, cpu, node, cache);
185 static jmp_buf jmpbuf;
187 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
189 siglongjmp(jmpbuf, 1);
192 static double tv_diff(const struct timeval *a, const struct timeval *b)
194 return (double)(a->tv_sec - b->tv_sec) +
195 (double)((int)a->tv_usec - (int)b->tv_usec) * 1e-6;
198 static int check_gtod(const struct timeval *tv_sys1,
199 const struct timeval *tv_sys2,
200 const struct timezone *tz_sys,
202 const struct timeval *tv_other,
203 const struct timezone *tz_other)
208 if (tz_other && (tz_sys->tz_minuteswest != tz_other->tz_minuteswest || tz_sys->tz_dsttime != tz_other->tz_dsttime)) {
209 printf("[FAIL] %s tz mismatch\n", which);
213 d1 = tv_diff(tv_other, tv_sys1);
214 d2 = tv_diff(tv_sys2, tv_other);
215 printf("\t%s time offsets: %lf %lf\n", which, d1, d2);
217 if (d1 < 0 || d2 < 0) {
218 printf("[FAIL]\t%s time was inconsistent with the syscall\n", which);
221 printf("[OK]\t%s gettimeofday()'s timeval was okay\n", which);
227 static int test_gtod(void)
229 struct timeval tv_sys1, tv_sys2, tv_vdso, tv_vsys;
230 struct timezone tz_sys, tz_vdso, tz_vsys;
235 printf("[RUN]\ttest gettimeofday()\n");
237 if (sys_gtod(&tv_sys1, &tz_sys) != 0)
238 err(1, "syscall gettimeofday");
240 ret_vdso = vdso_gtod(&tv_vdso, &tz_vdso);
242 ret_vsys = vgtod(&tv_vsys, &tz_vsys);
243 if (sys_gtod(&tv_sys2, &tz_sys) != 0)
244 err(1, "syscall gettimeofday");
248 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vDSO", &tv_vdso, &tz_vdso);
250 printf("[FAIL]\tvDSO gettimeofday() failed: %ld\n", ret_vdso);
257 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vsyscall", &tv_vsys, &tz_vsys);
259 printf("[FAIL]\tvsys gettimeofday() failed: %ld\n", ret_vsys);
267 static int test_time(void) {
270 printf("[RUN]\ttest time()\n");
271 long t_sys1, t_sys2, t_vdso = 0, t_vsys = 0;
272 long t2_sys1 = -1, t2_sys2 = -1, t2_vdso = -1, t2_vsys = -1;
273 t_sys1 = sys_time(&t2_sys1);
275 t_vdso = vdso_time(&t2_vdso);
277 t_vsys = vtime(&t2_vsys);
278 t_sys2 = sys_time(&t2_sys2);
279 if (t_sys1 < 0 || t_sys1 != t2_sys1 || t_sys2 < 0 || t_sys2 != t2_sys2) {
280 printf("[FAIL]\tsyscall failed (ret1:%ld output1:%ld ret2:%ld output2:%ld)\n", t_sys1, t2_sys1, t_sys2, t2_sys2);
286 if (t_vdso < 0 || t_vdso != t2_vdso) {
287 printf("[FAIL]\tvDSO failed (ret:%ld output:%ld)\n", t_vdso, t2_vdso);
289 } else if (t_vdso < t_sys1 || t_vdso > t_sys2) {
290 printf("[FAIL]\tvDSO returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vdso, t_sys2);
293 printf("[OK]\tvDSO time() is okay\n");
298 if (t_vsys < 0 || t_vsys != t2_vsys) {
299 printf("[FAIL]\tvsyscall failed (ret:%ld output:%ld)\n", t_vsys, t2_vsys);
301 } else if (t_vsys < t_sys1 || t_vsys > t_sys2) {
302 printf("[FAIL]\tvsyscall returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vsys, t_sys2);
305 printf("[OK]\tvsyscall time() is okay\n");
312 static int test_getcpu(int cpu)
315 long ret_sys, ret_vdso = -1, ret_vsys = -1;
317 printf("[RUN]\tgetcpu() on CPU %d\n", cpu);
321 CPU_SET(cpu, &cpuset);
322 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
323 printf("[SKIP]\tfailed to force CPU %d\n", cpu);
327 unsigned cpu_sys, cpu_vdso, cpu_vsys, node_sys, node_vdso, node_vsys;
329 bool have_node = false;
330 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
332 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
334 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
337 if (cpu_sys != cpu) {
338 printf("[FAIL]\tsyscall reported CPU %hu but should be %d\n", cpu_sys, cpu);
348 printf("[FAIL]\tvDSO getcpu() failed\n");
356 if (cpu_vdso != cpu) {
357 printf("[FAIL]\tvDSO reported CPU %hu but should be %d\n", cpu_vdso, cpu);
360 printf("[OK]\tvDSO reported correct CPU\n");
363 if (node_vdso != node) {
364 printf("[FAIL]\tvDSO reported node %hu but should be %hu\n", node_vdso, node);
367 printf("[OK]\tvDSO reported correct node\n");
374 printf("[FAIL]\tvsyscall getcpu() failed\n");
382 if (cpu_vsys != cpu) {
383 printf("[FAIL]\tvsyscall reported CPU %hu but should be %d\n", cpu_vsys, cpu);
386 printf("[OK]\tvsyscall reported correct CPU\n");
389 if (node_vsys != node) {
390 printf("[FAIL]\tvsyscall reported node %hu but should be %hu\n", node_vsys, node);
393 printf("[OK]\tvsyscall reported correct node\n");
401 static int test_vsys_r(void)
404 printf("[RUN]\tChecking read access to the vsyscall page\n");
406 if (sigsetjmp(jmpbuf, 1) == 0) {
407 *(volatile int *)0xffffffffff600000;
413 if (can_read && !should_read_vsyscall) {
414 printf("[FAIL]\tWe have read access, but we shouldn't\n");
416 } else if (!can_read && should_read_vsyscall) {
417 printf("[FAIL]\tWe don't have read access, but we should\n");
420 printf("[OK]\tgot expected result\n");
429 #define X86_EFLAGS_TF (1UL << 8)
430 static volatile sig_atomic_t num_vsyscall_traps;
432 static unsigned long get_eflags(void)
434 unsigned long eflags;
435 asm volatile ("pushfq\n\tpopq %0" : "=rm" (eflags));
439 static void set_eflags(unsigned long eflags)
441 asm volatile ("pushq %0\n\tpopfq" : : "rm" (eflags) : "flags");
444 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
446 ucontext_t *ctx = (ucontext_t *)ctx_void;
447 unsigned long ip = ctx->uc_mcontext.gregs[REG_RIP];
449 if (((ip ^ 0xffffffffff600000UL) & ~0xfffUL) == 0)
450 num_vsyscall_traps++;
453 static int test_native_vsyscall(void)
461 printf("[RUN]\tchecking for native vsyscall\n");
462 sethandler(SIGTRAP, sigtrap, 0);
463 set_eflags(get_eflags() | X86_EFLAGS_TF);
465 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
468 * If vsyscalls are emulated, we expect a single trap in the
469 * vsyscall page -- the call instruction will trap with RIP
470 * pointing to the entry point before emulation takes over.
471 * In native mode, we expect two traps, since whatever code
472 * the vsyscall page contains will be more than just a ret
475 is_native = (num_vsyscall_traps > 1);
477 printf("\tvsyscalls are %s (%d instructions in vsyscall page)\n",
478 (is_native ? "native" : "emulated"),
479 (int)num_vsyscall_traps);
485 int main(int argc, char **argv)
490 nerrs += init_vsys();
492 nerrs += test_gtod();
493 nerrs += test_time();
494 nerrs += test_getcpu(0);
495 nerrs += test_getcpu(1);
497 sethandler(SIGSEGV, sigsegv, 0);
498 nerrs += test_vsys_r();
501 nerrs += test_native_vsyscall();
504 return nerrs ? 1 : 0;