Skip to content

Commit 28b1a82

Browse files
thule-vincenzoKAGA-KOKO
authored andcommitted
arm64: vdso: Substitute gettimeofday() with C implementation
To take advantage of the commonly defined vdso interface for gettimeofday() the architectural code requires an adaptation. Re-implement the gettimeofday VDSO in C in order to use lib/vdso. With the new implementation arm64 gains support for CLOCK_BOOTTIME and CLOCK_TAI. [ tglx: Reformatted the function line breaks ] Signed-off-by: Vincenzo Frascino <vincenzo.frascino@arm.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Tested-by: Shijith Thotton <sthotton@marvell.com> Tested-by: Andre Przywara <andre.przywara@arm.com> Cc: linux-arch@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org Cc: linux-mips@vger.kernel.org Cc: linux-kselftest@vger.kernel.org Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Russell King <linux@armlinux.org.uk> Cc: Ralf Baechle <ralf@linux-mips.org> Cc: Paul Burton <paul.burton@mips.com> Cc: Daniel Lezcano <daniel.lezcano@linaro.org> Cc: Mark Salyzyn <salyzyn@android.com> Cc: Peter Collingbourne <pcc@google.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Dmitry Safonov <0x7f454c46@gmail.com> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk> Cc: Huw Davies <huw@codeweavers.com> Link: https://lkml.kernel.org/r/20190621095252.32307-5-vincenzo.frascino@arm.com
1 parent 44f57d7 commit 28b1a82

8 files changed

Lines changed: 220 additions & 387 deletions

File tree

‎arch/arm64/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ config ARM64
107107
select GENERIC_STRNCPY_FROM_USER
108108
select GENERIC_STRNLEN_USER
109109
select GENERIC_TIME_VSYSCALL
110+
select GENERIC_GETTIMEOFDAY
110111
select HANDLE_DOMAIN_IRQ
111112
select HARDIRQS_SW_RESEND
112113
select HAVE_PCI
@@ -160,6 +161,7 @@ config ARM64
160161
select HAVE_SYSCALL_TRACEPOINTS
161162
select HAVE_KPROBES
162163
select HAVE_KRETPROBES
164+
select HAVE_GENERIC_VDSO
163165
select IOMMU_DMA if IOMMU_SUPPORT
164166
select IRQ_DOMAIN
165167
select IRQ_FORCED_THREADING
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2018 ARM Limited
4+
*/
5+
#ifndef __ASM_VDSO_GETTIMEOFDAY_H
6+
#define __ASM_VDSO_GETTIMEOFDAY_H
7+
8+
#ifndef __ASSEMBLY__
9+
10+
#include <asm/unistd.h>
11+
#include <uapi/linux/time.h>
12+
13+
#define VDSO_HAS_CLOCK_GETRES 1
14+
15+
static __always_inline
16+
int gettimeofday_fallback(struct __kernel_old_timeval *_tv,
17+
struct timezone *_tz)
18+
{
19+
register struct timezone *tz asm("x1") = _tz;
20+
register struct __kernel_old_timeval *tv asm("x0") = _tv;
21+
register long ret asm ("x0");
22+
register long nr asm("x8") = __NR_gettimeofday;
23+
24+
asm volatile(
25+
" svc #0\n"
26+
: "=r" (ret)
27+
: "r" (tv), "r" (tz), "r" (nr)
28+
: "memory");
29+
30+
return ret;
31+
}
32+
33+
static __always_inline
34+
long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
35+
{
36+
register struct __kernel_timespec *ts asm("x1") = _ts;
37+
register clockid_t clkid asm("x0") = _clkid;
38+
register long ret asm ("x0");
39+
register long nr asm("x8") = __NR_clock_gettime;
40+
41+
asm volatile(
42+
" svc #0\n"
43+
: "=r" (ret)
44+
: "r" (clkid), "r" (ts), "r" (nr)
45+
: "memory");
46+
47+
return ret;
48+
}
49+
50+
static __always_inline
51+
int clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
52+
{
53+
register struct __kernel_timespec *ts asm("x1") = _ts;
54+
register clockid_t clkid asm("x0") = _clkid;
55+
register long ret asm ("x0");
56+
register long nr asm("x8") = __NR_clock_getres;
57+
58+
asm volatile(
59+
" svc #0\n"
60+
: "=r" (ret)
61+
: "r" (clkid), "r" (ts), "r" (nr)
62+
: "memory");
63+
64+
return ret;
65+
}
66+
67+
static __always_inline u64 __arch_get_hw_counter(s32 clock_mode)
68+
{
69+
u64 res;
70+
71+
asm volatile("mrs %0, cntvct_el0" : "=r" (res) :: "memory");
72+
73+
return res;
74+
}
75+
76+
static __always_inline
77+
const struct vdso_data *__arch_get_vdso_data(void)
78+
{
79+
return _vdso_data;
80+
}
81+
82+
#endif /* !__ASSEMBLY__ */
83+
84+
#endif /* __ASM_VDSO_GETTIMEOFDAY_H */
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __ASM_VDSO_VSYSCALL_H
3+
#define __ASM_VDSO_VSYSCALL_H
4+
5+
#ifndef __ASSEMBLY__
6+
7+
#include <linux/timekeeper_internal.h>
8+
#include <vdso/datapage.h>
9+
10+
#define VDSO_PRECISION_MASK ~(0xFF00ULL<<48)
11+
12+
extern struct vdso_data *vdso_data;
13+
14+
/*
15+
* Update the vDSO data page to keep in sync with kernel timekeeping.
16+
*/
17+
static __always_inline
18+
struct vdso_data *__arm64_get_k_vdso_data(void)
19+
{
20+
return vdso_data;
21+
}
22+
#define __arch_get_k_vdso_data __arm64_get_k_vdso_data
23+
24+
static __always_inline
25+
int __arm64_get_clock_mode(struct timekeeper *tk)
26+
{
27+
u32 use_syscall = !tk->tkr_mono.clock->archdata.vdso_direct;
28+
29+
return use_syscall;
30+
}
31+
#define __arch_get_clock_mode __arm64_get_clock_mode
32+
33+
static __always_inline
34+
int __arm64_use_vsyscall(struct vdso_data *vdata)
35+
{
36+
return !vdata[CS_HRES_COARSE].clock_mode;
37+
}
38+
#define __arch_use_vsyscall __arm64_use_vsyscall
39+
40+
static __always_inline
41+
void __arm64_update_vsyscall(struct vdso_data *vdata, struct timekeeper *tk)
42+
{
43+
vdata[CS_HRES_COARSE].mask = VDSO_PRECISION_MASK;
44+
vdata[CS_RAW].mask = VDSO_PRECISION_MASK;
45+
}
46+
#define __arch_update_vsyscall __arm64_update_vsyscall
47+
48+
/* The asm-generic header needs to be included after the definitions above */
49+
#include <asm-generic/vdso/vsyscall.h>
50+
51+
#endif /* !__ASSEMBLY__ */
52+
53+
#endif /* __ASM_VDSO_VSYSCALL_H */

