xen/arm: ffa: Add PARTITION_INFO_GET_REGS support
cover xen/arm: ffa: Add PARTITION_INFO_GET_REGS support
This series implements FF-A 1.2 PARTITION_INFO_GET_REGS support, providing register-based partition info retrieval as an efficient alternative to the RX buffer-based PARTITION_INFO_GET.
The serie is also introducing a caching system for the list of secure partitions to prevent retrieving it on each VM call as we expect it to be static and not changing over time.
The patches are organized as follows:
- patch 1 is modifying existing code to prepare for GET_REGS support by adding a start_index in the existing internal function
- patch 2 is introducing the secure partition list caching
- patch 3 is removing the existing suscribers list to directly use the cached secure partition list
- patch 4 is adding GET_REGS support
Bertrand Marquis (4):
xen/arm: ffa: Add start_index to VM partinfo helper xen/arm: ffa: Cache SP partition info at init xen/arm: ffa: Drop SP subscriber lists xen/arm: ffa: Add cached GET_REGS support
xen/arch/arm/tee/ffa.c | 16 + xen/arch/arm/tee/ffa_partinfo.c | 558 ++++++++++++++++++++++---------- xen/arch/arm/tee/ffa_private.h | 4 +- 3 files changed, 402 insertions(+), 176 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/4 xen/arm: ffa: Add start_index to VM partinfo helper
Windowed GET_REGS retrieval needs to emit VM entries starting from an arbitrary index, but ffa_get_vm_partinfo() always starts from index 0.
Add a start_index parameter to ffa_get_vm_partinfo() and skip entries until the local index reaches start_index. Update ffa_handle_partition_info_get() to pass start_index=0 to preserve existing behavior.
No functional changes.
--- xen/arch/arm/tee/ffa_partinfo.c | 61 +++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 26 deletions(-)
xen/arch/arm/tee/ffa_partinfo.c +35 -26
@@ -167,14 +167,15 @@ out:return ret; } -static int32_t ffa_get_vm_partinfo (struct ffa_uuid uuid,uint32_t *vm_count, -void **dst_buf,void *end_buf, -uint32_t dst_size) +static int32_t ffa_get_vm_partinfo (struct ffa_uuid uuid,uint32_t start_index, +uint32_t *vm_count,void **dst_buf, +void *end_buf,uint32_t dst_size) {struct domain *d = current->domain;struct ffa_ctx *curr_ctx = d->arch.tee;struct ffa_ctx *dest_ctx;uint32_t count =0 ; +uint32_t idx =0 ;int32_t ret =FFA_RET_OK ;/* * We do not have UUID info for VMs so use the 1.0 structure so that we set
@@ -202,17 +203,21 @@ static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t *vm_count,if (ACCESS_ONCE (curr_ctx->guest_vers) >=FFA_VERSION_1_2 ) {/* Add caller VM information */ - info.id = curr_ctx->ffa_id; - info.execution_context = curr_ctx->num_vcpus; - info.partition_properties =FFA_PART_VM_PROP ; -if (is_64bit_domain (d) ) - info.partition_properties |=FFA_PART_PROP_AARCH64_STATE ; - - ret =ffa_copy_info (dst_buf, end_buf, &info, dst_size,sizeof (info)); -if ( ret ) -return ret; +if ( start_index ==0 ) + { + info.id = curr_ctx->ffa_id; + info.execution_context = curr_ctx->num_vcpus; + info.partition_properties =FFA_PART_VM_PROP ; +if (is_64bit_domain (d) ) + info.partition_properties |=FFA_PART_PROP_AARCH64_STATE ; - count++; + ret =ffa_copy_info (dst_buf, end_buf, &info, dst_size, +sizeof (info)); +if ( ret ) +return ret; + count++; + } + idx++; }if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) )
@@ -231,21 +236,25 @@ static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t *vm_count,if ( dest_ctx == curr_ctx )continue ; - info.id = dest_ctx->ffa_id; - info.execution_context = dest_ctx->num_vcpus; - info.partition_properties =FFA_PART_VM_PROP ; -if ( dest_ctx->is_64bit ) - info.partition_properties |=FFA_PART_PROP_AARCH64_STATE ; - - ret =ffa_copy_info (dst_buf, end_buf, &info, dst_size, -sizeof (info)); -if ( ret ) +if ( idx >= start_index ) { -read_unlock (&ffa_ctx_list_rwlock); -return ret; + info.id = dest_ctx->ffa_id; + info.execution_context = dest_ctx->num_vcpus; + info.partition_properties =FFA_PART_VM_PROP ; +if ( dest_ctx->is_64bit ) + info.partition_properties |=FFA_PART_PROP_AARCH64_STATE ; + + ret =ffa_copy_info (dst_buf, end_buf, &info, dst_size, +sizeof (info)); +if ( ret ) + { +read_unlock (&ffa_ctx_list_rwlock); +return ret; + } + count++; } - count++; + idx++; }read_unlock (&ffa_ctx_list_rwlock); }
@@ -355,7 +364,7 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs) goto out_rx_release; } - ret =ffa_get_vm_partinfo (uuid, &ffa_vm_count, &dst_buf, end_buf, + ret =ffa_get_vm_partinfo (uuid,0 , &ffa_vm_count, &dst_buf, end_buf, dst_size); out_rx_release:
Signature · 1 line
2.52.0
Re: [PATCH 1/4] xen/arm: ffa: Add start_index to VM partinfo helper
Hi Bertrand,
Quoted 9 lines · Bertrand Marquis
Windowed
GET_REGSretrieval needs to emit VM entries starting from an arbitrary index, butffa_get_vm_partinfo()always starts from index 0.Add a
start_indexparameter toffa_get_vm_partinfo()and skip entries until the local index reachesstart_index. Updateffa_handle_partition_info_get()to pass start_index=0 to preserve existing behavior.
Quoted 6 lines
No functional changes.
Signed-off-by Bertrand Marquis <[email protected]>--- xen/arch/arm/tee/ffa_partinfo.c | 61 +++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 26 deletions(-)
Looks good:
Cheers,
Jens
Quoted 106 lines
xen/arch/arm/tee/ffa_partinfo.c +35
-27@@ -167,14 +167,15 @@ out:return ret; } -static int32_t ffa_get_vm_partinfo (struct ffa_uuid uuid,uint32_t *vm_count, -void **dst_buf,void *end_buf, -uint32_t dst_size) +static int32_t ffa_get_vm_partinfo (struct ffa_uuid uuid,uint32_t start_index, +uint32_t *vm_count,void **dst_buf, +void *end_buf,uint32_t dst_size) {struct domain *d = current->domain;struct ffa_ctx *curr_ctx = d->arch.tee;struct ffa_ctx *dest_ctx;uint32_t count =0 ; +uint32_t idx =0 ;int32_t ret =FFA_RET_OK ;/* * We do not have UUID info for VMs so use the 1.0 structure so that we set @@ -202,17 +203,21 @@ static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t *vm_count,if (ACCESS_ONCE (curr_ctx->guest_vers) >=FFA_VERSION_1_2 ) {/* Add caller VM information */ - info.id = curr_ctx->ffa_id; - info.execution_context = curr_ctx->num_vcpus; - info.partition_properties =FFA_PART_VM_PROP ; -if (is_64bit_domain (d) ) - info.partition_properties |=FFA_PART_PROP_AARCH64_STATE ; - - ret =ffa_copy_info (dst_buf, end_buf, &info, dst_size,sizeof (info)); -if ( ret ) -return ret; +if ( start_index ==0 ) + { + info.id = curr_ctx->ffa_id; + info.execution_context = curr_ctx->num_vcpus; + info.partition_properties =FFA_PART_VM_PROP ; +if (is_64bit_domain (d) ) + info.partition_properties |=FFA_PART_PROP_AARCH64_STATE ; - count++; + ret =ffa_copy_info (dst_buf, end_buf, &info, dst_size, +sizeof (info)); +if ( ret ) +return ret; + count++; + } + idx++; }if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) )@@ -231,21 +236,25 @@ static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t *vm_count,if ( dest_ctx == curr_ctx )continue ; - info.id = dest_ctx->ffa_id; - info.execution_context = dest_ctx->num_vcpus; - info.partition_properties =FFA_PART_VM_PROP ; -if ( dest_ctx->is_64bit ) - info.partition_properties |=FFA_PART_PROP_AARCH64_STATE ; - - ret =ffa_copy_info (dst_buf, end_buf, &info, dst_size, -sizeof (info)); -if ( ret ) +if ( idx >= start_index ) { -read_unlock (&ffa_ctx_list_rwlock); -return ret; + info.id = dest_ctx->ffa_id; + info.execution_context = dest_ctx->num_vcpus; + info.partition_properties =FFA_PART_VM_PROP ; +if ( dest_ctx->is_64bit ) + info.partition_properties |=FFA_PART_PROP_AARCH64_STATE ; + + ret =ffa_copy_info (dst_buf, end_buf, &info, dst_size, +sizeof (info)); +if ( ret ) + { +read_unlock (&ffa_ctx_list_rwlock); +return ret; + } + count++; } - count++; + idx++; }read_unlock (&ffa_ctx_list_rwlock); }@@ -355,7 +364,7 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs) goto out_rx_release; } - ret =ffa_get_vm_partinfo (uuid, &ffa_vm_count, &dst_buf, end_buf, + ret =ffa_get_vm_partinfo (uuid,0 , &ffa_vm_count, &dst_buf, end_buf, dst_size); out_rx_release: --2.52.0
PATCH 2/4 xen/arm: ffa: Cache SP partition info at init
In reply to Bertrand MarquisFFA_PARTITION_INFO_GET currently queries the SPMC on each call and walks the RX buffer every time. The SP list is expected to be static, so repeated firmware calls and validation are unnecessary.
Cache the SPMC partition-info list at init time, keeping only secure endpoints, and reuse the cached entries for SP count and partition-info responses. Initialize the VM create/destroy subscriber lists from the cached list and free the cache on init failure.
SP partition info now reflects the init-time snapshot and will not change.
--- xen/arch/arm/tee/ffa_partinfo.c | 205 +++++++++++++++++++++----------- 1 file changed, 138 insertions(+), 67 deletions(-)
xen/arch/arm/tee/ffa_partinfo.c +138 -67
@@ -6,6 +6,7 @@#include <xen/const.h> #include <xen/sizes.h> #include <xen/types.h> +#include <xen/xmalloc.h> #include <asm/smccc.h> #include <asm/regs.h>
@@ -33,6 +34,10 @@ static uint16_t subscr_vm_created_count __read_mostly;static uint16_t *subscr_vm_destroyed__read_mostly ;static uint16_t subscr_vm_destroyed_count__read_mostly ; +/* SP list cache (secure endpoints only); populated at init. */ +static void *sp_list__read_mostly ; +static uint32_t sp_list_count__read_mostly ; +static uint32_t sp_list_entry_size__read_mostly ;static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {
@@ -79,92 +84,78 @@ static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src,return FFA_RET_OK ; } -static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) +static bool ffa_sp_entry_matches_uuid (const void *entry,struct ffa_uuid uuid) { -uint32_t src_size; +const struct ffa_partition_info_1_1 *fpi = entry; +struct ffa_uuid sp_uuid; + +if (ffa_uuid_is_nil (uuid) ) +return true; -return ffa_partition_info_get (uuid,FFA_PARTITION_INFO_GET_COUNT_FLAG , - sp_count, &src_size); +if ( sp_list_entry_size <sizeof (*fpi) ) +return false; + +memcpy (&sp_uuid, fpi->uuid,sizeof (sp_uuid)); +return ffa_uuid_equal (uuid, sp_uuid); } -static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, -void **dst_buf,void *end_buf, -uint32_t dst_size) +static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) { -int32_t ret; -int32_t release_ret; -uint32_t src_size, real_sp_count; -void *src_buf;uint32_t count =0 ; -bool notify_fw = false; - -/* We need to use the RX buffer to receive the list */ - src_buf =ffa_rxtx_spmc_rx_acquire (); -if ( !src_buf ) -return FFA_RET_DENIED ; - - ret =ffa_partition_info_get (uuid,0 , &real_sp_count, &src_size); -if ( ret ) - goto out; - notify_fw = true; +uint32_t n; -/* Validate the src_size we got */ -if ( src_size <sizeof (struct ffa_partition_info_1_0 ) || - src_size >=FFA_PAGE_SIZE ) +for ( n =0 ; n < sp_list_count; n++ ) { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; +void *entry = sp_list + n * sp_list_entry_size; + +if (ffa_sp_entry_matches_uuid (entry, uuid) ) + count++; } -/* -* Limit the maximum time we hold the CPU by limiting the number of SPs. -* We just ignore the extra ones as this is tested during init in -* ffa_partinfo_init so the only possible reason is SP have been added -* since boot. -*/ -if ( real_sp_count >FFA_MAX_NUM_SP ) - real_sp_count =FFA_MAX_NUM_SP ; + *sp_count = count; -/* Make sure the data fits in our buffer */ -if ( real_sp_count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / src_size ) - { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; - } +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; -for (uint32_t sp_num =0 ; sp_num < real_sp_count; sp_num++ ) - { -struct ffa_partition_info_1_1 *fpi = src_buf; +return FFA_RET_OK ; +} -/* filter out SP not following bit 15 convention if any */ -if (FFA_ID_IS_SECURE (fpi->id) ) - { -/* -* If VM is 1.0 but firmware is 1.1 we could have several entries -* with the same ID but different UUIDs. In this case the VM will -* get a list with several time the same ID. -* This is a non-compliance to the specification but 1.0 VMs should -* handle that on their own to simplify Xen implementation. -*/ +static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, +void **dst_buf,void *end_buf, +uint32_t dst_size) +{ +int32_t ret; +uint32_t count =0 ; +uint32_t n; - ret =ffa_copy_info (dst_buf, end_buf, src_buf, dst_size, src_size); -if ( ret ) - goto out; +for ( n =0 ; n < sp_list_count; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; - count++; - } +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; - src_buf += src_size; +/* +* If VM is 1.0 but firmware is 1.1 we could have several entries +* with the same ID but different UUIDs. In this case the VM will +* get a list with several time the same ID. +* This is a non-compliance to the specification but 1.0 VMs should +* handle that on their own to simplify Xen implementation. +*/ + ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, + sp_list_entry_size); +if ( ret ) +return ret; + + count++; } *sp_count = count; -out: - release_ret =ffa_rxtx_spmc_rx_release (notify_fw); -if ( release_ret ) -gprintk (XENLOG_WARNING , -"ffa: Error releasing SPMC RX buffer: %d\n" , release_ret); -return ret; +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; + +return FFA_RET_OK ; } static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t start_index,
@@ -435,6 +426,14 @@ static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id,return res; } +static void ffa_sp_list_cache_free (void ) +{ +XFREE (sp_list); + sp_list =NULL ; + sp_list_count =0 ; + sp_list_entry_size =0 ; +} +static void uninit_subscribers (void ) { subscr_vm_created_count =0 ;
@@ -443,6 +442,68 @@ static void uninit_subscribers(void)XFREE (subscr_vm_destroyed); } +static bool ffa_sp_list_cache_init (const void *buf,uint32_t count, +uint32_t fpi_size) +{ +const uint8_t *src = buf; +uint32_t secure_count =0 ; +uint32_t n, idx =0 ; +bool warned = false; + +if ( fpi_size <sizeof (struct ffa_partition_info_1_0 ) || + fpi_size >=FFA_PAGE_SIZE ) +return false; + +if ( count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / fpi_size ) +return false; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); + +if ( !FFA_ID_IS_SECURE (fpi->id) ) + { +if ( !warned ) + { +printk_once (XENLOG_ERR +"ffa: Firmware is not using bit 15 convention for IDs !!\n" ); + warned = true; + } +printk (XENLOG_ERR +"ffa: Secure partition with id 0x%04x cannot be used\n" , + fpi->id); +continue ; + } + + secure_count++; + } + +if ( secure_count ) + { + sp_list =xzalloc_bytes (secure_count * fpi_size); +if ( !sp_list ) +return false; + } + + sp_list_count = secure_count; + sp_list_entry_size = fpi_size; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); + +if ( !FFA_ID_IS_SECURE (fpi->id) ) +continue ; + +memcpy (sp_list + idx * fpi_size, fpi, fpi_size); + idx++; + } + +return true; +} +static bool init_subscribers (void *buf,uint16_t count,uint32_t fpi_size) {uint16_t n;
@@ -549,12 +610,22 @@ bool ffa_partinfo_init(void) goto out; } - ret =init_subscribers (spmc_rx, count, fpi_size); +if ( !ffa_sp_list_cache_init (spmc_rx, count, fpi_size) ) + { +printk (XENLOG_ERR "ffa: Failed to cache SP list\n" ); + goto out; + } + + ret =init_subscribers (sp_list, sp_list_count, sp_list_entry_size); out: e =ffa_rxtx_spmc_rx_release (notify_fw);if ( e )printk (XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n" , e); +if ( !ret ) +uninit_subscribers (); +if ( !ret ) +ffa_sp_list_cache_free ();return ret; }
Signature · 1 line
2.52.0
Re: [PATCH 2/4] xen/arm: ffa: Cache SP partition info at init
Hi Bertrand,
Quoted 48 lines · Bertrand Marquis
FFA_PARTITION_INFO_GETcurrently queries the SPMC on each call and walks the RX buffer every time. The SP list is expected to be static, so repeated firmware calls and validation are unnecessary.Cache the SPMC partition-info list at init time, keeping only secure endpoints, and reuse the cached entries for SP count and partition-info responses. Initialize the VM create/destroy subscriber lists from the cached list and free the cache on init failure.
SP partition info now reflects the init-time snapshot and will not change.
Signed-off-by Bertrand Marquis <[email protected]>--- xen/arch/arm/tee/ffa_partinfo.c | 205 +++++++++++++++++++++----------- 1 file changed, 138 insertions(+), 67 deletions(-)xen/arch/arm/tee/ffa_partinfo.c +6
-2@@ -6,6 +6,7 @@#include <xen/const.h> #include <xen/sizes.h> #include <xen/types.h> +#include <xen/xmalloc.h> #include <asm/smccc.h> #include <asm/regs.h> @@ -33,6 +34,10 @@ static uint16_t subscr_vm_created_count __read_mostly;static uint16_t *subscr_vm_destroyed__read_mostly ;static uint16_t subscr_vm_destroyed_count__read_mostly ; +/* SP list cache (secure endpoints only); populated at init. */ +static void *sp_list__read_mostly ; +static uint32_t sp_list_count__read_mostly ; +static uint32_t sp_list_entry_size__read_mostly ;static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {@@ -79,92 +84,78 @@ static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src,return FFA_RET_OK ; } -static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) +static bool ffa_sp_entry_matches_uuid (const void *entry,struct ffa_uuid uuid) { -uint32_t src_size;
xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src, · line 96
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +7
-2+const struct ffa_partition_info_1_1 *fpi = entry; +struct ffa_uuid sp_uuid; + +if (ffa_uuid_is_nil (uuid) ) +return true; -return ffa_partition_info_get (uuid,FFA_PARTITION_INFO_GET_COUNT_FLAG , - sp_count, &src_size); +if ( sp_list_entry_size <sizeof (*fpi) ) +return false;
This can never happen since we don't accept SPMC below version 1.1. We even have a check to ensure the SPMC doesn't return a too-small spi_size.
Quoted 128 lines
xen/arch/arm/tee/ffa_partinfo.c +44
-62+ +memcpy (&sp_uuid, fpi->uuid,sizeof (sp_uuid)); +return ffa_uuid_equal (uuid, sp_uuid); } -static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, -void **dst_buf,void *end_buf, -uint32_t dst_size) +static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) { -int32_t ret; -int32_t release_ret; -uint32_t src_size, real_sp_count; -void *src_buf;uint32_t count =0 ; -bool notify_fw = false; - -/* We need to use the RX buffer to receive the list */ - src_buf =ffa_rxtx_spmc_rx_acquire (); -if ( !src_buf ) -return FFA_RET_DENIED ; - - ret =ffa_partition_info_get (uuid,0 , &real_sp_count, &src_size); -if ( ret ) - goto out; - notify_fw = true; +uint32_t n; -/* Validate the src_size we got */ -if ( src_size <sizeof (struct ffa_partition_info_1_0 ) || - src_size >=FFA_PAGE_SIZE ) +for ( n =0 ; n < sp_list_count; n++ ) { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; +void *entry = sp_list + n * sp_list_entry_size; + +if (ffa_sp_entry_matches_uuid (entry, uuid) ) + count++; } -/* -* Limit the maximum time we hold the CPU by limiting the number of SPs. -* We just ignore the extra ones as this is tested during init in -* ffa_partinfo_init so the only possible reason is SP have been added -* since boot. -*/ -if ( real_sp_count >FFA_MAX_NUM_SP ) - real_sp_count =FFA_MAX_NUM_SP ; + *sp_count = count; -/* Make sure the data fits in our buffer */ -if ( real_sp_count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / src_size ) - { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; - } +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; -for (uint32_t sp_num =0 ; sp_num < real_sp_count; sp_num++ ) - { -struct ffa_partition_info_1_1 *fpi = src_buf; +return FFA_RET_OK ; +} -/* filter out SP not following bit 15 convention if any */ -if (FFA_ID_IS_SECURE (fpi->id) ) - { -/* -* If VM is 1.0 but firmware is 1.1 we could have several entries -* with the same ID but different UUIDs. In this case the VM will -* get a list with several time the same ID. -* This is a non-compliance to the specification but 1.0 VMs should -* handle that on their own to simplify Xen implementation. -*/ +static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, +void **dst_buf,void *end_buf, +uint32_t dst_size) +{ +int32_t ret; +uint32_t count =0 ; +uint32_t n; - ret =ffa_copy_info (dst_buf, end_buf, src_buf, dst_size, src_size); -if ( ret ) - goto out; +for ( n =0 ; n < sp_list_count; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; - count++; - } +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; - src_buf += src_size; +/* +* If VM is 1.0 but firmware is 1.1 we could have several entries +* with the same ID but different UUIDs. In this case the VM will +* get a list with several time the same ID. +* This is a non-compliance to the specification but 1.0 VMs should +* handle that on their own to simplify Xen implementation. +*/ + ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, + sp_list_entry_size); +if ( ret ) +return ret; + + count++; } *sp_count = count; -out: - release_ret =ffa_rxtx_spmc_rx_release (notify_fw); -if ( release_ret ) -gprintk (XENLOG_WARNING , -"ffa: Error releasing SPMC RX buffer: %d\n" , release_ret); -return ret; +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; + +return FFA_RET_OK ; } static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t start_index,
xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id, · line 432
Quoted 7 lines
xen/arch/arm/tee/ffa_partinfo.c +4
-0@@ -435,6 +426,14 @@ static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id,return res; } +static void ffa_sp_list_cache_free (void ) +{ +XFREE (sp_list); + sp_list =NULL ;
XFREE() is already setting sp_list to NULL.
Quoted 12 lines
xen/arch/arm/tee/ffa_partinfo.c +5
-0+ sp_list_count =0 ; + sp_list_entry_size =0 ; +} +static void uninit_subscribers (void ) { subscr_vm_created_count =0 ;@@ -443,6 +442,68 @@ static void uninit_subscribers(void)XFREE (subscr_vm_destroyed); } +static bool ffa_sp_list_cache_init(const void *buf, uint32_t count,
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 455
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +10
-0+ uint32_t fpi_size) +{ +const uint8_t *src = buf; +uint32_t secure_count =0 ; +uint32_t n, idx =0 ; +bool warned = false; + +if ( fpi_size <sizeof (struct ffa_partition_info_1_0 ) || + fpi_size >=FFA_PAGE_SIZE ) +return false;
Would it make sense to check that fpi_size is well aligned with struct ffa_partition_info_1_0? If it's an odd size, we'll make unaligned accesses below with memcpy(). But perhaps that's a bit much. The SPMC isn't supposed to provide garbage.
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 467
Quoted 12 lines
xen/arch/arm/tee/ffa_partinfo.c +12
-0+ +if ( count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / fpi_size ) +return false; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); + +if ( !FFA_ID_IS_SECURE (fpi->id) ) + { +if ( !warned )
Is this needed? Doesn't printk_once() already ensure this? Or did you happen to leave printk_once() by mistake and meant for this to be printed once each time ffa_sp_list_cache_init() is called, since "warned" isn't static.
Quoted 51 lines
xen/arch/arm/tee/ffa_partinfo.c +43
-1+ { +printk_once (XENLOG_ERR +"ffa: Firmware is not using bit 15 convention for IDs !!\n" ); + warned = true; + } +printk (XENLOG_ERR +"ffa: Secure partition with id 0x%04x cannot be used\n" , + fpi->id); +continue ; + } + + secure_count++; + } + +if ( secure_count ) + { + sp_list =xzalloc_bytes (secure_count * fpi_size); +if ( !sp_list ) +return false; + } + + sp_list_count = secure_count; + sp_list_entry_size = fpi_size; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); + +if ( !FFA_ID_IS_SECURE (fpi->id) ) +continue ; + +memcpy (sp_list + idx * fpi_size, fpi, fpi_size); + idx++; + } + +return true; +} +static bool init_subscribers (void *buf,uint16_t count,uint32_t fpi_size) {uint16_t n;@@ -549,12 +610,22 @@ bool ffa_partinfo_init(void) goto out; } - ret =init_subscribers (spmc_rx, count, fpi_size); +if ( !ffa_sp_list_cache_init (spmc_rx, count, fpi_size) ) + { +printk (XENLOG_ERR "ffa: Failed to cache SP list\n" ); + goto out;
xen/arch/arm/tee/ffa_partinfo.c bool ffa_partinfo_init(void) · line 626
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +5
-0+ } + + ret =init_subscribers (sp_list, sp_list_count, sp_list_entry_size); out: e =ffa_rxtx_spmc_rx_release (notify_fw);if ( e )printk (XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n" , e); +if ( !ret ) +uninit_subscribers ();
ret is initially false and can only be set to true if init_subscribers() returns true. So there's never any point in calling uninit_subscribers().
xen/arch/arm/tee/ffa_partinfo.c bool ffa_partinfo_init(void) · line 628
Quoted 2 lines
xen/arch/arm/tee/ffa_partinfo.c +2
-0+if ( !ret ) +ffa_sp_list_cache_free ();
ret can be false even if ffa_sp_list_cache_init() hasn't been called yet. Calling ffa_sp_list_cache_free() anyway is harmless, but not elegant.
Cheers,
Jens
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +0
-1return ret; } --2.52.0
Re: [PATCH 2/4] xen/arm: ffa: Cache SP partition info at init
Hi Jens,
Quoted 60 lines · Jens Wiklander
Hi Bertrand,
Quoted 54 lines · Bertrand Marquis
FFA_PARTITION_INFO_GETcurrently queries the SPMC on each call and walks the RX buffer every time. The SP list is expected to be static, so repeated firmware calls and validation are unnecessary.Cache the SPMC partition-info list at init time, keeping only secure endpoints, and reuse the cached entries for SP count and partition-info responses. Initialize the VM create/destroy subscriber lists from the cached list and free the cache on init failure.
SP partition info now reflects the init-time snapshot and will not change.
Signed-off-by Bertrand Marquis <[email protected]>---
xen/arch/arm/tee/ffa_partinfo.c| 205 +++++++++++++++++++++-----------1 file changed, 138 insertions(+), 67 deletions(-)xen/arch/arm/tee/ffa_partinfo.c +11
-2@@ -6,6 +6,7 @@#include <xen/const.h> #include <xen/sizes.h> #include <xen/types.h> +#include <xen/xmalloc.h> #include <asm/smccc.h> #include <asm/regs.h> @@ -33,6 +34,10 @@ static uint16_t subscr_vm_created_count __read_mostly;static uint16_t *subscr_vm_destroyed__read_mostly ;static uint16_t subscr_vm_destroyed_count__read_mostly ; +/* SP list cache (secure endpoints only); populated at init. */ +static void *sp_list__read_mostly ; +static uint32_t sp_list_count__read_mostly ; +static uint32_t sp_list_entry_size__read_mostly ;static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {@@ -79,92 +84,78 @@ static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src,return FFA_RET_OK ; } -static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) +static bool ffa_sp_entry_matches_uuid (const void *entry,struct ffa_uuid uuid) { -uint32_t src_size; +const struct ffa_partition_info_1_1 *fpi = entry; +struct ffa_uuid sp_uuid; + +if (ffa_uuid_is_nil (uuid) ) +return true;
Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src, · line 96
Quoted 4 lines
xen/arch/arm/tee/ffa_partinfo.c +2
-2-return ffa_partition_info_get (uuid,FFA_PARTITION_INFO_GET_COUNT_FLAG , - sp_count, &src_size); +if ( sp_list_entry_size <sizeof (*fpi) ) +return false;This can never happen since we don't accept SPMC below version 1.1. We even have a check to ensure the SPMC doesn't return a too-small
spi_size.
I tried to make the code forward compatible. It is not really a possible case right now but we could fall into this in the future.
Quoted 132 lines
Quoted 132 lines
xen/arch/arm/tee/ffa_partinfo.c +44
-62+ +memcpy (&sp_uuid, fpi->uuid,sizeof (sp_uuid)); +return ffa_uuid_equal (uuid, sp_uuid); } -static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, -void **dst_buf,void *end_buf, -uint32_t dst_size) +static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) { -int32_t ret; -int32_t release_ret; -uint32_t src_size, real_sp_count; -void *src_buf;uint32_t count =0 ; -bool notify_fw = false; - -/* We need to use the RX buffer to receive the list */ - src_buf =ffa_rxtx_spmc_rx_acquire (); -if ( !src_buf ) -return FFA_RET_DENIED ; - - ret =ffa_partition_info_get (uuid,0 , &real_sp_count, &src_size); -if ( ret ) - goto out; - notify_fw = true; +uint32_t n; -/* Validate the src_size we got */ -if ( src_size <sizeof (struct ffa_partition_info_1_0 ) || - src_size >=FFA_PAGE_SIZE ) +for ( n =0 ; n < sp_list_count; n++ ) { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; +void *entry = sp_list + n * sp_list_entry_size; + +if (ffa_sp_entry_matches_uuid (entry, uuid) ) + count++; } -/* -* Limit the maximum time we hold the CPU by limiting the number of SPs. -* We just ignore the extra ones as this is tested during init in -* ffa_partinfo_init so the only possible reason is SP have been added -* since boot. -*/ -if ( real_sp_count >FFA_MAX_NUM_SP ) - real_sp_count =FFA_MAX_NUM_SP ; + *sp_count = count; -/* Make sure the data fits in our buffer */ -if ( real_sp_count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / src_size ) - { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; - } +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; -for (uint32_t sp_num =0 ; sp_num < real_sp_count; sp_num++ ) - { -struct ffa_partition_info_1_1 *fpi = src_buf; +return FFA_RET_OK ; +} -/* filter out SP not following bit 15 convention if any */ -if (FFA_ID_IS_SECURE (fpi->id) ) - { -/* -* If VM is 1.0 but firmware is 1.1 we could have several entries -* with the same ID but different UUIDs. In this case the VM will -* get a list with several time the same ID. -* This is a non-compliance to the specification but 1.0 VMs should -* handle that on their own to simplify Xen implementation. -*/ +static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, +void **dst_buf,void *end_buf, +uint32_t dst_size) +{ +int32_t ret; +uint32_t count =0 ; +uint32_t n; - ret =ffa_copy_info (dst_buf, end_buf, src_buf, dst_size, src_size); -if ( ret ) - goto out; +for ( n =0 ; n < sp_list_count; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; - count++; - } +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; - src_buf += src_size; +/* +* If VM is 1.0 but firmware is 1.1 we could have several entries +* with the same ID but different UUIDs. In this case the VM will +* get a list with several time the same ID. +* This is a non-compliance to the specification but 1.0 VMs should +* handle that on their own to simplify Xen implementation. +*/ + ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, + sp_list_entry_size); +if ( ret ) +return ret; + + count++; } *sp_count = count; -out: - release_ret =ffa_rxtx_spmc_rx_release (notify_fw); -if ( release_ret ) -gprintk (XENLOG_WARNING , -"ffa: Error releasing SPMC RX buffer: %d\n" , release_ret); -return ret; +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; + +return FFA_RET_OK ; } static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t start_index,@@ -435,6 +426,14 @@ static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id,return res; }
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id, · line 432
Quoted 4 lines
xen/arch/arm/tee/ffa_partinfo.c +4
-0+static void ffa_sp_list_cache_free (void ) +{ +XFREE (sp_list); + sp_list =NULL ;
XFREE()is already settingsp_listto NULL.
Ack will fix in v2
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/tee/ffa_partinfo.c +4
-0+ sp_list_count =0 ; + sp_list_entry_size =0 ; +} +static void uninit_subscribers (void ) { subscr_vm_created_count =0 ;@@ -443,6 +442,68 @@ static void uninit_subscribers(void)XFREE (subscr_vm_destroyed); }
Quoted 16 lines
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 455
Quoted 11 lines
xen/arch/arm/tee/ffa_partinfo.c +11
-0+static bool ffa_sp_list_cache_init (const void *buf,uint32_t count, +uint32_t fpi_size) +{ +const uint8_t *src = buf; +uint32_t secure_count =0 ; +uint32_t n, idx =0 ; +bool warned = false; + +if ( fpi_size <sizeof (struct ffa_partition_info_1_0 ) || + fpi_size >=FFA_PAGE_SIZE ) +return false;Would it make sense to check that
fpi_sizeis well aligned with structffa_partition_info_1_0? If it's an odd size, we'll make unaligned accesses below withmemcpy(). But perhaps that's a bit much. The SPMC isn't supposed to provide garbage.
Memcpy should prevent issues even if accesses are not aligned.
If we had this test, we cannot return an error to the SPMC so we would have to generate one to the caller. It is simpler i think to handle non-aligned as we do not expect the SPMC to generate such a case.
Tell me if you agree.
Quoted 10 lines
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +9
-0+ +if ( count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / fpi_size ) +return false; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); +
Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 467
Quoted 3 lines
xen/arch/arm/tee/ffa_partinfo.c +3
-0+if ( !FFA_ID_IS_SECURE (fpi->id) ) + { +if ( !warned )Is this needed? Doesn't
printk_once()already ensure this? Or did you happen to leaveprintk_once()by mistake and meant for this to be printed once each timeffa_sp_list_cache_init()is called, since "warned" isn't static.
Very right, i need to remove the warned, printk_once should be enough here.
Quoted 56 lines
Quoted 56 lines
xen/arch/arm/tee/ffa_partinfo.c +46
-1+ { +printk_once (XENLOG_ERR +"ffa: Firmware is not using bit 15 convention for IDs !!\n" ); + warned = true; + } +printk (XENLOG_ERR +"ffa: Secure partition with id 0x%04x cannot be used\n" , + fpi->id); +continue ; + } + + secure_count++; + } + +if ( secure_count ) + { + sp_list =xzalloc_bytes (secure_count * fpi_size); +if ( !sp_list ) +return false; + } + + sp_list_count = secure_count; + sp_list_entry_size = fpi_size; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); + +if ( !FFA_ID_IS_SECURE (fpi->id) ) +continue ; + +memcpy (sp_list + idx * fpi_size, fpi, fpi_size); + idx++; + } + +return true; +} +static bool init_subscribers (void *buf,uint16_t count,uint32_t fpi_size) {uint16_t n;@@ -549,12 +610,22 @@ bool ffa_partinfo_init(void) goto out; } - ret =init_subscribers (spmc_rx, count, fpi_size); +if ( !ffa_sp_list_cache_init (spmc_rx, count, fpi_size) ) + { +printk (XENLOG_ERR "ffa: Failed to cache SP list\n" ); + goto out; + } + + ret =init_subscribers (sp_list, sp_list_count, sp_list_entry_size);
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c bool ffa_partinfo_init(void) · line 626
Quoted 6 lines
out:
e = ffa_rxtx_spmc_rx_release(notify_fw); if ( e ) printk(XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n", e);xen/arch/arm/tee/ffa_partinfo.c +2
-0+if ( !ret ) +uninit_subscribers ();ret is initially false and can only be set to true if
init_subscribers()returns true. So there's never any point in callinguninit_subscribers().
True, I will fix that.
Quoted 7 lines
xen/arch/arm/tee/ffa_partinfo.c
Quoted 3 lines
xen/arch/arm/tee/ffa_partinfo.c +2
-0+if ( !ret ) +ffa_sp_list_cache_free ();ret can be false even if
ffa_sp_list_cache_init()hasn't been called yet. Callingffa_sp_list_cache_free()anyway is harmless, but not elegant.
yes, i will rework a bit the cleanup logic here.
Thanks for the review
Cheers
Bertrand
Quoted 9 lines
Cheers,
JensQuoted 5 lines
return ret; }--
2.52.0
Re: [PATCH 2/4] xen/arm: ffa: Cache SP partition info at init
Hi Bertrand,
Quoted 72 lines · Bertrand Marquis
Hi Jens,
Quoted 60 lines · Jens Wiklander
Hi Bertrand,
Quoted 54 lines · Bertrand Marquis
FFA_PARTITION_INFO_GETcurrently queries the SPMC on each call and walks the RX buffer every time. The SP list is expected to be static, so repeated firmware calls and validation are unnecessary.Cache the SPMC partition-info list at init time, keeping only secure endpoints, and reuse the cached entries for SP count and partition-info responses. Initialize the VM create/destroy subscriber lists from the cached list and free the cache on init failure.
SP partition info now reflects the init-time snapshot and will not change.
Signed-off-by Bertrand Marquis <[email protected]>---
xen/arch/arm/tee/ffa_partinfo.c| 205 +++++++++++++++++++++-----------1 file changed, 138 insertions(+), 67 deletions(-)xen/arch/arm/tee/ffa_partinfo.c +11
-2@@ -6,6 +6,7 @@#include <xen/const.h> #include <xen/sizes.h> #include <xen/types.h> +#include <xen/xmalloc.h> #include <asm/smccc.h> #include <asm/regs.h> @@ -33,6 +34,10 @@ static uint16_t subscr_vm_created_count __read_mostly;static uint16_t *subscr_vm_destroyed__read_mostly ;static uint16_t subscr_vm_destroyed_count__read_mostly ; +/* SP list cache (secure endpoints only); populated at init. */ +static void *sp_list__read_mostly ; +static uint32_t sp_list_count__read_mostly ; +static uint32_t sp_list_entry_size__read_mostly ;static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {@@ -79,92 +84,78 @@ static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src,return FFA_RET_OK ; } -static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) +static bool ffa_sp_entry_matches_uuid (const void *entry,struct ffa_uuid uuid) { -uint32_t src_size; +const struct ffa_partition_info_1_1 *fpi = entry; +struct ffa_uuid sp_uuid; + +if (ffa_uuid_is_nil (uuid) ) +return true;Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src, · line 96
Quoted 4 lines
xen/arch/arm/tee/ffa_partinfo.c +2
-2-return ffa_partition_info_get (uuid,FFA_PARTITION_INFO_GET_COUNT_FLAG , - sp_count, &src_size); +if ( sp_list_entry_size <sizeof (*fpi) ) +return false;This can never happen since we don't accept SPMC below version 1.1. We even have a check to ensure the SPMC doesn't return a too-small
spi_size.
Quoted 2 lines
I tried to make the code forward compatible. It is not really a possible case right now but we could fall into this in the future.
At the moment, it's only ffa_sp_list_cache_init(), which initializes and puts things in the SP. So as long as ffa_sp_list_cache_init() and this function are in sync, there should be no problem. It simplifies things if we can trust the SP cache to be usable.
Quoted 171 lines
Quoted 133 lines
Quoted 133 lines
xen/arch/arm/tee/ffa_partinfo.c +44
-62+ +memcpy (&sp_uuid, fpi->uuid,sizeof (sp_uuid)); +return ffa_uuid_equal (uuid, sp_uuid); } -static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, -void **dst_buf,void *end_buf, -uint32_t dst_size) +static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) { -int32_t ret; -int32_t release_ret; -uint32_t src_size, real_sp_count; -void *src_buf;uint32_t count =0 ; -bool notify_fw = false; - -/* We need to use the RX buffer to receive the list */ - src_buf =ffa_rxtx_spmc_rx_acquire (); -if ( !src_buf ) -return FFA_RET_DENIED ; - - ret =ffa_partition_info_get (uuid,0 , &real_sp_count, &src_size); -if ( ret ) - goto out; - notify_fw = true; +uint32_t n; -/* Validate the src_size we got */ -if ( src_size <sizeof (struct ffa_partition_info_1_0 ) || - src_size >=FFA_PAGE_SIZE ) +for ( n =0 ; n < sp_list_count; n++ ) { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; +void *entry = sp_list + n * sp_list_entry_size; + +if (ffa_sp_entry_matches_uuid (entry, uuid) ) + count++; } -/* -* Limit the maximum time we hold the CPU by limiting the number of SPs. -* We just ignore the extra ones as this is tested during init in -* ffa_partinfo_init so the only possible reason is SP have been added -* since boot. -*/ -if ( real_sp_count >FFA_MAX_NUM_SP ) - real_sp_count =FFA_MAX_NUM_SP ; + *sp_count = count; -/* Make sure the data fits in our buffer */ -if ( real_sp_count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / src_size ) - { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; - } +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; -for (uint32_t sp_num =0 ; sp_num < real_sp_count; sp_num++ ) - { -struct ffa_partition_info_1_1 *fpi = src_buf; +return FFA_RET_OK ; +} -/* filter out SP not following bit 15 convention if any */ -if (FFA_ID_IS_SECURE (fpi->id) ) - { -/* -* If VM is 1.0 but firmware is 1.1 we could have several entries -* with the same ID but different UUIDs. In this case the VM will -* get a list with several time the same ID. -* This is a non-compliance to the specification but 1.0 VMs should -* handle that on their own to simplify Xen implementation. -*/ +static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, +void **dst_buf,void *end_buf, +uint32_t dst_size) +{ +int32_t ret; +uint32_t count =0 ; +uint32_t n; - ret =ffa_copy_info (dst_buf, end_buf, src_buf, dst_size, src_size); -if ( ret ) - goto out; +for ( n =0 ; n < sp_list_count; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; - count++; - } +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; - src_buf += src_size; +/* +* If VM is 1.0 but firmware is 1.1 we could have several entries +* with the same ID but different UUIDs. In this case the VM will +* get a list with several time the same ID. +* This is a non-compliance to the specification but 1.0 VMs should +* handle that on their own to simplify Xen implementation. +*/ + ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, + sp_list_entry_size); +if ( ret ) +return ret; + + count++; } *sp_count = count; -out: - release_ret =ffa_rxtx_spmc_rx_release (notify_fw); -if ( release_ret ) -gprintk (XENLOG_WARNING , -"ffa: Error releasing SPMC RX buffer: %d\n" , release_ret); -return ret; +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; + +return FFA_RET_OK ; } static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t start_index,@@ -435,6 +426,14 @@ static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id,return res; }Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id, · line 432
Quoted 4 lines
xen/arch/arm/tee/ffa_partinfo.c +4
-0+static void ffa_sp_list_cache_free (void ) +{ +XFREE (sp_list); + sp_list =NULL ;
XFREE()is already settingsp_listto NULL.Ack will fix in v2
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/tee/ffa_partinfo.c +4
-0+ sp_list_count =0 ; + sp_list_entry_size =0 ; +} +static void uninit_subscribers (void ) { subscr_vm_created_count =0 ;@@ -443,6 +442,68 @@ static void uninit_subscribers(void)XFREE (subscr_vm_destroyed); }Quoted 16 lines
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 455
Quoted 11 lines
xen/arch/arm/tee/ffa_partinfo.c +11
-0+static bool ffa_sp_list_cache_init (const void *buf,uint32_t count, +uint32_t fpi_size) +{ +const uint8_t *src = buf; +uint32_t secure_count =0 ; +uint32_t n, idx =0 ; +bool warned = false; + +if ( fpi_size <sizeof (struct ffa_partition_info_1_0 ) || + fpi_size >=FFA_PAGE_SIZE ) +return false;Would it make sense to check that
fpi_sizeis well aligned with structffa_partition_info_1_0? If it's an odd size, we'll make unaligned accesses below withmemcpy(). But perhaps that's a bit much. The SPMC isn't supposed to provide garbage.
Quoted 5 lines
Memcpy should prevent issues even if accesses are not aligned.
If we had this test, we cannot return an error to the SPMC so we would have to generate one to the caller. It is simpler i think to handle non-aligned as we do not expect the SPMC to generate such a case.
Tell me if you agree.
We dereference fpi below, and depending on compiler flags and pointer types, memcpy() might not be safe with unaligned pointers. From 6.3.2.3 Pointers, paragraph 7, in the C standard:
"A pointer to an object type may be converted to a pointer to a different object type. If the
resulting pointer is not correctly aligned for the referenced type, the behavior is
undefined."
I've seen past examples where the compiler optimized memcpy() in a way that breaks with unaligned pointers.
We don't expect the test above to fail, but if it does we will not use the secure firmware. I think refusing unexpected sizes is even simpler. It should make finding eventual errors much easier.
So my question above is whether it's worth checking that fpi_size is well-aligned, or if it's so unlikely that we don't need to consider it.
Cheers,
Jens
Quoted 117 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/tee/ffa_partinfo.c +9
-0+ +if ( count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / fpi_size ) +return false; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); +Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 467
Quoted 3 lines
xen/arch/arm/tee/ffa_partinfo.c +3
-0+if ( !FFA_ID_IS_SECURE (fpi->id) ) + { +if ( !warned )Is this needed? Doesn't
printk_once()already ensure this? Or did you happen to leaveprintk_once()by mistake and meant for this to be printed once each timeffa_sp_list_cache_init()is called, since "warned" isn't static.Very right, i need to remove the warned,
printk_onceshould be enough here.Quoted 56 lines
Quoted 56 lines
xen/arch/arm/tee/ffa_partinfo.c +46
-1+ { +printk_once (XENLOG_ERR +"ffa: Firmware is not using bit 15 convention for IDs !!\n" ); + warned = true; + } +printk (XENLOG_ERR +"ffa: Secure partition with id 0x%04x cannot be used\n" , + fpi->id); +continue ; + } + + secure_count++; + } + +if ( secure_count ) + { + sp_list =xzalloc_bytes (secure_count * fpi_size); +if ( !sp_list ) +return false; + } + + sp_list_count = secure_count; + sp_list_entry_size = fpi_size; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); + +if ( !FFA_ID_IS_SECURE (fpi->id) ) +continue ; + +memcpy (sp_list + idx * fpi_size, fpi, fpi_size); + idx++; + } + +return true; +} +static bool init_subscribers (void *buf,uint16_t count,uint32_t fpi_size) {uint16_t n;@@ -549,12 +610,22 @@ bool ffa_partinfo_init(void) goto out; } - ret =init_subscribers (spmc_rx, count, fpi_size); +if ( !ffa_sp_list_cache_init (spmc_rx, count, fpi_size) ) + { +printk (XENLOG_ERR "ffa: Failed to cache SP list\n" ); + goto out; + } + + ret =init_subscribers (sp_list, sp_list_count, sp_list_entry_size);Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c bool ffa_partinfo_init(void) · line 626
Quoted 6 lines
out:
e = ffa_rxtx_spmc_rx_release(notify_fw); if ( e ) printk(XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n", e);xen/arch/arm/tee/ffa_partinfo.c +2
-0+if ( !ret ) +uninit_subscribers ();ret is initially false and can only be set to true if
init_subscribers()returns true. So there's never any point in callinguninit_subscribers().True, I will fix that.
Quoted 7 lines
xen/arch/arm/tee/ffa_partinfo.c
Quoted 3 lines
xen/arch/arm/tee/ffa_partinfo.c +2
-0+if ( !ret ) +ffa_sp_list_cache_free ();ret can be false even if
ffa_sp_list_cache_init()hasn't been called yet. Callingffa_sp_list_cache_free()anyway is harmless, but not elegant.yes, i will rework a bit the cleanup logic here.
Thanks for the review
Cheers
BertrandQuoted 9 lines
Cheers,
JensQuoted 5 lines
return ret; }--
2.52.0
Re: [PATCH 2/4] xen/arm: ffa: Cache SP partition info at init
Hi Jens,
Quoted 78 lines · Jens Wiklander
Hi Bertrand,
Quoted 72 lines · Bertrand Marquis
Hi Jens,
Quoted 68 lines · Jens Wiklander
Hi Bertrand,
Quoted 48 lines · Bertrand Marquis
FFA_PARTITION_INFO_GETcurrently queries the SPMC on each call and walks the RX buffer every time. The SP list is expected to be static, so repeated firmware calls and validation are unnecessary.Cache the SPMC partition-info list at init time, keeping only secure endpoints, and reuse the cached entries for SP count and partition-info responses. Initialize the VM create/destroy subscriber lists from the cached list and free the cache on init failure.
SP partition info now reflects the init-time snapshot and will not change.
Signed-off-by Bertrand Marquis <[email protected]>---
xen/arch/arm/tee/ffa_partinfo.c| 205 +++++++++++++++++++++-----------1 file changed, 138 insertions(+), 67 deletions(-)xen/arch/arm/tee/ffa_partinfo.c +6
-2@@ -6,6 +6,7 @@#include <xen/const.h> #include <xen/sizes.h> #include <xen/types.h> +#include <xen/xmalloc.h> #include <asm/smccc.h> #include <asm/regs.h> @@ -33,6 +34,10 @@ static uint16_t subscr_vm_created_count __read_mostly;static uint16_t *subscr_vm_destroyed__read_mostly ;static uint16_t subscr_vm_destroyed_count__read_mostly ; +/* SP list cache (secure endpoints only); populated at init. */ +static void *sp_list__read_mostly ; +static uint32_t sp_list_count__read_mostly ; +static uint32_t sp_list_entry_size__read_mostly ;static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {@@ -79,92 +84,78 @@ static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src,return FFA_RET_OK ; } -static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) +static bool ffa_sp_entry_matches_uuid (const void *entry,struct ffa_uuid uuid) { -uint32_t src_size;xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src, · line 96
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +7
-2+const struct ffa_partition_info_1_1 *fpi = entry; +struct ffa_uuid sp_uuid; + +if (ffa_uuid_is_nil (uuid) ) +return true; -return ffa_partition_info_get (uuid,FFA_PARTITION_INFO_GET_COUNT_FLAG , - sp_count, &src_size); +if ( sp_list_entry_size <sizeof (*fpi) ) +return false;This can never happen since we don't accept SPMC below version 1.1. We even have a check to ensure the SPMC doesn't return a too-small
spi_size.
Quoted 7 lines
Quoted 2 lines
I tried to make the code forward compatible. It is not really a possible case right now but we could fall into this in the future.
At the moment, it's only
ffa_sp_list_cache_init(), which initializes and puts things in the SP. So as long asffa_sp_list_cache_init()and this function are in sync, there should be no problem. It simplifies things if we can trust the SP cache to be usable.
Agree, we should not come into a situation where the SP cache content cannot be used. Anything to be check must have been checked or converted when creating the cache not when using it.
Quoted 187 lines
Quoted 172 lines
Quoted 134 lines
Quoted 134 lines
xen/arch/arm/tee/ffa_partinfo.c +44
-62+ +memcpy (&sp_uuid, fpi->uuid,sizeof (sp_uuid)); +return ffa_uuid_equal (uuid, sp_uuid); } -static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, -void **dst_buf,void *end_buf, -uint32_t dst_size) +static int32_t ffa_get_sp_count (struct ffa_uuid uuid,uint32_t *sp_count) { -int32_t ret; -int32_t release_ret; -uint32_t src_size, real_sp_count; -void *src_buf;uint32_t count =0 ; -bool notify_fw = false; - -/* We need to use the RX buffer to receive the list */ - src_buf =ffa_rxtx_spmc_rx_acquire (); -if ( !src_buf ) -return FFA_RET_DENIED ; - - ret =ffa_partition_info_get (uuid,0 , &real_sp_count, &src_size); -if ( ret ) - goto out; - notify_fw = true; +uint32_t n; -/* Validate the src_size we got */ -if ( src_size <sizeof (struct ffa_partition_info_1_0 ) || - src_size >=FFA_PAGE_SIZE ) +for ( n =0 ; n < sp_list_count; n++ ) { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; +void *entry = sp_list + n * sp_list_entry_size; + +if (ffa_sp_entry_matches_uuid (entry, uuid) ) + count++; } -/* -* Limit the maximum time we hold the CPU by limiting the number of SPs. -* We just ignore the extra ones as this is tested during init in -* ffa_partinfo_init so the only possible reason is SP have been added -* since boot. -*/ -if ( real_sp_count >FFA_MAX_NUM_SP ) - real_sp_count =FFA_MAX_NUM_SP ; + *sp_count = count; -/* Make sure the data fits in our buffer */ -if ( real_sp_count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / src_size ) - { - ret =FFA_RET_NOT_SUPPORTED ; - goto out; - } +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; -for (uint32_t sp_num =0 ; sp_num < real_sp_count; sp_num++ ) - { -struct ffa_partition_info_1_1 *fpi = src_buf; +return FFA_RET_OK ; +} -/* filter out SP not following bit 15 convention if any */ -if (FFA_ID_IS_SECURE (fpi->id) ) - { -/* -* If VM is 1.0 but firmware is 1.1 we could have several entries -* with the same ID but different UUIDs. In this case the VM will -* get a list with several time the same ID. -* This is a non-compliance to the specification but 1.0 VMs should -* handle that on their own to simplify Xen implementation. -*/ +static int32_t ffa_get_sp_partinfo (struct ffa_uuid uuid,uint32_t *sp_count, +void **dst_buf,void *end_buf, +uint32_t dst_size) +{ +int32_t ret; +uint32_t count =0 ; +uint32_t n; - ret =ffa_copy_info (dst_buf, end_buf, src_buf, dst_size, src_size); -if ( ret ) - goto out; +for ( n =0 ; n < sp_list_count; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; - count++; - } +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; - src_buf += src_size; +/* +* If VM is 1.0 but firmware is 1.1 we could have several entries +* with the same ID but different UUIDs. In this case the VM will +* get a list with several time the same ID. +* This is a non-compliance to the specification but 1.0 VMs should +* handle that on their own to simplify Xen implementation. +*/ + ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, + sp_list_entry_size); +if ( ret ) +return ret; + + count++; } *sp_count = count; -out: - release_ret =ffa_rxtx_spmc_rx_release (notify_fw); -if ( release_ret ) -gprintk (XENLOG_WARNING , -"ffa: Error releasing SPMC RX buffer: %d\n" , release_ret); -return ret; +if ( !ffa_uuid_is_nil (uuid) && !count ) +return FFA_RET_INVALID_PARAMETERS ; + +return FFA_RET_OK ; } static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t start_index,@@ -435,6 +426,14 @@ static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id,return res; }Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id, · line 432
Quoted 4 lines
xen/arch/arm/tee/ffa_partinfo.c +4
-0+static void ffa_sp_list_cache_free (void ) +{ +XFREE (sp_list); + sp_list =NULL ;
XFREE()is already settingsp_listto NULL.Ack will fix in v2
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/tee/ffa_partinfo.c +4
-0+ sp_list_count =0 ; + sp_list_entry_size =0 ; +} +static void uninit_subscribers (void ) { subscr_vm_created_count =0 ;@@ -443,6 +442,68 @@ static void uninit_subscribers(void)XFREE (subscr_vm_destroyed); }Quoted 16 lines
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 455
Quoted 11 lines
xen/arch/arm/tee/ffa_partinfo.c +11
-0+static bool ffa_sp_list_cache_init (const void *buf,uint32_t count, +uint32_t fpi_size) +{ +const uint8_t *src = buf; +uint32_t secure_count =0 ; +uint32_t n, idx =0 ; +bool warned = false; + +if ( fpi_size <sizeof (struct ffa_partition_info_1_0 ) || + fpi_size >=FFA_PAGE_SIZE ) +return false;Would it make sense to check that
fpi_sizeis well aligned with structffa_partition_info_1_0? If it's an odd size, we'll make unaligned accesses below withmemcpy(). But perhaps that's a bit much. The SPMC isn't supposed to provide garbage.Quoted 5 lines
Memcpy should prevent issues even if accesses are not aligned.
If we had this test, we cannot return an error to the SPMC so we would have to generate one to the caller. It is simpler i think to handle non-aligned as we do not expect the SPMC to generate such a case.
Tell me if you agree.We dereference fpi below, and depending on compiler flags and pointer types,
memcpy()might not be safe with unaligned pointers. From 6.3.2.3 Pointers, paragraph 7, in the C standard:
"A pointer to an object type may be converted to a pointer to a different object type. If the
resulting pointer is not correctly aligned for the referenced type, the behavior is
undefined."
Quoted 6 lines
I've seen past examples where the compiler optimized
memcpy()in a way that breaks with unaligned pointers.We don't expect the test above to fail, but if it does we will not use the secure firmware. I think refusing unexpected sizes is even simpler. It should make finding eventual errors much easier.
In the ffa spec, the size can grow and this is why there is a size field. FF-A expect that we either ignore or copy without looking the extra content.
I think we should not be dependent on any alignment so we need to make sure we do the copy in a way that is robust to non alignments.
Quoted 4 lines
So my question above is whether it's worth checking that
fpi_sizeis well-aligned, or if it's so unlikely that we don't need to consider it.
I think we need to find a solution where we handle properly things even if the size is not aligned. I though using memcpy would protect against that but maybe we need to use a stronger solution to ensure that works even if data is unaligned.
Cheers
Bertrand
Quoted 119 lines
Cheers,
JensQuoted 115 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/tee/ffa_partinfo.c +9
-0+ +if ( count > (FFA_RXTX_PAGE_COUNT *FFA_PAGE_SIZE ) / fpi_size ) +return false; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); +Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 467
Quoted 3 lines
xen/arch/arm/tee/ffa_partinfo.c +3
-0+if ( !FFA_ID_IS_SECURE (fpi->id) ) + { +if ( !warned )Is this needed? Doesn't
printk_once()already ensure this? Or did you happen to leaveprintk_once()by mistake and meant for this to be printed once each timeffa_sp_list_cache_init()is called, since "warned" isn't static.Very right, i need to remove the warned,
printk_onceshould be enough here.Quoted 56 lines
Quoted 56 lines
xen/arch/arm/tee/ffa_partinfo.c +46
-1+ { +printk_once (XENLOG_ERR +"ffa: Firmware is not using bit 15 convention for IDs !!\n" ); + warned = true; + } +printk (XENLOG_ERR +"ffa: Secure partition with id 0x%04x cannot be used\n" , + fpi->id); +continue ; + } + + secure_count++; + } + +if ( secure_count ) + { + sp_list =xzalloc_bytes (secure_count * fpi_size); +if ( !sp_list ) +return false; + } + + sp_list_count = secure_count; + sp_list_entry_size = fpi_size; + +for ( n =0 ; n < count; n++ ) + { +const struct ffa_partition_info_1_0 *fpi = + (const void *)(src + n * fpi_size); + +if ( !FFA_ID_IS_SECURE (fpi->id) ) +continue ; + +memcpy (sp_list + idx * fpi_size, fpi, fpi_size); + idx++; + } + +return true; +} +static bool init_subscribers (void *buf,uint16_t count,uint32_t fpi_size) {uint16_t n;@@ -549,12 +610,22 @@ bool ffa_partinfo_init(void) goto out; } - ret =init_subscribers (spmc_rx, count, fpi_size); +if ( !ffa_sp_list_cache_init (spmc_rx, count, fpi_size) ) + { +printk (XENLOG_ERR "ffa: Failed to cache SP list\n" ); + goto out; + } + + ret =init_subscribers (sp_list, sp_list_count, sp_list_entry_size);Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c bool ffa_partinfo_init(void) · line 626
Quoted 6 lines
out:
e = ffa_rxtx_spmc_rx_release(notify_fw); if ( e ) printk(XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n", e);xen/arch/arm/tee/ffa_partinfo.c +2
-0+if ( !ret ) +uninit_subscribers ();ret is initially false and can only be set to true if
init_subscribers()returns true. So there's never any point in callinguninit_subscribers().True, I will fix that.
Quoted 7 lines
xen/arch/arm/tee/ffa_partinfo.c
Quoted 3 lines
xen/arch/arm/tee/ffa_partinfo.c +2
-0+if ( !ret ) +ffa_sp_list_cache_free ();ret can be false even if
ffa_sp_list_cache_init()hasn't been called yet. Callingffa_sp_list_cache_free()anyway is harmless, but not elegant.yes, i will rework a bit the cleanup logic here.
Thanks for the review
Cheers
BertrandQuoted 9 lines
Cheers,
JensQuoted 5 lines
return ret; }--
2.52.0
Re: [PATCH 2/4] xen/arm: ffa: Cache SP partition info at init
Hi Bertrand,
Quoted 35 lines · Bertrand Marquis
Hi Jens,
Quoted 32 lines · Jens Wiklander
Hi Bertrand,
Quoted 26 lines · Bertrand Marquis
Hi Jens,
Quoted 23 lines · Jens Wiklander
Hi Bertrand,
Quoted 17 lines · Bertrand Marquis
FFA_PARTITION_INFO_GETcurrently queries the SPMC on each call and walks the RX buffer every time. The SP list is expected to be static, so repeated firmware calls and validation are unnecessary.Cache the SPMC partition-info list at init time, keeping only secure endpoints, and reuse the cached entries for SP count and partition-info responses. Initialize the VM create/destroy subscriber lists from the cached list and free the cache on init failure.
SP partition info now reflects the init-time snapshot and will not change.
Signed-off-by Bertrand Marquis <[email protected]>---
xen/arch/arm/tee/ffa_partinfo.c| 205 +++++++++++++++++++++-----------1 file changed, 138 insertions(+), 67 deletions(-)
[snip]
Quoted 46 lines
Quoted 32 lines
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c +8
-0+static bool ffa_sp_list_cache_init (const void *buf,uint32_t count, +uint32_t fpi_size) +{ +const uint8_t *src = buf; +uint32_t secure_count =0 ; +uint32_t n, idx =0 ; +bool warned = false; +Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c static void uninit_subscribers(void) · line 455
Quoted 3 lines
xen/arch/arm/tee/ffa_partinfo.c +3
-0+if ( fpi_size <sizeof (struct ffa_partition_info_1_0 ) || + fpi_size >=FFA_PAGE_SIZE ) +return false;Would it make sense to check that
fpi_sizeis well aligned with structffa_partition_info_1_0? If it's an odd size, we'll make unaligned accesses below withmemcpy(). But perhaps that's a bit much. The SPMC isn't supposed to provide garbage.Quoted 5 lines
Memcpy should prevent issues even if accesses are not aligned.
If we had this test, we cannot return an error to the SPMC so we would have to generate one to the caller. It is simpler i think to handle non-aligned as we do not expect the SPMC to generate such a case.
Tell me if you agree.We dereference fpi below, and depending on compiler flags and pointer types,
memcpy()might not be safe with unaligned pointers. From 6.3.2.3 Pointers, paragraph 7, in the C standard:
"A pointer to an object type may be converted to a pointer to a different object type. If the
resulting pointer is not correctly aligned for the referenced type, the behavior is
undefined."Quoted 6 lines
I've seen past examples where the compiler optimized
memcpy()in a way that breaks with unaligned pointers.We don't expect the test above to fail, but if it does we will not use the secure firmware. I think refusing unexpected sizes is even simpler. It should make finding eventual errors much easier.
In the ffa spec, the size can grow and this is why there is a size field. FF-A expect that we either ignore or copy without looking the extra content.
I think we should not be dependent on any alignment so we need to make sure we do the copy in a way that is robust to non alignments.Quoted 1 line
Quoted 6 lines
Quoted 3 lines
So my question above is whether it's worth checking that
fpi_sizeis well-aligned, or if it's so unlikely that we don't need to consider it.I think we need to find a solution where we handle properly things even if the size is not aligned.
I agree, that's the most robust.
Quoted 3 lines
I though using memcpy would protect against
that but maybe we need to use a stronger solution to ensure that works even if data is unaligned.
memcpy() can be used, but the pointer values must not pass through a struct ffa_partition_info_1_0 pointer or such.
Cheers,
Jens
PATCH 3/4 xen/arm: ffa: Drop SP subscriber lists
In reply to Bertrand MarquisThe init-time SP cache already contains partition properties, but the code still builds separate subscriber arrays for VM created/destroyed notifications. That duplicates state and allocation.
Use the cached SP list directly to:
- decide which SPs receive created/destroyed notifications
- build the per-domain destroy bitmap
- skip destroy notifications for SPs not notified on create
No functional changes.
--- xen/arch/arm/tee/ffa_partinfo.c | 155 ++++++++------------------------ 1 file changed, 36 insertions(+), 119 deletions(-)
xen/arch/arm/tee/ffa_partinfo.c +36 -119
@@ -28,12 +28,6 @@ struct ffa_partition_info_1_1 {uint8_t uuid[16 ]; }; -/* SPs subscribing to VM_CREATE and VM_DESTROYED events */ -static uint16_t *subscr_vm_created__read_mostly ; -static uint16_t subscr_vm_created_count__read_mostly ; -static uint16_t *subscr_vm_destroyed__read_mostly ; -static uint16_t subscr_vm_destroyed_count__read_mostly ; -/* SP list cache (secure endpoints only); populated at init. */ static void *sp_list__read_mostly ;static uint32_t sp_list_count__read_mostly ;
@@ -434,14 +428,6 @@ static void ffa_sp_list_cache_free(void) sp_list_entry_size =0 ; } -static void uninit_subscribers (void ) -{ - subscr_vm_created_count =0 ; - subscr_vm_destroyed_count =0 ; -XFREE (subscr_vm_created); -XFREE (subscr_vm_destroyed); -} -static bool ffa_sp_list_cache_init (const void *buf,uint32_t count,uint32_t fpi_size) {
@@ -504,79 +490,6 @@ static bool ffa_sp_list_cache_init(const void *buf, uint32_t count,return true; } -static bool init_subscribers (void *buf,uint16_t count,uint32_t fpi_size) -{ -uint16_t n; -uint16_t c_pos; -uint16_t d_pos; -struct ffa_partition_info_1_1 *fpi; - -if ( fpi_size <sizeof (struct ffa_partition_info_1_1 ) ) - { -printk (XENLOG_ERR "ffa: partition info size invalid: %u\n" , fpi_size); -return false; - } - - subscr_vm_created_count =0 ; - subscr_vm_destroyed_count =0 ; -for ( n =0 ; n < count; n++ ) - { - fpi = buf + n * fpi_size; - -/* -* We need to have secure partitions using bit 15 set convention for -* secure partition IDs. -* Inform the user with a log and discard giving created or destroy -* event to those IDs. -*/ -if ( !FFA_ID_IS_SECURE (fpi->id) ) - { -printk_once (XENLOG_ERR -"ffa: Firmware is not using bit 15 convention for IDs !!\n" ); -printk (XENLOG_ERR -"ffa: Secure partition with id 0x%04x cannot be used\n" , - fpi->id); - } -else - { -if ( fpi->partition_properties &FFA_PART_PROP_NOTIF_CREATED ) - subscr_vm_created_count++; -if ( fpi->partition_properties &FFA_PART_PROP_NOTIF_DESTROYED ) - subscr_vm_destroyed_count++; - } - } - -if ( subscr_vm_created_count ) - subscr_vm_created =xzalloc_array (uint16_t, subscr_vm_created_count); -if ( subscr_vm_destroyed_count ) - subscr_vm_destroyed =xzalloc_array (uint16_t, - subscr_vm_destroyed_count); -if ( (subscr_vm_created_count && !subscr_vm_created) || - (subscr_vm_destroyed_count && !subscr_vm_destroyed) ) - { -printk (XENLOG_ERR "ffa: Failed to allocate subscription lists\n" ); -uninit_subscribers (); -return false; - } - -for ( c_pos =0 , d_pos =0 , n =0 ; n < count; n++ ) - { - fpi = buf + n * fpi_size; - -if (FFA_ID_IS_SECURE (fpi->id) ) - { -if ( fpi->partition_properties &FFA_PART_PROP_NOTIF_CREATED ) - subscr_vm_created[c_pos++] = fpi->id; -if ( fpi->partition_properties &FFA_PART_PROP_NOTIF_DESTROYED ) - subscr_vm_destroyed[d_pos++] = fpi->id; - } - } - -return true; -} - - -bool ffa_partinfo_init (void ) {bool ret = false;
@@ -616,48 +529,43 @@ bool ffa_partinfo_init(void) goto out; } - ret =init_subscribers (sp_list, sp_list_count, sp_list_entry_size); +if ( sp_list_entry_size <sizeof (struct ffa_partition_info_1_1 ) ) + { +printk (XENLOG_ERR "ffa: partition info size invalid: %u\n" , + sp_list_entry_size); + goto out; + } + ret = true; out: e =ffa_rxtx_spmc_rx_release (notify_fw);if ( e )printk (XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n" , e); -if ( !ret ) -uninit_subscribers ();if ( !ret )ffa_sp_list_cache_free ();return ret; } -static bool is_in_subscr_list (const uint16_t *subscr,uint16_t start, -uint16_t end,uint16_t sp_id) +static void vm_destroy_bitmap_init (struct ffa_ctx *ctx, +unsigned int first_unnotified) {unsigned int n; +struct ffa_partition_info_1_1 *fpi; -for ( n = start; n < end; n++ ) +for ( n =0 ; n < sp_list_count; n++ ) { -if ( subscr[n] == sp_id ) -return true; - } - -return false; -} + fpi = sp_list + n * sp_list_entry_size; -static void vm_destroy_bitmap_init (struct ffa_ctx *ctx, -unsigned int create_signal_count) -{ -unsigned int n; +if ( !(fpi->partition_properties &FFA_PART_PROP_NOTIF_DESTROYED ) ) +continue ; -for ( n =0 ; n < subscr_vm_destroyed_count; n++ ) - {/* * Skip SPs subscribed to the VM created event that never was * notified of the VM creation due to an error during * ffa_domain_init(). */ -if (is_in_subscr_list (subscr_vm_created, create_signal_count, - subscr_vm_created_count, - subscr_vm_destroyed[n]) ) +if ( (fpi->partition_properties &FFA_PART_PROP_NOTIF_CREATED ) && + n >= first_unnotified )continue ;set_bit (n, ctx->vm_destroy_bitmap);
@@ -666,32 +574,39 @@ static void vm_destroy_bitmap_init(struct ffa_ctx *ctx,int32_t ffa_partinfo_domain_init (struct domain *d) { -unsigned int count =BITS_TO_LONGS (subscr_vm_destroyed_count); +unsigned int count =BITS_TO_LONGS (sp_list_count);struct ffa_ctx *ctx = d->arch.tee;unsigned int n; +unsigned int first_unnotified = sp_list_count;int32_t res; +struct ffa_partition_info_1_1 *fpi; -if ( !ffa_fw_supports_fid (FFA_MSG_SEND_DIRECT_REQ_32 ) ) +if ( !ffa_fw_supports_fid (FFA_MSG_SEND_DIRECT_REQ_32 ) || !sp_list_count )return 0 ; ctx->vm_destroy_bitmap = xzalloc_array(unsigned long, count);if ( !ctx->vm_destroy_bitmap )return -ENOMEM ; -for ( n =0 ; n < subscr_vm_created_count; n++ ) +for ( n =0 ; n < sp_list_count; n++ ) { - res =ffa_direct_req_send_vm (subscr_vm_created[n],ffa_get_vm_id (d), + fpi = sp_list + n * sp_list_entry_size; +if ( !(fpi->partition_properties &FFA_PART_PROP_NOTIF_CREATED ) ) +continue ; + + res =ffa_direct_req_send_vm (fpi->id,ffa_get_vm_id (d),FFA_MSG_SEND_VM_CREATED );if ( res ) {printk (XENLOG_ERR "ffa: Failed to report creation of vm_id %u to %u: res %d\n" , -ffa_get_vm_id (d), subscr_vm_created[n], res); +ffa_get_vm_id (d), fpi->id, res); + first_unnotified = n;break ; } } -vm_destroy_bitmap_init (ctx, n); +vm_destroy_bitmap_init (ctx, first_unnotified); -if ( n != subscr_vm_created_count ) +if ( first_unnotified != sp_list_count )return -EIO ;return 0 ;
@@ -702,22 +617,24 @@ bool ffa_partinfo_domain_destroy(struct domain *d)struct ffa_ctx *ctx = d->arch.tee;unsigned int n;int32_t res; +struct ffa_partition_info_1_1 *fpi;if ( !ctx->vm_destroy_bitmap )return true; -for ( n =0 ; n < subscr_vm_destroyed_count; n++ ) +for ( n =0 ; n < sp_list_count; n++ ) {if ( !test_bit (n, ctx->vm_destroy_bitmap) )continue ; - res =ffa_direct_req_send_vm (subscr_vm_destroyed[n],ffa_get_vm_id (d), + fpi = sp_list + n * sp_list_entry_size; + res =ffa_direct_req_send_vm (fpi->id,ffa_get_vm_id (d),FFA_MSG_SEND_VM_DESTROYED );if ( res &&printk_ratelimit () )printk (XENLOG_WARNING "%pd: ffa: Failed to report destruction of vm_id %u to %u: res %d\n" , - d,ffa_get_vm_id (d), subscr_vm_destroyed[n], res); + d,ffa_get_vm_id (d), fpi->id, res);/* * For these two error codes the hypervisor is expected to resend
@@ -729,7 +646,7 @@ bool ffa_partinfo_domain_destroy(struct domain *d)clear_bit (n, ctx->vm_destroy_bitmap); } -if (bitmap_empty (ctx->vm_destroy_bitmap, subscr_vm_destroyed_count) ) +if (bitmap_empty (ctx->vm_destroy_bitmap, sp_list_count) )XFREE (ctx->vm_destroy_bitmap);return !ctx->vm_destroy_bitmap;
Signature · 1 line
2.52.0
Re: [PATCH 3/4] xen/arm: ffa: Drop SP subscriber lists
Hi Bertrand,
Quoted 10 lines · Bertrand Marquis
The init-time SP cache already contains partition properties, but the code still builds separate subscriber arrays for VM created/destroyed notifications. That duplicates state and allocation.
Use the cached SP list directly to:
- decide which SPs receive created/destroyed notifications
- build the per-domain destroy bitmap
- skip destroy notifications for SPs not notified on create
Quoted 6 lines
No functional changes.
Signed-off-by Bertrand Marquis <[email protected]>--- xen/arch/arm/tee/ffa_partinfo.c | 155 ++++++++------------------------ 1 file changed, 36 insertions(+), 119 deletions(-)
Looks good:
Cheers,
Jens
Quoted 265 lines
xen/arch/arm/tee/ffa_partinfo.c +36
-120@@ -28,12 +28,6 @@ struct ffa_partition_info_1_1 {uint8_t uuid[16 ]; }; -/* SPs subscribing to VM_CREATE and VM_DESTROYED events */ -static uint16_t *subscr_vm_created__read_mostly ; -static uint16_t subscr_vm_created_count__read_mostly ; -static uint16_t *subscr_vm_destroyed__read_mostly ; -static uint16_t subscr_vm_destroyed_count__read_mostly ; -/* SP list cache (secure endpoints only); populated at init. */ static void *sp_list__read_mostly ;static uint32_t sp_list_count__read_mostly ;@@ -434,14 +428,6 @@ static void ffa_sp_list_cache_free(void) sp_list_entry_size =0 ; } -static void uninit_subscribers (void ) -{ - subscr_vm_created_count =0 ; - subscr_vm_destroyed_count =0 ; -XFREE (subscr_vm_created); -XFREE (subscr_vm_destroyed); -} -static bool ffa_sp_list_cache_init (const void *buf,uint32_t count,uint32_t fpi_size) {@@ -504,79 +490,6 @@ static bool ffa_sp_list_cache_init(const void *buf, uint32_t count,return true; } -static bool init_subscribers (void *buf,uint16_t count,uint32_t fpi_size) -{ -uint16_t n; -uint16_t c_pos; -uint16_t d_pos; -struct ffa_partition_info_1_1 *fpi; - -if ( fpi_size <sizeof (struct ffa_partition_info_1_1 ) ) - { -printk (XENLOG_ERR "ffa: partition info size invalid: %u\n" , fpi_size); -return false; - } - - subscr_vm_created_count =0 ; - subscr_vm_destroyed_count =0 ; -for ( n =0 ; n < count; n++ ) - { - fpi = buf + n * fpi_size; - -/* -* We need to have secure partitions using bit 15 set convention for -* secure partition IDs. -* Inform the user with a log and discard giving created or destroy -* event to those IDs. -*/ -if ( !FFA_ID_IS_SECURE (fpi->id) ) - { -printk_once (XENLOG_ERR -"ffa: Firmware is not using bit 15 convention for IDs !!\n" ); -printk (XENLOG_ERR -"ffa: Secure partition with id 0x%04x cannot be used\n" , - fpi->id); - } -else - { -if ( fpi->partition_properties &FFA_PART_PROP_NOTIF_CREATED ) - subscr_vm_created_count++; -if ( fpi->partition_properties &FFA_PART_PROP_NOTIF_DESTROYED ) - subscr_vm_destroyed_count++; - } - } - -if ( subscr_vm_created_count ) - subscr_vm_created =xzalloc_array (uint16_t, subscr_vm_created_count); -if ( subscr_vm_destroyed_count ) - subscr_vm_destroyed =xzalloc_array (uint16_t, - subscr_vm_destroyed_count); -if ( (subscr_vm_created_count && !subscr_vm_created) || - (subscr_vm_destroyed_count && !subscr_vm_destroyed) ) - { -printk (XENLOG_ERR "ffa: Failed to allocate subscription lists\n" ); -uninit_subscribers (); -return false; - } - -for ( c_pos =0 , d_pos =0 , n =0 ; n < count; n++ ) - { - fpi = buf + n * fpi_size; - -if (FFA_ID_IS_SECURE (fpi->id) ) - { -if ( fpi->partition_properties &FFA_PART_PROP_NOTIF_CREATED ) - subscr_vm_created[c_pos++] = fpi->id; -if ( fpi->partition_properties &FFA_PART_PROP_NOTIF_DESTROYED ) - subscr_vm_destroyed[d_pos++] = fpi->id; - } - } - -return true; -} - - -bool ffa_partinfo_init (void ) {bool ret = false;@@ -616,48 +529,43 @@ bool ffa_partinfo_init(void) goto out; } - ret =init_subscribers (sp_list, sp_list_count, sp_list_entry_size); +if ( sp_list_entry_size <sizeof (struct ffa_partition_info_1_1 ) ) + { +printk (XENLOG_ERR "ffa: partition info size invalid: %u\n" , + sp_list_entry_size); + goto out; + } + ret = true; out: e =ffa_rxtx_spmc_rx_release (notify_fw);if ( e )printk (XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n" , e); -if ( !ret ) -uninit_subscribers ();if ( !ret )ffa_sp_list_cache_free ();return ret; } -static bool is_in_subscr_list (const uint16_t *subscr,uint16_t start, -uint16_t end,uint16_t sp_id) +static void vm_destroy_bitmap_init (struct ffa_ctx *ctx, +unsigned int first_unnotified) {unsigned int n; +struct ffa_partition_info_1_1 *fpi; -for ( n = start; n < end; n++ ) +for ( n =0 ; n < sp_list_count; n++ ) { -if ( subscr[n] == sp_id ) -return true; - } - -return false; -} + fpi = sp_list + n * sp_list_entry_size; -static void vm_destroy_bitmap_init (struct ffa_ctx *ctx, -unsigned int create_signal_count) -{ -unsigned int n; +if ( !(fpi->partition_properties &FFA_PART_PROP_NOTIF_DESTROYED ) ) +continue ; -for ( n =0 ; n < subscr_vm_destroyed_count; n++ ) - {/* * Skip SPs subscribed to the VM created event that never was * notified of the VM creation due to an error during * ffa_domain_init(). */ -if (is_in_subscr_list (subscr_vm_created, create_signal_count, - subscr_vm_created_count, - subscr_vm_destroyed[n]) ) +if ( (fpi->partition_properties &FFA_PART_PROP_NOTIF_CREATED ) && + n >= first_unnotified )continue ;set_bit (n, ctx->vm_destroy_bitmap);@@ -666,32 +574,39 @@ static void vm_destroy_bitmap_init(struct ffa_ctx *ctx,int32_t ffa_partinfo_domain_init (struct domain *d) { -unsigned int count =BITS_TO_LONGS (subscr_vm_destroyed_count); +unsigned int count =BITS_TO_LONGS (sp_list_count);struct ffa_ctx *ctx = d->arch.tee;unsigned int n; +unsigned int first_unnotified = sp_list_count;int32_t res; +struct ffa_partition_info_1_1 *fpi; -if ( !ffa_fw_supports_fid (FFA_MSG_SEND_DIRECT_REQ_32 ) ) +if ( !ffa_fw_supports_fid (FFA_MSG_SEND_DIRECT_REQ_32 ) || !sp_list_count )return 0 ; ctx->vm_destroy_bitmap = xzalloc_array(unsigned long, count);if ( !ctx->vm_destroy_bitmap )return -ENOMEM ; -for ( n =0 ; n < subscr_vm_created_count; n++ ) +for ( n =0 ; n < sp_list_count; n++ ) { - res =ffa_direct_req_send_vm (subscr_vm_created[n],ffa_get_vm_id (d), + fpi = sp_list + n * sp_list_entry_size; +if ( !(fpi->partition_properties &FFA_PART_PROP_NOTIF_CREATED ) ) +continue ; + + res =ffa_direct_req_send_vm (fpi->id,ffa_get_vm_id (d),FFA_MSG_SEND_VM_CREATED );if ( res ) {printk (XENLOG_ERR "ffa: Failed to report creation of vm_id %u to %u: res %d\n" , -ffa_get_vm_id (d), subscr_vm_created[n], res); +ffa_get_vm_id (d), fpi->id, res); + first_unnotified = n;break ; } } -vm_destroy_bitmap_init (ctx, n); +vm_destroy_bitmap_init (ctx, first_unnotified); -if ( n != subscr_vm_created_count ) +if ( first_unnotified != sp_list_count )return -EIO ;return 0 ;@@ -702,22 +617,24 @@ bool ffa_partinfo_domain_destroy(struct domain *d)struct ffa_ctx *ctx = d->arch.tee;unsigned int n;int32_t res; +struct ffa_partition_info_1_1 *fpi;if ( !ctx->vm_destroy_bitmap )return true; -for ( n =0 ; n < subscr_vm_destroyed_count; n++ ) +for ( n =0 ; n < sp_list_count; n++ ) {if ( !test_bit (n, ctx->vm_destroy_bitmap) )continue ; - res =ffa_direct_req_send_vm (subscr_vm_destroyed[n],ffa_get_vm_id (d), + fpi = sp_list + n * sp_list_entry_size; + res =ffa_direct_req_send_vm (fpi->id,ffa_get_vm_id (d),FFA_MSG_SEND_VM_DESTROYED );if ( res &&printk_ratelimit () )printk (XENLOG_WARNING "%pd: ffa: Failed to report destruction of vm_id %u to %u: res %d\n" , - d,ffa_get_vm_id (d), subscr_vm_destroyed[n], res); + d,ffa_get_vm_id (d), fpi->id, res);/* * For these two error codes the hypervisor is expected to resend @@ -729,7 +646,7 @@ bool ffa_partinfo_domain_destroy(struct domain *d)clear_bit (n, ctx->vm_destroy_bitmap); } -if (bitmap_empty (ctx->vm_destroy_bitmap, subscr_vm_destroyed_count) ) +if (bitmap_empty (ctx->vm_destroy_bitmap, sp_list_count) )XFREE (ctx->vm_destroy_bitmap);return !ctx->vm_destroy_bitmap; --2.52.0
PATCH 4/4 xen/arm: ffa: Add cached GET_REGS support
In reply to Bertrand MarquisFF-A v1.2 defines PARTITION_INFO_GET_REGS for register-based partition info retrieval, but Xen currently only supports the buffer-based GET path for guests.
Implement GET_REGS using the cached SP list and VM entries, including the register window layout and input validation. Track VM list changes via the partinfo tag and use it to validate GET_REGS tag inputs. Ensure that when a non-Nil UUID is specified, the UUID fields in both GET and GET_REGS results are MBZ as required by the specification.
PARTITION_INFO_GET_REGS is available to v1.2 guests, returning cached SP entries and VM entries with UUIDs zeroed for non-Nil UUID queries.
--- xen/arch/arm/tee/ffa.c | 16 +++ xen/arch/arm/tee/ffa_partinfo.c | 211 ++++++++++++++++++++++++++++++++ xen/arch/arm/tee/ffa_private.h | 4 +- 3 files changed, 230 insertions(+), 1 deletion(-)
xen/arch/arm/tee/ffa.c +16 -0
@@ -44,6 +44,11 @@* - doesn't support signalling the secondary scheduler of pending * notification for secure partitions * - doesn't support notifications for Xen itself +* o FFA_PARTITION_INFO_GET/GET_REGS: +* - v1.0 guests may see duplicate SP IDs when firmware provides UUIDs +* - SP list is cached at init; SPMC tag changes are not tracked +* between calls +* - SP list is capped at FFA_MAX_NUM_SP entries * * There are some large locked sections with ffa_spmc_tx_lock and * ffa_spmc_rx_lock. Especially the ffa_spmc_tx_lock spinlock used
@@ -188,6 +193,7 @@ static bool ffa_negotiate_version(struct cpu_user_regs *regs)write_lock (&ffa_ctx_list_rwlock);list_add_tail (&ctx->ctx_list, &ffa_ctx_head);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); } goto out_continue;
@@ -341,6 +347,12 @@ static void handle_features(struct cpu_user_regs *regs)case FFA_FEATURE_SCHEDULE_RECV_INTR :ffa_set_regs_success (regs,GUEST_FFA_SCHEDULE_RECV_INTR_ID ,0 );break ; +case FFA_PARTITION_INFO_GET_REGS : +if (ACCESS_ONCE (ctx->guest_vers) >=FFA_VERSION_1_2 ) +ffa_set_regs_success (regs,0 ,0 ); +else +ffa_set_regs_error (regs,FFA_RET_NOT_SUPPORTED ); +break ;case FFA_NOTIFICATION_BIND :case FFA_NOTIFICATION_UNBIND :
@@ -402,6 +414,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)case FFA_PARTITION_INFO_GET :ffa_handle_partition_info_get (regs);return true; +case FFA_PARTITION_INFO_GET_REGS : +ffa_handle_partition_info_get_regs (regs); +return true;case FFA_RX_RELEASE : e =ffa_rx_release (ctx);break ;
@@ -629,6 +644,7 @@ static int ffa_domain_teardown(struct domain *d)write_lock (&ffa_ctx_list_rwlock);list_del (&ctx->ctx_list);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); }ffa_rxtx_domain_destroy (d);
xen/arch/arm/tee/ffa_partinfo.c +211 -0
250 lines not on this page · raw message
xen/arch/arm/tee/ffa_private.h +3 -1
@@ -289,7 +289,7 @@#define FFA_MSG_SEND2 0x84000086U#define FFA_CONSOLE_LOG_32 0x8400008AU#define FFA_CONSOLE_LOG_64 0xC400008AU -#define FFA_PARTITION_INFO_GET_REGS 0x8400008BU +#define FFA_PARTITION_INFO_GET_REGS 0xC400008BU#define FFA_MSG_SEND_DIRECT_REQ2 0xC400008DU#define FFA_MSG_SEND_DIRECT_RESP2 0xC400008EU
@@ -452,6 +452,8 @@ bool ffa_partinfo_init(void);int32_t ffa_partinfo_domain_init (struct domain *d);bool ffa_partinfo_domain_destroy (struct domain *d);void ffa_handle_partition_info_get (struct cpu_user_regs *regs); +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs); +void ffa_partinfo_inc_tag (void );int32_t ffa_endpoint_domain_lookup (uint16_t endpoint_id,struct domain **d_out,struct ffa_ctx **ctx_out);
Signature · 1 line
2.52.0
Re: [PATCH 4/4] xen/arm: ffa: Add cached GET_REGS support
Hi Bertrand,
Quoted 89 lines · Bertrand Marquis
FF-A v1.2 defines
PARTITION_INFO_GET_REGSfor register-based partition info retrieval, but Xen currently only supports the buffer-based GET path for guests.Implement
GET_REGSusing the cached SP list and VM entries, including the register window layout and input validation. Track VM list changes via the partinfo tag and use it to validateGET_REGStag inputs. Ensure that when a non-Nil UUID is specified, the UUID fields in both GET andGET_REGSresults are MBZ as required by the specification.
PARTITION_INFO_GET_REGSis available to v1.2 guests, returning cached SP entries and VM entries with UUIDs zeroed for non-Nil UUID queries.Signed-off-by Bertrand Marquis <[email protected]>--- xen/arch/arm/tee/ffa.c | 16 +++ xen/arch/arm/tee/ffa_partinfo.c | 211 ++++++++++++++++++++++++++++++++ xen/arch/arm/tee/ffa_private.h | 4 +- 3 files changed, 230 insertions(+), 1 deletion(-)xen/arch/arm/tee/ffa.c +16
-0@@ -44,6 +44,11 @@* - doesn't support signalling the secondary scheduler of pending * notification for secure partitions * - doesn't support notifications for Xen itself +* o FFA_PARTITION_INFO_GET/GET_REGS: +* - v1.0 guests may see duplicate SP IDs when firmware provides UUIDs +* - SP list is cached at init; SPMC tag changes are not tracked +* between calls +* - SP list is capped at FFA_MAX_NUM_SP entries * * There are some large locked sections with ffa_spmc_tx_lock and * ffa_spmc_rx_lock. Especially the ffa_spmc_tx_lock spinlock used @@ -188,6 +193,7 @@ static bool ffa_negotiate_version(struct cpu_user_regs *regs)write_lock (&ffa_ctx_list_rwlock);list_add_tail (&ctx->ctx_list, &ffa_ctx_head);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); } goto out_continue;@@ -341,6 +347,12 @@ static void handle_features(struct cpu_user_regs *regs)case FFA_FEATURE_SCHEDULE_RECV_INTR :ffa_set_regs_success (regs,GUEST_FFA_SCHEDULE_RECV_INTR_ID ,0 );break ; +case FFA_PARTITION_INFO_GET_REGS : +if (ACCESS_ONCE (ctx->guest_vers) >=FFA_VERSION_1_2 ) +ffa_set_regs_success (regs,0 ,0 ); +else +ffa_set_regs_error (regs,FFA_RET_NOT_SUPPORTED ); +break ;case FFA_NOTIFICATION_BIND :case FFA_NOTIFICATION_UNBIND :@@ -402,6 +414,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)case FFA_PARTITION_INFO_GET :ffa_handle_partition_info_get (regs);return true; +case FFA_PARTITION_INFO_GET_REGS : +ffa_handle_partition_info_get_regs (regs); +return true;case FFA_RX_RELEASE : e =ffa_rx_release (ctx);break ;@@ -629,6 +644,7 @@ static int ffa_domain_teardown(struct domain *d)write_lock (&ffa_ctx_list_rwlock);list_del (&ctx->ctx_list);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); }ffa_rxtx_domain_destroy (d);xen/arch/arm/tee/ffa_partinfo.c +4
-0@@ -28,10 +28,39 @@ struct ffa_partition_info_1_1 {uint8_t uuid[16 ]; }; +/* Registers a3..a17 (15 regs) carry partition descriptors, 3 regs each. */ +#define FFA_PARTINFO_REG_MAX_ENTRIES \ + ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info_1_1)) +/* SP list cache (secure endpoints only); populated at init. */
xen/arch/arm/tee/ffa_partinfo.c struct ffa_partition_info_1_1 { · line 45
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +7
-0static void *sp_list__read_mostly ;static uint32_t sp_list_count__read_mostly ;static uint32_t sp_list_entry_size__read_mostly ; + +/* SP list is static; tag only moves when VMs are added/removed. */ +static atomic_t ffa_partinfo_tag =ATOMIC_INIT (1 ); + +void ffa_partinfo_inc_tag (void ) +{ +atomic_inc (&ffa_partinfo_tag);
Do we need to worry about this value wrapping? Is wrapping permitted?
Quoted 114 lines
xen/arch/arm/tee/ffa_partinfo.c +81
-0+} + +static inline uint16_t ffa_partinfo_get_tag (void ) +{ +/* +* Tag moves with VM list changes only. +* +* Limitation: we cannot detect an SPMC tag change between calls because we +* do not retain the previous SPMC tag; we only refresh it via the mandatory +* start_index=0 call and assume it stays stable while combined_tag (our +* VM/SP-count tag) is used for guest validation. This means SPMC tag +* changes alone will not trigger RETRY. +*/ +if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) ) +return atomic_read (&ffa_partinfo_tag) &GENMASK (15 ,0 ); +else +return 1 ; +}static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {@@ -125,6 +154,7 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,for ( n =0 ; n < sp_list_count; n++ ) {void *entry = sp_list + n * sp_list_entry_size; +void *dst_pos;if ( !ffa_sp_entry_matches_uuid (entry, uuid) )continue ;@@ -136,11 +166,20 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,* This is a non-compliance to the specification but 1.0 VMs should * handle that on their own to simplify Xen implementation. */ + dst_pos = *dst_buf; ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, sp_list_entry_size);if ( ret )return ret; +if ( !ffa_uuid_is_nil (uuid) && + dst_size >=sizeof (struct ffa_partition_info_1_1 ) ) + { +struct ffa_partition_info_1_1 *fpi = dst_pos; + +memset (fpi->uuid,0 ,sizeof (fpi->uuid)); + } + count++; }@@ -152,6 +191,38 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,return FFA_RET_OK ; } +static uint16_t ffa_get_sp_partinfo_regs (struct ffa_uuid uuid, +uint16_t start_index, +uint64_t *out_regs, +uint16_t max_entries) +{ +uint32_t idx =0 ; +uint16_t filled =0 ; +uint32_t n; + +for ( n =0 ; n < sp_list_count && filled < max_entries; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; + +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; + +if ( idx++ < start_index ) +continue ; + +memcpy (&out_regs[filled *3 ], entry, +sizeof (struct ffa_partition_info_1_1 )); +if ( !ffa_uuid_is_nil (uuid) ) + { + out_regs[filled *3 +1 ] =0 ; + out_regs[filled *3 +2 ] =0 ; + } + filled++; + } + +return filled; +} +static int32_t ffa_get_vm_partinfo (struct ffa_uuid uuid,uint32_t start_index,uint32_t *vm_count,void **dst_buf,void *end_buf,uint32_t dst_size)@@ -368,6 +439,146 @@ out: } } +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs) +{ +struct domain *d = current->domain; +struct ffa_ctx *ctx = d->arch.tee; +struct ffa_uuid uuid; +uint32_t sp_count =0 , vm_count =0 , total_count; +uint16_t start_index, tag; +uint16_t num_entries =0 ; +uint64_t x3 =get_user_reg (regs,3 ); +int32_t ret =FFA_RET_OK ; +uint64_t out_regs[18 ] = {0 }; +unsigned int n; +uint16_t tag_out; + +if (ACCESS_ONCE (ctx->guest_vers) <FFA_VERSION_1_2 ) + { + ret =FFA_RET_NOT_SUPPORTED ; + goto out; + } + +/*
xen/arch/arm/tee/ffa_partinfo.c out: · line 472
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +10
-0+* Registers a3..a17 (15 regs) carry partition descriptors, 3 regs each. +* For FF-A 1.2, that yields a maximum of 5 entries per GET_REGS call. +* Enforce the assumed layout so window sizing stays correct. +*/ +BUILD_BUG_ON (FFA_PARTINFO_REG_MAX_ENTRIES !=5 ); + +for ( n =4 ; n <=17 ; n++ ) + { +if (get_user_reg (regs, n) ) + {
x4-x17 are SBZ, so I think we should only ignore them.
xen/arch/arm/tee/ffa_partinfo.c out: · line 478
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+ ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + } + +if ( x3 >>32 )
Same here: Bits[63:32] are SBZ.
Quoted 12 lines
xen/arch/arm/tee/ffa_partinfo.c +12
-0+ { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + + start_index = x3 &GENMASK (15 ,0 ); + tag = (x3 >>16 ) &GENMASK (15 ,0 ); + +/* Start index must allow room for up to 5 entries without 16-bit overflow. */ +if ( start_index > (GENMASK (15 ,0 ) - (FFA_PARTINFO_REG_MAX_ENTRIES -1 )) ) + { + ret =FFA_RET_INVALID_PARAMETERS ;
xen/arch/arm/tee/ffa_partinfo.c out: · line 500
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +10
-0+ goto out; + } + + uuid.val[0 ] =get_user_reg (regs,1 ); + uuid.val[1 ] =get_user_reg (regs,2 ); + +if ( sp_list_count && + sp_list_entry_size !=sizeof (struct ffa_partition_info_1_1 ) ) + { + ret =FFA_RET_NOT_SUPPORTED ;
This can't happen. But I guess a sp_list_entry_size > sizeof(struct ffa_partition_info_1_1) might be supported to be future proof.
Quoted 55 lines
xen/arch/arm/tee/ffa_partinfo.c +55
-0+ goto out; + } + + tag_out =ffa_partinfo_get_tag (); + +if ( start_index ==0 ) + { +if ( tag ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + } +else if ( tag != tag_out ) + { + ret =FFA_RET_RETRY ; + goto out; + } + +if (ffa_uuid_is_nil (uuid) ) + { +if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) ) + vm_count =get_ffa_vm_count (); +else + vm_count =1 ;/* Caller VM only */ + } + + ret =ffa_get_sp_count (uuid, &sp_count); +if ( ret ) + goto out; + + total_count = sp_count + vm_count; + +if ( total_count ==0 || start_index >= total_count ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + +if ( start_index < sp_count ) + num_entries =ffa_get_sp_partinfo_regs (uuid, start_index, &out_regs[3 ], +FFA_PARTINFO_REG_MAX_ENTRIES ); + +if ( num_entries <FFA_PARTINFO_REG_MAX_ENTRIES ) + { +uint32_t vm_start = start_index > sp_count ? + start_index - sp_count :0 ; +uint32_t filled =0 ; +void *vm_dst = &out_regs[3 + num_entries *3 ]; +void *vm_end = &out_regs[18 ]; + + ret =ffa_get_vm_partinfo (uuid, vm_start, &filled, &vm_dst, vm_end, +sizeof (struct ffa_partition_info_1_1 )); +if ( ret !=FFA_RET_OK && ret !=FFA_RET_NO_MEMORY ) + goto out;
xen/arch/arm/tee/ffa_partinfo.c out: · line 565
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +10
-0+ + num_entries += filled; + } + +if ( num_entries ==0 ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } +
What if the tag read with ffa_partinfo_get_tag() has changed?
Cheers,
Jens
Quoted 44 lines
xen/arch/arm/tee/ffa_partinfo.c +16
-0+ out_regs[0 ] =FFA_SUCCESS_64 ; + out_regs[2 ] = ((uint64_t )sizeof (struct ffa_partition_info_1_1 ) <<48 ) | + ((uint64_t )tag_out <<32 ) | + ((uint64_t )(start_index + num_entries -1 ) <<16 ) | + ((uint64_t )(total_count -1 ) &GENMASK (15 ,0 )); + +for ( n =0 ; n <ARRAY_SIZE (out_regs); n++ ) +set_user_reg (regs, n, out_regs[n]); + +return ; + +out: +if ( ret ) +ffa_set_regs_error (regs, ret); +} +static int32_t ffa_direct_req_send_vm (uint16_t sp_id,uint16_t vm_id,uint8_t msg) {xen/arch/arm/tee/ffa_private.h +3
-2@@ -289,7 +289,7 @@#define FFA_MSG_SEND2 0x84000086U#define FFA_CONSOLE_LOG_32 0x8400008AU#define FFA_CONSOLE_LOG_64 0xC400008AU -#define FFA_PARTITION_INFO_GET_REGS 0x8400008BU +#define FFA_PARTITION_INFO_GET_REGS 0xC400008BU#define FFA_MSG_SEND_DIRECT_REQ2 0xC400008DU#define FFA_MSG_SEND_DIRECT_RESP2 0xC400008EU@@ -452,6 +452,8 @@ bool ffa_partinfo_init(void);int32_t ffa_partinfo_domain_init (struct domain *d);bool ffa_partinfo_domain_destroy (struct domain *d);void ffa_handle_partition_info_get (struct cpu_user_regs *regs); +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs); +void ffa_partinfo_inc_tag (void );int32_t ffa_endpoint_domain_lookup (uint16_t endpoint_id,struct domain **d_out,struct ffa_ctx **ctx_out); --2.52.0
Re: [PATCH 4/4] xen/arm: ffa: Add cached GET_REGS support
Hi Jens,
Quoted 99 lines · Jens Wiklander
Hi Bertrand,
Quoted 93 lines · Bertrand Marquis
FF-A v1.2 defines
PARTITION_INFO_GET_REGSfor register-based partition info retrieval, but Xen currently only supports the buffer-based GET path for guests.Implement
GET_REGSusing the cached SP list and VM entries, including the register window layout and input validation. Track VM list changes via the partinfo tag and use it to validateGET_REGStag inputs. Ensure that when a non-Nil UUID is specified, the UUID fields in both GET andGET_REGSresults are MBZ as required by the specification.
PARTITION_INFO_GET_REGSis available to v1.2 guests, returning cached SP entries and VM entries with UUIDs zeroed for non-Nil UUID queries.Signed-off-by Bertrand Marquis <[email protected]>---
xen/arch/arm/tee/ffa.c | 16 +++ xen/arch/arm/tee/ffa_partinfo.c | 211 ++++++++++++++++++++++++++++++++ xen/arch/arm/tee/ffa_private.h | 4 +- 3 files changed, 230 insertions(+), 1 deletion(-)xen/arch/arm/tee/ffa.c +16
-0@@ -44,6 +44,11 @@* - doesn't support signalling the secondary scheduler of pending * notification for secure partitions * - doesn't support notifications for Xen itself +* o FFA_PARTITION_INFO_GET/GET_REGS: +* - v1.0 guests may see duplicate SP IDs when firmware provides UUIDs +* - SP list is cached at init; SPMC tag changes are not tracked +* between calls +* - SP list is capped at FFA_MAX_NUM_SP entries * * There are some large locked sections with ffa_spmc_tx_lock and * ffa_spmc_rx_lock. Especially the ffa_spmc_tx_lock spinlock used @@ -188,6 +193,7 @@ static bool ffa_negotiate_version(struct cpu_user_regs *regs)write_lock (&ffa_ctx_list_rwlock);list_add_tail (&ctx->ctx_list, &ffa_ctx_head);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); } goto out_continue;@@ -341,6 +347,12 @@ static void handle_features(struct cpu_user_regs *regs)case FFA_FEATURE_SCHEDULE_RECV_INTR :ffa_set_regs_success (regs,GUEST_FFA_SCHEDULE_RECV_INTR_ID ,0 );break ; +case FFA_PARTITION_INFO_GET_REGS : +if (ACCESS_ONCE (ctx->guest_vers) >=FFA_VERSION_1_2 ) +ffa_set_regs_success (regs,0 ,0 ); +else +ffa_set_regs_error (regs,FFA_RET_NOT_SUPPORTED ); +break ;case FFA_NOTIFICATION_BIND :case FFA_NOTIFICATION_UNBIND :@@ -402,6 +414,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)case FFA_PARTITION_INFO_GET :ffa_handle_partition_info_get (regs);return true; +case FFA_PARTITION_INFO_GET_REGS : +ffa_handle_partition_info_get_regs (regs); +return true;case FFA_RX_RELEASE : e =ffa_rx_release (ctx);break ;@@ -629,6 +644,7 @@ static int ffa_domain_teardown(struct domain *d)write_lock (&ffa_ctx_list_rwlock);list_del (&ctx->ctx_list);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); }ffa_rxtx_domain_destroy (d);xen/arch/arm/tee/ffa_partinfo.c +5
-0@@ -28,10 +28,39 @@ struct ffa_partition_info_1_1 {uint8_t uuid[16 ]; }; +/* Registers a3..a17 (15 regs) carry partition descriptors, 3 regs each. */ +#define FFA_PARTINFO_REG_MAX_ENTRIES \ + ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info_1_1)) +/* SP list cache (secure endpoints only); populated at init. */ static void *sp_list__read_mostly ;static uint32_t sp_list_count__read_mostly ;static uint32_t sp_list_entry_size__read_mostly ; +
Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c struct ffa_partition_info_1_1 { · line 45
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+/* SP list is static; tag only moves when VMs are added/removed. */ +static atomic_t ffa_partinfo_tag =ATOMIC_INIT (1 ); + +void ffa_partinfo_inc_tag (void ) +{ +atomic_inc (&ffa_partinfo_tag);Do we need to worry about this value wrapping? Is wrapping permitted?
wrapping is permitted, as the end this value is used to ensure changes in the middle of info_get_regs are detected. Having enough changes in the middle for this to wrap and end up un-detected by the caller is near to impossible. In any case, the status we return is a snapshot which have changed as soon as the result is returned so i would consider this a best effort (even if the probability for this to happen is very very near to 0).
Quoted 119 lines
Quoted 119 lines
xen/arch/arm/tee/ffa_partinfo.c +85
-0+} + +static inline uint16_t ffa_partinfo_get_tag (void ) +{ +/* +* Tag moves with VM list changes only. +* +* Limitation: we cannot detect an SPMC tag change between calls because we +* do not retain the previous SPMC tag; we only refresh it via the mandatory +* start_index=0 call and assume it stays stable while combined_tag (our +* VM/SP-count tag) is used for guest validation. This means SPMC tag +* changes alone will not trigger RETRY. +*/ +if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) ) +return atomic_read (&ffa_partinfo_tag) &GENMASK (15 ,0 ); +else +return 1 ; +}static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {@@ -125,6 +154,7 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,for ( n =0 ; n < sp_list_count; n++ ) {void *entry = sp_list + n * sp_list_entry_size; +void *dst_pos;if ( !ffa_sp_entry_matches_uuid (entry, uuid) )continue ;@@ -136,11 +166,20 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,* This is a non-compliance to the specification but 1.0 VMs should * handle that on their own to simplify Xen implementation. */ + dst_pos = *dst_buf; ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, sp_list_entry_size);if ( ret )return ret; +if ( !ffa_uuid_is_nil (uuid) && + dst_size >=sizeof (struct ffa_partition_info_1_1 ) ) + { +struct ffa_partition_info_1_1 *fpi = dst_pos; + +memset (fpi->uuid,0 ,sizeof (fpi->uuid)); + } + count++; }@@ -152,6 +191,38 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,return FFA_RET_OK ; } +static uint16_t ffa_get_sp_partinfo_regs (struct ffa_uuid uuid, +uint16_t start_index, +uint64_t *out_regs, +uint16_t max_entries) +{ +uint32_t idx =0 ; +uint16_t filled =0 ; +uint32_t n; + +for ( n =0 ; n < sp_list_count && filled < max_entries; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; + +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; + +if ( idx++ < start_index ) +continue ; + +memcpy (&out_regs[filled *3 ], entry, +sizeof (struct ffa_partition_info_1_1 )); +if ( !ffa_uuid_is_nil (uuid) ) + { + out_regs[filled *3 +1 ] =0 ; + out_regs[filled *3 +2 ] =0 ; + } + filled++; + } + +return filled; +} +static int32_t ffa_get_vm_partinfo (struct ffa_uuid uuid,uint32_t start_index,uint32_t *vm_count,void **dst_buf,void *end_buf,uint32_t dst_size)@@ -368,6 +439,146 @@ out: } } +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs) +{ +struct domain *d = current->domain; +struct ffa_ctx *ctx = d->arch.tee; +struct ffa_uuid uuid; +uint32_t sp_count =0 , vm_count =0 , total_count; +uint16_t start_index, tag; +uint16_t num_entries =0 ; +uint64_t x3 =get_user_reg (regs,3 ); +int32_t ret =FFA_RET_OK ; +uint64_t out_regs[18 ] = {0 }; +unsigned int n; +uint16_t tag_out; + +if (ACCESS_ONCE (ctx->guest_vers) <FFA_VERSION_1_2 ) + { + ret =FFA_RET_NOT_SUPPORTED ; + goto out; + } + +/* +* Registers a3..a17 (15 regs) carry partition descriptors, 3 regs each. +* For FF-A 1.2, that yields a maximum of 5 entries per GET_REGS call. +* Enforce the assumed layout so window sizing stays correct. +*/
Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 472
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+BUILD_BUG_ON (FFA_PARTINFO_REG_MAX_ENTRIES !=5 ); + +for ( n =4 ; n <=17 ; n++ ) + { +if (get_user_reg (regs, n) ) + {x4-x17 are SBZ, so I think we should only ignore them.
Ack, this was added to satisfy the compliance suite but this has been solved since. I will remove.
Quoted 9 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 478
Quoted 7 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+ ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + } + +if ( x3 >>32 )Same here: Bits[63:32] are SBZ.
Same here.
Quoted 18 lines
Quoted 18 lines
xen/arch/arm/tee/ffa_partinfo.c +17
-0+ { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + + start_index = x3 &GENMASK (15 ,0 ); + tag = (x3 >>16 ) &GENMASK (15 ,0 ); + +/* Start index must allow room for up to 5 entries without 16-bit overflow. */ +if ( start_index > (GENMASK (15 ,0 ) - (FFA_PARTINFO_REG_MAX_ENTRIES -1 )) ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + + uuid.val[0 ] =get_user_reg (regs,1 ); + uuid.val[1 ] =get_user_reg (regs,2 );
Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 500
Quoted 5 lines
xen/arch/arm/tee/ffa_partinfo.c +5
-0+ +if ( sp_list_count && + sp_list_entry_size !=sizeof (struct ffa_partition_info_1_1 ) ) + { + ret =FFA_RET_NOT_SUPPORTED ;This can't happen. But I guess a
sp_list_entry_size>sizeof(struct ffa_partition_info_1_1)might be supported to be future proof.
Right now we have FFA_PARTINFO_REG_MAX_ENTRIES enforcing the structure to be 1.1 size. If this is not true in the future we will have to modify this.
This is not really future proof and i will check if i can rework this.
Quoted 60 lines
Quoted 60 lines
xen/arch/arm/tee/ffa_partinfo.c +59
-0+ goto out; + } + + tag_out =ffa_partinfo_get_tag (); + +if ( start_index ==0 ) + { +if ( tag ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + } +else if ( tag != tag_out ) + { + ret =FFA_RET_RETRY ; + goto out; + } + +if (ffa_uuid_is_nil (uuid) ) + { +if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) ) + vm_count =get_ffa_vm_count (); +else + vm_count =1 ;/* Caller VM only */ + } + + ret =ffa_get_sp_count (uuid, &sp_count); +if ( ret ) + goto out; + + total_count = sp_count + vm_count; + +if ( total_count ==0 || start_index >= total_count ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + +if ( start_index < sp_count ) + num_entries =ffa_get_sp_partinfo_regs (uuid, start_index, &out_regs[3 ], +FFA_PARTINFO_REG_MAX_ENTRIES ); + +if ( num_entries <FFA_PARTINFO_REG_MAX_ENTRIES ) + { +uint32_t vm_start = start_index > sp_count ? + start_index - sp_count :0 ; +uint32_t filled =0 ; +void *vm_dst = &out_regs[3 + num_entries *3 ]; +void *vm_end = &out_regs[18 ]; + + ret =ffa_get_vm_partinfo (uuid, vm_start, &filled, &vm_dst, vm_end, +sizeof (struct ffa_partition_info_1_1 )); +if ( ret !=FFA_RET_OK && ret !=FFA_RET_NO_MEMORY ) + goto out; + + num_entries += filled; + } +
Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 565
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+if ( num_entries ==0 ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } +What if the tag read with
ffa_partinfo_get_tag()has changed?
As said this is a best effort, we provide a snapshot.
Now i could check and compare the tag at the end to handle this case.
I will check if i can make this a bit stronger by comparing the tag at the beginning and the end or try to handle it differently (get its value while we have the rwlock on the list of VMs maybe).
Cheers
Bertrand
Quoted 47 lines
Cheers,
JensQuoted 43 lines
xen/arch/arm/tee/ffa_partinfo.c +16
-0+ out_regs[0 ] =FFA_SUCCESS_64 ; + out_regs[2 ] = ((uint64_t )sizeof (struct ffa_partition_info_1_1 ) <<48 ) | + ((uint64_t )tag_out <<32 ) | + ((uint64_t )(start_index + num_entries -1 ) <<16 ) | + ((uint64_t )(total_count -1 ) &GENMASK (15 ,0 )); + +for ( n =0 ; n <ARRAY_SIZE (out_regs); n++ ) +set_user_reg (regs, n, out_regs[n]); + +return ; + +out: +if ( ret ) +ffa_set_regs_error (regs, ret); +} +static int32_t ffa_direct_req_send_vm (uint16_t sp_id,uint16_t vm_id,uint8_t msg) {xen/arch/arm/tee/ffa_private.h +3
-2@@ -289,7 +289,7 @@#define FFA_MSG_SEND2 0x84000086U#define FFA_CONSOLE_LOG_32 0x8400008AU#define FFA_CONSOLE_LOG_64 0xC400008AU -#define FFA_PARTITION_INFO_GET_REGS 0x8400008BU +#define FFA_PARTITION_INFO_GET_REGS 0xC400008BU#define FFA_MSG_SEND_DIRECT_REQ2 0xC400008DU#define FFA_MSG_SEND_DIRECT_RESP2 0xC400008EU@@ -452,6 +452,8 @@ bool ffa_partinfo_init(void);int32_t ffa_partinfo_domain_init (struct domain *d);bool ffa_partinfo_domain_destroy (struct domain *d);void ffa_handle_partition_info_get (struct cpu_user_regs *regs); +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs); +void ffa_partinfo_inc_tag (void );int32_t ffa_endpoint_domain_lookup (uint16_t endpoint_id,struct domain **d_out,struct ffa_ctx **ctx_out); --2.52.0
Re: [PATCH 4/4] xen/arm: ffa: Add cached GET_REGS support
Hi Bertrand,
Quoted 111 lines · Bertrand Marquis
Hi Jens,
Quoted 99 lines · Jens Wiklander
Hi Bertrand,
Quoted 93 lines · Bertrand Marquis
FF-A v1.2 defines
PARTITION_INFO_GET_REGSfor register-based partition info retrieval, but Xen currently only supports the buffer-based GET path for guests.Implement
GET_REGSusing the cached SP list and VM entries, including the register window layout and input validation. Track VM list changes via the partinfo tag and use it to validateGET_REGStag inputs. Ensure that when a non-Nil UUID is specified, the UUID fields in both GET andGET_REGSresults are MBZ as required by the specification.
PARTITION_INFO_GET_REGSis available to v1.2 guests, returning cached SP entries and VM entries with UUIDs zeroed for non-Nil UUID queries.Signed-off-by Bertrand Marquis <[email protected]>---
xen/arch/arm/tee/ffa.c | 16 +++ xen/arch/arm/tee/ffa_partinfo.c | 211 ++++++++++++++++++++++++++++++++ xen/arch/arm/tee/ffa_private.h | 4 +- 3 files changed, 230 insertions(+), 1 deletion(-)xen/arch/arm/tee/ffa.c +16
-0@@ -44,6 +44,11 @@* - doesn't support signalling the secondary scheduler of pending * notification for secure partitions * - doesn't support notifications for Xen itself +* o FFA_PARTITION_INFO_GET/GET_REGS: +* - v1.0 guests may see duplicate SP IDs when firmware provides UUIDs +* - SP list is cached at init; SPMC tag changes are not tracked +* between calls +* - SP list is capped at FFA_MAX_NUM_SP entries * * There are some large locked sections with ffa_spmc_tx_lock and * ffa_spmc_rx_lock. Especially the ffa_spmc_tx_lock spinlock used @@ -188,6 +193,7 @@ static bool ffa_negotiate_version(struct cpu_user_regs *regs)write_lock (&ffa_ctx_list_rwlock);list_add_tail (&ctx->ctx_list, &ffa_ctx_head);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); } goto out_continue;@@ -341,6 +347,12 @@ static void handle_features(struct cpu_user_regs *regs)case FFA_FEATURE_SCHEDULE_RECV_INTR :ffa_set_regs_success (regs,GUEST_FFA_SCHEDULE_RECV_INTR_ID ,0 );break ; +case FFA_PARTITION_INFO_GET_REGS : +if (ACCESS_ONCE (ctx->guest_vers) >=FFA_VERSION_1_2 ) +ffa_set_regs_success (regs,0 ,0 ); +else +ffa_set_regs_error (regs,FFA_RET_NOT_SUPPORTED ); +break ;case FFA_NOTIFICATION_BIND :case FFA_NOTIFICATION_UNBIND :@@ -402,6 +414,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)case FFA_PARTITION_INFO_GET :ffa_handle_partition_info_get (regs);return true; +case FFA_PARTITION_INFO_GET_REGS : +ffa_handle_partition_info_get_regs (regs); +return true;case FFA_RX_RELEASE : e =ffa_rx_release (ctx);break ;@@ -629,6 +644,7 @@ static int ffa_domain_teardown(struct domain *d)write_lock (&ffa_ctx_list_rwlock);list_del (&ctx->ctx_list);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); }ffa_rxtx_domain_destroy (d);xen/arch/arm/tee/ffa_partinfo.c +5
-0@@ -28,10 +28,39 @@ struct ffa_partition_info_1_1 {uint8_t uuid[16 ]; }; +/* Registers a3..a17 (15 regs) carry partition descriptors, 3 regs each. */ +#define FFA_PARTINFO_REG_MAX_ENTRIES \ + ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info_1_1)) +/* SP list cache (secure endpoints only); populated at init. */ static void *sp_list__read_mostly ;static uint32_t sp_list_count__read_mostly ;static uint32_t sp_list_entry_size__read_mostly ; +Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c struct ffa_partition_info_1_1 { · line 45
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+/* SP list is static; tag only moves when VMs are added/removed. */ +static atomic_t ffa_partinfo_tag =ATOMIC_INIT (1 ); + +void ffa_partinfo_inc_tag (void ) +{ +atomic_inc (&ffa_partinfo_tag);Do we need to worry about this value wrapping? Is wrapping permitted?
Quoted 6 lines
wrapping is permitted, as the end this value is used to ensure changes in the middle of
info_get_regsare detected. Having enough changes in the middle for this to wrap and end up un-detected by the caller is near to impossible. In any case, the status we return is a snapshot which have changed as soon as the result is returned so i would consider this a best effort (even if the probability for this to happen is very very near to 0).
I'm sorry for being unclear. atomic_t is implemented as struct { int counter; }, wrapping the counter element is undefined behaviour. But it will take quite some time before we get there and perhaps the assembly implementation of atomic_inc() is expected to mitigate the undefined behaviour part.
Quoted 246 lines
Quoted 120 lines
Quoted 120 lines
xen/arch/arm/tee/ffa_partinfo.c +85
-0+} + +static inline uint16_t ffa_partinfo_get_tag (void ) +{ +/* +* Tag moves with VM list changes only. +* +* Limitation: we cannot detect an SPMC tag change between calls because we +* do not retain the previous SPMC tag; we only refresh it via the mandatory +* start_index=0 call and assume it stays stable while combined_tag (our +* VM/SP-count tag) is used for guest validation. This means SPMC tag +* changes alone will not trigger RETRY. +*/ +if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) ) +return atomic_read (&ffa_partinfo_tag) &GENMASK (15 ,0 ); +else +return 1 ; +}static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {@@ -125,6 +154,7 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,for ( n =0 ; n < sp_list_count; n++ ) {void *entry = sp_list + n * sp_list_entry_size; +void *dst_pos;if ( !ffa_sp_entry_matches_uuid (entry, uuid) )continue ;@@ -136,11 +166,20 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,* This is a non-compliance to the specification but 1.0 VMs should * handle that on their own to simplify Xen implementation. */ + dst_pos = *dst_buf; ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, sp_list_entry_size);if ( ret )return ret; +if ( !ffa_uuid_is_nil (uuid) && + dst_size >=sizeof (struct ffa_partition_info_1_1 ) ) + { +struct ffa_partition_info_1_1 *fpi = dst_pos; + +memset (fpi->uuid,0 ,sizeof (fpi->uuid)); + } + count++; }@@ -152,6 +191,38 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,return FFA_RET_OK ; } +static uint16_t ffa_get_sp_partinfo_regs (struct ffa_uuid uuid, +uint16_t start_index, +uint64_t *out_regs, +uint16_t max_entries) +{ +uint32_t idx =0 ; +uint16_t filled =0 ; +uint32_t n; + +for ( n =0 ; n < sp_list_count && filled < max_entries; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; + +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; + +if ( idx++ < start_index ) +continue ; + +memcpy (&out_regs[filled *3 ], entry, +sizeof (struct ffa_partition_info_1_1 )); +if ( !ffa_uuid_is_nil (uuid) ) + { + out_regs[filled *3 +1 ] =0 ; + out_regs[filled *3 +2 ] =0 ; + } + filled++; + } + +return filled; +} +static int32_t ffa_get_vm_partinfo (struct ffa_uuid uuid,uint32_t start_index,uint32_t *vm_count,void **dst_buf,void *end_buf,uint32_t dst_size)@@ -368,6 +439,146 @@ out: } } +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs) +{ +struct domain *d = current->domain; +struct ffa_ctx *ctx = d->arch.tee; +struct ffa_uuid uuid; +uint32_t sp_count =0 , vm_count =0 , total_count; +uint16_t start_index, tag; +uint16_t num_entries =0 ; +uint64_t x3 =get_user_reg (regs,3 ); +int32_t ret =FFA_RET_OK ; +uint64_t out_regs[18 ] = {0 }; +unsigned int n; +uint16_t tag_out; + +if (ACCESS_ONCE (ctx->guest_vers) <FFA_VERSION_1_2 ) + { + ret =FFA_RET_NOT_SUPPORTED ; + goto out; + } + +/* +* Registers a3..a17 (15 regs) carry partition descriptors, 3 regs each. +* For FF-A 1.2, that yields a maximum of 5 entries per GET_REGS call. +* Enforce the assumed layout so window sizing stays correct. +*/ Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 472
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+BUILD_BUG_ON (FFA_PARTINFO_REG_MAX_ENTRIES !=5 ); + +for ( n =4 ; n <=17 ; n++ ) + { +if (get_user_reg (regs, n) ) + {x4-x17 are SBZ, so I think we should only ignore them.
Ack, this was added to satisfy the compliance suite but this has been solved since. I will remove.
Quoted 9 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 478
Quoted 7 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+ ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + } + +if ( x3 >>32 )Same here: Bits[63:32] are SBZ.
Same here.
Quoted 18 lines
Quoted 18 lines
xen/arch/arm/tee/ffa_partinfo.c +17
-0+ { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + + start_index = x3 &GENMASK (15 ,0 ); + tag = (x3 >>16 ) &GENMASK (15 ,0 ); + +/* Start index must allow room for up to 5 entries without 16-bit overflow. */ +if ( start_index > (GENMASK (15 ,0 ) - (FFA_PARTINFO_REG_MAX_ENTRIES -1 )) ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + + uuid.val[0 ] =get_user_reg (regs,1 ); + uuid.val[1 ] =get_user_reg (regs,2 );Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 500
Quoted 5 lines
xen/arch/arm/tee/ffa_partinfo.c +5
-0+ +if ( sp_list_count && + sp_list_entry_size !=sizeof (struct ffa_partition_info_1_1 ) ) + { + ret =FFA_RET_NOT_SUPPORTED ;This can't happen. But I guess a
sp_list_entry_size>sizeof(struct ffa_partition_info_1_1)might be supported to be future proof.Right now we have
FFA_PARTINFO_REG_MAX_ENTRIESenforcing the structure to be 1.1 size. If this is not true in the future we will have to modify this.This is not really future proof and i will check if i can rework this.
Quoted 60 lines
Quoted 60 lines
xen/arch/arm/tee/ffa_partinfo.c +59
-0+ goto out; + } + + tag_out =ffa_partinfo_get_tag (); + +if ( start_index ==0 ) + { +if ( tag ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + } +else if ( tag != tag_out ) + { + ret =FFA_RET_RETRY ; + goto out; + } + +if (ffa_uuid_is_nil (uuid) ) + { +if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) ) + vm_count =get_ffa_vm_count (); +else + vm_count =1 ;/* Caller VM only */ + } + + ret =ffa_get_sp_count (uuid, &sp_count); +if ( ret ) + goto out; + + total_count = sp_count + vm_count; + +if ( total_count ==0 || start_index >= total_count ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + +if ( start_index < sp_count ) + num_entries =ffa_get_sp_partinfo_regs (uuid, start_index, &out_regs[3 ], +FFA_PARTINFO_REG_MAX_ENTRIES ); + +if ( num_entries <FFA_PARTINFO_REG_MAX_ENTRIES ) + { +uint32_t vm_start = start_index > sp_count ? + start_index - sp_count :0 ; +uint32_t filled =0 ; +void *vm_dst = &out_regs[3 + num_entries *3 ]; +void *vm_end = &out_regs[18 ]; + + ret =ffa_get_vm_partinfo (uuid, vm_start, &filled, &vm_dst, vm_end, +sizeof (struct ffa_partition_info_1_1 )); +if ( ret !=FFA_RET_OK && ret !=FFA_RET_NO_MEMORY ) + goto out; + + num_entries += filled; + } +Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 565
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+if ( num_entries ==0 ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } +What if the tag read with
ffa_partinfo_get_tag()has changed?
Quoted 6 lines
As said this is a best effort, we provide a snapshot.
Now i could check and compare the tag at the end to handle this case.I will check if i can make this a bit stronger by comparing the tag at the beginning and the end or try to handle it differently (get its value while we have the rwlock on the list of VMs maybe).
Checking the tag at the end to let the caller retry might be enough.
Cheers,
Jens
Quoted 53 lines
Cheers
BertrandQuoted 47 lines
Cheers,
JensQuoted 43 lines
xen/arch/arm/tee/ffa_partinfo.c +16
-0+ out_regs[0 ] =FFA_SUCCESS_64 ; + out_regs[2 ] = ((uint64_t )sizeof (struct ffa_partition_info_1_1 ) <<48 ) | + ((uint64_t )tag_out <<32 ) | + ((uint64_t )(start_index + num_entries -1 ) <<16 ) | + ((uint64_t )(total_count -1 ) &GENMASK (15 ,0 )); + +for ( n =0 ; n <ARRAY_SIZE (out_regs); n++ ) +set_user_reg (regs, n, out_regs[n]); + +return ; + +out: +if ( ret ) +ffa_set_regs_error (regs, ret); +} +static int32_t ffa_direct_req_send_vm (uint16_t sp_id,uint16_t vm_id,uint8_t msg) {xen/arch/arm/tee/ffa_private.h +3
-2@@ -289,7 +289,7 @@#define FFA_MSG_SEND2 0x84000086U#define FFA_CONSOLE_LOG_32 0x8400008AU#define FFA_CONSOLE_LOG_64 0xC400008AU -#define FFA_PARTITION_INFO_GET_REGS 0x8400008BU +#define FFA_PARTITION_INFO_GET_REGS 0xC400008BU#define FFA_MSG_SEND_DIRECT_REQ2 0xC400008DU#define FFA_MSG_SEND_DIRECT_RESP2 0xC400008EU@@ -452,6 +452,8 @@ bool ffa_partinfo_init(void);int32_t ffa_partinfo_domain_init (struct domain *d);bool ffa_partinfo_domain_destroy (struct domain *d);void ffa_handle_partition_info_get (struct cpu_user_regs *regs); +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs); +void ffa_partinfo_inc_tag (void );int32_t ffa_endpoint_domain_lookup (uint16_t endpoint_id,struct domain **d_out,struct ffa_ctx **ctx_out); --2.52.0
Re: [PATCH 4/4] xen/arm: ffa: Add cached GET_REGS support
Hi Jens,
Quoted 124 lines · Jens Wiklander
Hi Bertrand,
Quoted 111 lines · Bertrand Marquis
Hi Jens,
Quoted 99 lines · Jens Wiklander
Hi Bertrand,
Quoted 93 lines · Bertrand Marquis
FF-A v1.2 defines
PARTITION_INFO_GET_REGSfor register-based partition info retrieval, but Xen currently only supports the buffer-based GET path for guests.Implement
GET_REGSusing the cached SP list and VM entries, including the register window layout and input validation. Track VM list changes via the partinfo tag and use it to validateGET_REGStag inputs. Ensure that when a non-Nil UUID is specified, the UUID fields in both GET andGET_REGSresults are MBZ as required by the specification.
PARTITION_INFO_GET_REGSis available to v1.2 guests, returning cached SP entries and VM entries with UUIDs zeroed for non-Nil UUID queries.Signed-off-by Bertrand Marquis <[email protected]>---
xen/arch/arm/tee/ffa.c | 16 +++ xen/arch/arm/tee/ffa_partinfo.c | 211 ++++++++++++++++++++++++++++++++ xen/arch/arm/tee/ffa_private.h | 4 +- 3 files changed, 230 insertions(+), 1 deletion(-)xen/arch/arm/tee/ffa.c +16
-0@@ -44,6 +44,11 @@* - doesn't support signalling the secondary scheduler of pending * notification for secure partitions * - doesn't support notifications for Xen itself +* o FFA_PARTITION_INFO_GET/GET_REGS: +* - v1.0 guests may see duplicate SP IDs when firmware provides UUIDs +* - SP list is cached at init; SPMC tag changes are not tracked +* between calls +* - SP list is capped at FFA_MAX_NUM_SP entries * * There are some large locked sections with ffa_spmc_tx_lock and * ffa_spmc_rx_lock. Especially the ffa_spmc_tx_lock spinlock used @@ -188,6 +193,7 @@ static bool ffa_negotiate_version(struct cpu_user_regs *regs)write_lock (&ffa_ctx_list_rwlock);list_add_tail (&ctx->ctx_list, &ffa_ctx_head);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); } goto out_continue;@@ -341,6 +347,12 @@ static void handle_features(struct cpu_user_regs *regs)case FFA_FEATURE_SCHEDULE_RECV_INTR :ffa_set_regs_success (regs,GUEST_FFA_SCHEDULE_RECV_INTR_ID ,0 );break ; +case FFA_PARTITION_INFO_GET_REGS : +if (ACCESS_ONCE (ctx->guest_vers) >=FFA_VERSION_1_2 ) +ffa_set_regs_success (regs,0 ,0 ); +else +ffa_set_regs_error (regs,FFA_RET_NOT_SUPPORTED ); +break ;case FFA_NOTIFICATION_BIND :case FFA_NOTIFICATION_UNBIND :@@ -402,6 +414,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)case FFA_PARTITION_INFO_GET :ffa_handle_partition_info_get (regs);return true; +case FFA_PARTITION_INFO_GET_REGS : +ffa_handle_partition_info_get_regs (regs); +return true;case FFA_RX_RELEASE : e =ffa_rx_release (ctx);break ;@@ -629,6 +644,7 @@ static int ffa_domain_teardown(struct domain *d)write_lock (&ffa_ctx_list_rwlock);list_del (&ctx->ctx_list);write_unlock (&ffa_ctx_list_rwlock); +ffa_partinfo_inc_tag (); }ffa_rxtx_domain_destroy (d);xen/arch/arm/tee/ffa_partinfo.c +5
-0@@ -28,10 +28,39 @@ struct ffa_partition_info_1_1 {uint8_t uuid[16 ]; }; +/* Registers a3..a17 (15 regs) carry partition descriptors, 3 regs each. */ +#define FFA_PARTINFO_REG_MAX_ENTRIES \ + ((15 * sizeof(uint64_t)) / sizeof(struct ffa_partition_info_1_1)) +/* SP list cache (secure endpoints only); populated at init. */ static void *sp_list__read_mostly ;static uint32_t sp_list_count__read_mostly ;static uint32_t sp_list_entry_size__read_mostly ; +Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c struct ffa_partition_info_1_1 { · line 45
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+/* SP list is static; tag only moves when VMs are added/removed. */ +static atomic_t ffa_partinfo_tag =ATOMIC_INIT (1 ); + +void ffa_partinfo_inc_tag (void ) +{ +atomic_inc (&ffa_partinfo_tag);Do we need to worry about this value wrapping? Is wrapping permitted?
Quoted 6 lines
wrapping is permitted, as the end this value is used to ensure changes in the middle of
info_get_regsare detected. Having enough changes in the middle for this to wrap and end up un-detected by the caller is near to impossible. In any case, the status we return is a snapshot which have changed as soon as the result is returned so i would consider this a best effort (even if the probability for this to happen is very very near to 0).
Quoted 5 lines
I'm sorry for being unclear.
atomic_tis implemented as struct { int counter; }, wrapping the counter element is undefined behaviour. But it will take quite some time before we get there and perhaps the assembly implementation ofatomic_inc()is expected to mitigate the undefined behaviour part.
On arm the atomic operations are implemented using and assembler add which is not typed.
At the end we mask the value to use only 15 bits so we will wrap around more often than the counter itself.
So i do not think we have an undefined behavior here.
Quoted 247 lines
Quoted 247 lines
Quoted 121 lines
Quoted 121 lines
xen/arch/arm/tee/ffa_partinfo.c +85
-0+} + +static inline uint16_t ffa_partinfo_get_tag (void ) +{ +/* +* Tag moves with VM list changes only. +* +* Limitation: we cannot detect an SPMC tag change between calls because we +* do not retain the previous SPMC tag; we only refresh it via the mandatory +* start_index=0 call and assume it stays stable while combined_tag (our +* VM/SP-count tag) is used for guest validation. This means SPMC tag +* changes alone will not trigger RETRY. +*/ +if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) ) +return atomic_read (&ffa_partinfo_tag) &GENMASK (15 ,0 ); +else +return 1 ; +}static int32_t ffa_partition_info_get (struct ffa_uuid uuid,uint32_t flags,uint32_t *count,uint32_t *fpi_size) {@@ -125,6 +154,7 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,for ( n =0 ; n < sp_list_count; n++ ) {void *entry = sp_list + n * sp_list_entry_size; +void *dst_pos;if ( !ffa_sp_entry_matches_uuid (entry, uuid) )continue ;@@ -136,11 +166,20 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,* This is a non-compliance to the specification but 1.0 VMs should * handle that on their own to simplify Xen implementation. */ + dst_pos = *dst_buf; ret =ffa_copy_info (dst_buf, end_buf, entry, dst_size, sp_list_entry_size);if ( ret )return ret; +if ( !ffa_uuid_is_nil (uuid) && + dst_size >=sizeof (struct ffa_partition_info_1_1 ) ) + { +struct ffa_partition_info_1_1 *fpi = dst_pos; + +memset (fpi->uuid,0 ,sizeof (fpi->uuid)); + } + count++; }@@ -152,6 +191,38 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,return FFA_RET_OK ; } +static uint16_t ffa_get_sp_partinfo_regs (struct ffa_uuid uuid, +uint16_t start_index, +uint64_t *out_regs, +uint16_t max_entries) +{ +uint32_t idx =0 ; +uint16_t filled =0 ; +uint32_t n; + +for ( n =0 ; n < sp_list_count && filled < max_entries; n++ ) + { +void *entry = sp_list + n * sp_list_entry_size; + +if ( !ffa_sp_entry_matches_uuid (entry, uuid) ) +continue ; + +if ( idx++ < start_index ) +continue ; + +memcpy (&out_regs[filled *3 ], entry, +sizeof (struct ffa_partition_info_1_1 )); +if ( !ffa_uuid_is_nil (uuid) ) + { + out_regs[filled *3 +1 ] =0 ; + out_regs[filled *3 +2 ] =0 ; + } + filled++; + } + +return filled; +} +static int32_t ffa_get_vm_partinfo (struct ffa_uuid uuid,uint32_t start_index,uint32_t *vm_count,void **dst_buf,void *end_buf,uint32_t dst_size)@@ -368,6 +439,146 @@ out: } } +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs) +{ +struct domain *d = current->domain; +struct ffa_ctx *ctx = d->arch.tee; +struct ffa_uuid uuid; +uint32_t sp_count =0 , vm_count =0 , total_count; +uint16_t start_index, tag; +uint16_t num_entries =0 ; +uint64_t x3 =get_user_reg (regs,3 ); +int32_t ret =FFA_RET_OK ; +uint64_t out_regs[18 ] = {0 }; +unsigned int n; +uint16_t tag_out; + +if (ACCESS_ONCE (ctx->guest_vers) <FFA_VERSION_1_2 ) + { + ret =FFA_RET_NOT_SUPPORTED ; + goto out; + } + +/* +* Registers a3..a17 (15 regs) carry partition descriptors, 3 regs each. +* For FF-A 1.2, that yields a maximum of 5 entries per GET_REGS call. +* Enforce the assumed layout so window sizing stays correct. +*/ Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 472
Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+BUILD_BUG_ON (FFA_PARTINFO_REG_MAX_ENTRIES !=5 ); + +for ( n =4 ; n <=17 ; n++ ) + { +if (get_user_reg (regs, n) ) + {x4-x17 are SBZ, so I think we should only ignore them.
Ack, this was added to satisfy the compliance suite but this has been solved since. I will remove.
Quoted 9 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 478
Quoted 7 lines
xen/arch/arm/tee/ffa_partinfo.c +6
-0+ ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + } + +if ( x3 >>32 )Same here: Bits[63:32] are SBZ.
Same here.
Quoted 18 lines
Quoted 18 lines
xen/arch/arm/tee/ffa_partinfo.c +17
-0+ { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + + start_index = x3 &GENMASK (15 ,0 ); + tag = (x3 >>16 ) &GENMASK (15 ,0 ); + +/* Start index must allow room for up to 5 entries without 16-bit overflow. */ +if ( start_index > (GENMASK (15 ,0 ) - (FFA_PARTINFO_REG_MAX_ENTRIES -1 )) ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + + uuid.val[0 ] =get_user_reg (regs,1 ); + uuid.val[1 ] =get_user_reg (regs,2 );Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c out: · line 500
Quoted 5 lines
xen/arch/arm/tee/ffa_partinfo.c +5
-0+ +if ( sp_list_count && + sp_list_entry_size !=sizeof (struct ffa_partition_info_1_1 ) ) + { + ret =FFA_RET_NOT_SUPPORTED ;This can't happen. But I guess a
sp_list_entry_size>sizeof(struct ffa_partition_info_1_1)might be supported to be future proof.Right now we have
FFA_PARTINFO_REG_MAX_ENTRIESenforcing the structure to be 1.1 size. If this is not true in the future we will have to modify this.This is not really future proof and i will check if i can rework this.
Quoted 68 lines
Quoted 56 lines
xen/arch/arm/tee/ffa_partinfo.c +55
-0+ goto out; + } + + tag_out =ffa_partinfo_get_tag (); + +if ( start_index ==0 ) + { +if ( tag ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + } +else if ( tag != tag_out ) + { + ret =FFA_RET_RETRY ; + goto out; + } + +if (ffa_uuid_is_nil (uuid) ) + { +if (IS_ENABLED (CONFIG_FFA_VM_TO_VM ) ) + vm_count =get_ffa_vm_count (); +else + vm_count =1 ;/* Caller VM only */ + } + + ret =ffa_get_sp_count (uuid, &sp_count); +if ( ret ) + goto out; + + total_count = sp_count + vm_count; + +if ( total_count ==0 || start_index >= total_count ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } + +if ( start_index < sp_count ) + num_entries =ffa_get_sp_partinfo_regs (uuid, start_index, &out_regs[3 ], +FFA_PARTINFO_REG_MAX_ENTRIES ); + +if ( num_entries <FFA_PARTINFO_REG_MAX_ENTRIES ) + { +uint32_t vm_start = start_index > sp_count ? + start_index - sp_count :0 ; +uint32_t filled =0 ; +void *vm_dst = &out_regs[3 + num_entries *3 ]; +void *vm_end = &out_regs[18 ]; + + ret =ffa_get_vm_partinfo (uuid, vm_start, &filled, &vm_dst, vm_end, +sizeof (struct ffa_partition_info_1_1 )); +if ( ret !=FFA_RET_OK && ret !=FFA_RET_NO_MEMORY ) + goto out;xen/arch/arm/tee/ffa_partinfo.c out: · line 565
Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +10
-0+ + num_entries += filled; + } + +if ( num_entries ==0 ) + { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out; + } +What if the tag read with
ffa_partinfo_get_tag()has changed?
Quoted 8 lines
Quoted 6 lines
As said this is a best effort, we provide a snapshot.
Now i could check and compare the tag at the end to handle this case.I will check if i can make this a bit stronger by comparing the tag at the beginning and the end or try to handle it differently (get its value while we have the rwlock on the list of VMs maybe).
Checking the tag at the end to let the caller retry might be enough.
i will give that a try in v2.
Thanks a lot for the review.
I will try to push v2 before the end of the week if not sooner.
Cheers
Bertrand
Quoted 55 lines
Cheers,
JensQuoted 51 lines
Cheers
BertrandQuoted 47 lines
Cheers,
JensQuoted 43 lines
xen/arch/arm/tee/ffa_partinfo.c +16
-0+ out_regs[0 ] =FFA_SUCCESS_64 ; + out_regs[2 ] = ((uint64_t )sizeof (struct ffa_partition_info_1_1 ) <<48 ) | + ((uint64_t )tag_out <<32 ) | + ((uint64_t )(start_index + num_entries -1 ) <<16 ) | + ((uint64_t )(total_count -1 ) &GENMASK (15 ,0 )); + +for ( n =0 ; n <ARRAY_SIZE (out_regs); n++ ) +set_user_reg (regs, n, out_regs[n]); + +return ; + +out: +if ( ret ) +ffa_set_regs_error (regs, ret); +} +static int32_t ffa_direct_req_send_vm (uint16_t sp_id,uint16_t vm_id,uint8_t msg) {xen/arch/arm/tee/ffa_private.h +3
-2@@ -289,7 +289,7 @@#define FFA_MSG_SEND2 0x84000086U#define FFA_CONSOLE_LOG_32 0x8400008AU#define FFA_CONSOLE_LOG_64 0xC400008AU -#define FFA_PARTITION_INFO_GET_REGS 0x8400008BU +#define FFA_PARTITION_INFO_GET_REGS 0xC400008BU#define FFA_MSG_SEND_DIRECT_REQ2 0xC400008DU#define FFA_MSG_SEND_DIRECT_RESP2 0xC400008EU@@ -452,6 +452,8 @@ bool ffa_partinfo_init(void);int32_t ffa_partinfo_domain_init (struct domain *d);bool ffa_partinfo_domain_destroy (struct domain *d);void ffa_handle_partition_info_get (struct cpu_user_regs *regs); +void ffa_handle_partition_info_get_regs (struct cpu_user_regs *regs); +void ffa_partinfo_inc_tag (void );int32_t ffa_endpoint_domain_lookup (uint16_t endpoint_id,struct domain **d_out,struct ffa_ctx **ctx_out); --2.52.0