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>
22 #include "vdso_config.h"
23 #include "vdso_call.h"
24 #include "../kselftest.h"
26 static const char **name
;
30 # define SYS_getcpu 309
32 # define SYS_getcpu 318
36 #ifndef __NR_clock_gettime64
37 #define __NR_clock_gettime64 403
40 #ifndef __kernel_timespec
41 struct __kernel_timespec
{
47 /* max length of lines in /proc/self/maps - anything longer is skipped here */
48 #define MAPS_LINE_LEN 128
52 typedef int (*vgettime_t
)(clockid_t
, struct timespec
*);
54 vgettime_t vdso_clock_gettime
;
56 typedef int (*vgettime64_t
)(clockid_t
, struct __kernel_timespec
*);
58 vgettime64_t vdso_clock_gettime64
;
60 typedef long (*vgtod_t
)(struct timeval
*tv
, struct timezone
*tz
);
62 vgtod_t vdso_gettimeofday
;
64 typedef long (*getcpu_t
)(unsigned *, unsigned *, void *);
69 static void *vsyscall_getcpu(void)
73 char line
[MAPS_LINE_LEN
];
76 maps
= fopen("/proc/self/maps", "r");
77 if (!maps
) /* might still be present, but ignore it here, as we test vDSO not vsyscall */
80 while (fgets(line
, MAPS_LINE_LEN
, maps
)) {
83 char name
[MAPS_LINE_LEN
];
85 /* sscanf() is safe here as strlen(name) >= strlen(line) */
86 if (sscanf(line
, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
87 &start
, &end
, &r
, &x
, name
) != 5)
90 if (strcmp(name
, "[vsyscall]"))
93 /* assume entries are OK, as we test vDSO here not vsyscall */
101 printf("Warning: failed to find vsyscall getcpu\n");
104 return (void *) (0xffffffffff600800);
111 static void fill_function_pointers()
113 void *vdso
= dlopen("linux-vdso.so.1",
114 RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
116 vdso
= dlopen("linux-gate.so.1",
117 RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
119 vdso
= dlopen("linux-vdso32.so.1",
120 RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
122 vdso
= dlopen("linux-vdso64.so.1",
123 RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
125 printf("[WARN]\tfailed to find vDSO\n");
129 vdso_getcpu
= (getcpu_t
)dlsym(vdso
, name
[4]);
131 printf("Warning: failed to find getcpu in vDSO\n");
133 vgetcpu
= (getcpu_t
) vsyscall_getcpu();
135 vdso_clock_gettime
= (vgettime_t
)dlsym(vdso
, name
[1]);
136 if (!vdso_clock_gettime
)
137 printf("Warning: failed to find clock_gettime in vDSO\n");
139 #if defined(VDSO_32BIT)
140 vdso_clock_gettime64
= (vgettime64_t
)dlsym(vdso
, name
[5]);
141 if (!vdso_clock_gettime64
)
142 printf("Warning: failed to find clock_gettime64 in vDSO\n");
145 vdso_gettimeofday
= (vgtod_t
)dlsym(vdso
, name
[0]);
146 if (!vdso_gettimeofday
)
147 printf("Warning: failed to find gettimeofday in vDSO\n");
151 static long sys_getcpu(unsigned * cpu
, unsigned * node
,
154 return syscall(__NR_getcpu
, cpu
, node
, cache
);
157 static inline int sys_clock_gettime(clockid_t id
, struct timespec
*ts
)
159 return syscall(__NR_clock_gettime
, id
, ts
);
162 static inline int sys_clock_gettime64(clockid_t id
, struct __kernel_timespec
*ts
)
164 return syscall(__NR_clock_gettime64
, id
, ts
);
167 static inline int sys_gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
169 return syscall(__NR_gettimeofday
, tv
, tz
);
172 static void test_getcpu(void)
174 printf("[RUN]\tTesting getcpu...\n");
176 for (int cpu
= 0; ; cpu
++) {
179 CPU_SET(cpu
, &cpuset
);
180 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
183 unsigned cpu_sys
, cpu_vdso
, cpu_vsys
,
184 node_sys
, node_vdso
, node_vsys
;
185 long ret_sys
, ret_vdso
= 1, ret_vsys
= 1;
188 ret_sys
= sys_getcpu(&cpu_sys
, &node_sys
, 0);
190 ret_vdso
= VDSO_CALL(vdso_getcpu
, 3, &cpu_vdso
, &node_vdso
, 0);
192 ret_vsys
= vgetcpu(&cpu_vsys
, &node_vsys
, 0);
202 if (!ret_sys
&& (cpu_sys
!= cpu
|| node_sys
!= node
))
204 if (!ret_vdso
&& (cpu_vdso
!= cpu
|| node_vdso
!= node
))
206 if (!ret_vsys
&& (cpu_vsys
!= cpu
|| node_vsys
!= node
))
209 printf("[%s]\tCPU %u:", ok
? "OK" : "FAIL", cpu
);
211 printf(" syscall: cpu %u, node %u", cpu_sys
, node_sys
);
213 printf(" vdso: cpu %u, node %u", cpu_vdso
, node_vdso
);
215 printf(" vsyscall: cpu %u, node %u", cpu_vsys
,
224 static bool ts_leq(const struct timespec
*a
, const struct timespec
*b
)
226 if (a
->tv_sec
!= b
->tv_sec
)
227 return a
->tv_sec
< b
->tv_sec
;
229 return a
->tv_nsec
<= b
->tv_nsec
;
232 static bool ts64_leq(const struct __kernel_timespec
*a
,
233 const struct __kernel_timespec
*b
)
235 if (a
->tv_sec
!= b
->tv_sec
)
236 return a
->tv_sec
< b
->tv_sec
;
238 return a
->tv_nsec
<= b
->tv_nsec
;
241 static bool tv_leq(const struct timeval
*a
, const struct timeval
*b
)
243 if (a
->tv_sec
!= b
->tv_sec
)
244 return a
->tv_sec
< b
->tv_sec
;
246 return a
->tv_usec
<= b
->tv_usec
;
249 static char const * const clocknames
[] = {
250 [0] = "CLOCK_REALTIME",
251 [1] = "CLOCK_MONOTONIC",
252 [2] = "CLOCK_PROCESS_CPUTIME_ID",
253 [3] = "CLOCK_THREAD_CPUTIME_ID",
254 [4] = "CLOCK_MONOTONIC_RAW",
255 [5] = "CLOCK_REALTIME_COARSE",
256 [6] = "CLOCK_MONOTONIC_COARSE",
257 [7] = "CLOCK_BOOTTIME",
258 [8] = "CLOCK_REALTIME_ALARM",
259 [9] = "CLOCK_BOOTTIME_ALARM",
260 [10] = "CLOCK_SGI_CYCLE",
264 static void test_one_clock_gettime(int clock
, const char *name
)
266 struct timespec start
, vdso
, end
;
267 int vdso_ret
, end_ret
;
269 printf("[RUN]\tTesting clock_gettime for clock %s (%d)...\n", name
, clock
);
271 if (sys_clock_gettime(clock
, &start
) < 0) {
272 if (errno
== EINVAL
) {
273 vdso_ret
= VDSO_CALL(vdso_clock_gettime
, 2, clock
, &vdso
);
274 if (vdso_ret
== -EINVAL
) {
275 printf("[OK]\tNo such clock.\n");
277 printf("[FAIL]\tNo such clock, but __vdso_clock_gettime returned %d\n", vdso_ret
);
281 printf("[WARN]\t clock_gettime(%d) syscall returned error %d\n", clock
, errno
);
286 vdso_ret
= VDSO_CALL(vdso_clock_gettime
, 2, clock
, &vdso
);
287 end_ret
= sys_clock_gettime(clock
, &end
);
289 if (vdso_ret
!= 0 || end_ret
!= 0) {
290 printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
296 printf("\t%llu.%09ld %llu.%09ld %llu.%09ld\n",
297 (unsigned long long)start
.tv_sec
, start
.tv_nsec
,
298 (unsigned long long)vdso
.tv_sec
, vdso
.tv_nsec
,
299 (unsigned long long)end
.tv_sec
, end
.tv_nsec
);
301 if (!ts_leq(&start
, &vdso
) || !ts_leq(&vdso
, &end
)) {
302 printf("[FAIL]\tTimes are out of sequence\n");
307 printf("[OK]\tTest Passed.\n");
310 static void test_clock_gettime(void)
312 if (!vdso_clock_gettime
) {
313 printf("[SKIP]\tNo vDSO, so skipping clock_gettime() tests\n");
317 for (int clock
= 0; clock
< ARRAY_SIZE(clocknames
); clock
++)
318 test_one_clock_gettime(clock
, clocknames
[clock
]);
320 /* Also test some invalid clock ids */
321 test_one_clock_gettime(-1, "invalid");
322 test_one_clock_gettime(INT_MIN
, "invalid");
323 test_one_clock_gettime(INT_MAX
, "invalid");
326 static void test_one_clock_gettime64(int clock
, const char *name
)
328 struct __kernel_timespec start
, vdso
, end
;
329 int vdso_ret
, end_ret
;
331 printf("[RUN]\tTesting clock_gettime64 for clock %s (%d)...\n", name
, clock
);
333 if (sys_clock_gettime64(clock
, &start
) < 0) {
334 if (errno
== EINVAL
) {
335 vdso_ret
= VDSO_CALL(vdso_clock_gettime64
, 2, clock
, &vdso
);
336 if (vdso_ret
== -EINVAL
) {
337 printf("[OK]\tNo such clock.\n");
339 printf("[FAIL]\tNo such clock, but __vdso_clock_gettime64 returned %d\n", vdso_ret
);
343 printf("[WARN]\t clock_gettime64(%d) syscall returned error %d\n", clock
, errno
);
348 vdso_ret
= VDSO_CALL(vdso_clock_gettime64
, 2, clock
, &vdso
);
349 end_ret
= sys_clock_gettime64(clock
, &end
);
351 if (vdso_ret
!= 0 || end_ret
!= 0) {
352 printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
358 printf("\t%llu.%09lld %llu.%09lld %llu.%09lld\n",
359 (unsigned long long)start
.tv_sec
, start
.tv_nsec
,
360 (unsigned long long)vdso
.tv_sec
, vdso
.tv_nsec
,
361 (unsigned long long)end
.tv_sec
, end
.tv_nsec
);
363 if (!ts64_leq(&start
, &vdso
) || !ts64_leq(&vdso
, &end
)) {
364 printf("[FAIL]\tTimes are out of sequence\n");
369 printf("[OK]\tTest Passed.\n");
372 static void test_clock_gettime64(void)
374 if (!vdso_clock_gettime64
) {
375 printf("[SKIP]\tNo vDSO, so skipping clock_gettime64() tests\n");
379 for (int clock
= 0; clock
< ARRAY_SIZE(clocknames
); clock
++)
380 test_one_clock_gettime64(clock
, clocknames
[clock
]);
382 /* Also test some invalid clock ids */
383 test_one_clock_gettime64(-1, "invalid");
384 test_one_clock_gettime64(INT_MIN
, "invalid");
385 test_one_clock_gettime64(INT_MAX
, "invalid");
388 static void test_gettimeofday(void)
390 struct timeval start
, vdso
, end
;
391 struct timezone sys_tz
, vdso_tz
;
392 int vdso_ret
, end_ret
;
394 if (!vdso_gettimeofday
)
397 printf("[RUN]\tTesting gettimeofday...\n");
399 if (sys_gettimeofday(&start
, &sys_tz
) < 0) {
400 printf("[FAIL]\tsys_gettimeofday failed (%d)\n", errno
);
405 vdso_ret
= VDSO_CALL(vdso_gettimeofday
, 2, &vdso
, &vdso_tz
);
406 end_ret
= sys_gettimeofday(&end
, NULL
);
408 if (vdso_ret
!= 0 || end_ret
!= 0) {
409 printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
415 printf("\t%llu.%06ld %llu.%06ld %llu.%06ld\n",
416 (unsigned long long)start
.tv_sec
, start
.tv_usec
,
417 (unsigned long long)vdso
.tv_sec
, vdso
.tv_usec
,
418 (unsigned long long)end
.tv_sec
, end
.tv_usec
);
420 if (!tv_leq(&start
, &vdso
) || !tv_leq(&vdso
, &end
)) {
421 printf("[FAIL]\tTimes are out of sequence\n");
425 if (sys_tz
.tz_minuteswest
== vdso_tz
.tz_minuteswest
&&
426 sys_tz
.tz_dsttime
== vdso_tz
.tz_dsttime
) {
427 printf("[OK]\ttimezones match: minuteswest=%d, dsttime=%d\n",
428 sys_tz
.tz_minuteswest
, sys_tz
.tz_dsttime
);
430 printf("[FAIL]\ttimezones do not match\n");
434 /* And make sure that passing NULL for tz doesn't crash. */
435 VDSO_CALL(vdso_gettimeofday
, 2, &vdso
, NULL
);
438 int main(int argc
, char **argv
)
440 name
= (const char **)&names
[VDSO_NAMES
];
442 fill_function_pointers();
444 test_clock_gettime();
445 test_clock_gettime64();
449 * Test getcpu() last so that, if something goes wrong setting affinity,
450 * we still run the other tests.
454 return nerrs
? 1 : 0;