Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * vdso_test.c: Sample code to test parse_vdso.c on x86
0004  * Copyright (c) 2011-2014 Andy Lutomirski
0005  *
0006  * You can amuse yourself by compiling with:
0007  * gcc -std=gnu99 -nostdlib
0008  *     -Os -fno-asynchronous-unwind-tables -flto -lgcc_s
0009  *      vdso_standalone_test_x86.c parse_vdso.c
0010  * to generate a small binary.  On x86_64, you can omit -lgcc_s
0011  * if you want the binary to be completely standalone.
0012  */
0013 
0014 #include <sys/syscall.h>
0015 #include <sys/time.h>
0016 #include <unistd.h>
0017 #include <stdint.h>
0018 
0019 #include "parse_vdso.h"
0020 
0021 /* We need a libc functions... */
0022 int strcmp(const char *a, const char *b)
0023 {
0024     /* This implementation is buggy: it never returns -1. */
0025     while (*a || *b) {
0026         if (*a != *b)
0027             return 1;
0028         if (*a == 0 || *b == 0)
0029             return 1;
0030         a++;
0031         b++;
0032     }
0033 
0034     return 0;
0035 }
0036 
0037 /* ...and two syscalls.  This is x86-specific. */
0038 static inline long x86_syscall3(long nr, long a0, long a1, long a2)
0039 {
0040     long ret;
0041 #ifdef __x86_64__
0042     asm volatile ("syscall" : "=a" (ret) : "a" (nr),
0043               "D" (a0), "S" (a1), "d" (a2) :
0044               "cc", "memory", "rcx",
0045               "r8", "r9", "r10", "r11" );
0046 #else
0047     asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
0048               "b" (a0), "c" (a1), "d" (a2) :
0049               "cc", "memory" );
0050 #endif
0051     return ret;
0052 }
0053 
0054 static inline long linux_write(int fd, const void *data, size_t len)
0055 {
0056     return x86_syscall3(__NR_write, fd, (long)data, (long)len);
0057 }
0058 
0059 static inline void linux_exit(int code)
0060 {
0061     x86_syscall3(__NR_exit, code, 0, 0);
0062 }
0063 
0064 void to_base10(char *lastdig, time_t n)
0065 {
0066     while (n) {
0067         *lastdig = (n % 10) + '0';
0068         n /= 10;
0069         lastdig--;
0070     }
0071 }
0072 
0073 __attribute__((externally_visible)) void c_main(void **stack)
0074 {
0075     /* Parse the stack */
0076     long argc = (long)*stack;
0077     stack += argc + 2;
0078 
0079     /* Now we're pointing at the environment.  Skip it. */
0080     while(*stack)
0081         stack++;
0082     stack++;
0083 
0084     /* Now we're pointing at auxv.  Initialize the vDSO parser. */
0085     vdso_init_from_auxv((void *)stack);
0086 
0087     /* Find gettimeofday. */
0088     typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
0089     gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
0090 
0091     if (!gtod)
0092         linux_exit(1);
0093 
0094     struct timeval tv;
0095     long ret = gtod(&tv, 0);
0096 
0097     if (ret == 0) {
0098         char buf[] = "The time is                     .000000\n";
0099         to_base10(buf + 31, tv.tv_sec);
0100         to_base10(buf + 38, tv.tv_usec);
0101         linux_write(1, buf, sizeof(buf) - 1);
0102     } else {
0103         linux_exit(ret);
0104     }
0105 
0106     linux_exit(0);
0107 }
0108 
0109 /*
0110  * This is the real entry point.  It passes the initial stack into
0111  * the C entry point.
0112  */
0113 asm (
0114     ".text\n"
0115     ".global _start\n"
0116     ".type _start,@function\n"
0117     "_start:\n\t"
0118 #ifdef __x86_64__
0119     "mov %rsp,%rdi\n\t"
0120     "jmp c_main"
0121 #else
0122     "push %esp\n\t"
0123     "call c_main\n\t"
0124     "int $3"
0125 #endif
0126     );