PatchworkOS
Loading...
Searching...
No Matches
vmm.h
Go to the documentation of this file.
1#pragma once
2
4#include <kernel/mem/space.h>
6
7#include <boot/boot_info.h>
8
9#include <sys/list.h>
10#include <sys/proc.h>
11
61#define VMM_KERNEL_BINARY_MAX PML_HIGHER_HALF_END
62#define VMM_KERNEL_BINARY_MIN \
63 PML_INDEX_TO_ADDR(PML_INDEX_AMOUNT - 1, PML4)
64
65#define VMM_KERNEL_STACKS_MAX VMM_KERNEL_BINARY_MIN
66#define VMM_KERNEL_STACKS_MIN PML_INDEX_TO_ADDR(PML_INDEX_AMOUNT - 3, PML4)
67
68#define VMM_KERNEL_HEAP_MAX VMM_KERNEL_STACKS_MIN
69#define VMM_KERNEL_HEAP_MIN PML_INDEX_TO_ADDR(PML_INDEX_AMOUNT - 5, PML4)
70
71#define VMM_IDENTITY_MAPPED_MAX VMM_KERNEL_HEAP_MIN
72#define VMM_IDENTITY_MAPPED_MIN PML_HIGHER_HALF_START
73
74#define VMM_USER_SPACE_MAX PML_LOWER_HALF_END
75#define VMM_USER_SPACE_MIN (0x400000)
76
83#define VMM_IS_PAGE_ALIGNED(addr) (((uintptr_t)(addr) & (PAGE_SIZE - 1)) == 0)
84
98
102#define VMM_MAX_SHOOTDOWN_REQUESTS 16
103
116
126
134void vmm_init(const boot_memory_t* memory, const boot_gop_t* gop, const boot_kernel_t* kernel);
135
144
158
170
177
185
201void* vmm_alloc(space_t* space, void* virtAddr, uint64_t length, pml_flags_t pmlFlags, vmm_alloc_flags_t allocFlags);
202
221void* vmm_map(space_t* space, void* virtAddr, void* physAddr, uint64_t length, pml_flags_t flags,
222 space_callback_func_t func, void* private);
223
241void* vmm_map_pages(space_t* space, void* virtAddr, void** pages, uint64_t pageAmount, pml_flags_t flags,
242 space_callback_func_t func, void* private);
243
258uint64_t vmm_unmap(space_t* space, void* virtAddr, uint64_t length);
259
276uint64_t vmm_protect(space_t* space, void* virtAddr, uint64_t length, pml_flags_t flags);
277
285
void(* space_callback_func_t)(void *private)
Space callback function.
Definition space.h:43
void vmm_cpu_ctx_init(vmm_cpu_ctx_t *ctx)
Initializes a per-CPU VMM context and performs per-CPU VMM initialization.
Definition vmm.c:110
void vmm_shootdown_handler(interrupt_frame_t *frame, cpu_t *self)
TLB shootdown interrupt handler.
Definition vmm.c:458
uint64_t vmm_unmap(space_t *space, void *virtAddr, uint64_t length)
Unmaps virtual memory from a given address space.
Definition vmm.c:339
uint64_t vmm_protect(space_t *space, void *virtAddr, uint64_t length, pml_flags_t flags)
Changes memory protection flags for a virtual memory region in a given address space.
Definition vmm.c:401
void vmm_init(const boot_memory_t *memory, const boot_gop_t *gop, const boot_kernel_t *kernel)
Initializes the Virtual Memory Manager.
Definition vmm.c:41
void * vmm_alloc(space_t *space, void *virtAddr, uint64_t length, pml_flags_t pmlFlags, vmm_alloc_flags_t allocFlags)
Allocates and maps virtual memory in a given address space.
Definition vmm.c:168
space_t * vmm_get_kernel_space(void)
Retrieves the kernel's address space.
Definition vmm.c:138
#define VMM_MAX_SHOOTDOWN_REQUESTS
Maximum number of shootdown requests that can be queued per CPU.
Definition vmm.h:102
void * vmm_map(space_t *space, void *virtAddr, void *physAddr, uint64_t length, pml_flags_t flags, space_callback_func_t func, void *private)
Maps physical memory to virtual memory in a given address space.
Definition vmm.c:231
void vmm_unmap_bootloader_lower_half(thread_t *bootThread)
Unmaps the lower half of the address space after kernel initialization.
Definition vmm.c:129
void vmm_map_bootloader_lower_half(thread_t *bootThread)
Maps the lower half of the address space to the boot thread during kernel initialization.
Definition vmm.c:121
pml_flags_t vmm_prot_to_flags(prot_t prot)
Converts the user space memory protection flags to page table entry flags.
Definition vmm.c:143
void * vmm_map_pages(space_t *space, void *virtAddr, void **pages, uint64_t pageAmount, pml_flags_t flags, space_callback_func_t func, void *private)
Maps an array of physical pages to virtual memory in a given address space.
Definition vmm.c:285
vmm_alloc_flags_t
Flags for vmm_alloc().
Definition vmm.h:122
@ VMM_ALLOC_FAIL_IF_MAPPED
If set and any page is already mapped, fail and set errno to EEXIST.
Definition vmm.h:124
@ VMM_ALLOC_NONE
Definition vmm.h:123
prot_t
Memory protection flags.
Definition proc.h:170
static thread_t bootThread
Definition thread.c:19
boot_gop_t * gop
Definition mem.c:18
static uint64_t pageAmount
Definition pmm.c:42
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
CPU structure.
Definition cpu.h:42
Trap Frame Structure.
Definition interrupt.h:42
A entry in a doubly linked list.
Definition list.h:38
A simple ticket lock implementation.
Definition lock.h:43
A entry in a page table without a specified address or callback ID.
Virtual address space structure.
Definition space.h:79
Thread of execution structure.
Definition thread.h:55
Per-CPU VMM context.
Definition vmm.h:109
uint8_t shootdownCount
Definition vmm.h:112
space_t * currentSpace
Will only be accessed by the owner CPU, so no lock.
Definition vmm.h:114
list_entry_t entry
Used by a space to know which CPUs are using it, protected by the space lock.
Definition vmm.h:110
lock_t lock
Definition vmm.h:113
TLB shootdown structure.
Definition vmm.h:92
uint64_t pageAmount
Definition vmm.h:96
space_t * space
Definition vmm.h:94
void * virtAddr
Definition vmm.h:95
list_entry_t entry
Definition vmm.h:93