2 * Copyright (c) 2000-2020 Apple Computer, 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@
29 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
37 #include <mach/mach_types.h>
38 #include <mach/mach_traps.h>
39 #include <mach/mach_port_server.h>
41 #include <mach/mk_timer.h>
43 #include <ipc/ipc_space.h>
45 #include <kern/lock_group.h>
46 #include <kern/mk_timer.h>
47 #include <kern/thread_call.h>
48 #include <ipc/ipc_kmsg.h>
51 decl_simple_lock_data(, lock
);
52 thread_call_data_t mkt_thread_call
;
59 static ZONE_DECLARE(mk_timer_zone
, "mk_timer",
60 sizeof(struct mk_timer
), ZC_NOENCRYPT
);
62 static mach_port_qos_t mk_timer_qos
= {
65 .len
= sizeof(mk_timer_expire_msg_t
),
68 static void mk_timer_expire(
74 __unused
struct mk_timer_create_trap_args
*args
)
76 struct mk_timer
* timer
;
77 ipc_space_t myspace
= current_space();
78 mach_port_name_t name
= MACH_PORT_NULL
;
79 ipc_port_init_flags_t init_flags
;
83 /* Allocate and initialize local state of a timer object */
84 timer
= (struct mk_timer
*)zalloc(mk_timer_zone
);
86 return MACH_PORT_NULL
;
88 simple_lock_init(&timer
->lock
, 0);
89 thread_call_setup(&timer
->mkt_thread_call
, mk_timer_expire
, timer
);
90 timer
->is_armed
= timer
->is_dead
= FALSE
;
93 /* Pre-allocate a kmsg for the timer messages */
95 kmsg
= ipc_kmsg_prealloc(mk_timer_qos
.len
+ MAX_TRAILER_SIZE
);
96 if (kmsg
== IKM_NULL
) {
97 zfree(mk_timer_zone
, timer
);
98 return MACH_PORT_NULL
;
101 init_flags
= IPC_PORT_INIT_MESSAGE_QUEUE
;
102 result
= ipc_port_alloc(myspace
, init_flags
, &name
, &port
);
103 if (result
!= KERN_SUCCESS
) {
104 zfree(mk_timer_zone
, timer
);
106 return MACH_PORT_NULL
;
109 /* Associate the pre-allocated kmsg with the port */
110 ipc_kmsg_set_prealloc(kmsg
, port
);
112 /* port locked, receive right at user-space */
113 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)timer
, IKOT_TIMER
);
115 /* make a (naked) send right for the timer to keep */
116 timer
->port
= ipc_port_make_send_locked(port
);
124 mk_timer_port_destroy(
127 struct mk_timer
* timer
= NULL
;
130 if (ip_kotype(port
) == IKOT_TIMER
) {
131 timer
= (struct mk_timer
*) ip_get_kobject(port
);
132 assert(timer
!= NULL
);
133 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
134 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
135 assert(timer
->port
== port
);
140 if (thread_call_cancel(&timer
->mkt_thread_call
)) {
143 timer
->is_armed
= FALSE
;
145 timer
->is_dead
= TRUE
;
146 if (timer
->active
== 0) {
147 simple_unlock(&timer
->lock
);
148 zfree(mk_timer_zone
, timer
);
150 ipc_port_release_send(port
);
154 simple_unlock(&timer
->lock
);
163 struct mk_timer
* timer
= p0
;
165 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
167 if (timer
->active
> 1) {
169 simple_unlock(&timer
->lock
);
173 ipc_port_t port
= timer
->port
;
174 assert(port
!= IP_NULL
);
175 assert(timer
->active
== 1);
177 while (timer
->is_armed
&& timer
->active
== 1) {
178 mk_timer_expire_msg_t msg
;
180 timer
->is_armed
= FALSE
;
181 simple_unlock(&timer
->lock
);
183 msg
.header
.msgh_bits
=
184 MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND
, 0, 0, 0);
185 msg
.header
.msgh_remote_port
= port
;
186 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
187 msg
.header
.msgh_voucher_port
= MACH_PORT_NULL
;
188 msg
.header
.msgh_id
= 0;
190 msg
.unused
[0] = msg
.unused
[1] = msg
.unused
[2] = 0;
192 (void) mach_msg_send_from_kernel_proper(&msg
.header
, sizeof(msg
));
194 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
197 if (--timer
->active
== 0 && timer
->is_dead
) {
198 simple_unlock(&timer
->lock
);
199 zfree(mk_timer_zone
, timer
);
201 ipc_port_release_send(port
);
205 simple_unlock(&timer
->lock
);
209 * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
211 * Parameters: args User argument descriptor (see below)
213 * Indirect: args->name Mach port name
221 mk_timer_destroy_trap(
222 struct mk_timer_destroy_trap_args
*args
)
224 mach_port_name_t name
= args
->name
;
225 ipc_space_t myspace
= current_space();
227 kern_return_t result
;
229 result
= ipc_port_translate_receive(myspace
, name
, &port
);
230 if (result
!= KERN_SUCCESS
) {
234 if (ip_kotype(port
) == IKOT_TIMER
) {
236 /* TODO: this should be mach_port_mod_refs */
237 result
= mach_port_destroy(myspace
, name
);
240 result
= KERN_INVALID_ARGUMENT
;
247 * mk_timer_arm_trap: Start (arm) a timer
249 * Parameters: args User argument descriptor (see below)
251 * Indirect: args->name Mach port name
252 * args->expire_time Time when timer expires
261 mk_timer_arm_trap_internal(mach_port_name_t name
, uint64_t expire_time
, uint64_t mk_leeway
, uint64_t mk_timer_flags
)
263 struct mk_timer
* timer
;
264 ipc_space_t myspace
= current_space();
266 kern_return_t result
;
268 result
= ipc_port_translate_receive(myspace
, name
, &port
);
269 if (result
!= KERN_SUCCESS
) {
273 if (ip_kotype(port
) == IKOT_TIMER
) {
275 timer
= (struct mk_timer
*) ip_get_kobject(port
);
276 assert(timer
!= NULL
);
278 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
279 assert(timer
->port
== port
);
282 if (!timer
->is_dead
) {
283 timer
->is_armed
= TRUE
;
285 if (expire_time
> mach_absolute_time()) {
286 uint32_t tcflags
= THREAD_CALL_DELAY_USER_NORMAL
;
288 if (mk_timer_flags
& MK_TIMER_CRITICAL
) {
289 tcflags
= THREAD_CALL_DELAY_USER_CRITICAL
;
292 if (mk_leeway
!= 0) {
293 tcflags
|= THREAD_CALL_DELAY_LEEWAY
;
296 if (!thread_call_enter_delayed_with_leeway(
297 &timer
->mkt_thread_call
, NULL
,
298 expire_time
, mk_leeway
, tcflags
)) {
302 if (!thread_call_enter1(&timer
->mkt_thread_call
, NULL
)) {
308 simple_unlock(&timer
->lock
);
311 result
= KERN_INVALID_ARGUMENT
;
317 mk_timer_arm_trap(struct mk_timer_arm_trap_args
*args
)
319 return mk_timer_arm_trap_internal(args
->name
, args
->expire_time
, 0, MK_TIMER_NORMAL
);
323 mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args
*args
)
325 return mk_timer_arm_trap_internal(args
->name
, args
->expire_time
, args
->mk_leeway
, args
->mk_timer_flags
);
329 * mk_timer_cancel_trap: Cancel a timer
331 * Parameters: args User argument descriptor (see below)
333 * Indirect: args->name Mach port name
334 * args->result_time The armed time of the cancelled timer (return value)
342 mk_timer_cancel_trap(
343 struct mk_timer_cancel_trap_args
*args
)
345 mach_port_name_t name
= args
->name
;
346 mach_vm_address_t result_time_addr
= args
->result_time
;
347 uint64_t armed_time
= 0;
348 struct mk_timer
* timer
;
349 ipc_space_t myspace
= current_space();
351 kern_return_t result
;
353 result
= ipc_port_translate_receive(myspace
, name
, &port
);
354 if (result
!= KERN_SUCCESS
) {
358 if (ip_kotype(port
) == IKOT_TIMER
) {
359 timer
= (struct mk_timer
*) ip_get_kobject(port
);
360 assert(timer
!= NULL
);
361 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
362 assert(timer
->port
== port
);
365 if (timer
->is_armed
) {
366 armed_time
= thread_call_get_armed_deadline(&timer
->mkt_thread_call
);
367 if (thread_call_cancel(&timer
->mkt_thread_call
)) {
370 timer
->is_armed
= FALSE
;
373 simple_unlock(&timer
->lock
);
376 result
= KERN_INVALID_ARGUMENT
;
379 if (result
== KERN_SUCCESS
&& result_time_addr
!= 0) {
380 if (copyout((void *)&armed_time
, result_time_addr
, sizeof(armed_time
)) != 0) {
381 result
= KERN_FAILURE
;