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 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
40 memset(&sa, 0, sizeof(sa));
41 sa.sa_sigaction = handler;
42 sa.sa_flags = SA_SIGINFO | flags;
43 sigemptyset(&sa.sa_mask);
44 if (sigaction(sig, &sa, 0))
48 /* vsyscalls and vDSO */
49 bool should_read_vsyscall = false;
51 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
52 gtod_t vgtod = (gtod_t)VSYS(0xffffffffff600000);
55 typedef int (*vgettime_t)(clockid_t, struct timespec *);
56 vgettime_t vdso_gettime;
58 typedef long (*time_func_t)(time_t *t);
59 time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400);
60 time_func_t vdso_time;
62 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
63 getcpu_t vgetcpu = (getcpu_t)VSYS(0xffffffffff600800);
66 static void init_vdso(void)
68 void *vdso = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
70 vdso = dlopen("linux-gate.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
72 printf("[WARN]\tfailed to find vDSO\n");
76 vdso_gtod = (gtod_t)dlsym(vdso, "__vdso_gettimeofday");
78 printf("[WARN]\tfailed to find gettimeofday in vDSO\n");
80 vdso_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
82 printf("[WARN]\tfailed to find clock_gettime in vDSO\n");
84 vdso_time = (time_func_t)dlsym(vdso, "__vdso_time");
86 printf("[WARN]\tfailed to find time in vDSO\n");
88 vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
90 /* getcpu() was never wired up in the 32-bit vDSO. */
91 printf("[%s]\tfailed to find getcpu in vDSO\n",
92 sizeof(long) == 8 ? "WARN" : "NOTE");
96 static int init_vsys(void)
104 maps = fopen("/proc/self/maps", "r");
106 printf("[WARN]\tCould not open /proc/self/maps -- assuming vsyscall is r-x\n");
107 should_read_vsyscall = true;
111 while (fgets(line, sizeof(line), maps)) {
115 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
116 &start, &end, &r, &x, name) != 5)
119 if (strcmp(name, "[vsyscall]"))
122 printf("\tvsyscall map: %s", line);
124 if (start != (void *)0xffffffffff600000 ||
125 end != (void *)0xffffffffff601000) {
126 printf("[FAIL]\taddress range is nonsense\n");
130 printf("\tvsyscall permissions are %c-%c\n", r, x);
131 should_read_vsyscall = (r == 'r');
145 printf("\tno vsyscall map in /proc/self/maps\n");
146 should_read_vsyscall = false;
159 static inline long sys_gtod(struct timeval *tv, struct timezone *tz)
161 return syscall(SYS_gettimeofday, tv, tz);
164 static inline int sys_clock_gettime(clockid_t id, struct timespec *ts)
166 return syscall(SYS_clock_gettime, id, ts);
169 static inline long sys_time(time_t *t)
171 return syscall(SYS_time, t);
174 static inline long sys_getcpu(unsigned * cpu, unsigned * node,
177 return syscall(SYS_getcpu, cpu, node, cache);
180 static jmp_buf jmpbuf;
182 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
184 siglongjmp(jmpbuf, 1);
187 static double tv_diff(const struct timeval *a, const struct timeval *b)
189 return (double)(a->tv_sec - b->tv_sec) +
190 (double)((int)a->tv_usec - (int)b->tv_usec) * 1e-6;
193 static int check_gtod(const struct timeval *tv_sys1,
194 const struct timeval *tv_sys2,
195 const struct timezone *tz_sys,
197 const struct timeval *tv_other,
198 const struct timezone *tz_other)
203 if (tz_other && (tz_sys->tz_minuteswest != tz_other->tz_minuteswest || tz_sys->tz_dsttime != tz_other->tz_dsttime)) {
204 printf("[FAIL] %s tz mismatch\n", which);
208 d1 = tv_diff(tv_other, tv_sys1);
209 d2 = tv_diff(tv_sys2, tv_other);
210 printf("\t%s time offsets: %lf %lf\n", which, d1, d2);
212 if (d1 < 0 || d2 < 0) {
213 printf("[FAIL]\t%s time was inconsistent with the syscall\n", which);
216 printf("[OK]\t%s gettimeofday()'s timeval was okay\n", which);
222 static int test_gtod(void)
224 struct timeval tv_sys1, tv_sys2, tv_vdso, tv_vsys;
225 struct timezone tz_sys, tz_vdso, tz_vsys;
230 printf("[RUN]\ttest gettimeofday()\n");
232 if (sys_gtod(&tv_sys1, &tz_sys) != 0)
233 err(1, "syscall gettimeofday");
235 ret_vdso = vdso_gtod(&tv_vdso, &tz_vdso);
237 ret_vsys = vgtod(&tv_vsys, &tz_vsys);
238 if (sys_gtod(&tv_sys2, &tz_sys) != 0)
239 err(1, "syscall gettimeofday");
243 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vDSO", &tv_vdso, &tz_vdso);
245 printf("[FAIL]\tvDSO gettimeofday() failed: %ld\n", ret_vdso);
252 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vsyscall", &tv_vsys, &tz_vsys);
254 printf("[FAIL]\tvsys gettimeofday() failed: %ld\n", ret_vsys);
262 static int test_time(void) {
265 printf("[RUN]\ttest time()\n");
266 long t_sys1, t_sys2, t_vdso = 0, t_vsys = 0;
267 long t2_sys1 = -1, t2_sys2 = -1, t2_vdso = -1, t2_vsys = -1;
268 t_sys1 = sys_time(&t2_sys1);
270 t_vdso = vdso_time(&t2_vdso);
272 t_vsys = vtime(&t2_vsys);
273 t_sys2 = sys_time(&t2_sys2);
274 if (t_sys1 < 0 || t_sys1 != t2_sys1 || t_sys2 < 0 || t_sys2 != t2_sys2) {
275 printf("[FAIL]\tsyscall failed (ret1:%ld output1:%ld ret2:%ld output2:%ld)\n", t_sys1, t2_sys1, t_sys2, t2_sys2);
281 if (t_vdso < 0 || t_vdso != t2_vdso) {
282 printf("[FAIL]\tvDSO failed (ret:%ld output:%ld)\n", t_vdso, t2_vdso);
284 } else if (t_vdso < t_sys1 || t_vdso > t_sys2) {
285 printf("[FAIL]\tvDSO returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vdso, t_sys2);
288 printf("[OK]\tvDSO time() is okay\n");
293 if (t_vsys < 0 || t_vsys != t2_vsys) {
294 printf("[FAIL]\tvsyscall failed (ret:%ld output:%ld)\n", t_vsys, t2_vsys);
296 } else if (t_vsys < t_sys1 || t_vsys > t_sys2) {
297 printf("[FAIL]\tvsyscall returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vsys, t_sys2);
300 printf("[OK]\tvsyscall time() is okay\n");
307 static int test_getcpu(int cpu)
310 long ret_sys, ret_vdso = -1, ret_vsys = -1;
312 printf("[RUN]\tgetcpu() on CPU %d\n", cpu);
316 CPU_SET(cpu, &cpuset);
317 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
318 printf("[SKIP]\tfailed to force CPU %d\n", cpu);
322 unsigned cpu_sys, cpu_vdso, cpu_vsys, node_sys, node_vdso, node_vsys;
324 bool have_node = false;
325 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
327 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
329 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
332 if (cpu_sys != cpu) {
333 printf("[FAIL]\tsyscall reported CPU %hu but should be %d\n", cpu_sys, cpu);
343 printf("[FAIL]\tvDSO getcpu() failed\n");
351 if (cpu_vdso != cpu) {
352 printf("[FAIL]\tvDSO reported CPU %hu but should be %d\n", cpu_vdso, cpu);
355 printf("[OK]\tvDSO reported correct CPU\n");
358 if (node_vdso != node) {
359 printf("[FAIL]\tvDSO reported node %hu but should be %hu\n", node_vdso, node);
362 printf("[OK]\tvDSO reported correct node\n");
369 printf("[FAIL]\tvsyscall getcpu() failed\n");
377 if (cpu_vsys != cpu) {
378 printf("[FAIL]\tvsyscall reported CPU %hu but should be %d\n", cpu_vsys, cpu);
381 printf("[OK]\tvsyscall reported correct CPU\n");
384 if (node_vsys != node) {
385 printf("[FAIL]\tvsyscall reported node %hu but should be %hu\n", node_vsys, node);
388 printf("[OK]\tvsyscall reported correct node\n");
396 static int test_vsys_r(void)
399 printf("[RUN]\tChecking read access to the vsyscall page\n");
401 if (sigsetjmp(jmpbuf, 1) == 0) {
402 *(volatile int *)0xffffffffff600000;
408 if (can_read && !should_read_vsyscall) {
409 printf("[FAIL]\tWe have read access, but we shouldn't\n");
411 } else if (!can_read && should_read_vsyscall) {
412 printf("[FAIL]\tWe don't have read access, but we should\n");
415 printf("[OK]\tgot expected result\n");
424 #define X86_EFLAGS_TF (1UL << 8)
425 static volatile sig_atomic_t num_vsyscall_traps;
427 static unsigned long get_eflags(void)
429 unsigned long eflags;
430 asm volatile ("pushfq\n\tpopq %0" : "=rm" (eflags));
434 static void set_eflags(unsigned long eflags)
436 asm volatile ("pushq %0\n\tpopfq" : : "rm" (eflags) : "flags");
439 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
441 ucontext_t *ctx = (ucontext_t *)ctx_void;
442 unsigned long ip = ctx->uc_mcontext.gregs[REG_RIP];
444 if (((ip ^ 0xffffffffff600000UL) & ~0xfffUL) == 0)
445 num_vsyscall_traps++;
448 static int test_native_vsyscall(void)
456 printf("[RUN]\tchecking for native vsyscall\n");
457 sethandler(SIGTRAP, sigtrap, 0);
458 set_eflags(get_eflags() | X86_EFLAGS_TF);
460 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
463 * If vsyscalls are emulated, we expect a single trap in the
464 * vsyscall page -- the call instruction will trap with RIP
465 * pointing to the entry point before emulation takes over.
466 * In native mode, we expect two traps, since whatever code
467 * the vsyscall page contains will be more than just a ret
470 is_native = (num_vsyscall_traps > 1);
472 printf("\tvsyscalls are %s (%d instructions in vsyscall page)\n",
473 (is_native ? "native" : "emulated"),
474 (int)num_vsyscall_traps);
480 int main(int argc, char **argv)
485 nerrs += init_vsys();
487 nerrs += test_gtod();
488 nerrs += test_time();
489 nerrs += test_getcpu(0);
490 nerrs += test_getcpu(1);
492 sethandler(SIGSEGV, sigsegv, 0);
493 nerrs += test_vsys_r();
496 nerrs += test_native_vsyscall();
499 return nerrs ? 1 : 0;