xref: /freebsd/sys/kern/kern_time.c (revision bd8621c6cd8a7499e7c8be189cf1ffd102dcc9d1)
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 			KASSERT(error == 0, ("kern_clock_gettime: %d", error));
575 			timespecsub(&ts, &now, &ts);
576 		}
577 		if (ts.tv_sec < 0 || (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
578 			error = EWOULDBLOCK;
579 			break;
580 		}
581 		if (ts.tv_sec > INT32_MAX / 2) {
582 			over = ts.tv_sec - INT32_MAX / 2;
583 			ts.tv_sec -= over;
584 		} else
585 			over = 0;
586 		tmp = tstosbt(ts);
587 		if (precise) {
588 			prec = 0;
589 			sbt = sbinuptime();
590 		} else {
591 			prec = tmp >> tc_precexp;
592 			if (TIMESEL(&sbt, tmp))
593 				sbt += tc_tick_sbt;
594 		}
595 		sbt += tmp;
596 		error = tsleep_sbt(&nanowait[curcpu], PWAIT | PCATCH, "nanslp",
597 		    sbt, prec, C_ABSOLUTE);
598 	} while (error == 0 && is_abs_real && td->td_rtcgen == 0);
599 	td->td_rtcgen = 0;
600 	if (error != EWOULDBLOCK) {
601 		if (TIMESEL(&sbtt, tmp))
602 			sbtt += tc_tick_sbt;
603 		if (sbtt >= sbt)
604 			return (0);
605 		if (error == ERESTART)
606 			error = EINTR;
607 		if ((flags & TIMER_ABSTIME) == 0 && rmt != NULL) {
608 			ts = sbttots(sbt - sbtt);
609 			ts.tv_sec += over;
610 			if (ts.tv_sec < 0)
611 				timespecclear(&ts);
612 			*rmt = ts;
613 		}
614 		return (error);
615 	}
616 	return (0);
617 }
618 
619 #ifndef _SYS_SYSPROTO_H_
620 struct nanosleep_args {
621 	struct	timespec *rqtp;
622 	struct	timespec *rmtp;
623 };
624 #endif
625 /* ARGSUSED */
626 int
sys_nanosleep(struct thread * td,struct nanosleep_args * uap)627 sys_nanosleep(struct thread *td, struct nanosleep_args *uap)
628 {
629 
630 	return (user_clock_nanosleep(td, CLOCK_REALTIME, TIMER_RELTIME,
631 	    uap->rqtp, uap->rmtp));
632 }
633 
634 #ifndef _SYS_SYSPROTO_H_
635 struct clock_nanosleep_args {
636 	clockid_t clock_id;
637 	int 	  flags;
638 	struct	timespec *rqtp;
639 	struct	timespec *rmtp;
640 };
641 #endif
642 /* ARGSUSED */
643 int
sys_clock_nanosleep(struct thread * td,struct clock_nanosleep_args * uap)644 sys_clock_nanosleep(struct thread *td, struct clock_nanosleep_args *uap)
645 {
646 	int error;
647 
648 	error = user_clock_nanosleep(td, uap->clock_id, uap->flags, uap->rqtp,
649 	    uap->rmtp);
650 	return (kern_posix_error(td, error));
651 }
652 
653 static int
user_clock_nanosleep(struct thread * td,clockid_t clock_id,int flags,const struct timespec * ua_rqtp,struct timespec * ua_rmtp)654 user_clock_nanosleep(struct thread *td, clockid_t clock_id, int flags,
655     const struct timespec *ua_rqtp, struct timespec *ua_rmtp)
656 {
657 	struct timespec rmt, rqt;
658 	int error, error2;
659 
660 	error = copyin(ua_rqtp, &rqt, sizeof(rqt));
661 	if (error)
662 		return (error);
663 	error = kern_clock_nanosleep(td, clock_id, flags, &rqt, &rmt);
664 	if (error == EINTR && ua_rmtp != NULL && (flags & TIMER_ABSTIME) == 0) {
665 		error2 = copyout(&rmt, ua_rmtp, sizeof(rmt));
666 		if (error2 != 0)
667 			error = error2;
668 	}
669 	return (error);
670 }
671 
672 #ifndef _SYS_SYSPROTO_H_
673 struct gettimeofday_args {
674 	struct	timeval *tp;
675 	struct	timezone *tzp;
676 };
677 #endif
678 /* ARGSUSED */
679 int
sys_gettimeofday(struct thread * td,struct gettimeofday_args * uap)680 sys_gettimeofday(struct thread *td, struct gettimeofday_args *uap)
681 {
682 	struct timeval atv;
683 	struct timezone rtz;
684 	int error = 0;
685 
686 	if (uap->tp) {
687 		microtime(&atv);
688 		error = copyout(&atv, uap->tp, sizeof (atv));
689 	}
690 	if (error == 0 && uap->tzp != NULL) {
691 		rtz.tz_minuteswest = 0;
692 		rtz.tz_dsttime = 0;
693 		error = copyout(&rtz, uap->tzp, sizeof (rtz));
694 	}
695 	return (error);
696 }
697 
698 #ifndef _SYS_SYSPROTO_H_
699 struct settimeofday_args {
700 	struct	timeval *tv;
701 	struct	timezone *tzp;
702 };
703 #endif
704 /* ARGSUSED */
705 int
sys_settimeofday(struct thread * td,struct settimeofday_args * uap)706 sys_settimeofday(struct thread *td, struct settimeofday_args *uap)
707 {
708 	struct timeval atv, *tvp;
709 	struct timezone atz, *tzp;
710 	int error;
711 
712 	if (uap->tv) {
713 		error = copyin(uap->tv, &atv, sizeof(atv));
714 		if (error)
715 			return (error);
716 		tvp = &atv;
717 	} else
718 		tvp = NULL;
719 	if (uap->tzp) {
720 		error = copyin(uap->tzp, &atz, sizeof(atz));
721 		if (error)
722 			return (error);
723 		tzp = &atz;
724 	} else
725 		tzp = NULL;
726 	return (kern_settimeofday(td, tvp, tzp));
727 }
728 
729 int
kern_settimeofday(struct thread * td,struct timeval * tv,struct timezone * tzp)730 kern_settimeofday(struct thread *td, struct timeval *tv, struct timezone *tzp)
731 {
732 	int error;
733 
734 	error = priv_check(td, PRIV_SETTIMEOFDAY);
735 	if (error)
736 		return (error);
737 	/* Verify all parameters before changing time. */
738 	if (tv) {
739 		if (tv->tv_usec < 0 || tv->tv_usec >= 1000000 ||
740 		    tv->tv_sec < 0)
741 			return (EINVAL);
742 		error = settime(td, tv);
743 	}
744 	return (error);
745 }
746 
747 /*
748  * Get value of an interval timer.  The process virtual and profiling virtual
749  * time timers are kept in the p_stats area, since they can be swapped out.
750  * These are kept internally in the way they are specified externally: in
751  * time until they expire.
752  *
753  * The real time interval timer is kept in the process table slot for the
754  * process, and its value (it_value) is kept as an absolute time rather than
755  * as a delta, so that it is easy to keep periodic real-time signals from
756  * drifting.
757  *
758  * Virtual time timers are processed in the hardclock() routine of
759  * kern_clock.c.  The real time timer is processed by a timeout routine,
760  * called from the softclock() routine.  Since a callout may be delayed in
761  * real time due to interrupt processing in the system, it is possible for
762  * the real time timeout routine (realitexpire, given below), to be delayed
763  * in real time past when it is supposed to occur.  It does not suffice,
764  * therefore, to reload the real timer .it_value from the real time timers
765  * .it_interval.  Rather, we compute the next time in absolute time the timer
766  * should go off.
767  */
768 #ifndef _SYS_SYSPROTO_H_
769 struct getitimer_args {
770 	u_int	which;
771 	struct	itimerval *itv;
772 };
773 #endif
774 int
sys_getitimer(struct thread * td,struct getitimer_args * uap)775 sys_getitimer(struct thread *td, struct getitimer_args *uap)
776 {
777 	struct itimerval aitv;
778 	int error;
779 
780 	error = kern_getitimer(td, uap->which, &aitv);
781 	if (error != 0)
782 		return (error);
783 	return (copyout(&aitv, uap->itv, sizeof (struct itimerval)));
784 }
785 
786 int
kern_getitimer(struct thread * td,u_int which,struct itimerval * aitv)787 kern_getitimer(struct thread *td, u_int which, struct itimerval *aitv)
788 {
789 	struct proc *p = td->td_proc;
790 	struct timeval ctv;
791 
792 	if (which > ITIMER_PROF)
793 		return (EINVAL);
794 
795 	if (which == ITIMER_REAL) {
796 		/*
797 		 * Convert from absolute to relative time in .it_value
798 		 * part of real time timer.  If time for real time timer
799 		 * has passed return 0, else return difference between
800 		 * current time and time for the timer to go off.
801 		 */
802 		PROC_LOCK(p);
803 		*aitv = p->p_realtimer;
804 		PROC_UNLOCK(p);
805 		if (timevalisset(&aitv->it_value)) {
806 			microuptime(&ctv);
807 			if (timevalcmp(&aitv->it_value, &ctv, <))
808 				timevalclear(&aitv->it_value);
809 			else
810 				timevalsub(&aitv->it_value, &ctv);
811 		}
812 	} else {
813 		PROC_ITIMLOCK(p);
814 		*aitv = p->p_stats->p_timer[which];
815 		PROC_ITIMUNLOCK(p);
816 	}
817 #ifdef KTRACE
818 	if (KTRPOINT(td, KTR_STRUCT))
819 		ktritimerval(aitv);
820 #endif
821 	return (0);
822 }
823 
824 #ifndef _SYS_SYSPROTO_H_
825 struct setitimer_args {
826 	u_int	which;
827 	struct	itimerval *itv, *oitv;
828 };
829 #endif
830 int
sys_setitimer(struct thread * td,struct setitimer_args * uap)831 sys_setitimer(struct thread *td, struct setitimer_args *uap)
832 {
833 	struct itimerval aitv, oitv;
834 	int error;
835 
836 	if (uap->itv == NULL) {
837 		uap->itv = uap->oitv;
838 		return (sys_getitimer(td, (struct getitimer_args *)uap));
839 	}
840 
841 	if ((error = copyin(uap->itv, &aitv, sizeof(struct itimerval))))
842 		return (error);
843 	error = kern_setitimer(td, uap->which, &aitv, &oitv);
844 	if (error != 0 || uap->oitv == NULL)
845 		return (error);
846 	return (copyout(&oitv, uap->oitv, sizeof(struct itimerval)));
847 }
848 
849 int
kern_setitimer(struct thread * td,u_int which,struct itimerval * aitv,struct itimerval * oitv)850 kern_setitimer(struct thread *td, u_int which, struct itimerval *aitv,
851     struct itimerval *oitv)
852 {
853 	struct proc *p = td->td_proc;
854 	struct timeval ctv;
855 	sbintime_t sbt, pr;
856 
857 	if (aitv == NULL)
858 		return (kern_getitimer(td, which, oitv));
859 
860 	if (which > ITIMER_PROF)
861 		return (EINVAL);
862 #ifdef KTRACE
863 	if (KTRPOINT(td, KTR_STRUCT))
864 		ktritimerval(aitv);
865 #endif
866 	if (itimerfix(&aitv->it_value) ||
867 	    aitv->it_value.tv_sec > INT32_MAX / 2)
868 		return (EINVAL);
869 	if (!timevalisset(&aitv->it_value))
870 		timevalclear(&aitv->it_interval);
871 	else if (itimerfix(&aitv->it_interval) ||
872 	    aitv->it_interval.tv_sec > INT32_MAX / 2)
873 		return (EINVAL);
874 
875 	if (which == ITIMER_REAL) {
876 		PROC_LOCK(p);
877 		if (timevalisset(&p->p_realtimer.it_value))
878 			callout_stop(&p->p_itcallout);
879 		microuptime(&ctv);
880 		if (timevalisset(&aitv->it_value)) {
881 			pr = tvtosbt(aitv->it_value) >> tc_precexp;
882 			timevaladd(&aitv->it_value, &ctv);
883 			sbt = tvtosbt(aitv->it_value);
884 			callout_reset_sbt(&p->p_itcallout, sbt, pr,
885 			    realitexpire, p, C_ABSOLUTE);
886 		}
887 		*oitv = p->p_realtimer;
888 		p->p_realtimer = *aitv;
889 		PROC_UNLOCK(p);
890 		if (timevalisset(&oitv->it_value)) {
891 			if (timevalcmp(&oitv->it_value, &ctv, <))
892 				timevalclear(&oitv->it_value);
893 			else
894 				timevalsub(&oitv->it_value, &ctv);
895 		}
896 	} else {
897 		if (aitv->it_interval.tv_sec == 0 &&
898 		    aitv->it_interval.tv_usec != 0 &&
899 		    aitv->it_interval.tv_usec < tick)
900 			aitv->it_interval.tv_usec = tick;
901 		if (aitv->it_value.tv_sec == 0 &&
902 		    aitv->it_value.tv_usec != 0 &&
903 		    aitv->it_value.tv_usec < tick)
904 			aitv->it_value.tv_usec = tick;
905 		PROC_ITIMLOCK(p);
906 		*oitv = p->p_stats->p_timer[which];
907 		p->p_stats->p_timer[which] = *aitv;
908 		PROC_ITIMUNLOCK(p);
909 	}
910 #ifdef KTRACE
911 	if (KTRPOINT(td, KTR_STRUCT))
912 		ktritimerval(oitv);
913 #endif
914 	return (0);
915 }
916 
917 static void
realitexpire_reset_callout(struct proc * p,sbintime_t * isbtp)918 realitexpire_reset_callout(struct proc *p, sbintime_t *isbtp)
919 {
920 	sbintime_t prec;
921 
922 	if ((p->p_flag & P_WEXIT) != 0)
923 		return;
924 	prec = isbtp == NULL ? tvtosbt(p->p_realtimer.it_interval) : *isbtp;
925 	callout_reset_sbt(&p->p_itcallout, tvtosbt(p->p_realtimer.it_value),
926 	    prec >> tc_precexp, realitexpire, p, C_ABSOLUTE);
927 }
928 
929 void
itimer_proc_continue(struct proc * p)930 itimer_proc_continue(struct proc *p)
931 {
932 	struct timeval ctv;
933 	struct itimer *it;
934 	int id;
935 
936 	PROC_LOCK_ASSERT(p, MA_OWNED);
937 
938 	if ((p->p_flag2 & P2_ITSTOPPED) != 0) {
939 		p->p_flag2 &= ~P2_ITSTOPPED;
940 		microuptime(&ctv);
941 		if (timevalcmp(&p->p_realtimer.it_value, &ctv, >=))
942 			realitexpire(p);
943 		else
944 			realitexpire_reset_callout(p, NULL);
945 	}
946 
947 	if (p->p_itimers != NULL) {
948 		for (id = 3; id < TIMER_MAX; id++) {
949 			it = p->p_itimers->its_timers[id];
950 			if (it == NULL)
951 				continue;
952 			if ((it->it_flags & ITF_PSTOPPED) != 0) {
953 				ITIMER_LOCK(it);
954 				if ((it->it_flags & ITF_PSTOPPED) != 0) {
955 					it->it_flags &= ~ITF_PSTOPPED;
956 					if ((it->it_flags & ITF_DELETING) == 0)
957 						realtimer_expire_l(it, true);
958 				}
959 				ITIMER_UNLOCK(it);
960 			}
961 		}
962 	}
963 }
964 
965 /*
966  * Real interval timer expired:
967  * send process whose timer expired an alarm signal.
968  * If time is not set up to reload, then just return.
969  * Else compute next time timer should go off which is > current time.
970  * This is where delay in processing this timeout causes multiple
971  * SIGALRM calls to be compressed into one.
972  * tvtohz() always adds 1 to allow for the time until the next clock
973  * interrupt being strictly less than 1 clock tick, but we don't want
974  * that here since we want to appear to be in sync with the clock
975  * interrupt even when we're delayed.
976  */
977 static void
realitexpire(void * arg)978 realitexpire(void *arg)
979 {
980 	struct proc *p;
981 	struct timeval ctv;
982 	sbintime_t isbt;
983 
984 	p = (struct proc *)arg;
985 	kern_psignal(p, SIGALRM);
986 	if (!timevalisset(&p->p_realtimer.it_interval)) {
987 		timevalclear(&p->p_realtimer.it_value);
988 		return;
989 	}
990 
991 	isbt = tvtosbt(p->p_realtimer.it_interval);
992 	if (isbt >= sbt_timethreshold)
993 		getmicrouptime(&ctv);
994 	else
995 		microuptime(&ctv);
996 	do {
997 		timevaladd(&p->p_realtimer.it_value,
998 		    &p->p_realtimer.it_interval);
999 	} while (timevalcmp(&p->p_realtimer.it_value, &ctv, <=));
1000 
1001 	if (P_SHOULDSTOP(p) || P_KILLED(p)) {
1002 		p->p_flag2 |= P2_ITSTOPPED;
1003 		return;
1004 	}
1005 
1006 	p->p_flag2 &= ~P2_ITSTOPPED;
1007 	realitexpire_reset_callout(p, &isbt);
1008 }
1009 
1010 /*
1011  * Check that a proposed value to load into the .it_value or
1012  * .it_interval part of an interval timer is acceptable, and
1013  * fix it to have at least minimal value (i.e. if it is less
1014  * than the resolution of the clock, round it up.)
1015  */
1016 int
itimerfix(struct timeval * tv)1017 itimerfix(struct timeval *tv)
1018 {
1019 
1020 	if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
1021 		return (EINVAL);
1022 	if (tv->tv_sec == 0 && tv->tv_usec != 0 &&
1023 	    tv->tv_usec < (u_int)tick / 16)
1024 		tv->tv_usec = (u_int)tick / 16;
1025 	return (0);
1026 }
1027 
1028 /*
1029  * Decrement an interval timer by a specified number
1030  * of microseconds, which must be less than a second,
1031  * i.e. < 1000000.  If the timer expires, then reload
1032  * it.  In this case, carry over (usec - old value) to
1033  * reduce the value reloaded into the timer so that
1034  * the timer does not drift.  This routine assumes
1035  * that it is called in a context where the timers
1036  * on which it is operating cannot change in value.
1037  */
1038 int
itimerdecr(struct itimerval * itp,int usec)1039 itimerdecr(struct itimerval *itp, int usec)
1040 {
1041 
1042 	if (itp->it_value.tv_usec < usec) {
1043 		if (itp->it_value.tv_sec == 0) {
1044 			/* expired, and already in next interval */
1045 			usec -= itp->it_value.tv_usec;
1046 			goto expire;
1047 		}
1048 		itp->it_value.tv_usec += 1000000;
1049 		itp->it_value.tv_sec--;
1050 	}
1051 	itp->it_value.tv_usec -= usec;
1052 	usec = 0;
1053 	if (timevalisset(&itp->it_value))
1054 		return (1);
1055 	/* expired, exactly at end of interval */
1056 expire:
1057 	if (timevalisset(&itp->it_interval)) {
1058 		itp->it_value = itp->it_interval;
1059 		itp->it_value.tv_usec -= usec;
1060 		if (itp->it_value.tv_usec < 0) {
1061 			itp->it_value.tv_usec += 1000000;
1062 			itp->it_value.tv_sec--;
1063 		}
1064 	} else
1065 		itp->it_value.tv_usec = 0;		/* sec is already 0 */
1066 	return (0);
1067 }
1068 
1069 /*
1070  * Add and subtract routines for timevals.
1071  * N.B.: subtract routine doesn't deal with
1072  * results which are before the beginning,
1073  * it just gets very confused in this case.
1074  * Caveat emptor.
1075  */
1076 void
timevaladd(struct timeval * t1,const struct timeval * t2)1077 timevaladd(struct timeval *t1, const struct timeval *t2)
1078 {
1079 
1080 	t1->tv_sec += t2->tv_sec;
1081 	t1->tv_usec += t2->tv_usec;
1082 	timevalfix(t1);
1083 }
1084 
1085 void
timevalsub(struct timeval * t1,const struct timeval * t2)1086 timevalsub(struct timeval *t1, const struct timeval *t2)
1087 {
1088 
1089 	t1->tv_sec -= t2->tv_sec;
1090 	t1->tv_usec -= t2->tv_usec;
1091 	timevalfix(t1);
1092 }
1093 
1094 static void
timevalfix(struct timeval * t1)1095 timevalfix(struct timeval *t1)
1096 {
1097 
1098 	if (t1->tv_usec < 0) {
1099 		t1->tv_sec--;
1100 		t1->tv_usec += 1000000;
1101 	}
1102 	if (t1->tv_usec >= 1000000) {
1103 		t1->tv_sec++;
1104 		t1->tv_usec -= 1000000;
1105 	}
1106 }
1107 
1108 /*
1109  * ratecheck(): simple time-based rate-limit checking.
1110  */
1111 int
ratecheck(struct timeval * lasttime,const struct timeval * mininterval)1112 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
1113 {
1114 	struct timeval tv, delta;
1115 	int rv = 0;
1116 
1117 	getmicrouptime(&tv);		/* NB: 10ms precision */
1118 	delta = tv;
1119 	timevalsub(&delta, lasttime);
1120 
1121 	/*
1122 	 * check for 0,0 is so that the message will be seen at least once,
1123 	 * even if interval is huge.
1124 	 */
1125 	if (timevalcmp(&delta, mininterval, >=) ||
1126 	    (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
1127 		*lasttime = tv;
1128 		rv = 1;
1129 	}
1130 
1131 	return (rv);
1132 }
1133 
1134 /*
1135  * eventratecheck(): events per second limitation.
1136  *
1137  * Return 0 if the limit is to be enforced (e.g. the caller
1138  * should ignore the event because of the rate limitation).
1139  *
1140  * maxeps of 0 always causes zero to be returned.  maxeps of -1
1141  * always causes 1 to be returned; this effectively defeats rate
1142  * limiting.
1143  *
1144  * Note that we maintain the struct timeval for compatibility
1145  * with other bsd systems.  We reuse the storage and just monitor
1146  * clock ticks for minimal overhead.
1147  */
1148 int
eventratecheck(struct timeval * lasttime,int * cureps,int maxeps)1149 eventratecheck(struct timeval *lasttime, int *cureps, int maxeps)
1150 {
1151 	int now;
1152 
1153 	/*
1154 	 * Reset the last time and counter if this is the first call
1155 	 * or more than a second has passed since the last update of
1156 	 * lasttime.
1157 	 */
1158 	now = ticks;
1159 	if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
1160 		lasttime->tv_sec = now;
1161 		*cureps = 1;
1162 		return (maxeps != 0);
1163 	} else {
1164 		(*cureps)++;		/* NB: ignore potential overflow */
1165 		return (maxeps < 0 || *cureps <= maxeps);
1166 	}
1167 }
1168 
1169 static void
itimer_start(void)1170 itimer_start(void)
1171 {
1172 	static const struct kclock rt_clock = {
1173 		.timer_create  = realtimer_create,
1174 		.timer_delete  = realtimer_delete,
1175 		.timer_settime = realtimer_settime,
1176 		.timer_gettime = realtimer_gettime,
1177 	};
1178 
1179 	itimer_zone = uma_zcreate("itimer", sizeof(struct itimer),
1180 		NULL, NULL, itimer_init, itimer_fini, UMA_ALIGN_PTR, 0);
1181 	register_posix_clock(CLOCK_REALTIME,  &rt_clock);
1182 	register_posix_clock(CLOCK_MONOTONIC, &rt_clock);
1183 	register_posix_clock(CLOCK_TAI, &rt_clock);
1184 	p31b_setcfg(CTL_P1003_1B_TIMERS, 200112L);
1185 	p31b_setcfg(CTL_P1003_1B_DELAYTIMER_MAX, INT_MAX);
1186 	p31b_setcfg(CTL_P1003_1B_TIMER_MAX, TIMER_MAX);
1187 }
1188 
1189 static int
register_posix_clock(int clockid,const struct kclock * clk)1190 register_posix_clock(int clockid, const struct kclock *clk)
1191 {
1192 	if ((unsigned)clockid >= MAX_CLOCKS) {
1193 		printf("%s: invalid clockid\n", __func__);
1194 		return (0);
1195 	}
1196 	posix_clocks[clockid] = *clk;
1197 	return (1);
1198 }
1199 
1200 static int
itimer_init(void * mem,int size,int flags)1201 itimer_init(void *mem, int size, int flags)
1202 {
1203 	struct itimer *it;
1204 
1205 	it = (struct itimer *)mem;
1206 	mtx_init(&it->it_mtx, "itimer lock", NULL, MTX_DEF);
1207 	return (0);
1208 }
1209 
1210 static void
itimer_fini(void * mem,int size)1211 itimer_fini(void *mem, int size)
1212 {
1213 	struct itimer *it;
1214 
1215 	it = (struct itimer *)mem;
1216 	mtx_destroy(&it->it_mtx);
1217 }
1218 
1219 static void
itimer_enter(struct itimer * it)1220 itimer_enter(struct itimer *it)
1221 {
1222 
1223 	mtx_assert(&it->it_mtx, MA_OWNED);
1224 	it->it_usecount++;
1225 }
1226 
1227 static void
itimer_leave(struct itimer * it)1228 itimer_leave(struct itimer *it)
1229 {
1230 
1231 	mtx_assert(&it->it_mtx, MA_OWNED);
1232 	KASSERT(it->it_usecount > 0, ("invalid it_usecount"));
1233 
1234 	if (--it->it_usecount == 0 && (it->it_flags & ITF_WANTED) != 0)
1235 		wakeup(it);
1236 }
1237 
1238 #ifndef _SYS_SYSPROTO_H_
1239 struct ktimer_create_args {
1240 	clockid_t clock_id;
1241 	struct sigevent * evp;
1242 	int * timerid;
1243 };
1244 #endif
1245 int
sys_ktimer_create(struct thread * td,struct ktimer_create_args * uap)1246 sys_ktimer_create(struct thread *td, struct ktimer_create_args *uap)
1247 {
1248 	struct sigevent *evp, ev;
1249 	int id;
1250 	int error;
1251 
1252 	if (uap->evp == NULL) {
1253 		evp = NULL;
1254 	} else {
1255 		error = copyin(uap->evp, &ev, sizeof(ev));
1256 		if (error != 0)
1257 			return (error);
1258 		evp = &ev;
1259 	}
1260 	error = kern_ktimer_create(td, uap->clock_id, evp, &id, -1);
1261 	if (error == 0) {
1262 		error = copyout(&id, uap->timerid, sizeof(int));
1263 		if (error != 0)
1264 			kern_ktimer_delete(td, id);
1265 	}
1266 	return (error);
1267 }
1268 
1269 int
kern_ktimer_create(struct thread * td,clockid_t clock_id,struct sigevent * evp,int * timerid,int preset_id)1270 kern_ktimer_create(struct thread *td, clockid_t clock_id, struct sigevent *evp,
1271     int *timerid, int preset_id)
1272 {
1273 	struct proc *p = td->td_proc;
1274 	struct itimer *it;
1275 	int id;
1276 	int error;
1277 
1278 	if (clock_id < 0 || clock_id >= MAX_CLOCKS)
1279 		return (EINVAL);
1280 
1281 	if (posix_clocks[clock_id].timer_create == NULL)
1282 		return (EINVAL);
1283 
1284 	if (evp != NULL) {
1285 		if (evp->sigev_notify != SIGEV_NONE &&
1286 		    evp->sigev_notify != SIGEV_SIGNAL &&
1287 		    evp->sigev_notify != SIGEV_THREAD_ID)
1288 			return (EINVAL);
1289 		if ((evp->sigev_notify == SIGEV_SIGNAL ||
1290 		     evp->sigev_notify == SIGEV_THREAD_ID) &&
1291 			!_SIG_VALID(evp->sigev_signo))
1292 			return (EINVAL);
1293 	}
1294 
1295 	if (p->p_itimers == NULL)
1296 		itimers_alloc(p);
1297 
1298 	it = uma_zalloc(itimer_zone, M_WAITOK);
1299 	it->it_flags = 0;
1300 	it->it_usecount = 0;
1301 	timespecclear(&it->it_time.it_value);
1302 	timespecclear(&it->it_time.it_interval);
1303 	it->it_overrun = 0;
1304 	it->it_overrun_last = 0;
1305 	it->it_clockid = clock_id;
1306 	it->it_proc = p;
1307 	ksiginfo_init(&it->it_ksi);
1308 	it->it_ksi.ksi_flags |= KSI_INS | KSI_EXT;
1309 	error = CLOCK_CALL(clock_id, timer_create, (it));
1310 	if (error != 0)
1311 		goto out;
1312 
1313 	PROC_LOCK(p);
1314 	if (preset_id != -1) {
1315 		KASSERT(preset_id >= 0 && preset_id < 3, ("invalid preset_id"));
1316 		id = preset_id;
1317 		if (p->p_itimers->its_timers[id] != NULL) {
1318 			PROC_UNLOCK(p);
1319 			error = 0;
1320 			goto out;
1321 		}
1322 	} else {
1323 		/*
1324 		 * Find a free timer slot, skipping those reserved
1325 		 * for setitimer().
1326 		 */
1327 		for (id = 3; id < TIMER_MAX; id++)
1328 			if (p->p_itimers->its_timers[id] == NULL)
1329 				break;
1330 		if (id == TIMER_MAX) {
1331 			PROC_UNLOCK(p);
1332 			error = EAGAIN;
1333 			goto out;
1334 		}
1335 	}
1336 	p->p_itimers->its_timers[id] = it;
1337 	if (evp != NULL)
1338 		it->it_sigev = *evp;
1339 	else {
1340 		it->it_sigev.sigev_notify = SIGEV_SIGNAL;
1341 		switch (clock_id) {
1342 		default:
1343 		case CLOCK_REALTIME:
1344 		case CLOCK_TAI:
1345 			it->it_sigev.sigev_signo = SIGALRM;
1346 			break;
1347 		case CLOCK_VIRTUAL:
1348  			it->it_sigev.sigev_signo = SIGVTALRM;
1349 			break;
1350 		case CLOCK_PROF:
1351 			it->it_sigev.sigev_signo = SIGPROF;
1352 			break;
1353 		}
1354 		it->it_sigev.sigev_value.sival_int = id;
1355 	}
1356 
1357 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1358 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1359 		it->it_ksi.ksi_signo = it->it_sigev.sigev_signo;
1360 		it->it_ksi.ksi_code = SI_TIMER;
1361 		it->it_ksi.ksi_value = it->it_sigev.sigev_value;
1362 		it->it_ksi.ksi_timerid = id;
1363 	}
1364 	PROC_UNLOCK(p);
1365 	*timerid = id;
1366 	return (0);
1367 
1368 out:
1369 	ITIMER_LOCK(it);
1370 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
1371 	ITIMER_UNLOCK(it);
1372 	uma_zfree(itimer_zone, it);
1373 	return (error);
1374 }
1375 
1376 #ifndef _SYS_SYSPROTO_H_
1377 struct ktimer_delete_args {
1378 	int timerid;
1379 };
1380 #endif
1381 int
sys_ktimer_delete(struct thread * td,struct ktimer_delete_args * uap)1382 sys_ktimer_delete(struct thread *td, struct ktimer_delete_args *uap)
1383 {
1384 
1385 	return (kern_ktimer_delete(td, uap->timerid));
1386 }
1387 
1388 static struct itimer *
itimer_find(struct proc * p,int timerid)1389 itimer_find(struct proc *p, int timerid)
1390 {
1391 	struct itimer *it;
1392 
1393 	PROC_LOCK_ASSERT(p, MA_OWNED);
1394 	if ((p->p_itimers == NULL) ||
1395 	    (timerid < 0) || (timerid >= TIMER_MAX) ||
1396 	    (it = p->p_itimers->its_timers[timerid]) == NULL) {
1397 		return (NULL);
1398 	}
1399 	ITIMER_LOCK(it);
1400 	if ((it->it_flags & ITF_DELETING) != 0) {
1401 		ITIMER_UNLOCK(it);
1402 		it = NULL;
1403 	}
1404 	return (it);
1405 }
1406 
1407 int
kern_ktimer_delete(struct thread * td,int timerid)1408 kern_ktimer_delete(struct thread *td, int timerid)
1409 {
1410 	struct proc *p = td->td_proc;
1411 	struct itimer *it;
1412 
1413 	PROC_LOCK(p);
1414 	it = itimer_find(p, timerid);
1415 	if (it == NULL) {
1416 		PROC_UNLOCK(p);
1417 		return (EINVAL);
1418 	}
1419 	PROC_UNLOCK(p);
1420 
1421 	it->it_flags |= ITF_DELETING;
1422 	while (it->it_usecount > 0) {
1423 		it->it_flags |= ITF_WANTED;
1424 		msleep(it, &it->it_mtx, PPAUSE, "itimer", 0);
1425 	}
1426 	it->it_flags &= ~ITF_WANTED;
1427 	CLOCK_CALL(it->it_clockid, timer_delete, (it));
1428 	ITIMER_UNLOCK(it);
1429 
1430 	PROC_LOCK(p);
1431 	if (KSI_ONQ(&it->it_ksi))
1432 		sigqueue_take(&it->it_ksi);
1433 	p->p_itimers->its_timers[timerid] = NULL;
1434 	PROC_UNLOCK(p);
1435 	uma_zfree(itimer_zone, it);
1436 	return (0);
1437 }
1438 
1439 #ifndef _SYS_SYSPROTO_H_
1440 struct ktimer_settime_args {
1441 	int timerid;
1442 	int flags;
1443 	const struct itimerspec * value;
1444 	struct itimerspec * ovalue;
1445 };
1446 #endif
1447 int
sys_ktimer_settime(struct thread * td,struct ktimer_settime_args * uap)1448 sys_ktimer_settime(struct thread *td, struct ktimer_settime_args *uap)
1449 {
1450 	struct itimerspec val, oval, *ovalp;
1451 	int error;
1452 
1453 	error = copyin(uap->value, &val, sizeof(val));
1454 	if (error != 0)
1455 		return (error);
1456 	ovalp = uap->ovalue != NULL ? &oval : NULL;
1457 	error = kern_ktimer_settime(td, uap->timerid, uap->flags, &val, ovalp);
1458 	if (error == 0 && uap->ovalue != NULL)
1459 		error = copyout(ovalp, uap->ovalue, sizeof(*ovalp));
1460 	return (error);
1461 }
1462 
1463 int
kern_ktimer_settime(struct thread * td,int timer_id,int flags,struct itimerspec * val,struct itimerspec * oval)1464 kern_ktimer_settime(struct thread *td, int timer_id, int flags,
1465     struct itimerspec *val, struct itimerspec *oval)
1466 {
1467 	struct proc *p;
1468 	struct itimer *it;
1469 	int error;
1470 
1471 	p = td->td_proc;
1472 	PROC_LOCK(p);
1473 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1474 		PROC_UNLOCK(p);
1475 		error = EINVAL;
1476 	} else {
1477 		PROC_UNLOCK(p);
1478 		itimer_enter(it);
1479 		error = CLOCK_CALL(it->it_clockid, timer_settime, (it,
1480 		    flags, val, oval));
1481 		itimer_leave(it);
1482 		ITIMER_UNLOCK(it);
1483 	}
1484 	return (error);
1485 }
1486 
1487 #ifndef _SYS_SYSPROTO_H_
1488 struct ktimer_gettime_args {
1489 	int timerid;
1490 	struct itimerspec * value;
1491 };
1492 #endif
1493 int
sys_ktimer_gettime(struct thread * td,struct ktimer_gettime_args * uap)1494 sys_ktimer_gettime(struct thread *td, struct ktimer_gettime_args *uap)
1495 {
1496 	struct itimerspec val;
1497 	int error;
1498 
1499 	error = kern_ktimer_gettime(td, uap->timerid, &val);
1500 	if (error == 0)
1501 		error = copyout(&val, uap->value, sizeof(val));
1502 	return (error);
1503 }
1504 
1505 int
kern_ktimer_gettime(struct thread * td,int timer_id,struct itimerspec * val)1506 kern_ktimer_gettime(struct thread *td, int timer_id, struct itimerspec *val)
1507 {
1508 	struct proc *p;
1509 	struct itimer *it;
1510 	int error;
1511 
1512 	p = td->td_proc;
1513 	PROC_LOCK(p);
1514 	if (timer_id < 3 || (it = itimer_find(p, timer_id)) == NULL) {
1515 		PROC_UNLOCK(p);
1516 		error = EINVAL;
1517 	} else {
1518 		PROC_UNLOCK(p);
1519 		itimer_enter(it);
1520 		error = CLOCK_CALL(it->it_clockid, timer_gettime, (it, val));
1521 		itimer_leave(it);
1522 		ITIMER_UNLOCK(it);
1523 	}
1524 	return (error);
1525 }
1526 
1527 #ifndef _SYS_SYSPROTO_H_
1528 struct timer_getoverrun_args {
1529 	int timerid;
1530 };
1531 #endif
1532 int
sys_ktimer_getoverrun(struct thread * td,struct ktimer_getoverrun_args * uap)1533 sys_ktimer_getoverrun(struct thread *td, struct ktimer_getoverrun_args *uap)
1534 {
1535 
1536 	return (kern_ktimer_getoverrun(td, uap->timerid));
1537 }
1538 
1539 int
kern_ktimer_getoverrun(struct thread * td,int timer_id)1540 kern_ktimer_getoverrun(struct thread *td, int timer_id)
1541 {
1542 	struct proc *p = td->td_proc;
1543 	struct itimer *it;
1544 	int error ;
1545 
1546 	PROC_LOCK(p);
1547 	if (timer_id < 3 ||
1548 	    (it = itimer_find(p, timer_id)) == NULL) {
1549 		PROC_UNLOCK(p);
1550 		error = EINVAL;
1551 	} else {
1552 		td->td_retval[0] = it->it_overrun_last;
1553 		ITIMER_UNLOCK(it);
1554 		PROC_UNLOCK(p);
1555 		error = 0;
1556 	}
1557 	return (error);
1558 }
1559 
1560 static int
realtimer_create(struct itimer * it)1561 realtimer_create(struct itimer *it)
1562 {
1563 	callout_init_mtx(&it->it_callout, &it->it_mtx, 0);
1564 	return (0);
1565 }
1566 
1567 static int
realtimer_delete(struct itimer * it)1568 realtimer_delete(struct itimer *it)
1569 {
1570 	mtx_assert(&it->it_mtx, MA_OWNED);
1571 
1572 	/*
1573 	 * clear timer's value and interval to tell realtimer_expire
1574 	 * to not rearm the timer.
1575 	 */
1576 	timespecclear(&it->it_time.it_value);
1577 	timespecclear(&it->it_time.it_interval);
1578 	ITIMER_UNLOCK(it);
1579 	callout_drain(&it->it_callout);
1580 	ITIMER_LOCK(it);
1581 	return (0);
1582 }
1583 
1584 static int
realtimer_gettime(struct itimer * it,struct itimerspec * ovalue)1585 realtimer_gettime(struct itimer *it, struct itimerspec *ovalue)
1586 {
1587 	struct timespec cts;
1588 	int error;
1589 
1590 	mtx_assert(&it->it_mtx, MA_OWNED);
1591 
1592 	error = kern_clock_gettime(curthread, it->it_clockid, &cts);
1593 	if (error != 0)
1594 		return (error);
1595 
1596 	*ovalue = it->it_time;
1597 	if (ovalue->it_value.tv_sec != 0 || ovalue->it_value.tv_nsec != 0) {
1598 		timespecsub(&ovalue->it_value, &cts, &ovalue->it_value);
1599 		if (ovalue->it_value.tv_sec < 0 ||
1600 		    (ovalue->it_value.tv_sec == 0 &&
1601 		     ovalue->it_value.tv_nsec == 0)) {
1602 			ovalue->it_value.tv_sec  = 0;
1603 			ovalue->it_value.tv_nsec = 1;
1604 		}
1605 	}
1606 	return (0);
1607 }
1608 
1609 static int
realtimer_settime(struct itimer * it,int flags,struct itimerspec * value,struct itimerspec * ovalue)1610 realtimer_settime(struct itimer *it, int flags, struct itimerspec *value,
1611     struct itimerspec *ovalue)
1612 {
1613 	struct timespec cts, ts;
1614 	struct timeval tv;
1615 	struct itimerspec val;
1616 	int error;
1617 
1618 	mtx_assert(&it->it_mtx, MA_OWNED);
1619 
1620 	val = *value;
1621 	if (itimespecfix(&val.it_value))
1622 		return (EINVAL);
1623 
1624 	if (timespecisset(&val.it_value)) {
1625 		if (itimespecfix(&val.it_interval))
1626 			return (EINVAL);
1627 	} else {
1628 		timespecclear(&val.it_interval);
1629 	}
1630 
1631 	if (ovalue != NULL)
1632 		realtimer_gettime(it, ovalue);
1633 
1634 	it->it_time = val;
1635 	if (timespecisset(&val.it_value)) {
1636 		error = kern_clock_gettime(curthread, it->it_clockid, &cts);
1637 		if (error != 0)
1638 			return (error);
1639 
1640 		ts = val.it_value;
1641 		if ((flags & TIMER_ABSTIME) == 0) {
1642 			/* Convert to absolute time. */
1643 			timespecadd(&it->it_time.it_value, &cts,
1644 			    &it->it_time.it_value);
1645 		} else {
1646 			timespecsub(&ts, &cts, &ts);
1647 			/*
1648 			 * We don't care if ts is negative, tztohz will
1649 			 * fix it.
1650 			 */
1651 		}
1652 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
1653 		callout_reset(&it->it_callout, tvtohz(&tv), realtimer_expire,
1654 		    it);
1655 	} else {
1656 		callout_stop(&it->it_callout);
1657 	}
1658 
1659 	return (0);
1660 }
1661 
1662 int
itimer_accept(struct proc * p,int timerid,ksiginfo_t * ksi)1663 itimer_accept(struct proc *p, int timerid, ksiginfo_t *ksi)
1664 {
1665 	struct itimer *it;
1666 
1667 	PROC_LOCK_ASSERT(p, MA_OWNED);
1668 	it = itimer_find(p, timerid);
1669 	if (it != NULL) {
1670 		ksi->ksi_overrun = it->it_overrun;
1671 		it->it_overrun_last = it->it_overrun;
1672 		it->it_overrun = 0;
1673 		ITIMER_UNLOCK(it);
1674 		return (0);
1675 	}
1676 	return (EINVAL);
1677 }
1678 
1679 static int
itimespecfix(struct timespec * ts)1680 itimespecfix(struct timespec *ts)
1681 {
1682 
1683 	if (!timespecvalid_interval(ts))
1684 		return (EINVAL);
1685 	if ((UINT64_MAX - ts->tv_nsec) / NS_PER_SEC < ts->tv_sec)
1686 		return (EINVAL);
1687 	if (ts->tv_sec == 0 && ts->tv_nsec != 0 && ts->tv_nsec < tick * 1000)
1688 		ts->tv_nsec = tick * 1000;
1689 	return (0);
1690 }
1691 
1692 #define	timespectons(tsp)			\
1693 	((uint64_t)(tsp)->tv_sec * NS_PER_SEC + (tsp)->tv_nsec)
1694 #define	timespecfromns(ns) (struct timespec){	\
1695 	.tv_sec = (ns) / NS_PER_SEC,		\
1696 	.tv_nsec = (ns) % NS_PER_SEC		\
1697 }
1698 
1699 static void
realtimer_expire_l(struct itimer * it,bool proc_locked)1700 realtimer_expire_l(struct itimer *it, bool proc_locked)
1701 {
1702 	struct timespec cts, ts;
1703 	struct timeval tv;
1704 	struct proc *p;
1705 	uint64_t interval, now, overruns, value;
1706 	int error;
1707 
1708 	error = kern_clock_gettime(curthread, it->it_clockid, &cts);
1709 
1710 	/* Only fire if time is reached. */
1711 	if (error == 0 && timespeccmp(&cts, &it->it_time.it_value, >=)) {
1712 		if (timespecisset(&it->it_time.it_interval)) {
1713 			timespecadd(&it->it_time.it_value,
1714 			    &it->it_time.it_interval,
1715 			    &it->it_time.it_value);
1716 
1717 			interval = timespectons(&it->it_time.it_interval);
1718 			value = timespectons(&it->it_time.it_value);
1719 			now = timespectons(&cts);
1720 
1721 			if (now >= value) {
1722 				/*
1723 				 * We missed at least one period.
1724 				 */
1725 				overruns = howmany(now - value + 1, interval);
1726 				if (it->it_overrun + overruns >=
1727 				    it->it_overrun &&
1728 				    it->it_overrun + overruns <= INT_MAX) {
1729 					it->it_overrun += (int)overruns;
1730 				} else {
1731 					it->it_overrun = INT_MAX;
1732 					it->it_ksi.ksi_errno = ERANGE;
1733 				}
1734 				value =
1735 				    now + interval - (now - value) % interval;
1736 				it->it_time.it_value = timespecfromns(value);
1737 			}
1738 		} else {
1739 			/* single shot timer ? */
1740 			timespecclear(&it->it_time.it_value);
1741 		}
1742 
1743 		p = it->it_proc;
1744 		if (timespecisset(&it->it_time.it_value)) {
1745 			if (P_SHOULDSTOP(p) || P_KILLED(p)) {
1746 				it->it_flags |= ITF_PSTOPPED;
1747 			} else {
1748 				timespecsub(&it->it_time.it_value, &cts, &ts);
1749 				TIMESPEC_TO_TIMEVAL(&tv, &ts);
1750 				callout_reset(&it->it_callout, tvtohz(&tv),
1751 				    realtimer_expire, it);
1752 			}
1753 		}
1754 
1755 		itimer_enter(it);
1756 		ITIMER_UNLOCK(it);
1757 		if (proc_locked)
1758 			PROC_UNLOCK(p);
1759 		itimer_fire(it);
1760 		if (proc_locked)
1761 			PROC_LOCK(p);
1762 		ITIMER_LOCK(it);
1763 		itimer_leave(it);
1764 	} else if (timespecisset(&it->it_time.it_value)) {
1765 		p = it->it_proc;
1766 		if (P_SHOULDSTOP(p) || P_KILLED(p)) {
1767 			it->it_flags |= ITF_PSTOPPED;
1768 		} else {
1769 			ts = it->it_time.it_value;
1770 			timespecsub(&ts, &cts, &ts);
1771 			TIMESPEC_TO_TIMEVAL(&tv, &ts);
1772 			callout_reset(&it->it_callout, tvtohz(&tv),
1773 			    realtimer_expire, it);
1774 		}
1775 	}
1776 }
1777 
1778 /* Timeout callback for realtime timer */
1779 static void
realtimer_expire(void * arg)1780 realtimer_expire(void *arg)
1781 {
1782 	realtimer_expire_l(arg, false);
1783 }
1784 
1785 static void
itimer_fire(struct itimer * it)1786 itimer_fire(struct itimer *it)
1787 {
1788 	struct proc *p = it->it_proc;
1789 	struct thread *td;
1790 
1791 	if (it->it_sigev.sigev_notify == SIGEV_SIGNAL ||
1792 	    it->it_sigev.sigev_notify == SIGEV_THREAD_ID) {
1793 		if (sigev_findtd(p, &it->it_sigev, &td) != 0) {
1794 			ITIMER_LOCK(it);
1795 			timespecclear(&it->it_time.it_value);
1796 			timespecclear(&it->it_time.it_interval);
1797 			callout_stop(&it->it_callout);
1798 			ITIMER_UNLOCK(it);
1799 			return;
1800 		}
1801 		if (!KSI_ONQ(&it->it_ksi)) {
1802 			it->it_ksi.ksi_errno = 0;
1803 			ksiginfo_set_sigev(&it->it_ksi, &it->it_sigev);
1804 			tdsendsignal(p, td, it->it_ksi.ksi_signo, &it->it_ksi);
1805 		} else {
1806 			if (it->it_overrun < INT_MAX)
1807 				it->it_overrun++;
1808 			else
1809 				it->it_ksi.ksi_errno = ERANGE;
1810 		}
1811 		PROC_UNLOCK(p);
1812 	}
1813 }
1814 
1815 static void
itimers_alloc(struct proc * p)1816 itimers_alloc(struct proc *p)
1817 {
1818 	struct itimers *its;
1819 
1820 	its = malloc(sizeof (struct itimers), M_SUBPROC, M_WAITOK | M_ZERO);
1821 	PROC_LOCK(p);
1822 	if (p->p_itimers == NULL) {
1823 		p->p_itimers = its;
1824 		PROC_UNLOCK(p);
1825 	}
1826 	else {
1827 		PROC_UNLOCK(p);
1828 		free(its, M_SUBPROC);
1829 	}
1830 }
1831 
1832 /* Clean up timers when some process events are being triggered. */
1833 static void
itimers_event_exit_exec(int start_idx,struct proc * p)1834 itimers_event_exit_exec(int start_idx, struct proc *p)
1835 {
1836 	struct itimers *its;
1837 	struct itimer *it;
1838 	int i;
1839 
1840 	its = p->p_itimers;
1841 	if (its == NULL)
1842 		return;
1843 
1844 	for (i = start_idx; i < TIMER_MAX; ++i) {
1845 		if ((it = its->its_timers[i]) != NULL)
1846 			kern_ktimer_delete(curthread, i);
1847 	}
1848 	if (its->its_timers[0] == NULL && its->its_timers[1] == NULL &&
1849 	    its->its_timers[2] == NULL) {
1850 		/* Synchronize with itimer_proc_continue(). */
1851 		PROC_LOCK(p);
1852 		p->p_itimers = NULL;
1853 		PROC_UNLOCK(p);
1854 		free(its, M_SUBPROC);
1855 	}
1856 }
1857 
1858 void
itimers_exec(struct proc * p)1859 itimers_exec(struct proc *p)
1860 {
1861 	/*
1862 	 * According to susv3, XSI interval timers should be inherited
1863 	 * by new image.
1864 	 */
1865 	itimers_event_exit_exec(3, p);
1866 }
1867 
1868 void
itimers_exit(struct proc * p)1869 itimers_exit(struct proc *p)
1870 {
1871 	itimers_event_exit_exec(0, p);
1872 }
1873