MOS Source Code
Loading...
Searching...
No Matches
Memory Management

Memory management functions and structures. More...

+ Collaboration diagram for Memory Management:

Topics

 Paging
 A platform-independent interface to map/unmap virtual memory to physical memory.
 
 Physical Memory Manager
 Allocate/free the physical memory, in the form of physical frames.
 

Classes

struct  pagefault_t
 
struct  vmap_t
 

Macros

#define pfn_va(pfn)   ((ptr_t) (platform_info->direct_map_base + (pfn) * (MOS_PAGE_SIZE)))
 
#define va_pfn(va)   ((((ptr_t) (va)) - platform_info->direct_map_base) / MOS_PAGE_SIZE)
 
#define va_phyframe(va)   (&phyframes[va_pfn(va)])
 
#define phyframe_va(frame)   ((ptr_t) pfn_va(phyframe_pfn(frame)))
 
#define pa_va(pa)   ((ptr_t) (pa) + platform_info->direct_map_base)
 
#define mm_free_page(frame)   pmm_free_frames(frame, 1)
 
#define mm_free_pages(frame, npages)   pmm_free_frames(frame, npages)
 

Typedefs

typedef struct _vmap vmap_t
 
typedef vmfault_result_t(* vmfault_handler_t) (vmap_t *vmap, ptr_t fault_addr, pagefault_t *info)
 

Enumerations

enum  vmap_content_t {
  VMAP_UNKNOWN = 0 , VMAP_HEAP , VMAP_STACK , VMAP_FILE ,
  VMAP_MMAP , VMAP_DMA
}
 
enum  vmap_type_t { VMAP_TYPE_PRIVATE = MMAP_PRIVATE , VMAP_TYPE_SHARED = MMAP_SHARED }
 
enum  vmfault_result_t {
  VMFAULT_COMPLETE , VMFAULT_MAP_BACKING_PAGE_RO , VMFAULT_MAP_BACKING_PAGE , VMFAULT_COPY_BACKING_PAGE ,
  VMFAULT_CANNOT_HANDLE = 0xff
}
 

Functions

phyframe_tmm_get_free_page (void)
 
phyframe_tmm_get_free_page_raw (void)
 
phyframe_tmm_get_free_pages (size_t npages)
 
mm_context_tmm_create_context (void)
 Create a user-mode platform-dependent page table.
 
void mm_destroy_context (mm_context_t *table)
 Destroy a user-mode platform-dependent page table.
 
void mm_lock_ctx_pair (mm_context_t *ctx1, mm_context_t *ctx2)
 Lock and unlock a pair of mm_context_t objects.
 
void mm_unlock_ctx_pair (mm_context_t *ctx1, mm_context_t *ctx2)
 
__nodiscard mm_context_tmm_switch_context (mm_context_t *new_ctx)
 
vmap_tvmap_create (mm_context_t *mmctx, ptr_t vaddr, size_t npages)
 Create a vmap object and insert it into the address space.
 
void vmap_destroy (vmap_t *vmap)
 Destroy a vmap object, and unmmap the region.
 
vmap_tvmap_obtain (mm_context_t *mmctx, ptr_t vaddr, size_t *out_offset)
 Get the vmap object for a virtual address.
 
vmap_tvmap_split (vmap_t *vmap, size_t split)
 Split a vmap object into two, at the specified offset.
 
vmap_tvmap_split_for_range (vmap_t *vmap, size_t rstart_pgoff, size_t rend_pgoff)
 Split a vmap to get a vmap object for a range of pages.
 
void vmap_finalise_init (vmap_t *vmap, vmap_content_t content, vmap_type_t type)
 Finalize the initialization of a vmap object.
 
vmfault_result_t mm_resolve_cow_fault (vmap_t *vmap, ptr_t fault_addr, pagefault_t *info)
 Helper function to resolve a copy-on-write fault.
 
void mm_handle_fault (ptr_t fault_addr, pagefault_t *info)
 Handle a page fault.
 

Detailed Description

Memory management functions and structures.

Macro Definition Documentation

◆ pfn_va

◆ va_pfn

#define va_pfn ( va)    ((((ptr_t) (va)) - platform_info->direct_map_base) / MOS_PAGE_SIZE)

