1/*
2 * Copyright (c) 2012-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifndef _ATM_ATM_INTERNAL_H_
30#define _ATM_ATM_INTERNAL_H_
31
32#include <stdint.h>
33#include <mach/mach_types.h>
34#include <atm/atm_types.h>
35
36#ifdef	MACH_KERNEL_PRIVATE
37
38#include <kern/thread.h>
39#include <kern/locks.h>
40#include <kern/queue.h>
41#include <ipc/ipc_voucher.h>
42
43/* Flags for atm task descriptor */
44#define ATM_TASK_DEAD             0x1
45
46/* Default value for Voucher Attribute Manager for ATM */
47#define VAM_DEFAULT_VALUE NULL
48
49typedef mach_voucher_attr_value_handle_t atm_voucher_id_t;
50
51struct atm_task_descriptor {
52	decl_lck_mtx_data(,lock)             /* lock to protect reference count */
53	mach_port_t     trace_buffer;		 /* named memory entry registered by user */
54	uint64_t        trace_buffer_size;   /* size of the trace_buffer registered */
55	uint64_t        mailbox_array_size;	 /* Mailbox array size in bytes. */
56	void *          mailbox_kernel_addr; /* Kernel address where the mailbox is mapped. */
57	uint32_t        reference_count:31,
58	                flags:1;
59#if DEVELOPMENT || DEBUG
60	task_t          task;           /* task pointer for debugging purposes */
61	queue_chain_t   descriptor_elt; /* global chain of all descriptors */
62#endif
63};
64
65typedef struct atm_task_descriptor *atm_task_descriptor_t;
66#define ATM_TASK_DESCRIPTOR_NULL NULL
67
68struct atm_value {
69	aid_t 		 aid;			/* activity id */
70	queue_head_t 	 listeners;		/* List of listeners who register for this activity */
71	decl_lck_mtx_data( ,listener_lock)	/* Lock to protect listener list */
72	queue_chain_t 	 vid_hash_elt;		/* Next hash element in the global hash table */
73#if DEVELOPMENT || DEBUG
74	queue_chain_t	 value_elt;	 	/* global chain of all values */
75#endif
76	uint32_t	 sync;			/* Made ref count given to voucher sub system. */
77	uint32_t	 listener_count;	/* Number of Listerners listening on the value. */
78	int32_t		 reference_count;	/* use count on the atm value, 1 taken by the global hash table */
79};
80
81typedef struct atm_value *atm_value_t;
82#define ATM_VALUE_NULL NULL
83
84/* Flags for atm link objects */
85#define ATM_LINK_REMOVE             0x1
86
87struct atm_link_object {
88	atm_task_descriptor_t  descriptor;
89	void *                 mailbox;		     /* Offset in the mailbox registered by the user for an activity. */
90	uint32_t                reference_count;     /* Refernece count for link object */
91	uint8_t                flags;		     /* Flags used mark for deletion from the listener list */
92	queue_chain_t	       listeners_element;    /* Head is atm_value->listeners. */
93};
94
95typedef struct atm_link_object *atm_link_object_t;
96
97#define atm_link_object_reference_internal(elem)	\
98	(hw_atomic_add(&(elem)->reference_count, 1))
99
100#define atm_link_object_release_internal(elem)	\
101	(hw_atomic_sub(&(elem)->reference_count, 1))
102
103struct atm_value_hash {
104	queue_head_t	hash_list;
105	decl_lck_mtx_data(, hash_list_lock)	/* lock to protect bucket list. */
106};
107
108typedef struct atm_value_hash *atm_value_hash_t;
109
110void atm_init(void);
111void atm_task_descriptor_destroy(atm_task_descriptor_t task_descriptor);
112kern_return_t atm_register_trace_memory(task_t	task, uint64_t trace_buffer_address, uint64_t buffer_size, uint64_t mailbox_array_size);
113kern_return_t atm_send_proc_inspect_notification(task_t task, int32_t traced_pid, uint64_t traced_uniqueid);
114
115#endif /* MACH_KERNEL_PRIVATE */
116
117#endif /* _ATM_ATM_INTERNAL_H_ */
118