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 "../kselftest.h"
25 static const char **name;
29 # define SYS_getcpu 309
31 # define SYS_getcpu 318
35 #ifndef __NR_clock_gettime64
36 #define __NR_clock_gettime64 403
39 #ifndef __kernel_timespec
40 struct __kernel_timespec {
46 /* max length of lines in /proc/self/maps - anything longer is skipped here */
47 #define MAPS_LINE_LEN 128
51 typedef int (*vgettime_t)(clockid_t, struct timespec *);
53 vgettime_t vdso_clock_gettime;
55 typedef int (*vgettime64_t)(clockid_t, struct __kernel_timespec *);
57 vgettime64_t vdso_clock_gettime64;
59 typedef long (*vgtod_t)(struct timeval *tv, struct timezone *tz);
61 vgtod_t vdso_gettimeofday;
63 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
68 static void *vsyscall_getcpu(void)
72 char line[MAPS_LINE_LEN];
75 maps = fopen("/proc/self/maps", "r");
76 if (!maps) /* might still be present, but ignore it here, as we test vDSO not vsyscall */
79 while (fgets(line, MAPS_LINE_LEN, maps)) {
82 char name[MAPS_LINE_LEN];
84 /* sscanf() is safe here as strlen(name) >= strlen(line) */
85 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
86 &start, &end, &r, &x, name) != 5)
89 if (strcmp(name, "[vsyscall]"))
92 /* assume entries are OK, as we test vDSO here not vsyscall */
100 printf("Warning: failed to find vsyscall getcpu\n");
103 return (void *) (0xffffffffff600800);
110 static void fill_function_pointers()
112 void *vdso = dlopen("linux-vdso.so.1",
113 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
115 vdso = dlopen("linux-gate.so.1",
116 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
118 printf("[WARN]\tfailed to find vDSO\n");
122 vdso_getcpu = (getcpu_t)dlsym(vdso, name[4]);
124 printf("Warning: failed to find getcpu in vDSO\n");
126 vgetcpu = (getcpu_t) vsyscall_getcpu();
128 vdso_clock_gettime = (vgettime_t)dlsym(vdso, name[1]);
129 if (!vdso_clock_gettime)
130 printf("Warning: failed to find clock_gettime in vDSO\n");
132 #if defined(VDSO_32BIT)
133 vdso_clock_gettime64 = (vgettime64_t)dlsym(vdso, name[5]);
134 if (!vdso_clock_gettime64)
135 printf("Warning: failed to find clock_gettime64 in vDSO\n");
138 vdso_gettimeofday = (vgtod_t)dlsym(vdso, name[0]);
139 if (!vdso_gettimeofday)
140 printf("Warning: failed to find gettimeofday in vDSO\n");
144 static long sys_getcpu(unsigned * cpu, unsigned * node,
147 return syscall(__NR_getcpu, cpu, node, cache);
150 static inline int sys_clock_gettime(clockid_t id, struct timespec *ts)
152 return syscall(__NR_clock_gettime, id, ts);
155 static inline int sys_clock_gettime64(clockid_t id, struct __kernel_timespec *ts)
157 return syscall(__NR_clock_gettime64, id, ts);
160 static inline int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
162 return syscall(__NR_gettimeofday, tv, tz);
165 static void test_getcpu(void)
167 printf("[RUN]\tTesting getcpu...\n");
169 for (int cpu = 0; ; cpu++) {
172 CPU_SET(cpu, &cpuset);
173 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
176 unsigned cpu_sys, cpu_vdso, cpu_vsys,
177 node_sys, node_vdso, node_vsys;
178 long ret_sys, ret_vdso = 1, ret_vsys = 1;
181 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
183 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
185 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
195 if (!ret_sys && (cpu_sys != cpu || node_sys != node))
197 if (!ret_vdso && (cpu_vdso != cpu || node_vdso != node))
199 if (!ret_vsys && (cpu_vsys != cpu || node_vsys != node))
202 printf("[%s]\tCPU %u:", ok ? "OK" : "FAIL", cpu);
204 printf(" syscall: cpu %u, node %u", cpu_sys, node_sys);
206 printf(" vdso: cpu %u, node %u", cpu_vdso, node_vdso);
208 printf(" vsyscall: cpu %u, node %u", cpu_vsys,
217 static bool ts_leq(const struct timespec *a, const struct timespec *b)
219 if (a->tv_sec != b->tv_sec)
220 return a->tv_sec < b->tv_sec;
222 return a->tv_nsec <= b->tv_nsec;
225 static bool ts64_leq(const struct __kernel_timespec *a,
226 const struct __kernel_timespec *b)
228 if (a->tv_sec != b->tv_sec)
229 return a->tv_sec < b->tv_sec;
231 return a->tv_nsec <= b->tv_nsec;
234 static bool tv_leq(const struct timeval *a, const struct timeval *b)
236 if (a->tv_sec != b->tv_sec)
237 return a->tv_sec < b->tv_sec;
239 return a->tv_usec <= b->tv_usec;
242 static char const * const clocknames[] = {
243 [0] = "CLOCK_REALTIME",
244 [1] = "CLOCK_MONOTONIC",
245 [2] = "CLOCK_PROCESS_CPUTIME_ID",
246 [3] = "CLOCK_THREAD_CPUTIME_ID",
247 [4] = "CLOCK_MONOTONIC_RAW",
248 [5] = "CLOCK_REALTIME_COARSE",
249 [6] = "CLOCK_MONOTONIC_COARSE",
250 [7] = "CLOCK_BOOTTIME",
251 [8] = "CLOCK_REALTIME_ALARM",
252 [9] = "CLOCK_BOOTTIME_ALARM",
253 [10] = "CLOCK_SGI_CYCLE",
257 static void test_one_clock_gettime(int clock, const char *name)
259 struct timespec start, vdso, end;
260 int vdso_ret, end_ret;
262 printf("[RUN]\tTesting clock_gettime for clock %s (%d)...\n", name, clock);
264 if (sys_clock_gettime(clock, &start) < 0) {
265 if (errno == EINVAL) {
266 vdso_ret = vdso_clock_gettime(clock, &vdso);
267 if (vdso_ret == -EINVAL) {
268 printf("[OK]\tNo such clock.\n");
270 printf("[FAIL]\tNo such clock, but __vdso_clock_gettime returned %d\n", vdso_ret);
274 printf("[WARN]\t clock_gettime(%d) syscall returned error %d\n", clock, errno);
279 vdso_ret = vdso_clock_gettime(clock, &vdso);
280 end_ret = sys_clock_gettime(clock, &end);
282 if (vdso_ret != 0 || end_ret != 0) {
283 printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
289 printf("\t%llu.%09ld %llu.%09ld %llu.%09ld\n",
290 (unsigned long long)start.tv_sec, start.tv_nsec,
291 (unsigned long long)vdso.tv_sec, vdso.tv_nsec,
292 (unsigned long long)end.tv_sec, end.tv_nsec);
294 if (!ts_leq(&start, &vdso) || !ts_leq(&vdso, &end)) {
295 printf("[FAIL]\tTimes are out of sequence\n");
300 printf("[OK]\tTest Passed.\n");
303 static void test_clock_gettime(void)
305 if (!vdso_clock_gettime) {
306 printf("[SKIP]\tNo vDSO, so skipping clock_gettime() tests\n");
310 for (int clock = 0; clock < ARRAY_SIZE(clocknames); clock++)
311 test_one_clock_gettime(clock, clocknames[clock]);
313 /* Also test some invalid clock ids */
314 test_one_clock_gettime(-1, "invalid");
315 test_one_clock_gettime(INT_MIN, "invalid");
316 test_one_clock_gettime(INT_MAX, "invalid");
319 static void test_one_clock_gettime64(int clock, const char *name)
321 struct __kernel_timespec start, vdso, end;
322 int vdso_ret, end_ret;
324 printf("[RUN]\tTesting clock_gettime64 for clock %s (%d)...\n", name, clock);
326 if (sys_clock_gettime64(clock, &start) < 0) {
327 if (errno == EINVAL) {
328 vdso_ret = vdso_clock_gettime64(clock, &vdso);
329 if (vdso_ret == -EINVAL) {
330 printf("[OK]\tNo such clock.\n");
332 printf("[FAIL]\tNo such clock, but __vdso_clock_gettime64 returned %d\n", vdso_ret);
336 printf("[WARN]\t clock_gettime64(%d) syscall returned error %d\n", clock, errno);
341 vdso_ret = vdso_clock_gettime64(clock, &vdso);
342 end_ret = sys_clock_gettime64(clock, &end);
344 if (vdso_ret != 0 || end_ret != 0) {
345 printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
351 printf("\t%llu.%09lld %llu.%09lld %llu.%09lld\n",
352 (unsigned long long)start.tv_sec, start.tv_nsec,
353 (unsigned long long)vdso.tv_sec, vdso.tv_nsec,
354 (unsigned long long)end.tv_sec, end.tv_nsec);
356 if (!ts64_leq(&start, &vdso) || !ts64_leq(&vdso, &end)) {
357 printf("[FAIL]\tTimes are out of sequence\n");
362 printf("[OK]\tTest Passed.\n");
365 static void test_clock_gettime64(void)
367 if (!vdso_clock_gettime64) {
368 printf("[SKIP]\tNo vDSO, so skipping clock_gettime64() tests\n");
372 for (int clock = 0; clock < ARRAY_SIZE(clocknames); clock++)
373 test_one_clock_gettime64(clock, clocknames[clock]);
375 /* Also test some invalid clock ids */
376 test_one_clock_gettime64(-1, "invalid");
377 test_one_clock_gettime64(INT_MIN, "invalid");
378 test_one_clock_gettime64(INT_MAX, "invalid");
381 static void test_gettimeofday(void)
383 struct timeval start, vdso, end;
384 struct timezone sys_tz, vdso_tz;
385 int vdso_ret, end_ret;
387 if (!vdso_gettimeofday)
390 printf("[RUN]\tTesting gettimeofday...\n");
392 if (sys_gettimeofday(&start, &sys_tz) < 0) {
393 printf("[FAIL]\tsys_gettimeofday failed (%d)\n", errno);
398 vdso_ret = vdso_gettimeofday(&vdso, &vdso_tz);
399 end_ret = sys_gettimeofday(&end, NULL);
401 if (vdso_ret != 0 || end_ret != 0) {
402 printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
408 printf("\t%llu.%06ld %llu.%06ld %llu.%06ld\n",
409 (unsigned long long)start.tv_sec, start.tv_usec,
410 (unsigned long long)vdso.tv_sec, vdso.tv_usec,
411 (unsigned long long)end.tv_sec, end.tv_usec);
413 if (!tv_leq(&start, &vdso) || !tv_leq(&vdso, &end)) {
414 printf("[FAIL]\tTimes are out of sequence\n");
418 if (sys_tz.tz_minuteswest == vdso_tz.tz_minuteswest &&
419 sys_tz.tz_dsttime == vdso_tz.tz_dsttime) {
420 printf("[OK]\ttimezones match: minuteswest=%d, dsttime=%d\n",
421 sys_tz.tz_minuteswest, sys_tz.tz_dsttime);
423 printf("[FAIL]\ttimezones do not match\n");
427 /* And make sure that passing NULL for tz doesn't crash. */
428 vdso_gettimeofday(&vdso, NULL);
431 int main(int argc, char **argv)
433 name = (const char **)&names[VDSO_NAMES];
435 fill_function_pointers();
437 test_clock_gettime();
438 test_clock_gettime64();
442 * Test getcpu() last so that, if something goes wrong setting affinity,
443 * we still run the other tests.
447 return nerrs ? 1 : 0;