Skip to content

Commit 19474fc

Browse files
author
Cruz Monrreal
authored
Merge pull request #9617 from kjbracey-arm/spe_atomics
SPE: Fix up atomic usage
2 parents feae56e + a3e7a6d commit 19474fc

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

components/TARGET_PSA/TARGET_MBED_SPM/COMPONENT_SPE/handles_manager.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,30 @@ psa_handle_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *
4747
// Get active partition id - Needed for requester identification
4848
spm_partition_t *curr_part_ptr = get_active_partition();
4949
int32_t current_pid = ((curr_part_ptr != NULL) ? curr_part_ptr->partition_id : PSA_NSPE_IDENTIFIER);
50-
uint32_t expected = UINT16_MAX;
51-
52-
// Avoid passing UINT16_MAX. Start again from 0 if reached.
53-
// The reason for this is that we use the 16 upper bits to store the handle's index in the handles pool (for performance reasons)
54-
core_util_atomic_cas_u32((uint32_t *)(&(handle_mgr->handle_generator)),
55-
&expected,
56-
PSA_HANDLE_MGR_INVALID_HANDLE
57-
);
5850

5951
// Generate a new handle identifier
60-
uint32_t tmp_handle = core_util_atomic_incr_u32(&(handle_mgr->handle_generator), 1);
61-
uint32_t new_handle = PSA_HANDLE_MGR_INVALID_HANDLE;
62-
uint32_t pool_ix = 0;
52+
uint32_t tmp_handle;
53+
do {
54+
tmp_handle = core_util_atomic_incr_u16(&(handle_mgr->handle_generator), 1);
55+
} while (tmp_handle == PSA_HANDLE_MGR_INVALID_HANDLE);
56+
psa_handle_t new_handle = PSA_NULL_HANDLE;
6357

