2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 #include <sys/errno.h>
27 #include <kern/host.h>
28 #include <mach/mach_types.h>
29 #include <vm/vm_map.h>
30 #include <vm/vm_kern.h>
31 #include <vm/vm_pageout.h>
32 #include <mach/kern_return.h>
33 #include <mach/memory_object_types.h>
34 #include <mach/port.h>
35 #include <mach/policy.h>
36 #include <ipc/ipc_port.h>
37 #include <ipc/ipc_space.h>
38 #include <kern/thread.h>
39 #include <vm/memory_object.h>
40 #include <vm/vm_pageout.h>
42 #include <default_pager/default_pager_types.h>
44 /* BSD VM COMPONENT INTERFACES */
64 return(map
->hdr
.nentries
);
74 return(vm_map_first_entry(map
)->vme_start
);
84 return(vm_map_last_entry(map
)->vme_end
);
91 /* until component support available */
92 int vnode_pager_workaround
;
94 typedef int vnode_port_t
;
96 typedef struct vnode_pager
{
97 int *pager
; /* pager workaround pointer */
98 unsigned int pager_ikot
; /* JMM: fake ip_kotype() */
99 unsigned int ref_count
; /* reference count */
100 memory_object_control_t control_handle
; /* mem object control handle */
101 vnode_port_t vnode_handle
; /* vnode handle */
106 trigger_name_to_port(
110 vnode_pager_bootstrap(
114 vnode_pager_alloc_map(
126 memory_object_control_t
,
130 vnode_pager_get_object_size(
132 memory_object_offset_t
*);
135 vnode_pager_data_request(
137 memory_object_offset_t
,
142 vnode_pager_data_return(
144 memory_object_offset_t
,
150 vnode_pager_data_initialize(
152 memory_object_offset_t
,
156 vnode_pager_deallocate(
160 vnode_pager_terminate(
164 vnode_pager_cluster_read(
170 vnode_pager_cluster_write(
196 vnode_pager_get_filesize(
208 vnode_pager_release_from_cache(
211 zone_t vnode_pager_zone
;
214 #define VNODE_PAGER_NULL ((vnode_pager_t) 0)
216 /* TODO: Should be set dynamically by vnode_pager_init() */
217 #define CLUSTER_SHIFT 1
219 /* TODO: Should be set dynamically by vnode_pager_bootstrap() */
220 #define MAX_VNODE 10000
226 #define PAGER_ALL 0xffffffff
227 #define PAGER_INIT 0x00000001
228 #define PAGER_PAGEIN 0x00000002
230 #define PAGER_DEBUG(LEVEL, A) {if ((pagerdebug & LEVEL)==LEVEL){printf A;}}
232 #define PAGER_DEBUG(LEVEL, A)
236 * Routine: macx_triggers
238 * Syscall interface to set the call backs for low and
246 mach_port_t trigger_name
)
249 memory_object_default_t default_pager
;
250 ipc_port_t trigger_port
;
252 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
253 kr
= host_default_memory_manager(host_priv_self(),
255 if(kr
!= KERN_SUCCESS
) {
258 if (flags
& HI_WAT_ALERT
) {
259 trigger_port
= trigger_name_to_port(trigger_name
);
260 if(trigger_port
== NULL
) {
263 /* trigger_port is locked and active */
264 ipc_port_make_send_locked(trigger_port
);
266 default_pager_triggers(default_pager
,
268 HI_WAT_ALERT
, trigger_port
);
271 if (flags
& LO_WAT_ALERT
) {
272 trigger_port
= trigger_name_to_port(trigger_name
);
273 if(trigger_port
== NULL
) {
276 /* trigger_port is locked and active */
277 ipc_port_make_send_locked(trigger_port
);
278 /* and now its unlocked */
279 default_pager_triggers(default_pager
,
281 LO_WAT_ALERT
, trigger_port
);
285 * Set thread scheduling priority and policy for the current thread
286 * it is assumed for the time being that the thread setting the alert
287 * is the same one which will be servicing it.
290 struct policy_timeshare_base fifo_base
;
291 struct policy_timeshare_limit fifo_limit
;
293 processor_set_t pset
;
294 policy_limit_t limit
;
296 pset
= (current_thread())->processor_set
;
297 base
= (policy_base_t
) &fifo_base
;
298 limit
= (policy_limit_t
) &fifo_limit
;
299 fifo_limit
.max_priority
= fifo_base
.base_priority
= MAXPRI_STANDARD
;
300 thread_set_policy((current_thread())->top_act
, pset
, POLICY_FIFO
, base
, POLICY_TIMESHARE_BASE_COUNT
, limit
, POLICY_TIMESHARE_LIMIT_COUNT
);
303 current_thread()->vm_privilege
= TRUE
;
310 trigger_name_to_port(
311 mach_port_t trigger_name
)
313 ipc_port_t trigger_port
;
316 if (trigger_name
== 0)
319 space
= current_space();
320 if(ipc_port_translate_receive(space
, (mach_port_name_t
)trigger_name
,
321 &trigger_port
) != KERN_SUCCESS
)
330 vnode_pager_bootstrap(void)
332 register vm_size_t size
;
334 size
= (vm_size_t
) sizeof(struct vnode_pager
);
335 vnode_pager_zone
= zinit(size
, (vm_size_t
) MAX_VNODE
*size
,
336 PAGE_SIZE
, "vnode pager structures");
346 memory_object_t pager
)
348 vnode_pager_t vnode_object
;
350 vnode_object
= vnode_object_create(vp
);
351 if (vnode_object
== VNODE_PAGER_NULL
)
352 panic("vnode_pager_setup: vnode_object_create() failed");
353 return((memory_object_t
)vnode_object
);
360 vnode_pager_init(memory_object_t mem_obj
,
361 memory_object_control_t control
,
364 vnode_pager_t vnode_object
;
366 memory_object_attr_info_data_t attributes
;
369 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_init: %x, %x, %x\n", pager
, pager_request
, pg_size
));
371 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
372 return KERN_INVALID_ARGUMENT
;
374 vnode_object
= vnode_pager_lookup(mem_obj
);
376 memory_object_control_reference(control
);
377 vnode_object
->control_handle
= control
;
379 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
380 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
381 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
382 attributes
.may_cache_object
= TRUE
;
383 attributes
.temporary
= TRUE
;
385 kr
= memory_object_change_attributes(
387 MEMORY_OBJECT_ATTRIBUTE_INFO
,
388 (memory_object_info_t
) &attributes
,
389 MEMORY_OBJECT_ATTR_INFO_COUNT
);
390 if (kr
!= KERN_SUCCESS
)
391 panic("vnode_pager_init: memory_object_change_attributes() failed");
393 return(KERN_SUCCESS
);
400 vnode_pager_data_return(
401 memory_object_t mem_obj
,
402 memory_object_offset_t offset
,
405 boolean_t kernel_copy
)
407 register vnode_pager_t vnode_object
;
409 vnode_object
= vnode_pager_lookup(mem_obj
);
411 vnode_pager_cluster_write(vnode_object
, offset
, data_cnt
);
417 vnode_pager_data_initialize(
418 memory_object_t mem_obj
,
419 memory_object_offset_t offset
,
426 vnode_pager_data_unlock(
427 memory_object_t mem_obj
,
428 memory_object_offset_t offset
,
430 vm_prot_t desired_access
)
436 vnode_pager_get_object_size(
437 memory_object_t mem_obj
,
438 memory_object_offset_t
*length
)
440 vnode_pager_t vnode_object
;
442 vnode_object
= vnode_pager_lookup(mem_obj
);
444 *length
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
452 vnode_pager_data_request(
453 memory_object_t mem_obj
,
454 memory_object_offset_t offset
,
456 vm_prot_t protection_required
)
458 register vnode_pager_t vnode_object
;
460 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_data_request: %x, %x, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
462 vnode_object
= vnode_pager_lookup(mem_obj
);
464 PAGER_DEBUG(PAGER_PAGEIN
, ("vnode_pager_data_request: %x, %x, %x, %x, vnode_object %x\n", mem_obj
, offset
, length
, protection_required
, vnode_object
));
466 vnode_pager_cluster_read(vnode_object
, offset
, length
);
475 vnode_pager_reference(
476 memory_object_t mem_obj
)
478 register vnode_pager_t vnode_object
;
479 unsigned int new_ref_count
;
481 vnode_object
= vnode_pager_lookup(mem_obj
);
482 new_ref_count
= hw_atomic_add(&vnode_object
->ref_count
, 1);
483 assert(new_ref_count
> 1);
490 vnode_pager_deallocate(
491 memory_object_t mem_obj
)
493 register vnode_pager_t vnode_object
;
495 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_deallocate: %x\n", mem_obj
));
497 vnode_object
= vnode_pager_lookup(mem_obj
);
499 if (hw_atomic_sub(&vnode_object
->ref_count
, 1) == 0) {
500 if (vnode_object
->vnode_handle
!= (vnode_port_t
) NULL
) {
501 vnode_pager_vrele(vnode_object
->vnode_handle
);
503 zfree(vnode_pager_zone
, (vm_offset_t
) vnode_object
);
512 vnode_pager_terminate(
513 memory_object_t mem_obj
)
515 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_terminate: %x\n", mem_obj
));
517 return(KERN_SUCCESS
);
524 vnode_pager_synchronize(
525 memory_object_t mem_obj
,
526 memory_object_offset_t offset
,
528 vm_sync_t sync_flags
)
530 register vnode_pager_t vnode_object
;
532 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_synchronize: %x\n", mem_obj
));
534 vnode_object
= vnode_pager_lookup(mem_obj
);
536 memory_object_synchronize_completed(vnode_object
->control_handle
, offset
, length
);
538 return (KERN_SUCCESS
);
546 memory_object_t mem_obj
)
548 register vnode_pager_t vnode_object
;
550 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_unmap: %x\n", mem_obj
));
552 vnode_object
= vnode_pager_lookup(mem_obj
);
554 ubc_unmap(vnode_object
->vnode_handle
);
563 vnode_pager_cluster_write(
564 vnode_pager_t vnode_object
,
565 vm_object_offset_t offset
,
573 if (cnt
& PAGE_MASK
) {
574 panic("vs_cluster_write: cnt not a multiple of PAGE_SIZE");
576 size
= (cnt
< (PAGE_SIZE
*32)) ? cnt
: (PAGE_SIZE
*32); /* effective min */
580 kret
= vnode_pageout(vnode_object
->vnode_handle
,
581 (upl_t
)NULL
, (vm_offset_t
)NULL
,
582 offset
, size
, 0, &local_error
);
584 if(kret == PAGER_ABSENT) {
585 Need to work out the defs here, 1 corresponds to
586 PAGER_ABSENT defined in bsd/vm/vm_pager.h However,
587 we should not be including that file here it is a
596 uplflags
= (UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
|
597 UPL_SET_INTERNAL
| UPL_COPYOUT_FROM
);
599 kr
= memory_object_upl_request(
600 vnode_object
->control_handle
,
601 offset
, size
, &upl
, NULL
, &count
, uplflags
);
602 if(kr
!= KERN_SUCCESS
) {
603 panic("vnode_pager_cluster_write: upl request failed\n");
612 if (local_error
!= 0) {
618 size
= (cnt
< (PAGE_SIZE
*32)) ? cnt
: (PAGE_SIZE
*32); /* effective min */
622 return(KERN_FAILURE
);
624 return(KERN_SUCCESS
);
633 vnode_pager_cluster_read(
634 vnode_pager_t vnode_object
,
635 vm_object_offset_t offset
,
642 if(cnt
& PAGE_MASK
) {
643 panic("vs_cluster_read: cnt not a multiple of PAGE_SIZE");
646 kret
= vnode_pagein(vnode_object
->vnode_handle
, (upl_t
)NULL
, (vm_offset_t
)NULL
, offset
, cnt
, 0, &local_error
);
648 if(kret == PAGER_ABSENT) {
649 Need to work out the defs here, 1 corresponds to PAGER_ABSENT
650 defined in bsd/vm/vm_pager.h However, we should not be including
651 that file here it is a layering violation.
659 uplflags
= (UPL_NO_SYNC
|
660 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
);
662 kr
= memory_object_upl_request(
663 vnode_object
->control_handle
, offset
, cnt
,
664 &upl
, NULL
, &count
, uplflags
);
665 if(kr
!= KERN_SUCCESS
) {
666 panic("vnode_pager_cluster_read: upl request failed\n");
675 return(KERN_FAILURE
);
677 return(KERN_SUCCESS
);
686 vnode_pager_release_from_cache(
689 memory_object_free_from_cache(
690 &realhost
, &vnode_pager_workaround
, cnt
);
700 register vnode_pager_t vnode_object
;
702 vnode_object
= (struct vnode_pager
*) zalloc(vnode_pager_zone
);
703 if (vnode_object
== VNODE_PAGER_NULL
)
704 return(VNODE_PAGER_NULL
);
707 * The vm_map call takes both named entry ports and raw memory
708 * objects in the same parameter. We need to make sure that
709 * vm_map does not see this object as a named entry port. So,
710 * we reserve the second word in the object for a fake ip_kotype
711 * setting - that will tell vm_map to use it as a memory object.
713 vnode_object
->pager
= &vnode_pager_workaround
;
714 vnode_object
->pager_ikot
= IKOT_MEMORY_OBJECT
;
715 vnode_object
->ref_count
= 1;
716 vnode_object
->control_handle
= MEMORY_OBJECT_CONTROL_NULL
;
717 vnode_object
->vnode_handle
= vp
;
719 return(vnode_object
);
727 memory_object_t name
)
729 vnode_pager_t vnode_object
;
731 vnode_object
= (vnode_pager_t
)name
;
732 assert(vnode_object
->pager
== &vnode_pager_workaround
);
733 return (vnode_object
);