]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/mk_timer.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / kern / mk_timer.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 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
f427ee49
A
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);
1c79356b
A
61
62static mach_port_qos_t mk_timer_qos = {
cb323159
A
63 .name = FALSE,
64 .prealloc = TRUE,
65 .len = sizeof(mk_timer_expire_msg_t),
1c79356b
A
66};
67
0a7de745
A
68static void mk_timer_expire(
69 void *p0,
70 void *p1);
1c79356b
A
71
72mach_port_name_t
91447636
A
73mk_timer_create_trap(
74 __unused struct mk_timer_create_trap_args *args)
1c79356b 75{
f427ee49 76 struct mk_timer* timer;
ea3f0419
A
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 */
f427ee49 84 timer = (struct mk_timer*)zalloc(mk_timer_zone);
0a7de745
A
85 if (timer == NULL) {
86 return MACH_PORT_NULL;
87 }
ea3f0419 88 simple_lock_init(&timer->lock, 0);
f427ee49 89 thread_call_setup(&timer->mkt_thread_call, mk_timer_expire, timer);
ea3f0419
A
90 timer->is_armed = timer->is_dead = FALSE;
91 timer->active = 0;
1c79356b 92
c6bf4f31
A
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) {
91447636 97 zfree(mk_timer_zone, timer);
0a7de745 98 return MACH_PORT_NULL;
1c79356b
A
99 }
100
ea3f0419
A
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 }
c6bf4f31 108
ea3f0419 109 /* Associate the pre-allocated kmsg with the port */
c6bf4f31
A
110 ipc_kmsg_set_prealloc(kmsg, port);
111
ea3f0419
A
112 /* port locked, receive right at user-space */
113 ipc_kobject_set_atomically(port, (ipc_kobject_t)timer, IKOT_TIMER);
1c79356b 114
ea3f0419
A
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);
1c79356b 119
0a7de745 120 return name;
1c79356b
A
121}
122
123void
124mk_timer_port_destroy(
0a7de745 125 ipc_port_t port)
1c79356b 126{
f427ee49 127 struct mk_timer* timer = NULL;
1c79356b
A
128
129 ip_lock(port);
130 if (ip_kotype(port) == IKOT_TIMER) {
f427ee49 131 timer = (struct mk_timer*) ip_get_kobject(port);
1c79356b
A
132 assert(timer != NULL);
133 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
0a7de745 134 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
135 assert(timer->port == port);
136 }
137 ip_unlock(port);
138
139 if (timer != NULL) {
f427ee49 140 if (thread_call_cancel(&timer->mkt_thread_call)) {
1c79356b 141 timer->active--;
0a7de745 142 }
1c79356b
A
143 timer->is_armed = FALSE;
144
145 timer->is_dead = TRUE;
146 if (timer->active == 0) {
147 simple_unlock(&timer->lock);
91447636 148 zfree(mk_timer_zone, timer);
1c79356b
A
149
150 ipc_port_release_send(port);
151 return;
152 }
153
154 simple_unlock(&timer->lock);
155 }
156}
157
1c79356b
A
158static void
159mk_timer_expire(
0a7de745
A
160 void *p0,
161 __unused void *p1)
1c79356b 162{
f427ee49 163 struct mk_timer* timer = p0;
1c79356b 164
0a7de745 165 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
166
167 if (timer->active > 1) {
168 timer->active--;
169 simple_unlock(&timer->lock);
170 return;
171 }
172
f427ee49 173 ipc_port_t port = timer->port;
1c79356b 174 assert(port != IP_NULL);
55e303ae 175 assert(timer->active == 1);
1c79356b 176
55e303ae 177 while (timer->is_armed && timer->active == 1) {
0a7de745 178 mk_timer_expire_msg_t msg;
1c79356b
A
179
180 timer->is_armed = FALSE;
1c79356b
A
181 simple_unlock(&timer->lock);
182
fe8ab488
A
183 msg.header.msgh_bits =
184 MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
1c79356b
A
185 msg.header.msgh_remote_port = port;
186 msg.header.msgh_local_port = MACH_PORT_NULL;
fe8ab488
A
187 msg.header.msgh_voucher_port = MACH_PORT_NULL;
188 msg.header.msgh_id = 0;
1c79356b 189
55e303ae
A
190 msg.unused[0] = msg.unused[1] = msg.unused[2] = 0;
191
0a7de745 192 (void) mach_msg_send_from_kernel_proper(&msg.header, sizeof(msg));
1c79356b 193
0a7de745 194 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
195 }
196
197 if (--timer->active == 0 && timer->is_dead) {
198 simple_unlock(&timer->lock);
91447636 199 zfree(mk_timer_zone, timer);
1c79356b
A
200
201 ipc_port_release_send(port);
202 return;
203 }
204
205 simple_unlock(&timer->lock);
206}
207
316670eb
A
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
0a7de745 217 * !0 Not success
316670eb
A
218 *
219 */
1c79356b 220kern_return_t
91447636
A
221mk_timer_destroy_trap(
222 struct mk_timer_destroy_trap_args *args)
1c79356b 223{
0a7de745
A
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;
1c79356b
A
228
229 result = ipc_port_translate_receive(myspace, name, &port);
0a7de745
A
230 if (result != KERN_SUCCESS) {
231 return result;
232 }
1c79356b
A
233
234 if (ip_kotype(port) == IKOT_TIMER) {
235 ip_unlock(port);
f427ee49 236 /* TODO: this should be mach_port_mod_refs */
1c79356b 237 result = mach_port_destroy(myspace, name);
0a7de745 238 } else {
1c79356b
A
239 ip_unlock(port);
240 result = KERN_INVALID_ARGUMENT;
241 }
242
0a7de745 243 return result;
1c79356b
A
244}
245
316670eb
A
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
0a7de745 256 * !0 Not success
316670eb
A
257 *
258 */
813fb2f6
A
259
260static kern_return_t
0a7de745
A
261mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t mk_leeway, uint64_t mk_timer_flags)
262{
f427ee49 263 struct mk_timer* timer;
0a7de745
A
264 ipc_space_t myspace = current_space();
265 ipc_port_t port;
266 kern_return_t result;
1c79356b 267
1c79356b 268 result = ipc_port_translate_receive(myspace, name, &port);
0a7de745
A
269 if (result != KERN_SUCCESS) {
270 return result;
271 }
1c79356b
A
272
273 if (ip_kotype(port) == IKOT_TIMER) {
f427ee49
A
274
275 timer = (struct mk_timer*) ip_get_kobject(port);
1c79356b 276 assert(timer != NULL);
39236c6e 277
0a7de745 278 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
279 assert(timer->port == port);
280 ip_unlock(port);
281
55e303ae 282 if (!timer->is_dead) {
55e303ae
A
283 timer->is_armed = TRUE;
284
39236c6e 285 if (expire_time > mach_absolute_time()) {
813fb2f6
A
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(
f427ee49 297 &timer->mkt_thread_call, NULL,
0a7de745 298 expire_time, mk_leeway, tcflags)) {
39236c6e 299 timer->active++;
813fb2f6
A
300 }
301 } else {
f427ee49 302 if (!thread_call_enter1(&timer->mkt_thread_call, NULL)) {
39236c6e 303 timer->active++;
0a7de745 304 }
39236c6e 305 }
55e303ae 306 }
1c79356b 307
1c79356b 308 simple_unlock(&timer->lock);
813fb2f6 309 } else {
1c79356b
A
310 ip_unlock(port);
311 result = KERN_INVALID_ARGUMENT;
312 }
0a7de745 313 return result;
1c79356b
A
314}
315
813fb2f6 316kern_return_t
0a7de745
A
317mk_timer_arm_trap(struct mk_timer_arm_trap_args *args)
318{
813fb2f6
A
319 return mk_timer_arm_trap_internal(args->name, args->expire_time, 0, MK_TIMER_NORMAL);
320}
321
322kern_return_t
0a7de745
A
323mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args *args)
324{
813fb2f6
A
325 return mk_timer_arm_trap_internal(args->name, args->expire_time, args->mk_leeway, args->mk_timer_flags);
326}
327
316670eb
A
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
0a7de745 338 * !0 Not success
316670eb
A
339 *
340 */
1c79356b 341kern_return_t
91447636
A
342mk_timer_cancel_trap(
343 struct mk_timer_cancel_trap_args *args)
1c79356b 344{
0a7de745
A
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;
f427ee49 348 struct mk_timer* timer;
0a7de745
A
349 ipc_space_t myspace = current_space();
350 ipc_port_t port;
351 kern_return_t result;
1c79356b
A
352
353 result = ipc_port_translate_receive(myspace, name, &port);
0a7de745
A
354 if (result != KERN_SUCCESS) {
355 return result;
356 }
1c79356b
A
357
358 if (ip_kotype(port) == IKOT_TIMER) {
f427ee49 359 timer = (struct mk_timer*) ip_get_kobject(port);
1c79356b 360 assert(timer != NULL);
0a7de745 361 simple_lock(&timer->lock, LCK_GRP_NULL);
1c79356b
A
362 assert(timer->port == port);
363 ip_unlock(port);
364
365 if (timer->is_armed) {
f427ee49
A
366 armed_time = thread_call_get_armed_deadline(&timer->mkt_thread_call);
367 if (thread_call_cancel(&timer->mkt_thread_call)) {
1c79356b 368 timer->active--;
0a7de745 369 }
1c79356b
A
370 timer->is_armed = FALSE;
371 }
372
373 simple_unlock(&timer->lock);
0a7de745 374 } else {
1c79356b
A
375 ip_unlock(port);
376 result = KERN_INVALID_ARGUMENT;
377 }
378
f427ee49
A
379 if (result == KERN_SUCCESS && result_time_addr != 0) {
380 if (copyout((void *)&armed_time, result_time_addr, sizeof(armed_time)) != 0) {
1c79356b 381 result = KERN_FAILURE;
0a7de745
A
382 }
383 }
1c79356b 384
0a7de745 385 return result;
1c79356b 386}