|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "utest/utest.h" |
| 18 | +#include "unity/unity.h" |
| 19 | +#include "greentea-client/test_env.h" |
| 20 | + |
| 21 | +#include "cmsis.h" |
| 22 | +#include <stdlib.h> |
| 23 | + |
| 24 | +#include "mpu_api.h" |
| 25 | +#include "mpu_test.h" |
| 26 | + |
| 27 | +#if !DEVICE_MPU |
| 28 | +#error [NOT_SUPPORTED] MPU API not supported for this target |
| 29 | +#endif |
| 30 | + |
| 31 | +using namespace utest::v1; |
| 32 | + |
| 33 | +#define HARDFAULT_IRQn ((IRQn_Type)-13) |
| 34 | +#define MEMFAULT_IRQn ((IRQn_Type)-12) |
| 35 | + |
| 36 | +// Assembly return instruction: bx lr |
| 37 | +#define ASM_BX_LR 0x4770 |
| 38 | + |
| 39 | +volatile uint32_t fault_count; |
| 40 | +uint32_t real_hard_fault_handler; |
| 41 | +uint32_t real_mem_fault_handler; |
| 42 | + |
| 43 | +static volatile uint16_t data_function = ASM_BX_LR; |
| 44 | +static volatile uint16_t bss_function; |
| 45 | + |
| 46 | +static void clear_caches() |
| 47 | +{ |
| 48 | +#if defined(__CORTEX_M7) |
| 49 | + /* Data cache clean and invalid */ |
| 50 | + SCB_CleanInvalidateDCache(); |
| 51 | + |
| 52 | + /* Instruction cache invalid */ |
| 53 | + SCB_InvalidateICache(); |
| 54 | +#endif |
| 55 | + |
| 56 | + __ISB(); |
| 57 | + __DSB(); |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +static void call_mem(const volatile uint16_t *mem_function) |
| 62 | +{ |
| 63 | + // or the address with 1 to ensure the thumb bit is set |
| 64 | + ((void (*)())((uint32_t)mem_function | 1))(); |
| 65 | +} |
| 66 | + |
| 67 | +static void hard_fault_handler_test() |
| 68 | +{ |
| 69 | + fault_count++; |
| 70 | + mbed_mpu_enable_ram_xn(false); |
| 71 | +} |
| 72 | + |
| 73 | +static void mpu_fault_test(const volatile uint16_t *mem_function) |
| 74 | +{ |
| 75 | + mbed_mpu_init(); |
| 76 | + |
| 77 | + // Verify that the mpu causes faults when executing ram |
| 78 | + fault_count = 0; |
| 79 | + mbed_mpu_enable_ram_xn(true); |
| 80 | + call_mem(mem_function); |
| 81 | + TEST_ASSERT_EQUAL(1, fault_count); |
| 82 | + |
| 83 | + // Verify that the mpu can be turned off |
| 84 | + fault_count = 0; |
| 85 | + mbed_mpu_enable_ram_xn(false); |
| 86 | + call_mem(mem_function); |
| 87 | + TEST_ASSERT_EQUAL(0, fault_count); |
| 88 | + |
| 89 | + // Verify that the mpu causes faults when executing ram |
| 90 | + fault_count = 0; |
| 91 | + mbed_mpu_enable_ram_xn(true); |
| 92 | + call_mem(mem_function); |
| 93 | + TEST_ASSERT_EQUAL(1, fault_count); |
| 94 | + |
| 95 | + // Verify that free turns off the mpu |
| 96 | + fault_count = 0; |
| 97 | + mbed_mpu_free(); |
| 98 | + call_mem(mem_function); |
| 99 | + TEST_ASSERT_EQUAL(0, fault_count); |
| 100 | +} |
| 101 | + |
| 102 | +void mpu_init_test() |
| 103 | +{ |
| 104 | + for (int i = 0; i < 10; i++) { |
| 105 | + mbed_mpu_init(); |
| 106 | + } |
| 107 | + |
| 108 | + mbed_mpu_free(); |
| 109 | +} |
| 110 | + |
| 111 | +void mpu_free_test() |
| 112 | +{ |
| 113 | + mbed_mpu_init(); |
| 114 | + |
| 115 | + // Enable the MPU |
| 116 | + mbed_mpu_enable_ram_xn(true); |
| 117 | + |
| 118 | + // Free and ensure execution from RAM is allowed |
| 119 | + mbed_mpu_free(); |
| 120 | + |
| 121 | + call_mem(&data_function); |
| 122 | +} |
| 123 | + |
| 124 | +void mpu_fault_test_data() |
| 125 | +{ |
| 126 | + mpu_fault_test(&data_function); |
| 127 | +} |
| 128 | + |
| 129 | +void mpu_fault_test_bss() |
| 130 | +{ |
| 131 | + bss_function = ASM_BX_LR; |
| 132 | + clear_caches(); |
| 133 | + mpu_fault_test(&bss_function); |
| 134 | +} |
| 135 | + |
| 136 | +void mpu_fault_test_stack() |
| 137 | +{ |
| 138 | + uint16_t stack_function; |
| 139 | + |
| 140 | + stack_function = ASM_BX_LR; |
| 141 | + clear_caches(); |
| 142 | + mpu_fault_test(&stack_function); |
| 143 | +} |
| 144 | + |
| 145 | +void mpu_fault_test_heap() |
| 146 | +{ |
| 147 | + uint16_t *heap_function = (uint16_t *)malloc(2); |
| 148 | + |
| 149 | + TEST_ASSERT_NOT_EQUAL(NULL, heap_function); |
| 150 | + *heap_function = ASM_BX_LR; |
| 151 | + clear_caches(); |
| 152 | + mpu_fault_test(heap_function); |
| 153 | + |
| 154 | + free(heap_function); |
| 155 | +} |
| 156 | + |
| 157 | +utest::v1::status_t fault_override_setup(const Case *const source, const size_t index_of_case) |
| 158 | +{ |
| 159 | + // Save old fault handlers and replace it with a new one |
| 160 | + real_hard_fault_handler = NVIC_GetVector(HARDFAULT_IRQn); |
| 161 | + real_mem_fault_handler = NVIC_GetVector(MEMFAULT_IRQn); |
| 162 | + NVIC_SetVector(HARDFAULT_IRQn, (uint32_t)&hard_fault_handler_test); |
| 163 | + NVIC_SetVector(MEMFAULT_IRQn, (uint32_t)&hard_fault_handler_test); |
| 164 | + |
| 165 | + return greentea_case_setup_handler(source, index_of_case); |
| 166 | +} |
| 167 | + |
| 168 | +utest::v1::status_t fault_override_teardown(const Case *const source, const size_t passed, const size_t failed, |
| 169 | + const failure_t reason) |
| 170 | +{ |
| 171 | + // Restore real fault handlers |
| 172 | + NVIC_SetVector(HARDFAULT_IRQn, real_hard_fault_handler); |
| 173 | + NVIC_SetVector(MEMFAULT_IRQn, real_mem_fault_handler); |
| 174 | + |
| 175 | + return greentea_case_teardown_handler(source, passed, failed, reason); |
| 176 | +} |
| 177 | + |
| 178 | +Case cases[] = { |
| 179 | + Case("MPU - init", fault_override_setup, mpu_init_test, fault_override_teardown), |
| 180 | + Case("MPU - free", fault_override_setup, mpu_free_test, fault_override_teardown), |
| 181 | +#if !((__ARM_ARCH_8M_BASE__ == 1U) || (__ARM_ARCH_8M_MAIN__ == 1U)) |
| 182 | + // Skip fault tests for ARMv8-M until a fault handler hook is provided |
| 183 | + Case("MPU - data fault", fault_override_setup, mpu_fault_test_data, fault_override_teardown), |
| 184 | + Case("MPU - bss fault", fault_override_setup, mpu_fault_test_bss, fault_override_teardown), |
| 185 | + Case("MPU - stack fault", fault_override_setup, mpu_fault_test_stack, fault_override_teardown), |
| 186 | + Case("MPU - heap fault", fault_override_setup, mpu_fault_test_heap, fault_override_teardown) |
| 187 | +#endif |
| 188 | +}; |
| 189 | + |
| 190 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 191 | +{ |
| 192 | + GREENTEA_SETUP(20, "default_auto"); |
| 193 | + return greentea_test_setup_handler(number_of_cases); |
| 194 | +} |
| 195 | + |
| 196 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 197 | + |
| 198 | +int main() |
| 199 | +{ |
| 200 | + Harness::run(specification); |
| 201 | +} |
0 commit comments