GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / arm64 / include / asm / vdso / compat_gettimeofday.h
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 #include <asm/vdso/clocksource.h>
14 #include <asm/vdso/compat_barrier.h>
15
16 #define __VDSO_USE_SYSCALL              ULLONG_MAX
17
18 #define VDSO_HAS_CLOCK_GETRES           1
19
20 #define VDSO_HAS_32BIT_FALLBACK         1
21
22 static __always_inline
23 int gettimeofday_fallback(struct __kernel_old_timeval *_tv,
24                           struct timezone *_tz)
25 {
26         register struct timezone *tz asm("r1") = _tz;
27         register struct __kernel_old_timeval *tv asm("r0") = _tv;
28         register long ret asm ("r0");
29         register long nr asm("r7") = __NR_compat_gettimeofday;
30
31         asm volatile(
32         "       swi #0\n"
33         : "=r" (ret)
34         : "r" (tv), "r" (tz), "r" (nr)
35         : "memory");
36
37         return ret;
38 }
39
40 static __always_inline
41 long clock_gettime_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
42 {
43         register struct __kernel_timespec *ts asm("r1") = _ts;
44         register clockid_t clkid asm("r0") = _clkid;
45         register long ret asm ("r0");
46         register long nr asm("r7") = __NR_compat_clock_gettime64;
47
48         asm volatile(
49         "       swi #0\n"
50         : "=r" (ret)
51         : "r" (clkid), "r" (ts), "r" (nr)
52         : "memory");
53
54         return ret;
55 }
56
57 static __always_inline
58 long clock_gettime32_fallback(clockid_t _clkid, struct old_timespec32 *_ts)
59 {
60         register struct old_timespec32 *ts asm("r1") = _ts;
61         register clockid_t clkid asm("r0") = _clkid;
62         register long ret asm ("r0");
63         register long nr asm("r7") = __NR_compat_clock_gettime;
64
65         asm volatile(
66         "       swi #0\n"
67         : "=r" (ret)
68         : "r" (clkid), "r" (ts), "r" (nr)
69         : "memory");
70
71         return ret;
72 }
73
74 static __always_inline
75 int clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts)
76 {
77         register struct __kernel_timespec *ts asm("r1") = _ts;
78         register clockid_t clkid asm("r0") = _clkid;
79         register long ret asm ("r0");
80         register long nr asm("r7") = __NR_compat_clock_getres_time64;
81
82         /* The checks below are required for ABI consistency with arm */
83         if ((_clkid >= MAX_CLOCKS) && (_ts == NULL))
84                 return -EINVAL;
85
86         asm volatile(
87         "       swi #0\n"
88         : "=r" (ret)
89         : "r" (clkid), "r" (ts), "r" (nr)
90         : "memory");
91
92         return ret;
93 }
94
95 static __always_inline
96 int clock_getres32_fallback(clockid_t _clkid, struct old_timespec32 *_ts)
97 {
98         register struct old_timespec32 *ts asm("r1") = _ts;
99         register clockid_t clkid asm("r0") = _clkid;
100         register long ret asm ("r0");
101         register long nr asm("r7") = __NR_compat_clock_getres;
102
103         /* The checks below are required for ABI consistency with arm */
104         if ((_clkid >= MAX_CLOCKS) && (_ts == NULL))
105                 return -EINVAL;
106
107         asm volatile(
108         "       swi #0\n"
109         : "=r" (ret)
110         : "r" (clkid), "r" (ts), "r" (nr)
111         : "memory");
112
113         return ret;
114 }
115
116 static __always_inline u64 __arch_get_hw_counter(s32 clock_mode)
117 {
118         u64 res;
119
120         /*
121          * clock_mode == ARCHTIMER implies that vDSO are enabled otherwise
122          * fallback on syscall.
123          */
124         if (clock_mode != VDSO_CLOCKMODE_ARCHTIMER)
125                 return __VDSO_USE_SYSCALL;
126
127         /*
128          * This isb() is required to prevent that the counter value
129          * is speculated.
130          */
131         isb();
132         asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (res));
133         /*
134          * This isb() is required to prevent that the seq lock is
135          * speculated.
136          */
137         isb();
138
139         return res;
140 }
141
142 static __always_inline const struct vdso_data *__arch_get_vdso_data(void)
143 {
144         const struct vdso_data *ret;
145
146         /*
147          * This simply puts &_vdso_data into ret. The reason why we don't use
148          * `ret = _vdso_data` is that the compiler tends to optimise this in a
149          * very suboptimal way: instead of keeping &_vdso_data in a register,
150          * it goes through a relocation almost every time _vdso_data must be
151          * accessed (even in subfunctions). This is both time and space
152          * consuming: each relocation uses a word in the code section, and it
153          * has to be loaded at runtime.
154          *
155          * This trick hides the assignment from the compiler. Since it cannot
156          * track where the pointer comes from, it will only use one relocation
157          * where __arch_get_vdso_data() is called, and then keep the result in
158          * a register.
159          */
160         asm volatile("mov %0, %1" : "=r"(ret) : "r"(_vdso_data));
161
162         return ret;
163 }
164
165 #endif /* !__ASSEMBLY__ */
166
167 #endif /* __ASM_VDSO_GETTIMEOFDAY_H */