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 16 17 struct iovec; 18 struct kernel_args; 19 struct ObjectCache; 20 struct system_memory_info; 21 struct team; 22 struct VMAddressSpace; 23 struct VMArea; 24 struct VMCache; 25 struct vm_page; 26 struct vnode; 27 struct VMPageWiringInfo; 28 29 30 // area creation flags 31 #define CREATE_AREA_DONT_WAIT 0x01 32 #define CREATE_AREA_UNMAP_ADDRESS_RANGE 0x02 33 #define CREATE_AREA_DONT_CLEAR 0x04 34 #define CREATE_AREA_PRIORITY_VIP 0x08 35 #define CREATE_AREA_DONT_COMMIT_MEMORY 0x10 36 37 // memory/page allocation priorities 38 #define VM_PRIORITY_USER 0 39 #define VM_PRIORITY_SYSTEM 1 40 #define VM_PRIORITY_VIP 2 41 42 // page reserves 43 #define VM_PAGE_RESERVE_USER 512 44 #define VM_PAGE_RESERVE_SYSTEM 128 45 46 // memory reserves 47 #define VM_MEMORY_RESERVE_USER (VM_PAGE_RESERVE_USER * B_PAGE_SIZE) 48 #define VM_MEMORY_RESERVE_SYSTEM (VM_PAGE_RESERVE_SYSTEM * B_PAGE_SIZE) 49 50 51 extern struct ObjectCache* gPageMappingsObjectCache; 52 53 54 #ifdef __cplusplus 55 extern "C" { 56 #endif 57 58 // startup only 59 status_t vm_init(struct kernel_args *args); 60 status_t vm_init_post_sem(struct kernel_args *args); 61 status_t vm_init_post_thread(struct kernel_args *args); 62 status_t vm_init_post_modules(struct kernel_args *args); 63 void vm_free_kernel_args(struct kernel_args *args); 64 void vm_free_unused_boot_loader_range(addr_t start, addr_t end); 65 addr_t vm_allocate_early(struct kernel_args *args, size_t virtualSize, 66 size_t physicalSize, uint32 attributes, bool blockAlign); 67 68 void slab_init(struct kernel_args *args); 69 void slab_init_post_area(); 70 void slab_init_post_sem(); 71 void slab_init_post_thread(); 72 73 // to protect code regions with interrupts turned on 74 void permit_page_faults(void); 75 void forbid_page_faults(void); 76 77 // private kernel only extension (should be moved somewhere else): 78 area_id create_area_etc(team_id team, const char *name, void **address, 79 uint32 addressSpec, uint32 size, uint32 lock, uint32 protection, 80 addr_t physicalAddress, uint32 flags); 81 area_id transfer_area(area_id id, void** _address, uint32 addressSpec, 82 team_id target, bool kernel); 83 84 status_t vm_block_address_range(const char* name, void* address, addr_t size); 85 status_t vm_unreserve_address_range(team_id team, void *address, addr_t size); 86 status_t vm_reserve_address_range(team_id team, void **_address, 87 uint32 addressSpec, addr_t size, uint32 flags); 88 area_id vm_create_anonymous_area(team_id team, const char *name, void **address, 89 uint32 addressSpec, addr_t size, uint32 wiring, uint32 protection, 90 addr_t physicalAddress, uint32 flags, bool kernel); 91 area_id vm_map_physical_memory(team_id team, const char *name, void **address, 92 uint32 addressSpec, addr_t size, uint32 protection, 93 addr_t physicalAddress, bool alreadyWired); 94 area_id vm_map_physical_memory_vecs(team_id team, const char* name, 95 void** _address, uint32 addressSpec, addr_t* _size, uint32 protection, 96 struct iovec* vecs, uint32 vecCount); 97 area_id vm_map_file(team_id aid, const char *name, void **address, 98 uint32 addressSpec, addr_t size, uint32 protection, uint32 mapping, 99 bool unmapAddressRange, int fd, off_t offset); 100 struct VMCache *vm_area_get_locked_cache(struct VMArea *area); 101 void vm_area_put_locked_cache(struct VMCache *cache); 102 area_id vm_create_null_area(team_id team, const char *name, void **address, 103 uint32 addressSpec, addr_t size, uint32 flags); 104 area_id vm_copy_area(team_id team, const char *name, void **_address, 105 uint32 addressSpec, uint32 protection, area_id sourceID); 106 area_id vm_clone_area(team_id team, const char *name, void **address, 107 uint32 addressSpec, uint32 protection, uint32 mapping, 108 area_id sourceArea, bool kernel); 109 status_t vm_delete_area(team_id teamID, area_id areaID, bool kernel); 110 status_t vm_create_vnode_cache(struct vnode *vnode, struct VMCache **_cache); 111 status_t vm_set_area_memory_type(area_id id, addr_t physicalBase, uint32 type); 112 status_t vm_get_page_mapping(team_id team, addr_t vaddr, addr_t *paddr); 113 bool vm_test_map_modification(struct vm_page *page); 114 void vm_clear_map_flags(struct vm_page *page, uint32 flags); 115 void vm_remove_all_page_mappings(struct vm_page *page); 116 int32 vm_clear_page_mapping_accessed_flags(struct vm_page *page); 117 int32 vm_remove_all_page_mappings_if_unaccessed(struct vm_page *page); 118 status_t vm_wire_page(team_id team, addr_t address, bool writable, 119 struct VMPageWiringInfo* info); 120 void vm_unwire_page(struct VMPageWiringInfo* info); 121 122 status_t vm_get_physical_page(addr_t paddr, addr_t* vaddr, void** _handle); 123 status_t vm_put_physical_page(addr_t vaddr, void* handle); 124 status_t vm_get_physical_page_current_cpu(addr_t paddr, addr_t* vaddr, 125 void** _handle); 126 status_t vm_put_physical_page_current_cpu(addr_t vaddr, void* handle); 127 status_t vm_get_physical_page_debug(addr_t paddr, addr_t* vaddr, 128 void** _handle); 129 status_t vm_put_physical_page_debug(addr_t vaddr, void* handle); 130 131 void vm_get_info(struct system_memory_info *info); 132 uint32 vm_num_page_faults(void); 133 off_t vm_available_memory(void); 134 off_t vm_available_not_needed_memory(void); 135 size_t vm_kernel_address_space_left(void); 136 137 status_t vm_memset_physical(addr_t address, int value, size_t length); 138 status_t vm_memcpy_from_physical(void* to, addr_t from, size_t length, 139 bool user); 140 status_t vm_memcpy_to_physical(addr_t to, const void* from, size_t length, 141 bool user); 142 void vm_memcpy_physical_page(addr_t to, addr_t from); 143 144 status_t vm_debug_copy_page_memory(team_id teamID, void* unsafeMemory, 145 void* buffer, size_t size, bool copyToUnsafe); 146 147 // user syscalls 148 area_id _user_create_area(const char *name, void **address, uint32 addressSpec, 149 size_t size, uint32 lock, uint32 protection); 150 status_t _user_delete_area(area_id area); 151 152 area_id _user_map_file(const char *uname, void **uaddress, uint32 addressSpec, 153 size_t size, uint32 protection, uint32 mapping, 154 bool unmapAddressRange, int fd, off_t offset); 155 status_t _user_unmap_memory(void *address, size_t size); 156 status_t _user_set_memory_protection(void* address, size_t size, 157 uint32 protection); 158 status_t _user_sync_memory(void *address, size_t size, uint32 flags); 159 status_t _user_memory_advice(void* address, size_t size, uint32 advice); 160 161 area_id _user_area_for(void *address); 162 area_id _user_find_area(const char *name); 163 status_t _user_get_area_info(area_id area, area_info *info); 164 status_t _user_get_next_area_info(team_id team, int32 *cookie, area_info *info); 165 status_t _user_resize_area(area_id area, size_t newSize); 166 area_id _user_transfer_area(area_id area, void **_address, uint32 addressSpec, 167 team_id target); 168 status_t _user_set_area_protection(area_id area, uint32 newProtection); 169 area_id _user_clone_area(const char *name, void **_address, uint32 addressSpec, 170 uint32 protection, area_id sourceArea); 171 status_t _user_reserve_address_range(addr_t* userAddress, uint32 addressSpec, 172 addr_t size); 173 status_t _user_unreserve_address_range(addr_t address, addr_t size); 174 175 #ifdef __cplusplus 176 } 177 #endif 178 179 #endif /* _KERNEL_VM_VM_H */ 180