1 /* SPDX-License-Identifier: GPL-2.0 */
9 #include <sys/syscall.h>
15 #include <sys/ucontext.h>
31 # define SYS_getcpu 309
33 # define SYS_getcpu 318
37 /* max length of lines in /proc/self/maps - anything longer is skipped here */
38 #define MAPS_LINE_LEN 128
40 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
44 memset(&sa, 0, sizeof(sa));
45 sa.sa_sigaction = handler;
46 sa.sa_flags = SA_SIGINFO | flags;
47 sigemptyset(&sa.sa_mask);
48 if (sigaction(sig, &sa, 0))
52 /* vsyscalls and vDSO */
53 bool vsyscall_map_r = false, vsyscall_map_x = false;
55 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
56 const gtod_t vgtod = (gtod_t)VSYS(0xffffffffff600000);
59 typedef int (*vgettime_t)(clockid_t, struct timespec *);
60 vgettime_t vdso_gettime;
62 typedef long (*time_func_t)(time_t *t);
63 const time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400);
64 time_func_t vdso_time;
66 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
67 const getcpu_t vgetcpu = (getcpu_t)VSYS(0xffffffffff600800);
70 static void init_vdso(void)
72 void *vdso = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
74 vdso = dlopen("linux-gate.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
76 printf("[WARN]\tfailed to find vDSO\n");
80 vdso_gtod = (gtod_t)dlsym(vdso, "__vdso_gettimeofday");
82 printf("[WARN]\tfailed to find gettimeofday in vDSO\n");
84 vdso_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
86 printf("[WARN]\tfailed to find clock_gettime in vDSO\n");
88 vdso_time = (time_func_t)dlsym(vdso, "__vdso_time");
90 printf("[WARN]\tfailed to find time in vDSO\n");
92 vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
94 /* getcpu() was never wired up in the 32-bit vDSO. */
95 printf("[%s]\tfailed to find getcpu in vDSO\n",
96 sizeof(long) == 8 ? "WARN" : "NOTE");
100 static int init_vsys(void)
105 char line[MAPS_LINE_LEN];
108 maps = fopen("/proc/self/maps", "r");
110 printf("[WARN]\tCould not open /proc/self/maps -- assuming vsyscall is r-x\n");
111 vsyscall_map_r = true;
115 while (fgets(line, MAPS_LINE_LEN, maps)) {
118 char name[MAPS_LINE_LEN];
120 /* sscanf() is safe here as strlen(name) >= strlen(line) */
121 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
122 &start, &end, &r, &x, name) != 5)
125 if (strcmp(name, "[vsyscall]"))
128 printf("\tvsyscall map: %s", line);
130 if (start != (void *)0xffffffffff600000 ||
131 end != (void *)0xffffffffff601000) {
132 printf("[FAIL]\taddress range is nonsense\n");
136 printf("\tvsyscall permissions are %c-%c\n", r, x);
137 vsyscall_map_r = (r == 'r');
138 vsyscall_map_x = (x == 'x');
147 printf("\tno vsyscall map in /proc/self/maps\n");
148 vsyscall_map_r = false;
149 vsyscall_map_x = 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;
181 static volatile unsigned long segv_err;
183 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
185 ucontext_t *ctx = (ucontext_t *)ctx_void;
187 segv_err = ctx->uc_mcontext.gregs[REG_ERR];
188 siglongjmp(jmpbuf, 1);
191 static double tv_diff(const struct timeval *a, const struct timeval *b)
193 return (double)(a->tv_sec - b->tv_sec) +
194 (double)((int)a->tv_usec - (int)b->tv_usec) * 1e-6;
197 static int check_gtod(const struct timeval *tv_sys1,
198 const struct timeval *tv_sys2,
199 const struct timezone *tz_sys,
201 const struct timeval *tv_other,
202 const struct timezone *tz_other)
207 if (tz_other && (tz_sys->tz_minuteswest != tz_other->tz_minuteswest || tz_sys->tz_dsttime != tz_other->tz_dsttime)) {
208 printf("[FAIL] %s tz mismatch\n", which);
212 d1 = tv_diff(tv_other, tv_sys1);
213 d2 = tv_diff(tv_sys2, tv_other);
214 printf("\t%s time offsets: %lf %lf\n", which, d1, d2);
216 if (d1 < 0 || d2 < 0) {
217 printf("[FAIL]\t%s time was inconsistent with the syscall\n", which);
220 printf("[OK]\t%s gettimeofday()'s timeval was okay\n", which);
226 static int test_gtod(void)
228 struct timeval tv_sys1, tv_sys2, tv_vdso, tv_vsys;
229 struct timezone tz_sys, tz_vdso, tz_vsys;
234 printf("[RUN]\ttest gettimeofday()\n");
236 if (sys_gtod(&tv_sys1, &tz_sys) != 0)
237 err(1, "syscall gettimeofday");
239 ret_vdso = vdso_gtod(&tv_vdso, &tz_vdso);
241 ret_vsys = vgtod(&tv_vsys, &tz_vsys);
242 if (sys_gtod(&tv_sys2, &tz_sys) != 0)
243 err(1, "syscall gettimeofday");
247 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vDSO", &tv_vdso, &tz_vdso);
249 printf("[FAIL]\tvDSO gettimeofday() failed: %ld\n", ret_vdso);
254 if (vsyscall_map_x) {
256 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vsyscall", &tv_vsys, &tz_vsys);
258 printf("[FAIL]\tvsys gettimeofday() failed: %ld\n", ret_vsys);
266 static int test_time(void) {
269 printf("[RUN]\ttest time()\n");
270 long t_sys1, t_sys2, t_vdso = 0, t_vsys = 0;
271 long t2_sys1 = -1, t2_sys2 = -1, t2_vdso = -1, t2_vsys = -1;
272 t_sys1 = sys_time(&t2_sys1);
274 t_vdso = vdso_time(&t2_vdso);
276 t_vsys = vtime(&t2_vsys);
277 t_sys2 = sys_time(&t2_sys2);
278 if (t_sys1 < 0 || t_sys1 != t2_sys1 || t_sys2 < 0 || t_sys2 != t2_sys2) {
279 printf("[FAIL]\tsyscall failed (ret1:%ld output1:%ld ret2:%ld output2:%ld)\n", t_sys1, t2_sys1, t_sys2, t2_sys2);
285 if (t_vdso < 0 || t_vdso != t2_vdso) {
286 printf("[FAIL]\tvDSO failed (ret:%ld output:%ld)\n", t_vdso, t2_vdso);
288 } else if (t_vdso < t_sys1 || t_vdso > t_sys2) {
289 printf("[FAIL]\tvDSO returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vdso, t_sys2);
292 printf("[OK]\tvDSO time() is okay\n");
296 if (vsyscall_map_x) {
297 if (t_vsys < 0 || t_vsys != t2_vsys) {
298 printf("[FAIL]\tvsyscall failed (ret:%ld output:%ld)\n", t_vsys, t2_vsys);
300 } else if (t_vsys < t_sys1 || t_vsys > t_sys2) {
301 printf("[FAIL]\tvsyscall returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vsys, t_sys2);
304 printf("[OK]\tvsyscall time() is okay\n");
311 static int test_getcpu(int cpu)
314 long ret_sys, ret_vdso = -1, ret_vsys = -1;
316 printf("[RUN]\tgetcpu() on CPU %d\n", cpu);
320 CPU_SET(cpu, &cpuset);
321 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
322 printf("[SKIP]\tfailed to force CPU %d\n", cpu);
326 unsigned cpu_sys, cpu_vdso, cpu_vsys, node_sys, node_vdso, node_vsys;
328 bool have_node = false;
329 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
331 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
333 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
336 if (cpu_sys != cpu) {
337 printf("[FAIL]\tsyscall reported CPU %hu but should be %d\n", cpu_sys, cpu);
347 printf("[FAIL]\tvDSO getcpu() failed\n");
355 if (cpu_vdso != cpu) {
356 printf("[FAIL]\tvDSO reported CPU %hu but should be %d\n", cpu_vdso, cpu);
359 printf("[OK]\tvDSO reported correct CPU\n");
362 if (node_vdso != node) {
363 printf("[FAIL]\tvDSO reported node %hu but should be %hu\n", node_vdso, node);
366 printf("[OK]\tvDSO reported correct node\n");
371 if (vsyscall_map_x) {
373 printf("[FAIL]\tvsyscall getcpu() failed\n");
381 if (cpu_vsys != cpu) {
382 printf("[FAIL]\tvsyscall reported CPU %hu but should be %d\n", cpu_vsys, cpu);
385 printf("[OK]\tvsyscall reported correct CPU\n");
388 if (node_vsys != node) {
389 printf("[FAIL]\tvsyscall reported node %hu but should be %hu\n", node_vsys, node);
392 printf("[OK]\tvsyscall reported correct node\n");
400 static int test_vsys_r(void)
403 printf("[RUN]\tChecking read access to the vsyscall page\n");
405 if (sigsetjmp(jmpbuf, 1) == 0) {
406 *(volatile int *)0xffffffffff600000;
412 if (can_read && !vsyscall_map_r) {
413 printf("[FAIL]\tWe have read access, but we shouldn't\n");
415 } else if (!can_read && vsyscall_map_r) {
416 printf("[FAIL]\tWe don't have read access, but we should\n");
418 } else if (can_read) {
419 printf("[OK]\tWe have read access\n");
421 printf("[OK]\tWe do not have read access: #PF(0x%lx)\n",
429 static int test_vsys_x(void)
432 if (vsyscall_map_x) {
433 /* We already tested this adequately. */
437 printf("[RUN]\tMake sure that vsyscalls really page fault\n");
440 if (sigsetjmp(jmpbuf, 1) == 0) {
448 printf("[FAIL]\tExecuting the vsyscall did not page fault\n");
450 } else if (segv_err & (1 << 4)) { /* INSTR */
451 printf("[OK]\tExecuting the vsyscall page failed: #PF(0x%lx)\n",
454 printf("[FAIL]\tExecution failed with the wrong error: #PF(0x%lx)\n",
463 static int test_process_vm_readv(void)
467 struct iovec local, remote;
470 printf("[RUN]\tprocess_vm_readv() from vsyscall page\n");
472 local.iov_base = buf;
473 local.iov_len = 4096;
474 remote.iov_base = (void *)0xffffffffff600000;
475 remote.iov_len = 4096;
476 ret = process_vm_readv(getpid(), &local, 1, &remote, 1, 0);
478 printf("[OK]\tprocess_vm_readv() failed (ret = %d, errno = %d)\n", ret, errno);
482 if (vsyscall_map_r) {
483 if (!memcmp(buf, (const void *)0xffffffffff600000, 4096)) {
484 printf("[OK]\tIt worked and read correct data\n");
486 printf("[FAIL]\tIt worked but returned incorrect data\n");
496 #define X86_EFLAGS_TF (1UL << 8)
497 static volatile sig_atomic_t num_vsyscall_traps;
499 static unsigned long get_eflags(void)
501 unsigned long eflags;
502 asm volatile ("pushfq\n\tpopq %0" : "=rm" (eflags));
506 static void set_eflags(unsigned long eflags)
508 asm volatile ("pushq %0\n\tpopfq" : : "rm" (eflags) : "flags");
511 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
513 ucontext_t *ctx = (ucontext_t *)ctx_void;
514 unsigned long ip = ctx->uc_mcontext.gregs[REG_RIP];
516 if (((ip ^ 0xffffffffff600000UL) & ~0xfffUL) == 0)
517 num_vsyscall_traps++;
520 static int test_emulation(void)
528 printf("[RUN]\tchecking that vsyscalls are emulated\n");
529 sethandler(SIGTRAP, sigtrap, 0);
530 set_eflags(get_eflags() | X86_EFLAGS_TF);
532 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
535 * If vsyscalls are emulated, we expect a single trap in the
536 * vsyscall page -- the call instruction will trap with RIP
537 * pointing to the entry point before emulation takes over.
538 * In native mode, we expect two traps, since whatever code
539 * the vsyscall page contains will be more than just a ret
542 is_native = (num_vsyscall_traps > 1);
544 printf("[%s]\tvsyscalls are %s (%d instructions in vsyscall page)\n",
545 (is_native ? "FAIL" : "OK"),
546 (is_native ? "native" : "emulated"),
547 (int)num_vsyscall_traps);
553 int main(int argc, char **argv)
558 nerrs += init_vsys();
560 nerrs += test_gtod();
561 nerrs += test_time();
562 nerrs += test_getcpu(0);
563 nerrs += test_getcpu(1);
565 sethandler(SIGSEGV, sigsegv, 0);
566 nerrs += test_vsys_r();
567 nerrs += test_vsys_x();
569 nerrs += test_process_vm_readv();
572 nerrs += test_emulation();
575 return nerrs ? 1 : 0;