2 * Copyright (c) 2000-2004 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>
49 static zone_t mk_timer_zone
;
51 static mach_port_qos_t mk_timer_qos
= {
54 .len
= sizeof(mk_timer_expire_msg_t
),
57 static void mk_timer_expire(
63 __unused
struct mk_timer_create_trap_args
*args
)
66 ipc_space_t myspace
= current_space();
67 mach_port_name_t name
= MACH_PORT_NULL
;
71 timer
= (mk_timer_t
)zalloc(mk_timer_zone
);
73 return MACH_PORT_NULL
;
76 result
= mach_port_allocate_internal(myspace
, MACH_PORT_RIGHT_RECEIVE
,
77 &mk_timer_qos
, &name
);
78 if (result
== KERN_SUCCESS
) {
79 result
= ipc_port_translate_receive(myspace
, name
, &port
);
82 if (result
!= KERN_SUCCESS
) {
83 zfree(mk_timer_zone
, timer
);
85 return MACH_PORT_NULL
;
88 simple_lock_init(&timer
->lock
, 0);
89 thread_call_setup(&timer
->call_entry
, mk_timer_expire
, timer
);
90 timer
->is_armed
= timer
->is_dead
= FALSE
;
94 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)timer
, IKOT_TIMER
);
104 mk_timer_port_destroy(
107 mk_timer_t timer
= NULL
;
110 if (ip_kotype(port
) == IKOT_TIMER
) {
111 timer
= (mk_timer_t
)port
->ip_kobject
;
112 assert(timer
!= NULL
);
113 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
114 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
115 assert(timer
->port
== port
);
120 if (thread_call_cancel(&timer
->call_entry
)) {
123 timer
->is_armed
= FALSE
;
125 timer
->is_dead
= TRUE
;
126 if (timer
->active
== 0) {
127 simple_unlock(&timer
->lock
);
128 zfree(mk_timer_zone
, timer
);
130 ipc_port_release_send(port
);
134 simple_unlock(&timer
->lock
);
141 int s
= sizeof(mk_timer_data_t
);
143 assert(!(mk_timer_zone
!= NULL
));
145 mk_timer_zone
= zinit(s
, (4096 * s
), (16 * s
), "mk_timer");
147 zone_change(mk_timer_zone
, Z_NOENCRYPT
, TRUE
);
155 mk_timer_t timer
= p0
;
158 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
160 if (timer
->active
> 1) {
162 simple_unlock(&timer
->lock
);
167 assert(port
!= IP_NULL
);
168 assert(timer
->active
== 1);
170 while (timer
->is_armed
&& timer
->active
== 1) {
171 mk_timer_expire_msg_t msg
;
173 timer
->is_armed
= FALSE
;
174 simple_unlock(&timer
->lock
);
176 msg
.header
.msgh_bits
=
177 MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND
, 0, 0, 0);
178 msg
.header
.msgh_remote_port
= port
;
179 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
180 msg
.header
.msgh_voucher_port
= MACH_PORT_NULL
;
181 msg
.header
.msgh_id
= 0;
183 msg
.unused
[0] = msg
.unused
[1] = msg
.unused
[2] = 0;
185 (void) mach_msg_send_from_kernel_proper(&msg
.header
, sizeof(msg
));
187 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
190 if (--timer
->active
== 0 && timer
->is_dead
) {
191 simple_unlock(&timer
->lock
);
192 zfree(mk_timer_zone
, timer
);
194 ipc_port_release_send(port
);
198 simple_unlock(&timer
->lock
);
202 * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
204 * Parameters: args User argument descriptor (see below)
206 * Indirect: args->name Mach port name
214 mk_timer_destroy_trap(
215 struct mk_timer_destroy_trap_args
*args
)
217 mach_port_name_t name
= args
->name
;
218 ipc_space_t myspace
= current_space();
220 kern_return_t result
;
222 result
= ipc_port_translate_receive(myspace
, name
, &port
);
223 if (result
!= KERN_SUCCESS
) {
227 if (ip_kotype(port
) == IKOT_TIMER
) {
229 result
= mach_port_destroy(myspace
, name
);
232 result
= KERN_INVALID_ARGUMENT
;
239 * mk_timer_arm_trap: Start (arm) a timer
241 * Parameters: args User argument descriptor (see below)
243 * Indirect: args->name Mach port name
244 * args->expire_time Time when timer expires
253 mk_timer_arm_trap_internal(mach_port_name_t name
, uint64_t expire_time
, uint64_t mk_leeway
, uint64_t mk_timer_flags
)
256 ipc_space_t myspace
= current_space();
258 kern_return_t result
;
260 result
= ipc_port_translate_receive(myspace
, name
, &port
);
261 if (result
!= KERN_SUCCESS
) {
265 if (ip_kotype(port
) == IKOT_TIMER
) {
266 timer
= (mk_timer_t
)port
->ip_kobject
;
267 assert(timer
!= NULL
);
269 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
270 assert(timer
->port
== port
);
273 if (!timer
->is_dead
) {
274 timer
->is_armed
= TRUE
;
276 if (expire_time
> mach_absolute_time()) {
277 uint32_t tcflags
= THREAD_CALL_DELAY_USER_NORMAL
;
279 if (mk_timer_flags
& MK_TIMER_CRITICAL
) {
280 tcflags
= THREAD_CALL_DELAY_USER_CRITICAL
;
283 if (mk_leeway
!= 0) {
284 tcflags
|= THREAD_CALL_DELAY_LEEWAY
;
287 if (!thread_call_enter_delayed_with_leeway(
288 &timer
->call_entry
, NULL
,
289 expire_time
, mk_leeway
, tcflags
)) {
293 if (!thread_call_enter1(&timer
->call_entry
, NULL
)) {
299 simple_unlock(&timer
->lock
);
302 result
= KERN_INVALID_ARGUMENT
;
308 mk_timer_arm_trap(struct mk_timer_arm_trap_args
*args
)
310 return mk_timer_arm_trap_internal(args
->name
, args
->expire_time
, 0, MK_TIMER_NORMAL
);
314 mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args
*args
)
316 return mk_timer_arm_trap_internal(args
->name
, args
->expire_time
, args
->mk_leeway
, args
->mk_timer_flags
);
320 * mk_timer_cancel_trap: Cancel a timer
322 * Parameters: args User argument descriptor (see below)
324 * Indirect: args->name Mach port name
325 * args->result_time The armed time of the cancelled timer (return value)
333 mk_timer_cancel_trap(
334 struct mk_timer_cancel_trap_args
*args
)
336 mach_port_name_t name
= args
->name
;
337 mach_vm_address_t result_time_addr
= args
->result_time
;
338 uint64_t armed_time
= 0;
340 ipc_space_t myspace
= current_space();
342 kern_return_t result
;
344 result
= ipc_port_translate_receive(myspace
, name
, &port
);
345 if (result
!= KERN_SUCCESS
) {
349 if (ip_kotype(port
) == IKOT_TIMER
) {
350 timer
= (mk_timer_t
)port
->ip_kobject
;
351 assert(timer
!= NULL
);
352 simple_lock(&timer
->lock
, LCK_GRP_NULL
);
353 assert(timer
->port
== port
);
356 if (timer
->is_armed
) {
357 armed_time
= timer
->call_entry
.tc_call
.deadline
;
358 if (thread_call_cancel(&timer
->call_entry
)) {
361 timer
->is_armed
= FALSE
;
364 simple_unlock(&timer
->lock
);
367 result
= KERN_INVALID_ARGUMENT
;
370 if (result
== KERN_SUCCESS
) {
371 if (result_time_addr
!= 0 &&
372 copyout((void *)&armed_time
, result_time_addr
,
373 sizeof(armed_time
)) != 0) {
374 result
= KERN_FAILURE
;