1 /* 2 * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 * 5 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. 6 * Distributed under the terms of the NewOS License. 7 */ 8 #ifndef _KERNEL_VM_VM_H 9 #define _KERNEL_VM_VM_H 10 11 #include <OS.h> 12 13 #include <arch/vm.h> 14 #include <vm_defs.h> 15 #include <vm/vm_types.h> 16 17 18 struct generic_io_vec; 19 struct kernel_args; 20 struct system_memory_info; 21 struct VMAddressSpace; 22 struct VMArea; 23 struct VMCache; 24 struct vm_page; 25 struct vnode; 26 struct VMPageWiringInfo; 27 28 29 // area creation flags 30 #define CREATE_AREA_DONT_WAIT 0x01 31 #define CREATE_AREA_UNMAP_ADDRESS_RANGE 0x02 32 #define CREATE_AREA_DONT_CLEAR 0x04 33 #define CREATE_AREA_PRIORITY_VIP 0x08 34 #define CREATE_AREA_DONT_COMMIT_MEMORY 0x10 35 36 // memory/page allocation priorities 37 #define VM_PRIORITY_USER 0 38 #define VM_PRIORITY_SYSTEM 1 39 #define VM_PRIORITY_VIP 2 40 41 // page reserves 42 #define VM_PAGE_RESERVE_USER 512 43 #define VM_PAGE_RESERVE_SYSTEM 128 44 45 // memory reserves 46 #define VM_MEMORY_RESERVE_USER (VM_PAGE_RESERVE_USER * B_PAGE_SIZE) 47 #define VM_MEMORY_RESERVE_SYSTEM (VM_PAGE_RESERVE_SYSTEM * B_PAGE_SIZE) 48 49 50 #ifdef __cplusplus 51 extern "C" { 52 #endif 53 54 // startup only 55 status_t vm_init(struct kernel_args *args); 56 status_t vm_init_post_sem(struct kernel_args *args); 57 status_t vm_init_post_thread(struct kernel_args *args); 58 status_t vm_init_post_modules(struct kernel_args *args); 59 void vm_free_kernel_args(struct kernel_args *args); 60 void vm_free_unused_boot_loader_range(addr_t start, addr_t end); 61 page_num_t vm_allocate_early_physical_page(kernel_args *args); 62 addr_t vm_allocate_early(struct kernel_args *args, size_t virtualSize, 63 size_t physicalSize, uint32 attributes, addr_t alignment); 64 65 void slab_init(struct kernel_args *args); 66 void slab_init_post_area(); 67 void slab_init_post_sem(); 68 void slab_init_post_thread(); 69 70 // to protect code regions with interrupts turned on 71 void permit_page_faults(void); 72 void forbid_page_faults(void); 73 74 // private kernel only extension (should be moved somewhere else): 75 area_id create_area_etc(team_id team, const char *name, size_t size, 76 uint32 lock, uint32 protection, uint32 flags, uint32 guardSize, 77 const virtual_address_restrictions* virtualAddressRestrictions, 78 const physical_address_restrictions* physicalAddressRestrictions, 79 void **_address); 80 area_id transfer_area(area_id id, void** _address, uint32 addressSpec, 81 team_id target, bool kernel); 82 83 const char* vm_cache_type_to_string(int32 type); 84 85 void vm_free_page_mapping(page_num_t page, vm_page_mapping* mapping, uint32 flags); 86 87 status_t vm_prepare_kernel_area_debug_protection(area_id id, void** cookie); 88 status_t vm_set_kernel_area_debug_protection(void* cookie, void* _address, 89 size_t size, uint32 protection); 90 91 status_t vm_block_address_range(const char* name, void* address, addr_t size); 92 status_t vm_unreserve_address_range(team_id team, void *address, addr_t size); 93 status_t vm_reserve_address_range(team_id team, void **_address, 94 uint32 addressSpec, addr_t size, uint32 flags); 95 area_id vm_create_anonymous_area(team_id team, const char* name, addr_t size, 96 uint32 wiring, uint32 protection, uint32 flags, addr_t guardSize, 97 const virtual_address_restrictions* virtualAddressRestrictions, 98 const physical_address_restrictions* physicalAddressRestrictions, 99 bool kernel, void** _address); 100 area_id vm_map_physical_memory(team_id team, const char *name, void **address, 101 uint32 addressSpec, addr_t size, uint32 protection, 102 phys_addr_t physicalAddress, bool alreadyWired); 103 area_id vm_map_physical_memory_vecs(team_id team, const char* name, 104 void** _address, uint32 addressSpec, addr_t* _size, uint32 protection, 105 struct generic_io_vec* vecs, uint32 vecCount); 106 area_id vm_map_file(team_id aid, const char *name, void **address, 107 uint32 addressSpec, addr_t size, uint32 protection, uint32 mapping, 108 bool unmapAddressRange, int fd, off_t offset); 109 struct VMCache *vm_area_get_locked_cache(struct VMArea *area); 110 void vm_area_put_locked_cache(struct VMCache *cache); 111 area_id vm_create_null_area(team_id team, const char *name, void **address, 112 uint32 addressSpec, addr_t size, uint32 flags); 113 area_id vm_copy_area(team_id team, const char *name, void **_address, 114 uint32 addressSpec, area_id sourceID); 115 area_id vm_clone_area(team_id team, const char *name, void **address, 116 uint32 addressSpec, uint32 protection, uint32 mapping, 117 area_id sourceArea, bool kernel); 118 status_t vm_delete_area(team_id teamID, area_id areaID, bool kernel); 119 status_t vm_create_vnode_cache(struct vnode *vnode, struct VMCache **_cache); 120 status_t vm_set_area_memory_type(area_id id, phys_addr_t physicalBase, 121 uint32 type); 122 status_t vm_set_area_protection(team_id team, area_id areaID, 123 uint32 newProtection, bool kernel); 124 status_t vm_get_page_mapping(team_id team, addr_t vaddr, phys_addr_t *paddr); 125 bool vm_test_map_modification(struct vm_page *page); 126 void vm_clear_map_flags(struct vm_page *page, uint32 flags); 127 void vm_remove_all_page_mappings(struct vm_page *page); 128 int32 vm_clear_page_mapping_accessed_flags(struct vm_page *page); 129 int32 vm_remove_all_page_mappings_if_unaccessed(struct vm_page *page); 130 status_t vm_wire_page(team_id team, addr_t address, bool writable, 131 struct VMPageWiringInfo* info); 132 void vm_unwire_page(struct VMPageWiringInfo* info); 133 134 status_t vm_get_physical_page(phys_addr_t paddr, addr_t* vaddr, void** _handle); 135 status_t vm_put_physical_page(addr_t vaddr, void* handle); 136 status_t vm_get_physical_page_current_cpu(phys_addr_t paddr, addr_t* vaddr, 137 void** _handle); 138 status_t vm_put_physical_page_current_cpu(addr_t vaddr, void* handle); 139 status_t vm_get_physical_page_debug(phys_addr_t paddr, addr_t* vaddr, 140 void** _handle); 141 status_t vm_put_physical_page_debug(addr_t vaddr, void* handle); 142 143 void vm_get_info(system_info *info); 144 uint32 vm_num_page_faults(void); 145 off_t vm_available_memory(void); 146 off_t vm_available_not_needed_memory(void); 147 off_t vm_available_not_needed_memory_debug(void); 148 size_t vm_kernel_address_space_left(void); 149 150 status_t vm_memset_physical(phys_addr_t address, int value, phys_size_t length); 151 status_t vm_memcpy_from_physical(void* to, phys_addr_t from, size_t length, 152 bool user); 153 status_t vm_memcpy_to_physical(phys_addr_t to, const void* from, size_t length, 154 bool user); 155 void vm_memcpy_physical_page(phys_addr_t to, phys_addr_t from); 156 157 status_t vm_debug_copy_page_memory(team_id teamID, void* unsafeMemory, 158 void* buffer, size_t size, bool copyToUnsafe); 159 160 // user syscalls 161 area_id _user_create_area(const char *name, void **address, uint32 addressSpec, 162 size_t size, uint32 lock, uint32 protection); 163 status_t _user_delete_area(area_id area); 164 165 area_id _user_map_file(const char *uname, void **uaddress, uint32 addressSpec, 166 size_t size, uint32 protection, uint32 mapping, 167 bool unmapAddressRange, int fd, off_t offset); 168 status_t _user_unmap_memory(void *address, size_t size); 169 status_t _user_set_memory_protection(void* address, size_t size, 170 uint32 protection); 171 status_t _user_sync_memory(void *address, size_t size, uint32 flags); 172 status_t _user_memory_advice(void* address, size_t size, uint32 advice); 173 status_t _user_get_memory_properties(team_id teamID, const void *address, 174 uint32 *_protected, uint32 *_lock); 175 176 status_t _user_mlock(const void* address, size_t size); 177 status_t _user_munlock(const void* address, size_t size); 178 179 area_id _user_area_for(void *address); 180 area_id _user_find_area(const char *name); 181 status_t _user_get_area_info(area_id area, area_info *info); 182 status_t _user_get_next_area_info(team_id team, ssize_t *cookie, area_info *info); 183 status_t _user_resize_area(area_id area, size_t newSize); 184 area_id _user_transfer_area(area_id area, void **_address, uint32 addressSpec, 185 team_id target); 186 status_t _user_set_area_protection(area_id area, uint32 newProtection); 187 area_id _user_clone_area(const char *name, void **_address, uint32 addressSpec, 188 uint32 protection, area_id sourceArea); 189 status_t _user_reserve_address_range(addr_t* userAddress, uint32 addressSpec, 190 addr_t size); 191 status_t _user_unreserve_address_range(addr_t address, addr_t size); 192 193 #ifdef __cplusplus 194 } 195 #endif 196 197 #endif /* _KERNEL_VM_VM_H */ 198