Definition at line 77 of file mm.h.

Referenced by limine_entry(), pml2_traverse(), and pml2e_get_or_create_pml1().

◆ va_phyframe

#define va_phyframe ( va)    (&phyframes[va_pfn(va)])

Definition at line 78 of file mm.h.

Referenced by __destroy_page_table(), pipe_close_one_end(), slab_impl_free_page(), and thread_destroy().

◆ phyframe_va

◆ pa_va

#define pa_va ( pa)    ((ptr_t) (pa) + platform_info->direct_map_base)

◆ mm_free_page

#define mm_free_page ( frame)    pmm_free_frames(frame, 1)

Definition at line 86 of file mm.h.

Referenced by __destroy_page_table().

◆ mm_free_pages

#define mm_free_pages ( frame,
npages )   pmm_free_frames(frame, npages)

Typedef Documentation

◆ vmap_t

typedef struct _vmap vmap_t

Definition at line 53 of file mm.h.

◆ vmfault_handler_t

typedef vmfault_result_t(* vmfault_handler_t) (vmap_t *vmap, ptr_t fault_addr, pagefault_t *info)

Definition at line 55 of file mm.h.

Enumeration Type Documentation

◆ vmap_content_t

Enumerator
VMAP_UNKNOWN 
VMAP_HEAP 
VMAP_STACK 
VMAP_FILE 
VMAP_MMAP 
VMAP_DMA 

Definition at line 19 of file mm.h.

◆ vmap_type_t

Enumerator
VMAP_TYPE_PRIVATE 
VMAP_TYPE_SHARED 

Definition at line 29 of file mm.h.

◆ vmfault_result_t

Enumerator
VMFAULT_COMPLETE 

no further action is needed, the page is correctly mapped now

VMFAULT_MAP_BACKING_PAGE_RO 

the caller should map the backing page into the faulting address, and mark it non-writable

VMFAULT_MAP_BACKING_PAGE 

the caller should map the backing page into the faulting address

VMFAULT_COPY_BACKING_PAGE 

the caller should copy the backing page into the faulting address

VMFAULT_CANNOT_HANDLE 

the handler cannot handle this fault

Definition at line 44 of file mm.h.

Function Documentation

◆ mm_get_free_page()

phyframe_t * mm_get_free_page ( void )

◆ mm_get_free_page_raw()

phyframe_t * mm_get_free_page_raw ( void )

Definition at line 31 of file mm.c.

Referenced by mm_get_free_page().

+ Here is the call graph for this function:

◆ mm_get_free_pages()

phyframe_t * mm_get_free_pages ( size_t npages)

Definition at line 52 of file mm.c.

Referenced by pipe_create(), process_do_fork(), register_sysfs_acpi_node(), slab_impl_new_page(), sysfs_expand_buffer(), and thread_new().

+ Here is the call graph for this function:

◆ mm_create_context()

mm_context_t * mm_create_context ( void )

Create a user-mode platform-dependent page table.

Returns
mm_context_t The created page table.
Note
A platform-independent page-map is also created.

Definition at line 64 of file mm.c.

Referenced by process_allocate().

+ Here is the call graph for this function:

◆ mm_destroy_context()

void mm_destroy_context ( mm_context_t * table)

Destroy a user-mode platform-dependent page table.

Parameters
tableThe page table to destroy.
Note
The platform-independent page-map is also destroyed.

Definition at line 80 of file mm.c.

Referenced by process_destroy().

+ Here is the call graph for this function:

◆ mm_lock_ctx_pair()

void mm_lock_ctx_pair ( mm_context_t * ctx1,
mm_context_t * ctx2 )

Lock and unlock a pair of mm_context_t objects.

Parameters
ctx1The first context
ctx2The second context

Definition at line 92 of file mm.c.

Referenced by mm_handle_fault(), mmap_file(), process_do_fork(), and thread_new().

◆ mm_unlock_ctx_pair()

void mm_unlock_ctx_pair ( mm_context_t * ctx1,
mm_context_t * ctx2 )

Definition at line 108 of file mm.c.

Referenced by mm_handle_fault(), mmap_file(), process_do_fork(), and thread_new().

◆ mm_switch_context()

__nodiscard mm_context_t * mm_switch_context ( mm_context_t * new_ctx)

