Zephyr API Documentation 4.2.0-rc3
A Scalable Open Source RTOS
 4.2.0-rc3
mm.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_KERNEL_INTERNAL_MM_H
8#define ZEPHYR_INCLUDE_KERNEL_INTERNAL_MM_H
9
10#include <zephyr/sys/util.h>
11#include <zephyr/toolchain.h>
12
18
40#ifdef CONFIG_MMU
41#define K_MEM_VIRT_OFFSET ((CONFIG_KERNEL_VM_BASE + CONFIG_KERNEL_VM_OFFSET) - \
42 (CONFIG_SRAM_BASE_ADDRESS + CONFIG_SRAM_OFFSET))
43#else
44#define K_MEM_VIRT_OFFSET 0
45#endif /* CONFIG_MMU */
46
47#if CONFIG_SRAM_BASE_ADDRESS != 0
48#define IS_SRAM_ADDRESS_LOWER(ADDR) ((ADDR) >= CONFIG_SRAM_BASE_ADDRESS)
49#else
50#define IS_SRAM_ADDRESS_LOWER(ADDR) true
51#endif /* CONFIG_SRAM_BASE_ADDRESS != 0 */
52
53
54#if (CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024UL)) != 0
55#define IS_SRAM_ADDRESS_UPPER(ADDR) \
56 ((ADDR) < (CONFIG_SRAM_BASE_ADDRESS + \
57 (CONFIG_SRAM_SIZE * 1024UL)))
58#else
59#define IS_SRAM_ADDRESS_UPPER(ADDR) false
60#endif
61
62#define IS_SRAM_ADDRESS(ADDR) \
63 (IS_SRAM_ADDRESS_LOWER(ADDR) && \
64 IS_SRAM_ADDRESS_UPPER(ADDR))
65
75#define K_MEM_PHYS_ADDR(virt) ((virt) - K_MEM_VIRT_OFFSET)
76
86#define K_MEM_VIRT_ADDR(phys) ((phys) + K_MEM_VIRT_OFFSET)
87
88#if K_MEM_VIRT_OFFSET != 0
92#define K_MEM_IS_VM_KERNEL 1
93#ifdef CONFIG_XIP
94#error "XIP and a virtual memory kernel are not allowed"
95#endif
96#endif
97
98#ifndef _ASMLANGUAGE
99#include <stdint.h>
100#include <stddef.h>
101#include <inttypes.h>
102#include <zephyr/sys/__assert.h>
104
116static inline uintptr_t k_mem_phys_addr(void *virt)
117{
118 uintptr_t addr = (uintptr_t)virt;
119
120#if defined(CONFIG_KERNEL_VM_USE_CUSTOM_MEM_RANGE_CHECK)
121 __ASSERT(sys_mm_is_virt_addr_in_range(virt),
122 "address %p not in permanent mappings", virt);
123#elif defined(CONFIG_MMU)
124 __ASSERT(
125#if CONFIG_KERNEL_VM_BASE != 0
126 (addr >= CONFIG_KERNEL_VM_BASE) &&
127#endif /* CONFIG_KERNEL_VM_BASE != 0 */
128#if (CONFIG_KERNEL_VM_BASE + CONFIG_KERNEL_VM_SIZE) != 0
129 (addr < (CONFIG_KERNEL_VM_BASE +
130 (CONFIG_KERNEL_VM_SIZE))),
131#else
132 false,
133#endif /* CONFIG_KERNEL_VM_BASE + CONFIG_KERNEL_VM_SIZE != 0 */
134 "address %p not in permanent mappings", virt);
135#else
136 /* Should be identity-mapped */
137 __ASSERT(IS_SRAM_ADDRESS(addr),
138 "physical address 0x%lx not in RAM",
139 (unsigned long)addr);
140#endif /* CONFIG_MMU */
141
142 /* TODO add assertion that this page is pinned to boot mapping,
143 * the above checks won't be sufficient with demand paging
144 */
145
146 return K_MEM_PHYS_ADDR(addr);
147}
148
160static inline void *k_mem_virt_addr(uintptr_t phys)
161{
162#if defined(CONFIG_KERNEL_VM_USE_CUSTOM_MEM_RANGE_CHECK)
163 __ASSERT(sys_mm_is_phys_addr_in_range(phys),
164 "physical address 0x%lx not in RAM", (unsigned long)phys);
165#else
166 __ASSERT(IS_SRAM_ADDRESS(phys),
167 "physical address 0x%lx not in RAM", (unsigned long)phys);
168#endif /* CONFIG_KERNEL_VM_USE_CUSTOM_MEM_RANGE_CHECK */
169
170 /* TODO add assertion that this page frame is pinned to boot mapping,
171 * the above check won't be sufficient with demand paging
172 */
173
174 return (void *)K_MEM_VIRT_ADDR(phys);
175}
176
177#ifdef __cplusplus
178extern "C" {
179#endif
180
225void k_mem_map_phys_bare(uint8_t **virt_ptr, uintptr_t phys, size_t size,
227
255void k_mem_unmap_phys_bare(uint8_t *virt, size_t size);
256
305void *k_mem_map_phys_guard(uintptr_t phys, size_t size, uint32_t flags, bool is_anon);
306
325void k_mem_unmap_phys_guard(void *addr, size_t size, bool is_anon);
326
327#ifdef __cplusplus
328}
329#endif
330
332
333#endif /* !_ASMLANGUAGE */
334#endif /* ZEPHYR_INCLUDE_KERNEL_INTERNAL_MM_H */
void * k_mem_map_phys_guard(uintptr_t phys, size_t size, uint32_t flags, bool is_anon)
Map memory into virtual address space with guard pages.
#define K_MEM_PHYS_ADDR(virt)
Get physical address from virtual address.
Definition mm.h:75
#define K_MEM_VIRT_ADDR(phys)
Get virtual address from physical address.
Definition mm.h:86
void k_mem_map_phys_bare(uint8_t **virt_ptr, uintptr_t phys, size_t size, uint32_t flags)
Map a physical memory region into the kernel's virtual address space.
void k_mem_unmap_phys_guard(void *addr, size_t size, bool is_anon)
Un-map memory mapped via k_mem_map_phys_guard().
static uintptr_t k_mem_phys_addr(void *virt)
Get physical address from virtual address.
Definition mm.h:116
#define IS_SRAM_ADDRESS(ADDR)
Definition mm.h:62
void k_mem_unmap_phys_bare(uint8_t *virt, size_t size)
Unmap a virtual memory region from kernel's virtual address space.
static void * k_mem_virt_addr(uintptr_t phys)
Get virtual address from physical address.
Definition mm.h:160
bool sys_mm_is_virt_addr_in_range(void *virt)
Check if a virtual address is within range of virtual memory.
bool sys_mm_is_phys_addr_in_range(uintptr_t phys)
Check if a physical address is within range of physical memory.
flags
Definition parser.h:97
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
Misc utilities.
Macros to abstract toolchain specific capabilities.