2 * Copyright (c) 2000-2007 Apple 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 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
30 * support for mandatory and extensible security protections. This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/proc_internal.h>
39 #include <sys/kauth.h>
42 #include <sys/vnode_internal.h>
43 #include <sys/namei.h>
44 #include <sys/ubc_internal.h>
45 #include <sys/mount_internal.h>
46 #include <sys/malloc.h>
48 #include <default_pager/default_pager_types.h>
49 #include <default_pager/default_pager_object.h>
51 #include <security/audit/audit.h>
52 #include <bsm/audit_kevents.h>
54 #include <mach/mach_types.h>
55 #include <mach/host_priv.h>
56 #include <mach/mach_traps.h>
57 #include <mach/boolean.h>
59 #include <kern/kern_types.h>
60 #include <kern/host.h>
61 #include <kern/task.h>
62 #include <kern/zalloc.h>
63 #include <kern/kalloc.h>
64 #include <kern/assert.h>
66 #include <libkern/libkern.h>
68 #include <vm/vm_pageout.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vnode_pager.h>
72 #include <vm/vm_protos.h>
74 #include <security/mac_framework.h>
78 * temporary support for delayed instantiation
81 int default_pager_init_flag
= 0;
83 struct bs_map bs_port_table
[MAX_BACKING_STORE
] = {
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},
86 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
87 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
88 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
89 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
90 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
91 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
92 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
93 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
95 /* ###################################################### */
99 * Routine: macx_backing_store_recovery
101 * Syscall interface to set a tasks privilege
102 * level so that it is not subject to
103 * macx_backing_store_suspend
106 macx_backing_store_recovery(
107 struct macx_backing_store_recovery_args
*args
)
111 struct proc
*p
= current_proc();
112 boolean_t funnel_state
;
114 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
115 if ((error
= suser(kauth_cred_get(), 0)))
116 goto backing_store_recovery_return
;
118 /* for now restrict backing_store_recovery */
119 /* usage to only present task */
120 if(pid
!= proc_selfpid()) {
122 goto backing_store_recovery_return
;
125 task_backing_store_privileged(p
->task
);
127 backing_store_recovery_return
:
128 (void) thread_funnel_set(kernel_flock
, FALSE
);
133 * Routine: macx_backing_store_suspend
135 * Syscall interface to stop new demand for
136 * backing store when backing store is low
140 macx_backing_store_suspend(
141 struct macx_backing_store_suspend_args
*args
)
143 boolean_t suspend
= args
->suspend
;
145 boolean_t funnel_state
;
147 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
148 if ((error
= suser(kauth_cred_get(), 0)))
149 goto backing_store_suspend_return
;
151 vm_backing_store_disable(suspend
);
153 backing_store_suspend_return
:
154 (void) thread_funnel_set(kernel_flock
, FALSE
);
158 extern boolean_t backing_store_stop_compaction
;
161 * Routine: macx_backing_store_compaction
163 * Turn compaction of swap space on or off. This is
164 * used during shutdown/restart so that the kernel
165 * doesn't waste time compacting swap files that are
166 * about to be deleted anyway. Compaction is always
167 * on by default when the system comes up and is turned
168 * off when a shutdown/restart is requested. It is
169 * re-enabled if the shutdown/restart is aborted for any reason.
173 macx_backing_store_compaction(int flags
)
177 if ((error
= suser(kauth_cred_get(), 0)))
180 if (flags
& SWAP_COMPACT_DISABLE
) {
181 backing_store_stop_compaction
= TRUE
;
183 } else if (flags
& SWAP_COMPACT_ENABLE
) {
184 backing_store_stop_compaction
= FALSE
;
191 * Routine: macx_triggers
193 * Syscall interface to set the call backs for low and
198 struct macx_triggers_args
*args
)
202 error
= suser(kauth_cred_get(), 0);
206 return mach_macx_triggers(args
);
210 extern boolean_t dp_isssd
;
213 * Routine: macx_swapon
215 * Syscall interface to add a file to backing store
219 struct macx_swapon_args
*args
)
221 int size
= args
->size
;
222 vnode_t vp
= (vnode_t
)NULL
;
223 struct nameidata nd
, *ndp
;
226 mach_port_t backing_store
;
227 memory_object_default_t default_pager
;
229 boolean_t funnel_state
;
231 vfs_context_t ctx
= vfs_context_current();
232 struct proc
*p
= current_proc();
236 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPON
);
237 AUDIT_ARG(value32
, args
->priority
);
239 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
242 if ((error
= suser(kauth_cred_get(), 0)))
246 * Get a vnode for the paging area.
248 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
249 ((IS_64BIT_PROCESS(p
)) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
250 (user_addr_t
) args
->filename
, ctx
);
252 if ((error
= namei(ndp
)))
257 if (vp
->v_type
!= VREG
) {
263 if ((error
= vnode_size(vp
, &file_size
, ctx
)) != 0)
267 error
= mac_system_check_swapon(vfs_context_ucred(ctx
), vp
);
273 /* resize to desired size if it's too small */
274 if ((file_size
< (off_t
)size
) && ((error
= vnode_setsize(vp
, (off_t
)size
, 0, ctx
)) != 0))
277 if (default_pager_init_flag
== 0) {
278 start_def_pager(NULL
);
279 default_pager_init_flag
= 1;
282 /* add new backing store to list */
284 while(bs_port_table
[i
].vp
!= 0) {
285 if(i
== MAX_BACKING_STORE
)
289 if(i
== MAX_BACKING_STORE
) {
294 /* remember the vnode. This vnode has namei() reference */
295 bs_port_table
[i
].vp
= vp
;
298 * Look to see if we are already paging to this file.
300 /* make certain the copy send of kernel call will work */
301 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
302 kr
= host_default_memory_manager(host_priv_self(), &default_pager
, 0);
303 if(kr
!= KERN_SUCCESS
) {
305 bs_port_table
[i
].vp
= 0;
309 if (vp
->v_mount
->mnt_kern_flag
& MNTK_SSD
) {
311 * keep the cluster size small since the
312 * seek cost is effectively 0 which means
313 * we don't care much about fragmentation
316 dp_cluster_size
= 2 * PAGE_SIZE
;
319 * use the default cluster size
324 kr
= default_pager_backing_store_create(default_pager
,
325 -1, /* default priority */
328 memory_object_default_deallocate(default_pager
);
330 if(kr
!= KERN_SUCCESS
) {
332 bs_port_table
[i
].vp
= 0;
336 /* Mark this vnode as being used for swapfile */
338 SET(vp
->v_flag
, VSWAP
);
342 * NOTE: we are able to supply PAGE_SIZE here instead of
343 * an actual record size or block number because:
344 * a: we do not support offsets from the beginning of the
345 * file (allowing for non page size/record modulo offsets.
346 * b: because allow paging will be done modulo page size
349 kr
= default_pager_add_file(backing_store
, (vnode_ptr_t
) vp
,
350 PAGE_SIZE
, (int)(file_size
/PAGE_SIZE
));
351 if(kr
!= KERN_SUCCESS
) {
352 bs_port_table
[i
].vp
= 0;
353 if(kr
== KERN_INVALID_ARGUMENT
)
358 /* This vnode is not to be used for swapfile */
360 CLR(vp
->v_flag
, VSWAP
);
365 bs_port_table
[i
].bs
= (void *)backing_store
;
368 ubc_setthreadcred(vp
, p
, current_thread());
371 * take a long term reference on the vnode to keep
372 * vnreclaim() away from this vnode.
380 (void) thread_funnel_set(kernel_flock
, FALSE
);
381 AUDIT_MACH_SYSCALL_EXIT(error
);
386 * Routine: macx_swapoff
388 * Syscall interface to remove a file from backing store
392 struct macx_swapoff_args
*args
)
394 __unused
int flags
= args
->flags
;
396 mach_port_t backing_store
;
398 struct vnode
*vp
= 0;
399 struct nameidata nd
, *ndp
;
400 struct proc
*p
= current_proc();
403 boolean_t funnel_state
;
404 vfs_context_t ctx
= vfs_context_current();
406 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPOFF
);
408 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
409 backing_store
= NULL
;
412 if ((error
= suser(kauth_cred_get(), 0)))
413 goto swapoff_bailout
;
416 * Get the vnode for the paging area.
418 NDINIT(ndp
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
419 ((IS_64BIT_PROCESS(p
)) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
420 (user_addr_t
) args
->filename
, ctx
);
422 if ((error
= namei(ndp
)))
423 goto swapoff_bailout
;
427 if (vp
->v_type
!= VREG
) {
429 goto swapoff_bailout
;
433 error
= mac_system_check_swapoff(vfs_context_ucred(ctx
), vp
);
436 goto swapoff_bailout
;
439 for(i
= 0; i
< MAX_BACKING_STORE
; i
++) {
440 if(bs_port_table
[i
].vp
== vp
) {
444 if (i
== MAX_BACKING_STORE
) {
446 goto swapoff_bailout
;
448 backing_store
= (mach_port_t
)bs_port_table
[i
].bs
;
450 kr
= default_pager_backing_store_delete(backing_store
);
454 bs_port_table
[i
].vp
= 0;
455 /* This vnode is no longer used for swapfile */
457 CLR(vp
->v_flag
, VSWAP
);
460 /* get rid of macx_swapon() "long term" reference */
473 /* get rid of macx_swapoff() namei() reference */
477 (void) thread_funnel_set(kernel_flock
, FALSE
);
478 AUDIT_MACH_SYSCALL_EXIT(error
);
483 * Routine: macx_swapinfo
485 * Syscall interface to get general swap statistics
489 memory_object_size_t
*total_p
,
490 memory_object_size_t
*avail_p
,
491 vm_size_t
*pagesize_p
,
492 boolean_t
*encrypted_p
)
495 memory_object_default_t default_pager
;
496 default_pager_info_64_t dpi64
;
502 * Get a handle on the default pager.
504 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
505 kr
= host_default_memory_manager(host_priv_self(), &default_pager
, 0);
506 if (kr
!= KERN_SUCCESS
) {
507 error
= EAGAIN
; /* XXX why EAGAIN ? */
510 if (default_pager
== MEMORY_OBJECT_DEFAULT_NULL
) {
512 * The default pager has not initialized yet,
513 * so it can't be using any swap space at all.
518 *encrypted_p
= FALSE
;
523 * Get swap usage data from default pager.
525 kr
= default_pager_info_64(default_pager
, &dpi64
);
526 if (kr
!= KERN_SUCCESS
) {
532 * Provide default pager info to caller.
534 *total_p
= dpi64
.dpi_total_space
;
535 *avail_p
= dpi64
.dpi_free_space
;
536 *pagesize_p
= dpi64
.dpi_page_size
;
537 if (dpi64
.dpi_flags
& DPI_ENCRYPTED
) {
540 *encrypted_p
= FALSE
;
544 if (default_pager
!= MEMORY_OBJECT_DEFAULT_NULL
) {
545 /* release our handle on default pager */
546 memory_object_default_deallocate(default_pager
);