vdso_standalone_test_x86.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vdso_test.c: Sample code to test parse_vdso.c on x86
  4. * Copyright (c) 2011-2014 Andy Lutomirski
  5. *
  6. * You can amuse yourself by compiling with:
  7. * gcc -std=gnu99 -nostdlib
  8. * -Os -fno-asynchronous-unwind-tables -flto -lgcc_s
  9. * vdso_standalone_test_x86.c parse_vdso.c
  10. * to generate a small binary. On x86_64, you can omit -lgcc_s
  11. * if you want the binary to be completely standalone.
  12. */
  13. #include <sys/syscall.h>
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #include <stdint.h>
  17. #include "parse_vdso.h"
  18. /* We need some libc functions... */
  19. int strcmp(const char *a, const char *b)
  20. {
  21. /* This implementation is buggy: it never returns -1. */
  22. while (*a || *b) {
  23. if (*a != *b)
  24. return 1;
  25. if (*a == 0 || *b == 0)
  26. return 1;
  27. a++;
  28. b++;
  29. }
  30. return 0;
  31. }
  32. /*
  33. * The clang build needs this, although gcc does not.
  34. * Stolen from lib/string.c.
  35. */
  36. void *memcpy(void *dest, const void *src, size_t count)
  37. {
  38. char *tmp = dest;
  39. const char *s = src;
  40. while (count--)
  41. *tmp++ = *s++;
  42. return dest;
  43. }
  44. /* ...and two syscalls. This is x86-specific. */
  45. static inline long x86_syscall3(long nr, long a0, long a1, long a2)
  46. {
  47. long ret;
  48. #ifdef __x86_64__
  49. asm volatile ("syscall" : "=a" (ret) : "a" (nr),
  50. "D" (a0), "S" (a1), "d" (a2) :
  51. "cc", "memory", "rcx",
  52. "r8", "r9", "r10", "r11" );
  53. #else
  54. asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
  55. "b" (a0), "c" (a1), "d" (a2) :
  56. "cc", "memory" );
  57. #endif
  58. return ret;
  59. }
  60. static inline long linux_write(int fd, const void *data, size_t len)
  61. {
  62. return x86_syscall3(__NR_write, fd, (long)data, (long)len);
  63. }
  64. static inline void linux_exit(int code)
  65. {
  66. x86_syscall3(__NR_exit, code, 0, 0);
  67. }
  68. void to_base10(char *lastdig, time_t n)
  69. {
  70. while (n) {
  71. *lastdig = (n % 10) + '0';
  72. n /= 10;
  73. lastdig--;
  74. }
  75. }
  76. void c_main(void **stack)
  77. {
  78. /* Parse the stack */
  79. long argc = (long)*stack;
  80. stack += argc + 2;
  81. /* Now we're pointing at the environment. Skip it. */
  82. while(*stack)
  83. stack++;
  84. stack++;
  85. /* Now we're pointing at auxv. Initialize the vDSO parser. */
  86. vdso_init_from_auxv((void *)stack);
  87. /* Find gettimeofday. */
  88. typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
  89. gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
  90. if (!gtod)
  91. linux_exit(1);
  92. struct timeval tv;
  93. long ret = gtod(&tv, 0);
  94. if (ret == 0) {
  95. char buf[] = "The time is .000000\n";
  96. to_base10(buf + 31, tv.tv_sec);
  97. to_base10(buf + 38, tv.tv_usec);
  98. linux_write(1, buf, sizeof(buf) - 1);
  99. } else {
  100. linux_exit(ret);
  101. }
  102. linux_exit(0);
  103. }
  104. /*
  105. * This is the real entry point. It passes the initial stack into
  106. * the C entry point.
  107. */
  108. asm (
  109. ".text\n"
  110. ".global _start\n"
  111. ".type _start,@function\n"
  112. "_start:\n\t"
  113. #ifdef __x86_64__
  114. "mov %rsp,%rdi\n\t"
  115. "and $-16,%rsp\n\t"
  116. "sub $8,%rsp\n\t"
  117. "jmp c_main"
  118. #else
  119. "push %esp\n\t"
  120. "call c_main\n\t"
  121. "int $3"
  122. #endif
  123. );