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/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)
191 clock
->cl_ops
= NULL
;
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 */
304 clock_get_system_nanotime(&secs
, &nsecs
);
305 time
->tv_sec
= (unsigned int)secs
;
306 time
->tv_nsec
= nsecs
;
308 return (KERN_SUCCESS
);
313 mach_timespec_t
*time
) /* OUT */
318 clock_get_calendar_nanotime(&secs
, &nsecs
);
319 time
->tv_sec
= (unsigned int)secs
;
320 time
->tv_nsec
= nsecs
;
322 return (KERN_SUCCESS
);
326 * Get clock attributes.
329 clock_get_attributes(
331 clock_flavor_t flavor
,
332 clock_attr_t attr
, /* OUT */
333 mach_msg_type_number_t
*count
) /* IN/OUT */
335 if (clock
== CLOCK_NULL
)
336 return (KERN_INVALID_ARGUMENT
);
337 if (clock
->cl_ops
->c_getattr
)
338 return (clock
->cl_ops
->c_getattr(flavor
, attr
, count
));
339 return (KERN_FAILURE
);
344 clock_flavor_t flavor
,
345 clock_attr_t attr
, /* OUT */
346 mach_msg_type_number_t
*count
) /* IN/OUT */
349 return (KERN_FAILURE
);
353 case CLOCK_GET_TIME_RES
: /* >0 res */
354 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
355 case CLOCK_ALARM_MINRES
:
356 case CLOCK_ALARM_MAXRES
:
357 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
361 return (KERN_INVALID_VALUE
);
364 return (KERN_SUCCESS
);
369 clock_flavor_t flavor
,
370 clock_attr_t attr
, /* OUT */
371 mach_msg_type_number_t
*count
) /* IN/OUT */
374 return (KERN_FAILURE
);
378 case CLOCK_GET_TIME_RES
: /* >0 res */
379 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
382 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
383 case CLOCK_ALARM_MINRES
:
384 case CLOCK_ALARM_MAXRES
:
385 *(clock_res_t
*) attr
= 0;
389 return (KERN_INVALID_VALUE
);
392 return (KERN_SUCCESS
);
396 * Set the current clock time.
401 __unused mach_timespec_t new_time
)
403 if (clock
== CLOCK_NULL
)
404 return (KERN_INVALID_ARGUMENT
);
405 return (KERN_FAILURE
);
409 * Set the clock alarm resolution.
412 clock_set_attributes(
414 __unused clock_flavor_t flavor
,
415 __unused clock_attr_t attr
,
416 __unused mach_msg_type_number_t count
)
418 if (clock
== CLOCK_NULL
)
419 return (KERN_INVALID_ARGUMENT
);
420 return (KERN_FAILURE
);
424 * Setup a clock alarm.
429 alarm_type_t alarm_type
,
430 mach_timespec_t alarm_time
,
431 ipc_port_t alarm_port
,
432 mach_msg_type_name_t alarm_port_type
)
435 mach_timespec_t clock_time
;
437 kern_return_t reply_code
;
440 if (clock
== CLOCK_NULL
)
441 return (KERN_INVALID_ARGUMENT
);
442 if (clock
!= &clock_list
[SYSTEM_CLOCK
])
443 return (KERN_FAILURE
);
444 if (IP_VALID(alarm_port
) == 0)
445 return (KERN_INVALID_CAPABILITY
);
448 * Check alarm parameters. If parameters are invalid,
449 * send alarm message immediately.
451 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
452 chkstat
= check_time(alarm_type
, &alarm_time
, &clock_time
);
454 reply_code
= (chkstat
< 0 ? KERN_INVALID_VALUE
: KERN_SUCCESS
);
455 clock_alarm_reply(alarm_port
, alarm_port_type
,
456 reply_code
, alarm_type
, clock_time
);
457 return (KERN_SUCCESS
);
461 * Get alarm and add to clock alarm list.
465 if ((alarm
= alrmfree
) == 0) {
467 alarm
= (alarm_t
) zalloc(alarm_zone
);
469 return (KERN_RESOURCE_SHORTAGE
);
473 alrmfree
= alarm
->al_next
;
475 alarm
->al_status
= ALARM_CLOCK
;
476 alarm
->al_time
= alarm_time
;
477 alarm
->al_type
= alarm_type
;
478 alarm
->al_port
= alarm_port
;
479 alarm
->al_port_type
= alarm_port_type
;
480 alarm
->al_clock
= clock
;
481 alarm
->al_seqno
= alrm_seqno
++;
485 return (KERN_SUCCESS
);
489 * Sleep on a clock. System trap. User-level libmach clock_sleep
490 * interface call takes a mach_timespec_t sleep_time argument which it
491 * converts to sleep_sec and sleep_nsec arguments which are then
492 * passed to clock_sleep_trap.
496 struct clock_sleep_trap_args
*args
)
498 mach_port_name_t clock_name
= args
->clock_name
;
499 sleep_type_t sleep_type
= args
->sleep_type
;
500 int sleep_sec
= args
->sleep_sec
;
501 int sleep_nsec
= args
->sleep_nsec
;
502 mach_vm_address_t wakeup_time_addr
= args
->wakeup_time
;
504 mach_timespec_t swtime
;
505 kern_return_t rvalue
;
508 * Convert the trap parameters.
510 if (clock_name
== MACH_PORT_NULL
)
511 clock
= &clock_list
[SYSTEM_CLOCK
];
513 clock
= port_name_to_clock(clock_name
);
515 swtime
.tv_sec
= sleep_sec
;
516 swtime
.tv_nsec
= sleep_nsec
;
519 * Call the actual clock_sleep routine.
521 rvalue
= clock_sleep_internal(clock
, sleep_type
, &swtime
);
524 * Return current time as wakeup time.
526 if (rvalue
!= KERN_INVALID_ARGUMENT
&& rvalue
!= KERN_FAILURE
) {
527 copyout((char *)&swtime
, wakeup_time_addr
, sizeof(mach_timespec_t
));
533 clock_sleep_internal(
535 sleep_type_t sleep_type
,
536 mach_timespec_t
*sleep_time
)
539 mach_timespec_t clock_time
;
540 kern_return_t rvalue
;
544 if (clock
== CLOCK_NULL
)
545 return (KERN_INVALID_ARGUMENT
);
547 if (clock
!= &clock_list
[SYSTEM_CLOCK
])
548 return (KERN_FAILURE
);
551 * Check sleep parameters. If parameters are invalid
552 * return an error, otherwise post alarm request.
554 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
556 chkstat
= check_time(sleep_type
, sleep_time
, &clock_time
);
558 return (KERN_INVALID_VALUE
);
559 rvalue
= KERN_SUCCESS
;
561 wait_result_t wait_result
;
564 * Get alarm and add to clock alarm list.
568 if ((alarm
= alrmfree
) == 0) {
570 alarm
= (alarm_t
) zalloc(alarm_zone
);
572 return (KERN_RESOURCE_SHORTAGE
);
576 alrmfree
= alarm
->al_next
;
579 * Wait for alarm to occur.
581 wait_result
= assert_wait((event_t
)alarm
, THREAD_ABORTSAFE
);
582 if (wait_result
== THREAD_WAITING
) {
583 alarm
->al_time
= *sleep_time
;
584 alarm
->al_status
= ALARM_SLEEP
;
588 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
591 * Note if alarm expired normally or whether it
592 * was aborted. If aborted, delete alarm from
593 * clock alarm list. Return alarm to free list.
596 if (alarm
->al_status
!= ALARM_DONE
) {
597 assert(wait_result
!= THREAD_AWAKENED
);
598 if (((alarm
->al_prev
)->al_next
= alarm
->al_next
) != NULL
)
599 (alarm
->al_next
)->al_prev
= alarm
->al_prev
;
600 rvalue
= KERN_ABORTED
;
602 *sleep_time
= alarm
->al_time
;
603 alarm
->al_status
= ALARM_FREE
;
605 assert(wait_result
== THREAD_INTERRUPTED
);
606 assert(alarm
->al_status
== ALARM_FREE
);
607 rvalue
= KERN_ABORTED
;
609 alarm
->al_next
= alrmfree
;
614 *sleep_time
= clock_time
;
620 * Service clock alarm expirations.
626 register alarm_t alrm1
;
627 register alarm_t alrm2
;
628 mach_timespec_t clock_time
;
629 mach_timespec_t
*alarm_time
;
632 clock
= &clock_list
[SYSTEM_CLOCK
];
633 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
636 * Update clock alarm list. Alarms that are due are moved
637 * to the alarmdone list to be serviced by a thread callout.
640 alrm1
= (alarm_t
)&alrmlist
;
641 while ((alrm2
= alrm1
->al_next
) != NULL
) {
642 alarm_time
= &alrm2
->al_time
;
643 if (CMP_MACH_TIMESPEC(alarm_time
, &clock_time
) > 0)
647 * Alarm has expired, so remove it from the
650 if ((alrm1
->al_next
= alrm2
->al_next
) != NULL
)
651 (alrm1
->al_next
)->al_prev
= alrm1
;
654 * If a clock_sleep() alarm, wakeup the thread
655 * which issued the clock_sleep() call.
657 if (alrm2
->al_status
== ALARM_SLEEP
) {
658 alrm2
->al_next
= NULL
;
659 alrm2
->al_status
= ALARM_DONE
;
660 alrm2
->al_time
= clock_time
;
661 thread_wakeup((event_t
)alrm2
);
665 * If a clock_alarm() alarm, place the alarm on
666 * the alarm done list and schedule the alarm
667 * delivery mechanism.
670 assert(alrm2
->al_status
== ALARM_CLOCK
);
671 if ((alrm2
->al_next
= alrmdone
) != NULL
)
672 alrmdone
->al_prev
= alrm2
;
674 thread_call_enter(&alarm_done_call
);
675 alrm2
->al_prev
= (alarm_t
)&alrmdone
;
677 alrm2
->al_status
= ALARM_DONE
;
678 alrm2
->al_time
= clock_time
;
683 * Setup to expire for the next pending alarm.
686 set_alarm(alarm_time
);
693 register alarm_t alrm
;
698 while ((alrm
= alrmdone
) != NULL
) {
699 if ((alrmdone
= alrm
->al_next
) != NULL
)
700 alrmdone
->al_prev
= (alarm_t
)&alrmdone
;
703 code
= (alrm
->al_status
== ALARM_DONE
? KERN_SUCCESS
: KERN_ABORTED
);
704 if (alrm
->al_port
!= IP_NULL
) {
705 /* Deliver message to designated port */
706 if (IP_VALID(alrm
->al_port
)) {
707 clock_alarm_reply(alrm
->al_port
, alrm
->al_port_type
, code
,
708 alrm
->al_type
, alrm
->al_time
);
712 alrm
->al_status
= ALARM_FREE
;
713 alrm
->al_next
= alrmfree
;
717 panic("clock_alarm_deliver");
724 * Post an alarm on the active alarm list.
726 * Always called from within a LOCK_ALARM() code section.
732 register alarm_t alrm1
, alrm2
;
733 mach_timespec_t
*alarm_time
;
734 mach_timespec_t
*queue_time
;
737 * Traverse alarm list until queue time is greater
738 * than alarm time, then insert alarm.
740 alarm_time
= &alarm
->al_time
;
741 alrm1
= (alarm_t
)&alrmlist
;
742 while ((alrm2
= alrm1
->al_next
) != NULL
) {
743 queue_time
= &alrm2
->al_time
;
744 if (CMP_MACH_TIMESPEC(queue_time
, alarm_time
) > 0)
748 alrm1
->al_next
= alarm
;
749 alarm
->al_next
= alrm2
;
750 alarm
->al_prev
= alrm1
;
752 alrm2
->al_prev
= alarm
;
755 * If the inserted alarm is the 'earliest' alarm,
756 * reset the device layer alarm time accordingly.
758 if (alrmlist
== alarm
)
759 set_alarm(alarm_time
);
764 mach_timespec_t
*alarm_time
)
768 nanotime_to_absolutetime(alarm_time
->tv_sec
, alarm_time
->tv_nsec
, &abstime
);
769 timer_call_enter(&alarm_expire_timer
, abstime
, 0);
773 * Check the validity of 'alarm_time' and 'alarm_type'. If either
774 * argument is invalid, return a negative value. If the 'alarm_time'
775 * is now, return a 0 value. If the 'alarm_time' is in the future,
776 * return a positive value.
780 alarm_type_t alarm_type
,
781 mach_timespec_t
*alarm_time
,
782 mach_timespec_t
*clock_time
)
786 if (BAD_ALRMTYPE(alarm_type
))
788 if (BAD_MACH_TIMESPEC(alarm_time
))
790 if ((alarm_type
& ALRMTYPE
) == TIME_RELATIVE
)
791 ADD_MACH_TIMESPEC(alarm_time
, clock_time
);
793 result
= CMP_MACH_TIMESPEC(alarm_time
, clock_time
);
795 return ((result
>= 0)? result
: 0);
801 clock_get_system_value(void)
803 clock_t clock
= &clock_list
[SYSTEM_CLOCK
];
804 mach_timespec_t value
;
806 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
812 clock_get_calendar_value(void)
814 clock_t clock
= &clock_list
[CALENDAR_CLOCK
];
815 mach_timespec_t value
= MACH_TIMESPEC_ZERO
;
817 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
822 #endif /* __LP64__ */