#ifndef _VM_VM_MAP_H_
#define _VM_VM_MAP_H_
#include <mach/mach_types.h>
#include <mach/kern_return.h>
#include <mach/boolean.h>
#include <mach/vm_types.h>
#include <mach/vm_prot.h>
#include <mach/vm_inherit.h>
#include <mach/vm_behavior.h>
#include <mach/vm_param.h>
#include <vm/pmap.h>
#ifdef KERNEL_PRIVATE
#include <sys/cdefs.h>
__BEGIN_DECLS
extern void vm_map_reference(vm_map_t map);
extern vm_map_t current_map(void);
extern kern_return_t vm_map_exec(
vm_map_t new_map,
task_t task,
void *fsroot,
cpu_type_t cpu);
__END_DECLS
#ifdef MACH_KERNEL_PRIVATE
#include <task_swapper.h>
#include <mach_assert.h>
#include <vm/vm_object.h>
#include <vm/vm_page.h>
#include <kern/lock.h>
#include <kern/zalloc.h>
#include <kern/macro_help.h>
#include <kern/thread.h>
#define current_map_fast() (current_thread()->map)
#define current_map() (current_map_fast())
#include <vm/vm_map_store.h>
typedef struct vm_map_entry *vm_map_entry_t;
#define VM_MAP_ENTRY_NULL ((vm_map_entry_t) 0)
typedef union vm_map_object {
vm_object_t vm_object;
vm_map_t sub_map;
} vm_map_object_t;
#define named_entry_lock_init(object) lck_mtx_init(&(object)->Lock, &vm_object_lck_grp, &vm_object_lck_attr)
#define named_entry_lock(object) lck_mtx_lock(&(object)->Lock)
#define named_entry_unlock(object) lck_mtx_unlock(&(object)->Lock)
struct vm_named_entry {
decl_lck_mtx_data(, Lock)
union {
vm_object_t object;
memory_object_t pager;
vm_map_t map;
} backing;
vm_object_offset_t offset;
vm_object_size_t size;
vm_prot_t protection;
int ref_count;
unsigned int
internal:1,
is_sub_map:1,
is_pager:1;
};
struct vm_map_links {
struct vm_map_entry *prev;
struct vm_map_entry *next;
vm_map_offset_t start;
vm_map_offset_t end;
};
struct vm_map_entry {
struct vm_map_links links;
#define vme_prev links.prev
#define vme_next links.next
#define vme_start links.start
#define vme_end links.end
struct vm_map_store store;
union vm_map_object object;
vm_object_offset_t offset;
unsigned int
is_shared:1,
is_sub_map:1,
in_transition:1,
needs_wakeup:1,
behavior:2,
needs_copy:1,
protection:3,
max_protection:3,
inheritance:2,
use_pmap:1,
alias:8,
no_cache:1,
permanent:1,
superpage_size:3,
zero_wired_pages:1,
used_for_jit:1,
pad:1;
unsigned short wired_count;
unsigned short user_wired_count;
};
#define SUPERPAGE_SIZE (PAGE_SIZE*SUPERPAGE_NBASEPAGES)
#define SUPERPAGE_MASK (-SUPERPAGE_SIZE)
#define SUPERPAGE_ROUND_DOWN(a) (a & SUPERPAGE_MASK)
#define SUPERPAGE_ROUND_UP(a) ((a + SUPERPAGE_SIZE-1) & SUPERPAGE_MASK)
#define MAX_WIRE_COUNT 65535
struct vm_map_header {
struct vm_map_links links;
int nentries;
boolean_t entries_pageable;
vm_map_offset_t highest_entry_end_addr;
#ifdef VM_MAP_STORE_USE_RB
struct rb_head rb_head_store;
#endif
};
struct _vm_map {
lock_t lock;
struct vm_map_header hdr;
#define min_offset hdr.links.start
#define max_offset hdr.links.end
#define highest_entry_end hdr.highest_entry_end_addr
pmap_t pmap;
vm_map_size_t size;
vm_map_size_t user_wire_limit;
vm_map_size_t user_wire_size;
int ref_count;
#if TASK_SWAPPER
int res_count;
int sw_state;
#endif
decl_lck_mtx_data(, s_lock)
lck_mtx_ext_t s_lock_ext;
vm_map_entry_t hint;
vm_map_entry_t first_free;
unsigned int
wait_for_space:1,
wiring_required:1,
no_zero_fill:1,
mapped:1,
switch_protect:1,
disable_vmentry_reuse:1,
map_disallow_data_exec:1,
pad:25;
unsigned int timestamp;
unsigned int color_rr;
#if CONFIG_FREEZE
void *default_freezer_toc;
#endif
boolean_t jit_entry_exists;
} ;
#define vm_map_to_entry(map) ((struct vm_map_entry *) &(map)->hdr.links)
#define vm_map_first_entry(map) ((map)->hdr.links.next)
#define vm_map_last_entry(map) ((map)->hdr.links.prev)
#if TASK_SWAPPER
#define MAP_SW_IN 1
#define MAP_SW_OUT 2
#endif
typedef struct vm_map_version {
unsigned int main_timestamp;
} vm_map_version_t;
struct vm_map_copy {
int type;
#define VM_MAP_COPY_ENTRY_LIST 1
#define VM_MAP_COPY_OBJECT 2
#define VM_MAP_COPY_KERNEL_BUFFER 3
vm_object_offset_t offset;
vm_map_size_t size;
union {
struct vm_map_header hdr;
vm_object_t object;
struct {
void *kdata;
vm_size_t kalloc_size;
} c_k;
} c_u;
};
#define cpy_hdr c_u.hdr
#define cpy_object c_u.object
#define cpy_kdata c_u.c_k.kdata
#define cpy_kalloc_size c_u.c_k.kalloc_size
#define vm_map_copy_to_entry(copy) \
((struct vm_map_entry *) &(copy)->cpy_hdr.links)
#define vm_map_copy_first_entry(copy) \
((copy)->cpy_hdr.links.next)
#define vm_map_copy_last_entry(copy) \
((copy)->cpy_hdr.links.prev)
#define vm_map_lock_init(map) \
((map)->timestamp = 0 , \
lock_init(&(map)->lock, TRUE, 0, 0))
#define vm_map_lock(map) lock_write(&(map)->lock)
#define vm_map_unlock(map) \
((map)->timestamp++ , lock_write_done(&(map)->lock))
#define vm_map_lock_read(map) lock_read(&(map)->lock)
#define vm_map_unlock_read(map) lock_read_done(&(map)->lock)
#define vm_map_lock_write_to_read(map) \
((map)->timestamp++ , lock_write_to_read(&(map)->lock))
#define vm_map_lock_read_to_write(map) (lock_read_to_write(&(map)->lock) != TRUE)
extern void vm_map_init(void) __attribute__((section("__TEXT, initcode")));
extern kern_return_t vm_map_find_space(
vm_map_t map,
vm_map_address_t *address,
vm_map_size_t size,
vm_map_offset_t mask,
int flags,
vm_map_entry_t *o_entry);
extern boolean_t vm_map_lookup_entry(
vm_map_t map,
vm_map_address_t address,
vm_map_entry_t *entry);
extern kern_return_t vm_map_lookup_locked(
vm_map_t *var_map,
vm_map_address_t vaddr,
vm_prot_t fault_type,
int object_lock_type,
vm_map_version_t *out_version,
vm_object_t *object,
vm_object_offset_t *offset,
vm_prot_t *out_prot,
boolean_t *wired,
vm_object_fault_info_t fault_info,
vm_map_t *real_map);
extern boolean_t vm_map_verify(
vm_map_t map,
vm_map_version_t *version);
extern vm_map_entry_t vm_map_entry_insert(
vm_map_t map,
vm_map_entry_t insp_entry,
vm_map_offset_t start,
vm_map_offset_t end,
vm_object_t object,
vm_object_offset_t offset,
boolean_t needs_copy,
boolean_t is_shared,
boolean_t in_transition,
vm_prot_t cur_protection,
vm_prot_t max_protection,
vm_behavior_t behavior,
vm_inherit_t inheritance,
unsigned wired_count,
boolean_t no_cache,
boolean_t permanent,
unsigned int superpage_size);
#define vm_map_min(map) ((map)->min_offset)
#define vm_map_max(map) ((map)->max_offset)
#define vm_map_pmap(map) ((map)->pmap)
#define vm_map_verify_done(map, version) vm_map_unlock_read(map)
#if TASK_SWAPPER
#if MACH_ASSERT
extern void vm_map_reference(
vm_map_t map);
extern void vm_map_res_deallocate(
vm_map_t map);
extern void vm_map_res_reference(
vm_map_t map);
extern void vm_map_reference_swap(
vm_map_t map);
#else
#define vm_map_reference(map) \
MACRO_BEGIN \
vm_map_t Map = (map); \
if (Map) { \
lck_mtx_lock(&Map->s_lock); \
Map->res_count++; \
Map->ref_count++; \
lck_mtx_unlock(&Map->s_lock); \
} \
MACRO_END
#define vm_map_res_reference(map) \
MACRO_BEGIN \
vm_map_t Lmap = (map); \
if (Lmap->res_count == 0) { \
lck_mtx_unlock(&Lmap->s_lock);\
vm_map_lock(Lmap); \
vm_map_swapin(Lmap); \
lck_mtx_lock(&Lmap->s_lock); \
++Lmap->res_count; \
vm_map_unlock(Lmap); \
} else \
++Lmap->res_count; \
MACRO_END
#define vm_map_res_deallocate(map) \
MACRO_BEGIN \
vm_map_t Map = (map); \
if (--Map->res_count == 0) { \
lck_mtx_unlock(&Map->s_lock); \
vm_map_lock(Map); \
vm_map_swapout(Map); \
vm_map_unlock(Map); \
lck_mtx_lock(&Map->s_lock); \
} \
MACRO_END
#define vm_map_reference_swap(map) \
MACRO_BEGIN \
vm_map_t Map = (map); \
lck_mtx_lock(&Map->s_lock); \
++Map->ref_count; \
vm_map_res_reference(Map); \
lck_mtx_unlock(&Map->s_lock); \
MACRO_END
#endif
extern void vm_map_swapin(
vm_map_t map);
extern void vm_map_swapout(
vm_map_t map);
#else
#define vm_map_reference(map) \
MACRO_BEGIN \
vm_map_t Map = (map); \
if (Map) { \
lck_mtx_lock(&Map->s_lock); \
Map->ref_count++; \
lck_mtx_unlock(&Map->s_lock); \
} \
MACRO_END
#define vm_map_reference_swap(map) vm_map_reference(map)
#define vm_map_res_reference(map)
#define vm_map_res_deallocate(map)
#endif
extern vm_object_t vm_submap_object;
#define vm_map_entry_wait(map, interruptible) \
((map)->timestamp++ , \
thread_sleep_lock_write((event_t)&(map)->hdr, \
&(map)->lock, interruptible))
#define vm_map_entry_wakeup(map) \
thread_wakeup((event_t)(&(map)->hdr))
#define vm_map_ref_fast(map) \
MACRO_BEGIN \
lck_mtx_lock(&map->s_lock); \
map->ref_count++; \
vm_map_res_reference(map); \
lck_mtx_unlock(&map->s_lock); \
MACRO_END
#define vm_map_dealloc_fast(map) \
MACRO_BEGIN \
register int c; \
\
lck_mtx_lock(&map->s_lock); \
c = --map->ref_count; \
if (c > 0) \
vm_map_res_deallocate(map); \
lck_mtx_unlock(&map->s_lock); \
if (c == 0) \
vm_map_destroy(map); \
MACRO_END
extern void vm_map_simplify_entry(
vm_map_t map,
vm_map_entry_t this_entry);
extern void vm_map_simplify(
vm_map_t map,
vm_map_offset_t start);
extern vm_map_copy_t vm_map_copy_copy(
vm_map_copy_t copy);
extern kern_return_t vm_map_copyin_object(
vm_object_t object,
vm_object_offset_t offset,
vm_object_size_t size,
vm_map_copy_t *copy_result);
extern kern_return_t vm_map_enter(
vm_map_t map,
vm_map_offset_t *address,
vm_map_size_t size,
vm_map_offset_t mask,
int flags,
vm_object_t object,
vm_object_offset_t offset,
boolean_t needs_copy,
vm_prot_t cur_protection,
vm_prot_t max_protection,
vm_inherit_t inheritance);
extern kern_return_t vm_map_enter_cpm(
vm_map_t map,
vm_map_address_t *addr,
vm_map_size_t size,
int flags);
extern kern_return_t vm_map_remap(
vm_map_t target_map,
vm_map_offset_t *address,
vm_map_size_t size,
vm_map_offset_t mask,
int flags,
vm_map_t src_map,
vm_map_offset_t memory_address,
boolean_t copy,
vm_prot_t *cur_protection,
vm_prot_t *max_protection,
vm_inherit_t inheritance);
extern kern_return_t vm_map_write_user(
vm_map_t map,
void *src_p,
vm_map_offset_t dst_addr,
vm_size_t size);
extern kern_return_t vm_map_read_user(
vm_map_t map,
vm_map_offset_t src_addr,
void *dst_p,
vm_size_t size);
extern vm_map_t vm_map_fork(
vm_map_t old_map);
extern kern_return_t vm_map_inherit(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
vm_inherit_t new_inheritance);
extern kern_return_t vm_map_machine_attribute(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
vm_machine_attribute_t attribute,
vm_machine_attribute_val_t* value);
extern kern_return_t vm_map_msync(
vm_map_t map,
vm_map_address_t address,
vm_map_size_t size,
vm_sync_t sync_flags);
extern kern_return_t vm_map_behavior_set(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
vm_behavior_t new_behavior);
extern kern_return_t vm_map_purgable_control(
vm_map_t map,
vm_map_offset_t address,
vm_purgable_t control,
int *state);
extern kern_return_t vm_map_region(
vm_map_t map,
vm_map_offset_t *address,
vm_map_size_t *size,
vm_region_flavor_t flavor,
vm_region_info_t info,
mach_msg_type_number_t *count,
mach_port_t *object_name);
extern kern_return_t vm_map_region_recurse_64(
vm_map_t map,
vm_map_offset_t *address,
vm_map_size_t *size,
natural_t *nesting_depth,
vm_region_submap_info_64_t info,
mach_msg_type_number_t *count);
extern kern_return_t vm_map_page_query_internal(
vm_map_t map,
vm_map_offset_t offset,
int *disposition,
int *ref_count);
extern kern_return_t vm_map_submap(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
vm_map_t submap,
vm_map_offset_t offset,
boolean_t use_pmap);
extern void vm_map_submap_pmap_clean(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
vm_map_t sub_map,
vm_map_offset_t offset);
extern vm_map_t convert_port_entry_to_map(
ipc_port_t port);
extern vm_object_t convert_port_entry_to_object(
ipc_port_t port);
extern kern_return_t vm_map_set_cache_attr(
vm_map_t map,
vm_map_offset_t va);
#define VM_ABI_32 0x1
#define VM_ABI_64 0x2
extern int override_nx(vm_map_t map, uint32_t user_tag);
#endif
__BEGIN_DECLS
extern vm_map_t vm_map_create(
pmap_t pmap,
vm_map_offset_t min_off,
vm_map_offset_t max_off,
boolean_t pageable);
extern void vm_map_destroy(
vm_map_t map,
int flags);
extern void vm_map_deallocate(
vm_map_t map);
extern vm_map_t vm_map_switch(
vm_map_t map);
extern kern_return_t vm_map_protect(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
vm_prot_t new_prot,
boolean_t set_max);
extern boolean_t vm_map_check_protection(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
vm_prot_t protection);
extern kern_return_t vm_map_wire(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
vm_prot_t access_type,
boolean_t user_wire);
extern kern_return_t vm_map_unwire(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
boolean_t user_wire);
extern kern_return_t vm_map_enter_mem_object(
vm_map_t map,
vm_map_offset_t *address,
vm_map_size_t size,
vm_map_offset_t mask,
int flags,
ipc_port_t port,
vm_object_offset_t offset,
boolean_t needs_copy,
vm_prot_t cur_protection,
vm_prot_t max_protection,
vm_inherit_t inheritance);
extern kern_return_t vm_map_enter_mem_object_control(
vm_map_t map,
vm_map_offset_t *address,
vm_map_size_t size,
vm_map_offset_t mask,
int flags,
memory_object_control_t control,
vm_object_offset_t offset,
boolean_t needs_copy,
vm_prot_t cur_protection,
vm_prot_t max_protection,
vm_inherit_t inheritance);
extern kern_return_t vm_map_remove(
vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end,
boolean_t flags);
extern void vm_map_copy_discard(
vm_map_copy_t copy);
extern kern_return_t vm_map_copy_overwrite(
vm_map_t dst_map,
vm_map_address_t dst_addr,
vm_map_copy_t copy,
boolean_t interruptible);
extern kern_return_t vm_map_copyout(
vm_map_t dst_map,
vm_map_address_t *dst_addr,
vm_map_copy_t copy);
extern kern_return_t vm_map_copyin(
vm_map_t src_map,
vm_map_address_t src_addr,
vm_map_size_t len,
boolean_t src_destroy,
vm_map_copy_t *copy_result);
extern kern_return_t vm_map_copyin_common(
vm_map_t src_map,
vm_map_address_t src_addr,
vm_map_size_t len,
boolean_t src_destroy,
boolean_t src_volatile,
vm_map_copy_t *copy_result,
boolean_t use_maxprot);
extern void vm_map_disable_NX(
vm_map_t map);
extern void vm_map_disallow_data_exec(
vm_map_t map);
extern void vm_map_set_64bit(
vm_map_t map);
extern void vm_map_set_32bit(
vm_map_t map);
extern boolean_t vm_map_is_64bit(
vm_map_t map);
extern boolean_t vm_map_has_4GB_pagezero(
vm_map_t map);
extern void vm_map_set_4GB_pagezero(
vm_map_t map);
extern void vm_map_clear_4GB_pagezero(
vm_map_t map);
extern kern_return_t vm_map_raise_min_offset(
vm_map_t map,
vm_map_offset_t new_min_offset);
extern vm_map_offset_t vm_compute_max_offset(
unsigned is64);
extern void vm_map_set_user_wire_limit(
vm_map_t map,
vm_size_t limit);
extern void vm_map_switch_protect(
vm_map_t map,
boolean_t val);
extern boolean_t first_free_is_valid(vm_map_t);
#ifdef XNU_KERNEL_PRIVATE
extern kern_return_t vm_map_page_info(
vm_map_t map,
vm_map_offset_t offset,
vm_page_info_flavor_t flavor,
vm_page_info_t info,
mach_msg_type_number_t *count);
#endif
#ifdef MACH_KERNEL_PRIVATE
#define vm_map_copyin(src_map, src_addr, len, src_destroy, copy_result) \
vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
FALSE, copy_result, FALSE)
#define vm_map_copyin_maxprot(src_map, \
src_addr, len, src_destroy, copy_result) \
vm_map_copyin_common(src_map, src_addr, len, src_destroy, \
FALSE, copy_result, TRUE)
#endif
#define vm_map_round_page(x) (((vm_map_offset_t)(x) + PAGE_MASK) & ~((signed)PAGE_MASK))
#define vm_map_trunc_page(x) ((vm_map_offset_t)(x) & ~((signed)PAGE_MASK))
#define VM_MAP_NO_FLAGS 0x0
#define VM_MAP_REMOVE_KUNWIRE 0x1
#define VM_MAP_REMOVE_INTERRUPTIBLE 0x2
#define VM_MAP_REMOVE_WAIT_FOR_KWIRE 0x4
#define VM_MAP_REMOVE_SAVE_ENTRIES 0x8
#define VM_MAP_REMOVE_NO_PMAP_CLEANUP 0x10
extern kern_return_t vm_map_get_upl(
vm_map_t target_map,
vm_map_offset_t map_offset,
upl_size_t *size,
upl_t *upl,
upl_page_info_array_t page_info,
unsigned int *page_infoCnt,
int *flags,
int force_data_sync);
#if CONFIG_DYNAMIC_CODE_SIGNING
extern kern_return_t vm_map_sign(vm_map_t map,
vm_map_offset_t start,
vm_map_offset_t end);
#endif
#if CONFIG_FREEZE
extern kern_return_t vm_map_freeze_walk(
vm_map_t map,
unsigned int *purgeable_count,
unsigned int *wired_count,
unsigned int *clean_count,
unsigned int *dirty_count,
boolean_t *has_shared);
extern kern_return_t vm_map_freeze(
vm_map_t map,
unsigned int *purgeable_count,
unsigned int *wired_count,
unsigned int *clean_count,
unsigned int *dirty_count,
boolean_t *has_shared);
extern void vm_map_thaw(
vm_map_t map);
#endif
__END_DECLS
#endif
#endif