‎arch/arm64/kernel/asm-offsets.c‎

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
#include <linux/kvm_host.h>
1515
#include <linux/preempt.h>
1616
#include <linux/suspend.h>
17+
#include <vdso/datapage.h>
1718
#include <asm/cpufeature.h>
1819
#include <asm/fixmap.h>
1920
#include <asm/thread_info.h>
2021
#include <asm/memory.h>
2122
#include <asm/smp_plat.h>
2223
#include <asm/suspend.h>
23-
#include <asm/vdso_datapage.h>
2424
#include <linux/kbuild.h>
2525
#include <linux/arm-smccc.h>
2626

@@ -89,17 +89,28 @@ int main(void)
8989
DEFINE(CLOCK_COARSE_RES, LOW_RES_NSEC);
9090
DEFINE(NSEC_PER_SEC, NSEC_PER_SEC);
9191
BLANK();
92-
DEFINE(VDSO_CS_CYCLE_LAST, offsetof(struct vdso_data, cs_cycle_last));
93-
DEFINE(VDSO_RAW_TIME_SEC, offsetof(struct vdso_data, raw_time_sec));
94-
DEFINE(VDSO_XTIME_CLK_SEC, offsetof(struct vdso_data, xtime_clock_sec));
95-
DEFINE(VDSO_XTIME_CRS_SEC, offsetof(struct vdso_data, xtime_coarse_sec));
96-
DEFINE(VDSO_XTIME_CRS_NSEC, offsetof(struct vdso_data, xtime_coarse_nsec));
97-
DEFINE(VDSO_WTM_CLK_SEC, offsetof(struct vdso_data, wtm_clock_sec));
98-
DEFINE(VDSO_TB_SEQ_COUNT, offsetof(struct vdso_data, tb_seq_count));
99-
DEFINE(VDSO_CS_MONO_MULT, offsetof(struct vdso_data, cs_mono_mult));
100-
DEFINE(VDSO_CS_SHIFT, offsetof(struct vdso_data, cs_shift));
92+
DEFINE(VDSO_SEQ, offsetof(struct vdso_data, seq));
93+
DEFINE(VDSO_CLK_MODE, offsetof(struct vdso_data, clock_mode));
94+
DEFINE(VDSO_CYCLE_LAST, offsetof(struct vdso_data, cycle_last));
95+
DEFINE(VDSO_MASK, offsetof(struct vdso_data, mask));
96+
DEFINE(VDSO_MULT, offsetof(struct vdso_data, mult));
97+
DEFINE(VDSO_SHIFT, offsetof(struct vdso_data, shift));
98+
DEFINE(VDSO_REALTIME_SEC, offsetof(struct vdso_data, basetime[CLOCK_REALTIME].sec));
99+
DEFINE(VDSO_REALTIME_NSEC, offsetof(struct vdso_data, basetime[CLOCK_REALTIME].nsec));
100+
DEFINE(VDSO_MONO_SEC, offsetof(struct vdso_data, basetime[CLOCK_MONOTONIC].sec));
101+
DEFINE(VDSO_MONO_NSEC, offsetof(struct vdso_data, basetime[CLOCK_MONOTONIC].nsec));
102+
DEFINE(VDSO_MONO_RAW_SEC, offsetof(struct vdso_data, basetime[CLOCK_MONOTONIC_RAW].sec));
103+
DEFINE(VDSO_MONO_RAW_NSEC, offsetof(struct vdso_data, basetime[CLOCK_MONOTONIC_RAW].nsec));
104+
DEFINE(VDSO_BOOTTIME_SEC, offsetof(struct vdso_data, basetime[CLOCK_BOOTTIME].sec));
105+
DEFINE(VDSO_BOOTTIME_NSEC, offsetof(struct vdso_data, basetime[CLOCK_BOOTTIME].nsec));
106+
DEFINE(VDSO_TAI_SEC, offsetof(struct vdso_data, basetime[CLOCK_TAI].sec));
107+
DEFINE(VDSO_TAI_NSEC, offsetof(struct vdso_data, basetime[CLOCK_TAI].nsec));
108+
DEFINE(VDSO_RT_COARSE_SEC, offsetof(struct vdso_data, basetime[CLOCK_REALTIME_COARSE].sec));
109+
DEFINE(VDSO_RT_COARSE_NSEC, offsetof(struct vdso_data, basetime[CLOCK_REALTIME_COARSE].nsec));
110+
DEFINE(VDSO_MONO_COARSE_SEC, offsetof(struct vdso_data, basetime[CLOCK_MONOTONIC_COARSE].sec));
111+
DEFINE(VDSO_MONO_COARSE_NSEC, offsetof(struct vdso_data, basetime[CLOCK_MONOTONIC_COARSE].nsec));
101112
DEFINE(VDSO_TZ_MINWEST, offsetof(struct vdso_data, tz_minuteswest));
102-
DEFINE(VDSO_USE_SYSCALL, offsetof(struct vdso_data, use_syscall));
113+
DEFINE(VDSO_TZ_DSTTIME, offsetof(struct vdso_data, tz_dsttime));
103114
BLANK();
104115
DEFINE(TVAL_TV_SEC, offsetof(struct timeval, tv_sec));
105116
DEFINE(TSPEC_TV_SEC, offsetof(struct timespec, tv_sec));

