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

(d392dda docs: fix and extend building.md)

#include <vm.h>
#include <mm.h>
#include <string.h>
#include <log.h>
#include <slab.h>
#include <panic.h>
#include <tpa.h>
#include <msr.h>
#include <unused_param.h>
+ Include dependency graph for vm.c:

Go to the source code of this file.

Data Structures

struct  page_descriptor
 
struct  vm_table_entry
 A single entry in a paging table. More...
 
struct  vm_table
 A paging table, when this is a PML4 it may also be called context. More...
 

Macros

#define BASE_TO_PHYS(x)   ((char*)(x << 12))
 
#define BASE_TO_DIRECT_MAPPED(x)   ((vm_direct_mapping_initialized ? ALLOCATOR_REGION_DIRECT_MAPPING.start : 0) + BASE_TO_PHYS(x))
 
#define BASE_TO_TABLE(x)   ((struct vm_table*)BASE_TO_DIRECT_MAPPED(x))
 
#define PML4_INDEX(x)   (((x) >> 39) & 0x1FF)
 
#define PDP_INDEX(x)   (((x) >> 30) & 0x1FF)
 
#define PD_INDEX(x)   (((x) >> 21) & 0x1FF)
 
#define PT_INDEX(x)   (((x) >> 12) & 0x1FF)
 

Functions

void load_cr3 (ptr_t cr3)
 
struct vm_table_entry __attribute__ ((packed))
 
void vm_setup_direct_mapping_init (struct vm_table *context)
 
struct page_descriptorvm_page_descriptor (ptr_t physical)
 
void vm_set_page_descriptor (ptr_t physical, uint32_t flags, uint8_t size)
 
void vm_ref_inc (ptr_t physical)
 
void vm_ref_dec (ptr_t physical)
 
void * vm_page_alloc (uint32_t flags, uint8_t size)
 
void init_vm (void)
 
void cleanup_boot_vm (void)
 
struct vm_tablevm_context_new (void)
 
void vm_context_activate (struct vm_table *context)
 
static void vm_ensure_table (struct vm_table *table, uint16_t index)
 
void vm_context_map (struct vm_table *pml4, ptr_t virtual, ptr_t physical, uint8_t pat)
 
void vm_context_unmap (struct vm_table *context, ptr_t virtual)
 
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 virtual)
 
bool vm_context_page_present (struct vm_table *context, ptr_t virtual)
 
ptr_t vm_context_find_free (struct vm_table *context, region_t region, size_t num)
 
ptr_t vm_context_alloc_pages (struct vm_table *context, region_t region, size_t num)
 
void vm_copy_page (struct vm_table *dst_ctx, ptr_t dst, struct vm_table *src_ctx, ptr_t src)
 
void vm_copy_range (struct vm_table *dst, struct vm_table *src, ptr_t addr, size_t size)
 
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
 
static void * kernel_alloc_fn (allocator_t *alloc, size_t size)
 
static void kernel_dealloc_fn (allocator_t *alloc, void *ptr)
 
struct vm_tablevm_current_context (void)
 
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 bool vm_direct_mapping_initialized = false
 
static tpa_tpage_descriptors = 0
 
struct vm_tableVM_KERNEL_CONTEXT
 
unsigned int present
 
unsigned int writeable
 
unsigned int userspace
 
unsigned int pat0
 
unsigned int pat1
 
unsigned int accessed
 
unsigned int dirty
 
unsigned int huge
 
unsigned int global
 
unsigned int available
 
unsigned long next_base
 
unsigned int available2
 
unsigned int nx
 
struct vm_table_entry entries [512]
 
allocator_t kernel_alloc
 

Data Structure Documentation

◆ page_descriptor

struct page_descriptor

Definition at line 18 of file vm.c.

+ Collaboration diagram for page_descriptor:
Data Fields
uint32_t flags: 30
uint32_t refcount
uint8_t size: 2

◆ vm_table_entry

struct vm_table_entry

