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