xref: /netbsd/sys/kern/kern_time.c (revision 82bb7a92)
1 /*	$NetBSD: kern_time.c,v 1.221 2023/02/23 02:57:17 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2004, 2005, 2007, 2008, 2009, 2020
5  *     The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Christopher G. Demetriou, by Andrew Doran, and by Jason R. Thorpe.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1989, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)kern_time.c	8.4 (Berkeley) 5/26/95
62  */
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.221 2023/02/23 02:57:17 riastradh Exp $");
66 
67 #include <sys/param.h>
68 #include <sys/resourcevar.h>
69 #include <sys/kernel.h>
70 #include <sys/systm.h>
71 #include <sys/proc.h>
72 #include <sys/vnode.h>
73 #include <sys/signalvar.h>
74 #include <sys/syslog.h>
75 #include <sys/timetc.h>
76 #include <sys/timevar.h>
77 #include <sys/timex.h>
78 #include <sys/kauth.h>
79 #include <sys/mount.h>
80 #include <sys/syscallargs.h>
81 #include <sys/cpu.h>
82 
83 kmutex_t	itimer_mutex __cacheline_aligned;	/* XXX static */
84 static struct itlist itimer_realtime_changed_notify;
85 
86 static void	itimer_callout(void *);
87 static void	ptimer_intr(void *);
88 static void	*ptimer_sih __read_mostly;
89 static TAILQ_HEAD(, ptimer) ptimer_queue;
90 
91 #define	CLOCK_VIRTUAL_P(clockid)	\
92 	((clockid) == CLOCK_VIRTUAL || (clockid) == CLOCK_PROF)
93 
94 CTASSERT(ITIMER_REAL == CLOCK_REALTIME);
95 CTASSERT(ITIMER_VIRTUAL == CLOCK_VIRTUAL);
96 CTASSERT(ITIMER_PROF == CLOCK_PROF);
97 CTASSERT(ITIMER_MONOTONIC == CLOCK_MONOTONIC);
98 
99 #define	DELAYTIMER_MAX	32
100 
101 /*
102  * Initialize timekeeping.
103  */
104 void
time_init(void)105 time_init(void)
106 {
107 
108 	mutex_init(&itimer_mutex, MUTEX_DEFAULT, IPL_SCHED);
109 	LIST_INIT(&itimer_realtime_changed_notify);
110 
111 	TAILQ_INIT(&ptimer_queue);
112 	ptimer_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
113 	    ptimer_intr, NULL);
114 }
115 
116 /*
117  * Check if the time will wrap if set to ts.
118  *
119  * ts - timespec describing the new time
120  * delta - the delta between the current time and ts
121  */
122 bool
time_wraps(struct timespec * ts,struct timespec * delta)123 time_wraps(struct timespec *ts, struct timespec *delta)
124 {
125 
126 	/*
127 	 * Don't allow the time to be set forward so far it
128 	 * will wrap and become negative, thus allowing an
129 	 * attacker to bypass the next check below.  The
130 	 * cutoff is 1 year before rollover occurs, so even
131 	 * if the attacker uses adjtime(2) to move the time
132 	 * past the cutoff, it will take a very long time
133 	 * to get to the wrap point.
134 	 */
135 	if ((ts->tv_sec > LLONG_MAX - 365*24*60*60) ||
136 	    (delta->tv_sec < 0 || delta->tv_nsec < 0))
137 		return true;
138 
139 	return false;
140 }
141 
142 /*
143  * itimer_lock:
144  *
145  *	Acquire the interval timer data lock.
146  */
147 void
itimer_lock(void)148 itimer_lock(void)
149 {
150 	mutex_spin_enter(&itimer_mutex);
151 }
152 
153 /*
154  * itimer_unlock:
155  *
156  *	Release the interval timer data lock.
157  */
158 void
itimer_unlock(void)159 itimer_unlock(void)
160 {
161 	mutex_spin_exit(&itimer_mutex);
162 }
163 
164 /*
165  * itimer_lock_held:
166  *
167  *	Check that the interval timer lock is held for diagnostic
168  *	assertions.
169  */
170 inline bool __diagused
itimer_lock_held(void)171 itimer_lock_held(void)
172 {
173 	return mutex_owned(&itimer_mutex);
174 }
175 
176 /*
177  * Time of day and interval timer support.
178  *
179  * These routines provide the kernel entry points to get and set
180  * the time-of-day and per-process interval timers.  Subroutines
181  * here provide support for adding and subtracting timeval structures
182  * and decrementing interval timers, optionally reloading the interval
183  * timers when they expire.
184  */
185 
186 /* This function is used by clock_settime and settimeofday */
187 static int
settime1(struct proc * p,const struct timespec * ts,bool check_kauth)188 settime1(struct proc *p, const struct timespec *ts, bool check_kauth)
189 {
190 	struct timespec delta, now;
191 
192 	/*
193 	 * The time being set to an unreasonable value will cause
194 	 * unreasonable system behaviour.
195 	 */
196 	if (ts->tv_sec < 0 || ts->tv_sec > (1LL << 36))
197 		return EINVAL;
198 
199 	nanotime(&now);
200 	timespecsub(ts, &now, &delta);
201 
202 	if (check_kauth && kauth_authorize_system(kauth_cred_get(),
203 	    KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, __UNCONST(ts),
204 	    &delta, KAUTH_ARG(check_kauth ? false : true)) != 0) {
205 		return EPERM;
206 	}
207 
208 #ifdef notyet
209 	if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */
210 		return EPERM;
211 	}
212 #endif
213 
214 	tc_setclock(ts);
215 
216 	resettodr();
217 
218 	/*
219 	 * Notify pending CLOCK_REALTIME timers about the real time change.
220 	 * There may be inactive timers on this list, but this happens
221 	 * comparatively less often than timers firing, and so it's better
222 	 * to put the extra checks here than to complicate the other code
223 	 * path.
224 	 */
225 	struct itimer *it;
226 	itimer_lock();
227 	LIST_FOREACH(it, &itimer_realtime_changed_notify, it_rtchgq) {
228 		KASSERT(it->it_ops->ito_realtime_changed != NULL);
229 		if (timespecisset(&it->it_time.it_value)) {
230 			(*it->it_ops->ito_realtime_changed)(it);
231 		}
232 	}
233 	itimer_unlock();
234 
235 	return 0;
236 }
237 
238 int
settime(struct proc * p,struct timespec * ts)239 settime(struct proc *p, struct timespec *ts)
240 {
241 	return settime1(p, ts, true);
242 }
243 
244 /* ARGSUSED */
245 int
sys___clock_gettime50(struct lwp * l,const struct sys___clock_gettime50_args * uap,register_t * retval)246 sys___clock_gettime50(struct lwp *l,
247     const struct sys___clock_gettime50_args *uap, register_t *retval)
248 {
249 	/* {
250 		syscallarg(clockid_t) clock_id;
251 		syscallarg(struct timespec *) tp;
252 	} */
253 	int error;
254 	struct timespec ats;
255 
256 	error = clock_gettime1(SCARG(uap, clock_id), &ats);
257 	if (error != 0)
258 		return error;
259 
260 	return copyout(&ats, SCARG(uap, tp), sizeof(ats));
261 }
262 
263 /* ARGSUSED */
264 int
sys___clock_settime50(struct lwp * l,const struct sys___clock_settime50_args * uap,register_t * retval)265 sys___clock_settime50(struct lwp *l,
266     const struct sys___clock_settime50_args *uap, register_t *retval)
267 {
268 	/* {
269 		syscallarg(clockid_t) clock_id;
270 		syscallarg(const struct timespec *) tp;
271 	} */
272 	int error;
273 	struct timespec ats;
274 
275 	if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
276 		return error;
277 
278 	return clock_settime1(l->l_proc, SCARG(uap, clock_id), &ats, true);
279 }
280 
281 
282 int
clock_settime1(struct proc * p,clockid_t clock_id,const struct timespec * tp,bool check_kauth)283 clock_settime1(struct proc *p, clockid_t clock_id, const struct timespec *tp,
284     bool check_kauth)
285 {
286 	int error;
287 
288 	if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000L)
289 		return EINVAL;
290 
291 	switch (clock_id) {
292 	case CLOCK_REALTIME:
293 		if ((error = settime1(p, tp, check_kauth)) != 0)
294 			return error;
295 		break;
296 	case CLOCK_MONOTONIC:
297 		return EINVAL;	/* read-only clock */
298 	default:
299 		return EINVAL;
300 	}
301 
302 	return 0;
303 }
304 
305 int
sys___clock_getres50(struct lwp * l,const struct sys___clock_getres50_args * uap,register_t * retval)306 sys___clock_getres50(struct lwp *l, const struct sys___clock_getres50_args *uap,
307     register_t *retval)
308 {
309 	/* {
310 		syscallarg(clockid_t) clock_id;
311 		syscallarg(struct timespec *) tp;
312 	} */
313 	struct timespec ts;
314 	int error;
315 
316 	if ((error = clock_getres1(SCARG(uap, clock_id), &ts)) != 0)
317 		return error;
318 
319 	if (SCARG(uap, tp))
320 		error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
321 
322 	return error;
323 }
324 
325 int
clock_getres1(clockid_t clock_id,struct timespec * ts)326 clock_getres1(clockid_t clock_id, struct timespec *ts)
327 {
328 
329 	switch (clock_id) {
330 	case CLOCK_REALTIME:
331 	case CLOCK_MONOTONIC:
332 		ts->tv_sec = 0;
333 		if (tc_getfrequency() > 1000000000)
334 			ts->tv_nsec = 1;
335 		else
336 			ts->tv_nsec = 1000000000 / tc_getfrequency();
337 		break;
338 	default:
339 		return EINVAL;
340 	}
341 
342 	return 0;
343 }
344 
345 /* ARGSUSED */
346 int
sys___nanosleep50(struct lwp * l,const struct sys___nanosleep50_args * uap,register_t * retval)347 sys___nanosleep50(struct lwp *l, const struct sys___nanosleep50_args *uap,
348     register_t *retval)
349 {
350 	/* {
351 		syscallarg(struct timespec *) rqtp;
352 		syscallarg(struct timespec *) rmtp;
353 	} */
354 	struct timespec rmt, rqt;
355 	int error, error1;
356 
357 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
358 	if (error)
359 		return error;
360 
361 	error = nanosleep1(l, CLOCK_MONOTONIC, 0, &rqt,
362 	    SCARG(uap, rmtp) ? &rmt : NULL);
363 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
364 		return error;
365 
366 	error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt));
367 	return error1 ? error1 : error;
368 }
369 
370 /* ARGSUSED */
371 int
sys_clock_nanosleep(struct lwp * l,const struct sys_clock_nanosleep_args * uap,register_t * retval)372 sys_clock_nanosleep(struct lwp *l, const struct sys_clock_nanosleep_args *uap,
373     register_t *retval)
374 {
375 	/* {
376 		syscallarg(clockid_t) clock_id;
377 		syscallarg(int) flags;
378 		syscallarg(struct timespec *) rqtp;
379 		syscallarg(struct timespec *) rmtp;
380 	} */
381 	struct timespec rmt, rqt;
382 	int error, error1;
383 
384 	error = copyin(SCARG(uap, rqtp), &rqt, sizeof(struct timespec));
385 	if (error)
386 		goto out;
387 
388 	error = nanosleep1(l, SCARG(uap, clock_id), SCARG(uap, flags), &rqt,
389 	    SCARG(uap, rmtp) ? &rmt : NULL);
390 	if (SCARG(uap, rmtp) == NULL || (error != 0 && error != EINTR))
391 		goto out;
392 
393 	if ((SCARG(uap, flags) & TIMER_ABSTIME) == 0 &&
394 	    (error1 = copyout(&rmt, SCARG(uap, rmtp), sizeof(rmt))) != 0)
395 		error = error1;
396 out:
397 	*retval = error;
398 	return 0;
399 }
400 
401 int
nanosleep1(struct lwp * l,clockid_t clock_id,int flags,struct timespec * rqt,struct timespec * rmt)402 nanosleep1(struct lwp *l, clockid_t clock_id, int flags, struct timespec *rqt,
403     struct timespec *rmt)
404 {
405 	struct timespec rmtstart;
406 	int error, timo;
407 
408 	if ((error = ts2timo(clock_id, flags, rqt, &timo, &rmtstart)) != 0) {
409 		if (error == ETIMEDOUT) {
410 			error = 0;
411 			if (rmt != NULL)
412 				rmt->tv_sec = rmt->tv_nsec = 0;
413 		}
414 		return error;
415 	}
416 
417 	/*
418 	 * Avoid inadvertently sleeping forever
419 	 */
420 	if (timo == 0)
421 		timo = 1;
422 again:
423 	error = kpause("nanoslp", true, timo, NULL);
424 	if (error == EWOULDBLOCK)
425 		error = 0;
426 	if (rmt != NULL || error == 0) {
427 		struct timespec rmtend;
428 		struct timespec t0;
429 		struct timespec *t;
430 		int err;
431 
432 		err = clock_gettime1(clock_id, &rmtend);
433 		if (err != 0)
434 			return err;
435 
436 		t = (rmt != NULL) ? rmt : &t0;
437 		if (flags & TIMER_ABSTIME) {
438 			timespecsub(rqt, &rmtend, t);
439 		} else {
440 			if (timespeccmp(&rmtend, &rmtstart, <))
441 				timespecclear(t); /* clock wound back */
442 			else
443 				timespecsub(&rmtend, &rmtstart, t);
444 			if (timespeccmp(rqt, t, <))
445 				timespecclear(t);
446 			else
447 				timespecsub(rqt, t, t);
448 		}
449 		if (t->tv_sec < 0)
450 			timespecclear(t);
451 		if (error == 0) {
452 			timo = tstohz(t);
453 			if (timo > 0)
454 				goto again;
455 		}
456 	}
457 
458 	if (error == ERESTART)
459 		error = EINTR;
460 
461 	return error;
462 }
463 
464 int
sys_clock_getcpuclockid2(struct lwp * l,const struct sys_clock_getcpuclockid2_args * uap,register_t * retval)465 sys_clock_getcpuclockid2(struct lwp *l,
466     const struct sys_clock_getcpuclockid2_args *uap,
467     register_t *retval)
468 {
469 	/* {
470 		syscallarg(idtype_t idtype;
471 		syscallarg(id_t id);
472 		syscallarg(clockid_t *)clock_id;
473 	} */
474 	pid_t pid;
475 	lwpid_t lid;
476 	clockid_t clock_id;
477 	id_t id = SCARG(uap, id);
478 
479 	switch (SCARG(uap, idtype)) {
480 	case P_PID:
481 		pid = id == 0 ? l->l_proc->p_pid : id;
482 		clock_id = CLOCK_PROCESS_CPUTIME_ID | pid;
483 		break;
484 	case P_LWPID:
485 		lid = id == 0 ? l->l_lid : id;
486 		clock_id = CLOCK_THREAD_CPUTIME_ID | lid;
487 		break;
488 	default:
489 		return EINVAL;
490 	}
491 	return copyout(&clock_id, SCARG(uap, clock_id), sizeof(clock_id));
492 }
493 
494 /* ARGSUSED */
495 int
sys___gettimeofday50(struct lwp * l,const struct sys___gettimeofday50_args * uap,register_t * retval)496 sys___gettimeofday50(struct lwp *l, const struct sys___gettimeofday50_args *uap,
497     register_t *retval)
498 {
499 	/* {
500 		syscallarg(struct timeval *) tp;
501 		syscallarg(void *) tzp;		really "struct timezone *";
502 	} */
503 	struct timeval atv;
504 	int error = 0;
505 	struct timezone tzfake;
506 
507 	if (SCARG(uap, tp)) {
508 		memset(&atv, 0, sizeof(atv));
509 		microtime(&atv);
510 		error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
511 		if (error)
512 			return error;
513 	}
514 	if (SCARG(uap, tzp)) {
515 		/*
516 		 * NetBSD has no kernel notion of time zone, so we just
517 		 * fake up a timezone struct and return it if demanded.
518 		 */
519 		tzfake.tz_minuteswest = 0;
520 		tzfake.tz_dsttime = 0;
521 		error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
522 	}
523 	return error;
524 }
525 
526 /* ARGSUSED */
527 int
sys___settimeofday50(struct lwp * l,const struct sys___settimeofday50_args * uap,register_t * retval)528 sys___settimeofday50(struct lwp *l, const struct sys___settimeofday50_args *uap,
529     register_t *retval)
530 {
531 	/* {
532 		syscallarg(const struct timeval *) tv;
533 		syscallarg(const void *) tzp; really "const struct timezone *";
534 	} */
535 
536 	return settimeofday1(SCARG(uap, tv), true, SCARG(uap, tzp), l, true);
537 }
538 
539 int
settimeofday1(const struct timeval * utv,bool userspace,const void * utzp,struct lwp * l,bool check_kauth)540 settimeofday1(const struct timeval *utv, bool userspace,
541     const void *utzp, struct lwp *l, bool check_kauth)
542 {
543 	struct timeval atv;
544 	struct timespec ts;
545 	int error;
546 
547 	/* Verify all parameters before changing time. */
548 
549 	/*
550 	 * NetBSD has no kernel notion of time zone, and only an
551 	 * obsolete program would try to set it, so we log a warning.
552 	 */
553 	if (utzp)
554 		log(LOG_WARNING, "pid %d attempted to set the "
555 		    "(obsolete) kernel time zone\n", l->l_proc->p_pid);
556 
557 	if (utv == NULL)
558 		return 0;
559 
560 	if (userspace) {
561 		if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
562 			return error;
563 		utv = &atv;
564 	}
565 
566 	if (utv->tv_usec < 0 || utv->tv_usec >= 1000000)
567 		return EINVAL;
568 
569 	TIMEVAL_TO_TIMESPEC(utv, &ts);
570 	return settime1(l->l_proc, &ts, check_kauth);
571 }
572 
573 int	time_adjusted;			/* set if an adjustment is made */
574 
575 /* ARGSUSED */
576 int
sys___adjtime50(struct lwp * l,const struct sys___adjtime50_args * uap,register_t * retval)577 sys___adjtime50(struct lwp *l, const struct sys___adjtime50_args *uap,
578     register_t *retval)
579 {
580 	/* {
581 		syscallarg(const struct timeval *) delta;
582 		syscallarg(struct timeval *) olddelta;
583 	} */
584 	int error;
585 	struct timeval atv, oldatv;
586 
587 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
588 	    KAUTH_REQ_SYSTEM_TIME_ADJTIME, NULL, NULL, NULL)) != 0)
589 		return error;
590 
591 	if (SCARG(uap, delta)) {
592 		error = copyin(SCARG(uap, delta), &atv,
593 		    sizeof(*SCARG(uap, delta)));
594 		if (error)
595 			return error;
596 	}
597 	adjtime1(SCARG(uap, delta) ? &atv : NULL,
598 	    SCARG(uap, olddelta) ? &oldatv : NULL, l->l_proc);
599 	if (SCARG(uap, olddelta))
600 		error = copyout(&oldatv, SCARG(uap, olddelta),
601 		    sizeof(*SCARG(uap, olddelta)));
602 	return error;
603 }
604 
605 void
adjtime1(const struct timeval * delta,struct timeval * olddelta,struct proc * p)606 adjtime1(const struct timeval *delta, struct timeval *olddelta, struct proc *p)
607 {
608 
609 	if (olddelta) {
610 		memset(olddelta, 0, sizeof(*olddelta));
611 		mutex_spin_enter(&timecounter_lock);
612 		olddelta->tv_sec = time_adjtime / 1000000;
613 		olddelta->tv_usec = time_adjtime % 1000000;
614 		if (olddelta->tv_usec < 0) {
615 			olddelta->tv_usec += 1000000;
616 			olddelta->tv_sec--;
617 		}
618 		mutex_spin_exit(&timecounter_lock);
619 	}
620 
621 	if (delta) {
622 		mutex_spin_enter(&timecounter_lock);
623 		/*
624 		 * XXX This should maybe just report failure to
625 		 * userland for nonsense deltas.
626 		 */
627 		if (delta->tv_sec > INT64_MAX/1000000 - 1) {
628 			time_adjtime = INT64_MAX;
629 		} else if (delta->tv_sec < INT64_MIN/1000000 + 1) {
630 			time_adjtime = INT64_MIN;
631 		} else {
632 			time_adjtime = delta->tv_sec * 1000000
633 			    + MAX(-999999, MIN(999999, delta->tv_usec));
634 		}
635 
636 		if (time_adjtime) {
637 			/* We need to save the system time during shutdown */
638 			time_adjusted |= 1;
639 		}
640 		mutex_spin_exit(&timecounter_lock);
641 	}
642 }
643 
644 /*
645  * Interval timer support.
646  *
647  * The itimer_*() routines provide generic support for interval timers,
648  * both real (CLOCK_REALTIME, CLOCK_MONOTIME), and virtual (CLOCK_VIRTUAL,
649  * CLOCK_PROF).
650  *
651  * Real timers keep their deadline as an absolute time, and are fired
652  * by a callout.  Virtual timers are kept as a linked-list of deltas,
653  * and are processed by hardclock().
654  *
655  * Because the real time timer callout may be delayed in real time due
656  * to interrupt processing on the system, it is possible for the real
657  * time timeout routine (itimer_callout()) run past after its deadline.
658  * It does not suffice, therefore, to reload the real timer .it_value
659  * from the timer's .it_interval.  Rather, we compute the next deadline
660  * in absolute time based on the current time and the .it_interval value,
661  * and report any overruns.
662  *
663  * Note that while the virtual timers are supported in a generic fashion
664  * here, they only (currently) make sense as per-process timers, and thus
665  * only really work for that case.
666  */
667 
668 /*
669  * itimer_init:
670  *
671  *	Initialize the common data for an interval timer.
672  */
673 void
itimer_init(struct itimer * const it,const struct itimer_ops * const ops,clockid_t const id,struct itlist * const itl)674 itimer_init(struct itimer * const it, const struct itimer_ops * const ops,
675     clockid_t const id, struct itlist * const itl)
676 {
677 
678 	KASSERT(itimer_lock_held());
679 	KASSERT(ops != NULL);
680 
681 	timespecclear(&it->it_time.it_value);
682 	it->it_ops = ops;
683 	it->it_clockid = id;
684 	it->it_overruns = 0;
685 	it->it_dying = false;
686 	if (!CLOCK_VIRTUAL_P(id)) {
687 		KASSERT(itl == NULL);
688 		callout_init(&it->it_ch, CALLOUT_MPSAFE);
689 		callout_setfunc(&it->it_ch, itimer_callout, it);
690 		if (id == CLOCK_REALTIME && ops->ito_realtime_changed != NULL) {
691 			LIST_INSERT_HEAD(&itimer_realtime_changed_notify,
692 			    it, it_rtchgq);
693 		}
694 	} else {
695 		KASSERT(itl != NULL);
696 		it->it_vlist = itl;
697 		it->it_active = false;
698 	}
699 }
700 
701 /*
702  * itimer_poison:
703  *
704  *	Poison an interval timer, preventing it from being scheduled
705  *	or processed, in preparation for freeing the timer.
706  */
707 void
itimer_poison(struct itimer * const it)708 itimer_poison(struct itimer * const it)
709 {
710 
711 	KASSERT(itimer_lock_held());
712 
713 	it->it_dying = true;
714 
715 	/*
716 	 * For non-virtual timers, stop the callout, or wait for it to
717 	 * run if it has already fired.  It cannot restart again after
718 	 * this point: the callout won't restart itself when dying, no
719 	 * other users holding the lock can restart it, and any other
720 	 * users waiting for callout_halt concurrently (itimer_settime)
721 	 * will restart from the top.
722 	 */
723 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
724 		callout_halt(&it->it_ch, &itimer_mutex);
725 		if (it->it_clockid == CLOCK_REALTIME &&
726 		    it->it_ops->ito_realtime_changed != NULL) {
727 			LIST_REMOVE(it, it_rtchgq);
728 		}
729 	}
730 }
731 
732 /*
733  * itimer_fini:
734  *
735  *	Release resources used by an interval timer.
736  *
737  *	N.B. itimer_lock must be held on entry, and is released on exit.
738  */
739 void
itimer_fini(struct itimer * const it)740 itimer_fini(struct itimer * const it)
741 {
742 
743 	KASSERT(itimer_lock_held());
744 
745 	/* All done with the global state. */
746 	itimer_unlock();
747 
748 	/* Destroy the callout, if needed. */
749 	if (!CLOCK_VIRTUAL_P(it->it_clockid))
750 		callout_destroy(&it->it_ch);
751 }
752 
753 /*
754  * itimer_decr:
755  *
756  *	Decrement an interval timer by a specified number of nanoseconds,
757  *	which must be less than a second, i.e. < 1000000000.  If the timer
758  *	expires, then reload it.  In this case, carry over (nsec - old value)
759  *	to reduce the value reloaded into the timer so that the timer does
760  *	not drift.  This routine assumes that it is called in a context where
761  *	the timers on which it is operating cannot change in value.
762  *
763  *	Returns true if the timer has expired.
764  */
765 static bool
itimer_decr(struct itimer * it,int nsec)766 itimer_decr(struct itimer *it, int nsec)
767 {
768 	struct itimerspec *itp;
769 	int error __diagused;
770 
771 	KASSERT(itimer_lock_held());
772 	KASSERT(CLOCK_VIRTUAL_P(it->it_clockid));
773 
774 	itp = &it->it_time;
775 	if (itp->it_value.tv_nsec < nsec) {
776 		if (itp->it_value.tv_sec == 0) {
777 			/* expired, and already in next interval */
778 			nsec -= itp->it_value.tv_nsec;
779 			goto expire;
780 		}
781 		itp->it_value.tv_nsec += 1000000000;
782 		itp->it_value.tv_sec--;
783 	}
784 	itp->it_value.tv_nsec -= nsec;
785 	nsec = 0;
786 	if (timespecisset(&itp->it_value))
787 		return false;
788 	/* expired, exactly at end of interval */
789  expire:
790 	if (timespecisset(&itp->it_interval)) {
791 		itp->it_value = itp->it_interval;
792 		itp->it_value.tv_nsec -= nsec;
793 		if (itp->it_value.tv_nsec < 0) {
794 			itp->it_value.tv_nsec += 1000000000;
795 			itp->it_value.tv_sec--;
796 		}
797 		error = itimer_settime(it);
798 		KASSERT(error == 0); /* virtual, never fails */
799 	} else
800 		itp->it_value.tv_nsec = 0;		/* sec is already 0 */
801 	return true;
802 }
803 
804 /*
805  * itimer_arm_real:
806  *
807  *	Arm a non-virtual timer.
808  */
809 static void
itimer_arm_real(struct itimer * const it)810 itimer_arm_real(struct itimer * const it)
811 {
812 
813 	KASSERT(!it->it_dying);
814 	KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
815 	KASSERT(!callout_pending(&it->it_ch));
816 
817 	/*
818 	 * Don't need to check tshzto() return value, here.
819 	 * callout_schedule() does it for us.
820 	 */
821 	callout_schedule(&it->it_ch,
822 	    (it->it_clockid == CLOCK_MONOTONIC
823 		? tshztoup(&it->it_time.it_value)
824 		: tshzto(&it->it_time.it_value)));
825 }
826 
827 /*
828  * itimer_callout:
829  *
830  *	Callout to expire a non-virtual timer.  Queue it up for processing,
831  *	and then reload, if it is configured to do so.
832  *
833  *	N.B. A delay in processing this callout causes multiple
834  *	SIGALRM calls to be compressed into one.
835  */
836 static void
itimer_callout(void * arg)837 itimer_callout(void *arg)
838 {
839 	uint64_t last_val, next_val, interval, now_ns;
840 	struct timespec now, next;
841 	struct itimer * const it = arg;
842 	int backwards;
843 
844 	itimer_lock();
845 	(*it->it_ops->ito_fire)(it);
846 
847 	if (!timespecisset(&it->it_time.it_interval)) {
848 		timespecclear(&it->it_time.it_value);
849 		itimer_unlock();
850 		return;
851 	}
852 
853 	if (it->it_clockid == CLOCK_MONOTONIC) {
854 		getnanouptime(&now);
855 	} else {
856 		getnanotime(&now);
857 	}
858 
859 	backwards = (timespeccmp(&it->it_time.it_value, &now, >));
860 
861 	/* Nonnegative interval guaranteed by itimerfix.  */
862 	KASSERT(it->it_time.it_interval.tv_sec >= 0);
863 	KASSERT(it->it_time.it_interval.tv_nsec >= 0);
864 
865 	/* Handle the easy case of non-overflown timers first. */
866 	if (!backwards &&
867 	    timespecaddok(&it->it_time.it_value, &it->it_time.it_interval)) {
868 		timespecadd(&it->it_time.it_value, &it->it_time.it_interval,
869 		    &next);
870 		it->it_time.it_value = next;
871 	} else {
872 		now_ns = timespec2ns(&now);
873 		last_val = timespec2ns(&it->it_time.it_value);
874 		interval = timespec2ns(&it->it_time.it_interval);
875 
876 		next_val = now_ns +
877 		    (now_ns - last_val + interval - 1) % interval;
878 
879 		if (backwards)
880 			next_val += interval;
881 		else
882 			it->it_overruns += (now_ns - last_val) / interval;
883 
884 		it->it_time.it_value.tv_sec = next_val / 1000000000;
885 		it->it_time.it_value.tv_nsec = next_val % 1000000000;
886 	}
887 
888 	/*
889 	 * Reset the callout, if it's not going away.
890 	 */
891 	if (!it->it_dying)
892 		itimer_arm_real(it);
893 	itimer_unlock();
894 }
895 
896 /*
897  * itimer_settime:
898  *
899  *	Set up the given interval timer. The value in it->it_time.it_value
900  *	is taken to be an absolute time for CLOCK_REALTIME/CLOCK_MONOTONIC
901  *	timers and a relative time for CLOCK_VIRTUAL/CLOCK_PROF timers.
902  *
903  *	If the callout had already fired but not yet run, fails with
904  *	ERESTART -- caller must restart from the top to look up a timer.
905  */
906 int
itimer_settime(struct itimer * it)907 itimer_settime(struct itimer *it)
908 {
909 	struct itimer *itn, *pitn;
910 	struct itlist *itl;
911 
912 	KASSERT(itimer_lock_held());
913 	KASSERT(!it->it_dying);
914 
915 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
916 		/*
917 		 * Try to stop the callout.  However, if it had already
918 		 * fired, we have to drop the lock to wait for it, so
919 		 * the world may have changed and pt may not be there
920 		 * any more.  In that case, tell the caller to start
921 		 * over from the top.
922 		 */
923 		if (callout_halt(&it->it_ch, &itimer_mutex))
924 			return ERESTART;
925 		KASSERT(!it->it_dying);
926 
927 		/* Now we can touch it and start it up again. */
928 		if (timespecisset(&it->it_time.it_value))
929 			itimer_arm_real(it);
930 	} else {
931 		if (it->it_active) {
932 			itn = LIST_NEXT(it, it_list);
933 			LIST_REMOVE(it, it_list);
934 			for ( ; itn; itn = LIST_NEXT(itn, it_list))
935 				timespecadd(&it->it_time.it_value,
936 				    &itn->it_time.it_value,
937 				    &itn->it_time.it_value);
938 		}
939 		if (timespecisset(&it->it_time.it_value)) {
940 			itl = it->it_vlist;
941 			for (itn = LIST_FIRST(itl), pitn = NULL;
942 			     itn && timespeccmp(&it->it_time.it_value,
943 				 &itn->it_time.it_value, >);
944 			     pitn = itn, itn = LIST_NEXT(itn, it_list))
945 				timespecsub(&it->it_time.it_value,
946 				    &itn->it_time.it_value,
947 				    &it->it_time.it_value);
948 
949 			if (pitn)
950 				LIST_INSERT_AFTER(pitn, it, it_list);
951 			else
952 				LIST_INSERT_HEAD(itl, it, it_list);
953 
954 			for ( ; itn ; itn = LIST_NEXT(itn, it_list))
955 				timespecsub(&itn->it_time.it_value,
956 				    &it->it_time.it_value,
957 				    &itn->it_time.it_value);
958 
959 			it->it_active = true;
960 		} else {
961 			it->it_active = false;
962 		}
963 	}
964 
965 	/* Success!  */
966 	return 0;
967 }
968 
969 /*
970  * itimer_gettime:
971  *
972  *	Return the remaining time of an interval timer.
973  */
974 void
itimer_gettime(const struct itimer * it,struct itimerspec * aits)975 itimer_gettime(const struct itimer *it, struct itimerspec *aits)
976 {
977 	struct timespec now;
978 	struct itimer *itn;
979 
980 	KASSERT(itimer_lock_held());
981 	KASSERT(!it->it_dying);
982 
983 	*aits = it->it_time;
984 	if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
985 		/*
986 		 * Convert from absolute to relative time in .it_value
987 		 * part of real time timer.  If time for real time
988 		 * timer has passed return 0, else return difference
989 		 * between current time and time for the timer to go
990 		 * off.
991 		 */
992 		if (timespecisset(&aits->it_value)) {
993 			if (it->it_clockid == CLOCK_REALTIME) {
994 				getnanotime(&now);
995 			} else { /* CLOCK_MONOTONIC */
996 				getnanouptime(&now);
997 			}
998 			if (timespeccmp(&aits->it_value, &now, <))
999 				timespecclear(&aits->it_value);
1000 			else
1001 				timespecsub(&aits->it_value, &now,
1002 				    &aits->it_value);
1003 		}
1004 	} else if (it->it_active) {
1005 		for (itn = LIST_FIRST(it->it_vlist); itn && itn != it;
1006 		     itn = LIST_NEXT(itn, it_list))
1007 			timespecadd(&aits->it_value,
1008 			    &itn->it_time.it_value, &aits->it_value);
1009 		KASSERT(itn != NULL); /* it should be findable on the list */
1010 	} else
1011 		timespecclear(&aits->it_value);
1012 }
1013 
1014 /*
1015  * Per-process timer support.
1016  *
1017  * Both the BSD getitimer() family and the POSIX timer_*() family of
1018  * routines are supported.
1019  *
1020  * All timers are kept in an array pointed to by p_timers, which is
1021  * allocated on demand - many processes don't use timers at all. The
1022  * first four elements in this array are reserved for the BSD timers:
1023  * element 0 is ITIMER_REAL, element 1 is ITIMER_VIRTUAL, element
1024  * 2 is ITIMER_PROF, and element 3 is ITIMER_MONOTONIC. The rest may be
1025  * allocated by the timer_create() syscall.
1026  *
1027  * These timers are a "sub-class" of interval timer.
1028  */
1029 
1030 /*
1031  * ptimer_free:
1032  *
1033  *	Free the per-process timer at the specified index.
1034  */
1035 static void
ptimer_free(struct ptimers * pts,int index)1036 ptimer_free(struct ptimers *pts, int index)
1037 {
1038 	struct itimer *it;
1039 	struct ptimer *pt;
1040 
1041 	KASSERT(itimer_lock_held());
1042 
1043 	it = pts->pts_timers[index];
1044 	pt = container_of(it, struct ptimer, pt_itimer);
1045 	pts->pts_timers[index] = NULL;
1046 	itimer_poison(it);
1047 
1048 	/*
1049 	 * Remove it from the queue to be signalled.  Must be done
1050 	 * after itimer is poisoned, because we may have had to wait
1051 	 * for the callout to complete.
1052 	 */
1053 	if (pt->pt_queued) {
1054 		TAILQ_REMOVE(&ptimer_queue, pt, pt_chain);
1055 		pt->pt_queued = false;
1056 	}
1057 
1058 	itimer_fini(it);	/* releases itimer_lock */
1059 	kmem_free(pt, sizeof(*pt));
1060 }
1061 
1062 /*
1063  * ptimers_alloc:
1064  *
1065  *	Allocate a ptimers for the specified process.
1066  */
1067 static struct ptimers *
ptimers_alloc(struct proc * p)1068 ptimers_alloc(struct proc *p)
1069 {
1070 	struct ptimers *pts;
1071 	int i;
1072 
1073 	pts = kmem_alloc(sizeof(*pts), KM_SLEEP);
1074 	LIST_INIT(&pts->pts_virtual);
1075 	LIST_INIT(&pts->pts_prof);
1076 	for (i = 0; i < TIMER_MAX; i++)
1077 		pts->pts_timers[i] = NULL;
1078 	itimer_lock();
1079 	if (p->p_timers == NULL) {
1080 		p->p_timers = pts;
1081 		itimer_unlock();
1082 		return pts;
1083 	}
1084 	itimer_unlock();
1085 	kmem_free(pts, sizeof(*pts));
1086 	return p->p_timers;
1087 }
1088 
1089 /*
1090  * ptimers_free:
1091  *
1092  *	Clean up the per-process timers. If "which" is set to TIMERS_ALL,
1093  *	then clean up all timers and free all the data structures. If
1094  *	"which" is set to TIMERS_POSIX, only clean up the timers allocated
1095  *	by timer_create(), not the BSD setitimer() timers, and only free the
1096  *	structure if none of those remain.
1097  *
1098  *	This function is exported because it is needed in the exec and
1099  *	exit code paths.
1100  */
1101 void
ptimers_free(struct proc * p,int which)1102 ptimers_free(struct proc *p, int which)
1103 {
1104 	struct ptimers *pts;
1105 	struct itimer *itn;
1106 	struct timespec ts;
1107 	int i;
1108 
1109 	if (p->p_timers == NULL)
1110 		return;
1111 
1112 	pts = p->p_timers;
1113 	itimer_lock();
1114 	if (which == TIMERS_ALL) {
1115 		p->p_timers = NULL;
1116 		i = 0;
1117 	} else {
1118 		timespecclear(&ts);
1119 		for (itn = LIST_FIRST(&pts->pts_virtual);
1120 		     itn && itn != pts->pts_timers[ITIMER_VIRTUAL];
1121 		     itn = LIST_NEXT(itn, it_list)) {
1122 			KASSERT(itn->it_clockid == CLOCK_VIRTUAL);
1123 			timespecadd(&ts, &itn->it_time.it_value, &ts);
1124 		}
1125 		LIST_FIRST(&pts->pts_virtual) = NULL;
1126 		if (itn) {
1127 			KASSERT(itn->it_clockid == CLOCK_VIRTUAL);
1128 			timespecadd(&ts, &itn->it_time.it_value,
1129 			    &itn->it_time.it_value);
1130 			LIST_INSERT_HEAD(&pts->pts_virtual, itn, it_list);
1131 		}
1132 		timespecclear(&ts);
1133 		for (itn = LIST_FIRST(&pts->pts_prof);
1134 		     itn && itn != pts->pts_timers[ITIMER_PROF];
1135 		     itn = LIST_NEXT(itn, it_list)) {
1136 			KASSERT(itn->it_clockid == CLOCK_PROF);
1137 			timespecadd(&ts, &itn->it_time.it_value, &ts);
1138 		}
1139 		LIST_FIRST(&pts->pts_prof) = NULL;
1140 		if (itn) {
1141 			KASSERT(itn->it_clockid == CLOCK_PROF);
1142 			timespecadd(&ts, &itn->it_time.it_value,
1143 			    &itn->it_time.it_value);
1144 			LIST_INSERT_HEAD(&pts->pts_prof, itn, it_list);
1145 		}
1146 		i = TIMER_MIN;
1147 	}
1148 	for ( ; i < TIMER_MAX; i++) {
1149 		if (pts->pts_timers[i] != NULL) {
1150 			/* Free the timer and release the lock.  */
1151 			ptimer_free(pts, i);
1152 			/* Reacquire the lock for the next one.  */
1153 			itimer_lock();
1154 		}
1155 	}
1156 	if (pts->pts_timers[0] == NULL && pts->pts_timers[1] == NULL &&
1157 	    pts->pts_timers[2] == NULL && pts->pts_timers[3] == NULL) {
1158 		p->p_timers = NULL;
1159 		itimer_unlock();
1160 		kmem_free(pts, sizeof(*pts));
1161 	} else
1162 		itimer_unlock();
1163 }
1164 
1165 /*
1166  * ptimer_fire:
1167  *
1168  *	Fire a per-process timer.
1169  */
1170 static void
ptimer_fire(struct itimer * it)1171 ptimer_fire(struct itimer *it)
1172 {
1173 	struct ptimer *pt = container_of(it, struct ptimer, pt_itimer);
1174 
1175 	KASSERT(itimer_lock_held());
1176 
1177 	/*
1178 	 * XXX Can overrun, but we don't do signal queueing yet, anyway.
1179 	 * XXX Relying on the clock interrupt is stupid.
1180 	 */
1181 	if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
1182 		return;
1183 	}
1184 
1185 	if (!pt->pt_queued) {
1186 		TAILQ_INSERT_TAIL(&ptimer_queue, pt, pt_chain);
1187 		pt->pt_queued = true;
1188 		softint_schedule(ptimer_sih);
1189 	}
1190 }
1191 
1192 /*
1193  * Operations vector for per-process timers (BSD and POSIX).
1194  */
1195 static const struct itimer_ops ptimer_itimer_ops = {
1196 	.ito_fire = ptimer_fire,
1197 };
1198 
1199 /*
1200  * sys_timer_create:
1201  *
1202  *	System call to create a POSIX timer.
1203  */
1204 int
sys_timer_create(struct lwp * l,const struct sys_timer_create_args * uap,register_t * retval)1205 sys_timer_create(struct lwp *l, const struct sys_timer_create_args *uap,
1206     register_t *retval)
1207 {
1208 	/* {
1209 		syscallarg(clockid_t) clock_id;
1210 		syscallarg(struct sigevent *) evp;
1211 		syscallarg(timer_t *) timerid;
1212 	} */
1213 
1214 	return timer_create1(SCARG(uap, timerid), SCARG(uap, clock_id),
1215 	    SCARG(uap, evp), copyin, l);
1216 }
1217 
1218 int
timer_create1(timer_t * tid,clockid_t id,struct sigevent * evp,copyin_t fetch_event,struct lwp * l)1219 timer_create1(timer_t *tid, clockid_t id, struct sigevent *evp,
1220     copyin_t fetch_event, struct lwp *l)
1221 {
1222 	int error;
1223 	timer_t timerid;
1224 	struct itlist *itl;
1225 	struct ptimers *pts;
1226 	struct ptimer *pt;
1227 	struct proc *p;
1228 
1229 	p = l->l_proc;
1230 
1231 	if ((u_int)id > CLOCK_MONOTONIC)
1232 		return EINVAL;
1233 
1234 	if ((pts = p->p_timers) == NULL)
1235 		pts = ptimers_alloc(p);
1236 
1237 	pt = kmem_zalloc(sizeof(*pt), KM_SLEEP);
1238 	if (evp != NULL) {
1239 		if (((error =
1240 		    (*fetch_event)(evp, &pt->pt_ev, sizeof(pt->pt_ev))) != 0) ||
1241 		    ((pt->pt_ev.sigev_notify < SIGEV_NONE) ||
1242 			(pt->pt_ev.sigev_notify > SIGEV_SA)) ||
1243 			(pt->pt_ev.sigev_notify == SIGEV_SIGNAL &&
1244 			 (pt->pt_ev.sigev_signo <= 0 ||
1245 			  pt->pt_ev.sigev_signo >= NSIG))) {
1246 			kmem_free(pt, sizeof(*pt));
1247 			return (error ? error : EINVAL);
1248 		}
1249 	}
1250 
1251 	/* Find a free timer slot, skipping those reserved for setitimer(). */
1252 	itimer_lock();
1253 	for (timerid = TIMER_MIN; timerid < TIMER_MAX; timerid++)
1254 		if (pts->pts_timers[timerid] == NULL)
1255 			break;
1256 	if (timerid == TIMER_MAX) {
1257 		itimer_unlock();
1258 		kmem_free(pt, sizeof(*pt));
1259 		return EAGAIN;
1260 	}
1261 	if (evp == NULL) {
1262 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1263 		switch (id) {
1264 		case CLOCK_REALTIME:
1265 		case CLOCK_MONOTONIC:
1266 			pt->pt_ev.sigev_signo = SIGALRM;
1267 			break;
1268 		case CLOCK_VIRTUAL:
1269 			pt->pt_ev.sigev_signo = SIGVTALRM;
1270 			break;
1271 		case CLOCK_PROF:
1272 			pt->pt_ev.sigev_signo = SIGPROF;
1273 			break;
1274 		}
1275 		pt->pt_ev.sigev_value.sival_int = timerid;
1276 	}
1277 
1278 	switch (id) {
1279 	case CLOCK_VIRTUAL:
1280 		itl = &pts->pts_virtual;
1281 		break;
1282 	case CLOCK_PROF:
1283 		itl = &pts->pts_prof;
1284 		break;
1285 	default:
1286 		itl = NULL;
1287 	}
1288 
1289 	itimer_init(&pt->pt_itimer, &ptimer_itimer_ops, id, itl);
1290 	pt->pt_proc = p;
1291 	pt->pt_poverruns = 0;
1292 	pt->pt_entry = timerid;
1293 	pt->pt_queued = false;
1294 
1295 	pts->pts_timers[timerid] = &pt->pt_itimer;
1296 	itimer_unlock();
1297 
1298 	return copyout(&timerid, tid, sizeof(timerid));
1299 }
1300 
1301 /*
1302  * sys_timer_delete:
1303  *
1304  *	System call to delete a POSIX timer.
1305  */
1306 int
sys_timer_delete(struct lwp * l,const struct sys_timer_delete_args * uap,register_t * retval)1307 sys_timer_delete(struct lwp *l, const struct sys_timer_delete_args *uap,
1308     register_t *retval)
1309 {
1310 	/* {
1311 		syscallarg(timer_t) timerid;
1312 	} */
1313 	struct proc *p = l->l_proc;
1314 	timer_t timerid;
1315 	struct ptimers *pts;
1316 	struct itimer *it, *itn;
1317 
1318 	timerid = SCARG(uap, timerid);
1319 	pts = p->p_timers;
1320 
1321 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1322 		return EINVAL;
1323 
1324 	itimer_lock();
1325 	if ((it = pts->pts_timers[timerid]) == NULL) {
1326 		itimer_unlock();
1327 		return EINVAL;
1328 	}
1329 
1330 	if (CLOCK_VIRTUAL_P(it->it_clockid)) {
1331 		if (it->it_active) {
1332 			itn = LIST_NEXT(it, it_list);
1333 			LIST_REMOVE(it, it_list);
1334 			for ( ; itn; itn = LIST_NEXT(itn, it_list))
1335 				timespecadd(&it->it_time.it_value,
1336 				    &itn->it_time.it_value,
1337 				    &itn->it_time.it_value);
1338 			it->it_active = false;
1339 		}
1340 	}
1341 
1342 	/* Free the timer and release the lock.  */
1343 	ptimer_free(pts, timerid);
1344 
1345 	return 0;
1346 }
1347 
1348 /*
1349  * sys___timer_settime50:
1350  *
1351  *	System call to set/arm a POSIX timer.
1352  */
1353 int
sys___timer_settime50(struct lwp * l,const struct sys___timer_settime50_args * uap,register_t * retval)1354 sys___timer_settime50(struct lwp *l,
1355     const struct sys___timer_settime50_args *uap,
1356     register_t *retval)
1357 {
1358 	/* {
1359 		syscallarg(timer_t) timerid;
1360 		syscallarg(int) flags;
1361 		syscallarg(const struct itimerspec *) value;
1362 		syscallarg(struct itimerspec *) ovalue;
1363 	} */
1364 	int error;
1365 	struct itimerspec value, ovalue, *ovp = NULL;
1366 
1367 	if ((error = copyin(SCARG(uap, value), &value,
1368 	    sizeof(struct itimerspec))) != 0)
1369 		return error;
1370 
1371 	if (SCARG(uap, ovalue))
1372 		ovp = &ovalue;
1373 
1374 	if ((error = dotimer_settime(SCARG(uap, timerid), &value, ovp,
1375 	    SCARG(uap, flags), l->l_proc)) != 0)
1376 		return error;
1377 
1378 	if (ovp)
1379 		return copyout(&ovalue, SCARG(uap, ovalue),
1380 		    sizeof(struct itimerspec));
1381 	return 0;
1382 }
1383 
1384 int
dotimer_settime(int timerid,struct itimerspec * value,struct itimerspec * ovalue,int flags,struct proc * p)1385 dotimer_settime(int timerid, struct itimerspec *value,
1386     struct itimerspec *ovalue, int flags, struct proc *p)
1387 {
1388 	struct timespec now;
1389 	struct itimerspec val, oval;
1390 	struct ptimers *pts;
1391 	struct itimer *it;
1392 	int error;
1393 
1394 	pts = p->p_timers;
1395 
1396 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1397 		return EINVAL;
1398 	val = *value;
1399 	if ((error = itimespecfix(&val.it_value)) != 0 ||
1400 	    (error = itimespecfix(&val.it_interval)) != 0)
1401 		return error;
1402 
1403 	itimer_lock();
1404  restart:
1405 	if ((it = pts->pts_timers[timerid]) == NULL) {
1406 		itimer_unlock();
1407 		return EINVAL;
1408 	}
1409 
1410 	oval = it->it_time;
1411 	it->it_time = val;
1412 
1413 	/*
1414 	 * If we've been passed a relative time for a realtime timer,
1415 	 * convert it to absolute; if an absolute time for a virtual
1416 	 * timer, convert it to relative and make sure we don't set it
1417 	 * to zero, which would cancel the timer, or let it go
1418 	 * negative, which would confuse the comparison tests.
1419 	 */
1420 	if (timespecisset(&it->it_time.it_value)) {
1421 		if (!CLOCK_VIRTUAL_P(it->it_clockid)) {
1422 			if ((flags & TIMER_ABSTIME) == 0) {
1423 				if (it->it_clockid == CLOCK_REALTIME) {
1424 					getnanotime(&now);
1425 				} else { /* CLOCK_MONOTONIC */
1426 					getnanouptime(&now);
1427 				}
1428 				timespecadd(&it->it_time.it_value, &now,
1429 				    &it->it_time.it_value);
1430 			}
1431 		} else {
1432 			if ((flags & TIMER_ABSTIME) != 0) {
1433 				getnanotime(&now);
1434 				timespecsub(&it->it_time.it_value, &now,
1435 				    &it->it_time.it_value);
1436 				if (!timespecisset(&it->it_time.it_value) ||
1437 				    it->it_time.it_value.tv_sec < 0) {
1438 					it->it_time.it_value.tv_sec = 0;
1439 					it->it_time.it_value.tv_nsec = 1;
1440 				}
1441 			}
1442 		}
1443 	}
1444 
1445 	error = itimer_settime(it);
1446 	if (error == ERESTART) {
1447 		KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
1448 		goto restart;
1449 	}
1450 	KASSERT(error == 0);
1451 	itimer_unlock();
1452 
1453 	if (ovalue)
1454 		*ovalue = oval;
1455 
1456 	return 0;
1457 }
1458 
1459 /*
1460  * sys___timer_gettime50:
1461  *
1462  *	System call to return the time remaining until a POSIX timer fires.
1463  */
1464 int
sys___timer_gettime50(struct lwp * l,const struct sys___timer_gettime50_args * uap,register_t * retval)1465 sys___timer_gettime50(struct lwp *l,
1466     const struct sys___timer_gettime50_args *uap, register_t *retval)
1467 {
1468 	/* {
1469 		syscallarg(timer_t) timerid;
1470 		syscallarg(struct itimerspec *) value;
1471 	} */
1472 	struct itimerspec its;
1473 	int error;
1474 
1475 	if ((error = dotimer_gettime(SCARG(uap, timerid), l->l_proc,
1476 	    &its)) != 0)
1477 		return error;
1478 
1479 	return copyout(&its, SCARG(uap, value), sizeof(its));
1480 }
1481 
1482 int
dotimer_gettime(int timerid,struct proc * p,struct itimerspec * its)1483 dotimer_gettime(int timerid, struct proc *p, struct itimerspec *its)
1484 {
1485 	struct itimer *it;
1486 	struct ptimers *pts;
1487 
1488 	pts = p->p_timers;
1489 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1490 		return EINVAL;
1491 	itimer_lock();
1492 	if ((it = pts->pts_timers[timerid]) == NULL) {
1493 		itimer_unlock();
1494 		return EINVAL;
1495 	}
1496 	itimer_gettime(it, its);
1497 	itimer_unlock();
1498 
1499 	return 0;
1500 }
1501 
1502 /*
1503  * sys_timer_getoverrun:
1504  *
1505  *	System call to return the number of times a POSIX timer has
1506  *	expired while a notification was already pending.  The counter
1507  *	is reset when a timer expires and a notification can be posted.
1508  */
1509 int
sys_timer_getoverrun(struct lwp * l,const struct sys_timer_getoverrun_args * uap,register_t * retval)1510 sys_timer_getoverrun(struct lwp *l, const struct sys_timer_getoverrun_args *uap,
1511     register_t *retval)
1512 {
1513 	/* {
1514 		syscallarg(timer_t) timerid;
1515 	} */
1516 	struct proc *p = l->l_proc;
1517 	struct ptimers *pts;
1518 	int timerid;
1519 	struct itimer *it;
1520 	struct ptimer *pt;
1521 
1522 	timerid = SCARG(uap, timerid);
1523 
1524 	pts = p->p_timers;
1525 	if (pts == NULL || timerid < 2 || timerid >= TIMER_MAX)
1526 		return EINVAL;
1527 	itimer_lock();
1528 	if ((it = pts->pts_timers[timerid]) == NULL) {
1529 		itimer_unlock();
1530 		return EINVAL;
1531 	}
1532 	pt = container_of(it, struct ptimer, pt_itimer);
1533 	*retval = pt->pt_poverruns;
1534 	if (*retval >= DELAYTIMER_MAX)
1535 		*retval = DELAYTIMER_MAX;
1536 	itimer_unlock();
1537 
1538 	return 0;
1539 }
1540 
1541 /*
1542  * sys___getitimer50:
1543  *
1544  *	System call to get the time remaining before a BSD timer fires.
1545  */
1546 int
sys___getitimer50(struct lwp * l,const struct sys___getitimer50_args * uap,register_t * retval)1547 sys___getitimer50(struct lwp *l, const struct sys___getitimer50_args *uap,
1548     register_t *retval)
1549 {
1550 	/* {
1551 		syscallarg(int) which;
1552 		syscallarg(struct itimerval *) itv;
1553 	} */
1554 	struct proc *p = l->l_proc;
1555 	struct itimerval aitv;
1556 	int error;
1557 
1558 	memset(&aitv, 0, sizeof(aitv));
1559 	error = dogetitimer(p, SCARG(uap, which), &aitv);
1560 	if (error)
1561 		return error;
1562 	return copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval));
1563 }
1564 
1565 int
dogetitimer(struct proc * p,int which,struct itimerval * itvp)1566 dogetitimer(struct proc *p, int which, struct itimerval *itvp)
1567 {
1568 	struct ptimers *pts;
1569 	struct itimer *it;
1570 	struct itimerspec its;
1571 
1572 	if ((u_int)which > ITIMER_MONOTONIC)
1573 		return EINVAL;
1574 
1575 	itimer_lock();
1576 	pts = p->p_timers;
1577 	if (pts == NULL || (it = pts->pts_timers[which]) == NULL) {
1578 		timerclear(&itvp->it_value);
1579 		timerclear(&itvp->it_interval);
1580 	} else {
1581 		itimer_gettime(it, &its);
1582 		TIMESPEC_TO_TIMEVAL(&itvp->it_value, &its.it_value);
1583 		TIMESPEC_TO_TIMEVAL(&itvp->it_interval, &its.it_interval);
1584 	}
1585 	itimer_unlock();
1586 
1587 	return 0;
1588 }
1589 
1590 /*
1591  * sys___setitimer50:
1592  *
1593  *	System call to set/arm a BSD timer.
1594  */
1595 int
sys___setitimer50(struct lwp * l,const struct sys___setitimer50_args * uap,register_t * retval)1596 sys___setitimer50(struct lwp *l, const struct sys___setitimer50_args *uap,
1597     register_t *retval)
1598 {
1599 	/* {
1600 		syscallarg(int) which;
1601 		syscallarg(const struct itimerval *) itv;
1602 		syscallarg(struct itimerval *) oitv;
1603 	} */
1604 	struct proc *p = l->l_proc;
1605 	int which = SCARG(uap, which);
1606 	struct sys___getitimer50_args getargs;
1607 	const struct itimerval *itvp;
1608 	struct itimerval aitv;
1609 	int error;
1610 
1611 	itvp = SCARG(uap, itv);
1612 	if (itvp &&
1613 	    (error = copyin(itvp, &aitv, sizeof(struct itimerval))) != 0)
1614 		return error;
1615 	if (SCARG(uap, oitv) != NULL) {
1616 		SCARG(&getargs, which) = which;
1617 		SCARG(&getargs, itv) = SCARG(uap, oitv);
1618 		if ((error = sys___getitimer50(l, &getargs, retval)) != 0)
1619 			return error;
1620 	}
1621 	if (itvp == 0)
1622 		return 0;
1623 
1624 	return dosetitimer(p, which, &aitv);
1625 }
1626 
1627 int
dosetitimer(struct proc * p,int which,struct itimerval * itvp)1628 dosetitimer(struct proc *p, int which, struct itimerval *itvp)
1629 {
1630 	struct timespec now;
1631 	struct ptimers *pts;
1632 	struct ptimer *spare;
1633 	struct itimer *it;
1634 	struct itlist *itl;
1635 	int error;
1636 
1637 	if ((u_int)which > ITIMER_MONOTONIC)
1638 		return EINVAL;
1639 	if (itimerfix(&itvp->it_value) || itimerfix(&itvp->it_interval))
1640 		return EINVAL;
1641 
1642 	/*
1643 	 * Don't bother allocating data structures if the process just
1644 	 * wants to clear the timer.
1645 	 */
1646 	spare = NULL;
1647 	pts = p->p_timers;
1648  retry:
1649 	if (!timerisset(&itvp->it_value) && (pts == NULL ||
1650 	    pts->pts_timers[which] == NULL))
1651 		return 0;
1652 	if (pts == NULL)
1653 		pts = ptimers_alloc(p);
1654 	itimer_lock();
1655  restart:
1656 	it = pts->pts_timers[which];
1657 	if (it == NULL) {
1658 		struct ptimer *pt;
1659 
1660 		if (spare == NULL) {
1661 			itimer_unlock();
1662 			spare = kmem_zalloc(sizeof(*spare), KM_SLEEP);
1663 			goto retry;
1664 		}
1665 		pt = spare;
1666 		spare = NULL;
1667 
1668 		it = &pt->pt_itimer;
1669 		pt->pt_ev.sigev_notify = SIGEV_SIGNAL;
1670 		pt->pt_ev.sigev_value.sival_int = which;
1671 
1672 		switch (which) {
1673 		case ITIMER_REAL:
1674 		case ITIMER_MONOTONIC:
1675 			itl = NULL;
1676 			pt->pt_ev.sigev_signo = SIGALRM;
1677 			break;
1678 		case ITIMER_VIRTUAL:
1679 			itl = &pts->pts_virtual;
1680 			pt->pt_ev.sigev_signo = SIGVTALRM;
1681 			break;
1682 		case ITIMER_PROF:
1683 			itl = &pts->pts_prof;
1684 			pt->pt_ev.sigev_signo = SIGPROF;
1685 			break;
1686 		default:
1687 			panic("%s: can't happen %d", __func__, which);
1688 		}
1689 		itimer_init(it, &ptimer_itimer_ops, which, itl);
1690 		pt->pt_proc = p;
1691 		pt->pt_entry = which;
1692 
1693 		pts->pts_timers[which] = it;
1694 	}
1695 
1696 	TIMEVAL_TO_TIMESPEC(&itvp->it_value, &it->it_time.it_value);
1697 	TIMEVAL_TO_TIMESPEC(&itvp->it_interval, &it->it_time.it_interval);
1698 
1699 	error = 0;
1700 	if (timespecisset(&it->it_time.it_value)) {
1701 		/* Convert to absolute time */
1702 		/* XXX need to wrap in splclock for timecounters case? */
1703 		switch (which) {
1704 		case ITIMER_REAL:
1705 			getnanotime(&now);
1706 			if (!timespecaddok(&it->it_time.it_value, &now)) {
1707 				error = EINVAL;
1708 				goto out;
1709 			}
1710 			timespecadd(&it->it_time.it_value, &now,
1711 			    &it->it_time.it_value);
1712 			break;
1713 		case ITIMER_MONOTONIC:
1714 			getnanouptime(&now);
1715 			if (!timespecaddok(&it->it_time.it_value, &now)) {
1716 				error = EINVAL;
1717 				goto out;
1718 			}
1719 			timespecadd(&it->it_time.it_value, &now,
1720 			    &it->it_time.it_value);
1721 			break;
1722 		default:
1723 			break;
1724 		}
1725 	}
1726 
1727 	error = itimer_settime(it);
1728 	if (error == ERESTART) {
1729 		KASSERT(!CLOCK_VIRTUAL_P(it->it_clockid));
1730 		goto restart;
1731 	}
1732 	KASSERT(error == 0);
1733 out:
1734 	itimer_unlock();
1735 	if (spare != NULL)
1736 		kmem_free(spare, sizeof(*spare));
1737 
1738 	return error;
1739 }
1740 
1741 /*
1742  * ptimer_tick:
1743  *
1744  *	Called from hardclock() to decrement per-process virtual timers.
1745  */
1746 void
ptimer_tick(lwp_t * l,bool user)1747 ptimer_tick(lwp_t *l, bool user)
1748 {
1749 	struct ptimers *pts;
1750 	struct itimer *it;
1751 	proc_t *p;
1752 
1753 	p = l->l_proc;
1754 	if (p->p_timers == NULL)
1755 		return;
1756 
1757 	itimer_lock();
1758 	if ((pts = l->l_proc->p_timers) != NULL) {
1759 		/*
1760 		 * Run current process's virtual and profile time, as needed.
1761 		 */
1762 		if (user && (it = LIST_FIRST(&pts->pts_virtual)) != NULL)
1763 			if (itimer_decr(it, tick * 1000))
1764 				(*it->it_ops->ito_fire)(it);
1765 		if ((it = LIST_FIRST(&pts->pts_prof)) != NULL)
1766 			if (itimer_decr(it, tick * 1000))
1767 				(*it->it_ops->ito_fire)(it);
1768 	}
1769 	itimer_unlock();
1770 }
1771 
1772 /*
1773  * ptimer_intr:
1774  *
1775  *	Software interrupt handler for processing per-process
1776  *	timer expiration.
1777  */
1778 static void
ptimer_intr(void * cookie)1779 ptimer_intr(void *cookie)
1780 {
1781 	ksiginfo_t ksi;
1782 	struct itimer *it;
1783 	struct ptimer *pt;
1784 	proc_t *p;
1785 
1786 	mutex_enter(&proc_lock);
1787 	itimer_lock();
1788 	while ((pt = TAILQ_FIRST(&ptimer_queue)) != NULL) {
1789 		it = &pt->pt_itimer;
1790 
1791 		TAILQ_REMOVE(&ptimer_queue, pt, pt_chain);
1792 		KASSERT(pt->pt_queued);
1793 		pt->pt_queued = false;
1794 
1795 		p = pt->pt_proc;
1796 		if (p->p_timers == NULL) {
1797 			/* Process is dying. */
1798 			continue;
1799 		}
1800 		if (pt->pt_ev.sigev_notify != SIGEV_SIGNAL) {
1801 			continue;
1802 		}
1803 		if (sigismember(&p->p_sigpend.sp_set, pt->pt_ev.sigev_signo)) {
1804 			it->it_overruns++;
1805 			continue;
1806 		}
1807 
1808 		KSI_INIT(&ksi);
1809 		ksi.ksi_signo = pt->pt_ev.sigev_signo;
1810 		ksi.ksi_code = SI_TIMER;
1811 		ksi.ksi_value = pt->pt_ev.sigev_value;
1812 		pt->pt_poverruns = it->it_overruns;
1813 		it->it_overruns = 0;
1814 		itimer_unlock();
1815 		kpsignal(p, &ksi, NULL);
1816 		itimer_lock();
1817 	}
1818 	itimer_unlock();
1819 	mutex_exit(&proc_lock);
1820 }
1821