2 * Copyright (c) 2000-2004 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@
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/proc_internal.h>
33 #include <sys/kauth.h>
36 #include <sys/vnode_internal.h>
37 #include <sys/namei.h>
38 #include <sys/ubc_internal.h>
39 #include <sys/malloc.h>
41 #include <default_pager/default_pager_types.h>
42 #include <default_pager/default_pager_object.h>
44 #include <bsm/audit_kernel.h>
45 #include <bsm/audit_kevents.h>
47 #include <mach/mach_types.h>
48 #include <mach/host_priv.h>
49 #include <mach/mach_traps.h>
50 #include <mach/boolean.h>
52 #include <kern/kern_types.h>
53 #include <kern/host.h>
54 #include <kern/task.h>
55 #include <kern/zalloc.h>
56 #include <kern/kalloc.h>
57 #include <kern/assert.h>
59 #include <libkern/libkern.h>
61 #include <vm/vm_pageout.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vnode_pager.h>
65 #include <vm/vm_protos.h>
67 extern thread_t
current_act(void);
70 * temporary support for delayed instantiation
73 int default_pager_init_flag
= 0;
75 struct bs_map bs_port_table
[MAX_BACKING_STORE
] = {
76 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
77 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
78 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
79 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
80 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
81 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
82 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
83 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
84 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
85 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
87 /* ###################################################### */
91 * Routine: macx_backing_store_recovery
93 * Syscall interface to set a tasks privilege
94 * level so that it is not subject to
95 * macx_backing_store_suspend
98 macx_backing_store_recovery(
99 struct macx_backing_store_recovery_args
*args
)
103 struct proc
*p
= current_proc();
104 boolean_t funnel_state
;
106 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
107 if ((error
= suser(kauth_cred_get(), 0)))
108 goto backing_store_recovery_return
;
110 /* for now restrict backing_store_recovery */
111 /* usage to only present task */
112 if(pid
!= proc_selfpid()) {
114 goto backing_store_recovery_return
;
117 task_backing_store_privileged(p
->task
);
119 backing_store_recovery_return
:
120 (void) thread_funnel_set(kernel_flock
, FALSE
);
125 * Routine: macx_backing_store_suspend
127 * Syscall interface to stop new demand for
128 * backing store when backing store is low
132 macx_backing_store_suspend(
133 struct macx_backing_store_suspend_args
*args
)
135 boolean_t suspend
= args
->suspend
;
137 boolean_t funnel_state
;
139 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
140 if ((error
= suser(kauth_cred_get(), 0)))
141 goto backing_store_suspend_return
;
143 vm_backing_store_disable(suspend
);
145 backing_store_suspend_return
:
146 (void) thread_funnel_set(kernel_flock
, FALSE
);
151 * Routine: macx_swapon
153 * Syscall interface to add a file to backing store
157 struct macx_swapon_args
*args
)
159 int size
= args
->size
;
160 vnode_t vp
= (vnode_t
)NULL
;
161 struct nameidata nd
, *ndp
;
162 struct proc
*p
= current_proc();
165 mach_port_t backing_store
;
166 memory_object_default_t default_pager
;
168 boolean_t funnel_state
;
170 struct vfs_context context
;
173 context
.vc_ucred
= kauth_cred_get();
175 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPON
);
176 AUDIT_ARG(value
, args
->priority
);
178 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
181 if ((error
= suser(kauth_cred_get(), 0)))
184 if(default_pager_init_flag
== 0) {
185 start_def_pager(NULL
);
186 default_pager_init_flag
= 1;
190 * Get a vnode for the paging area.
192 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
193 ((IS_64BIT_PROCESS(p
)) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
194 CAST_USER_ADDR_T(args
->filename
), &context
);
196 if ((error
= namei(ndp
)))
201 if (vp
->v_type
!= VREG
) {
205 UBCINFOCHECK("macx_swapon", vp
);
208 if ((error
= vnode_size(vp
, &file_size
, &context
)) != 0)
211 /* resize to desired size if it's too small */
212 if ((file_size
< (off_t
)size
) && ((error
= vnode_setsize(vp
, (off_t
)size
, 0, &context
)) != 0))
215 /* add new backing store to list */
217 while(bs_port_table
[i
].vp
!= 0) {
218 if(i
== MAX_BACKING_STORE
)
222 if(i
== MAX_BACKING_STORE
) {
227 /* remember the vnode. This vnode has namei() reference */
228 bs_port_table
[i
].vp
= vp
;
231 * Look to see if we are already paging to this file.
233 /* make certain the copy send of kernel call will work */
234 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
235 kr
= host_default_memory_manager(host_priv_self(), &default_pager
, 0);
236 if(kr
!= KERN_SUCCESS
) {
238 bs_port_table
[i
].vp
= 0;
242 kr
= default_pager_backing_store_create(default_pager
,
243 -1, /* default priority */
244 0, /* default cluster size */
246 memory_object_default_deallocate(default_pager
);
248 if(kr
!= KERN_SUCCESS
) {
250 bs_port_table
[i
].vp
= 0;
255 * NOTE: we are able to supply PAGE_SIZE here instead of
256 * an actual record size or block number because:
257 * a: we do not support offsets from the beginning of the
258 * file (allowing for non page size/record modulo offsets.
259 * b: because allow paging will be done modulo page size
262 kr
= default_pager_add_file(backing_store
, (vnode_ptr_t
) vp
,
263 PAGE_SIZE
, (int)(file_size
/PAGE_SIZE
));
264 if(kr
!= KERN_SUCCESS
) {
265 bs_port_table
[i
].vp
= 0;
266 if(kr
== KERN_INVALID_ARGUMENT
)
272 bs_port_table
[i
].bs
= (void *)backing_store
;
275 /* Mark this vnode as being used for swapfile */
276 SET(vp
->v_flag
, VSWAP
);
278 ubc_setthreadcred(vp
, p
, current_thread());
281 * take a long term reference on the vnode to keep
282 * vnreclaim() away from this vnode.
290 (void) thread_funnel_set(kernel_flock
, FALSE
);
291 AUDIT_MACH_SYSCALL_EXIT(error
);
296 * Routine: macx_swapoff
298 * Syscall interface to remove a file from backing store
302 struct macx_swapoff_args
*args
)
304 __unused
int flags
= args
->flags
;
306 mach_port_t backing_store
;
308 struct vnode
*vp
= 0;
309 struct nameidata nd
, *ndp
;
310 struct proc
*p
= current_proc();
313 boolean_t funnel_state
;
314 struct vfs_context context
;
317 context
.vc_ucred
= kauth_cred_get();
319 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPOFF
);
321 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
322 backing_store
= NULL
;
325 if ((error
= suser(kauth_cred_get(), 0)))
326 goto swapoff_bailout
;
329 * Get the vnode for the paging area.
331 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
332 ((IS_64BIT_PROCESS(p
)) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
333 CAST_USER_ADDR_T(args
->filename
), &context
);
335 if ((error
= namei(ndp
)))
336 goto swapoff_bailout
;
340 if (vp
->v_type
!= VREG
) {
342 goto swapoff_bailout
;
345 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
346 if(bs_port_table
[i
].vp
== vp
) {
350 if (i
== MAX_BACKING_STORE
) {
352 goto swapoff_bailout
;
354 backing_store
= (mach_port_t
)bs_port_table
[i
].bs
;
356 kr
= default_pager_backing_store_delete(backing_store
);
360 bs_port_table
[i
].vp
= 0;
361 /* This vnode is no longer used for swapfile */
362 CLR(vp
->v_flag
, VSWAP
);
364 /* get rid of macx_swapon() "long term" reference */
377 /* get rid of macx_swapoff() namei() reference */
381 (void) thread_funnel_set(kernel_flock
, FALSE
);
382 AUDIT_MACH_SYSCALL_EXIT(error
);
387 * Routine: macx_swapinfo
389 * Syscall interface to get general swap statistics
393 memory_object_size_t
*total_p
,
394 memory_object_size_t
*avail_p
,
395 vm_size_t
*pagesize_p
,
396 boolean_t
*encrypted_p
)
399 memory_object_default_t default_pager
;
400 default_pager_info_64_t dpi64
;
406 * Get a handle on the default pager.
408 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
409 kr
= host_default_memory_manager(host_priv_self(), &default_pager
, 0);
410 if (kr
!= KERN_SUCCESS
) {
411 error
= EAGAIN
; /* XXX why EAGAIN ? */
414 if (default_pager
== MEMORY_OBJECT_DEFAULT_NULL
) {
416 * The default pager has not initialized yet,
417 * so it can't be using any swap space at all.
422 *encrypted_p
= FALSE
;
427 * Get swap usage data from default pager.
429 kr
= default_pager_info_64(default_pager
, &dpi64
);
430 if (kr
!= KERN_SUCCESS
) {
436 * Provide default pager info to caller.
438 *total_p
= dpi64
.dpi_total_space
;
439 *avail_p
= dpi64
.dpi_free_space
;
440 *pagesize_p
= dpi64
.dpi_page_size
;
441 if (dpi64
.dpi_flags
& DPI_ENCRYPTED
) {
444 *encrypted_p
= FALSE
;
448 if (default_pager
!= MEMORY_OBJECT_DEFAULT_NULL
) {
449 /* release our handle on default pager */
450 memory_object_default_deallocate(default_pager
);