xref: /xnu-8796.101.5/osfmk/kern/mk_timer.c (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
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 	static_assert(sizeof(mk_timer_expire_msg_t) < IKM_SAVED_MSG_SIZE);
110 
111 	init_flags = IPC_PORT_INIT_MESSAGE_QUEUE;
112 	result = ipc_port_alloc(myspace, init_flags, &name, &port);
113 	if (result != KERN_SUCCESS) {
114 		zfree(mk_timer_zone, timer);
115 		ipc_kmsg_free(kmsg);
116 		return MACH_PORT_NULL;
117 	}
118 
119 	/* Associate the pre-allocated kmsg with the port */
120 	ipc_kmsg_set_prealloc(kmsg, port);
121 
122 	/* port locked, receive right at user-space */
123 	ipc_kobject_upgrade_mktimer_locked(port, (ipc_kobject_t)timer);
124 
125 	/* make a (naked) send right for the timer to keep */
126 	timer->port = ipc_port_make_send_any_locked(port);
127 
128 	ip_mq_unlock(port);
129 
130 	return name;
131 }
132 
133 static void
mk_timer_port_destroy(ipc_port_t port)134 mk_timer_port_destroy(
135 	ipc_port_t                      port)
136 {
137 	struct mk_timer *timer = NULL;
138 
139 	timer = ipc_kobject_disable(port, IKOT_TIMER);
140 
141 	simple_lock(&timer->lock, LCK_GRP_NULL);
142 
143 	if (thread_call_cancel(&timer->mkt_thread_call)) {
144 		timer->active--;
145 	}
146 	timer->is_armed = FALSE;
147 
148 	timer->is_dead = TRUE;
149 	if (timer->active == 0) {
150 		simple_unlock(&timer->lock);
151 		zfree(mk_timer_zone, timer);
152 
153 		ipc_port_release_send(port);
154 		return;
155 	}
156 
157 	simple_unlock(&timer->lock);
158 }
159 
160 static void
mk_timer_expire(void * p0,__unused void * p1)161 mk_timer_expire(
162 	void                    *p0,
163 	__unused void           *p1)
164 {
165 	struct mk_timer* timer = p0;
166 
167 	simple_lock(&timer->lock, LCK_GRP_NULL);
168 
169 	if (timer->active > 1) {
170 		timer->active--;
171 		simple_unlock(&timer->lock);
172 		return;
173 	}
174 
175 	ipc_port_t port = timer->port;
176 	assert(port != IP_NULL);
177 	assert(timer->active == 1);
178 
179 	while (timer->is_armed && timer->active == 1) {
180 		mk_timer_expire_msg_t           msg;
181 
182 		timer->is_armed = FALSE;
183 		simple_unlock(&timer->lock);
184 
185 		msg.header.msgh_bits =
186 		    MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
187 		msg.header.msgh_remote_port = port;
188 		msg.header.msgh_local_port = MACH_PORT_NULL;
189 		msg.header.msgh_voucher_port = MACH_PORT_NULL;
190 		msg.header.msgh_id = 0;
191 
192 		msg.unused[0] = msg.unused[1] = msg.unused[2] = 0;
193 
194 		(void) mach_msg_send_from_kernel_proper(&msg.header, sizeof(msg));
195 
196 		simple_lock(&timer->lock, LCK_GRP_NULL);
197 	}
198 
199 	if (--timer->active == 0 && timer->is_dead) {
200 		simple_unlock(&timer->lock);
201 		zfree(mk_timer_zone, timer);
202 
203 		ipc_port_release_send(port);
204 		return;
205 	}
206 
207 	simple_unlock(&timer->lock);
208 }
209 
210 /*
211  * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
212  *
213  * Parameters:  args                     User argument descriptor (see below)
214  *
215  * Indirect:     args->name               Mach port name
216  *
217  *
218  * Returns:        0                      Success
219  *                !0                      Not success
220  *
221  */
222 kern_return_t
mk_timer_destroy_trap(struct mk_timer_destroy_trap_args * args)223 mk_timer_destroy_trap(
224 	struct mk_timer_destroy_trap_args *args)
225 {
226 	mach_port_name_t        name = args->name;
227 	ipc_space_t                     myspace = current_space();
228 	ipc_port_t                      port;
229 	kern_return_t           result;
230 
231 	result = ipc_port_translate_receive(myspace, name, &port);
232 	if (result != KERN_SUCCESS) {
233 		return result;
234 	}
235 
236 	if (ip_kotype(port) == IKOT_TIMER) {
237 		ip_mq_unlock(port);
238 		/* TODO: this should be mach_port_mod_refs */
239 		result = mach_port_destroy(myspace, name);
240 	} else {
241 		ip_mq_unlock(port);
242 		result = KERN_INVALID_ARGUMENT;
243 	}
244 
245 	return result;
246 }
247 
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
258  *                !0                      Not success
259  *
260  */
261 
262 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)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 	struct mk_timer*                timer;
266 	ipc_space_t                     myspace = current_space();
267 	ipc_port_t                      port;
268 	kern_return_t                   result;
269 
270 	result = ipc_port_translate_receive(myspace, name, &port);
271 	if (result != KERN_SUCCESS) {
272 		return result;
273 	}
274 
275 	timer = ipc_kobject_get_locked(port, IKOT_TIMER);
276 
277 	if (timer) {
278 
279 		simple_lock(&timer->lock, LCK_GRP_NULL);
280 		assert(timer->port == port);
281 		ip_mq_unlock(port);
282 
283 		if (!timer->is_dead) {
284 			timer->is_armed = TRUE;
285 
286 			if (expire_time > mach_absolute_time()) {
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(
298 					    &timer->mkt_thread_call, NULL,
299 					    expire_time, mk_leeway, tcflags)) {
300 					timer->active++;
301 				}
302 			} else {
303 				if (!thread_call_enter1(&timer->mkt_thread_call, NULL)) {
304 					timer->active++;
305 				}
306 			}
307 		}
308 
309 		simple_unlock(&timer->lock);
310 	} else {
311 		ip_mq_unlock(port);
312 		result = KERN_INVALID_ARGUMENT;
313 	}
314 	return result;
315 }
316 
317 kern_return_t
mk_timer_arm_trap(struct mk_timer_arm_trap_args * args)318 mk_timer_arm_trap(struct mk_timer_arm_trap_args *args)
319 {
320 	return mk_timer_arm_trap_internal(args->name, args->expire_time, 0, MK_TIMER_NORMAL);
321 }
322 
323 kern_return_t
mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args * args)324 mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args *args)
325 {
326 	return mk_timer_arm_trap_internal(args->name, args->expire_time, args->mk_leeway, args->mk_timer_flags);
327 }
328 
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
339  *                !0                      Not success
340  *
341  */
342 kern_return_t
mk_timer_cancel_trap(struct mk_timer_cancel_trap_args * args)343 mk_timer_cancel_trap(
344 	struct mk_timer_cancel_trap_args *args)
345 {
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 	struct mk_timer*                timer;
350 	ipc_space_t                     myspace = current_space();
351 	ipc_port_t                      port;
352 	kern_return_t           result;
353 
354 	result = ipc_port_translate_receive(myspace, name, &port);
355 	if (result != KERN_SUCCESS) {
356 		return result;
357 	}
358 
359 	timer = ipc_kobject_get_locked(port, IKOT_TIMER);
360 	if (timer != NULL) {
361 		simple_lock(&timer->lock, LCK_GRP_NULL);
362 		assert(timer->port == port);
363 		ip_mq_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_mq_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 }
387