1 // SPDX-License-Identifier: GPL-2.0
3 * ldt_gdt.c - Test cases for LDT and GDT access
4 * Copyright (c) 2011-2015 Andrew Lutomirski
14 #include <sys/syscall.h>
24 # define SYS_getcpu 309
26 # define SYS_getcpu 318
30 /* max length of lines in /proc/self/maps - anything longer is skipped here */
31 #define MAPS_LINE_LEN 128
35 typedef int (*vgettime_t
)(clockid_t
, struct timespec
*);
37 vgettime_t vdso_clock_gettime
;
39 typedef long (*vgtod_t
)(struct timeval
*tv
, struct timezone
*tz
);
41 vgtod_t vdso_gettimeofday
;
43 typedef long (*getcpu_t
)(unsigned *, unsigned *, void *);
48 static void *vsyscall_getcpu(void)
52 char line
[MAPS_LINE_LEN
];
55 maps
= fopen("/proc/self/maps", "r");
56 if (!maps
) /* might still be present, but ignore it here, as we test vDSO not vsyscall */
59 while (fgets(line
, MAPS_LINE_LEN
, maps
)) {
62 char name
[MAPS_LINE_LEN
];
64 /* sscanf() is safe here as strlen(name) >= strlen(line) */
65 if (sscanf(line
, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
66 &start
, &end
, &r
, &x
, name
) != 5)
69 if (strcmp(name
, "[vsyscall]"))
72 /* assume entries are OK, as we test vDSO here not vsyscall */
80 printf("Warning: failed to find vsyscall getcpu\n");
83 return (void *) (0xffffffffff600800);
90 static void fill_function_pointers()
92 void *vdso
= dlopen("linux-vdso.so.1",
93 RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
95 vdso
= dlopen("linux-gate.so.1",
96 RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
98 printf("[WARN]\tfailed to find vDSO\n");
102 vdso_getcpu
= (getcpu_t
)dlsym(vdso
, "__vdso_getcpu");
104 printf("Warning: failed to find getcpu in vDSO\n");
106 vgetcpu
= (getcpu_t
) vsyscall_getcpu();
108 vdso_clock_gettime
= (vgettime_t
)dlsym(vdso
, "__vdso_clock_gettime");
109 if (!vdso_clock_gettime
)
110 printf("Warning: failed to find clock_gettime in vDSO\n");
112 vdso_gettimeofday
= (vgtod_t
)dlsym(vdso
, "__vdso_gettimeofday");
113 if (!vdso_gettimeofday
)
114 printf("Warning: failed to find gettimeofday in vDSO\n");
118 static long sys_getcpu(unsigned * cpu
, unsigned * node
,
121 return syscall(__NR_getcpu
, cpu
, node
, cache
);
124 static inline int sys_clock_gettime(clockid_t id
, struct timespec
*ts
)
126 return syscall(__NR_clock_gettime
, id
, ts
);
129 static inline int sys_gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
131 return syscall(__NR_gettimeofday
, tv
, tz
);
134 static void test_getcpu(void)
136 printf("[RUN]\tTesting getcpu...\n");
138 for (int cpu
= 0; ; cpu
++) {
141 CPU_SET(cpu
, &cpuset
);
142 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
145 unsigned cpu_sys
, cpu_vdso
, cpu_vsys
,
146 node_sys
, node_vdso
, node_vsys
;
147 long ret_sys
, ret_vdso
= 1, ret_vsys
= 1;
150 ret_sys
= sys_getcpu(&cpu_sys
, &node_sys
, 0);
152 ret_vdso
= vdso_getcpu(&cpu_vdso
, &node_vdso
, 0);
154 ret_vsys
= vgetcpu(&cpu_vsys
, &node_vsys
, 0);
164 if (!ret_sys
&& (cpu_sys
!= cpu
|| node_sys
!= node
))
166 if (!ret_vdso
&& (cpu_vdso
!= cpu
|| node_vdso
!= node
))
168 if (!ret_vsys
&& (cpu_vsys
!= cpu
|| node_vsys
!= node
))
171 printf("[%s]\tCPU %u:", ok
? "OK" : "FAIL", cpu
);
173 printf(" syscall: cpu %u, node %u", cpu_sys
, node_sys
);
175 printf(" vdso: cpu %u, node %u", cpu_vdso
, node_vdso
);
177 printf(" vsyscall: cpu %u, node %u", cpu_vsys
,
186 static bool ts_leq(const struct timespec
*a
, const struct timespec
*b
)
188 if (a
->tv_sec
!= b
->tv_sec
)
189 return a
->tv_sec
< b
->tv_sec
;
191 return a
->tv_nsec
<= b
->tv_nsec
;
194 static bool tv_leq(const struct timeval
*a
, const struct timeval
*b
)
196 if (a
->tv_sec
!= b
->tv_sec
)
197 return a
->tv_sec
< b
->tv_sec
;
199 return a
->tv_usec
<= b
->tv_usec
;
202 static char const * const clocknames
[] = {
203 [0] = "CLOCK_REALTIME",
204 [1] = "CLOCK_MONOTONIC",
205 [2] = "CLOCK_PROCESS_CPUTIME_ID",
206 [3] = "CLOCK_THREAD_CPUTIME_ID",
207 [4] = "CLOCK_MONOTONIC_RAW",
208 [5] = "CLOCK_REALTIME_COARSE",
209 [6] = "CLOCK_MONOTONIC_COARSE",
210 [7] = "CLOCK_BOOTTIME",
211 [8] = "CLOCK_REALTIME_ALARM",
212 [9] = "CLOCK_BOOTTIME_ALARM",
213 [10] = "CLOCK_SGI_CYCLE",
217 static void test_one_clock_gettime(int clock
, const char *name
)
219 struct timespec start
, vdso
, end
;
220 int vdso_ret
, end_ret
;
222 printf("[RUN]\tTesting clock_gettime for clock %s (%d)...\n", name
, clock
);
224 if (sys_clock_gettime(clock
, &start
) < 0) {
225 if (errno
== EINVAL
) {
226 vdso_ret
= vdso_clock_gettime(clock
, &vdso
);
227 if (vdso_ret
== -EINVAL
) {
228 printf("[OK]\tNo such clock.\n");
230 printf("[FAIL]\tNo such clock, but __vdso_clock_gettime returned %d\n", vdso_ret
);
234 printf("[WARN]\t clock_gettime(%d) syscall returned error %d\n", clock
, errno
);
239 vdso_ret
= vdso_clock_gettime(clock
, &vdso
);
240 end_ret
= sys_clock_gettime(clock
, &end
);
242 if (vdso_ret
!= 0 || end_ret
!= 0) {
243 printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
249 printf("\t%llu.%09ld %llu.%09ld %llu.%09ld\n",
250 (unsigned long long)start
.tv_sec
, start
.tv_nsec
,
251 (unsigned long long)vdso
.tv_sec
, vdso
.tv_nsec
,
252 (unsigned long long)end
.tv_sec
, end
.tv_nsec
);
254 if (!ts_leq(&start
, &vdso
) || !ts_leq(&vdso
, &end
)) {
255 printf("[FAIL]\tTimes are out of sequence\n");
260 static void test_clock_gettime(void)
262 for (int clock
= 0; clock
< sizeof(clocknames
) / sizeof(clocknames
[0]);
264 test_one_clock_gettime(clock
, clocknames
[clock
]);
267 /* Also test some invalid clock ids */
268 test_one_clock_gettime(-1, "invalid");
269 test_one_clock_gettime(INT_MIN
, "invalid");
270 test_one_clock_gettime(INT_MAX
, "invalid");
273 static void test_gettimeofday(void)
275 struct timeval start
, vdso
, end
;
276 struct timezone sys_tz
, vdso_tz
;
277 int vdso_ret
, end_ret
;
279 if (!vdso_gettimeofday
)
282 printf("[RUN]\tTesting gettimeofday...\n");
284 if (sys_gettimeofday(&start
, &sys_tz
) < 0) {
285 printf("[FAIL]\tsys_gettimeofday failed (%d)\n", errno
);
290 vdso_ret
= vdso_gettimeofday(&vdso
, &vdso_tz
);
291 end_ret
= sys_gettimeofday(&end
, NULL
);
293 if (vdso_ret
!= 0 || end_ret
!= 0) {
294 printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
300 printf("\t%llu.%06ld %llu.%06ld %llu.%06ld\n",
301 (unsigned long long)start
.tv_sec
, start
.tv_usec
,
302 (unsigned long long)vdso
.tv_sec
, vdso
.tv_usec
,
303 (unsigned long long)end
.tv_sec
, end
.tv_usec
);
305 if (!tv_leq(&start
, &vdso
) || !tv_leq(&vdso
, &end
)) {
306 printf("[FAIL]\tTimes are out of sequence\n");
310 if (sys_tz
.tz_minuteswest
== vdso_tz
.tz_minuteswest
&&
311 sys_tz
.tz_dsttime
== vdso_tz
.tz_dsttime
) {
312 printf("[OK]\ttimezones match: minuteswest=%d, dsttime=%d\n",
313 sys_tz
.tz_minuteswest
, sys_tz
.tz_dsttime
);
315 printf("[FAIL]\ttimezones do not match\n");
319 /* And make sure that passing NULL for tz doesn't crash. */
320 vdso_gettimeofday(&vdso
, NULL
);
323 int main(int argc
, char **argv
)
325 fill_function_pointers();
327 test_clock_gettime();
331 * Test getcpu() last so that, if something goes wrong setting affinity,
332 * we still run the other tests.
336 return nerrs
? 1 : 0;