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