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