]>
Commit | Line | Data |
---|---|---|
0c530ab8 | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
0c530ab8 | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0c530ab8 | 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. | |
0c530ab8 | 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 | |
0c530ab8 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. | |
0c530ab8 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
0c530ab8 A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * DEPRECATED INTERFACES - Should be removed | |
33 | * | |
34 | * Purpose: Routines for the creation and use of kernel | |
35 | * alarm clock services. This file and the ipc | |
36 | * routines in kern/ipc_clock.c constitute the | |
37 | * machine-independent clock service layer. | |
38 | */ | |
39 | ||
40 | #include <mach/mach_types.h> | |
41 | ||
42 | #include <kern/lock.h> | |
43 | #include <kern/host.h> | |
44 | #include <kern/spl.h> | |
45 | #include <kern/sched_prim.h> | |
46 | #include <kern/thread.h> | |
47 | #include <kern/ipc_host.h> | |
48 | #include <kern/clock.h> | |
49 | #include <kern/zalloc.h> | |
50 | ||
51 | #include <ipc/ipc_types.h> | |
52 | #include <ipc/ipc_port.h> | |
53 | ||
54 | #include <mach/mach_traps.h> | |
55 | #include <mach/mach_time.h> | |
56 | ||
57 | #include <mach/clock_server.h> | |
58 | #include <mach/clock_reply.h> | |
59 | #include <mach/clock_priv_server.h> | |
60 | ||
61 | #include <mach/mach_host_server.h> | |
62 | #include <mach/host_priv_server.h> | |
63 | ||
64 | /* | |
65 | * Actual clock alarm structure. Used for user clock_sleep() and | |
66 | * clock_alarm() calls. Alarms are allocated from the alarm free | |
67 | * list and entered in time priority order into the active alarm | |
68 | * chain of the target clock. | |
69 | */ | |
70 | struct alarm { | |
71 | struct alarm *al_next; /* next alarm in chain */ | |
72 | struct alarm *al_prev; /* previous alarm in chain */ | |
73 | int al_status; /* alarm status */ | |
74 | mach_timespec_t al_time; /* alarm time */ | |
75 | struct { /* message alarm data */ | |
76 | int type; /* alarm type */ | |
77 | ipc_port_t port; /* alarm port */ | |
78 | mach_msg_type_name_t | |
79 | port_type; /* alarm port type */ | |
80 | struct clock *clock; /* alarm clock */ | |
81 | void *data; /* alarm data */ | |
82 | } al_alrm; | |
83 | #define al_type al_alrm.type | |
84 | #define al_port al_alrm.port | |
85 | #define al_port_type al_alrm.port_type | |
86 | #define al_clock al_alrm.clock | |
87 | #define al_data al_alrm.data | |
88 | long al_seqno; /* alarm sequence number */ | |
89 | }; | |
90 | typedef struct alarm alarm_data_t; | |
91 | ||
92 | /* alarm status */ | |
93 | #define ALARM_FREE 0 /* alarm is on free list */ | |
94 | #define ALARM_SLEEP 1 /* active clock_sleep() */ | |
95 | #define ALARM_CLOCK 2 /* active clock_alarm() */ | |
96 | #define ALARM_DONE 4 /* alarm has expired */ | |
97 | ||
98 | /* local data declarations */ | |
99 | decl_simple_lock_data(static,alarm_lock) /* alarm synchronization */ | |
100 | static struct zone *alarm_zone; /* zone for user alarms */ | |
101 | static struct alarm *alrmfree; /* alarm free list pointer */ | |
102 | static struct alarm *alrmdone; /* alarm done list pointer */ | |
103 | static struct alarm *alrmlist; | |
104 | static long alrm_seqno; /* uniquely identifies alarms */ | |
105 | static thread_call_data_t alarm_done_call; | |
106 | static timer_call_data_t alarm_expire_timer; | |
107 | ||
108 | extern struct clock clock_list[]; | |
109 | extern int clock_count; | |
110 | ||
111 | static void post_alarm( | |
112 | alarm_t alarm); | |
113 | ||
114 | static void set_alarm( | |
115 | mach_timespec_t *alarm_time); | |
116 | ||
117 | static int check_time( | |
118 | alarm_type_t alarm_type, | |
119 | mach_timespec_t *alarm_time, | |
120 | mach_timespec_t *clock_time); | |
121 | ||
122 | static void alarm_done(void); | |
123 | ||
124 | static void alarm_expire(void); | |
125 | ||
126 | static kern_return_t clock_sleep_internal( | |
127 | clock_t clock, | |
128 | sleep_type_t sleep_type, | |
129 | mach_timespec_t *sleep_time); | |
130 | ||
131 | int rtclock_config(void); | |
132 | ||
133 | int rtclock_init(void); | |
134 | ||
135 | kern_return_t rtclock_gettime( | |
136 | mach_timespec_t *cur_time); | |
137 | ||
138 | kern_return_t rtclock_getattr( | |
139 | clock_flavor_t flavor, | |
140 | clock_attr_t attr, | |
141 | mach_msg_type_number_t *count); | |
142 | ||
143 | struct clock_ops sysclk_ops = { | |
144 | rtclock_config, rtclock_init, | |
145 | rtclock_gettime, | |
146 | rtclock_getattr, | |
147 | }; | |
148 | ||
149 | kern_return_t calend_gettime( | |
150 | mach_timespec_t *cur_time); | |
151 | ||
152 | kern_return_t calend_getattr( | |
153 | clock_flavor_t flavor, | |
154 | clock_attr_t attr, | |
155 | mach_msg_type_number_t *count); | |
156 | ||
157 | struct clock_ops calend_ops = { | |
2d21ac55 | 158 | NULL, NULL, |
0c530ab8 A |
159 | calend_gettime, |
160 | calend_getattr, | |
161 | }; | |
162 | ||
163 | /* | |
164 | * Macros to lock/unlock clock system. | |
165 | */ | |
166 | #define LOCK_ALARM(s) \ | |
167 | s = splclock(); \ | |
168 | simple_lock(&alarm_lock); | |
169 | ||
170 | #define UNLOCK_ALARM(s) \ | |
171 | simple_unlock(&alarm_lock); \ | |
172 | splx(s); | |
173 | ||
174 | void | |
175 | clock_oldconfig(void) | |
176 | { | |
177 | clock_t clock; | |
178 | register int i; | |
179 | ||
180 | simple_lock_init(&alarm_lock, 0); | |
181 | thread_call_setup(&alarm_done_call, (thread_call_func_t)alarm_done, NULL); | |
182 | timer_call_setup(&alarm_expire_timer, (timer_call_func_t)alarm_expire, NULL); | |
183 | ||
184 | /* | |
185 | * Configure clock devices. | |
186 | */ | |
187 | for (i = 0; i < clock_count; i++) { | |
188 | clock = &clock_list[i]; | |
189 | if (clock->cl_ops && clock->cl_ops->c_config) { | |
190 | if ((*clock->cl_ops->c_config)() == 0) | |
2d21ac55 | 191 | clock->cl_ops = NULL; |
0c530ab8 A |
192 | } |
193 | } | |
194 | ||
195 | /* start alarm sequence numbers at 0 */ | |
196 | alrm_seqno = 0; | |
197 | } | |
198 | ||
199 | void | |
200 | clock_oldinit(void) | |
201 | { | |
202 | clock_t clock; | |
203 | register int i; | |
204 | ||
205 | /* | |
206 | * Initialize basic clock structures. | |
207 | */ | |
208 | for (i = 0; i < clock_count; i++) { | |
209 | clock = &clock_list[i]; | |
210 | if (clock->cl_ops && clock->cl_ops->c_init) | |
211 | (*clock->cl_ops->c_init)(); | |
212 | } | |
213 | } | |
214 | ||
215 | /* | |
216 | * Initialize the clock ipc service facility. | |
217 | */ | |
218 | void | |
219 | clock_service_create(void) | |
220 | { | |
221 | clock_t clock; | |
222 | register int i; | |
223 | ||
224 | /* | |
225 | * Initialize ipc clock services. | |
226 | */ | |
227 | for (i = 0; i < clock_count; i++) { | |
228 | clock = &clock_list[i]; | |
229 | if (clock->cl_ops) { | |
230 | ipc_clock_init(clock); | |
231 | ipc_clock_enable(clock); | |
232 | } | |
233 | } | |
234 | ||
235 | /* | |
236 | * Perform miscellaneous late | |
237 | * initialization. | |
238 | */ | |
239 | i = sizeof(struct alarm); | |
240 | alarm_zone = zinit(i, (4096/i)*i, 10*i, "alarms"); | |
241 | } | |
242 | ||
243 | /* | |
244 | * Get the service port on a clock. | |
245 | */ | |
246 | kern_return_t | |
247 | host_get_clock_service( | |
248 | host_t host, | |
249 | clock_id_t clock_id, | |
250 | clock_t *clock) /* OUT */ | |
251 | { | |
252 | if (host == HOST_NULL || clock_id < 0 || clock_id >= clock_count) { | |
253 | *clock = CLOCK_NULL; | |
254 | return (KERN_INVALID_ARGUMENT); | |
255 | } | |
256 | ||
257 | *clock = &clock_list[clock_id]; | |
258 | if ((*clock)->cl_ops == 0) | |
259 | return (KERN_FAILURE); | |
260 | return (KERN_SUCCESS); | |
261 | } | |
262 | ||
263 | /* | |
264 | * Get the control port on a clock. | |
265 | */ | |
266 | kern_return_t | |
267 | host_get_clock_control( | |
268 | host_priv_t host_priv, | |
269 | clock_id_t clock_id, | |
270 | clock_t *clock) /* OUT */ | |
271 | { | |
272 | if (host_priv == HOST_PRIV_NULL || | |
273 | clock_id < 0 || clock_id >= clock_count) { | |
274 | *clock = CLOCK_NULL; | |
275 | return (KERN_INVALID_ARGUMENT); | |
276 | } | |
277 | ||
278 | *clock = &clock_list[clock_id]; | |
279 | if ((*clock)->cl_ops == 0) | |
280 | return (KERN_FAILURE); | |
281 | return (KERN_SUCCESS); | |
282 | } | |
283 | ||
284 | /* | |
285 | * Get the current clock time. | |
286 | */ | |
287 | kern_return_t | |
288 | clock_get_time( | |
289 | clock_t clock, | |
290 | mach_timespec_t *cur_time) /* OUT */ | |
291 | { | |
292 | if (clock == CLOCK_NULL) | |
293 | return (KERN_INVALID_ARGUMENT); | |
294 | return ((*clock->cl_ops->c_gettime)(cur_time)); | |
295 | } | |
296 | ||
297 | kern_return_t | |
298 | rtclock_gettime( | |
299 | mach_timespec_t *time) /* OUT */ | |
300 | { | |
301 | clock_get_system_nanotime(&time->tv_sec, (uint32_t *)&time->tv_nsec); | |
302 | ||
303 | return (KERN_SUCCESS); | |
304 | } | |
305 | ||
306 | kern_return_t | |
307 | calend_gettime( | |
308 | mach_timespec_t *time) /* OUT */ | |
309 | { | |
310 | clock_get_calendar_nanotime(&time->tv_sec, (uint32_t *)&time->tv_nsec); | |
311 | ||
312 | return (KERN_SUCCESS); | |
313 | } | |
314 | ||
315 | /* | |
316 | * Get clock attributes. | |
317 | */ | |
318 | kern_return_t | |
319 | clock_get_attributes( | |
320 | clock_t clock, | |
321 | clock_flavor_t flavor, | |
322 | clock_attr_t attr, /* OUT */ | |
323 | mach_msg_type_number_t *count) /* IN/OUT */ | |
324 | { | |
325 | if (clock == CLOCK_NULL) | |
326 | return (KERN_INVALID_ARGUMENT); | |
327 | if (clock->cl_ops->c_getattr) | |
328 | return (clock->cl_ops->c_getattr(flavor, attr, count)); | |
329 | return (KERN_FAILURE); | |
330 | } | |
331 | ||
332 | kern_return_t | |
333 | rtclock_getattr( | |
334 | clock_flavor_t flavor, | |
335 | clock_attr_t attr, /* OUT */ | |
336 | mach_msg_type_number_t *count) /* IN/OUT */ | |
337 | { | |
338 | if (*count != 1) | |
339 | return (KERN_FAILURE); | |
340 | ||
341 | switch (flavor) { | |
342 | ||
343 | case CLOCK_GET_TIME_RES: /* >0 res */ | |
344 | case CLOCK_ALARM_CURRES: /* =0 no alarm */ | |
345 | case CLOCK_ALARM_MINRES: | |
346 | case CLOCK_ALARM_MAXRES: | |
347 | *(clock_res_t *) attr = NSEC_PER_SEC / 100; | |
348 | break; | |
349 | ||
350 | default: | |
351 | return (KERN_INVALID_VALUE); | |
352 | } | |
353 | ||
354 | return (KERN_SUCCESS); | |
355 | } | |
356 | ||
357 | kern_return_t | |
358 | calend_getattr( | |
359 | clock_flavor_t flavor, | |
360 | clock_attr_t attr, /* OUT */ | |
361 | mach_msg_type_number_t *count) /* IN/OUT */ | |
362 | { | |
363 | if (*count != 1) | |
364 | return (KERN_FAILURE); | |
365 | ||
366 | switch (flavor) { | |
367 | ||
368 | case CLOCK_GET_TIME_RES: /* >0 res */ | |
369 | *(clock_res_t *) attr = NSEC_PER_SEC / 100; | |
370 | break; | |
371 | ||
372 | case CLOCK_ALARM_CURRES: /* =0 no alarm */ | |
373 | case CLOCK_ALARM_MINRES: | |
374 | case CLOCK_ALARM_MAXRES: | |
375 | *(clock_res_t *) attr = 0; | |
376 | break; | |
377 | ||
378 | default: | |
379 | return (KERN_INVALID_VALUE); | |
380 | } | |
381 | ||
382 | return (KERN_SUCCESS); | |
383 | } | |
384 | ||
385 | /* | |
386 | * Set the current clock time. | |
387 | */ | |
388 | kern_return_t | |
389 | clock_set_time( | |
390 | clock_t clock, | |
391 | __unused mach_timespec_t new_time) | |
392 | { | |
393 | if (clock == CLOCK_NULL) | |
394 | return (KERN_INVALID_ARGUMENT); | |
395 | return (KERN_FAILURE); | |
396 | } | |
397 | ||
398 | /* | |
399 | * Set the clock alarm resolution. | |
400 | */ | |
401 | kern_return_t | |
402 | clock_set_attributes( | |
403 | clock_t clock, | |
404 | __unused clock_flavor_t flavor, | |
405 | __unused clock_attr_t attr, | |
406 | __unused mach_msg_type_number_t count) | |
407 | { | |
408 | if (clock == CLOCK_NULL) | |
409 | return (KERN_INVALID_ARGUMENT); | |
410 | return (KERN_FAILURE); | |
411 | } | |
412 | ||
413 | /* | |
414 | * Setup a clock alarm. | |
415 | */ | |
416 | kern_return_t | |
417 | clock_alarm( | |
418 | clock_t clock, | |
419 | alarm_type_t alarm_type, | |
420 | mach_timespec_t alarm_time, | |
421 | ipc_port_t alarm_port, | |
422 | mach_msg_type_name_t alarm_port_type) | |
423 | { | |
424 | alarm_t alarm; | |
425 | mach_timespec_t clock_time; | |
426 | int chkstat; | |
427 | kern_return_t reply_code; | |
428 | spl_t s; | |
429 | ||
430 | if (clock == CLOCK_NULL) | |
431 | return (KERN_INVALID_ARGUMENT); | |
432 | if (clock != &clock_list[SYSTEM_CLOCK]) | |
433 | return (KERN_FAILURE); | |
434 | if (IP_VALID(alarm_port) == 0) | |
435 | return (KERN_INVALID_CAPABILITY); | |
436 | ||
437 | /* | |
438 | * Check alarm parameters. If parameters are invalid, | |
439 | * send alarm message immediately. | |
440 | */ | |
441 | (*clock->cl_ops->c_gettime)(&clock_time); | |
442 | chkstat = check_time(alarm_type, &alarm_time, &clock_time); | |
443 | if (chkstat <= 0) { | |
444 | reply_code = (chkstat < 0 ? KERN_INVALID_VALUE : KERN_SUCCESS); | |
445 | clock_alarm_reply(alarm_port, alarm_port_type, | |
446 | reply_code, alarm_type, clock_time); | |
447 | return (KERN_SUCCESS); | |
448 | } | |
449 | ||
450 | /* | |
451 | * Get alarm and add to clock alarm list. | |
452 | */ | |
453 | ||
454 | LOCK_ALARM(s); | |
455 | if ((alarm = alrmfree) == 0) { | |
456 | UNLOCK_ALARM(s); | |
457 | alarm = (alarm_t) zalloc(alarm_zone); | |
458 | if (alarm == 0) | |
459 | return (KERN_RESOURCE_SHORTAGE); | |
460 | LOCK_ALARM(s); | |
461 | } | |
462 | else | |
463 | alrmfree = alarm->al_next; | |
464 | ||
465 | alarm->al_status = ALARM_CLOCK; | |
466 | alarm->al_time = alarm_time; | |
467 | alarm->al_type = alarm_type; | |
468 | alarm->al_port = alarm_port; | |
469 | alarm->al_port_type = alarm_port_type; | |
470 | alarm->al_clock = clock; | |
471 | alarm->al_seqno = alrm_seqno++; | |
472 | post_alarm(alarm); | |
473 | UNLOCK_ALARM(s); | |
474 | ||
475 | return (KERN_SUCCESS); | |
476 | } | |
477 | ||
478 | /* | |
479 | * Sleep on a clock. System trap. User-level libmach clock_sleep | |
480 | * interface call takes a mach_timespec_t sleep_time argument which it | |
481 | * converts to sleep_sec and sleep_nsec arguments which are then | |
482 | * passed to clock_sleep_trap. | |
483 | */ | |
484 | kern_return_t | |
485 | clock_sleep_trap( | |
486 | struct clock_sleep_trap_args *args) | |
487 | { | |
488 | mach_port_name_t clock_name = args->clock_name; | |
489 | sleep_type_t sleep_type = args->sleep_type; | |
490 | int sleep_sec = args->sleep_sec; | |
491 | int sleep_nsec = args->sleep_nsec; | |
492 | mach_vm_address_t wakeup_time_addr = args->wakeup_time; | |
493 | clock_t clock; | |
494 | mach_timespec_t swtime; | |
495 | kern_return_t rvalue; | |
496 | ||
497 | /* | |
498 | * Convert the trap parameters. | |
499 | */ | |
500 | if (clock_name == MACH_PORT_NULL) | |
501 | clock = &clock_list[SYSTEM_CLOCK]; | |
502 | else | |
503 | clock = port_name_to_clock(clock_name); | |
504 | ||
505 | swtime.tv_sec = sleep_sec; | |
506 | swtime.tv_nsec = sleep_nsec; | |
507 | ||
508 | /* | |
509 | * Call the actual clock_sleep routine. | |
510 | */ | |
511 | rvalue = clock_sleep_internal(clock, sleep_type, &swtime); | |
512 | ||
513 | /* | |
514 | * Return current time as wakeup time. | |
515 | */ | |
516 | if (rvalue != KERN_INVALID_ARGUMENT && rvalue != KERN_FAILURE) { | |
517 | copyout((char *)&swtime, wakeup_time_addr, sizeof(mach_timespec_t)); | |
518 | } | |
519 | return (rvalue); | |
520 | } | |
521 | ||
522 | static kern_return_t | |
523 | clock_sleep_internal( | |
524 | clock_t clock, | |
525 | sleep_type_t sleep_type, | |
526 | mach_timespec_t *sleep_time) | |
527 | { | |
528 | alarm_t alarm; | |
529 | mach_timespec_t clock_time; | |
530 | kern_return_t rvalue; | |
531 | int chkstat; | |
532 | spl_t s; | |
533 | ||
534 | if (clock == CLOCK_NULL) | |
535 | return (KERN_INVALID_ARGUMENT); | |
536 | ||
537 | if (clock != &clock_list[SYSTEM_CLOCK]) | |
538 | return (KERN_FAILURE); | |
539 | ||
540 | /* | |
541 | * Check sleep parameters. If parameters are invalid | |
542 | * return an error, otherwise post alarm request. | |
543 | */ | |
544 | (*clock->cl_ops->c_gettime)(&clock_time); | |
545 | ||
546 | chkstat = check_time(sleep_type, sleep_time, &clock_time); | |
547 | if (chkstat < 0) | |
548 | return (KERN_INVALID_VALUE); | |
549 | rvalue = KERN_SUCCESS; | |
550 | if (chkstat > 0) { | |
551 | wait_result_t wait_result; | |
552 | ||
553 | /* | |
554 | * Get alarm and add to clock alarm list. | |
555 | */ | |
556 | ||
557 | LOCK_ALARM(s); | |
558 | if ((alarm = alrmfree) == 0) { | |
559 | UNLOCK_ALARM(s); | |
560 | alarm = (alarm_t) zalloc(alarm_zone); | |
561 | if (alarm == 0) | |
562 | return (KERN_RESOURCE_SHORTAGE); | |
563 | LOCK_ALARM(s); | |
564 | } | |
565 | else | |
566 | alrmfree = alarm->al_next; | |
567 | ||
568 | /* | |
569 | * Wait for alarm to occur. | |
570 | */ | |
571 | wait_result = assert_wait((event_t)alarm, THREAD_ABORTSAFE); | |
572 | if (wait_result == THREAD_WAITING) { | |
573 | alarm->al_time = *sleep_time; | |
574 | alarm->al_status = ALARM_SLEEP; | |
575 | post_alarm(alarm); | |
576 | UNLOCK_ALARM(s); | |
577 | ||
578 | wait_result = thread_block(THREAD_CONTINUE_NULL); | |
579 | ||
580 | /* | |
581 | * Note if alarm expired normally or whether it | |
582 | * was aborted. If aborted, delete alarm from | |
583 | * clock alarm list. Return alarm to free list. | |
584 | */ | |
585 | LOCK_ALARM(s); | |
586 | if (alarm->al_status != ALARM_DONE) { | |
587 | assert(wait_result != THREAD_AWAKENED); | |
588 | if (((alarm->al_prev)->al_next = alarm->al_next) != NULL) | |
589 | (alarm->al_next)->al_prev = alarm->al_prev; | |
590 | rvalue = KERN_ABORTED; | |
591 | } | |
592 | *sleep_time = alarm->al_time; | |
593 | alarm->al_status = ALARM_FREE; | |
594 | } else { | |
595 | assert(wait_result == THREAD_INTERRUPTED); | |
596 | assert(alarm->al_status == ALARM_FREE); | |
597 | rvalue = KERN_ABORTED; | |
598 | } | |
599 | alarm->al_next = alrmfree; | |
600 | alrmfree = alarm; | |
601 | UNLOCK_ALARM(s); | |
602 | } | |
603 | else | |
604 | *sleep_time = clock_time; | |
605 | ||
606 | return (rvalue); | |
607 | } | |
608 | ||
609 | /* | |
610 | * Service clock alarm expirations. | |
611 | */ | |
612 | static void | |
613 | alarm_expire(void) | |
614 | { | |
615 | clock_t clock; | |
616 | register alarm_t alrm1; | |
617 | register alarm_t alrm2; | |
618 | mach_timespec_t clock_time; | |
619 | mach_timespec_t *alarm_time; | |
620 | spl_t s; | |
621 | ||
622 | clock = &clock_list[SYSTEM_CLOCK]; | |
623 | (*clock->cl_ops->c_gettime)(&clock_time); | |
624 | ||
625 | /* | |
626 | * Update clock alarm list. Alarms that are due are moved | |
627 | * to the alarmdone list to be serviced by a thread callout. | |
628 | */ | |
629 | LOCK_ALARM(s); | |
630 | alrm1 = (alarm_t)&alrmlist; | |
631 | while ((alrm2 = alrm1->al_next) != NULL) { | |
632 | alarm_time = &alrm2->al_time; | |
633 | if (CMP_MACH_TIMESPEC(alarm_time, &clock_time) > 0) | |
634 | break; | |
635 | ||
636 | /* | |
637 | * Alarm has expired, so remove it from the | |
638 | * clock alarm list. | |
639 | */ | |
640 | if ((alrm1->al_next = alrm2->al_next) != NULL) | |
641 | (alrm1->al_next)->al_prev = alrm1; | |
642 | ||
643 | /* | |
644 | * If a clock_sleep() alarm, wakeup the thread | |
645 | * which issued the clock_sleep() call. | |
646 | */ | |
647 | if (alrm2->al_status == ALARM_SLEEP) { | |
2d21ac55 | 648 | alrm2->al_next = NULL; |
0c530ab8 A |
649 | alrm2->al_status = ALARM_DONE; |
650 | alrm2->al_time = clock_time; | |
651 | thread_wakeup((event_t)alrm2); | |
652 | } | |
653 | ||
654 | /* | |
655 | * If a clock_alarm() alarm, place the alarm on | |
656 | * the alarm done list and schedule the alarm | |
657 | * delivery mechanism. | |
658 | */ | |
659 | else { | |
660 | assert(alrm2->al_status == ALARM_CLOCK); | |
661 | if ((alrm2->al_next = alrmdone) != NULL) | |
662 | alrmdone->al_prev = alrm2; | |
663 | else | |
664 | thread_call_enter(&alarm_done_call); | |
665 | alrm2->al_prev = (alarm_t)&alrmdone; | |
666 | alrmdone = alrm2; | |
667 | alrm2->al_status = ALARM_DONE; | |
668 | alrm2->al_time = clock_time; | |
669 | } | |
670 | } | |
671 | ||
672 | /* | |
673 | * Setup to expire for the next pending alarm. | |
674 | */ | |
675 | if (alrm2) | |
676 | set_alarm(alarm_time); | |
677 | UNLOCK_ALARM(s); | |
678 | } | |
679 | ||
680 | static void | |
681 | alarm_done(void) | |
682 | { | |
683 | register alarm_t alrm; | |
684 | kern_return_t code; | |
685 | spl_t s; | |
686 | ||
687 | LOCK_ALARM(s); | |
688 | while ((alrm = alrmdone) != NULL) { | |
689 | if ((alrmdone = alrm->al_next) != NULL) | |
690 | alrmdone->al_prev = (alarm_t)&alrmdone; | |
691 | UNLOCK_ALARM(s); | |
692 | ||
693 | code = (alrm->al_status == ALARM_DONE? KERN_SUCCESS: KERN_ABORTED); | |
694 | if (alrm->al_port != IP_NULL) { | |
695 | /* Deliver message to designated port */ | |
696 | if (IP_VALID(alrm->al_port)) { | |
697 | clock_alarm_reply(alrm->al_port, alrm->al_port_type, code, | |
698 | alrm->al_type, alrm->al_time); | |
699 | } | |
700 | ||
701 | LOCK_ALARM(s); | |
702 | alrm->al_status = ALARM_FREE; | |
703 | alrm->al_next = alrmfree; | |
704 | alrmfree = alrm; | |
705 | } | |
706 | else | |
707 | panic("clock_alarm_deliver"); | |
708 | } | |
709 | ||
710 | UNLOCK_ALARM(s); | |
711 | } | |
712 | ||
713 | /* | |
714 | * Post an alarm on the active alarm list. | |
715 | * | |
716 | * Always called from within a LOCK_ALARM() code section. | |
717 | */ | |
718 | static void | |
719 | post_alarm( | |
720 | alarm_t alarm) | |
721 | { | |
722 | register alarm_t alrm1, alrm2; | |
723 | mach_timespec_t *alarm_time; | |
724 | mach_timespec_t *queue_time; | |
725 | ||
726 | /* | |
727 | * Traverse alarm list until queue time is greater | |
728 | * than alarm time, then insert alarm. | |
729 | */ | |
730 | alarm_time = &alarm->al_time; | |
731 | alrm1 = (alarm_t)&alrmlist; | |
732 | while ((alrm2 = alrm1->al_next) != NULL) { | |
733 | queue_time = &alrm2->al_time; | |
734 | if (CMP_MACH_TIMESPEC(queue_time, alarm_time) > 0) | |
735 | break; | |
736 | alrm1 = alrm2; | |
737 | } | |
738 | alrm1->al_next = alarm; | |
739 | alarm->al_next = alrm2; | |
740 | alarm->al_prev = alrm1; | |
741 | if (alrm2) | |
742 | alrm2->al_prev = alarm; | |
743 | ||
744 | /* | |
745 | * If the inserted alarm is the 'earliest' alarm, | |
746 | * reset the device layer alarm time accordingly. | |
747 | */ | |
748 | if (alrmlist == alarm) | |
749 | set_alarm(alarm_time); | |
750 | } | |
751 | ||
752 | static void | |
753 | set_alarm( | |
754 | mach_timespec_t *alarm_time) | |
755 | { | |
756 | uint64_t abstime; | |
757 | ||
758 | nanotime_to_absolutetime(alarm_time->tv_sec, alarm_time->tv_nsec, &abstime); | |
759 | timer_call_enter(&alarm_expire_timer, abstime); | |
760 | } | |
761 | ||
762 | /* | |
763 | * Check the validity of 'alarm_time' and 'alarm_type'. If either | |
764 | * argument is invalid, return a negative value. If the 'alarm_time' | |
765 | * is now, return a 0 value. If the 'alarm_time' is in the future, | |
766 | * return a positive value. | |
767 | */ | |
768 | static int | |
769 | check_time( | |
770 | alarm_type_t alarm_type, | |
771 | mach_timespec_t *alarm_time, | |
772 | mach_timespec_t *clock_time) | |
773 | { | |
774 | int result; | |
775 | ||
776 | if (BAD_ALRMTYPE(alarm_type)) | |
777 | return (-1); | |
778 | if (BAD_MACH_TIMESPEC(alarm_time)) | |
779 | return (-1); | |
780 | if ((alarm_type & ALRMTYPE) == TIME_RELATIVE) | |
781 | ADD_MACH_TIMESPEC(alarm_time, clock_time); | |
782 | ||
783 | result = CMP_MACH_TIMESPEC(alarm_time, clock_time); | |
784 | ||
785 | return ((result >= 0)? result: 0); | |
786 | } | |
787 | ||
788 | mach_timespec_t | |
789 | clock_get_system_value(void) | |
790 | { | |
791 | clock_t clock = &clock_list[SYSTEM_CLOCK]; | |
792 | mach_timespec_t value; | |
793 | ||
794 | (void) (*clock->cl_ops->c_gettime)(&value); | |
795 | ||
796 | return value; | |
797 | } | |
798 | ||
799 | mach_timespec_t | |
800 | clock_get_calendar_value(void) | |
801 | { | |
802 | clock_t clock = &clock_list[CALENDAR_CLOCK]; | |
803 | mach_timespec_t value = MACH_TIMESPEC_ZERO; | |
804 | ||
805 | (void) (*clock->cl_ops->c_gettime)(&value); | |
806 | ||
807 | return value; | |
808 | } |