]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ledger.h
xnu-4903.231.4.tar.gz
[apple/xnu.git] / osfmk / kern / ledger.h
1 /*
2 * Copyright (c) 2010-2018 Apple Computer, 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 * @OSF_COPYRIGHT@
30 */
31
32 #ifndef _KERN_LEDGER_H_
33 #define _KERN_LEDGER_H_
34
35 #include <mach/mach_types.h> /* ledger_t */
36
37 #ifdef MACH_KERNEL_PRIVATE
38 #include <os/refcnt.h>
39 #endif /* MACH_KERNEL_PRIVATE */
40
41 #define LEDGER_INFO 0
42 #define LEDGER_ENTRY_INFO 1
43 #define LEDGER_TEMPLATE_INFO 2
44 #define LEDGER_LIMIT 3
45 /* LEDGER_MAX_CMD always tracks the index of the last ledger command. */
46 #define LEDGER_MAX_CMD LEDGER_LIMIT
47
48 #define LEDGER_NAME_MAX 32
49
50 struct ledger_info {
51 char li_name[LEDGER_NAME_MAX];
52 int64_t li_id;
53 int64_t li_entries;
54 };
55
56 struct ledger_template_info {
57 char lti_name[LEDGER_NAME_MAX];
58 char lti_group[LEDGER_NAME_MAX];
59 char lti_units[LEDGER_NAME_MAX];
60 };
61
62 #ifdef MACH_KERNEL_PRIVATE
63 /*
64 * These definitions are only here to allow pmap.c to determine the expected
65 * size of a ledger at build time. Direct access to ledger fields or to
66 * ledger entries is prohibited.
67 */
68
69 /*
70 * The explicit alignment is to ensure that atomic operations don't panic
71 * on ARM.
72 */
73 struct ledger_entry {
74 volatile uint32_t le_flags;
75 ledger_amount_t le_limit;
76 ledger_amount_t le_warn_level;
77 volatile ledger_amount_t le_credit __attribute__((aligned(8)));
78 volatile ledger_amount_t le_debit __attribute__((aligned(8)));
79 union {
80 struct {
81 /*
82 * XXX - the following two fields can go away if we move all of
83 * the refill logic into process policy
84 */
85 uint64_t le_refill_period;
86 uint64_t le_last_refill;
87 } le_refill;
88 struct {
89 ledger_amount_t le_lifetime_max; /* Process lifetime peak */
90 #if CONFIG_LEDGER_INTERVAL_MAX
91 ledger_amount_t le_interval_max; /* Interval peak XXX better name needed */
92 #endif
93 } _le_max;
94 } _le;
95 } __attribute__((aligned(8)));
96
97 struct ledger {
98 uint64_t l_id;
99 struct os_refcnt l_refs;
100 int32_t l_size;
101 struct ledger_template *l_template;
102 struct ledger_entry l_entries[0] __attribute__((aligned(8)));
103 };
104 #endif /* MACH_KERNEL_PRIVATE */
105
106 struct ledger_entry_info {
107 int64_t lei_balance;
108 int64_t lei_credit;
109 int64_t lei_debit;
110 uint64_t lei_limit;
111 uint64_t lei_refill_period; /* In nanoseconds */
112 uint64_t lei_last_refill; /* Time since last refill */
113 };
114
115 struct ledger_limit_args {
116 char lla_name[LEDGER_NAME_MAX];
117 uint64_t lla_limit;
118 uint64_t lla_refill_period;
119 };
120
121 #ifdef KERNEL_PRIVATE
122
123 typedef struct ledger_template *ledger_template_t;
124
125 #define LEDGER_VALID(ledger) (ledger != LEDGER_NULL)
126
127 /* Action to take when a ledger goes into deficit */
128 #define LEDGER_ACTION_IGNORE 0x0000
129 #define LEDGER_ACTION_BLOCK 0x0010
130 #define LEDGER_ACTION_CALLBACK 0x0020
131 #define LEDGER_ACTION_MASK 0x00f0
132
133 /*
134 * Types of warnings that trigger a callback.
135 */
136 #define LEDGER_WARNING_ROSE_ABOVE 1
137 #define LEDGER_WARNING_DIPPED_BELOW 2
138
139 typedef void (*ledger_callback_t)(int warning, const void * param0, const void *param1);
140
141 extern void ledger_init(void);
142
143 extern ledger_template_t ledger_template_create(const char *name);
144 extern void ledger_template_dereference(ledger_template_t template);
145 extern int ledger_entry_add(ledger_template_t template, const char *key,
146 const char *group, const char *units);
147 extern kern_return_t ledger_set_callback(ledger_template_t template, int entry,
148 ledger_callback_t callback, const void *param0, const void *param1);
149 extern kern_return_t ledger_track_maximum(ledger_template_t template, int entry,
150 int period_in_secs);
151 extern kern_return_t ledger_panic_on_negative(ledger_template_t template,
152 int entry);
153 extern kern_return_t ledger_track_credit_only(ledger_template_t template,
154 int entry);
155 extern int ledger_key_lookup(ledger_template_t template, const char *key);
156
157 /* value of entry type */
158 #define LEDGER_CREATE_ACTIVE_ENTRIES 0
159 #define LEDGER_CREATE_INACTIVE_ENTRIES 1
160 extern ledger_t ledger_instantiate(ledger_template_t template, int entry_type);
161 extern void ledger_template_complete(ledger_template_t template);
162 extern void ledger_template_complete_secure_alloc(ledger_template_t template);
163 extern kern_return_t ledger_disable_callback(ledger_t ledger, int entry);
164 extern kern_return_t ledger_enable_callback(ledger_t ledger, int entry);
165 extern kern_return_t ledger_get_limit(ledger_t ledger, int entry,
166 ledger_amount_t *limit);
167 extern kern_return_t ledger_set_limit(ledger_t ledger, int entry,
168 ledger_amount_t limit, uint8_t warn_level_percentage);
169 #if CONFIG_LEDGER_INTERVAL_MAX
170 extern kern_return_t ledger_get_interval_max(ledger_t ledger, int entry,
171 ledger_amount_t *max_interval_balance, int reset);
172 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
173 extern kern_return_t ledger_get_lifetime_max(ledger_t ledger, int entry,
174 ledger_amount_t *max_lifetime_balance);
175 extern kern_return_t ledger_get_actions(ledger_t ledger, int entry, int *actions);
176 extern kern_return_t ledger_set_action(ledger_t ledger, int entry, int action);
177 extern kern_return_t ledger_get_period(ledger_t ledger, int entry,
178 uint64_t *period);
179 extern kern_return_t ledger_set_period(ledger_t ledger, int entry,
180 uint64_t period);
181 extern kern_return_t ledger_disable_refill(ledger_t l, int entry);
182 extern kern_return_t ledger_entry_setactive(ledger_t ledger, int entry);
183 extern void ledger_check_new_balance(thread_t thread, ledger_t ledger, int entry);
184 extern kern_return_t ledger_credit(ledger_t ledger, int entry,
185 ledger_amount_t amount);
186 extern kern_return_t ledger_credit_nocheck(ledger_t ledger, int entry,
187 ledger_amount_t amount);
188 extern kern_return_t ledger_debit(ledger_t ledger, int entry,
189 ledger_amount_t amount);
190 extern kern_return_t ledger_debit_nocheck(ledger_t ledger, int entry,
191 ledger_amount_t amount);
192 extern kern_return_t ledger_credit_thread(thread_t thread, ledger_t ledger,
193 int entry, ledger_amount_t amount);
194 extern kern_return_t ledger_debit_thread(thread_t thread, ledger_t ledger,
195 int entry, ledger_amount_t amount);
196 extern kern_return_t ledger_zero_balance(ledger_t ledger, int entry);
197 extern kern_return_t ledger_get_entries(ledger_t ledger, int entry,
198 ledger_amount_t *credit, ledger_amount_t *debit);
199 extern kern_return_t ledger_get_balance(ledger_t ledger, int entry,
200 ledger_amount_t *balance);
201 extern kern_return_t ledger_reset_callback_state(ledger_t ledger, int entry);
202 extern kern_return_t ledger_disable_panic_on_negative(ledger_t ledger, int entry);
203 extern kern_return_t ledger_get_panic_on_negative(ledger_t ledger, int entry, int *panic_on_negative);
204
205 extern kern_return_t ledger_rollup(ledger_t to_ledger, ledger_t from_ledger);
206 extern kern_return_t ledger_rollup_entry(ledger_t to_ledger, ledger_t from_ledger, int entry);
207
208 extern void ledger_ast(thread_t thread);
209
210 extern int ledger_reference_count(ledger_t ledger);
211 extern kern_return_t ledger_reference(ledger_t ledger);
212 extern kern_return_t ledger_dereference(ledger_t ledger);
213
214 /* Support for ledger() syscall */
215 #ifdef LEDGER_DEBUG
216 extern int ledger_limit(task_t task, struct ledger_limit_args *args);
217 #endif
218 extern int ledger_info(task_t task, struct ledger_info *info);
219
220 extern int
221 ledger_get_task_entry_info_multiple(task_t task, void **buf, int *len);
222
223 extern void
224 ledger_get_entry_info(ledger_t ledger, int entry,
225 struct ledger_entry_info *lei);
226
227 extern int ledger_template_info(void **buf, int *len);
228
229 #endif /* KERNEL_PRIVATE */
230
231 #endif /* _KERN_LEDGER_H_ */