30
30
31
31
32
32
33
+ /* ------------------------------------ Definitions ---------------------------------- */
34
+
35
+ #define PSA_HANDLE_MGR_HANDLE_INDEX_POS 16
36
+ #define PSA_HANDLE_MGR_HANDLE_INDEX_MSK 0xFFFF
37
+
38
+
39
+
33
40
/* -------------------------------- Handle Manager Module ---------------------------- */
34
41
35
42
/* The Handle Manager Module manages handles.
40
47
* handle memory.
41
48
*
42
49
* Users can:
43
- * - Initiate the module
44
50
* - Ask for a unique handle identifier for a given handle memory [handle_create]
45
51
* - Ask for a pointer to the handle memory corresponding to a
46
52
* handle identifier [handle_get_mem]
56
62
57
63
/* ------------------------------------- Functions ----------------------------------- */
58
64
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
-
98
65
/**********************************************************************************************************************************
99
66
* Function : psa_hndl_mgr_handle_create
100
67
*
@@ -103,18 +70,15 @@ void psa_hndl_mgr_init(psa_handle_manager_t *handle_mgr, psa_handle_item_t *hand
103
70
*
104
71
* Parameters : handle_mgr - [IN] A pointer to the handle manager object
105
72
* 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.
107
74
* Use PSA_HANDLE_MGR_INVALID_FRIEND_OWNER to denote there is no friend partition.
108
75
* handle - [OUT] A pointer to a handle, to be set with the created handle identifier
109
76
*
110
77
* Return : PSA_STATUS_SUCCESS on success. Error code on failure.
111
78
*********************************************************************************************************************************/
112
79
error_t psa_hndl_mgr_handle_create (psa_handle_manager_t * handle_mgr , void * handle_mem , int32_t friend_pid , psa_handle_t * handle )
113
80
{
114
- uint32_t new_handle = PSA_HANDLE_MGR_INVALID_HANDLE ;
115
-
116
-
117
- /* Make sanity checks on arguments */
81
+ // Make sanity checks on arguments
118
82
SPM_ASSERT (handle_mgr != NULL );
119
83
SPM_ASSERT (handle_mem != NULL );
120
84
SPM_ASSERT (handle != NULL );
@@ -123,34 +87,38 @@ error_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *handl
123
87
* handle = PSA_NULL_HANDLE ;
124
88
125
89
126
- /* Get active partition id - Needed for requester identification */
90
+ // Get active partition id - Needed for requester identification
127
91
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)
134
97
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
+ );
138
101
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 );
141
104
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
143
106
for (uint32_t pool_ix = 0 ; pool_ix < handle_mgr -> pool_size ; pool_ix ++ ) {
107
+
144
108
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
146
114
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
+ )) {
150
118
151
- /* Handle is successfully stored in handles pool */
119
+ // Handle is successfully stored in handles pool
152
120
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
154
122
handle_mgr -> handles_pool [pool_ix ].handle_mem = handle_mem ;
155
123
handle_mgr -> handles_pool [pool_ix ].handle_owner = current_pid ;
156
124
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
160
128
return PSA_SUCCESS ;
161
129
}
162
130
163
- /* Occupied index in handles pool - continue looping */
131
+ // Occupied index in handles pool - continue looping
164
132
}
165
133
166
134
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
168
136
169
137
return PSA_GENERIC_ERROR ;
170
138
}
@@ -182,50 +150,45 @@ error_t psa_hndl_mgr_handle_create(psa_handle_manager_t *handle_mgr, void *handl
182
150
*********************************************************************************************************************************/
183
151
void psa_hndl_mgr_handle_destroy (psa_handle_manager_t * handle_mgr , psa_handle_t handle )
184
152
{
185
- /* Make sanity checks on arguments */
153
+ // Make sanity checks on arguments
186
154
SPM_ASSERT (handle_mgr != NULL );
187
155
SPM_ASSERT (handle != PSA_NULL_HANDLE );
188
156
189
157
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
+ }
202
164
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
+ }
206
168
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 );
208
172
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
+ ) {
212
176
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" );
215
179
}
216
180
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 ;
221
184
}
222
185
223
186
224
187
/**********************************************************************************************************************************
225
188
* Function : psa_hndl_mgr_handle_get_mem
226
189
*
227
190
* 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.
229
192
*
230
193
* Parameters : handle_mgr - [IN] A pointer to the handle manager object
231
194
* 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
235
198
*********************************************************************************************************************************/
236
199
void psa_hndl_mgr_handle_get_mem (psa_handle_manager_t * handle_mgr , psa_handle_t handle , void * * handle_mem )
237
200
{
238
- /* Make sanity checks on arguments */
201
+ // Make sanity checks on arguments
239
202
240
203
SPM_ASSERT (handle_mgr != NULL );
241
204
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
247
210
}
248
211
249
212
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
+ }
268
219
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
+ }
272
223
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 );
274
227
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
+ ) {
278
231
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" );
281
234
}
282
235
283
236
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 ;
285
238
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 );
287
242
}
288
243
0 commit comments