kern_time.c revision 1.61.10.2 1 1.61.10.2 tron /* $NetBSD: kern_time.c,v 1.61.10.2 2005/12/07 10:43:07 tron 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.58 lukem
74 1.58 lukem #include <sys/cdefs.h>
75 1.61.10.2 tron __KERNEL_RCSID(0, "$NetBSD: kern_time.c,v 1.61.10.2 2005/12/07 10:43:07 tron Exp $");
76 1.31 thorpej
77 1.31 thorpej #include "fs_nfs.h"
78 1.54 bjh21 #include "opt_nfs.h"
79 1.34 thorpej #include "opt_nfsserver.h"
80 1.1 cgd
81 1.5 mycroft #include <sys/param.h>
82 1.5 mycroft #include <sys/resourcevar.h>
83 1.5 mycroft #include <sys/kernel.h>
84 1.8 cgd #include <sys/systm.h>
85 1.5 mycroft #include <sys/proc.h>
86 1.8 cgd #include <sys/vnode.h>
87 1.17 christos #include <sys/signalvar.h>
88 1.25 perry #include <sys/syslog.h>
89 1.1 cgd
90 1.11 cgd #include <sys/mount.h>
91 1.11 cgd #include <sys/syscallargs.h>
92 1.19 christos
93 1.37 thorpej #include <uvm/uvm_extern.h>
94 1.37 thorpej
95 1.26 thorpej #if defined(NFS) || defined(NFSSERVER)
96 1.20 fvdl #include <nfs/rpcv2.h>
97 1.20 fvdl #include <nfs/nfsproto.h>
98 1.19 christos #include <nfs/nfs_var.h>
99 1.19 christos #endif
100 1.17 christos
101 1.5 mycroft #include <machine/cpu.h>
102 1.23 cgd
103 1.23 cgd /*
104 1.1 cgd * Time of day and interval timer support.
105 1.1 cgd *
106 1.1 cgd * These routines provide the kernel entry points to get and set
107 1.1 cgd * the time-of-day and per-process interval timers. Subroutines
108 1.1 cgd * here provide support for adding and subtracting timeval structures
109 1.1 cgd * and decrementing interval timers, optionally reloading the interval
110 1.1 cgd * timers when they expire.
111 1.1 cgd */
112 1.1 cgd
113 1.22 jtc /* This function is used by clock_settime and settimeofday */
114 1.39 tron int
115 1.22 jtc settime(tv)
116 1.22 jtc struct timeval *tv;
117 1.22 jtc {
118 1.22 jtc struct timeval delta;
119 1.47 thorpej struct cpu_info *ci;
120 1.22 jtc int s;
121 1.22 jtc
122 1.61.10.1 tron /*
123 1.61.10.1 tron * Don't allow the time to be set forward so far it will wrap
124 1.61.10.1 tron * and become negative, thus allowing an attacker to bypass
125 1.61.10.1 tron * the next check below. The cutoff is 1 year before rollover
126 1.61.10.1 tron * occurs, so even if the attacker uses adjtime(2) to move
127 1.61.10.1 tron * the time past the cutoff, it will take a very long time
128 1.61.10.1 tron * to get to the wrap point.
129 1.61.10.1 tron *
130 1.61.10.1 tron * XXX: we check against INT_MAX since on 64-bit
131 1.61.10.1 tron * platforms, sizeof(int) != sizeof(long) and
132 1.61.10.1 tron * time_t is 32 bits even when atv.tv_sec is 64 bits.
133 1.61.10.1 tron */
134 1.61.10.1 tron if (tv->tv_sec > INT_MAX - 365*24*60*60) {
135 1.61.10.2 tron struct proc *p = curproc;
136 1.61.10.1 tron struct proc *pp = p->p_pptr;
137 1.61.10.1 tron log(LOG_WARNING, "pid %d (%s) "
138 1.61.10.1 tron "invoked by uid %d ppid %d (%s) "
139 1.61.10.1 tron "tried to set clock forward to %ld\n",
140 1.61.10.1 tron p->p_pid, p->p_comm, pp->p_ucred->cr_uid,
141 1.61.10.1 tron pp->p_pid, pp->p_comm, (long)tv->tv_sec);
142 1.61.10.1 tron return (EPERM);
143 1.61.10.1 tron }
144 1.22 jtc /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */
145 1.22 jtc s = splclock();
146 1.22 jtc timersub(tv, &time, &delta);
147 1.55 tron if ((delta.tv_sec < 0 || delta.tv_usec < 0) && securelevel > 1) {
148 1.55 tron splx(s);
149 1.29 tls return (EPERM);
150 1.55 tron }
151 1.29 tls #ifdef notyet
152 1.55 tron if ((delta.tv_sec < 86400) && securelevel > 0) {
153 1.55 tron splx(s);
154 1.29 tls return (EPERM);
155 1.55 tron }
156 1.29 tls #endif
157 1.22 jtc time = *tv;
158 1.38 thorpej (void) spllowersoftclock();
159 1.22 jtc timeradd(&boottime, &delta, &boottime);
160 1.47 thorpej /*
161 1.47 thorpej * XXXSMP
162 1.47 thorpej * This is wrong. We should traverse a list of all
163 1.47 thorpej * CPUs and add the delta to the runtime of those
164 1.47 thorpej * CPUs which have a process on them.
165 1.47 thorpej */
166 1.47 thorpej ci = curcpu();
167 1.47 thorpej timeradd(&ci->ci_schedstate.spc_runtime, &delta,
168 1.47 thorpej &ci->ci_schedstate.spc_runtime);
169 1.54 bjh21 # if (defined(NFS) && !defined (NFS_V2_ONLY)) || defined(NFSSERVER)
170 1.22 jtc nqnfs_lease_updatetime(delta.tv_sec);
171 1.22 jtc # endif
172 1.22 jtc splx(s);
173 1.22 jtc resettodr();
174 1.29 tls return (0);
175 1.22 jtc }
176 1.22 jtc
177 1.22 jtc /* ARGSUSED */
178 1.22 jtc int
179 1.22 jtc sys_clock_gettime(p, v, retval)
180 1.22 jtc struct proc *p;
181 1.22 jtc void *v;
182 1.22 jtc register_t *retval;
183 1.22 jtc {
184 1.45 augustss struct sys_clock_gettime_args /* {
185 1.22 jtc syscallarg(clockid_t) clock_id;
186 1.23 cgd syscallarg(struct timespec *) tp;
187 1.23 cgd } */ *uap = v;
188 1.22 jtc clockid_t clock_id;
189 1.22 jtc struct timeval atv;
190 1.22 jtc struct timespec ats;
191 1.61 simonb int s;
192 1.22 jtc
193 1.22 jtc clock_id = SCARG(uap, clock_id);
194 1.61 simonb switch (clock_id) {
195 1.61 simonb case CLOCK_REALTIME:
196 1.61 simonb microtime(&atv);
197 1.61 simonb TIMEVAL_TO_TIMESPEC(&atv,&ats);
198 1.61 simonb break;
199 1.61 simonb case CLOCK_MONOTONIC:
200 1.61 simonb /* XXX "hz" granularity */
201 1.61 simonb s = splclock();
202 1.61 simonb atv = mono_time;
203 1.61 simonb splx(s);
204 1.61 simonb TIMEVAL_TO_TIMESPEC(&atv,&ats);
205 1.61 simonb break;
206 1.61 simonb default:
207 1.22 jtc return (EINVAL);
208 1.61 simonb }
209 1.22 jtc
210 1.24 cgd return copyout(&ats, SCARG(uap, tp), sizeof(ats));
211 1.22 jtc }
212 1.22 jtc
213 1.22 jtc /* ARGSUSED */
214 1.22 jtc int
215 1.22 jtc sys_clock_settime(p, v, retval)
216 1.22 jtc struct proc *p;
217 1.22 jtc void *v;
218 1.22 jtc register_t *retval;
219 1.22 jtc {
220 1.45 augustss struct sys_clock_settime_args /* {
221 1.22 jtc syscallarg(clockid_t) clock_id;
222 1.23 cgd syscallarg(const struct timespec *) tp;
223 1.23 cgd } */ *uap = v;
224 1.22 jtc int error;
225 1.22 jtc
226 1.22 jtc if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
227 1.22 jtc return (error);
228 1.22 jtc
229 1.60 manu return (clock_settime1(SCARG(uap, clock_id), SCARG(uap, tp)));
230 1.56 manu }
231 1.56 manu
232 1.56 manu
233 1.56 manu int
234 1.60 manu clock_settime1(clock_id, tp)
235 1.56 manu clockid_t clock_id;
236 1.60 manu const struct timespec *tp;
237 1.56 manu {
238 1.60 manu struct timespec ats;
239 1.56 manu struct timeval atv;
240 1.56 manu int error;
241 1.56 manu
242 1.60 manu if ((error = copyin(tp, &ats, sizeof(ats))) != 0)
243 1.60 manu return (error);
244 1.60 manu
245 1.61 simonb switch (clock_id) {
246 1.61 simonb case CLOCK_REALTIME:
247 1.61 simonb TIMESPEC_TO_TIMEVAL(&atv, &ats);
248 1.61 simonb if ((error = settime(&atv)) != 0)
249 1.61 simonb return (error);
250 1.61 simonb break;
251 1.61 simonb case CLOCK_MONOTONIC:
252 1.61 simonb return (EINVAL); /* read-only clock */
253 1.61 simonb default:
254 1.56 manu return (EINVAL);
255 1.61 simonb }
256 1.22 jtc
257 1.22 jtc return 0;
258 1.22 jtc }
259 1.22 jtc
260 1.22 jtc int
261 1.22 jtc sys_clock_getres(p, v, retval)
262 1.22 jtc struct proc *p;
263 1.22 jtc void *v;
264 1.22 jtc register_t *retval;
265 1.22 jtc {
266 1.45 augustss struct sys_clock_getres_args /* {
267 1.22 jtc syscallarg(clockid_t) clock_id;
268 1.23 cgd syscallarg(struct timespec *) tp;
269 1.23 cgd } */ *uap = v;
270 1.22 jtc clockid_t clock_id;
271 1.22 jtc struct timespec ts;
272 1.22 jtc int error = 0;
273 1.22 jtc
274 1.22 jtc clock_id = SCARG(uap, clock_id);
275 1.61 simonb switch (clock_id) {
276 1.61 simonb case CLOCK_REALTIME:
277 1.61 simonb case CLOCK_MONOTONIC:
278 1.22 jtc ts.tv_sec = 0;
279 1.22 jtc ts.tv_nsec = 1000000000 / hz;
280 1.61 simonb break;
281 1.61 simonb default:
282 1.61 simonb return (EINVAL);
283 1.61 simonb }
284 1.22 jtc
285 1.61 simonb if (SCARG(uap, tp))
286 1.35 perry error = copyout(&ts, SCARG(uap, tp), sizeof(ts));
287 1.22 jtc
288 1.22 jtc return error;
289 1.22 jtc }
290 1.22 jtc
291 1.27 jtc /* ARGSUSED */
292 1.27 jtc int
293 1.27 jtc sys_nanosleep(p, v, retval)
294 1.27 jtc struct proc *p;
295 1.27 jtc void *v;
296 1.27 jtc register_t *retval;
297 1.27 jtc {
298 1.27 jtc static int nanowait;
299 1.45 augustss struct sys_nanosleep_args/* {
300 1.27 jtc syscallarg(struct timespec *) rqtp;
301 1.27 jtc syscallarg(struct timespec *) rmtp;
302 1.27 jtc } */ *uap = v;
303 1.27 jtc struct timespec rqt;
304 1.27 jtc struct timespec rmt;
305 1.27 jtc struct timeval atv, utv;
306 1.27 jtc int error, s, timo;
307 1.27 jtc
308 1.27 jtc error = copyin((caddr_t)SCARG(uap, rqtp), (caddr_t)&rqt,
309 1.27 jtc sizeof(struct timespec));
310 1.27 jtc if (error)
311 1.27 jtc return (error);
312 1.27 jtc
313 1.27 jtc TIMESPEC_TO_TIMEVAL(&atv,&rqt)
314 1.59 christos if (itimerfix(&atv) || atv.tv_sec > 1000000000)
315 1.27 jtc return (EINVAL);
316 1.27 jtc
317 1.27 jtc s = splclock();
318 1.27 jtc timeradd(&atv,&time,&atv);
319 1.27 jtc timo = hzto(&atv);
320 1.27 jtc /*
321 1.27 jtc * Avoid inadvertantly sleeping forever
322 1.27 jtc */
323 1.27 jtc if (timo == 0)
324 1.27 jtc timo = 1;
325 1.27 jtc splx(s);
326 1.27 jtc
327 1.27 jtc error = tsleep(&nanowait, PWAIT | PCATCH, "nanosleep", timo);
328 1.27 jtc if (error == ERESTART)
329 1.27 jtc error = EINTR;
330 1.27 jtc if (error == EWOULDBLOCK)
331 1.27 jtc error = 0;
332 1.27 jtc
333 1.27 jtc if (SCARG(uap, rmtp)) {
334 1.28 jtc int error;
335 1.28 jtc
336 1.27 jtc s = splclock();
337 1.27 jtc utv = time;
338 1.27 jtc splx(s);
339 1.27 jtc
340 1.27 jtc timersub(&atv, &utv, &utv);
341 1.27 jtc if (utv.tv_sec < 0)
342 1.27 jtc timerclear(&utv);
343 1.27 jtc
344 1.27 jtc TIMEVAL_TO_TIMESPEC(&utv,&rmt);
345 1.27 jtc error = copyout((caddr_t)&rmt, (caddr_t)SCARG(uap,rmtp),
346 1.28 jtc sizeof(rmt));
347 1.28 jtc if (error)
348 1.28 jtc return (error);
349 1.27 jtc }
350 1.27 jtc
351 1.27 jtc return error;
352 1.27 jtc }
353 1.22 jtc
354 1.1 cgd /* ARGSUSED */
355 1.3 andrew int
356 1.16 mycroft sys_gettimeofday(p, v, retval)
357 1.1 cgd struct proc *p;
358 1.15 thorpej void *v;
359 1.15 thorpej register_t *retval;
360 1.15 thorpej {
361 1.45 augustss struct sys_gettimeofday_args /* {
362 1.11 cgd syscallarg(struct timeval *) tp;
363 1.11 cgd syscallarg(struct timezone *) tzp;
364 1.15 thorpej } */ *uap = v;
365 1.1 cgd struct timeval atv;
366 1.1 cgd int error = 0;
367 1.25 perry struct timezone tzfake;
368 1.1 cgd
369 1.11 cgd if (SCARG(uap, tp)) {
370 1.1 cgd microtime(&atv);
371 1.35 perry error = copyout(&atv, SCARG(uap, tp), sizeof(atv));
372 1.17 christos if (error)
373 1.1 cgd return (error);
374 1.1 cgd }
375 1.25 perry if (SCARG(uap, tzp)) {
376 1.25 perry /*
377 1.32 mycroft * NetBSD has no kernel notion of time zone, so we just
378 1.25 perry * fake up a timezone struct and return it if demanded.
379 1.25 perry */
380 1.25 perry tzfake.tz_minuteswest = 0;
381 1.25 perry tzfake.tz_dsttime = 0;
382 1.35 perry error = copyout(&tzfake, SCARG(uap, tzp), sizeof(tzfake));
383 1.25 perry }
384 1.1 cgd return (error);
385 1.1 cgd }
386 1.1 cgd
387 1.1 cgd /* ARGSUSED */
388 1.3 andrew int
389 1.16 mycroft sys_settimeofday(p, v, retval)
390 1.1 cgd struct proc *p;
391 1.15 thorpej void *v;
392 1.15 thorpej register_t *retval;
393 1.15 thorpej {
394 1.16 mycroft struct sys_settimeofday_args /* {
395 1.24 cgd syscallarg(const struct timeval *) tv;
396 1.24 cgd syscallarg(const struct timezone *) tzp;
397 1.15 thorpej } */ *uap = v;
398 1.60 manu int error;
399 1.60 manu
400 1.60 manu if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
401 1.60 manu return (error);
402 1.60 manu
403 1.60 manu return settimeofday1(SCARG(uap, tv), SCARG(uap, tzp), p);
404 1.60 manu }
405 1.60 manu
406 1.60 manu int
407 1.60 manu settimeofday1(utv, utzp, p)
408 1.60 manu const struct timeval *utv;
409 1.60 manu const struct timezone *utzp;
410 1.60 manu struct proc *p;
411 1.60 manu {
412 1.22 jtc struct timeval atv;
413 1.1 cgd struct timezone atz;
414 1.56 manu struct timeval *tv = NULL;
415 1.56 manu struct timezone *tzp = NULL;
416 1.22 jtc int error;
417 1.1 cgd
418 1.8 cgd /* Verify all parameters before changing time. */
419 1.60 manu if (utv) {
420 1.60 manu if ((error = copyin(utv, &atv, sizeof(atv))) != 0)
421 1.56 manu return (error);
422 1.56 manu tv = &atv;
423 1.56 manu }
424 1.25 perry /* XXX since we don't use tz, probably no point in doing copyin. */
425 1.60 manu if (utzp) {
426 1.60 manu if ((error = copyin(utzp, &atz, sizeof(atz))) != 0)
427 1.56 manu return (error);
428 1.56 manu tzp = &atz;
429 1.56 manu }
430 1.56 manu
431 1.56 manu if (tv)
432 1.56 manu if ((error = settime(tv)) != 0)
433 1.29 tls return (error);
434 1.25 perry /*
435 1.32 mycroft * NetBSD has no kernel notion of time zone, and only an
436 1.25 perry * obsolete program would try to set it, so we log a warning.
437 1.25 perry */
438 1.56 manu if (tzp)
439 1.25 perry log(LOG_WARNING, "pid %d attempted to set the "
440 1.32 mycroft "(obsolete) kernel time zone\n", p->p_pid);
441 1.8 cgd return (0);
442 1.1 cgd }
443 1.1 cgd
444 1.1 cgd int tickdelta; /* current clock skew, us. per tick */
445 1.1 cgd long timedelta; /* unapplied time correction, us. */
446 1.1 cgd long bigadj = 1000000; /* use 10x skew above bigadj us. */
447 1.1 cgd
448 1.1 cgd /* ARGSUSED */
449 1.3 andrew int
450 1.16 mycroft sys_adjtime(p, v, retval)
451 1.1 cgd struct proc *p;
452 1.15 thorpej void *v;
453 1.15 thorpej register_t *retval;
454 1.15 thorpej {
455 1.45 augustss struct sys_adjtime_args /* {
456 1.24 cgd syscallarg(const struct timeval *) delta;
457 1.11 cgd syscallarg(struct timeval *) olddelta;
458 1.15 thorpej } */ *uap = v;
459 1.56 manu int error;
460 1.1 cgd
461 1.17 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
462 1.1 cgd return (error);
463 1.17 christos
464 1.60 manu return adjtime1(SCARG(uap, delta), SCARG(uap, olddelta), p);
465 1.56 manu }
466 1.56 manu
467 1.56 manu int
468 1.56 manu adjtime1(delta, olddelta, p)
469 1.60 manu const struct timeval *delta;
470 1.56 manu struct timeval *olddelta;
471 1.56 manu struct proc *p;
472 1.56 manu {
473 1.60 manu struct timeval atv;
474 1.60 manu struct timeval *oatv = NULL;
475 1.56 manu long ndelta, ntickdelta, odelta;
476 1.60 manu int error;
477 1.56 manu int s;
478 1.8 cgd
479 1.60 manu error = copyin(delta, &atv, sizeof(struct timeval));
480 1.60 manu if (error)
481 1.60 manu return (error);
482 1.60 manu
483 1.60 manu if (olddelta != NULL) {
484 1.60 manu if (uvm_useracc((caddr_t)olddelta,
485 1.60 manu sizeof(struct timeval), B_WRITE) == FALSE)
486 1.60 manu return (EFAULT);
487 1.60 manu oatv = olddelta;
488 1.60 manu }
489 1.60 manu
490 1.8 cgd /*
491 1.8 cgd * Compute the total correction and the rate at which to apply it.
492 1.8 cgd * Round the adjustment down to a whole multiple of the per-tick
493 1.8 cgd * delta, so that after some number of incremental changes in
494 1.8 cgd * hardclock(), tickdelta will become zero, lest the correction
495 1.8 cgd * overshoot and start taking us away from the desired final time.
496 1.8 cgd */
497 1.60 manu ndelta = atv.tv_sec * 1000000 + atv.tv_usec;
498 1.41 hwr if (ndelta > bigadj || ndelta < -bigadj)
499 1.8 cgd ntickdelta = 10 * tickadj;
500 1.8 cgd else
501 1.8 cgd ntickdelta = tickadj;
502 1.8 cgd if (ndelta % ntickdelta)
503 1.8 cgd ndelta = ndelta / ntickdelta * ntickdelta;
504 1.8 cgd
505 1.8 cgd /*
506 1.8 cgd * To make hardclock()'s job easier, make the per-tick delta negative
507 1.8 cgd * if we want time to run slower; then hardclock can simply compute
508 1.8 cgd * tick + tickdelta, and subtract tickdelta from timedelta.
509 1.8 cgd */
510 1.8 cgd if (ndelta < 0)
511 1.8 cgd ntickdelta = -ntickdelta;
512 1.1 cgd s = splclock();
513 1.8 cgd odelta = timedelta;
514 1.1 cgd timedelta = ndelta;
515 1.8 cgd tickdelta = ntickdelta;
516 1.1 cgd splx(s);
517 1.1 cgd
518 1.56 manu if (olddelta) {
519 1.60 manu atv.tv_sec = odelta / 1000000;
520 1.60 manu atv.tv_usec = odelta % 1000000;
521 1.60 manu (void) copyout(&atv, olddelta, sizeof(struct timeval));
522 1.8 cgd }
523 1.1 cgd return (0);
524 1.1 cgd }
525 1.1 cgd
526 1.1 cgd /*
527 1.1 cgd * Get value of an interval timer. The process virtual and
528 1.1 cgd * profiling virtual time timers are kept in the p_stats area, since
529 1.1 cgd * they can be swapped out. These are kept internally in the
530 1.1 cgd * way they are specified externally: in time until they expire.
531 1.1 cgd *
532 1.1 cgd * The real time interval timer is kept in the process table slot
533 1.1 cgd * for the process, and its value (it_value) is kept as an
534 1.1 cgd * absolute time rather than as a delta, so that it is easy to keep
535 1.1 cgd * periodic real-time signals from drifting.
536 1.1 cgd *
537 1.1 cgd * Virtual time timers are processed in the hardclock() routine of
538 1.1 cgd * kern_clock.c. The real time timer is processed by a timeout
539 1.1 cgd * routine, called from the softclock() routine. Since a callout
540 1.1 cgd * may be delayed in real time due to interrupt processing in the system,
541 1.1 cgd * it is possible for the real time timeout routine (realitexpire, given below),
542 1.1 cgd * to be delayed in real time past when it is supposed to occur. It
543 1.1 cgd * does not suffice, therefore, to reload the real timer .it_value from the
544 1.1 cgd * real time timers .it_interval. Rather, we compute the next time in
545 1.1 cgd * absolute time the timer should go off.
546 1.1 cgd */
547 1.1 cgd /* ARGSUSED */
548 1.3 andrew int
549 1.16 mycroft sys_getitimer(p, v, retval)
550 1.1 cgd struct proc *p;
551 1.15 thorpej void *v;
552 1.15 thorpej register_t *retval;
553 1.15 thorpej {
554 1.45 augustss struct sys_getitimer_args /* {
555 1.30 mycroft syscallarg(int) which;
556 1.11 cgd syscallarg(struct itimerval *) itv;
557 1.15 thorpej } */ *uap = v;
558 1.30 mycroft int which = SCARG(uap, which);
559 1.1 cgd struct itimerval aitv;
560 1.1 cgd int s;
561 1.1 cgd
562 1.30 mycroft if ((u_int)which > ITIMER_PROF)
563 1.1 cgd return (EINVAL);
564 1.1 cgd s = splclock();
565 1.30 mycroft if (which == ITIMER_REAL) {
566 1.1 cgd /*
567 1.12 mycroft * Convert from absolute to relative time in .it_value
568 1.1 cgd * part of real time timer. If time for real time timer
569 1.1 cgd * has passed return 0, else return difference between
570 1.1 cgd * current time and time for the timer to go off.
571 1.1 cgd */
572 1.1 cgd aitv = p->p_realtimer;
573 1.36 thorpej if (timerisset(&aitv.it_value)) {
574 1.1 cgd if (timercmp(&aitv.it_value, &time, <))
575 1.1 cgd timerclear(&aitv.it_value);
576 1.1 cgd else
577 1.14 mycroft timersub(&aitv.it_value, &time, &aitv.it_value);
578 1.36 thorpej }
579 1.1 cgd } else
580 1.30 mycroft aitv = p->p_stats->p_timer[which];
581 1.1 cgd splx(s);
582 1.35 perry return (copyout(&aitv, SCARG(uap, itv), sizeof(struct itimerval)));
583 1.1 cgd }
584 1.1 cgd
585 1.1 cgd /* ARGSUSED */
586 1.3 andrew int
587 1.16 mycroft sys_setitimer(p, v, retval)
588 1.1 cgd struct proc *p;
589 1.45 augustss void *v;
590 1.15 thorpej register_t *retval;
591 1.15 thorpej {
592 1.45 augustss struct sys_setitimer_args /* {
593 1.30 mycroft syscallarg(int) which;
594 1.24 cgd syscallarg(const struct itimerval *) itv;
595 1.11 cgd syscallarg(struct itimerval *) oitv;
596 1.15 thorpej } */ *uap = v;
597 1.30 mycroft int which = SCARG(uap, which);
598 1.21 cgd struct sys_getitimer_args getargs;
599 1.1 cgd struct itimerval aitv;
600 1.45 augustss const struct itimerval *itvp;
601 1.1 cgd int s, error;
602 1.1 cgd
603 1.30 mycroft if ((u_int)which > ITIMER_PROF)
604 1.1 cgd return (EINVAL);
605 1.11 cgd itvp = SCARG(uap, itv);
606 1.56 manu if (itvp &&
607 1.56 manu (error = copyin(itvp, &aitv, sizeof(struct itimerval)) != 0))
608 1.1 cgd return (error);
609 1.21 cgd if (SCARG(uap, oitv) != NULL) {
610 1.30 mycroft SCARG(&getargs, which) = which;
611 1.21 cgd SCARG(&getargs, itv) = SCARG(uap, oitv);
612 1.23 cgd if ((error = sys_getitimer(p, &getargs, retval)) != 0)
613 1.21 cgd return (error);
614 1.21 cgd }
615 1.1 cgd if (itvp == 0)
616 1.1 cgd return (0);
617 1.1 cgd if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval))
618 1.1 cgd return (EINVAL);
619 1.1 cgd s = splclock();
620 1.30 mycroft if (which == ITIMER_REAL) {
621 1.44 thorpej callout_stop(&p->p_realit_ch);
622 1.1 cgd if (timerisset(&aitv.it_value)) {
623 1.52 thorpej /*
624 1.52 thorpej * Don't need to check hzto() return value, here.
625 1.52 thorpej * callout_reset() does it for us.
626 1.52 thorpej */
627 1.14 mycroft timeradd(&aitv.it_value, &time, &aitv.it_value);
628 1.44 thorpej callout_reset(&p->p_realit_ch, hzto(&aitv.it_value),
629 1.44 thorpej realitexpire, p);
630 1.1 cgd }
631 1.1 cgd p->p_realtimer = aitv;
632 1.1 cgd } else
633 1.30 mycroft p->p_stats->p_timer[which] = aitv;
634 1.1 cgd splx(s);
635 1.1 cgd return (0);
636 1.1 cgd }
637 1.1 cgd
638 1.1 cgd /*
639 1.1 cgd * Real interval timer expired:
640 1.1 cgd * send process whose timer expired an alarm signal.
641 1.1 cgd * If time is not set up to reload, then just return.
642 1.1 cgd * Else compute next time timer should go off which is > current time.
643 1.1 cgd * This is where delay in processing this timeout causes multiple
644 1.1 cgd * SIGALRM calls to be compressed into one.
645 1.1 cgd */
646 1.3 andrew void
647 1.6 cgd realitexpire(arg)
648 1.6 cgd void *arg;
649 1.6 cgd {
650 1.45 augustss struct proc *p;
651 1.1 cgd int s;
652 1.1 cgd
653 1.6 cgd p = (struct proc *)arg;
654 1.1 cgd psignal(p, SIGALRM);
655 1.1 cgd if (!timerisset(&p->p_realtimer.it_interval)) {
656 1.1 cgd timerclear(&p->p_realtimer.it_value);
657 1.1 cgd return;
658 1.1 cgd }
659 1.1 cgd for (;;) {
660 1.1 cgd s = splclock();
661 1.14 mycroft timeradd(&p->p_realtimer.it_value,
662 1.14 mycroft &p->p_realtimer.it_interval, &p->p_realtimer.it_value);
663 1.1 cgd if (timercmp(&p->p_realtimer.it_value, &time, >)) {
664 1.52 thorpej /*
665 1.52 thorpej * Don't need to check hzto() return value, here.
666 1.52 thorpej * callout_reset() does it for us.
667 1.52 thorpej */
668 1.44 thorpej callout_reset(&p->p_realit_ch,
669 1.44 thorpej hzto(&p->p_realtimer.it_value), realitexpire, p);
670 1.1 cgd splx(s);
671 1.1 cgd return;
672 1.1 cgd }
673 1.1 cgd splx(s);
674 1.1 cgd }
675 1.1 cgd }
676 1.1 cgd
677 1.1 cgd /*
678 1.1 cgd * Check that a proposed value to load into the .it_value or
679 1.1 cgd * .it_interval part of an interval timer is acceptable, and
680 1.1 cgd * fix it to have at least minimal value (i.e. if it is less
681 1.1 cgd * than the resolution of the clock, round it up.)
682 1.1 cgd */
683 1.3 andrew int
684 1.1 cgd itimerfix(tv)
685 1.1 cgd struct timeval *tv;
686 1.1 cgd {
687 1.1 cgd
688 1.59 christos if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
689 1.1 cgd return (EINVAL);
690 1.1 cgd if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
691 1.1 cgd tv->tv_usec = tick;
692 1.1 cgd return (0);
693 1.1 cgd }
694 1.1 cgd
695 1.1 cgd /*
696 1.1 cgd * Decrement an interval timer by a specified number
697 1.1 cgd * of microseconds, which must be less than a second,
698 1.1 cgd * i.e. < 1000000. If the timer expires, then reload
699 1.1 cgd * it. In this case, carry over (usec - old value) to
700 1.8 cgd * reduce the value reloaded into the timer so that
701 1.1 cgd * the timer does not drift. This routine assumes
702 1.1 cgd * that it is called in a context where the timers
703 1.1 cgd * on which it is operating cannot change in value.
704 1.1 cgd */
705 1.3 andrew int
706 1.1 cgd itimerdecr(itp, usec)
707 1.45 augustss struct itimerval *itp;
708 1.1 cgd int usec;
709 1.1 cgd {
710 1.1 cgd
711 1.1 cgd if (itp->it_value.tv_usec < usec) {
712 1.1 cgd if (itp->it_value.tv_sec == 0) {
713 1.1 cgd /* expired, and already in next interval */
714 1.1 cgd usec -= itp->it_value.tv_usec;
715 1.1 cgd goto expire;
716 1.1 cgd }
717 1.1 cgd itp->it_value.tv_usec += 1000000;
718 1.1 cgd itp->it_value.tv_sec--;
719 1.1 cgd }
720 1.1 cgd itp->it_value.tv_usec -= usec;
721 1.1 cgd usec = 0;
722 1.1 cgd if (timerisset(&itp->it_value))
723 1.1 cgd return (1);
724 1.1 cgd /* expired, exactly at end of interval */
725 1.1 cgd expire:
726 1.1 cgd if (timerisset(&itp->it_interval)) {
727 1.1 cgd itp->it_value = itp->it_interval;
728 1.1 cgd itp->it_value.tv_usec -= usec;
729 1.1 cgd if (itp->it_value.tv_usec < 0) {
730 1.1 cgd itp->it_value.tv_usec += 1000000;
731 1.1 cgd itp->it_value.tv_sec--;
732 1.1 cgd }
733 1.1 cgd } else
734 1.1 cgd itp->it_value.tv_usec = 0; /* sec is already 0 */
735 1.1 cgd return (0);
736 1.42 cgd }
737 1.42 cgd
738 1.42 cgd /*
739 1.42 cgd * ratecheck(): simple time-based rate-limit checking. see ratecheck(9)
740 1.42 cgd * for usage and rationale.
741 1.42 cgd */
742 1.42 cgd int
743 1.42 cgd ratecheck(lasttime, mininterval)
744 1.42 cgd struct timeval *lasttime;
745 1.42 cgd const struct timeval *mininterval;
746 1.42 cgd {
747 1.49 itojun struct timeval tv, delta;
748 1.42 cgd int s, rv = 0;
749 1.42 cgd
750 1.42 cgd s = splclock();
751 1.49 itojun tv = mono_time;
752 1.49 itojun splx(s);
753 1.49 itojun
754 1.49 itojun timersub(&tv, lasttime, &delta);
755 1.42 cgd
756 1.42 cgd /*
757 1.42 cgd * check for 0,0 is so that the message will be seen at least once,
758 1.42 cgd * even if interval is huge.
759 1.42 cgd */
760 1.42 cgd if (timercmp(&delta, mininterval, >=) ||
761 1.42 cgd (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
762 1.49 itojun *lasttime = tv;
763 1.42 cgd rv = 1;
764 1.42 cgd }
765 1.50 itojun
766 1.50 itojun return (rv);
767 1.50 itojun }
768 1.50 itojun
769 1.50 itojun /*
770 1.50 itojun * ppsratecheck(): packets (or events) per second limitation.
771 1.50 itojun */
772 1.50 itojun int
773 1.50 itojun ppsratecheck(lasttime, curpps, maxpps)
774 1.50 itojun struct timeval *lasttime;
775 1.50 itojun int *curpps;
776 1.50 itojun int maxpps; /* maximum pps allowed */
777 1.50 itojun {
778 1.50 itojun struct timeval tv, delta;
779 1.50 itojun int s, rv;
780 1.50 itojun
781 1.50 itojun s = splclock();
782 1.50 itojun tv = mono_time;
783 1.50 itojun splx(s);
784 1.50 itojun
785 1.50 itojun timersub(&tv, lasttime, &delta);
786 1.50 itojun
787 1.50 itojun /*
788 1.50 itojun * check for 0,0 is so that the message will be seen at least once.
789 1.50 itojun * if more than one second have passed since the last update of
790 1.50 itojun * lasttime, reset the counter.
791 1.50 itojun *
792 1.50 itojun * we do increment *curpps even in *curpps < maxpps case, as some may
793 1.50 itojun * try to use *curpps for stat purposes as well.
794 1.50 itojun */
795 1.50 itojun if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
796 1.50 itojun delta.tv_sec >= 1) {
797 1.50 itojun *lasttime = tv;
798 1.50 itojun *curpps = 0;
799 1.50 itojun rv = 1;
800 1.53 itojun } else if (maxpps < 0)
801 1.53 itojun rv = 1;
802 1.53 itojun else if (*curpps < maxpps)
803 1.50 itojun rv = 1;
804 1.50 itojun else
805 1.50 itojun rv = 0;
806 1.50 itojun
807 1.51 jhawk #if 1 /*DIAGNOSTIC?*/
808 1.50 itojun /* be careful about wrap-around */
809 1.50 itojun if (*curpps + 1 > *curpps)
810 1.50 itojun *curpps = *curpps + 1;
811 1.50 itojun #else
812 1.50 itojun /*
813 1.50 itojun * assume that there's not too many calls to this function.
814 1.50 itojun * not sure if the assumption holds, as it depends on *caller's*
815 1.50 itojun * behavior, not the behavior of this function.
816 1.50 itojun * IMHO it is wrong to make assumption on the caller's behavior,
817 1.51 jhawk * so the above #if is #if 1, not #ifdef DIAGNOSTIC.
818 1.50 itojun */
819 1.50 itojun *curpps = *curpps + 1;
820 1.50 itojun #endif
821 1.42 cgd
822 1.42 cgd return (rv);
823 1.1 cgd }
824