xref: /xnu-11417.140.69/osfmk/kern/mk_timer.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
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/port.h>
44 #include <ipc/ipc_space.h>
45 
46 #include <kern/lock_group.h>
47 #include <kern/thread_call.h>
48 #include <ipc/ipc_kmsg.h>
49 
50 struct mk_timer {
51 	decl_simple_lock_data(, lock);
52 	thread_call_data_t      mkt_thread_call;
53 	bool                    is_dead;
54 	bool                    is_armed;
55 	int                     active;
56 	ipc_port_t XNU_PTRAUTH_SIGNED_PTR("mk_timer.port") port;
57 	ipc_kmsg_t XNU_PTRAUTH_SIGNED_PTR("mk_timer.prealloc") prealloc;
58 };
59 
60 static ZONE_DEFINE_TYPE(mk_timer_zone, "mk_timer",
61     struct mk_timer, ZC_ZFREE_CLEARMEM);
62 
63 static void mk_timer_port_destroy(ipc_port_t);
64 static void mk_timer_expire(void *p0, void *p1);
65 
66 IPC_KOBJECT_DEFINE(IKOT_TIMER,
67     .iko_op_destroy = mk_timer_port_destroy);
68 
69 mach_port_name_t
mk_timer_create_trap(__unused struct mk_timer_create_trap_args * args)70 mk_timer_create_trap(
71 	__unused struct mk_timer_create_trap_args *args)
72 {
73 	struct mk_timer*      timer;
74 	ipc_space_t           myspace = current_space();
75 	mach_port_name_t      name = MACH_PORT_NULL;
76 	ipc_port_init_flags_t init_flags;
77 	ipc_port_t            port;
78 	kern_return_t         result;
79 	ipc_kmsg_t            kmsg;
80 
81 	/* Allocate and initialize local state of a timer object */
82 	timer = zalloc_flags(mk_timer_zone, Z_ZERO | Z_WAITOK | Z_NOFAIL);
83 	simple_lock_init(&timer->lock, 0);
84 	thread_call_setup(&timer->mkt_thread_call, mk_timer_expire, timer);
85 
86 	/* Pre-allocate a kmsg for the timer messages */
87 	kmsg = ipc_kmsg_alloc(sizeof(mk_timer_expire_msg_t), 0, 0,
88 	    IPC_KMSG_ALLOC_KERNEL | IPC_KMSG_ALLOC_ZERO |
89 	    IPC_KMSG_ALLOC_ALL_INLINE | IPC_KMSG_ALLOC_NOFAIL |
90 	    IPC_KMSG_ALLOC_USE_KEEP_ALIVE);
91 	init_flags = IPC_PORT_INIT_MESSAGE_QUEUE;
92 	result = ipc_port_alloc(myspace, init_flags, &name, &port);
93 	if (result != KERN_SUCCESS) {
94 		zfree(mk_timer_zone, timer);
95 		ipc_kmsg_keep_alive_abandon(kmsg);
96 		return MACH_PORT_NULL;
97 	}
98 
99 	/* port locked, receive right at user-space */
100 	port->ip_immovable_receive = true;
101 	ipc_kobject_upgrade_mktimer_locked(port, (ipc_kobject_t)timer);
102 
103 	/* make a (naked) send right for the timer to keep */
104 	timer->port = ipc_port_make_send_any_locked(port);
105 
106 	/* Associate the pre-allocated kmsg with the port */
107 	timer->prealloc = kmsg;
108 
109 	ip_mq_unlock(port);
110 
111 	return name;
112 }
113 
114 static void
mk_timer_unlock_and_destroy(struct mk_timer * timer,ipc_port_t port)115 mk_timer_unlock_and_destroy(struct mk_timer *timer, ipc_port_t port)
116 {
117 	ipc_kmsg_t kmsg = timer->prealloc;
118 
119 	simple_unlock(&timer->lock);
120 
121 	zfree(mk_timer_zone, timer);
122 	ipc_kmsg_keep_alive_abandon(kmsg);
123 	ipc_port_release_send(port);
124 }
125 
126 static void
mk_timer_port_destroy(ipc_port_t port)127 mk_timer_port_destroy(
128 	ipc_port_t                      port)
129 {
130 	struct mk_timer *timer = NULL;
131 
132 	timer = ipc_kobject_disable(port, IKOT_TIMER);
133 
134 	simple_lock(&timer->lock, LCK_GRP_NULL);
135 
136 	if (thread_call_cancel(&timer->mkt_thread_call)) {
137 		timer->active--;
138 	}
139 	timer->is_armed = false;
140 
141 	timer->is_dead = true;
142 	if (timer->active == 0) {
143 		mk_timer_unlock_and_destroy(timer, port);
144 	} else {
145 		simple_unlock(&timer->lock);
146 	}
147 }
148 
149 static void
mk_timer_expire(void * p0,__unused void * p1)150 mk_timer_expire(
151 	void                    *p0,
152 	__unused void           *p1)
153 {
154 	struct mk_timer *timer = p0;
155 	ipc_kmsg_t kmsg;
156 	ipc_port_t port;
157 
158 	simple_lock(&timer->lock, LCK_GRP_NULL);
159 
160 	port = timer->port;
161 	kmsg = timer->prealloc;
162 	assert(port != IP_NULL);
163 	assert(timer->active > 0);
164 
165 	while (timer->is_armed && timer->active == 1) {
166 		timer->is_armed = false;
167 		simple_unlock(&timer->lock);
168 
169 		if (ipc_kmsg_keep_alive_try_reusing(kmsg)) {
170 			mk_timer_expire_msg_t *msg;
171 
172 			msg = __container_of(ikm_header(kmsg),
173 			    mk_timer_expire_msg_t, header);
174 			bzero(msg, sizeof(mk_timer_expire_msg_t));
175 			msg->header.msgh_bits =
176 			    MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
177 			msg->header.msgh_size = sizeof(mk_timer_expire_msg_t);
178 			msg->header.msgh_remote_port = port;
179 
180 			kernel_mach_msg_send_kmsg(kmsg);
181 		}
182 
183 		simple_lock(&timer->lock, LCK_GRP_NULL);
184 	}
185 
186 	timer->active -= 1;
187 
188 	if (timer->active == 0 && timer->is_dead) {
189 		mk_timer_unlock_and_destroy(timer, port);
190 	} else {
191 		simple_unlock(&timer->lock);
192 	}
193 }
194 
195 /*
196  * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
197  *
198  * Parameters:  args                     User argument descriptor (see below)
199  *
200  * Indirect:     args->name               Mach port name
201  *
202  *
203  * Returns:        0                      Success
204  *                !0                      Not success
205  *
206  */
207 kern_return_t
mk_timer_destroy_trap(struct mk_timer_destroy_trap_args * args)208 mk_timer_destroy_trap(
209 	struct mk_timer_destroy_trap_args *args)
210 {
211 	mach_port_name_t        name = args->name;
212 	ipc_space_t             myspace = current_space();
213 	kern_return_t           kr;
214 	ipc_entry_t             entry;
215 
216 	kr = ipc_right_lookup_write(myspace, name, &entry);
217 	if (kr != KERN_SUCCESS) {
218 		return kr;
219 	}
220 
221 	/* space is write-locked and active */
222 
223 	if ((IE_BITS_TYPE(entry->ie_bits) & MACH_PORT_TYPE_RECEIVE) == 0) {
224 		is_write_unlock(myspace);
225 		return KERN_INVALID_RIGHT;
226 	}
227 
228 	if (ip_kotype(entry->ie_port) != IKOT_TIMER) {
229 		is_write_unlock(myspace);
230 		return KERN_INVALID_ARGUMENT;
231 	}
232 
233 	/*
234 	 * This should have been a mach_mod_refs(RR, -1) but unfortunately,
235 	 * the fact this is a mach_port_destroy() is ABI now.
236 	 */
237 	return ipc_right_destroy(myspace, name, entry, TRUE, 0); /* unlocks space */
238 }
239 
240 /*
241  * mk_timer_arm_trap: Start (arm) a timer
242  *
243  * Parameters:  args                     User argument descriptor (see below)
244  *
245  * Indirect:     args->name               Mach port name
246  *               args->expire_time        Time when timer expires
247  *
248  *
249  * Returns:        0                      Success
250  *                !0                      Not success
251  *
252  */
253 
254 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)255 mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t mk_leeway, uint64_t mk_timer_flags)
256 {
257 	struct mk_timer*                timer;
258 	ipc_space_t                     myspace = current_space();
259 	ipc_port_t                      port;
260 	kern_return_t                   result;
261 
262 	result = ipc_port_translate_receive(myspace, name, &port);
263 	if (result != KERN_SUCCESS) {
264 		return result;
265 	}
266 
267 	timer = ipc_kobject_get_locked(port, IKOT_TIMER);
268 
269 	if (timer) {
270 
271 		simple_lock(&timer->lock, LCK_GRP_NULL);
272 		assert(timer->port == port);
273 		ip_mq_unlock(port);
274 
275 		if (!timer->is_dead) {
276 			timer->is_armed = true;
277 
278 			if (expire_time > mach_absolute_time()) {
279 				uint32_t tcflags = THREAD_CALL_DELAY_USER_NORMAL;
280 
281 				if (mk_timer_flags & MK_TIMER_CRITICAL) {
282 					tcflags = THREAD_CALL_DELAY_USER_CRITICAL;
283 				}
284 
285 				if (mk_leeway != 0) {
286 					tcflags |= THREAD_CALL_DELAY_LEEWAY;
287 				}
288 
289 				if (!thread_call_enter_delayed_with_leeway(
290 					    &timer->mkt_thread_call, NULL,
291 					    expire_time, mk_leeway, tcflags)) {
292 					timer->active++;
293 				}
294 			} else {
295 				if (!thread_call_enter1(&timer->mkt_thread_call, NULL)) {
296 					timer->active++;
297 				}
298 			}
299 		}
300 
301 		simple_unlock(&timer->lock);
302 	} else {
303 		ip_mq_unlock(port);
304 		result = KERN_INVALID_ARGUMENT;
305 	}
306 	return result;
307 }
308 
309 kern_return_t
mk_timer_arm_trap(struct mk_timer_arm_trap_args * args)310 mk_timer_arm_trap(struct mk_timer_arm_trap_args *args)
311 {
312 	return mk_timer_arm_trap_internal(args->name, args->expire_time, 0, MK_TIMER_NORMAL);
313 }
314 
315 kern_return_t
mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args * args)316 mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args *args)
317 {
318 	return mk_timer_arm_trap_internal(args->name, args->expire_time, args->mk_leeway, args->mk_timer_flags);
319 }
320 
321 /*
322  * mk_timer_cancel_trap: Cancel a timer
323  *
324  * Parameters:  args                     User argument descriptor (see below)
325  *
326  * Indirect:     args->name               Mach port name
327  *               args->result_time        The armed time of the cancelled timer (return value)
328  *
329  *
330  * Returns:        0                      Success
331  *                !0                      Not success
332  *
333  */
334 kern_return_t
mk_timer_cancel_trap(struct mk_timer_cancel_trap_args * args)335 mk_timer_cancel_trap(
336 	struct mk_timer_cancel_trap_args *args)
337 {
338 	mach_port_name_t        name = args->name;
339 	mach_vm_address_t       result_time_addr = args->result_time;
340 	uint64_t                        armed_time = 0;
341 	struct mk_timer*                timer;
342 	ipc_space_t                     myspace = current_space();
343 	ipc_port_t                      port;
344 	kern_return_t           result;
345 
346 	result = ipc_port_translate_receive(myspace, name, &port);
347 	if (result != KERN_SUCCESS) {
348 		return result;
349 	}
350 
351 	timer = ipc_kobject_get_locked(port, IKOT_TIMER);
352 	if (timer != NULL) {
353 		simple_lock(&timer->lock, LCK_GRP_NULL);
354 		assert(timer->port == port);
355 		ip_mq_unlock(port);
356 
357 		if (timer->is_armed) {
358 			armed_time = thread_call_get_armed_deadline(&timer->mkt_thread_call);
359 			if (thread_call_cancel(&timer->mkt_thread_call)) {
360 				timer->active--;
361 			}
362 			timer->is_armed = false;
363 		}
364 
365 		simple_unlock(&timer->lock);
366 	} else {
367 		ip_mq_unlock(port);
368 		result = KERN_INVALID_ARGUMENT;
369 	}
370 
371 	if (result == KERN_SUCCESS && result_time_addr != 0) {
372 		if (copyout((void *)&armed_time, result_time_addr, sizeof(armed_time)) != 0) {
373 			result = KERN_FAILURE;
374 		}
375 	}
376 
377 	return result;
378 }
379