My Kernel v0.1.0
vmm.h
1
52#ifndef KERNEL_VMM_H
53#define KERNEL_VMM_H
54
55#include <kernel/memory.h>
56#include <kernel/spinlock.h>
57#include <kernel/types.h>
58#include <kernel/vm.h>
59
60#include <libalgo/avl.h>
61#include <libalgo/bitmap.h>
62#include <libalgo/linked_list.h>
63#include <utils/compiler.h>
64#include <utils/container_of.h>
65
66#include <stdbool.h>
67#include <stddef.h>
68
69struct file;
70
91typedef struct vma {
92
95 bool allocated;
101 struct vma_avl {
103 struct avl by_size;
104 } avl;
105
106} vma_t;
107
108/* For simplicity, we will allocate 64B for each VMA structure */
109#define VMA_SIZE (64)
110static_assert(sizeof(vma_t) <= VMA_SIZE, "Update the allocated size for VMA "
111 "structures!");
112
113static inline struct vma *to_vma(struct vm_segment *segment)
114{
115 return container_of(segment, struct vma, segment);
116}
117
119static inline vaddr_t vma_end(const vma_t *vma)
120{
121 return segment_end(&vma->segment);
122}
123
125static inline vaddr_t vma_start(const vma_t *vma)
126{
127 return vma->segment.start;
128}
129
130static inline size_t vma_size(const vma_t *vma)
131{
132 return vma->segment.size;
133}
134
135static inline u32 vma_flags(const vma_t *vma)
136{
137 return vma->segment.flags;
138}
139
164typedef struct vmm {
165
166 vaddr_t start;
167 vaddr_t end;
169 struct address_space *as;
170
173 avl_t *by_address;
174 avl_t *by_size;
175 } vmas;
176
184 BITMAP(reserved, VMM_RESERVED_SIZE / VMA_SIZE);
185
186} vmm_t;
187
189#define MMAP_INVALID NULL
190
197extern vmm_t kernel_vmm;
198
202#define IS_KERNEL_ADDRESS(_addr) ((vaddr_t)(_addr) >= KERNEL_MEMORY_START)
203
204/* Allocate a new VMM structure */
205struct vmm *vmm_new(struct address_space *);
206
216bool vmm_init(vmm_t *vmm, vaddr_t start, vaddr_t end);
217
224void vmm_copy(vmm_t *dst, vmm_t *src);
225
244struct vm_segment *vmm_allocate(vmm_t *, vaddr_t, size_t size, int flags);
245
253void vmm_free(vmm_t *, vaddr_t, size_t length);
254
262error_t vmm_resize(vmm_t *, vma_t *, size_t);
263
268struct vm_segment *vmm_find(const vmm_t *, vaddr_t);
269
275void vmm_clear(vmm_t *vmm);
276
281void vmm_destroy(vmm_t *vmm);
282
290void *map_file(struct file *file, int prot);
291
305error_t unmap_file(struct file *file, void *addr);
306
307#endif /* KERNEL_VMM_H */
308
AVL tree implementation.
Bitmap.
error_t unmap_file(struct file *file, void *addr)
Delete a file's memory mapping.
Definition: vmm.c:696
static vaddr_t vma_end(const vma_t *vma)
Compute the end address of a VMA.
Definition: vmm.h:119
void vmm_free(vmm_t *, vaddr_t, size_t length)
Free a virtual address.
Definition: vmm.c:543
vmm_t kernel_vmm
Global kernel VMM, used to allocate shared kernel addresses.
Definition: vmm.c:45
static vaddr_t vma_start(const vma_t *vma)
Definition: vmm.h:125
struct vm_segment * vmm_find(const vmm_t *, vaddr_t)
Find the VMA to which a virtual address belongs.
Definition: vmm.c:592
void * map_file(struct file *file, int prot)
Map a file into kernel memory.
Definition: vmm.c:669
error_t vmm_resize(vmm_t *, vma_t *, size_t)
Change the size of a VMA.
Definition: vmm.c:565
struct vm_segment * vmm_allocate(vmm_t *, vaddr_t, size_t size, int flags)
Allocate a virtual area of the given size.
Definition: vmm.c:477
void vmm_destroy(vmm_t *vmm)
Free the VMM struct.
Definition: vmm.c:637
void vmm_clear(vmm_t *vmm)
Release all the VMAs inside a VMM instance.
Definition: vmm.c:630
bool vmm_init(vmm_t *vmm, vaddr_t start, vaddr_t end)
Initialize a VMM instance.
Definition: vmm.c:139
void vmm_copy(vmm_t *dst, vmm_t *src)
Copy the content of a VMM instance inside another one.
Definition: vmm.c:647
#define container_of(_ptr, _struct, _field)
Cast a member of a structure out to the containing structure.
Definition: container_of.h:12
Address space.
Definition: vm.h:45
A single node of an AVL tree.
Definition: avl.h:52
Opened file description.
Definition: file.h:29
Spinlock.
Definition: spinlock.h:29
Segment of contiguous virtual memory.
Definition: vm.h:152
size_t size
Definition: vm.h:155
vaddr_t start
Definition: vm.h:154
u32 flags
Definition: vm.h:156
Intrusive AVL tree structures used by the VMM libalgo/avl.H.
Definition: vmm.h:101
struct avl by_address
Definition: vmm.h:102
struct avl by_size
Definition: vmm.h:103
Virtual Memory Area.
Definition: vmm.h:91
bool allocated
Definition: vmm.h:95
struct vm_segment segment
Definition: vmm.h:93
Roots of the AVL trees containing the VMAs.
Definition: vmm.h:172
Virtual Memory Manager.
Definition: vmm.h:164
vaddr_t start
Definition: vmm.h:166
vaddr_t end
Definition: vmm.h:167
BITMAP(reserved, VMM_RESERVED_SIZE/VMA_SIZE)
Bitmap of the available virtual addreses inside the reserved area.
spinlock_t lock
Definition: vmm.h:177