@@ -47,35 +47,30 @@ psa_handle_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *
47
47
// Get active partition id - Needed for requester identification
48
48
spm_partition_t * curr_part_ptr = get_active_partition ();
49
49
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
- );
58
50
59
51
// 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 ;
63
57
64
58
// 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 ++ ) {
66
60
67
- expected = PSA_HANDLE_MGR_INVALID_HANDLE ;
61
+ psa_handle_t expected = PSA_NULL_HANDLE ;
68
62
69
63
// 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 );
71
65
72
66
// 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 ),
74
68
& expected ,
75
- new_handle
69
+ desired_handle
76
70
)) {
77
71
78
72
// Handle is successfully stored in handles pool
73
+ new_handle = desired_handle ;
79
74
80
75
// Store the handle memory in the handles pool, "coupled" with the stored handle
81
76
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 *
90
85
91
86
// Handle creation should only occur after a successful memory allocation
92
87
// and is not expected to fail.
93
- SPM_ASSERT (pool_ix != handle_mgr -> pool_size );
88
+ SPM_ASSERT (new_handle != PSA_NULL_HANDLE );
94
89
95
90
return new_handle ;
96
91
}
@@ -123,9 +118,9 @@ void psa_hndl_mgr_handle_destroy(psa_handle_manager_t *handle_mgr, psa_handle_t
123
118
SPM_PANIC ("[ERROR] Request for destroy by non-owner or friend!\n" );
124
119
}
125
120
126
- handle_mgr -> handles_pool [pool_ix ].handle = PSA_NULL_HANDLE ;
127
121
handle_mgr -> handles_pool [pool_ix ].handle_owner = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER ;
128
122
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 );
129
124
}
130
125
131
126
0 commit comments