Skip to content

Commit 4c68b71

Browse files
danny4478Michael Schwarcz
authored andcommitted
Add index hint to handle in handles manager
* Done to get better performance * Remove handles manager init() API function - initialize statically * Add handles manager initialization reboot flow for neg tests * Fix some compilation warnings
1 parent 3d40003 commit 4c68b71

File tree

7 files changed

+131
-131
lines changed

7 files changed

+131
-131
lines changed

TESTS/spm/neg_client_tests/spm_reboot.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ void spm_reboot(void)
5555
memset(g_active_messages_data, 0, (sizeof(spm_active_msg_t) * MBED_CONF_SPM_IPC_MAX_NUM_OF_MESSAGES));
5656
memset(&g_spm, 0, sizeof(g_spm));
5757

58+
g_spm.channels_handle_mgr.handle_generator = PSA_HANDLE_MGR_INVALID_HANDLE;
59+
g_spm.channels_handle_mgr.pool_size = MBED_CONF_SPM_IPC_MAX_NUM_OF_CHANNELS;
60+
g_spm.channels_handle_mgr.handles_pool = g_channels_handle_storage;
61+
g_spm.messages_handle_mgr.handle_generator = PSA_HANDLE_MGR_INVALID_HANDLE;
62+
g_spm.messages_handle_mgr.pool_size = MBED_CONF_SPM_IPC_MAX_NUM_OF_MESSAGES;
63+
g_spm.messages_handle_mgr.handles_pool = g_messages_handle_storage;
64+
5865
psa_spm_init();
5966
PSA_UNUSED(status);
6067
}

TESTS/spm/neg_dual_partition/spm_reboot.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ void spm_reboot(void)
5555
memset(g_active_messages_data, 0, (sizeof(spm_active_msg_t) * MBED_CONF_SPM_IPC_MAX_NUM_OF_MESSAGES));
5656
memset(&g_spm, 0, sizeof(g_spm));
5757

58+
g_spm.channels_handle_mgr.handle_generator = PSA_HANDLE_MGR_INVALID_HANDLE;
59+
g_spm.channels_handle_mgr.pool_size = MBED_CONF_SPM_IPC_MAX_NUM_OF_CHANNELS;
60+
g_spm.channels_handle_mgr.handles_pool = g_channels_handle_storage;
61+
g_spm.messages_handle_mgr.handle_generator = PSA_HANDLE_MGR_INVALID_HANDLE;
62+
g_spm.messages_handle_mgr.pool_size = MBED_CONF_SPM_IPC_MAX_NUM_OF_MESSAGES;
63+
g_spm.messages_handle_mgr.handles_pool = g_messages_handle_storage;
64+
5865
psa_spm_init();
5966
PSA_UNUSED(status);
6067
}

TESTS/spm/neg_server_tests/spm_reboot.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ void spm_reboot(void)
5555
memset(g_active_messages_data, 0, (sizeof(spm_active_msg_t) * MBED_CONF_SPM_IPC_MAX_NUM_OF_MESSAGES));
5656
memset(&g_spm, 0, sizeof(g_spm));
5757

58+
g_spm.channels_handle_mgr.handle_generator = PSA_HANDLE_MGR_INVALID_HANDLE;
59+
g_spm.channels_handle_mgr.pool_size = MBED_CONF_SPM_IPC_MAX_NUM_OF_CHANNELS;
60+
g_spm.channels_handle_mgr.handles_pool = g_channels_handle_storage;
61+
g_spm.messages_handle_mgr.handle_generator = PSA_HANDLE_MGR_INVALID_HANDLE;
62+
g_spm.messages_handle_mgr.pool_size = MBED_CONF_SPM_IPC_MAX_NUM_OF_MESSAGES;
63+
g_spm.messages_handle_mgr.handles_pool = g_messages_handle_storage;
64+
5865
psa_spm_init();
5966
PSA_UNUSED(status);
6067
}

spm/handles_manager.c

Lines changed: 78 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030

3131

3232

33+
/* ------------------------------------ Definitions ---------------------------------- */
34+
35+
#define PSA_HANDLE_MGR_HANDLE_INDEX_POS 16
36+
#define PSA_HANDLE_MGR_HANDLE_INDEX_MSK 0xFFFF
37+
38+
39+
3340
/* -------------------------------- Handle Manager Module ---------------------------- */
3441

