2 * Copyright (c) 2012-2013 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 <atm/atm_internal.h>
30 #include <mach/mach_types.h>
31 #include <mach/kern_return.h>
32 #include <ipc/ipc_port.h>
33 #include <mach/mach_vm.h>
34 #include <mach/vm_map.h>
35 #include <vm/vm_map.h>
36 #include <atm/atm_notification.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 <machine/commpage.h>
43 #define MAX_ATM_VALUES (2 * 4096)
44 #define MAX_TRACE_BUFFER_SIZE (0x40000000) /* Restrict to 1GB per task */
46 #define ATM_VALUE_TO_HANDLE(x) (CAST_DOWN(atm_voucher_id_t, (x)))
47 #define HANDLE_TO_ATM_VALUE(x) (CAST_DOWN(atm_value_t, (x)))
49 #define ATM_MAX_HASH_TABLE_SIZE (256)
50 #define AID_HASH_MASK (0xFF)
51 #define AID_TO_HASH(x) ((x) & (AID_HASH_MASK))
53 #define ATM_LIST_DEAD_MAX 15
55 #define AID_ARRAY_COUNT_MAX (256)
57 struct atm_value_hash atm_value_hash_table
[ATM_MAX_HASH_TABLE_SIZE
];
60 /* Global flag to disable ATM. ATM get value and memory registration will return error. */
61 boolean_t disable_atm
= FALSE
;
63 #if DEVELOPMENT || DEBUG
64 queue_head_t atm_descriptors_list
;
65 queue_head_t atm_values_list
;
68 ipc_voucher_attr_control_t voucher_attr_control
; /* communication channel from ATM to voucher system */
69 static zone_t atm_value_zone
, atm_descriptors_zone
, atm_link_objects_zone
;
71 static aid_t
get_aid();
72 static mach_atm_subaid_t
get_subaid();
73 static atm_value_t
atm_value_alloc_init(aid_t
);
74 static void atm_value_dealloc(atm_value_t atm_value
);
75 static void atm_hash_table_init();
76 static kern_return_t
atm_value_hash_table_insert(atm_value_t new_atm_value
);
77 static void atm_value_hash_table_delete(atm_value_t atm_value
);
78 static atm_value_t
get_atm_value_from_aid(aid_t aid
) __unused
;
79 static void atm_value_get_ref(atm_value_t atm_value
);
80 static kern_return_t
atm_listener_insert(atm_value_t atm_value
, atm_task_descriptor_t task_descriptor
, atm_guard_t guard
);
81 static void atm_listener_delete_all(atm_value_t atm_value
);
82 static atm_task_descriptor_t
atm_task_descriptor_alloc_init(mach_port_t trace_buffer
,uint64_t buffer_size
, __assert_only task_t task
);
83 static void atm_descriptor_get_reference(atm_task_descriptor_t task_descriptor
);
84 static void atm_task_descriptor_dealloc(atm_task_descriptor_t task_descriptor
);
85 static kern_return_t
atm_value_unregister(atm_value_t atm_value
, atm_task_descriptor_t task_descriptor
, atm_guard_t guard
);
86 static kern_return_t
atm_value_register(atm_value_t atm_value
, atm_task_descriptor_t task_descriptor
, atm_guard_t guard
);
87 static kern_return_t
atm_listener_delete(atm_value_t atm_value
, atm_task_descriptor_t task_descriptor
, atm_guard_t guard
);
88 static void atm_link_get_reference(atm_link_object_t link_object
) __unused
;
89 static void atm_link_dealloc(atm_link_object_t link_object
);
90 kern_return_t
atm_invoke_collection(atm_value_t atm_value
, mach_atm_subaid_t subaid
, uint32_t flags
);
91 kern_return_t
atm_send_user_notification(aid_t aid
, mach_atm_subaid_t sub_aid
, mach_port_t
*buffers_array
, uint64_t *sizes_array
, mach_msg_type_number_t count
, uint32_t flags
);
95 ipc_voucher_attr_manager_t __assert_only manager
,
96 mach_voucher_attr_key_t __assert_only key
,
97 mach_voucher_attr_value_handle_t value
,
98 mach_voucher_attr_value_reference_t sync
);
102 ipc_voucher_attr_manager_t __assert_only manager
,
103 mach_voucher_attr_key_t __assert_only key
,
104 mach_voucher_attr_recipe_command_t command
,
105 mach_voucher_attr_value_handle_array_t prev_values
,
106 mach_msg_type_number_t __assert_only prev_value_count
,
107 mach_voucher_attr_content_t recipe
,
108 mach_voucher_attr_content_size_t recipe_size
,
109 mach_voucher_attr_value_handle_t
*out_value
,
110 ipc_voucher_t
*out_value_voucher
);
114 ipc_voucher_attr_manager_t __assert_only manager
,
115 mach_voucher_attr_key_t __assert_only key
,
116 mach_voucher_attr_value_handle_array_t values
,
117 mach_msg_type_number_t value_count
,
118 mach_voucher_attr_recipe_command_t
*out_command
,
119 mach_voucher_attr_content_t out_recipe
,
120 mach_voucher_attr_content_size_t
*in_out_recipe_size
);
124 ipc_voucher_attr_manager_t __assert_only manager
,
125 mach_voucher_attr_key_t __assert_only key
,
126 mach_voucher_attr_value_handle_array_t values
,
127 mach_msg_type_number_t value_count
,
128 mach_voucher_attr_command_t command
,
129 mach_voucher_attr_content_t in_content
,
130 mach_voucher_attr_content_size_t in_content_size
,
131 mach_voucher_attr_content_t out_content
,
132 mach_voucher_attr_content_size_t
*in_out_content_size
);
135 atm_release(ipc_voucher_attr_manager_t __assert_only manager
);
138 * communication channel from voucher system to ATM
140 struct ipc_voucher_attr_manager atm_manager
= {
141 .ivam_release_value
= atm_release_value
,
142 .ivam_get_value
= atm_get_value
,
143 .ivam_extract_content
= atm_extract_content
,
144 .ivam_command
= atm_command
,
145 .ivam_release
= atm_release
,
148 #if DEVELOPMENT || DEBUG
149 decl_lck_mtx_data(, atm_descriptors_list_lock
);
150 decl_lck_mtx_data(, atm_values_list_lock
);
152 lck_grp_t atm_dev_lock_grp
;
153 lck_attr_t atm_dev_lock_attr
;
154 lck_grp_attr_t atm_dev_lock_grp_attr
;
157 extern vm_map_t kernel_map
;
159 * Global aid. Incremented on each get_aid.
164 * Global subaid. Incremented on each get_subaid.
166 mach_atm_subaid_t global_subaid
;
169 * Lock group attributes for atm sub system.
171 lck_grp_t atm_lock_grp
;
172 lck_attr_t atm_lock_attr
;
173 lck_grp_attr_t atm_lock_grp_attr
;
176 * Global that is set by diagnosticd and readable by userspace
179 static uint32_t atm_diagnostic_config
;
183 * Purpose: Initialize the atm subsystem.
189 kern_return_t kr
= KERN_SUCCESS
;
192 /* Disable atm if disable_atm present in device-tree properties or in boot-args */
193 if ((PE_get_default("kern.disable_atm", temp_buf
, sizeof(temp_buf
))) ||
194 (PE_parse_boot_argn("-disable_atm", temp_buf
, sizeof(temp_buf
)))) {
198 if (!PE_parse_boot_argn("atm_diagnostic_config", &atm_diagnostic_config
, sizeof(atm_diagnostic_config
))) {
199 if (!PE_get_default("kern.atm_diagnostic_config", &atm_diagnostic_config
, sizeof(atm_diagnostic_config
))) {
200 atm_diagnostic_config
= 0;
204 /* setup zones for descriptors, values and link objects */
205 atm_value_zone
= zinit(sizeof(struct atm_value
),
206 MAX_ATM_VALUES
* sizeof(struct atm_value
),
207 sizeof(struct atm_value
),
210 atm_descriptors_zone
= zinit(sizeof(struct atm_task_descriptor
),
211 MAX_ATM_VALUES
* sizeof(struct atm_task_descriptor
),
212 sizeof(struct atm_task_descriptor
),
213 "atm_task_descriptors");
215 atm_link_objects_zone
= zinit(sizeof(struct atm_link_object
),
216 MAX_ATM_VALUES
* sizeof(struct atm_link_object
),
217 sizeof(struct atm_link_object
),
220 /* Initialize atm lock group and lock attributes. */
221 lck_grp_attr_setdefault(&atm_lock_grp_attr
);
222 lck_grp_init(&atm_lock_grp
, "atm_lock", &atm_lock_grp_attr
);
223 lck_attr_setdefault(&atm_lock_attr
);
227 atm_hash_table_init();
229 #if DEVELOPMENT || DEBUG
230 /* Initialize global atm development lock group and lock attributes. */
231 lck_grp_attr_setdefault(&atm_dev_lock_grp_attr
);
232 lck_grp_init(&atm_dev_lock_grp
, "atm_dev_lock", &atm_dev_lock_grp_attr
);
233 lck_attr_setdefault(&atm_dev_lock_attr
);
235 lck_mtx_init(&atm_descriptors_list_lock
, &atm_dev_lock_grp
, &atm_dev_lock_attr
);
236 lck_mtx_init(&atm_values_list_lock
, &atm_dev_lock_grp
, &atm_dev_lock_attr
);
238 queue_init(&atm_descriptors_list
);
239 queue_init(&atm_values_list
);
242 /* Register the atm manager with the Vouchers sub system. */
243 kr
= ipc_register_well_known_mach_voucher_attr_manager(
246 MACH_VOUCHER_ATTR_KEY_ATM
,
247 &voucher_attr_control
);
248 if (kr
!= KERN_SUCCESS
)
249 panic("ATM subsystem initialization failed");
251 kprintf("ATM subsystem is initialized\n");
257 * ATM Resource Manager Routines.
262 * Routine: atm_release_value
263 * Purpose: Release a value, if sync matches the sync count in value.
264 * Returns: KERN_SUCCESS: on Successful deletion.
265 * KERN_FAILURE: if sync value does not matches.
269 ipc_voucher_attr_manager_t __assert_only manager
,
270 mach_voucher_attr_key_t __assert_only key
,
271 mach_voucher_attr_value_handle_t value
,
272 mach_voucher_attr_value_reference_t sync
)
274 atm_value_t atm_value
= ATM_VALUE_NULL
;
276 assert(MACH_VOUCHER_ATTR_KEY_ATM
== key
);
277 assert(manager
== &atm_manager
);
279 atm_value
= HANDLE_TO_ATM_VALUE(value
);
280 if (atm_value
== VAM_DEFAULT_VALUE
) {
281 /* Return success for default value */
285 if (atm_value
->sync
!= sync
) {
289 /* Deallocate the atm value. */
290 atm_value_hash_table_delete(atm_value
);
291 atm_value_dealloc(atm_value
);
297 * Routine: atm_get_value
301 ipc_voucher_attr_manager_t __assert_only manager
,
302 mach_voucher_attr_key_t __assert_only key
,
303 mach_voucher_attr_recipe_command_t command
,
304 mach_voucher_attr_value_handle_array_t prev_values
,
305 mach_msg_type_number_t __assert_only prev_value_count
,
306 mach_voucher_attr_content_t __unused recipe
,
307 mach_voucher_attr_content_size_t __unused recipe_size
,
308 mach_voucher_attr_value_handle_t
*out_value
,
309 ipc_voucher_t
*out_value_voucher
)
311 atm_value_t atm_value
= ATM_VALUE_NULL
;
312 mach_voucher_attr_value_handle_t atm_handle
;
313 atm_task_descriptor_t task_descriptor
= ATM_TASK_DESCRIPTOR_NULL
;
318 kern_return_t kr
= KERN_SUCCESS
;
320 assert(MACH_VOUCHER_ATTR_KEY_ATM
== key
);
321 assert(manager
== &atm_manager
);
323 /* never an out voucher */
324 *out_value_voucher
= IPC_VOUCHER_NULL
;
326 if (disable_atm
|| (atm_get_diagnostic_config() & ATM_TRACE_DISABLE
))
327 return KERN_NOT_SUPPORTED
;
331 case MACH_VOUCHER_ATTR_ATM_REGISTER
:
333 for (i
= 0; i
< prev_value_count
; i
++) {
334 atm_handle
= prev_values
[i
];
335 atm_value
= HANDLE_TO_ATM_VALUE(atm_handle
);
337 if (atm_value
== VAM_DEFAULT_VALUE
)
340 if (recipe_size
!= sizeof(atm_guard_t
)) {
341 kr
= KERN_INVALID_ARGUMENT
;
344 memcpy(&guard
, recipe
, sizeof(atm_guard_t
));
346 task
= current_task();
347 task_descriptor
= task
->atm_context
;
349 kr
= atm_value_register(atm_value
, task_descriptor
, guard
);
350 if (kr
!= KERN_SUCCESS
) {
354 /* Increment sync value. */
355 atm_sync_reference_internal(atm_value
);
357 *out_value
= atm_handle
;
361 *out_value
= ATM_VALUE_TO_HANDLE(VAM_DEFAULT_VALUE
);
364 case MACH_VOUCHER_ATTR_ATM_CREATE
:
366 /* Handle the old case where aid value is created in kernel */
367 if (recipe_size
== 0) {
369 } else if (recipe_size
== sizeof(aid_t
)) {
370 memcpy(&aid
, recipe
, sizeof(aid_t
));
372 kr
= KERN_INVALID_ARGUMENT
;
376 /* Allocate a new atm value. */
377 atm_value
= atm_value_alloc_init(aid
);
378 if (atm_value
== ATM_VALUE_NULL
) {
379 kr
= KERN_RESOURCE_SHORTAGE
;
383 kr
= atm_value_hash_table_insert(atm_value
);
384 if (kr
!= KERN_SUCCESS
) {
385 if (recipe_size
== 0) {
386 atm_value
->aid
= get_aid();
389 atm_value_dealloc(atm_value
);
393 *out_value
= ATM_VALUE_TO_HANDLE(atm_value
);
396 case MACH_VOUCHER_ATTR_ATM_NULL
:
398 kr
= KERN_INVALID_ARGUMENT
;
407 * Routine: atm_extract_content
408 * Purpose: Extract a set of aid from an array of voucher values.
409 * Returns: KERN_SUCCESS: on Success.
410 * KERN_FAILURE: one of the value is not present in the hash.
411 * KERN_NO_SPACE: insufficeint buffer provided to fill an array of aid.
415 ipc_voucher_attr_manager_t __assert_only manager
,
416 mach_voucher_attr_key_t __assert_only key
,
417 mach_voucher_attr_value_handle_array_t values
,
418 mach_msg_type_number_t value_count
,
419 mach_voucher_attr_recipe_command_t
*out_command
,
420 mach_voucher_attr_content_t out_recipe
,
421 mach_voucher_attr_content_size_t
*in_out_recipe_size
)
423 atm_value_t atm_value
;
424 mach_voucher_attr_value_handle_t atm_handle
;
427 assert(MACH_VOUCHER_ATTR_KEY_ATM
== key
);
428 assert(manager
== &atm_manager
);
430 for (i
= 0; i
< value_count
; i
++) {
431 atm_handle
= values
[i
];
432 atm_value
= HANDLE_TO_ATM_VALUE(atm_handle
);
433 if (atm_value
== VAM_DEFAULT_VALUE
)
436 if (( sizeof(aid_t
)) > *in_out_recipe_size
) {
437 *in_out_recipe_size
= 0;
438 return KERN_NO_SPACE
;
441 memcpy(&out_recipe
[0], &atm_value
->aid
, sizeof(aid_t
));
442 *out_command
= MACH_VOUCHER_ATTR_ATM_NULL
;
443 *in_out_recipe_size
= sizeof(aid_t
);
447 *in_out_recipe_size
= 0;
452 * Routine: atm_command
453 * Purpose: Execute a command against a set of ATM values.
454 * Returns: KERN_SUCCESS: On successful execution of command.
455 KERN_FAILURE: On failure.
459 ipc_voucher_attr_manager_t __assert_only manager
,
460 mach_voucher_attr_key_t __assert_only key
,
461 mach_voucher_attr_value_handle_array_t values
,
462 mach_msg_type_number_t value_count
,
463 mach_voucher_attr_command_t command
,
464 mach_voucher_attr_content_t in_content
,
465 mach_voucher_attr_content_size_t in_content_size
,
466 mach_voucher_attr_content_t out_content
,
467 mach_voucher_attr_content_size_t
*out_content_size
)
469 assert(MACH_VOUCHER_ATTR_KEY_ATM
== key
);
470 assert(manager
== &atm_manager
);
471 atm_value_t atm_value
= ATM_VALUE_NULL
;
473 mach_atm_subaid_t
*subaid_array
= NULL
;
474 mach_atm_subaid_t next_subaid
= 0;
475 uint32_t aid_array_count
= 0;
476 atm_task_descriptor_t task_descriptor
= ATM_TASK_DESCRIPTOR_NULL
;
478 uint32_t collection_flags
= ATM_ACTION_LOGFAIL
;
479 kern_return_t kr
= KERN_SUCCESS
;
483 case ATM_ACTION_COLLECT
:
484 collection_flags
= ATM_ACTION_COLLECT
;
487 case ATM_ACTION_LOGFAIL
: {
488 mach_atm_subaid_t sub_aid
= 0;
490 if (disable_atm
|| (atm_get_diagnostic_config() & ATM_TRACE_DISABLE
))
491 return KERN_NOT_SUPPORTED
;
493 /* find the first non-default atm_value */
494 for (i
= 0; i
< value_count
; i
++) {
495 atm_value
= HANDLE_TO_ATM_VALUE(values
[i
]);
496 if (atm_value
!= VAM_DEFAULT_VALUE
)
500 /* if we are not able to find any atm values
501 * in stack then this call was made in error
503 if (atm_value
== NULL
) {
507 if (in_content_size
>= sizeof(mach_atm_subaid_t
)) {
508 sub_aid
= *(mach_atm_subaid_t
*)(void *)in_content
;
511 *out_content_size
= 0;
512 kr
= atm_invoke_collection(atm_value
, sub_aid
, collection_flags
);
516 case ATM_FIND_MIN_SUB_AID
:
517 if ((in_content_size
/sizeof(aid_t
)) > (*out_content_size
/sizeof(mach_atm_subaid_t
)))
520 aid_array_count
= in_content_size
/ sizeof(aid_t
);
521 if (aid_array_count
> AID_ARRAY_COUNT_MAX
)
524 subaid_array
= (mach_atm_subaid_t
*) (void *) out_content
;
525 for (i
= 0; i
< aid_array_count
; i
++) {
526 subaid_array
[i
] = ATM_SUBAID32_MAX
;
529 *out_content_size
= aid_array_count
* sizeof(mach_atm_subaid_t
);
535 case ATM_ACTION_UNREGISTER
:
536 /* find the first non-default atm_value */
537 for (i
= 0; i
< value_count
; i
++) {
538 atm_value
= HANDLE_TO_ATM_VALUE(values
[i
]);
539 if (atm_value
!= VAM_DEFAULT_VALUE
)
543 /* if we are not able to find any atm values
544 * in stack then this call was made in error
546 if (atm_value
== NULL
) {
549 if (in_content
== NULL
|| in_content_size
!= sizeof(atm_guard_t
)){
550 return KERN_INVALID_ARGUMENT
;
553 memcpy(&guard
, in_content
, sizeof(atm_guard_t
));
554 task
= current_task();
555 task_descriptor
= task
->atm_context
;
557 kr
= atm_value_unregister(atm_value
, task_descriptor
, guard
);
561 case ATM_ACTION_REGISTER
:
562 for (i
= 0; i
< value_count
; i
++) {
563 atm_value
= HANDLE_TO_ATM_VALUE(values
[i
]);
564 if (atm_value
!= VAM_DEFAULT_VALUE
)
567 /* if we are not able to find any atm values
568 * in stack then this call was made in error
570 if (atm_value
== NULL
) {
573 if (in_content
== NULL
|| in_content_size
!= sizeof(atm_guard_t
)){
574 return KERN_INVALID_ARGUMENT
;
577 memcpy(&guard
, in_content
, sizeof(atm_guard_t
));
578 task
= current_task();
579 task_descriptor
= task
->atm_context
;
581 kr
= atm_value_register(atm_value
, task_descriptor
, guard
);
585 case ATM_ACTION_GETSUBAID
:
586 if (out_content
== NULL
|| *out_content_size
!= sizeof(mach_atm_subaid_t
))
589 next_subaid
= get_subaid();
590 memcpy(out_content
, &next_subaid
, sizeof(mach_atm_subaid_t
));
594 kr
= KERN_INVALID_ARGUMENT
;
604 ipc_voucher_attr_manager_t __assert_only manager
)
606 assert(manager
== &atm_manager
);
611 * Routine: atm_invoke_collection
612 * Purpose: Sends a notification with array of memory buffer.
613 * Note: may block till user daemon responds.
616 atm_invoke_collection(
617 atm_value_t atm_value
,
618 mach_atm_subaid_t sub_aid
,
621 aid_t aid
= atm_value
->aid
;
622 kern_return_t kr
= KERN_SUCCESS
;
623 uint32_t array_count
= 0, i
= 0, j
= 0, requestor_index
= 0;
624 uint64_t *sizes_array
= NULL
;
625 atm_link_object_t link_object
= NULL
;
626 mach_port_t
*mem_array
= NULL
;
627 boolean_t need_swap_first
= FALSE
;
628 atm_task_descriptor_t requesting_descriptor
= current_task()->atm_context
;
630 lck_mtx_lock(&atm_value
->listener_lock
);
631 array_count
= atm_value
->listener_count
;
632 lck_mtx_unlock(&atm_value
->listener_lock
);
634 if (array_count
== 0){
638 mem_array
= kalloc(sizeof(mach_port_t
) * array_count
);
639 if (mem_array
== NULL
){
640 return KERN_NO_SPACE
;
643 sizes_array
= kalloc(sizeof(uint64_t) * array_count
);
644 if (sizes_array
== NULL
){
645 kfree(mem_array
, sizeof(mach_port_t
) * array_count
);
646 return KERN_NO_SPACE
;
649 lck_mtx_lock(&atm_value
->listener_lock
);
650 queue_iterate(&atm_value
->listeners
, link_object
, atm_link_object_t
, listeners_element
) {
651 if (i
>= array_count
){
655 if (!need_swap_first
&& requesting_descriptor
== link_object
->descriptor
){
656 assert(requesting_descriptor
!= NULL
);
658 need_swap_first
= TRUE
;
661 sizes_array
[i
] = link_object
->descriptor
->trace_buffer_size
;
662 mem_array
[i
] = ipc_port_copy_send(link_object
->descriptor
->trace_buffer
);
663 if (!IPC_PORT_VALID(mem_array
[i
])){
668 lck_mtx_unlock(&atm_value
->listener_lock
);
671 * Swap the position of requesting task ahead, diagnostics can
672 * process its buffers the first.
674 if (need_swap_first
&& requestor_index
!= 0){
675 assert(requestor_index
< array_count
);
676 mach_port_t tmp_port
= mem_array
[0];
677 uint64_t tmp_size
= sizes_array
[0];
678 mem_array
[0] = mem_array
[requestor_index
];
679 sizes_array
[0] = sizes_array
[requestor_index
];
680 mem_array
[requestor_index
] = tmp_port
;
681 sizes_array
[requestor_index
] = tmp_size
;
685 kr
= atm_send_user_notification(aid
, sub_aid
, mem_array
, sizes_array
, i
, flags
);
688 for (j
= 0; j
< i
; j
++) {
689 if (mem_array
[j
] != NULL
)
690 ipc_port_release_send(mem_array
[j
]);
693 kfree(mem_array
, sizeof(mach_port_t
) * array_count
);
694 kfree(sizes_array
, sizeof(uint64_t) * array_count
);
700 * Routine: atm_send_user_notification
701 * Purpose: Make an upcall to user space daemon if its listening for atm notifications.
702 * Returns: KERN_SUCCESS for successful delivery.
703 * KERN_FAILURE if port is dead or NULL.
706 atm_send_user_notification(
708 mach_atm_subaid_t sub_aid
,
709 mach_port_t
*buffers_array
,
710 uint64_t *sizes_array
,
711 mach_msg_type_number_t count
,
714 mach_port_t user_port
;
716 thread_t th
= current_thread();
719 error
= host_get_atm_notification_port(host_priv_self(), &user_port
);
720 if ((error
!= KERN_SUCCESS
) || !IPC_PORT_VALID(user_port
)) {
724 /* Set the honor queue limit option on the thread. */
725 th
->options
|= TH_OPT_HONOR_QLIMIT
;
726 kr
= atm_collect_trace_info(user_port
, aid
, sub_aid
, flags
, buffers_array
, count
, sizes_array
, count
);
727 /* Make sure that honor queue limit option is unset on the thread. */
728 th
->options
&= (~TH_OPT_HONOR_QLIMIT
);
730 if (kr
== MACH_SEND_TIMED_OUT
) {
737 * Routine: atm_send_proc_inspect_notification
738 * Purpose: Make an upcall to user space daemon if its listening for trace
739 * notifications for per process inspection.
740 * Returns: KERN_SUCCESS for successful delivery.
741 * KERN_FAILURE if port is dead or NULL.
745 atm_send_proc_inspect_notification(
748 uint64_t traced_uniqueid
)
750 mach_port_t user_port
= MACH_PORT_NULL
;
751 mach_port_t memory_port
= MACH_PORT_NULL
;
753 atm_task_descriptor_t task_descriptor
= ATM_TASK_DESCRIPTOR_NULL
;
754 uint64_t buffer_size
= 0;
756 thread_t th
= current_thread();
758 if (disable_atm
|| (atm_get_diagnostic_config() & ATM_TRACE_DISABLE
))
759 return KERN_NOT_SUPPORTED
;
761 /* look for the requested memory in target task */
763 return KERN_INVALID_TASK
;
766 if (task
->atm_context
){
767 task_descriptor
= task
->atm_context
;
768 atm_descriptor_get_reference(task_descriptor
);
772 if (task_descriptor
== ATM_TASK_DESCRIPTOR_NULL
){
776 memory_port
= ipc_port_copy_send(task_descriptor
->trace_buffer
);
777 buffer_size
= task_descriptor
->trace_buffer_size
;
778 atm_task_descriptor_dealloc(task_descriptor
);
780 /* get the communication port */
781 error
= host_get_atm_notification_port(host_priv_self(), &user_port
);
782 if ((error
!= KERN_SUCCESS
) || !IPC_PORT_VALID(user_port
)) {
783 ipc_port_release_send(memory_port
);
787 /* Set the honor queue limit option on the thread. */
788 th
->options
|= TH_OPT_HONOR_QLIMIT
;
789 kr
= atm_inspect_process_buffer(user_port
, traced_pid
, traced_uniqueid
, buffer_size
, memory_port
);
790 /* Make sure that honor queue limit option is unset on the thread. */
791 th
->options
&= (~TH_OPT_HONOR_QLIMIT
);
793 if (kr
== MACH_SEND_TIMED_OUT
) {
797 ipc_port_release_send(memory_port
);
802 * Routine: atm_value_alloc_init
803 * Purpose: Allocates an atm value struct and initialize it.
804 * Returns: atm_value_t: On Success with a sync count on atm_value.
805 * ATM_VALUE_NULL: On failure.
808 atm_value_alloc_init(aid_t aid
)
810 atm_value_t new_atm_value
= ATM_VALUE_NULL
;
812 new_atm_value
= (atm_value_t
) zalloc(atm_value_zone
);
813 if (new_atm_value
== ATM_VALUE_NULL
)
814 panic("Ran out of ATM values structure.\n\n");
816 new_atm_value
->aid
= aid
;
817 queue_init(&new_atm_value
->listeners
);
818 new_atm_value
->sync
= 1;
819 new_atm_value
->listener_count
= 0;
820 new_atm_value
->reference_count
= 1;
821 lck_mtx_init(&new_atm_value
->listener_lock
, &atm_lock_grp
, &atm_lock_attr
);
823 #if DEVELOPMENT || DEBUG
824 lck_mtx_lock(&atm_values_list_lock
);
825 queue_enter(&atm_values_list
, new_atm_value
, atm_value_t
, value_elt
);
826 lck_mtx_unlock(&atm_values_list_lock
);
828 return new_atm_value
;
834 * Purpose: Increment the global aid counter and return it.
841 aid
= (aid_t
)OSIncrementAtomic64((SInt64
*)&global_aid
);
847 * Routine: get_subaid
848 * Purpose: Increment the global subaid counter and return it.
851 static mach_atm_subaid_t
854 mach_atm_subaid_t next_subaid
;
855 next_subaid
= (mach_atm_subaid_t
)OSIncrementAtomic64((SInt64
*)&global_subaid
);
861 * Routine: atm_value_dealloc
862 * Purpose: Drops the reference on atm value and deallocates.
863 * Deletes all the listeners on deallocation.
867 atm_value_dealloc(atm_value_t atm_value
)
869 if (0 < atm_value_release_internal(atm_value
)) {
873 assert(atm_value
->reference_count
== 0);
875 /* Free up the atm value and also remove all the listeners. */
876 atm_listener_delete_all(atm_value
);
878 lck_mtx_destroy(&atm_value
->listener_lock
, &atm_lock_grp
);
880 #if DEVELOPMENT || DEBUG
881 lck_mtx_lock(&atm_values_list_lock
);
882 queue_remove(&atm_values_list
, atm_value
, atm_value_t
, value_elt
);
883 lck_mtx_unlock(&atm_values_list_lock
);
885 zfree(atm_value_zone
, atm_value
);
891 * Routine: atm_hash_table_init
892 * Purpose: Initialize the atm aid hash table.
896 atm_hash_table_init()
900 for (i
= 0; i
< ATM_MAX_HASH_TABLE_SIZE
; i
++) {
901 queue_init(&atm_value_hash_table
[i
].hash_list
);
902 lck_mtx_init(&atm_value_hash_table
[i
].hash_list_lock
, &atm_lock_grp
, &atm_lock_attr
);
908 * Routine: atm_value_hash_table_insert
909 * Purpose: Insert an atm value in the hash table.
910 * Returns: KERN_SUCCESS on success.
911 * KERN_NAME_EXISTS if atm value already in the hash table.
914 atm_value_hash_table_insert(atm_value_t new_atm_value
)
917 atm_value_hash_t hash_list_head
;
918 aid_t aid
= new_atm_value
->aid
;
921 hash_index
= AID_TO_HASH(aid
);
922 hash_list_head
= &atm_value_hash_table
[hash_index
];
924 /* Lock the atm list and search for the aid. */
925 lck_mtx_lock(&hash_list_head
->hash_list_lock
);
927 queue_iterate(&hash_list_head
->hash_list
, next
, atm_value_t
, vid_hash_elt
) {
928 if (next
->aid
== aid
) {
930 * aid found. return error.
932 lck_mtx_unlock(&hash_list_head
->hash_list_lock
);
933 return (KERN_NAME_EXISTS
);
937 /* Enter the aid in hash and return success. */
938 queue_enter(&hash_list_head
->hash_list
, new_atm_value
, atm_value_t
, vid_hash_elt
);
939 lck_mtx_unlock(&hash_list_head
->hash_list_lock
);
945 * Routine: atm_value_hash_table_delete
946 * Purpose: Delete the atm value from the hash table.
950 atm_value_hash_table_delete(atm_value_t atm_value
)
953 atm_value_hash_t hash_list_head
;
954 aid_t aid
= atm_value
->aid
;
956 hash_index
= AID_TO_HASH(aid
);
957 hash_list_head
= &atm_value_hash_table
[hash_index
];
959 lck_mtx_lock(&hash_list_head
->hash_list_lock
);
960 queue_remove(&hash_list_head
->hash_list
, atm_value
, atm_value_t
, vid_hash_elt
);
961 lck_mtx_unlock(&hash_list_head
->hash_list_lock
);
966 * Routine: get_atm_value_from_aid
967 * Purpose: Search a given aid in atm value hash table and
968 * return the atm value stucture.
969 * Returns: atm value structure if aid found.
970 * ATM_VALUE_NULL: If aid not found in atm value hash table.
973 get_atm_value_from_aid(aid_t aid
)
976 atm_value_hash_t hash_list_head
;
979 hash_index
= AID_TO_HASH(aid
);
980 hash_list_head
= &atm_value_hash_table
[hash_index
];
982 /* Lock the atm list and search for the aid. */
983 lck_mtx_lock(&hash_list_head
->hash_list_lock
);
985 queue_iterate(&hash_list_head
->hash_list
, next
, atm_value_t
, vid_hash_elt
) {
986 if (next
->aid
== aid
) {
988 * Aid found. Incerease ref count and return
989 * the atm value structure.
991 atm_value_get_ref(next
);
992 lck_mtx_unlock(&hash_list_head
->hash_list_lock
);
996 lck_mtx_unlock(&hash_list_head
->hash_list_lock
);
997 return ATM_VALUE_NULL
;
1002 * Routine: atm_value_get_ref
1003 * Purpose: Get a reference on atm value.
1007 atm_value_get_ref(atm_value_t atm_value
)
1009 atm_value_reference_internal(atm_value
);
1014 * Routine: atm_listener_insert
1015 * Purpose: Insert a listener to an atm value.
1016 * Returns: KERN_SUCCESS on success.
1017 * KERN_FAILURE if the task is already present as a listener.
1019 static kern_return_t
1020 atm_listener_insert(
1021 atm_value_t atm_value
,
1022 atm_task_descriptor_t task_descriptor
,
1025 atm_link_object_t new_link_object
;
1026 atm_link_object_t next
, elem
;
1027 int32_t freed_count
= 0, dead_but_not_freed
= 0, listener_count
;
1028 boolean_t element_found
= FALSE
;
1029 queue_head_t free_listeners
;
1031 new_link_object
= (atm_link_object_t
) zalloc(atm_link_objects_zone
);
1032 new_link_object
->descriptor
= task_descriptor
;
1033 new_link_object
->reference_count
= 1;
1034 new_link_object
->guard
= guard
;
1036 /* Get a reference on the task descriptor */
1037 atm_descriptor_get_reference(task_descriptor
);
1038 queue_init(&free_listeners
);
1039 listener_count
= atm_value
->listener_count
;
1041 /* Check if the task is already on the listener list */
1042 lck_mtx_lock(&atm_value
->listener_lock
);
1044 next
= (atm_link_object_t
)(void *) queue_first(&atm_value
->listeners
);
1045 while (!queue_end(&atm_value
->listeners
, (queue_entry_t
)next
)) {
1047 next
= (atm_link_object_t
)(void *) queue_next(&next
->listeners_element
);
1049 /* Check for dead tasks */
1050 if (elem
->descriptor
->flags
== ATM_TASK_DEAD
) {
1051 if ((dead_but_not_freed
> ATM_LIST_DEAD_MAX
) || elem
->guard
== 0) {
1052 queue_remove(&atm_value
->listeners
, elem
, atm_link_object_t
, listeners_element
);
1053 queue_enter(&free_listeners
, elem
, atm_link_object_t
, listeners_element
);
1054 atm_listener_count_decr_internal(atm_value
);
1057 dead_but_not_freed
++;
1065 if (elem
->descriptor
== task_descriptor
) {
1066 /* Increment reference count on Link object. */
1067 atm_link_get_reference(elem
);
1069 /* Replace the guard with the new one, the old guard is anyways on unregister path. */
1070 elem
->guard
= guard
;
1071 element_found
= TRUE
;
1072 KERNEL_DEBUG_CONSTANT((ATM_CODE(ATM_GETVALUE_INFO
, (ATM_VALUE_REPLACED
))) | DBG_FUNC_NONE
,
1073 VM_KERNEL_ADDRPERM(atm_value
), atm_value
->aid
, guard
, 0, 0);
1078 if (element_found
) {
1079 lck_mtx_unlock(&atm_value
->listener_lock
);
1080 /* Drop the extra reference on task descriptor taken by this function. */
1081 atm_task_descriptor_dealloc(task_descriptor
);
1082 zfree(atm_link_objects_zone
, new_link_object
);
1084 KERNEL_DEBUG_CONSTANT((ATM_CODE(ATM_GETVALUE_INFO
, (ATM_VALUE_ADDED
))) | DBG_FUNC_NONE
,
1085 VM_KERNEL_ADDRPERM(atm_value
), atm_value
->aid
, guard
, 0, 0);
1087 queue_enter(&atm_value
->listeners
, new_link_object
, atm_link_object_t
, listeners_element
);
1088 atm_listener_count_incr_internal(atm_value
);
1089 lck_mtx_unlock(&atm_value
->listener_lock
);
1092 /* Free the link objects */
1093 while(!queue_empty(&free_listeners
)) {
1094 queue_remove_first(&free_listeners
, next
, atm_link_object_t
, listeners_element
);
1096 /* Deallocate the link object */
1097 atm_link_dealloc(next
);
1100 KERNEL_DEBUG_CONSTANT((ATM_CODE(ATM_SUBAID_INFO
, (ATM_LINK_LIST_TRIM
))) | DBG_FUNC_NONE
,
1101 listener_count
, freed_count
, dead_but_not_freed
, VM_KERNEL_ADDRPERM(atm_value
), 1);
1103 return KERN_SUCCESS
;
1108 * Routine: atm_listener_delete_all
1109 * Purpose: Deletes all the listeners for an atm value.
1113 atm_listener_delete_all(atm_value_t atm_value
)
1115 atm_link_object_t next
;
1117 while(!queue_empty(&atm_value
->listeners
)) {
1118 queue_remove_first(&atm_value
->listeners
, next
, atm_link_object_t
, listeners_element
);
1120 /* Deallocate the link object */
1121 atm_link_dealloc(next
);
1127 * Routine: atm_listener_delete
1128 * Purpose: Deletes a listerner for an atm value.
1129 * Returns: KERN_SUCCESS on successful unregister.
1130 * KERN_INVALID_VALUE on finding a different guard.
1131 * KERN_FAILURE on failure.
1133 static kern_return_t
1134 atm_listener_delete(
1135 atm_value_t atm_value
,
1136 atm_task_descriptor_t task_descriptor
,
1139 queue_head_t free_listeners
;
1140 atm_link_object_t next
, elem
;
1141 kern_return_t kr
= KERN_FAILURE
;
1143 queue_init(&free_listeners
);
1145 lck_mtx_lock(&atm_value
->listener_lock
);
1147 next
= (atm_link_object_t
)(void *) queue_first(&atm_value
->listeners
);
1148 while (!queue_end(&atm_value
->listeners
, (queue_entry_t
)next
)) {
1150 next
= (atm_link_object_t
)(void *) queue_next(&next
->listeners_element
);
1152 if (elem
->descriptor
== task_descriptor
) {
1153 if (elem
->guard
== guard
) {
1154 KERNEL_DEBUG_CONSTANT((ATM_CODE(ATM_UNREGISTER_INFO
,
1155 (ATM_VALUE_UNREGISTERED
))) | DBG_FUNC_NONE
,
1156 VM_KERNEL_ADDRPERM(atm_value
), atm_value
->aid
, guard
, elem
->reference_count
, 0);
1160 KERNEL_DEBUG_CONSTANT((ATM_CODE(ATM_UNREGISTER_INFO
,
1161 (ATM_VALUE_DIFF_MAILBOX
))) | DBG_FUNC_NONE
,
1162 VM_KERNEL_ADDRPERM(atm_value
), atm_value
->aid
, elem
->guard
, elem
->reference_count
, 0);
1163 kr
= KERN_INVALID_VALUE
;
1165 if (0 == atm_link_object_release_internal(elem
)) {
1166 queue_remove(&atm_value
->listeners
, elem
, atm_link_object_t
, listeners_element
);
1167 queue_enter(&free_listeners
, elem
, atm_link_object_t
, listeners_element
);
1168 atm_listener_count_decr_internal(atm_value
);
1173 lck_mtx_unlock(&atm_value
->listener_lock
);
1175 while(!queue_empty(&free_listeners
)) {
1176 queue_remove_first(&free_listeners
, next
, atm_link_object_t
, listeners_element
);
1178 /* Deallocate the link object */
1179 atm_link_dealloc(next
);
1186 * Routine: atm_descriptor_alloc_init
1187 * Purpose: Allocate an atm task descriptor and initialize it and takes a reference.
1188 * Returns: atm task descriptor: On success.
1191 static atm_task_descriptor_t
1192 atm_task_descriptor_alloc_init(
1193 mach_port_t trace_buffer
,
1194 uint64_t buffer_size
,
1195 task_t __assert_only task
)
1197 atm_task_descriptor_t new_task_descriptor
;
1199 new_task_descriptor
= (atm_task_descriptor_t
) zalloc(atm_descriptors_zone
);
1201 new_task_descriptor
->trace_buffer
= trace_buffer
;
1202 new_task_descriptor
->trace_buffer_size
= buffer_size
;
1203 new_task_descriptor
->reference_count
= 1;
1204 new_task_descriptor
->flags
= 0;
1205 lck_mtx_init(&new_task_descriptor
->lock
, &atm_lock_grp
, &atm_lock_attr
);
1207 #if DEVELOPMENT || DEBUG
1208 new_task_descriptor
->task
= task
;
1209 lck_mtx_lock(&atm_descriptors_list_lock
);
1210 queue_enter(&atm_descriptors_list
, new_task_descriptor
, atm_task_descriptor_t
, descriptor_elt
);
1211 lck_mtx_unlock(&atm_descriptors_list_lock
);
1214 return new_task_descriptor
;
1219 * Routine: atm_descriptor_get_reference
1220 * Purpose: Get a reference count on task descriptor.
1224 atm_descriptor_get_reference(atm_task_descriptor_t task_descriptor
)
1226 atm_task_desc_reference_internal(task_descriptor
);
1231 * Routine: atm_task_descriptor_dealloc
1232 * Prupose: Drops the reference on atm descriptor.
1236 atm_task_descriptor_dealloc(atm_task_descriptor_t task_descriptor
)
1238 if (0 < atm_task_desc_release_internal(task_descriptor
)) {
1242 assert(task_descriptor
->reference_count
== 0);
1244 #if DEVELOPMENT || DEBUG
1245 lck_mtx_lock(&atm_descriptors_list_lock
);
1246 queue_remove(&atm_descriptors_list
, task_descriptor
, atm_task_descriptor_t
, descriptor_elt
);
1247 lck_mtx_unlock(&atm_descriptors_list_lock
);
1249 /* release the send right for the named memory entry */
1250 ipc_port_release_send(task_descriptor
->trace_buffer
);
1251 lck_mtx_destroy(&task_descriptor
->lock
, &atm_lock_grp
);
1252 zfree(atm_descriptors_zone
, task_descriptor
);
1258 * Routine: atm_link_get_reference
1259 * Purpose: Get a reference count on atm link object.
1263 atm_link_get_reference(atm_link_object_t link_object
)
1265 atm_link_object_reference_internal(link_object
);
1270 * Routine: atm_link_dealloc
1271 * Prupose: Drops the reference on link object.
1275 atm_link_dealloc(atm_link_object_t link_object
)
1277 /* Drop the reference on atm task descriptor. */
1278 atm_task_descriptor_dealloc(link_object
->descriptor
);
1279 zfree(atm_link_objects_zone
, link_object
);
1284 * Routine: atm_register_trace_memory
1285 * Purpose: Registers trace memory for a task.
1286 * Returns: KERN_SUCCESS: on Success.
1287 * KERN_FAILURE: on Error.
1290 atm_register_trace_memory(
1292 uint64_t trace_buffer_address
,
1293 uint64_t buffer_size
)
1295 atm_task_descriptor_t task_descriptor
;
1296 mach_port_t trace_buffer
= MACH_PORT_NULL
;
1297 kern_return_t kr
= KERN_SUCCESS
;
1299 if (disable_atm
|| (atm_get_diagnostic_config() & ATM_TRACE_DISABLE
))
1300 return KERN_NOT_SUPPORTED
;
1302 if (task
!= current_task())
1303 return KERN_INVALID_ARGUMENT
;
1305 if (task
->atm_context
!= NULL
1306 || (void *)trace_buffer_address
== NULL
1308 || (buffer_size
& PAGE_MASK
) != 0
1309 || buffer_size
> MAX_TRACE_BUFFER_SIZE
) {
1310 return KERN_INVALID_ARGUMENT
;
1313 vm_map_t map
= current_map();
1314 memory_object_size_t mo_size
= (memory_object_size_t
) buffer_size
;
1315 kr
= mach_make_memory_entry_64(map
,
1317 (mach_vm_offset_t
)trace_buffer_address
,
1321 if (kr
!= KERN_SUCCESS
)
1324 task_descriptor
= atm_task_descriptor_alloc_init(trace_buffer
, buffer_size
, task
);
1325 if (task_descriptor
== ATM_TASK_DESCRIPTOR_NULL
) {
1326 ipc_port_release_send(trace_buffer
);
1327 return KERN_NO_SPACE
;
1331 if (task
->atm_context
== NULL
) {
1332 task
->atm_context
= task_descriptor
;
1339 if (kr
!= KERN_SUCCESS
) {
1340 /* undo the mapping and allocations since we failed to hook descriptor to task */
1341 atm_task_descriptor_dealloc(task_descriptor
);
1343 return KERN_SUCCESS
;
1347 * Routine: atm_set_diagnostic_config
1348 * Purpose: Set global atm_diagnostic_config and update the commpage to reflect
1350 * Returns: Error if ATM is disabled.
1352 extern uint32_t atm_diagnostic_config
; /* Proxied to commpage for fast user access */
1354 atm_set_diagnostic_config(uint32_t diagnostic_config
)
1357 return KERN_NOT_SUPPORTED
;
1359 atm_diagnostic_config
= diagnostic_config
;
1360 commpage_update_atm_diagnostic_config(atm_diagnostic_config
);
1362 return KERN_SUCCESS
;
1367 * Routine: atm_get_diagnostic_config
1368 * Purpose: Get global atm_diagnostic_config.
1369 * Returns: Diagnostic value
1372 atm_get_diagnostic_config(void)
1374 return atm_diagnostic_config
;
1379 * Routine: atm_value_unregister
1380 * Purpose: Unregisters a process from an activity id.
1381 * Returns: KERN_SUCCESS on successful unregister.
1382 * KERN_INVALID_VALUE on finding a diff guard.
1383 * KERN_FAILURE on failure.
1385 static kern_return_t
1386 atm_value_unregister(
1387 atm_value_t atm_value
,
1388 atm_task_descriptor_t task_descriptor
,
1393 if (task_descriptor
== ATM_TASK_DESCRIPTOR_NULL
)
1394 return KERN_INVALID_TASK
;
1396 kr
= atm_listener_delete(atm_value
, task_descriptor
, guard
);
1402 * Routine: atm_value_register
1403 * Purpose: Registers a process for an activity id.
1404 * Returns: KERN_SUCCESS on successful register.
1405 * KERN_INVALID_TASK on finding a null task atm context.
1406 * KERN_FAILURE on failure.
1408 static kern_return_t
1410 atm_value_t atm_value
,
1411 atm_task_descriptor_t task_descriptor
,
1416 if (task_descriptor
== ATM_TASK_DESCRIPTOR_NULL
)
1417 return KERN_INVALID_TASK
;
1419 kr
= atm_listener_insert(atm_value
, task_descriptor
, guard
);
1425 atm_task_descriptor_destroy(atm_task_descriptor_t task_descriptor
)
1427 /* Mark the task dead in the task descriptor to make task descriptor eligible for cleanup. */
1428 lck_mtx_lock(&task_descriptor
->lock
);
1429 task_descriptor
->flags
= ATM_TASK_DEAD
;
1430 lck_mtx_unlock(&task_descriptor
->lock
);
1432 atm_task_descriptor_dealloc(task_descriptor
);