]> git.saurik.com Git - apple/xnu.git/blame - osfmk/vm/bsd_vm.c
xnu-1228.7.58.tar.gz
[apple/xnu.git] / osfmk / vm / bsd_vm.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 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 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28
29#include <sys/errno.h>
91447636 30
1c79356b 31#include <mach/mach_types.h>
91447636
A
32#include <mach/mach_traps.h>
33#include <mach/host_priv.h>
1c79356b 34#include <mach/kern_return.h>
91447636 35#include <mach/memory_object_control.h>
1c79356b
A
36#include <mach/memory_object_types.h>
37#include <mach/port.h>
38#include <mach/policy.h>
91447636
A
39#include <mach/upl.h>
40#include <mach/thread_act.h>
41
2d21ac55 42#include <kern/assert.h>
91447636
A
43#include <kern/host.h>
44#include <kern/thread.h>
45
1c79356b
A
46#include <ipc/ipc_port.h>
47#include <ipc/ipc_space.h>
1c79356b 48
0b4e3aa0 49#include <default_pager/default_pager_types.h>
91447636
A
50#include <default_pager/default_pager_object_server.h>
51
52#include <vm/vm_map.h>
91447636
A
53#include <vm/vm_pageout.h>
54#include <vm/memory_object.h>
55#include <vm/vm_pageout.h>
56#include <vm/vm_protos.h>
2d21ac55
A
57#include <vm/vm_purgeable_internal.h>
58
1c79356b
A
59
60/* BSD VM COMPONENT INTERFACES */
61int
62get_map_nentries(
63 vm_map_t);
64
65vm_offset_t
66get_map_start(
67 vm_map_t);
68
69vm_offset_t
70get_map_end(
71 vm_map_t);
72
73/*
74 *
75 */
76int
77get_map_nentries(
78 vm_map_t map)
79{
80 return(map->hdr.nentries);
81}
82
91447636
A
83mach_vm_offset_t
84mach_get_vm_start(vm_map_t map)
85{
86 return( vm_map_first_entry(map)->vme_start);
87}
88
89mach_vm_offset_t
90mach_get_vm_end(vm_map_t map)
91{
92 return( vm_map_last_entry(map)->vme_end);
93}
94
1c79356b 95/*
91447636
A
96 * Legacy routines to get the start and end for a vm_map_t. They
97 * return them in the vm_offset_t format. So, they should only be
98 * called on maps that are the same size as the kernel map for
99 * accurate results.
1c79356b
A
100 */
101vm_offset_t
91447636 102get_vm_start(
1c79356b
A
103 vm_map_t map)
104{
91447636 105 return(CAST_DOWN(vm_offset_t, vm_map_first_entry(map)->vme_start));
1c79356b
A
106}
107
1c79356b 108vm_offset_t
91447636 109get_vm_end(
1c79356b
A
110 vm_map_t map)
111{
91447636 112 return(CAST_DOWN(vm_offset_t, vm_map_last_entry(map)->vme_end));
1c79356b
A
113}
114
115/*
116 * BSD VNODE PAGER
117 */
118
0c530ab8
A
119const struct memory_object_pager_ops vnode_pager_ops = {
120 vnode_pager_reference,
121 vnode_pager_deallocate,
122 vnode_pager_init,
123 vnode_pager_terminate,
124 vnode_pager_data_request,
125 vnode_pager_data_return,
126 vnode_pager_data_initialize,
127 vnode_pager_data_unlock,
128 vnode_pager_synchronize,
129 vnode_pager_unmap,
130 "vnode pager"
131};
1c79356b 132
1c79356b 133typedef struct vnode_pager {
0c530ab8 134 memory_object_pager_ops_t pager_ops; /* == &vnode_pager_ops */
0b4e3aa0
A
135 unsigned int pager_ikot; /* JMM: fake ip_kotype() */
136 unsigned int ref_count; /* reference count */
137 memory_object_control_t control_handle; /* mem object control handle */
91447636 138 struct vnode *vnode_handle; /* vnode handle */
1c79356b
A
139} *vnode_pager_t;
140
1c79356b
A
141
142ipc_port_t
91447636 143trigger_name_to_port( /* forward */
1c79356b
A
144 mach_port_t);
145
1c79356b 146kern_return_t
91447636 147vnode_pager_cluster_read( /* forward */
1c79356b
A
148 vnode_pager_t,
149 vm_object_offset_t,
150 vm_size_t);
151
152void
91447636 153vnode_pager_cluster_write( /* forward */
1c79356b
A
154 vnode_pager_t,
155 vm_object_offset_t,
91447636
A
156 vm_size_t,
157 vm_object_offset_t *,
158 int *,
159 int);
1c79356b 160
0b4e3aa0 161
1c79356b 162vnode_pager_t
91447636
A
163vnode_object_create( /* forward */
164 struct vnode *);
1c79356b 165
1c79356b 166vnode_pager_t
91447636 167vnode_pager_lookup( /* forward */
0b4e3aa0 168 memory_object_t);
1c79356b 169
1c79356b
A
170zone_t vnode_pager_zone;
171
172
173#define VNODE_PAGER_NULL ((vnode_pager_t) 0)
174
175/* TODO: Should be set dynamically by vnode_pager_init() */
176#define CLUSTER_SHIFT 1
177
178/* TODO: Should be set dynamically by vnode_pager_bootstrap() */
179#define MAX_VNODE 10000
180
181
182#if DEBUG
183int pagerdebug=0;
184
185#define PAGER_ALL 0xffffffff
186#define PAGER_INIT 0x00000001
187#define PAGER_PAGEIN 0x00000002
188
189#define PAGER_DEBUG(LEVEL, A) {if ((pagerdebug & LEVEL)==LEVEL){printf A;}}
190#else
191#define PAGER_DEBUG(LEVEL, A)
192#endif
193
194/*
195 * Routine: macx_triggers
196 * Function:
197 * Syscall interface to set the call backs for low and
198 * high water marks.
199 */
200int
201macx_triggers(
91447636 202 struct macx_triggers_args *args)
1c79356b 203{
91447636
A
204 int hi_water = args->hi_water;
205 int low_water = args->low_water;
206 int flags = args->flags;
207 mach_port_t trigger_name = args->alert_port;
1c79356b 208 kern_return_t kr;
0b4e3aa0 209 memory_object_default_t default_pager;
1c79356b
A
210 ipc_port_t trigger_port;
211
0b4e3aa0 212 default_pager = MEMORY_OBJECT_DEFAULT_NULL;
1c79356b 213 kr = host_default_memory_manager(host_priv_self(),
0b4e3aa0 214 &default_pager, 0);
1c79356b
A
215 if(kr != KERN_SUCCESS) {
216 return EINVAL;
217 }
91447636
A
218
219 if ((flags & SWAP_ENCRYPT_ON) &&
220 (flags & SWAP_ENCRYPT_OFF)) {
221 /* can't have it both ways */
222 return EINVAL;
223 }
224
2d21ac55 225 if (default_pager_init_flag == 0) {
0c530ab8
A
226 start_def_pager(NULL);
227 default_pager_init_flag = 1;
2d21ac55 228 }
0c530ab8 229
91447636
A
230 if (flags & SWAP_ENCRYPT_ON) {
231 /* ENCRYPTED SWAP: tell default_pager to encrypt */
232 default_pager_triggers(default_pager,
233 0, 0,
234 SWAP_ENCRYPT_ON,
235 IP_NULL);
236 } else if (flags & SWAP_ENCRYPT_OFF) {
237 /* ENCRYPTED SWAP: tell default_pager not to encrypt */
238 default_pager_triggers(default_pager,
239 0, 0,
240 SWAP_ENCRYPT_OFF,
241 IP_NULL);
242 }
243
0b4e3aa0
A
244 if (flags & HI_WAT_ALERT) {
245 trigger_port = trigger_name_to_port(trigger_name);
246 if(trigger_port == NULL) {
247 return EINVAL;
248 }
249 /* trigger_port is locked and active */
250 ipc_port_make_send_locked(trigger_port);
251 /* now unlocked */
252 default_pager_triggers(default_pager,
253 hi_water, low_water,
254 HI_WAT_ALERT, trigger_port);
255 }
256
257 if (flags & LO_WAT_ALERT) {
258 trigger_port = trigger_name_to_port(trigger_name);
259 if(trigger_port == NULL) {
260 return EINVAL;
261 }
262 /* trigger_port is locked and active */
263 ipc_port_make_send_locked(trigger_port);
264 /* and now its unlocked */
265 default_pager_triggers(default_pager,
266 hi_water, low_water,
267 LO_WAT_ALERT, trigger_port);
1c79356b 268 }
1c79356b
A
269
270 /*
271 * Set thread scheduling priority and policy for the current thread
272 * it is assumed for the time being that the thread setting the alert
55e303ae
A
273 * is the same one which will be servicing it.
274 *
275 * XXX This does not belong in the kernel XXX
1c79356b
A
276 */
277 {
55e303ae
A
278 thread_precedence_policy_data_t pre;
279 thread_extended_policy_data_t ext;
280
281 ext.timeshare = FALSE;
282 pre.importance = INT32_MAX;
283
91447636
A
284 thread_policy_set(current_thread(),
285 THREAD_EXTENDED_POLICY,
286 (thread_policy_t)&ext,
287 THREAD_EXTENDED_POLICY_COUNT);
55e303ae 288
91447636
A
289 thread_policy_set(current_thread(),
290 THREAD_PRECEDENCE_POLICY,
291 (thread_policy_t)&pre,
292 THREAD_PRECEDENCE_POLICY_COUNT);
1c79356b
A
293 }
294
91447636
A
295 current_thread()->options |= TH_OPT_VMPRIV;
296
297 return 0;
1c79356b
A
298}
299
300/*
301 *
302 */
303ipc_port_t
304trigger_name_to_port(
305 mach_port_t trigger_name)
306{
307 ipc_port_t trigger_port;
308 ipc_space_t space;
309
310 if (trigger_name == 0)
311 return (NULL);
312
313 space = current_space();
314 if(ipc_port_translate_receive(space, (mach_port_name_t)trigger_name,
315 &trigger_port) != KERN_SUCCESS)
316 return (NULL);
317 return trigger_port;
318}
319
91447636
A
320
321extern int uiomove64(addr64_t, int, void *);
322#define MAX_RUN 32
323
4a3eedf9
A
324unsigned long vm_cs_tainted_forces = 0;
325
91447636
A
326int
327memory_object_control_uiomove(
328 memory_object_control_t control,
329 memory_object_offset_t offset,
330 void * uio,
331 int start_offset,
332 int io_requested,
2d21ac55
A
333 int mark_dirty,
334 int take_reference)
91447636
A
335{
336 vm_object_t object;
337 vm_page_t dst_page;
338 int xsize;
339 int retval = 0;
340 int cur_run;
341 int cur_needed;
342 int i;
2d21ac55
A
343 int orig_offset;
344 boolean_t make_lru = FALSE;
91447636
A
345 vm_page_t page_run[MAX_RUN];
346
91447636
A
347 object = memory_object_control_to_vm_object(control);
348 if (object == VM_OBJECT_NULL) {
349 return (0);
350 }
351 assert(!object->internal);
352
353 vm_object_lock(object);
354
355 if (mark_dirty && object->copy != VM_OBJECT_NULL) {
356 /*
357 * We can't modify the pages without honoring
358 * copy-on-write obligations first, so fall off
359 * this optimized path and fall back to the regular
360 * path.
361 */
362 vm_object_unlock(object);
363 return 0;
364 }
2d21ac55 365 orig_offset = start_offset;
91447636
A
366
367 while (io_requested && retval == 0) {
368
369 cur_needed = (start_offset + io_requested + (PAGE_SIZE - 1)) / PAGE_SIZE;
370
371 if (cur_needed > MAX_RUN)
372 cur_needed = MAX_RUN;
373
374 for (cur_run = 0; cur_run < cur_needed; ) {
375
376 if ((dst_page = vm_page_lookup(object, offset)) == VM_PAGE_NULL)
377 break;
378 /*
379 * Sync up on getting the busy bit
380 */
381 if ((dst_page->busy || dst_page->cleaning)) {
382 /*
383 * someone else is playing with the page... if we've
384 * already collected pages into this run, go ahead
385 * and process now, we can't block on this
386 * page while holding other pages in the BUSY state
387 * otherwise we will wait
388 */
389 if (cur_run)
390 break;
391 PAGE_SLEEP(object, dst_page, THREAD_UNINT);
392 continue;
393 }
394 /*
395 * this routine is only called when copying
396 * to/from real files... no need to consider
397 * encrypted swap pages
398 */
399 assert(!dst_page->encrypted);
400
4a3eedf9 401 if (mark_dirty) {
91447636 402 dst_page->dirty = TRUE;
4a3eedf9
A
403 if (dst_page->cs_validated) {
404 /*
405 * CODE SIGNING:
406 * We're modifying a code-signed
407 * page: assume that it is now tainted.
408 */
409 dst_page->cs_tainted = TRUE;
410 vm_cs_tainted_forces++;
411 }
412 }
91447636
A
413 dst_page->busy = TRUE;
414
415 page_run[cur_run++] = dst_page;
416
417 offset += PAGE_SIZE_64;
418 }
419 if (cur_run == 0)
420 /*
421 * we hit a 'hole' in the cache
422 * we bail at this point
423 * we'll unlock the object below
424 */
425 break;
426 vm_object_unlock(object);
427
428 for (i = 0; i < cur_run; i++) {
429
430 dst_page = page_run[i];
431
432 if ((xsize = PAGE_SIZE - start_offset) > io_requested)
433 xsize = io_requested;
434
435 if ( (retval = uiomove64((addr64_t)(((addr64_t)(dst_page->phys_page) << 12) + start_offset), xsize, uio)) )
436 break;
437
438 io_requested -= xsize;
439 start_offset = 0;
440 }
441 vm_object_lock(object);
442
2d21ac55
A
443 /*
444 * if we have more than 1 page to work on
445 * in the current run, or the original request
446 * started at offset 0 of the page, or we're
447 * processing multiple batches, we will move
448 * the pages to the tail of the inactive queue
449 * to implement an LRU for read/write accesses
450 *
451 * the check for orig_offset == 0 is there to
452 * mitigate the cost of small (< page_size) requests
453 * to the same page (this way we only move it once)
454 */
455 if (take_reference && (cur_run > 1 || orig_offset == 0)) {
456 vm_page_lockspin_queues();
457 make_lru = TRUE;
458 }
91447636
A
459 for (i = 0; i < cur_run; i++) {
460 dst_page = page_run[i];
461
2d21ac55
A
462 /*
463 * someone is explicitly referencing this page...
464 * update clustered and speculative state
465 *
466 */
467 VM_PAGE_CONSUME_CLUSTERED(dst_page);
468
469 if (make_lru == TRUE)
470 vm_page_lru(dst_page);
471
91447636
A
472 PAGE_WAKEUP_DONE(dst_page);
473 }
2d21ac55
A
474 if (make_lru == TRUE) {
475 vm_page_unlock_queues();
476 make_lru = FALSE;
477 }
478 orig_offset = 0;
91447636
A
479 }
480 vm_object_unlock(object);
481
482 return (retval);
483}
484
485
1c79356b
A
486/*
487 *
488 */
489void
490vnode_pager_bootstrap(void)
491{
492 register vm_size_t size;
493
494 size = (vm_size_t) sizeof(struct vnode_pager);
495 vnode_pager_zone = zinit(size, (vm_size_t) MAX_VNODE*size,
496 PAGE_SIZE, "vnode pager structures");
0c530ab8
A
497#ifdef __i386__
498 apple_protect_pager_bootstrap();
499#endif /* __i386__ */
1c79356b
A
500 return;
501}
502
503/*
504 *
505 */
0b4e3aa0 506memory_object_t
1c79356b 507vnode_pager_setup(
91447636
A
508 struct vnode *vp,
509 __unused memory_object_t pager)
1c79356b
A
510{
511 vnode_pager_t vnode_object;
1c79356b
A
512
513 vnode_object = vnode_object_create(vp);
514 if (vnode_object == VNODE_PAGER_NULL)
515 panic("vnode_pager_setup: vnode_object_create() failed");
0b4e3aa0 516 return((memory_object_t)vnode_object);
1c79356b
A
517}
518
519/*
520 *
521 */
522kern_return_t
0b4e3aa0
A
523vnode_pager_init(memory_object_t mem_obj,
524 memory_object_control_t control,
91447636
A
525#if !DEBUG
526 __unused
527#endif
528 vm_size_t pg_size)
1c79356b
A
529{
530 vnode_pager_t vnode_object;
531 kern_return_t kr;
532 memory_object_attr_info_data_t attributes;
1c79356b
A
533
534
91447636 535 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_init: %p, %p, %x\n", mem_obj, control, pg_size));
1c79356b 536
0b4e3aa0
A
537 if (control == MEMORY_OBJECT_CONTROL_NULL)
538 return KERN_INVALID_ARGUMENT;
1c79356b 539
0b4e3aa0 540 vnode_object = vnode_pager_lookup(mem_obj);
1c79356b 541
0b4e3aa0 542 memory_object_control_reference(control);
91447636 543
0b4e3aa0 544 vnode_object->control_handle = control;
1c79356b
A
545
546 attributes.copy_strategy = MEMORY_OBJECT_COPY_DELAY;
547 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
548 attributes.cluster_size = (1 << (PAGE_SHIFT));
549 attributes.may_cache_object = TRUE;
550 attributes.temporary = TRUE;
551
552 kr = memory_object_change_attributes(
0b4e3aa0 553 control,
1c79356b
A
554 MEMORY_OBJECT_ATTRIBUTE_INFO,
555 (memory_object_info_t) &attributes,
0b4e3aa0 556 MEMORY_OBJECT_ATTR_INFO_COUNT);
1c79356b
A
557 if (kr != KERN_SUCCESS)
558 panic("vnode_pager_init: memory_object_change_attributes() failed");
559
560 return(KERN_SUCCESS);
561}
562
563/*
564 *
565 */
566kern_return_t
567vnode_pager_data_return(
0b4e3aa0
A
568 memory_object_t mem_obj,
569 memory_object_offset_t offset,
1c79356b 570 vm_size_t data_cnt,
91447636
A
571 memory_object_offset_t *resid_offset,
572 int *io_error,
573 __unused boolean_t dirty,
574 __unused boolean_t kernel_copy,
575 int upl_flags)
1c79356b
A
576{
577 register vnode_pager_t vnode_object;
578
0b4e3aa0 579 vnode_object = vnode_pager_lookup(mem_obj);
1c79356b 580
91447636 581 vnode_pager_cluster_write(vnode_object, offset, data_cnt, resid_offset, io_error, upl_flags);
1c79356b
A
582
583 return KERN_SUCCESS;
584}
585
0b4e3aa0
A
586kern_return_t
587vnode_pager_data_initialize(
91447636
A
588 __unused memory_object_t mem_obj,
589 __unused memory_object_offset_t offset,
590 __unused vm_size_t data_cnt)
0b4e3aa0 591{
91447636 592 panic("vnode_pager_data_initialize");
0b4e3aa0
A
593 return KERN_FAILURE;
594}
595
596kern_return_t
597vnode_pager_data_unlock(
91447636
A
598 __unused memory_object_t mem_obj,
599 __unused memory_object_offset_t offset,
600 __unused vm_size_t size,
601 __unused vm_prot_t desired_access)
0b4e3aa0
A
602{
603 return KERN_FAILURE;
604}
605
606kern_return_t
607vnode_pager_get_object_size(
608 memory_object_t mem_obj,
609 memory_object_offset_t *length)
610{
611 vnode_pager_t vnode_object;
612
0c530ab8
A
613 if (mem_obj->mo_pager_ops != &vnode_pager_ops) {
614 *length = 0;
615 return KERN_INVALID_ARGUMENT;
616 }
617
0b4e3aa0
A
618 vnode_object = vnode_pager_lookup(mem_obj);
619
620 *length = vnode_pager_get_filesize(vnode_object->vnode_handle);
621 return KERN_SUCCESS;
622}
623
0c530ab8
A
624kern_return_t
625vnode_pager_get_object_pathname(
626 memory_object_t mem_obj,
627 char *pathname,
628 vm_size_t *length_p)
629{
630 vnode_pager_t vnode_object;
631
632 if (mem_obj->mo_pager_ops != &vnode_pager_ops) {
633 return KERN_INVALID_ARGUMENT;
634 }
635
636 vnode_object = vnode_pager_lookup(mem_obj);
637
638 return vnode_pager_get_pathname(vnode_object->vnode_handle,
639 pathname,
640 length_p);
641}
642
643kern_return_t
644vnode_pager_get_object_filename(
645 memory_object_t mem_obj,
2d21ac55 646 const char **filename)
0c530ab8
A
647{
648 vnode_pager_t vnode_object;
649
650 if (mem_obj->mo_pager_ops != &vnode_pager_ops) {
651 return KERN_INVALID_ARGUMENT;
652 }
653
654 vnode_object = vnode_pager_lookup(mem_obj);
655
656 return vnode_pager_get_filename(vnode_object->vnode_handle,
657 filename);
658}
659
2d21ac55
A
660kern_return_t
661vnode_pager_get_object_cs_blobs(
662 memory_object_t mem_obj,
663 void **blobs)
664{
665 vnode_pager_t vnode_object;
666
667 if (mem_obj == MEMORY_OBJECT_NULL ||
668 mem_obj->mo_pager_ops != &vnode_pager_ops) {
669 return KERN_INVALID_ARGUMENT;
670 }
671
672 vnode_object = vnode_pager_lookup(mem_obj);
673
674 return vnode_pager_get_cs_blobs(vnode_object->vnode_handle,
675 blobs);
676}
677
1c79356b
A
678/*
679 *
680 */
681kern_return_t
682vnode_pager_data_request(
0b4e3aa0
A
683 memory_object_t mem_obj,
684 memory_object_offset_t offset,
2d21ac55
A
685 __unused vm_size_t length,
686 __unused vm_prot_t desired_access,
687 memory_object_fault_info_t fault_info)
1c79356b
A
688{
689 register vnode_pager_t vnode_object;
2d21ac55
A
690 vm_size_t size;
691#if MACH_ASSERT
692 memory_object_offset_t original_offset = offset;
693#endif /* MACH_ASSERT */
1c79356b 694
0b4e3aa0 695 vnode_object = vnode_pager_lookup(mem_obj);
1c79356b 696
2d21ac55
A
697 size = MAX_UPL_TRANSFER * PAGE_SIZE;
698
699 if (memory_object_cluster_size(vnode_object->control_handle, &offset, &size, fault_info) != KERN_SUCCESS)
700 size = PAGE_SIZE;
701
702 assert(original_offset >= offset &&
703 original_offset < offset + size);
704
705 return vnode_pager_cluster_read(vnode_object, offset, size);
1c79356b
A
706}
707
708/*
709 *
710 */
711void
0b4e3aa0
A
712vnode_pager_reference(
713 memory_object_t mem_obj)
714{
1c79356b 715 register vnode_pager_t vnode_object;
9bccf70c 716 unsigned int new_ref_count;
1c79356b 717
0b4e3aa0 718 vnode_object = vnode_pager_lookup(mem_obj);
9bccf70c
A
719 new_ref_count = hw_atomic_add(&vnode_object->ref_count, 1);
720 assert(new_ref_count > 1);
0b4e3aa0 721}
1c79356b 722
0b4e3aa0
A
723/*
724 *
725 */
726void
727vnode_pager_deallocate(
728 memory_object_t mem_obj)
729{
730 register vnode_pager_t vnode_object;
1c79356b 731
2d21ac55 732 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_deallocate: %p\n", mem_obj));
1c79356b 733
0b4e3aa0 734 vnode_object = vnode_pager_lookup(mem_obj);
1c79356b 735
9bccf70c 736 if (hw_atomic_sub(&vnode_object->ref_count, 1) == 0) {
91447636 737 if (vnode_object->vnode_handle != NULL) {
0b4e3aa0
A
738 vnode_pager_vrele(vnode_object->vnode_handle);
739 }
91447636 740 zfree(vnode_pager_zone, vnode_object);
0b4e3aa0 741 }
1c79356b
A
742 return;
743}
744
745/*
746 *
747 */
748kern_return_t
749vnode_pager_terminate(
91447636
A
750#if !DEBUG
751 __unused
752#endif
0b4e3aa0 753 memory_object_t mem_obj)
1c79356b 754{
2d21ac55 755 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_terminate: %p\n", mem_obj));
1c79356b 756
0b4e3aa0
A
757 return(KERN_SUCCESS);
758}
1c79356b 759
0b4e3aa0
A
760/*
761 *
762 */
763kern_return_t
764vnode_pager_synchronize(
765 memory_object_t mem_obj,
766 memory_object_offset_t offset,
767 vm_size_t length,
91447636 768 __unused vm_sync_t sync_flags)
0b4e3aa0
A
769{
770 register vnode_pager_t vnode_object;
1c79356b 771
2d21ac55 772 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_synchronize: %p\n", mem_obj));
1c79356b 773
0b4e3aa0 774 vnode_object = vnode_pager_lookup(mem_obj);
1c79356b 775
0b4e3aa0 776 memory_object_synchronize_completed(vnode_object->control_handle, offset, length);
1c79356b 777
0b4e3aa0 778 return (KERN_SUCCESS);
1c79356b
A
779}
780
781/*
782 *
783 */
784kern_return_t
0b4e3aa0
A
785vnode_pager_unmap(
786 memory_object_t mem_obj)
1c79356b 787{
0b4e3aa0 788 register vnode_pager_t vnode_object;
1c79356b 789
2d21ac55 790 PAGER_DEBUG(PAGER_ALL, ("vnode_pager_unmap: %p\n", mem_obj));
0b4e3aa0
A
791
792 vnode_object = vnode_pager_lookup(mem_obj);
793
794 ubc_unmap(vnode_object->vnode_handle);
795 return KERN_SUCCESS;
1c79356b
A
796}
797
0b4e3aa0 798
1c79356b
A
799/*
800 *
801 */
802void
803vnode_pager_cluster_write(
804 vnode_pager_t vnode_object,
805 vm_object_offset_t offset,
91447636
A
806 vm_size_t cnt,
807 vm_object_offset_t * resid_offset,
808 int * io_error,
809 int upl_flags)
1c79356b 810{
91447636
A
811 vm_size_t size;
812 upl_t upl = NULL;
813 int request_flags;
814 int errno;
1c79356b 815
91447636 816 if (upl_flags & UPL_MSYNC) {
1c79356b 817
91447636
A
818 upl_flags |= UPL_VNODE_PAGER;
819
820 if ( (upl_flags & UPL_IOSYNC) && io_error)
821 upl_flags |= UPL_KEEPCACHED;
822
823 while (cnt) {
824 kern_return_t kr;
825
826 size = (cnt < (PAGE_SIZE * MAX_UPL_TRANSFER)) ? cnt : (PAGE_SIZE * MAX_UPL_TRANSFER); /* effective max */
0b4e3aa0 827
91447636
A
828 request_flags = UPL_RET_ONLY_DIRTY | UPL_COPYOUT_FROM | UPL_CLEAN_IN_PLACE |
829 UPL_SET_INTERNAL | UPL_SET_LITE;
830
831 kr = memory_object_upl_request(vnode_object->control_handle,
832 offset, size, &upl, NULL, NULL, request_flags);
833 if (kr != KERN_SUCCESS)
834 panic("vnode_pager_cluster_write: upl request failed\n");
835
836 vnode_pageout(vnode_object->vnode_handle,
837 upl, (vm_offset_t)0, offset, size, upl_flags, &errno);
838
839 if ( (upl_flags & UPL_KEEPCACHED) ) {
840 if ( (*io_error = errno) )
841 break;
842 }
843 cnt -= size;
844 offset += size;
845 }
846 if (resid_offset)
847 *resid_offset = offset;
848
849 } else {
850 vm_object_offset_t vnode_size;
851 vm_object_offset_t base_offset;
852 vm_object_t object;
91447636
A
853
854 /*
855 * this is the pageout path
856 */
857 vnode_size = vnode_pager_get_filesize(vnode_object->vnode_handle);
858
859 if (vnode_size > (offset + PAGE_SIZE)) {
860 /*
861 * preset the maximum size of the cluster
862 * and put us on a nice cluster boundary...
863 * and then clip the size to insure we
864 * don't request past the end of the underlying file
865 */
866 size = PAGE_SIZE * MAX_UPL_TRANSFER;
867 base_offset = offset & ~((signed)(size - 1));
868
869 if ((base_offset + size) > vnode_size)
870 size = round_page_32(((vm_size_t)(vnode_size - base_offset)));
871 } else {
872 /*
873 * we've been requested to page out a page beyond the current
874 * end of the 'file'... don't try to cluster in this case...
875 * we still need to send this page through because it might
876 * be marked precious and the underlying filesystem may need
877 * to do something with it (besides page it out)...
878 */
879 base_offset = offset;
880 size = PAGE_SIZE;
0b4e3aa0 881 }
91447636
A
882 object = memory_object_control_to_vm_object(vnode_object->control_handle);
883
884 if (object == VM_OBJECT_NULL)
885 panic("vnode_pager_cluster_write: NULL vm_object in control handle\n");
886
887 request_flags = UPL_NOBLOCK | UPL_FOR_PAGEOUT | UPL_CLEAN_IN_PLACE |
888 UPL_RET_ONLY_DIRTY | UPL_COPYOUT_FROM |
889 UPL_SET_INTERNAL | UPL_SET_LITE;
890
91447636
A
891 vm_object_upl_request(object, base_offset, size,
892 &upl, NULL, NULL, request_flags);
893 if (upl == NULL)
894 panic("vnode_pager_cluster_write: upl request failed\n");
895
896 vnode_pageout(vnode_object->vnode_handle,
897 upl, (vm_offset_t)0, upl->offset, upl->size, UPL_VNODE_PAGER, NULL);
1c79356b 898 }
1c79356b
A
899}
900
901
902/*
903 *
904 */
905kern_return_t
906vnode_pager_cluster_read(
907 vnode_pager_t vnode_object,
908 vm_object_offset_t offset,
909 vm_size_t cnt)
910{
1c79356b
A
911 int local_error = 0;
912 int kret;
1c79356b 913
91447636 914 assert(! (cnt & PAGE_MASK));
1c79356b 915
91447636
A
916 kret = vnode_pagein(vnode_object->vnode_handle,
917 (upl_t) NULL,
918 (vm_offset_t) NULL,
919 offset,
920 cnt,
921 0,
922 &local_error);
0b4e3aa0
A
923/*
924 if(kret == PAGER_ABSENT) {
925 Need to work out the defs here, 1 corresponds to PAGER_ABSENT
926 defined in bsd/vm/vm_pager.h However, we should not be including
927 that file here it is a layering violation.
928*/
91447636
A
929 if (kret == 1) {
930 int uplflags;
931 upl_t upl = NULL;
0c530ab8 932 unsigned int count = 0;
91447636
A
933 kern_return_t kr;
934
935 uplflags = (UPL_NO_SYNC |
936 UPL_CLEAN_IN_PLACE |
937 UPL_SET_INTERNAL);
938 count = 0;
939 kr = memory_object_upl_request(vnode_object->control_handle,
940 offset, cnt,
941 &upl, NULL, &count, uplflags);
942 if (kr == KERN_SUCCESS) {
0b4e3aa0
A
943 upl_abort(upl, 0);
944 upl_deallocate(upl);
91447636
A
945 } else {
946 /*
947 * We couldn't gather the page list, probably
948 * because the memory object doesn't have a link
949 * to a VM object anymore (forced unmount, for
950 * example). Just return an error to the vm_fault()
951 * path and let it handle it.
952 */
953 }
0b4e3aa0 954
91447636 955 return KERN_FAILURE;
1c79356b 956 }
0b4e3aa0 957
91447636 958 return KERN_SUCCESS;
1c79356b
A
959
960}
961
962
963/*
964 *
965 */
966void
967vnode_pager_release_from_cache(
968 int *cnt)
969{
970 memory_object_free_from_cache(
0c530ab8 971 &realhost, &vnode_pager_ops, cnt);
1c79356b
A
972}
973
974/*
975 *
976 */
977vnode_pager_t
978vnode_object_create(
91447636 979 struct vnode *vp)
1c79356b
A
980{
981 register vnode_pager_t vnode_object;
982
983 vnode_object = (struct vnode_pager *) zalloc(vnode_pager_zone);
984 if (vnode_object == VNODE_PAGER_NULL)
985 return(VNODE_PAGER_NULL);
1c79356b 986
1c79356b 987 /*
0b4e3aa0
A
988 * The vm_map call takes both named entry ports and raw memory
989 * objects in the same parameter. We need to make sure that
990 * vm_map does not see this object as a named entry port. So,
991 * we reserve the second word in the object for a fake ip_kotype
992 * setting - that will tell vm_map to use it as a memory object.
1c79356b 993 */
0c530ab8 994 vnode_object->pager_ops = &vnode_pager_ops;
0b4e3aa0
A
995 vnode_object->pager_ikot = IKOT_MEMORY_OBJECT;
996 vnode_object->ref_count = 1;
997 vnode_object->control_handle = MEMORY_OBJECT_CONTROL_NULL;
998 vnode_object->vnode_handle = vp;
999
1000 return(vnode_object);
1c79356b
A
1001}
1002
1003/*
1004 *
1005 */
1006vnode_pager_t
0b4e3aa0
A
1007vnode_pager_lookup(
1008 memory_object_t name)
1c79356b 1009{
0b4e3aa0 1010 vnode_pager_t vnode_object;
1c79356b 1011
0b4e3aa0 1012 vnode_object = (vnode_pager_t)name;
0c530ab8 1013 assert(vnode_object->pager_ops == &vnode_pager_ops);
0b4e3aa0 1014 return (vnode_object);
1c79356b 1015}
0b4e3aa0 1016
0c530ab8
A
1017
1018/*********************** proc_info implementation *************/
1019
1020#include <sys/bsdtask_info.h>
1021
1022static int fill_vnodeinfoforaddr( vm_map_entry_t entry, uint32_t * vnodeaddr, uint32_t * vid);
1023
1024
1025int
1026fill_procregioninfo(task_t task, uint64_t arg, struct proc_regioninfo_internal *pinfo, uint32_t *vnodeaddr, uint32_t *vid)
1027{
1028
935ed37a 1029 vm_map_t map;
0c530ab8
A
1030 vm_map_offset_t address = (vm_map_offset_t )arg;
1031 vm_map_entry_t tmp_entry;
1032 vm_map_entry_t entry;
1033 vm_map_offset_t start;
1034 vm_region_extended_info_data_t extended;
1035 vm_region_top_info_data_t top;
1036
935ed37a
A
1037 task_lock(task);
1038 map = task->map;
1039 if (map == VM_MAP_NULL)
1040 {
1041 task_unlock(task);
1042 return(0);
1043 }
1044 vm_map_reference(map);
1045 task_unlock(task);
1046
0c530ab8
A
1047 vm_map_lock_read(map);
1048
1049 start = address;
1050 if (!vm_map_lookup_entry(map, start, &tmp_entry)) {
1051 if ((entry = tmp_entry->vme_next) == vm_map_to_entry(map)) {
1052 vm_map_unlock_read(map);
935ed37a 1053 vm_map_deallocate(map);
0c530ab8
A
1054 return(0);
1055 }
1056 } else {
1057 entry = tmp_entry;
1058 }
1059
1060 start = entry->vme_start;
1061
1062 pinfo->pri_offset = entry->offset;
1063 pinfo->pri_protection = entry->protection;
1064 pinfo->pri_max_protection = entry->max_protection;
1065 pinfo->pri_inheritance = entry->inheritance;
1066 pinfo->pri_behavior = entry->behavior;
1067 pinfo->pri_user_wired_count = entry->user_wired_count;
1068 pinfo->pri_user_tag = entry->alias;
1069
1070 if (entry->is_sub_map) {
1071 pinfo->pri_flags |= PROC_REGION_SUBMAP;
1072 } else {
1073 if (entry->is_shared)
1074 pinfo->pri_flags |= PROC_REGION_SHARED;
1075 }
1076
1077
1078 extended.protection = entry->protection;
1079 extended.user_tag = entry->alias;
1080 extended.pages_resident = 0;
1081 extended.pages_swapped_out = 0;
1082 extended.pages_shared_now_private = 0;
1083 extended.pages_dirtied = 0;
1084 extended.external_pager = 0;
1085 extended.shadow_depth = 0;
1086
1087 vm_map_region_walk(map, start, entry, entry->offset, entry->vme_end - start, &extended);
1088
1089 if (extended.external_pager && extended.ref_count == 2 && extended.share_mode == SM_SHARED)
1090 extended.share_mode = SM_PRIVATE;
1091
1092 top.private_pages_resident = 0;
1093 top.shared_pages_resident = 0;
1094 vm_map_region_top_walk(entry, &top);
1095
1096
1097 pinfo->pri_pages_resident = extended.pages_resident;
1098 pinfo->pri_pages_shared_now_private = extended.pages_shared_now_private;
1099 pinfo->pri_pages_swapped_out = extended.pages_swapped_out;
1100 pinfo->pri_pages_dirtied = extended.pages_dirtied;
1101 pinfo->pri_ref_count = extended.ref_count;
1102 pinfo->pri_shadow_depth = extended.shadow_depth;
1103 pinfo->pri_share_mode = extended.share_mode;
1104
1105 pinfo->pri_private_pages_resident = top.private_pages_resident;
1106 pinfo->pri_shared_pages_resident = top.shared_pages_resident;
1107 pinfo->pri_obj_id = top.obj_id;
1108
1109 pinfo->pri_address = (uint64_t)start;
1110 pinfo->pri_size = (uint64_t)(entry->vme_end - start);
1111 pinfo->pri_depth = 0;
1112
1113 if ((vnodeaddr != 0) && (entry->is_sub_map == 0)) {
1114 *vnodeaddr = (uint32_t)0;
1115
1116 if (fill_vnodeinfoforaddr(entry, vnodeaddr, vid) ==0) {
1117 vm_map_unlock_read(map);
935ed37a 1118 vm_map_deallocate(map);
0c530ab8
A
1119 return(1);
1120 }
1121 }
1122
1123 vm_map_unlock_read(map);
935ed37a 1124 vm_map_deallocate(map);
0c530ab8
A
1125 return(1);
1126}
1127
1128static int
1129fill_vnodeinfoforaddr(
1130 vm_map_entry_t entry,
1131 uint32_t * vnodeaddr,
1132 uint32_t * vid)
1133{
1134 vm_object_t top_object, object;
1135 memory_object_t memory_object;
1136 memory_object_pager_ops_t pager_ops;
1137 kern_return_t kr;
1138 int shadow_depth;
1139
1140
1141 if (entry->is_sub_map) {
1142 return(0);
1143 } else {
1144 /*
1145 * The last object in the shadow chain has the
1146 * relevant pager information.
1147 */
1148 top_object = entry->object.vm_object;
1149 if (top_object == VM_OBJECT_NULL) {
1150 object = VM_OBJECT_NULL;
1151 shadow_depth = 0;
1152 } else {
1153 vm_object_lock(top_object);
1154 for (object = top_object, shadow_depth = 0;
1155 object->shadow != VM_OBJECT_NULL;
1156 object = object->shadow, shadow_depth++) {
1157 vm_object_lock(object->shadow);
1158 vm_object_unlock(object);
1159 }
1160 }
1161 }
1162
1163 if (object == VM_OBJECT_NULL) {
1164 return(0);
1165 } else if (object->internal) {
1166 vm_object_unlock(object);
1167 return(0);
1168 } else if (! object->pager_ready ||
1169 object->terminating ||
1170 ! object->alive) {
1171 vm_object_unlock(object);
1172 return(0);
1173 } else {
1174 memory_object = object->pager;
1175 pager_ops = memory_object->mo_pager_ops;
1176 if (pager_ops == &vnode_pager_ops) {
1177 kr = vnode_pager_get_object_vnode(
1178 memory_object,
1179 vnodeaddr, vid);
1180 if (kr != KERN_SUCCESS) {
1181 vm_object_unlock(object);
1182 return(0);
1183 }
1184 } else {
1185 vm_object_unlock(object);
1186 return(0);
1187 }
1188 }
1189 vm_object_unlock(object);
1190 return(1);
1191}
1192
1193kern_return_t
1194vnode_pager_get_object_vnode (
1195 memory_object_t mem_obj,
1196 uint32_t * vnodeaddr,
1197 uint32_t * vid)
1198{
1199 vnode_pager_t vnode_object;
1200
1201 vnode_object = vnode_pager_lookup(mem_obj);
1202 if (vnode_object->vnode_handle) {
1203 *vnodeaddr = (uint32_t)vnode_object->vnode_handle;
1204 *vid = (uint32_t)vnode_vid((void *)vnode_object->vnode_handle);
1205
1206 return(KERN_SUCCESS);
1207 }
1208
1209 return(KERN_FAILURE);
1210}
1211