Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 1 | #ifndef _LINUX_TIME32_H |
| 2 | #define _LINUX_TIME32_H |
| 3 | /* |
| 4 | * These are all interfaces based on the old time_t definition |
| 5 | * that overflows in 2038 on 32-bit architectures. New code |
| 6 | * should use the replacements based on time64_t and timespec64. |
| 7 | * |
| 8 | * Any interfaces in here that become unused as we migrate |
| 9 | * code to time64_t should get removed. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/time64.h> |
Arnd Bergmann | 4d5f007 | 2019-01-02 13:28:47 +0100 | [diff] [blame^] | 13 | #include <linux/timex.h> |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 14 | |
| 15 | #define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1) |
| 16 | |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 17 | typedef s32 old_time32_t; |
| 18 | |
| 19 | struct old_timespec32 { |
| 20 | old_time32_t tv_sec; |
| 21 | s32 tv_nsec; |
| 22 | }; |
| 23 | |
| 24 | struct old_timeval32 { |
| 25 | old_time32_t tv_sec; |
| 26 | s32 tv_usec; |
| 27 | }; |
| 28 | |
| 29 | struct old_itimerspec32 { |
| 30 | struct old_timespec32 it_interval; |
| 31 | struct old_timespec32 it_value; |
| 32 | }; |
| 33 | |
Arnd Bergmann | 4faea23 | 2018-04-17 12:03:19 +0200 | [diff] [blame] | 34 | struct old_utimbuf32 { |
| 35 | old_time32_t actime; |
| 36 | old_time32_t modtime; |
| 37 | }; |
| 38 | |
Arnd Bergmann | 4d5f007 | 2019-01-02 13:28:47 +0100 | [diff] [blame^] | 39 | struct old_timex32 { |
| 40 | u32 modes; |
| 41 | s32 offset; |
| 42 | s32 freq; |
| 43 | s32 maxerror; |
| 44 | s32 esterror; |
| 45 | s32 status; |
| 46 | s32 constant; |
| 47 | s32 precision; |
| 48 | s32 tolerance; |
| 49 | struct old_timeval32 time; |
| 50 | s32 tick; |
| 51 | s32 ppsfreq; |
| 52 | s32 jitter; |
| 53 | s32 shift; |
| 54 | s32 stabil; |
| 55 | s32 jitcnt; |
| 56 | s32 calcnt; |
| 57 | s32 errcnt; |
| 58 | s32 stbcnt; |
| 59 | s32 tai; |
| 60 | |
| 61 | s32:32; s32:32; s32:32; s32:32; |
| 62 | s32:32; s32:32; s32:32; s32:32; |
| 63 | s32:32; s32:32; s32:32; |
| 64 | }; |
| 65 | |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 66 | extern int get_old_timespec32(struct timespec64 *, const void __user *); |
| 67 | extern int put_old_timespec32(const struct timespec64 *, void __user *); |
| 68 | extern int get_old_itimerspec32(struct itimerspec64 *its, |
| 69 | const struct old_itimerspec32 __user *uits); |
| 70 | extern int put_old_itimerspec32(const struct itimerspec64 *its, |
| 71 | struct old_itimerspec32 __user *uits); |
Arnd Bergmann | 4d5f007 | 2019-01-02 13:28:47 +0100 | [diff] [blame^] | 72 | struct timex; |
| 73 | int get_old_timex32(struct timex *, const struct old_timex32 __user *); |
| 74 | int put_old_timex32(struct old_timex32 __user *, const struct timex *); |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 75 | |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 76 | #if __BITS_PER_LONG == 64 |
| 77 | |
| 78 | /* timespec64 is defined as timespec here */ |
| 79 | static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64) |
| 80 | { |
Arnd Bergmann | 4f0fad9 | 2018-04-27 15:40:12 +0200 | [diff] [blame] | 81 | return *(const struct timespec *)&ts64; |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static inline struct timespec64 timespec_to_timespec64(const struct timespec ts) |
| 85 | { |
Arnd Bergmann | 4f0fad9 | 2018-04-27 15:40:12 +0200 | [diff] [blame] | 86 | return *(const struct timespec64 *)&ts; |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 89 | #else |
| 90 | static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64) |
| 91 | { |
| 92 | struct timespec ret; |
| 93 | |
| 94 | ret.tv_sec = (time_t)ts64.tv_sec; |
| 95 | ret.tv_nsec = ts64.tv_nsec; |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static inline struct timespec64 timespec_to_timespec64(const struct timespec ts) |
| 100 | { |
| 101 | struct timespec64 ret; |
| 102 | |
| 103 | ret.tv_sec = ts.tv_sec; |
| 104 | ret.tv_nsec = ts.tv_nsec; |
| 105 | return ret; |
| 106 | } |
Arnd Bergmann | 4f0fad9 | 2018-04-27 15:40:12 +0200 | [diff] [blame] | 107 | #endif |
Arnd Bergmann | abc8f96 | 2017-10-19 13:14:48 +0200 | [diff] [blame] | 108 | |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 109 | static inline int timespec_equal(const struct timespec *a, |
| 110 | const struct timespec *b) |
| 111 | { |
| 112 | return (a->tv_sec == b->tv_sec) && (a->tv_nsec == b->tv_nsec); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * lhs < rhs: return <0 |
| 117 | * lhs == rhs: return 0 |
| 118 | * lhs > rhs: return >0 |
| 119 | */ |
| 120 | static inline int timespec_compare(const struct timespec *lhs, const struct timespec *rhs) |
| 121 | { |
| 122 | if (lhs->tv_sec < rhs->tv_sec) |
| 123 | return -1; |
| 124 | if (lhs->tv_sec > rhs->tv_sec) |
| 125 | return 1; |
| 126 | return lhs->tv_nsec - rhs->tv_nsec; |
| 127 | } |
| 128 | |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 129 | /* |
| 130 | * Returns true if the timespec is norm, false if denorm: |
| 131 | */ |
| 132 | static inline bool timespec_valid(const struct timespec *ts) |
| 133 | { |
| 134 | /* Dates before 1970 are bogus */ |
| 135 | if (ts->tv_sec < 0) |
| 136 | return false; |
| 137 | /* Can't have more nanoseconds then a second */ |
| 138 | if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC) |
| 139 | return false; |
| 140 | return true; |
| 141 | } |
| 142 | |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 143 | /** |
| 144 | * timespec_to_ns - Convert timespec to nanoseconds |
| 145 | * @ts: pointer to the timespec variable to be converted |
| 146 | * |
| 147 | * Returns the scalar nanosecond representation of the timespec |
| 148 | * parameter. |
| 149 | */ |
| 150 | static inline s64 timespec_to_ns(const struct timespec *ts) |
| 151 | { |
| 152 | return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * ns_to_timespec - Convert nanoseconds to timespec |
| 157 | * @nsec: the nanoseconds value to be converted |
| 158 | * |
| 159 | * Returns the timespec representation of the nsec parameter. |
| 160 | */ |
| 161 | extern struct timespec ns_to_timespec(const s64 nsec); |
| 162 | |
| 163 | /** |
| 164 | * timespec_add_ns - Adds nanoseconds to a timespec |
| 165 | * @a: pointer to timespec to be incremented |
| 166 | * @ns: unsigned nanoseconds value to be added |
| 167 | * |
| 168 | * This must always be inlined because its used from the x86-64 vdso, |
| 169 | * which cannot call other kernel functions. |
| 170 | */ |
| 171 | static __always_inline void timespec_add_ns(struct timespec *a, u64 ns) |
| 172 | { |
| 173 | a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); |
| 174 | a->tv_nsec = ns; |
| 175 | } |
| 176 | |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 177 | static inline unsigned long mktime(const unsigned int year, |
| 178 | const unsigned int mon, const unsigned int day, |
| 179 | const unsigned int hour, const unsigned int min, |
| 180 | const unsigned int sec) |
| 181 | { |
| 182 | return mktime64(year, mon, day, hour, min, sec); |
| 183 | } |
| 184 | |
| 185 | static inline bool timeval_valid(const struct timeval *tv) |
| 186 | { |
| 187 | /* Dates before 1970 are bogus */ |
| 188 | if (tv->tv_sec < 0) |
| 189 | return false; |
| 190 | |
| 191 | /* Can't have more microseconds then a second */ |
| 192 | if (tv->tv_usec < 0 || tv->tv_usec >= USEC_PER_SEC) |
| 193 | return false; |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 198 | /** |
| 199 | * timeval_to_ns - Convert timeval to nanoseconds |
| 200 | * @ts: pointer to the timeval variable to be converted |
| 201 | * |
| 202 | * Returns the scalar nanosecond representation of the timeval |
| 203 | * parameter. |
| 204 | */ |
| 205 | static inline s64 timeval_to_ns(const struct timeval *tv) |
| 206 | { |
| 207 | return ((s64) tv->tv_sec * NSEC_PER_SEC) + |
| 208 | tv->tv_usec * NSEC_PER_USEC; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * ns_to_timeval - Convert nanoseconds to timeval |
| 213 | * @nsec: the nanoseconds value to be converted |
| 214 | * |
| 215 | * Returns the timeval representation of the nsec parameter. |
| 216 | */ |
| 217 | extern struct timeval ns_to_timeval(const s64 nsec); |
Arnd Bergmann | a84d116 | 2018-03-15 17:12:40 +0100 | [diff] [blame] | 218 | extern struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec); |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 219 | |
Arnd Bergmann | fd991a2 | 2018-08-21 22:33:00 +0200 | [diff] [blame] | 220 | /* |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 221 | * Old names for the 32-bit time_t interfaces, these will be removed |
| 222 | * when everything uses the new names. |
Arnd Bergmann | fd991a2 | 2018-08-21 22:33:00 +0200 | [diff] [blame] | 223 | */ |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 224 | #define compat_time_t old_time32_t |
| 225 | #define compat_timeval old_timeval32 |
| 226 | #define compat_timespec old_timespec32 |
| 227 | #define compat_itimerspec old_itimerspec32 |
| 228 | #define ns_to_compat_timeval ns_to_old_timeval32 |
| 229 | #define get_compat_itimerspec64 get_old_itimerspec32 |
| 230 | #define put_compat_itimerspec64 put_old_itimerspec32 |
| 231 | #define compat_get_timespec64 get_old_timespec32 |
| 232 | #define compat_put_timespec64 put_old_timespec32 |
Arnd Bergmann | fd991a2 | 2018-08-21 22:33:00 +0200 | [diff] [blame] | 233 | |
Arnd Bergmann | 5dbf201 | 2017-10-19 13:14:47 +0200 | [diff] [blame] | 234 | #endif |