General
Memory allocation and management is one of the most important things an operating system does. Dynamic memory allocation is used for allocating space for programs that are loaded from disk, as well as other tasks done by the kernel.
The kernel features an MPU (Memory Protection Unit) aware allocator, that makes it possible to restrict access between processes using the embedded hardware. All memory blocks that are directly available to user code is protected.
Additional (protected) memory may be requested by using the mmap
system call.
Memory allocation and the MPU
Because of the limited MPU, the management code has to take some special requirements into account. This is because the MPU only allows protection for regions with a size that is a power of two, with boundaries aligned to the same power of two. In order to make boundary aligned allocation easier, the information that describes what memory is allocated is separated and stored in a table within the internal RAM. The table size limits the number of allocations possible but it will be easier to align allocations without granting unintended access to the allocation metadata.
The allocation implementation has two methods available for use: one that makes sure the allocated region can be protected by an MPU region and one that does not. This way it is possible to allocate protected memory for processes and unprotected memory for use within the kernel, or when the MPU is not used.
External memory
Most microcontrollers have a rather small on-chip RAM in the kilobyte range. This is very often enough, but becomes a problem when loading program code to RAM is desired or large blocks of memory is necessary for things such as framebuffers. For instance, the STM32F407ZET6 only has 128 KiB of main RAM, and while it is a lot for a microcontroller it is not enough for complex applications in need of graphics or audio memory.
The kernel supports using external memory controllers, making the allocation pool much larger. As external memory is much slower than internal memory the text and stack sections are allocated in the smaller internal memory for speed while data is allocated in external memory.