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