1.. _memory_domain:
2
3Memory Protection Design
4########################
5
6Zephyr's memory protection design is geared towards microcontrollers with MPU
7(Memory Protection Unit) hardware. We do support some architectures, such as x86,
8which have a paged MMU (Memory Management Unit), but in that case the MMU is
9used like an MPU with an identity page table.
10
11All of the discussion below will be using MPU terminology; systems with MMUs
12can be considered to have an MPU with an unlimited number of programmable
13regions.
14
15There are a few different levels on how memory access is configured when
16Zephyr memory protection features are enabled, which we will describe here:
17
18Boot Time Memory Configuration
19******************************
20
21This is the configuration of the MPU after the kernel has started up. It should
22contain the following:
23
24- Any configuration of memory regions which need to have special caching or
25  write-back policies for basic hardware and driver function. Note that most
26  MPUs have the concept of a default memory access policy map, which can be
27  enabled as a "background" mapping for any area of memory that doesn't
28  have an MPU region configuring it. It is strongly recommended to use this
29  to maximize the number of available MPU regions for the end user. On
30  ARMv7-M/ARMv8-M this is called the System Address Map, other CPUs may
31  have similar capabilities. See :ref:`mem_mgmt_api` for information on
32  how to annotate the system map in the device tree.
33
34- A read-only, executable region or regions for program text and ro-data, that
35  is accessible to user mode. This could be further sub-divided into a
36  read-only region for ro-data, and a read-only, executable region for text, but
37  this will require an additional MPU region. This is required so that
38  threads running in user mode can read ro-data and fetch instructions.
39
40- Depending on configuration, user-accessible read-write regions to support
41  extra features like GCOV, HEP, etc.
42
43Assuming there is a background map which allows supervisor mode to access any
44memory it needs, and regions are defined which grant user mode access to
45text/ro-data, this is sufficient for the boot time configuration.
46
47Hardware Stack Overflow
48***********************
49
50:kconfig:option:`CONFIG_HW_STACK_PROTECTION` is an optional feature which detects stack
51buffer overflows when the system is running in supervisor mode. This
52catches issues when the entire stack buffer has overflowed, and not
53individual stack frames, use compiler-assisted :kconfig:option:`CONFIG_STACK_CANARIES`
54for that.
55
56Like any crash in supervisor mode, no guarantees can be made about the overall
57health of the system after a supervisor mode stack overflow, and any instances
58of this should be treated as a serious error. However it's still very useful to
59know when these overflows happen, as without robust detection logic the system
60will either crash in mysterious ways or behave in an undefined manner when the
61stack buffer overflows.
62
63Some systems implement this feature by creating at runtime a 'guard' MPU region
64which is set to be read-only and is at either the beginning or immediately
65preceding the supervisor mode stack buffer.  If the stack overflows an
66exception will be generated.
67
68This feature is optional and is not required to catch stack overflows in user
69mode; disabling this may free 1-2 MPU regions depending on the MPU design.
70
71Other systems may have dedicated CPU support for catching stack overflows
72and no extra MPU regions will be required.
73
74Thread Stack
75************
76
77Any thread running in user mode will need access to its own stack buffer.
78On context switch into a user mode thread, a dedicated MPU region or MMU
79page table entries will be programmed with the bounds of the stack buffer.
80A thread exceeding its stack buffer will start pushing data onto memory
81it doesn't have access to and a memory access violation exception will be
82generated.
83
84Note that user threads have access to the stacks of other user threads in
85the same memory domain. This is the minimum required for architectures to
86support memory domains. Architecture can further restrict access to stacks
87so each user thread only has access to its own stack if such architecture
88advertises this capability via
89:kconfig:option:`CONFIG_ARCH_MEM_DOMAIN_SUPPORTS_ISOLATED_STACKS`.
90This behavior is enabled by default if supported and can be selectively
91disabled via :kconfig:option:`CONFIG_MEM_DOMAIN_ISOLATED_STACKS` if
92architecture supports both operating modes. However, some architectures
93may decide to enable this all the time, and thus this option cannot be
94disabled. Regardless of these kconfigs, user threads cannot access
95the stacks of other user threads outside of their memory domains.
96
97Thread Resource Pools
98*********************
99
100A small subset of kernel APIs, invoked as system calls, require heap memory
101allocations. This memory is used only by the kernel and is not accessible
102directly by user mode. In order to use these system calls, invoking threads
103must assign themselves to a resource pool, which is a :c:struct:`k_heap`
104object. Memory is drawn from a thread's resource pool using
105:c:func:`z_thread_malloc` and freed with :c:func:`k_free`.
106
107The APIs which use resource pools are as follows, with any alternatives
108noted for users who do not want heap allocations within their application:
109
110 - :c:func:`k_stack_alloc_init` sets up a k_stack with its storage
111   buffer allocated out of a resource pool instead of a buffer provided by the
112   user. An alternative is to declare k_stacks that are automatically
113   initialized at boot with :c:macro:`K_STACK_DEFINE()`, or to initialize the
114   k_stack in supervisor mode with :c:func:`k_stack_init`.
115
116 - :c:func:`k_msgq_alloc_init` sets up a k_msgq object with its
117   storage buffer allocated out of a resource pool instead of a buffer provided
118   by the user. An alternative is to declare a k_msgq that is automatically
119   initialized at boot with :c:macro:`K_MSGQ_DEFINE()`, or to initialize the
120   k_msgq in supervisor mode with :c:func:`k_msgq_init`.
121
122 - :c:func:`k_poll` when invoked from user mode, needs to make a kernel-side
123   copy of the provided events array while waiting for an event. This copy is
124   freed when :c:func:`k_poll` returns for any reason.
125
126 - :c:func:`k_queue_alloc_prepend` and :c:func:`k_queue_alloc_append`
127   allocate a container structure to place the data in, since the internal
128   bookkeeping information that defines the queue cannot be placed in the
129   memory provided by the user.
130
131 - :c:func:`k_object_alloc` allows for entire kernel objects to be
132   dynamically allocated at runtime and a usable pointer to them returned to
133   the caller.
134
135The relevant API is :c:func:`k_thread_heap_assign` which assigns
136a k_heap to draw these allocations from for the target thread.
137
138If the system heap is enabled, then the system heap may be used with
139:c:func:`k_thread_system_pool_assign`, but it is preferable for different
140logical applications running on the system to have their own pools.
141
142Memory Domains
143**************
144
145The kernel ensures that any user thread will have access to its own stack
146buffer, plus program text and read-only data. The memory domain APIs are the
147way to grant access to additional blocks of memory to a user thread.
148
149Conceptually, a memory domain is a collection of some number of memory
150partitions. The maximum number of memory partitions in a domain
151is limited by the number of available MPU regions. This is why it is important
152to minimize the number of boot-time MPU regions.
153
154Memory domains are *not* intended to control access to memory from supervisor
155mode. In some cases this may be unavoidable; for example some architectures do
156not allow for the definition of regions which are read-only to user mode but
157read-write to supervisor mode. A great deal of care must be taken when working
158with such regions to not unintentionally cause the kernel to crash when
159accessing such a region. Any attempt to use memory domain APIs to control
160supervisor mode access is at best undefined behavior; supervisor mode access
161policy is only intended to be controlled by boot-time memory regions.
162
163Memory domain APIs are only available to supervisor mode. The only control
164user mode has over memory domains is that any user thread's child threads
165will automatically become members of the parent's domain.
166
167All threads are members of a memory domain, including supervisor threads
168(even though this has no implications on their memory access). There is a
169default domain ``k_mem_domain_default`` which will be assigned to threads if
170they have not been specifically assigned to a domain, or inherited a memory
171domain membership from their parent thread. The main thread starts as a
172member of the default domain.
173
174Memory Partitions
175=================
176
177Each memory partition consists of a memory address, a size,
178and access attributes. It is intended that memory partitions are used to
179control access to system memory. Defining memory partitions are subject
180to the following constraints:
181
182- The partition must represent a memory region that can be programmed by
183  the underlying memory management hardware, and needs to conform to any
184  underlying hardware constraints. For example, many MPU-based systems require
185  that partitions be sized to some power of two, and aligned to their own
186  size. For MMU-based systems, the partition must be aligned to a page and
187  the size some multiple of the page size.
188
189- Partitions within the same memory domain may not overlap each other. There is
190  no notion of precedence among partitions within a memory domain.  Partitions
191  within a memory domain are assumed to have a higher precedence than any
192  boot-time memory regions, however whether a memory domain partition can
193  overlap a boot-time memory region is architecture specific.
194
195- The same partition may be specified in multiple memory domains. For example
196  there may be a shared memory area that multiple domains grant access to.
197
198- Care must be taken in determining what memory to expose in a partition.
199  It is not appropriate to provide direct user mode access to any memory
200  containing private kernel data.
201
202- Memory domain partitions are intended to control access to system RAM.
203  Configuration of memory partitions which do not correspond to RAM
204  may not be supported by the architecture; this is true for MMU-based systems.
205
206There are two ways to define memory partitions: either manually or
207automatically.
208
209Manual Memory Partitions
210------------------------
211
212The following code declares a global array ``buf``, and then declares
213a read-write partition for it which may be added to a domain:
214
215.. code-block:: c
216
217    uint8_t __aligned(32) buf[32];
218
219    K_MEM_PARTITION_DEFINE(my_partition, buf, sizeof(buf),
220                           K_MEM_PARTITION_P_RW_U_RW);
221
222This does not scale particularly well when we are trying to contain multiple
223objects spread out across several C files into a single partition.
224
225Automatic Memory Partitions
226---------------------------
227
228Automatic memory partitions are created by the build system. All globals
229which need to be placed inside a partition are tagged with their destination
230partition. The build system will then coalesce all of these into a single
231contiguous block of memory, zero any BSS variables at boot, and define
232a memory partition of appropriate base address and size which contains all
233the tagged data.
234
235.. figure:: auto_mem_domain.png
236   :alt: Automatic Memory Domain build flow
237   :align: center
238
239   Automatic Memory Domain build flow
240
241Automatic memory partitions are only configured as read-write
242regions. They are defined with :c:macro:`K_APPMEM_PARTITION_DEFINE()`.
243Global variables are then routed to this partition using
244:c:macro:`K_APP_DMEM()` for initialized data and :c:macro:`K_APP_BMEM()` for
245BSS.
246
247.. code-block:: c
248
249    #include <zephyr/app_memory/app_memdomain.h>
250
251    /* Declare a k_mem_partition "my_partition" that is read-write to
252     * user mode. Note that we do not specify a base address or size.
253     */
254    K_APPMEM_PARTITION_DEFINE(my_partition);
255
256    /* The global variable var1 will be inside the bounds of my_partition
257     * and be initialized with 37 at boot.
258     */
259    K_APP_DMEM(my_partition) int var1 = 37;
260
261    /* The global variable var2 will be inside the bounds of my_partition
262     * and be zeroed at boot size K_APP_BMEM() was used, indicating a BSS
263     * variable.
264     */
265    K_APP_BMEM(my_partition) int var2;
266
267The build system will ensure that the base address of ``my_partition`` will
268be properly aligned, and the total size of the region conforms to the memory
269management hardware requirements, adding padding if necessary.
270
271If multiple partitions are being created, a variadic preprocessor macro can be
272used as provided in ``app_macro_support.h``:
273
274.. code-block:: c
275
276    FOR_EACH(K_APPMEM_PARTITION_DEFINE, part0, part1, part2);
277
278Automatic Partitions for Static Library Globals
279~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
280
281The build-time logic for setting up automatic memory partitions is in
282``scripts/build/gen_app_partitions.py``. If a static library is linked into Zephyr,
283it is possible to route all the globals in that library to a specific
284memory partition with the ``--library`` argument.
285
286For example, if the Newlib C library is enabled, the Newlib globals all need
287to be placed in ``z_libc_partition``. The invocation of the script in the
288top-level ``CMakeLists.txt`` adds the following:
289
290.. code-block:: none
291
292    gen_app_partitions.py ... --library libc.a z_libc_partition ..
293
294For pre-compiled libraries there is no support for expressing this in the
295project-level configuration or build files; the toplevel ``CMakeLists.txt`` must
296be edited.
297
298For Zephyr libraries created using ``zephyr_library`` or ``zephyr_library_named``
299the ``zephyr_library_app_memory`` function can be used to specify the memory
300partition where all globals in the library should be placed.
301
302.. _memory_domain_predefined_partitions:
303
304Pre-defined Memory Partitions
305-----------------------------
306
307There are a few memory partitions which are pre-defined by the system:
308
309 - ``z_malloc_partition`` - This partition contains the system-wide pool of
310   memory used by libc malloc(). Due to possible starvation issues, it is
311   not recommended to draw heap memory from a global pool, instead
312   it is better to define various sys_heap objects and assign them
313   to specific memory domains.
314
315 - ``z_libc_partition`` - Contains globals required by the C library and runtime.
316   Required when using either the Minimal C library or the Newlib C Library.
317   Required when :kconfig:option:`CONFIG_STACK_CANARIES` is enabled.
318
319Library-specific partitions are listed in :zephyr_file:`include/zephyr/app_memory/partitions.h`.
320For example, to use the MBEDTLS library from user mode, the
321``k_mbedtls_partition`` must be added to the domain.
322
323Memory Domain Usage
324===================
325
326Create a Memory Domain
327----------------------
328
329A memory domain is defined using a variable of type
330:c:struct:`k_mem_domain`. It must then be initialized by calling
331:c:func:`k_mem_domain_init`.
332
333The following code defines and initializes an empty memory domain.
334
335.. code-block:: c
336
337    struct k_mem_domain app0_domain;
338
339    k_mem_domain_init(&app0_domain, 0, NULL);
340
341Add Memory Partitions into a Memory Domain
342------------------------------------------
343
344There are two ways to add memory partitions into a memory domain.
345
346This first code sample shows how to add memory partitions while creating
347a memory domain.
348
349.. code-block:: c
350
351    /* the start address of the MPU region needs to align with its size */
352    uint8_t __aligned(32) app0_buf[32];
353    uint8_t __aligned(32) app1_buf[32];
354
355    K_MEM_PARTITION_DEFINE(app0_part0, app0_buf, sizeof(app0_buf),
356                           K_MEM_PARTITION_P_RW_U_RW);
357
358    K_MEM_PARTITION_DEFINE(app0_part1, app1_buf, sizeof(app1_buf),
359                           K_MEM_PARTITION_P_RW_U_RO);
360
361    struct k_mem_partition *app0_parts[] = {
362        app0_part0,
363        app0_part1
364    };
365
366    k_mem_domain_init(&app0_domain, ARRAY_SIZE(app0_parts), app0_parts);
367
368This second code sample shows how to add memory partitions into an initialized
369memory domain one by one.
370
371.. code-block:: c
372
373    /* the start address of the MPU region needs to align with its size */
374    uint8_t __aligned(32) app0_buf[32];
375    uint8_t __aligned(32) app1_buf[32];
376
377    K_MEM_PARTITION_DEFINE(app0_part0, app0_buf, sizeof(app0_buf),
378                           K_MEM_PARTITION_P_RW_U_RW);
379
380    K_MEM_PARTITION_DEFINE(app0_part1, app1_buf, sizeof(app1_buf),
381                           K_MEM_PARTITION_P_RW_U_RO);
382
383    k_mem_domain_add_partition(&app0_domain, &app0_part0);
384    k_mem_domain_add_partition(&app0_domain, &app0_part1);
385
386.. note::
387    The maximum number of memory partitions is limited by the maximum
388    number of MPU regions or the maximum number of MMU tables.
389
390Memory Domain Assignment
391------------------------
392
393Any thread may join a memory domain, and any memory domain may have multiple
394threads assigned to it. Threads are assigned to memory domains with an API
395call:
396
397.. code-block:: c
398
399    k_mem_domain_add_thread(&app0_domain, app_thread_id);
400
401If the thread was already a member of some other domain (including the
402default domain), it will be removed from it in favor of the new one.
403
404In addition, if a thread is a member of a memory domain, and it creates a
405child thread, that thread will belong to the domain as well.
406
407Remove a Memory Partition from a Memory Domain
408----------------------------------------------
409
410The following code shows how to remove a memory partition from a memory
411domain.
412
413.. code-block:: c
414
415    k_mem_domain_remove_partition(&app0_domain, &app0_part1);
416
417The k_mem_domain_remove_partition() API finds the memory partition
418that matches the given parameter and removes that partition from the
419memory domain.
420
421Available Partition Attributes
422------------------------------
423
424When defining a partition, we need to set access permission attributes
425to the partition. Since the access control of memory partitions relies on
426either an MPU or MMU, the available partition attributes would be architecture
427dependent.
428
429The complete list of available partition attributes for a specific architecture
430is found in the architecture-specific include file
431``include/zephyr/arch/<arch name>/arch.h``, (for example, :zephyr_file:`include/zephyr/arch/arm/arch.h`.)
432Some examples of partition attributes are:
433
434.. code-block:: c
435
436    /* Denote partition is privileged read/write, unprivileged read/write */
437    K_MEM_PARTITION_P_RW_U_RW
438    /* Denote partition is privileged read/write, unprivileged read-only */
439    K_MEM_PARTITION_P_RW_U_RO
440
441In almost all cases ``K_MEM_PARTITION_P_RW_U_RW`` is the right choice.
442
443Configuration Options
444*********************
445
446Related configuration options:
447
448* :kconfig:option:`CONFIG_MAX_DOMAIN_PARTITIONS`
449
450API Reference
451*************
452
453The following memory domain APIs are provided by :zephyr_file:`include/zephyr/kernel.h`:
454
455.. doxygengroup:: mem_domain_apis
456