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>
23 # define SYS_getcpu 309
25 # define SYS_getcpu 318
37 typedef long (*getcpu_t
)(unsigned *, unsigned *, void *);
39 const getcpu_t vgetcpu
= (getcpu_t
)VSYS(0xffffffffff600800);
42 void fill_function_pointers()
44 void *vdso
= dlopen("linux-vdso.so.1",
45 RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
47 vdso
= dlopen("linux-gate.so.1",
48 RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
50 printf("[WARN]\tfailed to find vDSO\n");
54 vdso_getcpu
= (getcpu_t
)dlsym(vdso
, "__vdso_getcpu");
56 printf("Warning: failed to find getcpu in vDSO\n");
59 static long sys_getcpu(unsigned * cpu
, unsigned * node
,
62 return syscall(__NR_getcpu
, cpu
, node
, cache
);
65 static void test_getcpu(void)
67 printf("[RUN]\tTesting getcpu...\n");
69 for (int cpu
= 0; ; cpu
++) {
72 CPU_SET(cpu
, &cpuset
);
73 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0)
76 unsigned cpu_sys
, cpu_vdso
, cpu_vsys
,
77 node_sys
, node_vdso
, node_vsys
;
78 long ret_sys
, ret_vdso
= 1, ret_vsys
= 1;
81 ret_sys
= sys_getcpu(&cpu_sys
, &node_sys
, 0);
83 ret_vdso
= vdso_getcpu(&cpu_vdso
, &node_vdso
, 0);
85 ret_vsys
= vgetcpu(&cpu_vsys
, &node_vsys
, 0);
95 if (!ret_sys
&& (cpu_sys
!= cpu
|| node_sys
!= node
))
97 if (!ret_vdso
&& (cpu_vdso
!= cpu
|| node_vdso
!= node
))
99 if (!ret_vsys
&& (cpu_vsys
!= cpu
|| node_vsys
!= node
))
102 printf("[%s]\tCPU %u:", ok
? "OK" : "FAIL", cpu
);
104 printf(" syscall: cpu %u, node %u", cpu_sys
, node_sys
);
106 printf(" vdso: cpu %u, node %u", cpu_vdso
, node_vdso
);
108 printf(" vsyscall: cpu %u, node %u", cpu_vsys
,
117 int main(int argc
, char **argv
)
119 fill_function_pointers();
123 return nerrs
? 1 : 0;