test_vdso.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ldt_gdt.c - Test cases for LDT and GDT access
  4. * Copyright (c) 2011-2015 Andrew Lutomirski
  5. */
  6. #define _GNU_SOURCE
  7. #include <stdio.h>
  8. #include <sys/time.h>
  9. #include <time.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <sys/syscall.h>
  13. #include <dlfcn.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <sched.h>
  17. #include <stdbool.h>
  18. #ifndef SYS_getcpu
  19. # ifdef __x86_64__
  20. # define SYS_getcpu 309
  21. # else
  22. # define SYS_getcpu 318
  23. # endif
  24. #endif
  25. /* max length of lines in /proc/self/maps - anything longer is skipped here */
  26. #define MAPS_LINE_LEN 128
  27. int nerrs = 0;
  28. typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
  29. getcpu_t vgetcpu;
  30. getcpu_t vdso_getcpu;
  31. static void *vsyscall_getcpu(void)
  32. {
  33. #ifdef __x86_64__
  34. FILE *maps;
  35. char line[MAPS_LINE_LEN];
  36. bool found = false;
  37. maps = fopen("/proc/self/maps", "r");
  38. if (!maps) /* might still be present, but ignore it here, as we test vDSO not vsyscall */
  39. return NULL;
  40. while (fgets(line, MAPS_LINE_LEN, maps)) {
  41. char r, x;
  42. void *start, *end;
  43. char name[MAPS_LINE_LEN];
  44. /* sscanf() is safe here as strlen(name) >= strlen(line) */
  45. if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
  46. &start, &end, &r, &x, name) != 5)
  47. continue;
  48. if (strcmp(name, "[vsyscall]"))
  49. continue;
  50. /* assume entries are OK, as we test vDSO here not vsyscall */
  51. found = true;
  52. break;
  53. }
  54. fclose(maps);
  55. if (!found) {
  56. printf("Warning: failed to find vsyscall getcpu\n");
  57. return NULL;
  58. }
  59. return (void *) (0xffffffffff600800);
  60. #else
  61. return NULL;
  62. #endif
  63. }
  64. static void fill_function_pointers()
  65. {
  66. void *vdso = dlopen("linux-vdso.so.1",
  67. RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
  68. if (!vdso)
  69. vdso = dlopen("linux-gate.so.1",
  70. RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
  71. if (!vdso) {
  72. printf("[WARN]\tfailed to find vDSO\n");
  73. return;
  74. }
  75. vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
  76. if (!vdso_getcpu)
  77. printf("Warning: failed to find getcpu in vDSO\n");
  78. vgetcpu = (getcpu_t) vsyscall_getcpu();
  79. }
  80. static long sys_getcpu(unsigned * cpu, unsigned * node,
  81. void* cache)
  82. {
  83. return syscall(__NR_getcpu, cpu, node, cache);
  84. }
  85. static void test_getcpu(void)
  86. {
  87. printf("[RUN]\tTesting getcpu...\n");
  88. for (int cpu = 0; ; cpu++) {
  89. cpu_set_t cpuset;
  90. CPU_ZERO(&cpuset);
  91. CPU_SET(cpu, &cpuset);
  92. if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
  93. return;
  94. unsigned cpu_sys, cpu_vdso, cpu_vsys,
  95. node_sys, node_vdso, node_vsys;
  96. long ret_sys, ret_vdso = 1, ret_vsys = 1;
  97. unsigned node;
  98. ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
  99. if (vdso_getcpu)
  100. ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
  101. if (vgetcpu)
  102. ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
  103. if (!ret_sys)
  104. node = node_sys;
  105. else if (!ret_vdso)
  106. node = node_vdso;
  107. else if (!ret_vsys)
  108. node = node_vsys;
  109. bool ok = true;
  110. if (!ret_sys && (cpu_sys != cpu || node_sys != node))
  111. ok = false;
  112. if (!ret_vdso && (cpu_vdso != cpu || node_vdso != node))
  113. ok = false;
  114. if (!ret_vsys && (cpu_vsys != cpu || node_vsys != node))
  115. ok = false;
  116. printf("[%s]\tCPU %u:", ok ? "OK" : "FAIL", cpu);
  117. if (!ret_sys)
  118. printf(" syscall: cpu %u, node %u", cpu_sys, node_sys);
  119. if (!ret_vdso)
  120. printf(" vdso: cpu %u, node %u", cpu_vdso, node_vdso);
  121. if (!ret_vsys)
  122. printf(" vsyscall: cpu %u, node %u", cpu_vsys,
  123. node_vsys);
  124. printf("\n");
  125. if (!ok)
  126. nerrs++;
  127. }
  128. }
  129. int main(int argc, char **argv)
  130. {
  131. fill_function_pointers();
  132. test_getcpu();
  133. return nerrs ? 1 : 0;
  134. }