LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
vm.h File Reference

(775762e fatcreate: fix GCC detection)

#include "stdint.h"
#include "stdbool.h"
+ Include dependency graph for vm.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  region_t
 

Macros

#define ALLOCATOR_REGION_NULL   (region_t){ .name = "NULL", .start = 0, .end = 0 }
 
#define ALLOCATOR_REGION_USER_STACK   (region_t){ .name = "User stack", .start = 0x00003F0000001000, .end = 0x00003FFFFFFFEFF8 }
 
#define ALLOCATOR_REGION_USER_HARDWARE   (region_t){ .name = "User MMIO", .start = 0x00007F0000000000, .end = 0x00007FFFFFFFDFFF }
 
#define ALLOCATOR_REGION_USER_IOPERM   (region_t){ .name = "User ioperm bitmask", .start = 0x00007FFFFFFFE000, .end = 0x00007FFFFFFFFFFF }
 
#define ALLOCATOR_REGION_SCRATCHPAD   (region_t){ .name = "Kernel scratchpad", .start = 0xFFFFFFFF80000000, .end = 0xFFFFFFFF80FFFFFF }
 
#define ALLOCATOR_REGION_KERNEL_BINARY   (region_t){ .name = "Kernel binary", .start = 0xFFFFFFFF81000000, .end = 0xFFFFFFFF88FFFFFF }
 
#define ALLOCATOR_REGION_SLAB_4K   (region_t){ .name = "4k slab allocator", .start = 0xFFFFFFFF89000000, .end = 0xFFFFFFFF8FFFFFFF }
 
#define ALLOCATOR_REGION_KERNEL_HEAP   (region_t){ .name = "Kernel heap", .start = 0xFFFFFFFF90000000, .end = 0xFFFFFFFFFFFFFFFF }
 
#define ALLOCATOR_REGION_DIRECT_MAPPING   (region_t){ .name = "Physical mapping", .start = 0xFFFF800000000000, .end = 0xFFFF83FFFFFFFFFF }
 

Functions

void init_vm (void)
 
void cleanup_boot_vm (void)
 
void * vm_alloc (size_t size)
 Like malloc but allocates full pages only. 16 byte data overhead.
 
void vm_free (void *ptr)
 the matching free() like function for vm_alloc
 
struct vm_tablevm_context_new (void)
 
struct vm_tablevm_current_context (void)
 
void vm_context_activate (struct vm_table *context)
 
void vm_context_map (struct vm_table *context, ptr_t virt, ptr_t physical, uint8_t pat)
 
void vm_context_unmap (struct vm_table *context, ptr_t virt)
 
ptr_t vm_context_find_free (struct vm_table *context, region_t region, size_t num)
 
int vm_table_get_free_index1 (struct vm_table *table)
 
int vm_table_get_free_index3 (struct vm_table *table, int start, int end)
 
ptr_t vm_context_get_physical_for_virtual (struct vm_table *context, ptr_t virt)
 
ptr_t vm_context_alloc_pages (struct vm_table *context, region_t region, size_t num)
 
void vm_copy_range (struct vm_table *dst_ctx, struct vm_table *src_ctx, ptr_t addr, size_t size)
 
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.
 

Variables

static const uint32_t PageUsageKernel = 1
 
static const uint32_t PageUsageHardware = 2
 Page is used by kernel, userspace otherwise.
 
static const uint32_t PageCoW = 4
 Page accesses MMIO hardware, plain memory otherwise.
 
static const uint32_t PageSharedMemory = 8
 Copy page on fault and give RW access on new page, normal #PF logic otherwise.
 
static const uint32_t PageLocked = 16
 Page is mapped in multiple processes as shared memory.
 
static const uint32_t PageUsagePagingStructure = 32
 Page is locked and cannot be unmapped.
 
static const uint8_t PageSize4KiB = 0
 Page is used as paging structure.
 
static const uint8_t PageSize2MiB = 1
 
static const uint8_t PageSize1GiB = 2
 
struct vm_tableVM_KERNEL_CONTEXT
 

Data Structure Documentation

◆ region_t

struct region_t

Definition at line 7 of file vm.h.

+ Collaboration diagram for region_t:
Data Fields
ptr_t end
char * name
ptr_t start

Macro Definition Documentation

◆ ALLOCATOR_REGION_DIRECT_MAPPING

#define ALLOCATOR_REGION_DIRECT_MAPPING   (region_t){ .name = "Physical mapping", .start = 0xFFFF800000000000, .end = 0xFFFF83FFFFFFFFFF }

Definition at line 29 of file vm.h.

◆ ALLOCATOR_REGION_KERNEL_BINARY

