My Kernel v0.1.0
vm.h
1
29#ifndef KERNEL_VM_H
30#define KERNEL_VM_H
31
32#include <kernel/error.h>
33#include <kernel/spinlock.h>
34
35#include <libalgo/linked_list.h>
36#include <utils/bits.h>
37
38struct vmm;
39
40/* We should not be modifying another process's address_space */
41#define AS_ASSERT_OWNED(_as) \
42 WARN_ON((_as) != &kernel_address_space && (_as) != current->process->as);
43
46 // TODO: replace this lock with a R/W one.
49 struct vmm *vmm;
50 paddr_t mmu;
52 vaddr_t data_end;
53 vaddr_t brk_end;
54};
55
57typedef enum vm_flags {
58 VM_NONE = 0,
59 VM_EXEC = BIT(0),
60 VM_READ = BIT(1),
61 VM_WRITE = BIT(2),
62 VM_KERNEL = BIT(3),
63 VM_CLEAR = BIT(4),
64 VM_FIXED = BIT(5),
65} vm_flags_t;
66
67#define VM_KERNEL_RO (VM_KERNEL | VM_READ)
68#define VM_KERNEL_WO (VM_KERNEL | VM_WRITE)
69#define VM_KERNEL_RW (VM_KERNEL | VM_READ | VM_WRITE)
70
71#define VM_USER_RO (VM_READ)
72#define VM_USER_WO (VM_WRITE)
73#define VM_USER_RW (VM_READ | VM_WRITE)
74
81
92 struct vm_segment *(*vm_alloc)(struct address_space *, vaddr_t, size_t,
93 vm_flags_t);
94
108 struct vm_segment *(*vm_alloc_at)(struct address_space *, paddr_t, size_t,
109 vm_flags_t);
110
116 void (*vm_free)(struct address_space *, struct vm_segment *);
117
127 error_t (*vm_resize)(struct address_space *, struct vm_segment *, size_t);
128
135 error_t (*vm_fault)(struct address_space *, struct vm_segment *);
136};
137
149extern struct address_space kernel_address_space;
150
153 node_t this;
154 vaddr_t start;
155 size_t size;
156 u32 flags;
157 const struct vm_segment_driver *driver;
159};
160
162static inline vaddr_t segment_end(const struct vm_segment *segment)
163{
164 return segment->start + segment->size;
165}
166
170struct address_space *address_space_new(void);
171
176error_t address_space_init(struct address_space *);
177
183error_t address_space_clear(struct address_space *);
184
186error_t address_space_destroy(struct address_space *);
187
195error_t address_space_copy_current(struct address_space *);
196
198error_t address_space_load(struct address_space *);
199
206error_t address_space_fault(struct address_space *, void *, bool is_cow);
207
209void *vm_alloc(struct address_space *, size_t, vm_flags_t);
210
214void *vm_alloc_start(struct address_space *, void *, size_t, vm_flags_t);
215
219void *vm_alloc_at(struct address_space *, paddr_t, size_t, vm_flags_t);
220
222void vm_free(struct address_space *, void *);
223
225struct vm_segment *vm_find(const struct address_space *, void *);
226
227#endif /* KERNEL_VM_H */
#define BIT(_n)
Generate the nth power of 2 (nth bit set)
Definition: bits.h:20
Address space.
Definition: vm.h:45
vaddr_t brk_end
Definition: vm.h:53
spinlock_t lock
Definition: vm.h:47
llist_t * segments
Definition: vm.h:51
vaddr_t data_end
Definition: vm.h:52
paddr_t mmu
Definition: vm.h:50
struct vmm * vmm
Definition: vm.h:49
The head of a doubly linked list.
Definition: linked_list.h:43
Intrusive doubly-linked list node.
Definition: linked_list.h:27
Spinlock.
Definition: spinlock.h:29
Segment driver.
Definition: vm.h:80
error_t(* vm_fault)(struct address_space *, struct vm_segment *)
Handle a page fault exception on a known segment.
Definition: vm.h:135
void(* vm_free)(struct address_space *, struct vm_segment *)
Free a contiguous virtual memory segment.
Definition: vm.h:116
error_t(* vm_resize)(struct address_space *, struct vm_segment *, size_t)
Resize a virtual memory segment.
Definition: vm.h:127
Segment of contiguous virtual memory.
Definition: vm.h:152
size_t size
Definition: vm.h:155
vaddr_t start
Definition: vm.h:154
const struct vm_segment_driver * driver
Definition: vm.h:157
u32 flags
Definition: vm.h:156
Virtual Memory Manager.
Definition: vmm.h:164