Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * vdso_test_gettimeofday.c: Sample code to test parse_vdso.c and
0004  *                           vDSO gettimeofday()
0005  * Copyright (c) 2014 Andy Lutomirski
0006  *
0007  * Compile with:
0008  * gcc -std=gnu99 vdso_test_gettimeofday.c parse_vdso_gettimeofday.c
0009  *
0010  * Tested on x86, 32-bit and 64-bit.  It may work on other architectures, too.
0011  */
0012 
0013 #include <stdint.h>
0014 #include <elf.h>
0015 #include <stdio.h>
0016 #include <sys/auxv.h>
0017 #include <sys/time.h>
0018 
0019 #include "../kselftest.h"
0020 #include "parse_vdso.h"
0021 
0022 /*
0023  * ARM64's vDSO exports its gettimeofday() implementation with a different
0024  * name and version from other architectures, so we need to handle it as
0025  * a special case.
0026  */
0027 #if defined(__aarch64__)
0028 const char *version = "LINUX_2.6.39";
0029 const char *name = "__kernel_gettimeofday";
0030 #else
0031 const char *version = "LINUX_2.6";
0032 const char *name = "__vdso_gettimeofday";
0033 #endif
0034 
0035 int main(int argc, char **argv)
0036 {
0037     unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
0038     if (!sysinfo_ehdr) {
0039         printf("AT_SYSINFO_EHDR is not present!\n");
0040         return KSFT_SKIP;
0041     }
0042 
0043     vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
0044 
0045     /* Find gettimeofday. */
0046     typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
0047     gtod_t gtod = (gtod_t)vdso_sym(version, name);
0048 
0049     if (!gtod) {
0050         printf("Could not find %s\n", name);
0051         return KSFT_SKIP;
0052     }
0053 
0054     struct timeval tv;
0055     long ret = gtod(&tv, 0);
0056 
0057     if (ret == 0) {
0058         printf("The time is %lld.%06lld\n",
0059                (long long)tv.tv_sec, (long long)tv.tv_usec);
0060     } else {
0061         printf("%s failed\n", name);
0062         return KSFT_FAIL;
0063     }
0064 
0065     return 0;
0066 }