vdso_standalone_test_x86 provides its own ASM syscall wrappers and
_start() implementation. The in-tree nolibc library already provides
this functionality for multiple architectures. By making use of nolibc,
the standalone testcase can be built from the exact same codebase as the
non-standalone version.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
tools/testing/selftests/vDSO/Makefile | 8 +-
.../selftests/vDSO/vdso_standalone_test_x86.c | 168 +--------------------
2 files changed, 7 insertions(+), 169 deletions(-)
diff --git a/tools/testing/selftests/vDSO/Makefile b/tools/testing/selftests/vDSO/Makefile
index bc8ca186fb877dc11740c37f1e07e45e84c2ae92..12a0614b9fd4983deffe5d6a7cfa06ba8d92a516 100644
--- a/tools/testing/selftests/vDSO/Makefile
+++ b/tools/testing/selftests/vDSO/Makefile
@@ -22,13 +22,17 @@ include ../lib.mk
CFLAGS += $(TOOLS_INCLUDES)
+CFLAGS_NOLIBC := -nostdlib -nostdinc -ffreestanding -fno-asynchronous-unwind-tables \
+ -fno-stack-protector -include $(top_srcdir)/tools/include/nolibc/nolibc.h \
+ -I$(top_srcdir)/tools/include/nolibc/ $(KHDR_INCLUDES)
+
$(OUTPUT)/vdso_test_gettimeofday: parse_vdso.c vdso_test_gettimeofday.c
$(OUTPUT)/vdso_test_getcpu: parse_vdso.c vdso_test_getcpu.c
$(OUTPUT)/vdso_test_abi: parse_vdso.c vdso_test_abi.c
$(OUTPUT)/vdso_test_clock_getres: vdso_test_clock_getres.c
-$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c
-$(OUTPUT)/vdso_standalone_test_x86: CFLAGS +=-nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector
+$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c | headers
+$(OUTPUT)/vdso_standalone_test_x86: CFLAGS:=$(CFLAGS_NOLIBC) $(CFLAGS)
$(OUTPUT)/vdso_test_correctness: vdso_test_correctness.c
$(OUTPUT)/vdso_test_correctness: LDFLAGS += -ldl
diff --git a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
deleted file mode 100644
index 500608f89c66b5747e3d845ebc54e4c3a35b6ccd..0000000000000000000000000000000000000000
--- a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
+++ /dev/null
@@ -1,167 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * vdso_test.c: Sample code to test parse_vdso.c on x86
- * Copyright (c) 2011-2014 Andy Lutomirski
- *
- * You can amuse yourself by compiling with:
- * gcc -std=gnu99 -nostdlib
- * -Os -fno-asynchronous-unwind-tables -flto -lgcc_s
- * vdso_standalone_test_x86.c parse_vdso.c
- * to generate a small binary. On x86_64, you can omit -lgcc_s
- * if you want the binary to be completely standalone.
- */
-
-#include <sys/syscall.h>
-#include <sys/time.h>
-#include <unistd.h>
-#include <stdint.h>
-#include <linux/auxvec.h>
-
-#include "parse_vdso.h"
-
-/* We need some libc functions... */
-int strcmp(const char *a, const char *b)
-{
- /* This implementation is buggy: it never returns -1. */
- while (*a || *b) {
- if (*a != *b)
- return 1;
- if (*a == 0 || *b == 0)
- return 1;
- a++;
- b++;
- }
-
- return 0;
-}
-
-/*
- * The clang build needs this, although gcc does not.
- * Stolen from lib/string.c.
- */
-void *memcpy(void *dest, const void *src, size_t count)
-{
- char *tmp = dest;
- const char *s = src;
-
- while (count--)
- *tmp++ = *s++;
- return dest;
-}
-
-/* ...and two syscalls. This is x86-specific. */
-static inline long x86_syscall3(long nr, long a0, long a1, long a2)
-{
- long ret;
-#ifdef __x86_64__
- asm volatile ("syscall" : "=a" (ret) : "a" (nr),
- "D" (a0), "S" (a1), "d" (a2) :
- "cc", "memory", "rcx",
- "r8", "r9", "r10", "r11" );
-#else
- asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
- "b" (a0), "c" (a1), "d" (a2) :
- "cc", "memory" );
-#endif
- return ret;
-}
-
-static inline long linux_write(int fd, const void *data, size_t len)
-{
- return x86_syscall3(__NR_write, fd, (long)data, (long)len);
-}
-
-static inline void linux_exit(int code)
-{
- x86_syscall3(__NR_exit, code, 0, 0);
-}
-
-void to_base10(char *lastdig, time_t n)
-{
- while (n) {
- *lastdig = (n % 10) + '0';
- n /= 10;
- lastdig--;
- }
-}
-
-unsigned long getauxval(const unsigned long *auxv, unsigned long type)
-{
- unsigned long ret;
-
- if (!auxv)
- return 0;
-
- while (1) {
- if (!auxv[0] && !auxv[1]) {
- ret = 0;
- break;
- }
-
- if (auxv[0] == type) {
- ret = auxv[1];
- break;
- }
-
- auxv += 2;
- }
-
- return ret;
-}
-
-void c_main(void **stack)
-{
- /* Parse the stack */
- long argc = (long)*stack;
- stack += argc + 2;
-
- /* Now we're pointing at the environment. Skip it. */
- while(*stack)
- stack++;
- stack++;
-
- /* Now we're pointing at auxv. Initialize the vDSO parser. */
- vdso_init_from_sysinfo_ehdr(getauxval((unsigned long *)stack, AT_SYSINFO_EHDR));
-
- /* Find gettimeofday. */
- typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
- gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
-
- if (!gtod)
- linux_exit(1);
-
- struct timeval tv;
- long ret = gtod(&tv, 0);
-
- if (ret == 0) {
- char buf[] = "The time is .000000\n";
- to_base10(buf + 31, tv.tv_sec);
- to_base10(buf + 38, tv.tv_usec);
- linux_write(1, buf, sizeof(buf) - 1);
- } else {
- linux_exit(ret);
- }
-
- linux_exit(0);
-}
-
-/*
- * This is the real entry point. It passes the initial stack into
- * the C entry point.
- */
-asm (
- ".text\n"
- ".global _start\n"
- ".type _start,@function\n"
- "_start:\n\t"
-#ifdef __x86_64__
- "mov %rsp,%rdi\n\t"
- "and $-16,%rsp\n\t"
- "sub $8,%rsp\n\t"
- "jmp c_main"
-#else
- "push %esp\n\t"
- "call c_main\n\t"
- "int $3"
-#endif
- );
diff --git a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
new file mode 120000
index 0000000000000000000000000000000000000000..4d3d96f1e440c965474681a6f35375a60b3921be
--- /dev/null
+++ b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
@@ -0,0 +1 @@
+vdso_test_gettimeofday.c
\ No newline at end of file
--
2.48.1
On Wed, Feb 26, 2025 at 12:44:55PM +0100, Thomas Weißschuh wrote: > -$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c > -$(OUTPUT)/vdso_standalone_test_x86: CFLAGS +=-nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector > +$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c | headers > +$(OUTPUT)/vdso_standalone_test_x86: CFLAGS:=$(CFLAGS_NOLIBC) $(CFLAGS) This addition of "| headers" breaks O=build builds. ie this: $ make O=build-x86 allnoconfig -s -j 14 $ make O=build-x86 -s -j 14 $ make O=build-x86 kselftest-all -s -j 16 &> /dev/null || true $ make O=build-x86 -s -j 14 Fails with: *** *** The source tree is not clean, please run 'make mrproper' *** in /home/jgg/oss/wip/kselftests_dirty *** Because the build now spews stuff outside the build directory that it should not do.. Bisection points to this patch and removing the "| headers" makes it stop doing that.. Any idea how to fix it? Thanks, Jason
Hi Jason,
On 2025-09-17 12:32:09-0300, Jason Gunthorpe wrote:
> On Wed, Feb 26, 2025 at 12:44:55PM +0100, Thomas Weißschuh wrote:
>
> > -$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c
> > -$(OUTPUT)/vdso_standalone_test_x86: CFLAGS +=-nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector
> > +$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c | headers
> > +$(OUTPUT)/vdso_standalone_test_x86: CFLAGS:=$(CFLAGS_NOLIBC) $(CFLAGS)
>
> This addition of "| headers" breaks O=build builds. ie this:
>
> $ make O=build-x86 allnoconfig -s -j 14
> $ make O=build-x86 -s -j 14
> $ make O=build-x86 kselftest-all -s -j 16 &> /dev/null || true
> $ make O=build-x86 -s -j 14
>
> Fails with:
>
> ***
> *** The source tree is not clean, please run 'make mrproper'
> *** in /home/jgg/oss/wip/kselftests_dirty
> ***
>
> Because the build now spews stuff outside the build directory that it
> should not do.. Bisection points to this patch and removing the "|
> headers" makes it stop doing that..
Sorry for the breakage and thanks for the report.
> Any idea how to fix it?
Care to try this:
diff --git a/tools/testing/selftests/lib.mk b/tools/testing/selftests/lib.mk
index 530390033929..a448fae57831 100644
--- a/tools/testing/selftests/lib.mk
+++ b/tools/testing/selftests/lib.mk
@@ -228,7 +228,10 @@ $(OUTPUT)/%:%.S
$(LINK.S) $^ $(LDLIBS) -o $@
endif
+# Extract the expected header directory
+khdr_output := $(patsubst %/usr/include,%,$(filter %/usr/include,$(KHDR_INCLUDES)))
+
headers:
- $(Q)$(MAKE) -C $(top_srcdir) headers
+ $(Q)$(MAKE) -f $(top_srcdir)/Makefile -C $(khdr_output) headers
.PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir headers
Thomas
On Wed, Sep 17, 2025 at 06:24:13PM +0200, Thomas Weißschuh wrote: > Care to try this: > > diff --git a/tools/testing/selftests/lib.mk b/tools/testing/selftests/lib.mk > index 530390033929..a448fae57831 100644 > --- a/tools/testing/selftests/lib.mk > +++ b/tools/testing/selftests/lib.mk > @@ -228,7 +228,10 @@ $(OUTPUT)/%:%.S > $(LINK.S) $^ $(LDLIBS) -o $@ > endif > > +# Extract the expected header directory > +khdr_output := $(patsubst %/usr/include,%,$(filter %/usr/include,$(KHDR_INCLUDES))) > + > headers: > - $(Q)$(MAKE) -C $(top_srcdir) headers > + $(Q)$(MAKE) -f $(top_srcdir)/Makefile -C $(khdr_output) headers > > .PHONY: run_tests all clean install emit_tests gen_mods_dir clean_mods_dir headers Worked for me, thanks! Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Though I notice it still spews stuff out of the build directory: tools/testing/selftests/mm/local_config.h tools/testing/selftests/mm/local_config.mk tools/testing/selftests/net/rds/include.sh tools/testing/selftests/prctl/disable-tsc-ctxt-sw-stress-test tools/testing/selftests/prctl/disable-tsc-on-off-stress-test tools/testing/selftests/prctl/disable-tsc-test tools/testing/selftests/prctl/set-anon-vma-name-test tools/testing/selftests/prctl/set-process-name tools/testing/selftests/uevent/uevent_filtering It is not related to this Jason
On 26/02/2025 11:44, Thomas Weißschuh wrote:
> vdso_standalone_test_x86 provides its own ASM syscall wrappers and
> _start() implementation. The in-tree nolibc library already provides
> this functionality for multiple architectures. By making use of nolibc,
> the standalone testcase can be built from the exact same codebase as the
> non-standalone version.
>
> Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Reviewed-by: Vincenzo Frascino <vincenzo.frascino@arm.com>
> ---
> tools/testing/selftests/vDSO/Makefile | 8 +-
> .../selftests/vDSO/vdso_standalone_test_x86.c | 168 +--------------------
> 2 files changed, 7 insertions(+), 169 deletions(-)
>
> diff --git a/tools/testing/selftests/vDSO/Makefile b/tools/testing/selftests/vDSO/Makefile
> index bc8ca186fb877dc11740c37f1e07e45e84c2ae92..12a0614b9fd4983deffe5d6a7cfa06ba8d92a516 100644
> --- a/tools/testing/selftests/vDSO/Makefile
> +++ b/tools/testing/selftests/vDSO/Makefile
> @@ -22,13 +22,17 @@ include ../lib.mk
>
> CFLAGS += $(TOOLS_INCLUDES)
>
> +CFLAGS_NOLIBC := -nostdlib -nostdinc -ffreestanding -fno-asynchronous-unwind-tables \
> + -fno-stack-protector -include $(top_srcdir)/tools/include/nolibc/nolibc.h \
> + -I$(top_srcdir)/tools/include/nolibc/ $(KHDR_INCLUDES)
> +
> $(OUTPUT)/vdso_test_gettimeofday: parse_vdso.c vdso_test_gettimeofday.c
> $(OUTPUT)/vdso_test_getcpu: parse_vdso.c vdso_test_getcpu.c
> $(OUTPUT)/vdso_test_abi: parse_vdso.c vdso_test_abi.c
> $(OUTPUT)/vdso_test_clock_getres: vdso_test_clock_getres.c
>
> -$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c
> -$(OUTPUT)/vdso_standalone_test_x86: CFLAGS +=-nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector
> +$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c | headers
> +$(OUTPUT)/vdso_standalone_test_x86: CFLAGS:=$(CFLAGS_NOLIBC) $(CFLAGS)
>
> $(OUTPUT)/vdso_test_correctness: vdso_test_correctness.c
> $(OUTPUT)/vdso_test_correctness: LDFLAGS += -ldl
> diff --git a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
> deleted file mode 100644
> index 500608f89c66b5747e3d845ebc54e4c3a35b6ccd..0000000000000000000000000000000000000000
> --- a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
> +++ /dev/null
> @@ -1,167 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0-only
> -/*
> - * vdso_test.c: Sample code to test parse_vdso.c on x86
> - * Copyright (c) 2011-2014 Andy Lutomirski
> - *
> - * You can amuse yourself by compiling with:
> - * gcc -std=gnu99 -nostdlib
> - * -Os -fno-asynchronous-unwind-tables -flto -lgcc_s
> - * vdso_standalone_test_x86.c parse_vdso.c
> - * to generate a small binary. On x86_64, you can omit -lgcc_s
> - * if you want the binary to be completely standalone.
> - */
> -
> -#include <sys/syscall.h>
> -#include <sys/time.h>
> -#include <unistd.h>
> -#include <stdint.h>
> -#include <linux/auxvec.h>
> -
> -#include "parse_vdso.h"
> -
> -/* We need some libc functions... */
> -int strcmp(const char *a, const char *b)
> -{
> - /* This implementation is buggy: it never returns -1. */
> - while (*a || *b) {
> - if (*a != *b)
> - return 1;
> - if (*a == 0 || *b == 0)
> - return 1;
> - a++;
> - b++;
> - }
> -
> - return 0;
> -}
> -
> -/*
> - * The clang build needs this, although gcc does not.
> - * Stolen from lib/string.c.
> - */
> -void *memcpy(void *dest, const void *src, size_t count)
> -{
> - char *tmp = dest;
> - const char *s = src;
> -
> - while (count--)
> - *tmp++ = *s++;
> - return dest;
> -}
> -
> -/* ...and two syscalls. This is x86-specific. */
> -static inline long x86_syscall3(long nr, long a0, long a1, long a2)
> -{
> - long ret;
> -#ifdef __x86_64__
> - asm volatile ("syscall" : "=a" (ret) : "a" (nr),
> - "D" (a0), "S" (a1), "d" (a2) :
> - "cc", "memory", "rcx",
> - "r8", "r9", "r10", "r11" );
> -#else
> - asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
> - "b" (a0), "c" (a1), "d" (a2) :
> - "cc", "memory" );
> -#endif
> - return ret;
> -}
> -
> -static inline long linux_write(int fd, const void *data, size_t len)
> -{
> - return x86_syscall3(__NR_write, fd, (long)data, (long)len);
> -}
> -
> -static inline void linux_exit(int code)
> -{
> - x86_syscall3(__NR_exit, code, 0, 0);
> -}
> -
> -void to_base10(char *lastdig, time_t n)
> -{
> - while (n) {
> - *lastdig = (n % 10) + '0';
> - n /= 10;
> - lastdig--;
> - }
> -}
> -
> -unsigned long getauxval(const unsigned long *auxv, unsigned long type)
> -{
> - unsigned long ret;
> -
> - if (!auxv)
> - return 0;
> -
> - while (1) {
> - if (!auxv[0] && !auxv[1]) {
> - ret = 0;
> - break;
> - }
> -
> - if (auxv[0] == type) {
> - ret = auxv[1];
> - break;
> - }
> -
> - auxv += 2;
> - }
> -
> - return ret;
> -}
> -
> -void c_main(void **stack)
> -{
> - /* Parse the stack */
> - long argc = (long)*stack;
> - stack += argc + 2;
> -
> - /* Now we're pointing at the environment. Skip it. */
> - while(*stack)
> - stack++;
> - stack++;
> -
> - /* Now we're pointing at auxv. Initialize the vDSO parser. */
> - vdso_init_from_sysinfo_ehdr(getauxval((unsigned long *)stack, AT_SYSINFO_EHDR));
> -
> - /* Find gettimeofday. */
> - typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
> - gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
> -
> - if (!gtod)
> - linux_exit(1);
> -
> - struct timeval tv;
> - long ret = gtod(&tv, 0);
> -
> - if (ret == 0) {
> - char buf[] = "The time is .000000\n";
> - to_base10(buf + 31, tv.tv_sec);
> - to_base10(buf + 38, tv.tv_usec);
> - linux_write(1, buf, sizeof(buf) - 1);
> - } else {
> - linux_exit(ret);
> - }
> -
> - linux_exit(0);
> -}
> -
> -/*
> - * This is the real entry point. It passes the initial stack into
> - * the C entry point.
> - */
> -asm (
> - ".text\n"
> - ".global _start\n"
> - ".type _start,@function\n"
> - "_start:\n\t"
> -#ifdef __x86_64__
> - "mov %rsp,%rdi\n\t"
> - "and $-16,%rsp\n\t"
> - "sub $8,%rsp\n\t"
> - "jmp c_main"
> -#else
> - "push %esp\n\t"
> - "call c_main\n\t"
> - "int $3"
> -#endif
> - );
> diff --git a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
> new file mode 120000
> index 0000000000000000000000000000000000000000..4d3d96f1e440c965474681a6f35375a60b3921be
> --- /dev/null
> +++ b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
> @@ -0,0 +1 @@
> +vdso_test_gettimeofday.c
> \ No newline at end of file
>
--
Regards,
Vincenzo
The following commit has been merged into the timers/vdso branch of tip:
Commit-ID: 8770a9183fe18442c7cfbb56bb7e006462775a91
Gitweb: https://git.kernel.org/tip/8770a9183fe18442c7cfbb56bb7e006462775a91
Author: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
AuthorDate: Wed, 26 Feb 2025 12:44:55 +01:00
Committer: Thomas Gleixner <tglx@linutronix.de>
CommitterDate: Mon, 03 Mar 2025 20:00:13 +01:00
selftests: vDSO: vdso_standalone_test_x86: Switch to nolibc
vdso_standalone_test_x86 provides its own ASM syscall wrappers and
_start() implementation. The in-tree nolibc library already provides
this functionality for multiple architectures. By making use of nolibc,
the standalone testcase can be built from the exact same codebase as the
non-standalone version.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Vincenzo Frascino <vincenzo.frascino@arm.com>
Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Link: https://lore.kernel.org/all/20250226-parse_vdso-nolibc-v2-16-28e14e031ed8@linutronix.de
---
tools/testing/selftests/vDSO/Makefile | 8 +-
tools/testing/selftests/vDSO/vdso_standalone_test_x86.c | 175 +------
2 files changed, 39 insertions(+), 144 deletions(-)
diff --git a/tools/testing/selftests/vDSO/Makefile b/tools/testing/selftests/vDSO/Makefile
index bc8ca18..12a0614 100644
--- a/tools/testing/selftests/vDSO/Makefile
+++ b/tools/testing/selftests/vDSO/Makefile
@@ -22,13 +22,17 @@ include ../lib.mk
CFLAGS += $(TOOLS_INCLUDES)
+CFLAGS_NOLIBC := -nostdlib -nostdinc -ffreestanding -fno-asynchronous-unwind-tables \
+ -fno-stack-protector -include $(top_srcdir)/tools/include/nolibc/nolibc.h \
+ -I$(top_srcdir)/tools/include/nolibc/ $(KHDR_INCLUDES)
+
$(OUTPUT)/vdso_test_gettimeofday: parse_vdso.c vdso_test_gettimeofday.c
$(OUTPUT)/vdso_test_getcpu: parse_vdso.c vdso_test_getcpu.c
$(OUTPUT)/vdso_test_abi: parse_vdso.c vdso_test_abi.c
$(OUTPUT)/vdso_test_clock_getres: vdso_test_clock_getres.c
-$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c
-$(OUTPUT)/vdso_standalone_test_x86: CFLAGS +=-nostdlib -fno-asynchronous-unwind-tables -fno-stack-protector
+$(OUTPUT)/vdso_standalone_test_x86: vdso_standalone_test_x86.c parse_vdso.c | headers
+$(OUTPUT)/vdso_standalone_test_x86: CFLAGS:=$(CFLAGS_NOLIBC) $(CFLAGS)
$(OUTPUT)/vdso_test_correctness: vdso_test_correctness.c
$(OUTPUT)/vdso_test_correctness: LDFLAGS += -ldl
diff --git a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
index 500608f..9ce795b 100644
--- a/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
+++ b/tools/testing/selftests/vDSO/vdso_standalone_test_x86.c
@@ -1,167 +1,58 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
- * vdso_test.c: Sample code to test parse_vdso.c on x86
- * Copyright (c) 2011-2014 Andy Lutomirski
+ * vdso_test_gettimeofday.c: Sample code to test parse_vdso.c and
+ * vDSO gettimeofday()
+ * Copyright (c) 2014 Andy Lutomirski
*
- * You can amuse yourself by compiling with:
- * gcc -std=gnu99 -nostdlib
- * -Os -fno-asynchronous-unwind-tables -flto -lgcc_s
- * vdso_standalone_test_x86.c parse_vdso.c
- * to generate a small binary. On x86_64, you can omit -lgcc_s
- * if you want the binary to be completely standalone.
+ * Compile with:
+ * gcc -std=gnu99 vdso_test_gettimeofday.c parse_vdso_gettimeofday.c
+ *
+ * Tested on x86, 32-bit and 64-bit. It may work on other architectures, too.
*/
-#include <sys/syscall.h>
+#include <stdio.h>
+#ifndef NOLIBC
+#include <sys/auxv.h>
#include <sys/time.h>
-#include <unistd.h>
-#include <stdint.h>
-#include <linux/auxvec.h>
-
-#include "parse_vdso.h"
-
-/* We need some libc functions... */
-int strcmp(const char *a, const char *b)
-{
- /* This implementation is buggy: it never returns -1. */
- while (*a || *b) {
- if (*a != *b)
- return 1;
- if (*a == 0 || *b == 0)
- return 1;
- a++;
- b++;
- }
-
- return 0;
-}
-
-/*
- * The clang build needs this, although gcc does not.
- * Stolen from lib/string.c.
- */
-void *memcpy(void *dest, const void *src, size_t count)
-{
- char *tmp = dest;
- const char *s = src;
-
- while (count--)
- *tmp++ = *s++;
- return dest;
-}
-
-/* ...and two syscalls. This is x86-specific. */
-static inline long x86_syscall3(long nr, long a0, long a1, long a2)
-{
- long ret;
-#ifdef __x86_64__
- asm volatile ("syscall" : "=a" (ret) : "a" (nr),
- "D" (a0), "S" (a1), "d" (a2) :
- "cc", "memory", "rcx",
- "r8", "r9", "r10", "r11" );
-#else
- asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
- "b" (a0), "c" (a1), "d" (a2) :
- "cc", "memory" );
#endif
- return ret;
-}
-
-static inline long linux_write(int fd, const void *data, size_t len)
-{
- return x86_syscall3(__NR_write, fd, (long)data, (long)len);
-}
-static inline void linux_exit(int code)
-{
- x86_syscall3(__NR_exit, code, 0, 0);
-}
-
-void to_base10(char *lastdig, time_t n)
-{
- while (n) {
- *lastdig = (n % 10) + '0';
- n /= 10;
- lastdig--;
- }
-}
+#include "../kselftest.h"
+#include "parse_vdso.h"
+#include "vdso_config.h"
+#include "vdso_call.h"
-unsigned long getauxval(const unsigned long *auxv, unsigned long type)
+int main(int argc, char **argv)
{
- unsigned long ret;
-
- if (!auxv)
- return 0;
-
- while (1) {
- if (!auxv[0] && !auxv[1]) {
- ret = 0;
- break;
- }
+ const char *version = versions[VDSO_VERSION];
+ const char **name = (const char **)&names[VDSO_NAMES];
- if (auxv[0] == type) {
- ret = auxv[1];
- break;
- }
-
- auxv += 2;
+ unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
+ if (!sysinfo_ehdr) {
+ printf("AT_SYSINFO_EHDR is not present!\n");
+ return KSFT_SKIP;
}
- return ret;
-}
-
-void c_main(void **stack)
-{
- /* Parse the stack */
- long argc = (long)*stack;
- stack += argc + 2;
-
- /* Now we're pointing at the environment. Skip it. */
- while(*stack)
- stack++;
- stack++;
-
- /* Now we're pointing at auxv. Initialize the vDSO parser. */
- vdso_init_from_sysinfo_ehdr(getauxval((unsigned long *)stack, AT_SYSINFO_EHDR));
+ vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
/* Find gettimeofday. */
typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
- gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
+ gtod_t gtod = (gtod_t)vdso_sym(version, name[0]);
- if (!gtod)
- linux_exit(1);
+ if (!gtod) {
+ printf("Could not find %s\n", name[0]);
+ return KSFT_SKIP;
+ }
struct timeval tv;
- long ret = gtod(&tv, 0);
+ long ret = VDSO_CALL(gtod, 2, &tv, 0);
if (ret == 0) {
- char buf[] = "The time is .000000\n";
- to_base10(buf + 31, tv.tv_sec);
- to_base10(buf + 38, tv.tv_usec);
- linux_write(1, buf, sizeof(buf) - 1);
+ printf("The time is %lld.%06lld\n",
+ (long long)tv.tv_sec, (long long)tv.tv_usec);
} else {
- linux_exit(ret);
+ printf("%s failed\n", name[0]);
+ return KSFT_FAIL;
}
- linux_exit(0);
+ return 0;
}
-
-/*
- * This is the real entry point. It passes the initial stack into
- * the C entry point.
- */
-asm (
- ".text\n"
- ".global _start\n"
- ".type _start,@function\n"
- "_start:\n\t"
-#ifdef __x86_64__
- "mov %rsp,%rdi\n\t"
- "and $-16,%rsp\n\t"
- "sub $8,%rsp\n\t"
- "jmp c_main"
-#else
- "push %esp\n\t"
- "call c_main\n\t"
- "int $3"
-#endif
- );
© 2016 - 2025 Red Hat, Inc.