6458
// Look for a vacant space in handles pool for the generated handle
65-
for (pool_ix = 0; pool_ix < handle_mgr->pool_size; pool_ix++) {
59+
for (uint32_t pool_ix = 0; pool_ix < handle_mgr->pool_size; pool_ix++) {
6660

67-
expected = PSA_HANDLE_MGR_INVALID_HANDLE;
61+
psa_handle_t expected = PSA_NULL_HANDLE;
6862

6963
// Write the handles pool index in the upper 16 bits of the handle
70-
new_handle = ((pool_ix << PSA_HANDLE_MGR_HANDLE_INDEX_POS) | tmp_handle);
64+
psa_handle_t desired_handle = ((pool_ix << PSA_HANDLE_MGR_HANDLE_INDEX_POS) | tmp_handle);
7165

7266
// Store the generated handle in the handles pool
73-
if (core_util_atomic_cas_u32((uint32_t *)(&(handle_mgr->handles_pool[pool_ix].handle)),
67+
if (core_util_atomic_cas_s32(&(handle_mgr->handles_pool[pool_ix].handle),
7468
&expected,
75-
new_handle
69+
desired_handle
7670
)) {
7771

7872
// Handle is successfully stored in handles pool
73+
new_handle = desired_handle;
7974

8075
// Store the handle memory in the handles pool, "coupled" with the stored handle
8176
handle_mgr->handles_pool[pool_ix].handle_mem = handle_mem;
@@ -90,7 +85,7 @@ psa_handle_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *
9085

9186
// Handle creation should only occur after a successful memory allocation
9287
// and is not expected to fail.
93-
SPM_ASSERT(pool_ix != handle_mgr->pool_size);
88+
SPM_ASSERT(new_handle != PSA_NULL_HANDLE);
9489

9590
return new_handle;
9691
}
@@ -123,9 +118,9 @@ void psa_hndl_mgr_handle_destroy(psa_handle_manager_t *handle_mgr, psa_handle_t
123118
SPM_PANIC("[ERROR] Request for destroy by non-owner or friend!\n");
124119
}
125120

126-
handle_mgr->handles_pool[pool_ix].handle = PSA_NULL_HANDLE;
127121
handle_mgr->handles_pool[pool_ix].handle_owner = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER;
128122
handle_mgr->handles_pool[pool_ix].handle_friend = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER;
123+
core_util_atomic_store_s32(&(handle_mgr->handles_pool[pool_ix].handle), PSA_NULL_HANDLE);
129124
}
130125

131126

components/TARGET_PSA/TARGET_MBED_SPM/COMPONENT_SPE/handles_manager.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extern "C" {
5555

5656
/* ------------------------------------ Definitions ---------------------------------- */
5757

58-
#define PSA_HANDLE_MGR_INVALID_HANDLE ((uint32_t)PSA_NULL_HANDLE)
58+
#define PSA_HANDLE_MGR_INVALID_HANDLE ((uint16_t)PSA_NULL_HANDLE)
5959

6060
#define PSA_HANDLE_MGR_INVALID_FRIEND_OWNER 0 // Denoting invalid friend or invalid owner
6161

@@ -80,8 +80,10 @@ typedef struct psa_handle_item_t {
8080

8181
typedef struct psa_handle_manager_t {
8282

83-
uint32_t handle_generator; /* A counter supplying handle numbers. */
84-
uint32_t pool_size; /* The maximum number of handles that pool can contain. */
83+
// Handle generator uses only 16 bits, and wraps.
84+
// The reason for this is that we use the 16 upper bits to store the handle's index in the handles pool (for performance reasons)
85+
uint16_t handle_generator; /* A counter supplying handle numbers. */
86+
uint16_t pool_size; /* The maximum number of handles that pool can contain. */
8587
psa_handle_item_t *handles_pool; /* Holds couples of handles and their memory "blocks". */
8688

8789
} psa_handle_manager_t;

components/TARGET_PSA/TARGET_MBED_SPM/COMPONENT_SPE/spm_common.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
*/
1717

1818
#include "cmsis_os2.h"
19+
#include "mbed_critical.h"
1920
#include "psa_defs.h"
2021
#include "spm_internal.h"
2122
#include "spm_panic.h"
2223

23-
bool core_util_atomic_cas_u8(volatile uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue);
24-
2524
inline void validate_iovec(
2625
const void *in_vec,
2726
const uint32_t in_len,
@@ -42,18 +41,19 @@ inline void validate_iovec(
4241

4342
inline void channel_state_switch(uint8_t *current_state, uint8_t expected_state, uint8_t new_state)
4443
{
45-
uint8_t backup_expected = expected_state;
46-
if (!core_util_atomic_cas_u8(current_state, &expected_state, new_state)) {
44+
uint8_t actual_state = expected_state;
45+
if (!core_util_atomic_cas_u8(current_state, &actual_state, new_state)) {
4746
SPM_PANIC("channel in incorrect processing state: %d while %d is expected!\n",
48-
expected_state, backup_expected);
47+
actual_state, expected_state);
4948
}
5049
}
5150

52-
inline void channel_state_assert(uint8_t *current_state, uint8_t expected_state)
51+
inline void channel_state_assert(const uint8_t *current_state, uint8_t expected_state)
5352
{
54-
if (*current_state != expected_state) {
53+
uint8_t actual_state = core_util_atomic_load_u8(current_state);
54+
if (actual_state != expected_state) {
5555
SPM_PANIC("channel in incorrect processing state: %d while %d is expected!\n",
56-
*current_state, expected_state);
56+
actual_state, expected_state);
5757
}
5858
}
5959

components/TARGET_PSA/TARGET_MBED_SPM/COMPONENT_SPE/spm_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void channel_state_switch(uint8_t *current_state, uint8_t expected_state, uint8_
242242
* @param[in] current_state - current state
243243
* @param[in] expected_state - expected state
244244
*/
245-
void channel_state_assert(uint8_t *current_state, uint8_t expected_state);
245+
void channel_state_assert(const uint8_t *current_state, uint8_t expected_state);
246246

247247
#ifdef __cplusplus
248248
}

0 commit comments

Comments
 (0)