xref: /xnu-8792.41.9/osfmk/kern/mk_timer.c (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
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/thread_call.h>
47 #include <ipc/ipc_kmsg.h>
48 
49 struct mk_timer {
50 	decl_simple_lock_data(, lock);
51 	thread_call_data_t      mkt_thread_call;
52 	uint32_t                is_dead:1,
53 	    is_armed:1;
54 	int                     active;
55 	ipc_port_t              port;
56 };
57 
58 static ZONE_DEFINE_TYPE(mk_timer_zone, "mk_timer",
59     struct mk_timer, ZC_ZFREE_CLEARMEM);
60 
61 static void mk_timer_port_destroy(ipc_port_t);
62 static void mk_timer_expire(void *p0, void *p1);
63 
64 IPC_KOBJECT_DEFINE(IKOT_TIMER,
65     .iko_op_destroy = mk_timer_port_destroy);
66 
67 __abortlike
68 static void
ipc_kobject_mktimer_require_panic(ipc_port_t port)69 ipc_kobject_mktimer_require_panic(
70 	ipc_port_t                  port)
71 {
72 	panic("port %p / mktimer %p: circularity check failed",
73 	    port, ipc_kobject_get_raw(port, IKOT_TIMER));
74 }
75 
76 void
ipc_kobject_mktimer_require_locked(ipc_port_t port)77 ipc_kobject_mktimer_require_locked(
78 	ipc_port_t                  port)
79 {
80 	struct mk_timer *timer;
81 
82 	timer = ipc_kobject_get_locked(port, IKOT_TIMER);
83 	if (timer->port != port) {
84 		ipc_kobject_mktimer_require_panic(port);
85 	}
86 }
87 
88 mach_port_name_t
mk_timer_create_trap(__unused struct mk_timer_create_trap_args * args)89 mk_timer_create_trap(
90 	__unused struct mk_timer_create_trap_args *args)
91 {
92 	struct mk_timer*      timer;
93 	ipc_space_t           myspace = current_space();
94 	mach_port_name_t      name = MACH_PORT_NULL;
95 	ipc_port_init_flags_t init_flags;
96 	ipc_port_t            port;
97 	kern_return_t         result;
98 	ipc_kmsg_t            kmsg;
99 
100 	/* Allocate and initialize local state of a timer object */
101 	timer = zalloc_flags(mk_timer_zone, Z_ZERO | Z_WAITOK | Z_NOFAIL);
102 	simple_lock_init(&timer->lock, 0);
103 	thread_call_setup(&timer->mkt_thread_call, mk_timer_expire, timer);
104 
105 	/* Pre-allocate a kmsg for the timer messages */
106 	kmsg = ipc_kmsg_alloc(sizeof(mk_timer_expire_msg_t), 0, 0,
107 	    IPC_KMSG_ALLOC_KERNEL | IPC_KMSG_ALLOC_ZERO |
108 	    IPC_KMSG_ALLOC_SAVED | IPC_KMSG_ALLOC_NOFAIL);
109 
110 	init_flags = IPC_PORT_INIT_MESSAGE_QUEUE;
111 	result = ipc_port_alloc(myspace, init_flags, &name, &port);
112 	if (result != KERN_SUCCESS) {
113 		zfree(mk_timer_zone, timer);
114 		ipc_kmsg_free(kmsg);
115 		return MACH_PORT_NULL;
116 	}
117 
118 	/* Associate the pre-allocated kmsg with the port */
119 	ipc_kmsg_set_prealloc(kmsg, port);
120 
121 	/* port locked, receive right at user-space */
122 	ipc_kobject_upgrade_mktimer_locked(port, (ipc_kobject_t)timer);
123 
124 	/* make a (naked) send right for the timer to keep */
125 	timer->port = ipc_port_make_send_any_locked(port);
126 
127 	ip_mq_unlock(port);
128 
129 	return name;
130 }
131 
132 static void
mk_timer_port_destroy(ipc_port_t port)133 mk_timer_port_destroy(
134 	ipc_port_t                      port)
135 {
136 	struct mk_timer *timer = NULL;
137 
138 	timer = ipc_kobject_disable(port, IKOT_TIMER);
139 
140 	simple_lock(&timer->lock, LCK_GRP_NULL);
141 
142 	if (thread_call_cancel(&timer->mkt_thread_call)) {
143 		timer->active--;
144 	}
145 	timer->is_armed = FALSE;
146 
147 	timer->is_dead = TRUE;
148 	if (timer->active == 0) {
149 		simple_unlock(&timer->lock);
150 		zfree(mk_timer_zone, timer);
151 
152 		ipc_port_release_send(port);
153 		return;
154 	}
155 
156 	simple_unlock(&timer->lock);
157 }
158 
159 static void
mk_timer_expire(void * p0,__unused void * p1)160 mk_timer_expire(
161 	void                    *p0,
162 	__unused void           *p1)
163 {
164 	struct mk_timer* timer = p0;
165 
166 	simple_lock(&timer->lock, LCK_GRP_NULL);
167 
168 	if (timer->active > 1) {
169 		timer->active--;
170 		simple_unlock(&timer->lock);
171 		return;
172 	}
173 
174 	ipc_port_t port = timer->port;
175 	assert(port != IP_NULL);
176 	assert(timer->active == 1);
177 
178 	while (timer->is_armed && timer->active == 1) {
179 		mk_timer_expire_msg_t           msg;
180 
181 		timer->is_armed = FALSE;
182 		simple_unlock(&timer->lock);
183 
184 		msg.header.msgh_bits =
185 		    MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
186 		msg.header.msgh_remote_port = port;
187 		msg.header.msgh_local_port = MACH_PORT_NULL;
188 		msg.header.msgh_voucher_port = MACH_PORT_NULL;
189 		msg.header.msgh_id = 0;
190 
191 		msg.unused[0] = msg.unused[1] = msg.unused[2] = 0;
192 
193 		(void) mach_msg_send_from_kernel_proper(&msg.header, sizeof(msg));
194 
195 		simple_lock(&timer->lock, LCK_GRP_NULL);
196 	}
197 
198 	if (--timer->active == 0 && timer->is_dead) {
199 		simple_unlock(&timer->lock);
200 		zfree(mk_timer_zone, timer);
201 
202 		ipc_port_release_send(port);
203 		return;
204 	}
205 
206 	simple_unlock(&timer->lock);
207 }
208 
209 /*
210  * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
211  *
212  * Parameters:  args                     User argument descriptor (see below)
213  *
214  * Indirect:     args->name               Mach port name
215  *
216  *
217  * Returns:        0                      Success
218  *                !0                      Not success
219  *
220  */
221 kern_return_t
mk_timer_destroy_trap(struct mk_timer_destroy_trap_args * args)222 mk_timer_destroy_trap(
223 	struct mk_timer_destroy_trap_args *args)
224 {
225 	mach_port_name_t        name = args->name;
226 	ipc_space_t                     myspace = current_space();
227 	ipc_port_t                      port;
228 	kern_return_t           result;
229 
230 	result = ipc_port_translate_receive(myspace, name, &port);
231 	if (result != KERN_SUCCESS) {
232 		return result;
233 	}
234 
235 	if (ip_kotype(port) == IKOT_TIMER) {
236 		ip_mq_unlock(port);
237 		/* TODO: this should be mach_port_mod_refs */
238 		result = mach_port_destroy(myspace, name);
239 	} else {
240 		ip_mq_unlock(port);
241 		result = KERN_INVALID_ARGUMENT;
242 	}
243 
244 	return result;
245 }
246 
247 /*
248  * mk_timer_arm_trap: Start (arm) a timer
249  *
250  * Parameters:  args                     User argument descriptor (see below)
251  *
252  * Indirect:     args->name               Mach port name
253  *               args->expire_time        Time when timer expires
254  *
255  *
256  * Returns:        0                      Success
257  *                !0                      Not success
258  *
259  */
260 
261 static kern_return_t
mk_timer_arm_trap_internal(mach_port_name_t name,uint64_t expire_time,uint64_t mk_leeway,uint64_t mk_timer_flags)262 mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t mk_leeway, uint64_t mk_timer_flags)
263 {
264 	struct mk_timer*                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 
274 	timer = ipc_kobject_get_locked(port, IKOT_TIMER);
275 
276 	if (timer) {
277 
278 		simple_lock(&timer->lock, LCK_GRP_NULL);
279 		assert(timer->port == port);
280 		ip_mq_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_mq_unlock(port);
311 		result = KERN_INVALID_ARGUMENT;
312 	}
313 	return result;
314 }
315 
316 kern_return_t
mk_timer_arm_trap(struct mk_timer_arm_trap_args * args)317 mk_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 
322 kern_return_t
mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args * args)323 mk_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  */
341 kern_return_t
mk_timer_cancel_trap(struct mk_timer_cancel_trap_args * args)342 mk_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 	timer = ipc_kobject_get_locked(port, IKOT_TIMER);
359 	if (timer != NULL) {
360 		simple_lock(&timer->lock, LCK_GRP_NULL);
361 		assert(timer->port == port);
362 		ip_mq_unlock(port);
363 
364 		if (timer->is_armed) {
365 			armed_time = thread_call_get_armed_deadline(&timer->mkt_thread_call);
366 			if (thread_call_cancel(&timer->mkt_thread_call)) {
367 				timer->active--;
368 			}
369 			timer->is_armed = FALSE;
370 		}
371 
372 		simple_unlock(&timer->lock);
373 	} else {
374 		ip_mq_unlock(port);
375 		result = KERN_INVALID_ARGUMENT;
376 	}
377 
378 	if (result == KERN_SUCCESS && result_time_addr != 0) {
379 		if (copyout((void *)&armed_time, result_time_addr, sizeof(armed_time)) != 0) {
380 			result = KERN_FAILURE;
381 		}
382 	}
383 
384 	return result;
385 }
386