1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "mos/mm/mm_types.hpp"
6
7#include <mos/mm/paging/paging.hpp>
8#include <mos/tasks/task_types.hpp>
9#include <mos/types.hpp>
10
11MOS_ENUM_FLAGS(mmap_flags_t, MMapFlags);
12
13/**
14 * @brief Map a page into the current process's address space
15 *
16 * @param ctx The memory management context
17 * @param hint_addr A hint for the address to map the page at, the actual address is determined based on the @p flags
18 * @param flags Flags to control the mapping, see @ref mmap_flags_t
19 * @param VMFlags Flags to control the permissions of the mapping, see @ref VMFlags
20 * @param n_pages Number of pages to map
21 * @return ptr_t The address the page was mapped at, or 0 if the mapping failed
22 */
23ptr_t mmap_anonymous(MMContext *ctx, ptr_t hint_addr, MMapFlags flags, VMFlags VMFlags, size_t n_pages);
24
25/**
26 * @brief Map a file into the current process's address space
27 *
28 * @param ctx The memory management context
29 * @param hint_addr A hint for the address to map the page at, the actual address is determined based on the @p flags
30 * @param flags Flags to control the mapping, see @ref mmap_flags_t
31 * @param VMFlags Flags to control the permissions of the mapping, see @ref VMFlags
32 * @param n_pages Number of pages to map
33 * @param io The io object to map, the io object must be backed by a @ref file_t
34 * @param offset The offset into the file to map, must be page aligned
35 * @return ptr_t The address the page was mapped at, or 0 if the mapping failed
36 */
37ptr_t mmap_file(MMContext *ctx, ptr_t hint_addr, MMapFlags flags, VMFlags VMFlags, size_t n_pages, IO *io, off_t offset);
38
39/**
40 * @brief Unmap a page from the current process's address space
41 *
42 * @param addr The address to unmap
43 * @param size The size of the mapping to unmap
44 * @return true If the unmap succeeded
45 *
46 * @note Neither @p addr nor @p size need to be page aligned, all pages contained within
47 * the range specified will be unmapped even if they are not fully contained within the range.
48 */
49bool munmap(ptr_t addr, size_t size);
50
51/**
52 * @brief Change the permissions of a mapping
53 *
54 * @param mmctx The memory management context
55 * @param addr The address of the mapping to change
56 * @param size The size of the mapping to change
57 * @param perm The new permissions for the mapping
58 */
59bool vm_protect(MMContext *mmctx, ptr_t addr, size_t size, VMFlags perm);
60