kern_time.c revision 1.28 1 1.28 jtc /* $NetBSD: kern_time.c,v 1.28 1997/04/21 16:56:54 jtc Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.8 cgd * Copyright (c) 1982, 1986, 1989, 1993
5 1.8 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.9 cgd * @(#)kern_time.c 8.1 (Berkeley) 6/10/93
36 1.1 cgd */
37 1.1 cgd
38 1.5 mycroft #include <sys/param.h>
39 1.5 mycroft #include <sys/resourcevar.h>
40 1.5 mycroft #include <sys/kernel.h>
41 1.8 cgd #include <sys/systm.h>
42 1.5 mycroft #include <sys/proc.h>
43 1.8 cgd #include <sys/vnode.h>
44 1.17 christos #include <sys/signalvar.h>
45 1.25 perry #include <sys/syslog.h>
46 1.1 cgd
47 1.11 cgd #include <sys/mount.h>
48 1.11 cgd #include <sys/syscallargs.h>
49 1.19 christos
50 1.26 thorpej #if defined(NFS) || defined(NFSSERVER)
51 1.20 fvdl #include <nfs/rpcv2.h>
52 1.20 fvdl #include <nfs/nfsproto.h>
53 1.19 christos #include <nfs/nfs_var.h>
54 1.19 christos #endif
55 1.17 christos
56 1.5 mycroft #include <machine/cpu.h>
57 1.1 cgd
58 1.23 cgd static void settime __P((struct timeval *));
59 1.23 cgd
60 1.23 cgd /*
61 1.1 cgd * Time of day and interval timer support.
62 1.1 cgd *
63 1.1 cgd * These routines provide the kernel entry points to get and set
64 1.1 cgd * the time-of-day and per-process interval timers. Subroutines
65 1.1 cgd * here provide support for adding and subtracting timeval structures
66 1.1 cgd * and decrementing interval timers, optionally reloading the interval
67 1.1 cgd * timers when they expire.
68 1.1 cgd */
69 1.1 cgd
70 1.22 jtc /* This function is used by clock_settime and settimeofday */
71 1.22 jtc static void
72 1.22 jtc settime(tv)
73 1.22 jtc struct timeval *tv;
74 1.22 jtc {
75 1.22 jtc struct timeval delta;
76 1.22 jtc int s;
77 1.22 jtc
78 1.22 jtc /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
79 1.22 jtc s = splclock();
80 1.22 jtc timersub(tv, &time, &delta);
81 1.22 jtc time = *tv;
82 1.22 jtc (void) splsoftclock();
83 1.22 jtc timeradd(&boottime, &delta, &boottime);
84 1.22 jtc timeradd(&runtime, &delta, &runtime);
85 1.26 thorpej # if defined(NFS) || defined(NFSSERVER)
86 1.22 jtc nqnfs_lease_updatetime(delta.tv_sec);
87 1.22 jtc # endif
88 1.22 jtc splx(s);
89 1.22 jtc resettodr();
90 1.22 jtc }
91 1.22 jtc
92 1.22 jtc /* ARGSUSED */
93 1.22 jtc int
94 1.22 jtc sys_clock_gettime(p, v, retval)
95 1.22 jtc struct proc *p;
96 1.22 jtc void *v;
97 1.22 jtc register_t *retval;
98 1.22 jtc {
99 1.22 jtc register struct sys_clock_gettime_args /* {
100 1.22 jtc syscallarg(clockid_t) clock_id;
101 1.23 cgd syscallarg(struct timespec *) tp;
102 1.23 cgd } */ *uap = v;
103 1.22 jtc clockid_t clock_id;
104 1.22 jtc struct timeval atv;
105 1.22 jtc struct timespec ats;
106 1.22 jtc
107 1.22 jtc clock_id = SCARG(uap, clock_id);
108 1.22 jtc if (clock_id != CLOCK_REALTIME)
109 1.22 jtc return (EINVAL);
110 1.22 jtc
111 1.22 jtc microtime(&atv);
112 1.22 jtc TIMEVAL_TO_TIMESPEC(&atv,&ats);
113 1.22 jtc
114 1.24 cgd return copyout(&ats, SCARG(uap, tp), sizeof(ats));
115 1.22 jtc }
116 1.22 jtc
117 1.22 jtc /* ARGSUSED */
118 1.22 jtc int
119 1.22 jtc sys_clock_settime(p, v, retval)
120 1.22 jtc struct proc *p;
121 1.22 jtc void *v;
122 1.22 jtc register_t *retval;
123 1.22 jtc {
124 1.22 jtc register struct sys_clock_settime_args /* {
125 1.22 jtc syscallarg(clockid_t) clock_id;
126 1.23 cgd syscallarg(const struct timespec *) tp;
127 1.23 cgd } */ *uap = v;
128 1.22 jtc clockid_t clock_id;
129 1.22 jtc struct timeval atv;
130 1.22 jtc struct timespec ats;
131 1.22 jtc int error;
132 1.22 jtc
133 1.22 jtc if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
134 1.22 jtc return (error);
135 1.22 jtc
136 1.22 jtc clock_id = SCARG(uap, clock_id);
137 1.22 jtc if (clock_id != CLOCK_REALTIME)
138 1.22 jtc return (EINVAL);
139 1.22 jtc
140 1.24 cgd if ((error = copyin(SCARG(uap, tp), &ats, sizeof(ats))) != 0)
141 1.23 cgd return (error);
142 1.22 jtc
143 1.22 jtc TIMESPEC_TO_TIMEVAL(&atv,&ats);
144 1.22 jtc settime(&atv);
145 1.22 jtc
146 1.22 jtc return 0;
147 1.22 jtc }
148 1.22 jtc
149 1.22 jtc int
150 1.22 jtc sys_clock_getres(p, v, retval)
151 1.22 jtc struct proc *p;
152 1.22 jtc void *v;
153 1.22 jtc register_t *retval;
154 1.22 jtc {
155 1.22 jtc register struct sys_clock_getres_args /* {
156 1.22 jtc syscallarg(clockid_t) clock_id;
157 1.23 cgd syscallarg(struct timespec *) tp;
158 1.23 cgd } */ *uap = v;
159 1.22 jtc clockid_t clock_id;
160 1.22 jtc struct timespec ts;
161 1.22 jtc int error = 0;
162 1.22 jtc
163 1.22 jtc clock_id = SCARG(uap, clock_id);
164 1.22 jtc if (clock_id != CLOCK_REALTIME)
165 1.22 jtc return (EINVAL);
166 1.22 jtc
167 1.22 jtc if (SCARG(uap, tp)) {
168 1.22 jtc ts.tv_sec = 0;
169 1.22 jtc ts.tv_nsec = 1000000000 / hz;
170 1.22 jtc
171 1.24 cgd error = copyout(&ts, SCARG(uap, tp), sizeof (ts));
172 1.22 jtc }
173 1.22 jtc
174 1.22 jtc return error;
175 1.22 jtc }
176 1.22 jtc
177 1.27 jtc /* ARGSUSED */
178 1.27 jtc int
179 1.27 jtc sys_nanosleep(p, v, retval)
180 1.27 jtc struct proc *p;
181 1.27 jtc void *v;
182 1.27 jtc register_t *retval;
183 1.27 jtc {
184 1.27 jtc static int nanowait;
185 1.27 jtc register struct sys_nanosleep_args/* {
186 1.27 jtc syscallarg(struct timespec *) rqtp;
187 1.27 jtc syscallarg(struct timespec *) rmtp;
188 1.27 jtc } */ *uap = v;
189 1.27 jtc struct timespec rqt;
190 1.27 jtc struct timespec rmt;
191 1.27 jtc struct timeval atv, utv;
192 1.27 jtc int error, s, timo;
193 1.27 jtc
194 1.27 jtc error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
195 1.27 jtc sizeof(struct timespec));
196 1.27 jtc if (error)
197 1.27 jtc return (error);
198 1.27 jtc
199 1.27 jtc TIMESPEC_TO_TIMEVAL(&atv,&rqt)
200 1.27 jtc if (itimerfix(&atv))
201 1.27 jtc return (EINVAL);
202 1.27 jtc
203 1.27 jtc s = splclock();
204 1.27 jtc timeradd(&atv,&time,&atv);
205 1.27 jtc timo = hzto(&atv);
206 1.27 jtc /*
207 1.27 jtc * Avoid inadvertantly sleeping forever
208 1.27 jtc */
209 1.27 jtc if (timo == 0)
210 1.27 jtc timo = 1;
211 1.27 jtc splx(s);
212 1.27 jtc
213 1.27 jtc error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
214 1.27 jtc if (error == ERESTART)
215 1.27 jtc error = EINTR;
216 1.27 jtc if (error == EWOULDBLOCK)
217 1.27 jtc error = 0;
218 1.27 jtc
219 1.27 jtc if (SCARG(uap, rmtp)) {
220 1.28 jtc int error;
221 1.28 jtc
222 1.27 jtc s = splclock();
223 1.27 jtc utv = time;
224 1.27 jtc splx(s);
225 1.27 jtc
226 1.27 jtc timersub(&atv, &utv, &utv);
227 1.27 jtc if (utv.tv_sec < 0)
228 1.27 jtc timerclear(&utv);
229 1.27 jtc
230 1.27 jtc TIMEVAL_TO_TIMESPEC(&utv,&rmt);
231 1.27 jtc error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
232 1.28 jtc sizeof(rmt));
233 1.28 jtc if (error)
234 1.28 jtc return (error);
235 1.27 jtc }
236 1.27 jtc
237 1.27 jtc return error;
238 1.27 jtc }
239 1.22 jtc
240 1.1 cgd /* ARGSUSED */
241 1.3 andrew int
242 1.16 mycroft sys_gettimeofday(p, v, retval)
243 1.1 cgd struct proc *p;
244 1.15 thorpej void *v;
245 1.15 thorpej register_t *retval;
246 1.15 thorpej {
247 1.16 mycroft register struct sys_gettimeofday_args /* {
248 1.11 cgd syscallarg(struct timeval *) tp;
249 1.11 cgd syscallarg(struct timezone *) tzp;
250 1.15 thorpej } */ *uap = v;
251 1.1 cgd struct timeval atv;
252 1.1 cgd int error = 0;
253 1.25 perry struct timezone tzfake;
254 1.1 cgd
255 1.11 cgd if (SCARG(uap, tp)) {
256 1.1 cgd microtime(&atv);
257 1.24 cgd error = copyout(&atv, SCARG(uap, tp), sizeof (atv));
258 1.17 christos if (error)
259 1.1 cgd return (error);
260 1.1 cgd }
261 1.25 perry if (SCARG(uap, tzp)) {
262 1.25 perry /*
263 1.25 perry * NetBSD has no kernel notion of timezone, so we just
264 1.25 perry * fake up a timezone struct and return it if demanded.
265 1.25 perry */
266 1.25 perry tzfake.tz_minuteswest = 0;
267 1.25 perry tzfake.tz_dsttime = 0;
268 1.25 perry error = copyout(&tzfake, SCARG(uap, tzp), sizeof (tzfake));
269 1.25 perry }
270 1.1 cgd return (error);
271 1.1 cgd }
272 1.1 cgd
273 1.1 cgd /* ARGSUSED */
274 1.3 andrew int
275 1.16 mycroft sys_settimeofday(p, v, retval)
276 1.1 cgd struct proc *p;
277 1.15 thorpej void *v;
278 1.15 thorpej register_t *retval;
279 1.15 thorpej {
280 1.16 mycroft struct sys_settimeofday_args /* {
281 1.24 cgd syscallarg(const struct timeval *) tv;
282 1.24 cgd syscallarg(const struct timezone *) tzp;
283 1.15 thorpej } */ *uap = v;
284 1.22 jtc struct timeval atv;
285 1.1 cgd struct timezone atz;
286 1.22 jtc int error;
287 1.1 cgd
288 1.17 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
289 1.1 cgd return (error);
290 1.8 cgd /* Verify all parameters before changing time. */
291 1.24 cgd if (SCARG(uap, tv) && (error = copyin(SCARG(uap, tv),
292 1.24 cgd &atv, sizeof(atv))))
293 1.8 cgd return (error);
294 1.25 perry /* XXX since we don't use tz, probably no point in doing copyin. */
295 1.24 cgd if (SCARG(uap, tzp) && (error = copyin(SCARG(uap, tzp),
296 1.24 cgd &atz, sizeof(atz))))
297 1.8 cgd return (error);
298 1.22 jtc if (SCARG(uap, tv))
299 1.22 jtc settime(&atv);
300 1.25 perry /*
301 1.25 perry * NetBSD has no kernel notion of timezone, and only an
302 1.25 perry * obsolete program would try to set it, so we log a warning.
303 1.25 perry */
304 1.11 cgd if (SCARG(uap, tzp))
305 1.25 perry log(LOG_WARNING, "pid %d attempted to set the "
306 1.25 perry "(obsolete) kernel timezone.", p->p_pid);
307 1.8 cgd return (0);
308 1.1 cgd }
309 1.1 cgd
310 1.1 cgd int tickdelta; /* current clock skew, us. per tick */
311 1.1 cgd long timedelta; /* unapplied time correction, us. */
312 1.1 cgd long bigadj = 1000000; /* use 10x skew above bigadj us. */
313 1.1 cgd
314 1.1 cgd /* ARGSUSED */
315 1.3 andrew int
316 1.16 mycroft sys_adjtime(p, v, retval)
317 1.1 cgd struct proc *p;
318 1.15 thorpej void *v;
319 1.15 thorpej register_t *retval;
320 1.15 thorpej {
321 1.16 mycroft register struct sys_adjtime_args /* {
322 1.24 cgd syscallarg(const struct timeval *) delta;
323 1.11 cgd syscallarg(struct timeval *) olddelta;
324 1.15 thorpej } */ *uap = v;
325 1.8 cgd struct timeval atv;
326 1.8 cgd register long ndelta, ntickdelta, odelta;
327 1.1 cgd int s, error;
328 1.1 cgd
329 1.17 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
330 1.1 cgd return (error);
331 1.17 christos
332 1.24 cgd error = copyin(SCARG(uap, delta), &atv, sizeof(struct timeval));
333 1.17 christos if (error)
334 1.1 cgd return (error);
335 1.8 cgd
336 1.8 cgd /*
337 1.8 cgd * Compute the total correction and the rate at which to apply it.
338 1.8 cgd * Round the adjustment down to a whole multiple of the per-tick
339 1.8 cgd * delta, so that after some number of incremental changes in
340 1.8 cgd * hardclock(), tickdelta will become zero, lest the correction
341 1.8 cgd * overshoot and start taking us away from the desired final time.
342 1.8 cgd */
343 1.1 cgd ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
344 1.8 cgd if (ndelta > bigadj)
345 1.8 cgd ntickdelta = 10 * tickadj;
346 1.8 cgd else
347 1.8 cgd ntickdelta = tickadj;
348 1.8 cgd if (ndelta % ntickdelta)
349 1.8 cgd ndelta = ndelta / ntickdelta * ntickdelta;
350 1.8 cgd
351 1.8 cgd /*
352 1.8 cgd * To make hardclock()'s job easier, make the per-tick delta negative
353 1.8 cgd * if we want time to run slower; then hardclock can simply compute
354 1.8 cgd * tick + tickdelta, and subtract tickdelta from timedelta.
355 1.8 cgd */
356 1.8 cgd if (ndelta < 0)
357 1.8 cgd ntickdelta = -ntickdelta;
358 1.1 cgd s = splclock();
359 1.8 cgd odelta = timedelta;
360 1.1 cgd timedelta = ndelta;
361 1.8 cgd tickdelta = ntickdelta;
362 1.1 cgd splx(s);
363 1.1 cgd
364 1.11 cgd if (SCARG(uap, olddelta)) {
365 1.8 cgd atv.tv_sec = odelta / 1000000;
366 1.8 cgd atv.tv_usec = odelta % 1000000;
367 1.24 cgd (void) copyout(&atv, SCARG(uap, olddelta),
368 1.8 cgd sizeof(struct timeval));
369 1.8 cgd }
370 1.1 cgd return (0);
371 1.1 cgd }
372 1.1 cgd
373 1.1 cgd /*
374 1.1 cgd * Get value of an interval timer. The process virtual and
375 1.1 cgd * profiling virtual time timers are kept in the p_stats area, since
376 1.1 cgd * they can be swapped out. These are kept internally in the
377 1.1 cgd * way they are specified externally: in time until they expire.
378 1.1 cgd *
379 1.1 cgd * The real time interval timer is kept in the process table slot
380 1.1 cgd * for the process, and its value (it_value) is kept as an
381 1.1 cgd * absolute time rather than as a delta, so that it is easy to keep
382 1.1 cgd * periodic real-time signals from drifting.
383 1.1 cgd *
384 1.1 cgd * Virtual time timers are processed in the hardclock() routine of
385 1.1 cgd * kern_clock.c. The real time timer is processed by a timeout
386 1.1 cgd * routine, called from the softclock() routine. Since a callout
387 1.1 cgd * may be delayed in real time due to interrupt processing in the system,
388 1.1 cgd * it is possible for the real time timeout routine (realitexpire, given below),
389 1.1 cgd * to be delayed in real time past when it is supposed to occur. It
390 1.1 cgd * does not suffice, therefore, to reload the real timer .it_value from the
391 1.1 cgd * real time timers .it_interval. Rather, we compute the next time in
392 1.1 cgd * absolute time the timer should go off.
393 1.1 cgd */
394 1.1 cgd /* ARGSUSED */
395 1.3 andrew int
396 1.16 mycroft sys_getitimer(p, v, retval)
397 1.1 cgd struct proc *p;
398 1.15 thorpej void *v;
399 1.15 thorpej register_t *retval;
400 1.15 thorpej {
401 1.16 mycroft register struct sys_getitimer_args /* {
402 1.11 cgd syscallarg(u_int) which;
403 1.11 cgd syscallarg(struct itimerval *) itv;
404 1.15 thorpej } */ *uap = v;
405 1.1 cgd struct itimerval aitv;
406 1.1 cgd int s;
407 1.1 cgd
408 1.11 cgd if (SCARG(uap, which) > ITIMER_PROF)
409 1.1 cgd return (EINVAL);
410 1.1 cgd s = splclock();
411 1.11 cgd if (SCARG(uap, which) == ITIMER_REAL) {
412 1.1 cgd /*
413 1.12 mycroft * Convert from absolute to relative time in .it_value
414 1.1 cgd * part of real time timer. If time for real time timer
415 1.1 cgd * has passed return 0, else return difference between
416 1.1 cgd * current time and time for the timer to go off.
417 1.1 cgd */
418 1.1 cgd aitv = p->p_realtimer;
419 1.1 cgd if (timerisset(&aitv.it_value))
420 1.1 cgd if (timercmp(&aitv.it_value, &time, <))
421 1.1 cgd timerclear(&aitv.it_value);
422 1.1 cgd else
423 1.14 mycroft timersub(&aitv.it_value, &time, &aitv.it_value);
424 1.1 cgd } else
425 1.11 cgd aitv = p->p_stats->p_timer[SCARG(uap, which)];
426 1.1 cgd splx(s);
427 1.24 cgd return (copyout(&aitv, SCARG(uap, itv), sizeof (struct itimerval)));
428 1.1 cgd }
429 1.1 cgd
430 1.1 cgd /* ARGSUSED */
431 1.3 andrew int
432 1.16 mycroft sys_setitimer(p, v, retval)
433 1.1 cgd struct proc *p;
434 1.17 christos register void *v;
435 1.15 thorpej register_t *retval;
436 1.15 thorpej {
437 1.16 mycroft register struct sys_setitimer_args /* {
438 1.11 cgd syscallarg(u_int) which;
439 1.24 cgd syscallarg(const struct itimerval *) itv;
440 1.11 cgd syscallarg(struct itimerval *) oitv;
441 1.15 thorpej } */ *uap = v;
442 1.21 cgd struct sys_getitimer_args getargs;
443 1.1 cgd struct itimerval aitv;
444 1.24 cgd register const struct itimerval *itvp;
445 1.1 cgd int s, error;
446 1.1 cgd
447 1.11 cgd if (SCARG(uap, which) > ITIMER_PROF)
448 1.1 cgd return (EINVAL);
449 1.11 cgd itvp = SCARG(uap, itv);
450 1.24 cgd if (itvp && (error = copyin(itvp, &aitv, sizeof(struct itimerval))))
451 1.1 cgd return (error);
452 1.21 cgd if (SCARG(uap, oitv) != NULL) {
453 1.21 cgd SCARG(&getargs, which) = SCARG(uap, which);
454 1.21 cgd SCARG(&getargs, itv) = SCARG(uap, oitv);
455 1.23 cgd if ((error = sys_getitimer(p, &getargs, retval)) != 0)
456 1.21 cgd return (error);
457 1.21 cgd }
458 1.1 cgd if (itvp == 0)
459 1.1 cgd return (0);
460 1.1 cgd if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
461 1.1 cgd return (EINVAL);
462 1.1 cgd s = splclock();
463 1.11 cgd if (SCARG(uap, which) == ITIMER_REAL) {
464 1.7 mycroft untimeout(realitexpire, p);
465 1.1 cgd if (timerisset(&aitv.it_value)) {
466 1.14 mycroft timeradd(&aitv.it_value, &time, &aitv.it_value);
467 1.7 mycroft timeout(realitexpire, p, hzto(&aitv.it_value));
468 1.1 cgd }
469 1.1 cgd p->p_realtimer = aitv;
470 1.1 cgd } else
471 1.11 cgd p->p_stats->p_timer[SCARG(uap, which)] = aitv;
472 1.1 cgd splx(s);
473 1.1 cgd return (0);
474 1.1 cgd }
475 1.1 cgd
476 1.1 cgd /*
477 1.1 cgd * Real interval timer expired:
478 1.1 cgd * send process whose timer expired an alarm signal.
479 1.1 cgd * If time is not set up to reload, then just return.
480 1.1 cgd * Else compute next time timer should go off which is > current time.
481 1.1 cgd * This is where delay in processing this timeout causes multiple
482 1.1 cgd * SIGALRM calls to be compressed into one.
483 1.1 cgd */
484 1.3 andrew void
485 1.6 cgd realitexpire(arg)
486 1.6 cgd void *arg;
487 1.6 cgd {
488 1.1 cgd register struct proc *p;
489 1.1 cgd int s;
490 1.1 cgd
491 1.6 cgd p = (struct proc *)arg;
492 1.1 cgd psignal(p, SIGALRM);
493 1.1 cgd if (!timerisset(&p->p_realtimer.it_interval)) {
494 1.1 cgd timerclear(&p->p_realtimer.it_value);
495 1.1 cgd return;
496 1.1 cgd }
497 1.1 cgd for (;;) {
498 1.1 cgd s = splclock();
499 1.14 mycroft timeradd(&p->p_realtimer.it_value,
500 1.14 mycroft &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
501 1.1 cgd if (timercmp(&p->p_realtimer.it_value, &time, >)) {
502 1.7 mycroft timeout(realitexpire, p,
503 1.1 cgd hzto(&p->p_realtimer.it_value));
504 1.1 cgd splx(s);
505 1.1 cgd return;
506 1.1 cgd }
507 1.1 cgd splx(s);
508 1.1 cgd }
509 1.1 cgd }
510 1.1 cgd
511 1.1 cgd /*
512 1.1 cgd * Check that a proposed value to load into the .it_value or
513 1.1 cgd * .it_interval part of an interval timer is acceptable, and
514 1.1 cgd * fix it to have at least minimal value (i.e. if it is less
515 1.1 cgd * than the resolution of the clock, round it up.)
516 1.1 cgd */
517 1.3 andrew int
518 1.1 cgd itimerfix(tv)
519 1.1 cgd struct timeval *tv;
520 1.1 cgd {
521 1.1 cgd
522 1.1 cgd if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
523 1.1 cgd tv->tv_usec < 0 || tv->tv_usec >= 1000000)
524 1.1 cgd return (EINVAL);
525 1.1 cgd if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
526 1.1 cgd tv->tv_usec = tick;
527 1.1 cgd return (0);
528 1.1 cgd }
529 1.1 cgd
530 1.1 cgd /*
531 1.1 cgd * Decrement an interval timer by a specified number
532 1.1 cgd * of microseconds, which must be less than a second,
533 1.1 cgd * i.e. < 1000000. If the timer expires, then reload
534 1.1 cgd * it. In this case, carry over (usec - old value) to
535 1.8 cgd * reduce the value reloaded into the timer so that
536 1.1 cgd * the timer does not drift. This routine assumes
537 1.1 cgd * that it is called in a context where the timers
538 1.1 cgd * on which it is operating cannot change in value.
539 1.1 cgd */
540 1.3 andrew int
541 1.1 cgd itimerdecr(itp, usec)
542 1.1 cgd register struct itimerval *itp;
543 1.1 cgd int usec;
544 1.1 cgd {
545 1.1 cgd
546 1.1 cgd if (itp->it_value.tv_usec < usec) {
547 1.1 cgd if (itp->it_value.tv_sec == 0) {
548 1.1 cgd /* expired, and already in next interval */
549 1.1 cgd usec -= itp->it_value.tv_usec;
550 1.1 cgd goto expire;
551 1.1 cgd }
552 1.1 cgd itp->it_value.tv_usec += 1000000;
553 1.1 cgd itp->it_value.tv_sec--;
554 1.1 cgd }
555 1.1 cgd itp->it_value.tv_usec -= usec;
556 1.1 cgd usec = 0;
557 1.1 cgd if (timerisset(&itp->it_value))
558 1.1 cgd return (1);
559 1.1 cgd /* expired, exactly at end of interval */
560 1.1 cgd expire:
561 1.1 cgd if (timerisset(&itp->it_interval)) {
562 1.1 cgd itp->it_value = itp->it_interval;
563 1.1 cgd itp->it_value.tv_usec -= usec;
564 1.1 cgd if (itp->it_value.tv_usec < 0) {
565 1.1 cgd itp->it_value.tv_usec += 1000000;
566 1.1 cgd itp->it_value.tv_sec--;
567 1.1 cgd }
568 1.1 cgd } else
569 1.1 cgd itp->it_value.tv_usec = 0; /* sec is already 0 */
570 1.1 cgd return (0);
571 1.1 cgd }
572