A single entry in a paging table.

Definition at line 25 of file vm.c.

+ Collaboration diagram for vm_table_entry:
Data Fields
unsigned int accessed: 1
unsigned int available: 3
unsigned int available2: 11
unsigned int dirty: 1
unsigned int global: 1
unsigned int huge: 1
unsigned long next_base: 40
unsigned int nx: 1
unsigned int pat0: 1
unsigned int pat1: 1
unsigned int present: 1
unsigned int userspace: 1
unsigned int writeable: 1

◆ vm_table

struct vm_table

A paging table, when this is a PML4 it may also be called context.

Definition at line 42 of file vm.c.

+ Collaboration diagram for vm_table:
Data Fields
struct vm_table_entry entries[512]

Macro Definition Documentation

◆ BASE_TO_DIRECT_MAPPED

#define BASE_TO_DIRECT_MAPPED (   x)    ((vm_direct_mapping_initialized ? ALLOCATOR_REGION_DIRECT_MAPPING.start : 0) + BASE_TO_PHYS(x))

Definition at line 47 of file vm.c.

◆ BASE_TO_PHYS

#define BASE_TO_PHYS (   x)    ((char*)(x << 12))

Definition at line 46 of file vm.c.

◆ BASE_TO_TABLE

#define BASE_TO_TABLE (   x)    ((struct vm_table*)BASE_TO_DIRECT_MAPPED(x))

Definition at line 48 of file vm.c.

◆ PD_INDEX

#define PD_INDEX (   x)    (((x) >> 21) & 0x1FF)

Definition at line 52 of file vm.c.

◆ PDP_INDEX

#define PDP_INDEX (   x)    (((x) >> 30) & 0x1FF)

Definition at line 51 of file vm.c.

◆ PML4_INDEX

#define PML4_INDEX (   x)    (((x) >> 39) & 0x1FF)

Definition at line 50 of file vm.c.

◆ PT_INDEX

#define PT_INDEX (   x)    (((x) >> 12) & 0x1FF)

Definition at line 53 of file vm.c.

Function Documentation

◆ __attribute__()

struct vm_table_entry __attribute__ ( (packed)  )

Definition at line 28 of file hpet.c.

◆ 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:

◆ kernel_alloc_fn()

static void * kernel_alloc_fn ( allocator_t alloc,
size_t  size 
)
static

Definition at line 726 of file vm.c.

References alloc, size, allocator::tag, and vm_alloc().

+ Here is the call graph for this function:

◆ kernel_dealloc_fn()

static void kernel_dealloc_fn ( allocator_t alloc,
void *  ptr 
)
static

Definition at line 731 of file vm.c.

References alloc, size, allocator::tag, and vm_free().

+ Here is the call graph for this function:

◆ load_cr3()

void load_cr3 ( ptr_t  cr3)
+ 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  virtual 
)

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 pml4,
ptr_t  virtual,
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_page_present()

bool vm_context_page_present ( struct vm_table context,
ptr_t  virtual 
)

Definition at line 505 of file vm.c.

References 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_unmap()

void vm_context_unmap ( struct vm_table context,
ptr_t  virtual 
)

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_page()

void vm_copy_page ( struct vm_table dst_ctx,
ptr_t  dst,
struct vm_table src_ctx,
ptr_t  src 
)

Definition at line 574 of file vm.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, memcpy(), mm_alloc_pages(), panic_message(), vm_context_get_physical_for_virtual(), vm_context_map(), and vm_context_page_present().

+ Here is the call graph for this function:

◆ vm_copy_range()