3542
/* The Handle Manager Module manages handles.
@@ -40,7 +47,6 @@
4047
* handle memory.
4148
*
4249
* Users can:
43-
* - Initiate the module
4450
* - Ask for a unique handle identifier for a given handle memory [handle_create]
4551
* - Ask for a pointer to the handle memory corresponding to a
4652
* handle identifier [handle_get_mem]
@@ -56,45 +62,6 @@
5662

5763
/* ------------------------------------- Functions ----------------------------------- */
5864

59-
/**********************************************************************************************************************************
60-
* Function : psa_hndl_mgr_init
61-
*
62-
* Description: This function initializes a new handle manager object.
63-
* It should be called by users who want to use the handle manager module.
64-
*
65-
* Parameters : handle_mgr - [OUT] A pointer to a handle manager object, to be filled in with handle manager instance data.
66-
* Should be allocated by the caller.
67-
* handle_mgr_storage - [IN] A pointer to the the module storage [the handles pool].
68-
* Should be allocated by the caller.
69-
* storage_size - [IN] Number of elements in <handle_mgr_storage>.
70-
*
71-
* Return : Void
72-
*********************************************************************************************************************************/
73-
void psa_hndl_mgr_init(psa_handle_manager_t *handle_mgr, psa_handle_item_t *handle_mgr_storage, size_t storage_size)
74-
{
75-
/* Make sanity checks on arguments */
76-
77-
SPM_ASSERT(handle_mgr != NULL);
78-
SPM_ASSERT(handle_mgr_storage != NULL);
79-
SPM_ASSERT(storage_size != 0);
80-
81-
/* Reset and validate handle manager object */
82-
83-
memset(handle_mgr_storage, 0, sizeof(*handle_mgr_storage));
84-
memset(handle_mgr, 0, sizeof(*handle_mgr));
85-
86-
for (uint32_t idx = 0; idx < storage_size; idx++) {
87-
88-
handle_mgr_storage[idx].handle_owner = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER;
89-
handle_mgr_storage[idx].handle_friend = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER;
90-
}
91-
92-
handle_mgr->handle_generator = PSA_HANDLE_MGR_INVALID_HANDLE;
93-
handle_mgr->pool_size = storage_size;
94-
handle_mgr->handles_pool = handle_mgr_storage;
95-
}
96-
97-
9865
/**********************************************************************************************************************************
9966
* Function : psa_hndl_mgr_handle_create
10067
*
@@ -103,18 +70,15 @@ void psa_hndl_mgr_init(psa_handle_manager_t *handle_mgr, psa_handle_item_t *hand
10370
*
10471
* Parameters : handle_mgr - [IN] A pointer to the handle manager object
10572
* handle_mem - [IN] A pointer to a pre-allocated handle memory to get a handle identifier for
106-
* friend_pid - [IN] The partition id which is allowed to get_mem() in addition to the handle owner.
73+
* friend_pid - [IN] The partition id which is allowed to get_mem() and destroy() in addition to the handle owner.
10774
* Use PSA_HANDLE_MGR_INVALID_FRIEND_OWNER to denote there is no friend partition.
10875
* handle - [OUT] A pointer to a handle, to be set with the created handle identifier
10976
*
11077
* Return : PSA_STATUS_SUCCESS on success. Error code on failure.
11178
*********************************************************************************************************************************/
11279
error_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *handle_mem, int32_t friend_pid, psa_handle_t *handle)
11380
{
114-
uint32_t new_handle = PSA_HANDLE_MGR_INVALID_HANDLE;
115-
116-
117-
/* Make sanity checks on arguments */
81+
// Make sanity checks on arguments
11882
SPM_ASSERT(handle_mgr != NULL);
11983
SPM_ASSERT(handle_mem != NULL);
12084
SPM_ASSERT(handle != NULL);
@@ -123,34 +87,38 @@ error_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *handl
12387
*handle = PSA_NULL_HANDLE;
12488

12589

126-
/* Get active partition id - Needed for requester identification */
90+
// Get active partition id - Needed for requester identification
12791
spm_partition_t *curr_part_ptr = get_active_partition();
128-
int32_t current_pid = ((curr_part_ptr != NULL) ? curr_part_ptr->partition_id : PSA_NSPE_IDENTIFIER);
129-
uint32_t expected = INT32_MAX;
130-
/* Avoid passing INT32_MAX. Start again from 0 if reached.
131-
* The reason for that is that SPM which uses the handles manager will relate to a negative int32_t integer
132-
* as an error code and not as a valid handle.
133-
* */
92+
int32_t current_pid = ((curr_part_ptr != NULL) ? curr_part_ptr->partition_id : PSA_NSPE_IDENTIFIER);
93+
uint32_t expected = UINT16_MAX;
94+
95+
// Avoid passing UINT16_MAX. Start again from 0 if reached.
96+
// 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)
13497
core_util_atomic_cas_u32( (uint32_t *)( &(handle_mgr->handle_generator) ),
135-
&expected,
136-
PSA_HANDLE_MGR_INVALID_HANDLE
137-
);
98+
&expected,
99+
PSA_HANDLE_MGR_INVALID_HANDLE
100+
);
138101

