2 * Copyright (c) 2000-2020 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 /* zone for user alarms */
101 static ZONE_DECLARE(alarm_zone
, "alarms", sizeof(struct alarm
), ZC_NONE
);
102 static struct alarm
*alrmfree
; /* alarm free list pointer */
103 static struct alarm
*alrmdone
; /* alarm done list pointer */
104 static struct alarm
*alrmlist
;
105 static long alrm_seqno
; /* uniquely identifies alarms */
106 static thread_call_data_t alarm_done_call
;
107 static timer_call_data_t alarm_expire_timer
;
109 extern struct clock clock_list
[];
110 extern int clock_count
;
112 static void post_alarm(
115 static void set_alarm(
116 mach_timespec_t
*alarm_time
);
118 static int check_time(
119 alarm_type_t alarm_type
,
120 mach_timespec_t
*alarm_time
,
121 mach_timespec_t
*clock_time
);
123 static void alarm_done(void);
125 static void alarm_expire(void);
127 static kern_return_t
clock_sleep_internal(
129 sleep_type_t sleep_type
,
130 mach_timespec_t
*sleep_time
);
132 int rtclock_init(void);
134 kern_return_t
rtclock_gettime(
135 mach_timespec_t
*cur_time
);
137 kern_return_t
rtclock_getattr(
138 clock_flavor_t flavor
,
140 mach_msg_type_number_t
*count
);
142 SECURITY_READ_ONLY_EARLY(struct clock_ops
) sysclk_ops
= {
144 .c_init
= rtclock_init
,
145 .c_gettime
= rtclock_gettime
,
146 .c_getattr
= rtclock_getattr
,
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 SECURITY_READ_ONLY_EARLY(struct clock_ops
) calend_ops
= {
160 .c_gettime
= calend_gettime
,
161 .c_getattr
= calend_getattr
,
165 * List of clock devices.
167 SECURITY_READ_ONLY_LATE(struct clock
) clock_list
[] = {
169 .cl_ops
= &sysclk_ops
,
170 .cl_service
= IPC_PORT_NULL
,
171 .cl_control
= IPC_PORT_NULL
,
174 .cl_ops
= &calend_ops
,
175 .cl_service
= IPC_PORT_NULL
,
176 .cl_control
= IPC_PORT_NULL
,
179 int clock_count
= sizeof(clock_list
) / sizeof(clock_list
[0]);
182 * Macros to lock/unlock clock system.
184 #define LOCK_ALARM(s) \
186 simple_lock(&alarm_lock, LCK_GRP_NULL);
188 #define UNLOCK_ALARM(s) \
189 simple_unlock(&alarm_lock); \
193 clock_oldconfig(void)
198 simple_lock_init(&alarm_lock
, 0);
199 thread_call_setup(&alarm_done_call
, (thread_call_func_t
)alarm_done
, NULL
);
200 timer_call_setup(&alarm_expire_timer
, (timer_call_func_t
)alarm_expire
, NULL
);
203 * Configure clock devices.
205 for (i
= 0; i
< clock_count
; i
++) {
206 clock
= &clock_list
[i
];
207 if (clock
->cl_ops
&& clock
->cl_ops
->c_config
) {
208 if ((*clock
->cl_ops
->c_config
)() == 0) {
209 clock
->cl_ops
= NULL
;
214 /* start alarm sequence numbers at 0 */
225 * Initialize basic clock structures.
227 for (i
= 0; i
< clock_count
; i
++) {
228 clock
= &clock_list
[i
];
229 if (clock
->cl_ops
&& clock
->cl_ops
->c_init
) {
230 (*clock
->cl_ops
->c_init
)();
236 * Initialize the clock ipc service facility.
239 clock_service_create(void)
242 * Initialize ipc clock services.
244 for (int i
= 0; i
< clock_count
; i
++) {
245 clock_t clock
= &clock_list
[i
];
247 ipc_clock_init(clock
);
248 ipc_clock_enable(clock
);
254 * Get the service port on a clock.
257 host_get_clock_service(
260 clock_t *clock
) /* OUT */
262 if (host
== HOST_NULL
|| clock_id
< 0 || clock_id
>= clock_count
) {
264 return KERN_INVALID_ARGUMENT
;
267 *clock
= &clock_list
[clock_id
];
268 if ((*clock
)->cl_ops
== 0) {
275 * Get the control port on a clock.
278 host_get_clock_control(
279 host_priv_t host_priv
,
281 clock_t *clock
) /* OUT */
283 if (host_priv
== HOST_PRIV_NULL
||
284 clock_id
< 0 || clock_id
>= clock_count
) {
286 return KERN_INVALID_ARGUMENT
;
289 *clock
= &clock_list
[clock_id
];
290 if ((*clock
)->cl_ops
== 0) {
297 * Get the current clock time.
302 mach_timespec_t
*cur_time
) /* OUT */
304 if (clock
== CLOCK_NULL
) {
305 return KERN_INVALID_ARGUMENT
;
307 return (*clock
->cl_ops
->c_gettime
)(cur_time
);
312 mach_timespec_t
*time
) /* OUT */
317 clock_get_system_nanotime(&secs
, &nsecs
);
318 time
->tv_sec
= (unsigned int)secs
;
319 time
->tv_nsec
= nsecs
;
326 mach_timespec_t
*time
) /* OUT */
331 clock_get_calendar_nanotime(&secs
, &nsecs
);
332 time
->tv_sec
= (unsigned int)secs
;
333 time
->tv_nsec
= nsecs
;
339 * Get clock attributes.
342 clock_get_attributes(
344 clock_flavor_t flavor
,
345 clock_attr_t attr
, /* OUT */
346 mach_msg_type_number_t
*count
) /* IN/OUT */
348 if (clock
== CLOCK_NULL
) {
349 return KERN_INVALID_ARGUMENT
;
351 if (clock
->cl_ops
->c_getattr
) {
352 return clock
->cl_ops
->c_getattr(flavor
, attr
, count
);
359 clock_flavor_t flavor
,
360 clock_attr_t attr
, /* OUT */
361 mach_msg_type_number_t
*count
) /* IN/OUT */
368 case CLOCK_GET_TIME_RES
: /* >0 res */
369 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
370 case CLOCK_ALARM_MINRES
:
371 case CLOCK_ALARM_MAXRES
:
372 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
376 return KERN_INVALID_VALUE
;
384 clock_flavor_t flavor
,
385 clock_attr_t attr
, /* OUT */
386 mach_msg_type_number_t
*count
) /* IN/OUT */
393 case CLOCK_GET_TIME_RES
: /* >0 res */
394 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
397 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
398 case CLOCK_ALARM_MINRES
:
399 case CLOCK_ALARM_MAXRES
:
400 *(clock_res_t
*) attr
= 0;
404 return KERN_INVALID_VALUE
;
411 * Set the current clock time.
416 __unused mach_timespec_t new_time
)
418 if (clock
== CLOCK_NULL
) {
419 return KERN_INVALID_ARGUMENT
;
425 * Set the clock alarm resolution.
428 clock_set_attributes(
430 __unused clock_flavor_t flavor
,
431 __unused clock_attr_t attr
,
432 __unused mach_msg_type_number_t count
)
434 if (clock
== CLOCK_NULL
) {
435 return KERN_INVALID_ARGUMENT
;
441 * Setup a clock alarm.
446 alarm_type_t alarm_type
,
447 mach_timespec_t alarm_time
,
448 ipc_port_t alarm_port
,
449 mach_msg_type_name_t alarm_port_type
)
452 mach_timespec_t clock_time
;
454 kern_return_t reply_code
;
457 if (clock
== CLOCK_NULL
) {
458 return KERN_INVALID_ARGUMENT
;
460 if (clock
!= &clock_list
[SYSTEM_CLOCK
]) {
463 if (IP_VALID(alarm_port
) == 0) {
464 return KERN_INVALID_CAPABILITY
;
468 * Check alarm parameters. If parameters are invalid,
469 * send alarm message immediately.
471 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
472 chkstat
= check_time(alarm_type
, &alarm_time
, &clock_time
);
474 reply_code
= (chkstat
< 0 ? KERN_INVALID_VALUE
: KERN_SUCCESS
);
475 clock_alarm_reply(alarm_port
, alarm_port_type
,
476 reply_code
, alarm_type
, clock_time
);
481 * Get alarm and add to clock alarm list.
485 if ((alarm
= alrmfree
) == 0) {
487 alarm
= (alarm_t
) zalloc(alarm_zone
);
489 return KERN_RESOURCE_SHORTAGE
;
493 alrmfree
= alarm
->al_next
;
496 alarm
->al_status
= ALARM_CLOCK
;
497 alarm
->al_time
= alarm_time
;
498 alarm
->al_type
= alarm_type
;
499 alarm
->al_port
= alarm_port
;
500 alarm
->al_port_type
= alarm_port_type
;
501 alarm
->al_clock
= clock
;
502 alarm
->al_seqno
= alrm_seqno
++;
510 * Sleep on a clock. System trap. User-level libmach clock_sleep
511 * interface call takes a mach_timespec_t sleep_time argument which it
512 * converts to sleep_sec and sleep_nsec arguments which are then
513 * passed to clock_sleep_trap.
517 struct clock_sleep_trap_args
*args
)
519 mach_port_name_t clock_name
= args
->clock_name
;
520 sleep_type_t sleep_type
= args
->sleep_type
;
521 int sleep_sec
= args
->sleep_sec
;
522 int sleep_nsec
= args
->sleep_nsec
;
523 mach_vm_address_t wakeup_time_addr
= args
->wakeup_time
;
525 mach_timespec_t swtime
= {};
526 kern_return_t rvalue
;
529 * Convert the trap parameters.
531 if (clock_name
== MACH_PORT_NULL
) {
532 clock
= &clock_list
[SYSTEM_CLOCK
];
534 clock
= port_name_to_clock(clock_name
);
537 swtime
.tv_sec
= sleep_sec
;
538 swtime
.tv_nsec
= sleep_nsec
;
541 * Call the actual clock_sleep routine.
543 rvalue
= clock_sleep_internal(clock
, sleep_type
, &swtime
);
546 * Return current time as wakeup time.
548 if (rvalue
!= KERN_INVALID_ARGUMENT
&& rvalue
!= KERN_FAILURE
) {
549 copyout((char *)&swtime
, wakeup_time_addr
, sizeof(mach_timespec_t
));
555 clock_sleep_internal(
557 sleep_type_t sleep_type
,
558 mach_timespec_t
*sleep_time
)
561 mach_timespec_t clock_time
;
562 kern_return_t rvalue
;
566 if (clock
== CLOCK_NULL
) {
567 return KERN_INVALID_ARGUMENT
;
570 if (clock
!= &clock_list
[SYSTEM_CLOCK
]) {
575 * Check sleep parameters. If parameters are invalid
576 * return an error, otherwise post alarm request.
578 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
580 chkstat
= check_time(sleep_type
, sleep_time
, &clock_time
);
582 return KERN_INVALID_VALUE
;
584 rvalue
= KERN_SUCCESS
;
586 wait_result_t wait_result
;
589 * Get alarm and add to clock alarm list.
593 if ((alarm
= alrmfree
) == 0) {
595 alarm
= (alarm_t
) zalloc(alarm_zone
);
597 return KERN_RESOURCE_SHORTAGE
;
601 alrmfree
= alarm
->al_next
;
605 * Wait for alarm to occur.
607 wait_result
= assert_wait((event_t
)alarm
, THREAD_ABORTSAFE
);
608 if (wait_result
== THREAD_WAITING
) {
609 alarm
->al_time
= *sleep_time
;
610 alarm
->al_status
= ALARM_SLEEP
;
614 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
617 * Note if alarm expired normally or whether it
618 * was aborted. If aborted, delete alarm from
619 * clock alarm list. Return alarm to free list.
622 if (alarm
->al_status
!= ALARM_DONE
) {
623 assert(wait_result
!= THREAD_AWAKENED
);
624 if (((alarm
->al_prev
)->al_next
= alarm
->al_next
) != NULL
) {
625 (alarm
->al_next
)->al_prev
= alarm
->al_prev
;
627 rvalue
= KERN_ABORTED
;
629 *sleep_time
= alarm
->al_time
;
630 alarm
->al_status
= ALARM_FREE
;
632 assert(wait_result
== THREAD_INTERRUPTED
);
633 assert(alarm
->al_status
== ALARM_FREE
);
634 rvalue
= KERN_ABORTED
;
636 alarm
->al_next
= alrmfree
;
640 *sleep_time
= clock_time
;
647 * Service clock alarm expirations.
655 mach_timespec_t clock_time
;
656 mach_timespec_t
*alarm_time
;
659 clock
= &clock_list
[SYSTEM_CLOCK
];
660 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
663 * Update clock alarm list. Alarms that are due are moved
664 * to the alarmdone list to be serviced by a thread callout.
667 alrm1
= (alarm_t
)&alrmlist
;
668 while ((alrm2
= alrm1
->al_next
) != NULL
) {
669 alarm_time
= &alrm2
->al_time
;
670 if (CMP_MACH_TIMESPEC(alarm_time
, &clock_time
) > 0) {
675 * Alarm has expired, so remove it from the
678 if ((alrm1
->al_next
= alrm2
->al_next
) != NULL
) {
679 (alrm1
->al_next
)->al_prev
= alrm1
;
683 * If a clock_sleep() alarm, wakeup the thread
684 * which issued the clock_sleep() call.
686 if (alrm2
->al_status
== ALARM_SLEEP
) {
687 alrm2
->al_next
= NULL
;
688 alrm2
->al_status
= ALARM_DONE
;
689 alrm2
->al_time
= clock_time
;
690 thread_wakeup((event_t
)alrm2
);
693 * If a clock_alarm() alarm, place the alarm on
694 * the alarm done list and schedule the alarm
695 * delivery mechanism.
698 assert(alrm2
->al_status
== ALARM_CLOCK
);
699 if ((alrm2
->al_next
= alrmdone
) != NULL
) {
700 alrmdone
->al_prev
= alrm2
;
702 thread_call_enter(&alarm_done_call
);
704 alrm2
->al_prev
= (alarm_t
)&alrmdone
;
706 alrm2
->al_status
= ALARM_DONE
;
707 alrm2
->al_time
= clock_time
;
712 * Setup to expire for the next pending alarm.
715 set_alarm(alarm_time
);
728 while ((alrm
= alrmdone
) != NULL
) {
729 if ((alrmdone
= alrm
->al_next
) != NULL
) {
730 alrmdone
->al_prev
= (alarm_t
)&alrmdone
;
734 code
= (alrm
->al_status
== ALARM_DONE
? KERN_SUCCESS
: KERN_ABORTED
);
735 if (alrm
->al_port
!= IP_NULL
) {
736 /* Deliver message to designated port */
737 if (IP_VALID(alrm
->al_port
)) {
738 clock_alarm_reply(alrm
->al_port
, alrm
->al_port_type
, code
,
739 alrm
->al_type
, alrm
->al_time
);
743 alrm
->al_status
= ALARM_FREE
;
744 alrm
->al_next
= alrmfree
;
747 panic("clock_alarm_deliver");
755 * Post an alarm on the active alarm list.
757 * Always called from within a LOCK_ALARM() code section.
763 alarm_t alrm1
, alrm2
;
764 mach_timespec_t
*alarm_time
;
765 mach_timespec_t
*queue_time
;
768 * Traverse alarm list until queue time is greater
769 * than alarm time, then insert alarm.
771 alarm_time
= &alarm
->al_time
;
772 alrm1
= (alarm_t
)&alrmlist
;
773 while ((alrm2
= alrm1
->al_next
) != NULL
) {
774 queue_time
= &alrm2
->al_time
;
775 if (CMP_MACH_TIMESPEC(queue_time
, alarm_time
) > 0) {
780 alrm1
->al_next
= alarm
;
781 alarm
->al_next
= alrm2
;
782 alarm
->al_prev
= alrm1
;
784 alrm2
->al_prev
= alarm
;
788 * If the inserted alarm is the 'earliest' alarm,
789 * reset the device layer alarm time accordingly.
791 if (alrmlist
== alarm
) {
792 set_alarm(alarm_time
);
798 mach_timespec_t
*alarm_time
)
802 nanotime_to_absolutetime(alarm_time
->tv_sec
, alarm_time
->tv_nsec
, &abstime
);
803 timer_call_enter_with_leeway(&alarm_expire_timer
, NULL
, abstime
, 0, TIMER_CALL_USER_NORMAL
, FALSE
);
807 * Check the validity of 'alarm_time' and 'alarm_type'. If either
808 * argument is invalid, return a negative value. If the 'alarm_time'
809 * is now, return a 0 value. If the 'alarm_time' is in the future,
810 * return a positive value.
814 alarm_type_t alarm_type
,
815 mach_timespec_t
*alarm_time
,
816 mach_timespec_t
*clock_time
)
820 if (BAD_ALRMTYPE(alarm_type
)) {
823 if (BAD_MACH_TIMESPEC(alarm_time
)) {
826 if ((alarm_type
& ALRMTYPE
) == TIME_RELATIVE
) {
827 ADD_MACH_TIMESPEC(alarm_time
, clock_time
);
830 result
= CMP_MACH_TIMESPEC(alarm_time
, clock_time
);
832 return (result
>= 0)? result
: 0;
838 clock_get_system_value(void)
840 clock_t clock
= &clock_list
[SYSTEM_CLOCK
];
841 mach_timespec_t value
;
843 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
849 clock_get_calendar_value(void)
851 clock_t clock
= &clock_list
[CALENDAR_CLOCK
];
852 mach_timespec_t value
= MACH_TIMESPEC_ZERO
;
854 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
859 #endif /* __LP64__ */