‎arch/arm64/kernel/vdso.c‎

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
#include <linux/slab.h>
2121
#include <linux/timekeeper_internal.h>
2222
#include <linux/vmalloc.h>
23+
#include <vdso/datapage.h>
24+
#include <vdso/helpers.h>
25+
#include <vdso/vsyscall.h>
2326

2427
#include <asm/cacheflush.h>
2528
#include <asm/signal32.h>
2629
#include <asm/vdso.h>
27-
#include <asm/vdso_datapage.h>
2830

2931
extern char vdso_start[], vdso_end[];
3032
static unsigned long vdso_pages __ro_after_init;
@@ -33,10 +35,10 @@ static unsigned long vdso_pages __ro_after_init;
3335
* The vDSO data page.
3436
*/
3537
static union {
36-
struct vdso_data data;
38+
struct vdso_data data[CS_BASES];
3739
u8 page[PAGE_SIZE];
3840
} vdso_data_store __page_aligned_data;
39-
struct vdso_data *vdso_data = &vdso_data_store.data;
41+
struct vdso_data *vdso_data = vdso_data_store.data;
4042

4143
#ifdef CONFIG_COMPAT
4244
/*
@@ -269,46 +271,3 @@ int arch_setup_additional_pages(struct linux_binprm *bprm,
269271
up_write(&mm->mmap_sem);
270272
return PTR_ERR(ret);
271273
}
272-
273-
/*
274-
* Update the vDSO data page to keep in sync with kernel timekeeping.
275-
*/
276-
void update_vsyscall(struct timekeeper *tk)
277-
{
278-
u32 use_syscall = !tk->tkr_mono.clock->archdata.vdso_direct;
279-
280-
++vdso_data->tb_seq_count;
281-
smp_wmb();
282-
283-
vdso_data->use_syscall = use_syscall;
284-
vdso_data->xtime_coarse_sec = tk->xtime_sec;
285-
vdso_data->xtime_coarse_nsec = tk->tkr_mono.xtime_nsec >>
286-
tk->tkr_mono.shift;
287-
vdso_data->wtm_clock_sec = tk->wall_to_monotonic.tv_sec;
288-
vdso_data->wtm_clock_nsec = tk->wall_to_monotonic.tv_nsec;
289-
290-
/* Read without the seqlock held by clock_getres() */
291-
WRITE_ONCE(vdso_data->hrtimer_res, hrtimer_resolution);
292-
293-
if (!use_syscall) {
294-
/* tkr_mono.cycle_last == tkr_raw.cycle_last */
295-
vdso_data->cs_cycle_last = tk->tkr_mono.cycle_last;
296-
vdso_data->raw_time_sec = tk->raw_sec;
297-
vdso_data->raw_time_nsec = tk->tkr_raw.xtime_nsec;
298-
vdso_data->xtime_clock_sec = tk->xtime_sec;
299-
vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
300-
vdso_data->cs_mono_mult = tk->tkr_mono.mult;
301-
vdso_data->cs_raw_mult = tk->tkr_raw.mult;
302-
/* tkr_mono.shift == tkr_raw.shift */
303-
vdso_data->cs_shift = tk->tkr_mono.shift;
304-
}
305-
306-
smp_wmb();
307-
++vdso_data->tb_seq_count;
308-
}
309-
310-
void update_vsyscall_tz(void)
311-
{
312-
vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
313-
vdso_data->tz_dsttime = sys_tz.tz_dsttime;
314-
}

