MOS Source Code
Loading...
Searching...
No Matches
mm.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "mos/io/io.h"
6#include "mos/mm/mmstat.h"
9
12#include <mos/mm/mm_types.h>
13
20typedef enum
21{
23 VMAP_STACK, // stack (user)
24 VMAP_FILE, // file mapping
25 VMAP_MMAP, // mmap mapping
26 VMAP_DMA, // DMA mapping
28
29typedef enum
30{
31 VMAP_TYPE_PRIVATE = MMAP_PRIVATE, // there will be distinct copies of the memory region in the child process
32 VMAP_TYPE_SHARED = MMAP_SHARED, // the memory region will be shared between the parent and child processes
34
35typedef struct
36{
37 bool is_present, is_write, is_user, is_exec;
43
52
53typedef struct _vmap vmap_t;
54
55typedef vmfault_result_t (*vmfault_handler_t)(vmap_t *vmap, ptr_t fault_addr, pagefault_t *info);
56
57typedef struct _vmap
58{
61
62 ptr_t vaddr; // virtual addresses
63 size_t npages;
64 vm_flags vmflags; // the expected flags for the region, regardless of the copy-on-write state
66
67 io_t *io; // the io object that (possibly) backs this vmap
68 off_t io_offset; // the offset in the io object, page-aligned
69
74} vmap_t;
75
76#define pfn_va(pfn) ((ptr_t) (platform_info->direct_map_base + (pfn) * (MOS_PAGE_SIZE)))
77#define va_pfn(va) ((((ptr_t) (va)) - platform_info->direct_map_base) / MOS_PAGE_SIZE)
78#define va_phyframe(va) (&phyframes[va_pfn(va)])
79#define phyframe_va(frame) ((ptr_t) pfn_va(phyframe_pfn(frame)))
80#define pa_va(pa) ((ptr_t) (pa) + platform_info->direct_map_base)
81
83
86phyframe_t *mm_get_free_pages(size_t npages);
87
88#define mm_free_page(frame) pmm_free_frames(frame, 1)
89#define mm_free_pages(frame, npages) pmm_free_frames(frame, npages)
90
97
104
113
115
124vmap_t *vmap_create(mm_context_t *mmctx, ptr_t vaddr, size_t npages);
125
132void vmap_destroy(vmap_t *vmap);
133
142vmap_t *vmap_obtain(mm_context_t *mmctx, ptr_t vaddr, size_t *out_offset);
143
151vmap_t *vmap_split(vmap_t *vmap, size_t split);
152
161vmap_t *vmap_split_for_range(vmap_t *vmap, size_t rstart_pgoff, size_t rend_pgoff);
162
171void vmap_finalise_init(vmap_t *vmap, vmap_content_t content, vmap_type_t type);
172
181[[nodiscard("resolve completed")]] vmfault_result_t mm_resolve_cow_fault(vmap_t *vmap, ptr_t fault_addr, pagefault_t *info);
182
189void mm_handle_fault(ptr_t fault_addr, pagefault_t *info);
190
192
__BEGIN_DECLS phyframe_t * mm_get_free_page(void)
Definition mm.c:46
__nodiscard mm_context_t * mm_switch_context(mm_context_t *new_ctx)
Definition mm.c:127
void vmap_finalise_init(vmap_t *vmap, vmap_content_t content, vmap_type_t type)
Finalize the initialization of a vmap object.
Definition mm.c:257
vmfault_result_t
Definition mm.h:45
vmap_type_t
Definition mm.h:30
vmap_t * vmap_obtain(mm_context_t *mmctx, ptr_t vaddr, size_t *out_offset)
Get the vmap object for a virtual address.
Definition mm.c:192
phyframe_t * mm_get_free_page_raw(void)
Definition mm.c:34
vmap_t * vmap_split(vmap_t *vmap, size_t split)
Split a vmap object into two, at the specified offset.
Definition mm.c:212
void mm_unlock_ctx_pair(mm_context_t *ctx1, mm_context_t *ctx2)
Definition mm.c:111
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.
Definition mm.c:159
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.
Definition mm.c:273
vmfault_result_t(* vmfault_handler_t)(vmap_t *vmap, ptr_t fault_addr, pagefault_t *info)
Definition mm.h:55
void mm_destroy_context(mm_context_t *table)
Destroy a user-mode platform-dependent page table.
Definition mm.c:83
void mm_handle_fault(ptr_t fault_addr, pagefault_t *info)
Handle a page fault.
Definition mm.c:365
phyframe_t * mm_get_free_pages(size_t npages)
Definition mm.c:55
struct _vmap vmap_t
Definition io.h:12
mm_context_t * mm_create_context(void)
Create a user-mode platform-dependent page table.
Definition mm.c:67
void vmap_destroy(vmap_t *vmap)
Destroy a vmap object, and unmmap the region.
Definition mm.c:171
vmap_content_t
Definition mm.h:21
void mm_lock_ctx_pair(mm_context_t *ctx1, mm_context_t *ctx2)
Lock and unlock a pair of mm_context_t objects.
Definition mm.c:95
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.
Definition mm.c:234
@ VMFAULT_COPY_BACKING_PAGE
the caller should copy the backing page into the faulting address
Definition mm.h:49
@ VMFAULT_MAP_BACKING_PAGE
the caller should map the backing page into the faulting address
Definition mm.h:48
@ VMFAULT_COMPLETE
no further action is needed, the page is correctly mapped now
Definition mm.h:46
@ VMFAULT_CANNOT_HANDLE
the handler cannot handle this fault
Definition mm.h:50
@ VMFAULT_MAP_BACKING_PAGE_RO
the caller should map the backing page into the faulting address, and mark it non-writable
Definition mm.h:47
@ VMAP_TYPE_PRIVATE
Definition mm.h:31
@ VMAP_TYPE_SHARED
Definition mm.h:32
@ VMAP_MMAP
Definition mm.h:25
@ VMAP_DMA
Definition mm.h:26
@ VMAP_UNKNOWN
Definition mm.h:22
@ VMAP_FILE
Definition mm.h:24
@ VMAP_STACK
Definition mm.h:23
@ MMAP_PRIVATE
Definition mm_types.h:17
@ MMAP_SHARED
Definition mm_types.h:18
#define __END_DECLS
Definition mos_global.h:23
#define __nodiscard
Definition mos_global.h:35
#define __BEGIN_DECLS
Definition mos_global.h:22
vm_flags
Definition platform.h:40
Definition io.h:46
bool is_exec
Definition mm.h:37
ptr_t ip
the instruction pointer which caused the fault
Definition mm.h:38
platform_regs_t * regs
the registers of the moment that caused the fault
Definition mm.h:39
phyframe_t * faulting_page
the frame that contains the copy-on-write data (if any)
Definition mm.h:40
const phyframe_t * backing_page
the frame that contains the data for this page, the on_fault handler should set this
Definition mm.h:41
Memory usage statistics for a specific vmap area.
Definition mmstat.h:64
Definition mm.h:58
vmap_content_t content
Definition mm.h:70
ptr_t vaddr
Definition mm.h:62
size_t npages
Definition mm.h:63
vmfault_handler_t on_fault
Definition mm.h:73
as_linked_list
Definition mm.h:59
vm_flags vmflags
Definition mm.h:64
vmap_stat_t stat
Definition mm.h:72
spinlock_t lock
Definition mm.h:60
io_t * io
Definition mm.h:67
mm_context_t * mmctx
Definition mm.h:65
off_t io_offset
Definition mm.h:68
vmap_type_t type
Definition mm.h:71
ssize_t off_t
Definition types.h:84
unsigned long ptr_t
Definition types.h:25