]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/mk_timer.c
xnu-6153.81.5.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@
0a7de745 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.
0a7de745 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.
0a7de745 17 *
2d21ac55
A
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.
0a7de745 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
0a7de745 45#include <kern/lock_group.h>
1c79356b
A
46#include <kern/mk_timer.h>
47#include <kern/thread_call.h>
48
0a7de745 49static zone_t mk_timer_zone;
1c79356b
A
50
51static mach_port_qos_t mk_timer_qos = {
cb323159
A
52 .name = FALSE,
53 .prealloc = TRUE,
54 .len = sizeof(mk_timer_expire_msg_t),
1c79356b
A
55};
56
0a7de745
A
57static void mk_timer_expire(
58 void *p0,
59 void *p1);
1c79356b
A
60
61mach_port_name_t
91447636
A
62mk_timer_create_trap(
63 __unused struct mk_timer_create_trap_args *args)
1c79356b 64{
0a7de745
A
65 mk_timer_t timer;
66 ipc_space_t myspace = current_space();
67 mach_port_name_t name = MACH_PORT_NULL;
68 ipc_port_t port;
69 kern_return_t result;
1c79356b
A
70
71 timer = (mk_timer_t)zalloc(mk_timer_zone);
0a7de745
A
72 if (timer == NULL) {
73 return MACH_PORT_NULL;
74 }
1c79356b 75
c6bf4f31
A
76 /* Pre-allocate a kmsg for the timer messages */
77 ipc_kmsg_t kmsg;
78 kmsg = ipc_kmsg_prealloc(mk_timer_qos.len + MAX_TRAILER_SIZE);
79 if (kmsg == IKM_NULL) {
91447636 80 zfree(mk_timer_zone, timer);
0a7de745 81 return MACH_PORT_NULL;
1c79356b
A
82 }
83
c6bf4f31
A
84 /* Allocate an in-transit kobject port with a send right */
85 ipc_kobject_alloc_options_t options;
86 options = (IPC_KOBJECT_ALLOC_IN_TRANSIT | IPC_KOBJECT_ALLOC_MAKE_SEND);
87 port = ipc_kobject_alloc_port((ipc_kobject_t)timer, IKOT_TIMER, options);
88 assert(port != IP_NULL);
89
90 /* Associate the kmsg */
91 ipc_kmsg_set_prealloc(kmsg, port);
92
93 /* Initialize the timer object and bind port to it */
91447636 94 simple_lock_init(&timer->lock, 0);
316670eb 95 thread_call_setup(&timer->call_entry, mk_timer_expire, timer);
1c79356b
A
96 timer->is_armed = timer->is_dead = FALSE;
97 timer->active = 0;
1c79356b 98 timer->port = port;
1c79356b 99
c6bf4f31
A
100 /* Copyout the receive right for the timer port to user-space */
101 current_thread()->ith_knote = ITH_KNOTE_NULL;
102 result = ipc_object_copyout(myspace, ip_to_object(port),
103 MACH_MSG_TYPE_MOVE_RECEIVE,
104 NULL, NULL, &name);
105 if (result != KERN_SUCCESS) {
106 ipc_object_destroy(ip_to_object(port), MACH_MSG_TYPE_MOVE_RECEIVE);
107 /* should trigger mk_timer_port_destroy() call */
108 return MACH_PORT_NULL;
109 }
1c79356b 110
0a7de745 111 return name;
1c79356b
A
112}
113
114void
115mk_timer_port_destroy(
0a7de745 116 ipc_port_t port)
1c79356b 117{
0a7de745 118 mk_timer_t timer = NULL;
1c79356b
A
119
120 ip_lock(port);
121 if (ip_kotype(port) == IKOT_TIMER) {
122 timer = (mk_timer_t)port->ip_kobject;
123 assert(timer != NULL);
124 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
0a7de745 125 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
126 assert(timer->port == port);
127 }
128 ip_unlock(port);
129
130 if (timer != NULL) {
0a7de745 131 if (thread_call_cancel(&timer->call_entry)) {
1c79356b 132 timer->active--;
0a7de745 133 }
1c79356b
A
134 timer->is_armed = FALSE;
135
136 timer->is_dead = TRUE;
137 if (timer->active == 0) {
138 simple_unlock(&timer->lock);
91447636 139 zfree(mk_timer_zone, timer);
1c79356b
A
140
141 ipc_port_release_send(port);
142 return;
143 }
144
145 simple_unlock(&timer->lock);
146 }
147}
148
149void
55e303ae 150mk_timer_init(void)
1c79356b 151{
0a7de745 152 int s = sizeof(mk_timer_data_t);
1c79356b
A
153
154 assert(!(mk_timer_zone != NULL));
155
156 mk_timer_zone = zinit(s, (4096 * s), (16 * s), "mk_timer");
0b4c1975
A
157
158 zone_change(mk_timer_zone, Z_NOENCRYPT, TRUE);
1c79356b
A
159}
160
161static void
162mk_timer_expire(
0a7de745
A
163 void *p0,
164 __unused void *p1)
1c79356b 165{
0a7de745
A
166 mk_timer_t timer = p0;
167 ipc_port_t port;
1c79356b 168
0a7de745 169 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
170
171 if (timer->active > 1) {
172 timer->active--;
173 simple_unlock(&timer->lock);
174 return;
175 }
176
177 port = timer->port;
178 assert(port != IP_NULL);
55e303ae 179 assert(timer->active == 1);
1c79356b 180
55e303ae 181 while (timer->is_armed && timer->active == 1) {
0a7de745 182 mk_timer_expire_msg_t msg;
1c79356b
A
183
184 timer->is_armed = FALSE;
1c79356b
A
185 simple_unlock(&timer->lock);
186
fe8ab488
A
187 msg.header.msgh_bits =
188 MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
1c79356b
A
189 msg.header.msgh_remote_port = port;
190 msg.header.msgh_local_port = MACH_PORT_NULL;
fe8ab488
A
191 msg.header.msgh_voucher_port = MACH_PORT_NULL;
192 msg.header.msgh_id = 0;
1c79356b 193
55e303ae
A
194 msg.unused[0] = msg.unused[1] = msg.unused[2] = 0;
195
0a7de745 196 (void) mach_msg_send_from_kernel_proper(&msg.header, sizeof(msg));
1c79356b 197
0a7de745 198 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
199 }
200
201 if (--timer->active == 0 && timer->is_dead) {
202 simple_unlock(&timer->lock);
91447636 203 zfree(mk_timer_zone, timer);
1c79356b
A
204
205 ipc_port_release_send(port);
206 return;
207 }
208
209 simple_unlock(&timer->lock);
210}
211
316670eb
A
212/*
213 * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
214 *
215 * Parameters: args User argument descriptor (see below)
216 *
217 * Indirect: args->name Mach port name
218 *
219 *
220 * Returns: 0 Success
0a7de745 221 * !0 Not success
316670eb
A
222 *
223 */
1c79356b 224kern_return_t
91447636
A
225mk_timer_destroy_trap(
226 struct mk_timer_destroy_trap_args *args)
1c79356b 227{
0a7de745
A
228 mach_port_name_t name = args->name;
229 ipc_space_t myspace = current_space();
230 ipc_port_t port;
231 kern_return_t result;
1c79356b
A
232
233 result = ipc_port_translate_receive(myspace, name, &port);
0a7de745
A
234 if (result != KERN_SUCCESS) {
235 return result;
236 }
1c79356b
A
237
238 if (ip_kotype(port) == IKOT_TIMER) {
239 ip_unlock(port);
240 result = mach_port_destroy(myspace, name);
0a7de745 241 } else {
1c79356b
A
242 ip_unlock(port);
243 result = KERN_INVALID_ARGUMENT;
244 }
245
0a7de745 246 return result;
1c79356b
A
247}
248
316670eb
A
249/*
250 * mk_timer_arm_trap: Start (arm) a timer
251 *
252 * Parameters: args User argument descriptor (see below)
253 *
254 * Indirect: args->name Mach port name
255 * args->expire_time Time when timer expires
256 *
257 *
258 * Returns: 0 Success
0a7de745 259 * !0 Not success
316670eb
A
260 *
261 */
813fb2f6
A
262
263static kern_return_t
0a7de745
A
264mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t mk_leeway, uint64_t mk_timer_flags)
265{
266 mk_timer_t timer;
267 ipc_space_t myspace = current_space();
268 ipc_port_t port;
269 kern_return_t result;
1c79356b 270
1c79356b 271 result = ipc_port_translate_receive(myspace, name, &port);
0a7de745
A
272 if (result != KERN_SUCCESS) {
273 return result;
274 }
1c79356b
A
275
276 if (ip_kotype(port) == IKOT_TIMER) {
277 timer = (mk_timer_t)port->ip_kobject;
278 assert(timer != NULL);
39236c6e 279
0a7de745 280 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
281 assert(timer->port == port);
282 ip_unlock(port);
283
55e303ae 284 if (!timer->is_dead) {
55e303ae
A
285 timer->is_armed = TRUE;
286
39236c6e 287 if (expire_time > mach_absolute_time()) {
813fb2f6
A
288 uint32_t tcflags = THREAD_CALL_DELAY_USER_NORMAL;
289
290 if (mk_timer_flags & MK_TIMER_CRITICAL) {
291 tcflags = THREAD_CALL_DELAY_USER_CRITICAL;
292 }
293
294 if (mk_leeway != 0) {
295 tcflags |= THREAD_CALL_DELAY_LEEWAY;
296 }
297
298 if (!thread_call_enter_delayed_with_leeway(
0a7de745
A
299 &timer->call_entry, NULL,
300 expire_time, mk_leeway, tcflags)) {
39236c6e 301 timer->active++;
813fb2f6
A
302 }
303 } else {
0a7de745 304 if (!thread_call_enter1(&timer->call_entry, NULL)) {
39236c6e 305 timer->active++;
0a7de745 306 }
39236c6e 307 }
55e303ae 308 }
1c79356b 309
1c79356b 310 simple_unlock(&timer->lock);
813fb2f6 311 } else {
1c79356b
A
312 ip_unlock(port);
313 result = KERN_INVALID_ARGUMENT;
314 }
0a7de745 315 return result;
1c79356b
A
316}
317
813fb2f6 318kern_return_t
0a7de745
A
319mk_timer_arm_trap(struct mk_timer_arm_trap_args *args)
320{
813fb2f6
A
321 return mk_timer_arm_trap_internal(args->name, args->expire_time, 0, MK_TIMER_NORMAL);
322}
323
324kern_return_t
0a7de745
A
325mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args *args)
326{
813fb2f6
A
327 return mk_timer_arm_trap_internal(args->name, args->expire_time, args->mk_leeway, args->mk_timer_flags);
328}
329
316670eb
A
330/*
331 * mk_timer_cancel_trap: Cancel a timer
332 *
333 * Parameters: args User argument descriptor (see below)
334 *
335 * Indirect: args->name Mach port name
336 * args->result_time The armed time of the cancelled timer (return value)
337 *
338 *
339 * Returns: 0 Success
0a7de745 340 * !0 Not success
316670eb
A
341 *
342 */
1c79356b 343kern_return_t
91447636
A
344mk_timer_cancel_trap(
345 struct mk_timer_cancel_trap_args *args)
1c79356b 346{
0a7de745
A
347 mach_port_name_t name = args->name;
348 mach_vm_address_t result_time_addr = args->result_time;
349 uint64_t armed_time = 0;
350 mk_timer_t timer;
351 ipc_space_t myspace = current_space();
352 ipc_port_t port;
353 kern_return_t result;
1c79356b
A
354
355 result = ipc_port_translate_receive(myspace, name, &port);
0a7de745
A
356 if (result != KERN_SUCCESS) {
357 return result;
358 }
1c79356b
A
359
360 if (ip_kotype(port) == IKOT_TIMER) {
361 timer = (mk_timer_t)port->ip_kobject;
362 assert(timer != NULL);
0a7de745 363 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
364 assert(timer->port == port);
365 ip_unlock(port);
366
367 if (timer->is_armed) {
316670eb 368 armed_time = timer->call_entry.tc_call.deadline;
0a7de745 369 if (thread_call_cancel(&timer->call_entry)) {
1c79356b 370 timer->active--;
0a7de745 371 }
1c79356b
A
372 timer->is_armed = FALSE;
373 }
374
375 simple_unlock(&timer->lock);
0a7de745 376 } else {
1c79356b
A
377 ip_unlock(port);
378 result = KERN_INVALID_ARGUMENT;
379 }
380
0a7de745
A
381 if (result == KERN_SUCCESS) {
382 if (result_time_addr != 0 &&
383 copyout((void *)&armed_time, result_time_addr,
384 sizeof(armed_time)) != 0) {
1c79356b 385 result = KERN_FAILURE;
0a7de745
A
386 }
387 }
1c79356b 388
0a7de745 389 return result;
1c79356b 390}