linux/tools/testing/selftests/x86/test_vdso.c
<<
>>
Prefs
   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
   7#define _GNU_SOURCE
   8
   9#include <stdio.h>
  10#include <sys/time.h>
  11#include <time.h>
  12#include <stdlib.h>
  13#include <unistd.h>
  14#include <sys/syscall.h>
  15#include <dlfcn.h>
  16#include <string.h>
  17#include <errno.h>
  18#include <sched.h>
  19#include <stdbool.h>
  20
  21#ifndef SYS_getcpu
  22# ifdef __x86_64__
  23#  define SYS_getcpu 309
  24# else
  25#  define SYS_getcpu 318
  26# endif
  27#endif
  28
  29/* max length of lines in /proc/self/maps - anything longer is skipped here */
  30#define MAPS_LINE_LEN 128
  31
  32int nerrs = 0;
  33
  34typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
  35
  36getcpu_t vgetcpu;
  37getcpu_t vdso_getcpu;
  38
  39static void *vsyscall_getcpu(void)
  40{
  41#ifdef __x86_64__
  42        FILE *maps;
  43        char line[MAPS_LINE_LEN];
  44        bool found = false;
  45
  46        maps = fopen("/proc/self/maps", "r");
  47        if (!maps) /* might still be present, but ignore it here, as we test vDSO not vsyscall */
  48                return NULL;
  49
  50        while (fgets(line, MAPS_LINE_LEN, maps)) {
  51                char r, x;
  52                void *start, *end;
  53                char name[MAPS_LINE_LEN];
  54
  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)
  58                        continue;
  59
  60                if (strcmp(name, "[vsyscall]"))
  61                        continue;
  62
  63                /* assume entries are OK, as we test vDSO here not vsyscall */
  64                found = true;
  65                break;
  66        }
  67
  68        fclose(maps);
  69
  70        if (!found) {
  71                printf("Warning: failed to find vsyscall getcpu\n");
  72                return NULL;
  73        }
  74        return (void *) (0xffffffffff600800);
  75#else
  76        return NULL;
  77#endif
  78}
  79
  80
  81static void fill_function_pointers()
  82{
  83        void *vdso = dlopen("linux-vdso.so.1",
  84                            RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
  85        if (!vdso)
  86                vdso = dlopen("linux-gate.so.1",
  87                              RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
  88        if (!vdso) {
  89                printf("[WARN]\tfailed to find vDSO\n");
  90                return;
  91        }
  92
  93        vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
  94        if (!vdso_getcpu)
  95                printf("Warning: failed to find getcpu in vDSO\n");
  96
  97        vgetcpu = (getcpu_t) vsyscall_getcpu();
  98}
  99
 100static long sys_getcpu(unsigned * cpu, unsigned * node,
 101                       void* cache)
 102{
 103        return syscall(__NR_getcpu, cpu, node, cache);
 104}
 105
 106static void test_getcpu(void)
 107{
 108        printf("[RUN]\tTesting getcpu...\n");
 109
 110        for (int cpu = 0; ; cpu++) {
 111                cpu_set_t cpuset;
 112                CPU_ZERO(&cpuset);
 113                CPU_SET(cpu, &cpuset);
 114                if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
 115                        return;
 116
 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;
 120                unsigned node;
 121
 122                ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
 123                if (vdso_getcpu)
 124                        ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
 125                if (vgetcpu)
 126                        ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
 127
 128                if (!ret_sys)
 129                        node = node_sys;
 130                else if (!ret_vdso)
 131                        node = node_vdso;
 132                else if (!ret_vsys)
 133                        node = node_vsys;
 134
 135                bool ok = true;
 136                if (!ret_sys && (cpu_sys != cpu || node_sys != node))
 137                        ok = false;
 138                if (!ret_vdso && (cpu_vdso != cpu || node_vdso != node))
 139                        ok = false;
 140                if (!ret_vsys && (cpu_vsys != cpu || node_vsys != node))
 141                        ok = false;
 142
 143                printf("[%s]\tCPU %u:", ok ? "OK" : "FAIL", cpu);
 144                if (!ret_sys)
 145                        printf(" syscall: cpu %u, node %u", cpu_sys, node_sys);
 146                if (!ret_vdso)
 147                        printf(" vdso: cpu %u, node %u", cpu_vdso, node_vdso);
 148                if (!ret_vsys)
 149                        printf(" vsyscall: cpu %u, node %u", cpu_vsys,
 150                               node_vsys);
 151                printf("\n");
 152
 153                if (!ok)
 154                        nerrs++;
 155        }
 156}
 157
 158int main(int argc, char **argv)
 159{
 160        fill_function_pointers();
 161
 162        test_getcpu();
 163
 164        return nerrs ? 1 : 0;
 165}
 166