Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 1 | // 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 | |
| 16 | static inline void update_vdso_data(struct vdso_data *vdata, |
| 17 | struct timekeeper *tk) |
| 18 | { |
| 19 | struct vdso_timestamp *vdso_ts; |
Thomas Gleixner | b99328a | 2019-08-22 13:00:15 +0200 | [diff] [blame] | 20 | u64 nsec, sec; |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 21 | |
| 22 | vdata[CS_HRES_COARSE].cycle_last = tk->tkr_mono.cycle_last; |
| 23 | vdata[CS_HRES_COARSE].mask = tk->tkr_mono.mask; |
| 24 | vdata[CS_HRES_COARSE].mult = tk->tkr_mono.mult; |
| 25 | vdata[CS_HRES_COARSE].shift = tk->tkr_mono.shift; |
| 26 | vdata[CS_RAW].cycle_last = tk->tkr_raw.cycle_last; |
| 27 | vdata[CS_RAW].mask = tk->tkr_raw.mask; |
| 28 | vdata[CS_RAW].mult = tk->tkr_raw.mult; |
| 29 | vdata[CS_RAW].shift = tk->tkr_raw.shift; |
| 30 | |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 31 | /* CLOCK_MONOTONIC */ |
| 32 | vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_MONOTONIC]; |
| 33 | vdso_ts->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; |
| 34 | |
| 35 | nsec = tk->tkr_mono.xtime_nsec; |
| 36 | nsec += ((u64)tk->wall_to_monotonic.tv_nsec << tk->tkr_mono.shift); |
| 37 | while (nsec >= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift)) { |
| 38 | nsec -= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift); |
| 39 | vdso_ts->sec++; |
| 40 | } |
| 41 | vdso_ts->nsec = nsec; |
| 42 | |
Thomas Gleixner | b99328a | 2019-08-22 13:00:15 +0200 | [diff] [blame] | 43 | /* Copy MONOTONIC time for BOOTTIME */ |
| 44 | sec = vdso_ts->sec; |
| 45 | /* Add the boot offset */ |
| 46 | sec += tk->monotonic_to_boot.tv_sec; |
| 47 | nsec += (u64)tk->monotonic_to_boot.tv_nsec << tk->tkr_mono.shift; |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 48 | |
| 49 | /* CLOCK_BOOTTIME */ |
| 50 | vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_BOOTTIME]; |
Thomas Gleixner | b99328a | 2019-08-22 13:00:15 +0200 | [diff] [blame] | 51 | vdso_ts->sec = sec; |
| 52 | |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 53 | while (nsec >= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift)) { |
| 54 | nsec -= (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift); |
| 55 | vdso_ts->sec++; |
| 56 | } |
| 57 | vdso_ts->nsec = nsec; |
| 58 | |
Thomas Gleixner | b99328a | 2019-08-22 13:00:15 +0200 | [diff] [blame] | 59 | /* CLOCK_MONOTONIC_RAW */ |
| 60 | vdso_ts = &vdata[CS_RAW].basetime[CLOCK_MONOTONIC_RAW]; |
| 61 | vdso_ts->sec = tk->raw_sec; |
| 62 | vdso_ts->nsec = tk->tkr_raw.xtime_nsec; |
| 63 | |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 64 | /* CLOCK_TAI */ |
| 65 | vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_TAI]; |
| 66 | vdso_ts->sec = tk->xtime_sec + (s64)tk->tai_offset; |
| 67 | vdso_ts->nsec = tk->tkr_mono.xtime_nsec; |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void update_vsyscall(struct timekeeper *tk) |
| 71 | { |
| 72 | struct vdso_data *vdata = __arch_get_k_vdso_data(); |
| 73 | struct vdso_timestamp *vdso_ts; |
| 74 | u64 nsec; |
| 75 | |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 76 | /* copy vsyscall data */ |
| 77 | vdso_write_begin(vdata); |
| 78 | |
| 79 | vdata[CS_HRES_COARSE].clock_mode = __arch_get_clock_mode(tk); |
| 80 | vdata[CS_RAW].clock_mode = __arch_get_clock_mode(tk); |
| 81 | |
Thomas Gleixner | 9f24c54 | 2020-01-14 19:52:39 +0100 | [diff] [blame] | 82 | /* CLOCK_REALTIME also required for time() */ |
| 83 | vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_REALTIME]; |
| 84 | vdso_ts->sec = tk->xtime_sec; |
| 85 | vdso_ts->nsec = tk->tkr_mono.xtime_nsec; |
| 86 | |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 87 | /* CLOCK_REALTIME_COARSE */ |
| 88 | vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_REALTIME_COARSE]; |
| 89 | vdso_ts->sec = tk->xtime_sec; |
| 90 | vdso_ts->nsec = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift; |
| 91 | |
| 92 | /* CLOCK_MONOTONIC_COARSE */ |
| 93 | vdso_ts = &vdata[CS_HRES_COARSE].basetime[CLOCK_MONOTONIC_COARSE]; |
| 94 | vdso_ts->sec = tk->xtime_sec + tk->wall_to_monotonic.tv_sec; |
| 95 | nsec = tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift; |
| 96 | nsec = nsec + tk->wall_to_monotonic.tv_nsec; |
Arnd Bergmann | 0df1c98 | 2019-07-10 15:01:53 +0200 | [diff] [blame] | 97 | vdso_ts->sec += __iter_div_u64_rem(nsec, NSEC_PER_SEC, &vdso_ts->nsec); |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 98 | |
Thomas Gleixner | 9f24c54 | 2020-01-14 19:52:39 +0100 | [diff] [blame] | 99 | /* |
| 100 | * Read without the seqlock held by clock_getres(). |
| 101 | * Note: No need to have a second copy. |
| 102 | */ |
| 103 | WRITE_ONCE(vdata[CS_HRES_COARSE].hrtimer_res, hrtimer_resolution); |
| 104 | |
| 105 | /* |
| 106 | * Architectures can opt out of updating the high resolution part |
| 107 | * of the VDSO. |
| 108 | */ |
| 109 | if (__arch_update_vdso_data()) |
| 110 | update_vdso_data(vdata, tk); |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 111 | |
| 112 | __arch_update_vsyscall(vdata, tk); |
| 113 | |
| 114 | vdso_write_end(vdata); |
| 115 | |
| 116 | __arch_sync_vdso_data(vdata); |
| 117 | } |
| 118 | |
| 119 | void update_vsyscall_tz(void) |
| 120 | { |
| 121 | struct vdso_data *vdata = __arch_get_k_vdso_data(); |
| 122 | |
Huacai Chen | 5233841 | 2019-10-24 11:28:29 +0800 | [diff] [blame] | 123 | vdata[CS_HRES_COARSE].tz_minuteswest = sys_tz.tz_minuteswest; |
| 124 | vdata[CS_HRES_COARSE].tz_dsttime = sys_tz.tz_dsttime; |
Vincenzo Frascino | 44f57d7 | 2019-06-21 10:52:30 +0100 | [diff] [blame] | 125 | |
| 126 | __arch_sync_vdso_data(vdata); |
| 127 | } |