]> git.saurik.com Git - apple/xnu.git/blame - osfmk/ipc/ipc_voucher.h
xnu-7195.81.3.tar.gz
[apple/xnu.git] / osfmk / ipc / ipc_voucher.h
CommitLineData
fe8ab488
A
1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
fe8ab488
A
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.
0a7de745 14 *
fe8ab488
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
fe8ab488
A
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.
0a7de745 25 *
fe8ab488
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
0a7de745
A
28#ifndef _IPC_IPC_VOUCHER_H_
29#define _IPC_IPC_VOUCHER_H_
fe8ab488
A
30
31#include <mach/mach_types.h>
32#include <mach/mach_voucher_types.h>
33#include <mach/boolean.h>
34#include <ipc/ipc_types.h>
d9a64523 35#include <os/refcnt.h>
fe8ab488
A
36
37#ifdef MACH_KERNEL_PRIVATE
38
39#include <kern/queue.h>
40#include <kern/locks.h>
41#include <kern/simple_lock.h>
39037602 42#include <voucher/ipc_pthread_priority_types.h>
fe8ab488
A
43
44/* locking */
0a7de745
A
45extern lck_grp_t ipc_lck_grp;
46extern lck_attr_t ipc_lck_attr;
fe8ab488 47
fe8ab488
A
48/* some shorthand for longer types */
49typedef mach_voucher_attr_value_handle_t iv_value_handle_t;
50typedef mach_voucher_attr_value_reference_t iv_value_refs_t;
51
0a7de745
A
52typedef natural_t iv_index_t;
53#define IV_UNUSED_VALINDEX ((iv_index_t) 0)
54#define IV_UNUSED_KEYINDEX ((iv_index_t) ~0)
fe8ab488 55
0a7de745
A
56typedef iv_index_t *iv_entry_t;
57#define IVE_NULL ((iv_entry_t) 0)
fe8ab488
A
58
59#define IV_ENTRIES_INLINE MACH_VOUCHER_ATTR_KEY_NUM_WELL_KNOWN
60
61/*
62 * IPC Voucher
63 *
64 * Vouchers are a reference counted immutable (once-created) set of
65 * indexes to particular resource manager attribute values
66 * (which themselves are reference counted).
67 */
68struct ipc_voucher {
0a7de745
A
69 iv_index_t iv_hash; /* checksum hash */
70 iv_index_t iv_sum; /* checksum of values */
71 os_refcnt_t iv_refs; /* reference count */
72 iv_index_t iv_table_size; /* size of the voucher table */
73 iv_index_t iv_inline_table[IV_ENTRIES_INLINE];
74 iv_entry_t iv_table; /* table of voucher attr entries */
75 ipc_port_t iv_port; /* port representing the voucher */
76 queue_chain_t iv_hash_link; /* link on hash chain */
fe8ab488
A
77};
78
0a7de745 79#define IV_NULL IPC_VOUCHER_NULL
fe8ab488
A
80
81
82/*
83 * Voucher Attribute Cache Control Object
84 *
85 * This is where the Voucher system stores its caches/references to
86 * returned resource manager attribute values. Each value only appears
87 * once in the table. If a value is returned more than once by the
88 * resource manager, the voucher system will increase the reference
0a7de745 89 * on the previous value.
fe8ab488
A
90 *
91 * The voucher itself contains one entry per key, that indexes into
92 * this table.
93 *
94 * A voucher that does not have an explicit index for a given key
95 * is assumed to have a reference on slot zero - which is where the
96 * voucher system stores the default value for the given attribute
97 * (specified at the time of resource manager registration).
98 *
99 * The ivace_releasing field limits the entry to a single concurrent
100 * return. Without it, a previous release's reply might still be
101 * working its way back to the voucher code, and a subsequent get-
102 * value could return the same value as was previously returned. If
103 * the resource manager already knew that, it would return a failure
104 * on the return, and all is well. We just treat the additional made
105 * references on the value as we normally would. However, if the resource
106 * manager accepted the return, and the get-value response raced the
107 * release's reply, the newly made references will look like an extension
108 * of the old value's cache lifetime, rather than a new one. Dropping
0a7de745 109 * that new lifetime's references to zero would result in a second
fe8ab488
A
110 * release callback to the resource manager - this time with the wrong
111 * "made" reference count. We avoid the race with this flag.
112 */
113
114struct ivac_entry_s {
115 iv_value_handle_t ivace_value;
116 iv_value_refs_t ivace_layered:1, /* layered effective entry */
0a7de745
A
117 ivace_releasing:1, /* release in progress */
118 ivace_free:1, /* on freelist */
119 ivace_persist:1, /* Persist the entry, don't count made refs */
120 ivace_refs:28; /* reference count */
fe8ab488
A
121 union {
122 iv_value_refs_t ivaceu_made; /* made count (non-layered) */
123 iv_index_t ivaceu_layer; /* next effective layer (layered) */
124 } ivace_u;
125 iv_index_t ivace_next; /* hash or freelist */
126 iv_index_t ivace_index; /* hash head (independent) */
127};
128typedef struct ivac_entry_s ivac_entry;
129typedef ivac_entry *ivac_entry_t;
130
131#define ivace_made ivace_u.ivaceu_made
132#define ivace_layer ivace_u.ivaceu_layer
133
134#define IVACE_NULL ((ivac_entry_t) 0);
135
490019cf 136#define IVACE_REFS_MAX ((1 << 28) - 1)
fe8ab488
A
137
138#define IVAC_ENTRIES_MIN 512
139#define IVAC_ENTRIES_MAX 524288
140
141struct ipc_voucher_attr_control {
d9a64523 142 os_refcnt_t ivac_refs;
0a7de745
A
143 boolean_t ivac_is_growing; /* is the table being grown */
144 ivac_entry_t ivac_table; /* table of voucher attr value entries */
145 iv_index_t ivac_table_size; /* size of the attr value table */
146 iv_index_t ivac_init_table_size; /* size of the attr value table */
147 iv_index_t ivac_freelist; /* index of the first free element */
148 ipc_port_t ivac_port; /* port for accessing the cache control */
fe8ab488 149 lck_spin_t ivac_lock_data;
0a7de745 150 iv_index_t ivac_key_index; /* key index for this value */
fe8ab488
A
151};
152typedef ipc_voucher_attr_control_t iv_attr_control_t;
153
154#define IVAC_NULL IPC_VOUCHER_ATTR_CONTROL_NULL
155
156extern ipc_voucher_attr_control_t ivac_alloc(iv_index_t);
490019cf
A
157extern void ipc_voucher_receive_postprocessing(ipc_kmsg_t kmsg, mach_msg_option_t option);
158extern void ipc_voucher_send_preprocessing(ipc_kmsg_t kmsg);
39037602
A
159extern void mach_init_activity_id(void);
160extern kern_return_t ipc_get_pthpriority_from_kmsg_voucher(ipc_kmsg_t kmsg, ipc_pthread_priority_value_t *qos);
fe8ab488
A
161#define ivac_lock_init(ivac) \
162 lck_spin_init(&(ivac)->ivac_lock_data, &ipc_lck_grp, &ipc_lck_attr)
163#define ivac_lock_destroy(ivac) \
164 lck_spin_destroy(&(ivac)->ivac_lock_data, &ipc_lck_grp)
0a7de745
A
165#define ivac_lock(ivac) \
166 lck_spin_lock_grp(&(ivac)->ivac_lock_data, &ipc_lck_grp)
167#define ivac_lock_try(ivac) \
168 lck_spin_try_lock_grp(&(ivac)->ivac_lock_data, &ipc_lck_grp)
169#define ivac_unlock(ivac) \
fe8ab488 170 lck_spin_unlock(&(ivac)->ivac_lock_data)
0a7de745
A
171#define ivac_sleep(ivac) lck_spin_sleep_grp(&(ivac)->ivac_lock_data, \
172 LCK_SLEEP_DEFAULT, \
173 (event_t)(ivac), \
174 THREAD_UNINT, &ipc_lck_grp)
fe8ab488
A
175#define ivac_wakeup(ivac) thread_wakeup((event_t)(ivac))
176
177extern void ivac_dealloc(ipc_voucher_attr_control_t ivac);
178
179static inline void
180ivac_reference(ipc_voucher_attr_control_t ivac)
181{
0a7de745 182 if (ivac == IVAC_NULL) {
d9a64523 183 return;
0a7de745 184 }
d9a64523 185 os_ref_retain(&ivac->ivac_refs);
fe8ab488
A
186}
187
188static inline void
189ivac_release(ipc_voucher_attr_control_t ivac)
190{
0a7de745 191 if (IVAC_NULL == ivac) {
fe8ab488 192 return;
0a7de745 193 }
fe8ab488 194
d9a64523 195 if (os_ref_release(&ivac->ivac_refs) == 0) {
fe8ab488 196 ivac_dealloc(ivac);
d9a64523 197 }
fe8ab488
A
198}
199
200#define IVAM_NULL IPC_VOUCHER_ATTR_MANAGER_NULL
201
202/*
203 * IPC voucher Resource Manager table element
204 *
205 * Information Associated with a specific registration of
206 * a voucher resource manager.
207 *
208 * NOTE: For now, this table is indexed directly by the key. In the future,
209 * it will have to be growable and sparse by key. When that is implemented
210 * the index will be independent from the key (but there will be a hash to
211 * find the index by key).
212 */
213typedef struct ipc_voucher_global_table_element {
0a7de745
A
214 ipc_voucher_attr_manager_t ivgte_manager;
215 ipc_voucher_attr_control_t ivgte_control;
216 mach_voucher_attr_key_t ivgte_key;
fe8ab488
A
217} ipc_voucher_global_table_element;
218
219typedef ipc_voucher_global_table_element *ipc_voucher_global_table_element_t;
220
221#endif /* MACH_KERNEL_PRIVATE */
222
223/*
224 * IPC voucher attribute recipe
225 *
226 * In-kernel recipe format with an ipc_voucher_t pointer for the previous
227 * voucher reference.
228 */
229#pragma pack(1)
230typedef struct ipc_voucher_attr_recipe_data {
0a7de745 231 mach_voucher_attr_key_t key;
fe8ab488 232 mach_voucher_attr_recipe_command_t command;
0a7de745
A
233 ipc_voucher_t previous_voucher;
234 mach_voucher_attr_content_size_t content_size;
235 uint8_t content[];
fe8ab488
A
236} ipc_voucher_attr_recipe_data_t;
237typedef ipc_voucher_attr_recipe_data_t *ipc_voucher_attr_recipe_t;
238typedef mach_msg_type_number_t ipc_voucher_attr_recipe_size_t;
239
240typedef uint8_t *ipc_voucher_attr_raw_recipe_t;
241typedef ipc_voucher_attr_raw_recipe_t ipc_voucher_attr_raw_recipe_array_t;
242typedef mach_msg_type_number_t ipc_voucher_attr_raw_recipe_size_t;
243typedef mach_msg_type_number_t ipc_voucher_attr_raw_recipe_array_size_t;
244
245#pragma pack()
246
247/*
248 * In-kernel Resource Manager Definition
249 *
250 * In-kernel resource managers are defined by a v-table like structure for
251 * the three callouts supported by a resource manager (and release function).
252 *
253 * There is a single in-kernel resource manager that represents all the
254 * outside kernel managers (and reflects the calls through MIG to user-space).
255 */
256
257typedef kern_return_t (*ipc_voucher_attr_manager_release_value_t)(ipc_voucher_attr_manager_t,
0a7de745
A
258 mach_voucher_attr_key_t,
259 mach_voucher_attr_value_handle_t,
260 mach_voucher_attr_value_reference_t);
fe8ab488
A
261
262typedef kern_return_t (*ipc_voucher_attr_manager_get_value_t)(ipc_voucher_attr_manager_t,
0a7de745
A
263 mach_voucher_attr_key_t,
264 mach_voucher_attr_recipe_command_t,
265 mach_voucher_attr_value_handle_array_t,
266 mach_voucher_attr_value_handle_array_size_t,
267 mach_voucher_attr_content_t,
268 mach_voucher_attr_content_size_t,
269 mach_voucher_attr_value_handle_t *,
270 mach_voucher_attr_value_flags_t *,
271 ipc_voucher_t *);
fe8ab488
A
272
273typedef kern_return_t (*ipc_voucher_attr_manager_extract_content_t)(ipc_voucher_attr_manager_t,
0a7de745
A
274 mach_voucher_attr_key_t,
275 mach_voucher_attr_value_handle_array_t,
276 mach_voucher_attr_value_handle_array_size_t,
277 mach_voucher_attr_recipe_command_t *,
278 mach_voucher_attr_content_t,
279 mach_voucher_attr_content_size_t *);
fe8ab488
A
280
281typedef kern_return_t (*ipc_voucher_attr_manager_command_t)(ipc_voucher_attr_manager_t,
0a7de745
A
282 mach_voucher_attr_key_t,
283 mach_voucher_attr_value_handle_array_t,
284 mach_voucher_attr_value_handle_array_size_t,
285 mach_voucher_attr_command_t,
286 mach_voucher_attr_content_t,
287 mach_voucher_attr_content_size_t,
288 mach_voucher_attr_content_t,
289 mach_voucher_attr_content_size_t *);
fe8ab488
A
290
291typedef void (*ipc_voucher_attr_manager_release_t)(ipc_voucher_attr_manager_t);
292
490019cf
A
293typedef uint32_t ipc_voucher_attr_manager_flags;
294
fe8ab488 295struct ipc_voucher_attr_manager {
0a7de745
A
296 ipc_voucher_attr_manager_release_value_t ivam_release_value;
297 ipc_voucher_attr_manager_get_value_t ivam_get_value;
298 ipc_voucher_attr_manager_extract_content_t ivam_extract_content;
299 ipc_voucher_attr_manager_command_t ivam_command;
300 ipc_voucher_attr_manager_release_t ivam_release;
301 ipc_voucher_attr_manager_flags ivam_flags;
fe8ab488
A
302};
303
490019cf
A
304#define IVAM_FLAGS_NONE 0
305#define IVAM_FLAGS_SUPPORT_SEND_PREPROCESS 0x1
306#define IVAM_FLAGS_SUPPORT_RECEIVE_POSTPROCESS 0x2
307
fe8ab488
A
308__BEGIN_DECLS
309
310/* DEBUG/TRACE Convert from a port to a voucher */
311extern uintptr_t unsafe_convert_port_to_voucher(
0a7de745 312 ipc_port_t port);
fe8ab488
A
313
314/* Convert from a port to a voucher */
315extern ipc_voucher_t convert_port_to_voucher(
0a7de745 316 ipc_port_t port);
fe8ab488
A
317
318/* Convert from a port name to an ipc_voucher */
319extern ipc_voucher_t convert_port_name_to_voucher(
0a7de745 320 mach_port_name_t name);
fe8ab488
A
321
322/* add a reference to the specified voucher */
323extern void ipc_voucher_reference(
0a7de745 324 ipc_voucher_t voucher);
fe8ab488
A
325
326/* drop the voucher reference picked up above */
327extern void ipc_voucher_release(
0a7de745 328 ipc_voucher_t voucher);
fe8ab488
A
329
330/* deliver voucher notifications */
331extern void ipc_voucher_notify(
0a7de745 332 mach_msg_header_t *msg);
fe8ab488
A
333
334/* Convert from a voucher to a port */
335extern ipc_port_t convert_voucher_to_port(
0a7de745 336 ipc_voucher_t voucher);
fe8ab488
A
337
338/* convert from a voucher attribute control to a port */
339extern ipc_port_t convert_voucher_attr_control_to_port(
0a7de745 340 ipc_voucher_attr_control_t control);
fe8ab488
A
341
342/* add a reference to the specified voucher */
343extern void ipc_voucher_attr_control_reference(
0a7de745 344 ipc_voucher_attr_control_t control);
fe8ab488
A
345
346/* drop the reference picked up above */
347extern void ipc_voucher_attr_control_release(
0a7de745 348 ipc_voucher_attr_control_t control);
fe8ab488
A
349
350/* deliver voucher control notifications */
351extern void ipc_voucher_attr_control_notify(
0a7de745 352 mach_msg_header_t *msg);
fe8ab488
A
353
354/* convert from a port to a voucher attribute control */
355extern ipc_voucher_attr_control_t convert_port_to_voucher_attr_control(
0a7de745 356 ipc_port_t port);
fe8ab488
A
357
358/*
359 * In-kernel equivalents to the user syscalls
360 */
361extern kern_return_t
362ipc_create_mach_voucher(
0a7de745
A
363 ipc_voucher_attr_raw_recipe_array_t recipes,
364 ipc_voucher_attr_raw_recipe_array_size_t recipe_size,
365 ipc_voucher_t *new_voucher);
fe8ab488
A
366
367extern kern_return_t
368ipc_voucher_attr_control_create_mach_voucher(
0a7de745
A
369 ipc_voucher_attr_control_t control,
370 ipc_voucher_attr_raw_recipe_array_t recipes,
371 ipc_voucher_attr_raw_recipe_array_size_t recipe_size,
372 ipc_voucher_t *new_voucher);
fe8ab488 373
0a7de745 374extern kern_return_t
fe8ab488 375ipc_register_well_known_mach_voucher_attr_manager(
0a7de745
A
376 ipc_voucher_attr_manager_t manager,
377 mach_voucher_attr_value_handle_t default_value,
378 mach_voucher_attr_key_t key,
379 ipc_voucher_attr_control_t *control);
fe8ab488
A
380
381
382extern kern_return_t
383ipc_register_mach_voucher_attr_manager(
0a7de745
A
384 ipc_voucher_attr_manager_t manager,
385 mach_voucher_attr_value_handle_t default_value,
386 mach_voucher_attr_key_t *key,
387 ipc_voucher_attr_control_t *control);
fe8ab488
A
388
389__END_DECLS
0a7de745
A
390
391#endif /* _IPC_IPC_VOUCHER_H_ */