Skip to content

Commit 53be3cc

Browse files
author
Oren Cohen
committed
Fix the SPM HAL test
1 parent 9406450 commit 53be3cc

File tree

2 files changed

+96
-56
lines changed

2 files changed

+96
-56
lines changed

TESTS/mbed_hal/spm/fault_functions.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
#ifndef __MBED_HAL_SPM_FAULT_FUNCTIONS__
18+
#define __MBED_HAL_SPM_FAULT_FUNCTIONS__
19+
20+
#include "cmsis_compiler.h"
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
// Retruns the value of the LR register
27+
// Used to determine which stack the exception happend in
28+
__STATIC_FORCEINLINE uint32_t __get_LR(void);
29+
30+
// This function is required as we need a symbol/address
31+
// to jump to from fault handler.
32+
void do_nothing(void);
33+
34+
// Test exception handler
35+
static void hard_fault_handler_test();
36+
37+
// Using naked function as it will not be executed from beginning to the end.
38+
// The execution flow expected to be interrupted by exception and we will
39+
// return to other function.
40+
// compiler will not produce prolog and epilog code for naked function
41+
// and thus will preserve stack in un-corrupted state
42+
__attribute__((naked)) void call_mem(uint32_t addr);
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif // __MBED_HAL_SPM_FAULT_FUNCTIONS__

TESTS/mbed_hal/spm/main.cpp

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,82 +14,74 @@
1414
* limitations under the License.
1515
*/
1616

17+
#if !defined(COMPONENT_PSA_SRV_IPC)
18+
#error [NOT_SUPPORTED] Test supported only on PSA targets
19+
#endif
20+
21+
#if (defined( __CC_ARM ) || defined(__ARMCC_VERSION) || defined( __ICCARM__ ))
22+
#error [NOT_SUPPORTED] this test is supported on GCC only
23+
#endif
24+
1725
#include "utest/utest.h"
1826
#include "unity/unity.h"
1927
#include "greentea-client/test_env.h"
2028
#include "cmsis.h"
2129
#include "spm_api.h"
2230
#include <stdlib.h>
31+
#include "fault_functions.h"
2332

2433
using namespace utest::v1;
2534

26-
#if !defined(COMPONENT_PSA_SRV_IPC)
27-
#error [NOT_SUPPORTED] Test supported only on PSA targets
28-
#endif
29-
30-
#if !defined ( __GNUC__ )
31-
#error [NOT_SUPPORTED] this test is supported on GCC only
32-
#endif
3335

34-
extern "C" {
3536
#define HARDFAULT_IRQn ((IRQn_Type)-13)
3637
#define EXC_RETURN_RETURN_STACK_MSK ((uint32_t)(0x00000004))
3738
#define PC_INDEX_IN_STACK_FRAME 6
3839

39-
volatile uint32_t fault_occurred;
40-
uint32_t real_hard_fault_handler;
40+
volatile uint32_t fault_occurred;
41+
uint32_t real_hard_fault_handler;
4142

42-
__attribute__((always_inline)) __STATIC_INLINE uint32_t __get_LR(void)
43-
{
44-
register uint32_t result;
43+
__STATIC_FORCEINLINE uint32_t __get_LR(void)
44+
{
45+
uint32_t result;
4546

46-
__ASM volatile("MOV %0, LR\n" : "=r"(result));
47-
return result;
48-
}
47+
__ASM volatile("MOV %0, lr" : "=r"(result));
48+
return (result);
49+
}
4950

50-
// This function is required as we need a symbol/address
51-
// to jump to from fault handler.
52-
void do_nothing(void)
53-
{
54-
__NOP();
55-
}
51+
void do_nothing(void)
52+
{
53+
__NOP();
54+
}
5655

57-
// Test exception handler
58-
static void hard_fault_handler_test()
59-
{
60-
fault_occurred++;
61-
// LR is set EXC_RETURN
62-
// lowest bits identify PSP vs MSP stack used for stacking
63-
uint32_t lr = __get_LR();
64-
uint32_t sp;
65-
66-
if (lr & EXC_RETURN_RETURN_STACK_MSK) {
67-
sp = __get_PSP();
68-
} else {
69-
sp = __get_MSP();
70-
}
71-
72-
// Overwrite return address.
73-
// Fake return to a our special function since current
74-
// instruction under test will always fail due to memory protection
75-
((uint32_t *)sp)[PC_INDEX_IN_STACK_FRAME] = (uint32_t)do_nothing;
56+
static void hard_fault_handler_test()
57+
{
58+
fault_occurred++;
59+
// LR is set EXC_RETURN
60+
// lowest bits identify PSP vs MSP stack used for stacking
61+
uint32_t lr = __get_LR();
62+
uint32_t sp;
63+
64+
if (lr & EXC_RETURN_RETURN_STACK_MSK) {
65+
sp = __get_PSP();
66+
} else {
67+
sp = __get_MSP();
7668
}
7769

78-
// Using naked function as it will not be executed from beginning to the end.
79-
// The execution flow expected to be interrupted by exception and we will
80-
// return to other function.
81-
// compiler will not produce prolog and epilog code for naked function
82-
// and thus will preserve stack in un-corrupted state
83-
__attribute__((naked)) void call_mem(uint32_t addr)
84-
{
85-
// Only first instruction will be executed in positive flow,
86-
// since exception will be generated for invalid memory access.
87-
// Other instructions are for calling do_nothing function according to AAPCS.
88-
__ASM(
89-
"LDR r1, [r0]\n"
90-
"BX lr\n"
91-
);
92-
}
70+
// Overwrite return address.
71+
// Fake return to a our special function since current
72+
// instruction under test will always fail due to memory protection
73+
((uint32_t *)sp)[PC_INDEX_IN_STACK_FRAME] = (uint32_t)do_nothing;
74+
}
75+
76+
__attribute__((naked)) void call_mem(uint32_t addr)
77+
{
78+
// Only first instruction will be executed in positive flow,
79+
// since exception will be generated for invalid memory access.
80+
// Other instructions are for calling do_nothing function according to AAPCS.
81+
__ASM(
82+
"LDR r1, [r0]\n"
83+
"BX lr\n"
84+
);
9385
}
9486

9587
static void test_memory(uint32_t addr, uint32_t expected_fatal_count)

0 commit comments

Comments
 (0)