1 /*
2 * Copyright (c) 2000-2010 Apple 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 * 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,
32 * Version 2.0.
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/lock.h>
38 #include <sys/proc_internal.h>
39 #include <sys/kauth.h>
40 #include <sys/buf.h>
41 #include <sys/uio.h>
42 #include <sys/vnode_internal.h>
43 #include <sys/namei.h>
44 #include <sys/ubc_internal.h>
45 #include <sys/malloc.h>
46 #include <sys/user.h>
47
48 #include <default_pager/default_pager_types.h>
49
50 #include <security/audit/audit.h>
51 #include <bsm/audit_kevents.h>
52
53 #include <mach/mach_types.h>
54 #include <mach/host_priv.h>
55 #include <mach/mach_traps.h>
56 #include <mach/boolean.h>
57
58 #include <kern/kern_types.h>
59 #include <kern/locks.h>
60 #include <kern/host.h>
61 #include <kern/task.h>
62 #include <kern/zalloc.h>
63 #include <kern/policy_internal.h>
64
65 #include <libkern/libkern.h>
66
67 #include <vm/vm_pageout.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vnode_pager.h>
71 #include <vm/vm_protos_internal.h>
72 #include <vm/vm_compressor_backing_store_xnu.h>
73 #if CONFIG_MACF
74 #include <security/mac_framework.h>
75 #endif
76
77 #include <pexpert/pexpert.h>
78
79
80 /*
81 * Routine: macx_backing_store_recovery
82 * Function:
83 * Syscall interface to set a tasks privilege
84 * level so that it is not subject to
85 * macx_backing_store_suspend
86 */
87 int
macx_backing_store_recovery(__unused struct macx_backing_store_recovery_args * args)88 macx_backing_store_recovery(
89 __unused struct macx_backing_store_recovery_args *args)
90 {
91 return ENOTSUP;
92 }
93
94 /*
95 * Routine: macx_backing_store_suspend
96 * Function:
97 * Syscall interface to stop new demand for
98 * backing store when backing store is low
99 */
100
101 int
macx_backing_store_suspend(__unused struct macx_backing_store_suspend_args * args)102 macx_backing_store_suspend(
103 __unused struct macx_backing_store_suspend_args *args)
104 {
105 return ENOTSUP;
106 }
107
108
109 extern boolean_t compressor_store_stop_compaction;
110
111 /*
112 * Routine: macx_backing_store_compaction
113 * Function:
114 * Turn compaction of swap space on or off. This is
115 * used during shutdown/restart so that the kernel
116 * doesn't waste time compacting swap files that are
117 * about to be deleted anyway. Compaction is always
118 * on by default when the system comes up and is turned
119 * off when a shutdown/restart is requested. It is
120 * re-enabled if the shutdown/restart is aborted for any reason.
121 *
122 * This routine assumes macx_lock has been locked by macx_triggers ->
123 * mach_macx_triggers -> macx_backing_store_compaction
124 */
125 extern int vm_swap_enabled;
126 int
macx_backing_store_compaction(int flags)127 macx_backing_store_compaction(int flags)
128 {
129 int error;
130
131 if ((error = suser(kauth_cred_get(), 0))) {
132 return error;
133 }
134
135 if (flags & SWAP_COMPACT_DISABLE) {
136 #if (XNU_TARGET_OS_OSX && __arm64__)
137 /*
138 * There's no synch. between the swap being turned
139 * OFF from user-space and all processes having exited.
140 * On fast SSD AS macs we can accumulate a lot of
141 * compressed memory between those 2 operations.
142 * So we allow swap till we are ready to shutdown the
143 * system. Even with a bunch of processes
144 * still running and creating a lot of compressed
145 * memory the system can shutdown normally.
146 */
147 #else /* (XNU_TARGET_OS_OSX && __arm64__) */
148 compressor_store_stop_compaction = TRUE;
149 vm_swap_enabled = 0;
150 kprintf("compressor_store_stop_compaction = TRUE\n");
151 #endif /* (XNU_TARGET_OS_OSX && __arm64__) */
152 } else if (flags & SWAP_COMPACT_ENABLE) {
153 compressor_store_stop_compaction = FALSE;
154 vm_swap_enabled = 1;
155 kprintf("compressor_store_stop_compaction = FALSE\n");
156 }
157
158 return 0;
159 }
160
161 /*
162 * Routine: macx_triggers
163 * Function:
164 * Syscall interface to set the call backs for low and
165 * high water marks.
166 */
167 int
macx_triggers(struct macx_triggers_args * args)168 macx_triggers(
169 struct macx_triggers_args *args)
170 {
171 int flags = args->flags;
172
173 if (flags & (SWAP_COMPACT_DISABLE | SWAP_COMPACT_ENABLE)) {
174 return macx_backing_store_compaction(flags);
175 }
176
177 return ENOTSUP;
178 }
179
180
181 int
macx_swapon(__unused struct macx_swapon_args * args)182 macx_swapon(
183 __unused struct macx_swapon_args *args)
184 {
185 return ENOTSUP;
186 }
187
188
189 /*
190 * Routine: macx_swapoff
191 * Function:
192 * Syscall interface to remove a file from backing store
193 */
194 int
macx_swapoff(__unused struct macx_swapoff_args * args)195 macx_swapoff(
196 __unused struct macx_swapoff_args *args)
197 {
198 return ENOTSUP;
199 }
200
201 /*
202 * Routine: macx_swapinfo
203 * Function:
204 * Syscall interface to get general swap statistics
205 */
206 extern boolean_t vm_swap_up;
207
208 int
macx_swapinfo(memory_object_size_t * total_p,memory_object_size_t * avail_p,vm_size_t * pagesize_p,boolean_t * encrypted_p)209 macx_swapinfo(
210 memory_object_size_t *total_p,
211 memory_object_size_t *avail_p,
212 vm_size_t *pagesize_p,
213 boolean_t *encrypted_p)
214 {
215 if (VM_CONFIG_SWAP_IS_PRESENT) {
216 *total_p = vm_swap_get_total_space();
217 *avail_p = vm_swap_get_free_space();
218 *pagesize_p = (vm_size_t)PAGE_SIZE_64;
219 *encrypted_p = TRUE;
220 } else {
221 *total_p = 0;
222 *avail_p = 0;
223 *pagesize_p = 0;
224 *encrypted_p = FALSE;
225 }
226 return 0;
227 }
228