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