xref: /freebsd/sys/kern/kern_time.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include "opt_ktrace.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/limits.h>
38 #include <sys/clock.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/sysproto.h>
42 #include <sys/resourcevar.h>
43 #include <sys/signalvar.h>
44 #include <sys/kernel.h>
45 #include <sys/sleepqueue.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysctl.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/posix4.h>
51 #include <sys/time.h>
52 #include <sys/timers.h>
53 #include <sys/timetc.h>
54 #include <sys/vnode.h>
55 #ifdef KTRACE
56 #include <sys/ktrace.h>
57 #endif
58 
59 #include <vm/vm.h>
60 #include <vm/vm_extern.h>
61 
62 #define MAX_CLOCKS 	(CLOCK_MONOTONIC+1)
63 #define CPUCLOCK_BIT		0x80000000
64 #define CPUCLOCK_PROCESS_BIT	0x40000000
65 #define CPUCLOCK_ID_MASK	(~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT))
66 #define MAKE_THREAD_CPUCLOCK(tid)	(CPUCLOCK_BIT|(tid))
67 #define MAKE_PROCESS_CPUCLOCK(pid)	\
68 	(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid))
69 
70 #define NS_PER_SEC	1000000000
71 
72 static struct kclock	posix_clocks[MAX_CLOCKS];
73 static uma_zone_t	itimer_zone = NULL;
74 
75 /*
76  * Time of day and interval timer support.
77  *
78  * These routines provide the kernel entry points to get and set
79  * the time-of-day and per-process interval timers.  Subroutines
80  * here provide support for adding and subtracting timeval structures
81  * and decrementing interval timers, optionally reloading the interval
82  * timers when they expire.
83  */
84 
85 static int	settime(struct thread *, struct timeval *);
86 static void	timevalfix(struct timeval *);
87 static int	user_clock_nanosleep(struct thread *td, clockid_t clock_id,
88 		    int flags, const struct timespec *ua_rqtp,
89 		    struct timespec *ua_rmtp);
90 
91 static void	itimer_start(void);
92 static int	itimer_init(void *, int, int);
93 static void	itimer_fini(void *, int);
94 static void	itimer_enter(struct itimer *);
95 static void	itimer_leave(struct itimer *);
96 static struct itimer *itimer_find(struct proc *, int);
97 static void	itimers_alloc(struct proc *);
98 static int	realtimer_create(struct itimer *);
99 static int	realtimer_gettime(struct itimer *, struct itimerspec *);
100 static int	realtimer_settime(struct itimer *, int,
101 			struct itimerspec *, struct itimerspec *);
102 static int	realtimer_delete(struct itimer *);
103 static void	realtimer_clocktime(clockid_t, struct timespec *);
104 static void	realtimer_expire(void *);
105 static void	realtimer_expire_l(struct itimer *it, bool proc_locked);
106 
107 static void	realitexpire(void *arg);
108 
109 static int	register_posix_clock(int, const struct kclock *);
110 static void	itimer_fire(struct itimer *it);
111 static int	itimespecfix(struct timespec *ts);
112 
113 #define CLOCK_CALL(clock, call, arglist)		\
114 	((*posix_clocks[clock].call) arglist)
115 
116 SYSINIT(posix_timer, SI_SUB_P1003_1B, SI_ORDER_FIRST+4, itimer_start, NULL);
117 
118 static int
119 settime(struct thread *td, struct timeval *tv)
120 {
121 	struct timeval delta, tv1, tv2;
122 	static struct timeval maxtime, laststep;
123 	struct timespec ts;
124 
125 	microtime(&tv1);
126 	delta = *tv;
127 	timevalsub(&delta, &tv1);
128 
129 	/*
130 	 * If the system is secure, we do not allow the time to be
131 	 * set to a value earlier than 1 second less than the highest
132 	 * time we have yet seen. The worst a miscreant can do in
133 	 * this circumstance is "freeze" time. He couldn't go
134 	 * back to the past.
135 	 *
136 	 * We similarly do not allow the clock to be stepped more
137 	 * than one second, nor more than once per second. This allows
138 	 * a miscreant to make the clock march double-time, but no worse.
139 	 */
140 	if (securelevel_gt(td->td_ucred, 1) != 0) {
141 		if (delta.tv_sec < 0 || delta.tv_usec < 0) {
142 			/*
143 			 * Update maxtime to latest time we've seen.
144 			 */
145 			if (tv1.tv_sec > maxtime.tv_sec)
146 				maxtime = tv1;
147 			tv2 = *tv;
148 			timevalsub(&tv2, &maxtime);
149 			if (tv2.tv_sec < -1) {
150 				tv->tv_sec = maxtime.tv_sec - 1;
151 				printf("Time adjustment clamped to -1 second\n");
152 			}
153 		} else {
154 			if (tv1.tv_sec == laststep.tv_sec)
155 				return (EPERM);
156 			if (delta.tv_sec > 1) {
157 				tv->tv_sec = tv1.tv_sec + 1;
158 				printf("Time adjustment clamped to +1 second\n");
159 			}
160 			laststep = *tv;
161 		}
162 	}
163 
164 	ts.tv_sec = tv->tv_sec;
165 	ts.tv_nsec = tv->tv_usec * 1000;
166 	tc_setclock(&ts);
167 	resettodr();
168 	return (0);
169 }
170 
171 #ifndef _SYS_SYSPROTO_H_
172 struct clock_getcpuclockid2_args {
173 	id_t id;
174 	int which,
175 	clockid_t *clock_id;
176 };
177 #endif
178 /* ARGSUSED */
179 int
180 sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap)
181 {
182 	clockid_t clk_id;
183 	int error;
184 
185 	error = kern_clock_getcpuclockid2(td, uap->id, uap->which, &clk_id);
186 	if (error == 0)
187 		error = copyout(&clk_id, uap->clock_id, sizeof(clockid_t));
188 	return (error);
189 }
190 
191 int
192 kern_clock_getcpuclockid2(struct thread *td, id_t id, int which,
193     clockid_t *clk_id)
194 {
195 	struct proc *p;
196 	pid_t pid;
197 	lwpid_t tid;
198 	int error;
199 
200 	switch (which) {
201 	case CPUCLOCK_WHICH_PID:
202 		if (id != 0) {
203 			error = pget(id, PGET_CANSEE | PGET_NOTID, &p);
204 			if (error != 0)
205 				return (error);
206 			PROC_UNLOCK(p);
207 			pid = id;
208 		} else {
209 			pid = td->td_proc->p_pid;
210 		}
211 		*clk_id = MAKE_PROCESS_CPUCLOCK(pid);
212 		return (0);
213 	case CPUCLOCK_WHICH_TID:
214 		tid = id == 0 ? td->td_tid : id;
215 		*clk_id = MAKE_THREAD_CPUCLOCK(tid);
216 		return (0);
217 	default:
218 		return (EINVAL);
219 	}
220 }
221 
222 #ifndef _SYS_SYSPROTO_H_
223 struct clock_gettime_args {
224 	clockid_t clock_id;
225 	struct	timespec *tp;
226 };
227 #endif
228 /* ARGSUSED */
229 int
230 sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap)
231 {
232 	struct timespec ats;
233 	int error;
234 
235 	error = kern_clock_gettime(td, uap->clock_id, &ats);
236 	if (error == 0)
237 		error = copyout(&ats, uap->tp, sizeof(ats));
238 
239 	return (error);
240 }
241 
242 static inline void
243 cputick2timespec(uint64_t runtime, struct timespec *ats)
244 {
245 	uint64_t tr;
246 	tr = cpu_tickrate();
247 	ats->tv_sec = runtime / tr;
248 	ats->tv_nsec = ((runtime % tr) * 1000000000ULL) / tr;
249 }
250 
251 void
252 kern_thread_cputime(struct thread *targettd, struct timespec *ats)
253 {
254 	uint64_t runtime, curtime, switchtime;
255 
256 	if (targettd == NULL) { /* current thread */
257 		spinlock_enter();
258 		switchtime = PCPU_GET(switchtime);
259 		curtime = cpu_ticks();
260 		runtime = curthread->td_runtime;
261 		spinlock_exit();
262 		runtime += curtime - switchtime;
263 	} else {
264 		PROC_LOCK_ASSERT(targettd->td_proc, MA_OWNED);
265 		thread_lock(targettd);
266 		runtime = targettd->td_runtime;
267 		thread_unlock(targettd);
268 	}
269 	cputick2timespec(runtime, ats);
270 }
271 
272 void
273 kern_process_cputime(struct proc *targetp, struct timespec *ats)
274 {
275 	uint64_t runtime;
276 	struct rusage ru;
277 
278 	PROC_LOCK_ASSERT(targetp, MA_OWNED);
279 	PROC_STATLOCK(targetp);
280 	rufetch(targetp, &ru);
281 	runtime = targetp->p_rux.rux_runtime;
282 	if (curthread->td_proc == targetp)
283 		runtime += cpu_ticks() - PCPU_GET(switchtime);
284 	PROC_STATUNLOCK(targetp);
285 	cputick2timespec(runtime, ats);
286 }
287 
288 static int
289 get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats)
290 {
291 	struct proc *p, *p2;
292 	struct thread *td2;
293 	lwpid_t tid;
294 	pid_t pid;
295 	int error;
296 
297 	p = td->td_proc;
298 	if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) {
299 		tid = clock_id & CPUCLOCK_ID_MASK;
300 		td2 = tdfind(tid, p->p_pid);
301 		if (td2 == NULL)
302 			return (EINVAL);
303 		kern_thread_cputime(td2, ats);
304 		PROC_UNLOCK(td2->td_proc);
305 	} else {
306 		pid = clock_id & CPUCLOCK_ID_MASK;
307 		error = pget(pid, PGET_CANSEE, &p2);
308 		if (error != 0)
309 			return (EINVAL);
310 		kern_process_cputime(p2, ats);
311 		PROC_UNLOCK(p2);
312 	}
313 	return (0);
314 }
315 
316 int
317 kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats)
318 {
319 	struct timeval sys, user;
320 	struct proc *p;
321 
322 	p = td->td_proc;
323 	switch (clock_id) {
324 	case CLOCK_REALTIME:		/* Default to precise. */
325 	case CLOCK_REALTIME_PRECISE:
326 		nanotime(ats);
327 		break;
328 	case CLOCK_REALTIME_FAST:
329 		getnanotime(ats);
330 		break;
331 	case CLOCK_VIRTUAL:
332 		PROC_LOCK(p);
333 		PROC_STATLOCK(p);
334 		calcru(p, &user, &sys);
335 		PROC_STATUNLOCK(p);
336 		PROC_UNLOCK(p);
337 		TIMEVAL_TO_TIMESPEC(&user, ats);
338 		break;
339 	case CLOCK_PROF:
340 		PROC_LOCK(p);
341 		PROC_STATLOCK(p);
342 		calcru(p, &user, &sys);
343 		PROC_STATUNLOCK(p);
344 		PROC_UNLOCK(p);
345 		timevaladd(&user, &sys);
346 		TIMEVAL_TO_TIMESPEC(&user, ats);
347 		break;
348 	case CLOCK_MONOTONIC:		/* Default to precise. */
349 	case CLOCK_MONOTONIC_PRECISE:
350 	case CLOCK_UPTIME:
351 	case CLOCK_UPTIME_PRECISE:
352 		nanouptime(ats);
353 		break;
354 	case CLOCK_UPTIME_FAST:
355 	case CLOCK_MONOTONIC_FAST:
356 		getnanouptime(ats);
357 		break;
358 	case CLOCK_SECOND:
359 		ats->tv_sec = time_second;
360 		ats->tv_nsec = 0;
361 		break;
362 	case CLOCK_THREAD_CPUTIME_ID:
363 		kern_thread_cputime(NULL, ats);
364 		break;
365 	case CLOCK_PROCESS_CPUTIME_ID:
366 		PROC_LOCK(p);
367 		kern_process_cputime(p, ats);
368 		PROC_UNLOCK(p);
369 		break;
370 	default:
371 		if ((int)clock_id >= 0)
372 			return (EINVAL);
373 		return (get_cputime(td, clock_id, ats));
374 	}
375 	return (0);
376 }
377 
378 #ifndef _SYS_SYSPROTO_H_
379 struct clock_settime_args {
380 	clockid_t clock_id;
381 	const struct	timespec *tp;
382 };
383 #endif
384 /* ARGSUSED */
385 int
386 sys_clock_settime(struct thread *td, struct clock_settime_args *uap)
387 {
388 	struct timespec ats;
389 	int error;
390 
391 	if ((error = copyin(uap->tp, &ats, sizeof(ats))) != 0)
392 		return (error);
393 	return (kern_clock_settime(td, uap->clock_id, &ats));
394 }
395 
396 static int allow_insane_settime = 0;
397 SYSCTL_INT(_debug, OID_AUTO, allow_insane_settime, CTLFLAG_RWTUN,
398     &allow_insane_settime, 0,
399     "do not perform possibly restrictive checks on settime(2) args");
400 
401 int
402 kern_clock_settime(struct thread *td, clockid_t clock_id, struct timespec *ats)
403 {
404 	struct timeval atv;
405 	int error;
406 
407 	if ((error = priv_check(td, PRIV_CLOCK_SETTIME)) != 0)
408 		return (error);
409 	if (clock_id != CLOCK_REALTIME)
410 		return (EINVAL);
411 	if (!timespecvalid_interval(ats))
412 		return (EINVAL);
413 	if (!allow_insane_settime &&
414 	    (ats->tv_sec > 8000ULL * 365 * 24 * 60 * 60 ||
415 	    ats->tv_sec < utc_offset()))
416 		return (EINVAL);
417 	/* XXX Don't convert nsec->usec and back */
418 	TIMESPEC_TO_TIMEVAL(&atv, ats);
419 	error = settime(td, &atv);
420 	return (error);
421 }
422 
423 #ifndef _SYS_SYSPROTO_H_
424 struct clock_getres_args {
425 	clockid_t clock_id;
426 	struct	timespec *tp;
427 };
428 #endif
429 int
430 sys_clock_getres(struct thread *td, struct clock_getres_args *uap)
431 {
432 	struct timespec ts;
433 	int error;
434 
435 	if (uap->tp == NULL)
436 		return (0);
437 
438 	error = kern_clock_getres(td, uap->clock_id, &ts);
439 	if (error == 0)
440 		error = copyout(&ts, uap->tp, sizeof(ts));
441 	return (error);
442 }
443 
444 int
445 kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts)
446 {
447 
448 	ts->tv_sec = 0;
449 	switch (clock_id) {
450 	case CLOCK_REALTIME:
451 	case CLOCK_REALTIME_FAST:
452 	case CLOCK_REALTIME_PRECISE:
453 	case CLOCK_MONOTONIC:
454 	case CLOCK_MONOTONIC_FAST:
455 	case CLOCK_MONOTONIC_PRECISE:
456 	case CLOCK_UPTIME:
457 	case CLOCK_UPTIME_FAST:
458 	case CLOCK_UPTIME_PRECISE:
459 		/*
460 		 * Round up the result of the division cheaply by adding 1.
461 		 * Rounding up is especially important if rounding down
462 		 * would give 0.  Perfect rounding is unimportant.
463 		 */
464 		ts->tv_nsec = NS_PER_SEC / tc_getfrequency() + 1;
465 		break;
466 	case CLOCK_VIRTUAL:
467 	case CLOCK_PROF:
468 		/* Accurately round up here because we can do so cheaply. */
469 		ts->tv_nsec = howmany(NS_PER_SEC, hz);
470 		break;
471 	case CLOCK_SECOND:
472 		ts->tv_sec = 1;
473 		ts->tv_nsec = 0;
474 		break;
475 	case CLOCK_THREAD_CPUTIME_ID:
476 	case CLOCK_PROCESS_CPUTIME_ID:
477 	cputime:
478 		ts->tv_nsec = 1000000000 / cpu_tickrate() + 1;
479 		break;
480 	default:
481 		if ((int)clock_id < 0)
482 			goto cputime;
483 		return (EINVAL);
484 	}
485 	return (0);
486 }
487 
488 int
489 kern_nanosleep(struct thread *td, struct timespec *rqt, struct timespec *rmt)
490 {
491 
492 	return (kern_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME, rqt,
493 	    rmt));
494 }
495 
496 static uint8_t nanowait[MAXCPU];
497 
498 int
499 kern_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
500     const struct timespec *rqt, struct timespec *rmt)
501 {
502 	struct timespec ts, now;
503 	sbintime_t sbt, sbtt, prec, tmp;
504 	time_t over;
505 	int error;
506 	bool is_abs_real;
507 
508 	if (rqt->tv_nsec < 0 || rqt->tv_nsec >= NS_PER_SEC)
509 		return (EINVAL);
510 	if ((flags & ~TIMER_ABSTIME) != 0)
511 		return (EINVAL);
512 	switch (clock_id) {
513 	case CLOCK_REALTIME:
514 	case CLOCK_REALTIME_PRECISE:
515 	case CLOCK_REALTIME_FAST:
516 	case CLOCK_SECOND:
517 		is_abs_real = (flags & TIMER_ABSTIME) != 0;
518 		break;
519 	case CLOCK_MONOTONIC:
520 	case CLOCK_MONOTONIC_PRECISE:
521 	case CLOCK_MONOTONIC_FAST:
522 	case CLOCK_UPTIME:
523 	case CLOCK_UPTIME_PRECISE:
524 	case CLOCK_UPTIME_FAST:
525 		is_abs_real = false;
526 		break;
527 	case CLOCK_VIRTUAL:
528 	case CLOCK_PROF:
529 	case CLOCK_PROCESS_CPUTIME_ID:
530 		return (ENOTSUP);
531 	case CLOCK_THREAD_CPUTIME_ID:
532 	default:
533 		return (EINVAL);
534 	}
535 	do {
536 		ts = *rqt;
537 		if ((flags & TIMER_ABSTIME) != 0) {
538 			if (is_abs_real)
539 				td->td_rtcgen =
540 				    atomic_load_acq_int(&rtc_generation);
541 			error = kern_clock_gettime(td, clock_id, &now);
542 			KASSERT(error == 0, ("kern_clock_gettime: %d", error));
543 			timespecsub(&ts, &now, &ts);
544 		}
545 		if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
546 			error = EWOULDBLOCK;
547 			break;
548 		}
549 		if (ts.tv_sec > INT32_MAX / 2) {
550 			over = ts.tv_sec - INT32_MAX / 2;
551 			ts.tv_sec -= over;
552 		} else
553 			over = 0;
554 		tmp = tstosbt(ts);
555 		prec = tmp;
556 		prec >>= tc_precexp;
557 		if (TIMESEL(&sbt, tmp))
558 			sbt += tc_tick_sbt;
559 		sbt += tmp;
560 		error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
561 		    sbt, prec, C_ABSOLUTE);
562 	} while (error == 0 && is_abs_real && td->td_rtcgen == 0);
563 	td->td_rtcgen = 0;
564 	if (error != EWOULDBLOCK) {
565 		if (TIMESEL(&sbtt, tmp))
566 			sbtt += tc_tick_sbt;
567 		if (sbtt >= sbt)
568 			return (0);
569 		if (error == ERESTART)
570 			error = EINTR;
571 		if ((flags & TIMER_ABSTIME) == 0 && rmt != NULL) {
572 			ts = sbttots(sbt - sbtt);
573 			ts.tv_sec += over;
574 			if (ts.tv_sec < 0)
575 				timespecclear(&ts);
576 			*rmt = ts;
577 		}
578 		return (error);
579 	}
580 	return (0);
581 }
582 
583 #ifndef _SYS_SYSPROTO_H_
584 struct nanosleep_args {
585 	struct	timespec *rqtp;
586 	struct	timespec *rmtp;
587 };
588 #endif
589 /* ARGSUSED */
590 int
591 sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
592 {
593 
594 	return (user_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME,
595 	    uap->rqtp, uap->rmtp));
596 }
597 
598 #ifndef _SYS_SYSPROTO_H_
599 struct clock_nanosleep_args {
600 	clockid_t clock_id;
601 	int 	  flags;
602 	struct	timespec *rqtp;
603 	struct	timespec *rmtp;
604 };
605 #endif
606 /* ARGSUSED */
607 int
608 sys_clock_nanosleep(struct thread *td, struct clock_nanosleep_args *uap)
609 {
610 	int error;
611 
612 	error = user_clock_nanosleep(td, uap->clock_id, uap->flags, uap->rqtp,
613 	    uap->rmtp);
614 	return (kern_posix_error(td, error));
615 }
616 
617 static int
618 user_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
619     const struct timespec *ua_rqtp, struct timespec *ua_rmtp)
620 {
621 	struct timespec rmt, rqt;
622 	int error, error2;
623 
624 	error = copyin(ua_rqtp, &rqt, sizeof(rqt));
625 	if (error)
626 		return (error);
627 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
628 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
629 		error2 = copyout(&rmt, ua_rmtp, sizeof(rmt));
630 		if (error2 != 0)
631 			error = error2;
632 	}
633 	return (error);
634 }
635 
636 #ifndef _SYS_SYSPROTO_H_
637 struct gettimeofday_args {
638 	struct	timeval *tp;
639 	struct	timezone *tzp;
640 };
641 #endif
642 /* ARGSUSED */
643 int
644 sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
645 {
646 	struct timeval atv;
647 	struct timezone rtz;
648 	int error = 0;
649 
650 	if (uap->tp) {
651 		microtime(&atv);
652 		error = copyout(&atv, uap->tp, sizeof (atv));
653 	}
654 	if (error == 0 && uap->tzp != NULL) {
655 		rtz.tz_minuteswest = 0;
656 		rtz.tz_dsttime = 0;
657 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
658 	}
659 	return (error);
660 }
661 
662 #ifndef _SYS_SYSPROTO_H_
663 struct settimeofday_args {
664 	struct	timeval *tv;
665 	struct	timezone *tzp;
666 };
667 #endif
668 /* ARGSUSED */
669 int
670 sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
671 {
672 	struct timeval atv, *tvp;
673 	struct timezone atz, *tzp;
674 	int error;
675 
676 	if (uap->tv) {
677 		error = copyin(uap->tv, &atv, sizeof(atv));
678 		if (error)
679 			return (error);
680 		tvp = &atv;
681 	} else
682 		tvp = NULL;
683 	if (uap->tzp) {
684 		error = copyin(uap->tzp, &atz, sizeof(atz));
685 		if (error)
686 			return (error);
687 		tzp = &atz;
688 	} else
689 		tzp = NULL;
690 	return (kern_settimeofday(td, tvp, tzp));
691 }
692 
693 int
694 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
695 {
696 	int error;
697 
698 	error = priv_check(td, PRIV_SETTIMEOFDAY);
699 	if (error)
700 		return (error);
701 	/* Verify all parameters before changing time. */
702 	if (tv) {
703 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 ||
704 		    tv->tv_sec < 0)
705 			return (EINVAL);
706 		error = settime(td, tv);
707 	}
708 	return (error);
709 }
710 
711 /*
712  * Get value of an interval timer.  The process virtual and profiling virtual
713  * time timers are kept in the p_stats area, since they can be swapped out.
714  * These are kept internally in the way they are specified externally: in
715  * time until they expire.
716  *
717  * The real time interval timer is kept in the process table slot for the
718  * process, and its value (it_value) is kept as an absolute time rather than
719  * as a delta, so that it is easy to keep periodic real-time signals from
720  * drifting.
721  *
722  * Virtual time timers are processed in the hardclock() routine of
723  * kern_clock.c.  The real time timer is processed by a timeout routine,
724  * called from the softclock() routine.  Since a callout may be delayed in
725  * real time due to interrupt processing in the system, it is possible for
726  * the real time timeout routine (realitexpire, given below), to be delayed
727  * in real time past when it is supposed to occur.  It does not suffice,
728  * therefore, to reload the real timer .it_value from the real time timers
729  * .it_interval.  Rather, we compute the next time in absolute time the timer
730  * should go off.
731  */
732 #ifndef _SYS_SYSPROTO_H_
733 struct getitimer_args {
734 	u_int	which;
735 	struct	itimerval *itv;
736 };
737 #endif
738 int
739 sys_getitimer(struct thread *td, struct getitimer_args *uap)
740 {
741 	struct itimerval aitv;
742 	int error;
743 
744 	error = kern_getitimer(td, uap->which, &aitv);
745 	if (error != 0)
746 		return (error);
747 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
748 }
749 
750 int
751 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
752 {
753 	struct proc *p = td->td_proc;
754 	struct timeval ctv;
755 
756 	if (which > ITIMER_PROF)
757 		return (EINVAL);
758 
759 	if (which == ITIMER_REAL) {
760 		/*
761 		 * Convert from absolute to relative time in .it_value
762 		 * part of real time timer.  If time for real time timer
763 		 * has passed return 0, else return difference between
764 		 * current time and time for the timer to go off.
765 		 */
766 		PROC_LOCK(p);
767 		*aitv = p->p_realtimer;
768 		PROC_UNLOCK(p);
769 		if (timevalisset(&aitv->it_value)) {
770 			microuptime(&ctv);
771 			if (timevalcmp(&aitv->it_value, &ctv, <))
772 				timevalclear(&aitv->it_value);
773 			else
774 				timevalsub(&aitv->it_value, &ctv);
775 		}
776 	} else {
777 		PROC_ITIMLOCK(p);
778 		*aitv = p->p_stats->p_timer[which];
779 		PROC_ITIMUNLOCK(p);
780 	}
781 #ifdef KTRACE
782 	if (KTRPOINT(td, KTR_STRUCT))
783 		ktritimerval(aitv);
784 #endif
785 	return (0);
786 }
787 
788 #ifndef _SYS_SYSPROTO_H_
789 struct setitimer_args {
790 	u_int	which;
791 	struct	itimerval *itv, *oitv;
792 };
793 #endif
794 int
795 sys_setitimer(struct thread *td, struct setitimer_args *uap)
796 {
797 	struct itimerval aitv, oitv;
798 	int error;
799 
800 	if (uap->itv == NULL) {
801 		uap->itv = uap->oitv;
802 		return (sys_getitimer(td, (struct getitimer_args *)uap));
803 	}
804 
805 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
806 		return (error);
807 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
808 	if (error != 0 || uap->oitv == NULL)
809 		return (error);
810 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
811 }
812 
813 int
814 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
815     struct itimerval *oitv)
816 {
817 	struct proc *p = td->td_proc;
818 	struct timeval ctv;
819 	sbintime_t sbt, pr;
820 
821 	if (aitv == NULL)
822 		return (kern_getitimer(td, which, oitv));
823 
824 	if (which > ITIMER_PROF)
825 		return (EINVAL);
826 #ifdef KTRACE
827 	if (KTRPOINT(td, KTR_STRUCT))
828 		ktritimerval(aitv);
829 #endif
830 	if (itimerfix(&aitv->it_value) ||
831 	    aitv->it_value.tv_sec > INT32_MAX / 2)
832 		return (EINVAL);
833 	if (!timevalisset(&aitv->it_value))
834 		timevalclear(&aitv->it_interval);
835 	else if (itimerfix(&aitv->it_interval) ||
836 	    aitv->it_interval.tv_sec > INT32_MAX / 2)
837 		return (EINVAL);
838 
839 	if (which == ITIMER_REAL) {
840 		PROC_LOCK(p);
841 		if (timevalisset(&p->p_realtimer.it_value))
842 			callout_stop(&p->p_itcallout);
843 		microuptime(&ctv);
844 		if (timevalisset(&aitv->it_value)) {
845 			pr = tvtosbt(aitv->it_value) >> tc_precexp;
846 			timevaladd(&aitv->it_value, &ctv);
847 			sbt = tvtosbt(aitv->it_value);
848 			callout_reset_sbt(&p->p_itcallout, sbt, pr,
849 			    realitexpire, p, C_ABSOLUTE);
850 		}
851 		*oitv = p->p_realtimer;
852 		p->p_realtimer = *aitv;
853 		PROC_UNLOCK(p);
854 		if (timevalisset(&oitv->it_value)) {
855 			if (timevalcmp(&oitv->it_value, &ctv, <))
856 				timevalclear(&oitv->it_value);
857 			else
858 				timevalsub(&oitv->it_value, &ctv);
859 		}
860 	} else {
861 		if (aitv->it_interval.tv_sec == 0 &&
862 		    aitv->it_interval.tv_usec != 0 &&
863 		    aitv->it_interval.tv_usec < tick)
864 			aitv->it_interval.tv_usec = tick;
865 		if (aitv->it_value.tv_sec == 0 &&
866 		    aitv->it_value.tv_usec != 0 &&
867 		    aitv->it_value.tv_usec < tick)
868 			aitv->it_value.tv_usec = tick;
869 		PROC_ITIMLOCK(p);
870 		*oitv = p->p_stats->p_timer[which];
871 		p->p_stats->p_timer[which] = *aitv;
872 		PROC_ITIMUNLOCK(p);
873 	}
874 #ifdef KTRACE
875 	if (KTRPOINT(td, KTR_STRUCT))
876 		ktritimerval(oitv);
877 #endif
878 	return (0);
879 }
880 
881 static void
882 realitexpire_reset_callout(struct proc *p, sbintime_t *isbtp)
883 {
884 	sbintime_t prec;
885 
886 	prec = isbtp == NULL ? tvtosbt(p->p_realtimer.it_interval) : *isbtp;
887 	callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
888 	    prec >> tc_precexp, realitexpire, p, C_ABSOLUTE);
889 }
890 
891 void
892 itimer_proc_continue(struct proc *p)
893 {
894 	struct timeval ctv;
895 	struct itimer *it;
896 	int id;
897 
898 	PROC_LOCK_ASSERT(p, MA_OWNED);
899 
900 	if ((p->p_flag2 & P2_ITSTOPPED) != 0) {
901 		p->p_flag2 &= ~P2_ITSTOPPED;
902 		microuptime(&ctv);
903 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >=))
904 			realitexpire(p);
905 		else
906 			realitexpire_reset_callout(p, NULL);
907 	}
908 
909 	if (p->p_itimers != NULL) {
910 		for (id = 3; id < TIMER_MAX; id++) {
911 			it = p->p_itimers->its_timers[id];
912 			if (it == NULL)
913 				continue;
914 			if ((it->it_flags & ITF_PSTOPPED) != 0) {
915 				ITIMER_LOCK(it);
916 				if ((it->it_flags & ITF_PSTOPPED) != 0) {
917 					it->it_flags &= ~ITF_PSTOPPED;
918 					if ((it->it_flags & ITF_DELETING) == 0)
919 						realtimer_expire_l(it, true);
920 				}
921 				ITIMER_UNLOCK(it);
922 			}
923 		}
924 	}
925 }
926 
927 /*
928  * Real interval timer expired:
929  * send process whose timer expired an alarm signal.
930  * If time is not set up to reload, then just return.
931  * Else compute next time timer should go off which is > current time.
932  * This is where delay in processing this timeout causes multiple
933  * SIGALRM calls to be compressed into one.
934  * tvtohz() always adds 1 to allow for the time until the next clock
935  * interrupt being strictly less than 1 clock tick, but we don't want
936  * that here since we want to appear to be in sync with the clock
937  * interrupt even when we're delayed.
938  */
939 static void
940 realitexpire(void *arg)
941 {
942 	struct proc *p;
943 	struct timeval ctv;
944 	sbintime_t isbt;
945 
946 	p = (struct proc *)arg;
947 	kern_psignal(p, SIGALRM);
948 	if (!timevalisset(&p->p_realtimer.it_interval)) {
949 		timevalclear(&p->p_realtimer.it_value);
950 		return;
951 	}
952 
953 	isbt = tvtosbt(p->p_realtimer.it_interval);
954 	if (isbt >= sbt_timethreshold)
955 		getmicrouptime(&ctv);
956 	else
957 		microuptime(&ctv);
958 	do {
959 		timevaladd(&p->p_realtimer.it_value,
960 		    &p->p_realtimer.it_interval);
961 	} while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
962 
963 	if (P_SHOULDSTOP(p) || P_KILLED(p)) {
964 		p->p_flag2 |= P2_ITSTOPPED;
965 		return;
966 	}
967 
968 	p->p_flag2 &= ~P2_ITSTOPPED;
969 	realitexpire_reset_callout(p, &isbt);
970 }
971 
972 /*
973  * Check that a proposed value to load into the .it_value or
974  * .it_interval part of an interval timer is acceptable, and
975  * fix it to have at least minimal value (i.e. if it is less
976  * than the resolution of the clock, round it up.)
977  */
978 int
979 itimerfix(struct timeval *tv)
980 {
981 
982 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
983 		return (EINVAL);
984 	if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
985 	    tv->tv_usec < (u_int)tick / 16)
986 		tv->tv_usec = (u_int)tick / 16;
987 	return (0);
988 }
989 
990 /*
991  * Decrement an interval timer by a specified number
992  * of microseconds, which must be less than a second,
993  * i.e. < 1000000.  If the timer expires, then reload
994  * it.  In this case, carry over (usec - old value) to
995  * reduce the value reloaded into the timer so that
996  * the timer does not drift.  This routine assumes
997  * that it is called in a context where the timers
998  * on which it is operating cannot change in value.
999  */
1000 int
1001 itimerdecr(struct itimerval *itp, int usec)
1002 {
1003 
1004 	if (itp->it_value.tv_usec < usec) {
1005 		if (itp->it_value.tv_sec == 0) {
1006 			/* expired, and already in next interval */
1007 			usec -= itp->it_value.tv_usec;
1008 			goto expire;
1009 		}
1010 		itp->it_value.tv_usec += 1000000;
1011 		itp->it_value.tv_sec--;
1012 	}
1013 	itp->it_value.tv_usec -= usec;
1014 	usec = 0;
1015 	if (timevalisset(&itp->it_value))
1016 		return (1);
1017 	/* expired, exactly at end of interval */
1018 expire:
1019 	if (timevalisset(&itp->it_interval)) {
1020 		itp->it_value = itp->it_interval;
1021 		itp->it_value.tv_usec -= usec;
1022 		if (itp->it_value.tv_usec < 0) {
1023 			itp->it_value.tv_usec += 1000000;
1024 			itp->it_value.tv_sec--;
1025 		}
1026 	} else
1027 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
1028 	return (0);
1029 }
1030 
1031 /*
1032  * Add and subtract routines for timevals.
1033  * N.B.: subtract routine doesn't deal with
1034  * results which are before the beginning,
1035  * it just gets very confused in this case.
1036  * Caveat emptor.
1037  */
1038 void
1039 timevaladd(struct timeval *t1, const struct timeval *t2)
1040 {
1041 
1042 	t1->tv_sec += t2->tv_sec;
1043 	t1->tv_usec += t2->tv_usec;
1044 	timevalfix(t1);
1045 }
1046 
1047 void
1048 timevalsub(struct timeval *t1, const struct timeval *t2)
1049 {
1050 
1051 	t1->tv_sec -= t2->tv_sec;
1052 	t1->tv_usec -= t2->tv_usec;
1053 	timevalfix(t1);
1054 }
1055 
1056 static void
1057 timevalfix(struct timeval *t1)
1058 {
1059 
1060 	if (t1->tv_usec < 0) {
1061 		t1->tv_sec--;
1062 		t1->tv_usec += 1000000;
1063 	}
1064 	if (t1->tv_usec >= 1000000) {
1065 		t1->tv_sec++;
1066 		t1->tv_usec -= 1000000;
1067 	}
1068 }
1069 
1070 /*
1071  * ratecheck(): simple time-based rate-limit checking.
1072  */
1073 int
1074 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1075 {
1076 	struct timeval tv, delta;
1077 	int rv = 0;
1078 
1079 	getmicrouptime(&tv);		/* NB: 10ms precision */
1080 	delta = tv;
1081 	timevalsub(&delta, lasttime);
1082 
1083 	/*
1084 	 * check for 0,0 is so that the message will be seen at least once,
1085 	 * even if interval is huge.
1086 	 */
1087 	if (timevalcmp(&delta, mininterval, >=) ||
1088 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1089 		*lasttime = tv;
1090 		rv = 1;
1091 	}
1092 
1093 	return (rv);
1094 }
1095 
1096 /*
1097  * eventratecheck(): events per second limitation.
1098  *
1099  * Return 0 if the limit is to be enforced (e.g. the caller
1100  * should ignore the event because of the rate limitation).
1101  *
1102  * maxeps of 0 always causes zero to be returned.  maxeps of -1
1103  * always causes 1 to be returned; this effectively defeats rate
1104  * limiting.
1105  *
1106  * Note that we maintain the struct timeval for compatibility
1107  * with other bsd systems.  We reuse the storage and just monitor
1108  * clock ticks for minimal overhead.
1109  */
1110 int
1111 eventratecheck(struct timeval *lasttime, int *cureps, int maxeps)
1112 {
1113 	int now;
1114 
1115 	/*
1116 	 * Reset the last time and counter if this is the first call
1117 	 * or more than a second has passed since the last update of
1118 	 * lasttime.
1119 	 */
1120 	now = ticks;
1121 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1122 		lasttime->tv_sec = now;
1123 		*cureps = 1;
1124 		return (maxeps != 0);
1125 	} else {
1126 		(*cureps)++;		/* NB: ignore potential overflow */
1127 		return (maxeps < 0 || *cureps <= maxeps);
1128 	}
1129 }
1130 
1131 static void
1132 itimer_start(void)
1133 {
1134 	static const struct kclock rt_clock = {
1135 		.timer_create  = realtimer_create,
1136 		.timer_delete  = realtimer_delete,
1137 		.timer_settime = realtimer_settime,
1138 		.timer_gettime = realtimer_gettime,
1139 	};
1140 
1141 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
1142 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
1143 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
1144 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
1145 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
1146 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
1147 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1148 }
1149 
1150 static int
1151 register_posix_clock(int clockid, const struct kclock *clk)
1152 {
1153 	if ((unsigned)clockid >= MAX_CLOCKS) {
1154 		printf("%s: invalid clockid\n", __func__);
1155 		return (0);
1156 	}
1157 	posix_clocks[clockid] = *clk;
1158 	return (1);
1159 }
1160 
1161 static int
1162 itimer_init(void *mem, int size, int flags)
1163 {
1164 	struct itimer *it;
1165 
1166 	it = (struct itimer *)mem;
1167 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
1168 	return (0);
1169 }
1170 
1171 static void
1172 itimer_fini(void *mem, int size)
1173 {
1174 	struct itimer *it;
1175 
1176 	it = (struct itimer *)mem;
1177 	mtx_destroy(&it->it_mtx);
1178 }
1179 
1180 static void
1181 itimer_enter(struct itimer *it)
1182 {
1183 
1184 	mtx_assert(&it->it_mtx, MA_OWNED);
1185 	it->it_usecount++;
1186 }
1187 
1188 static void
1189 itimer_leave(struct itimer *it)
1190 {
1191 
1192 	mtx_assert(&it->it_mtx, MA_OWNED);
1193 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
1194 
1195 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
1196 		wakeup(it);
1197 }
1198 
1199 #ifndef _SYS_SYSPROTO_H_
1200 struct ktimer_create_args {
1201 	clockid_t clock_id;
1202 	struct sigevent * evp;
1203 	int * timerid;
1204 };
1205 #endif
1206 int
1207 sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
1208 {
1209 	struct sigevent *evp, ev;
1210 	int id;
1211 	int error;
1212 
1213 	if (uap->evp == NULL) {
1214 		evp = NULL;
1215 	} else {
1216 		error = copyin(uap->evp, &ev, sizeof(ev));
1217 		if (error != 0)
1218 			return (error);
1219 		evp = &ev;
1220 	}
1221 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
1222 	if (error == 0) {
1223 		error = copyout(&id, uap->timerid, sizeof(int));
1224 		if (error != 0)
1225 			kern_ktimer_delete(td, id);
1226 	}
1227 	return (error);
1228 }
1229 
1230 int
1231 kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1232     int *timerid, int preset_id)
1233 {
1234 	struct proc *p = td->td_proc;
1235 	struct itimer *it;
1236 	int id;
1237 	int error;
1238 
1239 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
1240 		return (EINVAL);
1241 
1242 	if (posix_clocks[clock_id].timer_create == NULL)
1243 		return (EINVAL);
1244 
1245 	if (evp != NULL) {
1246 		if (evp->sigev_notify != SIGEV_NONE &&
1247 		    evp->sigev_notify != SIGEV_SIGNAL &&
1248 		    evp->sigev_notify != SIGEV_THREAD_ID)
1249 			return (EINVAL);
1250 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
1251 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
1252 			!_SIG_VALID(evp->sigev_signo))
1253 			return (EINVAL);
1254 	}
1255 
1256 	if (p->p_itimers == NULL)
1257 		itimers_alloc(p);
1258 
1259 	it = uma_zalloc(itimer_zone, M_WAITOK);
1260 	it->it_flags = 0;
1261 	it->it_usecount = 0;
1262 	timespecclear(&it->it_time.it_value);
1263 	timespecclear(&it->it_time.it_interval);
1264 	it->it_overrun = 0;
1265 	it->it_overrun_last = 0;
1266 	it->it_clockid = clock_id;
1267 	it->it_proc = p;
1268 	ksiginfo_init(&it->it_ksi);
1269 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1270 	error = CLOCK_CALL(clock_id, timer_create, (it));
1271 	if (error != 0)
1272 		goto out;
1273 
1274 	PROC_LOCK(p);
1275 	if (preset_id != -1) {
1276 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1277 		id = preset_id;
1278 		if (p->p_itimers->its_timers[id] != NULL) {
1279 			PROC_UNLOCK(p);
1280 			error = 0;
1281 			goto out;
1282 		}
1283 	} else {
1284 		/*
1285 		 * Find a free timer slot, skipping those reserved
1286 		 * for setitimer().
1287 		 */
1288 		for (id = 3; id < TIMER_MAX; id++)
1289 			if (p->p_itimers->its_timers[id] == NULL)
1290 				break;
1291 		if (id == TIMER_MAX) {
1292 			PROC_UNLOCK(p);
1293 			error = EAGAIN;
1294 			goto out;
1295 		}
1296 	}
1297 	p->p_itimers->its_timers[id] = it;
1298 	if (evp != NULL)
1299 		it->it_sigev = *evp;
1300 	else {
1301 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1302 		switch (clock_id) {
1303 		default:
1304 		case CLOCK_REALTIME:
1305 			it->it_sigev.sigev_signo = SIGALRM;
1306 			break;
1307 		case CLOCK_VIRTUAL:
1308  			it->it_sigev.sigev_signo = SIGVTALRM;
1309 			break;
1310 		case CLOCK_PROF:
1311 			it->it_sigev.sigev_signo = SIGPROF;
1312 			break;
1313 		}
1314 		it->it_sigev.sigev_value.sival_int = id;
1315 	}
1316 
1317 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1318 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1319 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1320 		it->it_ksi.ksi_code = SI_TIMER;
1321 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1322 		it->it_ksi.ksi_timerid = id;
1323 	}
1324 	PROC_UNLOCK(p);
1325 	*timerid = id;
1326 	return (0);
1327 
1328 out:
1329 	ITIMER_LOCK(it);
1330 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
1331 	ITIMER_UNLOCK(it);
1332 	uma_zfree(itimer_zone, it);
1333 	return (error);
1334 }
1335 
1336 #ifndef _SYS_SYSPROTO_H_
1337 struct ktimer_delete_args {
1338 	int timerid;
1339 };
1340 #endif
1341 int
1342 sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1343 {
1344 
1345 	return (kern_ktimer_delete(td, uap->timerid));
1346 }
1347 
1348 static struct itimer *
1349 itimer_find(struct proc *p, int timerid)
1350 {
1351 	struct itimer *it;
1352 
1353 	PROC_LOCK_ASSERT(p, MA_OWNED);
1354 	if ((p->p_itimers == NULL) ||
1355 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
1356 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
1357 		return (NULL);
1358 	}
1359 	ITIMER_LOCK(it);
1360 	if ((it->it_flags & ITF_DELETING) != 0) {
1361 		ITIMER_UNLOCK(it);
1362 		it = NULL;
1363 	}
1364 	return (it);
1365 }
1366 
1367 int
1368 kern_ktimer_delete(struct thread *td, int timerid)
1369 {
1370 	struct proc *p = td->td_proc;
1371 	struct itimer *it;
1372 
1373 	PROC_LOCK(p);
1374 	it = itimer_find(p, timerid);
1375 	if (it == NULL) {
1376 		PROC_UNLOCK(p);
1377 		return (EINVAL);
1378 	}
1379 	PROC_UNLOCK(p);
1380 
1381 	it->it_flags |= ITF_DELETING;
1382 	while (it->it_usecount > 0) {
1383 		it->it_flags |= ITF_WANTED;
1384 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1385 	}
1386 	it->it_flags &= ~ITF_WANTED;
1387 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
1388 	ITIMER_UNLOCK(it);
1389 
1390 	PROC_LOCK(p);
1391 	if (KSI_ONQ(&it->it_ksi))
1392 		sigqueue_take(&it->it_ksi);
1393 	p->p_itimers->its_timers[timerid] = NULL;
1394 	PROC_UNLOCK(p);
1395 	uma_zfree(itimer_zone, it);
1396 	return (0);
1397 }
1398 
1399 #ifndef _SYS_SYSPROTO_H_
1400 struct ktimer_settime_args {
1401 	int timerid;
1402 	int flags;
1403 	const struct itimerspec * value;
1404 	struct itimerspec * ovalue;
1405 };
1406 #endif
1407 int
1408 sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1409 {
1410 	struct itimerspec val, oval, *ovalp;
1411 	int error;
1412 
1413 	error = copyin(uap->value, &val, sizeof(val));
1414 	if (error != 0)
1415 		return (error);
1416 	ovalp = uap->ovalue != NULL ? &oval : NULL;
1417 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1418 	if (error == 0 && uap->ovalue != NULL)
1419 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1420 	return (error);
1421 }
1422 
1423 int
1424 kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1425     struct itimerspec *val, struct itimerspec *oval)
1426 {
1427 	struct proc *p;
1428 	struct itimer *it;
1429 	int error;
1430 
1431 	p = td->td_proc;
1432 	PROC_LOCK(p);
1433 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1434 		PROC_UNLOCK(p);
1435 		error = EINVAL;
1436 	} else {
1437 		PROC_UNLOCK(p);
1438 		itimer_enter(it);
1439 		error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1440 		    flags, val, oval));
1441 		itimer_leave(it);
1442 		ITIMER_UNLOCK(it);
1443 	}
1444 	return (error);
1445 }
1446 
1447 #ifndef _SYS_SYSPROTO_H_
1448 struct ktimer_gettime_args {
1449 	int timerid;
1450 	struct itimerspec * value;
1451 };
1452 #endif
1453 int
1454 sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1455 {
1456 	struct itimerspec val;
1457 	int error;
1458 
1459 	error = kern_ktimer_gettime(td, uap->timerid, &val);
1460 	if (error == 0)
1461 		error = copyout(&val, uap->value, sizeof(val));
1462 	return (error);
1463 }
1464 
1465 int
1466 kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1467 {
1468 	struct proc *p;
1469 	struct itimer *it;
1470 	int error;
1471 
1472 	p = td->td_proc;
1473 	PROC_LOCK(p);
1474 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1475 		PROC_UNLOCK(p);
1476 		error = EINVAL;
1477 	} else {
1478 		PROC_UNLOCK(p);
1479 		itimer_enter(it);
1480 		error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
1481 		itimer_leave(it);
1482 		ITIMER_UNLOCK(it);
1483 	}
1484 	return (error);
1485 }
1486 
1487 #ifndef _SYS_SYSPROTO_H_
1488 struct timer_getoverrun_args {
1489 	int timerid;
1490 };
1491 #endif
1492 int
1493 sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1494 {
1495 
1496 	return (kern_ktimer_getoverrun(td, uap->timerid));
1497 }
1498 
1499 int
1500 kern_ktimer_getoverrun(struct thread *td, int timer_id)
1501 {
1502 	struct proc *p = td->td_proc;
1503 	struct itimer *it;
1504 	int error ;
1505 
1506 	PROC_LOCK(p);
1507 	if (timer_id < 3 ||
1508 	    (it = itimer_find(p, timer_id)) == NULL) {
1509 		PROC_UNLOCK(p);
1510 		error = EINVAL;
1511 	} else {
1512 		td->td_retval[0] = it->it_overrun_last;
1513 		ITIMER_UNLOCK(it);
1514 		PROC_UNLOCK(p);
1515 		error = 0;
1516 	}
1517 	return (error);
1518 }
1519 
1520 static int
1521 realtimer_create(struct itimer *it)
1522 {
1523 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1524 	return (0);
1525 }
1526 
1527 static int
1528 realtimer_delete(struct itimer *it)
1529 {
1530 	mtx_assert(&it->it_mtx, MA_OWNED);
1531 
1532 	/*
1533 	 * clear timer's value and interval to tell realtimer_expire
1534 	 * to not rearm the timer.
1535 	 */
1536 	timespecclear(&it->it_time.it_value);
1537 	timespecclear(&it->it_time.it_interval);
1538 	ITIMER_UNLOCK(it);
1539 	callout_drain(&it->it_callout);
1540 	ITIMER_LOCK(it);
1541 	return (0);
1542 }
1543 
1544 static int
1545 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1546 {
1547 	struct timespec cts;
1548 
1549 	mtx_assert(&it->it_mtx, MA_OWNED);
1550 
1551 	realtimer_clocktime(it->it_clockid, &cts);
1552 	*ovalue = it->it_time;
1553 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1554 		timespecsub(&ovalue->it_value, &cts, &ovalue->it_value);
1555 		if (ovalue->it_value.tv_sec < 0 ||
1556 		    (ovalue->it_value.tv_sec == 0 &&
1557 		     ovalue->it_value.tv_nsec == 0)) {
1558 			ovalue->it_value.tv_sec  = 0;
1559 			ovalue->it_value.tv_nsec = 1;
1560 		}
1561 	}
1562 	return (0);
1563 }
1564 
1565 static int
1566 realtimer_settime(struct itimer *it, int flags, struct itimerspec *value,
1567     struct itimerspec *ovalue)
1568 {
1569 	struct timespec cts, ts;
1570 	struct timeval tv;
1571 	struct itimerspec val;
1572 
1573 	mtx_assert(&it->it_mtx, MA_OWNED);
1574 
1575 	val = *value;
1576 	if (itimespecfix(&val.it_value))
1577 		return (EINVAL);
1578 
1579 	if (timespecisset(&val.it_value)) {
1580 		if (itimespecfix(&val.it_interval))
1581 			return (EINVAL);
1582 	} else {
1583 		timespecclear(&val.it_interval);
1584 	}
1585 
1586 	if (ovalue != NULL)
1587 		realtimer_gettime(it, ovalue);
1588 
1589 	it->it_time = val;
1590 	if (timespecisset(&val.it_value)) {
1591 		realtimer_clocktime(it->it_clockid, &cts);
1592 		ts = val.it_value;
1593 		if ((flags & TIMER_ABSTIME) == 0) {
1594 			/* Convert to absolute time. */
1595 			timespecadd(&it->it_time.it_value, &cts,
1596 			    &it->it_time.it_value);
1597 		} else {
1598 			timespecsub(&ts, &cts, &ts);
1599 			/*
1600 			 * We don't care if ts is negative, tztohz will
1601 			 * fix it.
1602 			 */
1603 		}
1604 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
1605 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1606 		    it);
1607 	} else {
1608 		callout_stop(&it->it_callout);
1609 	}
1610 
1611 	return (0);
1612 }
1613 
1614 static void
1615 realtimer_clocktime(clockid_t id, struct timespec *ts)
1616 {
1617 	if (id == CLOCK_REALTIME)
1618 		getnanotime(ts);
1619 	else	/* CLOCK_MONOTONIC */
1620 		getnanouptime(ts);
1621 }
1622 
1623 int
1624 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1625 {
1626 	struct itimer *it;
1627 
1628 	PROC_LOCK_ASSERT(p, MA_OWNED);
1629 	it = itimer_find(p, timerid);
1630 	if (it != NULL) {
1631 		ksi->ksi_overrun = it->it_overrun;
1632 		it->it_overrun_last = it->it_overrun;
1633 		it->it_overrun = 0;
1634 		ITIMER_UNLOCK(it);
1635 		return (0);
1636 	}
1637 	return (EINVAL);
1638 }
1639 
1640 static int
1641 itimespecfix(struct timespec *ts)
1642 {
1643 
1644 	if (!timespecvalid_interval(ts))
1645 		return (EINVAL);
1646 	if ((UINT64_MAX - ts->tv_nsec) / NS_PER_SEC < ts->tv_sec)
1647 		return (EINVAL);
1648 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1649 		ts->tv_nsec = tick * 1000;
1650 	return (0);
1651 }
1652 
1653 #define	timespectons(tsp)			\
1654 	((uint64_t)(tsp)->tv_sec * NS_PER_SEC + (tsp)->tv_nsec)
1655 #define	timespecfromns(ns) (struct timespec){	\
1656 	.tv_sec = (ns) / NS_PER_SEC,		\
1657 	.tv_nsec = (ns) % NS_PER_SEC		\
1658 }
1659 
1660 static void
1661 realtimer_expire_l(struct itimer *it, bool proc_locked)
1662 {
1663 	struct timespec cts, ts;
1664 	struct timeval tv;
1665 	struct proc *p;
1666 	uint64_t interval, now, overruns, value;
1667 
1668 	realtimer_clocktime(it->it_clockid, &cts);
1669 	/* Only fire if time is reached. */
1670 	if (timespeccmp(&cts, &it->it_time.it_value, >=)) {
1671 		if (timespecisset(&it->it_time.it_interval)) {
1672 			timespecadd(&it->it_time.it_value,
1673 			    &it->it_time.it_interval,
1674 			    &it->it_time.it_value);
1675 
1676 			interval = timespectons(&it->it_time.it_interval);
1677 			value = timespectons(&it->it_time.it_value);
1678 			now = timespectons(&cts);
1679 
1680 			if (now >= value) {
1681 				/*
1682 				 * We missed at least one period.
1683 				 */
1684 				overruns = howmany(now - value + 1, interval);
1685 				if (it->it_overrun + overruns >=
1686 				    it->it_overrun &&
1687 				    it->it_overrun + overruns <= INT_MAX) {
1688 					it->it_overrun += (int)overruns;
1689 				} else {
1690 					it->it_overrun = INT_MAX;
1691 					it->it_ksi.ksi_errno = ERANGE;
1692 				}
1693 				value =
1694 				    now + interval - (now - value) % interval;
1695 				it->it_time.it_value = timespecfromns(value);
1696 			}
1697 		} else {
1698 			/* single shot timer ? */
1699 			timespecclear(&it->it_time.it_value);
1700 		}
1701 
1702 		p = it->it_proc;
1703 		if (timespecisset(&it->it_time.it_value)) {
1704 			if (P_SHOULDSTOP(p) || P_KILLED(p)) {
1705 				it->it_flags |= ITF_PSTOPPED;
1706 			} else {
1707 				timespecsub(&it->it_time.it_value, &cts, &ts);
1708 				TIMESPEC_TO_TIMEVAL(&tv, &ts);
1709 				callout_reset(&it->it_callout, tvtohz(&tv),
1710 				    realtimer_expire, it);
1711 			}
1712 		}
1713 
1714 		itimer_enter(it);
1715 		ITIMER_UNLOCK(it);
1716 		if (proc_locked)
1717 			PROC_UNLOCK(p);
1718 		itimer_fire(it);
1719 		if (proc_locked)
1720 			PROC_LOCK(p);
1721 		ITIMER_LOCK(it);
1722 		itimer_leave(it);
1723 	} else if (timespecisset(&it->it_time.it_value)) {
1724 		p = it->it_proc;
1725 		if (P_SHOULDSTOP(p) || P_KILLED(p)) {
1726 			it->it_flags |= ITF_PSTOPPED;
1727 		} else {
1728 			ts = it->it_time.it_value;
1729 			timespecsub(&ts, &cts, &ts);
1730 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
1731 			callout_reset(&it->it_callout, tvtohz(&tv),
1732 			    realtimer_expire, it);
1733 		}
1734 	}
1735 }
1736 
1737 /* Timeout callback for realtime timer */
1738 static void
1739 realtimer_expire(void *arg)
1740 {
1741 	realtimer_expire_l(arg, false);
1742 }
1743 
1744 static void
1745 itimer_fire(struct itimer *it)
1746 {
1747 	struct proc *p = it->it_proc;
1748 	struct thread *td;
1749 
1750 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1751 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1752 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
1753 			ITIMER_LOCK(it);
1754 			timespecclear(&it->it_time.it_value);
1755 			timespecclear(&it->it_time.it_interval);
1756 			callout_stop(&it->it_callout);
1757 			ITIMER_UNLOCK(it);
1758 			return;
1759 		}
1760 		if (!KSI_ONQ(&it->it_ksi)) {
1761 			it->it_ksi.ksi_errno = 0;
1762 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1763 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
1764 		} else {
1765 			if (it->it_overrun < INT_MAX)
1766 				it->it_overrun++;
1767 			else
1768 				it->it_ksi.ksi_errno = ERANGE;
1769 		}
1770 		PROC_UNLOCK(p);
1771 	}
1772 }
1773 
1774 static void
1775 itimers_alloc(struct proc *p)
1776 {
1777 	struct itimers *its;
1778 
1779 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1780 	PROC_LOCK(p);
1781 	if (p->p_itimers == NULL) {
1782 		p->p_itimers = its;
1783 		PROC_UNLOCK(p);
1784 	}
1785 	else {
1786 		PROC_UNLOCK(p);
1787 		free(its, M_SUBPROC);
1788 	}
1789 }
1790 
1791 /* Clean up timers when some process events are being triggered. */
1792 static void
1793 itimers_event_exit_exec(int start_idx, struct proc *p)
1794 {
1795 	struct itimers *its;
1796 	struct itimer *it;
1797 	int i;
1798 
1799 	its = p->p_itimers;
1800 	if (its == NULL)
1801 		return;
1802 
1803 	for (i = start_idx; i < TIMER_MAX; ++i) {
1804 		if ((it = its->its_timers[i]) != NULL)
1805 			kern_ktimer_delete(curthread, i);
1806 	}
1807 	if (its->its_timers[0] == NULL && its->its_timers[1] == NULL &&
1808 	    its->its_timers[2] == NULL) {
1809 		/* Synchronize with itimer_proc_continue(). */
1810 		PROC_LOCK(p);
1811 		p->p_itimers = NULL;
1812 		PROC_UNLOCK(p);
1813 		free(its, M_SUBPROC);
1814 	}
1815 }
1816 
1817 void
1818 itimers_exec(struct proc *p)
1819 {
1820 	/*
1821 	 * According to susv3, XSI interval timers should be inherited
1822 	 * by new image.
1823 	 */
1824 	itimers_event_exit_exec(3, p);
1825 }
1826 
1827 void
1828 itimers_exit(struct proc *p)
1829 {
1830 	itimers_event_exit_exec(0, p);
1831 }
1832