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 <default_pager/default_pager_types.h>
41 /* BSD VM COMPONENT INTERFACES */
61 return(map
->hdr
.nentries
);
71 return(vm_map_first_entry(map
)->vme_start
);
81 return(vm_map_last_entry(map
)->vme_end
);
88 /* until component support available */
89 int vnode_pager_workaround
;
91 typedef int vnode_port_t
;
93 typedef struct vnode_pager
{
94 int *pager
; /* pager workaround pointer */
95 unsigned int pager_ikot
; /* JMM: fake ip_kotype() */
96 unsigned int ref_count
; /* reference count */
97 memory_object_control_t control_handle
; /* mem object control handle */
98 vnode_port_t vnode_handle
; /* vnode handle */
103 trigger_name_to_port(
107 vnode_pager_bootstrap(
111 vnode_pager_alloc_map(
123 memory_object_control_t
,
127 vnode_pager_get_object_size(
129 memory_object_offset_t
*);
132 vnode_pager_data_request(
134 memory_object_offset_t
,
139 vnode_pager_data_return(
141 memory_object_offset_t
,
147 vnode_pager_data_initialize(
149 memory_object_offset_t
,
153 vnode_pager_deallocate(
157 vnode_pager_terminate(
161 vnode_pager_cluster_read(
167 vnode_pager_cluster_write(
193 vnode_pager_get_filesize(
205 vnode_pager_release_from_cache(
208 zone_t vnode_pager_zone
;
211 #define VNODE_PAGER_NULL ((vnode_pager_t) 0)
213 /* TODO: Should be set dynamically by vnode_pager_init() */
214 #define CLUSTER_SHIFT 1
216 /* TODO: Should be set dynamically by vnode_pager_bootstrap() */
217 #define MAX_VNODE 10000
223 #define PAGER_ALL 0xffffffff
224 #define PAGER_INIT 0x00000001
225 #define PAGER_PAGEIN 0x00000002
227 #define PAGER_DEBUG(LEVEL, A) {if ((pagerdebug & LEVEL)==LEVEL){printf A;}}
229 #define PAGER_DEBUG(LEVEL, A)
233 * Routine: macx_triggers
235 * Syscall interface to set the call backs for low and
243 mach_port_t trigger_name
)
246 memory_object_default_t default_pager
;
247 ipc_port_t trigger_port
;
249 default_pager
= MEMORY_OBJECT_DEFAULT_NULL
;
250 kr
= host_default_memory_manager(host_priv_self(),
252 if(kr
!= KERN_SUCCESS
) {
255 if (flags
& HI_WAT_ALERT
) {
256 trigger_port
= trigger_name_to_port(trigger_name
);
257 if(trigger_port
== NULL
) {
260 /* trigger_port is locked and active */
261 ipc_port_make_send_locked(trigger_port
);
263 default_pager_triggers(default_pager
,
265 HI_WAT_ALERT
, trigger_port
);
268 if (flags
& LO_WAT_ALERT
) {
269 trigger_port
= trigger_name_to_port(trigger_name
);
270 if(trigger_port
== NULL
) {
273 /* trigger_port is locked and active */
274 ipc_port_make_send_locked(trigger_port
);
275 /* and now its unlocked */
276 default_pager_triggers(default_pager
,
278 LO_WAT_ALERT
, trigger_port
);
282 * Set thread scheduling priority and policy for the current thread
283 * it is assumed for the time being that the thread setting the alert
284 * is the same one which will be servicing it.
287 struct policy_timeshare_base fifo_base
;
288 struct policy_timeshare_limit fifo_limit
;
290 processor_set_t pset
;
291 policy_limit_t limit
;
293 pset
= (current_thread())->processor_set
;
294 base
= (policy_base_t
) &fifo_base
;
295 limit
= (policy_limit_t
) &fifo_limit
;
296 fifo_limit
.max_priority
= fifo_base
.base_priority
= MAXPRI_STANDARD
;
297 thread_set_policy((current_thread())->top_act
, pset
, POLICY_FIFO
, base
, POLICY_TIMESHARE_BASE_COUNT
, limit
, POLICY_TIMESHARE_LIMIT_COUNT
);
300 current_thread()->vm_privilege
= TRUE
;
307 trigger_name_to_port(
308 mach_port_t trigger_name
)
310 ipc_port_t trigger_port
;
313 if (trigger_name
== 0)
316 space
= current_space();
317 if(ipc_port_translate_receive(space
, (mach_port_name_t
)trigger_name
,
318 &trigger_port
) != KERN_SUCCESS
)
327 vnode_pager_bootstrap(void)
329 register vm_size_t size
;
331 size
= (vm_size_t
) sizeof(struct vnode_pager
);
332 vnode_pager_zone
= zinit(size
, (vm_size_t
) MAX_VNODE
*size
,
333 PAGE_SIZE
, "vnode pager structures");
343 memory_object_t pager
)
345 vnode_pager_t vnode_object
;
347 vnode_object
= vnode_object_create(vp
);
348 if (vnode_object
== VNODE_PAGER_NULL
)
349 panic("vnode_pager_setup: vnode_object_create() failed");
350 return((memory_object_t
)vnode_object
);
357 vnode_pager_init(memory_object_t mem_obj
,
358 memory_object_control_t control
,
361 vnode_pager_t vnode_object
;
363 memory_object_attr_info_data_t attributes
;
366 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_init: %x, %x, %x\n", pager
, pager_request
, pg_size
));
368 if (control
== MEMORY_OBJECT_CONTROL_NULL
)
369 return KERN_INVALID_ARGUMENT
;
371 vnode_object
= vnode_pager_lookup(mem_obj
);
373 memory_object_control_reference(control
);
374 vnode_object
->control_handle
= control
;
376 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
377 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
378 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
379 attributes
.may_cache_object
= TRUE
;
380 attributes
.temporary
= TRUE
;
382 kr
= memory_object_change_attributes(
384 MEMORY_OBJECT_ATTRIBUTE_INFO
,
385 (memory_object_info_t
) &attributes
,
386 MEMORY_OBJECT_ATTR_INFO_COUNT
);
387 if (kr
!= KERN_SUCCESS
)
388 panic("vnode_pager_init: memory_object_change_attributes() failed");
390 return(KERN_SUCCESS
);
397 vnode_pager_data_return(
398 memory_object_t mem_obj
,
399 memory_object_offset_t offset
,
402 boolean_t kernel_copy
)
404 register vnode_pager_t vnode_object
;
406 vnode_object
= vnode_pager_lookup(mem_obj
);
408 vnode_pager_cluster_write(vnode_object
, offset
, data_cnt
);
414 vnode_pager_data_initialize(
415 memory_object_t mem_obj
,
416 memory_object_offset_t offset
,
423 vnode_pager_data_unlock(
424 memory_object_t mem_obj
,
425 memory_object_offset_t offset
,
427 vm_prot_t desired_access
)
433 vnode_pager_get_object_size(
434 memory_object_t mem_obj
,
435 memory_object_offset_t
*length
)
437 vnode_pager_t vnode_object
;
439 vnode_object
= vnode_pager_lookup(mem_obj
);
441 *length
= vnode_pager_get_filesize(vnode_object
->vnode_handle
);
449 vnode_pager_data_request(
450 memory_object_t mem_obj
,
451 memory_object_offset_t offset
,
453 vm_prot_t protection_required
)
455 register vnode_pager_t vnode_object
;
457 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_data_request: %x, %x, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
459 vnode_object
= vnode_pager_lookup(mem_obj
);
461 PAGER_DEBUG(PAGER_PAGEIN
, ("vnode_pager_data_request: %x, %x, %x, %x, vnode_object %x\n", mem_obj
, offset
, length
, protection_required
, vnode_object
));
463 vnode_pager_cluster_read(vnode_object
, offset
, length
);
472 vnode_pager_reference(
473 memory_object_t mem_obj
)
475 register vnode_pager_t vnode_object
;
476 unsigned int new_ref_count
;
478 vnode_object
= vnode_pager_lookup(mem_obj
);
479 new_ref_count
= hw_atomic_add(&vnode_object
->ref_count
, 1);
480 assert(new_ref_count
> 1);
487 vnode_pager_deallocate(
488 memory_object_t mem_obj
)
490 register vnode_pager_t vnode_object
;
492 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_deallocate: %x\n", mem_obj
));
494 vnode_object
= vnode_pager_lookup(mem_obj
);
496 if (hw_atomic_sub(&vnode_object
->ref_count
, 1) == 0) {
497 if (vnode_object
->vnode_handle
!= (vnode_port_t
) NULL
) {
498 vnode_pager_vrele(vnode_object
->vnode_handle
);
500 zfree(vnode_pager_zone
, (vm_offset_t
) vnode_object
);
509 vnode_pager_terminate(
510 memory_object_t mem_obj
)
512 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_terminate: %x\n", mem_obj
));
514 return(KERN_SUCCESS
);
521 vnode_pager_synchronize(
522 memory_object_t mem_obj
,
523 memory_object_offset_t offset
,
525 vm_sync_t sync_flags
)
527 register vnode_pager_t vnode_object
;
529 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_synchronize: %x\n", mem_obj
));
531 vnode_object
= vnode_pager_lookup(mem_obj
);
533 memory_object_synchronize_completed(vnode_object
->control_handle
, offset
, length
);
535 return (KERN_SUCCESS
);
543 memory_object_t mem_obj
)
545 register vnode_pager_t vnode_object
;
547 PAGER_DEBUG(PAGER_ALL
, ("vnode_pager_unmap: %x\n", mem_obj
));
549 vnode_object
= vnode_pager_lookup(mem_obj
);
551 ubc_unmap(vnode_object
->vnode_handle
);
560 vnode_pager_cluster_write(
561 vnode_pager_t vnode_object
,
562 vm_object_offset_t offset
,
570 if (cnt
& PAGE_MASK
) {
571 panic("vs_cluster_write: cnt not a multiple of PAGE_SIZE");
573 size
= (cnt
< (PAGE_SIZE
*32)) ? cnt
: (PAGE_SIZE
*32); /* effective min */
577 kret
= vnode_pageout(vnode_object
->vnode_handle
,
578 (upl_t
)NULL
, (vm_offset_t
)NULL
,
579 offset
, size
, 0, &local_error
);
581 if(kret == PAGER_ABSENT) {
582 Need to work out the defs here, 1 corresponds to
583 PAGER_ABSENT defined in bsd/vm/vm_pager.h However,
584 we should not be including that file here it is a
593 uplflags
= (UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
|
594 UPL_SET_INTERNAL
| UPL_COPYOUT_FROM
);
596 kr
= memory_object_upl_request(
597 vnode_object
->control_handle
,
598 offset
, size
, &upl
, NULL
, &count
, uplflags
);
599 if(kr
!= KERN_SUCCESS
) {
600 panic("vnode_pager_cluster_write: upl request failed\n");
609 if (local_error
!= 0) {
615 size
= (cnt
< (PAGE_SIZE
*32)) ? cnt
: (PAGE_SIZE
*32); /* effective min */
619 return(KERN_FAILURE
);
621 return(KERN_SUCCESS
);
630 vnode_pager_cluster_read(
631 vnode_pager_t vnode_object
,
632 vm_object_offset_t offset
,
639 if(cnt
& PAGE_MASK
) {
640 panic("vs_cluster_read: cnt not a multiple of PAGE_SIZE");
643 kret
= vnode_pagein(vnode_object
->vnode_handle
, (upl_t
)NULL
, (vm_offset_t
)NULL
, offset
, cnt
, 0, &local_error
);
645 if(kret == PAGER_ABSENT) {
646 Need to work out the defs here, 1 corresponds to PAGER_ABSENT
647 defined in bsd/vm/vm_pager.h However, we should not be including
648 that file here it is a layering violation.
656 uplflags
= (UPL_NO_SYNC
|
657 UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
);
659 kr
= memory_object_upl_request(
660 vnode_object
->control_handle
, offset
, cnt
,
661 &upl
, NULL
, &count
, uplflags
);
662 if(kr
!= KERN_SUCCESS
) {
663 panic("vnode_pager_cluster_read: upl request failed\n");
672 return(KERN_FAILURE
);
674 return(KERN_SUCCESS
);
683 vnode_pager_release_from_cache(
686 memory_object_free_from_cache(
687 &realhost
, &vnode_pager_workaround
, cnt
);
697 register vnode_pager_t vnode_object
;
699 vnode_object
= (struct vnode_pager
*) zalloc(vnode_pager_zone
);
700 if (vnode_object
== VNODE_PAGER_NULL
)
701 return(VNODE_PAGER_NULL
);
704 * The vm_map call takes both named entry ports and raw memory
705 * objects in the same parameter. We need to make sure that
706 * vm_map does not see this object as a named entry port. So,
707 * we reserve the second word in the object for a fake ip_kotype
708 * setting - that will tell vm_map to use it as a memory object.
710 vnode_object
->pager
= &vnode_pager_workaround
;
711 vnode_object
->pager_ikot
= IKOT_MEMORY_OBJECT
;
712 vnode_object
->ref_count
= 1;
713 vnode_object
->control_handle
= MEMORY_OBJECT_CONTROL_NULL
;
714 vnode_object
->vnode_handle
= vp
;
716 return(vnode_object
);
724 memory_object_t name
)
726 vnode_pager_t vnode_object
;
728 vnode_object
= (vnode_pager_t
)name
;
729 assert(vnode_object
->pager
== &vnode_pager_workaround
);
730 return (vnode_object
);