]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /* |
2 | * Copyright (c) 2008 Apple Computer, 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 | #include <mach/kern_return.h> | |
30 | #include <mach/memory_object_control.h> | |
31 | #include <mach/upl.h> | |
32 | ||
33 | #include <kern/ipc_kobject.h> | |
34 | #include <kern/kalloc.h> | |
35 | #include <kern/queue.h> | |
36 | ||
37 | #include <vm/vm_kern.h> | |
38 | #include <vm/vm_map.h> | |
39 | #include <vm/vm_pageout.h> | |
40 | #include <vm/vm_protos.h> | |
41 | ||
42 | ||
43 | /* | |
44 | * APPLE SWAPFILE MEMORY PAGER | |
45 | * | |
46 | * This external memory manager (EMM) handles mappings of the swap files. | |
47 | * Swap files are not regular files and are used solely to store contents of | |
48 | * anonymous memory mappings while not resident in memory. | |
49 | * There's no valid reason to map a swap file. This just puts extra burden | |
50 | * on the system, is potentially a security issue and is not reliable since | |
51 | * the contents can change at any time with pageout operations. | |
52 | * Here are some of the issues with mapping a swap file. | |
53 | * * PERFORMANCE: | |
54 | * Each page in the swap file belong to an anonymous memory object. Mapping | |
55 | * the swap file makes those pages also accessible via a vnode memory | |
56 | * object and each page can now be resident twice. | |
57 | * * SECURITY: | |
58 | * Mapping a swap file allows access to other processes' memory. Swap files | |
59 | * are only accessible by the "root" super-user, who can already access any | |
60 | * process's memory, so this is not a real issue but if permissions on the | |
61 | * swap file got changed, it could become one. | |
62 | * Swap files are not "zero-filled" on creation, so until their contents are | |
63 | * overwritten with pageout operations, they still contain whatever was on | |
64 | * the disk blocks they were allocated. The "super-user" could see the | |
65 | * contents of free blocks anyway, so this is not a new security issue but | |
66 | * it may be perceive as one. | |
67 | * * ENCRYPTED SWAP: | |
68 | * When swap is encrypted, one does not expect to find any clear contents | |
69 | * in the swap files. Since unused blocks are not scrubbed, they could still | |
70 | * contain clear contents. If these contents are visible through a mapping | |
71 | * of the swap file, it makes it look like swap is not really encrypted. | |
72 | * | |
73 | * We can't legitimately prevent a user process with appropriate privileges | |
74 | * from mapping a swap file, but we can prevent it from accessing its actual | |
75 | * contents. | |
76 | * This pager mostly handles page-in request (from memory_object_data_request()) | |
77 | * for swap file mappings and just returns bogus data. | |
78 | * Pageouts are not handled, so mmap() has to make sure it does not allow | |
79 | * writable (i.e. MAP_SHARED and PROT_WRITE) mappings of swap files. | |
80 | */ | |
81 | ||
82 | /* forward declarations */ | |
83 | void swapfile_pager_reference(memory_object_t mem_obj); | |
84 | void swapfile_pager_deallocate(memory_object_t mem_obj); | |
85 | kern_return_t swapfile_pager_init(memory_object_t mem_obj, | |
86 | memory_object_control_t control, | |
87 | memory_object_cluster_size_t pg_size); | |
88 | kern_return_t swapfile_pager_terminate(memory_object_t mem_obj); | |
89 | kern_return_t swapfile_pager_data_request(memory_object_t mem_obj, | |
90 | memory_object_offset_t offset, | |
91 | memory_object_cluster_size_t length, | |
92 | vm_prot_t protection_required, | |
93 | memory_object_fault_info_t fault_info); | |
94 | kern_return_t swapfile_pager_data_return(memory_object_t mem_obj, | |
95 | memory_object_offset_t offset, | |
96 | memory_object_cluster_size_t data_cnt, | |
97 | memory_object_offset_t *resid_offset, | |
98 | int *io_error, | |
99 | boolean_t dirty, | |
100 | boolean_t kernel_copy, | |
101 | int upl_flags); | |
102 | kern_return_t swapfile_pager_data_initialize(memory_object_t mem_obj, | |
103 | memory_object_offset_t offset, | |
104 | memory_object_cluster_size_t data_cnt); | |
105 | kern_return_t swapfile_pager_data_unlock(memory_object_t mem_obj, | |
106 | memory_object_offset_t offset, | |
107 | memory_object_size_t size, | |
108 | vm_prot_t desired_access); | |
109 | kern_return_t swapfile_pager_synchronize(memory_object_t mem_obj, | |
110 | memory_object_offset_t offset, | |
111 | memory_object_size_t length, | |
112 | vm_sync_t sync_flags); | |
113 | kern_return_t swapfile_pager_map(memory_object_t mem_obj, | |
114 | vm_prot_t prot); | |
115 | kern_return_t swapfile_pager_last_unmap(memory_object_t mem_obj); | |
116 | ||
117 | /* | |
118 | * Vector of VM operations for this EMM. | |
119 | * These routines are invoked by VM via the memory_object_*() interfaces. | |
120 | */ | |
121 | const struct memory_object_pager_ops swapfile_pager_ops = { | |
122 | swapfile_pager_reference, | |
123 | swapfile_pager_deallocate, | |
124 | swapfile_pager_init, | |
125 | swapfile_pager_terminate, | |
126 | swapfile_pager_data_request, | |
127 | swapfile_pager_data_return, | |
128 | swapfile_pager_data_initialize, | |
129 | swapfile_pager_data_unlock, | |
130 | swapfile_pager_synchronize, | |
131 | swapfile_pager_map, | |
132 | swapfile_pager_last_unmap, | |
133 | "swapfile pager" | |
134 | }; | |
135 | ||
136 | /* | |
137 | * The "swapfile_pager" describes a memory object backed by | |
138 | * the "swapfile" EMM. | |
139 | */ | |
140 | typedef struct swapfile_pager { | |
141 | struct ipc_object_header pager_header; /* fake ip_kotype() */ | |
142 | memory_object_pager_ops_t pager_ops; /* == &swapfile_pager_ops */ | |
143 | queue_chain_t pager_queue; /* next & prev pagers */ | |
144 | unsigned int ref_count; /* reference count */ | |
145 | boolean_t is_ready; /* is this pager ready ? */ | |
146 | boolean_t is_mapped; /* is this pager mapped ? */ | |
147 | memory_object_control_t pager_control; /* mem object control handle */ | |
148 | struct vnode *swapfile_vnode;/* the swapfile's vnode */ | |
149 | } *swapfile_pager_t; | |
150 | #define SWAPFILE_PAGER_NULL ((swapfile_pager_t) NULL) | |
151 | #define pager_ikot pager_header.io_bits | |
152 | ||
153 | /* | |
154 | * List of memory objects managed by this EMM. | |
155 | * The list is protected by the "swapfile_pager_lock" lock. | |
156 | */ | |
157 | int swapfile_pager_count = 0; /* number of pagers */ | |
158 | queue_head_t swapfile_pager_queue; | |
159 | decl_lck_mtx_data(,swapfile_pager_lock) | |
160 | ||
161 | /* | |
162 | * Statistics & counters. | |
163 | */ | |
164 | int swapfile_pager_count_max = 0; | |
165 | ||
166 | ||
167 | lck_grp_t swapfile_pager_lck_grp; | |
168 | lck_grp_attr_t swapfile_pager_lck_grp_attr; | |
169 | lck_attr_t swapfile_pager_lck_attr; | |
170 | ||
171 | ||
172 | /* internal prototypes */ | |
173 | swapfile_pager_t swapfile_pager_create(struct vnode *vp); | |
174 | swapfile_pager_t swapfile_pager_lookup(memory_object_t mem_obj); | |
175 | void swapfile_pager_dequeue(swapfile_pager_t pager); | |
176 | void swapfile_pager_deallocate_internal(swapfile_pager_t pager, | |
177 | boolean_t locked); | |
178 | void swapfile_pager_terminate_internal(swapfile_pager_t pager); | |
179 | ||
180 | ||
181 | #if DEBUG | |
182 | int swapfile_pagerdebug = 0; | |
183 | #define PAGER_ALL 0xffffffff | |
184 | #define PAGER_INIT 0x00000001 | |
185 | #define PAGER_PAGEIN 0x00000002 | |
186 | ||
187 | #define PAGER_DEBUG(LEVEL, A) \ | |
188 | MACRO_BEGIN \ | |
189 | if ((swapfile_pagerdebug & LEVEL)==LEVEL) { \ | |
190 | printf A; \ | |
191 | } \ | |
192 | MACRO_END | |
193 | #else | |
194 | #define PAGER_DEBUG(LEVEL, A) | |
195 | #endif | |
196 | ||
197 | ||
198 | void | |
199 | swapfile_pager_bootstrap(void) | |
200 | { | |
201 | lck_grp_attr_setdefault(&swapfile_pager_lck_grp_attr); | |
202 | lck_grp_init(&swapfile_pager_lck_grp, "swapfile pager", &swapfile_pager_lck_grp_attr); | |
203 | lck_attr_setdefault(&swapfile_pager_lck_attr); | |
204 | lck_mtx_init(&swapfile_pager_lock, &swapfile_pager_lck_grp, &swapfile_pager_lck_attr); | |
205 | queue_init(&swapfile_pager_queue); | |
206 | } | |
207 | ||
208 | /* | |
209 | * swapfile_pager_init() | |
210 | * | |
211 | * Initialize the memory object and makes it ready to be used and mapped. | |
212 | */ | |
213 | kern_return_t | |
214 | swapfile_pager_init( | |
215 | memory_object_t mem_obj, | |
216 | memory_object_control_t control, | |
217 | #if !DEBUG | |
218 | __unused | |
219 | #endif | |
220 | memory_object_cluster_size_t pg_size) | |
221 | { | |
222 | swapfile_pager_t pager; | |
223 | kern_return_t kr; | |
224 | memory_object_attr_info_data_t attributes; | |
225 | ||
226 | PAGER_DEBUG(PAGER_ALL, | |
227 | ("swapfile_pager_init: %p, %p, %x\n", | |
228 | mem_obj, control, pg_size)); | |
229 | ||
230 | if (control == MEMORY_OBJECT_CONTROL_NULL) | |
231 | return KERN_INVALID_ARGUMENT; | |
232 | ||
233 | pager = swapfile_pager_lookup(mem_obj); | |
234 | ||
235 | memory_object_control_reference(control); | |
236 | ||
237 | pager->pager_control = control; | |
238 | ||
239 | attributes.copy_strategy = MEMORY_OBJECT_COPY_DELAY; | |
240 | attributes.cluster_size = (1 << (PAGE_SHIFT)); | |
241 | attributes.may_cache_object = FALSE; | |
242 | attributes.temporary = TRUE; | |
243 | ||
244 | kr = memory_object_change_attributes( | |
245 | control, | |
246 | MEMORY_OBJECT_ATTRIBUTE_INFO, | |
247 | (memory_object_info_t) &attributes, | |
248 | MEMORY_OBJECT_ATTR_INFO_COUNT); | |
249 | if (kr != KERN_SUCCESS) | |
250 | panic("swapfile_pager_init: " | |
251 | "memory_object_change_attributes() failed"); | |
252 | ||
253 | return KERN_SUCCESS; | |
254 | } | |
255 | ||
256 | /* | |
257 | * swapfile_data_return() | |
258 | * | |
259 | * Handles page-out requests from VM. This should never happen since | |
260 | * the pages provided by this EMM are not supposed to be dirty or dirtied | |
261 | * and VM should simply discard the contents and reclaim the pages if it | |
262 | * needs to. | |
263 | */ | |
264 | kern_return_t | |
265 | swapfile_pager_data_return( | |
266 | __unused memory_object_t mem_obj, | |
267 | __unused memory_object_offset_t offset, | |
268 | __unused memory_object_cluster_size_t data_cnt, | |
269 | __unused memory_object_offset_t *resid_offset, | |
270 | __unused int *io_error, | |
271 | __unused boolean_t dirty, | |
272 | __unused boolean_t kernel_copy, | |
273 | __unused int upl_flags) | |
274 | { | |
275 | panic("swapfile_pager_data_return: should never get called"); | |
276 | return KERN_FAILURE; | |
277 | } | |
278 | ||
279 | kern_return_t | |
280 | swapfile_pager_data_initialize( | |
281 | __unused memory_object_t mem_obj, | |
282 | __unused memory_object_offset_t offset, | |
283 | __unused memory_object_cluster_size_t data_cnt) | |
284 | { | |
285 | panic("swapfile_pager_data_initialize: should never get called"); | |
286 | return KERN_FAILURE; | |
287 | } | |
288 | ||
289 | kern_return_t | |
290 | swapfile_pager_data_unlock( | |
291 | __unused memory_object_t mem_obj, | |
292 | __unused memory_object_offset_t offset, | |
293 | __unused memory_object_size_t size, | |
294 | __unused vm_prot_t desired_access) | |
295 | { | |
296 | return KERN_FAILURE; | |
297 | } | |
298 | ||
299 | /* | |
300 | * swapfile_pager_data_request() | |
301 | * | |
302 | * Handles page-in requests from VM. | |
303 | */ | |
304 | kern_return_t | |
305 | swapfile_pager_data_request( | |
306 | memory_object_t mem_obj, | |
307 | memory_object_offset_t offset, | |
308 | memory_object_cluster_size_t length, | |
309 | #if !DEBUG | |
310 | __unused | |
311 | #endif | |
312 | vm_prot_t protection_required, | |
313 | __unused memory_object_fault_info_t mo_fault_info) | |
314 | { | |
315 | swapfile_pager_t pager; | |
316 | memory_object_control_t mo_control; | |
317 | upl_t upl; | |
318 | int upl_flags; | |
319 | upl_size_t upl_size; | |
320 | upl_page_info_t *upl_pl = NULL; | |
321 | unsigned int pl_count; | |
322 | vm_object_t dst_object; | |
323 | kern_return_t kr, retval; | |
324 | vm_map_offset_t kernel_mapping; | |
325 | vm_offset_t dst_vaddr; | |
326 | char *dst_ptr; | |
327 | vm_offset_t cur_offset; | |
328 | vm_map_entry_t map_entry; | |
329 | ||
330 | PAGER_DEBUG(PAGER_ALL, ("swapfile_pager_data_request: %p, %llx, %x, %x\n", mem_obj, offset, length, protection_required)); | |
331 | ||
332 | kernel_mapping = 0; | |
333 | upl = NULL; | |
334 | upl_pl = NULL; | |
335 | ||
336 | pager = swapfile_pager_lookup(mem_obj); | |
337 | assert(pager->is_ready); | |
338 | assert(pager->ref_count > 1); /* pager is alive and mapped */ | |
339 | ||
340 | PAGER_DEBUG(PAGER_PAGEIN, ("swapfile_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj, offset, length, protection_required, pager)); | |
341 | ||
342 | /* | |
343 | * Gather in a UPL all the VM pages requested by VM. | |
344 | */ | |
345 | mo_control = pager->pager_control; | |
346 | ||
347 | upl_size = length; | |
348 | upl_flags = | |
349 | UPL_RET_ONLY_ABSENT | | |
350 | UPL_SET_LITE | | |
351 | UPL_NO_SYNC | | |
352 | UPL_CLEAN_IN_PLACE | /* triggers UPL_CLEAR_DIRTY */ | |
353 | UPL_SET_INTERNAL; | |
354 | pl_count = 0; | |
355 | kr = memory_object_upl_request(mo_control, | |
356 | offset, upl_size, | |
357 | &upl, NULL, NULL, upl_flags); | |
358 | if (kr != KERN_SUCCESS) { | |
359 | retval = kr; | |
360 | goto done; | |
361 | } | |
362 | dst_object = mo_control->moc_object; | |
363 | assert(dst_object != VM_OBJECT_NULL); | |
364 | ||
365 | ||
366 | /* | |
367 | * Reserve a virtual page in the kernel address space to map each | |
368 | * destination physical page when it's its turn to be processed. | |
369 | */ | |
370 | vm_object_reference(kernel_object); /* ref. for mapping */ | |
371 | kr = vm_map_find_space(kernel_map, | |
372 | &kernel_mapping, | |
373 | PAGE_SIZE_64, | |
374 | 0, | |
375 | 0, | |
376 | &map_entry); | |
377 | if (kr != KERN_SUCCESS) { | |
378 | vm_object_deallocate(kernel_object); | |
379 | retval = kr; | |
380 | goto done; | |
381 | } | |
382 | map_entry->object.vm_object = kernel_object; | |
383 | map_entry->offset = kernel_mapping - VM_MIN_KERNEL_ADDRESS; | |
384 | vm_map_unlock(kernel_map); | |
385 | dst_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping); | |
386 | dst_ptr = (char *) dst_vaddr; | |
387 | ||
388 | /* | |
389 | * Fill in the contents of the pages requested by VM. | |
390 | */ | |
391 | upl_pl = UPL_GET_INTERNAL_PAGE_LIST(upl); | |
392 | pl_count = length / PAGE_SIZE; | |
393 | for (cur_offset = 0; cur_offset < length; cur_offset += PAGE_SIZE) { | |
394 | ppnum_t dst_pnum; | |
395 | ||
396 | if (!upl_page_present(upl_pl, (int)(cur_offset / PAGE_SIZE))) { | |
397 | /* this page is not in the UPL: skip it */ | |
398 | continue; | |
399 | } | |
400 | ||
401 | /* | |
402 | * Establish an explicit pmap mapping of the destination | |
403 | * physical page. | |
404 | * We can't do a regular VM mapping because the VM page | |
405 | * is "busy". | |
406 | */ | |
407 | dst_pnum = (ppnum_t) | |
408 | upl_phys_page(upl_pl, (int)(cur_offset / PAGE_SIZE)); | |
409 | assert(dst_pnum != 0); | |
410 | pmap_enter(kernel_pmap, | |
411 | kernel_mapping, | |
412 | dst_pnum, | |
413 | VM_PROT_READ | VM_PROT_WRITE, | |
414 | dst_object->wimg_bits & VM_WIMG_MASK, | |
415 | TRUE); | |
416 | ||
417 | memset(dst_ptr, '\0', PAGE_SIZE); | |
418 | /* add an end-of-line to keep line counters happy */ | |
419 | dst_ptr[PAGE_SIZE-1] = '\n'; | |
420 | ||
421 | /* | |
422 | * Remove the pmap mapping of the destination page | |
423 | * in the kernel. | |
424 | */ | |
425 | pmap_remove(kernel_pmap, | |
426 | (addr64_t) kernel_mapping, | |
427 | (addr64_t) (kernel_mapping + PAGE_SIZE_64)); | |
428 | ||
429 | } | |
430 | ||
431 | retval = KERN_SUCCESS; | |
432 | done: | |
433 | if (upl != NULL) { | |
434 | /* clean up the UPL */ | |
435 | ||
436 | /* | |
437 | * The pages are currently dirty because we've just been | |
438 | * writing on them, but as far as we're concerned, they're | |
439 | * clean since they contain their "original" contents as | |
440 | * provided by us, the pager. | |
441 | * Tell the UPL to mark them "clean". | |
442 | */ | |
443 | upl_clear_dirty(upl, TRUE); | |
444 | ||
445 | /* abort or commit the UPL */ | |
446 | if (retval != KERN_SUCCESS) { | |
447 | upl_abort(upl, 0); | |
448 | } else { | |
449 | boolean_t empty; | |
450 | upl_commit_range(upl, 0, upl->size, | |
451 | UPL_COMMIT_CS_VALIDATED, | |
452 | upl_pl, pl_count, &empty); | |
453 | } | |
454 | ||
455 | /* and deallocate the UPL */ | |
456 | upl_deallocate(upl); | |
457 | upl = NULL; | |
458 | } | |
459 | if (kernel_mapping != 0) { | |
460 | /* clean up the mapping of the source and destination pages */ | |
461 | kr = vm_map_remove(kernel_map, | |
462 | kernel_mapping, | |
463 | kernel_mapping + PAGE_SIZE_64, | |
464 | VM_MAP_NO_FLAGS); | |
465 | assert(kr == KERN_SUCCESS); | |
466 | kernel_mapping = 0; | |
467 | dst_vaddr = 0; | |
468 | } | |
469 | ||
470 | return retval; | |
471 | } | |
472 | ||
473 | /* | |
474 | * swapfile_pager_reference() | |
475 | * | |
476 | * Get a reference on this memory object. | |
477 | * For external usage only. Assumes that the initial reference count is not 0, | |
478 | * i.e one should not "revive" a dead pager this way. | |
479 | */ | |
480 | void | |
481 | swapfile_pager_reference( | |
482 | memory_object_t mem_obj) | |
483 | { | |
484 | swapfile_pager_t pager; | |
485 | ||
486 | pager = swapfile_pager_lookup(mem_obj); | |
487 | ||
488 | lck_mtx_lock(&swapfile_pager_lock); | |
489 | assert(pager->ref_count > 0); | |
490 | pager->ref_count++; | |
491 | lck_mtx_unlock(&swapfile_pager_lock); | |
492 | } | |
493 | ||
494 | ||
495 | /* | |
496 | * swapfile_pager_dequeue: | |
497 | * | |
498 | * Removes a pager from the list of pagers. | |
499 | * | |
500 | * The caller must hold "swapfile_pager_lock". | |
501 | */ | |
502 | void | |
503 | swapfile_pager_dequeue( | |
504 | swapfile_pager_t pager) | |
505 | { | |
506 | assert(!pager->is_mapped); | |
507 | ||
508 | queue_remove(&swapfile_pager_queue, | |
509 | pager, | |
510 | swapfile_pager_t, | |
511 | pager_queue); | |
512 | pager->pager_queue.next = NULL; | |
513 | pager->pager_queue.prev = NULL; | |
514 | ||
515 | swapfile_pager_count--; | |
516 | } | |
517 | ||
518 | /* | |
519 | * swapfile_pager_terminate_internal: | |
520 | * | |
521 | * Trigger the asynchronous termination of the memory object associated | |
522 | * with this pager. | |
523 | * When the memory object is terminated, there will be one more call | |
524 | * to memory_object_deallocate() (i.e. swapfile_pager_deallocate()) | |
525 | * to finish the clean up. | |
526 | * | |
527 | * "swapfile_pager_lock" should not be held by the caller. | |
528 | * We don't need the lock because the pager has already been removed from | |
529 | * the pagers' list and is now ours exclusively. | |
530 | */ | |
531 | void | |
532 | swapfile_pager_terminate_internal( | |
533 | swapfile_pager_t pager) | |
534 | { | |
535 | assert(pager->is_ready); | |
536 | assert(!pager->is_mapped); | |
537 | ||
538 | if (pager->swapfile_vnode != NULL) { | |
539 | pager->swapfile_vnode = NULL; | |
540 | } | |
541 | ||
542 | /* trigger the destruction of the memory object */ | |
543 | memory_object_destroy(pager->pager_control, 0); | |
544 | } | |
545 | ||
546 | /* | |
547 | * swapfile_pager_deallocate_internal() | |
548 | * | |
549 | * Release a reference on this pager and free it when the last | |
550 | * reference goes away. | |
551 | * Can be called with swapfile_pager_lock held or not but always returns | |
552 | * with it unlocked. | |
553 | */ | |
554 | void | |
555 | swapfile_pager_deallocate_internal( | |
556 | swapfile_pager_t pager, | |
557 | boolean_t locked) | |
558 | { | |
559 | if (! locked) { | |
560 | lck_mtx_lock(&swapfile_pager_lock); | |
561 | } | |
562 | ||
563 | /* drop a reference on this pager */ | |
564 | pager->ref_count--; | |
565 | ||
566 | if (pager->ref_count == 1) { | |
567 | /* | |
568 | * Only the "named" reference is left, which means that | |
569 | * no one is really holding on to this pager anymore. | |
570 | * Terminate it. | |
571 | */ | |
572 | swapfile_pager_dequeue(pager); | |
573 | /* the pager is all ours: no need for the lock now */ | |
574 | lck_mtx_unlock(&swapfile_pager_lock); | |
575 | swapfile_pager_terminate_internal(pager); | |
576 | } else if (pager->ref_count == 0) { | |
577 | /* | |
578 | * Dropped the existence reference; the memory object has | |
579 | * been terminated. Do some final cleanup and release the | |
580 | * pager structure. | |
581 | */ | |
582 | lck_mtx_unlock(&swapfile_pager_lock); | |
583 | if (pager->pager_control != MEMORY_OBJECT_CONTROL_NULL) { | |
584 | memory_object_control_deallocate(pager->pager_control); | |
585 | pager->pager_control = MEMORY_OBJECT_CONTROL_NULL; | |
586 | } | |
587 | kfree(pager, sizeof (*pager)); | |
588 | pager = SWAPFILE_PAGER_NULL; | |
589 | } else { | |
590 | /* there are still plenty of references: keep going... */ | |
591 | lck_mtx_unlock(&swapfile_pager_lock); | |
592 | } | |
593 | ||
594 | /* caution: lock is not held on return... */ | |
595 | } | |
596 | ||
597 | /* | |
598 | * swapfile_pager_deallocate() | |
599 | * | |
600 | * Release a reference on this pager and free it when the last | |
601 | * reference goes away. | |
602 | */ | |
603 | void | |
604 | swapfile_pager_deallocate( | |
605 | memory_object_t mem_obj) | |
606 | { | |
607 | swapfile_pager_t pager; | |
608 | ||
609 | PAGER_DEBUG(PAGER_ALL, ("swapfile_pager_deallocate: %p\n", mem_obj)); | |
610 | pager = swapfile_pager_lookup(mem_obj); | |
611 | swapfile_pager_deallocate_internal(pager, FALSE); | |
612 | } | |
613 | ||
614 | /* | |
615 | * | |
616 | */ | |
617 | kern_return_t | |
618 | swapfile_pager_terminate( | |
619 | #if !DEBUG | |
620 | __unused | |
621 | #endif | |
622 | memory_object_t mem_obj) | |
623 | { | |
624 | PAGER_DEBUG(PAGER_ALL, ("swapfile_pager_terminate: %p\n", mem_obj)); | |
625 | ||
626 | return KERN_SUCCESS; | |
627 | } | |
628 | ||
629 | /* | |
630 | * | |
631 | */ | |
632 | kern_return_t | |
633 | swapfile_pager_synchronize( | |
634 | memory_object_t mem_obj, | |
635 | memory_object_offset_t offset, | |
636 | memory_object_size_t length, | |
637 | __unused vm_sync_t sync_flags) | |
638 | { | |
639 | swapfile_pager_t pager; | |
640 | ||
641 | PAGER_DEBUG(PAGER_ALL, ("swapfile_pager_synchronize: %p\n", mem_obj)); | |
642 | ||
643 | pager = swapfile_pager_lookup(mem_obj); | |
644 | ||
645 | memory_object_synchronize_completed(pager->pager_control, | |
646 | offset, length); | |
647 | ||
648 | return KERN_SUCCESS; | |
649 | } | |
650 | ||
651 | /* | |
652 | * swapfile_pager_map() | |
653 | * | |
654 | * This allows VM to let us, the EMM, know that this memory object | |
655 | * is currently mapped one or more times. This is called by VM each time | |
656 | * the memory object gets mapped and we take one extra reference on the | |
657 | * memory object to account for all its mappings. | |
658 | */ | |
659 | kern_return_t | |
660 | swapfile_pager_map( | |
661 | memory_object_t mem_obj, | |
662 | __unused vm_prot_t prot) | |
663 | { | |
664 | swapfile_pager_t pager; | |
665 | ||
666 | PAGER_DEBUG(PAGER_ALL, ("swapfile_pager_map: %p\n", mem_obj)); | |
667 | ||
668 | pager = swapfile_pager_lookup(mem_obj); | |
669 | ||
670 | lck_mtx_lock(&swapfile_pager_lock); | |
671 | assert(pager->is_ready); | |
672 | assert(pager->ref_count > 0); /* pager is alive */ | |
673 | if (pager->is_mapped == FALSE) { | |
674 | /* | |
675 | * First mapping of this pager: take an extra reference | |
676 | * that will remain until all the mappings of this pager | |
677 | * are removed. | |
678 | */ | |
679 | pager->is_mapped = TRUE; | |
680 | pager->ref_count++; | |
681 | } | |
682 | lck_mtx_unlock(&swapfile_pager_lock); | |
683 | ||
684 | return KERN_SUCCESS; | |
685 | } | |
686 | ||
687 | /* | |
688 | * swapfile_pager_last_unmap() | |
689 | * | |
690 | * This is called by VM when this memory object is no longer mapped anywhere. | |
691 | */ | |
692 | kern_return_t | |
693 | swapfile_pager_last_unmap( | |
694 | memory_object_t mem_obj) | |
695 | { | |
696 | swapfile_pager_t pager; | |
697 | ||
698 | PAGER_DEBUG(PAGER_ALL, | |
699 | ("swapfile_pager_last_unmap: %p\n", mem_obj)); | |
700 | ||
701 | pager = swapfile_pager_lookup(mem_obj); | |
702 | ||
703 | lck_mtx_lock(&swapfile_pager_lock); | |
704 | if (pager->is_mapped) { | |
705 | /* | |
706 | * All the mappings are gone, so let go of the one extra | |
707 | * reference that represents all the mappings of this pager. | |
708 | */ | |
709 | pager->is_mapped = FALSE; | |
710 | swapfile_pager_deallocate_internal(pager, TRUE); | |
711 | /* caution: deallocate_internal() released the lock ! */ | |
712 | } else { | |
713 | lck_mtx_unlock(&swapfile_pager_lock); | |
714 | } | |
715 | ||
716 | return KERN_SUCCESS; | |
717 | } | |
718 | ||
719 | ||
720 | /* | |
721 | * | |
722 | */ | |
723 | swapfile_pager_t | |
724 | swapfile_pager_lookup( | |
725 | memory_object_t mem_obj) | |
726 | { | |
727 | swapfile_pager_t pager; | |
728 | ||
729 | pager = (swapfile_pager_t) mem_obj; | |
730 | assert(pager->pager_ops == &swapfile_pager_ops); | |
731 | assert(pager->ref_count > 0); | |
732 | return pager; | |
733 | } | |
734 | ||
735 | swapfile_pager_t | |
736 | swapfile_pager_create( | |
737 | struct vnode *vp) | |
738 | { | |
739 | swapfile_pager_t pager, pager2; | |
740 | memory_object_control_t control; | |
741 | kern_return_t kr; | |
742 | ||
743 | pager = (swapfile_pager_t) kalloc(sizeof (*pager)); | |
744 | if (pager == SWAPFILE_PAGER_NULL) { | |
745 | return SWAPFILE_PAGER_NULL; | |
746 | } | |
747 | ||
748 | /* | |
749 | * The vm_map call takes both named entry ports and raw memory | |
750 | * objects in the same parameter. We need to make sure that | |
751 | * vm_map does not see this object as a named entry port. So, | |
752 | * we reserve the second word in the object for a fake ip_kotype | |
753 | * setting - that will tell vm_map to use it as a memory object. | |
754 | */ | |
755 | pager->pager_ops = &swapfile_pager_ops; | |
756 | pager->pager_ikot = IKOT_MEMORY_OBJECT; | |
757 | pager->is_ready = FALSE;/* not ready until it has a "name" */ | |
758 | pager->ref_count = 1; /* setup reference */ | |
759 | pager->is_mapped = FALSE; | |
760 | pager->pager_control = MEMORY_OBJECT_CONTROL_NULL; | |
761 | pager->swapfile_vnode = vp; | |
762 | ||
763 | lck_mtx_lock(&swapfile_pager_lock); | |
764 | /* see if anyone raced us to create a pager for the same object */ | |
765 | queue_iterate(&swapfile_pager_queue, | |
766 | pager2, | |
767 | swapfile_pager_t, | |
768 | pager_queue) { | |
769 | if (pager2->swapfile_vnode == vp) { | |
770 | break; | |
771 | } | |
772 | } | |
773 | if (! queue_end(&swapfile_pager_queue, | |
774 | (queue_entry_t) pager2)) { | |
775 | /* while we hold the lock, transfer our setup ref to winner */ | |
776 | pager2->ref_count++; | |
777 | /* we lost the race, down with the loser... */ | |
778 | lck_mtx_unlock(&swapfile_pager_lock); | |
779 | pager->swapfile_vnode = NULL; | |
780 | kfree(pager, sizeof (*pager)); | |
781 | /* ... and go with the winner */ | |
782 | pager = pager2; | |
783 | /* let the winner make sure the pager gets ready */ | |
784 | return pager; | |
785 | } | |
786 | ||
787 | /* enter new pager at the head of our list of pagers */ | |
788 | queue_enter_first(&swapfile_pager_queue, | |
789 | pager, | |
790 | swapfile_pager_t, | |
791 | pager_queue); | |
792 | swapfile_pager_count++; | |
793 | if (swapfile_pager_count > swapfile_pager_count_max) { | |
794 | swapfile_pager_count_max = swapfile_pager_count; | |
795 | } | |
796 | lck_mtx_unlock(&swapfile_pager_lock); | |
797 | ||
798 | kr = memory_object_create_named((memory_object_t) pager, | |
799 | 0, | |
800 | &control); | |
801 | assert(kr == KERN_SUCCESS); | |
802 | ||
803 | lck_mtx_lock(&swapfile_pager_lock); | |
804 | /* the new pager is now ready to be used */ | |
805 | pager->is_ready = TRUE; | |
806 | lck_mtx_unlock(&swapfile_pager_lock); | |
807 | ||
808 | /* wakeup anyone waiting for this pager to be ready */ | |
809 | thread_wakeup(&pager->is_ready); | |
810 | ||
811 | return pager; | |
812 | } | |
813 | ||
814 | /* | |
815 | * swapfile_pager_setup() | |
816 | * | |
817 | * Provide the caller with a memory object backed by the provided | |
818 | * "backing_object" VM object. If such a memory object already exists, | |
819 | * re-use it, otherwise create a new memory object. | |
820 | */ | |
821 | memory_object_t | |
822 | swapfile_pager_setup( | |
823 | struct vnode *vp) | |
824 | { | |
825 | swapfile_pager_t pager; | |
826 | ||
827 | lck_mtx_lock(&swapfile_pager_lock); | |
828 | ||
829 | queue_iterate(&swapfile_pager_queue, | |
830 | pager, | |
831 | swapfile_pager_t, | |
832 | pager_queue) { | |
833 | if (pager->swapfile_vnode == vp) { | |
834 | break; | |
835 | } | |
836 | } | |
837 | if (queue_end(&swapfile_pager_queue, | |
838 | (queue_entry_t) pager)) { | |
839 | /* no existing pager for this backing object */ | |
840 | pager = SWAPFILE_PAGER_NULL; | |
841 | } else { | |
842 | /* make sure pager doesn't disappear */ | |
843 | pager->ref_count++; | |
844 | } | |
845 | ||
846 | lck_mtx_unlock(&swapfile_pager_lock); | |
847 | ||
848 | if (pager == SWAPFILE_PAGER_NULL) { | |
849 | pager = swapfile_pager_create(vp); | |
850 | if (pager == SWAPFILE_PAGER_NULL) { | |
851 | return MEMORY_OBJECT_NULL; | |
852 | } | |
853 | } | |
854 | ||
855 | lck_mtx_lock(&swapfile_pager_lock); | |
856 | while (!pager->is_ready) { | |
857 | lck_mtx_sleep(&swapfile_pager_lock, | |
858 | LCK_SLEEP_DEFAULT, | |
859 | &pager->is_ready, | |
860 | THREAD_UNINT); | |
861 | } | |
862 | lck_mtx_unlock(&swapfile_pager_lock); | |
863 | ||
864 | return (memory_object_t) pager; | |
865 | } | |
866 | ||
867 | memory_object_control_t | |
868 | swapfile_pager_control( | |
869 | memory_object_t mem_obj) | |
870 | { | |
871 | swapfile_pager_t pager; | |
872 | ||
873 | pager = swapfile_pager_lookup(mem_obj); | |
874 | ||
875 | return pager->pager_control; | |
876 | } |