]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/mk_timer.c
xnu-2422.100.13.tar.gz
[apple/xnu.git] / osfmk / kern / mk_timer.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
30 *
31 * HISTORY
32 *
33 * 29 June 2000 (debo)
34 * Created.
35 */
36
37#include <mach/mach_types.h>
91447636 38#include <mach/mach_traps.h>
1c79356b
A
39#include <mach/mach_port_server.h>
40
41#include <mach/mk_timer.h>
42
43#include <ipc/ipc_space.h>
44
45#include <kern/mk_timer.h>
46#include <kern/thread_call.h>
47
48static zone_t mk_timer_zone;
49
50static mach_port_qos_t mk_timer_qos = {
51 FALSE, TRUE, 0, sizeof (mk_timer_expire_msg_t)
52};
53
54static void mk_timer_expire(
55 void *p0,
56 void *p1);
57
58mach_port_name_t
91447636
A
59mk_timer_create_trap(
60 __unused struct mk_timer_create_trap_args *args)
1c79356b
A
61{
62 mk_timer_t timer;
63 ipc_space_t myspace = current_space();
64 mach_port_name_t name = MACH_PORT_NULL;
65 ipc_port_t port;
66 kern_return_t result;
67
68 timer = (mk_timer_t)zalloc(mk_timer_zone);
69 if (timer == NULL)
70 return (MACH_PORT_NULL);
71
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);
76
77 if (result != KERN_SUCCESS) {
91447636 78 zfree(mk_timer_zone, timer);
1c79356b
A
79
80 return (MACH_PORT_NULL);
81 }
82
91447636 83 simple_lock_init(&timer->lock, 0);
316670eb 84 thread_call_setup(&timer->call_entry, mk_timer_expire, timer);
1c79356b
A
85 timer->is_armed = timer->is_dead = FALSE;
86 timer->active = 0;
87
88 timer->port = port;
89 ipc_kobject_set_atomically(port, (ipc_kobject_t)timer, IKOT_TIMER);
90
91 port->ip_srights++;
92 ip_reference(port);
93 ip_unlock(port);
94
95 return (name);
96}
97
98void
99mk_timer_port_destroy(
100 ipc_port_t port)
101{
102 mk_timer_t timer = NULL;
103
104 ip_lock(port);
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);
111 }
112 ip_unlock(port);
113
114 if (timer != NULL) {
115 if (thread_call_cancel(&timer->call_entry))
116 timer->active--;
117 timer->is_armed = FALSE;
118
119 timer->is_dead = TRUE;
120 if (timer->active == 0) {
121 simple_unlock(&timer->lock);
91447636 122 zfree(mk_timer_zone, timer);
1c79356b
A
123
124 ipc_port_release_send(port);
125 return;
126 }
127
128 simple_unlock(&timer->lock);
129 }
130}
131
132void
55e303ae 133mk_timer_init(void)
1c79356b
A
134{
135 int s = sizeof (mk_timer_data_t);
136
137 assert(!(mk_timer_zone != NULL));
138
139 mk_timer_zone = zinit(s, (4096 * s), (16 * s), "mk_timer");
0b4c1975
A
140
141 zone_change(mk_timer_zone, Z_NOENCRYPT, TRUE);
1c79356b
A
142}
143
144static void
145mk_timer_expire(
146 void *p0,
91447636 147 __unused void *p1)
1c79356b 148{
1c79356b
A
149 mk_timer_t timer = p0;
150 ipc_port_t port;
151
1c79356b
A
152 simple_lock(&timer->lock);
153
154 if (timer->active > 1) {
155 timer->active--;
156 simple_unlock(&timer->lock);
157 return;
158 }
159
160 port = timer->port;
161 assert(port != IP_NULL);
55e303ae 162 assert(timer->active == 1);
1c79356b 163
55e303ae 164 while (timer->is_armed && timer->active == 1) {
1c79356b
A
165 mk_timer_expire_msg_t msg;
166
167 timer->is_armed = FALSE;
1c79356b
A
168 simple_unlock(&timer->lock);
169
170 msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
171 msg.header.msgh_remote_port = port;
172 msg.header.msgh_local_port = MACH_PORT_NULL;
173 msg.header.msgh_reserved = msg.header.msgh_id = 0;
174
55e303ae
A
175 msg.unused[0] = msg.unused[1] = msg.unused[2] = 0;
176
b0d623f7 177 (void) mach_msg_send_from_kernel_proper(&msg.header, sizeof (msg));
1c79356b
A
178
179 simple_lock(&timer->lock);
180 }
181
182 if (--timer->active == 0 && timer->is_dead) {
183 simple_unlock(&timer->lock);
91447636 184 zfree(mk_timer_zone, timer);
1c79356b
A
185
186 ipc_port_release_send(port);
187 return;
188 }
189
190 simple_unlock(&timer->lock);
191}
192
316670eb
A
193/*
194 * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
195 *
196 * Parameters: args User argument descriptor (see below)
197 *
198 * Indirect: args->name Mach port name
199 *
200 *
201 * Returns: 0 Success
202 * !0 Not success
203 *
204 */
1c79356b 205kern_return_t
91447636
A
206mk_timer_destroy_trap(
207 struct mk_timer_destroy_trap_args *args)
1c79356b 208{
91447636 209 mach_port_name_t name = args->name;
1c79356b
A
210 ipc_space_t myspace = current_space();
211 ipc_port_t port;
212 kern_return_t result;
213
214 result = ipc_port_translate_receive(myspace, name, &port);
215 if (result != KERN_SUCCESS)
216 return (result);
217
218 if (ip_kotype(port) == IKOT_TIMER) {
219 ip_unlock(port);
220 result = mach_port_destroy(myspace, name);
221 }
222 else {
223 ip_unlock(port);
224 result = KERN_INVALID_ARGUMENT;
225 }
226
227 return (result);
228}
229
316670eb
A
230/*
231 * mk_timer_arm_trap: Start (arm) a timer
232 *
233 * Parameters: args User argument descriptor (see below)
234 *
235 * Indirect: args->name Mach port name
236 * args->expire_time Time when timer expires
237 *
238 *
239 * Returns: 0 Success
240 * !0 Not success
241 *
242 */
1c79356b 243kern_return_t
91447636
A
244mk_timer_arm_trap(
245 struct mk_timer_arm_trap_args *args)
1c79356b 246{
39236c6e 247 mach_port_name_t name = args->name;
91447636 248 uint64_t expire_time = args->expire_time;
1c79356b
A
249 mk_timer_t timer;
250 ipc_space_t myspace = current_space();
251 ipc_port_t port;
39236c6e 252 kern_return_t result;
1c79356b 253
1c79356b
A
254 result = ipc_port_translate_receive(myspace, name, &port);
255 if (result != KERN_SUCCESS)
256 return (result);
257
258 if (ip_kotype(port) == IKOT_TIMER) {
259 timer = (mk_timer_t)port->ip_kobject;
260 assert(timer != NULL);
39236c6e 261
1c79356b
A
262 simple_lock(&timer->lock);
263 assert(timer->port == port);
264 ip_unlock(port);
265
55e303ae 266 if (!timer->is_dead) {
55e303ae
A
267 timer->is_armed = TRUE;
268
39236c6e
A
269 if (expire_time > mach_absolute_time()) {
270 if (!thread_call_enter_delayed_with_leeway(&timer->call_entry, NULL,
271 expire_time, 0, THREAD_CALL_DELAY_USER_NORMAL))
272 timer->active++;
273 }
274 else {
275 if (!thread_call_enter1(&timer->call_entry, NULL))
276 timer->active++;
277 }
55e303ae 278 }
1c79356b 279
1c79356b
A
280 simple_unlock(&timer->lock);
281 }
282 else {
283 ip_unlock(port);
284 result = KERN_INVALID_ARGUMENT;
285 }
286
287 return (result);
288}
289
316670eb
A
290/*
291 * mk_timer_cancel_trap: Cancel a timer
292 *
293 * Parameters: args User argument descriptor (see below)
294 *
295 * Indirect: args->name Mach port name
296 * args->result_time The armed time of the cancelled timer (return value)
297 *
298 *
299 * Returns: 0 Success
300 * !0 Not success
301 *
302 */
1c79356b 303kern_return_t
91447636
A
304mk_timer_cancel_trap(
305 struct mk_timer_cancel_trap_args *args)
1c79356b 306{
91447636
A
307 mach_port_name_t name = args->name;
308 mach_vm_address_t result_time_addr = args->result_time;
0b4e3aa0 309 uint64_t armed_time = 0;
1c79356b
A
310 mk_timer_t timer;
311 ipc_space_t myspace = current_space();
312 ipc_port_t port;
313 kern_return_t result;
314
315 result = ipc_port_translate_receive(myspace, name, &port);
316 if (result != KERN_SUCCESS)
317 return (result);
318
319 if (ip_kotype(port) == IKOT_TIMER) {
320 timer = (mk_timer_t)port->ip_kobject;
321 assert(timer != NULL);
322 simple_lock(&timer->lock);
323 assert(timer->port == port);
324 ip_unlock(port);
325
326 if (timer->is_armed) {
316670eb 327 armed_time = timer->call_entry.tc_call.deadline;
1c79356b
A
328 if (thread_call_cancel(&timer->call_entry))
329 timer->active--;
330 timer->is_armed = FALSE;
331 }
332
333 simple_unlock(&timer->lock);
334 }
335 else {
336 ip_unlock(port);
337 result = KERN_INVALID_ARGUMENT;
338 }
339
340 if (result == KERN_SUCCESS)
91447636
A
341 if ( result_time_addr != 0 &&
342 copyout((void *)&armed_time, result_time_addr,
1c79356b
A
343 sizeof (armed_time)) != 0 )
344 result = KERN_FAILURE;
345
346 return (result);
347}