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