[PATCH v2 1/5] um: vdso: add shadow headers for the vDSO build
Haikal Firmansyah
haikalfirmansyh.kernel at gmail.com
Wed Nov 25 01:37:34 PST 2026
The generic vDSO build needs asm/vdso headers that UML cannot
reuse verbatim from x86, so add shadow copies active only with
-DBUILD_VDSO:
- gettimeofday.h: rdtsc_ordered() as plain "lfence; rdtsc",
because the x86 ALTERNATIVE patching machinery is not available
in the UML vDSO build;
- processor.h: avoid redefining cpu_relax, which UML already
defines with its time-travel handling;
- clocksource.h, vsyscall.h: minimal shims for the vDSO build.
These are workarounds for UML build limitations; a cleaner fix
(such as making ALTERNATIVE patching work in the vDSO build) may
be preferred on review.
Signed-off-by: Haikal Firmansyah <haikalfirmansyh.kernel at gmail.com>
---
arch/x86/um/asm/vdso/clocksource.h | 14 +++
arch/x86/um/asm/vdso/gettimeofday.h | 174 ++++++++++++++++++++++++++++
arch/x86/um/asm/vdso/processor.h | 40 +++++++
arch/x86/um/asm/vdso/vsyscall.h | 25 ++++
4 files changed, 253 insertions(+)
create mode 100644 arch/x86/um/asm/vdso/clocksource.h
create mode 100644 arch/x86/um/asm/vdso/gettimeofday.h
create mode 100644 arch/x86/um/asm/vdso/processor.h
create mode 100644 arch/x86/um/asm/vdso/vsyscall.h
diff --git a/arch/x86/um/asm/vdso/clocksource.h
b/arch/x86/um/asm/vdso/clocksource.h
new file mode 100644
index 000000000..2cbfdc454
--- /dev/null
+++ b/arch/x86/um/asm/vdso/clocksource.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_VDSO_CLOCKSOURCE_H
+#define __ASM_VDSO_CLOCKSOURCE_H
+
+/*
+ * UML vDSO clocksource modes (shadow of arch/x86/include/asm/vdso/
+ * clocksource.h, picked up first via -I arch/x86/um).
+ *
+ * The host TSC is the only counter guest userspace can read without a
+ * trap. No pvclock/hvclock pages exist under UML.
+ */
+#define VDSO_ARCH_CLOCKMODES VDSO_CLOCKMODE_TSC
+
+#endif /* __ASM_VDSO_CLOCKSOURCE_H */
diff --git a/arch/x86/um/asm/vdso/gettimeofday.h
b/arch/x86/um/asm/vdso/gettimeofday.h
new file mode 100644
index 000000000..3f9d3a54a
--- /dev/null
+++ b/arch/x86/um/asm/vdso/gettimeofday.h
@@ -0,0 +1,174 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Fast user context implementation of clock_gettime, gettimeofday,
+ * time and clock_getres for UML.
+ *
+ * Shadow of arch/x86/include/asm/vdso/gettimeofday.h (picked up first
+ * via -I arch/x86/um), reduced to what UML needs: TSC only, no
+ * pvclock/hvclock, no getcpu/getrandom. The generic code in
+ * lib/vdso/gettimeofday.c requires this header to provide:
+ * - __arch_get_hw_counter()
+ * - clock_gettime_fallback(), gettimeofday_fallback(),
+ * clock_getres_fallback()
+ * - vdso_calc_ns() (the x86 TSC variant, which filters small
+ * negative motion from TSC skew between host CPUs)
+ */
+#ifndef __ASM_VDSO_GETTIMEOFDAY_H
+#define __ASM_VDSO_GETTIMEOFDAY_H
+
+#ifndef __ASSEMBLER__
+
+#include <uapi/linux/time.h>
+#include <asm/unistd.h>
+#include <linux/compiler.h>
+#include <linux/types.h>
+#include <vdso/datapage.h>
+#include <vdso/limits.h>
+#include <vdso/math64.h>
+
+#define VDSO_HAS_TIME 1
+
+#define VDSO_HAS_CLOCK_GETRES 1
+
+/*
+ * Ordered TSC read.
+ *
+ * x86's rdtsc_ordered() uses the
+ * ALTERNATIVE patching machinery (lfence;rdtsc vs rdtscp selected at
+ * boot), which is unavailable in the UML vDSO build. The sequence
+ * used here, "lfence; rdtsc", is exactly the ordered variant x86
+ * selects when X86_FEATURE_LFENCE_RDTSC is present, and is the
+ * architecturally recommended ordered read on AMD and Intel. The
+ * "memory" clobber additionally stops the compiler from hoisting
+ * vvar loads above the counter read.
+ */
+static __always_inline u64 um_rdtsc_ordered(void)
+{
+ u32 lo, hi;
+
+ asm volatile("lfence; rdtsc" : "=a" (lo), "=d" (hi) : : "memory");
+ return ((u64)hi << 32) | lo;
+}
+
+static __always_inline
+long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
+{
+ long ret;
+
+ asm volatile("syscall"
+ : "=a" (ret)
+ : "0" (__NR_clock_gettime), "D" (_clkid), "S" (_ts)
+ : "rcx", "r11", "memory");
+ return ret;
+}
+
+static __always_inline
+long gettimeofday_fallback(struct __kernel_old_timeval *_tv,
+ struct timezone *_tz)
+{
+ long ret;
+
+ asm volatile("syscall"
+ : "=a" (ret)
+ : "0" (__NR_gettimeofday), "D" (_tv), "S" (_tz)
+ : "rcx", "r11", "memory");
+ return ret;
+}
+
+static __always_inline
+long clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
+{
+ long ret;
+
+ asm volatile("syscall"
+ : "=a" (ret)
+ : "0" (__NR_clock_getres), "D" (_clkid), "S" (_ts)
+ : "rcx", "r11", "memory");
+ return ret;
+}
+
+static inline u64 __arch_get_hw_counter(s32 clock_mode,
+ const struct vdso_time_data *vd)
+{
+ if (likely(clock_mode == VDSO_CLOCKMODE_TSC))
+ return um_rdtsc_ordered() & S64_MAX;
+ return U64_MAX;
+}
+
+static inline bool arch_vdso_clocksource_ok(const struct vdso_clock *vc)
+{
+ return true;
+}
+
+#define vdso_clocksource_ok arch_vdso_clocksource_ok
+
+/*
+ * Clocksource read value validation: U64_MAX is the invalid marker
+ * for the memory-mapped vclock types, and the x86 variant below uses
+ * the sign bit the same way, so the effective mask is S64_MAX.
+ */
+static inline bool arch_vdso_cycles_ok(u64 cycles)
+{
+ return (s64)cycles >= 0;
+}
+
+#define vdso_cycles_ok arch_vdso_cycles_ok
+
+/*
+ * x86 specific calculation of nanoseconds for the current cycle count
+ * (copied from arch/x86/include/asm/vdso/gettimeofday.h).
+ *
+ * The regular implementation assumes that clocksource reads are
+ * globally monotonic. The TSC can be slightly off across sockets
+ * which can cause the regular delta calculation (@cycles - @last)
+ * to return a huge time jump.
+ *
+ * Therefore it needs to be verified that @cycles are greater than
+ * @vd->cycles_last. If not then use @vd->cycles_last, which is the
+ * base time of the current conversion period.
+ *
+ * This variant also uses a custom mask because while the clocksource
+ * mask of all the VDSO capable clocksources on x86 is U64_MAX, the
+ * above code uses U64_MASK as an exception value, additionally
+ * arch_vdso_cycles_ok() above declares everything with the
+ * MSB/Sign-bit set as invalid. Therefore the effective mask is
+ * S64_MAX.
+ *
+ * Requires CONFIG_GENERIC_VDSO_OVERFLOW_PROTECT (vc->max_cycles).
+ */
+static __always_inline u64 vdso_calc_ns(const struct vdso_clock *vc,
+ u64 cycles, u64 base)
+{
+ u64 delta = cycles - vc->cycle_last;
+
+ /*
+ * Negative motion and deltas which can cause multiplication
+ * overflow require special treatment. This check covers both as
+ * negative motion is guaranteed to be greater than @vc::max_cycles
+ * due to unsigned comparison.
+ *
+ * Due to the MSB/Sign-bit being used as invalid marker (see
+ * arch_vdso_cycles_ok() above), the effective mask is S64_MAX, but that
+ * case is also unlikely and will also take the unlikely path here.
+ */
+ if (unlikely(delta > vc->max_cycles)) {
+ /*
+ * Due to the above mentioned TSC wobbles, filter out
+ * negative motion. Per the above masking, the effective
+ * sign bit is now bit 62.
+ */
+ if (delta & (1ULL << 62))
+ return base >> vc->shift;
+
+ /* Handle multiplication overflow gracefully */
+ return mul_u64_u32_add_u64_shr(delta & S64_MAX, vc->mult, base, vc->shift);
+ }
+
+ return ((delta * vc->mult) + base) >> vc->shift;
+}
+
+#define vdso_calc_ns vdso_calc_ns
+
+#endif /* !__ASSEMBLER__ */
+
+#endif /* __ASM_VDSO_GETTIMEOFDAY_H */
diff --git a/arch/x86/um/asm/vdso/processor.h b/arch/x86/um/asm/vdso/processor.h
new file mode 100644
index 000000000..599835e97
--- /dev/null
+++ b/arch/x86/um/asm/vdso/processor.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Shadow of arch/x86/include/asm/vdso/processor.h (picked up first
+ * via -I arch/x86/um).
+ *
+ * The x86 header defines native_pause()/cpu_relax() for the vDSO.
+ * Under UML those two are already defined by arch/x86/um/asm/
+ * processor.h (with extra time-travel handling in cpu_relax()), so
+ * defining them again kernel-side would be a redefinition error
+ * (kernel/time/vsyscall.c includes <vdso/helpers.h>, which includes
+ * this header, after <asm/processor.h>).
+ *
+ * Therefore the definitions below are only active for the vDSO build
+ * itself (-DBUILD_VDSO, see arch/x86/um/vdso/Makefile), where
+ * <asm/processor.h> is not included. Kernel-side this header is
+ * empty and cpu_relax() keeps coming from <asm/processor.h>.
+ *
+ * Deliberately no __vdso_getcpu declaration: UML exports no getcpu.
+ */
+#ifndef __ASM_VDSO_PROCESSOR_H
+#define __ASM_VDSO_PROCESSOR_H
+
+#ifndef __ASSEMBLER__
+
+#ifdef BUILD_VDSO
+/* PAUSE is a good thing to insert into busy-wait loops. */
+static __always_inline void native_pause(void)
+{
+ asm volatile("pause" ::: "memory");
+}
+
+static __always_inline void cpu_relax(void)
+{
+ native_pause();
+}
+#endif /* BUILD_VDSO */
+
+#endif /* !__ASSEMBLER__ */
+
+#endif /* __ASM_VDSO_PROCESSOR_H */
diff --git a/arch/x86/um/asm/vdso/vsyscall.h b/arch/x86/um/asm/vdso/vsyscall.h
new file mode 100644
index 000000000..da5a12651
--- /dev/null
+++ b/arch/x86/um/asm/vdso/vsyscall.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_VDSO_VSYSCALL_H
+#define __ASM_VDSO_VSYSCALL_H
+
+/*
+ * UML vDSO layout (shadow of arch/x86/include/asm/vdso/vsyscall.h,
+ * picked up first via -I arch/x86/um).
+ *
+ * UML has 4 vvar pages: TIME, TIMENS, RNG, ARCH (see the vdso_pages
+ * enum in include/vdso/datapage.h, with the default 1-page
+ * struct vdso_arch_data). There are no pvclock/hvclock vclock pages,
+ * unlike x86 which uses 6 pages.
+ */
+#define __VDSO_PAGES 4
+
+#ifndef __ASSEMBLER__
+
+#include <vdso/datapage.h>
+
+/* The asm-generic header needs to be included after the definitions above */
+#include <asm-generic/vdso/vsyscall.h>
+
+#endif /* !__ASSEMBLER__ */
+
+#endif /* __ASM_VDSO_VSYSCALL_H */
--
2.43.0
More information about the linux-um
mailing list