139-
/* Generate a new handle identifier */
140-
new_handle = core_util_atomic_incr_u32(&(handle_mgr->handle_generator), 1);
102+
// Generate a new handle identifier
103+
uint32_t tmp_handle = core_util_atomic_incr_u32(&(handle_mgr->handle_generator), 1);
141104

142-
/* Look for a vacant space in handles pool for the generated handle */
105+
// Look for a vacant space in handles pool for the generated handle
143106
for(uint32_t pool_ix = 0; pool_ix < handle_mgr->pool_size; pool_ix++) {
107+
144108
expected = PSA_HANDLE_MGR_INVALID_HANDLE;
145-
/* Store the generated handle in the handles pool */
109+
110+
// Write the handles pool index in the upper 16 bits of the handle
111+
uint32_t new_handle = ((pool_ix << PSA_HANDLE_MGR_HANDLE_INDEX_POS) | tmp_handle);
112+
113+
// Store the generated handle in the handles pool
146114
if(core_util_atomic_cas_u32( (uint32_t *)( &(handle_mgr->handles_pool[pool_ix].handle) ),
147-
&expected,
148-
new_handle
149-
)) {
115+
&expected,
116+
new_handle
117+
)) {
150118

151-
/* Handle is successfully stored in handles pool */
119+
// Handle is successfully stored in handles pool
152120

153-
/* Store the handle memory in the handles pool, "coupled" with the stored handle */
121+
// Store the handle memory in the handles pool, "coupled" with the stored handle
154122
handle_mgr->handles_pool[pool_ix].handle_mem = handle_mem;
155123
handle_mgr->handles_pool[pool_ix].handle_owner = current_pid;
156124
handle_mgr->handles_pool[pool_ix].handle_friend = friend_pid;
@@ -160,11 +128,11 @@ error_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *handl
160128
return PSA_SUCCESS;
161129
}
162130

163-
/* Occupied index in handles pool - continue looping */
131+
// Occupied index in handles pool - continue looping
164132
}
165133

166134

167-
/* No vacant space for new handle. No worries about the generated handle - it will not be used */
135+
// No vacant space for new handle. No worries about the generated handle - it will not be used
168136

