]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
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. | |
8f6c56a5 | 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 A |
76 | #include <sys/vnode.h> |
77 | ||
91447636 A |
78 | #include <sys/mount_internal.h> |
79 | #include <sys/sysproto.h> | |
80 | #include <sys/signalvar.h> | |
1c79356b | 81 | |
1c79356b | 82 | #include <kern/clock.h> |
2d21ac55 | 83 | #include <kern/task.h> |
91447636 | 84 | #include <kern/thread_call.h> |
2d21ac55 A |
85 | #if CONFIG_MACF |
86 | #include <security/mac_framework.h> | |
87 | #endif | |
1c79356b A |
88 | |
89 | #define HZ 100 /* XXX */ | |
90 | ||
9bccf70c | 91 | /* simple lock used to access timezone, tz structure */ |
91447636 A |
92 | lck_spin_t * tz_slock; |
93 | lck_grp_t * tz_slock_grp; | |
94 | lck_attr_t * tz_slock_attr; | |
95 | lck_grp_attr_t *tz_slock_grp_attr; | |
96 | ||
97 | static void setthetime( | |
98 | struct timeval *tv); | |
99 | ||
2d21ac55 | 100 | void time_zone_slock_init(void) __attribute__((section("__TEXT, initcode"))); |
91447636 | 101 | |
1c79356b A |
102 | /* |
103 | * Time of day and interval timer support. | |
104 | * | |
105 | * These routines provide the kernel entry points to get and set | |
106 | * the time-of-day and per-process interval timers. Subroutines | |
107 | * here provide support for adding and subtracting timeval structures | |
108 | * and decrementing interval timers, optionally reloading the interval | |
109 | * timers when they expire. | |
110 | */ | |
1c79356b A |
111 | /* ARGSUSED */ |
112 | int | |
0c530ab8 A |
113 | gettimeofday( |
114 | __unused struct proc *p, | |
115 | struct gettimeofday_args *uap, | |
116 | register_t *retval) | |
1c79356b | 117 | { |
1c79356b | 118 | int error = 0; |
9bccf70c A |
119 | struct timezone ltz; /* local copy */ |
120 | ||
0c530ab8 | 121 | if (uap->tp) |
2d21ac55 | 122 | clock_gettimeofday((uint32_t *)&retval[0], (uint32_t *)&retval[1]); |
1c79356b | 123 | |
9bccf70c | 124 | if (uap->tzp) { |
91447636 | 125 | lck_spin_lock(tz_slock); |
9bccf70c | 126 | ltz = tz; |
91447636 | 127 | lck_spin_unlock(tz_slock); |
0c530ab8 A |
128 | |
129 | error = copyout((caddr_t)<z, CAST_USER_ADDR_T(uap->tzp), sizeof (tz)); | |
9bccf70c | 130 | } |
1c79356b | 131 | |
0c530ab8 | 132 | return (error); |
1c79356b A |
133 | } |
134 | ||
91447636 A |
135 | /* |
136 | * XXX Y2038 bug because of setthetime() argument | |
137 | */ | |
1c79356b A |
138 | /* ARGSUSED */ |
139 | int | |
2d21ac55 | 140 | settimeofday(__unused struct proc *p, struct settimeofday_args *uap, __unused register_t *retval) |
1c79356b A |
141 | { |
142 | struct timeval atv; | |
143 | struct timezone atz; | |
91447636 | 144 | int error; |
1c79356b | 145 | |
2d21ac55 A |
146 | #if CONFIG_MACF |
147 | error = mac_system_check_settime(kauth_cred_get()); | |
148 | if (error) | |
149 | return (error); | |
150 | #endif | |
151 | #ifndef CONFIG_EMBEDDED | |
91447636 | 152 | if ((error = suser(kauth_cred_get(), &p->p_acflag))) |
1c79356b | 153 | return (error); |
2d21ac55 | 154 | #endif |
91447636 A |
155 | /* Verify all parameters before changing time */ |
156 | if (uap->tv) { | |
157 | if (IS_64BIT_PROCESS(p)) { | |
158 | struct user_timeval user_atv; | |
159 | error = copyin(uap->tv, &user_atv, sizeof(struct user_timeval)); | |
160 | atv.tv_sec = user_atv.tv_sec; | |
161 | atv.tv_usec = user_atv.tv_usec; | |
162 | } else { | |
163 | error = copyin(uap->tv, &atv, sizeof(struct timeval)); | |
164 | } | |
165 | if (error) | |
166 | return (error); | |
167 | } | |
168 | if (uap->tzp && (error = copyin(uap->tzp, (caddr_t)&atz, sizeof(atz)))) | |
1c79356b | 169 | return (error); |
91447636 A |
170 | if (uap->tv) { |
171 | timevalfix(&atv); | |
172 | if (atv.tv_sec < 0 || (atv.tv_sec == 0 && atv.tv_usec < 0)) | |
173 | return (EPERM); | |
1c79356b | 174 | setthetime(&atv); |
91447636 | 175 | } |
9bccf70c | 176 | if (uap->tzp) { |
91447636 | 177 | lck_spin_lock(tz_slock); |
1c79356b | 178 | tz = atz; |
91447636 | 179 | lck_spin_unlock(tz_slock); |
9bccf70c | 180 | } |
1c79356b A |
181 | return (0); |
182 | } | |
183 | ||
91447636 A |
184 | static void |
185 | setthetime( | |
186 | struct timeval *tv) | |
1c79356b | 187 | { |
55e303ae | 188 | clock_set_calendar_microtime(tv->tv_sec, tv->tv_usec); |
1c79356b A |
189 | } |
190 | ||
91447636 A |
191 | /* |
192 | * XXX Y2038 bug because of clock_adjtime() first argument | |
193 | */ | |
1c79356b A |
194 | /* ARGSUSED */ |
195 | int | |
2d21ac55 | 196 | adjtime(struct proc *p, struct adjtime_args *uap, __unused register_t *retval) |
1c79356b | 197 | { |
9bccf70c | 198 | struct timeval atv; |
9bccf70c | 199 | int error; |
1c79356b | 200 | |
2d21ac55 A |
201 | #if CONFIG_MACF |
202 | error = mac_system_check_settime(kauth_cred_get()); | |
203 | if (error) | |
204 | return (error); | |
205 | #endif | |
91447636 | 206 | if ((error = suser(kauth_cred_get(), &p->p_acflag))) |
1c79356b | 207 | return (error); |
91447636 A |
208 | if (IS_64BIT_PROCESS(p)) { |
209 | struct user_timeval user_atv; | |
210 | error = copyin(uap->delta, &user_atv, sizeof(struct user_timeval)); | |
211 | atv.tv_sec = user_atv.tv_sec; | |
212 | atv.tv_usec = user_atv.tv_usec; | |
213 | } else { | |
214 | error = copyin(uap->delta, &atv, sizeof(struct timeval)); | |
215 | } | |
216 | if (error) | |
9bccf70c | 217 | return (error); |
1c79356b | 218 | |
91447636 A |
219 | /* |
220 | * Compute the total correction and the rate at which to apply it. | |
221 | */ | |
222 | clock_adjtime((int32_t *)&atv.tv_sec, &atv.tv_usec); | |
1c79356b | 223 | |
1c79356b | 224 | if (uap->olddelta) { |
91447636 A |
225 | if (IS_64BIT_PROCESS(p)) { |
226 | struct user_timeval user_atv; | |
227 | user_atv.tv_sec = atv.tv_sec; | |
228 | user_atv.tv_usec = atv.tv_usec; | |
229 | error = copyout(&user_atv, uap->olddelta, sizeof(struct user_timeval)); | |
230 | } else { | |
231 | error = copyout(&atv, uap->olddelta, sizeof(struct timeval)); | |
232 | } | |
1c79356b | 233 | } |
1c79356b | 234 | |
9bccf70c | 235 | return (0); |
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 A |
243 | inittodr( |
244 | __unused time_t base) | |
1c79356b | 245 | { |
55e303ae A |
246 | struct timeval tv; |
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) { |
1c79356b | 259 | printf ("WARNING: preposterous time in Real Time Clock"); |
91447636 A |
260 | tv.tv_sec = 0; /* the UNIX epoch */ |
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 | { | |
270 | uint32_t sec, nanosec; | |
271 | clock_get_boottime_nanotime(&sec, &nanosec); | |
272 | return (sec); | |
273 | } | |
9bccf70c | 274 | |
91447636 | 275 | uint64_t tvtoabstime(struct timeval *tvp); |
9bccf70c | 276 | |
1c79356b A |
277 | /* |
278 | * Get value of an interval timer. The process virtual and | |
9bccf70c | 279 | * profiling virtual time timers are kept internally in the |
1c79356b A |
280 | * way they are specified externally: in time until they expire. |
281 | * | |
9bccf70c A |
282 | * The real time interval timer expiration time (p_rtime) |
283 | * is kept as an absolute time rather than as a delta, so that | |
284 | * it is easy to keep periodic real-time signals from drifting. | |
1c79356b | 285 | * |
2d21ac55 A |
286 | * The real time timer is processed by a callout routine. |
287 | * Since a callout may be delayed in real time due to | |
9bccf70c A |
288 | * other processing in the system, it is possible for the real |
289 | * time callout routine (realitexpire, given below), to be delayed | |
290 | * in real time past when it is supposed to occur. It does not | |
291 | * suffice, therefore, to reload the real time .it_value from the | |
292 | * real time .it_interval. Rather, we compute the next time in | |
293 | * absolute time when the timer should go off. | |
2d21ac55 A |
294 | * |
295 | * Returns: 0 Success | |
296 | * EINVAL Invalid argument | |
297 | * copyout:EFAULT Bad address | |
1c79356b | 298 | */ |
1c79356b A |
299 | /* ARGSUSED */ |
300 | int | |
2d21ac55 | 301 | getitimer(struct proc *p, struct getitimer_args *uap, __unused register_t *retval) |
1c79356b A |
302 | { |
303 | struct itimerval aitv; | |
1c79356b A |
304 | |
305 | if (uap->which > ITIMER_PROF) | |
306 | return(EINVAL); | |
2d21ac55 A |
307 | |
308 | proc_spinlock(p); | |
309 | switch (uap->which) { | |
310 | ||
311 | case ITIMER_REAL: | |
1c79356b | 312 | /* |
9bccf70c A |
313 | * If time for real time timer has passed return 0, |
314 | * else return difference between current time and | |
315 | * time for the timer to go off. | |
1c79356b A |
316 | */ |
317 | aitv = p->p_realtimer; | |
9bccf70c A |
318 | if (timerisset(&p->p_rtime)) { |
319 | struct timeval now; | |
320 | ||
321 | microuptime(&now); | |
322 | if (timercmp(&p->p_rtime, &now, <)) | |
1c79356b | 323 | timerclear(&aitv.it_value); |
9bccf70c A |
324 | else { |
325 | aitv.it_value = p->p_rtime; | |
326 | timevalsub(&aitv.it_value, &now); | |
327 | } | |
328 | } | |
329 | else | |
330 | timerclear(&aitv.it_value); | |
2d21ac55 A |
331 | break; |
332 | ||
333 | case ITIMER_VIRTUAL: | |
334 | aitv = p->p_vtimer_user; | |
335 | break; | |
336 | ||
337 | case ITIMER_PROF: | |
338 | aitv = p->p_vtimer_prof; | |
339 | break; | |
9bccf70c | 340 | } |
2d21ac55 A |
341 | |
342 | proc_spinunlock(p); | |
9bccf70c | 343 | |
91447636 A |
344 | if (IS_64BIT_PROCESS(p)) { |
345 | struct user_itimerval user_itv; | |
346 | user_itv.it_interval.tv_sec = aitv.it_interval.tv_sec; | |
347 | user_itv.it_interval.tv_usec = aitv.it_interval.tv_usec; | |
348 | user_itv.it_value.tv_sec = aitv.it_value.tv_sec; | |
349 | user_itv.it_value.tv_usec = aitv.it_value.tv_usec; | |
350 | return (copyout((caddr_t)&user_itv, uap->itv, sizeof (struct user_itimerval))); | |
351 | } else { | |
352 | return (copyout((caddr_t)&aitv, uap->itv, sizeof (struct itimerval))); | |
353 | } | |
1c79356b A |
354 | } |
355 | ||
2d21ac55 A |
356 | /* |
357 | * Returns: 0 Success | |
358 | * EINVAL Invalid argument | |
359 | * copyin:EFAULT Bad address | |
360 | * getitimer:EINVAL Invalid argument | |
361 | * getitimer:EFAULT Bad address | |
362 | */ | |
1c79356b A |
363 | /* ARGSUSED */ |
364 | int | |
2d21ac55 | 365 | setitimer(struct proc *p, struct setitimer_args *uap, register_t *retval) |
1c79356b A |
366 | { |
367 | struct itimerval aitv; | |
91447636 | 368 | user_addr_t itvp; |
9bccf70c | 369 | int error; |
1c79356b A |
370 | |
371 | if (uap->which > ITIMER_PROF) | |
9bccf70c | 372 | return (EINVAL); |
91447636 A |
373 | if ((itvp = uap->itv)) { |
374 | if (IS_64BIT_PROCESS(p)) { | |
375 | struct user_itimerval user_itv; | |
376 | if ((error = copyin(itvp, (caddr_t)&user_itv, sizeof (struct user_itimerval)))) | |
377 | return (error); | |
378 | aitv.it_interval.tv_sec = user_itv.it_interval.tv_sec; | |
379 | aitv.it_interval.tv_usec = user_itv.it_interval.tv_usec; | |
380 | aitv.it_value.tv_sec = user_itv.it_value.tv_sec; | |
381 | aitv.it_value.tv_usec = user_itv.it_value.tv_usec; | |
382 | } else { | |
383 | if ((error = copyin(itvp, (caddr_t)&aitv, sizeof (struct itimerval)))) | |
384 | return (error); | |
385 | } | |
386 | } | |
387 | if ((uap->itv = uap->oitv) && (error = getitimer(p, (struct getitimer_args *)uap, retval))) | |
1c79356b A |
388 | return (error); |
389 | if (itvp == 0) | |
390 | return (0); | |
391 | if (itimerfix(&aitv.it_value) || itimerfix(&aitv.it_interval)) | |
392 | return (EINVAL); | |
2d21ac55 A |
393 | |
394 | switch (uap->which) { | |
395 | ||
396 | case ITIMER_REAL: | |
397 | proc_spinlock(p); | |
1c79356b | 398 | if (timerisset(&aitv.it_value)) { |
9bccf70c A |
399 | microuptime(&p->p_rtime); |
400 | timevaladd(&p->p_rtime, &aitv.it_value); | |
2d21ac55 A |
401 | p->p_realtimer = aitv; |
402 | if (!thread_call_enter_delayed(p->p_rcall, tvtoabstime(&p->p_rtime))) | |
403 | p->p_ractive++; | |
404 | } else { | |
405 | timerclear(&p->p_rtime); | |
406 | p->p_realtimer = aitv; | |
407 | if (thread_call_cancel(p->p_rcall)) | |
408 | p->p_ractive--; | |
1c79356b | 409 | } |
2d21ac55 A |
410 | proc_spinunlock(p); |
411 | ||
412 | break; | |
413 | ||
414 | ||
415 | case ITIMER_VIRTUAL: | |
416 | if (timerisset(&aitv.it_value)) | |
417 | task_vtimer_set(p->task, TASK_VTIMER_USER); | |
418 | else | |
419 | task_vtimer_clear(p->task, TASK_VTIMER_USER); | |
420 | ||
421 | proc_spinlock(p); | |
422 | p->p_vtimer_user = aitv; | |
423 | proc_spinunlock(p); | |
424 | break; | |
425 | ||
426 | case ITIMER_PROF: | |
427 | if (timerisset(&aitv.it_value)) | |
428 | task_vtimer_set(p->task, TASK_VTIMER_PROF); | |
9bccf70c | 429 | else |
2d21ac55 | 430 | task_vtimer_clear(p->task, TASK_VTIMER_PROF); |
9bccf70c | 431 | |
2d21ac55 A |
432 | proc_spinlock(p); |
433 | p->p_vtimer_prof = aitv; | |
434 | proc_spinunlock(p); | |
435 | break; | |
9bccf70c | 436 | } |
9bccf70c A |
437 | |
438 | return (0); | |
1c79356b A |
439 | } |
440 | ||
441 | /* | |
442 | * Real interval timer expired: | |
443 | * send process whose timer expired an alarm signal. | |
444 | * If time is not set up to reload, then just return. | |
445 | * Else compute next time timer should go off which is > current time. | |
446 | * This is where delay in processing this timeout causes multiple | |
447 | * SIGALRM calls to be compressed into one. | |
448 | */ | |
449 | void | |
9bccf70c | 450 | realitexpire( |
2d21ac55 | 451 | struct proc *p) |
1c79356b | 452 | { |
2d21ac55 A |
453 | struct proc *r; |
454 | struct timeval t; | |
455 | ||
456 | r = proc_find(p->p_pid); | |
457 | ||
458 | proc_spinlock(p); | |
459 | ||
460 | if (--p->p_ractive > 0 || r != p) { | |
461 | proc_spinunlock(p); | |
462 | ||
463 | if (r != NULL) | |
464 | proc_rele(r); | |
1c79356b A |
465 | return; |
466 | } | |
2d21ac55 | 467 | |
9bccf70c A |
468 | if (!timerisset(&p->p_realtimer.it_interval)) { |
469 | timerclear(&p->p_rtime); | |
2d21ac55 | 470 | proc_spinunlock(p); |
9bccf70c | 471 | |
2d21ac55 A |
472 | psignal(p, SIGALRM); |
473 | proc_rele(p); | |
1c79356b | 474 | return; |
1c79356b | 475 | } |
9bccf70c | 476 | |
2d21ac55 | 477 | microuptime(&t); |
9bccf70c | 478 | timevaladd(&p->p_rtime, &p->p_realtimer.it_interval); |
2d21ac55 A |
479 | if (timercmp(&p->p_rtime, &t, <=)) { |
480 | if ((p->p_rtime.tv_sec + 2) >= t.tv_sec) { | |
9bccf70c A |
481 | for (;;) { |
482 | timevaladd(&p->p_rtime, &p->p_realtimer.it_interval); | |
2d21ac55 | 483 | if (timercmp(&p->p_rtime, &t, >)) |
9bccf70c A |
484 | break; |
485 | } | |
486 | } | |
487 | else { | |
488 | p->p_rtime = p->p_realtimer.it_interval; | |
2d21ac55 | 489 | timevaladd(&p->p_rtime, &t); |
1c79356b | 490 | } |
1c79356b | 491 | } |
9bccf70c | 492 | |
2d21ac55 A |
493 | if (!thread_call_enter_delayed(p->p_rcall, tvtoabstime(&p->p_rtime))) |
494 | p->p_ractive++; | |
495 | proc_spinunlock(p); | |
55e303ae | 496 | |
2d21ac55 A |
497 | psignal(p, SIGALRM); |
498 | proc_rele(p); | |
1c79356b A |
499 | } |
500 | ||
501 | /* | |
502 | * Check that a proposed value to load into the .it_value or | |
2d21ac55 | 503 | * .it_interval part of an interval timer is acceptable. |
1c79356b A |
504 | */ |
505 | int | |
2d21ac55 A |
506 | itimerfix( |
507 | struct timeval *tv) | |
1c79356b A |
508 | { |
509 | ||
510 | if (tv->tv_sec < 0 || tv->tv_sec > 100000000 || | |
511 | tv->tv_usec < 0 || tv->tv_usec >= 1000000) | |
512 | return (EINVAL); | |
1c79356b A |
513 | return (0); |
514 | } | |
515 | ||
516 | /* | |
517 | * Decrement an interval timer by a specified number | |
518 | * of microseconds, which must be less than a second, | |
519 | * i.e. < 1000000. If the timer expires, then reload | |
520 | * it. In this case, carry over (usec - old value) to | |
2d21ac55 | 521 | * reduce the value reloaded into the timer so that |
1c79356b A |
522 | * the timer does not drift. This routine assumes |
523 | * that it is called in a context where the timers | |
524 | * on which it is operating cannot change in value. | |
525 | */ | |
526 | int | |
2d21ac55 A |
527 | itimerdecr(proc_t p, |
528 | struct itimerval *itp, int usec) | |
1c79356b A |
529 | { |
530 | ||
2d21ac55 A |
531 | proc_spinlock(p); |
532 | ||
1c79356b A |
533 | if (itp->it_value.tv_usec < usec) { |
534 | if (itp->it_value.tv_sec == 0) { | |
535 | /* expired, and already in next interval */ | |
536 | usec -= itp->it_value.tv_usec; | |
537 | goto expire; | |
538 | } | |
539 | itp->it_value.tv_usec += 1000000; | |
540 | itp->it_value.tv_sec--; | |
541 | } | |
542 | itp->it_value.tv_usec -= usec; | |
543 | usec = 0; | |
2d21ac55 A |
544 | if (timerisset(&itp->it_value)) { |
545 | proc_spinunlock(p); | |
1c79356b | 546 | return (1); |
2d21ac55 | 547 | } |
1c79356b A |
548 | /* expired, exactly at end of interval */ |
549 | expire: | |
550 | if (timerisset(&itp->it_interval)) { | |
551 | itp->it_value = itp->it_interval; | |
2d21ac55 | 552 | if (itp->it_value.tv_sec > 0) { |
1c79356b A |
553 | itp->it_value.tv_usec -= usec; |
554 | if (itp->it_value.tv_usec < 0) { | |
555 | itp->it_value.tv_usec += 1000000; | |
556 | itp->it_value.tv_sec--; | |
2d21ac55 | 557 | } |
1c79356b A |
558 | } |
559 | } else | |
560 | itp->it_value.tv_usec = 0; /* sec is already 0 */ | |
2d21ac55 | 561 | proc_spinunlock(p); |
1c79356b A |
562 | return (0); |
563 | } | |
564 | ||
565 | /* | |
566 | * Add and subtract routines for timevals. | |
567 | * N.B.: subtract routine doesn't deal with | |
568 | * results which are before the beginning, | |
569 | * it just gets very confused in this case. | |
570 | * Caveat emptor. | |
571 | */ | |
572 | void | |
9bccf70c A |
573 | timevaladd( |
574 | struct timeval *t1, | |
575 | struct timeval *t2) | |
1c79356b A |
576 | { |
577 | ||
578 | t1->tv_sec += t2->tv_sec; | |
579 | t1->tv_usec += t2->tv_usec; | |
580 | timevalfix(t1); | |
581 | } | |
582 | void | |
9bccf70c A |
583 | timevalsub( |
584 | struct timeval *t1, | |
585 | struct timeval *t2) | |
1c79356b A |
586 | { |
587 | ||
588 | t1->tv_sec -= t2->tv_sec; | |
589 | t1->tv_usec -= t2->tv_usec; | |
590 | timevalfix(t1); | |
591 | } | |
592 | void | |
9bccf70c A |
593 | timevalfix( |
594 | struct timeval *t1) | |
1c79356b A |
595 | { |
596 | ||
597 | if (t1->tv_usec < 0) { | |
598 | t1->tv_sec--; | |
599 | t1->tv_usec += 1000000; | |
600 | } | |
601 | if (t1->tv_usec >= 1000000) { | |
602 | t1->tv_sec++; | |
603 | t1->tv_usec -= 1000000; | |
604 | } | |
605 | } | |
606 | ||
607 | /* | |
608 | * Return the best possible estimate of the time in the timeval | |
609 | * to which tvp points. | |
610 | */ | |
611 | void | |
9bccf70c A |
612 | microtime( |
613 | struct timeval *tvp) | |
1c79356b | 614 | { |
2d21ac55 | 615 | clock_get_calendar_microtime((uint32_t *)&tvp->tv_sec, (uint32_t *)&tvp->tv_usec); |
1c79356b | 616 | } |
9bccf70c A |
617 | |
618 | void | |
619 | microuptime( | |
620 | struct timeval *tvp) | |
621 | { | |
2d21ac55 | 622 | clock_get_system_microtime((uint32_t *)&tvp->tv_sec, (uint32_t *)&tvp->tv_usec); |
9bccf70c A |
623 | } |
624 | ||
625 | /* | |
626 | * Ditto for timespec. | |
627 | */ | |
628 | void | |
629 | nanotime( | |
630 | struct timespec *tsp) | |
631 | { | |
91447636 | 632 | clock_get_calendar_nanotime((uint32_t *)&tsp->tv_sec, (uint32_t *)&tsp->tv_nsec); |
9bccf70c A |
633 | } |
634 | ||
635 | void | |
636 | nanouptime( | |
637 | struct timespec *tsp) | |
638 | { | |
91447636 | 639 | clock_get_system_nanotime((uint32_t *)&tsp->tv_sec, (uint32_t *)&tsp->tv_nsec); |
9bccf70c A |
640 | } |
641 | ||
642 | uint64_t | |
643 | tvtoabstime( | |
644 | struct timeval *tvp) | |
645 | { | |
646 | uint64_t result, usresult; | |
647 | ||
648 | clock_interval_to_absolutetime_interval( | |
649 | tvp->tv_sec, NSEC_PER_SEC, &result); | |
650 | clock_interval_to_absolutetime_interval( | |
651 | tvp->tv_usec, NSEC_PER_USEC, &usresult); | |
652 | ||
653 | return (result + usresult); | |
654 | } | |
655 | void | |
656 | time_zone_slock_init(void) | |
657 | { | |
91447636 A |
658 | /* allocate lock group attribute and group */ |
659 | tz_slock_grp_attr = lck_grp_attr_alloc_init(); | |
9bccf70c | 660 | |
91447636 | 661 | tz_slock_grp = lck_grp_alloc_init("tzlock", tz_slock_grp_attr); |
9bccf70c | 662 | |
91447636 A |
663 | /* Allocate lock attribute */ |
664 | tz_slock_attr = lck_attr_alloc_init(); | |
9bccf70c | 665 | |
91447636 A |
666 | /* Allocate the spin lock */ |
667 | tz_slock = lck_spin_alloc_init(tz_slock_grp, tz_slock_attr); | |
9bccf70c | 668 | } |