Skip to content

Commit 61f1c1c

Browse files
author
Cruz Monrreal
authored
Merge pull request #9030 from kjbracey-arm/mpu_size
Reduce ROM impact of MPU code
2 parents 3df9421 + 390ef30 commit 61f1c1c

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

TESTS/mbed_hal/mpu/main.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@ static void hard_fault_handler_test()
7272

7373
static void mpu_fault_test(const volatile uint16_t *mem_function)
7474
{
75-
mbed_mpu_init();
76-
77-
// Verify that the mpu causes faults when executing ram
75+
// Verify that the mpu causes faults when executing ram after init
7876
fault_count = 0;
79-
mbed_mpu_enable_ram_xn(true);
77+
mbed_mpu_init();
8078
call_mem(mem_function);
8179
TEST_ASSERT_EQUAL(1, fault_count);
8280

@@ -86,7 +84,7 @@ static void mpu_fault_test(const volatile uint16_t *mem_function)
8684
call_mem(mem_function);
8785
TEST_ASSERT_EQUAL(0, fault_count);
8886

89-
// Verify that the mpu causes faults when executing ram
87+
// Verify that the mpu can be turned back on
9088
fault_count = 0;
9189
mbed_mpu_enable_ram_xn(true);
9290
call_mem(mem_function);

hal/mpu/mbed_mpu_v8m.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ void mbed_mpu_init()
7878
(3 << MPU_RBAR_AP_Pos) | // RO allowed by all privilege levels
7979
(0 << MPU_RBAR_XN_Pos); // Execute Never disabled
8080
MPU->RLAR = (0x1FFFFFFF & MPU_RLAR_LIMIT_Msk) | // Last address is 0x1FFFFFFF
81-
(region << MPU_RLAR_AttrIndx_Pos); // Attribute index - configured to be the same as the region number
81+
(region << MPU_RLAR_AttrIndx_Pos) | // Attribute index - configured to be the same as the region number
82+
(1 << MPU_RLAR_EN_Pos); // Region enabled
8283

8384
region = 1;
8485
MPU->RNR = region;
@@ -90,7 +91,8 @@ void mbed_mpu_init()
9091
(1 << MPU_RBAR_AP_Pos) | // RW allowed by all privilege levels
9192
(1 << MPU_RBAR_XN_Pos); // Execute Never enabled
9293
MPU->RLAR = (0x3FFFFFFF & MPU_RLAR_LIMIT_Msk) | // Last address is 0x3FFFFFFF
93-
(region << MPU_RLAR_AttrIndx_Pos); // Attribute index - configured to be the same as the region number
94+
(region << MPU_RLAR_AttrIndx_Pos) | // Attribute index - configured to be the same as the region number
95+
(1 << MPU_RLAR_EN_Pos); // Region enabled
9496

9597
region = 2;
9698
MPU->RNR = region;
@@ -102,7 +104,8 @@ void mbed_mpu_init()
102104
(1 << MPU_RBAR_AP_Pos) | // RW allowed by all privilege levels
103105
(1 << MPU_RBAR_XN_Pos); // Execute Never enabled
104106
MPU->RLAR = (0x7FFFFFFF & MPU_RLAR_LIMIT_Msk) | // Last address is 0x7FFFFFFF
105-
(region << MPU_RLAR_AttrIndx_Pos); // Attribute index - configured to be the same as the region number
107+
(region << MPU_RLAR_AttrIndx_Pos) | // Attribute index - configured to be the same as the region number
108+
(1 << MPU_RLAR_EN_Pos); // Region enabled
106109

107110
region = 3;
108111
MPU->RNR = region;
@@ -114,7 +117,8 @@ void mbed_mpu_init()
114117
(1 << MPU_RBAR_AP_Pos) | // RW allowed by all privilege levels
115118
(1 << MPU_RBAR_XN_Pos); // Execute Never enabled
116119
MPU->RLAR = (0x9FFFFFFF & MPU_RLAR_LIMIT_Msk) | // Last address is 0x9FFFFFFF
117-
(region << MPU_RLAR_AttrIndx_Pos); // Attribute index - configured to be the same as the region number
120+
(region << MPU_RLAR_AttrIndx_Pos) | // Attribute index - configured to be the same as the region number
121+
(1 << MPU_RLAR_EN_Pos); // Region enabled
118122

119123
// Enable the MPU
120124
MPU->CTRL =

hal/mpu_api.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ extern "C" {
6464
* Initialize the MPU
6565
*
6666
* Initialize or re-initialize the memory protection unit.
67-
* It is implementation defined what region are protected
68-
* by the MPU after initialization.
67+
* After initialization or re-initialization, ROM and RAM protection
68+
* are both enabled.
6969
*/
7070
void mbed_mpu_init(void);
7171

@@ -75,7 +75,9 @@ void mbed_mpu_init(void);
7575
* This function is used to mark all of ROM as read and execute only.
7676
* When enabled writes to ROM cause a fault.
7777
*
78-
* @param enable true to disable execution in ram, false otherwise
78+
* By default writes to ROM are disabled.
79+
*
80+
* @param enable true to disable writes to ROM, false otherwise
7981
*/
8082
void mbed_mpu_enable_rom_wn(bool enable);
8183

@@ -85,7 +87,9 @@ void mbed_mpu_enable_rom_wn(bool enable);
8587
* This function is used to mark all of RAM as execute never.
8688
* When enabled code is only allowed to execute from flash.
8789
*
88-
* @param enable true to disable execution in ram, false otherwise
90+
* By default execution from RAM is disabled.
91+
*
92+
* @param enable true to disable execution from RAM, false otherwise
8993
*/
9094
void mbed_mpu_enable_ram_xn(bool enable);
9195

platform/mbed_mpu_mgmt.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
#include "platform/mbed_mpu_mgmt.h"
1818
#include "platform/mbed_critical.h"
1919
#include "platform/mbed_error.h"
20+
#include "platform/mbed_assert.h"
2021
#include "hal/mpu_api.h"
2122
#include <limits.h>
2223

24+
#if DEVICE_MPU
25+
2326
static uint16_t mem_xn_lock;
2427
static uint16_t mem_wn_lock;
2528

2629
void mbed_mpu_manager_lock_ram_execution()
2730
{
2831
core_util_critical_section_enter();
29-
if (mem_xn_lock == USHRT_MAX) {
30-
core_util_critical_section_exit();
31-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OVERFLOW), "Ram execute never lock overflow (> USHRT_MAX)", mem_xn_lock);
32-
}
32+
MBED_ASSERT(mem_xn_lock != USHRT_MAX);
3333
if (mem_xn_lock == 0) {
3434
mbed_mpu_enable_ram_xn(false);
3535
}
@@ -40,10 +40,7 @@ void mbed_mpu_manager_lock_ram_execution()
4040
void mbed_mpu_manager_unlock_ram_execution()
4141
{
4242
core_util_critical_section_enter();
43-
if (mem_xn_lock == 0) {
44-
core_util_critical_section_exit();
45-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNDERFLOW), "Ram execute never lock underflow (< 0)", mem_xn_lock);
46-
}
43+
MBED_ASSERT(mem_xn_lock != 0);
4744
mem_xn_lock--;
4845
if (mem_xn_lock == 0) {
4946
mbed_mpu_enable_ram_xn(true);
@@ -54,10 +51,7 @@ void mbed_mpu_manager_unlock_ram_execution()
5451
void mbed_mpu_manager_lock_rom_write()
5552
{
5653
core_util_critical_section_enter();
57-
if (mem_wn_lock == USHRT_MAX) {
58-
core_util_critical_section_exit();
59-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OVERFLOW), "Rom write never lock overflow (> USHRT_MAX)", mem_wn_lock);
60-
}
54+
MBED_ASSERT(mem_wn_lock != USHRT_MAX);
6155
if (mem_wn_lock == 0) {
6256
mbed_mpu_enable_rom_wn(false);
6357
}
@@ -68,13 +62,12 @@ void mbed_mpu_manager_lock_rom_write()
6862
void mbed_mpu_manager_unlock_rom_write()
6963
{
7064
core_util_critical_section_enter();
71-
if (mem_wn_lock == 0) {
72-
core_util_critical_section_exit();
73-
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNDERFLOW), "Rom write never lock underflow (< 0)", mem_wn_lock);
74-
}
65+
MBED_ASSERT(mem_wn_lock != 0);
7566
mem_wn_lock--;
7667
if (mem_wn_lock == 0) {
7768
mbed_mpu_enable_rom_wn(true);
7869
}
7970
core_util_critical_section_exit();
8071
}
72+
73+
#endif

