2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
30 * Purpose: Routines for the creation and use of kernel
31 * alarm clock services. This file and the ipc
32 * routines in kern/ipc_clock.c constitute the
33 * machine-independent clock service layer.
37 #include <mach_host.h>
39 #include <mach/boolean.h>
40 #include <mach/processor_info.h>
41 #include <mach/vm_param.h>
42 #include <machine/mach_param.h>
43 #include <kern/cpu_number.h>
44 #include <kern/misc_protos.h>
45 #include <kern/lock.h>
46 #include <kern/host.h>
48 #include <kern/sched_prim.h>
49 #include <kern/thread.h>
50 #include <kern/thread_swap.h>
51 #include <kern/ipc_host.h>
52 #include <kern/clock.h>
53 #include <kern/zalloc.h>
54 #include <ipc/ipc_port.h>
56 #include <mach/mach_syscalls.h>
57 #include <mach/clock_reply.h>
58 #include <mach/mach_time.h>
64 #include <mach/clock_server.h>
65 #include <mach/mach_host_server.h>
67 /* local data declarations */
68 decl_simple_lock_data(static,ClockLock
) /* clock system synchronization */
69 static struct zone
*alarm_zone
; /* zone for user alarms */
70 static struct alarm
*alrmfree
; /* alarm free list pointer */
71 static struct alarm
*alrmdone
; /* alarm done list pointer */
72 static long alrm_seqno
; /* uniquely identifies alarms */
73 static thread_call_data_t alarm_deliver
;
75 decl_simple_lock_data(static,calend_adjlock
)
77 static timer_call_data_t calend_adjcall
;
78 static uint64_t calend_adjinterval
, calend_adjdeadline
;
80 static thread_call_data_t calend_wakecall
;
82 /* backwards compatibility */
83 int hz
= HZ
; /* GET RID OF THIS !!! */
84 int tick
= (1000000 / HZ
); /* GET RID OF THIS !!! */
86 /* external declarations */
87 extern struct clock clock_list
[];
88 extern int clock_count
;
90 /* local clock subroutines */
102 alarm_type_t alarm_type
,
103 mach_timespec_t
*alarm_time
,
104 mach_timespec_t
*clock_time
);
107 void clock_alarm_deliver(
108 thread_call_param_t p0
,
109 thread_call_param_t p1
);
112 void calend_adjust_call(
113 timer_call_param_t p0
,
114 timer_call_param_t p1
);
117 void calend_dowakeup(
118 thread_call_param_t p0
,
119 thread_call_param_t p1
);
122 * Macros to lock/unlock clock system.
124 #define LOCK_CLOCK(s) \
126 simple_lock(&ClockLock);
128 #define UNLOCK_CLOCK(s) \
129 simple_unlock(&ClockLock); \
133 * Configure the clock system. (Not sure if we need this,
134 * as separate from clock_init()).
142 if (cpu_number() != master_cpu
)
143 panic("clock_config");
145 simple_lock_init(&ClockLock
, ETAP_MISC_CLOCK
);
146 thread_call_setup(&alarm_deliver
, clock_alarm_deliver
, NULL
);
148 simple_lock_init(&calend_adjlock
, ETAP_MISC_CLOCK
);
149 timer_call_setup(&calend_adjcall
, calend_adjust_call
, NULL
);
151 thread_call_setup(&calend_wakecall
, calend_dowakeup
, NULL
);
154 * Configure clock devices.
156 for (i
= 0; i
< clock_count
; i
++) {
157 clock
= &clock_list
[i
];
159 if ((*clock
->cl_ops
->c_config
)() == 0)
164 /* start alarm sequence numbers at 0 */
169 * Initialize the clock system.
178 * Initialize basic clock structures.
180 for (i
= 0; i
< clock_count
; i
++) {
181 clock
= &clock_list
[i
];
183 (*clock
->cl_ops
->c_init
)();
188 * Called by machine dependent code
189 * to initialize areas dependent on the
190 * timebase value. May be called multiple
191 * times during start up.
194 clock_timebase_init(void)
196 sched_timebase_init();
200 * Initialize the clock ipc service facility.
203 clock_service_create(void)
209 * Initialize ipc clock services.
211 for (i
= 0; i
< clock_count
; i
++) {
212 clock
= &clock_list
[i
];
214 ipc_clock_init(clock
);
215 ipc_clock_enable(clock
);
220 * Perform miscellaneous late
223 i
= sizeof(struct alarm
);
224 alarm_zone
= zinit(i
, (4096/i
)*i
, 10*i
, "alarms");
228 * Get the service port on a clock.
231 host_get_clock_service(
234 clock_t *clock
) /* OUT */
236 if (host
== HOST_NULL
|| clock_id
< 0 || clock_id
>= clock_count
) {
238 return (KERN_INVALID_ARGUMENT
);
241 *clock
= &clock_list
[clock_id
];
242 if ((*clock
)->cl_ops
== 0)
243 return (KERN_FAILURE
);
244 return (KERN_SUCCESS
);
248 * Get the control port on a clock.
251 host_get_clock_control(
252 host_priv_t host_priv
,
254 clock_t *clock
) /* OUT */
256 if (host_priv
== HOST_PRIV_NULL
|| clock_id
< 0 || clock_id
>= clock_count
) {
258 return (KERN_INVALID_ARGUMENT
);
261 *clock
= &clock_list
[clock_id
];
262 if ((*clock
)->cl_ops
== 0)
263 return (KERN_FAILURE
);
264 return (KERN_SUCCESS
);
268 * Get the current clock time.
273 mach_timespec_t
*cur_time
) /* OUT */
275 if (clock
== CLOCK_NULL
)
276 return (KERN_INVALID_ARGUMENT
);
277 return ((*clock
->cl_ops
->c_gettime
)(cur_time
));
281 * Get clock attributes.
284 clock_get_attributes(
286 clock_flavor_t flavor
,
287 clock_attr_t attr
, /* OUT */
288 mach_msg_type_number_t
*count
) /* IN/OUT */
290 kern_return_t (*getattr
)(
291 clock_flavor_t flavor
,
293 mach_msg_type_number_t
*count
);
295 if (clock
== CLOCK_NULL
)
296 return (KERN_INVALID_ARGUMENT
);
297 if (getattr
= clock
->cl_ops
->c_getattr
)
298 return((*getattr
)(flavor
, attr
, count
));
300 return (KERN_FAILURE
);
304 * Set the current clock time.
309 mach_timespec_t new_time
)
311 mach_timespec_t
*clock_time
;
312 kern_return_t (*settime
)(
313 mach_timespec_t
*clock_time
);
315 if (clock
== CLOCK_NULL
)
316 return (KERN_INVALID_ARGUMENT
);
317 if ((settime
= clock
->cl_ops
->c_settime
) == 0)
318 return (KERN_FAILURE
);
319 clock_time
= &new_time
;
320 if (BAD_MACH_TIMESPEC(clock_time
))
321 return (KERN_INVALID_VALUE
);
324 * Flush all outstanding alarms.
331 return ((*settime
)(clock_time
));
335 * Set the clock alarm resolution.
338 clock_set_attributes(
340 clock_flavor_t flavor
,
342 mach_msg_type_number_t count
)
344 kern_return_t (*setattr
)(
345 clock_flavor_t flavor
,
347 mach_msg_type_number_t count
);
349 if (clock
== CLOCK_NULL
)
350 return (KERN_INVALID_ARGUMENT
);
351 if (setattr
= clock
->cl_ops
->c_setattr
)
352 return ((*setattr
)(flavor
, attr
, count
));
354 return (KERN_FAILURE
);
358 * Setup a clock alarm.
363 alarm_type_t alarm_type
,
364 mach_timespec_t alarm_time
,
365 ipc_port_t alarm_port
,
366 mach_msg_type_name_t alarm_port_type
)
369 mach_timespec_t clock_time
;
371 kern_return_t reply_code
;
374 if (clock
== CLOCK_NULL
)
375 return (KERN_INVALID_ARGUMENT
);
376 if (clock
->cl_ops
->c_setalrm
== 0)
377 return (KERN_FAILURE
);
378 if (IP_VALID(alarm_port
) == 0)
379 return (KERN_INVALID_CAPABILITY
);
382 * Check alarm parameters. If parameters are invalid,
383 * send alarm message immediately.
385 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
386 chkstat
= check_time(alarm_type
, &alarm_time
, &clock_time
);
388 reply_code
= (chkstat
< 0 ? KERN_INVALID_VALUE
: KERN_SUCCESS
);
389 clock_alarm_reply(alarm_port
, alarm_port_type
,
390 reply_code
, alarm_type
, clock_time
);
391 return (KERN_SUCCESS
);
395 * Get alarm and add to clock alarm list.
399 if ((alarm
= alrmfree
) == 0) {
401 alarm
= (alarm_t
) zalloc(alarm_zone
);
403 return (KERN_RESOURCE_SHORTAGE
);
407 alrmfree
= alarm
->al_next
;
409 alarm
->al_status
= ALARM_CLOCK
;
410 alarm
->al_time
= alarm_time
;
411 alarm
->al_type
= alarm_type
;
412 alarm
->al_port
= alarm_port
;
413 alarm
->al_port_type
= alarm_port_type
;
414 alarm
->al_clock
= clock
;
415 alarm
->al_seqno
= alrm_seqno
++;
416 post_alarm(clock
, alarm
);
419 return (KERN_SUCCESS
);
423 * Sleep on a clock. System trap. User-level libmach clock_sleep
424 * interface call takes a mach_timespec_t sleep_time argument which it
425 * converts to sleep_sec and sleep_nsec arguments which are then
426 * passed to clock_sleep_trap.
430 mach_port_name_t clock_name
,
431 sleep_type_t sleep_type
,
434 mach_timespec_t
*wakeup_time
)
437 mach_timespec_t swtime
;
438 kern_return_t rvalue
;
441 * Convert the trap parameters.
443 if (clock_name
!= MACH_PORT_NULL
)
444 clock
= port_name_to_clock(clock_name
);
446 clock
= &clock_list
[SYSTEM_CLOCK
];
448 swtime
.tv_sec
= sleep_sec
;
449 swtime
.tv_nsec
= sleep_nsec
;
452 * Call the actual clock_sleep routine.
454 rvalue
= clock_sleep_internal(clock
, sleep_type
, &swtime
);
457 * Return current time as wakeup time.
459 if (rvalue
!= KERN_INVALID_ARGUMENT
&& rvalue
!= KERN_FAILURE
) {
460 copyout((char *)&swtime
, (char *)wakeup_time
,
461 sizeof(mach_timespec_t
));
467 * Kernel internally callable clock sleep routine. The calling
468 * thread is suspended until the requested sleep time is reached.
471 clock_sleep_internal(
473 sleep_type_t sleep_type
,
474 mach_timespec_t
*sleep_time
)
477 mach_timespec_t clock_time
;
478 kern_return_t rvalue
;
482 if (clock
== CLOCK_NULL
)
483 return (KERN_INVALID_ARGUMENT
);
484 if (clock
->cl_ops
->c_setalrm
== 0)
485 return (KERN_FAILURE
);
488 * Check sleep parameters. If parameters are invalid
489 * return an error, otherwise post alarm request.
491 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
493 chkstat
= check_time(sleep_type
, sleep_time
, &clock_time
);
495 return (KERN_INVALID_VALUE
);
496 rvalue
= KERN_SUCCESS
;
498 wait_result_t wait_result
;
501 * Get alarm and add to clock alarm list.
505 if ((alarm
= alrmfree
) == 0) {
507 alarm
= (alarm_t
) zalloc(alarm_zone
);
509 return (KERN_RESOURCE_SHORTAGE
);
513 alrmfree
= alarm
->al_next
;
516 * Wait for alarm to occur.
518 wait_result
= assert_wait((event_t
)alarm
, THREAD_ABORTSAFE
);
519 if (wait_result
== THREAD_WAITING
) {
520 alarm
->al_time
= *sleep_time
;
521 alarm
->al_status
= ALARM_SLEEP
;
522 post_alarm(clock
, alarm
);
525 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
528 * Note if alarm expired normally or whether it
529 * was aborted. If aborted, delete alarm from
530 * clock alarm list. Return alarm to free list.
533 if (alarm
->al_status
!= ALARM_DONE
) {
534 assert(wait_result
!= THREAD_AWAKENED
);
535 if ((alarm
->al_prev
)->al_next
= alarm
->al_next
)
536 (alarm
->al_next
)->al_prev
= alarm
->al_prev
;
537 rvalue
= KERN_ABORTED
;
539 *sleep_time
= alarm
->al_time
;
540 alarm
->al_status
= ALARM_FREE
;
542 assert(wait_result
== THREAD_INTERRUPTED
);
543 assert(alarm
->al_status
== ALARM_FREE
);
544 rvalue
= KERN_ABORTED
;
546 alarm
->al_next
= alrmfree
;
551 *sleep_time
= clock_time
;
557 * CLOCK INTERRUPT SERVICE ROUTINES.
561 * Service clock alarm interrupts. Called from machine dependent
562 * layer at splclock(). The clock_id argument specifies the clock,
563 * and the clock_time argument gives that clock's current time.
568 mach_timespec_t
*clock_time
)
571 register alarm_t alrm1
;
572 register alarm_t alrm2
;
573 mach_timespec_t
*alarm_time
;
576 clock
= &clock_list
[clock_id
];
579 * Update clock alarm list. All alarms that are due are moved
580 * to the alarmdone list to be serviced by the alarm_thread.
584 alrm1
= (alarm_t
) &clock
->cl_alarm
;
585 while (alrm2
= alrm1
->al_next
) {
586 alarm_time
= &alrm2
->al_time
;
587 if (CMP_MACH_TIMESPEC(alarm_time
, clock_time
) > 0)
591 * Alarm has expired, so remove it from the
594 if (alrm1
->al_next
= alrm2
->al_next
)
595 (alrm1
->al_next
)->al_prev
= alrm1
;
598 * If a clock_sleep() alarm, wakeup the thread
599 * which issued the clock_sleep() call.
601 if (alrm2
->al_status
== ALARM_SLEEP
) {
603 alrm2
->al_status
= ALARM_DONE
;
604 alrm2
->al_time
= *clock_time
;
605 thread_wakeup((event_t
)alrm2
);
609 * If a clock_alarm() alarm, place the alarm on
610 * the alarm done list and schedule the alarm
611 * delivery mechanism.
614 assert(alrm2
->al_status
== ALARM_CLOCK
);
615 if (alrm2
->al_next
= alrmdone
)
616 alrmdone
->al_prev
= alrm2
;
618 thread_call_enter(&alarm_deliver
);
619 alrm2
->al_prev
= (alarm_t
) &alrmdone
;
621 alrm2
->al_status
= ALARM_DONE
;
622 alrm2
->al_time
= *clock_time
;
627 * Setup the clock dependent layer to deliver another
628 * interrupt for the next pending alarm.
631 (*clock
->cl_ops
->c_setalrm
)(alarm_time
);
636 * ALARM DELIVERY ROUTINES.
641 thread_call_param_t p0
,
642 thread_call_param_t p1
)
644 register alarm_t alrm
;
649 while (alrm
= alrmdone
) {
650 if (alrmdone
= alrm
->al_next
)
651 alrmdone
->al_prev
= (alarm_t
) &alrmdone
;
654 code
= (alrm
->al_status
== ALARM_DONE
? KERN_SUCCESS
: KERN_ABORTED
);
655 if (alrm
->al_port
!= IP_NULL
) {
656 /* Deliver message to designated port */
657 if (IP_VALID(alrm
->al_port
)) {
658 clock_alarm_reply(alrm
->al_port
, alrm
->al_port_type
, code
,
659 alrm
->al_type
, alrm
->al_time
);
663 alrm
->al_status
= ALARM_FREE
;
664 alrm
->al_next
= alrmfree
;
668 panic("clock_alarm_deliver");
675 * CLOCK PRIVATE SERVICING SUBROUTINES.
679 * Flush all pending alarms on a clock. All alarms
680 * are activated and timestamped correctly, so any
681 * programs waiting on alarms/threads will proceed
682 * with accurate information.
689 register alarm_t alrm1
, alrm2
;
693 * Flush all outstanding alarms.
696 alrm1
= (alarm_t
) &clock
->cl_alarm
;
697 while (alrm2
= alrm1
->al_next
) {
699 * Remove alarm from the clock alarm list.
701 if (alrm1
->al_next
= alrm2
->al_next
)
702 (alrm1
->al_next
)->al_prev
= alrm1
;
705 * If a clock_sleep() alarm, wakeup the thread
706 * which issued the clock_sleep() call.
708 if (alrm2
->al_status
== ALARM_SLEEP
) {
710 thread_wakeup((event_t
)alrm2
);
714 * If a clock_alarm() alarm, place the alarm on
715 * the alarm done list and wakeup the dedicated
716 * kernel alarm_thread to service the alarm.
718 assert(alrm2
->al_status
== ALARM_CLOCK
);
719 if (alrm2
->al_next
= alrmdone
)
720 alrmdone
->al_prev
= alrm2
;
722 thread_wakeup((event_t
)&alrmdone
);
723 alrm2
->al_prev
= (alarm_t
) &alrmdone
;
731 * Post an alarm on a clock's active alarm list. The alarm is
732 * inserted in time-order into the clock's active alarm list.
733 * Always called from within a LOCK_CLOCK() code section.
741 register alarm_t alrm1
, alrm2
;
742 mach_timespec_t
*alarm_time
;
743 mach_timespec_t
*queue_time
;
746 * Traverse alarm list until queue time is greater
747 * than alarm time, then insert alarm.
749 alarm_time
= &alarm
->al_time
;
750 alrm1
= (alarm_t
) &clock
->cl_alarm
;
751 while (alrm2
= alrm1
->al_next
) {
752 queue_time
= &alrm2
->al_time
;
753 if (CMP_MACH_TIMESPEC(queue_time
, alarm_time
) > 0)
757 alrm1
->al_next
= alarm
;
758 alarm
->al_next
= alrm2
;
759 alarm
->al_prev
= alrm1
;
761 alrm2
->al_prev
= alarm
;
764 * If the inserted alarm is the 'earliest' alarm,
765 * reset the device layer alarm time accordingly.
767 if (clock
->cl_alarm
.al_next
== alarm
)
768 (*clock
->cl_ops
->c_setalrm
)(alarm_time
);
772 * Check the validity of 'alarm_time' and 'alarm_type'. If either
773 * argument is invalid, return a negative value. If the 'alarm_time'
774 * is now, return a 0 value. If the 'alarm_time' is in the future,
775 * 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);
799 clock_get_system_value(void)
801 clock_t clock
= &clock_list
[SYSTEM_CLOCK
];
802 mach_timespec_t value
;
804 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
810 clock_get_calendar_value(void)
812 clock_t clock
= &clock_list
[CALENDAR_CLOCK
];
813 mach_timespec_t value
= MACH_TIMESPEC_ZERO
;
815 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
821 clock_deadline_for_periodic_event(
826 assert(interval
!= 0);
828 *deadline
+= interval
;
830 if (*deadline
<= abstime
) {
831 *deadline
= abstime
+ interval
;
832 abstime
= mach_absolute_time();
834 if (*deadline
<= abstime
)
835 *deadline
= abstime
+ interval
;
842 uint32_t *abs_to_ns_numer
,
843 uint32_t *abs_to_ns_denom
,
844 uint32_t *proc_to_abs_numer
,
845 uint32_t *proc_to_abs_denom
)
847 mach_timebase_info_data_t info
;
850 clock_timebase_info(&info
);
852 copyout((void *)&one
, (void *)delta
, sizeof (uint32_t));
854 copyout((void *)&info
.numer
, (void *)abs_to_ns_numer
, sizeof (uint32_t));
855 copyout((void *)&info
.denom
, (void *)abs_to_ns_denom
, sizeof (uint32_t));
857 copyout((void *)&one
, (void *)proc_to_abs_numer
, sizeof (uint32_t));
858 copyout((void *)&one
, (void *)proc_to_abs_denom
, sizeof (uint32_t));
863 mach_timebase_info_t out_info
)
865 mach_timebase_info_data_t info
;
867 clock_timebase_info(&info
);
869 copyout((void *)&info
, (void *)out_info
, sizeof (info
));
871 return (KERN_SUCCESS
);
880 wait_result
= assert_wait((event_t
)&mach_wait_until
, THREAD_ABORTSAFE
);
881 if (wait_result
== THREAD_WAITING
) {
882 thread_set_timer_deadline(deadline
);
883 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
884 if (wait_result
!= THREAD_TIMED_OUT
)
885 thread_cancel_timer();
888 return ((wait_result
== THREAD_INTERRUPTED
)? KERN_ABORTED
: KERN_SUCCESS
);
900 simple_lock(&calend_adjlock
);
902 interval
= clock_set_calendar_adjtime(secs
, microsecs
);
904 if (calend_adjdeadline
>= interval
)
905 calend_adjdeadline
-= interval
;
906 clock_deadline_for_periodic_event(interval
, mach_absolute_time(),
907 &calend_adjdeadline
);
909 timer_call_enter(&calend_adjcall
, calend_adjdeadline
);
912 timer_call_cancel(&calend_adjcall
);
914 simple_unlock(&calend_adjlock
);
920 timer_call_param_t p0
,
921 timer_call_param_t p1
)
927 simple_lock(&calend_adjlock
);
929 interval
= clock_adjust_calendar();
931 clock_deadline_for_periodic_event(interval
, mach_absolute_time(),
932 &calend_adjdeadline
);
934 timer_call_enter(&calend_adjcall
, calend_adjdeadline
);
937 simple_unlock(&calend_adjlock
);
942 clock_wakeup_calendar(void)
944 thread_call_enter(&calend_wakecall
);
949 thread_call_param_t p0
,
950 thread_call_param_t p1
)
952 void IOKitResetTime(void);