1 | /* |
2 | * Copyright (c) 2012-2019 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 _BANK_BANK_INTERNAL_H_ |
30 | #define _BANK_BANK_INTERNAL_H_ |
31 | |
32 | #include <stdint.h> |
33 | #include <mach/mach_types.h> |
34 | |
35 | #ifdef MACH_KERNEL_PRIVATE |
36 | |
37 | #include <kern/thread.h> |
38 | #include <kern/thread_group.h> |
39 | #include <kern/locks.h> |
40 | #include <kern/queue.h> |
41 | #include <ipc/ipc_voucher.h> |
42 | #include <bank/bank_types.h> |
43 | |
44 | /* Default value for Voucher Attribute Manager for BANK */ |
45 | #define BANK_DEFAULT_VALUE NULL |
46 | #define BANK_DEFAULT_TASK_VALUE ((void *) 1) |
47 | |
48 | typedef mach_voucher_attr_value_handle_t bank_handle_t __kernel_ptr_semantics; |
49 | |
50 | #define BANK_TASK 0 |
51 | #define BANK_ACCOUNT 1 |
52 | |
53 | os_refgrp_decl(static, bank_elem_refgrp, "bank element" , NULL); |
54 | |
55 | struct bank_element { |
56 | unsigned int be_type:31, /* Type of element */ |
57 | be_voucher_ref:1; /* Voucher system holds a ref */ |
58 | os_ref_atomic_t be_refs; /* Ref count */ |
59 | unsigned int be_made; /* Made refs for voucher, Actual ref is only taken for voucher ref transition (0 to 1) */ |
60 | #if DEVELOPMENT || DEBUG |
61 | task_t be_task; /* Customer task, do not use it since ref is not taken on task */ |
62 | #endif |
63 | }; |
64 | |
65 | typedef struct bank_element * bank_element_t; |
66 | #define BANK_ELEMENT_NULL ((bank_element_t) 0) |
67 | |
68 | struct bank_task { |
69 | struct bank_element bt_elem; /* Bank element */ |
70 | struct proc_persona_info bt_proc_persona; /* Persona of the process */ |
71 | ledger_t bt_ledger; /* Ledger of the customer task */ |
72 | queue_head_t bt_accounts_to_pay; /* List of accounts worked for me and need to pay */ |
73 | queue_head_t bt_accounts_to_charge; /* List of accounts I did work and need to charge */ |
74 | decl_lck_mtx_data(, bt_acc_to_pay_lock); /* Lock to protect accounts to pay list */ |
75 | decl_lck_mtx_data(, bt_acc_to_charge_lock); /* Lock to protect accounts to charge list */ |
76 | uint32_t bt_persona_uid; /* Persona UID of the process */ |
77 | uint32_t bt_hasentitlement:1; /* If the secure persona entitlement is set on the task */ |
78 | #if CONFIG_COALITIONS && CONFIG_TELEMETRY |
79 | uint64_t bt_rsrc_coal_id; /* Task's resource coalition ID for microstackshot/telemetry */ |
80 | #endif /* CONFIG_COALITIONS && CONFIG_TELEMETRY */ |
81 | #if CONFIG_THREAD_GROUPS |
82 | struct thread_group * bt_thread_group; /* Task's home thread group pointer */ |
83 | #endif |
84 | #if DEVELOPMENT || DEBUG |
85 | queue_chain_t bt_global_elt; /* Element on the global bank task chain */ |
86 | #endif |
87 | }; |
88 | |
89 | #define bt_type bt_elem.be_type |
90 | #define bt_voucher_ref bt_elem.be_voucher_ref |
91 | #define bt_refs bt_elem.be_refs |
92 | #define bt_made bt_elem.be_made |
93 | |
94 | #define bt_flags bt_proc_persona.flags |
95 | #define bt_unique_pid bt_proc_persona.unique_pid |
96 | #define bt_pid bt_proc_persona.pid |
97 | #define bt_pidversion bt_proc_persona.pidversion |
98 | #define bt_persona_id bt_proc_persona.persona_id |
99 | #define bt_uid bt_proc_persona.uid |
100 | #define bt_gid bt_proc_persona.gid |
101 | #define bt_macho_uuid bt_proc_persona.macho_uuid |
102 | |
103 | #if DEVELOPMENT || DEBUG |
104 | #define bt_task bt_elem.be_task |
105 | #endif |
106 | |
107 | typedef struct bank_task * bank_task_t; |
108 | #define BANK_TASK_NULL ((bank_task_t) 0) |
109 | |
110 | #define bank_task_ref_init(elem) \ |
111 | (os_ref_init_raw(&(elem)->bt_refs, &bank_elem_refgrp)) |
112 | |
113 | #define bank_task_reference(elem) \ |
114 | (os_ref_retain_raw(&(elem)->bt_refs, &bank_elem_refgrp)) |
115 | |
116 | #define bank_task_release(elem) \ |
117 | (os_ref_release_raw(&(elem)->bt_refs, &bank_elem_refgrp)) |
118 | |
119 | #define bank_task_made_reference(elem) \ |
120 | (os_atomic_inc_orig(&(elem)->bt_made, relaxed)) |
121 | |
122 | #define bank_task_made_release(elem) \ |
123 | (os_atomic_dec_orig(&(elem)->bt_made, relaxed)) |
124 | |
125 | #define bank_task_made_release_num(elem, num) \ |
126 | (os_atomic_sub_orig(&(elem)->bt_made, (num), relaxed)) |
127 | |
128 | struct bank_persona { |
129 | uint32_t persona_id; |
130 | uint32_t persona_uid; |
131 | }; |
132 | |
133 | struct bank_account { |
134 | struct bank_element ba_elem; /* Bank element */ |
135 | ledger_t ba_bill; /* Temporary ledger i.e. chit */ |
136 | bank_task_t ba_merchant; /* Task who worked for me, who will charge me on behalf of */ |
137 | bank_task_t ba_holder; /* Credit Card task holder */ |
138 | bank_task_t ba_secureoriginator; /* Bank task of the secure originator */ |
139 | bank_task_t ba_proximateprocess; /* Process who propagated the voucher to us */ |
140 | queue_chain_t ba_next_acc_to_pay; /* Next account I need to pay to */ |
141 | queue_chain_t ba_next_acc_to_charge; /* Next account I need to charge to */ |
142 | #if CONFIG_THREAD_GROUPS |
143 | struct thread_group * ba_thread_group; /* thread group to be adopted */ |
144 | #endif |
145 | #if DEVELOPMENT || DEBUG |
146 | queue_chain_t ba_global_elt; /* Element on the global account chain */ |
147 | #endif |
148 | struct bank_persona ba_so_persona; /* Persona of ba_secureoriginator, |
149 | * unless modified by a entitled process */ |
150 | }; |
151 | |
152 | #define ba_type ba_elem.be_type |
153 | #define ba_voucher_ref ba_elem.be_voucher_ref |
154 | #define ba_refs ba_elem.be_refs |
155 | #define ba_made ba_elem.be_made |
156 | |
157 | #if DEVELOPMENT || DEBUG |
158 | #define ba_task ba_elem.be_task |
159 | #endif |
160 | |
161 | typedef struct bank_account * bank_account_t; |
162 | #define BANK_ACCOUNT_NULL ((bank_account_t) 0) |
163 | |
164 | #define bank_account_ref_init(elem) \ |
165 | (os_ref_init_raw(&(elem)->ba_refs, &bank_elem_refgrp)) |
166 | |
167 | #define bank_account_release(elem) \ |
168 | (os_ref_release_raw(&(elem)->ba_refs, &bank_elem_refgrp)) |
169 | |
170 | #define bank_account_made_reference(elem) \ |
171 | (os_atomic_inc_orig(&(elem)->ba_made, relaxed)) |
172 | |
173 | #define bank_account_made_release(elem) \ |
174 | (os_atomic_dec_orig(&(elem)->ba_made, relaxed)) |
175 | |
176 | #define bank_account_made_release_num(elem, num) \ |
177 | (os_atomic_sub_orig(&(elem)->ba_made, (num), relaxed)) |
178 | |
179 | struct _bank_ledger_indices { |
180 | int cpu_time; |
181 | int energy; |
182 | }; |
183 | |
184 | extern struct _bank_ledger_indices bank_ledgers; |
185 | |
186 | extern void bank_task_destroy(task_t); |
187 | extern void bank_task_initialize(task_t task); |
188 | extern void bank_billed_balance_safe(task_t task, uint64_t *cpu_time, uint64_t *energy); |
189 | extern void bank_billed_balance(bank_task_t bank_task, uint64_t *cpu_time, uint64_t *energy); |
190 | extern void bank_serviced_balance_safe(task_t task, uint64_t *cpu_time, uint64_t *energy); |
191 | extern void bank_serviced_balance(bank_task_t bank_task, uint64_t *cpu_time, uint64_t *energy); |
192 | extern kern_return_t bank_get_bank_ledger_thread_group_and_persona(ipc_voucher_t voucher, |
193 | ledger_t *bankledger, struct thread_group **banktg, uint32_t *persona_id); |
194 | extern uint64_t bank_get_bank_ledger_resource_coalition_id(ipc_voucher_t voucher); |
195 | extern void bank_swap_thread_bank_ledger(thread_t thread, ledger_t ledger); |
196 | #if CONFIG_PREADOPT_TG |
197 | extern kern_return_t |
198 | bank_get_preadopt_thread_group(ipc_voucher_t voucher, struct thread_group **banktg); |
199 | #endif |
200 | |
201 | #endif /* MACH_KERNEL_PRIVATE */ |
202 | #endif /* _BANK_BANK_INTERNAL_H_ */ |
203 | |