2 * Copyright (c) 2012-2016 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@
29 #include <voucher/ipc_pthread_priority_types.h>
30 #include <voucher/ipc_pthread_priority_internal.h>
31 #include <mach/mach_types.h>
32 #include <mach/kern_return.h>
33 #include <ipc/ipc_port.h>
34 #include <mach/mach_vm.h>
35 #include <mach/vm_map.h>
36 #include <vm/vm_map.h>
37 #include <mach/host_priv.h>
38 #include <mach/host_special_ports.h>
39 #include <kern/host.h>
40 #include <kern/kalloc.h>
41 #include <kern/ledger.h>
42 #include <sys/kdebug.h>
43 #include <IOKit/IOBSD.h>
44 #include <mach/mach_voucher_attr_control.h>
45 #include <pthread/priority_private.h>
47 ipc_voucher_attr_control_t ipc_pthread_priority_voucher_attr_control
; /* communication channel from PTHPRIORITY to voucher system */
49 #define IPC_PTHREAD_PRIORITY_VALUE_TO_HANDLE(x) ((mach_voucher_attr_value_handle_t)(x))
50 #define HANDLE_TO_IPC_PTHREAD_PRIORITY_VALUE(x) ((ipc_pthread_priority_value_t)(x))
53 ipc_pthread_priority_release_value(
54 ipc_voucher_attr_manager_t __assert_only manager
,
55 mach_voucher_attr_key_t __assert_only key
,
56 mach_voucher_attr_value_handle_t value
,
57 mach_voucher_attr_value_reference_t sync
);
60 ipc_pthread_priority_get_value(
61 ipc_voucher_attr_manager_t __assert_only manager
,
62 mach_voucher_attr_key_t __assert_only key
,
63 mach_voucher_attr_recipe_command_t command
,
64 mach_voucher_attr_value_handle_array_t prev_values
,
65 mach_msg_type_number_t __assert_only prev_value_count
,
66 mach_voucher_attr_content_t recipe
,
67 mach_voucher_attr_content_size_t recipe_size
,
68 mach_voucher_attr_value_handle_t
*out_value
,
69 mach_voucher_attr_value_flags_t
*out_flags
,
70 ipc_voucher_t
*out_value_voucher
);
73 ipc_pthread_priority_extract_content(
74 ipc_voucher_attr_manager_t __assert_only manager
,
75 mach_voucher_attr_key_t __assert_only key
,
76 mach_voucher_attr_value_handle_array_t values
,
77 mach_msg_type_number_t value_count
,
78 mach_voucher_attr_recipe_command_t
*out_command
,
79 mach_voucher_attr_content_t out_recipe
,
80 mach_voucher_attr_content_size_t
*in_out_recipe_size
);
83 ipc_pthread_priority_command(
84 ipc_voucher_attr_manager_t __assert_only manager
,
85 mach_voucher_attr_key_t __assert_only key
,
86 mach_voucher_attr_value_handle_array_t values
,
87 mach_msg_type_number_t value_count
,
88 mach_voucher_attr_command_t command
,
89 mach_voucher_attr_content_t in_content
,
90 mach_voucher_attr_content_size_t in_content_size
,
91 mach_voucher_attr_content_t out_content
,
92 mach_voucher_attr_content_size_t
*in_out_content_size
);
95 ipc_pthread_priority_release(ipc_voucher_attr_manager_t __assert_only manager
);
98 * communication channel from voucher system to IPC_PTHREAD_PRIORITY
100 const struct ipc_voucher_attr_manager ipc_pthread_priority_manager
= {
101 .ivam_release_value
= ipc_pthread_priority_release_value
,
102 .ivam_get_value
= ipc_pthread_priority_get_value
,
103 .ivam_extract_content
= ipc_pthread_priority_extract_content
,
104 .ivam_command
= ipc_pthread_priority_command
,
105 .ivam_release
= ipc_pthread_priority_release
,
106 .ivam_flags
= IVAM_FLAGS_NONE
,
110 * Routine: ipc_pthread_priority_init
111 * Purpose: Initialize the IPC_PTHREAD_PRIORITY subsystem.
115 ipc_pthread_priority_init()
117 kern_return_t kr
= KERN_SUCCESS
;
119 /* Register the ipc_pthread_priority manager with the Vouchers sub system. */
120 kr
= ipc_register_well_known_mach_voucher_attr_manager(
121 &ipc_pthread_priority_manager
,
123 MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
,
124 &ipc_pthread_priority_voucher_attr_control
);
125 if (kr
!= KERN_SUCCESS
) {
126 panic("IPC_PTHREAD_PRIORITY subsystem initialization failed");
129 kprintf("IPC_PTHREAD_PRIORITY subsystem is initialized\n");
134 * IPC_PTHREAD_PRIORITY Resource Manager Routines.
139 * Routine: ipc_pthread_priority_release_value
140 * Purpose: Release a value, if sync matches the sync count in value.
141 * Returns: KERN_SUCCESS: on Successful deletion.
142 * KERN_FAILURE: if sync value does not matches.
145 ipc_pthread_priority_release_value(
146 ipc_voucher_attr_manager_t __assert_only manager
,
147 mach_voucher_attr_key_t __assert_only key
,
148 mach_voucher_attr_value_handle_t value
,
149 mach_voucher_attr_value_reference_t sync
)
151 assert(MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
== key
);
152 assert(manager
== &ipc_pthread_priority_manager
);
154 ipc_pthread_priority_value_t ipc_pthread_priority_value
= HANDLE_TO_IPC_PTHREAD_PRIORITY_VALUE(value
);
156 panic("ipc_pthread_priority_release_value called for a persistent PTHPRIORITY value %x with sync value %d\n", ipc_pthread_priority_value
, sync
);
161 * Routine: ipc_pthread_priority_get_value
164 ipc_pthread_priority_get_value(
165 ipc_voucher_attr_manager_t __assert_only manager
,
166 mach_voucher_attr_key_t __assert_only key
,
167 mach_voucher_attr_recipe_command_t command
,
168 mach_voucher_attr_value_handle_array_t __unused prev_values
,
169 mach_msg_type_number_t __unused prev_value_count
,
170 mach_voucher_attr_content_t recipe
,
171 mach_voucher_attr_content_size_t recipe_size
,
172 mach_voucher_attr_value_handle_t
*out_value
,
173 mach_voucher_attr_value_flags_t
*out_flags
,
174 ipc_voucher_t
*out_value_voucher
)
176 kern_return_t kr
= KERN_SUCCESS
;
177 ipc_pthread_priority_value_t ipc_pthread_priority_value
;
178 ipc_pthread_priority_value_t canonicalize_priority_value
;
180 assert(MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
== key
);
181 assert(manager
== &ipc_pthread_priority_manager
);
183 /* never an out voucher */
184 *out_value_voucher
= IPC_VOUCHER_NULL
;
185 *out_flags
= MACH_VOUCHER_ATTR_VALUE_FLAGS_NONE
;
188 case MACH_VOUCHER_ATTR_PTHPRIORITY_CREATE
:
190 if (recipe_size
!= sizeof(ipc_pthread_priority_value_t
)) {
191 return KERN_INVALID_ARGUMENT
;
194 memcpy(&ipc_pthread_priority_value
, recipe
, recipe_size
);
196 if (ipc_pthread_priority_value
== PTHPRIORITY_ATTR_DEFAULT_VALUE
) {
197 *out_value
= IPC_PTHREAD_PRIORITY_VALUE_TO_HANDLE(PTHPRIORITY_ATTR_DEFAULT_VALUE
);
201 /* Callout to pthread kext to get the canonicalized value */
202 canonicalize_priority_value
= (ipc_pthread_priority_value_t
)
203 _pthread_priority_normalize_for_ipc((unsigned long)ipc_pthread_priority_value
);
205 *out_value
= IPC_PTHREAD_PRIORITY_VALUE_TO_HANDLE(canonicalize_priority_value
);
206 *out_flags
= MACH_VOUCHER_ATTR_VALUE_FLAGS_PERSIST
;
210 kr
= KERN_INVALID_ARGUMENT
;
218 * Routine: ipc_pthread_priority_extract_content
219 * Purpose: Extract a set of pthread_priority value from an array of voucher values.
220 * Returns: KERN_SUCCESS: on Success.
221 * KERN_NO_SPACE: insufficeint buffer provided to fill an array of pthread_priority values.
224 ipc_pthread_priority_extract_content(
225 ipc_voucher_attr_manager_t __assert_only manager
,
226 mach_voucher_attr_key_t __assert_only key
,
227 mach_voucher_attr_value_handle_array_t values
,
228 mach_msg_type_number_t value_count
,
229 mach_voucher_attr_recipe_command_t
*out_command
,
230 mach_voucher_attr_content_t out_recipe
,
231 mach_voucher_attr_content_size_t
*in_out_recipe_size
)
233 kern_return_t kr
= KERN_SUCCESS
;
234 mach_msg_type_number_t i
;
235 ipc_pthread_priority_value_t ipc_pthread_priority_value
;
237 assert(MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
== key
);
238 assert(manager
== &ipc_pthread_priority_manager
);
240 for (i
= 0; i
< value_count
&& *in_out_recipe_size
> 0; i
++) {
241 ipc_pthread_priority_value
= HANDLE_TO_IPC_PTHREAD_PRIORITY_VALUE(values
[i
]);
243 if (ipc_pthread_priority_value
== PTHPRIORITY_ATTR_DEFAULT_VALUE
) {
247 if (MACH_VOUCHER_PTHPRIORITY_CONTENT_SIZE
> *in_out_recipe_size
) {
248 *in_out_recipe_size
= 0;
249 return KERN_NO_SPACE
;
252 memcpy(&out_recipe
[0], &ipc_pthread_priority_value
, sizeof(ipc_pthread_priority_value
));
253 *out_command
= MACH_VOUCHER_ATTR_PTHPRIORITY_NULL
;
254 *in_out_recipe_size
= (mach_voucher_attr_content_size_t
)sizeof(ipc_pthread_priority_value
);
258 *in_out_recipe_size
= 0;
259 return KERN_INVALID_VALUE
;
263 * Routine: ipc_pthread_priority_command
264 * Purpose: Execute a command against a set of PTHPRIORITY values.
265 * Returns: KERN_SUCCESS: On successful execution of command.
266 * KERN_FAILURE: On failure.
269 ipc_pthread_priority_command(
270 ipc_voucher_attr_manager_t __assert_only manager
,
271 mach_voucher_attr_key_t __assert_only key
,
272 mach_voucher_attr_value_handle_array_t __unused values
,
273 mach_msg_type_number_t __unused value_count
,
274 mach_voucher_attr_command_t __unused command
,
275 mach_voucher_attr_content_t __unused in_content
,
276 mach_voucher_attr_content_size_t __unused in_content_size
,
277 mach_voucher_attr_content_t __unused out_content
,
278 mach_voucher_attr_content_size_t __unused
*out_content_size
)
280 assert(MACH_VOUCHER_ATTR_KEY_PTHPRIORITY
== key
);
281 assert(manager
== &ipc_pthread_priority_manager
);
287 ipc_pthread_priority_release(
288 ipc_voucher_attr_manager_t __assert_only manager
)
290 assert(manager
== &ipc_pthread_priority_manager
);