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/mk_timer.h>
46 #include <kern/thread_call.h>
48 static zone_t mk_timer_zone
;
50 static mach_port_qos_t mk_timer_qos
= {
51 FALSE
, TRUE
, 0, sizeof (mk_timer_expire_msg_t
)
54 static void mk_timer_expire(
60 __unused
struct mk_timer_create_trap_args
*args
)
63 ipc_space_t myspace
= current_space();
64 mach_port_name_t name
= MACH_PORT_NULL
;
68 timer
= (mk_timer_t
)zalloc(mk_timer_zone
);
70 return (MACH_PORT_NULL
);
72 result
= mach_port_allocate_qos(myspace
, MACH_PORT_RIGHT_RECEIVE
,
73 &mk_timer_qos
, &name
);
74 if (result
== KERN_SUCCESS
)
75 result
= ipc_port_translate_receive(myspace
, name
, &port
);
77 if (result
!= KERN_SUCCESS
) {
78 zfree(mk_timer_zone
, timer
);
80 return (MACH_PORT_NULL
);
83 simple_lock_init(&timer
->lock
, 0);
84 thread_call_setup(&timer
->call_entry
, mk_timer_expire
, timer
);
85 timer
->is_armed
= timer
->is_dead
= FALSE
;
89 ipc_kobject_set_atomically(port
, (ipc_kobject_t
)timer
, IKOT_TIMER
);
99 mk_timer_port_destroy(
102 mk_timer_t timer
= NULL
;
105 if (ip_kotype(port
) == IKOT_TIMER
) {
106 timer
= (mk_timer_t
)port
->ip_kobject
;
107 assert(timer
!= NULL
);
108 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
109 simple_lock(&timer
->lock
);
110 assert(timer
->port
== port
);
115 if (thread_call_cancel(&timer
->call_entry
))
117 timer
->is_armed
= FALSE
;
119 timer
->is_dead
= TRUE
;
120 if (timer
->active
== 0) {
121 simple_unlock(&timer
->lock
);
122 zfree(mk_timer_zone
, timer
);
124 ipc_port_release_send(port
);
128 simple_unlock(&timer
->lock
);
135 int s
= sizeof (mk_timer_data_t
);
137 assert(!(mk_timer_zone
!= NULL
));
139 mk_timer_zone
= zinit(s
, (4096 * s
), (16 * s
), "mk_timer");
141 zone_change(mk_timer_zone
, Z_NOENCRYPT
, TRUE
);
149 mk_timer_t timer
= p0
;
152 simple_lock(&timer
->lock
);
154 if (timer
->active
> 1) {
156 simple_unlock(&timer
->lock
);
161 assert(port
!= IP_NULL
);
162 assert(timer
->active
== 1);
164 while (timer
->is_armed
&& timer
->active
== 1) {
165 mk_timer_expire_msg_t msg
;
167 timer
->is_armed
= FALSE
;
168 simple_unlock(&timer
->lock
);
170 msg
.header
.msgh_bits
=
171 MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND
, 0, 0, 0);
172 msg
.header
.msgh_remote_port
= port
;
173 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
174 msg
.header
.msgh_voucher_port
= MACH_PORT_NULL
;
175 msg
.header
.msgh_id
= 0;
177 msg
.unused
[0] = msg
.unused
[1] = msg
.unused
[2] = 0;
179 (void) mach_msg_send_from_kernel_proper(&msg
.header
, sizeof (msg
));
181 simple_lock(&timer
->lock
);
184 if (--timer
->active
== 0 && timer
->is_dead
) {
185 simple_unlock(&timer
->lock
);
186 zfree(mk_timer_zone
, timer
);
188 ipc_port_release_send(port
);
192 simple_unlock(&timer
->lock
);
196 * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
198 * Parameters: args User argument descriptor (see below)
200 * Indirect: args->name Mach port name
208 mk_timer_destroy_trap(
209 struct mk_timer_destroy_trap_args
*args
)
211 mach_port_name_t name
= args
->name
;
212 ipc_space_t myspace
= current_space();
214 kern_return_t result
;
216 result
= ipc_port_translate_receive(myspace
, name
, &port
);
217 if (result
!= KERN_SUCCESS
)
220 if (ip_kotype(port
) == IKOT_TIMER
) {
222 result
= mach_port_destroy(myspace
, name
);
226 result
= KERN_INVALID_ARGUMENT
;
233 * mk_timer_arm_trap: Start (arm) a timer
235 * Parameters: args User argument descriptor (see below)
237 * Indirect: args->name Mach port name
238 * args->expire_time Time when timer expires
247 mk_timer_arm_trap_internal(mach_port_name_t name
, uint64_t expire_time
, uint64_t mk_leeway
, uint64_t mk_timer_flags
) {
249 ipc_space_t myspace
= current_space();
251 kern_return_t result
;
253 result
= ipc_port_translate_receive(myspace
, name
, &port
);
254 if (result
!= KERN_SUCCESS
)
257 if (ip_kotype(port
) == IKOT_TIMER
) {
258 timer
= (mk_timer_t
)port
->ip_kobject
;
259 assert(timer
!= NULL
);
261 simple_lock(&timer
->lock
);
262 assert(timer
->port
== port
);
265 if (!timer
->is_dead
) {
266 timer
->is_armed
= TRUE
;
268 if (expire_time
> mach_absolute_time()) {
269 uint32_t tcflags
= THREAD_CALL_DELAY_USER_NORMAL
;
271 if (mk_timer_flags
& MK_TIMER_CRITICAL
) {
272 tcflags
= THREAD_CALL_DELAY_USER_CRITICAL
;
275 if (mk_leeway
!= 0) {
276 tcflags
|= THREAD_CALL_DELAY_LEEWAY
;
279 if (!thread_call_enter_delayed_with_leeway(
280 &timer
->call_entry
, NULL
,
281 expire_time
, mk_leeway
, tcflags
)) {
286 if (!thread_call_enter1(&timer
->call_entry
, NULL
))
291 simple_unlock(&timer
->lock
);
294 result
= KERN_INVALID_ARGUMENT
;
300 mk_timer_arm_trap(struct mk_timer_arm_trap_args
*args
) {
301 return mk_timer_arm_trap_internal(args
->name
, args
->expire_time
, 0, MK_TIMER_NORMAL
);
305 mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args
*args
) {
306 return mk_timer_arm_trap_internal(args
->name
, args
->expire_time
, args
->mk_leeway
, args
->mk_timer_flags
);
310 * mk_timer_cancel_trap: Cancel a timer
312 * Parameters: args User argument descriptor (see below)
314 * Indirect: args->name Mach port name
315 * args->result_time The armed time of the cancelled timer (return value)
323 mk_timer_cancel_trap(
324 struct mk_timer_cancel_trap_args
*args
)
326 mach_port_name_t name
= args
->name
;
327 mach_vm_address_t result_time_addr
= args
->result_time
;
328 uint64_t armed_time
= 0;
330 ipc_space_t myspace
= current_space();
332 kern_return_t result
;
334 result
= ipc_port_translate_receive(myspace
, name
, &port
);
335 if (result
!= KERN_SUCCESS
)
338 if (ip_kotype(port
) == IKOT_TIMER
) {
339 timer
= (mk_timer_t
)port
->ip_kobject
;
340 assert(timer
!= NULL
);
341 simple_lock(&timer
->lock
);
342 assert(timer
->port
== port
);
345 if (timer
->is_armed
) {
346 armed_time
= timer
->call_entry
.tc_call
.deadline
;
347 if (thread_call_cancel(&timer
->call_entry
))
349 timer
->is_armed
= FALSE
;
352 simple_unlock(&timer
->lock
);
356 result
= KERN_INVALID_ARGUMENT
;
359 if (result
== KERN_SUCCESS
)
360 if ( result_time_addr
!= 0 &&
361 copyout((void *)&armed_time
, result_time_addr
,
362 sizeof (armed_time
)) != 0 )
363 result
= KERN_FAILURE
;