2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <sys/work_interval.h>
32 #include <kern/work_interval.h>
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>
43 #include <mach/kern_return.h>
44 #include <mach/notify.h>
46 #include <stdatomic.h>
49 * Work Interval structs
51 * This struct represents a thread group and/or work interval context
52 * in a mechanism that is represented with a kobject.
54 * Every thread that has joined a WI has a +1 ref, and the port
55 * has a +1 ref as well.
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
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.
67 struct work_interval
{
69 _Atomic
uint32_t wi_ref_count
;
70 uint32_t wi_create_flags
;
72 /* for debugging purposes only, does not hold a ref on port */
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
80 uint64_t wi_creator_uniqueid
;
81 uint32_t wi_creator_pid
;
82 int wi_creator_pidversion
;
87 wi_retain(struct work_interval
*work_interval
)
90 old_count
= atomic_fetch_add_explicit(&work_interval
->wi_ref_count
,
91 1, memory_order_relaxed
);
92 assert(old_count
> 0);
96 wi_release(struct work_interval
*work_interval
)
99 old_count
= atomic_fetch_sub_explicit(&work_interval
->wi_ref_count
,
100 1, memory_order_relaxed
);
101 assert(old_count
> 0);
103 if (old_count
== 1) {
105 kfree(work_interval
, sizeof(struct work_interval
));
110 * work_interval_port_alloc
112 * Description: Obtain a send right for the given work interval struct.
114 * Parameters: work_interval - A work_interval struct
115 * Consumes a +1 ref count on work_interval, now owned by the port.
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.
122 work_interval_port_alloc(struct work_interval
*work_interval
)
124 ipc_port_t work_interval_port
= ipc_port_alloc_kernel();
126 if (work_interval_port
== IP_NULL
) {
127 panic("failed to allocate work interval port");
130 assert(work_interval
->wi_port
== IP_NULL
);
132 ip_lock(work_interval_port
);
133 ipc_kobject_set_atomically(work_interval_port
, (ipc_kobject_t
)work_interval
,
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
);
141 assert(old_notify_port
== IP_NULL
);
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
));
147 work_interval
->wi_port
= work_interval_port
;
153 * work_interval_port_convert
155 * Called with port locked, returns reference to work interval
156 * if indeed the port is a work interval kobject port
158 static struct work_interval
*
159 work_interval_port_convert_locked(ipc_port_t port
)
161 struct work_interval
*work_interval
= NULL
;
163 if (!IP_VALID(port
)) {
167 if (!ip_active(port
)) {
171 if (IKOT_WORK_INTERVAL
!= ip_kotype(port
)) {
175 work_interval
= (struct work_interval
*)port
->ip_kobject
;
177 wi_retain(work_interval
);
179 return work_interval
;
183 * port_name_to_work_interval
185 * Description: Obtain a reference to the work_interval associated with a given port.
187 * Parameters: name A Mach port name to translate.
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.
193 port_name_to_work_interval(mach_port_name_t name
,
194 struct work_interval
**work_interval
)
196 if (!MACH_PORT_VALID(name
)) {
197 return KERN_INVALID_NAME
;
200 ipc_port_t port
= IPC_PORT_NULL
;
201 kern_return_t kr
= KERN_SUCCESS
;
203 kr
= ipc_port_translate_send(current_space(), name
, &port
);
204 if (kr
!= KERN_SUCCESS
) {
209 assert(IP_VALID(port
));
211 struct work_interval
*converted_work_interval
;
213 converted_work_interval
= work_interval_port_convert_locked(port
);
215 /* the port is valid, but doesn't denote a work_interval */
216 if (converted_work_interval
== NULL
) {
217 kr
= KERN_INVALID_CAPABILITY
;
222 if (kr
== KERN_SUCCESS
) {
223 *work_interval
= converted_work_interval
;
231 * work_interval_port_notify
233 * Description: Handle a no-senders notification for a work interval port.
234 * Destroys the port and releases its reference on the work interval.
236 * Parameters: msg A Mach no-senders notification message.
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.
243 work_interval_port_notify(mach_msg_header_t
*msg
)
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
;
249 if (!IP_VALID(port
)) {
250 panic("work_interval_port_notify(): invalid port");
255 if (!ip_active(port
)) {
256 panic("work_interval_port_notify(): inactive port %p", port
);
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
));
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
);
269 if (port
->ip_srights
!= 0) {
270 panic("work_interval_port_notify(): unexpected send right count: %p, %d",
271 port
, port
->ip_srights
);
274 work_interval
= (struct work_interval
*)port
->ip_kobject
;
276 if (work_interval
== NULL
) {
277 panic("work_interval_port_notify(): missing kobject: %p", port
);
280 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
282 work_interval
->wi_port
= MACH_PORT_NULL
;
286 ipc_port_dealloc_kernel(port
);
287 wi_release(work_interval
);
291 * Change thread's bound work interval to the passed-in work interval
292 * Consumes +1 ref on work_interval
294 * May also pass NULL to un-set work_interval on the thread
296 * Will deallocate any old work interval on the thread
299 thread_set_work_interval(thread_t thread
,
300 struct work_interval
*work_interval
)
302 assert(thread
== current_thread());
304 struct work_interval
*old_th_wi
= thread
->th_work_interval
;
306 /* transfer +1 ref to thread */
307 thread
->th_work_interval
= work_interval
;
310 if (old_th_wi
!= NULL
) {
311 wi_release(old_th_wi
);
316 work_interval_thread_terminate(thread_t thread
)
318 if (thread
->th_work_interval
!= NULL
) {
319 thread_set_work_interval(thread
, NULL
);
326 kern_work_interval_notify(thread_t thread
, struct kern_work_interval_args
* kwi_args
)
328 assert(thread
== current_thread());
329 assert(kwi_args
->work_interval_id
!= 0);
331 struct work_interval
*work_interval
= thread
->th_work_interval
;
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
;
339 task_t notifying_task
= current_task();
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
;
347 spl_t s
= splsched();
350 uint64_t urgency_param1
, urgency_param2
;
351 kwi_args
->urgency
= thread_get_urgency(thread
, &urgency_param1
, &urgency_param2
);
355 /* called without interrupts disabled */
356 machine_work_interval_notify(thread
, kwi_args
);
361 /* Start at 1, 0 is not a valid work interval ID */
362 static _Atomic
uint64_t unique_work_interval_id
= 1;
365 kern_work_interval_create(thread_t thread
,
366 struct kern_work_interval_create_args
*create_params
)
368 assert(thread
== current_thread());
370 if (thread
->th_work_interval
!= NULL
) {
371 /* already assigned a work interval */
375 struct work_interval
*work_interval
= kalloc(sizeof(*work_interval
));
377 if (work_interval
== NULL
) {
378 panic("failed to allocate work_interval");
381 bzero(work_interval
, sizeof(*work_interval
));
383 uint64_t old_value
= atomic_fetch_add_explicit(&unique_work_interval_id
, 1,
384 memory_order_relaxed
);
386 uint64_t work_interval_id
= old_value
+ 1;
388 uint32_t create_flags
= create_params
->wica_create_flags
;
390 task_t creating_task
= current_task();
391 if ((create_flags
& WORK_INTERVAL_TYPE_MASK
) == WORK_INTERVAL_TYPE_CA_CLIENT
) {
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
398 if (create_flags
& (WORK_INTERVAL_FLAG_JOINABLE
| WORK_INTERVAL_FLAG_GROUP
)) {
401 if (!task_is_app(creating_task
)) {
402 return KERN_NOT_SUPPORTED
;
404 if (task_set_ca_client_wi(creating_task
, true) == false) {
409 *work_interval
= (struct work_interval
) {
410 .wi_id
= work_interval_id
,
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
),
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
;
424 name
= ipc_port_copyout_send(port
, current_space());
426 if (!MACH_PORT_VALID(name
)) {
428 * copyout failed (port is already deallocated)
429 * Because of the port-destroyed magic,
430 * the work interval is already deallocated too.
432 return KERN_RESOURCE_SHORTAGE
;
435 create_params
->wica_port
= name
;
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
;
442 create_params
->wica_id
= work_interval_id
;
448 kern_work_interval_destroy(thread_t thread
, uint64_t work_interval_id
)
450 if (work_interval_id
== 0) {
451 return KERN_INVALID_ARGUMENT
;
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
;
460 thread_set_work_interval(thread
, NULL
);
466 kern_work_interval_join(thread_t thread
,
467 mach_port_name_t port_name
)
469 struct work_interval
*work_interval
= NULL
;
472 if (port_name
== MACH_PORT_NULL
) {
473 /* 'Un-join' the current work interval */
474 thread_set_work_interval(thread
, NULL
);
478 kr
= port_name_to_work_interval(port_name
, &work_interval
);
479 if (kr
!= KERN_SUCCESS
) {
482 /* work_interval has a +1 ref */
484 assert(work_interval
!= NULL
);
486 thread_set_work_interval(thread
, work_interval
);
488 /* ref was consumed by passing it to the thread */