]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
b0d623f7 | 2 | * Copyright (c) 2000-2008 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1982, 1986, 1989, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
36 | * 1. Redistributions of source code must retain the above copyright | |
37 | * notice, this list of conditions and the following disclaimer. | |
38 | * 2. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in the | |
40 | * documentation and/or other materials provided with the distribution. | |
41 | * 3. All advertising materials mentioning features or use of this software | |
42 | * must display the following acknowledgement: | |
43 | * This product includes software developed by the University of | |
44 | * California, Berkeley and its contributors. | |
45 | * 4. Neither the name of the University nor the names of its contributors | |
46 | * may be used to endorse or promote products derived from this software | |
47 | * without specific prior written permission. | |
48 | * | |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
59 | * SUCH DAMAGE. | |
60 | * | |
61 | * @(#)kern_time.c 8.4 (Berkeley) 5/26/95 | |
62 | */ | |
2d21ac55 A |
63 | /* |
64 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
65 | * support for mandatory and extensible security protections. This notice | |
66 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
67 | * Version 2.0. | |
68 | */ | |
1c79356b A |
69 | |
70 | #include <sys/param.h> | |
71 | #include <sys/resourcevar.h> | |
72 | #include <sys/kernel.h> | |
73 | #include <sys/systm.h> | |
91447636 A |
74 | #include <sys/proc_internal.h> |
75 | #include <sys/kauth.h> | |
1c79356b | 76 | #include <sys/vnode.h> |
b0d623f7 | 77 | #include <sys/time.h> |
6d2010ae | 78 | #include <sys/priv.h> |
1c79356b | 79 | |
91447636 A |
80 | #include <sys/mount_internal.h> |
81 | #include <sys/sysproto.h> | |
82 | #include <sys/signalvar.h> | |
39236c6e | 83 | #include <sys/protosw.h> /* for net_uptime2timeval() */ |
1c79356b | 84 | |
1c79356b | 85 | #include <kern/clock.h> |
2d21ac55 | 86 | #include <kern/task.h> |
91447636 | 87 | #include <kern/thread_call.h> |
2d21ac55 A |
88 | #if CONFIG_MACF |
89 | #include <security/mac_framework.h> | |
90 | #endif | |
5ba3f43e A |
91 | #include <IOKit/IOBSD.h> |
92 | #include <sys/time.h> | |
0a7de745 | 93 | #include <kern/remote_time.h> |
1c79356b | 94 | |
0a7de745 | 95 | #define HZ 100 /* XXX */ |
1c79356b | 96 | |
9bccf70c | 97 | /* simple lock used to access timezone, tz structure */ |
c3c9b80d A |
98 | static LCK_GRP_DECLARE(tz_slock_grp, "tzlock"); |
99 | static LCK_SPIN_DECLARE(tz_slock, &tz_slock_grp); | |
91447636 | 100 | |
0a7de745 A |
101 | static void setthetime( |
102 | struct timeval *tv); | |
91447636 | 103 | |
d9a64523 | 104 | static boolean_t timeval_fixusec(struct timeval *t1); |
91447636 | 105 | |
39037602 | 106 | /* |
1c79356b A |
107 | * Time of day and interval timer support. |
108 | * | |
109 | * These routines provide the kernel entry points to get and set | |
110 | * the time-of-day and per-process interval timers. Subroutines | |
111 | * here provide support for adding and subtracting timeval structures | |
112 | * and decrementing interval timers, optionally reloading the interval | |
113 | * timers when they expire. | |
114 | */ | |
1c79356b A |
115 | /* ARGSUSED */ |
116 | int | |
0c530ab8 | 117 | gettimeofday( |
0a7de745 A |
118 | struct proc *p, |
119 | struct gettimeofday_args *uap, | |
120 | __unused int32_t *retval) | |
1c79356b | 121 | { |
1c79356b | 122 | int error = 0; |
9bccf70c | 123 | struct timezone ltz; /* local copy */ |
39037602 A |
124 | clock_sec_t secs; |
125 | clock_usec_t usecs; | |
126 | uint64_t mach_time; | |
9bccf70c | 127 | |
39037602 A |
128 | if (uap->tp || uap->mach_absolute_time) { |
129 | clock_gettimeofday_and_absolute_time(&secs, &usecs, &mach_time); | |
130 | } | |
b0d623f7 | 131 | |
39037602 A |
132 | if (uap->tp) { |
133 | /* Casting secs through a uint32_t to match arm64 commpage */ | |
134 | if (IS_64BIT_PROCESS(p)) { | |
135 | struct user64_timeval user_atv = {}; | |
136 | user_atv.tv_sec = (uint32_t)secs; | |
137 | user_atv.tv_usec = usecs; | |
138 | error = copyout(&user_atv, uap->tp, sizeof(user_atv)); | |
139 | } else { | |
140 | struct user32_timeval user_atv = {}; | |
141 | user_atv.tv_sec = (uint32_t)secs; | |
142 | user_atv.tv_usec = usecs; | |
143 | error = copyout(&user_atv, uap->tp, sizeof(user_atv)); | |
144 | } | |
145 | if (error) { | |
146 | return error; | |
147 | } | |
b0d623f7 | 148 | } |
39037602 | 149 | |
9bccf70c | 150 | if (uap->tzp) { |
c3c9b80d | 151 | lck_spin_lock(&tz_slock); |
9bccf70c | 152 | ltz = tz; |
c3c9b80d | 153 | lck_spin_unlock(&tz_slock); |
0c530ab8 | 154 | |
39037602 | 155 | error = copyout((caddr_t)<z, CAST_USER_ADDR_T(uap->tzp), sizeof(tz)); |
9bccf70c | 156 | } |
1c79356b | 157 | |
39037602 A |
158 | if (error == 0 && uap->mach_absolute_time) { |
159 | error = copyout(&mach_time, uap->mach_absolute_time, sizeof(mach_time)); | |
160 | } | |
161 | ||
162 | return error; | |
1c79356b A |
163 | } |
164 | ||
91447636 A |
165 | /* |
166 | * XXX Y2038 bug because of setthetime() argument | |
167 | */ | |
1c79356b A |
168 | /* ARGSUSED */ |
169 | int | |
b0d623f7 | 170 | settimeofday(__unused struct proc *p, struct settimeofday_args *uap, __unused int32_t *retval) |
1c79356b A |
171 | { |
172 | struct timeval atv; | |
173 | struct timezone atz; | |
91447636 | 174 | int error; |
1c79356b | 175 | |
b0d623f7 A |
176 | bzero(&atv, sizeof(atv)); |
177 | ||
5ba3f43e A |
178 | /* Check that this task is entitled to set the time or it is root */ |
179 | if (!IOTaskHasEntitlement(current_task(), SETTIME_ENTITLEMENT)) { | |
2d21ac55 | 180 | #if CONFIG_MACF |
5ba3f43e | 181 | error = mac_system_check_settime(kauth_cred_get()); |
0a7de745 A |
182 | if (error) { |
183 | return error; | |
184 | } | |
2d21ac55 | 185 | #endif |
f427ee49 | 186 | #if defined(XNU_TARGET_OS_OSX) |
0a7de745 A |
187 | if ((error = suser(kauth_cred_get(), &p->p_acflag))) { |
188 | return error; | |
189 | } | |
5ba3f43e A |
190 | #endif |
191 | } | |
192 | ||
91447636 A |
193 | /* Verify all parameters before changing time */ |
194 | if (uap->tv) { | |
195 | if (IS_64BIT_PROCESS(p)) { | |
b0d623f7 A |
196 | struct user64_timeval user_atv; |
197 | error = copyin(uap->tv, &user_atv, sizeof(user_atv)); | |
f427ee49 | 198 | atv.tv_sec = (__darwin_time_t)user_atv.tv_sec; |
91447636 A |
199 | atv.tv_usec = user_atv.tv_usec; |
200 | } else { | |
b0d623f7 A |
201 | struct user32_timeval user_atv; |
202 | error = copyin(uap->tv, &user_atv, sizeof(user_atv)); | |
203 | atv.tv_sec = user_atv.tv_sec; | |
204 | atv.tv_usec = user_atv.tv_usec; | |
91447636 | 205 | } |
0a7de745 A |
206 | if (error) { |
207 | return error; | |
208 | } | |
209 | } | |
210 | if (uap->tzp && (error = copyin(uap->tzp, (caddr_t)&atz, sizeof(atz)))) { | |
211 | return error; | |
91447636 | 212 | } |
91447636 | 213 | if (uap->tv) { |
d9a64523 | 214 | /* only positive values of sec/usec are accepted */ |
0a7de745 A |
215 | if (atv.tv_sec < 0 || atv.tv_usec < 0) { |
216 | return EPERM; | |
217 | } | |
218 | if (!timeval_fixusec(&atv)) { | |
219 | return EPERM; | |
220 | } | |
1c79356b | 221 | setthetime(&atv); |
91447636 | 222 | } |
9bccf70c | 223 | if (uap->tzp) { |
c3c9b80d | 224 | lck_spin_lock(&tz_slock); |
1c79356b | 225 | tz = atz; |
c3c9b80d | 226 | lck_spin_unlock(&tz_slock); |
9bccf70c | 227 | } |
0a7de745 | 228 | return 0; |
1c79356b A |
229 | } |
230 | ||
91447636 A |
231 | static void |
232 | setthetime( | |
0a7de745 | 233 | struct timeval *tv) |
1c79356b | 234 | { |
55e303ae | 235 | clock_set_calendar_microtime(tv->tv_sec, tv->tv_usec); |
1c79356b A |
236 | } |
237 | ||
1c79356b | 238 | /* |
91447636 A |
239 | * Verify the calendar value. If negative, |
240 | * reset to zero (the epoch). | |
1c79356b A |
241 | */ |
242 | void | |
91447636 | 243 | inittodr( |
0a7de745 | 244 | __unused time_t base) |
1c79356b | 245 | { |
0a7de745 | 246 | struct timeval tv; |
55e303ae | 247 | |
1c79356b | 248 | /* |
0b4e3aa0 A |
249 | * Assertion: |
250 | * The calendar has already been | |
91447636 | 251 | * set up from the platform clock. |
0b4e3aa0 | 252 | * |
1c79356b A |
253 | * The value returned by microtime() |
254 | * is gotten from the calendar. | |
255 | */ | |
55e303ae | 256 | microtime(&tv); |
1c79356b | 257 | |
91447636 | 258 | if (tv.tv_sec < 0 || tv.tv_usec < 0) { |
0a7de745 A |
259 | printf("WARNING: preposterous time in Real Time Clock"); |
260 | tv.tv_sec = 0; /* the UNIX epoch */ | |
91447636 A |
261 | tv.tv_usec = 0; |
262 | setthetime(&tv); | |
1c79356b A |
263 | printf(" -- CHECK AND RESET THE DATE!\n"); |
264 | } | |
1c79356b A |
265 | } |
266 | ||
91447636 A |
267 | time_t |
268 | boottime_sec(void) | |
269 | { | |
0a7de745 A |
270 | clock_sec_t secs; |
271 | clock_nsec_t nanosecs; | |
9bccf70c | 272 | |
b0d623f7 | 273 | clock_get_boottime_nanotime(&secs, &nanosecs); |
0a7de745 | 274 | return secs; |
b0d623f7 | 275 | } |
9bccf70c | 276 | |
39037602 A |
277 | void |
278 | boottime_timeval(struct timeval *tv) | |
279 | { | |
0a7de745 A |
280 | clock_sec_t secs; |
281 | clock_usec_t microsecs; | |
39037602 A |
282 | |
283 | clock_get_boottime_microtime(&secs, µsecs); | |
284 | ||
285 | tv->tv_sec = secs; | |
286 | tv->tv_usec = microsecs; | |
287 | } | |
288 | ||
1c79356b A |
289 | /* |
290 | * Get value of an interval timer. The process virtual and | |
9bccf70c | 291 | * profiling virtual time timers are kept internally in the |
1c79356b A |
292 | * way they are specified externally: in time until they expire. |
293 | * | |
9bccf70c A |
294 | * The real time interval timer expiration time (p_rtime) |
295 | * is kept as an absolute time rather than as a delta, so that | |
296 | * it is easy to keep periodic real-time signals from drifting. | |
1c79356b | 297 | * |
2d21ac55 A |
298 | * The real time timer is processed by a callout routine. |
299 | * Since a callout may be delayed in real time due to | |
9bccf70c A |
300 | * other processing in the system, it is possible for the real |
301 | * time callout routine (realitexpire, given below), to be delayed | |
302 | * in real time past when it is supposed to occur. It does not | |
303 | * suffice, therefore, to reload the real time .it_value from the | |
304 | * real time .it_interval. Rather, we compute the next time in | |
305 | * absolute time when the timer should go off. | |
2d21ac55 A |
306 | * |
307 | * Returns: 0 Success | |
308 | * EINVAL Invalid argument | |
309 | * copyout:EFAULT Bad address | |
1c79356b | 310 | */ |
1c79356b A |
311 | /* ARGSUSED */ |
312 | int | |
b0d623f7 | 313 | getitimer(struct proc *p, struct getitimer_args *uap, __unused int32_t *retval) |
1c79356b A |
314 | { |
315 | struct itimerval aitv; | |
1c79356b | 316 | |
0a7de745 A |
317 | if (uap->which > ITIMER_PROF) { |
318 | return EINVAL; | |
319 | } | |
2d21ac55 | 320 | |
b0d623f7 A |
321 | bzero(&aitv, sizeof(aitv)); |
322 | ||
2d21ac55 A |
323 | proc_spinlock(p); |
324 | switch (uap->which) { | |
2d21ac55 | 325 | case ITIMER_REAL: |
1c79356b | 326 | /* |
9bccf70c A |
327 | * If time for real time timer has passed return 0, |
328 | * else return difference between current time and | |
329 | * time for the timer to go off. | |
1c79356b A |
330 | */ |
331 | aitv = p->p_realtimer; | |
9bccf70c | 332 | if (timerisset(&p->p_rtime)) { |
0a7de745 | 333 | struct timeval now; |
9bccf70c A |
334 | |
335 | microuptime(&now); | |
0a7de745 | 336 | if (timercmp(&p->p_rtime, &now, <)) { |
1c79356b | 337 | timerclear(&aitv.it_value); |
0a7de745 | 338 | } else { |
9bccf70c A |
339 | aitv.it_value = p->p_rtime; |
340 | timevalsub(&aitv.it_value, &now); | |
341 | } | |
0a7de745 | 342 | } else { |
9bccf70c | 343 | timerclear(&aitv.it_value); |
0a7de745 | 344 | } |
2d21ac55 A |
345 | break; |
346 | ||
347 | case ITIMER_VIRTUAL: | |
348 | aitv = p->p_vtimer_user; | |
349 | break; | |
350 | ||
351 | case ITIMER_PROF: | |
352 | aitv = p->p_vtimer_prof; | |
353 | break; | |
9bccf70c | 354 | } |
2d21ac55 A |
355 | |
356 | proc_spinunlock(p); | |
9bccf70c | 357 | |
91447636 | 358 | if (IS_64BIT_PROCESS(p)) { |
b0d623f7 | 359 | struct user64_itimerval user_itv; |
0a7de745 | 360 | bzero(&user_itv, sizeof(user_itv)); |
91447636 A |
361 | user_itv.it_interval.tv_sec = aitv.it_interval.tv_sec; |
362 | user_itv.it_interval.tv_usec = aitv.it_interval.tv_usec; | |
363 | user_itv.it_value.tv_sec = aitv.it_value.tv_sec; | |
364 | user_itv.it_value.tv_usec = aitv.it_value.tv_usec; | |
0a7de745 | 365 | return copyout((caddr_t)&user_itv, uap->itv, sizeof(user_itv)); |
91447636 | 366 | } else { |
b0d623f7 | 367 | struct user32_itimerval user_itv; |
0a7de745 | 368 | bzero(&user_itv, sizeof(user_itv)); |
f427ee49 | 369 | user_itv.it_interval.tv_sec = (user32_time_t)aitv.it_interval.tv_sec; |
b0d623f7 | 370 | user_itv.it_interval.tv_usec = aitv.it_interval.tv_usec; |
f427ee49 | 371 | user_itv.it_value.tv_sec = (user32_time_t)aitv.it_value.tv_sec; |
b0d623f7 | 372 | user_itv.it_value.tv_usec = aitv.it_value.tv_usec; |
0a7de745 | 373 | return copyout((caddr_t)&user_itv, uap->itv, sizeof(user_itv)); |
91447636 | 374 | } |
1c79356b A |
375 | } |
376 | ||
2d21ac55 A |
377 | /* |
378 | * Returns: 0 Success | |
379 | * EINVAL Invalid argument | |
380 | * copyin:EFAULT Bad address | |
381 | * getitimer:EINVAL Invalid argument | |
382 | * getitimer:EFAULT Bad address | |
383 | */ | |
1c79356b A |
384 | /* ARGSUSED */ |
385 | int | |
b0d623f7 | 386 | setitimer(struct proc *p, struct setitimer_args *uap, int32_t *retval) |
1c79356b A |
387 | { |
388 | struct itimerval aitv; | |
91447636 | 389 | user_addr_t itvp; |
9bccf70c | 390 | int error; |
1c79356b | 391 | |
b0d623f7 A |
392 | bzero(&aitv, sizeof(aitv)); |
393 | ||
0a7de745 A |
394 | if (uap->which > ITIMER_PROF) { |
395 | return EINVAL; | |
396 | } | |
91447636 A |
397 | if ((itvp = uap->itv)) { |
398 | if (IS_64BIT_PROCESS(p)) { | |
b0d623f7 | 399 | struct user64_itimerval user_itv; |
0a7de745 A |
400 | if ((error = copyin(itvp, (caddr_t)&user_itv, sizeof(user_itv)))) { |
401 | return error; | |
402 | } | |
f427ee49 | 403 | aitv.it_interval.tv_sec = (__darwin_time_t)user_itv.it_interval.tv_sec; |
91447636 | 404 | aitv.it_interval.tv_usec = user_itv.it_interval.tv_usec; |
f427ee49 | 405 | aitv.it_value.tv_sec = (__darwin_time_t)user_itv.it_value.tv_sec; |
91447636 | 406 | aitv.it_value.tv_usec = user_itv.it_value.tv_usec; |
0a7de745 | 407 | } else { |
b0d623f7 | 408 | struct user32_itimerval user_itv; |
0a7de745 A |
409 | if ((error = copyin(itvp, (caddr_t)&user_itv, sizeof(user_itv)))) { |
410 | return error; | |
411 | } | |
b0d623f7 A |
412 | aitv.it_interval.tv_sec = user_itv.it_interval.tv_sec; |
413 | aitv.it_interval.tv_usec = user_itv.it_interval.tv_usec; | |
414 | aitv.it_value.tv_sec = user_itv.it_value.tv_sec; | |
415 | aitv.it_value.tv_usec = user_itv.it_value.tv_usec; | |
91447636 A |
416 | } |
417 | } | |
0a7de745 A |
418 | if ((uap->itv = uap->oitv) && (error = getitimer(p, (struct getitimer_args *)uap, retval))) { |
419 | return error; | |
420 | } | |
421 | if (itvp == 0) { | |
422 | return 0; | |
423 | } | |
424 | if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) { | |
425 | return EINVAL; | |
426 | } | |
2d21ac55 A |
427 | |
428 | switch (uap->which) { | |
2d21ac55 A |
429 | case ITIMER_REAL: |
430 | proc_spinlock(p); | |
1c79356b | 431 | if (timerisset(&aitv.it_value)) { |
9bccf70c A |
432 | microuptime(&p->p_rtime); |
433 | timevaladd(&p->p_rtime, &aitv.it_value); | |
2d21ac55 | 434 | p->p_realtimer = aitv; |
39236c6e | 435 | if (!thread_call_enter_delayed_with_leeway(p->p_rcall, NULL, |
0a7de745 | 436 | tvtoabstime(&p->p_rtime), 0, THREAD_CALL_DELAY_USER_NORMAL)) { |
2d21ac55 | 437 | p->p_ractive++; |
0a7de745 A |
438 | } |
439 | } else { | |
2d21ac55 A |
440 | timerclear(&p->p_rtime); |
441 | p->p_realtimer = aitv; | |
0a7de745 | 442 | if (thread_call_cancel(p->p_rcall)) { |
2d21ac55 | 443 | p->p_ractive--; |
0a7de745 | 444 | } |
1c79356b | 445 | } |
2d21ac55 A |
446 | proc_spinunlock(p); |
447 | ||
448 | break; | |
449 | ||
450 | ||
451 | case ITIMER_VIRTUAL: | |
0a7de745 | 452 | if (timerisset(&aitv.it_value)) { |
2d21ac55 | 453 | task_vtimer_set(p->task, TASK_VTIMER_USER); |
0a7de745 | 454 | } else { |
2d21ac55 | 455 | task_vtimer_clear(p->task, TASK_VTIMER_USER); |
0a7de745 | 456 | } |
2d21ac55 A |
457 | |
458 | proc_spinlock(p); | |
459 | p->p_vtimer_user = aitv; | |
460 | proc_spinunlock(p); | |
461 | break; | |
462 | ||
463 | case ITIMER_PROF: | |
0a7de745 | 464 | if (timerisset(&aitv.it_value)) { |
2d21ac55 | 465 | task_vtimer_set(p->task, TASK_VTIMER_PROF); |
0a7de745 | 466 | } else { |
2d21ac55 | 467 | task_vtimer_clear(p->task, TASK_VTIMER_PROF); |
0a7de745 | 468 | } |
9bccf70c | 469 | |
2d21ac55 A |
470 | proc_spinlock(p); |
471 | p->p_vtimer_prof = aitv; | |
472 | proc_spinunlock(p); | |
473 | break; | |
9bccf70c | 474 | } |
9bccf70c | 475 | |
0a7de745 | 476 | return 0; |
1c79356b A |
477 | } |
478 | ||
479 | /* | |
480 | * Real interval timer expired: | |
481 | * send process whose timer expired an alarm signal. | |
482 | * If time is not set up to reload, then just return. | |
483 | * Else compute next time timer should go off which is > current time. | |
484 | * This is where delay in processing this timeout causes multiple | |
485 | * SIGALRM calls to be compressed into one. | |
486 | */ | |
487 | void | |
9bccf70c | 488 | realitexpire( |
39037602 | 489 | struct proc *p) |
1c79356b | 490 | { |
2d21ac55 | 491 | struct proc *r; |
39037602 | 492 | struct timeval t; |
2d21ac55 A |
493 | |
494 | r = proc_find(p->p_pid); | |
495 | ||
496 | proc_spinlock(p); | |
497 | ||
39037602 A |
498 | assert(p->p_ractive > 0); |
499 | ||
2d21ac55 | 500 | if (--p->p_ractive > 0 || r != p) { |
39037602 A |
501 | /* |
502 | * bail, because either proc is exiting | |
503 | * or there's another active thread call | |
504 | */ | |
2d21ac55 A |
505 | proc_spinunlock(p); |
506 | ||
0a7de745 | 507 | if (r != NULL) { |
2d21ac55 | 508 | proc_rele(r); |
0a7de745 | 509 | } |
1c79356b A |
510 | return; |
511 | } | |
39037602 | 512 | |
9bccf70c | 513 | if (!timerisset(&p->p_realtimer.it_interval)) { |
39037602 A |
514 | /* |
515 | * p_realtimer was cleared while this call was pending, | |
516 | * send one last SIGALRM, but don't re-arm | |
517 | */ | |
9bccf70c | 518 | timerclear(&p->p_rtime); |
2d21ac55 | 519 | proc_spinunlock(p); |
9bccf70c | 520 | |
2d21ac55 A |
521 | psignal(p, SIGALRM); |
522 | proc_rele(p); | |
1c79356b | 523 | return; |
1c79356b | 524 | } |
9bccf70c | 525 | |
39037602 A |
526 | proc_spinunlock(p); |
527 | ||
528 | /* | |
529 | * Send the signal before re-arming the next thread call, | |
530 | * so in case psignal blocks, we won't create yet another thread call. | |
531 | */ | |
532 | ||
533 | psignal(p, SIGALRM); | |
534 | ||
535 | proc_spinlock(p); | |
536 | ||
537 | /* Should we still re-arm the next thread call? */ | |
538 | if (!timerisset(&p->p_realtimer.it_interval)) { | |
539 | timerclear(&p->p_rtime); | |
540 | proc_spinunlock(p); | |
541 | ||
542 | proc_rele(p); | |
543 | return; | |
544 | } | |
545 | ||
2d21ac55 | 546 | microuptime(&t); |
9bccf70c | 547 | timevaladd(&p->p_rtime, &p->p_realtimer.it_interval); |
39037602 | 548 | |
2d21ac55 A |
549 | if (timercmp(&p->p_rtime, &t, <=)) { |
550 | if ((p->p_rtime.tv_sec + 2) >= t.tv_sec) { | |
9bccf70c A |
551 | for (;;) { |
552 | timevaladd(&p->p_rtime, &p->p_realtimer.it_interval); | |
0a7de745 | 553 | if (timercmp(&p->p_rtime, &t, >)) { |
9bccf70c | 554 | break; |
0a7de745 | 555 | } |
9bccf70c | 556 | } |
39037602 | 557 | } else { |
9bccf70c | 558 | p->p_rtime = p->p_realtimer.it_interval; |
2d21ac55 | 559 | timevaladd(&p->p_rtime, &t); |
1c79356b | 560 | } |
1c79356b | 561 | } |
9bccf70c | 562 | |
39037602 A |
563 | assert(p->p_rcall != NULL); |
564 | ||
565 | if (!thread_call_enter_delayed_with_leeway(p->p_rcall, NULL, tvtoabstime(&p->p_rtime), 0, | |
0a7de745 | 566 | THREAD_CALL_DELAY_USER_NORMAL)) { |
2d21ac55 | 567 | p->p_ractive++; |
39037602 A |
568 | } |
569 | ||
2d21ac55 | 570 | proc_spinunlock(p); |
55e303ae | 571 | |
2d21ac55 | 572 | proc_rele(p); |
1c79356b A |
573 | } |
574 | ||
39037602 A |
575 | /* |
576 | * Called once in proc_exit to clean up after an armed or pending realitexpire | |
577 | * | |
578 | * This will only be called after the proc refcount is drained, | |
579 | * so realitexpire cannot be currently holding a proc ref. | |
580 | * i.e. it will/has gotten PROC_NULL from proc_find. | |
581 | */ | |
582 | void | |
583 | proc_free_realitimer(proc_t p) | |
584 | { | |
585 | proc_spinlock(p); | |
586 | ||
587 | assert(p->p_rcall != NULL); | |
588 | assert(p->p_refcount == 0); | |
589 | ||
590 | timerclear(&p->p_realtimer.it_interval); | |
591 | ||
592 | if (thread_call_cancel(p->p_rcall)) { | |
593 | assert(p->p_ractive > 0); | |
594 | p->p_ractive--; | |
595 | } | |
596 | ||
597 | while (p->p_ractive > 0) { | |
598 | proc_spinunlock(p); | |
599 | ||
600 | delay(1); | |
601 | ||
602 | proc_spinlock(p); | |
603 | } | |
604 | ||
605 | thread_call_t call = p->p_rcall; | |
606 | p->p_rcall = NULL; | |
607 | ||
608 | proc_spinunlock(p); | |
609 | ||
610 | thread_call_free(call); | |
611 | } | |
612 | ||
1c79356b A |
613 | /* |
614 | * Check that a proposed value to load into the .it_value or | |
2d21ac55 | 615 | * .it_interval part of an interval timer is acceptable. |
1c79356b A |
616 | */ |
617 | int | |
2d21ac55 A |
618 | itimerfix( |
619 | struct timeval *tv) | |
1c79356b | 620 | { |
1c79356b | 621 | if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || |
0a7de745 A |
622 | tv->tv_usec < 0 || tv->tv_usec >= 1000000) { |
623 | return EINVAL; | |
624 | } | |
625 | return 0; | |
1c79356b A |
626 | } |
627 | ||
4bd07ac2 A |
628 | int |
629 | timespec_is_valid(const struct timespec *ts) | |
630 | { | |
631 | /* The INT32_MAX limit ensures the timespec is safe for clock_*() functions | |
632 | * which accept 32-bit ints. */ | |
633 | if (ts->tv_sec < 0 || ts->tv_sec > INT32_MAX || | |
0a7de745 | 634 | ts->tv_nsec < 0 || (unsigned long long)ts->tv_nsec > NSEC_PER_SEC) { |
4bd07ac2 A |
635 | return 0; |
636 | } | |
637 | return 1; | |
638 | } | |
639 | ||
1c79356b A |
640 | /* |
641 | * Decrement an interval timer by a specified number | |
642 | * of microseconds, which must be less than a second, | |
643 | * i.e. < 1000000. If the timer expires, then reload | |
644 | * it. In this case, carry over (usec - old value) to | |
2d21ac55 | 645 | * reduce the value reloaded into the timer so that |
1c79356b A |
646 | * the timer does not drift. This routine assumes |
647 | * that it is called in a context where the timers | |
648 | * on which it is operating cannot change in value. | |
649 | */ | |
650 | int | |
2d21ac55 | 651 | itimerdecr(proc_t p, |
0a7de745 | 652 | struct itimerval *itp, int usec) |
1c79356b | 653 | { |
2d21ac55 | 654 | proc_spinlock(p); |
0a7de745 | 655 | |
1c79356b A |
656 | if (itp->it_value.tv_usec < usec) { |
657 | if (itp->it_value.tv_sec == 0) { | |
658 | /* expired, and already in next interval */ | |
659 | usec -= itp->it_value.tv_usec; | |
660 | goto expire; | |
661 | } | |
662 | itp->it_value.tv_usec += 1000000; | |
663 | itp->it_value.tv_sec--; | |
664 | } | |
665 | itp->it_value.tv_usec -= usec; | |
666 | usec = 0; | |
2d21ac55 A |
667 | if (timerisset(&itp->it_value)) { |
668 | proc_spinunlock(p); | |
0a7de745 | 669 | return 1; |
2d21ac55 | 670 | } |
1c79356b A |
671 | /* expired, exactly at end of interval */ |
672 | expire: | |
673 | if (timerisset(&itp->it_interval)) { | |
674 | itp->it_value = itp->it_interval; | |
2d21ac55 | 675 | if (itp->it_value.tv_sec > 0) { |
0a7de745 A |
676 | itp->it_value.tv_usec -= usec; |
677 | if (itp->it_value.tv_usec < 0) { | |
678 | itp->it_value.tv_usec += 1000000; | |
679 | itp->it_value.tv_sec--; | |
2d21ac55 | 680 | } |
1c79356b | 681 | } |
0a7de745 A |
682 | } else { |
683 | itp->it_value.tv_usec = 0; /* sec is already 0 */ | |
684 | } | |
2d21ac55 | 685 | proc_spinunlock(p); |
0a7de745 | 686 | return 0; |
1c79356b A |
687 | } |
688 | ||
689 | /* | |
690 | * Add and subtract routines for timevals. | |
691 | * N.B.: subtract routine doesn't deal with | |
692 | * results which are before the beginning, | |
693 | * it just gets very confused in this case. | |
694 | * Caveat emptor. | |
695 | */ | |
696 | void | |
9bccf70c A |
697 | timevaladd( |
698 | struct timeval *t1, | |
699 | struct timeval *t2) | |
1c79356b | 700 | { |
1c79356b A |
701 | t1->tv_sec += t2->tv_sec; |
702 | t1->tv_usec += t2->tv_usec; | |
703 | timevalfix(t1); | |
704 | } | |
705 | void | |
9bccf70c A |
706 | timevalsub( |
707 | struct timeval *t1, | |
708 | struct timeval *t2) | |
1c79356b | 709 | { |
1c79356b A |
710 | t1->tv_sec -= t2->tv_sec; |
711 | t1->tv_usec -= t2->tv_usec; | |
712 | timevalfix(t1); | |
713 | } | |
714 | void | |
9bccf70c A |
715 | timevalfix( |
716 | struct timeval *t1) | |
1c79356b | 717 | { |
1c79356b A |
718 | if (t1->tv_usec < 0) { |
719 | t1->tv_sec--; | |
720 | t1->tv_usec += 1000000; | |
721 | } | |
722 | if (t1->tv_usec >= 1000000) { | |
723 | t1->tv_sec++; | |
724 | t1->tv_usec -= 1000000; | |
725 | } | |
726 | } | |
727 | ||
d9a64523 A |
728 | static boolean_t |
729 | timeval_fixusec( | |
730 | struct timeval *t1) | |
731 | { | |
732 | assert(t1->tv_usec >= 0); | |
733 | assert(t1->tv_sec >= 0); | |
734 | ||
735 | if (t1->tv_usec >= 1000000) { | |
0a7de745 | 736 | if (os_add_overflow(t1->tv_sec, t1->tv_usec / 1000000, &t1->tv_sec)) { |
d9a64523 | 737 | return FALSE; |
0a7de745 | 738 | } |
d9a64523 A |
739 | t1->tv_usec = t1->tv_usec % 1000000; |
740 | } | |
741 | ||
742 | return TRUE; | |
743 | } | |
744 | ||
1c79356b A |
745 | /* |
746 | * Return the best possible estimate of the time in the timeval | |
747 | * to which tvp points. | |
748 | */ | |
749 | void | |
9bccf70c | 750 | microtime( |
0a7de745 | 751 | struct timeval *tvp) |
1c79356b | 752 | { |
0a7de745 A |
753 | clock_sec_t tv_sec; |
754 | clock_usec_t tv_usec; | |
b0d623f7 A |
755 | |
756 | clock_get_calendar_microtime(&tv_sec, &tv_usec); | |
757 | ||
758 | tvp->tv_sec = tv_sec; | |
759 | tvp->tv_usec = tv_usec; | |
1c79356b | 760 | } |
9bccf70c | 761 | |
39236c6e A |
762 | void |
763 | microtime_with_abstime( | |
0a7de745 | 764 | struct timeval *tvp, uint64_t *abstime) |
39236c6e | 765 | { |
0a7de745 A |
766 | clock_sec_t tv_sec; |
767 | clock_usec_t tv_usec; | |
39236c6e A |
768 | |
769 | clock_get_calendar_absolute_and_microtime(&tv_sec, &tv_usec, abstime); | |
770 | ||
771 | tvp->tv_sec = tv_sec; | |
772 | tvp->tv_usec = tv_usec; | |
773 | } | |
774 | ||
9bccf70c A |
775 | void |
776 | microuptime( | |
0a7de745 | 777 | struct timeval *tvp) |
9bccf70c | 778 | { |
0a7de745 A |
779 | clock_sec_t tv_sec; |
780 | clock_usec_t tv_usec; | |
b0d623f7 A |
781 | |
782 | clock_get_system_microtime(&tv_sec, &tv_usec); | |
783 | ||
784 | tvp->tv_sec = tv_sec; | |
785 | tvp->tv_usec = tv_usec; | |
9bccf70c A |
786 | } |
787 | ||
788 | /* | |
789 | * Ditto for timespec. | |
790 | */ | |
791 | void | |
792 | nanotime( | |
793 | struct timespec *tsp) | |
794 | { | |
0a7de745 A |
795 | clock_sec_t tv_sec; |
796 | clock_nsec_t tv_nsec; | |
b0d623f7 A |
797 | |
798 | clock_get_calendar_nanotime(&tv_sec, &tv_nsec); | |
799 | ||
800 | tsp->tv_sec = tv_sec; | |
801 | tsp->tv_nsec = tv_nsec; | |
9bccf70c A |
802 | } |
803 | ||
804 | void | |
805 | nanouptime( | |
806 | struct timespec *tsp) | |
807 | { | |
0a7de745 A |
808 | clock_sec_t tv_sec; |
809 | clock_nsec_t tv_nsec; | |
b0d623f7 A |
810 | |
811 | clock_get_system_nanotime(&tv_sec, &tv_nsec); | |
812 | ||
813 | tsp->tv_sec = tv_sec; | |
814 | tsp->tv_nsec = tv_nsec; | |
9bccf70c A |
815 | } |
816 | ||
817 | uint64_t | |
818 | tvtoabstime( | |
0a7de745 | 819 | struct timeval *tvp) |
9bccf70c | 820 | { |
0a7de745 | 821 | uint64_t result, usresult; |
9bccf70c A |
822 | |
823 | clock_interval_to_absolutetime_interval( | |
f427ee49 | 824 | (uint32_t)tvp->tv_sec, NSEC_PER_SEC, &result); |
9bccf70c | 825 | clock_interval_to_absolutetime_interval( |
0a7de745 | 826 | tvp->tv_usec, NSEC_PER_USEC, &usresult); |
9bccf70c | 827 | |
0a7de745 | 828 | return result + usresult; |
9bccf70c | 829 | } |
39236c6e | 830 | |
4bd07ac2 A |
831 | uint64_t |
832 | tstoabstime(struct timespec *ts) | |
833 | { | |
834 | uint64_t abstime_s, abstime_ns; | |
f427ee49 A |
835 | clock_interval_to_absolutetime_interval((uint32_t)ts->tv_sec, NSEC_PER_SEC, &abstime_s); |
836 | clock_interval_to_absolutetime_interval((uint32_t)ts->tv_nsec, 1, &abstime_ns); | |
4bd07ac2 A |
837 | return abstime_s + abstime_ns; |
838 | } | |
839 | ||
39236c6e A |
840 | #if NETWORKING |
841 | /* | |
842 | * ratecheck(): simple time-based rate-limit checking. | |
843 | */ | |
844 | int | |
845 | ratecheck(struct timeval *lasttime, const struct timeval *mininterval) | |
846 | { | |
847 | struct timeval tv, delta; | |
848 | int rv = 0; | |
849 | ||
850 | net_uptime2timeval(&tv); | |
851 | delta = tv; | |
852 | timevalsub(&delta, lasttime); | |
853 | ||
854 | /* | |
855 | * check for 0,0 is so that the message will be seen at least once, | |
856 | * even if interval is huge. | |
857 | */ | |
858 | if (timevalcmp(&delta, mininterval, >=) || | |
859 | (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) { | |
860 | *lasttime = tv; | |
861 | rv = 1; | |
862 | } | |
863 | ||
0a7de745 | 864 | return rv; |
39236c6e A |
865 | } |
866 | ||
867 | /* | |
868 | * ppsratecheck(): packets (or events) per second limitation. | |
869 | */ | |
870 | int | |
871 | ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) | |
872 | { | |
873 | struct timeval tv, delta; | |
874 | int rv; | |
875 | ||
876 | net_uptime2timeval(&tv); | |
877 | ||
878 | timersub(&tv, lasttime, &delta); | |
879 | ||
880 | /* | |
881 | * Check for 0,0 so that the message will be seen at least once. | |
882 | * If more than one second has passed since the last update of | |
883 | * lasttime, reset the counter. | |
884 | * | |
885 | * we do increment *curpps even in *curpps < maxpps case, as some may | |
886 | * try to use *curpps for stat purposes as well. | |
887 | */ | |
888 | if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || | |
889 | delta.tv_sec >= 1) { | |
890 | *lasttime = tv; | |
891 | *curpps = 0; | |
892 | rv = 1; | |
0a7de745 | 893 | } else if (maxpps < 0) { |
39236c6e | 894 | rv = 1; |
0a7de745 | 895 | } else if (*curpps < maxpps) { |
39236c6e | 896 | rv = 1; |
0a7de745 | 897 | } else { |
39236c6e | 898 | rv = 0; |
0a7de745 | 899 | } |
39236c6e A |
900 | |
901 | #if 1 /* DIAGNOSTIC? */ | |
902 | /* be careful about wrap-around */ | |
0a7de745 | 903 | if (*curpps + 1 > 0) { |
39236c6e | 904 | *curpps = *curpps + 1; |
0a7de745 | 905 | } |
39236c6e A |
906 | #else |
907 | /* | |
908 | * assume that there's not too many calls to this function. | |
909 | * not sure if the assumption holds, as it depends on *caller's* | |
910 | * behavior, not the behavior of this function. | |
911 | * IMHO it is wrong to make assumption on the caller's behavior, | |
912 | * so the above #if is #if 1, not #ifdef DIAGNOSTIC. | |
913 | */ | |
914 | *curpps = *curpps + 1; | |
915 | #endif | |
916 | ||
0a7de745 | 917 | return rv; |
39236c6e A |
918 | } |
919 | #endif /* NETWORKING */ | |
920 | ||
0a7de745 A |
921 | int |
922 | __mach_bridge_remote_time(__unused struct proc *p, struct __mach_bridge_remote_time_args *mbrt_args, uint64_t *retval) | |
923 | { | |
924 | *retval = mach_bridge_remote_time(mbrt_args->local_timestamp); | |
925 | return 0; | |
926 | } |