Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / x86 / unwind_vdso.c
blob00a26a82fa98b1eec4f82046b9a88517ceb8c7e4
1 /*
2 * unwind_vdso.c - tests unwind info for AT_SYSINFO in the vDSO
3 * Copyright (c) 2014-2015 Andrew Lutomirski
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * This tests __kernel_vsyscall's unwind info.
17 #define _GNU_SOURCE
19 #include <features.h>
20 #include <stdio.h>
22 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16
24 int main()
26 /* We need getauxval(). */
27 printf("[SKIP]\tGLIBC before 2.16 cannot compile this test\n");
28 return 0;
31 #else
33 #include <sys/time.h>
34 #include <stdlib.h>
35 #include <syscall.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <inttypes.h>
39 #include <sys/mman.h>
40 #include <signal.h>
41 #include <sys/ucontext.h>
42 #include <err.h>
43 #include <stddef.h>
44 #include <stdbool.h>
45 #include <sys/ptrace.h>
46 #include <sys/user.h>
47 #include <sys/ucontext.h>
48 #include <link.h>
49 #include <sys/auxv.h>
50 #include <dlfcn.h>
51 #include <unwind.h>
53 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
54 int flags)
56 struct sigaction sa;
57 memset(&sa, 0, sizeof(sa));
58 sa.sa_sigaction = handler;
59 sa.sa_flags = SA_SIGINFO | flags;
60 sigemptyset(&sa.sa_mask);
61 if (sigaction(sig, &sa, 0))
62 err(1, "sigaction");
65 #ifdef __x86_64__
66 # define WIDTH "q"
67 #else
68 # define WIDTH "l"
69 #endif
71 static unsigned long get_eflags(void)
73 unsigned long eflags;
74 asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
75 return eflags;
78 static void set_eflags(unsigned long eflags)
80 asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
81 : : "rm" (eflags) : "flags");
84 #define X86_EFLAGS_TF (1UL << 8)
86 static volatile sig_atomic_t nerrs;
87 static unsigned long sysinfo;
88 static bool got_sysinfo = false;
89 static unsigned long return_address;
91 struct unwind_state {
92 unsigned long ip; /* trap source */
93 int depth; /* -1 until we hit the trap source */
96 _Unwind_Reason_Code trace_fn(struct _Unwind_Context * ctx, void *opaque)
98 struct unwind_state *state = opaque;
99 unsigned long ip = _Unwind_GetIP(ctx);
101 if (state->depth == -1) {
102 if (ip == state->ip)
103 state->depth = 0;
104 else
105 return _URC_NO_REASON; /* Not there yet */
107 printf("\t 0x%lx\n", ip);
109 if (ip == return_address) {
110 /* Here we are. */
111 unsigned long eax = _Unwind_GetGR(ctx, 0);
112 unsigned long ecx = _Unwind_GetGR(ctx, 1);
113 unsigned long edx = _Unwind_GetGR(ctx, 2);
114 unsigned long ebx = _Unwind_GetGR(ctx, 3);
115 unsigned long ebp = _Unwind_GetGR(ctx, 5);
116 unsigned long esi = _Unwind_GetGR(ctx, 6);
117 unsigned long edi = _Unwind_GetGR(ctx, 7);
118 bool ok = (eax == SYS_getpid || eax == getpid()) &&
119 ebx == 1 && ecx == 2 && edx == 3 &&
120 esi == 4 && edi == 5 && ebp == 6;
122 if (!ok)
123 nerrs++;
124 printf("[%s]\t NR = %ld, args = %ld, %ld, %ld, %ld, %ld, %ld\n",
125 (ok ? "OK" : "FAIL"),
126 eax, ebx, ecx, edx, esi, edi, ebp);
128 return _URC_NORMAL_STOP;
129 } else {
130 state->depth++;
131 return _URC_NO_REASON;
135 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
137 ucontext_t *ctx = (ucontext_t *)ctx_void;
138 struct unwind_state state;
139 unsigned long ip = ctx->uc_mcontext.gregs[REG_EIP];
141 if (!got_sysinfo && ip == sysinfo) {
142 got_sysinfo = true;
144 /* Find the return address. */
145 return_address = *(unsigned long *)(unsigned long)ctx->uc_mcontext.gregs[REG_ESP];
147 printf("\tIn vsyscall at 0x%lx, returning to 0x%lx\n",
148 ip, return_address);
151 if (!got_sysinfo)
152 return; /* Not there yet */
154 if (ip == return_address) {
155 ctx->uc_mcontext.gregs[REG_EFL] &= ~X86_EFLAGS_TF;
156 printf("\tVsyscall is done\n");
157 return;
160 printf("\tSIGTRAP at 0x%lx\n", ip);
162 state.ip = ip;
163 state.depth = -1;
164 _Unwind_Backtrace(trace_fn, &state);
167 int main()
169 sysinfo = getauxval(AT_SYSINFO);
170 printf("\tAT_SYSINFO is 0x%lx\n", sysinfo);
172 Dl_info info;
173 if (!dladdr((void *)sysinfo, &info)) {
174 printf("[WARN]\tdladdr failed on AT_SYSINFO\n");
175 } else {
176 printf("[OK]\tAT_SYSINFO maps to %s, loaded at 0x%p\n",
177 info.dli_fname, info.dli_fbase);
180 sethandler(SIGTRAP, sigtrap, 0);
182 syscall(SYS_getpid); /* Force symbol binding without TF set. */
183 printf("[RUN]\tSet TF and check a fast syscall\n");
184 set_eflags(get_eflags() | X86_EFLAGS_TF);
185 syscall(SYS_getpid, 1, 2, 3, 4, 5, 6);
186 if (!got_sysinfo) {
187 set_eflags(get_eflags() & ~X86_EFLAGS_TF);
190 * The most likely cause of this is that you're on Debian or
191 * a Debian-based distro, you're missing libc6-i686, and you're
192 * affected by libc/19006 (https://sourceware.org/PR19006).
194 printf("[WARN]\tsyscall(2) didn't enter AT_SYSINFO\n");
197 if (get_eflags() & X86_EFLAGS_TF) {
198 printf("[FAIL]\tTF is still set\n");
199 nerrs++;
202 if (nerrs) {
203 printf("[FAIL]\tThere were errors\n");
204 return 1;
205 } else {
206 printf("[OK]\tAll is well\n");
207 return 0;
211 #endif /* New enough libc */