@@ -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
0 commit comments