1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "mos/mm/mm.h"
6
7#include <mos/lib/structures/bitmap.h>
8#include <mos/lib/sync/spinlock.h>
9#include <mos/platform/platform.h>
10
11/**
12 * @defgroup paging Paging
13 * @ingroup mm
14 * @brief A platform-independent interface to map/unmap virtual memory to physical memory.
15 *
16 * @{
17 */
18
19typedef enum
20{
21 VALLOC_DEFAULT = 0, ///< Default allocation flags.
22 VALLOC_EXACT = MMAP_EXACT, ///< Allocate pages at the exact address.
23} valloc_flags;
24
25/**
26 * @brief Gets npages unmapped free pages from a page table.
27 *
28 * @param mmctx The memory management context to allocate from.
29 * @param n_pages The number of pages to allocate.
30 * @param base_vaddr The base virtual address to allocate at.
31 * @param flags Flags to set on the pages, see @ref valloc_flags.
32 * @return ptr_t The virtual address of the block of virtual memory.
33 *
34 * @note This function neither allocates nor maps the pages, it only
35 * determines the block of virtual memory that can be used to allocate
36 * and map the pages.
37 *
38 * @note The returned @ref vmap_t only contains the virtual address
39 * and the number of pages, it does not contain any physical addresses,
40 * nor the flags of the pages.
41 *
42 * @warning Should call with mmctx->mm_lock held.
43 */
44
45vmap_t *mm_get_free_vaddr_locked(mm_context_t *mmctx, size_t n_pages, ptr_t base_vaddr, valloc_flags flags);
46
47/**
48 * @brief Map a block of virtual memory to a block of physical memory.
49 *
50 * @param mmctx The memory management context to map to.
51 * @param vaddr The virtual address to map to.
52 * @param pfn The physical frame to map from.
53 * @param npages The number of pages to map.
54 * @param flags Flags to set on the pages, see @ref vm_flags.
55 *
56 * @details This function maps the pages in the block, their reference count will NOT be incremented.
57 *
58 * @note This function is rarely used directly, you don't always know the physical address of the
59 * pages you want to map.
60 */
61void mm_map_kernel_pages(mm_context_t *mmctx, ptr_t vaddr, pfn_t pfn, size_t npages, vm_flags flags);
62vmap_t *mm_map_user_pages(mm_context_t *mmctx, ptr_t vaddr, pfn_t pfn, size_t npages, vm_flags flags, valloc_flags vaflags, vmap_type_t type, vmap_content_t content);
63
64/**
65 * @brief Replace the mappings of a page with a new physical frame.
66 *
67 * @param mmctx The memory management context to replace in.
68 * @param vaddr The virtual address to replace.
69 * @param pfn The physical frame to replace to.
70 * @param flags The new flags to set on the pages.
71 *
72 * @note The reference count of the physical frame will be incremented, and the reference count of the
73 * old physical frame will be decremented.
74 */
75void mm_replace_page_locked(mm_context_t *mmctx, ptr_t vaddr, pfn_t pfn, vm_flags flags);
76
77/**
78 * @brief Remap a block of virtual memory from one page table to another, i.e. copy the mappings.
79 *
80 * @param src_vmap The source vmap
81 * @param dst_ctx The destination paging context
82 *
83 * @details This function firstly unmaps the pages in the destination page table, then copies the pages
84 * mappings, including the flags and physical addresses, the physical page reference count is updated
85 * accordingly.
86 *
87 * @note If clear_dest is set to true, then the destination page table is cleared before copying, otherwise
88 * the function assumes that there are no existing mappings in the destination page table.
89 */
90vmap_t *mm_clone_vmap_locked(vmap_t *src_vmap, mm_context_t *dst_ctx);
91
92/**
93 * @brief Get if a virtual address is mapped in a page table.
94 *
95 * @param mmctx The memory management context to check in.
96 * @param vaddr The virtual address to get the physical address of.
97 * @return bool True if the virtual address is mapped, false otherwise.
98 */
99bool mm_get_is_mapped_locked(mm_context_t *mmctx, ptr_t vaddr);
100
101/**
102 * @brief Update the flags of a block of virtual memory.
103 *
104 * @param mmctx The memory management context to update the flags in.
105 * @param vaddr The virtual address to update the flags of.
106 * @param npages The number of pages to update the flags of.
107 * @param flags The flags to set on the pages, see @ref vm_flags.
108 */
109void mm_flag_pages_locked(mm_context_t *mmctx, ptr_t vaddr, size_t npages, vm_flags flags);
110
111ptr_t mm_get_phys_addr(mm_context_t *ctx, ptr_t vaddr);
112
113/** @} */
114