2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * DEPRECATED INTERFACES - Should be removed
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.
40 #include <mach/mach_types.h>
42 #include <kern/lock.h>
43 #include <kern/host.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>
51 #include <ipc/ipc_types.h>
52 #include <ipc/ipc_port.h>
54 #include <mach/mach_traps.h>
55 #include <mach/mach_time.h>
57 #include <mach/clock_server.h>
58 #include <mach/clock_reply.h>
59 #include <mach/clock_priv_server.h>
61 #include <mach/mach_host_server.h>
62 #include <mach/host_priv_server.h>
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.
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 */
79 port_type
; /* alarm port type */
80 struct clock
*clock
; /* alarm clock */
81 void *data
; /* alarm data */
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 */
90 typedef struct alarm alarm_data_t
;
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 */
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
;
108 extern struct clock clock_list
[];
109 extern int clock_count
;
111 static void post_alarm(
114 static void set_alarm(
115 mach_timespec_t
*alarm_time
);
117 static int check_time(
118 alarm_type_t alarm_type
,
119 mach_timespec_t
*alarm_time
,
120 mach_timespec_t
*clock_time
);
122 static void alarm_done(void);
124 static void alarm_expire(void);
126 static kern_return_t
clock_sleep_internal(
128 sleep_type_t sleep_type
,
129 mach_timespec_t
*sleep_time
);
131 int rtclock_config(void);
133 int rtclock_init(void);
135 kern_return_t
rtclock_gettime(
136 mach_timespec_t
*cur_time
);
138 kern_return_t
rtclock_getattr(
139 clock_flavor_t flavor
,
141 mach_msg_type_number_t
*count
);
143 struct clock_ops sysclk_ops
= {
144 rtclock_config
, rtclock_init
,
149 kern_return_t
calend_gettime(
150 mach_timespec_t
*cur_time
);
152 kern_return_t
calend_getattr(
153 clock_flavor_t flavor
,
155 mach_msg_type_number_t
*count
);
157 struct clock_ops calend_ops
= {
164 * Macros to lock/unlock clock system.
166 #define LOCK_ALARM(s) \
168 simple_lock(&alarm_lock);
170 #define UNLOCK_ALARM(s) \
171 simple_unlock(&alarm_lock); \
175 clock_oldconfig(void)
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
);
185 * Configure clock devices.
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)
195 /* start alarm sequence numbers at 0 */
206 * Initialize basic clock structures.
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
)();
216 * Initialize the clock ipc service facility.
219 clock_service_create(void)
225 * Initialize ipc clock services.
227 for (i
= 0; i
< clock_count
; i
++) {
228 clock
= &clock_list
[i
];
230 ipc_clock_init(clock
);
231 ipc_clock_enable(clock
);
236 * Perform miscellaneous late
239 i
= sizeof(struct alarm
);
240 alarm_zone
= zinit(i
, (4096/i
)*i
, 10*i
, "alarms");
244 * Get the service port on a clock.
247 host_get_clock_service(
250 clock_t *clock
) /* OUT */
252 if (host
== HOST_NULL
|| clock_id
< 0 || clock_id
>= clock_count
) {
254 return (KERN_INVALID_ARGUMENT
);
257 *clock
= &clock_list
[clock_id
];
258 if ((*clock
)->cl_ops
== 0)
259 return (KERN_FAILURE
);
260 return (KERN_SUCCESS
);
264 * Get the control port on a clock.
267 host_get_clock_control(
268 host_priv_t host_priv
,
270 clock_t *clock
) /* OUT */
272 if (host_priv
== HOST_PRIV_NULL
||
273 clock_id
< 0 || clock_id
>= clock_count
) {
275 return (KERN_INVALID_ARGUMENT
);
278 *clock
= &clock_list
[clock_id
];
279 if ((*clock
)->cl_ops
== 0)
280 return (KERN_FAILURE
);
281 return (KERN_SUCCESS
);
285 * Get the current clock time.
290 mach_timespec_t
*cur_time
) /* OUT */
292 if (clock
== CLOCK_NULL
)
293 return (KERN_INVALID_ARGUMENT
);
294 return ((*clock
->cl_ops
->c_gettime
)(cur_time
));
299 mach_timespec_t
*time
) /* OUT */
301 clock_get_system_nanotime(&time
->tv_sec
, (uint32_t *)&time
->tv_nsec
);
303 return (KERN_SUCCESS
);
308 mach_timespec_t
*time
) /* OUT */
310 clock_get_calendar_nanotime(&time
->tv_sec
, (uint32_t *)&time
->tv_nsec
);
312 return (KERN_SUCCESS
);
316 * Get clock attributes.
319 clock_get_attributes(
321 clock_flavor_t flavor
,
322 clock_attr_t attr
, /* OUT */
323 mach_msg_type_number_t
*count
) /* IN/OUT */
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
);
334 clock_flavor_t flavor
,
335 clock_attr_t attr
, /* OUT */
336 mach_msg_type_number_t
*count
) /* IN/OUT */
339 return (KERN_FAILURE
);
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;
351 return (KERN_INVALID_VALUE
);
354 return (KERN_SUCCESS
);
359 clock_flavor_t flavor
,
360 clock_attr_t attr
, /* OUT */
361 mach_msg_type_number_t
*count
) /* IN/OUT */
364 return (KERN_FAILURE
);
368 case CLOCK_GET_TIME_RES
: /* >0 res */
369 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
372 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
373 case CLOCK_ALARM_MINRES
:
374 case CLOCK_ALARM_MAXRES
:
375 *(clock_res_t
*) attr
= 0;
379 return (KERN_INVALID_VALUE
);
382 return (KERN_SUCCESS
);
386 * Set the current clock time.
391 __unused mach_timespec_t new_time
)
393 if (clock
== CLOCK_NULL
)
394 return (KERN_INVALID_ARGUMENT
);
395 return (KERN_FAILURE
);
399 * Set the clock alarm resolution.
402 clock_set_attributes(
404 __unused clock_flavor_t flavor
,
405 __unused clock_attr_t attr
,
406 __unused mach_msg_type_number_t count
)
408 if (clock
== CLOCK_NULL
)
409 return (KERN_INVALID_ARGUMENT
);
410 return (KERN_FAILURE
);
414 * Setup a clock alarm.
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
)
425 mach_timespec_t clock_time
;
427 kern_return_t reply_code
;
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
);
438 * Check alarm parameters. If parameters are invalid,
439 * send alarm message immediately.
441 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
442 chkstat
= check_time(alarm_type
, &alarm_time
, &clock_time
);
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
);
451 * Get alarm and add to clock alarm list.
455 if ((alarm
= alrmfree
) == 0) {
457 alarm
= (alarm_t
) zalloc(alarm_zone
);
459 return (KERN_RESOURCE_SHORTAGE
);
463 alrmfree
= alarm
->al_next
;
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
++;
475 return (KERN_SUCCESS
);
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.
486 struct clock_sleep_trap_args
*args
)
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
;
494 mach_timespec_t swtime
;
495 kern_return_t rvalue
;
498 * Convert the trap parameters.
500 if (clock_name
== MACH_PORT_NULL
)
501 clock
= &clock_list
[SYSTEM_CLOCK
];
503 clock
= port_name_to_clock(clock_name
);
505 swtime
.tv_sec
= sleep_sec
;
506 swtime
.tv_nsec
= sleep_nsec
;
509 * Call the actual clock_sleep routine.
511 rvalue
= clock_sleep_internal(clock
, sleep_type
, &swtime
);
514 * Return current time as wakeup time.
516 if (rvalue
!= KERN_INVALID_ARGUMENT
&& rvalue
!= KERN_FAILURE
) {
517 copyout((char *)&swtime
, wakeup_time_addr
, sizeof(mach_timespec_t
));
523 clock_sleep_internal(
525 sleep_type_t sleep_type
,
526 mach_timespec_t
*sleep_time
)
529 mach_timespec_t clock_time
;
530 kern_return_t rvalue
;
534 if (clock
== CLOCK_NULL
)
535 return (KERN_INVALID_ARGUMENT
);
537 if (clock
!= &clock_list
[SYSTEM_CLOCK
])
538 return (KERN_FAILURE
);
541 * Check sleep parameters. If parameters are invalid
542 * return an error, otherwise post alarm request.
544 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
546 chkstat
= check_time(sleep_type
, sleep_time
, &clock_time
);
548 return (KERN_INVALID_VALUE
);
549 rvalue
= KERN_SUCCESS
;
551 wait_result_t wait_result
;
554 * Get alarm and add to clock alarm list.
558 if ((alarm
= alrmfree
) == 0) {
560 alarm
= (alarm_t
) zalloc(alarm_zone
);
562 return (KERN_RESOURCE_SHORTAGE
);
566 alrmfree
= alarm
->al_next
;
569 * Wait for alarm to occur.
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
;
578 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
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.
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
;
592 *sleep_time
= alarm
->al_time
;
593 alarm
->al_status
= ALARM_FREE
;
595 assert(wait_result
== THREAD_INTERRUPTED
);
596 assert(alarm
->al_status
== ALARM_FREE
);
597 rvalue
= KERN_ABORTED
;
599 alarm
->al_next
= alrmfree
;
604 *sleep_time
= clock_time
;
610 * Service clock alarm expirations.
616 register alarm_t alrm1
;
617 register alarm_t alrm2
;
618 mach_timespec_t clock_time
;
619 mach_timespec_t
*alarm_time
;
622 clock
= &clock_list
[SYSTEM_CLOCK
];
623 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
626 * Update clock alarm list. Alarms that are due are moved
627 * to the alarmdone list to be serviced by a thread callout.
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)
637 * Alarm has expired, so remove it from the
640 if ((alrm1
->al_next
= alrm2
->al_next
) != NULL
)
641 (alrm1
->al_next
)->al_prev
= alrm1
;
644 * If a clock_sleep() alarm, wakeup the thread
645 * which issued the clock_sleep() call.
647 if (alrm2
->al_status
== ALARM_SLEEP
) {
649 alrm2
->al_status
= ALARM_DONE
;
650 alrm2
->al_time
= clock_time
;
651 thread_wakeup((event_t
)alrm2
);
655 * If a clock_alarm() alarm, place the alarm on
656 * the alarm done list and schedule the alarm
657 * delivery mechanism.
660 assert(alrm2
->al_status
== ALARM_CLOCK
);
661 if ((alrm2
->al_next
= alrmdone
) != NULL
)
662 alrmdone
->al_prev
= alrm2
;
664 thread_call_enter(&alarm_done_call
);
665 alrm2
->al_prev
= (alarm_t
)&alrmdone
;
667 alrm2
->al_status
= ALARM_DONE
;
668 alrm2
->al_time
= clock_time
;
673 * Setup to expire for the next pending alarm.
676 set_alarm(alarm_time
);
683 register alarm_t alrm
;
688 while ((alrm
= alrmdone
) != NULL
) {
689 if ((alrmdone
= alrm
->al_next
) != NULL
)
690 alrmdone
->al_prev
= (alarm_t
)&alrmdone
;
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
);
702 alrm
->al_status
= ALARM_FREE
;
703 alrm
->al_next
= alrmfree
;
707 panic("clock_alarm_deliver");
714 * Post an alarm on the active alarm list.
716 * Always called from within a LOCK_ALARM() code section.
722 register alarm_t alrm1
, alrm2
;
723 mach_timespec_t
*alarm_time
;
724 mach_timespec_t
*queue_time
;
727 * Traverse alarm list until queue time is greater
728 * than alarm time, then insert alarm.
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)
738 alrm1
->al_next
= alarm
;
739 alarm
->al_next
= alrm2
;
740 alarm
->al_prev
= alrm1
;
742 alrm2
->al_prev
= alarm
;
745 * If the inserted alarm is the 'earliest' alarm,
746 * reset the device layer alarm time accordingly.
748 if (alrmlist
== alarm
)
749 set_alarm(alarm_time
);
754 mach_timespec_t
*alarm_time
)
758 nanotime_to_absolutetime(alarm_time
->tv_sec
, alarm_time
->tv_nsec
, &abstime
);
759 timer_call_enter(&alarm_expire_timer
, abstime
);
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.
770 alarm_type_t alarm_type
,
771 mach_timespec_t
*alarm_time
,
772 mach_timespec_t
*clock_time
)
776 if (BAD_ALRMTYPE(alarm_type
))
778 if (BAD_MACH_TIMESPEC(alarm_time
))
780 if ((alarm_type
& ALRMTYPE
) == TIME_RELATIVE
)
781 ADD_MACH_TIMESPEC(alarm_time
, clock_time
);
783 result
= CMP_MACH_TIMESPEC(alarm_time
, clock_time
);
785 return ((result
>= 0)? result
: 0);
789 clock_get_system_value(void)
791 clock_t clock
= &clock_list
[SYSTEM_CLOCK
];
792 mach_timespec_t value
;
794 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
800 clock_get_calendar_value(void)
802 clock_t clock
= &clock_list
[CALENDAR_CLOCK
];
803 mach_timespec_t value
= MACH_TIMESPEC_ZERO
;
805 (void) (*clock
->cl_ops
->c_gettime
)(&value
);