2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
27 * Purpose: Routines for the creation and use of kernel
28 * alarm clock services. This file and the ipc
29 * routines in kern/ipc_clock.c constitute the
30 * machine-independent clock service layer.
34 #include <mach_host.h>
36 #include <mach/boolean.h>
37 #include <mach/processor_info.h>
38 #include <mach/vm_param.h>
39 #include <machine/mach_param.h>
40 #include <kern/cpu_number.h>
41 #include <kern/misc_protos.h>
42 #include <kern/lock.h>
43 #include <kern/host.h>
45 #include <kern/thread.h>
46 #include <kern/thread_swap.h>
47 #include <kern/ipc_host.h>
48 #include <kern/clock.h>
49 #include <kern/zalloc.h>
50 #include <ipc/ipc_port.h>
52 #include <mach/mach_syscalls.h>
53 #include <mach/clock_reply.h>
54 #include <mach/mach_time.h>
56 #include <kern/mk_timer.h>
62 #include <mach/clock_server.h>
63 #include <mach/mach_host_server.h>
65 /* local data declarations */
66 decl_simple_lock_data(static,ClockLock
) /* clock system synchronization */
67 static struct zone
*alarm_zone
; /* zone for user alarms */
68 static struct alarm
*alrmfree
; /* alarm free list pointer */
69 static struct alarm
*alrmdone
; /* alarm done list pointer */
70 static long alrm_seqno
; /* uniquely identifies alarms */
71 static thread_call_data_t alarm_deliver
;
73 /* backwards compatibility */
74 int hz
= HZ
; /* GET RID OF THIS !!! */
75 int tick
= (1000000 / HZ
); /* GET RID OF THIS !!! */
77 /* external declarations */
78 extern struct clock clock_list
[];
79 extern int clock_count
;
81 /* local clock subroutines */
93 alarm_type_t alarm_type
,
94 mach_timespec_t
*alarm_time
,
95 mach_timespec_t
*clock_time
);
98 void clock_alarm_deliver(
99 thread_call_param_t p0
,
100 thread_call_param_t p1
);
103 * Macros to lock/unlock clock system.
105 #define LOCK_CLOCK(s) \
107 simple_lock(&ClockLock);
109 #define UNLOCK_CLOCK(s) \
110 simple_unlock(&ClockLock); \
114 * Configure the clock system. (Not sure if we need this,
115 * as separate from clock_init()).
123 if (cpu_number() != master_cpu
)
124 panic("clock_config");
127 * Configure clock devices.
129 simple_lock_init(&ClockLock
, ETAP_MISC_CLOCK
);
130 for (i
= 0; i
< clock_count
; i
++) {
131 clock
= &clock_list
[i
];
133 if ((*clock
->cl_ops
->c_config
)() == 0)
138 /* start alarm sequence numbers at 0 */
143 * Initialize the clock system.
152 * Initialize basic clock structures.
154 for (i
= 0; i
< clock_count
; i
++) {
155 clock
= &clock_list
[i
];
157 (*clock
->cl_ops
->c_init
)();
162 * Initialize the clock ipc service facility.
165 clock_service_create(void)
170 mk_timer_initialize();
173 * Initialize ipc clock services.
175 for (i
= 0; i
< clock_count
; i
++) {
176 clock
= &clock_list
[i
];
178 ipc_clock_init(clock
);
179 ipc_clock_enable(clock
);
184 * Initialize clock service alarms.
186 i
= sizeof(struct alarm
);
187 alarm_zone
= zinit(i
, (4096/i
)*i
, 10*i
, "alarms");
190 * Initialize the clock alarm delivery mechanism.
192 thread_call_setup(&alarm_deliver
, clock_alarm_deliver
, NULL
);
196 * Get the service port on a clock.
199 host_get_clock_service(
202 clock_t *clock
) /* OUT */
204 if (host
== HOST_NULL
|| clock_id
< 0 || clock_id
>= clock_count
) {
206 return (KERN_INVALID_ARGUMENT
);
209 *clock
= &clock_list
[clock_id
];
210 if ((*clock
)->cl_ops
== 0)
211 return (KERN_FAILURE
);
212 return (KERN_SUCCESS
);
216 * Get the control port on a clock.
219 host_get_clock_control(
220 host_priv_t host_priv
,
222 clock_t *clock
) /* OUT */
224 if (host_priv
== HOST_PRIV_NULL
|| clock_id
< 0 || clock_id
>= clock_count
) {
226 return (KERN_INVALID_ARGUMENT
);
229 *clock
= &clock_list
[clock_id
];
230 if ((*clock
)->cl_ops
== 0)
231 return (KERN_FAILURE
);
232 return (KERN_SUCCESS
);
236 * Get the current clock time.
241 mach_timespec_t
*cur_time
) /* OUT */
243 if (clock
== CLOCK_NULL
)
244 return (KERN_INVALID_ARGUMENT
);
245 return ((*clock
->cl_ops
->c_gettime
)(cur_time
));
249 * Get clock attributes.
252 clock_get_attributes(
254 clock_flavor_t flavor
,
255 clock_attr_t attr
, /* OUT */
256 mach_msg_type_number_t
*count
) /* IN/OUT */
258 kern_return_t (*getattr
)(
259 clock_flavor_t flavor
,
261 mach_msg_type_number_t
*count
);
263 if (clock
== CLOCK_NULL
)
264 return (KERN_INVALID_ARGUMENT
);
265 if (getattr
= clock
->cl_ops
->c_getattr
)
266 return((*getattr
)(flavor
, attr
, count
));
268 return (KERN_FAILURE
);
272 * Set the current clock time.
277 mach_timespec_t new_time
)
279 mach_timespec_t
*clock_time
;
280 kern_return_t (*settime
)(
281 mach_timespec_t
*clock_time
);
283 if (clock
== CLOCK_NULL
)
284 return (KERN_INVALID_ARGUMENT
);
285 if ((settime
= clock
->cl_ops
->c_settime
) == 0)
286 return (KERN_FAILURE
);
287 clock_time
= &new_time
;
288 if (BAD_MACH_TIMESPEC(clock_time
))
289 return (KERN_INVALID_VALUE
);
292 * Flush all outstanding alarms.
299 return ((*settime
)(clock_time
));
303 * Set the clock alarm resolution.
306 clock_set_attributes(
308 clock_flavor_t flavor
,
310 mach_msg_type_number_t count
)
312 kern_return_t (*setattr
)(
313 clock_flavor_t flavor
,
315 mach_msg_type_number_t count
);
317 if (clock
== CLOCK_NULL
)
318 return (KERN_INVALID_ARGUMENT
);
319 if (setattr
= clock
->cl_ops
->c_setattr
)
320 return ((*setattr
)(flavor
, attr
, count
));
322 return (KERN_FAILURE
);
326 * Setup a clock alarm.
331 alarm_type_t alarm_type
,
332 mach_timespec_t alarm_time
,
333 ipc_port_t alarm_port
,
334 mach_msg_type_name_t alarm_port_type
)
337 mach_timespec_t clock_time
;
339 kern_return_t reply_code
;
342 if (clock
== CLOCK_NULL
)
343 return (KERN_INVALID_ARGUMENT
);
344 if (clock
->cl_ops
->c_setalrm
== 0)
345 return (KERN_FAILURE
);
346 if (IP_VALID(alarm_port
) == 0)
347 return (KERN_INVALID_CAPABILITY
);
350 * Check alarm parameters. If parameters are invalid,
351 * send alarm message immediately.
353 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
354 chkstat
= check_time(alarm_type
, &alarm_time
, &clock_time
);
356 reply_code
= (chkstat
< 0 ? KERN_INVALID_VALUE
: KERN_SUCCESS
);
357 clock_alarm_reply(alarm_port
, alarm_port_type
,
358 reply_code
, alarm_type
, clock_time
);
359 return (KERN_SUCCESS
);
363 * Get alarm and add to clock alarm list.
367 if ((alarm
= alrmfree
) == 0) {
369 alarm
= (alarm_t
) zalloc(alarm_zone
);
371 return (KERN_RESOURCE_SHORTAGE
);
375 alrmfree
= alarm
->al_next
;
377 alarm
->al_status
= ALARM_CLOCK
;
378 alarm
->al_time
= alarm_time
;
379 alarm
->al_type
= alarm_type
;
380 alarm
->al_port
= alarm_port
;
381 alarm
->al_port_type
= alarm_port_type
;
382 alarm
->al_clock
= clock
;
383 alarm
->al_seqno
= alrm_seqno
++;
384 post_alarm(clock
, alarm
);
387 return (KERN_SUCCESS
);
391 * Sleep on a clock. System trap. User-level libmach clock_sleep
392 * interface call takes a mach_timespec_t sleep_time argument which it
393 * converts to sleep_sec and sleep_nsec arguments which are then
394 * passed to clock_sleep_trap.
398 mach_port_name_t clock_name
,
399 sleep_type_t sleep_type
,
402 mach_timespec_t
*wakeup_time
)
405 mach_timespec_t swtime
;
406 kern_return_t rvalue
;
409 * Convert the trap parameters.
411 if (clock_name
!= MACH_PORT_NULL
)
412 clock
= port_name_to_clock(clock_name
);
414 clock
= &clock_list
[SYSTEM_CLOCK
];
416 swtime
.tv_sec
= sleep_sec
;
417 swtime
.tv_nsec
= sleep_nsec
;
420 * Call the actual clock_sleep routine.
422 rvalue
= clock_sleep_internal(clock
, sleep_type
, &swtime
);
425 * Return current time as wakeup time.
427 if (rvalue
!= KERN_INVALID_ARGUMENT
&& rvalue
!= KERN_FAILURE
) {
428 copyout((char *)&swtime
, (char *)wakeup_time
,
429 sizeof(mach_timespec_t
));
435 * Kernel internally callable clock sleep routine. The calling
436 * thread is suspended until the requested sleep time is reached.
439 clock_sleep_internal(
441 sleep_type_t sleep_type
,
442 mach_timespec_t
*sleep_time
)
445 mach_timespec_t clock_time
;
446 kern_return_t rvalue
;
450 if (clock
== CLOCK_NULL
)
451 return (KERN_INVALID_ARGUMENT
);
452 if (clock
->cl_ops
->c_setalrm
== 0)
453 return (KERN_FAILURE
);
456 * Check sleep parameters. If parameters are invalid
457 * return an error, otherwise post alarm request.
459 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
461 chkstat
= check_time(sleep_type
, sleep_time
, &clock_time
);
463 return (KERN_INVALID_VALUE
);
464 rvalue
= KERN_SUCCESS
;
467 * Get alarm and add to clock alarm list.
471 if ((alarm
= alrmfree
) == 0) {
473 alarm
= (alarm_t
) zalloc(alarm_zone
);
475 return (KERN_RESOURCE_SHORTAGE
);
479 alrmfree
= alarm
->al_next
;
481 alarm
->al_time
= *sleep_time
;
482 alarm
->al_status
= ALARM_SLEEP
;
483 post_alarm(clock
, alarm
);
486 * Wait for alarm to occur.
488 assert_wait((event_t
)alarm
, THREAD_ABORTSAFE
);
490 /* should we force spl(0) at this point? */
491 thread_block((void (*)(void)) 0);
492 /* we should return here at ipl0 */
495 * Note if alarm expired normally or whether it
496 * was aborted. If aborted, delete alarm from
497 * clock alarm list. Return alarm to free list.
500 if (alarm
->al_status
!= ALARM_DONE
) {
501 /* This means we were interrupted and that
502 thread->wait_result != THREAD_AWAKENED. */
503 if ((alarm
->al_prev
)->al_next
= alarm
->al_next
)
504 (alarm
->al_next
)->al_prev
= alarm
->al_prev
;
505 rvalue
= KERN_ABORTED
;
507 *sleep_time
= alarm
->al_time
;
508 alarm
->al_status
= ALARM_FREE
;
509 alarm
->al_next
= alrmfree
;
514 *sleep_time
= clock_time
;
520 * CLOCK INTERRUPT SERVICE ROUTINES.
524 * Service clock alarm interrupts. Called from machine dependent
525 * layer at splclock(). The clock_id argument specifies the clock,
526 * and the clock_time argument gives that clock's current time.
531 mach_timespec_t
*clock_time
)
534 register alarm_t alrm1
;
535 register alarm_t alrm2
;
536 mach_timespec_t
*alarm_time
;
539 clock
= &clock_list
[clock_id
];
542 * Update clock alarm list. All alarms that are due are moved
543 * to the alarmdone list to be serviced by the alarm_thread.
547 alrm1
= (alarm_t
) &clock
->cl_alarm
;
548 while (alrm2
= alrm1
->al_next
) {
549 alarm_time
= &alrm2
->al_time
;
550 if (CMP_MACH_TIMESPEC(alarm_time
, clock_time
) > 0)
554 * Alarm has expired, so remove it from the
557 if (alrm1
->al_next
= alrm2
->al_next
)
558 (alrm1
->al_next
)->al_prev
= alrm1
;
561 * If a clock_sleep() alarm, wakeup the thread
562 * which issued the clock_sleep() call.
564 if (alrm2
->al_status
== ALARM_SLEEP
) {
566 alrm2
->al_status
= ALARM_DONE
;
567 alrm2
->al_time
= *clock_time
;
568 thread_wakeup((event_t
)alrm2
);
572 * If a clock_alarm() alarm, place the alarm on
573 * the alarm done list and schedule the alarm
574 * delivery mechanism.
577 assert(alrm2
->al_status
== ALARM_CLOCK
);
578 if (alrm2
->al_next
= alrmdone
)
579 alrmdone
->al_prev
= alrm2
;
581 thread_call_enter(&alarm_deliver
);
582 alrm2
->al_prev
= (alarm_t
) &alrmdone
;
584 alrm2
->al_status
= ALARM_DONE
;
585 alrm2
->al_time
= *clock_time
;
590 * Setup the clock dependent layer to deliver another
591 * interrupt for the next pending alarm.
594 (*clock
->cl_ops
->c_setalrm
)(alarm_time
);
599 * ALARM DELIVERY ROUTINES.
604 thread_call_param_t p0
,
605 thread_call_param_t p1
)
607 register alarm_t alrm
;
612 while (alrm
= alrmdone
) {
613 if (alrmdone
= alrm
->al_next
)
614 alrmdone
->al_prev
= (alarm_t
) &alrmdone
;
617 code
= (alrm
->al_status
== ALARM_DONE
? KERN_SUCCESS
: KERN_ABORTED
);
618 if (alrm
->al_port
!= IP_NULL
) {
619 /* Deliver message to designated port */
620 if (IP_VALID(alrm
->al_port
)) {
621 clock_alarm_reply(alrm
->al_port
, alrm
->al_port_type
, code
,
622 alrm
->al_type
, alrm
->al_time
);
626 alrm
->al_status
= ALARM_FREE
;
627 alrm
->al_next
= alrmfree
;
631 panic("clock_alarm_deliver");
638 * CLOCK PRIVATE SERVICING SUBROUTINES.
642 * Flush all pending alarms on a clock. All alarms
643 * are activated and timestamped correctly, so any
644 * programs waiting on alarms/threads will proceed
645 * with accurate information.
652 register alarm_t alrm1
, alrm2
;
656 * Flush all outstanding alarms.
659 alrm1
= (alarm_t
) &clock
->cl_alarm
;
660 while (alrm2
= alrm1
->al_next
) {
662 * Remove alarm from the clock alarm list.
664 if (alrm1
->al_next
= alrm2
->al_next
)
665 (alrm1
->al_next
)->al_prev
= alrm1
;
668 * If a clock_sleep() alarm, wakeup the thread
669 * which issued the clock_sleep() call.
671 if (alrm2
->al_status
== ALARM_SLEEP
) {
673 thread_wakeup((event_t
)alrm2
);
677 * If a clock_alarm() alarm, place the alarm on
678 * the alarm done list and wakeup the dedicated
679 * kernel alarm_thread to service the alarm.
681 assert(alrm2
->al_status
== ALARM_CLOCK
);
682 if (alrm2
->al_next
= alrmdone
)
683 alrmdone
->al_prev
= alrm2
;
685 thread_wakeup((event_t
)&alrmdone
);
686 alrm2
->al_prev
= (alarm_t
) &alrmdone
;
694 * Post an alarm on a clock's active alarm list. The alarm is
695 * inserted in time-order into the clock's active alarm list.
696 * Always called from within a LOCK_CLOCK() code section.
704 register alarm_t alrm1
, alrm2
;
705 mach_timespec_t
*alarm_time
;
706 mach_timespec_t
*queue_time
;
709 * Traverse alarm list until queue time is greater
710 * than alarm time, then insert alarm.
712 alarm_time
= &alarm
->al_time
;
713 alrm1
= (alarm_t
) &clock
->cl_alarm
;
714 while (alrm2
= alrm1
->al_next
) {
715 queue_time
= &alrm2
->al_time
;
716 if (CMP_MACH_TIMESPEC(queue_time
, alarm_time
) > 0)
720 alrm1
->al_next
= alarm
;
721 alarm
->al_next
= alrm2
;
722 alarm
->al_prev
= alrm1
;
724 alrm2
->al_prev
= alarm
;
727 * If the inserted alarm is the 'earliest' alarm,
728 * reset the device layer alarm time accordingly.
730 if (clock
->cl_alarm
.al_next
== alarm
)
731 (*clock
->cl_ops
->c_setalrm
)(alarm_time
);
735 * Check the validity of 'alarm_time' and 'alarm_type'. If either
736 * argument is invalid, return a negative value. If the 'alarm_time'
737 * is now, return a 0 value. If the 'alarm_time' is in the future,
738 * return a positive value.
743 alarm_type_t alarm_type
,
744 mach_timespec_t
*alarm_time
,
745 mach_timespec_t
*clock_time
)
749 if (BAD_ALRMTYPE(alarm_type
))
751 if (BAD_MACH_TIMESPEC(alarm_time
))
753 if ((alarm_type
& ALRMTYPE
) == TIME_RELATIVE
)
754 ADD_MACH_TIMESPEC(alarm_time
, clock_time
);
756 result
= CMP_MACH_TIMESPEC(alarm_time
, clock_time
);
758 return ((result
>= 0)? result
: 0);
762 clock_get_system_value(void)
764 clock_t clock
= &clock_list
[SYSTEM_CLOCK
];
765 mach_timespec_t value
;
767 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
773 clock_get_calendar_value(void)
775 clock_t clock
= &clock_list
[CALENDAR_CLOCK
];
776 mach_timespec_t value
= MACH_TIMESPEC_ZERO
;
778 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
784 clock_set_calendar_value(
785 mach_timespec_t value
)
787 clock_t clock
= &clock_list
[CALENDAR_CLOCK
];
789 (void) (*clock
->cl_ops
->c_settime
)(&value
);
793 clock_deadline_for_periodic_event(
798 assert(interval
!= 0);
800 *deadline
+= interval
;
802 if (*deadline
<= abstime
) {
804 clock_get_uptime(&abstime
);
805 *deadline
+= interval
;
807 if (*deadline
<= abstime
) {
809 *deadline
+= interval
;
817 uint32_t *abs_to_ns_numer
,
818 uint32_t *abs_to_ns_denom
,
819 uint32_t *proc_to_abs_numer
,
820 uint32_t *proc_to_abs_denom
)
822 mach_timebase_info_data_t info
;
825 clock_timebase_info(&info
);
827 copyout((void *)&one
, (void *)delta
, sizeof (uint32_t));
829 copyout((void *)&info
.numer
, (void *)abs_to_ns_numer
, sizeof (uint32_t));
830 copyout((void *)&info
.denom
, (void *)abs_to_ns_denom
, sizeof (uint32_t));
832 copyout((void *)&one
, (void *)proc_to_abs_numer
, sizeof (uint32_t));
833 copyout((void *)&one
, (void *)proc_to_abs_denom
, sizeof (uint32_t));
838 mach_timebase_info_t out_info
)
840 mach_timebase_info_data_t info
;
842 clock_timebase_info(&info
);
844 copyout((void *)&info
, (void *)out_info
, sizeof (info
));
846 return (KERN_SUCCESS
);
855 assert_wait((event_t
)&mach_wait_until
, THREAD_ABORTSAFE
);
856 thread_set_timer_deadline(deadline
);
857 wait_result
= thread_block((void (*)) 0);
858 if (wait_result
!= THREAD_TIMED_OUT
)
859 thread_cancel_timer();
861 return ((wait_result
== THREAD_INTERRUPTED
)? KERN_ABORTED
: KERN_SUCCESS
);