]> git.saurik.com Git - apple/xnu.git/blame - osfmk/bank/bank_internal.h
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / bank / bank_internal.h
CommitLineData
fe8ab488
A
1/*
2 * Copyright (c) 2012-2013 Apple 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 */
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
0a7de745 35#ifdef MACH_KERNEL_PRIVATE
fe8ab488
A
36
37#include <kern/thread.h>
5ba3f43e 38#include <kern/thread_group.h>
fe8ab488
A
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
490019cf
A
46#define BANK_DEFAULT_TASK_VALUE ((void *) 1)
47
fe8ab488
A
48typedef mach_voucher_attr_value_handle_t bank_handle_t;
49
50#define BANK_TASK 0
51#define BANK_ACCOUNT 1
52
53struct bank_element {
39037602 54 unsigned int be_type:31, /* Type of element */
0a7de745 55 be_voucher_ref:1; /* Voucher system holds a ref */
fe8ab488 56 int be_refs; /* Ref count */
39037602 57 unsigned int be_made; /* Made refs for voucher, Actual ref is only taken for voucher ref transition (0 to 1) */
fe8ab488
A
58#if DEVELOPMENT || DEBUG
59 task_t be_task; /* Customer task, do not use it since ref is not taken on task */
60#endif
61};
62
63typedef struct bank_element * bank_element_t;
64#define BANK_ELEMENT_NULL ((bank_element_t) 0)
65
66struct bank_task {
490019cf
A
67 struct bank_element bt_elem; /* Bank element */
68 struct proc_persona_info bt_proc_persona; /* Persona of the process */
d9a64523 69 ledger_t bt_ledger; /* Ledger of the customer task */
490019cf
A
70 queue_head_t bt_accounts_to_pay; /* List of accounts worked for me and need to pay */
71 queue_head_t bt_accounts_to_charge; /* List of accounts I did work and need to charge */
0a7de745
A
72 decl_lck_mtx_data(, bt_acc_to_pay_lock) /* Lock to protect accounts to pay list */
73 decl_lck_mtx_data(, bt_acc_to_charge_lock) /* Lock to protect accounts to charge list */
490019cf 74 uint8_t bt_hasentitlement; /* If the secure persona entitlement is set on the task */
fe8ab488 75#if DEVELOPMENT || DEBUG
490019cf 76 queue_chain_t bt_global_elt; /* Element on the global bank task chain */
fe8ab488
A
77#endif
78};
79
80#define bt_type bt_elem.be_type
39037602 81#define bt_voucher_ref bt_elem.be_voucher_ref
fe8ab488
A
82#define bt_refs bt_elem.be_refs
83#define bt_made bt_elem.be_made
490019cf
A
84
85#define bt_flags bt_proc_persona.flags
86#define bt_unique_pid bt_proc_persona.unique_pid
87#define bt_pid bt_proc_persona.pid
88#define bt_pidversion bt_proc_persona.pidversion
89#define bt_persona_id bt_proc_persona.persona_id
90#define bt_uid bt_proc_persona.uid
91#define bt_gid bt_proc_persona.gid
92#define bt_macho_uuid bt_proc_persona.macho_uuid
fe8ab488
A
93
94#if DEVELOPMENT || DEBUG
95#define bt_task bt_elem.be_task
96#endif
97
98typedef struct bank_task * bank_task_t;
99#define BANK_TASK_NULL ((bank_task_t) 0)
100
0a7de745
A
101#define bank_task_reference(elem) \
102 (OSAddAtomic(1, &(elem)->bt_refs))
fe8ab488 103
0a7de745
A
104#define bank_task_release(elem) \
105 (OSAddAtomic(-1, &(elem)->bt_refs))
fe8ab488 106
0a7de745
A
107#define bank_task_release_num(elem, num) \
108 (OSAddAtomic(-(num), &(elem)->bt_refs))
fe8ab488 109
0a7de745
A
110#define bank_task_made_reference(elem) \
111 (hw_atomic_add(&(elem)->bt_made, 1) - 1)
fe8ab488 112
0a7de745
A
113#define bank_task_made_release(elem) \
114 (hw_atomic_sub(&(elem)->bt_made, 1) + 1)
fe8ab488 115
0a7de745
A
116#define bank_task_made_release_num(elem, num) \
117 (hw_atomic_sub(&(elem)->bt_made, (num)) + (num))
fe8ab488
A
118
119
120struct bank_account {
121 struct bank_element ba_elem; /* Bank element */
122 ledger_t ba_bill; /* Temporary ledger i.e. chit */
123 bank_task_t ba_merchant; /* Task who worked for me, who will charge me on behalf of */
124 bank_task_t ba_holder; /* Credit Card task holder */
490019cf
A
125 bank_task_t ba_secureoriginator; /* Bank task of the secure originator */
126 bank_task_t ba_proximateprocess; /* Process who propagated the voucher to us */
fe8ab488
A
127 queue_chain_t ba_next_acc_to_pay; /* Next account I need to pay to */
128 queue_chain_t ba_next_acc_to_charge; /* Next account I need to charge to */
129#if DEVELOPMENT || DEBUG
130 queue_chain_t ba_global_elt; /* Element on the global account chain */
131#endif
132};
133
134#define ba_type ba_elem.be_type
39037602 135#define ba_voucher_ref ba_elem.be_voucher_ref
fe8ab488
A
136#define ba_refs ba_elem.be_refs
137#define ba_made ba_elem.be_made
fe8ab488
A
138
139#if DEVELOPMENT || DEBUG
140#define ba_task ba_elem.be_task
141#endif
142
143typedef struct bank_account * bank_account_t;
144#define BANK_ACCOUNT_NULL ((bank_account_t) 0)
145
0a7de745
A
146#define bank_account_reference(elem) \
147 (OSAddAtomic(1, &(elem)->ba_refs))
fe8ab488 148
0a7de745
A
149#define bank_account_release(elem) \
150 (OSAddAtomic(-1, &(elem)->ba_refs))
fe8ab488 151
0a7de745
A
152#define bank_account_release_num(elem, num) \
153 (OSAddAtomic(-(num), &(elem)->ba_refs))
fe8ab488 154
0a7de745
A
155#define bank_account_made_reference(elem) \
156 (hw_atomic_add(&(elem)->ba_made, 1) - 1)
fe8ab488 157
0a7de745
A
158#define bank_account_made_release(elem) \
159 (hw_atomic_sub(&(elem)->ba_made, 1) + 1)
fe8ab488 160
0a7de745
A
161#define bank_account_made_release_num(elem, num) \
162 (hw_atomic_sub(&(elem)->ba_made, (num)) + (num))
fe8ab488
A
163
164struct _bank_ledger_indices {
165 int cpu_time;
5ba3f43e 166 int energy;
fe8ab488
A
167};
168
169extern struct _bank_ledger_indices bank_ledgers;
170
171extern void bank_init(void);
490019cf
A
172extern void bank_task_destroy(task_t);
173extern void bank_task_initialize(task_t task);
5ba3f43e
A
174extern void bank_billed_balance_safe(task_t task, uint64_t *cpu_time, uint64_t *energy);
175extern void bank_billed_balance(bank_task_t bank_task, uint64_t *cpu_time, uint64_t *energy);
176extern void bank_serviced_balance_safe(task_t task, uint64_t *cpu_time, uint64_t *energy);
177extern void bank_serviced_balance(bank_task_t bank_task, uint64_t *cpu_time, uint64_t *energy);
178extern kern_return_t bank_get_bank_ledger_and_thread_group(ipc_voucher_t voucher,
0a7de745 179 ledger_t *bankledger, struct thread_group **banktg);
fe8ab488
A
180extern void bank_swap_thread_bank_ledger(thread_t thread, ledger_t ledger);
181
182#endif /* MACH_KERNEL_PRIVATE */
183#endif /* _BANK_BANK_INTERNAL_H_ */