blob: f0d5062d9cbc621a2ab32141b53c2f1f8cba0337 [file] [log] [blame]
Vincenzo Frascino44f57d72019-06-21 10:52:30 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2019 ARM Ltd.
4 *
5 * Generic implementation of update_vsyscall and update_vsyscall_tz.
6 *
7 * Based on the x86 specific implementation.
8 */
9
10#include <linux/hrtimer.h>
11#include <linux/timekeeper_internal.h>
12#include <vdso/datapage.h>
13#include <vdso/helpers.h>
14#include <vdso/vsyscall.h>
15
Thomas Gleixner19d00702020-08-04 17:01:23 +020016#include "timekeeping_internal.h"
17
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010018static inline void update_vdso_data(struct vdso_data *vdata,
19 struct timekeeper *tk)
20{
21 struct vdso_timestamp *vdso_ts;
Thomas Gleixnerb99328a2019-08-22 13:00:15 +020022 u64 nsec, sec;
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010023
24 vdata[CS_HRES_COARSE].cycle_last = tk->tkr_mono.cycle_last;
25 vdata[CS_HRES_COARSE].mask = tk->tkr_mono.mask;
26 vdata[CS_HRES_COARSE].mult = tk->tkr_mono.mult;
27 vdata[CS_HRES_COARSE].shift = tk->tkr_mono.shift;
28 vdata[CS_RAW].cycle_last = tk->tkr_raw.cycle_last;
29 vdata[CS_RAW].mask = tk->tkr_raw.mask;
30 vdata[CS_RAW].mult = tk->tkr_raw.mult;
31 vdata[CS_RAW].shift = tk->tkr_raw.shift;
32
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010033 /* CLOCK_MONOTONIC */
34 vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_MONOTONIC];
35 vdso_ts->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
36
37 nsec = tk->tkr_mono.xtime_nsec;
38 nsec += ((u64)tk->wall_to_monotonic.tv_nsec << tk->tkr_mono.shift);
39 while (nsec >= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift)) {
40 nsec -= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift);
41 vdso_ts->sec++;
42 }
43 vdso_ts->nsec = nsec;
44
Thomas Gleixnerb99328a2019-08-22 13:00:15 +020045 /* Copy MONOTONIC time for BOOTTIME */
46 sec = vdso_ts->sec;
47 /* Add the boot offset */
48 sec += tk->monotonic_to_boot.tv_sec;
49 nsec += (u64)tk->monotonic_to_boot.tv_nsec << tk->tkr_mono.shift;
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010050
51 /* CLOCK_BOOTTIME */
52 vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_BOOTTIME];
Thomas Gleixnerb99328a2019-08-22 13:00:15 +020053 vdso_ts->sec = sec;
54
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010055 while (nsec >= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift)) {
56 nsec -= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift);
57 vdso_ts->sec++;
58 }
59 vdso_ts->nsec = nsec;
60
Thomas Gleixnerb99328a2019-08-22 13:00:15 +020061 /* CLOCK_MONOTONIC_RAW */
62 vdso_ts = &vdata[CS_RAW].basetime[CLOCK_MONOTONIC_RAW];
63 vdso_ts->sec = tk->raw_sec;
64 vdso_ts->nsec = tk->tkr_raw.xtime_nsec;
65
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010066 /* CLOCK_TAI */
67 vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_TAI];
68 vdso_ts->sec = tk->xtime_sec + (s64)tk->tai_offset;
69 vdso_ts->nsec = tk->tkr_mono.xtime_nsec;
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010070}
71
72void update_vsyscall(struct timekeeper *tk)
73{
74 struct vdso_data *vdata = __arch_get_k_vdso_data();
75 struct vdso_timestamp *vdso_ts;
Thomas Gleixner5d51bee2020-02-07 13:38:55 +010076 s32 clock_mode;
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010077 u64 nsec;
78
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010079 /* copy vsyscall data */
80 vdso_write_begin(vdata);
81
Thomas Gleixner5d51bee2020-02-07 13:38:55 +010082 clock_mode = tk->tkr_mono.clock->vdso_clock_mode;
Thomas Gleixner5d51bee2020-02-07 13:38:55 +010083 vdata[CS_HRES_COARSE].clock_mode = clock_mode;
84 vdata[CS_RAW].clock_mode = clock_mode;
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010085
Thomas Gleixner9f24c542020-01-14 19:52:39 +010086 /* CLOCK_REALTIME also required for time() */
87 vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_REALTIME];
88 vdso_ts->sec = tk->xtime_sec;
89 vdso_ts->nsec = tk->tkr_mono.xtime_nsec;
90
Vincenzo Frascino44f57d72019-06-21 10:52:30 +010091 /* CLOCK_REALTIME_COARSE */
92 vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_REALTIME_COARSE];
93 vdso_ts->sec = tk->xtime_sec;
94 vdso_ts->nsec = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
95
96 /* CLOCK_MONOTONIC_COARSE */
97 vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_MONOTONIC_COARSE];
98 vdso_ts->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
99 nsec = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift;
100 nsec = nsec + tk->wall_to_monotonic.tv_nsec;
Arnd Bergmann0df1c982019-07-10 15:01:53 +0200101 vdso_ts->sec += __iter_div_u64_rem(nsec, NSEC_PER_SEC, &vdso_ts->nsec);
Vincenzo Frascino44f57d72019-06-21 10:52:30 +0100102
Thomas Gleixner9f24c542020-01-14 19:52:39 +0100103 /*
104 * Read without the seqlock held by clock_getres().
105 * Note: No need to have a second copy.
106 */
107 WRITE_ONCE(vdata[CS_HRES_COARSE].hrtimer_res, hrtimer_resolution);
108
109 /*
Thomas Gleixnerc7a181002020-02-07 13:39:00 +0100110 * If the current clocksource is not VDSO capable, then spare the
Ingo Molnar4bf07f62021-03-22 22:39:03 +0100111 * update of the high resolution parts.
Thomas Gleixner9f24c542020-01-14 19:52:39 +0100112 */
Thomas Gleixnerc7a181002020-02-07 13:39:00 +0100113 if (clock_mode != VDSO_CLOCKMODE_NONE)
Thomas Gleixner9f24c542020-01-14 19:52:39 +0100114 update_vdso_data(vdata, tk);
Vincenzo Frascino44f57d72019-06-21 10:52:30 +0100115
116 __arch_update_vsyscall(vdata, tk);
117
118 vdso_write_end(vdata);
119
120 __arch_sync_vdso_data(vdata);
121}
122
123void update_vsyscall_tz(void)
124{
125 struct vdso_data *vdata = __arch_get_k_vdso_data();
126
Huacai Chen52338412019-10-24 11:28:29 +0800127 vdata[CS_HRES_COARSE].tz_minuteswest = sys_tz.tz_minuteswest;
128 vdata[CS_HRES_COARSE].tz_dsttime = sys_tz.tz_dsttime;
Vincenzo Frascino44f57d72019-06-21 10:52:30 +0100129
130 __arch_sync_vdso_data(vdata);
131}
Thomas Gleixner19d00702020-08-04 17:01:23 +0200132
133/**
134 * vdso_update_begin - Start of a VDSO update section
135 *
136 * Allows architecture code to safely update the architecture specific VDSO
137 * data. Disables interrupts, acquires timekeeper lock to serialize against
138 * concurrent updates from timekeeping and invalidates the VDSO data
139 * sequence counter to prevent concurrent readers from accessing
140 * inconsistent data.
141 *
142 * Returns: Saved interrupt flags which need to be handed in to
143 * vdso_update_end().
144 */
145unsigned long vdso_update_begin(void)
146{
147 struct vdso_data *vdata = __arch_get_k_vdso_data();
148 unsigned long flags;
149
150 raw_spin_lock_irqsave(&timekeeper_lock, flags);
151 vdso_write_begin(vdata);
152 return flags;
153}
154
155/**
156 * vdso_update_end - End of a VDSO update section
157 * @flags: Interrupt flags as returned from vdso_update_begin()
158 *
159 * Pairs with vdso_update_begin(). Marks vdso data consistent, invokes data
160 * synchronization if the architecture requires it, drops timekeeper lock
161 * and restores interrupt flags.
162 */
163void vdso_update_end(unsigned long flags)
164{
165 struct vdso_data *vdata = __arch_get_k_vdso_data();
166
167 vdso_write_end(vdata);
168 __arch_sync_vdso_data(vdata);
169 raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
170}