]> git.saurik.com Git - apple/xnu.git/blame - bsd/vm/dp_backing_file.c
xnu-1228.0.2.tar.gz
[apple/xnu.git] / bsd / vm / dp_backing_file.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55
A
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.
1c79356b
A
33 */
34
1c79356b
A
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/lock.h>
91447636
A
38#include <sys/proc_internal.h>
39#include <sys/kauth.h>
1c79356b
A
40#include <sys/buf.h>
41#include <sys/uio.h>
91447636 42#include <sys/vnode_internal.h>
1c79356b 43#include <sys/namei.h>
91447636
A
44#include <sys/ubc_internal.h>
45#include <sys/malloc.h>
46
47#include <default_pager/default_pager_types.h>
48#include <default_pager/default_pager_object.h>
1c79356b 49
e5568f75
A
50#include <bsm/audit_kernel.h>
51#include <bsm/audit_kevents.h>
52
1c79356b 53#include <mach/mach_types.h>
91447636
A
54#include <mach/host_priv.h>
55#include <mach/mach_traps.h>
56#include <mach/boolean.h>
57
58#include <kern/kern_types.h>
1c79356b 59#include <kern/host.h>
91447636 60#include <kern/task.h>
1c79356b
A
61#include <kern/zalloc.h>
62#include <kern/kalloc.h>
91447636
A
63#include <kern/assert.h>
64
1c79356b 65#include <libkern/libkern.h>
1c79356b 66
91447636
A
67#include <vm/vm_pageout.h>
68#include <vm/vm_map.h>
69#include <vm/vm_kern.h>
1c79356b 70#include <vm/vnode_pager.h>
91447636 71#include <vm/vm_protos.h>
2d21ac55
A
72#if CONFIG_MACF
73#include <security/mac_framework.h>
74#endif
1c79356b
A
75
76/*
77 * temporary support for delayed instantiation
78 * of default_pager
79 */
80int default_pager_init_flag = 0;
81
82struct bs_map bs_port_table[MAX_BACKING_STORE] = {
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},
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
94/* ###################################################### */
95
96
55e303ae
A
97/*
98 * Routine: macx_backing_store_recovery
99 * Function:
100 * Syscall interface to set a tasks privilege
101 * level so that it is not subject to
102 * macx_backing_store_suspend
103 */
104int
105macx_backing_store_recovery(
91447636 106 struct macx_backing_store_recovery_args *args)
55e303ae 107{
91447636 108 int pid = args->pid;
55e303ae
A
109 int error;
110 struct proc *p = current_proc();
111 boolean_t funnel_state;
112
113 funnel_state = thread_funnel_set(kernel_flock, TRUE);
91447636 114 if ((error = suser(kauth_cred_get(), 0)))
55e303ae
A
115 goto backing_store_recovery_return;
116
117 /* for now restrict backing_store_recovery */
118 /* usage to only present task */
91447636 119 if(pid != proc_selfpid()) {
55e303ae
A
120 error = EINVAL;
121 goto backing_store_recovery_return;
122 }
123
124 task_backing_store_privileged(p->task);
125
126backing_store_recovery_return:
127 (void) thread_funnel_set(kernel_flock, FALSE);
128 return(error);
129}
130
131/*
132 * Routine: macx_backing_store_suspend
133 * Function:
134 * Syscall interface to stop new demand for
135 * backing store when backing store is low
136 */
137
138int
139macx_backing_store_suspend(
91447636 140 struct macx_backing_store_suspend_args *args)
55e303ae 141{
91447636 142 boolean_t suspend = args->suspend;
55e303ae 143 int error;
55e303ae
A
144 boolean_t funnel_state;
145
146 funnel_state = thread_funnel_set(kernel_flock, TRUE);
91447636 147 if ((error = suser(kauth_cred_get(), 0)))
55e303ae
A
148 goto backing_store_suspend_return;
149
150 vm_backing_store_disable(suspend);
151
152backing_store_suspend_return:
153 (void) thread_funnel_set(kernel_flock, FALSE);
154 return(error);
155}
156
1c79356b
A
157/*
158 * Routine: macx_swapon
159 * Function:
160 * Syscall interface to add a file to backing store
161 */
162int
163macx_swapon(
91447636 164 struct macx_swapon_args *args)
1c79356b 165{
91447636
A
166 int size = args->size;
167 vnode_t vp = (vnode_t)NULL;
1c79356b 168 struct nameidata nd, *ndp;
1c79356b
A
169 register int error;
170 kern_return_t kr;
171 mach_port_t backing_store;
0b4e3aa0 172 memory_object_default_t default_pager;
1c79356b
A
173 int i;
174 boolean_t funnel_state;
91447636 175 off_t file_size;
2d21ac55
A
176 vfs_context_t ctx = vfs_context_current();
177 struct proc *p = current_proc();
1c79356b 178
e5568f75 179 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPON);
91447636 180 AUDIT_ARG(value, args->priority);
e5568f75 181
1c79356b
A
182 funnel_state = thread_funnel_set(kernel_flock, TRUE);
183 ndp = &nd;
184
91447636 185 if ((error = suser(kauth_cred_get(), 0)))
1c79356b
A
186 goto swapon_bailout;
187
1c79356b
A
188 if(default_pager_init_flag == 0) {
189 start_def_pager(NULL);
190 default_pager_init_flag = 1;
191 }
192
193 /*
194 * Get a vnode for the paging area.
195 */
91447636
A
196 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
197 ((IS_64BIT_PROCESS(p)) ? UIO_USERSPACE64 : UIO_USERSPACE32),
2d21ac55 198 CAST_USER_ADDR_T(args->filename), ctx);
1c79356b
A
199
200 if ((error = namei(ndp)))
201 goto swapon_bailout;
91447636 202 nameidone(ndp);
1c79356b
A
203 vp = ndp->ni_vp;
204
205 if (vp->v_type != VREG) {
206 error = EINVAL;
1c79356b
A
207 goto swapon_bailout;
208 }
1c79356b 209
91447636 210 /* get file size */
2d21ac55
A
211 if ((error = vnode_size(vp, &file_size, ctx)) != 0)
212 goto swapon_bailout;
213#if CONFIG_MACF
214 vnode_lock(vp);
215 error = mac_system_check_swapon(vfs_context_ucred(ctx), vp);
216 vnode_unlock(vp);
217 if (error)
1c79356b 218 goto swapon_bailout;
2d21ac55 219#endif
1c79356b 220
91447636 221 /* resize to desired size if it's too small */
2d21ac55 222 if ((file_size < (off_t)size) && ((error = vnode_setsize(vp, (off_t)size, 0, ctx)) != 0))
91447636 223 goto swapon_bailout;
1c79356b
A
224
225 /* add new backing store to list */
226 i = 0;
227 while(bs_port_table[i].vp != 0) {
228 if(i == MAX_BACKING_STORE)
229 break;
230 i++;
231 }
232 if(i == MAX_BACKING_STORE) {
233 error = ENOMEM;
1c79356b
A
234 goto swapon_bailout;
235 }
236
237 /* remember the vnode. This vnode has namei() reference */
238 bs_port_table[i].vp = vp;
239
240 /*
241 * Look to see if we are already paging to this file.
242 */
243 /* make certain the copy send of kernel call will work */
0b4e3aa0
A
244 default_pager = MEMORY_OBJECT_DEFAULT_NULL;
245 kr = host_default_memory_manager(host_priv_self(), &default_pager, 0);
1c79356b
A
246 if(kr != KERN_SUCCESS) {
247 error = EAGAIN;
1c79356b
A
248 bs_port_table[i].vp = 0;
249 goto swapon_bailout;
250 }
251
0b4e3aa0 252 kr = default_pager_backing_store_create(default_pager,
1c79356b
A
253 -1, /* default priority */
254 0, /* default cluster size */
255 &backing_store);
0b4e3aa0
A
256 memory_object_default_deallocate(default_pager);
257
1c79356b
A
258 if(kr != KERN_SUCCESS) {
259 error = ENOMEM;
1c79356b
A
260 bs_port_table[i].vp = 0;
261 goto swapon_bailout;
262 }
263
264 /*
265 * NOTE: we are able to supply PAGE_SIZE here instead of
266 * an actual record size or block number because:
267 * a: we do not support offsets from the beginning of the
268 * file (allowing for non page size/record modulo offsets.
269 * b: because allow paging will be done modulo page size
270 */
271
91447636
A
272 kr = default_pager_add_file(backing_store, (vnode_ptr_t) vp,
273 PAGE_SIZE, (int)(file_size/PAGE_SIZE));
1c79356b
A
274 if(kr != KERN_SUCCESS) {
275 bs_port_table[i].vp = 0;
276 if(kr == KERN_INVALID_ARGUMENT)
277 error = EINVAL;
278 else
279 error = ENOMEM;
280 goto swapon_bailout;
281 }
282 bs_port_table[i].bs = (void *)backing_store;
283 error = 0;
1c79356b
A
284
285 /* Mark this vnode as being used for swapfile */
286 SET(vp->v_flag, VSWAP);
287
13fec989 288 ubc_setthreadcred(vp, p, current_thread());
55e303ae 289
1c79356b 290 /*
91447636 291 * take a long term reference on the vnode to keep
1c79356b
A
292 * vnreclaim() away from this vnode.
293 */
91447636 294 vnode_ref(vp);
1c79356b
A
295
296swapon_bailout:
297 if (vp) {
91447636 298 vnode_put(vp);
1c79356b 299 }
1c79356b 300 (void) thread_funnel_set(kernel_flock, FALSE);
e5568f75 301 AUDIT_MACH_SYSCALL_EXIT(error);
1c79356b
A
302 return(error);
303}
304
305/*
306 * Routine: macx_swapoff
307 * Function:
308 * Syscall interface to remove a file from backing store
309 */
310int
311macx_swapoff(
91447636 312 struct macx_swapoff_args *args)
1c79356b 313{
91447636 314 __unused int flags = args->flags;
1c79356b
A
315 kern_return_t kr;
316 mach_port_t backing_store;
317
318 struct vnode *vp = 0;
319 struct nameidata nd, *ndp;
320 struct proc *p = current_proc();
321 int i;
322 int error;
323 boolean_t funnel_state;
2d21ac55 324 vfs_context_t ctx = vfs_context_current();
1c79356b 325
e5568f75 326 AUDIT_MACH_SYSCALL_ENTER(AUE_SWAPOFF);
91447636 327
1c79356b
A
328 funnel_state = thread_funnel_set(kernel_flock, TRUE);
329 backing_store = NULL;
330 ndp = &nd;
331
91447636 332 if ((error = suser(kauth_cred_get(), 0)))
1c79356b
A
333 goto swapoff_bailout;
334
1c79356b
A
335 /*
336 * Get the vnode for the paging area.
337 */
91447636
A
338 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
339 ((IS_64BIT_PROCESS(p)) ? UIO_USERSPACE64 : UIO_USERSPACE32),
2d21ac55 340 CAST_USER_ADDR_T(args->filename), ctx);
1c79356b
A
341
342 if ((error = namei(ndp)))
343 goto swapoff_bailout;
91447636 344 nameidone(ndp);
1c79356b
A
345 vp = ndp->ni_vp;
346
347 if (vp->v_type != VREG) {
348 error = EINVAL;
1c79356b
A
349 goto swapoff_bailout;
350 }
2d21ac55
A
351#if CONFIG_MACF
352 vnode_lock(vp);
353 error = mac_system_check_swapoff(vfs_context_ucred(ctx), vp);
354 vnode_unlock(vp);
355 if (error)
356 goto swapoff_bailout;
357#endif
1c79356b
A
358
359 for(i = 0; i < MAX_BACKING_STORE; i++) {
360 if(bs_port_table[i].vp == vp) {
1c79356b
A
361 break;
362 }
363 }
364 if (i == MAX_BACKING_STORE) {
365 error = EINVAL;
1c79356b
A
366 goto swapoff_bailout;
367 }
368 backing_store = (mach_port_t)bs_port_table[i].bs;
369
1c79356b
A
370 kr = default_pager_backing_store_delete(backing_store);
371 switch (kr) {
372 case KERN_SUCCESS:
373 error = 0;
374 bs_port_table[i].vp = 0;
1c79356b
A
375 /* This vnode is no longer used for swapfile */
376 CLR(vp->v_flag, VSWAP);
377
91447636
A
378 /* get rid of macx_swapon() "long term" reference */
379 vnode_rele(vp);
1c79356b 380
1c79356b
A
381 break;
382 case KERN_FAILURE:
383 error = EAGAIN;
384 break;
385 default:
386 error = EAGAIN;
387 break;
388 }
389
390swapoff_bailout:
391 /* get rid of macx_swapoff() namei() reference */
392 if (vp)
91447636 393 vnode_put(vp);
1c79356b 394
1c79356b 395 (void) thread_funnel_set(kernel_flock, FALSE);
e5568f75 396 AUDIT_MACH_SYSCALL_EXIT(error);
1c79356b
A
397 return(error);
398}
91447636
A
399
400/*
401 * Routine: macx_swapinfo
402 * Function:
403 * Syscall interface to get general swap statistics
404 */
405int
406macx_swapinfo(
407 memory_object_size_t *total_p,
408 memory_object_size_t *avail_p,
409 vm_size_t *pagesize_p,
410 boolean_t *encrypted_p)
411{
412 int error;
413 memory_object_default_t default_pager;
414 default_pager_info_64_t dpi64;
415 kern_return_t kr;
416
417 error = 0;
418
419 /*
420 * Get a handle on the default pager.
421 */
422 default_pager = MEMORY_OBJECT_DEFAULT_NULL;
423 kr = host_default_memory_manager(host_priv_self(), &default_pager, 0);
424 if (kr != KERN_SUCCESS) {
425 error = EAGAIN; /* XXX why EAGAIN ? */
426 goto done;
427 }
428 if (default_pager == MEMORY_OBJECT_DEFAULT_NULL) {
429 /*
430 * The default pager has not initialized yet,
431 * so it can't be using any swap space at all.
432 */
433 *total_p = 0;
434 *avail_p = 0;
435 *pagesize_p = 0;
436 *encrypted_p = FALSE;
437 goto done;
438 }
439
440 /*
441 * Get swap usage data from default pager.
442 */
443 kr = default_pager_info_64(default_pager, &dpi64);
444 if (kr != KERN_SUCCESS) {
445 error = ENOTSUP;
446 goto done;
447 }
448
449 /*
450 * Provide default pager info to caller.
451 */
452 *total_p = dpi64.dpi_total_space;
453 *avail_p = dpi64.dpi_free_space;
454 *pagesize_p = dpi64.dpi_page_size;
455 if (dpi64.dpi_flags & DPI_ENCRYPTED) {
456 *encrypted_p = TRUE;
457 } else {
458 *encrypted_p = FALSE;
459 }
460
461done:
462 if (default_pager != MEMORY_OBJECT_DEFAULT_NULL) {
463 /* release our handle on default pager */
464 memory_object_default_deallocate(default_pager);
465 }
466 return error;
467}