‎arch/arm64/kernel/vdso/Makefile‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
# Heavily based on the vDSO Makefiles for other archs.
77
#
88

9-
obj-vdso := gettimeofday.o note.o sigreturn.o
9+
# Absolute relocation type $(ARCH_REL_TYPE_ABS) needs to be defined before
10+
# the inclusion of generic Makefile.
11+
ARCH_REL_TYPE_ABS := R_AARCH64_JUMP_SLOT|R_AARCH64_GLOB_DAT|R_AARCH64_ABS64
12+
include $(srctree)/lib/vdso/Makefile
13+
14+
obj-vdso := vgettimeofday.o note.o sigreturn.o
1015

1116
# Build rules
1217
targets := $(obj-vdso) vdso.so vdso.so.dbg
@@ -15,6 +20,24 @@ obj-vdso := $(addprefix $(obj)/, $(obj-vdso))
1520
ldflags-y := -shared -nostdlib -soname=linux-vdso.so.1 --hash-style=sysv \
1621
--build-id -n -T
1722

23+
ccflags-y := -fno-common -fno-builtin -fno-stack-protector
24+
ccflags-y += -DDISABLE_BRANCH_PROFILING
25+
26+
VDSO_LDFLAGS := -Bsymbolic
27+
28+
CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os
29+
KBUILD_CFLAGS += $(DISABLE_LTO)
30+
KASAN_SANITIZE := n
31+
UBSAN_SANITIZE := n
32+
OBJECT_FILES_NON_STANDARD := y
33+
KCOV_INSTRUMENT := n
34+
35+
ifeq ($(c-gettimeofday-y),)
36+
CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny
37+
else
38+
CFLAGS_vgettimeofday.o = -O2 -mcmodel=tiny -include $(c-gettimeofday-y)
39+
endif
40+
1841
# Disable gcov profiling for VDSO code
1942
GCOV_PROFILE := n
2043

@@ -28,6 +51,7 @@ $(obj)/vdso.o : $(obj)/vdso.so
2851
# Link rule for the .so file, .lds has to be first
2952
$(obj)/vdso.so.dbg: $(obj)/vdso.lds $(obj-vdso) FORCE
3053
$(call if_changed,ld)
54+
$(call if_changed,vdso_check)
3155

3256
# Strip rule for the .so file
3357
$(obj)/%.so: OBJCOPYFLAGS := -S
@@ -42,13 +66,9 @@ quiet_cmd_vdsosym = VDSOSYM $@
4266
include/generated/vdso-offsets.h: $(obj)/vdso.so.dbg FORCE
4367
$(call if_changed,vdsosym)
4468

45-
# Assembly rules for the .S files
46-
$(obj-vdso): %.o: %.S FORCE
47-
$(call if_changed_dep,vdsoas)
48-
4969
# Actual build commands
50-
quiet_cmd_vdsoas = VDSOA $@
51-
cmd_vdsoas = $(CC) $(a_flags) -c -o $@ $<
70+
quiet_cmd_vdsocc = VDSOCC $@
71+
cmd_vdsocc = $(CC) $(a_flags) $(c_flags) -c -o $@ $<
5272

5373
# Install commands for the unstripped file
5474
quiet_cmd_vdso_install = INSTALL $@

0 commit comments

Comments
 (0)