Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | /* * Copyright (c) 2012-2016 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <voucher/ipc_pthread_priority_types.h> #include <voucher/ipc_pthread_priority_internal.h> #include <mach/mach_types.h> #include <mach/kern_return.h> #include <ipc/ipc_port.h> #include <mach/mach_vm.h> #include <mach/vm_map.h> #include <vm/vm_map.h> #include <mach/host_priv.h> #include <mach/host_special_ports.h> #include <kern/host.h> #include <kern/kalloc.h> #include <kern/ledger.h> #include <sys/kdebug.h> #include <IOKit/IOBSD.h> #include <mach/mach_voucher_attr_control.h> ipc_voucher_attr_control_t ipc_pthread_priority_voucher_attr_control; /* communication channel from PTHPRIORITY to voucher system */ #define IPC_PTHREAD_PRIORITY_VALUE_TO_HANDLE(x) ((mach_voucher_attr_value_handle_t)(x)) #define HANDLE_TO_IPC_PTHREAD_PRIORITY_VALUE(x) ((ipc_pthread_priority_value_t)(x)) extern unsigned long pthread_priority_canonicalize(unsigned long priority, boolean_t for_propagation); kern_return_t ipc_pthread_priority_release_value( ipc_voucher_attr_manager_t __assert_only manager, mach_voucher_attr_key_t __assert_only key, mach_voucher_attr_value_handle_t value, mach_voucher_attr_value_reference_t sync); kern_return_t ipc_pthread_priority_get_value( ipc_voucher_attr_manager_t __assert_only manager, mach_voucher_attr_key_t __assert_only key, mach_voucher_attr_recipe_command_t command, mach_voucher_attr_value_handle_array_t prev_values, mach_msg_type_number_t __assert_only prev_value_count, mach_voucher_attr_content_t recipe, mach_voucher_attr_content_size_t recipe_size, mach_voucher_attr_value_handle_t *out_value, mach_voucher_attr_value_flags_t *out_flags, ipc_voucher_t *out_value_voucher); kern_return_t ipc_pthread_priority_extract_content( ipc_voucher_attr_manager_t __assert_only manager, mach_voucher_attr_key_t __assert_only key, mach_voucher_attr_value_handle_array_t values, mach_msg_type_number_t value_count, mach_voucher_attr_recipe_command_t *out_command, mach_voucher_attr_content_t out_recipe, mach_voucher_attr_content_size_t *in_out_recipe_size); kern_return_t ipc_pthread_priority_command( ipc_voucher_attr_manager_t __assert_only manager, mach_voucher_attr_key_t __assert_only key, mach_voucher_attr_value_handle_array_t values, mach_msg_type_number_t value_count, mach_voucher_attr_command_t command, mach_voucher_attr_content_t in_content, mach_voucher_attr_content_size_t in_content_size, mach_voucher_attr_content_t out_content, mach_voucher_attr_content_size_t *in_out_content_size); void ipc_pthread_priority_release(ipc_voucher_attr_manager_t __assert_only manager); /* * communication channel from voucher system to IPC_PTHREAD_PRIORITY */ struct ipc_voucher_attr_manager ipc_pthread_priority_manager = { .ivam_release_value = ipc_pthread_priority_release_value, .ivam_get_value = ipc_pthread_priority_get_value, .ivam_extract_content = ipc_pthread_priority_extract_content, .ivam_command = ipc_pthread_priority_command, .ivam_release = ipc_pthread_priority_release, .ivam_flags = IVAM_FLAGS_NONE, }; /* * Routine: ipc_pthread_priority_init * Purpose: Initialize the IPC_PTHREAD_PRIORITY subsystem. * Returns: None. */ void ipc_pthread_priority_init() { kern_return_t kr = KERN_SUCCESS; /* Register the ipc_pthread_priority manager with the Vouchers sub system. */ kr = ipc_register_well_known_mach_voucher_attr_manager( &ipc_pthread_priority_manager, 0, MACH_VOUCHER_ATTR_KEY_PTHPRIORITY, &ipc_pthread_priority_voucher_attr_control); if (kr != KERN_SUCCESS ) panic("IPC_PTHREAD_PRIORITY subsystem initialization failed"); kprintf("IPC_PTHREAD_PRIORITY subsystem is initialized\n"); return ; } /* * IPC_PTHREAD_PRIORITY Resource Manager Routines. */ /* * Routine: ipc_pthread_priority_release_value * Purpose: Release a value, if sync matches the sync count in value. * Returns: KERN_SUCCESS: on Successful deletion. * KERN_FAILURE: if sync value does not matches. */ kern_return_t ipc_pthread_priority_release_value( ipc_voucher_attr_manager_t __assert_only manager, mach_voucher_attr_key_t __assert_only key, mach_voucher_attr_value_handle_t value, mach_voucher_attr_value_reference_t sync) { assert(MACH_VOUCHER_ATTR_KEY_PTHPRIORITY == key); assert(manager == &ipc_pthread_priority_manager); ipc_pthread_priority_value_t ipc_pthread_priority_value = HANDLE_TO_IPC_PTHREAD_PRIORITY_VALUE(value); panic("ipc_pthread_priority_release_value called for a persistent PTHPRIORITY value %x with sync value %d\n", ipc_pthread_priority_value, sync); return KERN_FAILURE; } /* * Routine: ipc_pthread_priority_get_value */ kern_return_t ipc_pthread_priority_get_value( ipc_voucher_attr_manager_t __assert_only manager, mach_voucher_attr_key_t __assert_only key, mach_voucher_attr_recipe_command_t command, mach_voucher_attr_value_handle_array_t __unused prev_values, mach_msg_type_number_t __unused prev_value_count, mach_voucher_attr_content_t recipe, mach_voucher_attr_content_size_t recipe_size, mach_voucher_attr_value_handle_t *out_value, mach_voucher_attr_value_flags_t *out_flags, ipc_voucher_t *out_value_voucher) { kern_return_t kr = KERN_SUCCESS; ipc_pthread_priority_value_t ipc_pthread_priority_value; ipc_pthread_priority_value_t canonicalize_priority_value; assert(MACH_VOUCHER_ATTR_KEY_PTHPRIORITY == key); assert(manager == &ipc_pthread_priority_manager); /* never an out voucher */ *out_value_voucher = IPC_VOUCHER_NULL; *out_flags = MACH_VOUCHER_ATTR_VALUE_FLAGS_NONE; switch (command) { case MACH_VOUCHER_ATTR_PTHPRIORITY_CREATE: if (recipe_size != sizeof(ipc_pthread_priority_value_t)) { return KERN_INVALID_ARGUMENT; } memcpy(&ipc_pthread_priority_value, recipe, recipe_size); if (ipc_pthread_priority_value == PTHPRIORITY_ATTR_DEFAULT_VALUE) { *out_value = IPC_PTHREAD_PRIORITY_VALUE_TO_HANDLE(PTHPRIORITY_ATTR_DEFAULT_VALUE); return kr; } /* Callout to pthread kext to get the canonicalized value */ canonicalize_priority_value = (ipc_pthread_priority_value_t) pthread_priority_canonicalize( (unsigned long)ipc_pthread_priority_value, true); *out_value = IPC_PTHREAD_PRIORITY_VALUE_TO_HANDLE(canonicalize_priority_value); *out_flags = MACH_VOUCHER_ATTR_VALUE_FLAGS_PERSIST; return kr; default: kr = KERN_INVALID_ARGUMENT; break; } return kr; } /* * Routine: ipc_pthread_priority_extract_content * Purpose: Extract a set of pthread_priority value from an array of voucher values. * Returns: KERN_SUCCESS: on Success. * KERN_NO_SPACE: insufficeint buffer provided to fill an array of pthread_priority values. */ kern_return_t ipc_pthread_priority_extract_content( ipc_voucher_attr_manager_t __assert_only manager, mach_voucher_attr_key_t __assert_only key, mach_voucher_attr_value_handle_array_t values, mach_msg_type_number_t value_count, mach_voucher_attr_recipe_command_t *out_command, mach_voucher_attr_content_t out_recipe, mach_voucher_attr_content_size_t *in_out_recipe_size) { kern_return_t kr = KERN_SUCCESS; mach_msg_type_number_t i; ipc_pthread_priority_value_t ipc_pthread_priority_value; assert(MACH_VOUCHER_ATTR_KEY_PTHPRIORITY == key); assert(manager == &ipc_pthread_priority_manager); for (i = 0; i < value_count; i++) { ipc_pthread_priority_value = HANDLE_TO_IPC_PTHREAD_PRIORITY_VALUE(values[i]); if (ipc_pthread_priority_value == PTHPRIORITY_ATTR_DEFAULT_VALUE) { continue; } if (MACH_VOUCHER_PTHPRIORITY_CONTENT_SIZE > *in_out_recipe_size) { *in_out_recipe_size = 0; return KERN_NO_SPACE; } memcpy(&out_recipe[0], &ipc_pthread_priority_value, sizeof(ipc_pthread_priority_value)); *out_command = MACH_VOUCHER_ATTR_PTHPRIORITY_NULL; *in_out_recipe_size = (mach_voucher_attr_content_size_t)sizeof(ipc_pthread_priority_value); return kr; } *in_out_recipe_size = 0; return KERN_INVALID_VALUE; } /* * Routine: ipc_pthread_priority_command * Purpose: Execute a command against a set of PTHPRIORITY values. * Returns: KERN_SUCCESS: On successful execution of command. * KERN_FAILURE: On failure. */ kern_return_t ipc_pthread_priority_command( ipc_voucher_attr_manager_t __assert_only manager, mach_voucher_attr_key_t __assert_only key, mach_voucher_attr_value_handle_array_t __unused values, mach_msg_type_number_t __unused value_count, mach_voucher_attr_command_t __unused command, mach_voucher_attr_content_t __unused in_content, mach_voucher_attr_content_size_t __unused in_content_size, mach_voucher_attr_content_t __unused out_content, mach_voucher_attr_content_size_t __unused *out_content_size) { assert(MACH_VOUCHER_ATTR_KEY_PTHPRIORITY == key); assert(manager == &ipc_pthread_priority_manager); return KERN_FAILURE; } void ipc_pthread_priority_release( ipc_voucher_attr_manager_t __assert_only manager) { assert(manager == &ipc_pthread_priority_manager); } |