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