vdso_test_getcpu.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vdso_test_getcpu.c: Sample code to test parse_vdso.c and vDSO getcpu()
  4. *
  5. * Copyright (c) 2020 Arm Ltd
  6. */
  7. #include <stdint.h>
  8. #include <elf.h>
  9. #include <stdio.h>
  10. #include <sys/auxv.h>
  11. #include <sys/time.h>
  12. #include "../kselftest.h"
  13. #include "parse_vdso.h"
  14. #include "vdso_config.h"
  15. #include "vdso_call.h"
  16. struct getcpu_cache;
  17. typedef long (*getcpu_t)(unsigned int *, unsigned int *,
  18. struct getcpu_cache *);
  19. int main(int argc, char **argv)
  20. {
  21. const char *version = versions[VDSO_VERSION];
  22. const char **name = (const char **)&names[VDSO_NAMES];
  23. unsigned long sysinfo_ehdr;
  24. unsigned int cpu, node;
  25. getcpu_t get_cpu;
  26. long ret;
  27. sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
  28. if (!sysinfo_ehdr) {
  29. printf("AT_SYSINFO_EHDR is not present!\n");
  30. return KSFT_SKIP;
  31. }
  32. vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
  33. get_cpu = (getcpu_t)vdso_sym(version, name[4]);
  34. if (!get_cpu) {
  35. printf("Could not find %s\n", name[4]);
  36. return KSFT_SKIP;
  37. }
  38. ret = VDSO_CALL(get_cpu, 3, &cpu, &node, 0);
  39. if (ret == 0) {
  40. printf("Running on CPU %u node %u\n", cpu, node);
  41. } else {
  42. printf("%s failed\n", name[4]);
  43. return KSFT_FAIL;
  44. }
  45. return 0;
  46. }