]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/work_interval.c
4c1d4cbdae9fb5dec3ccc31b8b9ee6f876bb46c6
[apple/xnu.git] / osfmk / kern / work_interval.c
1 /*
2 * Copyright (c) 2017 Apple 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
30 #include <sys/work_interval.h>
31
32 #include <kern/work_interval.h>
33
34 #include <kern/thread.h>
35 #include <kern/sched_prim.h>
36 #include <kern/machine.h>
37 #include <kern/thread_group.h>
38 #include <kern/ipc_kobject.h>
39 #include <kern/task.h>
40 #include <kern/coalition.h>
41 #include <kern/policy_internal.h>
42
43 #include <mach/kern_return.h>
44 #include <mach/notify.h>
45
46 #include <stdatomic.h>
47
48 /*
49 * Work Interval structs
50 *
51 * This struct represents a thread group and/or work interval context
52 * in a mechanism that is represented with a kobject.
53 *
54 * Every thread that has joined a WI has a +1 ref, and the port
55 * has a +1 ref as well.
56 *
57 * TODO: groups need to have a 'is for WI' flag
58 * and they need a flag to create that says 'for WI'
59 * This would allow CLPC to avoid allocating WI support
60 * data unless it is needed
61 *
62 * TODO: Enforce not having more than one non-group joinable work
63 * interval per thread group.
64 * CLPC only wants to see one WI-notify callout per group.
65 */
66
67 struct work_interval {
68 uint64_t wi_id;
69 _Atomic uint32_t wi_ref_count;
70 uint32_t wi_create_flags;
71
72 /* for debugging purposes only, does not hold a ref on port */
73 ipc_port_t wi_port;
74
75 /*
76 * holds uniqueid and version of creating process,
77 * used to permission-gate notify
78 * TODO: you'd think there would be a better way to do this
79 */
80 uint64_t wi_creator_uniqueid;
81 uint32_t wi_creator_pid;
82 int wi_creator_pidversion;
83
84 };
85
86 static inline void
87 wi_retain(struct work_interval *work_interval)
88 {
89 uint32_t old_count;
90 old_count = atomic_fetch_add_explicit(&work_interval->wi_ref_count,
91 1, memory_order_relaxed);
92 assert(old_count > 0);
93 }
94
95 static inline void
96 wi_release(struct work_interval *work_interval)
97 {
98 uint32_t old_count;
99 old_count = atomic_fetch_sub_explicit(&work_interval->wi_ref_count,
100 1, memory_order_relaxed);
101 assert(old_count > 0);
102
103 if (old_count == 1) {
104
105 kfree(work_interval, sizeof(struct work_interval));
106 }
107 }
108
109 /*
110 * work_interval_port_alloc
111 *
112 * Description: Obtain a send right for the given work interval struct.
113 *
114 * Parameters: work_interval - A work_interval struct
115 * Consumes a +1 ref count on work_interval, now owned by the port.
116 *
117 * Returns: Port of type IKOT_WORK_INTERVAL with work_interval set as its kobject.
118 * Returned with a +1 send right and no-senders notification armed.
119 * Work interval struct reference is held by the port.
120 */
121 static ipc_port_t
122 work_interval_port_alloc(struct work_interval *work_interval)
123 {
124 ipc_port_t work_interval_port = ipc_port_alloc_kernel();
125
126 if (work_interval_port == IP_NULL) {
127 panic("failed to allocate work interval port");
128 }
129
130 assert(work_interval->wi_port == IP_NULL);
131
132 ip_lock(work_interval_port);
133 ipc_kobject_set_atomically(work_interval_port, (ipc_kobject_t)work_interval,
134 IKOT_WORK_INTERVAL);
135
136 ipc_port_t notify_port = ipc_port_make_sonce_locked(work_interval_port);
137 ipc_port_t old_notify_port = IP_NULL;
138 ipc_port_nsrequest(work_interval_port, 1, notify_port, &old_notify_port);
139 /* port unlocked */
140
141 assert(old_notify_port == IP_NULL);
142
143 /* This is the only make-send that will happen on this port */
144 ipc_port_t send_port = ipc_port_make_send(work_interval_port);
145 assert(IP_VALID(send_port));
146
147 work_interval->wi_port = work_interval_port;
148
149 return send_port;
150 }
151
152 /*
153 * work_interval_port_convert
154 *
155 * Called with port locked, returns reference to work interval
156 * if indeed the port is a work interval kobject port
157 */
158 static struct work_interval *
159 work_interval_port_convert_locked(ipc_port_t port)
160 {
161 struct work_interval *work_interval = NULL;
162
163 if (!IP_VALID(port)) {
164 return NULL;
165 }
166
167 if (!ip_active(port)) {
168 return NULL;
169 }
170
171 if (IKOT_WORK_INTERVAL != ip_kotype(port)) {
172 return NULL;
173 }
174
175 work_interval = (struct work_interval *)port->ip_kobject;
176
177 wi_retain(work_interval);
178
179 return work_interval;
180 }
181
182 /*
183 * port_name_to_work_interval
184 *
185 * Description: Obtain a reference to the work_interval associated with a given port.
186 *
187 * Parameters: name A Mach port name to translate.
188 *
189 * Returns: NULL The given Mach port did not reference a work_interval.
190 * !NULL The work_interval that is associated with the Mach port.
191 */
192 static kern_return_t
193 port_name_to_work_interval(mach_port_name_t name,
194 struct work_interval **work_interval)
195 {
196 if (!MACH_PORT_VALID(name)) {
197 return KERN_INVALID_NAME;
198 }
199
200 ipc_port_t port = IPC_PORT_NULL;
201 kern_return_t kr = KERN_SUCCESS;
202
203 kr = ipc_port_translate_send(current_space(), name, &port);
204 if (kr != KERN_SUCCESS) {
205 return kr;
206 }
207 /* port is locked */
208
209 assert(IP_VALID(port));
210
211 struct work_interval *converted_work_interval;
212
213 converted_work_interval = work_interval_port_convert_locked(port);
214
215 /* the port is valid, but doesn't denote a work_interval */
216 if (converted_work_interval == NULL) {
217 kr = KERN_INVALID_CAPABILITY;
218 }
219
220 ip_unlock(port);
221
222 if (kr == KERN_SUCCESS) {
223 *work_interval = converted_work_interval;
224 }
225
226 return kr;
227 }
228
229
230 /*
231 * work_interval_port_notify
232 *
233 * Description: Handle a no-senders notification for a work interval port.
234 * Destroys the port and releases its reference on the work interval.
235 *
236 * Parameters: msg A Mach no-senders notification message.
237 *
238 * Note: This assumes that there is only one create-right-from-work-interval point,
239 * if the ability to extract another send right after creation is added,
240 * this will have to change to handle make-send counts correctly.
241 */
242 void
243 work_interval_port_notify(mach_msg_header_t *msg)
244 {
245 mach_no_senders_notification_t *notification = (void *)msg;
246 ipc_port_t port = notification->not_header.msgh_remote_port;
247 struct work_interval *work_interval = NULL;
248
249 if (!IP_VALID(port)) {
250 panic("work_interval_port_notify(): invalid port");
251 }
252
253 ip_lock(port);
254
255 if (!ip_active(port)) {
256 panic("work_interval_port_notify(): inactive port %p", port);
257 }
258
259 if (ip_kotype(port) != IKOT_WORK_INTERVAL) {
260 panic("work_interval_port_notify(): not the right kobject: %p, %d\n",
261 port, ip_kotype(port));
262 }
263
264 if (port->ip_mscount != notification->not_count) {
265 panic("work_interval_port_notify(): unexpected make-send count: %p, %d, %d",
266 port, port->ip_mscount, notification->not_count);
267 }
268
269 if (port->ip_srights != 0) {
270 panic("work_interval_port_notify(): unexpected send right count: %p, %d",
271 port, port->ip_srights);
272 }
273
274 work_interval = (struct work_interval *)port->ip_kobject;
275
276 if (work_interval == NULL) {
277 panic("work_interval_port_notify(): missing kobject: %p", port);
278 }
279
280 ipc_kobject_set_atomically(port, IKO_NULL, IKOT_NONE);
281
282 work_interval->wi_port = MACH_PORT_NULL;
283
284 ip_unlock(port);
285
286 ipc_port_dealloc_kernel(port);
287 wi_release(work_interval);
288 }
289
290 /*
291 * Change thread's bound work interval to the passed-in work interval
292 * Consumes +1 ref on work_interval
293 *
294 * May also pass NULL to un-set work_interval on the thread
295 *
296 * Will deallocate any old work interval on the thread
297 */
298 static void
299 thread_set_work_interval(thread_t thread,
300 struct work_interval *work_interval)
301 {
302 assert(thread == current_thread());
303
304 struct work_interval *old_th_wi = thread->th_work_interval;
305
306 /* transfer +1 ref to thread */
307 thread->th_work_interval = work_interval;
308
309
310 if (old_th_wi != NULL) {
311 wi_release(old_th_wi);
312 }
313 }
314
315 void
316 work_interval_thread_terminate(thread_t thread)
317 {
318 if (thread->th_work_interval != NULL) {
319 thread_set_work_interval(thread, NULL);
320 }
321 }
322
323
324
325 kern_return_t
326 kern_work_interval_notify(thread_t thread, struct kern_work_interval_args* kwi_args)
327 {
328 assert(thread == current_thread());
329 assert(kwi_args->work_interval_id != 0);
330
331 struct work_interval *work_interval = thread->th_work_interval;
332
333 if (work_interval == NULL ||
334 work_interval->wi_id != kwi_args->work_interval_id) {
335 /* This thread must have adopted the work interval to be able to notify */
336 return KERN_INVALID_ARGUMENT;
337 }
338
339 task_t notifying_task = current_task();
340
341 if (work_interval->wi_creator_uniqueid != get_task_uniqueid(notifying_task) ||
342 work_interval->wi_creator_pidversion != get_task_version(notifying_task)) {
343 /* Only the creating task can do a notify */
344 return KERN_INVALID_ARGUMENT;
345 }
346
347 spl_t s = splsched();
348
349
350 uint64_t urgency_param1, urgency_param2;
351 kwi_args->urgency = thread_get_urgency(thread, &urgency_param1, &urgency_param2);
352
353 splx(s);
354
355 /* called without interrupts disabled */
356 machine_work_interval_notify(thread, kwi_args);
357
358 return KERN_SUCCESS;
359 }
360
361 /* Start at 1, 0 is not a valid work interval ID */
362 static _Atomic uint64_t unique_work_interval_id = 1;
363
364 kern_return_t
365 kern_work_interval_create(thread_t thread,
366 struct kern_work_interval_create_args *create_params)
367 {
368 assert(thread == current_thread());
369
370 if (thread->th_work_interval != NULL) {
371 /* already assigned a work interval */
372 return KERN_FAILURE;
373 }
374
375 struct work_interval *work_interval = kalloc(sizeof(*work_interval));
376
377 if (work_interval == NULL) {
378 panic("failed to allocate work_interval");
379 }
380
381 bzero(work_interval, sizeof(*work_interval));
382
383 uint64_t old_value = atomic_fetch_add_explicit(&unique_work_interval_id, 1,
384 memory_order_relaxed);
385
386 uint64_t work_interval_id = old_value + 1;
387
388 uint32_t create_flags = create_params->wica_create_flags;
389
390 task_t creating_task = current_task();
391 if ((create_flags & WORK_INTERVAL_TYPE_MASK) == WORK_INTERVAL_TYPE_CA_CLIENT) {
392 /*
393 * CA_CLIENT work intervals do not create new thread groups
394 * and are non-joinable.
395 * There can only be one CA_CLIENT work interval (created by UIKit)
396 * per each application task
397 */
398 if (create_flags & (WORK_INTERVAL_FLAG_JOINABLE | WORK_INTERVAL_FLAG_GROUP)) {
399 return KERN_FAILURE;
400 }
401 if (!task_is_app(creating_task)) {
402 return KERN_NOT_SUPPORTED;
403 }
404 if (task_set_ca_client_wi(creating_task, true) == false) {
405 return KERN_FAILURE;
406 }
407 }
408
409 *work_interval = (struct work_interval) {
410 .wi_id = work_interval_id,
411 .wi_ref_count = 1,
412 .wi_create_flags = create_flags,
413 .wi_creator_pid = pid_from_task(creating_task),
414 .wi_creator_uniqueid = get_task_uniqueid(creating_task),
415 .wi_creator_pidversion = get_task_version(creating_task),
416 };
417
418
419 if (create_flags & WORK_INTERVAL_FLAG_JOINABLE) {
420 /* work_interval has a +1 ref, moves to the port */
421 ipc_port_t port = work_interval_port_alloc(work_interval);
422 mach_port_name_t name = MACH_PORT_NULL;
423
424 name = ipc_port_copyout_send(port, current_space());
425
426 if (!MACH_PORT_VALID(name)) {
427 /*
428 * copyout failed (port is already deallocated)
429 * Because of the port-destroyed magic,
430 * the work interval is already deallocated too.
431 */
432 return KERN_RESOURCE_SHORTAGE;
433 }
434
435 create_params->wica_port = name;
436 } else {
437 /* work_interval has a +1 ref, moves to the thread */
438 thread_set_work_interval(thread, work_interval);
439 create_params->wica_port = MACH_PORT_NULL;
440 }
441
442 create_params->wica_id = work_interval_id;
443 return KERN_SUCCESS;
444 }
445
446
447 kern_return_t
448 kern_work_interval_destroy(thread_t thread, uint64_t work_interval_id)
449 {
450 if (work_interval_id == 0) {
451 return KERN_INVALID_ARGUMENT;
452 }
453
454 if (thread->th_work_interval == NULL ||
455 thread->th_work_interval->wi_id != work_interval_id) {
456 /* work ID isn't valid or doesn't match joined work interval ID */
457 return KERN_INVALID_ARGUMENT;
458 }
459
460 thread_set_work_interval(thread, NULL);
461
462 return KERN_SUCCESS;
463 }
464
465 kern_return_t
466 kern_work_interval_join(thread_t thread,
467 mach_port_name_t port_name)
468 {
469 struct work_interval *work_interval = NULL;
470 kern_return_t kr;
471
472 if (port_name == MACH_PORT_NULL) {
473 /* 'Un-join' the current work interval */
474 thread_set_work_interval(thread, NULL);
475 return KERN_SUCCESS;
476 }
477
478 kr = port_name_to_work_interval(port_name, &work_interval);
479 if (kr != KERN_SUCCESS) {
480 return kr;
481 }
482 /* work_interval has a +1 ref */
483
484 assert(work_interval != NULL);
485
486 thread_set_work_interval(thread, work_interval);
487
488 /* ref was consumed by passing it to the thread */
489
490 return KERN_SUCCESS;
491 }