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
29 /* max length of lines in /proc/self/maps - anything longer is skipped here */
30 #define MAPS_LINE_LEN 128
34 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
39 static void *vsyscall_getcpu(void)
43 char line[MAPS_LINE_LEN];
46 maps = fopen("/proc/self/maps", "r");
47 if (!maps) /* might still be present, but ignore it here, as we test vDSO not vsyscall */
50 while (fgets(line, MAPS_LINE_LEN, maps)) {
53 char name[MAPS_LINE_LEN];
55 /* sscanf() is safe here as strlen(name) >= strlen(line) */
56 if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
57 &start, &end, &r, &x, name) != 5)
60 if (strcmp(name, "[vsyscall]"))
63 /* assume entries are OK, as we test vDSO here not vsyscall */
71 printf("Warning: failed to find vsyscall getcpu\n");
74 return (void *) (0xffffffffff600800);
81 static void fill_function_pointers()
83 void *vdso = dlopen("linux-vdso.so.1",
84 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
86 vdso = dlopen("linux-gate.so.1",
87 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
89 printf("[WARN]\tfailed to find vDSO\n");
93 vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
95 printf("Warning: failed to find getcpu in vDSO\n");
97 vgetcpu = (getcpu_t) vsyscall_getcpu();
100 static long sys_getcpu(unsigned * cpu, unsigned * node,
103 return syscall(__NR_getcpu, cpu, node, cache);
106 static void test_getcpu(void)
108 printf("[RUN]\tTesting getcpu...\n");
110 for (int cpu = 0; ; cpu++) {
113 CPU_SET(cpu, &cpuset);
114 if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
117 unsigned cpu_sys, cpu_vdso, cpu_vsys,
118 node_sys, node_vdso, node_vsys;
119 long ret_sys, ret_vdso = 1, ret_vsys = 1;
122 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
124 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
126 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
136 if (!ret_sys && (cpu_sys != cpu || node_sys != node))
138 if (!ret_vdso && (cpu_vdso != cpu || node_vdso != node))
140 if (!ret_vsys && (cpu_vsys != cpu || node_vsys != node))
143 printf("[%s]\tCPU %u:", ok ? "OK" : "FAIL", cpu);
145 printf(" syscall: cpu %u, node %u", cpu_sys, node_sys);
147 printf(" vdso: cpu %u, node %u", cpu_vdso, node_vdso);
149 printf(" vsyscall: cpu %u, node %u", cpu_vsys,
158 int main(int argc, char **argv)
160 fill_function_pointers();
164 return nerrs ? 1 : 0;