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