void vm_copy_range ( struct vm_table dst,
struct vm_table src,
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_ensure_table()

static void vm_ensure_table ( struct vm_table table,
uint16_t  index 
)
static

Definition at line 392 of file vm.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, vm_table::entries, entry, memset(), PageSize4KiB, PageUsageKernel, PageUsagePagingStructure, and vm_page_alloc().

+ Here is the call graph for this function:
+ 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_page_alloc()

void * vm_page_alloc ( uint32_t  flags,
uint8_t  size 
)

Definition at line 205 of file vm.c.

References flags, GiB, KiB, MiB, mm_alloc_pages(), PageSize1GiB, PageSize2MiB, PageSize4KiB, size, and UNUSED_PARAM.

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

◆ vm_page_descriptor()

struct page_descriptor * vm_page_descriptor ( ptr_t  physical)

Definition at line 159 of file vm.c.

References page_descriptors, PageSize4KiB, PageUsageKernel, panic_message(), page_descriptor::size, tpa_get(), and tpa_set().

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

◆ vm_ref_dec()

void vm_ref_dec ( ptr_t  physical)

Definition at line 196 of file vm.c.

References page_descriptors, page_descriptor::refcount, tpa_set(), and vm_page_descriptor().

+ Here is the call graph for this function:

◆ vm_ref_inc()

void vm_ref_inc ( ptr_t  physical)

Definition at line 191 of file vm.c.

References page_descriptor::refcount, and vm_page_descriptor().

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

◆ vm_set_page_descriptor()

void vm_set_page_descriptor ( ptr_t  physical,
uint32_t  flags,
uint8_t  size 
)

Definition at line 180 of file vm.c.

References flags, page_descriptor::flags, panic_message(), size, page_descriptor::size, and vm_page_descriptor().

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

◆ vm_setup_direct_mapping_init()

void vm_setup_direct_mapping_init ( struct vm_table context)

Definition at line 55 of file vm.c.

References ALLOCATOR_REGION_DIRECT_MAPPING, ALLOCATOR_REGION_SCRATCHPAD, vm_table::entries, GiB, vm_table_entry::huge, KiB, logd, logw, memset(), MiB, mm_highest_address(), page_descriptors, PD_INDEX, PDP_INDEX, PML4_INDEX, vm_table_entry::present, slab_alloc(), vm_context_get_physical_for_virtual(), and vm_direct_mapping_initialized.

+ 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

◆ accessed

unsigned int accessed

Definition at line 5 of file vm.c.

◆ available

unsigned int available

Definition at line 9 of file vm.c.

◆ available2

unsigned int available2

Definition at line 11 of file vm.c.

◆ dirty

unsigned int dirty

Definition at line 6 of file vm.c.

◆ entries

struct vm_table_entry entries[512]

Definition at line 0 of file vm.c.

◆ global

unsigned int global

Definition at line 8 of file vm.c.

◆ huge

unsigned int huge

Definition at line 7 of file vm.c.

◆ kernel_alloc

allocator_t kernel_alloc
Initial value:
.alloc = kernel_alloc_fn,
.dealloc = kernel_dealloc_fn,
.tag = 0,
}
struct allocator allocator_t
static void * kernel_alloc_fn(allocator_t *alloc, size_t size)
Definition vm.c:726
static void kernel_dealloc_fn(allocator_t *alloc, void *ptr)
Definition vm.c:731

Definition at line 737 of file vm.c.

◆ next_base

unsigned long next_base

Definition at line 10 of file vm.c.

◆ nx

unsigned int nx

Definition at line 12 of file vm.c.

◆ page_descriptors

tpa_t* page_descriptors = 0
static

Definition at line 15 of file vm.c.

◆ pat0

unsigned int pat0

Definition at line 3 of file vm.c.

◆ pat1

unsigned int pat1

Definition at line 4 of file vm.c.

◆ present

unsigned int present

Definition at line 0 of file vm.c.

◆ userspace

unsigned int userspace

Definition at line 2 of file vm.c.

◆ vm_direct_mapping_initialized

bool vm_direct_mapping_initialized = false
static

Definition at line 14 of file vm.c.

◆ VM_KERNEL_CONTEXT

struct vm_table* VM_KERNEL_CONTEXT

Definition at line 16 of file vm.c.

◆ writeable

unsigned int writeable

Definition at line 1 of file vm.c.