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