Mbed OS Reference
Loading...
Searching...
No Matches
mpu_api.h
1
2/** \addtogroup hal */
3/** @{*/
4/* mbed Microcontroller Library
5 * Copyright (c) 2018-2018 ARM Limited
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#ifndef MBED_MPU_API_H
21#define MBED_MPU_API_H
22
23#include "device.h"
24#include <stdbool.h>
25#include "cmsis.h"
26
27#include <mstd_cstddef>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#if DEVICE_MPU
34
35/**
36 * \defgroup hal_mpu MPU hal
37 *
38 * The MPU hal provides a simple MPU API to enhance device security by preventing
39 * execution from ram.
40 *
41 * # Defined behavior
42 * * The function ::mbed_mpu_init is safe to call repeatedly - Verified by mpu_init_test
43 * * The function ::mbed_mpu_free disables MPU protection - Verified by mpu_free_test
44 * * Execution from RAM results in a fault when execute never is enabled.
45 * This RAM includes heap, stack, data and zero init - Verified by mpu_fault_test_data,
46 * mpu_fault_test_bss, mpu_fault_test_stack and mpu_fault_test_heap.
47 * * Writing to ROM results in a fault when write never is enabled - Not verified
48 *
49 * # Undefined behavior
50 * * Calling any function other than ::mbed_mpu_init before the initialization of the MPU.
51 *
52 * @see hal_mpu_tests
53 *
54 * @{
55 */
56
57/**
58 * \defgroup hal_mpu_tests MPU hal tests
59 * The MPU test validates proper implementation of the MPU hal.
60 *
61 * To run the MPU hal tests use the command:
62 *
63 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-mpu*
64 */
65
66#if !defined(MBED_MPU_CUSTOM)
67
68#ifdef MBED_CONF_TARGET_MPU_ROM_END
69#define MBED_MPU_ROM_END MBED_CONF_TARGET_MPU_ROM_END
70#else
71#define MBED_MPU_ROM_END (0x10000000 - 1)
72#endif
73
74#ifdef MBED_CONF_TARGET_MPU_RAM_START
75#define MBED_MPU_RAM_START MBED_CONF_TARGET_MPU_RAM_START
76#else
77// Default to the end of ROM
78#define MBED_MPU_RAM_START (MBED_MPU_ROM_END + 1)
79#endif
80
81#if ((__ARM_ARCH_8M_BASE__ == 1U) || (__ARM_ARCH_8M_MAIN__ == 1U) || (__ARM_ARCH_8_1M_MAIN__ == 1U))
82/// On ARMv8 cores, MPU regions must use one of 8 global "attribute registers", which give the attributes
83/// for one or more memory regions. This enum gives the attribute registers (i.e. the first arg to
84/// \c ARM_MPU_SetMemAttr() ) that are defined by Mbed.
85/// The application is free to use attribute registers above this number.
86enum mbed_mpu_attr_index {
87 /// Normal memory, write-through (i.e. writes are always sent immediately to main memory)
88 MBED_MPU_ATTR_INDEX_NORMAL_WRITE_THROUGH = 0,
89
90 /// Normal memory, write-back (i.e. writes may only write to the cache immediately, and get synced to main memory later)
91 MBED_MPU_ATTR_INDEX_NORMAL_WRITE_BACK = 1,
92
93 /// Non-cacheable: Reads and writes hit main memory directly.
94 /// Note that this is still normal memory, not device, so the CPU can still reorder accesses.
95 MBED_MPU_ATTR_INDEX_NON_CACHEABLE = 2,
96};
97#endif
98
99/// Number of MPU regions that will be used by Mbed OS's MPU configuration.
100/// The application is generally free to use regions above this number.
101static MSTD_CONSTEXPR_OBJ_11 size_t mbed_used_mpu_regions =
102#if ((__ARM_ARCH_8M_BASE__ == 1U) || (__ARM_ARCH_8M_MAIN__ == 1U) || (__ARM_ARCH_8_1M_MAIN__ == 1U))
103 4
104#else
105 3
106#if MBED_MPU_RAM_START < 0x20000000
107 + 1
108#endif
109#endif
110#if __DCACHE_PRESENT
111 + 1
112#endif
113#if MBED_MPU_HAS_RAM_FUNCTION_REGION
114 + 1
115#endif
116 ;
117#endif
118
119/**
120 * Initialize the MPU
121 *
122 * Initialize or re-initialize the memory protection unit.
123 * After initialization or re-initialization, ROM and RAM protection
124 * are both enabled.
125 */
126void mbed_mpu_init(void);
127
128/**
129 * Enable or disable ROM MPU protection
130 *
131 * This function is used to mark all of ROM as read and execute only.
132 * When enabled writes to ROM cause a fault.
133 *
134 * By default writes to ROM are disabled.
135 *
136 * @param disable true to disable writes to ROM, false otherwise
137 */
138void mbed_mpu_enable_rom_wn(bool disable);
139
140/**
141 * Enable or disable ram MPU protection
142 *
143 * This function is used to mark all of RAM as execute never.
144 * When enabled code is only allowed to execute from flash.
145 *
146 * By default execution from RAM is disabled.
147 *
148 * @param disable true to disable execution from RAM, false otherwise
149 */
150void mbed_mpu_enable_ram_xn(bool disable);
151
152/** Deinitialize the MPU
153 *
154 * Powerdown the MPU in preparation for powerdown, reset or jumping to another application.
155 */
156void mbed_mpu_free(void);
157
158/**@}*/
159
160#else
161
162#define mbed_mpu_init()
163
164#define mbed_mpu_enable_rom_wn(enable) (void)enable
165
166#define mbed_mpu_enable_ram_xn(enable) (void)enable
167
168#define mbed_mpu_free()
169
170#endif
171
172#ifdef __cplusplus
173}
174#endif
175
176#endif
177
178/** @}*/
void mbed_mpu_enable_rom_wn(bool disable)
Enable or disable ROM MPU protection.
void mbed_mpu_init(void)
Initialize the MPU.
void mbed_mpu_enable_ram_xn(bool disable)
Enable or disable ram MPU protection.
void mbed_mpu_free(void)
Deinitialize the MPU.