169137
return PSA_GENERIC_ERROR;
170138
}
@@ -182,50 +150,45 @@ error_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *handl
182150
*********************************************************************************************************************************/
183151
void psa_hndl_mgr_handle_destroy(psa_handle_manager_t *handle_mgr, psa_handle_t handle)
184152
{
185-
/* Make sanity checks on arguments */
153+
// Make sanity checks on arguments
186154
SPM_ASSERT(handle_mgr != NULL);
187155
SPM_ASSERT(handle != PSA_NULL_HANDLE);
188156

189157

190-
/* Get active partition id - Needed for requester identification */
191-
spm_partition_t *curr_part_ptr = get_active_partition();
192-
int32_t current_pid = ((curr_part_ptr != NULL) ? curr_part_ptr->partition_id : PSA_NSPE_IDENTIFIER);
193-
194-
195-
/* Look for <handle> in handles pool */
196-
for(uint32_t pool_ix = 0; pool_ix < handle_mgr->pool_size; pool_ix++) {
197-
198-
if(handle_mgr->handles_pool[pool_ix].handle == handle) {
199-
200-
if((handle_mgr->handles_pool[pool_ix].handle_owner != current_pid) &&
201-
(handle_mgr->handles_pool[pool_ix].handle_friend != current_pid)) {
158+
// Get the handle's index in the handles pool
159+
uint32_t pool_ix = ((handle >> PSA_HANDLE_MGR_HANDLE_INDEX_POS) & PSA_HANDLE_MGR_HANDLE_INDEX_MSK);
160+
if(pool_ix >= handle_mgr->pool_size)
161+
{
162+
SPM_PANIC("[ERROR] Handle's index [%d] is bigger than handles pool size [%d]! \n", (int)pool_ix, (int)(handle_mgr->pool_size));
163+
}
202164

203-
// The SPM_PANIC() macro will exit the program
204-
SPM_PANIC("[ERROR] Request for destroy by non-owner or friend!\n");
205-
}
165+
if(handle_mgr->handles_pool[pool_ix].handle != handle) {
166+
SPM_PANIC("[ERROR] Handle %d is not found in expected index! \n", (int)handle);
167+
}
206168

207-
/* Handle found in handles pool */
169+
// Get active partition id - Needed for requester identification
170+
spm_partition_t *curr_part_ptr = get_active_partition();
171+
int32_t current_pid = ((curr_part_ptr != NULL) ? curr_part_ptr->partition_id : PSA_NSPE_IDENTIFIER);
208172

209-
handle_mgr->handles_pool[pool_ix].handle = PSA_NULL_HANDLE;
210-
handle_mgr->handles_pool[pool_ix].handle_owner = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER;
211-
handle_mgr->handles_pool[pool_ix].handle_friend = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER;
173+
if( (handle_mgr->handles_pool[pool_ix].handle_owner != current_pid) &&
174+
(handle_mgr->handles_pool[pool_ix].handle_friend != current_pid)
175+
) {
212176

213-
return;
214-
}
177+
// The SPM_PANIC() macro will exit the program
178+
SPM_PANIC("[ERROR] Request for destroy by non-owner or friend!\n");
215179
}
216180

217-
218-
// Handle not found in handles pool - the SPM_PANIC() macro will exit the program
219-
220-
SPM_PANIC("[ERROR] Handle not found %d! \n", (int)handle);
181+
handle_mgr->handles_pool[pool_ix].handle = PSA_NULL_HANDLE;
182+
handle_mgr->handles_pool[pool_ix].handle_owner = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER;
183+
handle_mgr->handles_pool[pool_ix].handle_friend = PSA_HANDLE_MGR_INVALID_FRIEND_OWNER;
221184
}
222185

223186

224187
/**********************************************************************************************************************************
225188
* Function : psa_hndl_mgr_handle_get_mem
226189
*
227190
* Description: This function looks for the handle memory corresponding to <handle>.
228-
* If it is not found in the handles pool, the function fails.
191+
* If it is not found in the expected index in the handles pool, the function fails.
229192
*
230193
* Parameters : handle_mgr - [IN] A pointer to the handle manager object
231194
* handle - [IN] The handle for which we request the corresponding memory handle
@@ -235,7 +198,7 @@ void psa_hndl_mgr_handle_destroy(psa_handle_manager_t *handle_mgr, psa_handle_t
235198
*********************************************************************************************************************************/
236199
void psa_hndl_mgr_handle_get_mem(psa_handle_manager_t *handle_mgr, psa_handle_t handle, void **handle_mem)
237200
{
238-
/* Make sanity checks on arguments */
201+
// Make sanity checks on arguments
239202

240203
SPM_ASSERT(handle_mgr != NULL);
241204
SPM_ASSERT(handle_mem != NULL);
@@ -247,42 +210,34 @@ void psa_hndl_mgr_handle_get_mem(psa_handle_manager_t *handle_mgr, psa_handle_t
247210
}
248211

249212

250-
/* Get active partition id - Needed for requester identification */
251-
spm_partition_t *curr_part_ptr = get_active_partition();
252-
int32_t current_pid = ((curr_part_ptr != NULL) ? curr_part_ptr->partition_id : PSA_NSPE_IDENTIFIER);
253-
254-
255-
*handle_mem = NULL;
256-
257-
258-
/* Look for <handle> in handles pool */
259-
for(uint32_t pool_ix = 0; pool_ix < handle_mgr->pool_size; pool_ix++) {
260-
261-
if(handle_mgr->handles_pool[pool_ix].handle == handle) {
262-
263-
/* Handle found in handles pool */
264-
265-
if( (current_pid != handle_mgr->handles_pool[pool_ix].handle_owner) &&
266-
(current_pid != handle_mgr->handles_pool[pool_ix].handle_friend)
267-
) {
213+
// Get the handle's index in the handles pool
214+
uint32_t pool_ix = ((handle >> PSA_HANDLE_MGR_HANDLE_INDEX_POS) & PSA_HANDLE_MGR_HANDLE_INDEX_MSK);
215+
if(pool_ix >= handle_mgr->pool_size)
216+
{
217+
SPM_PANIC("[ERROR] Handle's index [%d] is bigger than handles pool size [%d]! \n", (int)pool_ix, (int)(handle_mgr->pool_size));
218+
}
268219

269-
// The SPM_PANIC() macro will exit the program
270-
SPM_PANIC("[ERROR] Request for handle memory is not allowed for this partition! \n");
271-
}
220+
if(handle_mgr->handles_pool[pool_ix].handle != handle) {
221+
SPM_PANIC("[ERROR] Handle %d is not found in expected index! \n", (int)handle);
222+
}
272223

273-
*handle_mem = handle_mgr->handles_pool[pool_ix].handle_mem;
224+
// Get active partition id - Needed for requester identification
225+
spm_partition_t *curr_part_ptr = get_active_partition();
226+
int32_t current_pid = ((curr_part_ptr != NULL) ? curr_part_ptr->partition_id : PSA_NSPE_IDENTIFIER);
274227

275-
/* If a valid handle is "coupled" with a NULL handle memory then
276-
* it is an internal module error or memory was overwritten --> Assert */
277-
SPM_ASSERT(*handle_mem != NULL);
228+
if( (current_pid != handle_mgr->handles_pool[pool_ix].handle_owner) &&
229+
(current_pid != handle_mgr->handles_pool[pool_ix].handle_friend)
230+
) {
278231

279-
return;
280-
}
232+
// The SPM_PANIC() macro will exit the program
233+
SPM_PANIC("[ERROR] Request for handle memory is not allowed for this partition! \n");
281234
}
282235

283236

284-
// Handle not found in handles pool - the SPM_PANIC() macro will exit the program
237+
*handle_mem = handle_mgr->handles_pool[pool_ix].handle_mem;
285238

286-
SPM_PANIC("[ERROR] Handle %d not found! \n", (int)handle);
239+
/* If a valid handle is "coupled" with a NULL handle memory then
240+
* it is an internal module error or memory was overwritten --> Assert */
241+
SPM_ASSERT(*handle_mem != NULL);
287242
}
288243

spm/handles_manager.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ extern "C" {
3737
/* ------------------------------------ Definitions ---------------------------------- */
3838

3939
#define PSA_HANDLE_MGR_INVALID_HANDLE ((uint32_t)PSA_NULL_HANDLE)
40-
#define PSA_HANDLE_MGR_INVALID_FRIEND_OWNER (-2) // Denoting invalid friend or invalid owner
40+
41+
#define PSA_HANDLE_MGR_INVALID_FRIEND_OWNER 0 // Denoting invalid friend or invalid owner
42+
43+
#define PSA_HANDLE_MGR_MAX_HANDLES_NUM 0x8000 // Handles manager pool indexes must be in range 0 - 0x7FFF.
44+
// The reason for this limitation is that the index is stored in the upper 16 bits of a handle,
45+
// and the most significant bit must be zero to keep handles non negative.
4146

4247

4348

@@ -82,7 +87,6 @@ handles_pool
8287

8388
/* ------------------------------------- Functions ----------------------------------- */
8489

85-
void psa_hndl_mgr_init(psa_handle_manager_t *handle_manager, psa_handle_item_t *handle_mgr_storage, size_t storage_size);
8690
error_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_manager, void *handle_mem, int32_t friend_pid, psa_handle_t *handle);
8791
void psa_hndl_mgr_handle_destroy(psa_handle_manager_t *handle_manager, psa_handle_t handle);
8892
void psa_hndl_mgr_handle_get_mem(psa_handle_manager_t *handle_manager, psa_handle_t handle, void **handle_mem);

0 commit comments

Comments
 (0)