LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
vm.h
Go to the documentation of this file.
1#ifndef _VM_H_INCLUDED
2#define _VM_H_INCLUDED
3
4#include "stdint.h"
5#include "stdbool.h"
6
7typedef struct {
8 char* name;
11} region_t;
12
13#define ALLOCATOR_REGION_NULL (region_t){ .name = "NULL", .start = 0, .end = 0 }
14
15// x86-64 SysV ABI defines the stack has to be 16-bytes aligned "right before the call instruction", which
16// pushes the return address onto the stack. Since we do not enter user code via call, we emulate this by
17// misaligning by 8 bytes.
18#define ALLOCATOR_REGION_USER_STACK (region_t){ .name = "User stack", .start = 0x00003F0000001000, .end = 0x00003FFFFFFFEFF8 }
19#define ALLOCATOR_REGION_USER_HARDWARE (region_t){ .name = "User MMIO", .start = 0x00007F0000000000, .end = 0x00007FFFFFFFDFFF }
20#define ALLOCATOR_REGION_USER_IOPERM (region_t){ .name = "User ioperm bitmask", .start = 0x00007FFFFFFFE000, .end = 0x00007FFFFFFFFFFF }
21
22#define ALLOCATOR_REGION_SCRATCHPAD (region_t){ .name = "Kernel scratchpad", .start = 0xFFFFFFFF80000000, .end = 0xFFFFFFFF80FFFFFF }
23#define ALLOCATOR_REGION_KERNEL_BINARY (region_t){ .name = "Kernel binary", .start = 0xFFFFFFFF81000000, .end = 0xFFFFFFFF88FFFFFF }
24#define ALLOCATOR_REGION_SLAB_4K (region_t){ .name = "4k slab allocator", .start = 0xFFFFFFFF89000000, .end = 0xFFFFFFFF8FFFFFFF }
25#define ALLOCATOR_REGION_KERNEL_HEAP (region_t){ .name = "Kernel heap", .start = 0xFFFFFFFF90000000, .end = 0xFFFFFFFFFFFFFFFF }
26
27// this one must be PML4 aligned! (PDP, PD and PT indexes must be zero for the start and 511 for the end)
28// must also be the last region
29#define ALLOCATOR_REGION_DIRECT_MAPPING (region_t){ .name = "Physical mapping", .start = 0xFFFF800000000000, .end = 0xFFFF83FFFFFFFFFF }
30
31// Some usage flags for pages
32static const uint32_t PageUsageKernel = 1;
33static const uint32_t PageUsageHardware = 2;
34static const uint32_t PageCoW = 4;
35static const uint32_t PageSharedMemory = 8;
36static const uint32_t PageLocked = 16;
38
39// Sizes a page can have
40static const uint8_t PageSize4KiB = 0;
41static const uint8_t PageSize2MiB = 1;
42static const uint8_t PageSize1GiB = 2;
43
44struct vm_table;
45extern struct vm_table* VM_KERNEL_CONTEXT;
46
47void init_vm(void);
48void cleanup_boot_vm(void);
49
51void* vm_alloc(size_t size);
52
54void vm_free(void* ptr);
55
56struct vm_table* vm_context_new(void);
57
58struct vm_table* vm_current_context(void);
59
60void vm_context_activate(struct vm_table* context);
61
62void vm_context_map(struct vm_table* context, ptr_t virt, ptr_t physical, uint8_t pat);
63void vm_context_unmap(struct vm_table* context, ptr_t virt);
64
65ptr_t vm_context_find_free(struct vm_table* context, region_t region, size_t num);
66
67int vm_table_get_free_index1(struct vm_table* table);
68int vm_table_get_free_index3(struct vm_table* table, int start, int end);
69
71
72ptr_t vm_context_alloc_pages(struct vm_table* context, region_t region, size_t num);
73
74void vm_copy_range(struct vm_table* dst_ctx, struct vm_table* src_ctx, ptr_t addr, size_t size);
75
77ptr_t vm_map_hardware(ptr_t hw, size_t len);
78
79#endif
uint64_t ptr_t
Definition arch.h:17
unsigned int uint32_t
Definition arch.h:11
unsigned char uint8_t
Definition arch.h:5
void vm_context_unmap(struct vm_table *context, ptr_t virt)
Definition vm.c:427
static const uint8_t PageSize2MiB
Definition vm.h:41
void vm_context_map(struct vm_table *context, ptr_t virt, ptr_t physical, uint8_t pat)
Definition vm.c:406
void cleanup_boot_vm(void)
Definition vm.c:334
ptr_t vm_context_alloc_pages(struct vm_table *context, region_t region, size_t num)
Definition vm.c:560
struct vm_table * vm_current_context(void)
Definition vm.c:743
ptr_t vm_context_find_free(struct vm_table *context, region_t region, size_t num)
Definition vm.c:531
static const uint32_t PageLocked
Page is mapped in multiple processes as shared memory.
Definition vm.h:36
static const uint32_t PageCoW
Page accesses MMIO hardware, plain memory otherwise.
Definition vm.h:34
ptr_t vm_map_hardware(ptr_t hw, size_t len)
Map a given memory area in the currently running userspace process at a random location.
Definition vm.c:755
char * name
Definition vm.h:8
static const uint32_t PageUsagePagingStructure
Page is locked and cannot be unmapped.
Definition vm.h:37
static const uint32_t PageSharedMemory
Copy page on fault and give RW access on new page, normal #PF logic otherwise.
Definition vm.h:35
void vm_copy_range(struct vm_table *dst_ctx, struct vm_table *src_ctx, ptr_t addr, size_t size)
Definition vm.c:597
static const uint32_t PageUsageHardware
Page is used by kernel, userspace otherwise.
Definition vm.h:33
static const uint32_t PageUsageKernel
Definition vm.h:32
ptr_t start
Definition vm.h:9
static const uint8_t PageSize4KiB
Page is used as paging structure.
Definition vm.h:40
void vm_context_activate(struct vm_table *context)
Definition vm.c:388
struct vm_table * vm_context_new(void)
Definition vm.c:381
void init_vm(void)
Definition vm.c:228
int vm_table_get_free_index1(struct vm_table *table)
Definition vm.c:461
int vm_table_get_free_index3(struct vm_table *table, int start, int end)
Definition vm.c:465
void * vm_alloc(size_t size)
Like malloc but allocates full pages only. 16 byte data overhead.
Definition vm.c:697
struct vm_table * VM_KERNEL_CONTEXT
Definition vm.c:16
void vm_free(void *ptr)
the matching free() like function for vm_alloc
Definition vm.c:707
ptr_t vm_context_get_physical_for_virtual(struct vm_table *context, ptr_t virt)
Definition vm.c:475
ptr_t end
Definition vm.h:10
static const uint8_t PageSize1GiB
Definition vm.h:42
Definition vm.h:7
ptr_t addr
Definition elf.h:3
uint16_t size
Size of the loaded file.
Definition loader.h:5
static uint16_t num
Definition syscalls.h:126
A paging table, when this is a PML4 it may also be called context.
Definition vm.c:42