Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /* * Copyright (c) 2020 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #ifndef ZEPHYR_INCLUDE_KERNEL_INTERNAL_MM_H #define ZEPHYR_INCLUDE_KERNEL_INTERNAL_MM_H #include <zephyr/sys/util.h> #include <zephyr/toolchain.h> /** * @defgroup kernel_mm_internal_apis Kernel Memory Management Internal APIs * @ingroup internal_api * @{ */ /** * @def K_MEM_VIRT_OFFSET * @brief Address offset of permanent virtual mapping from physical address. * * This is the offset to subtract from a virtual address mapped in the * kernel's permanent mapping of RAM, to obtain its physical address. * * virt_addr = phys_addr + K_MEM_VIRT_OFFSET * * This only works for virtual addresses within the interval * [CONFIG_KERNEL_VM_BASE, CONFIG_KERNEL_VM_BASE + (CONFIG_SRAM_SIZE * 1024)). * * These macros are intended for assembly, linker code, and static initializers. * Use with care. * * Note that when demand paging is active, these will only work with page * frames that are pinned to their virtual mapping at boot. * * TODO: This will likely need to move to an arch API or need additional * constraints defined. */ #ifdef CONFIG_MMU #define K_MEM_VIRT_OFFSET ((CONFIG_KERNEL_VM_BASE + CONFIG_KERNEL_VM_OFFSET) - \ (CONFIG_SRAM_BASE_ADDRESS + CONFIG_SRAM_OFFSET)) #else #define K_MEM_VIRT_OFFSET 0 #endif /* CONFIG_MMU */ /** * @brief Get physical address from virtual address. * * This only works in the kernel's permanent mapping of RAM. * * @param virt Virtual address * * @return Physical address. */ #define K_MEM_PHYS_ADDR(virt) ((virt) - K_MEM_VIRT_OFFSET) /** * @brief Get virtual address from physical address. * * This only works in the kernel's permanent mapping of RAM. * * @param phys Physical address * * @return Virtual address. */ #define K_MEM_VIRT_ADDR(phys) ((phys) + K_MEM_VIRT_OFFSET) #if K_MEM_VIRT_OFFSET != 0 /** * @brief Kernel is mapped in virtual memory if defined. */ #define K_MEM_IS_VM_KERNEL 1 #ifdef CONFIG_XIP #error "XIP and a virtual memory kernel are not allowed" #endif #endif #ifndef _ASMLANGUAGE #include <stdint.h> #include <stddef.h> #include <inttypes.h> #include <zephyr/sys/__assert.h> #include <zephyr/sys/mem_manage.h> /** * @brief Get physical address from virtual address. * * This only works in the kernel's permanent mapping of RAM. * * Just like K_MEM_PHYS_ADDR() but with type safety and assertions. * * @param virt Virtual address * * @return Physical address. */ static inline uintptr_t k_mem_phys_addr(void *virt) { uintptr_t addr = (uintptr_t)virt; #if defined(CONFIG_KERNEL_VM_USE_CUSTOM_MEM_RANGE_CHECK) __ASSERT(sys_mm_is_virt_addr_in_range(virt), "address %p not in permanent mappings", virt); #elif defined(CONFIG_MMU) __ASSERT( #if CONFIG_KERNEL_VM_BASE != 0 (addr >= CONFIG_KERNEL_VM_BASE) && #endif /* CONFIG_KERNEL_VM_BASE != 0 */ #if (CONFIG_KERNEL_VM_BASE + CONFIG_KERNEL_VM_SIZE) != 0 (addr < (CONFIG_KERNEL_VM_BASE + (CONFIG_KERNEL_VM_SIZE))), #else false, #endif /* CONFIG_KERNEL_VM_BASE + CONFIG_KERNEL_VM_SIZE != 0 */ "address %p not in permanent mappings", virt); #else /* Should be identity-mapped */ __ASSERT( #if CONFIG_SRAM_BASE_ADDRESS != 0 (addr >= CONFIG_SRAM_BASE_ADDRESS) && #endif /* CONFIG_SRAM_BASE_ADDRESS != 0 */ #if (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0 (addr < (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL))), #else false, #endif /* (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0 */ "physical address 0x%lx not in RAM", (unsigned long)addr); #endif /* CONFIG_MMU */ /* TODO add assertion that this page is pinned to boot mapping, * the above checks won't be sufficient with demand paging */ return K_MEM_PHYS_ADDR(addr); } /** * @brief Get virtual address from physical address. * * This only works in the kernel's permanent mapping of RAM. * * Just like K_MEM_VIRT_ADDR() but with type safety and assertions. * * @param phys Physical address * * @return Virtual address. */ static inline void *k_mem_virt_addr(uintptr_t phys) { #if defined(CONFIG_KERNEL_VM_USE_CUSTOM_MEM_RANGE_CHECK) __ASSERT(sys_mm_is_phys_addr_in_range(phys), "physical address 0x%lx not in RAM", (unsigned long)phys); #else __ASSERT( #if CONFIG_SRAM_BASE_ADDRESS != 0 (phys >= CONFIG_SRAM_BASE_ADDRESS) && #endif /* CONFIG_SRAM_BASE_ADDRESS != 0 */ #if (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0 (phys < (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL))), #else false, #endif /* (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0 */ "physical address 0x%lx not in RAM", (unsigned long)phys); #endif /* CONFIG_KERNEL_VM_USE_CUSTOM_MEM_RANGE_CHECK */ /* TODO add assertion that this page frame is pinned to boot mapping, * the above check won't be sufficient with demand paging */ return (void *)K_MEM_VIRT_ADDR(phys); } #ifdef __cplusplus extern "C" { #endif /** * Map a physical memory region into the kernel's virtual address space * * This function is intended for mapping memory-mapped I/O regions into * the virtual address space. Given a physical address and a size, return a * linear address representing the base of where the physical region is mapped * in the virtual address space for the Zephyr kernel. * * The memory mapped via this function must be unmapped using * k_mem_unmap_phys_bare(). * * This function alters the active page tables in the area reserved * for the kernel. This function will choose the virtual address * and return it to the caller. * * Portable code should never assume that phys_addr and linear_addr will * be equal. * * Caching and access properties are controlled by the 'flags' parameter. * Unused bits in 'flags' are reserved for future expansion. * A caching mode must be selected. By default, the region is read-only * with user access and code execution forbidden. This policy is changed * by passing K_MEM_CACHE_* and K_MEM_PERM_* macros into the 'flags' parameter. * * If there is insufficient virtual address space for the mapping this will * generate a kernel panic. * * This API is only available if CONFIG_MMU is enabled. * * It is highly discouraged to use this function to map system RAM page * frames. It may conflict with anonymous memory mappings and demand paging * and produce undefined behavior. Do not use this for RAM unless you know * exactly what you are doing. If you need a chunk of memory, use k_mem_map(). * If you need a contiguous buffer of physical memory, statically declare it * and pin it at build time, it will be mapped when the system boots. * * This API is part of infrastructure still under development and may * change. * * @param[out] virt_ptr Output virtual address storage location * @param[in] phys Physical address base of the memory region * @param[in] size Size of the memory region * @param[in] flags Caching mode and access flags, see K_MAP_* macros */ void k_mem_map_phys_bare(uint8_t **virt_ptr, uintptr_t phys, size_t size, uint32_t flags); /** * Unmap a virtual memory region from kernel's virtual address space. * * This function is intended to be used by drivers and early boot routines * where temporary memory mappings need to be made. This allows these * memory mappings to be discarded once they are no longer needed. * * This function alters the active page tables in the area reserved * for the kernel. * * This will align the input parameters to page boundaries so that * this can be used with the virtual address as returned by * k_mem_map_phys_bare(). * * This API is only available if CONFIG_MMU is enabled. * * It is highly discouraged to use this function to unmap memory mappings. * It may conflict with anonymous memory mappings and demand paging and * produce undefined behavior. Do not use this unless you know exactly * what you are doing. * * This API is part of infrastructure still under development and may * change. * * @param virt Starting address of the virtual address region to be unmapped. * @param size Size of the virtual address region */ void k_mem_unmap_phys_bare(uint8_t *virt, size_t size); /** * Map memory into virtual address space with guard pages. * * This maps memory into virtual address space with a preceding and * a succeeding guard pages. The memory mapped via this function must be * unmapped using k_mem_unmap_phys_guard(). * * This function maps a contiguous physical memory region into kernel's * virtual address space with a preceding and a succeeding guard pages. * Given a physical address and a size, return a linear address representing * the base of where the physical region is mapped in the virtual address * space for the Zephyr kernel. * * This function alters the active page tables in the area reserved * for the kernel. This function will choose the virtual address * and return it to the caller. * * If user thread access control needs to be managed in any way, do not enable * K_MEM_PERM_USER flags here; instead manage the region's permissions * with memory domain APIs after the mapping has been established. Setting * K_MEM_PERM_USER here will allow all user threads to access this memory * which is usually undesirable. * * Unless K_MEM_MAP_UNINIT is used, the returned memory will be zeroed. * * The returned virtual memory pointer will be page-aligned. The size * parameter, and any base address for re-mapping purposes must be page- * aligned. * * Note that the allocation includes two guard pages immediately before * and after the requested region. The total size of the allocation will be * the requested size plus the size of these two guard pages. * * Many K_MEM_MAP_* flags have been implemented to alter the behavior of this * function, with details in the documentation for these flags. * * @see k_mem_map() for additional information if called via that. * * @param phys Physical address base of the memory region if not requesting * anonymous memory. Must be page-aligned. * @param size Size of the memory mapping. This must be page-aligned. * @param flags K_MEM_PERM_*, K_MEM_MAP_* control flags. * @param is_anon True is requesting mapping with anonymous memory. * * @return The mapped memory location, or NULL if insufficient virtual address * space, insufficient physical memory to establish the mapping, * or insufficient memory for paging structures. */ void *k_mem_map_phys_guard(uintptr_t phys, size_t size, uint32_t flags, bool is_anon); /** * Un-map memory mapped via k_mem_map_phys_guard(). * * This removes the memory mappings for the provided page-aligned region, * and the two guard pages surrounding the region. * * This function alters the active page tables in the area reserved * for the kernel. * * @see k_mem_unmap() for additional information if called via that. * * @note Calling this function on a region which was not mapped via * k_mem_map_phys_guard() to begin with is undefined behavior. * * @param addr Page-aligned memory region base virtual address * @param size Page-aligned memory region size * @param is_anon True if the mapped memory is from anonymous memory. */ void k_mem_unmap_phys_guard(void *addr, size_t size, bool is_anon); #ifdef __cplusplus } #endif /** @} */ #endif /* !_ASMLANGUAGE */ #endif /* ZEPHYR_INCLUDE_KERNEL_INTERNAL_MM_H */ |