2 * Copyright (c) 2000-2006 Apple Computer, 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@
34 * This is a half-hearted attempt at providing the parts of the
35 * ledger facility to satisfy the ledger interfaces.
37 * This implementation basically leaves the (dysfunctional) ledgers
38 * unfunctional and are mearly here to satisfy the Mach spec interface
42 #include <mach/mach_types.h>
43 #include <mach/message.h>
44 #include <mach/port.h>
45 #include <mach/ledger_server.h>
47 #include <kern/mach_param.h>
48 #include <kern/misc_protos.h>
49 #include <kern/lock.h>
50 #include <kern/ipc_kobject.h>
51 #include <kern/host.h>
52 #include <kern/ledger.h>
53 #include <kern/kalloc.h>
55 #include <ipc/ipc_space.h>
56 #include <ipc/ipc_port.h>
58 ledger_t root_wired_ledger
;
59 ledger_t root_paged_ledger
;
62 /* Utility routine to handle entries to a ledger */
68 /* Need to lock the ledger */
72 if (ledger
->ledger_limit
!= LEDGER_ITEM_INFINITY
&&
73 ledger
->ledger_balance
+ amount
> ledger
->ledger_limit
) {
74 /* XXX this is where you do BAD things */
75 printf("Ledger limit exceeded ! ledger=%p lim=%d balance=%d\n",
76 ledger
, ledger
->ledger_limit
,
77 ledger
->ledger_balance
);
78 ledger_unlock(ledger
);
79 return(KERN_RESOURCE_SHORTAGE
);
81 if ((ledger
->ledger_balance
+ amount
)
82 < LEDGER_ITEM_INFINITY
)
83 ledger
->ledger_balance
+= amount
;
85 ledger
->ledger_balance
= LEDGER_ITEM_INFINITY
;
88 if (ledger
->ledger_balance
+ amount
> 0)
89 ledger
->ledger_balance
+= amount
;
91 ledger
->ledger_balance
= 0;
93 ledger_unlock(ledger
);
97 /* Utility routine to create a new ledger */
101 ledger_t ledger_ledger
,
102 ledger_t ledger_parent
)
106 ledger
= (ledger_t
)kalloc(sizeof(ledger_data_t
));
107 if (ledger
== LEDGER_NULL
)
110 ledger
->ledger_self
= ipc_port_alloc_kernel();
111 if (ledger
->ledger_self
== IP_NULL
) {
112 kfree(ledger
, sizeof(ledger_data_t
));
116 ledger_lock_init(ledger
);
117 ledger
->ledger_limit
= limit
;
118 ledger
->ledger_balance
= 0;
119 ledger
->ledger_service_port
= MACH_PORT_NULL
;
120 ledger
->ledger_ledger
= ledger_ledger
;
121 ledger
->ledger_parent
= ledger_parent
;
122 ipc_kobject_set(ledger
->ledger_self
, (ipc_kobject_t
)ledger
,
128 /* Utility routine to destroy a ledger */
133 /* XXX can be many send rights (copies) of this */
134 ipc_port_dealloc_kernel(ledger
->ledger_self
);
136 /* XXX release send right on service port */
137 kfree(ledger
, sizeof(*ledger
));
142 * Inititalize the ledger facility
144 void ledger_init(void)
147 * Allocate the root ledgers; wired and paged.
149 root_wired_ledger
= ledger_allocate(LEDGER_ITEM_INFINITY
,
150 LEDGER_NULL
, LEDGER_NULL
);
151 if (root_wired_ledger
== LEDGER_NULL
)
152 panic("can't allocate root (wired) ledger");
153 ipc_port_make_send(root_wired_ledger
->ledger_self
);
155 root_paged_ledger
= ledger_allocate(LEDGER_ITEM_INFINITY
,
156 LEDGER_NULL
, LEDGER_NULL
);
157 if (root_paged_ledger
== LEDGER_NULL
)
158 panic("can't allocate root (paged) ledger");
159 ipc_port_make_send(root_paged_ledger
->ledger_self
);
163 * Create a subordinate ledger
165 kern_return_t
ledger_create(
166 ledger_t parent_ledger
,
167 ledger_t ledger_ledger
,
168 ledger_t
*new_ledger
,
169 ledger_item_t transfer
)
171 if (parent_ledger
== LEDGER_NULL
)
172 return(KERN_INVALID_ARGUMENT
);
174 if (ledger_ledger
== LEDGER_NULL
)
175 return(KERN_INVALID_LEDGER
);
178 * Allocate a new ledger and change the ledger_ledger for
181 ledger_lock(ledger_ledger
);
182 if ((ledger_ledger
->ledger_limit
!= LEDGER_ITEM_INFINITY
) &&
183 (ledger_ledger
->ledger_balance
+ sizeof(ledger_data_t
) >
184 ledger_ledger
->ledger_limit
)) {
185 ledger_unlock(ledger_ledger
);
186 return(KERN_RESOURCE_SHORTAGE
);
189 *new_ledger
= ledger_allocate(LEDGER_ITEM_INFINITY
, ledger_ledger
, parent_ledger
);
190 if (*new_ledger
== LEDGER_NULL
) {
191 ledger_unlock(ledger_ledger
);
192 return(KERN_RESOURCE_SHORTAGE
);
196 * Now transfer the limit for the new ledger from the parent
198 ledger_lock(parent_ledger
);
199 if (parent_ledger
->ledger_limit
!= LEDGER_ITEM_INFINITY
) {
200 /* Would the existing balance exceed the new limit ? */
201 if (parent_ledger
->ledger_limit
- transfer
< parent_ledger
->ledger_balance
) {
202 ledger_unlock(parent_ledger
);
203 ledger_unlock(ledger_ledger
);
204 return(KERN_RESOURCE_SHORTAGE
);
206 if (parent_ledger
->ledger_limit
- transfer
> 0)
207 parent_ledger
->ledger_limit
-= transfer
;
209 parent_ledger
->ledger_limit
= 0;
211 (*new_ledger
)->ledger_limit
= transfer
;
213 /* Charge the ledger against the ledger_ledger */
214 ledger_ledger
->ledger_balance
+= sizeof(ledger_data_t
);
215 ledger_unlock(parent_ledger
);
217 ledger_unlock(ledger_ledger
);
219 return(KERN_SUCCESS
);
225 kern_return_t
ledger_terminate(
228 if (ledger
== LEDGER_NULL
)
229 return(KERN_INVALID_ARGUMENT
);
231 /* You can't deallocate kernel ledgers */
232 if (ledger
== root_wired_ledger
||
233 ledger
== root_paged_ledger
)
234 return(KERN_INVALID_LEDGER
);
236 /* Lock the ledger */
239 /* the parent ledger gets back the limit */
240 ledger_lock(ledger
->ledger_parent
);
241 if (ledger
->ledger_parent
->ledger_limit
!= LEDGER_ITEM_INFINITY
) {
242 assert((natural_t
)(ledger
->ledger_parent
->ledger_limit
+
243 ledger
->ledger_limit
) <
244 LEDGER_ITEM_INFINITY
);
245 ledger
->ledger_parent
->ledger_limit
+= ledger
->ledger_limit
;
247 ledger_unlock(ledger
->ledger_parent
);
250 * XXX The spec says that you have to destroy all objects that
251 * have been created with this ledger. Nice work eh? For now
252 * Transfer the balance to the parent and let it worry about
255 /* XXX the parent ledger inherits the debt ?? */
256 (void) ledger_enter(ledger
->ledger_parent
, ledger
->ledger_balance
);
258 /* adjust the balance of the creation ledger */
259 (void) ledger_enter(ledger
->ledger_ledger
, -sizeof(*ledger
));
261 /* delete the ledger */
262 ledger_deallocate(ledger
);
264 return(KERN_SUCCESS
);
268 * Return the ledger limit and balance
270 kern_return_t
ledger_read(
272 ledger_item_t
*balance
,
273 ledger_item_t
*limit
)
275 if (ledger
== LEDGER_NULL
)
276 return(KERN_INVALID_ARGUMENT
);
279 *balance
= ledger
->ledger_balance
;
280 *limit
= ledger
->ledger_limit
;
281 ledger_unlock(ledger
);
283 return(KERN_SUCCESS
);
287 * Transfer resources from a parent ledger to a child
289 kern_return_t
ledger_transfer(
290 ledger_t parent_ledger
,
291 ledger_t child_ledger
,
292 ledger_item_t transfer
)
294 #define abs(v) ((v) > 0)?(v):-(v)
297 ledger_item_t amount
= abs(transfer
);
299 if (parent_ledger
== LEDGER_NULL
)
300 return(KERN_INVALID_ARGUMENT
);
302 if (child_ledger
== LEDGER_NULL
)
303 return(KERN_INVALID_ARGUMENT
);
305 /* Must be different ledgers */
306 if (parent_ledger
== child_ledger
)
307 return(KERN_INVALID_ARGUMENT
);
310 return(KERN_SUCCESS
);
312 ledger_lock(child_ledger
);
313 ledger_lock(parent_ledger
);
315 /* XXX Should be the parent you created it from ?? */
316 if (parent_ledger
!= child_ledger
->ledger_parent
) {
317 ledger_unlock(parent_ledger
);
318 ledger_unlock(child_ledger
);
319 return(KERN_INVALID_LEDGER
);
328 dest
= parent_ledger
;
331 if (src
->ledger_limit
!= LEDGER_ITEM_INFINITY
) {
332 /* Would the existing balance exceed the new limit ? */
333 if (src
->ledger_limit
- amount
< src
->ledger_balance
) {
334 ledger_unlock(parent_ledger
);
335 ledger_unlock(child_ledger
);
336 return(KERN_RESOURCE_SHORTAGE
);
338 if (src
->ledger_limit
- amount
> 0)
339 src
->ledger_limit
-= amount
;
341 src
->ledger_limit
= 0;
344 if (dest
->ledger_limit
!= LEDGER_ITEM_INFINITY
) {
345 if ((natural_t
)(dest
->ledger_limit
+ amount
)
346 < LEDGER_ITEM_INFINITY
)
347 dest
->ledger_limit
+= amount
;
349 dest
->ledger_limit
= (LEDGER_ITEM_INFINITY
- 1);
352 ledger_unlock(parent_ledger
);
353 ledger_unlock(child_ledger
);
355 return(KERN_SUCCESS
);
360 * Routine: convert_port_to_ledger
362 * Convert from a port to a ledger.
363 * Doesn't consume the port ref; the ledger produced may be null.
369 convert_port_to_ledger(
372 ledger_t ledger
= LEDGER_NULL
;
374 if (IP_VALID(port
)) {
376 if (ip_active(port
) &&
377 (ip_kotype(port
) == IKOT_LEDGER
))
378 ledger
= (ledger_t
) port
->ip_kobject
;
386 * Routine: convert_ledger_to_port
388 * Convert from a ledger to a port.
389 * Produces a naked send right which may be invalid.
395 convert_ledger_to_port(
400 port
= ipc_port_make_send(ledger
->ledger_self
);
412 /* XXX reference counting */
414 return(ipc_port_copy_send(ledger
->ledger_self
));