platform/mbed_mpu_mgmt.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
extern "C" {
3333
#endif
3434

35+
#if DEVICE_MPU
36+
3537
/** Lock ram execute never mode off
3638
*
3739
* This disables the MPU's execute never ram protection and allows
@@ -80,6 +82,18 @@ void mbed_mpu_manager_lock_rom_write(void);
8082
*/
8183
void mbed_mpu_manager_unlock_rom_write(void);
8284

85+
#else
86+
87+
#define mbed_mpu_manager_lock_ram_execution() (void)0
88+
89+
#define mbed_mpu_manager_unlock_ram_execution() (void)0
90+
91+
#define mbed_mpu_manager_lock_rom_write() (void)0
92+
93+
#define mbed_mpu_manager_unlock_rom_write() (void)0
94+
95+
#endif
96+
8397
#ifdef __cplusplus
8498
}
8599
#endif

rtos/TARGET_CORTEX/mbed_boot.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ uint32_t mbed_stack_isr_size = 0;
8989
void mbed_init(void)
9090
{
9191
mbed_mpu_init();
92-
mbed_mpu_enable_ram_xn(true);
93-
mbed_mpu_enable_rom_wn(true);
9492
mbed_cpy_nvic();
9593
mbed_sdk_init();
9694
mbed_rtos_init();

0 commit comments

Comments
 (0)