2 * Copyright (c) 2000-2019 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
= {
143 .c_init
= rtclock_init
,
144 .c_gettime
= rtclock_gettime
,
145 .c_getattr
= rtclock_getattr
,
148 kern_return_t
calend_gettime(
149 mach_timespec_t
*cur_time
);
151 kern_return_t
calend_getattr(
152 clock_flavor_t flavor
,
154 mach_msg_type_number_t
*count
);
156 SECURITY_READ_ONLY_EARLY(struct clock_ops
) calend_ops
= {
159 .c_gettime
= calend_gettime
,
160 .c_getattr
= calend_getattr
,
164 * List of clock devices.
166 SECURITY_READ_ONLY_LATE(struct clock
) clock_list
[] = {
168 .cl_ops
= &sysclk_ops
,
169 .cl_service
= IPC_PORT_NULL
,
170 .cl_control
= IPC_PORT_NULL
,
173 .cl_ops
= &calend_ops
,
174 .cl_service
= IPC_PORT_NULL
,
175 .cl_control
= IPC_PORT_NULL
,
178 int clock_count
= sizeof(clock_list
) / sizeof(clock_list
[0]);
181 * Macros to lock/unlock clock system.
183 #define LOCK_ALARM(s) \
185 simple_lock(&alarm_lock, LCK_GRP_NULL);
187 #define UNLOCK_ALARM(s) \
188 simple_unlock(&alarm_lock); \
192 clock_oldconfig(void)
197 simple_lock_init(&alarm_lock
, 0);
198 thread_call_setup(&alarm_done_call
, (thread_call_func_t
)alarm_done
, NULL
);
199 timer_call_setup(&alarm_expire_timer
, (timer_call_func_t
)alarm_expire
, NULL
);
202 * Configure clock devices.
204 for (i
= 0; i
< clock_count
; i
++) {
205 clock
= &clock_list
[i
];
206 if (clock
->cl_ops
&& clock
->cl_ops
->c_config
) {
207 if ((*clock
->cl_ops
->c_config
)() == 0) {
208 clock
->cl_ops
= NULL
;
213 /* start alarm sequence numbers at 0 */
224 * Initialize basic clock structures.
226 for (i
= 0; i
< clock_count
; i
++) {
227 clock
= &clock_list
[i
];
228 if (clock
->cl_ops
&& clock
->cl_ops
->c_init
) {
229 (*clock
->cl_ops
->c_init
)();
235 * Initialize the clock ipc service facility.
238 clock_service_create(void)
244 * Initialize ipc clock services.
246 for (i
= 0; i
< clock_count
; i
++) {
247 clock
= &clock_list
[i
];
249 ipc_clock_init(clock
);
250 ipc_clock_enable(clock
);
255 * Perform miscellaneous late
258 i
= sizeof(struct alarm
);
259 alarm_zone
= zinit(i
, (4096 / i
) * i
, 10 * i
, "alarms");
263 * Get the service port on a clock.
266 host_get_clock_service(
269 clock_t *clock
) /* OUT */
271 if (host
== HOST_NULL
|| clock_id
< 0 || clock_id
>= clock_count
) {
273 return KERN_INVALID_ARGUMENT
;
276 *clock
= &clock_list
[clock_id
];
277 if ((*clock
)->cl_ops
== 0) {
284 * Get the control port on a clock.
287 host_get_clock_control(
288 host_priv_t host_priv
,
290 clock_t *clock
) /* OUT */
292 if (host_priv
== HOST_PRIV_NULL
||
293 clock_id
< 0 || clock_id
>= clock_count
) {
295 return KERN_INVALID_ARGUMENT
;
298 *clock
= &clock_list
[clock_id
];
299 if ((*clock
)->cl_ops
== 0) {
306 * Get the current clock time.
311 mach_timespec_t
*cur_time
) /* OUT */
313 if (clock
== CLOCK_NULL
) {
314 return KERN_INVALID_ARGUMENT
;
316 return (*clock
->cl_ops
->c_gettime
)(cur_time
);
321 mach_timespec_t
*time
) /* OUT */
326 clock_get_system_nanotime(&secs
, &nsecs
);
327 time
->tv_sec
= (unsigned int)secs
;
328 time
->tv_nsec
= nsecs
;
335 mach_timespec_t
*time
) /* OUT */
340 clock_get_calendar_nanotime(&secs
, &nsecs
);
341 time
->tv_sec
= (unsigned int)secs
;
342 time
->tv_nsec
= nsecs
;
348 * Get clock attributes.
351 clock_get_attributes(
353 clock_flavor_t flavor
,
354 clock_attr_t attr
, /* OUT */
355 mach_msg_type_number_t
*count
) /* IN/OUT */
357 if (clock
== CLOCK_NULL
) {
358 return KERN_INVALID_ARGUMENT
;
360 if (clock
->cl_ops
->c_getattr
) {
361 return clock
->cl_ops
->c_getattr(flavor
, attr
, count
);
368 clock_flavor_t flavor
,
369 clock_attr_t attr
, /* OUT */
370 mach_msg_type_number_t
*count
) /* IN/OUT */
377 case CLOCK_GET_TIME_RES
: /* >0 res */
378 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
379 case CLOCK_ALARM_MINRES
:
380 case CLOCK_ALARM_MAXRES
:
381 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
385 return KERN_INVALID_VALUE
;
393 clock_flavor_t flavor
,
394 clock_attr_t attr
, /* OUT */
395 mach_msg_type_number_t
*count
) /* IN/OUT */
402 case CLOCK_GET_TIME_RES
: /* >0 res */
403 *(clock_res_t
*) attr
= NSEC_PER_SEC
/ 100;
406 case CLOCK_ALARM_CURRES
: /* =0 no alarm */
407 case CLOCK_ALARM_MINRES
:
408 case CLOCK_ALARM_MAXRES
:
409 *(clock_res_t
*) attr
= 0;
413 return KERN_INVALID_VALUE
;
420 * Set the current clock time.
425 __unused mach_timespec_t new_time
)
427 if (clock
== CLOCK_NULL
) {
428 return KERN_INVALID_ARGUMENT
;
434 * Set the clock alarm resolution.
437 clock_set_attributes(
439 __unused clock_flavor_t flavor
,
440 __unused clock_attr_t attr
,
441 __unused mach_msg_type_number_t count
)
443 if (clock
== CLOCK_NULL
) {
444 return KERN_INVALID_ARGUMENT
;
450 * Setup a clock alarm.
455 alarm_type_t alarm_type
,
456 mach_timespec_t alarm_time
,
457 ipc_port_t alarm_port
,
458 mach_msg_type_name_t alarm_port_type
)
461 mach_timespec_t clock_time
;
463 kern_return_t reply_code
;
466 if (clock
== CLOCK_NULL
) {
467 return KERN_INVALID_ARGUMENT
;
469 if (clock
!= &clock_list
[SYSTEM_CLOCK
]) {
472 if (IP_VALID(alarm_port
) == 0) {
473 return KERN_INVALID_CAPABILITY
;
477 * Check alarm parameters. If parameters are invalid,
478 * send alarm message immediately.
480 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
481 chkstat
= check_time(alarm_type
, &alarm_time
, &clock_time
);
483 reply_code
= (chkstat
< 0 ? KERN_INVALID_VALUE
: KERN_SUCCESS
);
484 clock_alarm_reply(alarm_port
, alarm_port_type
,
485 reply_code
, alarm_type
, clock_time
);
490 * Get alarm and add to clock alarm list.
494 if ((alarm
= alrmfree
) == 0) {
496 alarm
= (alarm_t
) zalloc(alarm_zone
);
498 return KERN_RESOURCE_SHORTAGE
;
502 alrmfree
= alarm
->al_next
;
505 alarm
->al_status
= ALARM_CLOCK
;
506 alarm
->al_time
= alarm_time
;
507 alarm
->al_type
= alarm_type
;
508 alarm
->al_port
= alarm_port
;
509 alarm
->al_port_type
= alarm_port_type
;
510 alarm
->al_clock
= clock
;
511 alarm
->al_seqno
= alrm_seqno
++;
519 * Sleep on a clock. System trap. User-level libmach clock_sleep
520 * interface call takes a mach_timespec_t sleep_time argument which it
521 * converts to sleep_sec and sleep_nsec arguments which are then
522 * passed to clock_sleep_trap.
526 struct clock_sleep_trap_args
*args
)
528 mach_port_name_t clock_name
= args
->clock_name
;
529 sleep_type_t sleep_type
= args
->sleep_type
;
530 int sleep_sec
= args
->sleep_sec
;
531 int sleep_nsec
= args
->sleep_nsec
;
532 mach_vm_address_t wakeup_time_addr
= args
->wakeup_time
;
534 mach_timespec_t swtime
= {};
535 kern_return_t rvalue
;
538 * Convert the trap parameters.
540 if (clock_name
== MACH_PORT_NULL
) {
541 clock
= &clock_list
[SYSTEM_CLOCK
];
543 clock
= port_name_to_clock(clock_name
);
546 swtime
.tv_sec
= sleep_sec
;
547 swtime
.tv_nsec
= sleep_nsec
;
550 * Call the actual clock_sleep routine.
552 rvalue
= clock_sleep_internal(clock
, sleep_type
, &swtime
);
555 * Return current time as wakeup time.
557 if (rvalue
!= KERN_INVALID_ARGUMENT
&& rvalue
!= KERN_FAILURE
) {
558 copyout((char *)&swtime
, wakeup_time_addr
, sizeof(mach_timespec_t
));
564 clock_sleep_internal(
566 sleep_type_t sleep_type
,
567 mach_timespec_t
*sleep_time
)
570 mach_timespec_t clock_time
;
571 kern_return_t rvalue
;
575 if (clock
== CLOCK_NULL
) {
576 return KERN_INVALID_ARGUMENT
;
579 if (clock
!= &clock_list
[SYSTEM_CLOCK
]) {
584 * Check sleep parameters. If parameters are invalid
585 * return an error, otherwise post alarm request.
587 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
589 chkstat
= check_time(sleep_type
, sleep_time
, &clock_time
);
591 return KERN_INVALID_VALUE
;
593 rvalue
= KERN_SUCCESS
;
595 wait_result_t wait_result
;
598 * Get alarm and add to clock alarm list.
602 if ((alarm
= alrmfree
) == 0) {
604 alarm
= (alarm_t
) zalloc(alarm_zone
);
606 return KERN_RESOURCE_SHORTAGE
;
610 alrmfree
= alarm
->al_next
;
614 * Wait for alarm to occur.
616 wait_result
= assert_wait((event_t
)alarm
, THREAD_ABORTSAFE
);
617 if (wait_result
== THREAD_WAITING
) {
618 alarm
->al_time
= *sleep_time
;
619 alarm
->al_status
= ALARM_SLEEP
;
623 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
626 * Note if alarm expired normally or whether it
627 * was aborted. If aborted, delete alarm from
628 * clock alarm list. Return alarm to free list.
631 if (alarm
->al_status
!= ALARM_DONE
) {
632 assert(wait_result
!= THREAD_AWAKENED
);
633 if (((alarm
->al_prev
)->al_next
= alarm
->al_next
) != NULL
) {
634 (alarm
->al_next
)->al_prev
= alarm
->al_prev
;
636 rvalue
= KERN_ABORTED
;
638 *sleep_time
= alarm
->al_time
;
639 alarm
->al_status
= ALARM_FREE
;
641 assert(wait_result
== THREAD_INTERRUPTED
);
642 assert(alarm
->al_status
== ALARM_FREE
);
643 rvalue
= KERN_ABORTED
;
645 alarm
->al_next
= alrmfree
;
649 *sleep_time
= clock_time
;
656 * Service clock alarm expirations.
664 mach_timespec_t clock_time
;
665 mach_timespec_t
*alarm_time
;
668 clock
= &clock_list
[SYSTEM_CLOCK
];
669 (*clock
->cl_ops
->c_gettime
)(&clock_time
);
672 * Update clock alarm list. Alarms that are due are moved
673 * to the alarmdone list to be serviced by a thread callout.
676 alrm1
= (alarm_t
)&alrmlist
;
677 while ((alrm2
= alrm1
->al_next
) != NULL
) {
678 alarm_time
= &alrm2
->al_time
;
679 if (CMP_MACH_TIMESPEC(alarm_time
, &clock_time
) > 0) {
684 * Alarm has expired, so remove it from the
687 if ((alrm1
->al_next
= alrm2
->al_next
) != NULL
) {
688 (alrm1
->al_next
)->al_prev
= alrm1
;
692 * If a clock_sleep() alarm, wakeup the thread
693 * which issued the clock_sleep() call.
695 if (alrm2
->al_status
== ALARM_SLEEP
) {
696 alrm2
->al_next
= NULL
;
697 alrm2
->al_status
= ALARM_DONE
;
698 alrm2
->al_time
= clock_time
;
699 thread_wakeup((event_t
)alrm2
);
702 * If a clock_alarm() alarm, place the alarm on
703 * the alarm done list and schedule the alarm
704 * delivery mechanism.
707 assert(alrm2
->al_status
== ALARM_CLOCK
);
708 if ((alrm2
->al_next
= alrmdone
) != NULL
) {
709 alrmdone
->al_prev
= alrm2
;
711 thread_call_enter(&alarm_done_call
);
713 alrm2
->al_prev
= (alarm_t
)&alrmdone
;
715 alrm2
->al_status
= ALARM_DONE
;
716 alrm2
->al_time
= clock_time
;
721 * Setup to expire for the next pending alarm.
724 set_alarm(alarm_time
);
737 while ((alrm
= alrmdone
) != NULL
) {
738 if ((alrmdone
= alrm
->al_next
) != NULL
) {
739 alrmdone
->al_prev
= (alarm_t
)&alrmdone
;
743 code
= (alrm
->al_status
== ALARM_DONE
? KERN_SUCCESS
: KERN_ABORTED
);
744 if (alrm
->al_port
!= IP_NULL
) {
745 /* Deliver message to designated port */
746 if (IP_VALID(alrm
->al_port
)) {
747 clock_alarm_reply(alrm
->al_port
, alrm
->al_port_type
, code
,
748 alrm
->al_type
, alrm
->al_time
);
752 alrm
->al_status
= ALARM_FREE
;
753 alrm
->al_next
= alrmfree
;
756 panic("clock_alarm_deliver");
764 * Post an alarm on the active alarm list.
766 * Always called from within a LOCK_ALARM() code section.
772 alarm_t alrm1
, alrm2
;
773 mach_timespec_t
*alarm_time
;
774 mach_timespec_t
*queue_time
;
777 * Traverse alarm list until queue time is greater
778 * than alarm time, then insert alarm.
780 alarm_time
= &alarm
->al_time
;
781 alrm1
= (alarm_t
)&alrmlist
;
782 while ((alrm2
= alrm1
->al_next
) != NULL
) {
783 queue_time
= &alrm2
->al_time
;
784 if (CMP_MACH_TIMESPEC(queue_time
, alarm_time
) > 0) {
789 alrm1
->al_next
= alarm
;
790 alarm
->al_next
= alrm2
;
791 alarm
->al_prev
= alrm1
;
793 alrm2
->al_prev
= alarm
;
797 * If the inserted alarm is the 'earliest' alarm,
798 * reset the device layer alarm time accordingly.
800 if (alrmlist
== alarm
) {
801 set_alarm(alarm_time
);
807 mach_timespec_t
*alarm_time
)
811 nanotime_to_absolutetime(alarm_time
->tv_sec
, alarm_time
->tv_nsec
, &abstime
);
812 timer_call_enter_with_leeway(&alarm_expire_timer
, NULL
, abstime
, 0, TIMER_CALL_USER_NORMAL
, FALSE
);
816 * Check the validity of 'alarm_time' and 'alarm_type'. If either
817 * argument is invalid, return a negative value. If the 'alarm_time'
818 * is now, return a 0 value. If the 'alarm_time' is in the future,
819 * return a positive value.
823 alarm_type_t alarm_type
,
824 mach_timespec_t
*alarm_time
,
825 mach_timespec_t
*clock_time
)
829 if (BAD_ALRMTYPE(alarm_type
)) {
832 if (BAD_MACH_TIMESPEC(alarm_time
)) {
835 if ((alarm_type
& ALRMTYPE
) == TIME_RELATIVE
) {
836 ADD_MACH_TIMESPEC(alarm_time
, clock_time
);
839 result
= CMP_MACH_TIMESPEC(alarm_time
, clock_time
);
841 return (result
>= 0)? result
: 0;
847 clock_get_system_value(void)
849 clock_t clock
= &clock_list
[SYSTEM_CLOCK
];
850 mach_timespec_t value
;
852 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
858 clock_get_calendar_value(void)
860 clock_t clock
= &clock_list
[CALENDAR_CLOCK
];
861 mach_timespec_t value
= MACH_TIMESPEC_ZERO
;
863 (void) (*clock
->cl_ops
->c_gettime
)(&value
);
868 #endif /* __LP64__ */