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