AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
vm.h
Go to the documentation of this file.
1#ifndef _MM_VM_H
2#define _MM_VM_H
3
4#include <core/system.h>
5#include <core/printk.h>
6
7struct vm_space;
8struct vm_entry;
9struct vm_object;
10struct vm_page;
11
12MALLOC_DECLARE(M_VM_ENTRY);
13MALLOC_DECLARE(M_VM_OBJECT);
14MALLOC_DECLARE(M_VM_ANON);
15MALLOC_DECLARE(M_VM_AREF);
16
17#include <ds/queue.h>
18
19/*
20 * Virtual memory: address spaces, their mappings, and where the mappings'
21 * pages come from (anons and vm objects). See docs/kernel/memory.md.
22 */
23
25struct vm_space {
27 struct pmap *pmap;
28
31};
32
33#include <fs/vfs.h>
34#include <mm/mm.h>
35#include <ds/hashmap.h>
36
37/* A mapping's flags: its permissions, and more. */
38#define VM_KR 0x0001
39#define VM_KW 0x0002
40#define VM_KX 0x0004
41#define VM_UR 0x0008
42#define VM_UW 0x0010
43#define VM_UX 0x0020
44#define VM_PERM 0x003F
45#define VM_NOCACHE 0x0040
46#define VM_SHARED 0x0080
47#define VM_COPY 0x0100
48
49#define VM_KRW (VM_KR|VM_KW)
50#define VM_KRX (VM_KR|VM_KX)
51#define VM_KWX (VM_KW|VM_KX)
52#define VM_KRWX (VM_KR|VM_KW|VM_KX)
53#define VM_URW (VM_UR|VM_UW)
54#define VM_URX (VM_UR|VM_UX)
55#define VM_UWX (VM_UW|VM_UX)
56#define VM_URWX (VM_UR|VM_UW|VM_UX)
57
58/* The kinds of vm objects. */
59#define VMOBJ_ZERO 0x0000
60#define VMOBJ_FILE 0x0001
61
66struct vm_entry {
70
73
75 size_t size;
76
79
82
85
87 size_t off;
88
90 struct qnode *qnode;
91};
92
94struct vm_pager {
96 struct vm_page *(*in)(struct vm_object *vm_object, size_t off);
97
99 int (*out)(struct vm_object *vm_object, size_t off);
100};
101
107struct vm_aref {
110
112 size_t ref;
113
116};
117
119struct vm_anon {
121 struct hashmap *arefs;
122
124 size_t ref;
125
127 int flags;
128};
129
132struct vm_object {
134 struct hashmap *pages;
135
137 int type;
138
140 size_t ref;
141
144
146 void *p;
147};
148
150struct vm_page {
152
154
155 size_t off;
156
157 size_t ref;
158};
159
160/* The kernel's address space. */
161extern struct vm_space kvm_space;
162
163/* mm/kvmem.c: sets up kmalloc(); its self-checks */
164void kvmem_setup(void);
165void kvmem_selftest(void);
166void kvmem_guardtest(void);
167
168/* mm/vmm.c: maps and unmaps a map entry's range */
169int vm_map(struct vm_space *vm_space, struct vm_entry *vm_entry);
170void vm_unmap_full(struct vm_space *vm_space, struct vm_entry *vm_entry);
171
172/* mm/vm_space.c: address spaces */
173int vm_space_fork(struct vm_space *parent, struct vm_space *child);
175struct vm_entry *vm_space_find(struct vm_space *vm_space, vaddr_t vaddr);
177
178/* mm/vm_entry.c: map entries */
179struct vm_entry *vm_entry_new(void);
181
182/* mm/vm_anon.c: anons */
183struct vm_anon *vm_anon_new(void);
184struct vm_anon *vm_anon_copy(struct vm_anon *vm_anon);
185void vm_anon_incref(struct vm_anon *vm_anon);
186void vm_anon_decref(struct vm_anon *vm_anon);
187void vm_anon_destroy(struct vm_anon *vm_anon);
188
189/* fs/vm_object.c and mm/vm_object.c: vm objects, and a file's pages */
190struct vm_object *vm_object_vnode(struct vnode *vnode);
191void vnode_pages_update(struct vnode *vnode, off_t start, off_t end);
192void vnode_pages_free(struct vnode *vnode);
196
197#endif /* ! _MM_VM_H */
uint64_t paddr_t
Definition mm.h:39
uint64_t vaddr_t
Definition mm.h:40
void vnode_pages_update(struct vnode *vnode, off_t start, off_t end)
Reads a file's cached pages again after a write or a truncation changed a range of it.
Definition vm_object.c:128
void vnode_pages_free(struct vnode *vnode)
Frees a vnode's vm object and its cached pages, when the vnode itself is freed.
Definition vm_object.c:172
struct vm_object * vm_object_vnode(struct vnode *vnode)
Gets a vnode's vm object, making it the first time.
Definition vm_object.c:31
#define MALLOC_DECLARE(type)
Definition kvmem.h:29
unsigned int uint32_t
Definition stddef.h:22
A hash table.
Definition hashmap.h:32
A queue's node, holding one entry.
Definition queue.h:19
A queue.
Definition queue.h:29
An anon: a mapping's private pages, those the process wrote to.
Definition vm.h:119
struct hashmap * arefs
its arefs, by the offset of their page in the mapping
Definition vm.h:121
int flags
its flags (VM_COPY: to be copied before it's written to)
Definition vm.h:127
size_t ref
how many mappings share the anon
Definition vm.h:124
An anon's reference to a page (an aref).
Definition vm.h:107
uint32_t flags
its flags (VM_COPY)
Definition vm.h:115
size_t ref
how many anons have the aref
Definition vm.h:112
struct vm_page * vm_page
the page
Definition vm.h:109
A mapping (map entry): a range of an address space, its permissions, and where its pages come from.
Definition vm.h:66
struct vm_anon * vm_anon
its anon: the pages the process wrote to
Definition vm.h:81
uint32_t flags
its permissions and flags (VM_*)
Definition vm.h:78
paddr_t paddr
physical memory to map all at once (vm_map()), or 0 for pages that come in on page faults
Definition vm.h:69
size_t off
where the mapping starts in its object
Definition vm.h:87
vaddr_t base
where the mapping starts in its address space
Definition vm.h:72
struct vm_object * vm_object
its object: what its other pages come from (a file)
Definition vm.h:84
size_t size
its size
Definition vm.h:75
A vm object: what a mapping's pages come from (a file), and its pages that have been read in.
Definition vm.h:132
size_t ref
how many mappings it has
Definition vm.h:140
void * p
the pager's own data (a file's vnode)
Definition vm.h:146
int type
its kind (VMOBJ_*)
Definition vm.h:137
struct vm_pager * pager
its pager
Definition vm.h:143
struct hashmap * pages
its pages that have been read in, by their offset
Definition vm.h:134
A page of physical memory (pages[] has one for every page of RAM).
Definition vm.h:150
size_t off
its offset in its object or anon
Definition vm.h:155
size_t ref
how many references it has
Definition vm.h:157
struct vm_object * vm_object
the object it belongs to, or NULL
Definition vm.h:153
paddr_t paddr
its physical address
Definition vm.h:151
A vm object's pager: reads its pages in, and writes them out.
Definition vm.h:94
int(*) out(struct vm_object *vm_object, size_t off)
writes out the page at off
Definition vm.h:99
An address space: its mappings, and its page tables.
Definition vm.h:25
struct pmap * pmap
its page tables (the architecture's)
Definition vm.h:27
struct queue vm_entries
its mappings (map entries), in address order
Definition vm.h:30
A file in memory (a vnode): what its inode says, and the kernel's own state.
Definition vfs.h:113
long int off_t
Definition types.h:10
void kvmem_selftest(void)
Runs the allocator's self-test, for the kvmem.test kernel argument, and prints whether it passed.
Definition kvmem.c:264
void vm_unmap_full(struct vm_space *vm_space, struct vm_entry *vm_entry)
Unmaps every page of a map entry's range.
Definition vmm.c:42
void kvmem_setup(void)
Sets up the kernel's allocator at boot, once physical memory and the page tables are ready.
Definition kvmem.c:55
void vm_anon_destroy(struct vm_anon *vm_anon)
Releases an anon's arefs, and the pages of those that no other anon shares.
Definition vm_anon.c:85
int vm_map(struct vm_space *vm_space, struct vm_entry *vm_entry)
Maps a map entry's whole range at once: its physical memory (paddr), or new pages of memory if it has...
Definition vmm.c:29
int vm_space_insert(struct vm_space *vm_space, struct vm_entry *vm_entry)
Adds a map entry to an address space, in address order.
Definition vm_space.c:27
void vm_object_decref(struct vm_object *vm_object)
Drops a reference to a vm object: one mapping of it less.
Definition vm_object.c:38
int vm_object_page_insert(struct vm_object *vm_object, struct vm_page *vm_page)
Adds a page to a vm object, at the page's offset (vm_page->off).
Definition vm_object.c:52
void kvmem_guardtest(void)
Writes one byte past the end of an object, for the kvmem.guard kernel argument: the guard page should...
Definition kvmem.c:278
void vm_entry_destroy(struct vm_entry *vm_entry)
Releases what a map entry holds: its references to its anon and its object.
Definition vm_entry.c:32
void vm_anon_incref(struct vm_anon *vm_anon)
Adds a reference to an anon: another mapping shares it (a fork).
Definition vm_anon.c:117
struct vm_anon * vm_anon_new(void)
Makes a new, empty anon.
Definition vm_anon.c:36
struct vm_entry * vm_entry_new(void)
Makes a new, empty map entry.
Definition vm_entry.c:18
struct vm_space kvm_space
Definition vmm.c:17
struct vm_anon * vm_anon_copy(struct vm_anon *vm_anon)
Makes a copy of an anon that shares its arefs, and so its pages, when a mapping's shared anon is writ...
Definition vm_anon.c:174
int vm_space_fork(struct vm_space *parent, struct vm_space *child)
Copies an address space's mappings into another, for fork().
Definition vm_space.c:152
void vm_object_incref(struct vm_object *vm_object)
Adds a reference to a vm object: one more mapping of it.
Definition vm_object.c:25
void vm_anon_decref(struct vm_anon *vm_anon)
Drops a reference to an anon, and frees it with the last one.
Definition vm_anon.c:130
void vm_space_destroy(struct vm_space *vm_space)
Removes all the mappings of an address space: its entries are released and freed, and its page tables...
Definition vm_space.c:124
struct vm_entry * vm_space_find(struct vm_space *vm_space, vaddr_t vaddr)
Finds the map entry that an address is in.
Definition vm_space.c:99