]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
8ad349bb | 4 | * @APPLE_LICENSE_OSREFERENCE_HEADER_START@ |
1c79356b | 5 | * |
8ad349bb 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 | |
10 | * License may not be used to create, or enable the creation or | |
11 | * redistribution of, unlawful or unlicensed copies of an Apple operating | |
12 | * system, or to circumvent, violate, or enable the circumvention or | |
13 | * violation of, any terms of an Apple operating system software license | |
14 | * agreement. | |
15 | * | |
16 | * Please obtain a copy of the License at | |
17 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
18 | * file. | |
19 | * | |
20 | * The Original Code and all software distributed under the License are | |
21 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
22 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
23 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
25 | * Please see the License for the specific language governing rights and | |
26 | * limitations under the License. | |
27 | * | |
28 | * @APPLE_LICENSE_OSREFERENCE_HEADER_END@ | |
1c79356b A |
29 | */ |
30 | /* | |
31 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
32 | * | |
33 | * HISTORY | |
34 | * | |
35 | * 29 June 2000 (debo) | |
36 | * Created. | |
37 | */ | |
38 | ||
39 | #include <mach/mach_types.h> | |
91447636 | 40 | #include <mach/mach_traps.h> |
1c79356b A |
41 | #include <mach/mach_port_server.h> |
42 | ||
43 | #include <mach/mk_timer.h> | |
44 | ||
45 | #include <ipc/ipc_space.h> | |
46 | ||
47 | #include <kern/mk_timer.h> | |
48 | #include <kern/thread_call.h> | |
49 | ||
50 | static zone_t mk_timer_zone; | |
51 | ||
52 | static mach_port_qos_t mk_timer_qos = { | |
53 | FALSE, TRUE, 0, sizeof (mk_timer_expire_msg_t) | |
54 | }; | |
55 | ||
56 | static void mk_timer_expire( | |
57 | void *p0, | |
58 | void *p1); | |
59 | ||
60 | mach_port_name_t | |
91447636 A |
61 | mk_timer_create_trap( |
62 | __unused struct mk_timer_create_trap_args *args) | |
1c79356b A |
63 | { |
64 | mk_timer_t timer; | |
65 | ipc_space_t myspace = current_space(); | |
66 | mach_port_name_t name = MACH_PORT_NULL; | |
67 | ipc_port_t port; | |
68 | kern_return_t result; | |
69 | ||
70 | timer = (mk_timer_t)zalloc(mk_timer_zone); | |
71 | if (timer == NULL) | |
72 | return (MACH_PORT_NULL); | |
73 | ||
74 | result = mach_port_allocate_qos(myspace, MACH_PORT_RIGHT_RECEIVE, | |
75 | &mk_timer_qos, &name); | |
76 | if (result == KERN_SUCCESS) | |
77 | result = ipc_port_translate_receive(myspace, name, &port); | |
78 | ||
79 | if (result != KERN_SUCCESS) { | |
91447636 | 80 | zfree(mk_timer_zone, timer); |
1c79356b A |
81 | |
82 | return (MACH_PORT_NULL); | |
83 | } | |
84 | ||
91447636 | 85 | simple_lock_init(&timer->lock, 0); |
1c79356b A |
86 | call_entry_setup(&timer->call_entry, mk_timer_expire, timer); |
87 | timer->is_armed = timer->is_dead = FALSE; | |
88 | timer->active = 0; | |
89 | ||
90 | timer->port = port; | |
91 | ipc_kobject_set_atomically(port, (ipc_kobject_t)timer, IKOT_TIMER); | |
92 | ||
93 | port->ip_srights++; | |
94 | ip_reference(port); | |
95 | ip_unlock(port); | |
96 | ||
97 | return (name); | |
98 | } | |
99 | ||
100 | void | |
101 | mk_timer_port_destroy( | |
102 | ipc_port_t port) | |
103 | { | |
104 | mk_timer_t timer = NULL; | |
105 | ||
106 | ip_lock(port); | |
107 | if (ip_kotype(port) == IKOT_TIMER) { | |
108 | timer = (mk_timer_t)port->ip_kobject; | |
109 | assert(timer != NULL); | |
110 | ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE); | |
111 | simple_lock(&timer->lock); | |
112 | assert(timer->port == port); | |
113 | } | |
114 | ip_unlock(port); | |
115 | ||
116 | if (timer != NULL) { | |
117 | if (thread_call_cancel(&timer->call_entry)) | |
118 | timer->active--; | |
119 | timer->is_armed = FALSE; | |
120 | ||
121 | timer->is_dead = TRUE; | |
122 | if (timer->active == 0) { | |
123 | simple_unlock(&timer->lock); | |
91447636 | 124 | zfree(mk_timer_zone, timer); |
1c79356b A |
125 | |
126 | ipc_port_release_send(port); | |
127 | return; | |
128 | } | |
129 | ||
130 | simple_unlock(&timer->lock); | |
131 | } | |
132 | } | |
133 | ||
134 | void | |
55e303ae | 135 | mk_timer_init(void) |
1c79356b A |
136 | { |
137 | int s = sizeof (mk_timer_data_t); | |
138 | ||
139 | assert(!(mk_timer_zone != NULL)); | |
140 | ||
141 | mk_timer_zone = zinit(s, (4096 * s), (16 * s), "mk_timer"); | |
142 | } | |
143 | ||
144 | static void | |
145 | mk_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 | ||
1c79356b A |
177 | (void) mach_msg_send_from_kernel(&msg.header, sizeof (msg)); |
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 | ||
193 | kern_return_t | |
91447636 A |
194 | mk_timer_destroy_trap( |
195 | struct mk_timer_destroy_trap_args *args) | |
1c79356b | 196 | { |
91447636 | 197 | mach_port_name_t name = args->name; |
1c79356b A |
198 | ipc_space_t myspace = current_space(); |
199 | ipc_port_t port; | |
200 | kern_return_t result; | |
201 | ||
202 | result = ipc_port_translate_receive(myspace, name, &port); | |
203 | if (result != KERN_SUCCESS) | |
204 | return (result); | |
205 | ||
206 | if (ip_kotype(port) == IKOT_TIMER) { | |
207 | ip_unlock(port); | |
208 | result = mach_port_destroy(myspace, name); | |
209 | } | |
210 | else { | |
211 | ip_unlock(port); | |
212 | result = KERN_INVALID_ARGUMENT; | |
213 | } | |
214 | ||
215 | return (result); | |
216 | } | |
217 | ||
218 | kern_return_t | |
91447636 A |
219 | mk_timer_arm_trap( |
220 | struct mk_timer_arm_trap_args *args) | |
1c79356b | 221 | { |
91447636 A |
222 | mach_port_name_t name = args->name; |
223 | uint64_t expire_time = args->expire_time; | |
1c79356b A |
224 | mk_timer_t timer; |
225 | ipc_space_t myspace = current_space(); | |
226 | ipc_port_t port; | |
227 | kern_return_t result; | |
228 | ||
1c79356b A |
229 | result = ipc_port_translate_receive(myspace, name, &port); |
230 | if (result != KERN_SUCCESS) | |
231 | return (result); | |
232 | ||
233 | if (ip_kotype(port) == IKOT_TIMER) { | |
234 | timer = (mk_timer_t)port->ip_kobject; | |
235 | assert(timer != NULL); | |
236 | simple_lock(&timer->lock); | |
237 | assert(timer->port == port); | |
238 | ip_unlock(port); | |
239 | ||
55e303ae | 240 | if (!timer->is_dead) { |
55e303ae A |
241 | timer->is_armed = TRUE; |
242 | ||
243 | if (!thread_call_enter_delayed(&timer->call_entry, expire_time)) | |
244 | timer->active++; | |
245 | } | |
1c79356b | 246 | |
1c79356b A |
247 | simple_unlock(&timer->lock); |
248 | } | |
249 | else { | |
250 | ip_unlock(port); | |
251 | result = KERN_INVALID_ARGUMENT; | |
252 | } | |
253 | ||
254 | return (result); | |
255 | } | |
256 | ||
257 | kern_return_t | |
91447636 A |
258 | mk_timer_cancel_trap( |
259 | struct mk_timer_cancel_trap_args *args) | |
1c79356b | 260 | { |
91447636 A |
261 | mach_port_name_t name = args->name; |
262 | mach_vm_address_t result_time_addr = args->result_time; | |
0b4e3aa0 | 263 | uint64_t armed_time = 0; |
1c79356b A |
264 | mk_timer_t timer; |
265 | ipc_space_t myspace = current_space(); | |
266 | ipc_port_t port; | |
267 | kern_return_t result; | |
268 | ||
269 | result = ipc_port_translate_receive(myspace, name, &port); | |
270 | if (result != KERN_SUCCESS) | |
271 | return (result); | |
272 | ||
273 | if (ip_kotype(port) == IKOT_TIMER) { | |
274 | timer = (mk_timer_t)port->ip_kobject; | |
275 | assert(timer != NULL); | |
276 | simple_lock(&timer->lock); | |
277 | assert(timer->port == port); | |
278 | ip_unlock(port); | |
279 | ||
280 | if (timer->is_armed) { | |
281 | armed_time = timer->call_entry.deadline; | |
282 | if (thread_call_cancel(&timer->call_entry)) | |
283 | timer->active--; | |
284 | timer->is_armed = FALSE; | |
285 | } | |
286 | ||
287 | simple_unlock(&timer->lock); | |
288 | } | |
289 | else { | |
290 | ip_unlock(port); | |
291 | result = KERN_INVALID_ARGUMENT; | |
292 | } | |
293 | ||
294 | if (result == KERN_SUCCESS) | |
91447636 A |
295 | if ( result_time_addr != 0 && |
296 | copyout((void *)&armed_time, result_time_addr, | |
1c79356b A |
297 | sizeof (armed_time)) != 0 ) |
298 | result = KERN_FAILURE; | |
299 | ||
300 | return (result); | |
301 | } |