Definition at line 124 of file mm.c.

Referenced by elf_do_fill_process(), and schedule_to_thread().

+ Here is the call graph for this function:

◆ vmap_create()

vmap_t * vmap_create ( mm_context_t * mmctx,
ptr_t vaddr,
size_t npages )

Create a vmap object and insert it into the address space.

Parameters
mmctxThe address space
vaddrStarting virtual address of the region
npagesNumber of pages in the region
Returns
vmap_t* The created vmap object, with its lock held for further initialization.

Definition at line 156 of file mm.c.

Referenced by mm_get_free_vaddr_locked().

+ Here is the call graph for this function:

◆ vmap_destroy()

void vmap_destroy ( vmap_t * vmap)

Destroy a vmap object, and unmmap the region.

Parameters
vmapThe vmap object
Note
The vmap object will be freed.

Definition at line 168 of file mm.c.

Referenced by dmabuf_free(), mmap_file(), munmap(), process_destroy(), process_do_execveat(), and thread_destroy().

+ Here is the call graph for this function:

◆ vmap_obtain()

vmap_t * vmap_obtain ( mm_context_t * mmctx,
ptr_t vaddr,
size_t * out_offset )

Get the vmap object for a virtual address.

Parameters
mmctxThe address space to search
vaddrThe virtual address
out_offsetAn optional pointer to receive the offset of the address in the vmap
Returns
vmap_t* The vmap object, or NULL if not found, with its lock held.

Definition at line 189 of file mm.c.

Referenced by dmabuf_free(), mm_handle_fault(), munmap(), thread_destroy(), thread_new(), vm_protect(), and x86_dump_stack_at().

+ Here is the call graph for this function:

◆ vmap_split()

vmap_t * vmap_split ( vmap_t * vmap,
size_t split )

Split a vmap object into two, at the specified offset.

Parameters
vmapThe vmap object
splitThe number of pages in the first vmap object
Returns
vmap_t* The second vmap object, with its lock held.

Definition at line 209 of file mm.c.

Referenced by thread_new(), vm_protect(), and vmap_split_for_range().

+ Here is the call graph for this function:

◆ vmap_split_for_range()

vmap_t * vmap_split_for_range ( vmap_t * vmap,
size_t rstart_pgoff,
size_t rend_pgoff )

Split a vmap to get a vmap object for a range of pages.

Parameters
vmapThe vmap object to split
rstart_pgoffThe starting page offset
rend_pgoffThe ending page offset
Returns
vmap_t* The vmap object for the range, with its lock held.

|----—|----—|----—| |begin |rstart |rend |end |----—|----—|----—|

Definition at line 231 of file mm.c.

Referenced by munmap().

+ Here is the call graph for this function:

◆ vmap_finalise_init()

void vmap_finalise_init ( vmap_t * vmap,
vmap_content_t content,
vmap_type_t type )

Finalize the initialization of a vmap object.

Parameters
vmapThe vmap object
contentThe content type of the region,
See also
vmap_content_t
Parameters
typeThe fork behavior of the region,
See also
vmap_type_t
Note
The vmap object must be locked, and will be unlocked after this function returns.

Definition at line 254 of file mm.c.

Referenced by mm_map_user_pages(), mmap_anonymous(), mmap_file(), process_do_execveat(), process_do_fork(), process_new(), and thread_new().

+ Here is the call graph for this function:

◆ mm_resolve_cow_fault()

vmfault_result_t mm_resolve_cow_fault ( vmap_t * vmap,
ptr_t fault_addr,
pagefault_t * info )

Helper function to resolve a copy-on-write fault.

Parameters
vmapThe vmap object
fault_addrThe fault address
infoThe page fault info
Returns
vmfault_result_t always VMFAULT_COMPLETE

Definition at line 270 of file mm.c.

Referenced by cow_zod_fault_handler(), and vfs_fault_handler().

+ Here is the call graph for this function:

◆ mm_handle_fault()

void mm_handle_fault ( ptr_t fault_addr,
pagefault_t * info )

Handle a page fault.

Parameters
fault_addrThe fault address
infoThe page fault info

Definition at line 359 of file mm.c.

Referenced by x86_handle_exception().

+ Here is the call graph for this function: