2 ** Copyright 2001-2004, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
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>
15 typedef struct vm_page
{
18 struct list_node queue_node
;
20 struct vm_page
*hash_next
;
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;
35 #define VM_PAGE_MAGIC 'vmpg'
38 PAGE_TYPE_PHYSICAL
= 0,
44 PAGE_STATE_ACTIVE
= 0,
48 PAGE_STATE_MODIFIED_TEMPORARY
,
56 typedef struct vm_cache_ref
{
58 struct vm_cache
*cache
;
61 struct list_node region_list_head
;
66 #define VM_CACHE_REF_MAGIC 'vmcr'
69 typedef struct vm_cache
{
71 struct list_node page_list_head
;
73 struct vm_cache
*source
;
74 struct vm_store
*store
;
75 unsigned int temporary
: 1;
76 unsigned int scan_skip
: 1;
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
{
90 char name
[SYS_MAX_OS_NAME_LEN
];
94 typedef struct vm_region
{
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
;
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
;
122 struct vm_address_space
*aspace
;
129 VM_ASPACE_STATE_NORMAL
= 0,
130 VM_ASPACE_STATE_DELETION
134 typedef struct vm_address_space
{
136 vm_virtual_map virtual_map
;
137 vm_translation_map translation_map
;
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
;
151 #define VM_ASPACE_MAGIC 'vmas'
154 typedef struct vm_store
{
156 struct vm_store_ops
*ops
;
157 struct vm_cache
*cache
;
159 off_t committed_size
;
162 #define VM_STORE_MAGIC 'vmst'
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
);
176 // args for the create_area funcs
178 REGION_ADDR_ANY_ADDRESS
= 0,
179 REGION_ADDR_EXACT_ADDRESS
183 REGION_NO_PRIVATE_MAP
= 0,
188 REGION_WIRING_LAZY
= 0,
190 REGION_WIRING_WIRED_ALREADY
,
191 REGION_WIRING_WIRED_CONTIG
195 PHYSICAL_PAGE_NO_WAIT
= 0,
196 PHYSICAL_PAGE_CAN_WAIT
,
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
254 // info about the size of memory in the system
255 int physical_page_size
;
258 // amount of virtual memory left to commit
261 // info about the page queues
267 int modified_temporary_pages
;
272 // info about vm activity
276 addr_t
vm_get_mem_size(void);
277 int user_vm_get_vm_info(vm_info_t
*uinfo
);