#define ALLOCATOR_REGION_KERNEL_BINARY   (region_t){ .name = "Kernel binary", .start = 0xFFFFFFFF81000000, .end = 0xFFFFFFFF88FFFFFF }

Definition at line 23 of file vm.h.

◆ ALLOCATOR_REGION_KERNEL_HEAP

#define ALLOCATOR_REGION_KERNEL_HEAP   (region_t){ .name = "Kernel heap", .start = 0xFFFFFFFF90000000, .end = 0xFFFFFFFFFFFFFFFF }

Definition at line 25 of file vm.h.

◆ ALLOCATOR_REGION_NULL

#define ALLOCATOR_REGION_NULL   (region_t){ .name = "NULL", .start = 0, .end = 0 }

Definition at line 13 of file vm.h.

◆ ALLOCATOR_REGION_SCRATCHPAD

#define ALLOCATOR_REGION_SCRATCHPAD   (region_t){ .name = "Kernel scratchpad", .start = 0xFFFFFFFF80000000, .end = 0xFFFFFFFF80FFFFFF }

Definition at line 22 of file vm.h.

◆ ALLOCATOR_REGION_SLAB_4K

#define ALLOCATOR_REGION_SLAB_4K   (region_t){ .name = "4k slab allocator", .start = 0xFFFFFFFF89000000, .end = 0xFFFFFFFF8FFFFFFF }

Definition at line 24 of file vm.h.

◆ ALLOCATOR_REGION_USER_HARDWARE

#define ALLOCATOR_REGION_USER_HARDWARE   (region_t){ .name = "User MMIO", .start = 0x00007F0000000000, .end = 0x00007FFFFFFFDFFF }

Definition at line 19 of file vm.h.

◆ ALLOCATOR_REGION_USER_IOPERM

#define ALLOCATOR_REGION_USER_IOPERM   (region_t){ .name = "User ioperm bitmask", .start = 0x00007FFFFFFFE000, .end = 0x00007FFFFFFFFFFF }

Definition at line 20 of file vm.h.

◆ ALLOCATOR_REGION_USER_STACK

#define ALLOCATOR_REGION_USER_STACK   (region_t){ .name = "User stack", .start = 0x00003F0000001000, .end = 0x00003FFFFFFFEFF8 }

Definition at line 18 of file vm.h.

Function Documentation

◆ cleanup_boot_vm()

void cleanup_boot_vm ( void  )

Definition at line 334 of file vm.c.

References BASE_TO_TABLE, vm_table::entries, GiB, vm_table_entry::huge, KiB, logi, MiB, vm_table_entry::next_base, vm_table_entry::present, and VM_KERNEL_CONTEXT.

+ Here is the caller graph for this function:

◆ init_vm()

void init_vm ( void  )

Definition at line 228 of file vm.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, ALLOCATOR_REGION_KERNEL_HEAP, BASE_TO_TABLE, vm_table::entries, vm_table_entry::global, vm_table_entry::huge, kernel_alloc, KiB, logd, logw, memcpy(), memset(), vm_table_entry::next_base, page_descriptors, PageSize1GiB, PageSize2MiB, PageSize4KiB, PageUsageKernel, PageUsagePagingStructure, PML4_INDEX, vm_table_entry::present, read_msr(), tpa_new(), vm_context_alloc_pages(), VM_KERNEL_CONTEXT, vm_page_alloc(), vm_ref_inc(), vm_set_page_descriptor(), vm_setup_direct_mapping_init(), and write_msr().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_alloc()

void * vm_alloc ( size_t  size)

Like malloc but allocates full pages only. 16 byte data overhead.

Definition at line 697 of file vm.c.

References ALLOCATOR_REGION_KERNEL_HEAP, size, vm_context_alloc_pages(), and VM_KERNEL_CONTEXT.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_context_activate()

void vm_context_activate ( struct vm_table context)

Definition at line 388 of file vm.c.

References load_cr3(), vm_context_get_physical_for_virtual(), and VM_KERNEL_CONTEXT.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_context_alloc_pages()

ptr_t vm_context_alloc_pages ( struct vm_table context,
region_t  region,
size_t  num 
)

Definition at line 560 of file vm.c.

References mm_alloc_pages(), num, vm_context_find_free(), and vm_context_map().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_context_find_free()

ptr_t vm_context_find_free ( struct vm_table context,
region_t  region,
size_t  num 
)

Definition at line 531 of file vm.c.

References region_t::end, num, region_t::start, and vm_context_page_present().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_context_get_physical_for_virtual()

ptr_t vm_context_get_physical_for_virtual ( struct vm_table context,
ptr_t  virt 
)

Definition at line 475 of file vm.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, BASE_TO_TABLE, vm_table::entries, huge, next_base, PD_INDEX, PDP_INDEX, PML4_INDEX, present, and PT_INDEX.

