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