| 1 | //SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* From wut: |
| 3 | * https://github.com/devkitPro/wut/blob/0b196e8abcedeb0238105f3ffab7cb0093638b86/include/coreinit/memorymap.h |
| 4 | */ |
| 5 | |
| 6 | #pragma once |
| 7 | #include <stdint.h> |
| 8 | #include <stdbool.h> |
| 9 | typedef bool BOOL; |
| 10 | |
| 11 | /** |
| 12 | * \defgroup coreinit_memorymap Memory Map |
| 13 | * \ingroup coreinit |
| 14 | * |
| 15 | * @{ |
| 16 | */ |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | typedef enum OSMemoryMapMode |
| 23 | { |
| 24 | OS_MAP_MEMORY_INVALID = 0, |
| 25 | OS_MAP_MEMORY_READ_ONLY = 1, |
| 26 | OS_MAP_MEMORY_READ_WRITE = 2, |
| 27 | OS_MAP_MEMORY_FREE = 3, |
| 28 | OS_MAP_MEMORY_ALLOCATED = 4, |
| 29 | } OSMemoryMapMode; |
| 30 | |
| 31 | #define OS_PAGE_SIZE (128 * 1024) |
| 32 | |
| 33 | uint32_t |
| 34 | OSEffectiveToPhysical(uint32_t virtualAddress); |
| 35 | |
| 36 | BOOL |
| 37 | OSIsAddressValid(uint32_t virtualAddress); |
| 38 | |
| 39 | BOOL |
| 40 | __OSValidateAddressSpaceRange(int /* unused */, |
| 41 | uint32_t virtualAddress, |
| 42 | uint32_t size); |
| 43 | |
| 44 | /** |
| 45 | * Allocates virtual address range for later mapping. |
| 46 | * |
| 47 | * \param virtualAddress |
| 48 | * Requested start address for the range. If there is no preference, NULL can be |
| 49 | * used. |
| 50 | * |
| 51 | * \param size |
| 52 | * Size of address range to allocate. |
| 53 | * |
| 54 | * \param align |
| 55 | * Alignment of address range to allocate. |
| 56 | * |
| 57 | * \return |
| 58 | * The starting address of the newly allocated range, or NULL on failure. |
| 59 | * |
| 60 | * \sa |
| 61 | * - OSFreeVirtAddr() |
| 62 | * - OSMapMemory() |
| 63 | */ |
| 64 | uint32_t |
| 65 | OSAllocVirtAddr(uint32_t virtualAddress, |
| 66 | uint32_t size, |
| 67 | uint32_t align); |
| 68 | |
| 69 | /** |
| 70 | * Frees a previously allocated virtual address range back to the system. |
| 71 | * |
| 72 | * \param virtualAddress |
| 73 | * The start of the virtual address range to free. |
| 74 | * |
| 75 | * \param size |
| 76 | * The size of the virtual address range to free. |
| 77 | * |
| 78 | * \return |
| 79 | * \c true on success. |
| 80 | */ |
| 81 | BOOL |
| 82 | OSFreeVirtAddr(uint32_t virtualAddress, |
| 83 | uint32_t size); |
| 84 | |
| 85 | /** |
| 86 | * Determines the status of the given virtual memory address - mapped read-write |
| 87 | * or read-only, free, allocated or invalid. |
| 88 | * |
| 89 | * \param virtualAddress |
| 90 | * The virtual address to query. |
| 91 | * |
| 92 | * \return |
| 93 | * The status of the memory address - see #OSMemoryMapMode. |
| 94 | */ |
| 95 | OSMemoryMapMode |
| 96 | OSQueryVirtAddr(uint32_t virtualAddress); |
| 97 | |
| 98 | /** |
| 99 | * Maps a physical address to a virtual address, with a given size and set of |
| 100 | * permissions. |
| 101 | * |
| 102 | * \param virtualAddress |
| 103 | * The target virtual address for the mapping. |
| 104 | * |
| 105 | * \param physicalAddress |
| 106 | * Physical address of the memory to back the mapping. |
| 107 | * |
| 108 | * \param size |
| 109 | * Size, in bytes, of the desired mapping. Likely has an alignment requirement. |
| 110 | * |
| 111 | * \param mode |
| 112 | * Permissions to map the memory with - see #OSMemoryMapMode. |
| 113 | * |
| 114 | * \return |
| 115 | * \c true on success. |
| 116 | * |
| 117 | * \sa |
| 118 | * - OSAllocVirtAddr() |
| 119 | * - OSUnmapMemory() |
| 120 | */ |
| 121 | BOOL |
| 122 | OSMapMemory(uint32_t virtualAddress, |
| 123 | uint32_t physicalAddress, |
| 124 | uint32_t size, |
| 125 | OSMemoryMapMode mode); |
| 126 | |
| 127 | /** |
| 128 | * Unmaps previously mapped memory. |
| 129 | * |
| 130 | * \param virtualAddress |
| 131 | * Starting address of the area to unmap. |
| 132 | * |
| 133 | * \param size |
| 134 | * Size of the memory area to unmap. |
| 135 | * |
| 136 | * \return |
| 137 | * \c true on success. |
| 138 | */ |
| 139 | BOOL |
| 140 | OSUnmapMemory(uint32_t virtualAddress, |
| 141 | uint32_t size); |
| 142 | |
| 143 | /** |
| 144 | * Gets the range of virtual addresses available for mapping. |
| 145 | * |
| 146 | * \param outVirtualAddress |
| 147 | * Pointer to write the starting address of the memory area to. |
| 148 | * |
| 149 | * \param outSize |
| 150 | * Pointer to write the size of the memory area to. |
| 151 | * |
| 152 | * \sa |
| 153 | * - OSMapMemory() |
| 154 | */ |
| 155 | void |
| 156 | OSGetMapVirtAddrRange(uint32_t *outVirtualAddress, |
| 157 | uint32_t *outSize); |
| 158 | |
| 159 | /** |
| 160 | * Gets the range of available physical memory (not reserved for app code or |
| 161 | * data). |
| 162 | * |
| 163 | * \param outPhysicalAddress |
| 164 | * Pointer to write the starting physical address of the memory area to. |
| 165 | * |
| 166 | * \param outSize |
| 167 | * Pointer to write the size of the memory area to. |
| 168 | * |
| 169 | * \if false |
| 170 | * Is memory returned by this function actually safe to map and use? couldn't |
| 171 | * get a straight answer from decaf-emu's kernel_memory.cpp... |
| 172 | * \endif |
| 173 | */ |
| 174 | void |
| 175 | OSGetAvailPhysAddrRange(uint32_t *outPhysicalAddress, |
| 176 | uint32_t *outSize); |
| 177 | |
| 178 | /** |
| 179 | * Gets the range of physical memory used for the application's data. |
| 180 | * |
| 181 | * \param outPhysicalAddress |
| 182 | * Pointer to write the starting physical address of the memory area to. |
| 183 | * |
| 184 | * \param outSize |
| 185 | * Pointer to write the size of the memory area to. |
| 186 | * |
| 187 | * \if false |
| 188 | * does this include the main heap? |
| 189 | * \endif |
| 190 | */ |
| 191 | void |
| 192 | OSGetDataPhysAddrRange(uint32_t *outPhysicalAddress, |
| 193 | uint32_t *outSize); |
| 194 | |
| 195 | #ifdef __cplusplus |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | /** @} */ |