WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / vDSO / vdso_test_abi.c
blob3d603f1394af4f80d23cfb8de723771112818d4a
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * vdso_full_test.c: Sample code to test all the timers.
4 * Copyright (c) 2019 Arm Ltd.
6 * Compile with:
7 * gcc -std=gnu99 vdso_full_test.c parse_vdso.c
9 */
11 #include <stdint.h>
12 #include <elf.h>
13 #include <stdio.h>
14 #include <time.h>
15 #include <sys/auxv.h>
16 #include <sys/time.h>
17 #define _GNU_SOURCE
18 #include <unistd.h>
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]);
44 return KSFT_SKIP;
47 struct timeval tv;
48 long ret = vdso_gettimeofday(&tv, 0);
50 if (ret == 0) {
51 printf("The time is %lld.%06lld\n",
52 (long long)tv.tv_sec, (long long)tv.tv_usec);
53 } else {
54 printf("%s failed\n", name[0]);
55 return KSFT_FAIL;
58 return KSFT_PASS;
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]);
69 return KSFT_SKIP;
72 struct timespec ts;
73 long ret = vdso_clock_gettime(clk_id, &ts);
75 if (ret == 0) {
76 printf("The time is %lld.%06lld\n",
77 (long long)ts.tv_sec, (long long)ts.tv_nsec);
78 } else {
79 printf("%s failed\n", name[1]);
80 return KSFT_FAIL;
83 return KSFT_PASS;
86 static int vdso_test_time(void)
88 /* Find time. */
89 vdso_time_t vdso_time =
90 (vdso_time_t)vdso_sym(version, name[2]);
92 if (!vdso_time) {
93 printf("Could not find %s\n", name[2]);
94 return KSFT_SKIP;
97 long ret = vdso_time(NULL);
99 if (ret > 0) {
100 printf("The time in hours since January 1, 1970 is %lld\n",
101 (long long)(ret / 3600));
102 } else {
103 printf("%s failed\n", name[2]);
104 return KSFT_FAIL;
107 return KSFT_PASS;
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]);
118 return KSFT_SKIP;
121 struct timespec ts, sys_ts;
122 long ret = vdso_clock_getres(clk_id, &ts);
124 if (ret == 0) {
125 printf("The resolution is %lld %lld\n",
126 (long long)ts.tv_sec, (long long)ts.tv_nsec);
127 } else {
128 printf("%s failed\n", name[3]);
129 return KSFT_FAIL;
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]);
136 return KSFT_FAIL;
139 return KSFT_PASS;
142 const char *vdso_clock_name[12] = {
143 "CLOCK_REALTIME",
144 "CLOCK_MONOTONIC",
145 "CLOCK_PROCESS_CPUTIME_ID",
146 "CLOCK_THREAD_CPUTIME_ID",
147 "CLOCK_MONOTONIC_RAW",
148 "CLOCK_REALTIME_COARSE",
149 "CLOCK_MONOTONIC_COARSE",
150 "CLOCK_BOOTTIME",
151 "CLOCK_REALTIME_ALARM",
152 "CLOCK_BOOTTIME_ALARM",
153 "CLOCK_SGI_CYCLE",
154 "CLOCK_TAI",
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)
163 int ret0, ret1;
165 ret0 = vdso_test_clock_gettime(clock_id);
166 /* A skipped test is considered passed */
167 if (ret0 == KSFT_SKIP)
168 ret0 = KSFT_PASS;
170 ret1 = vdso_test_clock_getres(clock_id);
171 /* A skipped test is considered passed */
172 if (ret1 == KSFT_SKIP)
173 ret1 = KSFT_PASS;
175 ret0 += ret1;
177 printf("clock_id: %s", vdso_clock_name[clock_id]);
179 if (ret0 > 0)
180 printf(" [FAIL]\n");
181 else
182 printf(" [PASS]\n");
184 return ret0;
187 int main(int argc, char **argv)
189 unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
190 int ret;
192 if (!sysinfo_ehdr) {
193 printf("AT_SYSINFO_EHDR is not present!\n");
194 return KSFT_SKIP;
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);
210 #endif
212 #ifdef CLOCK_BOOTTIME
213 ret += vdso_test_clock(CLOCK_BOOTTIME);
214 #endif
216 #ifdef CLOCK_TAI
217 ret += vdso_test_clock(CLOCK_TAI);
218 #endif
220 #ifdef CLOCK_REALTIME_COARSE
221 ret += vdso_test_clock(CLOCK_REALTIME_COARSE);
222 #endif
224 #ifdef CLOCK_MONOTONIC
225 ret += vdso_test_clock(CLOCK_MONOTONIC);
226 #endif
228 #ifdef CLOCK_MONOTONIC_RAW
229 ret += vdso_test_clock(CLOCK_MONOTONIC_RAW);
230 #endif
232 #ifdef CLOCK_MONOTONIC_COARSE
233 ret += vdso_test_clock(CLOCK_MONOTONIC_COARSE);
234 #endif
236 #endif
238 ret += vdso_test_time();
240 if (ret > 0)
241 return KSFT_FAIL;
243 return KSFT_PASS;