use the -newos toolchain even if -elf is present.
[newos.git] / include / kernel / vm.h
blob7c01f252eeacd0bc467a2592347261640897364e
1 /*
2 ** Copyright 2001-2004, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #ifndef _KERNEL_VM_H
6 #define _KERNEL_VM_H
8 #include <kernel/kernel.h>
9 #include <boot/stage2.h>
10 #include <kernel/vfs.h>
11 #include <kernel/arch/vm_translation_map.h>
12 #include <kernel/list.h>
14 // vm page
15 typedef struct vm_page {
16 int magic;
18 struct list_node queue_node;
20 struct vm_page *hash_next;
22 off_t offset;
23 addr_t ppn; // physical page number
25 struct vm_cache_ref *cache_ref;
27 struct list_node cache_node;
29 unsigned int ref_count;
31 unsigned int type : 2;
32 unsigned int state : 4;
33 } vm_page;
35 #define VM_PAGE_MAGIC 'vmpg'
37 enum {
38 PAGE_TYPE_PHYSICAL = 0,
39 PAGE_TYPE_DUMMY,
40 PAGE_TYPE_GUARD
43 enum {
44 PAGE_STATE_ACTIVE = 0,
45 PAGE_STATE_INACTIVE,
46 PAGE_STATE_BUSY,
47 PAGE_STATE_MODIFIED,
48 PAGE_STATE_MODIFIED_TEMPORARY,
49 PAGE_STATE_FREE,
50 PAGE_STATE_CLEAR,
51 PAGE_STATE_WIRED,
52 PAGE_STATE_UNUSED
55 // vm_cache_ref
56 typedef struct vm_cache_ref {
57 int magic;
58 struct vm_cache *cache;
59 mutex lock;
61 struct list_node region_list_head;
63 int ref_count;
64 } vm_cache_ref;
66 #define VM_CACHE_REF_MAGIC 'vmcr'
68 // vm_cache
69 typedef struct vm_cache {
70 int magic;
71 struct list_node page_list_head;
72 vm_cache_ref *ref;
73 struct vm_cache *source;
74 struct vm_store *store;
75 unsigned int temporary : 1;
76 unsigned int scan_skip : 1;
77 off_t virtual_size;
78 } vm_cache;
80 #define VM_CACHE_MAGIC 'vmca'
82 // info about a region that external entities may want to know
83 // used in vm_get_region_info()
84 typedef struct vm_region_info {
85 region_id id;
86 addr_t base;
87 addr_t size;
88 int lock;
89 int wiring;
90 char name[SYS_MAX_OS_NAME_LEN];
91 } vm_region_info;
93 // vm region
94 typedef struct vm_region {
95 int magic;
96 char *name;
97 region_id id;
98 addr_t base;
99 addr_t size;
100 int lock;
101 int wiring;
102 int ref_count;
104 off_t cache_offset;
105 struct vm_cache_ref *cache_ref;
107 struct vm_address_space *aspace;
108 struct vm_region *aspace_next;
109 struct vm_virtual_map *map;
110 struct list_node cache_node;
111 struct vm_region *hash_next;
112 } vm_region;
114 #define VM_REGION_MAGIC 'vmrg'
116 // virtual map (1 per address space)
117 typedef struct vm_virtual_map {
118 vm_region *region_list;
119 vm_region *region_hint;
120 int change_count;
121 sem_id sem;
122 struct vm_address_space *aspace;
123 addr_t base;
124 addr_t alloc_base;
125 addr_t size;
126 } vm_virtual_map;
128 enum {
129 VM_ASPACE_STATE_NORMAL = 0,
130 VM_ASPACE_STATE_DELETION
133 // address space
134 typedef struct vm_address_space {
135 int magic;
136 vm_virtual_map virtual_map;
137 vm_translation_map translation_map;
138 char *name;
139 aspace_id id;
140 int ref_count;
141 int fault_count;
142 int state;
143 addr_t scan_va;
144 addr_t working_set_size;
145 addr_t max_working_set;
146 addr_t min_working_set;
147 bigtime_t last_working_set_adjust;
148 struct vm_address_space *hash_next;
149 } vm_address_space;
151 #define VM_ASPACE_MAGIC 'vmas'
153 // vm_store
154 typedef struct vm_store {
155 int magic;
156 struct vm_store_ops *ops;
157 struct vm_cache *cache;
158 void *data;
159 off_t committed_size;
160 } vm_store;
162 #define VM_STORE_MAGIC 'vmst'
164 // vm_store_ops
165 typedef struct vm_store_ops {
166 void (*destroy)(struct vm_store *backing_store);
167 off_t (*commit)(struct vm_store *backing_store, off_t size);
168 int (*has_page)(struct vm_store *backing_store, off_t offset);
169 ssize_t (*read)(struct vm_store *backing_store, off_t offset, iovecs *vecs);
170 ssize_t (*write)(struct vm_store *backing_store, off_t offset, iovecs *vecs);
171 int (*fault)(struct vm_store *backing_store, struct vm_address_space *aspace, off_t offset);
172 void (*acquire_ref)(struct vm_store *backing_store);
173 void (*release_ref)(struct vm_store *backing_store);
174 } vm_store_ops;
176 // args for the create_area funcs
177 enum {
178 REGION_ADDR_ANY_ADDRESS = 0,
179 REGION_ADDR_EXACT_ADDRESS
182 enum {
183 REGION_NO_PRIVATE_MAP = 0,
184 REGION_PRIVATE_MAP
187 enum {
188 REGION_WIRING_LAZY = 0,
189 REGION_WIRING_WIRED,
190 REGION_WIRING_WIRED_ALREADY,
191 REGION_WIRING_WIRED_CONTIG
194 enum {
195 PHYSICAL_PAGE_NO_WAIT = 0,
196 PHYSICAL_PAGE_CAN_WAIT,
199 #define LOCK_RO 0x0
200 #define LOCK_RW 0x1
201 #define LOCK_KERNEL 0x2
202 #define LOCK_MASK 0x3
204 //void vm_dump_areas(vm_address_space *aspace);
205 int vm_init(kernel_args *ka);
206 int vm_init_postsem(kernel_args *ka);
207 int vm_init_postthread(kernel_args *ka);
209 aspace_id vm_create_aspace(const char *name, addr_t base, addr_t alloc_base, addr_t size, bool kernel);
210 int vm_delete_aspace(aspace_id);
211 vm_address_space *vm_get_kernel_aspace(void);
212 aspace_id vm_get_kernel_aspace_id(void);
213 vm_address_space *vm_get_current_user_aspace(void);
214 aspace_id vm_get_current_user_aspace_id(void);
215 vm_address_space *vm_get_aspace_by_id(aspace_id aid);
216 void vm_put_aspace(vm_address_space *aspace);
217 vm_region *vm_get_region_by_id(region_id rid);
218 void vm_put_region(vm_region *region);
219 #define vm_aspace_swap(aspace) arch_vm_aspace_swap(aspace)
221 region_id vm_create_anonymous_region(aspace_id aid, char *name, void **address, int addr_type,
222 addr_t size, int wiring, int lock);
223 region_id vm_map_physical_memory(aspace_id aid, char *name, void **address, int addr_type,
224 addr_t size, int lock, addr_t phys_addr);
225 region_id vm_map_file(aspace_id aid, char *name, void **address, int addr_type,
226 addr_t size, int lock, int mapping, const char *path, off_t offset);
227 region_id vm_create_null_region(aspace_id aid, char *name, void **address, int addr_type, addr_t size);
228 region_id vm_clone_region(aspace_id aid, char *name, void **address, int addr_type,
229 region_id source_region, int mapping, int lock);
230 int vm_delete_region(aspace_id aid, region_id id);
231 region_id vm_find_region_by_name(aspace_id aid, const char *name);
232 int vm_get_region_info(region_id id, vm_region_info *info);
234 int vm_get_page_mapping(aspace_id aid, addr_t vaddr, addr_t *paddr);
235 int vm_get_physical_page(addr_t paddr, addr_t *vaddr, int flags);
236 int vm_put_physical_page(addr_t vaddr);
238 int user_memcpy(void *to, const void *from, size_t size);
239 int user_strcpy(char *to, const char *from);
240 int user_strncpy(char *to, const char *from, size_t size);
241 int user_memset(void *s, char c, size_t count);
243 region_id user_vm_create_anonymous_region(char *uname, void **uaddress, int addr_type,
244 addr_t size, int wiring, int lock);
245 region_id user_vm_clone_region(char *uname, void **uaddress, int addr_type,
246 region_id source_region, int mapping, int lock);
247 region_id user_vm_map_file(char *uname, void **uaddress, int addr_type,
248 addr_t size, int lock, int mapping, const char *upath, off_t offset);
249 int user_vm_get_region_info(region_id id, vm_region_info *uinfo);
250 int user_vm_delete_region(region_id id);
252 // state of the vm, for informational purposes only
253 typedef struct {
254 // info about the size of memory in the system
255 int physical_page_size;
256 int physical_pages;
258 // amount of virtual memory left to commit
259 int max_commit;
261 // info about the page queues
262 int active_pages;
263 int inactive_pages;
264 int busy_pages;
265 int unused_pages;
266 int modified_pages;
267 int modified_temporary_pages;
268 int free_pages;
269 int clear_pages;
270 int wired_pages;
272 // info about vm activity
273 int page_faults;
274 } vm_info_t;
276 addr_t vm_get_mem_size(void);
277 int user_vm_get_vm_info(vm_info_t *uinfo);
279 // XXX remove later
280 void vm_test(void);
282 #endif