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),
66 /* Caching policies. */
67 VM_CACHE_UC = BIT(6),
68 VM_CACHE_WC = BIT(7),
69 VM_CACHE_WT = BIT(8),
70 VM_CACHE_WB = BIT(9),
71} vm_flags_t;
72
73#define VM_KERNEL_RO (VM_KERNEL | VM_READ)
74#define VM_KERNEL_WO (VM_KERNEL | VM_WRITE)
75#define VM_KERNEL_RW (VM_KERNEL | VM_READ | VM_WRITE)
76
77#define VM_USER_RO (VM_READ)
78#define VM_USER_WO (VM_WRITE)
79#define VM_USER_RW (VM_READ | VM_WRITE)
80
81/*
82 * Memory-mapped I/O should be mapped as uncacheable since writes/read can
83 * have side-effects and should not be combined or delayed.
84 */
85#define VM_IOMEM (VM_KERNEL_RW | VM_CACHE_UC)
86
87#define VM_CACHE_MASK (VM_CACHE_UC | VM_CACHE_WT | VM_CACHE_WB | VM_CACHE_WC)
88
95
106 struct vm_segment *(*vm_alloc)(struct address_space *, vaddr_t, size_t,
107 vm_flags_t);
108
122 struct vm_segment *(*vm_alloc_at)(struct address_space *, paddr_t, size_t,
123 vm_flags_t);
124
130 void (*vm_free)(struct address_space *, struct vm_segment *);
131
141 error_t (*vm_resize)(struct address_space *, struct vm_segment *, size_t);
142
149 error_t (*vm_fault)(struct address_space *, struct vm_segment *);
150
152 error_t (*vm_map)(struct address_space *, struct vm_segment *, vm_flags_t);
153
155 error_t (*vm_set_policy)(struct address_space *, struct vm_segment *,
156 vm_flags_t policy);
157};
158
170extern struct address_space kernel_address_space;
171
174 node_t this;
175 vaddr_t start;
176 size_t size;
177 u32 flags;
178 const struct vm_segment_driver *driver;
180};
181
183static inline vaddr_t segment_end(const struct vm_segment *segment)
184{
185 return segment->start + segment->size;
186}
187
191struct address_space *address_space_new(void);
192
197error_t address_space_init(struct address_space *);
198
204error_t address_space_clear(struct address_space *);
205
207error_t address_space_destroy(struct address_space *);
208
216error_t address_space_copy_current(struct address_space *);
217
219error_t address_space_load(struct address_space *);
220
227error_t address_space_fault(struct address_space *, void *, bool is_cow);
228
230void *vm_alloc(struct address_space *, size_t, vm_flags_t);
231
235void *vm_alloc_start(struct address_space *, void *, size_t, vm_flags_t);
236
240void *vm_alloc_at(struct address_space *, paddr_t, size_t, vm_flags_t);
241
243void vm_free(struct address_space *, void *);
244
246struct vm_segment *vm_find(const struct address_space *, void *);
247
252error_t vm_map(struct address_space *, void *);
253
255error_t vm_set_policy(struct address_space *, void *, vm_flags_t policy);
256
257#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:94
error_t(* vm_fault)(struct address_space *, struct vm_segment *)
Handle a page fault exception on a known segment.
Definition: vm.h:149
error_t(* vm_set_policy)(struct address_space *, struct vm_segment *, vm_flags_t policy)
Configure the effective caching policy for a segment.
Definition: vm.h:155
error_t(* vm_map)(struct address_space *, struct vm_segment *, vm_flags_t)
Map this segment onto a physical address.
Definition: vm.h:152
void(* vm_free)(struct address_space *, struct vm_segment *)
Free a contiguous virtual memory segment.
Definition: vm.h:130
error_t(* vm_resize)(struct address_space *, struct vm_segment *, size_t)
Resize a virtual memory segment.
Definition: vm.h:141
Segment of contiguous virtual memory.
Definition: vm.h:173
size_t size
Definition: vm.h:176
vaddr_t start
Definition: vm.h:175
const struct vm_segment_driver * driver
Definition: vm.h:178
u32 flags
Definition: vm.h:177
Virtual Memory Manager.
Definition: vmm.h:164