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_convert
112 * Called with port locked, returns reference to work interval
113 * if indeed the port is a work interval kobject port
115 static struct work_interval
*
116 work_interval_port_convert_locked(ipc_port_t port
)
118 struct work_interval
*work_interval
= NULL
;
120 if (!IP_VALID(port
)) {
124 if (!ip_active(port
)) {
128 if (IKOT_WORK_INTERVAL
!= ip_kotype(port
)) {
132 work_interval
= (struct work_interval
*) ip_get_kobject(port
);
134 wi_retain(work_interval
);
136 return work_interval
;
140 * port_name_to_work_interval
142 * Description: Obtain a reference to the work_interval associated with a given port.
144 * Parameters: name A Mach port name to translate.
146 * Returns: NULL The given Mach port did not reference a work_interval.
147 * !NULL The work_interval that is associated with the Mach port.
150 port_name_to_work_interval(mach_port_name_t name
,
151 struct work_interval
**work_interval
)
153 if (!MACH_PORT_VALID(name
)) {
154 return KERN_INVALID_NAME
;
157 ipc_port_t port
= IPC_PORT_NULL
;
158 kern_return_t kr
= KERN_SUCCESS
;
160 kr
= ipc_port_translate_send(current_space(), name
, &port
);
161 if (kr
!= KERN_SUCCESS
) {
166 assert(IP_VALID(port
));
168 struct work_interval
*converted_work_interval
;
170 converted_work_interval
= work_interval_port_convert_locked(port
);
172 /* the port is valid, but doesn't denote a work_interval */
173 if (converted_work_interval
== NULL
) {
174 kr
= KERN_INVALID_CAPABILITY
;
179 if (kr
== KERN_SUCCESS
) {
180 *work_interval
= converted_work_interval
;
188 * work_interval_port_notify
190 * Description: Handle a no-senders notification for a work interval port.
191 * Destroys the port and releases its reference on the work interval.
193 * Parameters: msg A Mach no-senders notification message.
195 * Note: This assumes that there is only one create-right-from-work-interval point,
196 * if the ability to extract another send right after creation is added,
197 * this will have to change to handle make-send counts correctly.
200 work_interval_port_notify(mach_msg_header_t
*msg
)
202 mach_no_senders_notification_t
*notification
= (void *)msg
;
203 ipc_port_t port
= notification
->not_header
.msgh_remote_port
;
204 struct work_interval
*work_interval
= NULL
;
206 if (!IP_VALID(port
)) {
207 panic("work_interval_port_notify(): invalid port");
212 if (!ip_active(port
)) {
213 panic("work_interval_port_notify(): inactive port %p", port
);
216 if (ip_kotype(port
) != IKOT_WORK_INTERVAL
) {
217 panic("work_interval_port_notify(): not the right kobject: %p, %d\n",
218 port
, ip_kotype(port
));
221 if (port
->ip_mscount
!= notification
->not_count
) {
222 panic("work_interval_port_notify(): unexpected make-send count: %p, %d, %d",
223 port
, port
->ip_mscount
, notification
->not_count
);
226 if (port
->ip_srights
!= 0) {
227 panic("work_interval_port_notify(): unexpected send right count: %p, %d",
228 port
, port
->ip_srights
);
231 work_interval
= (struct work_interval
*) ip_get_kobject(port
);
233 if (work_interval
== NULL
) {
234 panic("work_interval_port_notify(): missing kobject: %p", port
);
237 ipc_kobject_set_atomically(port
, IKO_NULL
, IKOT_NONE
);
239 work_interval
->wi_port
= MACH_PORT_NULL
;
243 ipc_port_dealloc_kernel(port
);
244 wi_release(work_interval
);
248 * Change thread's bound work interval to the passed-in work interval
249 * Consumes +1 ref on work_interval
251 * May also pass NULL to un-set work_interval on the thread
253 * Will deallocate any old work interval on the thread
256 thread_set_work_interval(thread_t thread
,
257 struct work_interval
*work_interval
)
259 assert(thread
== current_thread());
261 struct work_interval
*old_th_wi
= thread
->th_work_interval
;
263 /* transfer +1 ref to thread */
264 thread
->th_work_interval
= work_interval
;
267 if (old_th_wi
!= NULL
) {
268 wi_release(old_th_wi
);
273 work_interval_thread_terminate(thread_t thread
)
275 if (thread
->th_work_interval
!= NULL
) {
276 thread_set_work_interval(thread
, NULL
);
283 kern_work_interval_notify(thread_t thread
, struct kern_work_interval_args
* kwi_args
)
285 assert(thread
== current_thread());
286 assert(kwi_args
->work_interval_id
!= 0);
288 struct work_interval
*work_interval
= thread
->th_work_interval
;
290 if (work_interval
== NULL
||
291 work_interval
->wi_id
!= kwi_args
->work_interval_id
) {
292 /* This thread must have adopted the work interval to be able to notify */
293 return KERN_INVALID_ARGUMENT
;
296 task_t notifying_task
= current_task();
298 if (work_interval
->wi_creator_uniqueid
!= get_task_uniqueid(notifying_task
) ||
299 work_interval
->wi_creator_pidversion
!= get_task_version(notifying_task
)) {
300 /* Only the creating task can do a notify */
301 return KERN_INVALID_ARGUMENT
;
304 spl_t s
= splsched();
307 uint64_t urgency_param1
, urgency_param2
;
308 kwi_args
->urgency
= thread_get_urgency(thread
, &urgency_param1
, &urgency_param2
);
312 /* called without interrupts disabled */
313 machine_work_interval_notify(thread
, kwi_args
);
318 /* Start at 1, 0 is not a valid work interval ID */
319 static _Atomic
uint64_t unique_work_interval_id
= 1;
322 kern_work_interval_create(thread_t thread
,
323 struct kern_work_interval_create_args
*create_params
)
325 assert(thread
== current_thread());
327 if (thread
->th_work_interval
!= NULL
) {
328 /* already assigned a work interval */
332 struct work_interval
*work_interval
= kalloc(sizeof(*work_interval
));
334 if (work_interval
== NULL
) {
335 panic("failed to allocate work_interval");
338 bzero(work_interval
, sizeof(*work_interval
));
340 uint64_t old_value
= atomic_fetch_add_explicit(&unique_work_interval_id
, 1,
341 memory_order_relaxed
);
343 uint64_t work_interval_id
= old_value
+ 1;
345 uint32_t create_flags
= create_params
->wica_create_flags
;
347 task_t creating_task
= current_task();
348 if ((create_flags
& WORK_INTERVAL_TYPE_MASK
) == WORK_INTERVAL_TYPE_CA_CLIENT
) {
350 * CA_CLIENT work intervals do not create new thread groups.
351 * There can only be one CA_CLIENT work interval (created by UIKit or AppKit)
352 * per each application task
354 if (create_flags
& WORK_INTERVAL_FLAG_GROUP
) {
357 if (!task_is_app(creating_task
)) {
358 return KERN_NOT_SUPPORTED
;
360 if (task_set_ca_client_wi(creating_task
, true) == false) {
365 *work_interval
= (struct work_interval
) {
366 .wi_id
= work_interval_id
,
368 .wi_create_flags
= create_flags
,
369 .wi_creator_pid
= pid_from_task(creating_task
),
370 .wi_creator_uniqueid
= get_task_uniqueid(creating_task
),
371 .wi_creator_pidversion
= get_task_version(creating_task
),
375 if (create_flags
& WORK_INTERVAL_FLAG_JOINABLE
) {
376 mach_port_name_t name
= MACH_PORT_NULL
;
378 /* work_interval has a +1 ref, moves to the port */
379 work_interval
->wi_port
= ipc_kobject_alloc_port(
380 (ipc_kobject_t
)work_interval
, IKOT_WORK_INTERVAL
,
381 IPC_KOBJECT_ALLOC_MAKE_SEND
| IPC_KOBJECT_ALLOC_NSREQUEST
);
383 name
= ipc_port_copyout_send(work_interval
->wi_port
, current_space());
385 if (!MACH_PORT_VALID(name
)) {
387 * copyout failed (port is already deallocated)
388 * Because of the port-destroyed magic,
389 * the work interval is already deallocated too.
391 return KERN_RESOURCE_SHORTAGE
;
394 create_params
->wica_port
= name
;
396 /* work_interval has a +1 ref, moves to the thread */
397 thread_set_work_interval(thread
, work_interval
);
398 create_params
->wica_port
= MACH_PORT_NULL
;
401 create_params
->wica_id
= work_interval_id
;
407 kern_work_interval_destroy(thread_t thread
, uint64_t work_interval_id
)
409 if (work_interval_id
== 0) {
410 return KERN_INVALID_ARGUMENT
;
413 if (thread
->th_work_interval
== NULL
||
414 thread
->th_work_interval
->wi_id
!= work_interval_id
) {
415 /* work ID isn't valid or doesn't match joined work interval ID */
416 return KERN_INVALID_ARGUMENT
;
419 thread_set_work_interval(thread
, NULL
);
425 kern_work_interval_join(thread_t thread
,
426 mach_port_name_t port_name
)
428 struct work_interval
*work_interval
= NULL
;
431 if (port_name
== MACH_PORT_NULL
) {
432 /* 'Un-join' the current work interval */
433 thread_set_work_interval(thread
, NULL
);
437 kr
= port_name_to_work_interval(port_name
, &work_interval
);
438 if (kr
!= KERN_SUCCESS
) {
441 /* work_interval has a +1 ref */
443 assert(work_interval
!= NULL
);
445 thread_set_work_interval(thread
, work_interval
);
447 /* ref was consumed by passing it to the thread */