diff options
author | Andy Lutomirski <luto@amacapital.net> | 2014-06-12 20:53:09 -0400 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2014-06-12 22:01:16 -0400 |
commit | 6e8f21584a30ba6ce73cfef34f316d5bf3fadaab (patch) | |
tree | f2aa19a9791b48abd9011aa944c0eb1bb2020667 /Documentation/vDSO/vdso_standalone_test_x86.c | |
parent | 4d048b0255e3dd4fb001c5f1f609fb67463d04d6 (diff) |
x86/vdso/doc: Rename vdso_test.c to vdso_standalone_test_x86.c
This thing is hopelessly x86_64-specific: it's an example of how to
access the vDSO without any runtime support at all.
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Link: http://lkml.kernel.org/r/3efc170e0e166e15f0150c9fdb37d52488b9c0a4.1402620737.git.luto@amacapital.net
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Diffstat (limited to 'Documentation/vDSO/vdso_standalone_test_x86.c')
-rw-r--r-- | Documentation/vDSO/vdso_standalone_test_x86.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/Documentation/vDSO/vdso_standalone_test_x86.c b/Documentation/vDSO/vdso_standalone_test_x86.c new file mode 100644 index 000000000000..1523f5d681f3 --- /dev/null +++ b/Documentation/vDSO/vdso_standalone_test_x86.c | |||
@@ -0,0 +1,111 @@ | |||
1 | /* | ||
2 | * vdso_test.c: Sample code to test parse_vdso.c on x86_64 | ||
3 | * Copyright (c) 2011 Andy Lutomirski | ||
4 | * Subject to the GNU General Public License, version 2 | ||
5 | * | ||
6 | * You can amuse yourself by compiling with: | ||
7 | * gcc -std=gnu99 -nostdlib | ||
8 | * -Os -fno-asynchronous-unwind-tables -flto | ||
9 | * vdso_standalone_test_x86.c parse_vdso.c | ||
10 | * to generate a small binary with no dependencies at all. | ||
11 | */ | ||
12 | |||
13 | #include <sys/syscall.h> | ||
14 | #include <sys/time.h> | ||
15 | #include <unistd.h> | ||
16 | #include <stdint.h> | ||
17 | |||
18 | extern void *vdso_sym(const char *version, const char *name); | ||
19 | extern void vdso_init_from_sysinfo_ehdr(uintptr_t base); | ||
20 | extern void vdso_init_from_auxv(void *auxv); | ||
21 | |||
22 | /* We need a libc functions... */ | ||
23 | int strcmp(const char *a, const char *b) | ||
24 | { | ||
25 | /* This implementation is buggy: it never returns -1. */ | ||
26 | while (*a || *b) { | ||
27 | if (*a != *b) | ||
28 | return 1; | ||
29 | if (*a == 0 || *b == 0) | ||
30 | return 1; | ||
31 | a++; | ||
32 | b++; | ||
33 | } | ||
34 | |||
35 | return 0; | ||
36 | } | ||
37 | |||
38 | /* ...and two syscalls. This is x86_64-specific. */ | ||
39 | static inline long linux_write(int fd, const void *data, size_t len) | ||
40 | { | ||
41 | |||
42 | long ret; | ||
43 | asm volatile ("syscall" : "=a" (ret) : "a" (__NR_write), | ||
44 | "D" (fd), "S" (data), "d" (len) : | ||
45 | "cc", "memory", "rcx", | ||
46 | "r8", "r9", "r10", "r11" ); | ||
47 | return ret; | ||
48 | } | ||
49 | |||
50 | static inline void linux_exit(int code) | ||
51 | { | ||
52 | asm volatile ("syscall" : : "a" (__NR_exit), "D" (code)); | ||
53 | } | ||
54 | |||
55 | void to_base10(char *lastdig, uint64_t n) | ||
56 | { | ||
57 | while (n) { | ||
58 | *lastdig = (n % 10) + '0'; | ||
59 | n /= 10; | ||
60 | lastdig--; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | __attribute__((externally_visible)) void c_main(void **stack) | ||
65 | { | ||
66 | /* Parse the stack */ | ||
67 | long argc = (long)*stack; | ||
68 | stack += argc + 2; | ||
69 | |||
70 | /* Now we're pointing at the environment. Skip it. */ | ||
71 | while(*stack) | ||
72 | stack++; | ||
73 | stack++; | ||
74 | |||
75 | /* Now we're pointing at auxv. Initialize the vDSO parser. */ | ||
76 | vdso_init_from_auxv((void *)stack); | ||
77 | |||
78 | /* Find gettimeofday. */ | ||
79 | typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz); | ||
80 | gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday"); | ||
81 | |||
82 | if (!gtod) | ||
83 | linux_exit(1); | ||
84 | |||
85 | struct timeval tv; | ||
86 | long ret = gtod(&tv, 0); | ||
87 | |||
88 | if (ret == 0) { | ||
89 | char buf[] = "The time is .000000\n"; | ||
90 | to_base10(buf + 31, tv.tv_sec); | ||
91 | to_base10(buf + 38, tv.tv_usec); | ||
92 | linux_write(1, buf, sizeof(buf) - 1); | ||
93 | } else { | ||
94 | linux_exit(ret); | ||
95 | } | ||
96 | |||
97 | linux_exit(0); | ||
98 | } | ||
99 | |||
100 | /* | ||
101 | * This is the real entry point. It passes the initial stack into | ||
102 | * the C entry point. | ||
103 | */ | ||
104 | asm ( | ||
105 | ".text\n" | ||
106 | ".global _start\n" | ||
107 | ".type _start,@function\n" | ||
108 | "_start:\n\t" | ||
109 | "mov %rsp,%rdi\n\t" | ||
110 | "jmp c_main" | ||
111 | ); | ||