1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1982, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah $Hdr: clock.c 1.18 91/01/21$
37 * from: @(#)clock.c 8.2 (Berkeley) 1/12/94
38 * from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
39 * and
40 * from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
41 */
42
43 #include <sys/cdefs.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/bus.h>
48 #include <sys/clock.h>
49 #include <sys/limits.h>
50 #include <sys/sysctl.h>
51 #include <sys/timetc.h>
52
53 /*
54 * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
55 * namespace because they were misplaced there originally.
56 */
57 static int adjkerntz;
58 static int
sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)59 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
60 {
61 int error;
62 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
63 if (!error && req->newptr)
64 resettodr();
65 return (error);
66 }
67 SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT | CTLFLAG_RW |
68 CTLFLAG_MPSAFE, &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
69 "Local offset from UTC in seconds");
70
71 static int ct_debug;
72 SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RWTUN,
73 &ct_debug, 0, "Enable printing of clocktime debugging");
74
75 static int wall_cmos_clock;
76 SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
77 &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
78
79 /*--------------------------------------------------------------------*
80 * Generic routines to convert between a POSIX date
81 * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
82 * Derived from NetBSD arch/hp300/hp300/clock.c
83 */
84
85 #define FEBRUARY 2
86 #define days_in_year(y) (leapyear(y) ? 366 : 365)
87 #define days_in_month(y, m) \
88 (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
89 /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
90 #define day_of_week(days) (((days) + 4) % 7)
91
92 static const int month_days[12] = {
93 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
94 };
95
96 /*
97 * Optimization: using a precomputed count of days between POSIX_BASE_YEAR and
98 * some recent year avoids lots of unnecessary loop iterations in conversion.
99 * recent_base_days is the number of days before the start of recent_base_year.
100 */
101 static const int recent_base_year = 2017;
102 static const int recent_base_days = 17167;
103
104 /*
105 * Table to 'calculate' pow(10, 9 - nsdigits) via lookup of nsdigits.
106 * Before doing the lookup, the code asserts 0 <= nsdigits <= 9.
107 */
108 static u_int nsdivisors[] = {
109 1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1
110 };
111
112 /*
113 * This inline avoids some unnecessary modulo operations
114 * as compared with the usual macro:
115 * ( ((year % 4) == 0 &&
116 * (year % 100) != 0) ||
117 * ((year % 400) == 0) )
118 * It is otherwise equivalent.
119 */
120 static int
leapyear(int year)121 leapyear(int year)
122 {
123 int rv = 0;
124
125 if ((year & 3) == 0) {
126 rv = 1;
127 if ((year % 100) == 0) {
128 rv = 0;
129 if ((year % 400) == 0)
130 rv = 1;
131 }
132 }
133 return (rv);
134 }
135
136 int
clock_ct_to_ts(const struct clocktime * ct,struct timespec * ts)137 clock_ct_to_ts(const struct clocktime *ct, struct timespec *ts)
138 {
139 int i, year, days;
140
141 if (ct_debug) {
142 printf("ct_to_ts([");
143 clock_print_ct(ct, 9);
144 printf("])");
145 }
146
147 /*
148 * Many realtime clocks store the year as 2-digit BCD; pivot on 70 to
149 * determine century. Some clocks have a "century bit" and drivers do
150 * year += 100, so interpret values between 70-199 as relative to 1900.
151 */
152 year = ct->year;
153 if (year < 70)
154 year += 2000;
155 else if (year < 200)
156 year += 1900;
157
158 /* Sanity checks. */
159 if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
160 ct->day > days_in_month(year, ct->mon) ||
161 ct->hour > 23 || ct->min > 59 || ct->sec > 59 || year < 1970 ||
162 (sizeof(time_t) == 4 && year > 2037)) { /* time_t overflow */
163 if (ct_debug)
164 printf(" = EINVAL\n");
165 return (EINVAL);
166 }
167
168 /*
169 * Compute days since start of time
170 * First from years, then from months.
171 */
172 if (year >= recent_base_year) {
173 i = recent_base_year;
174 days = recent_base_days;
175 } else {
176 i = POSIX_BASE_YEAR;
177 days = 0;
178 }
179 for (; i < year; i++)
180 days += days_in_year(i);
181
182 /* Months */
183 for (i = 1; i < ct->mon; i++)
184 days += days_in_month(year, i);
185 days += (ct->day - 1);
186
187 ts->tv_sec = (((time_t)days * 24 + ct->hour) * 60 + ct->min) * 60 +
188 ct->sec;
189 ts->tv_nsec = ct->nsec;
190
191 if (ct_debug)
192 printf(" = %jd.%09ld\n", (intmax_t)ts->tv_sec, ts->tv_nsec);
193 return (0);
194 }
195
196 int
clock_bcd_to_ts(const struct bcd_clocktime * bct,struct timespec * ts,bool ampm)197 clock_bcd_to_ts(const struct bcd_clocktime *bct, struct timespec *ts, bool ampm)
198 {
199 struct clocktime ct;
200 int bcent, byear;
201
202 /*
203 * Year may come in as 2-digit or 4-digit BCD. Split the value into
204 * separate BCD century and year values for validation and conversion.
205 */
206 bcent = bct->year >> 8;
207 byear = bct->year & 0xff;
208
209 /*
210 * Ensure that all values are valid BCD numbers, to avoid assertions in
211 * the BCD-to-binary conversion routines. clock_ct_to_ts() will further
212 * validate the field ranges (such as 0 <= min <= 59) during conversion.
213 */
214 if (!validbcd(bcent) || !validbcd(byear) || !validbcd(bct->mon) ||
215 !validbcd(bct->day) || !validbcd(bct->hour) ||
216 !validbcd(bct->min) || !validbcd(bct->sec)) {
217 if (ct_debug)
218 printf("clock_bcd_to_ts: bad BCD: "
219 "[%04x-%02x-%02x %02x:%02x:%02x]\n",
220 bct->year, bct->mon, bct->day,
221 bct->hour, bct->min, bct->sec);
222 return (EINVAL);
223 }
224
225 ct.year = FROMBCD(byear) + FROMBCD(bcent) * 100;
226 ct.mon = FROMBCD(bct->mon);
227 ct.day = FROMBCD(bct->day);
228 ct.hour = FROMBCD(bct->hour);
229 ct.min = FROMBCD(bct->min);
230 ct.sec = FROMBCD(bct->sec);
231 ct.dow = bct->dow;
232 ct.nsec = bct->nsec;
233
234 /* If asked to handle am/pm, convert from 12hr+pmflag to 24hr. */
235 if (ampm) {
236 if (ct.hour == 12)
237 ct.hour = 0;
238 if (bct->ispm)
239 ct.hour += 12;
240 }
241
242 return (clock_ct_to_ts(&ct, ts));
243 }
244
245 void
clock_ts_to_ct(const struct timespec * ts,struct clocktime * ct)246 clock_ts_to_ct(const struct timespec *ts, struct clocktime *ct)
247 {
248 time_t i, year, days;
249 time_t rsec; /* remainder seconds */
250 time_t secs;
251
252 secs = ts->tv_sec;
253 days = secs / SECDAY;
254 rsec = secs % SECDAY;
255
256 ct->dow = day_of_week(days);
257
258 /* Subtract out whole years. */
259 if (days >= recent_base_days) {
260 year = recent_base_year;
261 days -= recent_base_days;
262 } else {
263 year = POSIX_BASE_YEAR;
264 }
265 for (; days >= days_in_year(year); year++)
266 days -= days_in_year(year);
267 ct->year = year;
268
269 /* Subtract out whole months, counting them in i. */
270 for (i = 1; days >= days_in_month(year, i); i++)
271 days -= days_in_month(year, i);
272 ct->mon = i;
273
274 /* Days are what is left over (+1) from all that. */
275 ct->day = days + 1;
276
277 /* Hours, minutes, seconds are easy */
278 ct->hour = rsec / 3600;
279 rsec = rsec % 3600;
280 ct->min = rsec / 60;
281 rsec = rsec % 60;
282 ct->sec = rsec;
283 ct->nsec = ts->tv_nsec;
284 if (ct_debug) {
285 printf("ts_to_ct(%jd.%09ld) = [",
286 (intmax_t)ts->tv_sec, ts->tv_nsec);
287 clock_print_ct(ct, 9);
288 printf("]\n");
289 }
290
291 KASSERT(ct->year >= 0 && ct->year < 10000,
292 ("year %d isn't a 4 digit year", ct->year));
293 KASSERT(ct->mon >= 1 && ct->mon <= 12,
294 ("month %d not in 1-12", ct->mon));
295 KASSERT(ct->day >= 1 && ct->day <= 31,
296 ("day %d not in 1-31", ct->day));
297 KASSERT(ct->hour >= 0 && ct->hour <= 23,
298 ("hour %d not in 0-23", ct->hour));
299 KASSERT(ct->min >= 0 && ct->min <= 59,
300 ("minute %d not in 0-59", ct->min));
301 /* Not sure if this interface needs to handle leapseconds or not. */
302 KASSERT(ct->sec >= 0 && ct->sec <= 60,
303 ("seconds %d not in 0-60", ct->sec));
304 }
305
306 void
clock_ts_to_bcd(const struct timespec * ts,struct bcd_clocktime * bct,bool ampm)307 clock_ts_to_bcd(const struct timespec *ts, struct bcd_clocktime *bct, bool ampm)
308 {
309 struct clocktime ct;
310
311 clock_ts_to_ct(ts, &ct);
312
313 /* If asked to handle am/pm, convert from 24hr to 12hr+pmflag. */
314 bct->ispm = false;
315 if (ampm) {
316 if (ct.hour >= 12) {
317 ct.hour -= 12;
318 bct->ispm = true;
319 }
320 if (ct.hour == 0)
321 ct.hour = 12;
322 }
323
324 bct->year = TOBCD(ct.year % 100) | (TOBCD(ct.year / 100) << 8);
325 bct->mon = TOBCD(ct.mon);
326 bct->day = TOBCD(ct.day);
327 bct->hour = TOBCD(ct.hour);
328 bct->min = TOBCD(ct.min);
329 bct->sec = TOBCD(ct.sec);
330 bct->dow = ct.dow;
331 bct->nsec = ct.nsec;
332 }
333
334 void
clock_print_bcd(const struct bcd_clocktime * bct,int nsdigits)335 clock_print_bcd(const struct bcd_clocktime *bct, int nsdigits)
336 {
337
338 KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
339
340 if (nsdigits > 0) {
341 printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x.%*.*ld",
342 bct->year, bct->mon, bct->day,
343 bct->hour, bct->min, bct->sec,
344 nsdigits, nsdigits, bct->nsec / nsdivisors[nsdigits]);
345 } else {
346 printf("%4.4x-%2.2x-%2.2x %2.2x:%2.2x:%2.2x",
347 bct->year, bct->mon, bct->day,
348 bct->hour, bct->min, bct->sec);
349 }
350 }
351
352 void
clock_print_ct(const struct clocktime * ct,int nsdigits)353 clock_print_ct(const struct clocktime *ct, int nsdigits)
354 {
355
356 KASSERT(nsdigits >= 0 && nsdigits <= 9, ("bad nsdigits %d", nsdigits));
357
358 if (nsdigits > 0) {
359 printf("%04d-%02d-%02d %02d:%02d:%02d.%*.*ld",
360 ct->year, ct->mon, ct->day,
361 ct->hour, ct->min, ct->sec,
362 nsdigits, nsdigits, ct->nsec / nsdivisors[nsdigits]);
363 } else {
364 printf("%04d-%02d-%02d %02d:%02d:%02d",
365 ct->year, ct->mon, ct->day,
366 ct->hour, ct->min, ct->sec);
367 }
368 }
369
370 void
clock_print_ts(const struct timespec * ts,int nsdigits)371 clock_print_ts(const struct timespec *ts, int nsdigits)
372 {
373 struct clocktime ct;
374
375 clock_ts_to_ct(ts, &ct);
376 clock_print_ct(&ct, nsdigits);
377 }
378
379 int
utc_offset(void)380 utc_offset(void)
381 {
382
383 return (wall_cmos_clock ? adjkerntz : 0);
384 }
385