1 // SPDX-License-Identifier: GPL-2.0
3 * vdso_full_test.c: Sample code to test all the timers.
4 * Copyright (c) 2019 Arm Ltd.
7 * gcc -std=gnu99 vdso_full_test.c parse_vdso.c
19 #include <sys/syscall.h>
21 #include "../kselftest.h"
22 #include "vdso_config.h"
24 extern void *vdso_sym(const char *version, const char *name);
25 extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
26 extern void vdso_init_from_auxv(void *auxv);
28 static const char *version;
29 static const char **name;
31 typedef long (*vdso_gettimeofday_t)(struct timeval *tv, struct timezone *tz);
32 typedef long (*vdso_clock_gettime_t)(clockid_t clk_id, struct timespec *ts);
33 typedef long (*vdso_clock_getres_t)(clockid_t clk_id, struct timespec *ts);
34 typedef time_t (*vdso_time_t)(time_t *t);
36 static int vdso_test_gettimeofday(void)
38 /* Find gettimeofday. */
39 vdso_gettimeofday_t vdso_gettimeofday =
40 (vdso_gettimeofday_t)vdso_sym(version, name[0]);
42 if (!vdso_gettimeofday) {
43 printf("Could not find %s\n", name[0]);
48 long ret = vdso_gettimeofday(&tv, 0);
51 printf("The time is %lld.%06lld\n",
52 (long long)tv.tv_sec, (long long)tv.tv_usec);
54 printf("%s failed\n", name[0]);
61 static int vdso_test_clock_gettime(clockid_t clk_id)
63 /* Find clock_gettime. */
64 vdso_clock_gettime_t vdso_clock_gettime =
65 (vdso_clock_gettime_t)vdso_sym(version, name[1]);
67 if (!vdso_clock_gettime) {
68 printf("Could not find %s\n", name[1]);
73 long ret = vdso_clock_gettime(clk_id, &ts);
76 printf("The time is %lld.%06lld\n",
77 (long long)ts.tv_sec, (long long)ts.tv_nsec);
79 printf("%s failed\n", name[1]);
86 static int vdso_test_time(void)
89 vdso_time_t vdso_time =
90 (vdso_time_t)vdso_sym(version, name[2]);
93 printf("Could not find %s\n", name[2]);
97 long ret = vdso_time(NULL);
100 printf("The time in hours since January 1, 1970 is %lld\n",
101 (long long)(ret / 3600));
103 printf("%s failed\n", name[2]);
110 static int vdso_test_clock_getres(clockid_t clk_id)
112 /* Find clock_getres. */
113 vdso_clock_getres_t vdso_clock_getres =
114 (vdso_clock_getres_t)vdso_sym(version, name[3]);
116 if (!vdso_clock_getres) {
117 printf("Could not find %s\n", name[3]);
121 struct timespec ts, sys_ts;
122 long ret = vdso_clock_getres(clk_id, &ts);
125 printf("The resolution is %lld %lld\n",
126 (long long)ts.tv_sec, (long long)ts.tv_nsec);
128 printf("%s failed\n", name[3]);
132 ret = syscall(SYS_clock_getres, clk_id, &sys_ts);
134 if ((sys_ts.tv_sec != ts.tv_sec) || (sys_ts.tv_nsec != ts.tv_nsec)) {
135 printf("%s failed\n", name[3]);
142 const char *vdso_clock_name[12] = {
145 "CLOCK_PROCESS_CPUTIME_ID",
146 "CLOCK_THREAD_CPUTIME_ID",
147 "CLOCK_MONOTONIC_RAW",
148 "CLOCK_REALTIME_COARSE",
149 "CLOCK_MONOTONIC_COARSE",
151 "CLOCK_REALTIME_ALARM",
152 "CLOCK_BOOTTIME_ALARM",
158 * This function calls vdso_test_clock_gettime and vdso_test_clock_getres
159 * with different values for clock_id.
161 static inline int vdso_test_clock(clockid_t clock_id)
165 ret0 = vdso_test_clock_gettime(clock_id);
166 /* A skipped test is considered passed */
167 if (ret0 == KSFT_SKIP)
170 ret1 = vdso_test_clock_getres(clock_id);
171 /* A skipped test is considered passed */
172 if (ret1 == KSFT_SKIP)
177 printf("clock_id: %s", vdso_clock_name[clock_id]);
187 int main(int argc, char **argv)
189 unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
193 printf("AT_SYSINFO_EHDR is not present!\n");
197 version = versions[VDSO_VERSION];
198 name = (const char **)&names[VDSO_NAMES];
200 printf("[vDSO kselftest] VDSO_VERSION: %s\n", version);
202 vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
204 ret = vdso_test_gettimeofday();
206 #if _POSIX_TIMERS > 0
208 #ifdef CLOCK_REALTIME
209 ret += vdso_test_clock(CLOCK_REALTIME);
212 #ifdef CLOCK_BOOTTIME
213 ret += vdso_test_clock(CLOCK_BOOTTIME);
217 ret += vdso_test_clock(CLOCK_TAI);
220 #ifdef CLOCK_REALTIME_COARSE
221 ret += vdso_test_clock(CLOCK_REALTIME_COARSE);
224 #ifdef CLOCK_MONOTONIC
225 ret += vdso_test_clock(CLOCK_MONOTONIC);
228 #ifdef CLOCK_MONOTONIC_RAW
229 ret += vdso_test_clock(CLOCK_MONOTONIC_RAW);
232 #ifdef CLOCK_MONOTONIC_COARSE
233 ret += vdso_test_clock(CLOCK_MONOTONIC_COARSE);
238 ret += vdso_test_time();