2 * Copyright (c) 2000-2008 Apple 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/host.h>
44 #include <kern/sched_prim.h>
45 #include <kern/thread.h>
46 #include <kern/ipc_host.h>
47 #include <kern/clock.h>
48 #include <kern/zalloc.h>
50 #include <ipc/ipc_types.h>
51 #include <ipc/ipc_port.h>
53 #include <mach/mach_traps.h>
54 #include <mach/mach_time.h>
56 #include <mach/clock_server.h>
57 #include <mach/clock_reply.h>
58 #include <mach/clock_priv_server.h>
60 #include <mach/mach_host_server.h>
61 #include <mach/host_priv_server.h>
62 #include <libkern/section_keywords.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_init(void);
133 kern_return_t
rtclock_gettime(
134 mach_timespec_t
*cur_time
);
136 kern_return_t
rtclock_getattr(
137 clock_flavor_t flavor
,
139 mach_msg_type_number_t
*count
);
141 SECURITY_READ_ONLY_EARLY(struct clock_ops
) sysclk_ops
= {
147 kern_return_t
calend_gettime(
148 mach_timespec_t
*cur_time
);
150 kern_return_t
calend_getattr(
151 clock_flavor_t flavor
,
153 mach_msg_type_number_t
*count
);
155 SECURITY_READ_ONLY_EARLY(struct clock_ops
) calend_ops
= {
162 * List of clock devices.
164 SECURITY_READ_ONLY_LATE(struct clock
) clock_list
[] = {
166 { &sysclk_ops
, 0, 0 },
169 { &calend_ops
, 0, 0 }
171 int clock_count
= sizeof(clock_list
) / sizeof(clock_list
[0]);
174 * Macros to lock/unlock clock system.
176 #define LOCK_ALARM(s) \
178 simple_lock(&alarm_lock, LCK_GRP_NULL);
180 #define UNLOCK_ALARM(s) \
181 simple_unlock(&alarm_lock); \
185 clock_oldconfig(void)
190 simple_lock_init(&alarm_lock
, 0);
191 thread_call_setup(&alarm_done_call
, (thread_call_func_t
)alarm_done
, NULL
);
192 timer_call_setup(&alarm_expire_timer
, (timer_call_func_t
)alarm_expire
, NULL
);
195 * Configure clock devices.
197 for (i
= 0; i
< clock_count
; i
++) {
198 clock
= &clock_list
[i
];
199 if (clock
->cl_ops
&& clock
->cl_ops
->c_config
) {
200 if ((*clock
->cl_ops
->c_config
)() == 0) {
201 clock
->cl_ops
= NULL
;
206 /* start alarm sequence numbers at 0 */
217 * Initialize basic clock structures.
219 for (i
= 0; i
< clock_count
; i
++) {
220 clock
= &clock_list
[i
];
221 if (clock
->cl_ops
&& clock
->cl_ops
->c_init
) {
222 (*clock
->cl_ops
->c_init
)();
228 * Initialize the clock ipc service facility.
231 clock_service_create(void)
237 * Initialize ipc clock services.
239 for (i
= 0; i
< clock_count
; i
++) {
240 clock
= &clock_list
[i
];
242 ipc_clock_init(clock
);
243 ipc_clock_enable(clock
);
248 * Perform miscellaneous late
251 i
= sizeof(struct alarm
);
252 alarm_zone
= zinit(i
, (4096 / i
) * i
, 10 * i
, "alarms");
256 * Get the service port on a clock.
259 host_get_clock_service(
262 clock_t *clock
) /* OUT */
264 if (host
== HOST_NULL
|| clock_id
< 0 || clock_id
>= clock_count
) {
266 return KERN_INVALID_ARGUMENT
;
269 *clock
= &clock_list
[clock_id
];
270 if ((*clock
)->cl_ops
== 0) {
277 * Get the control port on a clock.
280 host_get_clock_control(
281 host_priv_t host_priv
,
283 clock_t *clock
) /* OUT */
285 if (host_priv
== HOST_PRIV_NULL
||
286 clock_id
< 0 || clock_id
>= clock_count
) {
288 return KERN_INVALID_ARGUMENT
;
291 *clock
= &clock_list
[clock_id
];
292 if ((*clock
)->cl_ops
== 0) {
299 * Get the current clock time.
304 mach_timespec_t
*cur_time
) /* OUT */
306 if (clock
== CLOCK_NULL
) {
307 return KERN_INVALID_ARGUMENT
;
309 return (*clock
->cl_ops
->c_gettime
)(cur_time
);
314 mach_timespec_t
*time
) /* OUT */
319 clock_get_system_nanotime(&secs
, &nsecs
);
320 time
->tv_sec
= (unsigned int)secs
;
321 time
->tv_nsec
= nsecs
;
328 mach_timespec_t
*time
) /* OUT */
333 clock_get_calendar_nanotime(&secs
, &nsecs
);
334 time
->tv_sec
= (unsigned int)secs
;
335 time
->tv_nsec
= nsecs
;
341 * Get clock attributes.
344 clock_get_attributes(
346 clock_flavor_t flavor
,
347 clock_attr_t attr
, /* OUT */
348 mach_msg_type_number_t
*count
) /* IN/OUT */
350 if (clock
== CLOCK_NULL
) {
351 return KERN_INVALID_ARGUMENT
;
353 if (clock
->cl_ops
->c_getattr
) {
354 return clock
->cl_ops
->c_getattr(flavor
, attr
, count
);
361 clock_flavor_t flavor
,
362 clock_attr_t attr
, /* OUT */
363 mach_msg_type_number_t
*count
) /* IN/OUT */
370 case CLOCK_GET_TIME_RES
: /* >0 res */
371 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
372 case CLOCK_ALARM_MINRES
:
373 case CLOCK_ALARM_MAXRES
:
374 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
378 return KERN_INVALID_VALUE
;
386 clock_flavor_t flavor
,
387 clock_attr_t attr
, /* OUT */
388 mach_msg_type_number_t
*count
) /* IN/OUT */
395 case CLOCK_GET_TIME_RES
: /* >0 res */
396 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
399 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
400 case CLOCK_ALARM_MINRES
:
401 case CLOCK_ALARM_MAXRES
:
402 *(clock_res_t
*) attr
= 0;
406 return KERN_INVALID_VALUE
;
413 * Set the current clock time.
418 __unused mach_timespec_t new_time
)
420 if (clock
== CLOCK_NULL
) {
421 return KERN_INVALID_ARGUMENT
;
427 * Set the clock alarm resolution.
430 clock_set_attributes(
432 __unused clock_flavor_t flavor
,
433 __unused clock_attr_t attr
,
434 __unused mach_msg_type_number_t count
)
436 if (clock
== CLOCK_NULL
) {
437 return KERN_INVALID_ARGUMENT
;
443 * Setup a clock alarm.
448 alarm_type_t alarm_type
,
449 mach_timespec_t alarm_time
,
450 ipc_port_t alarm_port
,
451 mach_msg_type_name_t alarm_port_type
)
454 mach_timespec_t clock_time
;
456 kern_return_t reply_code
;
459 if (clock
== CLOCK_NULL
) {
460 return KERN_INVALID_ARGUMENT
;
462 if (clock
!= &clock_list
[SYSTEM_CLOCK
]) {
465 if (IP_VALID(alarm_port
) == 0) {
466 return KERN_INVALID_CAPABILITY
;
470 * Check alarm parameters. If parameters are invalid,
471 * send alarm message immediately.
473 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
474 chkstat
= check_time(alarm_type
, &alarm_time
, &clock_time
);
476 reply_code
= (chkstat
< 0 ? KERN_INVALID_VALUE
: KERN_SUCCESS
);
477 clock_alarm_reply(alarm_port
, alarm_port_type
,
478 reply_code
, alarm_type
, clock_time
);
483 * Get alarm and add to clock alarm list.
487 if ((alarm
= alrmfree
) == 0) {
489 alarm
= (alarm_t
) zalloc(alarm_zone
);
491 return KERN_RESOURCE_SHORTAGE
;
495 alrmfree
= alarm
->al_next
;
498 alarm
->al_status
= ALARM_CLOCK
;
499 alarm
->al_time
= alarm_time
;
500 alarm
->al_type
= alarm_type
;
501 alarm
->al_port
= alarm_port
;
502 alarm
->al_port_type
= alarm_port_type
;
503 alarm
->al_clock
= clock
;
504 alarm
->al_seqno
= alrm_seqno
++;
512 * Sleep on a clock. System trap. User-level libmach clock_sleep
513 * interface call takes a mach_timespec_t sleep_time argument which it
514 * converts to sleep_sec and sleep_nsec arguments which are then
515 * passed to clock_sleep_trap.
519 struct clock_sleep_trap_args
*args
)
521 mach_port_name_t clock_name
= args
->clock_name
;
522 sleep_type_t sleep_type
= args
->sleep_type
;
523 int sleep_sec
= args
->sleep_sec
;
524 int sleep_nsec
= args
->sleep_nsec
;
525 mach_vm_address_t wakeup_time_addr
= args
->wakeup_time
;
527 mach_timespec_t swtime
= {};
528 kern_return_t rvalue
;
531 * Convert the trap parameters.
533 if (clock_name
== MACH_PORT_NULL
) {
534 clock
= &clock_list
[SYSTEM_CLOCK
];
536 clock
= port_name_to_clock(clock_name
);
539 swtime
.tv_sec
= sleep_sec
;
540 swtime
.tv_nsec
= sleep_nsec
;
543 * Call the actual clock_sleep routine.
545 rvalue
= clock_sleep_internal(clock
, sleep_type
, &swtime
);
548 * Return current time as wakeup time.
550 if (rvalue
!= KERN_INVALID_ARGUMENT
&& rvalue
!= KERN_FAILURE
) {
551 copyout((char *)&swtime
, wakeup_time_addr
, sizeof(mach_timespec_t
));
557 clock_sleep_internal(
559 sleep_type_t sleep_type
,
560 mach_timespec_t
*sleep_time
)
563 mach_timespec_t clock_time
;
564 kern_return_t rvalue
;
568 if (clock
== CLOCK_NULL
) {
569 return KERN_INVALID_ARGUMENT
;
572 if (clock
!= &clock_list
[SYSTEM_CLOCK
]) {
577 * Check sleep parameters. If parameters are invalid
578 * return an error, otherwise post alarm request.
580 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
582 chkstat
= check_time(sleep_type
, sleep_time
, &clock_time
);
584 return KERN_INVALID_VALUE
;
586 rvalue
= KERN_SUCCESS
;
588 wait_result_t wait_result
;
591 * Get alarm and add to clock alarm list.
595 if ((alarm
= alrmfree
) == 0) {
597 alarm
= (alarm_t
) zalloc(alarm_zone
);
599 return KERN_RESOURCE_SHORTAGE
;
603 alrmfree
= alarm
->al_next
;
607 * Wait for alarm to occur.
609 wait_result
= assert_wait((event_t
)alarm
, THREAD_ABORTSAFE
);
610 if (wait_result
== THREAD_WAITING
) {
611 alarm
->al_time
= *sleep_time
;
612 alarm
->al_status
= ALARM_SLEEP
;
616 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
619 * Note if alarm expired normally or whether it
620 * was aborted. If aborted, delete alarm from
621 * clock alarm list. Return alarm to free list.
624 if (alarm
->al_status
!= ALARM_DONE
) {
625 assert(wait_result
!= THREAD_AWAKENED
);
626 if (((alarm
->al_prev
)->al_next
= alarm
->al_next
) != NULL
) {
627 (alarm
->al_next
)->al_prev
= alarm
->al_prev
;
629 rvalue
= KERN_ABORTED
;
631 *sleep_time
= alarm
->al_time
;
632 alarm
->al_status
= ALARM_FREE
;
634 assert(wait_result
== THREAD_INTERRUPTED
);
635 assert(alarm
->al_status
== ALARM_FREE
);
636 rvalue
= KERN_ABORTED
;
638 alarm
->al_next
= alrmfree
;
642 *sleep_time
= clock_time
;
649 * Service clock alarm expirations.
657 mach_timespec_t clock_time
;
658 mach_timespec_t
*alarm_time
;
661 clock
= &clock_list
[SYSTEM_CLOCK
];
662 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
665 * Update clock alarm list. Alarms that are due are moved
666 * to the alarmdone list to be serviced by a thread callout.
669 alrm1
= (alarm_t
)&alrmlist
;
670 while ((alrm2
= alrm1
->al_next
) != NULL
) {
671 alarm_time
= &alrm2
->al_time
;
672 if (CMP_MACH_TIMESPEC(alarm_time
, &clock_time
) > 0) {
677 * Alarm has expired, so remove it from the
680 if ((alrm1
->al_next
= alrm2
->al_next
) != NULL
) {
681 (alrm1
->al_next
)->al_prev
= alrm1
;
685 * If a clock_sleep() alarm, wakeup the thread
686 * which issued the clock_sleep() call.
688 if (alrm2
->al_status
== ALARM_SLEEP
) {
689 alrm2
->al_next
= NULL
;
690 alrm2
->al_status
= ALARM_DONE
;
691 alrm2
->al_time
= clock_time
;
692 thread_wakeup((event_t
)alrm2
);
695 * If a clock_alarm() alarm, place the alarm on
696 * the alarm done list and schedule the alarm
697 * delivery mechanism.
700 assert(alrm2
->al_status
== ALARM_CLOCK
);
701 if ((alrm2
->al_next
= alrmdone
) != NULL
) {
702 alrmdone
->al_prev
= alrm2
;
704 thread_call_enter(&alarm_done_call
);
706 alrm2
->al_prev
= (alarm_t
)&alrmdone
;
708 alrm2
->al_status
= ALARM_DONE
;
709 alrm2
->al_time
= clock_time
;
714 * Setup to expire for the next pending alarm.
717 set_alarm(alarm_time
);
730 while ((alrm
= alrmdone
) != NULL
) {
731 if ((alrmdone
= alrm
->al_next
) != NULL
) {
732 alrmdone
->al_prev
= (alarm_t
)&alrmdone
;
736 code
= (alrm
->al_status
== ALARM_DONE
? KERN_SUCCESS
: KERN_ABORTED
);
737 if (alrm
->al_port
!= IP_NULL
) {
738 /* Deliver message to designated port */
739 if (IP_VALID(alrm
->al_port
)) {
740 clock_alarm_reply(alrm
->al_port
, alrm
->al_port_type
, code
,
741 alrm
->al_type
, alrm
->al_time
);
745 alrm
->al_status
= ALARM_FREE
;
746 alrm
->al_next
= alrmfree
;
749 panic("clock_alarm_deliver");
757 * Post an alarm on the active alarm list.
759 * Always called from within a LOCK_ALARM() code section.
765 alarm_t alrm1
, alrm2
;
766 mach_timespec_t
*alarm_time
;
767 mach_timespec_t
*queue_time
;
770 * Traverse alarm list until queue time is greater
771 * than alarm time, then insert alarm.
773 alarm_time
= &alarm
->al_time
;
774 alrm1
= (alarm_t
)&alrmlist
;
775 while ((alrm2
= alrm1
->al_next
) != NULL
) {
776 queue_time
= &alrm2
->al_time
;
777 if (CMP_MACH_TIMESPEC(queue_time
, alarm_time
) > 0) {
782 alrm1
->al_next
= alarm
;
783 alarm
->al_next
= alrm2
;
784 alarm
->al_prev
= alrm1
;
786 alrm2
->al_prev
= alarm
;
790 * If the inserted alarm is the 'earliest' alarm,
791 * reset the device layer alarm time accordingly.
793 if (alrmlist
== alarm
) {
794 set_alarm(alarm_time
);
800 mach_timespec_t
*alarm_time
)
804 nanotime_to_absolutetime(alarm_time
->tv_sec
, alarm_time
->tv_nsec
, &abstime
);
805 timer_call_enter_with_leeway(&alarm_expire_timer
, NULL
, abstime
, 0, TIMER_CALL_USER_NORMAL
, FALSE
);
809 * Check the validity of 'alarm_time' and 'alarm_type'. If either
810 * argument is invalid, return a negative value. If the 'alarm_time'
811 * is now, return a 0 value. If the 'alarm_time' is in the future,
812 * return a positive value.
816 alarm_type_t alarm_type
,
817 mach_timespec_t
*alarm_time
,
818 mach_timespec_t
*clock_time
)
822 if (BAD_ALRMTYPE(alarm_type
)) {
825 if (BAD_MACH_TIMESPEC(alarm_time
)) {
828 if ((alarm_type
& ALRMTYPE
) == TIME_RELATIVE
) {
829 ADD_MACH_TIMESPEC(alarm_time
, clock_time
);
832 result
= CMP_MACH_TIMESPEC(alarm_time
, clock_time
);
834 return (result
>= 0)? result
: 0;
840 clock_get_system_value(void)
842 clock_t clock
= &clock_list
[SYSTEM_CLOCK
];
843 mach_timespec_t value
;
845 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
851 clock_get_calendar_value(void)
853 clock_t clock
= &clock_list
[CALENDAR_CLOCK
];
854 mach_timespec_t value
= MACH_TIMESPEC_ZERO
;
856 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
861 #endif /* __LP64__ */