staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / x86 / test_vsyscall.c
bloba4f4d4cf22c3b47c40acf7ae4c6d25d335e3b313
1 /* SPDX-License-Identifier: GPL-2.0 */
3 #define _GNU_SOURCE
5 #include <stdio.h>
6 #include <sys/time.h>
7 #include <time.h>
8 #include <stdlib.h>
9 #include <sys/syscall.h>
10 #include <unistd.h>
11 #include <dlfcn.h>
12 #include <string.h>
13 #include <inttypes.h>
14 #include <signal.h>
15 #include <sys/ucontext.h>
16 #include <errno.h>
17 #include <err.h>
18 #include <sched.h>
19 #include <stdbool.h>
20 #include <setjmp.h>
21 #include <sys/uio.h>
23 #ifdef __x86_64__
24 # define VSYS(x) (x)
25 #else
26 # define VSYS(x) 0
27 #endif
29 #ifndef SYS_getcpu
30 # ifdef __x86_64__
31 # define SYS_getcpu 309
32 # else
33 # define SYS_getcpu 318
34 # endif
35 #endif
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 *),
41 int flags)
43 struct sigaction sa;
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))
49 err(1, "sigaction");
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);
57 gtod_t vdso_gtod;
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);
68 getcpu_t vdso_getcpu;
70 static void init_vdso(void)
72 void *vdso = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
73 if (!vdso)
74 vdso = dlopen("linux-gate.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
75 if (!vdso) {
76 printf("[WARN]\tfailed to find vDSO\n");
77 return;
80 vdso_gtod = (gtod_t)dlsym(vdso, "__vdso_gettimeofday");
81 if (!vdso_gtod)
82 printf("[WARN]\tfailed to find gettimeofday in vDSO\n");
84 vdso_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
85 if (!vdso_gettime)
86 printf("[WARN]\tfailed to find clock_gettime in vDSO\n");
88 vdso_time = (time_func_t)dlsym(vdso, "__vdso_time");
89 if (!vdso_time)
90 printf("[WARN]\tfailed to find time in vDSO\n");
92 vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
93 if (!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)
102 #ifdef __x86_64__
103 int nerrs = 0;
104 FILE *maps;
105 char line[MAPS_LINE_LEN];
106 bool found = false;
108 maps = fopen("/proc/self/maps", "r");
109 if (!maps) {
110 printf("[WARN]\tCould not open /proc/self/maps -- assuming vsyscall is r-x\n");
111 vsyscall_map_r = true;
112 return 0;
115 while (fgets(line, MAPS_LINE_LEN, maps)) {
116 char r, x;
117 void *start, *end;
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)
123 continue;
125 if (strcmp(name, "[vsyscall]"))
126 continue;
128 printf("\tvsyscall map: %s", line);
130 if (start != (void *)0xffffffffff600000 ||
131 end != (void *)0xffffffffff601000) {
132 printf("[FAIL]\taddress range is nonsense\n");
133 nerrs++;
136 printf("\tvsyscall permissions are %c-%c\n", r, x);
137 vsyscall_map_r = (r == 'r');
138 vsyscall_map_x = (x == 'x');
140 found = true;
141 break;
144 fclose(maps);
146 if (!found) {
147 printf("\tno vsyscall map in /proc/self/maps\n");
148 vsyscall_map_r = false;
149 vsyscall_map_x = false;
152 return nerrs;
153 #else
154 return 0;
155 #endif
158 /* syscalls */
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,
175 void* cache)
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,
200 const char *which,
201 const struct timeval *tv_other,
202 const struct timezone *tz_other)
204 int nerrs = 0;
205 double d1, d2;
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);
209 nerrs++;
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);
218 nerrs++;
219 } else {
220 printf("[OK]\t%s gettimeofday()'s timeval was okay\n", which);
223 return nerrs;
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;
230 long ret_vdso = -1;
231 long ret_vsys = -1;
232 int nerrs = 0;
234 printf("[RUN]\ttest gettimeofday()\n");
236 if (sys_gtod(&tv_sys1, &tz_sys) != 0)
237 err(1, "syscall gettimeofday");
238 if (vdso_gtod)
239 ret_vdso = vdso_gtod(&tv_vdso, &tz_vdso);
240 if (vsyscall_map_x)
241 ret_vsys = vgtod(&tv_vsys, &tz_vsys);
242 if (sys_gtod(&tv_sys2, &tz_sys) != 0)
243 err(1, "syscall gettimeofday");
245 if (vdso_gtod) {
246 if (ret_vdso == 0) {
247 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vDSO", &tv_vdso, &tz_vdso);
248 } else {
249 printf("[FAIL]\tvDSO gettimeofday() failed: %ld\n", ret_vdso);
250 nerrs++;
254 if (vsyscall_map_x) {
255 if (ret_vsys == 0) {
256 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vsyscall", &tv_vsys, &tz_vsys);
257 } else {
258 printf("[FAIL]\tvsys gettimeofday() failed: %ld\n", ret_vsys);
259 nerrs++;
263 return nerrs;
266 static int test_time(void) {
267 int nerrs = 0;
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);
273 if (vdso_time)
274 t_vdso = vdso_time(&t2_vdso);
275 if (vsyscall_map_x)
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);
280 nerrs++;
281 return nerrs;
284 if (vdso_time) {
285 if (t_vdso < 0 || t_vdso != t2_vdso) {
286 printf("[FAIL]\tvDSO failed (ret:%ld output:%ld)\n", t_vdso, t2_vdso);
287 nerrs++;
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);
290 nerrs++;
291 } else {
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);
299 nerrs++;
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);
302 nerrs++;
303 } else {
304 printf("[OK]\tvsyscall time() is okay\n");
308 return nerrs;
311 static int test_getcpu(int cpu)
313 int nerrs = 0;
314 long ret_sys, ret_vdso = -1, ret_vsys = -1;
316 printf("[RUN]\tgetcpu() on CPU %d\n", cpu);
318 cpu_set_t cpuset;
319 CPU_ZERO(&cpuset);
320 CPU_SET(cpu, &cpuset);
321 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
322 printf("[SKIP]\tfailed to force CPU %d\n", cpu);
323 return nerrs;
326 unsigned cpu_sys, cpu_vdso, cpu_vsys, node_sys, node_vdso, node_vsys;
327 unsigned node = 0;
328 bool have_node = false;
329 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
330 if (vdso_getcpu)
331 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
332 if (vsyscall_map_x)
333 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
335 if (ret_sys == 0) {
336 if (cpu_sys != cpu) {
337 printf("[FAIL]\tsyscall reported CPU %hu but should be %d\n", cpu_sys, cpu);
338 nerrs++;
341 have_node = true;
342 node = node_sys;
345 if (vdso_getcpu) {
346 if (ret_vdso) {
347 printf("[FAIL]\tvDSO getcpu() failed\n");
348 nerrs++;
349 } else {
350 if (!have_node) {
351 have_node = true;
352 node = node_vdso;
355 if (cpu_vdso != cpu) {
356 printf("[FAIL]\tvDSO reported CPU %hu but should be %d\n", cpu_vdso, cpu);
357 nerrs++;
358 } else {
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);
364 nerrs++;
365 } else {
366 printf("[OK]\tvDSO reported correct node\n");
371 if (vsyscall_map_x) {
372 if (ret_vsys) {
373 printf("[FAIL]\tvsyscall getcpu() failed\n");
374 nerrs++;
375 } else {
376 if (!have_node) {
377 have_node = true;
378 node = node_vsys;
381 if (cpu_vsys != cpu) {
382 printf("[FAIL]\tvsyscall reported CPU %hu but should be %d\n", cpu_vsys, cpu);
383 nerrs++;
384 } else {
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);
390 nerrs++;
391 } else {
392 printf("[OK]\tvsyscall reported correct node\n");
397 return nerrs;
400 static int test_vsys_r(void)
402 #ifdef __x86_64__
403 printf("[RUN]\tChecking read access to the vsyscall page\n");
404 bool can_read;
405 if (sigsetjmp(jmpbuf, 1) == 0) {
406 *(volatile int *)0xffffffffff600000;
407 can_read = true;
408 } else {
409 can_read = false;
412 if (can_read && !vsyscall_map_r) {
413 printf("[FAIL]\tWe have read access, but we shouldn't\n");
414 return 1;
415 } else if (!can_read && vsyscall_map_r) {
416 printf("[FAIL]\tWe don't have read access, but we should\n");
417 return 1;
418 } else if (can_read) {
419 printf("[OK]\tWe have read access\n");
420 } else {
421 printf("[OK]\tWe do not have read access: #PF(0x%lx)\n",
422 segv_err);
424 #endif
426 return 0;
429 static int test_vsys_x(void)
431 #ifdef __x86_64__
432 if (vsyscall_map_x) {
433 /* We already tested this adequately. */
434 return 0;
437 printf("[RUN]\tMake sure that vsyscalls really page fault\n");
439 bool can_exec;
440 if (sigsetjmp(jmpbuf, 1) == 0) {
441 vgtod(NULL, NULL);
442 can_exec = true;
443 } else {
444 can_exec = false;
447 if (can_exec) {
448 printf("[FAIL]\tExecuting the vsyscall did not page fault\n");
449 return 1;
450 } else if (segv_err & (1 << 4)) { /* INSTR */
451 printf("[OK]\tExecuting the vsyscall page failed: #PF(0x%lx)\n",
452 segv_err);
453 } else {
454 printf("[FAIL]\tExecution failed with the wrong error: #PF(0x%lx)\n",
455 segv_err);
456 return 1;
458 #endif
460 return 0;
463 static int test_process_vm_readv(void)
465 #ifdef __x86_64__
466 char buf[4096];
467 struct iovec local, remote;
468 int ret;
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);
477 if (ret != 4096) {
478 printf("[OK]\tprocess_vm_readv() failed (ret = %d, errno = %d)\n", ret, errno);
479 return 0;
482 if (vsyscall_map_r) {
483 if (!memcmp(buf, (const void *)0xffffffffff600000, 4096)) {
484 printf("[OK]\tIt worked and read correct data\n");
485 } else {
486 printf("[FAIL]\tIt worked but returned incorrect data\n");
487 return 1;
490 #endif
492 return 0;
495 #ifdef __x86_64__
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));
503 return 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)
522 time_t tmp;
523 bool is_native;
525 if (!vsyscall_map_x)
526 return 0;
528 printf("[RUN]\tchecking that vsyscalls are emulated\n");
529 sethandler(SIGTRAP, sigtrap, 0);
530 set_eflags(get_eflags() | X86_EFLAGS_TF);
531 vtime(&tmp);
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
540 * instruction.
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);
549 return is_native;
551 #endif
553 int main(int argc, char **argv)
555 int nerrs = 0;
557 init_vdso();
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();
571 #ifdef __x86_64__
572 nerrs += test_emulation();
573 #endif
575 return nerrs ? 1 : 0;