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 vsyscall_map_r = false, vsyscall_map_x = false;
54 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
55 const 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 const time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400);
63 time_func_t vdso_time;
65 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
66 const 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 vsyscall_map_r = 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 vsyscall_map_r = (r == 'r');
137 vsyscall_map_x = (x == 'x');
146 printf("\tno vsyscall map in /proc/self/maps\n");
147 vsyscall_map_r = false;
148 vsyscall_map_x = false;
158 static inline long sys_gtod(struct timeval *tv, struct timezone *tz)
160 return syscall(SYS_gettimeofday, tv, tz);
163 static inline int sys_clock_gettime(clockid_t id, struct timespec *ts)
165 return syscall(SYS_clock_gettime, id, ts);
168 static inline long sys_time(time_t *t)
170 return syscall(SYS_time, t);
173 static inline long sys_getcpu(unsigned * cpu, unsigned * node,
176 return syscall(SYS_getcpu, cpu, node, cache);
179 static jmp_buf jmpbuf;
180 static volatile unsigned long segv_err;
182 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
184 ucontext_t *ctx = (ucontext_t *)ctx_void;
186 segv_err = ctx->uc_mcontext.gregs[REG_ERR];
187 siglongjmp(jmpbuf, 1);
190 static double tv_diff(const struct timeval *a, const struct timeval *b)
192 return (double)(a->tv_sec - b->tv_sec) +
193 (double)((int)a->tv_usec - (int)b->tv_usec) * 1e-6;
196 static int check_gtod(const struct timeval *tv_sys1,
197 const struct timeval *tv_sys2,
198 const struct timezone *tz_sys,
200 const struct timeval *tv_other,
201 const struct timezone *tz_other)
206 if (tz_other && (tz_sys->tz_minuteswest != tz_other->tz_minuteswest || tz_sys->tz_dsttime != tz_other->tz_dsttime)) {
207 printf("[FAIL] %s tz mismatch\n", which);
211 d1 = tv_diff(tv_other, tv_sys1);
212 d2 = tv_diff(tv_sys2, tv_other);
213 printf("\t%s time offsets: %lf %lf\n", which, d1, d2);
215 if (d1 < 0 || d2 < 0) {
216 printf("[FAIL]\t%s time was inconsistent with the syscall\n", which);
219 printf("[OK]\t%s gettimeofday()'s timeval was okay\n", which);
225 static int test_gtod(void)
227 struct timeval tv_sys1, tv_sys2, tv_vdso, tv_vsys;
228 struct timezone tz_sys, tz_vdso, tz_vsys;
233 printf("[RUN]\ttest gettimeofday()\n");
235 if (sys_gtod(&tv_sys1, &tz_sys) != 0)
236 err(1, "syscall gettimeofday");
238 ret_vdso = vdso_gtod(&tv_vdso, &tz_vdso);
240 ret_vsys = vgtod(&tv_vsys, &tz_vsys);
241 if (sys_gtod(&tv_sys2, &tz_sys) != 0)
242 err(1, "syscall gettimeofday");
246 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vDSO", &tv_vdso, &tz_vdso);
248 printf("[FAIL]\tvDSO gettimeofday() failed: %ld\n", ret_vdso);
253 if (vsyscall_map_x) {
255 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vsyscall", &tv_vsys, &tz_vsys);
257 printf("[FAIL]\tvsys gettimeofday() failed: %ld\n", ret_vsys);
265 static int test_time(void) {
268 printf("[RUN]\ttest time()\n");
269 long t_sys1, t_sys2, t_vdso = 0, t_vsys = 0;
270 long t2_sys1 = -1, t2_sys2 = -1, t2_vdso = -1, t2_vsys = -1;
271 t_sys1 = sys_time(&t2_sys1);
273 t_vdso = vdso_time(&t2_vdso);
275 t_vsys = vtime(&t2_vsys);
276 t_sys2 = sys_time(&t2_sys2);
277 if (t_sys1 < 0 || t_sys1 != t2_sys1 || t_sys2 < 0 || t_sys2 != t2_sys2) {
278 printf("[FAIL]\tsyscall failed (ret1:%ld output1:%ld ret2:%ld output2:%ld)\n", t_sys1, t2_sys1, t_sys2, t2_sys2);
284 if (t_vdso < 0 || t_vdso != t2_vdso) {
285 printf("[FAIL]\tvDSO failed (ret:%ld output:%ld)\n", t_vdso, t2_vdso);
287 } else if (t_vdso < t_sys1 || t_vdso > t_sys2) {
288 printf("[FAIL]\tvDSO returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vdso, t_sys2);
291 printf("[OK]\tvDSO time() is okay\n");
295 if (vsyscall_map_x) {
296 if (t_vsys < 0 || t_vsys != t2_vsys) {
297 printf("[FAIL]\tvsyscall failed (ret:%ld output:%ld)\n", t_vsys, t2_vsys);
299 } else if (t_vsys < t_sys1 || t_vsys > t_sys2) {
300 printf("[FAIL]\tvsyscall returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vsys, t_sys2);
303 printf("[OK]\tvsyscall time() is okay\n");
310 static int test_getcpu(int cpu)
313 long ret_sys, ret_vdso = -1, ret_vsys = -1;
315 printf("[RUN]\tgetcpu() on CPU %d\n", cpu);
319 CPU_SET(cpu, &cpuset);
320 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
321 printf("[SKIP]\tfailed to force CPU %d\n", cpu);
325 unsigned cpu_sys, cpu_vdso, cpu_vsys, node_sys, node_vdso, node_vsys;
327 bool have_node = false;
328 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
330 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
332 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
335 if (cpu_sys != cpu) {
336 printf("[FAIL]\tsyscall reported CPU %hu but should be %d\n", cpu_sys, cpu);
346 printf("[FAIL]\tvDSO getcpu() failed\n");
354 if (cpu_vdso != cpu) {
355 printf("[FAIL]\tvDSO reported CPU %hu but should be %d\n", cpu_vdso, cpu);
358 printf("[OK]\tvDSO reported correct CPU\n");
361 if (node_vdso != node) {
362 printf("[FAIL]\tvDSO reported node %hu but should be %hu\n", node_vdso, node);
365 printf("[OK]\tvDSO reported correct node\n");
370 if (vsyscall_map_x) {
372 printf("[FAIL]\tvsyscall getcpu() failed\n");
380 if (cpu_vsys != cpu) {
381 printf("[FAIL]\tvsyscall reported CPU %hu but should be %d\n", cpu_vsys, cpu);
384 printf("[OK]\tvsyscall reported correct CPU\n");
387 if (node_vsys != node) {
388 printf("[FAIL]\tvsyscall reported node %hu but should be %hu\n", node_vsys, node);
391 printf("[OK]\tvsyscall reported correct node\n");
399 static int test_vsys_r(void)
402 printf("[RUN]\tChecking read access to the vsyscall page\n");
404 if (sigsetjmp(jmpbuf, 1) == 0) {
405 *(volatile int *)0xffffffffff600000;
411 if (can_read && !vsyscall_map_r) {
412 printf("[FAIL]\tWe have read access, but we shouldn't\n");
414 } else if (!can_read && vsyscall_map_r) {
415 printf("[FAIL]\tWe don't have read access, but we should\n");
417 } else if (can_read) {
418 printf("[OK]\tWe have read access\n");
420 printf("[OK]\tWe do not have read access: #PF(0x%lx)\n",
428 static int test_vsys_x(void)
431 if (vsyscall_map_x) {
432 /* We already tested this adequately. */
436 printf("[RUN]\tMake sure that vsyscalls really page fault\n");
439 if (sigsetjmp(jmpbuf, 1) == 0) {
447 printf("[FAIL]\tExecuting the vsyscall did not page fault\n");
449 } else if (segv_err & (1 << 4)) { /* INSTR */
450 printf("[OK]\tExecuting the vsyscall page failed: #PF(0x%lx)\n",
453 printf("[FAILT]\tExecution failed with the wrong error: #PF(0x%lx)\n",
463 #define X86_EFLAGS_TF (1UL << 8)
464 static volatile sig_atomic_t num_vsyscall_traps;
466 static unsigned long get_eflags(void)
468 unsigned long eflags;
469 asm volatile ("pushfq\n\tpopq %0" : "=rm" (eflags));
473 static void set_eflags(unsigned long eflags)
475 asm volatile ("pushq %0\n\tpopfq" : : "rm" (eflags) : "flags");
478 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
480 ucontext_t *ctx = (ucontext_t *)ctx_void;
481 unsigned long ip = ctx->uc_mcontext.gregs[REG_RIP];
483 if (((ip ^ 0xffffffffff600000UL) & ~0xfffUL) == 0)
484 num_vsyscall_traps++;
487 static int test_emulation(void)
495 printf("[RUN]\tchecking that vsyscalls are emulated\n");
496 sethandler(SIGTRAP, sigtrap, 0);
497 set_eflags(get_eflags() | X86_EFLAGS_TF);
499 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
502 * If vsyscalls are emulated, we expect a single trap in the
503 * vsyscall page -- the call instruction will trap with RIP
504 * pointing to the entry point before emulation takes over.
505 * In native mode, we expect two traps, since whatever code
506 * the vsyscall page contains will be more than just a ret
509 is_native = (num_vsyscall_traps > 1);
511 printf("[%s]\tvsyscalls are %s (%d instructions in vsyscall page)\n",
512 (is_native ? "FAIL" : "OK"),
513 (is_native ? "native" : "emulated"),
514 (int)num_vsyscall_traps);
520 int main(int argc, char **argv)
525 nerrs += init_vsys();
527 nerrs += test_gtod();
528 nerrs += test_time();
529 nerrs += test_getcpu(0);
530 nerrs += test_getcpu(1);
532 sethandler(SIGSEGV, sigsegv, 0);
533 nerrs += test_vsys_r();
534 nerrs += test_vsys_x();
537 nerrs += test_emulation();
540 return nerrs ? 1 : 0;