+ Here is the caller graph for this function:

◆ vm_context_map()

void vm_context_map ( struct vm_table context,
ptr_t  virt,
ptr_t  physical,
uint8_t  pat 
)

Definition at line 406 of file vm.c.

References BASE_TO_TABLE, vm_table::entries, vm_table_entry::huge, next_base, vm_table_entry::next_base, vm_table_entry::pat0, vm_table_entry::pat1, PD_INDEX, PDP_INDEX, PML4_INDEX, vm_table_entry::present, PT_INDEX, vm_table_entry::userspace, vm_ensure_table(), and vm_table_entry::writeable.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_context_new()

struct vm_table * vm_context_new ( void  )

Definition at line 381 of file vm.c.

References ALLOCATOR_REGION_KERNEL_HEAP, memcpy(), vm_context_alloc_pages(), and VM_KERNEL_CONTEXT.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_context_unmap()

void vm_context_unmap ( struct vm_table context,
ptr_t  virt 
)

Definition at line 427 of file vm.c.

References BASE_TO_TABLE, vm_table::entries, vm_table_entry::next_base, PD_INDEX, PDP_INDEX, PML4_INDEX, vm_table_entry::present, PT_INDEX, vm_table_entry::userspace, and vm_table_entry::writeable.

+ Here is the caller graph for this function:

◆ vm_copy_range()

void vm_copy_range ( struct vm_table dst_ctx,
struct vm_table src_ctx,
ptr_t  addr,
size_t  size 
)

Definition at line 597 of file vm.c.

References addr, BASE_TO_DIRECT_MAPPED, BASE_TO_TABLE, vm_table::entries, GiB, vm_table_entry::huge, KiB, logw, memcpy(), MiB, mm_alloc_pages(), vm_table_entry::next_base, panic_message(), PD_INDEX, PDP_INDEX, PML4_INDEX, vm_table_entry::present, PT_INDEX, size, and vm_ensure_table().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_current_context()

struct vm_table * vm_current_context ( void  )

Definition at line 743 of file vm.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, and vm_direct_mapping_initialized.

+ Here is the caller graph for this function:

◆ vm_free()

void vm_free ( void *  ptr)

the matching free() like function for vm_alloc

Todo:
clear and unmap pages

Definition at line 707 of file vm.c.

References MM_FREE, mm_mark_physical_pages(), panic_message(), size, vm_context_get_physical_for_virtual(), vm_context_unmap(), and VM_KERNEL_CONTEXT.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_map_hardware()

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 at line 755 of file vm.c.

References ALLOCATOR_REGION_USER_HARDWARE, vm_context_find_free(), vm_context_map(), and vm_current_context().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vm_table_get_free_index1()

int vm_table_get_free_index1 ( struct vm_table table)

Definition at line 461 of file vm.c.

References vm_table_get_free_index3().

+ Here is the call graph for this function:

◆ vm_table_get_free_index3()

int vm_table_get_free_index3 ( struct vm_table table,
int  start,
int  end 
)

Definition at line 465 of file vm.c.

References vm_table::entries, and vm_table_entry::present.

+ Here is the caller graph for this function:

Variable Documentation

◆ PageCoW

const uint32_t PageCoW = 4
static

Page accesses MMIO hardware, plain memory otherwise.

Definition at line 34 of file vm.h.

◆ PageLocked

const uint32_t PageLocked = 16
static

Page is mapped in multiple processes as shared memory.

Definition at line 36 of file vm.h.

◆ PageSharedMemory

const uint32_t PageSharedMemory = 8
static

Copy page on fault and give RW access on new page, normal #PF logic otherwise.

Definition at line 35 of file vm.h.

◆ PageSize1GiB

const uint8_t PageSize1GiB = 2
static

Definition at line 42 of file vm.h.

◆ PageSize2MiB

const uint8_t PageSize2MiB = 1
static

Definition at line 41 of file vm.h.

◆ PageSize4KiB

const uint8_t PageSize4KiB = 0
static

Page is used as paging structure.

Definition at line 40 of file vm.h.

◆ PageUsageHardware

const uint32_t PageUsageHardware = 2
static

Page is used by kernel, userspace otherwise.

Definition at line 33 of file vm.h.

◆ PageUsageKernel

const uint32_t PageUsageKernel = 1
static

Definition at line 32 of file vm.h.

◆ PageUsagePagingStructure

const uint32_t PageUsagePagingStructure = 32
static

Page is locked and cannot be unmapped.

Definition at line 37 of file vm.h.

◆ VM_KERNEL_CONTEXT

struct vm_table* VM_KERNEL_CONTEXT
extern

Definition at line 16 of file vm.c.