1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * vdso_test.c: Sample code to test parse_vdso.c
4 * Copyright (c) 2014 Andy Lutomirski
5 *
6 * Compile with:
7 * gcc -std=gnu99 vdso_test.c parse_vdso.c
8 *
9 * Tested on x86, 32-bit and 64-bit. It may work on other architectures, too.
10 */
11
12 #include <stdint.h>
13 #include <elf.h>
14 #include <stdio.h>
15 #include <sys/auxv.h>
16 #include <sys/time.h>
17
18 #include "../kselftest.h"
19
20 extern void *vdso_sym(const char *version, const char *name);
21 extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
22 extern void vdso_init_from_auxv(void *auxv);
23
24 /*
25 * ARM64's vDSO exports its gettimeofday() implementation with a different
26 * name and version from other architectures, so we need to handle it as
27 * a special case.
28 */
29 #if defined(__aarch64__)
30 const char *version = "LINUX_2.6.39";
31 const char *name = "__kernel_gettimeofday";
32 #else
33 const char *version = "LINUX_2.6";
34 const char *name = "__vdso_gettimeofday";
35 #endif
36
main(int argc,char ** argv)37 int main(int argc, char **argv)
38 {
39 unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
40 if (!sysinfo_ehdr) {
41 printf("AT_SYSINFO_EHDR is not present!\n");
42 return KSFT_SKIP;
43 }
44
45 vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
46
47 /* Find gettimeofday. */
48 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
49 gtod_t gtod = (gtod_t)vdso_sym(version, name);
50
51 if (!gtod) {
52 printf("Could not find %s\n", name);
53 return KSFT_SKIP;
54 }
55
56 struct timeval tv;
57 long ret = gtod(&tv, 0);
58
59 if (ret == 0) {
60 printf("The time is %lld.%06lld\n",
61 (long long)tv.tv_sec, (long long)tv.tv_usec);
62 } else {
63 printf("%s failed\n", name);
64 return KSFT_FAIL;
65 }
66
67 return 0;
68 }
69