Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / testing / selftests / vDSO / vdso_test.c
blob2df26bd0099ccb0033f0d48bbc4d9485bb42f5da
1 /*
2 * vdso_test.c: Sample code to test parse_vdso.c
3 * Copyright (c) 2014 Andy Lutomirski
4 * Subject to the GNU General Public License, version 2
6 * Compile with:
7 * gcc -std=gnu99 vdso_test.c parse_vdso.c
9 * Tested on x86, 32-bit and 64-bit. It may work on other architectures, too.
12 #include <stdint.h>
13 #include <elf.h>
14 #include <stdio.h>
15 #include <sys/auxv.h>
16 #include <sys/time.h>
18 extern void *vdso_sym(const char *version, const char *name);
19 extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
20 extern void vdso_init_from_auxv(void *auxv);
23 * ARM64's vDSO exports its gettimeofday() implementation with a different
24 * name and version from other architectures, so we need to handle it as
25 * a special case.
27 #if defined(__aarch64__)
28 const char *version = "LINUX_2.6.39";
29 const char *name = "__kernel_gettimeofday";
30 #else
31 const char *version = "LINUX_2.6";
32 const char *name = "__vdso_gettimeofday";
33 #endif
35 int main(int argc, char **argv)
37 unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
38 if (!sysinfo_ehdr) {
39 printf("AT_SYSINFO_EHDR is not present!\n");
40 return 0;
43 vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
45 /* Find gettimeofday. */
46 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
47 gtod_t gtod = (gtod_t)vdso_sym(version, name);
49 if (!gtod) {
50 printf("Could not find %s\n", name);
51 return 1;
54 struct timeval tv;
55 long ret = gtod(&tv, 0);
57 if (ret == 0) {
58 printf("The time is %lld.%06lld\n",
59 (long long)tv.tv_sec, (long long)tv.tv_usec);
60 } else {
61 printf("%s failed\n", name);
64 return 0;