2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/errno.h>
24 #include <kern/host.h>
25 #include <mach/mach_types.h>
26 #include <vm/vm_map.h>
27 #include <vm/vm_kern.h>
28 #include <vm/vm_pageout.h>
29 #include <mach/kern_return.h>
30 #include <mach/memory_object_types.h>
31 #include <mach/port.h>
32 #include <mach/policy.h>
33 #include <ipc/ipc_port.h>
34 #include <ipc/ipc_space.h>
35 #include <kern/thread.h>
36 #include <vm/memory_object.h>
37 #include <vm/vm_pageout.h>
39 #include <libkern/OSAtomic.h>
41 #include <default_pager/default_pager_types.h>
43 /* BSD VM COMPONENT INTERFACES */
63 return(map
->hdr
.nentries
);
73 return(vm_map_first_entry(map
)->vme_start
);
83 return(vm_map_last_entry(map
)->vme_end
);
90 /* until component support available */
91 int vnode_pager_workaround
;
93 typedef int vnode_port_t
;
95 typedef struct vnode_pager
{
96 int *pager
; /* pager workaround pointer */
97 unsigned int pager_ikot
; /* JMM: fake ip_kotype() */
98 unsigned int ref_count
; /* reference count */
99 memory_object_control_t control_handle
; /* mem object control handle */
100 vnode_port_t vnode_handle
; /* vnode handle */
105 trigger_name_to_port(
109 vnode_pager_bootstrap(
113 vnode_pager_alloc_map(
125 memory_object_control_t
,
129 vnode_pager_get_object_size(
131 memory_object_offset_t
*);
134 vnode_pager_data_request(
136 memory_object_offset_t
,
141 vnode_pager_data_return(
143 memory_object_offset_t
,
149 vnode_pager_data_initialize(
151 memory_object_offset_t
,
155 vnode_pager_deallocate(
159 vnode_pager_terminate(
163 vnode_pager_cluster_read(
169 vnode_pager_cluster_write(
195 vnode_pager_get_filesize(
207 vnode_pager_release_from_cache(
210 zone_t vnode_pager_zone
;
213 #define VNODE_PAGER_NULL ((vnode_pager_t) 0)
215 /* TODO: Should be set dynamically by vnode_pager_init() */
216 #define CLUSTER_SHIFT 1
218 /* TODO: Should be set dynamically by vnode_pager_bootstrap() */
219 #define MAX_VNODE 10000
225 #define PAGER_ALL 0xffffffff
226 #define PAGER_INIT 0x00000001
227 #define PAGER_PAGEIN 0x00000002
229 #define PAGER_DEBUG(LEVEL, A) {if ((pagerdebug & LEVEL)==LEVEL){printf A;}}
231 #define PAGER_DEBUG(LEVEL, A)
235 * Routine: macx_triggers
237 * Syscall interface to set the call backs for low and
245 mach_port_t trigger_name
)
248 memory_object_default_t default_pager
;
249 ipc_port_t trigger_port
;
251 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
252 kr
= host_default_memory_manager(host_priv_self(),
254 if(kr
!= KERN_SUCCESS
) {
257 if (flags
& HI_WAT_ALERT
) {
258 trigger_port
= trigger_name_to_port(trigger_name
);
259 if(trigger_port
== NULL
) {
262 /* trigger_port is locked and active */
263 ipc_port_make_send_locked(trigger_port
);
265 default_pager_triggers(default_pager
,
267 HI_WAT_ALERT
, trigger_port
);
270 if (flags
& LO_WAT_ALERT
) {
271 trigger_port
= trigger_name_to_port(trigger_name
);
272 if(trigger_port
== NULL
) {
275 /* trigger_port is locked and active */
276 ipc_port_make_send_locked(trigger_port
);
277 /* and now its unlocked */
278 default_pager_triggers(default_pager
,
280 LO_WAT_ALERT
, trigger_port
);
284 * Set thread scheduling priority and policy for the current thread
285 * it is assumed for the time being that the thread setting the alert
286 * is the same one which will be servicing it.
289 struct policy_timeshare_base fifo_base
;
290 struct policy_timeshare_limit fifo_limit
;
292 processor_set_t pset
;
293 policy_limit_t limit
;
295 pset
= (current_thread())->processor_set
;
296 base
= (policy_base_t
) &fifo_base
;
297 limit
= (policy_limit_t
) &fifo_limit
;
298 fifo_limit
.max_priority
= fifo_base
.base_priority
= MAXPRI_STANDARD
;
299 thread_set_policy((current_thread())->top_act
, pset
, POLICY_FIFO
, base
, POLICY_TIMESHARE_BASE_COUNT
, limit
, POLICY_TIMESHARE_LIMIT_COUNT
);
302 current_thread()->vm_privilege
= TRUE
;
309 trigger_name_to_port(
310 mach_port_t trigger_name
)
312 ipc_port_t trigger_port
;
315 if (trigger_name
== 0)
318 space
= current_space();
319 if(ipc_port_translate_receive(space
, (mach_port_name_t
)trigger_name
,
320 &trigger_port
) != KERN_SUCCESS
)
329 vnode_pager_bootstrap(void)
331 register vm_size_t size
;
333 size
= (vm_size_t
) sizeof(struct vnode_pager
);
334 vnode_pager_zone
= zinit(size
, (vm_size_t
) MAX_VNODE
*size
,
335 PAGE_SIZE
, "vnode pager structures");
345 memory_object_t pager
)
347 vnode_pager_t vnode_object
;
349 vnode_object
= vnode_object_create(vp
);
350 if (vnode_object
== VNODE_PAGER_NULL
)
351 panic("vnode_pager_setup: vnode_object_create() failed");
352 return((memory_object_t
)vnode_object
);
359 vnode_pager_init(memory_object_t mem_obj
,
360 memory_object_control_t control
,
363 vnode_pager_t vnode_object
;
365 memory_object_attr_info_data_t attributes
;
368 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_init: %x, %x, %x\n", pager
, pager_request
, pg_size
));
370 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
371 return KERN_INVALID_ARGUMENT
;
373 vnode_object
= vnode_pager_lookup(mem_obj
);
375 memory_object_control_reference(control
);
376 vnode_object
->control_handle
= control
;
378 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
379 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
380 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
381 attributes
.may_cache_object
= TRUE
;
382 attributes
.temporary
= TRUE
;
384 kr
= memory_object_change_attributes(
386 MEMORY_OBJECT_ATTRIBUTE_INFO
,
387 (memory_object_info_t
) &attributes
,
388 MEMORY_OBJECT_ATTR_INFO_COUNT
);
389 if (kr
!= KERN_SUCCESS
)
390 panic("vnode_pager_init: memory_object_change_attributes() failed");
392 return(KERN_SUCCESS
);
399 vnode_pager_data_return(
400 memory_object_t mem_obj
,
401 memory_object_offset_t offset
,
404 boolean_t kernel_copy
)
406 register vnode_pager_t vnode_object
;
408 vnode_object
= vnode_pager_lookup(mem_obj
);
410 vnode_pager_cluster_write(vnode_object
, offset
, data_cnt
);
416 vnode_pager_data_initialize(
417 memory_object_t mem_obj
,
418 memory_object_offset_t offset
,
425 vnode_pager_data_unlock(
426 memory_object_t mem_obj
,
427 memory_object_offset_t offset
,
429 vm_prot_t desired_access
)
435 vnode_pager_get_object_size(
436 memory_object_t mem_obj
,
437 memory_object_offset_t
*length
)
439 vnode_pager_t vnode_object
;
441 vnode_object
= vnode_pager_lookup(mem_obj
);
443 *length
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
451 vnode_pager_data_request(
452 memory_object_t mem_obj
,
453 memory_object_offset_t offset
,
455 vm_prot_t protection_required
)
457 register vnode_pager_t vnode_object
;
459 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_data_request: %x, %x, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
461 vnode_object
= vnode_pager_lookup(mem_obj
);
463 PAGER_DEBUG(PAGER_PAGEIN
, ("vnode_pager_data_request: %x, %x, %x, %x, vnode_object %x\n", mem_obj
, offset
, length
, protection_required
, vnode_object
));
465 vnode_pager_cluster_read(vnode_object
, offset
, length
);
474 vnode_pager_reference(
475 memory_object_t mem_obj
)
477 register vnode_pager_t vnode_object
;
478 unsigned int prev_ref_count
;
480 vnode_object
= vnode_pager_lookup(mem_obj
);
481 prev_ref_count
= OSIncrementAtomic((UInt32
*)&vnode_object
->ref_count
);
482 assert(prev_ref_count
> 0);
489 vnode_pager_deallocate(
490 memory_object_t mem_obj
)
492 register vnode_pager_t vnode_object
;
494 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_deallocate: %x\n", mem_obj
));
496 vnode_object
= vnode_pager_lookup(mem_obj
);
498 if (OSDecrementAtomic((UInt32
*)&vnode_object
->ref_count
) == 1) {
499 if (vnode_object
->vnode_handle
!= (vnode_port_t
) NULL
) {
500 vnode_pager_vrele(vnode_object
->vnode_handle
);
502 zfree(vnode_pager_zone
, (vm_offset_t
) vnode_object
);
511 vnode_pager_terminate(
512 memory_object_t mem_obj
)
514 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_terminate: %x\n", mem_obj
));
516 return(KERN_SUCCESS
);
523 vnode_pager_synchronize(
524 memory_object_t mem_obj
,
525 memory_object_offset_t offset
,
527 vm_sync_t sync_flags
)
529 register vnode_pager_t vnode_object
;
531 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_synchronize: %x\n", mem_obj
));
533 vnode_object
= vnode_pager_lookup(mem_obj
);
535 memory_object_synchronize_completed(vnode_object
->control_handle
, offset
, length
);
537 return (KERN_SUCCESS
);
545 memory_object_t mem_obj
)
547 register vnode_pager_t vnode_object
;
549 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_unmap: %x\n", mem_obj
));
551 vnode_object
= vnode_pager_lookup(mem_obj
);
553 ubc_unmap(vnode_object
->vnode_handle
);
562 vnode_pager_cluster_write(
563 vnode_pager_t vnode_object
,
564 vm_object_offset_t offset
,
572 if (cnt
& PAGE_MASK
) {
573 panic("vs_cluster_write: cnt not a multiple of PAGE_SIZE");
575 size
= (cnt
< (PAGE_SIZE
*32)) ? cnt
: (PAGE_SIZE
*32); /* effective min */
579 kret
= vnode_pageout(vnode_object
->vnode_handle
,
580 (upl_t
)NULL
, (vm_offset_t
)NULL
,
581 offset
, size
, 0, &local_error
);
583 if(kret == PAGER_ABSENT) {
584 Need to work out the defs here, 1 corresponds to
585 PAGER_ABSENT defined in bsd/vm/vm_pager.h However,
586 we should not be including that file here it is a
595 uplflags
= (UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
|
596 UPL_SET_INTERNAL
| UPL_COPYOUT_FROM
);
598 kr
= memory_object_upl_request(
599 vnode_object
->control_handle
,
600 offset
, size
, &upl
, NULL
, &count
, uplflags
);
601 if(kr
!= KERN_SUCCESS
) {
602 panic("vnode_pager_cluster_write: upl request failed\n");
611 if (local_error
!= 0) {
617 size
= (cnt
< (PAGE_SIZE
*32)) ? cnt
: (PAGE_SIZE
*32); /* effective min */
621 return(KERN_FAILURE
);
623 return(KERN_SUCCESS
);
632 vnode_pager_cluster_read(
633 vnode_pager_t vnode_object
,
634 vm_object_offset_t offset
,
641 if(cnt
& PAGE_MASK
) {
642 panic("vs_cluster_read: cnt not a multiple of PAGE_SIZE");
645 kret
= vnode_pagein(vnode_object
->vnode_handle
, (upl_t
)NULL
, (vm_offset_t
)NULL
, offset
, cnt
, 2, &local_error
);
647 if(kret == PAGER_ABSENT) {
648 Need to work out the defs here, 1 corresponds to PAGER_ABSENT
649 defined in bsd/vm/vm_pager.h However, we should not be including
650 that file here it is a layering violation.
658 uplflags
= (UPL_NO_SYNC
|
659 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
);
661 kr
= memory_object_upl_request(
662 vnode_object
->control_handle
, offset
, cnt
,
663 &upl
, NULL
, &count
, uplflags
);
664 if(kr
!= KERN_SUCCESS
) {
665 panic("vnode_pager_cluster_read: upl request failed\n");
674 return(KERN_FAILURE
);
676 return(KERN_SUCCESS
);
685 vnode_pager_release_from_cache(
688 memory_object_free_from_cache(
689 &realhost
, &vnode_pager_workaround
, cnt
);
699 register vnode_pager_t vnode_object
;
701 vnode_object
= (struct vnode_pager
*) zalloc(vnode_pager_zone
);
702 if (vnode_object
== VNODE_PAGER_NULL
)
703 return(VNODE_PAGER_NULL
);
706 * The vm_map call takes both named entry ports and raw memory
707 * objects in the same parameter. We need to make sure that
708 * vm_map does not see this object as a named entry port. So,
709 * we reserve the second word in the object for a fake ip_kotype
710 * setting - that will tell vm_map to use it as a memory object.
712 vnode_object
->pager
= &vnode_pager_workaround
;
713 vnode_object
->pager_ikot
= IKOT_MEMORY_OBJECT
;
714 vnode_object
->ref_count
= 1;
715 vnode_object
->control_handle
= MEMORY_OBJECT_CONTROL_NULL
;
716 vnode_object
->vnode_handle
= vp
;
718 return(vnode_object
);
726 memory_object_t name
)
728 vnode_pager_t vnode_object
;
730 vnode_object
= (vnode_pager_t
)name
;
731 assert(vnode_object
->pager
== &vnode_pager_workaround
);
732 return (vnode_object
);