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"
23 #include "vdso_call.h"
24 #include "parse_vdso.h"
26 static const char *version
;
27 static const char **name
;
29 typedef long (*vdso_gettimeofday_t
)(struct timeval
*tv
, struct timezone
*tz
);
30 typedef long (*vdso_clock_gettime_t
)(clockid_t clk_id
, struct timespec
*ts
);
31 typedef long (*vdso_clock_getres_t
)(clockid_t clk_id
, struct timespec
*ts
);
32 typedef time_t (*vdso_time_t
)(time_t *t
);
34 const char *vdso_clock_name
[12] = {
37 "CLOCK_PROCESS_CPUTIME_ID",
38 "CLOCK_THREAD_CPUTIME_ID",
39 "CLOCK_MONOTONIC_RAW",
40 "CLOCK_REALTIME_COARSE",
41 "CLOCK_MONOTONIC_COARSE",
43 "CLOCK_REALTIME_ALARM",
44 "CLOCK_BOOTTIME_ALARM",
49 static void vdso_test_gettimeofday(void)
51 /* Find gettimeofday. */
52 vdso_gettimeofday_t vdso_gettimeofday
=
53 (vdso_gettimeofday_t
)vdso_sym(version
, name
[0]);
55 if (!vdso_gettimeofday
) {
56 ksft_print_msg("Couldn't find %s\n", name
[0]);
57 ksft_test_result_skip("%s\n", name
[0]);
62 long ret
= VDSO_CALL(vdso_gettimeofday
, 2, &tv
, 0);
65 ksft_print_msg("The time is %lld.%06lld\n",
66 (long long)tv
.tv_sec
, (long long)tv
.tv_usec
);
67 ksft_test_result_pass("%s\n", name
[0]);
69 ksft_test_result_fail("%s\n", name
[0]);
73 static void vdso_test_clock_gettime(clockid_t clk_id
)
75 /* Find clock_gettime. */
76 vdso_clock_gettime_t vdso_clock_gettime
=
77 (vdso_clock_gettime_t
)vdso_sym(version
, name
[1]);
79 if (!vdso_clock_gettime
) {
80 ksft_print_msg("Couldn't find %s\n", name
[1]);
81 ksft_test_result_skip("%s %s\n", name
[1],
82 vdso_clock_name
[clk_id
]);
87 long ret
= VDSO_CALL(vdso_clock_gettime
, 2, clk_id
, &ts
);
90 ksft_print_msg("The time is %lld.%06lld\n",
91 (long long)ts
.tv_sec
, (long long)ts
.tv_nsec
);
92 ksft_test_result_pass("%s %s\n", name
[1],
93 vdso_clock_name
[clk_id
]);
95 ksft_test_result_fail("%s %s\n", name
[1],
96 vdso_clock_name
[clk_id
]);
100 static void vdso_test_time(void)
103 vdso_time_t vdso_time
=
104 (vdso_time_t
)vdso_sym(version
, name
[2]);
107 ksft_print_msg("Couldn't find %s\n", name
[2]);
108 ksft_test_result_skip("%s\n", name
[2]);
112 long ret
= VDSO_CALL(vdso_time
, 1, NULL
);
115 ksft_print_msg("The time in hours since January 1, 1970 is %lld\n",
116 (long long)(ret
/ 3600));
117 ksft_test_result_pass("%s\n", name
[2]);
119 ksft_test_result_fail("%s\n", name
[2]);
123 static void vdso_test_clock_getres(clockid_t clk_id
)
125 int clock_getres_fail
= 0;
127 /* Find clock_getres. */
128 vdso_clock_getres_t vdso_clock_getres
=
129 (vdso_clock_getres_t
)vdso_sym(version
, name
[3]);
131 if (!vdso_clock_getres
) {
132 ksft_print_msg("Couldn't find %s\n", name
[3]);
133 ksft_test_result_skip("%s %s\n", name
[3],
134 vdso_clock_name
[clk_id
]);
138 struct timespec ts
, sys_ts
;
139 long ret
= VDSO_CALL(vdso_clock_getres
, 2, clk_id
, &ts
);
142 ksft_print_msg("The vdso resolution is %lld %lld\n",
143 (long long)ts
.tv_sec
, (long long)ts
.tv_nsec
);
148 ret
= syscall(SYS_clock_getres
, clk_id
, &sys_ts
);
150 ksft_print_msg("The syscall resolution is %lld %lld\n",
151 (long long)sys_ts
.tv_sec
, (long long)sys_ts
.tv_nsec
);
153 if ((sys_ts
.tv_sec
!= ts
.tv_sec
) || (sys_ts
.tv_nsec
!= ts
.tv_nsec
))
156 if (clock_getres_fail
> 0) {
157 ksft_test_result_fail("%s %s\n", name
[3],
158 vdso_clock_name
[clk_id
]);
160 ksft_test_result_pass("%s %s\n", name
[3],
161 vdso_clock_name
[clk_id
]);
166 * This function calls vdso_test_clock_gettime and vdso_test_clock_getres
167 * with different values for clock_id.
169 static inline void vdso_test_clock(clockid_t clock_id
)
171 ksft_print_msg("clock_id: %s\n", vdso_clock_name
[clock_id
]);
173 vdso_test_clock_gettime(clock_id
);
175 vdso_test_clock_getres(clock_id
);
178 #define VDSO_TEST_PLAN 16
180 int main(int argc
, char **argv
)
182 unsigned long sysinfo_ehdr
= getauxval(AT_SYSINFO_EHDR
);
185 ksft_set_plan(VDSO_TEST_PLAN
);
188 ksft_print_msg("AT_SYSINFO_EHDR is not present!\n");
192 version
= versions
[VDSO_VERSION
];
193 name
= (const char **)&names
[VDSO_NAMES
];
195 ksft_print_msg("[vDSO kselftest] VDSO_VERSION: %s\n", version
);
197 vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR
));
199 vdso_test_gettimeofday();
201 #if _POSIX_TIMERS > 0
203 #ifdef CLOCK_REALTIME
204 vdso_test_clock(CLOCK_REALTIME
);
207 #ifdef CLOCK_BOOTTIME
208 vdso_test_clock(CLOCK_BOOTTIME
);
212 vdso_test_clock(CLOCK_TAI
);
215 #ifdef CLOCK_REALTIME_COARSE
216 vdso_test_clock(CLOCK_REALTIME_COARSE
);
219 #ifdef CLOCK_MONOTONIC
220 vdso_test_clock(CLOCK_MONOTONIC
);
223 #ifdef CLOCK_MONOTONIC_RAW
224 vdso_test_clock(CLOCK_MONOTONIC_RAW
);
227 #ifdef CLOCK_MONOTONIC_COARSE
228 vdso_test_clock(CLOCK_MONOTONIC_COARSE
);
236 return ksft_get_fail_cnt() == 0 ? KSFT_PASS
: KSFT_FAIL
;