Skip to contentSkip to conversation

xen/arm: ffa: v1.2 improvements

PATCH Superseded Bertrand Marquis 2 participants 41 messages Feb 3 – Feb 11

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#16 lore raw

PATCH 5/12 xen/arm: ffa: Fix PARTINFO RX release errors

In reply to Bertrand Marquis

On PARTITION_INFO_GET error paths, Xen unconditionally called FFA_RX_RELEASE for the SPMC RX buffer. If the SPMC didn't grant RX ownership (i.e., the call failed early), this issues a spurious release that returns DENIED and produces warnings.

Modify ffa_rxtx_spmc_rx_release() to return the release status and let callers choose whether to log it. Only issue FFA_RX_RELEASE after a successful PARTINFO SMC, while always releasing the local RX lock to avoid deadlocks.

Update handle_partition_info_get() to only release the SPMC RX buffer after successful fw_ret checks, and ignore release errors during the error path.

Functional impact: eliminates spurious FFA_RX_RELEASE calls and associated DENIED warnings when PARTITION_INFO_GET fails before obtaining SPMC RX buffer ownership.

---
 xen/arch/arm/tee/ffa_partinfo.c | 14 ++++++++++++--
 xen/arch/arm/tee/ffa_private.h  |  2 +-
 xen/arch/arm/tee/ffa_rxtx.c     | 14 +++++++++-----
 3 files changed, 22 insertions(+), 8 deletions(-)
xen/arch/arm/tee/ffa_partinfo.c +12 -2
index bf906ed0c88f..6b01c4abe915 100644
@@ -92,9 +92,11 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
                                    uint32_t dst_size)
 {
     int32_t ret;
+    int32_t release_ret;
     uint32_t src_size, real_sp_count;
     void *src_buf;
     uint32_t count = 0;
+    bool spmc_ok = false;
 
     /* We need to use the RX buffer to receive the list */
     src_buf = ffa_rxtx_spmc_rx_acquire();
@@ -104,6 +106,7 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
     ret = ffa_partition_info_get(uuid, 0, &real_sp_count, &src_size);
     if ( ret )
         goto out;
+    spmc_ok = true;
 
     /* Validate the src_size we got */
     if ( src_size < sizeof(struct ffa_partition_info_1_0) ||
@@ -157,7 +160,10 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
     *sp_count = count;
 
 out:
-    ffa_rxtx_spmc_rx_release();
+    release_ret = ffa_rxtx_spmc_rx_release(spmc_ok);
+    if ( release_ret )
+        gprintk(XENLOG_WARNING,
+                "ffa: Error releasing SPMC RX buffer: %d\n", release_ret);
     return ret;
 }
 
@@ -507,6 +513,7 @@ bool ffa_partinfo_init(void)
     int32_t e;
     void *spmc_rx;
     struct ffa_uuid nil_uuid = { .val = { 0ULL, 0ULL } };
+    bool spmc_ok = false;
 
     if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
          !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32))
@@ -522,6 +529,7 @@ bool ffa_partinfo_init(void)
         printk(XENLOG_ERR "ffa: Failed to get list of SPs: %d\n", e);
         goto out;
     }
+    spmc_ok = true;
 
     if ( count >= FFA_MAX_NUM_SP )
     {
@@ -533,7 +541,9 @@ bool ffa_partinfo_init(void)
     ret = init_subscribers(spmc_rx, count, fpi_size);
 
 out:
-    ffa_rxtx_spmc_rx_release();
+    e = ffa_rxtx_spmc_rx_release(spmc_ok);
+    if ( e )
+        printk(XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n", e);
     return ret;
 }
 
xen/arch/arm/tee/ffa_private.h +1 -1
index 58562d8e733c..461e87f6f9c4 100644
@@ -458,7 +458,7 @@ int32_t ffa_endpoint_domain_lookup(uint16_t endpoint_id, struct domain **d_out,
 bool ffa_rxtx_spmc_init(void);
 void ffa_rxtx_spmc_destroy(void);
 void *ffa_rxtx_spmc_rx_acquire(void);
-void ffa_rxtx_spmc_rx_release(void);
+int32_t ffa_rxtx_spmc_rx_release(bool notify_fw);
 void *ffa_rxtx_spmc_tx_acquire(void);
 void ffa_rxtx_spmc_tx_release(void);
 
xen/arch/arm/tee/ffa_rxtx.c +9 -5
index 7d8bb4f4d031..50758fb57cdf 100644
@@ -375,18 +375,22 @@ void *ffa_rxtx_spmc_rx_acquire(void)
     return NULL;
 }
 
-void ffa_rxtx_spmc_rx_release(void)
+int32_t ffa_rxtx_spmc_rx_release(bool notify_fw)
 {
     int32_t ret;
 
     ASSERT(spin_is_locked(&ffa_spmc_rx_lock));
 
-    /* Inform the SPMC that we are done with our RX buffer */
-    ret = ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
-    if ( ret != FFA_RET_OK )
-        printk(XENLOG_DEBUG "Error releasing SPMC RX buffer: %d\n", ret);
+    if ( notify_fw )
+    {
+        /* Inform the SPMC that we are done with our RX buffer */
+        ret = ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
+    }
+    else
+        ret = FFA_RET_OK;
 
     spin_unlock(&ffa_spmc_rx_lock);
+    return ret;
 }
 
 void *ffa_rxtx_spmc_tx_acquire(void)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/12 xen/arm: ffa: Add endpoint lookup helper

In reply to Bertrand Marquis

Direct messaging paths duplicate endpoint validation and RCU domain lookup logic across multiple call sites, which makes the checks easy to drift and complicates maintenance.

Introduce ffa_endpoint_domain_lookup() to centralize this logic. The helper validates the endpoint ID (rejecting ID 0 for the hypervisor), performs RCU domain lookup, ensures the domain is live and has an initialized FF-A context with a negotiated version, and returns the domain locked via RCU.

Switch ffa_msg_send2_vm() to use the helper, replacing its open-coded validation sequence. This consolidates approximately 20 lines of duplicated checks into a single call.

No functional changes.

---
 xen/arch/arm/tee/ffa.c         | 45 ++++++++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_msg.c     | 24 +++---------------
 xen/arch/arm/tee/ffa_private.h |  3 +++
 3 files changed, 51 insertions(+), 21 deletions(-)
xen/arch/arm/tee/ffa.c +45 -0
index ed18e76080d0..6de2b9f8ac8e 100644
@@ -433,6 +433,51 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     return true;
 }
 
+/*
+ * Look up a domain by its FF-A endpoint ID and validate it's ready for FF-A.
+ * Returns FFA_RET_OK on success with domain locked via RCU.
+ * Caller must call rcu_unlock_domain() when done.
+ *
+ * Validates:
+ * - endpoint_id is not 0 (the hypervisor)
+ * - domain exists and is live
+ * - domain has FF-A context initialized
+ * - domain has negotiated an FF-A version
+ */
+int32_t ffa_endpoint_domain_lookup(uint16_t endpoint_id, struct domain **d_out,
+                                   struct ffa_ctx **ctx_out)
+{
+    struct domain *d;
+    struct ffa_ctx *ctx;
+    int err;
+
+    if ( endpoint_id == 0 )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    err = rcu_lock_live_remote_domain_by_id(endpoint_id - 1, &d);
+    if ( err )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    if ( !d->arch.tee )
+    {
+        rcu_unlock_domain(d);
+        return FFA_RET_INVALID_PARAMETERS;
+    }
+
+    ctx = d->arch.tee;
+    if ( !ACCESS_ONCE(ctx->guest_vers) )
+    {
+        rcu_unlock_domain(d);
+        return FFA_RET_INVALID_PARAMETERS;
+    }
+
+    *d_out = d;
+    if ( ctx_out )
+        *ctx_out = ctx;
+
+    return FFA_RET_OK;
+}
+
 static int ffa_domain_init(struct domain *d)
 {
     struct ffa_ctx *ctx;
xen/arch/arm/tee/ffa_msg.c +3 -21
index 4e26596461a9..10856fddcbc4 100644
@@ -161,30 +161,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     struct ffa_part_msg_rxtx_1_2 *dst_msg;
     void *rx_buf;
     size_t rx_size;
-    int err;
     int32_t ret;
 
-    if ( dst_id == 0 )
-        /* FF-A ID 0 is the hypervisor, this is not valid */
-        return FFA_RET_INVALID_PARAMETERS;
-
     /* This is also checking that dest is not src */
-    err = rcu_lock_live_remote_domain_by_id(dst_id - 1, &dst_d);
-    if ( err )
-        return FFA_RET_INVALID_PARAMETERS;
-
-    if ( dst_d->arch.tee == NULL )
-    {
-        ret = FFA_RET_INVALID_PARAMETERS;
-        goto out_unlock;
-    }
-
-    dst_ctx = dst_d->arch.tee;
-    if ( !ACCESS_ONCE(dst_ctx->guest_vers) )
-    {
-        ret = FFA_RET_INVALID_PARAMETERS;
-        goto out_unlock;
-    }
+    ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
+    if ( ret )
+        return ret;
 
     /* This also checks that destination has set a Rx buffer */
     ret = ffa_rx_acquire(dst_ctx , &rx_buf, &rx_size);
xen/arch/arm/tee/ffa_private.h +3 -0
index 282c105f3bce..cd7ecabc7eff 100644
@@ -437,6 +437,9 @@ 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);
 
+int32_t ffa_endpoint_domain_lookup(uint16_t endpoint_id, struct domain **d_out,
+                                   struct ffa_ctx **ctx_out);
+
 bool ffa_rxtx_spmc_init(void);
 void ffa_rxtx_spmc_destroy(void);
 void *ffa_rxtx_spmc_rx_acquire(void);
Signature · 1 line
2.50.1 (Apple Git-155)
#3 lore raw

PATCH 2/12 xen/arm: ffa: Fix MEM_SHARE NS attribute handling

In reply to Bertrand Marquis

The FF-A memory attribute encoding is currently a literal value (0x2f), which makes reviews and validation harder. In addition, MEM_SHARE accepts the NS (non-secure) attribute bit even though the normal world must not set it according to FF-A specification.

Introduce named attribute bit masks and express FFA_NORMAL_MEM_REG_ATTR in terms of them for clarity.

Reject MEM_SHARE descriptors with the NS bit set, returning INVALID_PARAMETERS to match FF-A v1.1 rules that prohibit normal world from setting this bit.

Functional impact: MEM_SHARE now rejects descriptors with NS bit set, which were previously accepted but violate FF-A specification.

---
 xen/arch/arm/tee/ffa_private.h | 17 ++++++++++++++++-
 xen/arch/arm/tee/ffa_shm.c     |  6 ++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
xen/arch/arm/tee/ffa_private.h +16 -1
index cd7ecabc7eff..b625f1c72914 100644
@@ -129,11 +129,26 @@
 #define FFA_HANDLE_HYP_FLAG             BIT(63, ULL)
 #define FFA_HANDLE_INVALID              0xffffffffffffffffULL
 
+/* NS attribute was introduced in v1.1 */
+#define FFA_MEM_ATTR_NS                 BIT(6, U)
+
+#define FFA_MEM_ATTR_TYPE_DEV           (1U << 3)
+#define FFA_MEM_ATTR_TYPE_MEM           (2U << 4)
+
+#define FFA_MEM_ATTR_NC                 (1U << 2)
+#define FFA_MEM_ATTR_WB                 (3U << 2)
+
+#define FFA_MEM_ATTR_NON_SHARE          (0U)
+#define FFA_MEM_ATTR_OUT_SHARE          (2U)
+#define FFA_MEM_ATTR_INN_SHARE          (3U)
+
 /*
  * Memory attributes: Normal memory, Write-Back cacheable, Inner shareable
  * Defined in FF-A-1.1-REL0 Table 10.18 at page 175.
  */
-#define FFA_NORMAL_MEM_REG_ATTR         0x2fU
+#define FFA_NORMAL_MEM_REG_ATTR         (FFA_MEM_ATTR_TYPE_MEM | \
+                                         FFA_MEM_ATTR_WB | \
+                                         FFA_MEM_ATTR_INN_SHARE)
 /*
  * Memory access permissions: Read-write
  * Defined in FF-A-1.1-REL0 Table 10.15 at page 168.
xen/arch/arm/tee/ffa_shm.c +6 -0
index 8282bacf85d3..90800e44a86a 100644
@@ -512,6 +512,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     if ( ret )
         goto out_unlock;
 
+    if ( trans.mem_reg_attr & FFA_MEM_ATTR_NS )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock;
+    }
+
     if ( trans.mem_reg_attr != FFA_NORMAL_MEM_REG_ATTR )
     {
         ret = FFA_RET_NOT_SUPPORTED;
Signature · 1 line
2.50.1 (Apple Git-155)
#7 lore raw

PATCH 3/12 xen/arm: ffa: Harden shm page parsing

In reply to Bertrand Marquis

get_shm_pages() uses unchecked address arithmetic and does not enforce alignment, so malformed descriptors can cause overflow or slip through validation. The reclaim path also repeats handle-to-shm-mem conversion in multiple places, duplicating error handling.

Harden page parsing and reclaim handling:
- add ffa_safe_addr_add() and use it to detect address overflows
- enforce alignment checks in get_shm_pages() and return FF-A errors
- introduce ffa_secure_reclaim() and use it for MEM_RECLAIM and teardown
- simplify ffa_mem_share() argument handling and allow max page count

Functional impact: invalid or misaligned memory ranges now fail earlier with proper error codes; behavior for valid descriptors is unchanged.

---
 xen/arch/arm/tee/ffa_private.h | 11 +++++++
 xen/arch/arm/tee/ffa_shm.c     | 57 +++++++++++++++++-----------------
 2 files changed, 40 insertions(+), 28 deletions(-)
xen/arch/arm/tee/ffa_private.h +11 -0
index b625f1c72914..58562d8e733c 100644
@@ -632,4 +632,15 @@ static inline void ffa_uuid_set(struct ffa_uuid *id, uint32_t val0,
     id->val[1] = ((uint64_t)val3 << 32U) | val2;
 }
 
+/*
+ * Common overflow-safe helper to verify that adding a number of pages to an
+ * address will not wrap around.
+ */
+static inline bool ffa_safe_addr_add(uint64_t addr, uint64_t pages)
+{
+    uint64_t off = pages * FFA_PAGE_SIZE;
+
+    return (off / FFA_PAGE_SIZE) == pages && addr <= UINT64_MAX - off;
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_shm.c +29 -28

150 lines not on this page · raw message

Signature · 1 line
2.50.1 (Apple Git-155)
#12 lore raw

PATCH 4/12 xen/arm: ffa: Add FF-A 1.2 endpoint memory access descriptors

In reply to Bertrand Marquis

FF-A 1.2 extends the endpoint memory access descriptor (EMAD) from 16 to 32 bytes, adding implementation-defined (IMPDEF) fields and reserved space. The MEM_SHARE path currently assumes the 1.1 EMAD size and rejects the 1.2 layout.

Add FF-A 1.2 EMAD support to MEM_SHARE:
- define ffa_mem_access_1_2 and store IMPDEF payload in ffa_shm_mem
- emit 1.2 EMADs to the SPMC for FF-A 1.2 guests, forwarding IMPDEF
- refactor header parsing into read_mem_transaction() for 1.0/1.1+
- detect EMAD format by mem_access_size to allow 1.1 on 1.2 guests

Functional impact: MEM_SHARE supports FF-A 1.2 EMADs.

---
 xen/arch/arm/tee/ffa_shm.c | 108 +++++++++++++++++++++++++++++--------
 1 file changed, 86 insertions(+), 22 deletions(-)
xen/arch/arm/tee/ffa_shm.c +86 -22
index 4c0b45cde6ee..905a64e3db01 100644
@@ -30,6 +30,14 @@ struct ffa_mem_access {
     uint64_t reserved;
 };
 
+/* Endpoint memory access descriptor (FF-A 1.2) */
+struct ffa_mem_access_1_2 {
+    struct ffa_mem_access_perm access_perm;
+    uint32_t region_offs;
+    uint8_t impdef[16];
+    uint8_t reserved[8];
+};
+
 /* Lend, donate or share memory transaction descriptor */
 struct ffa_mem_transaction_1_0 {
     uint16_t sender_id;
@@ -73,7 +81,7 @@ struct ffa_mem_transaction_1_1 {
 /*
  * The parts needed from struct ffa_mem_transaction_1_0 or struct
  * ffa_mem_transaction_1_1, used to provide an abstraction of difference in
- * data structures between version 1.0 and 1.1. This is just an internal
+ * data structures between version 1.0 and 1.2. This is just an internal
  * interface and can be changed without changing any ABI.
  */
 struct ffa_mem_transaction_int {
@@ -92,6 +100,8 @@ struct ffa_shm_mem {
     uint16_t sender_id;
     uint16_t ep_id;     /* endpoint, the one lending */
     uint64_t handle;    /* FFA_HANDLE_INVALID if not set yet */
+    /* Endpoint memory access descriptor IMPDEF value (FF-A 1.2). */
+    uint64_t impdef[2];
     unsigned int page_count;
     struct page_info *pages[];
 };
@@ -297,17 +307,21 @@ static void init_range(struct ffa_address_range *addr_range,
  * This function uses the ffa_spmc tx buffer to transmit the memory transaction
  * descriptor.
  */
-static int share_shm(struct ffa_shm_mem *shm)
+static int share_shm(struct ffa_shm_mem *shm, uint32_t ffa_vers)
 {
     const uint32_t max_frag_len = FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE;
     struct ffa_mem_access *mem_access_array;
+    struct ffa_mem_access_1_2 *mem_access_array_1_2;
     struct ffa_mem_transaction_1_1 *descr;
     struct ffa_address_range *addr_range;
     struct ffa_mem_region *region_descr;
-    const unsigned int region_count = 1;
     uint32_t frag_len;
     uint32_t tot_len;
+    uint32_t mem_access_size;
+    uint32_t mem_access_offs;
+    uint32_t region_offs;
     paddr_t last_pa;
+    uint32_t range_count;
     unsigned int n;
     paddr_t pa;
     int32_t ret;
@@ -326,16 +340,35 @@ static int share_shm(struct ffa_shm_mem *shm)
     descr->handle = shm->handle;
     descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
     descr->mem_access_count = 1;
-    descr->mem_access_size = sizeof(*mem_access_array);
-    descr->mem_access_offs = MEM_ACCESS_OFFSET(0);
+    if ( ffa_vers >= FFA_VERSION_1_2 )
+        mem_access_size = sizeof(struct ffa_mem_access_1_2);
+    else
+        mem_access_size = sizeof(struct ffa_mem_access);
+    mem_access_offs = sizeof(struct ffa_mem_transaction_1_1);
+    region_offs = mem_access_offs + mem_access_size;
+    descr->mem_access_size = mem_access_size;
+    descr->mem_access_offs = mem_access_offs;
 
-    mem_access_array = buf + descr->mem_access_offs;
-    memset(mem_access_array, 0, sizeof(*mem_access_array));
-    mem_access_array[0].access_perm.endpoint_id = shm->ep_id;
-    mem_access_array[0].access_perm.perm = FFA_MEM_ACC_RW;
-    mem_access_array[0].region_offs = REGION_OFFSET(descr->mem_access_count, 0);
+    if ( ffa_vers >= FFA_VERSION_1_2 )
+    {
+        mem_access_array_1_2 = buf + mem_access_offs;
+        memset(mem_access_array_1_2, 0, sizeof(*mem_access_array_1_2));
+        mem_access_array_1_2[0].access_perm.endpoint_id = shm->ep_id;
+        mem_access_array_1_2[0].access_perm.perm = FFA_MEM_ACC_RW;
+        mem_access_array_1_2[0].region_offs = region_offs;
+        memcpy(mem_access_array_1_2[0].impdef, shm->impdef,
+               sizeof(mem_access_array_1_2[0].impdef));
+    }
+    else
+    {
+        mem_access_array = buf + mem_access_offs;
+        memset(mem_access_array, 0, sizeof(*mem_access_array));
+        mem_access_array[0].access_perm.endpoint_id = shm->ep_id;
+        mem_access_array[0].access_perm.perm = FFA_MEM_ACC_RW;
+        mem_access_array[0].region_offs = region_offs;
+    }
 
-    region_descr = buf + mem_access_array[0].region_offs;
+    region_descr = buf + region_offs;
     memset(region_descr, 0, sizeof(*region_descr));
     region_descr->total_page_count = shm->page_count;
 
@@ -349,8 +382,9 @@ static int share_shm(struct ffa_shm_mem *shm)
         region_descr->address_range_count++;
     }
 
-    tot_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count,
-                                region_descr->address_range_count);
+    range_count = region_descr->address_range_count;
+    tot_len = region_offs + sizeof(*region_descr) +
+              range_count * sizeof(struct ffa_address_range);
     if ( tot_len > max_frag_len )
     {
         ret = FFA_RET_NOT_SUPPORTED;
@@ -358,7 +392,7 @@ static int share_shm(struct ffa_shm_mem *shm)
     }
 
     addr_range = region_descr->address_range_array;
-    frag_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count, 1);
+    frag_len = region_offs + sizeof(*region_descr) + sizeof(*addr_range);
     last_pa = page_to_maddr(shm->pages[0]);
     init_range(addr_range, last_pa);
     for ( n = 1; n < shm->page_count; last_pa = pa, n++ )
@@ -448,6 +482,12 @@ static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen,
     if ( size * count + offs > blen )
         return FFA_RET_INVALID_PARAMETERS;
 
+    if ( size < sizeof(struct ffa_mem_access) )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    if ( offs & 0xF )
+        return FFA_RET_INVALID_PARAMETERS;
+
     trans->mem_reg_attr = mem_reg_attr;
     trans->flags = flags;
     trans->mem_access_size = size;
@@ -464,7 +504,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     uint64_t addr = get_user_reg(regs, 3);
     uint32_t page_count = get_user_reg(regs, 4);
     const struct ffa_mem_region *region_descr;
-    const struct ffa_mem_access *mem_access;
+    const struct ffa_mem_access_1_2 *mem_access;
     struct ffa_mem_transaction_int trans;
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
@@ -474,9 +514,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     register_t handle_hi = 0;
     register_t handle_lo = 0;
     int ret = FFA_RET_DENIED;
+    uint32_t ffa_vers;
     uint32_t range_count;
     uint32_t region_offs;
     uint16_t dst_id;
+    uint8_t perm;
+    uint64_t impdef[2];
 
     if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
     {
@@ -515,8 +558,8 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     if ( frag_len > tx_size )
         goto out_unlock;
 
-    ret = read_mem_transaction(ACCESS_ONCE(ctx->guest_vers), tx_buf,
-                               frag_len, &trans);
+    ffa_vers = ACCESS_ONCE(ctx->guest_vers);
+    ret = read_mem_transaction(ffa_vers, tx_buf, frag_len, &trans);
     if ( ret )
         goto out_unlock;
 
@@ -545,13 +588,35 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
     }
 
+    if ( trans.mem_access_size < sizeof(struct ffa_mem_access) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock;
+    }
+
     /* Check that it fits in the supplied data */
     if ( trans.mem_access_offs + trans.mem_access_size > frag_len )
         goto out_unlock;
 
     mem_access = tx_buf + trans.mem_access_offs;
-
     dst_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    perm = ACCESS_ONCE(mem_access->access_perm.perm);
+    region_offs = ACCESS_ONCE(mem_access->region_offs);
+
+    /*
+     * FF-A 1.2 introduced an extended mem_access descriptor with impdef
+     * fields, but guests can still use the 1.1 format if they don't need
+     * implementation-defined data. Detect which format is used based on
+     * the mem_access_size field rather than the negotiated FF-A version.
+     */
+    if ( trans.mem_access_size >= sizeof(struct ffa_mem_access_1_2) )
+        memcpy(impdef, mem_access->impdef, sizeof(impdef));
+    else
+    {
+        impdef[0] = 0;
+        impdef[1] = 0;
+    }
+
     if ( !FFA_ID_IS_SECURE(dst_id) )
     {
         /* we do not support sharing with VMs */
@@ -559,13 +624,11 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
     }
 
-    if ( ACCESS_ONCE(mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
+    if ( perm != FFA_MEM_ACC_RW )
     {
         ret = FFA_RET_NOT_SUPPORTED;
         goto out_unlock;
     }
-
-    region_offs = ACCESS_ONCE(mem_access->region_offs);
     if ( sizeof(*region_descr) + region_offs > frag_len )
     {
         ret = FFA_RET_NOT_SUPPORTED;
@@ -590,6 +653,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     }
     shm->sender_id = trans.sender_id;
     shm->ep_id = dst_id;
+    memcpy(shm->impdef, impdef, sizeof(shm->impdef));
 
     /*
      * Check that the Composite memory region descriptor fits.
@@ -605,7 +669,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     if ( ret )
         goto out;
 
-    ret = share_shm(shm);
+    ret = share_shm(shm, ffa_vers);
     if ( ret )
         goto out;
 
Signature · 1 line
2.50.1 (Apple Git-155)
#19 lore raw

PATCH 6/12 xen/arm: ffa: Fix RX/TX map layout and errors

In reply to Bertrand Marquis

FFA_RXTX_MAP builds a descriptor for the SPMC and the composite region offsets must be 8-byte aligned. Xen currently uses a 12-byte header size, which produces misaligned RX/TX offsets. Mapping failures also return INVALID_PARAMETERS even when the failure is due to resource exhaustion, which misreports the error condition.

Round the descriptor header size up to 8 bytes before placing the RX region and derive the TX offset from the aligned RX offset. Return FFA_RET_NO_MEMORY when the TX or RX buffer mapping fails before the error paths unwind.

Functional impact: RXTX_MAP now returns NO_MEMORY on mapping failures and the SPMC receives an aligned RX/TX descriptor layout.

---
 xen/arch/arm/tee/ffa_rxtx.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
xen/arch/arm/tee/ffa_rxtx.c +10 -3
index 50758fb57cdf..eff95a7955d7 100644
@@ -119,11 +119,17 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 
     tx = __map_domain_page_global(tx_pg);
     if ( !tx )
+    {
+        ret = FFA_RET_NO_MEMORY;
         goto err_put_rx_pg;
+    }
 
     rx = __map_domain_page_global(rx_pg);
     if ( !rx )
+    {
+        ret = FFA_RET_NO_MEMORY;
         goto err_unmap_tx;
+    }
 
     /*
      * Transmit the RX/TX buffer information to the SPM if acquire is supported
@@ -136,7 +142,8 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
         struct ffa_mem_region *mem_reg;
 
         /* All must fit in our TX buffer */
-        BUILD_BUG_ON(sizeof(*rxtx_desc) + sizeof(*mem_reg) * 2 +
+        BUILD_BUG_ON(ROUNDUP(sizeof(*rxtx_desc), 8) +
+                     sizeof(*mem_reg) * 2 +
                      sizeof(struct ffa_address_range) * 2 >
                      FFA_MAX_RXTX_PAGE_COUNT * FFA_PAGE_SIZE);
 
@@ -153,8 +160,8 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
          */
         rxtx_desc->sender_id = ffa_get_vm_id(d);
         rxtx_desc->reserved = 0;
-        rxtx_desc->rx_region_offs = sizeof(*rxtx_desc);
-        rxtx_desc->tx_region_offs = sizeof(*rxtx_desc) +
+        rxtx_desc->rx_region_offs = ROUNDUP(sizeof(*rxtx_desc), 8);
+        rxtx_desc->tx_region_offs = rxtx_desc->rx_region_offs +
                                     offsetof(struct ffa_mem_region,
                                              address_range_array[1]);
 
Signature · 1 line
2.50.1 (Apple Git-155)
#21 lore raw

PATCH 7/12 xen/arm: ffa: Fix RXTX_UNMAP ownership race

In reply to Bertrand Marquis

rxtx_unmap() checks RX ownership without holding the RX/TX locks and only enforces the ownership rule when FFA_RX_ACQUIRE is supported. This allows a vCPU to acquire RX between the check and unmap, and it lets RXTX_UNMAP proceed while RX is owned when buffers are not forwarded to firmware.

Hold rx_lock/tx_lock across the ownership check and unmap, and deny RXTX_UNMAP whenever RX is owned, independent of RX_ACQUIRE support. For teardown, release RX ownership under the same lock window; use FFA_RX_RELEASE directly because rx_lock is held, and clear the local flag when the firmware path is unavailable.

Functional impact: RXTX_UNMAP now reliably returns DENIED while RX is owned, and teardown releases/clears ownership without a race.

---
 xen/arch/arm/tee/ffa_rxtx.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
xen/arch/arm/tee/ffa_rxtx.c +33 -3
index eff95a7955d7..450ce102cbdc 100644
@@ -220,7 +220,7 @@ err_unlock_rxtx:
     return ret;
 }
 
-static int32_t rxtx_unmap(struct domain *d)
+static int32_t rxtx_unmap(struct domain *d, bool teardown)
 {
     struct ffa_ctx *ctx = d->arch.tee;
     int32_t ret = FFA_RET_OK;
@@ -234,6 +234,36 @@ static int32_t rxtx_unmap(struct domain *d)
         goto err_unlock_rxtx;
     }
 
+    if ( !ctx->rx_is_free )
+    {
+        if ( teardown )
+        {
+            if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+            {
+                int32_t rel_ret;
+
+                /* Can't use ffa_rx_release() while holding rx_lock. */
+                rel_ret = ffa_simple_call(FFA_RX_RELEASE, ctx->ffa_id,
+                                          0, 0, 0);
+                if ( rel_ret )
+                    gdprintk(XENLOG_DEBUG,
+                             "ffa: RX release during teardown failed: %d\n",
+                             rel_ret);
+                else
+                    ctx->rx_is_free = true;
+            }
+            else
+                ctx->rx_is_free = true;
+        }
+        else
+        {
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: RXTX_UNMAP denied, RX buffer owned by VM\n");
+            ret = FFA_RET_DENIED;
+            goto err_unlock_rxtx;
+        }
+    }
+
     if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
     {
         ret = ffa_rxtx_unmap(ffa_get_vm_id(d));
@@ -261,7 +291,7 @@ err_unlock_rxtx:
 
 int32_t ffa_handle_rxtx_unmap(void)
 {
-    return rxtx_unmap(current->domain);
+    return rxtx_unmap(current->domain, false);
 }
 
 int32_t ffa_rx_acquire(struct ffa_ctx *ctx, void **buf, size_t *buf_size)
@@ -369,7 +399,7 @@ int32_t ffa_rxtx_domain_init(struct domain *d)
 
 void ffa_rxtx_domain_destroy(struct domain *d)
 {
-    rxtx_unmap(d);
+    rxtx_unmap(d, true);
 }
 
 void *ffa_rxtx_spmc_rx_acquire(void)
Signature · 1 line
2.50.1 (Apple Git-155)
#0 lore raw

cover xen/arm: ffa: v1.2 improvements

This series tightens Xen's FF-A mediator after the v1.2 enablement work, focusing on correctness, validation, and diagnostics while keeping existing v1.0/v1.1 guests working.

Hardening and validation (Patches 1-3):
1) centralize endpoint lookup validation for messaging paths
2) reject NS MEM_SHARE attributes and name normal memory encoding
3) tighten MEM_SHARE page parsing with overflow/alignment checks

FF-A 1.2 support (Patch 4):
4) accept 1.2 EMADs in MEM_SHARE and forward IMPDEF while allowing 1.1 layouts

Correctness fixes (Patches 5-9):
5) avoid spurious SPMC RX releases on PARTINFO failures
6) align RX/TX map descriptor layout and return NO_MEMORY on map failures
7) deny RXTX_UNMAP while a VM owns the RX buffer and close the race
8) validate FFA_FEATURES properties and gate ABIs, with RX_ACQUIRE workaround
9) gate SEND2 SP support on RX_ACQUIRE

Code quality and diagnostics (Patches 10-12):
10) reduce log noise and ratelimit expected failures
11) add MEM_SHARE page diagnostics for malformed ranges and mapping issues
12) add message parameter diagnostics for SEND2 and direct requests

Backward compatibility: v1.0/v1.1 guests continue to use their ABI unchanged; v1.2 guests use EMADs and negotiated ABI features.

The implementation passes the FF-A Architecture Compliance Suite.

This serie was validated through gitlab-ci here:
https://gitlab.com/xen-project/people/bmarquis/xen-ffa-research/-/tree/ffa-v12-improve/v1
Build pipeline for the serie:
https://gitlab.com/xen-project/people/bmarquis/xen-ffa-research/-/pipelines/2303128986

Bertrand Marquis (12):
xen/arm: ffa: Add endpoint lookup helper
xen/arm: ffa: Fix MEM_SHARE NS attribute handling xen/arm: ffa: Harden shm page parsing
xen/arm: ffa: Add FF-A 1.2 endpoint memory access descriptors xen/arm: ffa: Fix PARTINFO RX release errors xen/arm: ffa: Fix RX/TX map layout and errors xen/arm: ffa: Fix RXTX_UNMAP ownership race xen/arm: ffa: Fix FFA_FEATURES validation
xen/arm: ffa: Fix SEND2 SP support gating
xen/arm: ffa: Reduce logging noise
xen/arm: ffa: Add MEM_SHARE page diagnostics xen/arm: ffa: Add message parameter diagnostics

 xen/arch/arm/tee/ffa.c          | 124 +++++++++++++++--
 xen/arch/arm/tee/ffa_msg.c      |  76 +++++++---
 xen/arch/arm/tee/ffa_notif.c    |  11 +-
 xen/arch/arm/tee/ffa_partinfo.c |  29 ++--
 xen/arch/arm/tee/ffa_private.h  |  33 ++++-
 xen/arch/arm/tee/ffa_rxtx.c     |  84 +++++++++--
 xen/arch/arm/tee/ffa_shm.c      | 240 ++++++++++++++++++++++++--------
 7 files changed, 479 insertions(+), 118 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#24 lore raw

PATCH 8/12 xen/arm: ffa: Fix FFA_FEATURES validation

FFA_FEATURES currently accepts non-zero input properties (w2-w7) from guests and advertises several ABIs unconditionally, even when firmware support is missing or when the ABI is physical-instance-only. This can mislead guests about what Xen can actually provide and violates FF-A calling conventions. Some SPMCs (Hafnium v2.14 or earlier) also fail to report FFA_RX_ACQUIRE despite supporting it.

Update FFA_FEATURES validation to match spec and firmware support:
- reject non-zero w2-w7 input properties with INVALID_PARAMETERS
- reject 64-bit calling conventions from 32-bit guests with NOT_SUPPORTED
- return NOT_SUPPORTED for physical-instance-only ABIs (FFA_NOTIFICATION_BITMAP_{CREATE,DESTROY}, FFA_RX_ACQUIRE)
- advertise FFA_INTERRUPT as supported
- gate message ABIs on firmware support:
- FFA_MSG_SEND_DIRECT_REQ_{32,64}
- FFA_MSG_SEND_DIRECT_REQ2 (also requires FF-A 1.2 negotiation)
- FFA_MSG_SEND2 (or VM-to-VM enabled)
- report MEM_SHARE_{32,64} only when FFA_MEM_SHARE_64 is supported
- stop advertising FFA_MSG_YIELD (not implemented)

Update firmware probing: drop FFA_MEM_SHARE_32 checks (deprecated) and add FFA_RX_ACQUIRE to the probed set. If FFA_MSG_SEND2 is reported but FFA_RX_ACQUIRE is not, assume RX_ACQUIRE support and warn to work around the Hafnium bug.

Functional impact: guests now see ABI support that reflects firmware capabilities and Xen implementation status. When SEND2 is present but RX_ACQUIRE is not reported, Xen assumes RX_ACQUIRE support.

---
 xen/arch/arm/tee/ffa.c | 62 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 55 insertions(+), 7 deletions(-)
xen/arch/arm/tee/ffa.c +55 -7
index 6de2b9f8ac8e..e9e020bb0cb3 100644
@@ -91,10 +91,10 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
     FW_ABI(FFA_PARTITION_INFO_GET),
     FW_ABI(FFA_NOTIFICATION_INFO_GET_64),
     FW_ABI(FFA_NOTIFICATION_GET),
+    FW_ABI(FFA_RX_ACQUIRE),
     FW_ABI(FFA_RX_RELEASE),
     FW_ABI(FFA_RXTX_MAP_64),
     FW_ABI(FFA_RXTX_UNMAP),
-    FW_ABI(FFA_MEM_SHARE_32),
     FW_ABI(FFA_MEM_SHARE_64),
     FW_ABI(FFA_MEM_RECLAIM),
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
@@ -240,19 +240,39 @@ static void handle_features(struct cpu_user_regs *regs)
     struct ffa_ctx *ctx = d->arch.tee;
     unsigned int n;
 
+    /*
+     * Xen does not accept any non-zero FFA_FEATURES input properties from
+     * VMs. The spec only defines w2 input properties for FFA_MEM_RETRIEVE_REQ
+     * (NS-bit negotiation for SP/SPMC) and FFA_RXTX_MAP (buffer size and
+     * alignment), so w2 must be MBZ for our callers.
+     */
     for ( n = 2; n <= 7; n++ )
     {
         if ( get_user_reg(regs, n) )
         {
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+            ffa_set_regs_error(regs, FFA_RET_INVALID_PARAMETERS);
             return;
         }
     }
 
+    if ( !is_64bit_domain(d) && smccc_is_conv_64(a1) )
+    {
+        /* 32bit guests should only use 32bit convention calls */
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        return;
+    }
+
     switch ( a1 )
     {
+    case FFA_NOTIFICATION_BITMAP_CREATE:
+    case FFA_NOTIFICATION_BITMAP_DESTROY:
+    case FFA_RX_ACQUIRE:
+        /* Physical-instance-only ABIs are not exposed to VMs. */
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        break;
     case FFA_ERROR:
     case FFA_VERSION:
+    case FFA_INTERRUPT:
     case FFA_SUCCESS_32:
     case FFA_SUCCESS_64:
     case FFA_FEATURES:
@@ -261,16 +281,25 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_RXTX_UNMAP:
     case FFA_MEM_RECLAIM:
     case FFA_PARTITION_INFO_GET:
+        ffa_set_regs_success(regs, 0, 0);
+        break;
     case FFA_MSG_SEND_DIRECT_REQ_32:
     case FFA_MSG_SEND_DIRECT_REQ_64:
-    case FFA_MSG_SEND2:
     case FFA_RUN:
-    case FFA_INTERRUPT:
-    case FFA_MSG_YIELD:
-        ffa_set_regs_success(regs, 0, 0);
+        if ( ffa_fw_supports_fid(a1) )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        break;
+    case FFA_MSG_SEND2:
+        if ( ffa_fw_supports_fid(a1) || IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     case FFA_MSG_SEND_DIRECT_REQ2:
-        if ( ACCESS_ONCE(ctx->guest_vers) >= FFA_VERSION_1_2 )
+        if ( ACCESS_ONCE(ctx->guest_vers) >= FFA_VERSION_1_2 &&
+             ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ2) )
         {
             ffa_set_regs_success(regs, 0, 0);
         }
@@ -281,6 +310,11 @@ static void handle_features(struct cpu_user_regs *regs)
         break;
     case FFA_MEM_SHARE_64:
     case FFA_MEM_SHARE_32:
+        if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
+        {
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+            break;
+        }
         /*
          * We currently don't support dynamically allocated buffers. Report
          * that with 0 in bit[0] of w2.
@@ -688,6 +722,20 @@ static bool ffa_probe_fw(void)
                    ffa_fw_abi_needed[i].name);
     }
 
+    /*
+     * Hafnium v2.14 or earlier does not report FFA_RX_ACQUIRE in
+     * FFA_FEATURES even though it supports it.
+     */
+    if ( !ffa_fw_supports_fid(FFA_RX_ACQUIRE) &&
+         ffa_fw_supports_fid(FFA_MSG_SEND2) )
+    {
+        printk(XENLOG_WARNING
+               "ARM FF-A Firmware reports FFA_MSG_SEND2 without FFA_RX_ACQUIRE\n");
+        printk(XENLOG_WARNING
+               "ffa: assuming RX_ACQUIRE support (workaround)\n");
+        set_bit(FFA_ABI_BITNUM(FFA_RX_ACQUIRE), ffa_fw_abi_supported);
+    }
+
     if ( !ffa_rxtx_spmc_init() )
     {
         printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
Signature · 1 line
2.50.1 (Apple Git-155)
#29 lore raw

PATCH 9/12 xen/arm: ffa: Fix SEND2 SP support gating

In reply to Bertrand Marquis

Xen only maps a VM RX/TX buffer into the SPMC when firmware supports FFA_RX_ACQUIRE. Without that mapping, the SPMC cannot access the VM TX buffer to relay FFA_MSG_SEND2 to a secure partition.

Advertise FFA_MSG_SEND2 to guests only when VM-to-VM is enabled or when the firmware supports both FFA_MSG_SEND2 and FFA_RX_ACQUIRE, and reject SEND2 to a secure partition otherwise. Add comments to document the mapping/ownership dependency.

Functional impact: SEND2 to a secure partition is reported as NOT_SUPPORTED when FFA_RX_ACQUIRE is absent.

---
 xen/arch/arm/tee/ffa.c     |  8 +++++++-
 xen/arch/arm/tee/ffa_msg.c | 11 +++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
xen/arch/arm/tee/ffa.c +7 -1
index e9e020bb0cb3..a46c6e5e67f1 100644
@@ -292,7 +292,13 @@ static void handle_features(struct cpu_user_regs *regs)
             ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     case FFA_MSG_SEND2:
-        if ( ffa_fw_supports_fid(a1) || IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+        /*
+         * Forwarding SEND2 to an SP requires the SPMC to see the VM TX buffer.
+         * We only map VM RX/TX into the SPMC when RX_ACQUIRE is supported.
+         */
+        if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) ||
+             (ffa_fw_supports_fid(FFA_MSG_SEND2) &&
+              ffa_fw_supports_fid(FFA_RX_ACQUIRE)) )
             ffa_set_regs_success(regs, 0, 0);
         else
             ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_msg.c +11 -0
index 10856fddcbc4..928f269f6c3a 100644
@@ -274,6 +274,17 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
             ret = FFA_RET_NOT_SUPPORTED;
             goto out;
         }
+        /*
+         * The SPMC needs access to the VM TX buffer to relay SEND2.
+         * We only map VM RX/TX into the SPMC when RX_ACQUIRE is supported.
+         */
+        if ( !ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+        {
+            ret = FFA_RET_NOT_SUPPORTED;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 to SP requires RX_ACQUIRE\n");
+            goto out;
+        }
 
         ret = ffa_simple_call(FFA_MSG_SEND2,
                               ((uint32_t)ffa_get_vm_id(src_d)) << 16, 0, 0, 0);
Signature · 1 line
2.50.1 (Apple Git-155)
#31 lore raw

PATCH 10/12 xen/arm: ffa: Reduce logging noise

In reply to Bertrand Marquis

Several FF-A paths log warnings for expected or recoverable conditions, leading to noisy logs during normal operation.

Reduce log noise by:
- demoting unhandled FID warnings to debug
- ratelimiting notification and VM teardown failures
- printing the bit-15 convention warning once per boot
- adjusting log prefixes for hypervisor-only events
- adding debug diagnostics for RX/TX acquire failures, with busy cases ratelimited

No functional changes.

---
 xen/arch/arm/tee/ffa.c          | 11 ++++++-----
 xen/arch/arm/tee/ffa_notif.c    | 11 ++++++-----
 xen/arch/arm/tee/ffa_partinfo.c | 15 ++++++++-------
 xen/arch/arm/tee/ffa_rxtx.c     | 21 +++++++++++++++++++--
 4 files changed, 39 insertions(+), 19 deletions(-)
xen/arch/arm/tee/ffa.c +6 -5
index a46c6e5e67f1..d0b9c9f1ab2e 100644
@@ -461,7 +461,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         break;
 
     default:
-        gprintk(XENLOG_ERR, "ffa: unhandled fid 0x%x\n", fid);
+        gdprintk(XENLOG_DEBUG, "ffa: unhandled fid 0x%x\n", fid);
         e = FFA_RET_NOT_SUPPORTED;
         break;
     }
@@ -581,7 +581,8 @@ static void ffa_domain_teardown_continue(struct ffa_ctx *ctx, bool first_time)
 
     if ( retry )
     {
-        printk(XENLOG_G_INFO "%pd: ffa: Remaining cleanup, retrying\n", ctx->teardown_d);
+        printk(XENLOG_G_DEBUG "%pd: ffa: Remaining cleanup, retrying\n",
+               ctx->teardown_d);
 
         ctx->teardown_expire = NOW() + FFA_CTX_TEARDOWN_DELAY;
 
@@ -622,7 +623,7 @@ static void ffa_teardown_timer_callback(void *arg)
     if ( ctx )
         ffa_domain_teardown_continue(ctx, false /* !first_time */);
     else
-        printk(XENLOG_G_ERR "%s: teardown list is empty\n", __func__);
+        printk(XENLOG_ERR "%s: teardown list is empty\n", __func__);
 }
 
 /* This function is supposed to undo what ffa_domain_init() has done */
@@ -684,7 +685,7 @@ static bool ffa_probe_fw(void)
 
     if ( !ffa_get_version(&vers) )
     {
-        gprintk(XENLOG_ERR, "Cannot retrieve the FFA version\n");
+        printk(XENLOG_ERR "ffa: Cannot retrieve the FFA version\n");
         goto err_no_fw;
     }
 
@@ -724,7 +725,7 @@ static bool ffa_probe_fw(void)
             set_bit(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id),
                     ffa_fw_abi_supported);
         else
-            printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
+            printk(XENLOG_WARNING "ARM FF-A Firmware does not support %s\n",
                    ffa_fw_abi_needed[i].name);
     }
 
xen/arch/arm/tee/ffa_notif.c +6 -5
index 37b05747cd21..186e72641237 100644
@@ -292,8 +292,9 @@ static void notif_vm_pend_intr(uint16_t vm_id)
             break;
         }
     }
-    if ( !v )
-        printk(XENLOG_ERR "ffa: can't inject NPI, all vCPUs offline\n");
+    if ( !v && printk_ratelimit() )
+        printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
+               d);
 
 out_unlock:
     rcu_unlock_domain(d);
@@ -316,9 +317,9 @@ static void notif_sri_action(void *unused)
         res = ffa_get_ret_code(&resp);
         if ( res )
         {
-            if ( res != FFA_RET_NO_DATA )
-                printk(XENLOG_ERR "ffa: notification info get failed: error %d\n",
-                       res);
+            if ( res != FFA_RET_NO_DATA && printk_ratelimit() )
+                printk(XENLOG_WARNING
+                       "ffa: notification info get failed: error %d\n", res);
             return;
         }
 
xen/arch/arm/tee/ffa_partinfo.c +8 -7
index 6b01c4abe915..c9f186b3720c 100644
@@ -461,9 +461,11 @@ static bool init_subscribers(void *buf, uint16_t count, uint32_t fpi_size)
          */
         if ( !FFA_ID_IS_SECURE(fpi->id) )
         {
-            printk(XENLOG_ERR "ffa: Firmware is not using bit 15 convention for IDs !!\n"
-                              "ffa: Secure partition with id 0x%04x cannot be used\n",
-                              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
         {
@@ -632,11 +634,10 @@ bool ffa_partinfo_domain_destroy(struct domain *d)
         res = ffa_direct_req_send_vm(subscr_vm_destroyed[n], ffa_get_vm_id(d),
                                      FFA_MSG_SEND_VM_DESTROYED);
 
-        if ( res )
-        {
-            printk(XENLOG_ERR "%pd: ffa: Failed to report destruction of vm_id %u to %u: res %d\n",
+        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);
-        }
 
         /*
          * For these two error codes the hypervisor is expected to resend
xen/arch/arm/tee/ffa_rxtx.c +19 -2
index 450ce102cbdc..3147aba21ddf 100644
@@ -4,6 +4,7 @@
  */
 
 #include <xen/const.h>
+#include <xen/lib.h>
 #include <xen/domain_page.h>
 #include <xen/mm.h>
 #include <xen/sizes.h>
@@ -82,8 +83,9 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 
     if ( page_count > FFA_MAX_RXTX_PAGE_COUNT || !page_count )
     {
-        printk(XENLOG_ERR "ffa: RXTX_MAP: error: %u pages requested (limit %u)\n",
-               page_count, FFA_MAX_RXTX_PAGE_COUNT);
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: RXTX_MAP: error: %u pages requested (limit %u)\n",
+                 page_count, FFA_MAX_RXTX_PAGE_COUNT);
         return FFA_RET_INVALID_PARAMETERS;
     }
 
@@ -303,12 +305,15 @@ int32_t ffa_rx_acquire(struct ffa_ctx *ctx, void **buf, size_t *buf_size)
     if ( !ctx->page_count )
     {
         ret = FFA_RET_DENIED;
+        gdprintk(XENLOG_DEBUG, "ffa: RX acquire denied, no RX/TX mapped\n");
         goto out;
     }
 
     if ( !ctx->rx_is_free )
     {
         ret = FFA_RET_BUSY;
+        if ( printk_ratelimit() )
+            gdprintk(XENLOG_DEBUG, "ffa: RX acquire busy\n");
         goto out;
     }
 
@@ -316,7 +321,10 @@ int32_t ffa_rx_acquire(struct ffa_ctx *ctx, void **buf, size_t *buf_size)
     {
         ret = ffa_simple_call(FFA_RX_ACQUIRE, ctx->ffa_id, 0, 0, 0);
         if ( ret != FFA_RET_OK )
+        {
+            gdprintk(XENLOG_DEBUG, "ffa: RX acquire failed: %d\n", ret);
             goto out;
+        }
     }
     ctx->rx_is_free = false;
     *buf = ctx->rx;
@@ -355,13 +363,22 @@ int32_t ffa_tx_acquire(struct ffa_ctx *ctx, const void **buf, size_t *buf_size)
     int32_t ret = FFA_RET_DENIED;
 
     if ( !spin_trylock(&ctx->tx_lock) )
+    {
+        gdprintk(XENLOG_DEBUG, "ffa: TX acquire busy\n");
         return FFA_RET_BUSY;
+    }
 
     if ( !ctx->page_count )
+    {
+        gdprintk(XENLOG_DEBUG, "ffa: TX acquire denied, no RX/TX mapped\n");
         goto err_unlock;
+    }
 
     if ( !ctx->tx )
+    {
+        gdprintk(XENLOG_DEBUG, "ffa: TX acquire denied, TX buffer missing\n");
         goto err_unlock;
+    }
 
     *buf = ctx->tx;
     *buf_size = ctx->page_count * FFA_PAGE_SIZE;
Signature · 1 line
2.50.1 (Apple Git-155)
#33 lore raw

PATCH 11/12 xen/arm: ffa: Add MEM_SHARE page diagnostics

In reply to Bertrand Marquis

MEM_SHARE failures in get_shm_pages() are silent, which makes malformed ranges and page mapping failures hard to diagnose.

Add debug logging for page validation failures:
- descriptor validation failures (unaligned, range short/overflow)
- per-page mapping failures (unmapped GFN, wrong p2m type)
- address overflow detection in range walks

Ratelimit temporary reclaim failures and log permanent reclaim failures as errors.

No functional changes.

---
 xen/arch/arm/tee/ffa_shm.c | 73 ++++++++++++++++++++++++++++++++------
 1 file changed, 63 insertions(+), 10 deletions(-)
xen/arch/arm/tee/ffa_shm.c +63 -10
index 905a64e3db01..89161753e922 100644
@@ -169,6 +169,12 @@ static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
     uint64_t addr;
     uint64_t page_count;
     uint64_t gaddr;
+    int32_t ret = FFA_RET_OK;
+    const char *reason = NULL;
+    unsigned int bad_rg = 0;
+    unsigned int bad_pg = 0;
+    unsigned long bad_addr = 0;
+    p2m_type_t bad_t = p2m_invalid;
 
     for ( n = 0; n < range_count; n++ )
     {
@@ -176,34 +182,78 @@ static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
         addr = ACCESS_ONCE(range[n].address);
 
         if ( !IS_ALIGNED(addr, FFA_PAGE_SIZE) )
-            return FFA_RET_INVALID_PARAMETERS;
+        {
+            ret = FFA_RET_INVALID_PARAMETERS;
+            reason = "unaligned";
+            bad_rg = n;
+            bad_addr = (unsigned long)addr;
+            goto out;
+        }
 
         for ( m = 0; m < page_count; m++ )
         {
             if ( pg_idx >= shm->page_count )
-                return FFA_RET_INVALID_PARAMETERS;
+            {
+                ret = FFA_RET_INVALID_PARAMETERS;
+                reason = "range overflow";
+                bad_rg = n;
+                bad_pg = m;
+                goto out;
+            }
 
             if ( !ffa_safe_addr_add(addr, m) )
-                return FFA_RET_INVALID_PARAMETERS;
+            {
+                ret = FFA_RET_INVALID_PARAMETERS;
+                reason = "addr overflow";
+                bad_rg = n;
+                bad_pg = m;
+                bad_addr = (unsigned long)addr;
+                goto out;
+            }
 
             gaddr = addr + m * FFA_PAGE_SIZE;
             gfn = gaddr_to_gfn(gaddr);
             shm->pages[pg_idx] = get_page_from_gfn(d, gfn_x(gfn), &t,
 						   P2M_ALLOC);
             if ( !shm->pages[pg_idx] )
-                return FFA_RET_DENIED;
+            {
+                ret = FFA_RET_DENIED;
+                reason = "gfn unmapped";
+                bad_rg = n;
+                bad_pg = m;
+                bad_addr = (unsigned long)gaddr;
+                goto out;
+            }
             /* Only normal RW RAM for now */
             if ( t != p2m_ram_rw )
-                return FFA_RET_DENIED;
+            {
+                ret = FFA_RET_DENIED;
+                reason = "p2m type";
+                bad_rg = n;
+                bad_pg = m;
+                bad_addr = (unsigned long)gaddr;
+                bad_t = t;
+                goto out;
+            }
             pg_idx++;
         }
     }
 
     /* The ranges must add up */
     if ( pg_idx < shm->page_count )
-        return FFA_RET_INVALID_PARAMETERS;
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        reason = "range short";
+        bad_pg = pg_idx;
+        goto out;
+    }
 
-    return FFA_RET_OK;
+out:
+    if ( ret )
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: mem share pages invalid: %s rg %u pg %u addr %#lx p2m %u\n",
+                 reason ? reason : "unknown", bad_rg, bad_pg, bad_addr, bad_t);
+    return ret;
 }
 
 static void put_shm_pages(struct ffa_shm_mem *shm)
@@ -759,8 +809,10 @@ bool ffa_shm_domain_destroy(struct domain *d)
              * A temporary error that may get resolved a bit later, it's
              * worth retrying.
              */
-            printk(XENLOG_G_INFO "%pd: ffa: Failed to reclaim handle %#lx : %d\n",
-                   d, shm->handle, res);
+            if ( printk_ratelimit() )
+                printk(XENLOG_G_WARNING
+                       "%pd: ffa: Failed to reclaim handle %#lx : %d\n",
+                       d, shm->handle, res);
             break; /* We will retry later */
         default:
             /*
@@ -772,7 +824,8 @@ bool ffa_shm_domain_destroy(struct domain *d)
              * FFA_RET_NO_MEMORY might be a temporary error as it it could
              * succeed if retried later, but treat it as permanent for now.
              */
-            printk(XENLOG_G_INFO "%pd: ffa: Permanent failure to reclaim handle %#lx : %d\n",
+            printk(XENLOG_G_ERR
+                   "%pd: ffa: Permanent failure to reclaim handle %#lx : %d\n",
                    d, shm->handle, res);
 
             /*
Signature · 1 line
2.50.1 (Apple Git-155)
#36 lore raw

PATCH 12/12 xen/arm: ffa: Add message parameter diagnostics

In reply to Bertrand Marquis

MSG_SEND2 and direct request validation failures are silent, making it hard to diagnose invalid IDs, oversized messages, or unsupported destination types.

Add debug logs for parameter validation failures:
- invalid endpoint IDs
- oversized messages
- unsupported destination types
- invalid sender/receiver combinations
- ratelimit MSG_SEND2 busy failures to avoid log flooding

No functional changes.

---
 xen/arch/arm/tee/ffa_msg.c | 45 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
xen/arch/arm/tee/ffa_msg.c +45 -0
index 928f269f6c3a..cc273c9a8e80 100644
@@ -4,6 +4,7 @@
  */
 
 #include <xen/const.h>
+#include <xen/lib.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
 
@@ -100,6 +101,7 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     if ( !ffa_fw_supports_fid(fid) )
     {
         ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: direct req fid %#x not supported\n", fid);
         goto out;
     }
 
@@ -108,6 +110,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
          (src_dst & GENMASK(15,0)) == ffa_get_vm_id(d) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req invalid src/dst %#x\n",
+                 (uint32_t)src_dst);
         goto out;
     }
 
@@ -115,6 +120,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
     {
         ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req to non-secure dst %#x\n",
+                 (uint32_t)(src_dst & GENMASK(15, 0)));
         goto out;
     }
 
@@ -166,7 +174,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     /* This is also checking that dest is not src */
     ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
     if ( ret )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 lookup failed: dst %#x ret %d\n",
+                 dst_id, ret);
         return ret;
+    }
 
     /* This also checks that destination has set a Rx buffer */
     ret = ffa_rx_acquire(dst_ctx , &rx_buf, &rx_size);
@@ -199,6 +212,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     /* receiver rx buffer will be released by the receiver*/
 
 out_unlock:
+    if ( ret )
+    {
+        if ( ret != FFA_RET_BUSY || printk_ratelimit() )
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to %#x failed: %d\n",
+                     dst_id, ret);
+    }
     rcu_unlock_domain(dst_d);
     if ( !ret )
         ffa_raise_rx_buffer_full(dst_d);
@@ -226,7 +245,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
     ret = ffa_tx_acquire(src_ctx, &tx_buf, &tx_size);
     if ( ret != FFA_RET_OK )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 TX acquire failed: %d\n", ret);
         return ret;
+    }
 
     /* create a copy of the message header */
     memcpy(&src_msg, tx_buf, sizeof(src_msg));
@@ -238,6 +261,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
          dst_id == ffa_get_vm_id(src_d) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid src/dst src %#x dst %#x\n",
+                 src_id, dst_id);
         goto out;
     }
 
@@ -246,6 +272,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
         if (src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_1))
         {
             ret = FFA_RET_INVALID_PARAMETERS;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 invalid msg_offset %u (v1.1)\n",
+                     src_msg.msg_offset);
             goto out;
         }
         /* Set uuid to Nil UUID for v1.1 guests */
@@ -255,6 +284,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
     else if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_2) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_offset %u (v1.2)\n",
+                 src_msg.msg_offset);
         goto out;
     }
 
@@ -263,6 +295,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
             src_msg.msg_size > (tx_size - src_msg.msg_offset) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_size %u offset %u tx %zu\n",
+                 src_msg.msg_size, src_msg.msg_offset, tx_size);
         goto out;
     }
 
@@ -272,6 +307,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
         if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
         {
             ret = FFA_RET_NOT_SUPPORTED;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 to SP not supported\n");
             goto out;
         }
         /*
@@ -288,6 +325,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
         ret = ffa_simple_call(FFA_MSG_SEND2,
                               ((uint32_t)ffa_get_vm_id(src_d)) << 16, 0, 0, 0);
+        if ( ret )
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to SP failed: %d\n", ret);
     }
     else if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
@@ -295,7 +334,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
         ret = ffa_msg_send2_vm(dst_id, tx_buf, &src_msg);
     }
     else
+    {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 to VM disabled (dst %#x)\n", dst_id);
+    }
 
 out:
     ffa_tx_release(src_ctx);
@@ -311,6 +354,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
     if ( !ffa_fw_supports_fid(fid) )
     {
         ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run fid %#x not supported\n", fid);
         goto out;
     }
 
@@ -322,6 +366,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
     if ( !FFA_ID_IS_SECURE(dst >> 16) )
     {
         ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run to non-secure dst %#x\n", dst);
         goto out;
     }
 
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/12
#2 lore raw

Re: [PATCH 01/12] xen/arm: ffa: Add endpoint lookup helper

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 15 lines · Bertrand Marquis

Direct messaging paths duplicate endpoint validation and RCU domain lookup logic across multiple call sites, which makes the checks easy to drift and complicates maintenance.

Introduce ffa_endpoint_domain_lookup() to centralize this logic. The helper validates the endpoint ID (rejecting ID 0 for the hypervisor), performs RCU domain lookup, ensures the domain is live and has an initialized FF-A context with a negotiated version, and returns the domain locked via RCU.

Switch ffa_msg_send2_vm() to use the helper, replacing its open-coded validation sequence. This consolidates approximately 20 lines of duplicated checks into a single call.

Quoted 8 lines

No functional changes.

---
 xen/arch/arm/tee/ffa.c         | 45 ++++++++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_msg.c     | 24 +++---------------
 xen/arch/arm/tee/ffa_private.h |  3 +++
 3 files changed, 51 insertions(+), 21 deletions(-)

Looks good:

Cheers,
Jens

Quoted 112 lines
xen/arch/arm/tee/ffa.c +45 -0
index ed18e76080d0..6de2b9f8ac8e 100644
@@ -433,6 +433,51 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     return true;
 }
 
+/*
+ * Look up a domain by its FF-A endpoint ID and validate it's ready for FF-A.
+ * Returns FFA_RET_OK on success with domain locked via RCU.
+ * Caller must call rcu_unlock_domain() when done.
+ *
+ * Validates:
+ * - endpoint_id is not 0 (the hypervisor)
+ * - domain exists and is live
+ * - domain has FF-A context initialized
+ * - domain has negotiated an FF-A version
+ */
+int32_t ffa_endpoint_domain_lookup(uint16_t endpoint_id, struct domain **d_out,
+                                   struct ffa_ctx **ctx_out)
+{
+    struct domain *d;
+    struct ffa_ctx *ctx;
+    int err;
+
+    if ( endpoint_id == 0 )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    err = rcu_lock_live_remote_domain_by_id(endpoint_id - 1, &d);
+    if ( err )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    if ( !d->arch.tee )
+    {
+        rcu_unlock_domain(d);
+        return FFA_RET_INVALID_PARAMETERS;
+    }
+
+    ctx = d->arch.tee;
+    if ( !ACCESS_ONCE(ctx->guest_vers) )
+    {
+        rcu_unlock_domain(d);
+        return FFA_RET_INVALID_PARAMETERS;
+    }
+
+    *d_out = d;
+    if ( ctx_out )
+        *ctx_out = ctx;
+
+    return FFA_RET_OK;
+}
+
 static int ffa_domain_init(struct domain *d)
 {
     struct ffa_ctx *ctx;
xen/arch/arm/tee/ffa_msg.c +3 -21
index 4e26596461a9..10856fddcbc4 100644
@@ -161,30 +161,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     struct ffa_part_msg_rxtx_1_2 *dst_msg;
     void *rx_buf;
     size_t rx_size;
-    int err;
     int32_t ret;
 
-    if ( dst_id == 0 )
-        /* FF-A ID 0 is the hypervisor, this is not valid */
-        return FFA_RET_INVALID_PARAMETERS;
-
     /* This is also checking that dest is not src */
-    err = rcu_lock_live_remote_domain_by_id(dst_id - 1, &dst_d);
-    if ( err )
-        return FFA_RET_INVALID_PARAMETERS;
-
-    if ( dst_d->arch.tee == NULL )
-    {
-        ret = FFA_RET_INVALID_PARAMETERS;
-        goto out_unlock;
-    }
-
-    dst_ctx = dst_d->arch.tee;
-    if ( !ACCESS_ONCE(dst_ctx->guest_vers) )
-    {
-        ret = FFA_RET_INVALID_PARAMETERS;
-        goto out_unlock;
-    }
+    ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
+    if ( ret )
+        return ret;
 
     /* This also checks that destination has set a Rx buffer */
     ret = ffa_rx_acquire(dst_ctx , &rx_buf, &rx_size);
xen/arch/arm/tee/ffa_private.h +3 -1
index 282c105f3bce..cd7ecabc7eff 100644
@@ -437,6 +437,9 @@ 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);
 
+int32_t ffa_endpoint_domain_lookup(uint16_t endpoint_id, struct domain **d_out,
+                                   struct ffa_ctx **ctx_out);
+
 bool ffa_rxtx_spmc_init(void);
 void ffa_rxtx_spmc_destroy(void);
 void *ffa_rxtx_spmc_rx_acquire(void);
--

2.50.1 (Apple Git-155)

#4 lore raw

Re: [PATCH 02/12] xen/arm: ffa: Fix MEM_SHARE NS attribute handling

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 9 lines · Bertrand Marquis

The FF-A memory attribute encoding is currently a literal value (0x2f), which makes reviews and validation harder. In addition, MEM_SHARE accepts the NS (non-secure) attribute bit even though the normal world must not set it according to FF-A specification.

Introduce named attribute bit masks and express FFA_NORMAL_MEM_REG_ATTR in terms of them for clarity.

Quoted 6 lines

Reject MEM_SHARE descriptors with the NS bit set, returning INVALID_PARAMETERS to match FF-A v1.1 rules that prohibit normal world from setting this bit.

Functional impact: MEM_SHARE now rejects descriptors with NS bit set, which were previously accepted but violate FF-A specification.

To be fair, it was also rejected earlier, but with a different error code.

Quoted 57 lines
---
 xen/arch/arm/tee/ffa_private.h | 17 ++++++++++++++++-
 xen/arch/arm/tee/ffa_shm.c     |  6 ++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
xen/arch/arm/tee/ffa_private.h +16 -1
index cd7ecabc7eff..b625f1c72914 100644
@@ -129,11 +129,26 @@
 #define FFA_HANDLE_HYP_FLAG             BIT(63, ULL)
 #define FFA_HANDLE_INVALID              0xffffffffffffffffULL
 
+/* NS attribute was introduced in v1.1 */
+#define FFA_MEM_ATTR_NS                 BIT(6, U)
+
+#define FFA_MEM_ATTR_TYPE_DEV           (1U << 3)
+#define FFA_MEM_ATTR_TYPE_MEM           (2U << 4)
+
+#define FFA_MEM_ATTR_NC                 (1U << 2)
+#define FFA_MEM_ATTR_WB                 (3U << 2)
+
+#define FFA_MEM_ATTR_NON_SHARE          (0U)
+#define FFA_MEM_ATTR_OUT_SHARE          (2U)
+#define FFA_MEM_ATTR_INN_SHARE          (3U)
+
 /*
  * Memory attributes: Normal memory, Write-Back cacheable, Inner shareable
  * Defined in FF-A-1.1-REL0 Table 10.18 at page 175.
  */
-#define FFA_NORMAL_MEM_REG_ATTR         0x2fU
+#define FFA_NORMAL_MEM_REG_ATTR         (FFA_MEM_ATTR_TYPE_MEM | \
+                                         FFA_MEM_ATTR_WB | \
+                                         FFA_MEM_ATTR_INN_SHARE)
 /*
  * Memory access permissions: Read-write
  * Defined in FF-A-1.1-REL0 Table 10.15 at page 168.
xen/arch/arm/tee/ffa_shm.c +6 -1
index 8282bacf85d3..90800e44a86a 100644
@@ -512,6 +512,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     if ( ret )
         goto out_unlock;
 
+    if ( trans.mem_reg_attr & FFA_MEM_ATTR_NS )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock;
+    }
+
     if ( trans.mem_reg_attr != FFA_NORMAL_MEM_REG_ATTR )
     {
         ret = FFA_RET_NOT_SUPPORTED;
--
Quoted 2 lines

2.50.1 (Apple Git-155)

Looks good, but I think the commit message needs a small update or clarification.

Cheers,
Jens

#5 lore raw

Re: [PATCH 02/12] xen/arm: ffa: Fix MEM_SHARE NS attribute handling

Quoted 17 lines · Jens Wiklander

Hi Bertrand,

Quoted 9 lines · Bertrand Marquis

The FF-A memory attribute encoding is currently a literal value (0x2f), which makes reviews and validation harder. In addition, MEM_SHARE accepts the NS (non-secure) attribute bit even though the normal world must not set it according to FF-A specification.

Introduce named attribute bit masks and express FFA_NORMAL_MEM_REG_ATTR in terms of them for clarity.

Quoted 8 lines
Quoted 6 lines

Reject MEM_SHARE descriptors with the NS bit set, returning INVALID_PARAMETERS to match FF-A v1.1 rules that prohibit normal world from setting this bit.

Functional impact: MEM_SHARE now rejects descriptors with NS bit set, which were previously accepted but violate FF-A specification.

To be fair, it was also rejected earlier, but with a different error code.

True, will adapt the impact comment to say:

Functional impact: MEM_SHARE now rejects descriptors with NS bit set with the right error code, INVALID_PARAMETER.

Tell me if that would be ok for you and if it could be fixed on commit with your R-b if it is the case.

Quoted 48 lines
Quoted 48 lines

---
xen/arch/arm/tee/ffa_private.h | 17 ++++++++++++++++-
xen/arch/arm/tee/ffa_shm.c | 6 ++++++

2 files changed, 22 insertions(+), 1 deletion(-)
xen/arch/arm/tee/ffa_private.h +16 -1
index cd7ecabc7eff..b625f1c72914 100644
@@ -129,11 +129,26 @@
 #define FFA_HANDLE_HYP_FLAG             BIT(63, ULL)
 #define FFA_HANDLE_INVALID              0xffffffffffffffffULL
 
+/* NS attribute was introduced in v1.1 */
+#define FFA_MEM_ATTR_NS                 BIT(6, U)
+
+#define FFA_MEM_ATTR_TYPE_DEV           (1U << 3)
+#define FFA_MEM_ATTR_TYPE_MEM           (2U << 4)
+
+#define FFA_MEM_ATTR_NC                 (1U << 2)
+#define FFA_MEM_ATTR_WB                 (3U << 2)
+
+#define FFA_MEM_ATTR_NON_SHARE          (0U)
+#define FFA_MEM_ATTR_OUT_SHARE          (2U)
+#define FFA_MEM_ATTR_INN_SHARE          (3U)
+
 /*
 * Memory attributes: Normal memory, Write-Back cacheable, Inner shareable
 * Defined in FF-A-1.1-REL0 Table 10.18 at page 175.
 */
-#define FFA_NORMAL_MEM_REG_ATTR         0x2fU
+#define FFA_NORMAL_MEM_REG_ATTR         (FFA_MEM_ATTR_TYPE_MEM | \
+                                         FFA_MEM_ATTR_WB | \
+                                         FFA_MEM_ATTR_INN_SHARE)
 /*
 * Memory access permissions: Read-write
 * Defined in FF-A-1.1-REL0 Table 10.15 at page 168.
xen/arch/arm/tee/ffa_shm.c +0 -0
index 8282bacf85d3..90800e44a86a 100644
@@ -512,6 +512,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    if ( ret )
        goto out_unlock;
 
Quoted 15 lines
Quoted 10 lines
xen/arch/arm/tee/ffa_shm.c +6 -1
+    if ( trans.mem_reg_attr & FFA_MEM_ATTR_NS )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock;
+    }
+
    if ( trans.mem_reg_attr != FFA_NORMAL_MEM_REG_ATTR )
    {
        ret = FFA_RET_NOT_SUPPORTED;
--
Quoted 2 lines

2.50.1 (Apple Git-155)

Looks good, but I think the commit message needs a small update or clarification.

Thanks.

Cheers
Bertrand

Quoted 3 lines

Cheers,
Jens

Reviewed-by
#6 lore raw

Re: [PATCH 02/12] xen/arm: ffa: Fix MEM_SHARE NS attribute handling

Quoted 27 lines · Bertrand Marquis
Quoted 18 lines · Jens Wiklander

Hi Bertrand,

Quoted 9 lines · Bertrand Marquis

The FF-A memory attribute encoding is currently a literal value (0x2f), which makes reviews and validation harder. In addition, MEM_SHARE accepts the NS (non-secure) attribute bit even though the normal world must not set it according to FF-A specification.

Introduce named attribute bit masks and express FFA_NORMAL_MEM_REG_ATTR in terms of them for clarity.

Quoted 8 lines
Quoted 6 lines

Reject MEM_SHARE descriptors with the NS bit set, returning INVALID_PARAMETERS to match FF-A v1.1 rules that prohibit normal world from setting this bit.

Functional impact: MEM_SHARE now rejects descriptors with NS bit set, which were previously accepted but violate FF-A specification.

To be fair, it was also rejected earlier, but with a different error code.

Quoted 7 lines

True, will adapt the impact comment to say:

Functional impact: MEM_SHARE now rejects descriptors with NS bit set with the right error code, INVALID_PARAMETER.

Tell me if that would be ok for you and if it could be fixed on commit with your R-b if it is the case.

Sounds good. With that, please add:

Cheers,
Jens

Quoted 75 lines
Quoted 49 lines
Quoted 49 lines

---
xen/arch/arm/tee/ffa_private.h | 17 ++++++++++++++++-
xen/arch/arm/tee/ffa_shm.c | 6 ++++++

2 files changed, 22 insertions(+), 1 deletion(-)
xen/arch/arm/tee/ffa_private.h +16 -1
index cd7ecabc7eff..b625f1c72914 100644
@@ -129,11 +129,26 @@
 #define FFA_HANDLE_HYP_FLAG             BIT(63, ULL)
 #define FFA_HANDLE_INVALID              0xffffffffffffffffULL
 
+/* NS attribute was introduced in v1.1 */
+#define FFA_MEM_ATTR_NS                 BIT(6, U)
+
+#define FFA_MEM_ATTR_TYPE_DEV           (1U << 3)
+#define FFA_MEM_ATTR_TYPE_MEM           (2U << 4)
+
+#define FFA_MEM_ATTR_NC                 (1U << 2)
+#define FFA_MEM_ATTR_WB                 (3U << 2)
+
+#define FFA_MEM_ATTR_NON_SHARE          (0U)
+#define FFA_MEM_ATTR_OUT_SHARE          (2U)
+#define FFA_MEM_ATTR_INN_SHARE          (3U)
+
 /*
 * Memory attributes: Normal memory, Write-Back cacheable, Inner shareable
 * Defined in FF-A-1.1-REL0 Table 10.18 at page 175.
 */
-#define FFA_NORMAL_MEM_REG_ATTR         0x2fU
+#define FFA_NORMAL_MEM_REG_ATTR         (FFA_MEM_ATTR_TYPE_MEM | \
+                                         FFA_MEM_ATTR_WB | \
+                                         FFA_MEM_ATTR_INN_SHARE)
 /*
 * Memory access permissions: Read-write
 * Defined in FF-A-1.1-REL0 Table 10.15 at page 168.
xen/arch/arm/tee/ffa_shm.c +0 -0
index 8282bacf85d3..90800e44a86a 100644
@@ -512,6 +512,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    if ( ret )
        goto out_unlock;
 
Quoted 15 lines
Quoted 10 lines
xen/arch/arm/tee/ffa_shm.c +6 -1
+    if ( trans.mem_reg_attr & FFA_MEM_ATTR_NS )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock;
+    }
+
    if ( trans.mem_reg_attr != FFA_NORMAL_MEM_REG_ATTR )
    {
        ret = FFA_RET_NOT_SUPPORTED;
--
Quoted 2 lines

2.50.1 (Apple Git-155)

Looks good, but I think the commit message needs a small update or clarification.

Thanks.

Cheers
Bertrand

Quoted 3 lines

Cheers,
Jens

#8 lore raw

Re: [PATCH 03/12] xen/arm: ffa: Harden shm page parsing

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 67 lines · Bertrand Marquis

get_shm_pages() uses unchecked address arithmetic and does not enforce alignment, so malformed descriptors can cause overflow or slip through validation. The reclaim path also repeats handle-to-shm-mem conversion in multiple places, duplicating error handling.

Harden page parsing and reclaim handling:
- add ffa_safe_addr_add() and use it to detect address overflows
- enforce alignment checks in get_shm_pages() and return FF-A errors
- introduce ffa_secure_reclaim() and use it for MEM_RECLAIM and teardown
- simplify ffa_mem_share() argument handling and allow max page count

Functional impact: invalid or misaligned memory ranges now fail earlier with proper error codes; behavior for valid descriptors is unchanged.

---
 xen/arch/arm/tee/ffa_private.h | 11 +++++++
 xen/arch/arm/tee/ffa_shm.c     | 57 +++++++++++++++++-----------------
 2 files changed, 40 insertions(+), 28 deletions(-)
xen/arch/arm/tee/ffa_private.h +11 -0
index b625f1c72914..58562d8e733c 100644
@@ -632,4 +632,15 @@ static inline void ffa_uuid_set(struct ffa_uuid *id, uint32_t val0,
     id->val[1] = ((uint64_t)val3 << 32U) | val2;
 }
 
+/*
+ * Common overflow-safe helper to verify that adding a number of pages to an
+ * address will not wrap around.
+ */
+static inline bool ffa_safe_addr_add(uint64_t addr, uint64_t pages)
+{
+    uint64_t off = pages * FFA_PAGE_SIZE;
+
+    return (off / FFA_PAGE_SIZE) == pages && addr <= UINT64_MAX - off;
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_shm.c +4 -6
index 90800e44a86a..4c0b45cde6ee 100644
@@ -96,16 +96,14 @@ struct ffa_shm_mem {
     struct page_info *pages[];
 };
 
-static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
-                             register_t addr, uint32_t pg_count,
-                             uint64_t *handle)
+static int32_t ffa_mem_share(uint32_t tot_len, uint64_t *handle)
 {
     struct arm_smccc_1_2_regs arg = {
         .a0 = FFA_MEM_SHARE_64,
         .a1 = tot_len,
-        .a2 = frag_len,
-        .a3 = addr,
-        .a4 = pg_count,
+        .a2 = tot_len,
+        .a3 = 0,
+        .a4 = 0,
     };
     struct arm_smccc_1_2_regs resp;
 

xen/arch/arm/tee/ffa_shm.c static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len, · line 132

Quoted 6 lines
xen/arch/arm/tee/ffa_shm.c +1 -2
@@ -131,12 +129,16 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
     }
 }
 
-static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
-                               uint32_t flags)
+static int32_t ffa_secure_reclaim(struct ffa_shm_mem *shm, uint32_t flags)

I agree with moving the uint64_to_regpair() call into this function, but I'm puzzled by the new name. What's secure?

Quoted 94 lines
xen/arch/arm/tee/ffa_shm.c +21 -10
 {
+    register_t handle_hi;
+    register_t handle_lo;
+
     if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
         return FFA_RET_NOT_SUPPORTED;
 
+    uint64_to_regpair(&handle_hi, &handle_lo, shm->handle);
+
     return ffa_simple_call(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, 0);
 }
 
@@ -145,7 +147,7 @@ static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
  * this function fails then the caller is still expected to call
  * put_shm_pages() as a cleanup.
  */
-static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
+static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
                          const struct ffa_address_range *range,
                          uint32_t range_count)
 {
@@ -156,17 +158,26 @@ static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
     p2m_type_t t;
     uint64_t addr;
     uint64_t page_count;
+    uint64_t gaddr;
 
     for ( n = 0; n < range_count; n++ )
     {
         page_count = ACCESS_ONCE(range[n].page_count);
         addr = ACCESS_ONCE(range[n].address);
+
+        if ( !IS_ALIGNED(addr, FFA_PAGE_SIZE) )
+            return FFA_RET_INVALID_PARAMETERS;
+
         for ( m = 0; m < page_count; m++ )
         {
             if ( pg_idx >= shm->page_count )
                 return FFA_RET_INVALID_PARAMETERS;
 
-            gfn = gaddr_to_gfn(addr + m * FFA_PAGE_SIZE);
+            if ( !ffa_safe_addr_add(addr, m) )
+                return FFA_RET_INVALID_PARAMETERS;
+
+            gaddr = addr + m * FFA_PAGE_SIZE;
+            gfn = gaddr_to_gfn(gaddr);
             shm->pages[pg_idx] = get_page_from_gfn(d, gfn_x(gfn), &t,
                                                   P2M_ALLOC);
             if ( !shm->pages[pg_idx] )
@@ -180,7 +191,7 @@ static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
 
     /* The ranges must add up */
     if ( pg_idx < shm->page_count )
-            return FFA_RET_INVALID_PARAMETERS;
+        return FFA_RET_INVALID_PARAMETERS;
 
     return FFA_RET_OK;
 }
@@ -198,15 +209,11 @@ static void put_shm_pages(struct ffa_shm_mem *shm)
 
 static bool inc_ctx_shm_count(struct domain *d, struct ffa_ctx *ctx)
 {
-    bool ret = true;
+    bool ret = false;
 
     spin_lock(&ctx->lock);
 
-    if ( ctx->shm_count >= FFA_MAX_SHM_COUNT )
-    {
-        ret = false;
-    }
-    else
+    if ( ctx->shm_count < FFA_MAX_SHM_COUNT )
     {
         /*
          * If this is the first shm added, increase the domain reference
@@ -217,6 +224,7 @@ static bool inc_ctx_shm_count(struct domain *d, struct ffa_ctx *ctx)
             get_knownalive_domain(d);
 
         ctx->shm_count++;
+        ret = true;
     }
 
     spin_unlock(&ctx->lock);
@@ -251,7 +259,7 @@ static struct ffa_shm_mem *alloc_ffa_shm_mem(struct domain *d,
     struct ffa_ctx *ctx = d->arch.tee;
     struct ffa_shm_mem *shm;
 
-    if ( page_count >= FFA_MAX_SHM_PAGE_COUNT )
+    if ( page_count > FFA_MAX_SHM_PAGE_COUNT )
         return NULL;
     if ( !inc_ctx_shm_count(d, ctx) )
         return NULL;

xen/arch/arm/tee/ffa_shm.c static int share_shm(struct ffa_shm_mem *shm) · line 370

Quoted 4 lines
xen/arch/arm/tee/ffa_shm.c +0 -1
@@ -367,7 +375,7 @@ static int share_shm(struct ffa_shm_mem *shm)
         init_range(addr_range, pa);
     }
 
-    ret = ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);

Please remove frag_len from share_shm() since it's not needed any longer.

Cheers,
Jens

Quoted 39 lines
xen/arch/arm/tee/ffa_shm.c +3 -10
+    ret = ffa_mem_share(tot_len, &shm->handle);
 
 out:
     ffa_rxtx_spmc_tx_release();
@@ -637,8 +645,6 @@ int32_t ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
     struct ffa_shm_mem *shm;
-    register_t handle_hi;
-    register_t handle_lo;
     int32_t ret;
 
     if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
@@ -652,8 +658,7 @@ int32_t ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
     if ( !shm )
         return FFA_RET_INVALID_PARAMETERS;
 
-    uint64_to_regpair(&handle_hi, &handle_lo, handle);
-    ret = ffa_mem_reclaim(handle_lo, handle_hi, flags);
+    ret = ffa_secure_reclaim(shm, flags);
 
     if ( ret )
     {
@@ -677,11 +682,7 @@ bool ffa_shm_domain_destroy(struct domain *d)
 
     list_for_each_entry_safe(shm, tmp, &ctx->shm_list, list)
     {
-        register_t handle_hi;
-        register_t handle_lo;
-
-        uint64_to_regpair(&handle_hi, &handle_lo, shm->handle);
-        res = ffa_mem_reclaim(handle_lo, handle_hi, 0);
+        res = ffa_secure_reclaim(shm, 0);
         switch ( res ) {
         case FFA_RET_OK:
             printk(XENLOG_G_DEBUG "%pd: ffa: Reclaimed handle %#lx\n",
--

2.50.1 (Apple Git-155)

#13 lore raw

Re: [PATCH 04/12] xen/arm: ffa: Add FF-A 1.2 endpoint memory access descriptors

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 125 lines · Bertrand Marquis

FF-A 1.2 extends the endpoint memory access descriptor (EMAD) from 16 to 32 bytes, adding implementation-defined (IMPDEF) fields and reserved space. The MEM_SHARE path currently assumes the 1.1 EMAD size and rejects the 1.2 layout.

Add FF-A 1.2 EMAD support to MEM_SHARE:
- define ffa_mem_access_1_2 and store IMPDEF payload in ffa_shm_mem
- emit 1.2 EMADs to the SPMC for FF-A 1.2 guests, forwarding IMPDEF
- refactor header parsing into read_mem_transaction() for 1.0/1.1+
- detect EMAD format by mem_access_size to allow 1.1 on 1.2 guests

Functional impact: MEM_SHARE supports FF-A 1.2 EMADs.

---
 xen/arch/arm/tee/ffa_shm.c | 108 +++++++++++++++++++++++++++++--------
 1 file changed, 86 insertions(+), 22 deletions(-)
xen/arch/arm/tee/ffa_shm.c +44 -11
index 4c0b45cde6ee..905a64e3db01 100644
@@ -30,6 +30,14 @@ struct ffa_mem_access {
     uint64_t reserved;
 };
 
+/* Endpoint memory access descriptor (FF-A 1.2) */
+struct ffa_mem_access_1_2 {
+    struct ffa_mem_access_perm access_perm;
+    uint32_t region_offs;
+    uint8_t impdef[16];
+    uint8_t reserved[8];
+};
+
 /* Lend, donate or share memory transaction descriptor */
 struct ffa_mem_transaction_1_0 {
     uint16_t sender_id;
@@ -73,7 +81,7 @@ struct ffa_mem_transaction_1_1 {
 /*
  * The parts needed from struct ffa_mem_transaction_1_0 or struct
  * ffa_mem_transaction_1_1, used to provide an abstraction of difference in
- * data structures between version 1.0 and 1.1. This is just an internal
+ * data structures between version 1.0 and 1.2. This is just an internal
  * interface and can be changed without changing any ABI.
  */
 struct ffa_mem_transaction_int {
@@ -92,6 +100,8 @@ struct ffa_shm_mem {
     uint16_t sender_id;
     uint16_t ep_id;     /* endpoint, the one lending */
     uint64_t handle;    /* FFA_HANDLE_INVALID if not set yet */
+    /* Endpoint memory access descriptor IMPDEF value (FF-A 1.2). */
+    uint64_t impdef[2];
     unsigned int page_count;
     struct page_info *pages[];
 };
@@ -297,17 +307,21 @@ static void init_range(struct ffa_address_range *addr_range,
  * This function uses the ffa_spmc tx buffer to transmit the memory transaction
  * descriptor.
  */
-static int share_shm(struct ffa_shm_mem *shm)
+static int share_shm(struct ffa_shm_mem *shm, uint32_t ffa_vers)
 {
     const uint32_t max_frag_len = FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE;
     struct ffa_mem_access *mem_access_array;
+    struct ffa_mem_access_1_2 *mem_access_array_1_2;
     struct ffa_mem_transaction_1_1 *descr;
     struct ffa_address_range *addr_range;
     struct ffa_mem_region *region_descr;
-    const unsigned int region_count = 1;
     uint32_t frag_len;
     uint32_t tot_len;
+    uint32_t mem_access_size;
+    uint32_t mem_access_offs;
+    uint32_t region_offs;
     paddr_t last_pa;
+    uint32_t range_count;
     unsigned int n;
     paddr_t pa;
     int32_t ret;
@@ -326,16 +340,35 @@ static int share_shm(struct ffa_shm_mem *shm)
     descr->handle = shm->handle;
     descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
     descr->mem_access_count = 1;
-    descr->mem_access_size = sizeof(*mem_access_array);
-    descr->mem_access_offs = MEM_ACCESS_OFFSET(0);
+    if ( ffa_vers >= FFA_VERSION_1_2 )
+        mem_access_size = sizeof(struct ffa_mem_access_1_2);
+    else
+        mem_access_size = sizeof(struct ffa_mem_access);
+    mem_access_offs = sizeof(struct ffa_mem_transaction_1_1);
+    region_offs = mem_access_offs + mem_access_size;
+    descr->mem_access_size = mem_access_size;
+    descr->mem_access_offs = mem_access_offs;
 
-    mem_access_array = buf + descr->mem_access_offs;
-    memset(mem_access_array, 0, sizeof(*mem_access_array));
-    mem_access_array[0].access_perm.endpoint_id = shm->ep_id;
-    mem_access_array[0].access_perm.perm = FFA_MEM_ACC_RW;
-    mem_access_array[0].region_offs = REGION_OFFSET(descr->mem_access_count, 0);
+    if ( ffa_vers >= FFA_VERSION_1_2 )
+    {
+        mem_access_array_1_2 = buf + mem_access_offs;
+        memset(mem_access_array_1_2, 0, sizeof(*mem_access_array_1_2));
+        mem_access_array_1_2[0].access_perm.endpoint_id = shm->ep_id;
+        mem_access_array_1_2[0].access_perm.perm = FFA_MEM_ACC_RW;
+        mem_access_array_1_2[0].region_offs = region_offs;
+        memcpy(mem_access_array_1_2[0].impdef, shm->impdef,
+               sizeof(mem_access_array_1_2[0].impdef));
+    }
+    else
+    {
+        mem_access_array = buf + mem_access_offs;
+        memset(mem_access_array, 0, sizeof(*mem_access_array));
+        mem_access_array[0].access_perm.endpoint_id = shm->ep_id;
+        mem_access_array[0].access_perm.perm = FFA_MEM_ACC_RW;
+        mem_access_array[0].region_offs = region_offs;
+    }
 
-    region_descr = buf + mem_access_array[0].region_offs;
+    region_descr = buf + region_offs;
     memset(region_descr, 0, sizeof(*region_descr));
     region_descr->total_page_count = shm->page_count;
 

xen/arch/arm/tee/ffa_shm.c static int share_shm(struct ffa_shm_mem *shm) · line 353

Quoted 5 lines
xen/arch/arm/tee/ffa_shm.c +0 -2
@@ -349,8 +382,9 @@ static int share_shm(struct ffa_shm_mem *shm)
         region_descr->address_range_count++;
     }
 
-    tot_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count,
-                                region_descr->address_range_count);

Please remove the unused ADDR_RANGE_OFFSET() macro and friends, as they're no longer accurate.

Quoted 16 lines
xen/arch/arm/tee/ffa_shm.c +4 -1
+    range_count = region_descr->address_range_count;
+    tot_len = region_offs + sizeof(*region_descr) +
+              range_count * sizeof(struct ffa_address_range);
     if ( tot_len > max_frag_len )
     {
         ret = FFA_RET_NOT_SUPPORTED;
@@ -358,7 +392,7 @@ static int share_shm(struct ffa_shm_mem *shm)
     }
 
     addr_range = region_descr->address_range_array;
-    frag_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count, 1);
+    frag_len = region_offs + sizeof(*region_descr) + sizeof(*addr_range);
     last_pa = page_to_maddr(shm->pages[0]);
     init_range(addr_range, last_pa);
     for ( n = 1; n < shm->page_count; last_pa = pa, n++ )

xen/arch/arm/tee/ffa_shm.c static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen, · line 486

Quoted 5 lines
xen/arch/arm/tee/ffa_shm.c +2 -0
@@ -448,6 +482,12 @@ static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen,
     if ( size * count + offs > blen )
         return FFA_RET_INVALID_PARAMETERS;
 
+    if ( size < sizeof(struct ffa_mem_access) )
+        return FFA_RET_INVALID_PARAMETERS;

Implicitly, size should also be a multiple of 16. Don't you agree?

Quoted 101 lines
xen/arch/arm/tee/ffa_shm.c +35 -7
+
+    if ( offs & 0xF )
+        return FFA_RET_INVALID_PARAMETERS;
+
     trans->mem_reg_attr = mem_reg_attr;
     trans->flags = flags;
     trans->mem_access_size = size;
@@ -464,7 +504,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     uint64_t addr = get_user_reg(regs, 3);
     uint32_t page_count = get_user_reg(regs, 4);
     const struct ffa_mem_region *region_descr;
-    const struct ffa_mem_access *mem_access;
+    const struct ffa_mem_access_1_2 *mem_access;
     struct ffa_mem_transaction_int trans;
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
@@ -474,9 +514,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     register_t handle_hi = 0;
     register_t handle_lo = 0;
     int ret = FFA_RET_DENIED;
+    uint32_t ffa_vers;
     uint32_t range_count;
     uint32_t region_offs;
     uint16_t dst_id;
+    uint8_t perm;
+    uint64_t impdef[2];
 
     if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
     {
@@ -515,8 +558,8 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     if ( frag_len > tx_size )
         goto out_unlock;
 
-    ret = read_mem_transaction(ACCESS_ONCE(ctx->guest_vers), tx_buf,
-                               frag_len, &trans);
+    ffa_vers = ACCESS_ONCE(ctx->guest_vers);
+    ret = read_mem_transaction(ffa_vers, tx_buf, frag_len, &trans);
     if ( ret )
         goto out_unlock;
 
@@ -545,13 +588,35 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
     }
 
+    if ( trans.mem_access_size < sizeof(struct ffa_mem_access) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock;
+    }
+
     /* Check that it fits in the supplied data */
     if ( trans.mem_access_offs + trans.mem_access_size > frag_len )
         goto out_unlock;
 
     mem_access = tx_buf + trans.mem_access_offs;
-
     dst_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    perm = ACCESS_ONCE(mem_access->access_perm.perm);
+    region_offs = ACCESS_ONCE(mem_access->region_offs);
+
+    /*
+     * FF-A 1.2 introduced an extended mem_access descriptor with impdef
+     * fields, but guests can still use the 1.1 format if they don't need
+     * implementation-defined data. Detect which format is used based on
+     * the mem_access_size field rather than the negotiated FF-A version.
+     */
+    if ( trans.mem_access_size >= sizeof(struct ffa_mem_access_1_2) )
+        memcpy(impdef, mem_access->impdef, sizeof(impdef));
+    else
+    {
+        impdef[0] = 0;
+        impdef[1] = 0;
+    }
+
     if ( !FFA_ID_IS_SECURE(dst_id) )
     {
         /* we do not support sharing with VMs */
@@ -559,13 +624,11 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
     }
 
-    if ( ACCESS_ONCE(mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
+    if ( perm != FFA_MEM_ACC_RW )
     {
         ret = FFA_RET_NOT_SUPPORTED;
         goto out_unlock;
     }
-
-    region_offs = ACCESS_ONCE(mem_access->region_offs);
     if ( sizeof(*region_descr) + region_offs > frag_len )
     {
         ret = FFA_RET_NOT_SUPPORTED;
@@ -590,6 +653,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     }
     shm->sender_id = trans.sender_id;
     shm->ep_id = dst_id;
+    memcpy(shm->impdef, impdef, sizeof(shm->impdef));
 
     /*
      * Check that the Composite memory region descriptor fits.

xen/arch/arm/tee/ffa_shm.c void ffa_handle_mem_share(struct cpu_user_regs *regs) · line 672

Quoted 5 lines
xen/arch/arm/tee/ffa_shm.c +1 -1
@@ -605,7 +669,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     if ( ret )
         goto out;
 
-    ret = share_shm(shm);
+    ret = share_shm(shm, ffa_vers);

Shouldn't we rather use ffa_fw_version?

Cheers,
Jens

Quoted 6 lines
xen/arch/arm/tee/ffa_shm.c +0 -1
     if ( ret )
         goto out;
 
--

2.50.1 (Apple Git-155)

Reviewed-by on v1 5/12
#17 lore raw

Re: [PATCH 05/12] xen/arm: ffa: Fix PARTINFO RX release errors

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 31 lines · Bertrand Marquis

On PARTITION_INFO_GET error paths, Xen unconditionally called FFA_RX_RELEASE for the SPMC RX buffer. If the SPMC didn't grant RX ownership (i.e., the call failed early), this issues a spurious release that returns DENIED and produces warnings.

Modify ffa_rxtx_spmc_rx_release() to return the release status and let callers choose whether to log it. Only issue FFA_RX_RELEASE after a successful PARTINFO SMC, while always releasing the local RX lock to avoid deadlocks.

Update handle_partition_info_get() to only release the SPMC RX buffer after successful fw_ret checks, and ignore release errors during the error path.

Functional impact: eliminates spurious FFA_RX_RELEASE calls and associated DENIED warnings when PARTITION_INFO_GET fails before obtaining SPMC RX buffer ownership.

---
 xen/arch/arm/tee/ffa_partinfo.c | 14 ++++++++++++--
 xen/arch/arm/tee/ffa_private.h  |  2 +-
 xen/arch/arm/tee/ffa_rxtx.c     | 14 +++++++++-----
 3 files changed, 22 insertions(+), 8 deletions(-)
xen/arch/arm/tee/ffa_partinfo.c +0 -0
index bf906ed0c88f..6b01c4abe915 100644

xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count, · line 99

Quoted 8 lines
xen/arch/arm/tee/ffa_partinfo.c +2 -0
@@ -92,9 +92,11 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
                                    uint32_t dst_size)
 {
     int32_t ret;
+    int32_t release_ret;
     uint32_t src_size, real_sp_count;
     void *src_buf;
     uint32_t count = 0;
+    bool spmc_ok = false;

Wouldn't notify_fw be clearer, and the same in ffa_partinfo_init()?

Either way, please add:

Cheers,
Jens

Quoted 98 lines
xen/arch/arm/tee/ffa_partinfo.c +10 -2
     /* We need to use the RX buffer to receive the list */
     src_buf = ffa_rxtx_spmc_rx_acquire();
@@ -104,6 +106,7 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
     ret = ffa_partition_info_get(uuid, 0, &real_sp_count, &src_size);
     if ( ret )
         goto out;
+    spmc_ok = true;
 
     /* Validate the src_size we got */
     if ( src_size < sizeof(struct ffa_partition_info_1_0) ||
@@ -157,7 +160,10 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
     *sp_count = count;
 
 out:
-    ffa_rxtx_spmc_rx_release();
+    release_ret = ffa_rxtx_spmc_rx_release(spmc_ok);
+    if ( release_ret )
+        gprintk(XENLOG_WARNING,
+                "ffa: Error releasing SPMC RX buffer: %d\n", release_ret);
     return ret;
 }
 
@@ -507,6 +513,7 @@ bool ffa_partinfo_init(void)
     int32_t e;
     void *spmc_rx;
     struct ffa_uuid nil_uuid = { .val = { 0ULL, 0ULL } };
+    bool spmc_ok = false;
 
     if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
          !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32))
@@ -522,6 +529,7 @@ bool ffa_partinfo_init(void)
         printk(XENLOG_ERR "ffa: Failed to get list of SPs: %d\n", e);
         goto out;
     }
+    spmc_ok = true;
 
     if ( count >= FFA_MAX_NUM_SP )
     {
@@ -533,7 +541,9 @@ bool ffa_partinfo_init(void)
     ret = init_subscribers(spmc_rx, count, fpi_size);
 
 out:
-    ffa_rxtx_spmc_rx_release();
+    e = ffa_rxtx_spmc_rx_release(spmc_ok);
+    if ( e )
+        printk(XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n", e);
     return ret;
 }
 
xen/arch/arm/tee/ffa_private.h +1 -1
index 58562d8e733c..461e87f6f9c4 100644
@@ -458,7 +458,7 @@ int32_t ffa_endpoint_domain_lookup(uint16_t endpoint_id, struct domain **d_out,
 bool ffa_rxtx_spmc_init(void);
 void ffa_rxtx_spmc_destroy(void);
 void *ffa_rxtx_spmc_rx_acquire(void);
-void ffa_rxtx_spmc_rx_release(void);
+int32_t ffa_rxtx_spmc_rx_release(bool notify_fw);
 void *ffa_rxtx_spmc_tx_acquire(void);
 void ffa_rxtx_spmc_tx_release(void);
 
xen/arch/arm/tee/ffa_rxtx.c +9 -6
index 7d8bb4f4d031..50758fb57cdf 100644
@@ -375,18 +375,22 @@ void *ffa_rxtx_spmc_rx_acquire(void)
     return NULL;
 }
 
-void ffa_rxtx_spmc_rx_release(void)
+int32_t ffa_rxtx_spmc_rx_release(bool notify_fw)
 {
     int32_t ret;
 
     ASSERT(spin_is_locked(&ffa_spmc_rx_lock));
 
-    /* Inform the SPMC that we are done with our RX buffer */
-    ret = ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
-    if ( ret != FFA_RET_OK )
-        printk(XENLOG_DEBUG "Error releasing SPMC RX buffer: %d\n", ret);
+    if ( notify_fw )
+    {
+        /* Inform the SPMC that we are done with our RX buffer */
+        ret = ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
+    }
+    else
+        ret = FFA_RET_OK;
 
     spin_unlock(&ffa_spmc_rx_lock);
+    return ret;
 }
 
 void *ffa_rxtx_spmc_tx_acquire(void)
--

2.50.1 (Apple Git-155)

#9 lore raw

Re: [PATCH 03/12] xen/arm: ffa: Harden shm page parsing

In reply to Jens Wiklander

Hi Jens,

Quoted 76 lines · Jens Wiklander

Hi Bertrand,

Quoted 70 lines · Bertrand Marquis

get_shm_pages() uses unchecked address arithmetic and does not enforce alignment, so malformed descriptors can cause overflow or slip through validation. The reclaim path also repeats handle-to-shm-mem conversion in multiple places, duplicating error handling.

Harden page parsing and reclaim handling:
- add ffa_safe_addr_add() and use it to detect address overflows
- enforce alignment checks in get_shm_pages() and return FF-A errors
- introduce ffa_secure_reclaim() and use it for MEM_RECLAIM and teardown
- simplify ffa_mem_share() argument handling and allow max page count

Functional impact: invalid or misaligned memory ranges now fail earlier with proper error codes; behavior for valid descriptors is unchanged.

---
xen/arch/arm/tee/ffa_private.h | 11 +++++++
xen/arch/arm/tee/ffa_shm.c | 57 +++++++++++++++++-----------------

2 files changed, 40 insertions(+), 28 deletions(-)
xen/arch/arm/tee/ffa_private.h +11 -0
index b625f1c72914..58562d8e733c 100644
@@ -632,4 +632,15 @@ static inline void ffa_uuid_set(struct ffa_uuid *id, uint32_t val0,
    id->val[1] = ((uint64_t)val3 << 32U) | val2;
 }
 
+/*
+ * Common overflow-safe helper to verify that adding a number of pages to an
+ * address will not wrap around.
+ */
+static inline bool ffa_safe_addr_add(uint64_t addr, uint64_t pages)
+{
+    uint64_t off = pages * FFA_PAGE_SIZE;
+
+    return (off / FFA_PAGE_SIZE) == pages && addr <= UINT64_MAX - off;
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_shm.c +4 -6
index 90800e44a86a..4c0b45cde6ee 100644
@@ -96,16 +96,14 @@ struct ffa_shm_mem {
    struct page_info *pages[];
 };
 
-static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
-                             register_t addr, uint32_t pg_count,
-                             uint64_t *handle)
+static int32_t ffa_mem_share(uint32_t tot_len, uint64_t *handle)
 {
    struct arm_smccc_1_2_regs arg = {
        .a0 = FFA_MEM_SHARE_64,
        .a1 = tot_len,
-        .a2 = frag_len,
-        .a3 = addr,
-        .a4 = pg_count,
+        .a2 = tot_len,
+        .a3 = 0,
+        .a4 = 0,
    };
    struct arm_smccc_1_2_regs resp;
 
@@ -131,12 +129,16 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
    }
 }
 
Quoted 6 lines

xen/arch/arm/tee/ffa_shm.c static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len, · line 132

Quoted 3 lines
xen/arch/arm/tee/ffa_shm.c +1 -2
-static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
-                               uint32_t flags)
+static int32_t ffa_secure_reclaim(struct ffa_shm_mem *shm, uint32_t flags)

I agree with moving the uint64_to_regpair() call into this function, but I'm puzzled by the new name. What's secure?

This is to distinguish with reclaim for VM to VM sharing in the future as here reclaim is asked to the secure world.

But in fact to be coherent I should also have renamed ffa_mem_share to ffa_secure_share.

Would you be ok with that ?

Quoted 98 lines
Quoted 98 lines
xen/arch/arm/tee/ffa_shm.c +21 -10
 {
+    register_t handle_hi;
+    register_t handle_lo;
+
    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
        return FFA_RET_NOT_SUPPORTED;
 
+    uint64_to_regpair(&handle_hi, &handle_lo, shm->handle);
+
    return ffa_simple_call(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, 0);
 }
 
@@ -145,7 +147,7 @@ static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
 * this function fails then the caller is still expected to call
 * put_shm_pages() as a cleanup.
 */
-static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
+static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
                         const struct ffa_address_range *range,
                         uint32_t range_count)
 {
@@ -156,17 +158,26 @@ static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
    p2m_type_t t;
    uint64_t addr;
    uint64_t page_count;
+    uint64_t gaddr;
 
    for ( n = 0; n < range_count; n++ )
    {
        page_count = ACCESS_ONCE(range[n].page_count);
        addr = ACCESS_ONCE(range[n].address);
+
+        if ( !IS_ALIGNED(addr, FFA_PAGE_SIZE) )
+            return FFA_RET_INVALID_PARAMETERS;
+
        for ( m = 0; m < page_count; m++ )
        {
            if ( pg_idx >= shm->page_count )
                return FFA_RET_INVALID_PARAMETERS;
 
-            gfn = gaddr_to_gfn(addr + m * FFA_PAGE_SIZE);
+            if ( !ffa_safe_addr_add(addr, m) )
+                return FFA_RET_INVALID_PARAMETERS;
+
+            gaddr = addr + m * FFA_PAGE_SIZE;
+            gfn = gaddr_to_gfn(gaddr);
            shm->pages[pg_idx] = get_page_from_gfn(d, gfn_x(gfn), &t,
                                                  P2M_ALLOC);
            if ( !shm->pages[pg_idx] )
@@ -180,7 +191,7 @@ static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
 
    /* The ranges must add up */
    if ( pg_idx < shm->page_count )
-            return FFA_RET_INVALID_PARAMETERS;
+        return FFA_RET_INVALID_PARAMETERS;
 
    return FFA_RET_OK;
 }
@@ -198,15 +209,11 @@ static void put_shm_pages(struct ffa_shm_mem *shm)
 
 static bool inc_ctx_shm_count(struct domain *d, struct ffa_ctx *ctx)
 {
-    bool ret = true;
+    bool ret = false;
 
    spin_lock(&ctx->lock);
 
-    if ( ctx->shm_count >= FFA_MAX_SHM_COUNT )
-    {
-        ret = false;
-    }
-    else
+    if ( ctx->shm_count < FFA_MAX_SHM_COUNT )
    {
        /*
         * If this is the first shm added, increase the domain reference
@@ -217,6 +224,7 @@ static bool inc_ctx_shm_count(struct domain *d, struct ffa_ctx *ctx)
            get_knownalive_domain(d);
 
        ctx->shm_count++;
+        ret = true;
    }
 
    spin_unlock(&ctx->lock);
@@ -251,7 +259,7 @@ static struct ffa_shm_mem *alloc_ffa_shm_mem(struct domain *d,
    struct ffa_ctx *ctx = d->arch.tee;
    struct ffa_shm_mem *shm;
 
-    if ( page_count >= FFA_MAX_SHM_PAGE_COUNT )
+    if ( page_count > FFA_MAX_SHM_PAGE_COUNT )
        return NULL;
    if ( !inc_ctx_shm_count(d, ctx) )
        return NULL;
@@ -367,7 +375,7 @@ static int share_shm(struct ffa_shm_mem *shm)
        init_range(addr_range, pa);
    }
 
Quoted 3 lines

xen/arch/arm/tee/ffa_shm.c static int share_shm(struct ffa_shm_mem *shm) · line 370

Quoted 1 line
xen/arch/arm/tee/ffa_shm.c +0 -1
-    ret = ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);

Please remove frag_len from share_shm() since it's not needed any longer.

Ack, I will remove it in v2.

Cheers
Bertrand

Quoted 42 lines

Cheers,
Jens

Quoted 38 lines
xen/arch/arm/tee/ffa_shm.c +1 -0
+    ret = ffa_mem_share(tot_len, &shm->handle);
 

out:

    ffa_rxtx_spmc_tx_release();
xen/arch/arm/tee/ffa_shm.c +2 -10
@@ -637,8 +645,6 @@ int32_t ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
    struct ffa_shm_mem *shm;
-    register_t handle_hi;
-    register_t handle_lo;
    int32_t ret;
 
    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
@@ -652,8 +658,7 @@ int32_t ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
    if ( !shm )
        return FFA_RET_INVALID_PARAMETERS;
 
-    uint64_to_regpair(&handle_hi, &handle_lo, handle);
-    ret = ffa_mem_reclaim(handle_lo, handle_hi, flags);
+    ret = ffa_secure_reclaim(shm, flags);
 
    if ( ret )
    {
@@ -677,11 +682,7 @@ bool ffa_shm_domain_destroy(struct domain *d)
 
    list_for_each_entry_safe(shm, tmp, &ctx->shm_list, list)
    {
-        register_t handle_hi;
-        register_t handle_lo;
-
-        uint64_to_regpair(&handle_hi, &handle_lo, shm->handle);
-        res = ffa_mem_reclaim(handle_lo, handle_hi, 0);
+        res = ffa_secure_reclaim(shm, 0);
        switch ( res ) {
        case FFA_RET_OK:
            printk(XENLOG_G_DEBUG "%pd: ffa: Reclaimed handle %#lx\n",
--

2.50.1 (Apple Git-155)

#14 lore raw

Re: [PATCH 04/12] xen/arm: ffa: Add FF-A 1.2 endpoint memory access descriptors

In reply to Jens Wiklander

Hi Jens,

Quoted 134 lines · Jens Wiklander

Hi Bertrand,

Quoted 128 lines · Bertrand Marquis

FF-A 1.2 extends the endpoint memory access descriptor (EMAD) from 16 to 32 bytes, adding implementation-defined (IMPDEF) fields and reserved space. The MEM_SHARE path currently assumes the 1.1 EMAD size and rejects the 1.2 layout.

Add FF-A 1.2 EMAD support to MEM_SHARE:
- define ffa_mem_access_1_2 and store IMPDEF payload in ffa_shm_mem
- emit 1.2 EMADs to the SPMC for FF-A 1.2 guests, forwarding IMPDEF
- refactor header parsing into read_mem_transaction() for 1.0/1.1+
- detect EMAD format by mem_access_size to allow 1.1 on 1.2 guests

Functional impact: MEM_SHARE supports FF-A 1.2 EMADs.

---
xen/arch/arm/tee/ffa_shm.c | 108 +++++++++++++++++++++++++++++--------

1 file changed, 86 insertions(+), 22 deletions(-)
xen/arch/arm/tee/ffa_shm.c +44 -11
index 4c0b45cde6ee..905a64e3db01 100644
@@ -30,6 +30,14 @@ struct ffa_mem_access {
    uint64_t reserved;
 };
 
+/* Endpoint memory access descriptor (FF-A 1.2) */
+struct ffa_mem_access_1_2 {
+    struct ffa_mem_access_perm access_perm;
+    uint32_t region_offs;
+    uint8_t impdef[16];
+    uint8_t reserved[8];
+};
+
 /* Lend, donate or share memory transaction descriptor */
 struct ffa_mem_transaction_1_0 {
    uint16_t sender_id;
@@ -73,7 +81,7 @@ struct ffa_mem_transaction_1_1 {
 /*
 * The parts needed from struct ffa_mem_transaction_1_0 or struct
 * ffa_mem_transaction_1_1, used to provide an abstraction of difference in
- * data structures between version 1.0 and 1.1. This is just an internal
+ * data structures between version 1.0 and 1.2. This is just an internal
 * interface and can be changed without changing any ABI.
 */
 struct ffa_mem_transaction_int {
@@ -92,6 +100,8 @@ struct ffa_shm_mem {
    uint16_t sender_id;
    uint16_t ep_id;     /* endpoint, the one lending */
    uint64_t handle;    /* FFA_HANDLE_INVALID if not set yet */
+    /* Endpoint memory access descriptor IMPDEF value (FF-A 1.2). */
+    uint64_t impdef[2];
    unsigned int page_count;
    struct page_info *pages[];
 };
@@ -297,17 +307,21 @@ static void init_range(struct ffa_address_range *addr_range,
 * This function uses the ffa_spmc tx buffer to transmit the memory transaction
 * descriptor.
 */
-static int share_shm(struct ffa_shm_mem *shm)
+static int share_shm(struct ffa_shm_mem *shm, uint32_t ffa_vers)
 {
    const uint32_t max_frag_len = FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE;
    struct ffa_mem_access *mem_access_array;
+    struct ffa_mem_access_1_2 *mem_access_array_1_2;
    struct ffa_mem_transaction_1_1 *descr;
    struct ffa_address_range *addr_range;
    struct ffa_mem_region *region_descr;
-    const unsigned int region_count = 1;
    uint32_t frag_len;
    uint32_t tot_len;
+    uint32_t mem_access_size;
+    uint32_t mem_access_offs;
+    uint32_t region_offs;
    paddr_t last_pa;
+    uint32_t range_count;
    unsigned int n;
    paddr_t pa;
    int32_t ret;
@@ -326,16 +340,35 @@ static int share_shm(struct ffa_shm_mem *shm)
    descr->handle = shm->handle;
    descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
    descr->mem_access_count = 1;
-    descr->mem_access_size = sizeof(*mem_access_array);
-    descr->mem_access_offs = MEM_ACCESS_OFFSET(0);
+    if ( ffa_vers >= FFA_VERSION_1_2 )
+        mem_access_size = sizeof(struct ffa_mem_access_1_2);
+    else
+        mem_access_size = sizeof(struct ffa_mem_access);
+    mem_access_offs = sizeof(struct ffa_mem_transaction_1_1);
+    region_offs = mem_access_offs + mem_access_size;
+    descr->mem_access_size = mem_access_size;
+    descr->mem_access_offs = mem_access_offs;
 
-    mem_access_array = buf + descr->mem_access_offs;
-    memset(mem_access_array, 0, sizeof(*mem_access_array));
-    mem_access_array[0].access_perm.endpoint_id = shm->ep_id;
-    mem_access_array[0].access_perm.perm = FFA_MEM_ACC_RW;
-    mem_access_array[0].region_offs = REGION_OFFSET(descr->mem_access_count, 0);
+    if ( ffa_vers >= FFA_VERSION_1_2 )
+    {
+        mem_access_array_1_2 = buf + mem_access_offs;
+        memset(mem_access_array_1_2, 0, sizeof(*mem_access_array_1_2));
+        mem_access_array_1_2[0].access_perm.endpoint_id = shm->ep_id;
+        mem_access_array_1_2[0].access_perm.perm = FFA_MEM_ACC_RW;
+        mem_access_array_1_2[0].region_offs = region_offs;
+        memcpy(mem_access_array_1_2[0].impdef, shm->impdef,
+               sizeof(mem_access_array_1_2[0].impdef));
+    }
+    else
+    {
+        mem_access_array = buf + mem_access_offs;
+        memset(mem_access_array, 0, sizeof(*mem_access_array));
+        mem_access_array[0].access_perm.endpoint_id = shm->ep_id;
+        mem_access_array[0].access_perm.perm = FFA_MEM_ACC_RW;
+        mem_access_array[0].region_offs = region_offs;
+    }
 
-    region_descr = buf + mem_access_array[0].region_offs;
+    region_descr = buf + region_offs;
    memset(region_descr, 0, sizeof(*region_descr));
    region_descr->total_page_count = shm->page_count;
 
@@ -349,8 +382,9 @@ static int share_shm(struct ffa_shm_mem *shm)
        region_descr->address_range_count++;
    }
 
Quoted 5 lines

xen/arch/arm/tee/ffa_shm.c static int share_shm(struct ffa_shm_mem *shm) · line 353

Quoted 2 lines
xen/arch/arm/tee/ffa_shm.c +0 -2
-    tot_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count,
-                                region_descr->address_range_count);

Please remove the unused ADDR_RANGE_OFFSET() macro and friends, as they're no longer accurate.

Very true, will remove them in v2.

Quoted 20 lines
Quoted 20 lines
xen/arch/arm/tee/ffa_shm.c +4 -1
+    range_count = region_descr->address_range_count;
+    tot_len = region_offs + sizeof(*region_descr) +
+              range_count * sizeof(struct ffa_address_range);
    if ( tot_len > max_frag_len )
    {
        ret = FFA_RET_NOT_SUPPORTED;
@@ -358,7 +392,7 @@ static int share_shm(struct ffa_shm_mem *shm)
    }
 
    addr_range = region_descr->address_range_array;
-    frag_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count, 1);
+    frag_len = region_offs + sizeof(*region_descr) + sizeof(*addr_range);
    last_pa = page_to_maddr(shm->pages[0]);
    init_range(addr_range, last_pa);
    for ( n = 1; n < shm->page_count; last_pa = pa, n++ )
@@ -448,6 +482,12 @@ static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen,
    if ( size * count + offs > blen )
        return FFA_RET_INVALID_PARAMETERS;
 
Quoted 4 lines

xen/arch/arm/tee/ffa_shm.c static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen, · line 486

Quoted 2 lines
xen/arch/arm/tee/ffa_shm.c +2 -0
+    if ( size < sizeof(struct ffa_mem_access) )
+        return FFA_RET_INVALID_PARAMETERS;

Implicitly, size should also be a multiple of 16. Don't you agree?

The spec is giving some constraints on the offset but there is nothing explicit for the per mem access size. As we have no 64bit fields in it, I am not really seeing any implicit multiple of 16.

I am checking the offset because it is enforced by the spec but for the size i would rather not put something as who knows what might be added in the future.

Quoted 105 lines
Quoted 105 lines
xen/arch/arm/tee/ffa_shm.c +35 -7
+
+    if ( offs & 0xF )
+        return FFA_RET_INVALID_PARAMETERS;
+
    trans->mem_reg_attr = mem_reg_attr;
    trans->flags = flags;
    trans->mem_access_size = size;
@@ -464,7 +504,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    uint64_t addr = get_user_reg(regs, 3);
    uint32_t page_count = get_user_reg(regs, 4);
    const struct ffa_mem_region *region_descr;
-    const struct ffa_mem_access *mem_access;
+    const struct ffa_mem_access_1_2 *mem_access;
    struct ffa_mem_transaction_int trans;
    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
@@ -474,9 +514,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    register_t handle_hi = 0;
    register_t handle_lo = 0;
    int ret = FFA_RET_DENIED;
+    uint32_t ffa_vers;
    uint32_t range_count;
    uint32_t region_offs;
    uint16_t dst_id;
+    uint8_t perm;
+    uint64_t impdef[2];
 
    if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
    {
@@ -515,8 +558,8 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    if ( frag_len > tx_size )
        goto out_unlock;
 
-    ret = read_mem_transaction(ACCESS_ONCE(ctx->guest_vers), tx_buf,
-                               frag_len, &trans);
+    ffa_vers = ACCESS_ONCE(ctx->guest_vers);
+    ret = read_mem_transaction(ffa_vers, tx_buf, frag_len, &trans);
    if ( ret )
        goto out_unlock;
 
@@ -545,13 +588,35 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
        goto out_unlock;
    }
 
+    if ( trans.mem_access_size < sizeof(struct ffa_mem_access) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock;
+    }
+
    /* Check that it fits in the supplied data */
    if ( trans.mem_access_offs + trans.mem_access_size > frag_len )
        goto out_unlock;
 
    mem_access = tx_buf + trans.mem_access_offs;
-
    dst_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    perm = ACCESS_ONCE(mem_access->access_perm.perm);
+    region_offs = ACCESS_ONCE(mem_access->region_offs);
+
+    /*
+     * FF-A 1.2 introduced an extended mem_access descriptor with impdef
+     * fields, but guests can still use the 1.1 format if they don't need
+     * implementation-defined data. Detect which format is used based on
+     * the mem_access_size field rather than the negotiated FF-A version.
+     */
+    if ( trans.mem_access_size >= sizeof(struct ffa_mem_access_1_2) )
+        memcpy(impdef, mem_access->impdef, sizeof(impdef));
+    else
+    {
+        impdef[0] = 0;
+        impdef[1] = 0;
+    }
+
    if ( !FFA_ID_IS_SECURE(dst_id) )
    {
        /* we do not support sharing with VMs */
@@ -559,13 +624,11 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
        goto out_unlock;
    }
 
-    if ( ACCESS_ONCE(mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
+    if ( perm != FFA_MEM_ACC_RW )
    {
        ret = FFA_RET_NOT_SUPPORTED;
        goto out_unlock;
    }
-
-    region_offs = ACCESS_ONCE(mem_access->region_offs);
    if ( sizeof(*region_descr) + region_offs > frag_len )
    {
        ret = FFA_RET_NOT_SUPPORTED;
@@ -590,6 +653,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    }
    shm->sender_id = trans.sender_id;
    shm->ep_id = dst_id;
+    memcpy(shm->impdef, impdef, sizeof(shm->impdef));
 
    /*
     * Check that the Composite memory region descriptor fits.
@@ -605,7 +669,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    if ( ret )
        goto out;
 
Quoted 4 lines

xen/arch/arm/tee/ffa_shm.c void ffa_handle_mem_share(struct cpu_user_regs *regs) · line 672

Quoted 2 lines
xen/arch/arm/tee/ffa_shm.c +1 -1
-    ret = share_shm(shm);
+    ret = share_shm(shm, ffa_vers);

Shouldn't we rather use ffa_fw_version?

Definitely yes.

In fact i have done this in a follow up patch and i need to export the fw version to be able to do that but I will bring that forward and do it here, that makes a lot more sense.

Cheers
Bertrand

Quoted 9 lines

Cheers,
Jens

Quoted 5 lines
xen/arch/arm/tee/ffa_shm.c +0 -1
    if ( ret )
        goto out;
 
--

2.50.1 (Apple Git-155)

#18 lore raw

Re: [PATCH 05/12] xen/arm: ffa: Fix PARTINFO RX release errors

In reply to Jens Wiklander

Hi Jens,

Quoted 32 lines · Jens Wiklander

Hi Bertrand,

Quoted 26 lines · Bertrand Marquis

On PARTITION_INFO_GET error paths, Xen unconditionally called FFA_RX_RELEASE for the SPMC RX buffer. If the SPMC didn't grant RX ownership (i.e., the call failed early), this issues a spurious release that returns DENIED and produces warnings.

Modify ffa_rxtx_spmc_rx_release() to return the release status and let callers choose whether to log it. Only issue FFA_RX_RELEASE after a successful PARTINFO SMC, while always releasing the local RX lock to avoid deadlocks.

Update handle_partition_info_get() to only release the SPMC RX buffer after successful fw_ret checks, and ignore release errors during the error path.

Functional impact: eliminates spurious FFA_RX_RELEASE calls and associated DENIED warnings when PARTITION_INFO_GET fails before obtaining SPMC RX buffer ownership.

---

xen/arch/arm/tee/ffa_partinfo.c | 14 ++++++++++++--
xen/arch/arm/tee/ffa_private.h  |  2 +-
xen/arch/arm/tee/ffa_rxtx.c     | 14 +++++++++-----
3 files changed, 22 insertions(+), 8 deletions(-)
Quoted 15 lines

xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count, · line 99

Quoted 13 lines
xen/arch/arm/tee/ffa_partinfo.c +2 -0
index bf906ed0c88f..6b01c4abe915 100644
@@ -92,9 +92,11 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
                                   uint32_t dst_size)
 {
    int32_t ret;
+    int32_t release_ret;
    uint32_t src_size, real_sp_count;
    void *src_buf;
    uint32_t count = 0;
+    bool spmc_ok = false;

Wouldn't notify_fw be clearer, and the same in ffa_partinfo_init()?

Yes that would make more sense.
I will rename spmc_ok to notify_fw in v2.

Quoted 3 lines

Either way, please add:

Thanks

Cheers
Bertrand

Quoted 101 lines

Cheers,
Jens

Quoted 97 lines
xen/arch/arm/tee/ffa_partinfo.c +1 -0
    /* We need to use the RX buffer to receive the list */
    src_buf = ffa_rxtx_spmc_rx_acquire();
@@ -104,6 +106,7 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
    ret = ffa_partition_info_get(uuid, 0, &real_sp_count, &src_size);
    if ( ret )
        goto out;
+    spmc_ok = true;
 
    /* Validate the src_size we got */
    if ( src_size < sizeof(struct ffa_partition_info_1_0) ||
@@ -157,7 +160,10 @@ static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
    *sp_count = count;
 

out:

xen/arch/arm/tee/ffa_partinfo.c +4 -1
-    ffa_rxtx_spmc_rx_release();
+    release_ret = ffa_rxtx_spmc_rx_release(spmc_ok);
+    if ( release_ret )
+        gprintk(XENLOG_WARNING,
+                "ffa: Error releasing SPMC RX buffer: %d\n", release_ret);
    return ret;

}

xen/arch/arm/tee/ffa_partinfo.c +2 -0
@@ -507,6 +513,7 @@ bool ffa_partinfo_init(void)
    int32_t e;
    void *spmc_rx;
    struct ffa_uuid nil_uuid = { .val = { 0ULL, 0ULL } };
+    bool spmc_ok = false;
 
    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32))
@@ -522,6 +529,7 @@ bool ffa_partinfo_init(void)
        printk(XENLOG_ERR "ffa: Failed to get list of SPs: %d\n", e);
        goto out;
    }
+    spmc_ok = true;
 
    if ( count >= FFA_MAX_NUM_SP )
    {
@@ -533,7 +541,9 @@ bool ffa_partinfo_init(void)
    ret = init_subscribers(spmc_rx, count, fpi_size);
 

out:

xen/arch/arm/tee/ffa_partinfo.c +3 -1
-    ffa_rxtx_spmc_rx_release();
+    e = ffa_rxtx_spmc_rx_release(spmc_ok);
+    if ( e )
+        printk(XENLOG_WARNING "ffa: Error releasing SPMC RX buffer: %d\n", e);
    return ret;

}

xen/arch/arm/tee/ffa_private.h +1 -1
index 58562d8e733c..461e87f6f9c4 100644
@@ -458,7 +458,7 @@ int32_t ffa_endpoint_domain_lookup(uint16_t endpoint_id, struct domain **d_out,
 bool ffa_rxtx_spmc_init(void);
 void ffa_rxtx_spmc_destroy(void);
 void *ffa_rxtx_spmc_rx_acquire(void);
-void ffa_rxtx_spmc_rx_release(void);
+int32_t ffa_rxtx_spmc_rx_release(bool notify_fw);
 void *ffa_rxtx_spmc_tx_acquire(void);
 void ffa_rxtx_spmc_tx_release(void);
 
xen/arch/arm/tee/ffa_rxtx.c +9 -6
index 7d8bb4f4d031..50758fb57cdf 100644
@@ -375,18 +375,22 @@ void *ffa_rxtx_spmc_rx_acquire(void)
    return NULL;
 }
 
-void ffa_rxtx_spmc_rx_release(void)
+int32_t ffa_rxtx_spmc_rx_release(bool notify_fw)
 {
    int32_t ret;
 
    ASSERT(spin_is_locked(&ffa_spmc_rx_lock));
 
-    /* Inform the SPMC that we are done with our RX buffer */
-    ret = ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
-    if ( ret != FFA_RET_OK )
-        printk(XENLOG_DEBUG "Error releasing SPMC RX buffer: %d\n", ret);
+    if ( notify_fw )
+    {
+        /* Inform the SPMC that we are done with our RX buffer */
+        ret = ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
+    }
+    else
+        ret = FFA_RET_OK;
 
    spin_unlock(&ffa_spmc_rx_lock);
+    return ret;
 }
 
 void *ffa_rxtx_spmc_tx_acquire(void)
--

2.50.1 (Apple Git-155)

#10 lore raw

Re: [PATCH 03/12] xen/arm: ffa: Harden shm page parsing

In reply to Bertrand Marquis

Hi Jens,

Quoted 87 lines · Bertrand Marquis

Hi Jens,

Quoted 76 lines · Jens Wiklander

Hi Bertrand,

Quoted 70 lines · Bertrand Marquis

get_shm_pages() uses unchecked address arithmetic and does not enforce alignment, so malformed descriptors can cause overflow or slip through validation. The reclaim path also repeats handle-to-shm-mem conversion in multiple places, duplicating error handling.

Harden page parsing and reclaim handling:
- add ffa_safe_addr_add() and use it to detect address overflows
- enforce alignment checks in get_shm_pages() and return FF-A errors
- introduce ffa_secure_reclaim() and use it for MEM_RECLAIM and teardown
- simplify ffa_mem_share() argument handling and allow max page count

Functional impact: invalid or misaligned memory ranges now fail earlier with proper error codes; behavior for valid descriptors is unchanged.

---
xen/arch/arm/tee/ffa_private.h | 11 +++++++
xen/arch/arm/tee/ffa_shm.c | 57 +++++++++++++++++-----------------

2 files changed, 40 insertions(+), 28 deletions(-)
xen/arch/arm/tee/ffa_private.h +11 -0
index b625f1c72914..58562d8e733c 100644
@@ -632,4 +632,15 @@ static inline void ffa_uuid_set(struct ffa_uuid *id, uint32_t val0,
   id->val[1] = ((uint64_t)val3 << 32U) | val2;
 }
 
+/*
+ * Common overflow-safe helper to verify that adding a number of pages to an
+ * address will not wrap around.
+ */
+static inline bool ffa_safe_addr_add(uint64_t addr, uint64_t pages)
+{
+    uint64_t off = pages * FFA_PAGE_SIZE;
+
+    return (off / FFA_PAGE_SIZE) == pages && addr <= UINT64_MAX - off;
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_shm.c +4 -6
index 90800e44a86a..4c0b45cde6ee 100644
@@ -96,16 +96,14 @@ struct ffa_shm_mem {
   struct page_info *pages[];
 };
 
-static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
-                             register_t addr, uint32_t pg_count,
-                             uint64_t *handle)
+static int32_t ffa_mem_share(uint32_t tot_len, uint64_t *handle)
 {
   struct arm_smccc_1_2_regs arg = {
       .a0 = FFA_MEM_SHARE_64,
       .a1 = tot_len,
-        .a2 = frag_len,
-        .a3 = addr,
-        .a4 = pg_count,
+        .a2 = tot_len,
+        .a3 = 0,
+        .a4 = 0,
   };
   struct arm_smccc_1_2_regs resp;
 
@@ -131,12 +129,16 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
   }
 }
 
Quoted 6 lines

xen/arch/arm/tee/ffa_shm.c static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len, · line 132

Quoted 3 lines
xen/arch/arm/tee/ffa_shm.c +1 -2
-static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
-                               uint32_t flags)
+static int32_t ffa_secure_reclaim(struct ffa_shm_mem *shm, uint32_t flags)

I agree with moving the uint64_to_regpair() call into this function, but I'm puzzled by the new name. What's secure?

Quoted 6 lines

This is to distinguish with reclaim for VM to VM sharing in the future as here reclaim is asked to the secure world.

But in fact to be coherent I should also have renamed ffa_mem_share to ffa_secure_share.

Would you be ok with that ?

Looking at this a bit more, we are usually using spmc and not secure.

Would you be ok if I rename both those to:
ffa_spmc_share
ffa_spmc_reclaim

Cheers
Bertrand

Quoted 150 lines
Quoted 99 lines
Quoted 99 lines
xen/arch/arm/tee/ffa_shm.c +21 -10
 {
+    register_t handle_hi;
+    register_t handle_lo;
+
   if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
       return FFA_RET_NOT_SUPPORTED;
 
+    uint64_to_regpair(&handle_hi, &handle_lo, shm->handle);
+
   return ffa_simple_call(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, 0);
 }
 
@@ -145,7 +147,7 @@ static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
 * this function fails then the caller is still expected to call
 * put_shm_pages() as a cleanup.
 */
-static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
+static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
                        const struct ffa_address_range *range,
                        uint32_t range_count)
 {
@@ -156,17 +158,26 @@ static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
   p2m_type_t t;
   uint64_t addr;
   uint64_t page_count;
+    uint64_t gaddr;
 
   for ( n = 0; n < range_count; n++ )
   {
       page_count = ACCESS_ONCE(range[n].page_count);
       addr = ACCESS_ONCE(range[n].address);
+
+        if ( !IS_ALIGNED(addr, FFA_PAGE_SIZE) )
+            return FFA_RET_INVALID_PARAMETERS;
+
       for ( m = 0; m < page_count; m++ )
       {
           if ( pg_idx >= shm->page_count )
               return FFA_RET_INVALID_PARAMETERS;
 
-            gfn = gaddr_to_gfn(addr + m * FFA_PAGE_SIZE);
+            if ( !ffa_safe_addr_add(addr, m) )
+                return FFA_RET_INVALID_PARAMETERS;
+
+            gaddr = addr + m * FFA_PAGE_SIZE;
+            gfn = gaddr_to_gfn(gaddr);
           shm->pages[pg_idx] = get_page_from_gfn(d, gfn_x(gfn), &t,
                                                 P2M_ALLOC);
           if ( !shm->pages[pg_idx] )
@@ -180,7 +191,7 @@ static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
 
   /* The ranges must add up */
   if ( pg_idx < shm->page_count )
-            return FFA_RET_INVALID_PARAMETERS;
+        return FFA_RET_INVALID_PARAMETERS;
 
   return FFA_RET_OK;
 }
@@ -198,15 +209,11 @@ static void put_shm_pages(struct ffa_shm_mem *shm)
 
 static bool inc_ctx_shm_count(struct domain *d, struct ffa_ctx *ctx)
 {
-    bool ret = true;
+    bool ret = false;
 
   spin_lock(&ctx->lock);
 
-    if ( ctx->shm_count >= FFA_MAX_SHM_COUNT )
-    {
-        ret = false;
-    }
-    else
+    if ( ctx->shm_count < FFA_MAX_SHM_COUNT )
   {
       /*
        * If this is the first shm added, increase the domain reference
@@ -217,6 +224,7 @@ static bool inc_ctx_shm_count(struct domain *d, struct ffa_ctx *ctx)
           get_knownalive_domain(d);
 
       ctx->shm_count++;
+        ret = true;
   }
 
   spin_unlock(&ctx->lock);
@@ -251,7 +259,7 @@ static struct ffa_shm_mem *alloc_ffa_shm_mem(struct domain *d,
   struct ffa_ctx *ctx = d->arch.tee;
   struct ffa_shm_mem *shm;
 
-    if ( page_count >= FFA_MAX_SHM_PAGE_COUNT )
+    if ( page_count > FFA_MAX_SHM_PAGE_COUNT )
       return NULL;
   if ( !inc_ctx_shm_count(d, ctx) )
       return NULL;
@@ -367,7 +375,7 @@ static int share_shm(struct ffa_shm_mem *shm)
       init_range(addr_range, pa);
   }
 
Quoted 3 lines

xen/arch/arm/tee/ffa_shm.c static int share_shm(struct ffa_shm_mem *shm) · line 370

Quoted 1 line
xen/arch/arm/tee/ffa_shm.c +0 -1
-    ret = ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);

Please remove frag_len from share_shm() since it's not needed any longer.

Ack, I will remove it in v2.

Cheers
Bertrand

Quoted 42 lines

Cheers,
Jens

Quoted 38 lines
xen/arch/arm/tee/ffa_shm.c +1 -0
+    ret = ffa_mem_share(tot_len, &shm->handle);
 

out:

   ffa_rxtx_spmc_tx_release();
xen/arch/arm/tee/ffa_shm.c +2 -10
@@ -637,8 +645,6 @@ int32_t ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
   struct domain *d = current->domain;
   struct ffa_ctx *ctx = d->arch.tee;
   struct ffa_shm_mem *shm;
-    register_t handle_hi;
-    register_t handle_lo;
   int32_t ret;
 
   if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
@@ -652,8 +658,7 @@ int32_t ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
   if ( !shm )
       return FFA_RET_INVALID_PARAMETERS;
 
-    uint64_to_regpair(&handle_hi, &handle_lo, handle);
-    ret = ffa_mem_reclaim(handle_lo, handle_hi, flags);
+    ret = ffa_secure_reclaim(shm, flags);
 
   if ( ret )
   {
@@ -677,11 +682,7 @@ bool ffa_shm_domain_destroy(struct domain *d)
 
   list_for_each_entry_safe(shm, tmp, &ctx->shm_list, list)
   {
-        register_t handle_hi;
-        register_t handle_lo;
-
-        uint64_to_regpair(&handle_hi, &handle_lo, shm->handle);
-        res = ffa_mem_reclaim(handle_lo, handle_hi, 0);
+        res = ffa_secure_reclaim(shm, 0);
       switch ( res ) {
       case FFA_RET_OK:
           printk(XENLOG_G_DEBUG "%pd: ffa: Reclaimed handle %#lx\n",
--

2.50.1 (Apple Git-155)

#11 lore raw

Re: [PATCH 03/12] xen/arm: ffa: Harden shm page parsing

Hi Bertrand,

Quoted 95 lines · Bertrand Marquis

Hi Jens,

Quoted 92 lines · Bertrand Marquis

Hi Jens,

Quoted 76 lines · Jens Wiklander

Hi Bertrand,

Quoted 70 lines · Bertrand Marquis

get_shm_pages() uses unchecked address arithmetic and does not enforce alignment, so malformed descriptors can cause overflow or slip through validation. The reclaim path also repeats handle-to-shm-mem conversion in multiple places, duplicating error handling.

Harden page parsing and reclaim handling:
- add ffa_safe_addr_add() and use it to detect address overflows
- enforce alignment checks in get_shm_pages() and return FF-A errors
- introduce ffa_secure_reclaim() and use it for MEM_RECLAIM and teardown
- simplify ffa_mem_share() argument handling and allow max page count

Functional impact: invalid or misaligned memory ranges now fail earlier with proper error codes; behavior for valid descriptors is unchanged.

---
xen/arch/arm/tee/ffa_private.h | 11 +++++++
xen/arch/arm/tee/ffa_shm.c | 57 +++++++++++++++++-----------------

2 files changed, 40 insertions(+), 28 deletions(-)
xen/arch/arm/tee/ffa_private.h +11 -0
index b625f1c72914..58562d8e733c 100644
@@ -632,4 +632,15 @@ static inline void ffa_uuid_set(struct ffa_uuid *id, uint32_t val0,
   id->val[1] = ((uint64_t)val3 << 32U) | val2;
 }
 
+/*
+ * Common overflow-safe helper to verify that adding a number of pages to an
+ * address will not wrap around.
+ */
+static inline bool ffa_safe_addr_add(uint64_t addr, uint64_t pages)
+{
+    uint64_t off = pages * FFA_PAGE_SIZE;
+
+    return (off / FFA_PAGE_SIZE) == pages && addr <= UINT64_MAX - off;
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_shm.c +4 -6
index 90800e44a86a..4c0b45cde6ee 100644
@@ -96,16 +96,14 @@ struct ffa_shm_mem {
   struct page_info *pages[];
 };
 
-static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
-                             register_t addr, uint32_t pg_count,
-                             uint64_t *handle)
+static int32_t ffa_mem_share(uint32_t tot_len, uint64_t *handle)
 {
   struct arm_smccc_1_2_regs arg = {
       .a0 = FFA_MEM_SHARE_64,
       .a1 = tot_len,
-        .a2 = frag_len,
-        .a3 = addr,
-        .a4 = pg_count,
+        .a2 = tot_len,
+        .a3 = 0,
+        .a4 = 0,
   };
   struct arm_smccc_1_2_regs resp;
 
@@ -131,12 +129,16 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
   }
 }
 
Quoted 6 lines

xen/arch/arm/tee/ffa_shm.c static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len, · line 132

Quoted 3 lines
xen/arch/arm/tee/ffa_shm.c +1 -2
-static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
-                               uint32_t flags)
+static int32_t ffa_secure_reclaim(struct ffa_shm_mem *shm, uint32_t flags)

I agree with moving the uint64_to_regpair() call into this function, but I'm puzzled by the new name. What's secure?

This is to distinguish with reclaim for VM to VM sharing in the future as here reclaim is asked to the secure world.

But in fact to be coherent I should also have renamed ffa_mem_share to ffa_secure_share.

Quoted 7 lines
Quoted 1 line

Would you be ok with that ?

Looking at this a bit more, we are usually using spmc and not secure.

Would you be ok if I rename both those to:
ffa_spmc_share
ffa_spmc_reclaim

Yes, that sounds good.

Cheers,
Jens

#15 lore raw

Re: [PATCH 04/12] xen/arm: ffa: Add FF-A 1.2 endpoint memory access descriptors

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 165 lines · Bertrand Marquis

Hi Jens,

Quoted 134 lines · Jens Wiklander

Hi Bertrand,

Quoted 128 lines · Bertrand Marquis

FF-A 1.2 extends the endpoint memory access descriptor (EMAD) from 16 to 32 bytes, adding implementation-defined (IMPDEF) fields and reserved space. The MEM_SHARE path currently assumes the 1.1 EMAD size and rejects the 1.2 layout.

Add FF-A 1.2 EMAD support to MEM_SHARE:
- define ffa_mem_access_1_2 and store IMPDEF payload in ffa_shm_mem
- emit 1.2 EMADs to the SPMC for FF-A 1.2 guests, forwarding IMPDEF
- refactor header parsing into read_mem_transaction() for 1.0/1.1+
- detect EMAD format by mem_access_size to allow 1.1 on 1.2 guests

Functional impact: MEM_SHARE supports FF-A 1.2 EMADs.

---
xen/arch/arm/tee/ffa_shm.c | 108 +++++++++++++++++++++++++++++--------

1 file changed, 86 insertions(+), 22 deletions(-)
xen/arch/arm/tee/ffa_shm.c +44 -11
index 4c0b45cde6ee..905a64e3db01 100644
@@ -30,6 +30,14 @@ struct ffa_mem_access {
    uint64_t reserved;
 };
 
+/* Endpoint memory access descriptor (FF-A 1.2) */
+struct ffa_mem_access_1_2 {
+    struct ffa_mem_access_perm access_perm;
+    uint32_t region_offs;
+    uint8_t impdef[16];
+    uint8_t reserved[8];
+};
+
 /* Lend, donate or share memory transaction descriptor */
 struct ffa_mem_transaction_1_0 {
    uint16_t sender_id;
@@ -73,7 +81,7 @@ struct ffa_mem_transaction_1_1 {
 /*
 * The parts needed from struct ffa_mem_transaction_1_0 or struct
 * ffa_mem_transaction_1_1, used to provide an abstraction of difference in
- * data structures between version 1.0 and 1.1. This is just an internal
+ * data structures between version 1.0 and 1.2. This is just an internal
 * interface and can be changed without changing any ABI.
 */
 struct ffa_mem_transaction_int {
@@ -92,6 +100,8 @@ struct ffa_shm_mem {
    uint16_t sender_id;
    uint16_t ep_id;     /* endpoint, the one lending */
    uint64_t handle;    /* FFA_HANDLE_INVALID if not set yet */
+    /* Endpoint memory access descriptor IMPDEF value (FF-A 1.2). */
+    uint64_t impdef[2];
    unsigned int page_count;
    struct page_info *pages[];
 };
@@ -297,17 +307,21 @@ static void init_range(struct ffa_address_range *addr_range,
 * This function uses the ffa_spmc tx buffer to transmit the memory transaction
 * descriptor.
 */
-static int share_shm(struct ffa_shm_mem *shm)
+static int share_shm(struct ffa_shm_mem *shm, uint32_t ffa_vers)
 {
    const uint32_t max_frag_len = FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE;
    struct ffa_mem_access *mem_access_array;
+    struct ffa_mem_access_1_2 *mem_access_array_1_2;
    struct ffa_mem_transaction_1_1 *descr;
    struct ffa_address_range *addr_range;
    struct ffa_mem_region *region_descr;
-    const unsigned int region_count = 1;
    uint32_t frag_len;
    uint32_t tot_len;
+    uint32_t mem_access_size;
+    uint32_t mem_access_offs;
+    uint32_t region_offs;
    paddr_t last_pa;
+    uint32_t range_count;
    unsigned int n;
    paddr_t pa;
    int32_t ret;
@@ -326,16 +340,35 @@ static int share_shm(struct ffa_shm_mem *shm)
    descr->handle = shm->handle;
    descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR;
    descr->mem_access_count = 1;
-    descr->mem_access_size = sizeof(*mem_access_array);
-    descr->mem_access_offs = MEM_ACCESS_OFFSET(0);
+    if ( ffa_vers >= FFA_VERSION_1_2 )
+        mem_access_size = sizeof(struct ffa_mem_access_1_2);
+    else
+        mem_access_size = sizeof(struct ffa_mem_access);
+    mem_access_offs = sizeof(struct ffa_mem_transaction_1_1);
+    region_offs = mem_access_offs + mem_access_size;
+    descr->mem_access_size = mem_access_size;
+    descr->mem_access_offs = mem_access_offs;
 
-    mem_access_array = buf + descr->mem_access_offs;
-    memset(mem_access_array, 0, sizeof(*mem_access_array));
-    mem_access_array[0].access_perm.endpoint_id = shm->ep_id;
-    mem_access_array[0].access_perm.perm = FFA_MEM_ACC_RW;
-    mem_access_array[0].region_offs = REGION_OFFSET(descr->mem_access_count, 0);
+    if ( ffa_vers >= FFA_VERSION_1_2 )
+    {
+        mem_access_array_1_2 = buf + mem_access_offs;
+        memset(mem_access_array_1_2, 0, sizeof(*mem_access_array_1_2));
+        mem_access_array_1_2[0].access_perm.endpoint_id = shm->ep_id;
+        mem_access_array_1_2[0].access_perm.perm = FFA_MEM_ACC_RW;
+        mem_access_array_1_2[0].region_offs = region_offs;
+        memcpy(mem_access_array_1_2[0].impdef, shm->impdef,
+               sizeof(mem_access_array_1_2[0].impdef));
+    }
+    else
+    {
+        mem_access_array = buf + mem_access_offs;
+        memset(mem_access_array, 0, sizeof(*mem_access_array));
+        mem_access_array[0].access_perm.endpoint_id = shm->ep_id;
+        mem_access_array[0].access_perm.perm = FFA_MEM_ACC_RW;
+        mem_access_array[0].region_offs = region_offs;
+    }
 
-    region_descr = buf + mem_access_array[0].region_offs;
+    region_descr = buf + region_offs;
    memset(region_descr, 0, sizeof(*region_descr));
    region_descr->total_page_count = shm->page_count;
 
@@ -349,8 +382,9 @@ static int share_shm(struct ffa_shm_mem *shm)
        region_descr->address_range_count++;
    }
 
Quoted 5 lines

xen/arch/arm/tee/ffa_shm.c static int share_shm(struct ffa_shm_mem *shm) · line 353

Quoted 2 lines
xen/arch/arm/tee/ffa_shm.c +0 -2
-    tot_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count,
-                                region_descr->address_range_count);

Please remove the unused ADDR_RANGE_OFFSET() macro and friends, as they're no longer accurate.

Very true, will remove them in v2.

Quoted 20 lines
Quoted 20 lines
xen/arch/arm/tee/ffa_shm.c +4 -1
+    range_count = region_descr->address_range_count;
+    tot_len = region_offs + sizeof(*region_descr) +
+              range_count * sizeof(struct ffa_address_range);
    if ( tot_len > max_frag_len )
    {
        ret = FFA_RET_NOT_SUPPORTED;
@@ -358,7 +392,7 @@ static int share_shm(struct ffa_shm_mem *shm)
    }
 
    addr_range = region_descr->address_range_array;
-    frag_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count, 1);
+    frag_len = region_offs + sizeof(*region_descr) + sizeof(*addr_range);
    last_pa = page_to_maddr(shm->pages[0]);
    init_range(addr_range, last_pa);
    for ( n = 1; n < shm->page_count; last_pa = pa, n++ )
@@ -448,6 +482,12 @@ static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen,
    if ( size * count + offs > blen )
        return FFA_RET_INVALID_PARAMETERS;
 
Quoted 8 lines
Quoted 4 lines

xen/arch/arm/tee/ffa_shm.c static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen, · line 486

Quoted 2 lines
xen/arch/arm/tee/ffa_shm.c +2 -0
+    if ( size < sizeof(struct ffa_mem_access) )
+        return FFA_RET_INVALID_PARAMETERS;

Implicitly, size should also be a multiple of 16. Don't you agree?

The spec is giving some constraints on the offset but there is nothing explicit for the per mem access size. As we have no 64bit fields in it, I am not really seeing any implicit multiple of 16.

I was perhaps assuming too much. The offset is required to be a multiple of 16, so it would make sense for all elements in the array to be at a 16-byte aligned offset.

Quoted 3 lines

I am checking the offset because it is enforced by the spec but for the size i would rather not put something as who knows what might be added in the future.

It's not important right now, since we only accept a single EMAD. But once we accept more than one, the second EMAD might be accessed from an unaligned address if we're not careful.

Quoted 111 lines
Quoted 106 lines
Quoted 106 lines
xen/arch/arm/tee/ffa_shm.c +35 -7
+
+    if ( offs & 0xF )
+        return FFA_RET_INVALID_PARAMETERS;
+
    trans->mem_reg_attr = mem_reg_attr;
    trans->flags = flags;
    trans->mem_access_size = size;
@@ -464,7 +504,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    uint64_t addr = get_user_reg(regs, 3);
    uint32_t page_count = get_user_reg(regs, 4);
    const struct ffa_mem_region *region_descr;
-    const struct ffa_mem_access *mem_access;
+    const struct ffa_mem_access_1_2 *mem_access;
    struct ffa_mem_transaction_int trans;
    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
@@ -474,9 +514,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    register_t handle_hi = 0;
    register_t handle_lo = 0;
    int ret = FFA_RET_DENIED;
+    uint32_t ffa_vers;
    uint32_t range_count;
    uint32_t region_offs;
    uint16_t dst_id;
+    uint8_t perm;
+    uint64_t impdef[2];
 
    if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
    {
@@ -515,8 +558,8 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    if ( frag_len > tx_size )
        goto out_unlock;
 
-    ret = read_mem_transaction(ACCESS_ONCE(ctx->guest_vers), tx_buf,
-                               frag_len, &trans);
+    ffa_vers = ACCESS_ONCE(ctx->guest_vers);
+    ret = read_mem_transaction(ffa_vers, tx_buf, frag_len, &trans);
    if ( ret )
        goto out_unlock;
 
@@ -545,13 +588,35 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
        goto out_unlock;
    }
 
+    if ( trans.mem_access_size < sizeof(struct ffa_mem_access) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock;
+    }
+
    /* Check that it fits in the supplied data */
    if ( trans.mem_access_offs + trans.mem_access_size > frag_len )
        goto out_unlock;
 
    mem_access = tx_buf + trans.mem_access_offs;
-
    dst_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    perm = ACCESS_ONCE(mem_access->access_perm.perm);
+    region_offs = ACCESS_ONCE(mem_access->region_offs);
+
+    /*
+     * FF-A 1.2 introduced an extended mem_access descriptor with impdef
+     * fields, but guests can still use the 1.1 format if they don't need
+     * implementation-defined data. Detect which format is used based on
+     * the mem_access_size field rather than the negotiated FF-A version.
+     */
+    if ( trans.mem_access_size >= sizeof(struct ffa_mem_access_1_2) )
+        memcpy(impdef, mem_access->impdef, sizeof(impdef));
+    else
+    {
+        impdef[0] = 0;
+        impdef[1] = 0;
+    }
+
    if ( !FFA_ID_IS_SECURE(dst_id) )
    {
        /* we do not support sharing with VMs */
@@ -559,13 +624,11 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
        goto out_unlock;
    }
 
-    if ( ACCESS_ONCE(mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
+    if ( perm != FFA_MEM_ACC_RW )
    {
        ret = FFA_RET_NOT_SUPPORTED;
        goto out_unlock;
    }
-
-    region_offs = ACCESS_ONCE(mem_access->region_offs);
    if ( sizeof(*region_descr) + region_offs > frag_len )
    {
        ret = FFA_RET_NOT_SUPPORTED;
@@ -590,6 +653,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    }
    shm->sender_id = trans.sender_id;
    shm->ep_id = dst_id;
+    memcpy(shm->impdef, impdef, sizeof(shm->impdef));
 
    /*
     * Check that the Composite memory region descriptor fits.
@@ -605,7 +669,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    if ( ret )
        goto out;
 
Quoted 4 lines

xen/arch/arm/tee/ffa_shm.c void ffa_handle_mem_share(struct cpu_user_regs *regs) · line 672

Quoted 2 lines
xen/arch/arm/tee/ffa_shm.c +1 -1
-    ret = share_shm(shm);
+    ret = share_shm(shm, ffa_vers);

Shouldn't we rather use ffa_fw_version?

Quoted 4 lines

Definitely yes.

In fact i have done this in a follow up patch and i need to export the fw version to be able to do that but I will bring that forward and do it here, that makes a lot more sense.

Sounds good.

Cheers,
Jens

Quoted 15 lines

Cheers
Bertrand

Quoted 9 lines

Cheers,
Jens

Quoted 5 lines
xen/arch/arm/tee/ffa_shm.c +0 -1
    if ( ret )
        goto out;
 
--

2.50.1 (Apple Git-155)

Reviewed-by on v1 6/12
#20 lore raw

Re: [PATCH 06/12] xen/arm: ffa: Fix RX/TX map layout and errors

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 12 lines · Bertrand Marquis

FFA_RXTX_MAP builds a descriptor for the SPMC and the composite region offsets must be 8-byte aligned. Xen currently uses a 12-byte header size, which produces misaligned RX/TX offsets. Mapping failures also return INVALID_PARAMETERS even when the failure is due to resource exhaustion, which misreports the error condition.

Round the descriptor header size up to 8 bytes before placing the RX region and derive the TX offset from the aligned RX offset. Return FFA_RET_NO_MEMORY when the TX or RX buffer mapping fails before the error paths unwind.

Quoted 7 lines

Functional impact: RXTX_MAP now returns NO_MEMORY on mapping failures and the SPMC receives an aligned RX/TX descriptor layout.

---
 xen/arch/arm/tee/ffa_rxtx.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

Looks good:

Cheers,
Jens

Quoted 47 lines
xen/arch/arm/tee/ffa_rxtx.c +10 -4
index 50758fb57cdf..eff95a7955d7 100644
@@ -119,11 +119,17 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 
     tx = __map_domain_page_global(tx_pg);
     if ( !tx )
+    {
+        ret = FFA_RET_NO_MEMORY;
         goto err_put_rx_pg;
+    }
 
     rx = __map_domain_page_global(rx_pg);
     if ( !rx )
+    {
+        ret = FFA_RET_NO_MEMORY;
         goto err_unmap_tx;
+    }
 
     /*
      * Transmit the RX/TX buffer information to the SPM if acquire is supported
@@ -136,7 +142,8 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
         struct ffa_mem_region *mem_reg;
 
         /* All must fit in our TX buffer */
-        BUILD_BUG_ON(sizeof(*rxtx_desc) + sizeof(*mem_reg) * 2 +
+        BUILD_BUG_ON(ROUNDUP(sizeof(*rxtx_desc), 8) +
+                     sizeof(*mem_reg) * 2 +
                      sizeof(struct ffa_address_range) * 2 >
                      FFA_MAX_RXTX_PAGE_COUNT * FFA_PAGE_SIZE);
 
@@ -153,8 +160,8 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
          */
         rxtx_desc->sender_id = ffa_get_vm_id(d);
         rxtx_desc->reserved = 0;
-        rxtx_desc->rx_region_offs = sizeof(*rxtx_desc);
-        rxtx_desc->tx_region_offs = sizeof(*rxtx_desc) +
+        rxtx_desc->rx_region_offs = ROUNDUP(sizeof(*rxtx_desc), 8);
+        rxtx_desc->tx_region_offs = rxtx_desc->rx_region_offs +
                                     offsetof(struct ffa_mem_region,
                                              address_range_array[1]);
 
--

2.50.1 (Apple Git-155)

#22 lore raw

Re: [PATCH 07/12] xen/arm: ffa: Fix RXTX_UNMAP ownership race

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 45 lines · Bertrand Marquis

rxtx_unmap() checks RX ownership without holding the RX/TX locks and only enforces the ownership rule when FFA_RX_ACQUIRE is supported. This allows a vCPU to acquire RX between the check and unmap, and it lets RXTX_UNMAP proceed while RX is owned when buffers are not forwarded to firmware.

Hold rx_lock/tx_lock across the ownership check and unmap, and deny RXTX_UNMAP whenever RX is owned, independent of RX_ACQUIRE support. For teardown, release RX ownership under the same lock window; use FFA_RX_RELEASE directly because rx_lock is held, and clear the local flag when the firmware path is unavailable.

Functional impact: RXTX_UNMAP now reliably returns DENIED while RX is owned, and teardown releases/clears ownership without a race.

---
 xen/arch/arm/tee/ffa_rxtx.c | 36 +++++++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)
xen/arch/arm/tee/ffa_rxtx.c +8 -1
index eff95a7955d7..450ce102cbdc 100644
@@ -220,7 +220,7 @@ err_unlock_rxtx:
     return ret;
 }
 
-static int32_t rxtx_unmap(struct domain *d)
+static int32_t rxtx_unmap(struct domain *d, bool teardown)
 {
     struct ffa_ctx *ctx = d->arch.tee;
     int32_t ret = FFA_RET_OK;
@@ -234,6 +234,36 @@ static int32_t rxtx_unmap(struct domain *d)
         goto err_unlock_rxtx;
     }
 
+    if ( !ctx->rx_is_free )
+    {
+        if ( teardown )
+        {
+            if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+            {
+                int32_t rel_ret;

xen/arch/arm/tee/ffa_rxtx.c static int32_t rxtx_unmap(struct domain *d) · line 253

Quoted 10 lines
xen/arch/arm/tee/ffa_rxtx.c +10 -0
+
+                /* Can't use ffa_rx_release() while holding rx_lock. */
+                rel_ret = ffa_simple_call(FFA_RX_RELEASE, ctx->ffa_id,
+                                          0, 0, 0);
+                if ( rel_ret )
+                    gdprintk(XENLOG_DEBUG,
+                             "ffa: RX release during teardown failed: %d\n",
+                             rel_ret);
+                else
+                    ctx->rx_is_free = true;

I don't see why this assignment is needed, or the one just below.

Quoted 26 lines
xen/arch/arm/tee/ffa_rxtx.c +14 -1
+            }
+            else
+                ctx->rx_is_free = true;
+        }
+        else
+        {
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: RXTX_UNMAP denied, RX buffer owned by VM\n");
+            ret = FFA_RET_DENIED;
+            goto err_unlock_rxtx;
+        }
+    }
+
     if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
     {
         ret = ffa_rxtx_unmap(ffa_get_vm_id(d));
@@ -261,7 +291,7 @@ err_unlock_rxtx:
 
 int32_t ffa_handle_rxtx_unmap(void)
 {
-    return rxtx_unmap(current->domain);
+    return rxtx_unmap(current->domain, false);
 }
 
 int32_t ffa_rx_acquire(struct ffa_ctx *ctx, void **buf, size_t *buf_size)

xen/arch/arm/tee/ffa_rxtx.c int32_t ffa_rxtx_domain_init(struct domain *d) · line 402

Quoted 5 lines
xen/arch/arm/tee/ffa_rxtx.c +1 -1
@@ -369,7 +399,7 @@ int32_t ffa_rxtx_domain_init(struct domain *d)
 
 void ffa_rxtx_domain_destroy(struct domain *d)
 {
-    rxtx_unmap(d);
+    rxtx_unmap(d, true);

How about adding a /* teardown */ just after true as a reminder of what true is supposed to represent.

Adding such comments isn't very common, but we're doing it at a few places in the source tree, and I think it helps when reading the code.

Cheers,
Jens

Quoted 6 lines
xen/arch/arm/tee/ffa_rxtx.c +0 -1
 }
 
 void *ffa_rxtx_spmc_rx_acquire(void)
--

2.50.1 (Apple Git-155)

#25 lore raw

Re: [PATCH 08/12] xen/arm: ffa: Fix FFA_FEATURES validation

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 52 lines · Bertrand Marquis

FFA_FEATURES currently accepts non-zero input properties (w2-w7) from guests and advertises several ABIs unconditionally, even when firmware support is missing or when the ABI is physical-instance-only. This can mislead guests about what Xen can actually provide and violates FF-A calling conventions. Some SPMCs (Hafnium v2.14 or earlier) also fail to report FFA_RX_ACQUIRE despite supporting it.

Update FFA_FEATURES validation to match spec and firmware support:
- reject non-zero w2-w7 input properties with INVALID_PARAMETERS
- reject 64-bit calling conventions from 32-bit guests with NOT_SUPPORTED
- return NOT_SUPPORTED for physical-instance-only ABIs (FFA_NOTIFICATION_BITMAP_{CREATE,DESTROY}, FFA_RX_ACQUIRE)
- advertise FFA_INTERRUPT as supported
- gate message ABIs on firmware support:
- FFA_MSG_SEND_DIRECT_REQ_{32,64}
- FFA_MSG_SEND_DIRECT_REQ2 (also requires FF-A 1.2 negotiation)
- FFA_MSG_SEND2 (or VM-to-VM enabled)
- report MEM_SHARE_{32,64} only when FFA_MEM_SHARE_64 is supported
- stop advertising FFA_MSG_YIELD (not implemented)

Update firmware probing: drop FFA_MEM_SHARE_32 checks (deprecated) and add FFA_RX_ACQUIRE to the probed set. If FFA_MSG_SEND2 is reported but FFA_RX_ACQUIRE is not, assume RX_ACQUIRE support and warn to work around the Hafnium bug.

Functional impact: guests now see ABI support that reflects firmware capabilities and Xen implementation status. When SEND2 is present but RX_ACQUIRE is not reported, Xen assumes RX_ACQUIRE support.

---
 xen/arch/arm/tee/ffa.c | 62 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 55 insertions(+), 7 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index 6de2b9f8ac8e..e9e020bb0cb3 100644
@@ -91,10 +91,10 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
     FW_ABI(FFA_PARTITION_INFO_GET),
     FW_ABI(FFA_NOTIFICATION_INFO_GET_64),
     FW_ABI(FFA_NOTIFICATION_GET),
+    FW_ABI(FFA_RX_ACQUIRE),
     FW_ABI(FFA_RX_RELEASE),
     FW_ABI(FFA_RXTX_MAP_64),
     FW_ABI(FFA_RXTX_UNMAP),
-    FW_ABI(FFA_MEM_SHARE_32),
     FW_ABI(FFA_MEM_SHARE_64),
     FW_ABI(FFA_MEM_RECLAIM),
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),

xen/arch/arm/tee/ffa.c static void handle_features(struct cpu_user_regs *regs) · line 248

Quoted 9 lines
xen/arch/arm/tee/ffa.c +6 -0
@@ -240,19 +240,39 @@ static void handle_features(struct cpu_user_regs *regs)
     struct ffa_ctx *ctx = d->arch.tee;
     unsigned int n;
 
+    /*
+     * Xen does not accept any non-zero FFA_FEATURES input properties from
+     * VMs. The spec only defines w2 input properties for FFA_MEM_RETRIEVE_REQ
+     * (NS-bit negotiation for SP/SPMC) and FFA_RXTX_MAP (buffer size and
+     * alignment), so w2 must be MBZ for our callers.
+     */

The spec (version 1.2) lists them as SBZ, except for w2, which is MBZ, for Feature IDs.
However, if we're to return an error, invalid parameters is a better choice.

Cheers,
Jens

Quoted 98 lines
xen/arch/arm/tee/ffa.c +48 -7
     for ( n = 2; n <= 7; n++ )
     {
         if ( get_user_reg(regs, n) )
         {
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+            ffa_set_regs_error(regs, FFA_RET_INVALID_PARAMETERS);
             return;
         }
     }
 
+    if ( !is_64bit_domain(d) && smccc_is_conv_64(a1) )
+    {
+        /* 32bit guests should only use 32bit convention calls */
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        return;
+    }
+
     switch ( a1 )
     {
+    case FFA_NOTIFICATION_BITMAP_CREATE:
+    case FFA_NOTIFICATION_BITMAP_DESTROY:
+    case FFA_RX_ACQUIRE:
+        /* Physical-instance-only ABIs are not exposed to VMs. */
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        break;
     case FFA_ERROR:
     case FFA_VERSION:
+    case FFA_INTERRUPT:
     case FFA_SUCCESS_32:
     case FFA_SUCCESS_64:
     case FFA_FEATURES:
@@ -261,16 +281,25 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_RXTX_UNMAP:
     case FFA_MEM_RECLAIM:
     case FFA_PARTITION_INFO_GET:
+        ffa_set_regs_success(regs, 0, 0);
+        break;
     case FFA_MSG_SEND_DIRECT_REQ_32:
     case FFA_MSG_SEND_DIRECT_REQ_64:
-    case FFA_MSG_SEND2:
     case FFA_RUN:
-    case FFA_INTERRUPT:
-    case FFA_MSG_YIELD:
-        ffa_set_regs_success(regs, 0, 0);
+        if ( ffa_fw_supports_fid(a1) )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        break;
+    case FFA_MSG_SEND2:
+        if ( ffa_fw_supports_fid(a1) || IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     case FFA_MSG_SEND_DIRECT_REQ2:
-        if ( ACCESS_ONCE(ctx->guest_vers) >= FFA_VERSION_1_2 )
+        if ( ACCESS_ONCE(ctx->guest_vers) >= FFA_VERSION_1_2 &&
+             ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ2) )
         {
             ffa_set_regs_success(regs, 0, 0);
         }
@@ -281,6 +310,11 @@ static void handle_features(struct cpu_user_regs *regs)
         break;
     case FFA_MEM_SHARE_64:
     case FFA_MEM_SHARE_32:
+        if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
+        {
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+            break;
+        }
         /*
          * We currently don't support dynamically allocated buffers. Report
          * that with 0 in bit[0] of w2.
@@ -688,6 +722,20 @@ static bool ffa_probe_fw(void)
                    ffa_fw_abi_needed[i].name);
     }
 
+    /*
+     * Hafnium v2.14 or earlier does not report FFA_RX_ACQUIRE in
+     * FFA_FEATURES even though it supports it.
+     */
+    if ( !ffa_fw_supports_fid(FFA_RX_ACQUIRE) &&
+         ffa_fw_supports_fid(FFA_MSG_SEND2) )
+    {
+        printk(XENLOG_WARNING
+               "ARM FF-A Firmware reports FFA_MSG_SEND2 without FFA_RX_ACQUIRE\n");
+        printk(XENLOG_WARNING
+               "ffa: assuming RX_ACQUIRE support (workaround)\n");
+        set_bit(FFA_ABI_BITNUM(FFA_RX_ACQUIRE), ffa_fw_abi_supported);
+    }
+
     if ( !ffa_rxtx_spmc_init() )
     {
         printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
--

2.50.1 (Apple Git-155)

Reviewed-by on v1 9/12
#30 lore raw

Re: [PATCH 09/12] xen/arm: ffa: Fix SEND2 SP support gating

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 10 lines · Bertrand Marquis

Xen only maps a VM RX/TX buffer into the SPMC when firmware supports FFA_RX_ACQUIRE. Without that mapping, the SPMC cannot access the VM TX buffer to relay FFA_MSG_SEND2 to a secure partition.

Advertise FFA_MSG_SEND2 to guests only when VM-to-VM is enabled or when the firmware supports both FFA_MSG_SEND2 and FFA_RX_ACQUIRE, and reject SEND2 to a secure partition otherwise. Add comments to document the mapping/ownership dependency.

Quoted 8 lines

Functional impact: SEND2 to a secure partition is reported as NOT_SUPPORTED when FFA_RX_ACQUIRE is absent.

---
 xen/arch/arm/tee/ffa.c     |  8 +++++++-
 xen/arch/arm/tee/ffa_msg.c | 11 +++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

Looks good:

Cheers,
Jens

Quoted 45 lines
xen/arch/arm/tee/ffa.c +7 -1
index e9e020bb0cb3..a46c6e5e67f1 100644
@@ -292,7 +292,13 @@ static void handle_features(struct cpu_user_regs *regs)
             ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         break;
     case FFA_MSG_SEND2:
-        if ( ffa_fw_supports_fid(a1) || IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+        /*
+         * Forwarding SEND2 to an SP requires the SPMC to see the VM TX buffer.
+         * We only map VM RX/TX into the SPMC when RX_ACQUIRE is supported.
+         */
+        if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) ||
+             (ffa_fw_supports_fid(FFA_MSG_SEND2) &&
+              ffa_fw_supports_fid(FFA_RX_ACQUIRE)) )
             ffa_set_regs_success(regs, 0, 0);
         else
             ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_msg.c +11 -1
index 10856fddcbc4..928f269f6c3a 100644
@@ -274,6 +274,17 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
             ret = FFA_RET_NOT_SUPPORTED;
             goto out;
         }
+        /*
+         * The SPMC needs access to the VM TX buffer to relay SEND2.
+         * We only map VM RX/TX into the SPMC when RX_ACQUIRE is supported.
+         */
+        if ( !ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+        {
+            ret = FFA_RET_NOT_SUPPORTED;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 to SP requires RX_ACQUIRE\n");
+            goto out;
+        }
 
         ret = ffa_simple_call(FFA_MSG_SEND2,
                               ((uint32_t)ffa_get_vm_id(src_d)) << 16, 0, 0, 0);
--

2.50.1 (Apple Git-155)

Reviewed-by on v1 10/12
#32 lore raw

Re: [PATCH 10/12] xen/arm: ffa: Reduce logging noise

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 14 lines · Bertrand Marquis

Several FF-A paths log warnings for expected or recoverable conditions, leading to noisy logs during normal operation.

Reduce log noise by:
- demoting unhandled FID warnings to debug
- ratelimiting notification and VM teardown failures
- printing the bit-15 convention warning once per boot
- adjusting log prefixes for hypervisor-only events
- adding debug diagnostics for RX/TX acquire failures, with busy cases ratelimited

No functional changes.

Quoted 7 lines
---
 xen/arch/arm/tee/ffa.c          | 11 ++++++-----
 xen/arch/arm/tee/ffa_notif.c    | 11 ++++++-----
 xen/arch/arm/tee/ffa_partinfo.c | 15 ++++++++-------
 xen/arch/arm/tee/ffa_rxtx.c     | 21 +++++++++++++++++++--
 4 files changed, 39 insertions(+), 19 deletions(-)

Looks good:

Cheers,
Jens

Quoted 191 lines
xen/arch/arm/tee/ffa.c +6 -5
index a46c6e5e67f1..d0b9c9f1ab2e 100644
@@ -461,7 +461,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         break;
 
     default:
-        gprintk(XENLOG_ERR, "ffa: unhandled fid 0x%x\n", fid);
+        gdprintk(XENLOG_DEBUG, "ffa: unhandled fid 0x%x\n", fid);
         e = FFA_RET_NOT_SUPPORTED;
         break;
     }
@@ -581,7 +581,8 @@ static void ffa_domain_teardown_continue(struct ffa_ctx *ctx, bool first_time)
 
     if ( retry )
     {
-        printk(XENLOG_G_INFO "%pd: ffa: Remaining cleanup, retrying\n", ctx->teardown_d);
+        printk(XENLOG_G_DEBUG "%pd: ffa: Remaining cleanup, retrying\n",
+               ctx->teardown_d);
 
         ctx->teardown_expire = NOW() + FFA_CTX_TEARDOWN_DELAY;
 
@@ -622,7 +623,7 @@ static void ffa_teardown_timer_callback(void *arg)
     if ( ctx )
         ffa_domain_teardown_continue(ctx, false /* !first_time */);
     else
-        printk(XENLOG_G_ERR "%s: teardown list is empty\n", __func__);
+        printk(XENLOG_ERR "%s: teardown list is empty\n", __func__);
 }
 
 /* This function is supposed to undo what ffa_domain_init() has done */
@@ -684,7 +685,7 @@ static bool ffa_probe_fw(void)
 
     if ( !ffa_get_version(&vers) )
     {
-        gprintk(XENLOG_ERR, "Cannot retrieve the FFA version\n");
+        printk(XENLOG_ERR "ffa: Cannot retrieve the FFA version\n");
         goto err_no_fw;
     }
 
@@ -724,7 +725,7 @@ static bool ffa_probe_fw(void)
             set_bit(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id),
                     ffa_fw_abi_supported);
         else
-            printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
+            printk(XENLOG_WARNING "ARM FF-A Firmware does not support %s\n",
                    ffa_fw_abi_needed[i].name);
     }
 
xen/arch/arm/tee/ffa_notif.c +6 -5
index 37b05747cd21..186e72641237 100644
@@ -292,8 +292,9 @@ static void notif_vm_pend_intr(uint16_t vm_id)
             break;
         }
     }
-    if ( !v )
-        printk(XENLOG_ERR "ffa: can't inject NPI, all vCPUs offline\n");
+    if ( !v && printk_ratelimit() )
+        printk(XENLOG_G_DEBUG "%pd: ffa: can't inject NPI, all vCPUs offline\n",
+               d);
 
 out_unlock:
     rcu_unlock_domain(d);
@@ -316,9 +317,9 @@ static void notif_sri_action(void *unused)
         res = ffa_get_ret_code(&resp);
         if ( res )
         {
-            if ( res != FFA_RET_NO_DATA )
-                printk(XENLOG_ERR "ffa: notification info get failed: error %d\n",
-                       res);
+            if ( res != FFA_RET_NO_DATA && printk_ratelimit() )
+                printk(XENLOG_WARNING
+                       "ffa: notification info get failed: error %d\n", res);
             return;
         }
 
xen/arch/arm/tee/ffa_partinfo.c +8 -7
index 6b01c4abe915..c9f186b3720c 100644
@@ -461,9 +461,11 @@ static bool init_subscribers(void *buf, uint16_t count, uint32_t fpi_size)
          */
         if ( !FFA_ID_IS_SECURE(fpi->id) )
         {
-            printk(XENLOG_ERR "ffa: Firmware is not using bit 15 convention for IDs !!\n"
-                              "ffa: Secure partition with id 0x%04x cannot be used\n",
-                              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
         {
@@ -632,11 +634,10 @@ bool ffa_partinfo_domain_destroy(struct domain *d)
         res = ffa_direct_req_send_vm(subscr_vm_destroyed[n], ffa_get_vm_id(d),
                                      FFA_MSG_SEND_VM_DESTROYED);
 
-        if ( res )
-        {
-            printk(XENLOG_ERR "%pd: ffa: Failed to report destruction of vm_id %u to %u: res %d\n",
+        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);
-        }
 
         /*
          * For these two error codes the hypervisor is expected to resend
xen/arch/arm/tee/ffa_rxtx.c +19 -3
index 450ce102cbdc..3147aba21ddf 100644
@@ -4,6 +4,7 @@
  */
 
 #include <xen/const.h>
+#include <xen/lib.h>
 #include <xen/domain_page.h>
 #include <xen/mm.h>
 #include <xen/sizes.h>
@@ -82,8 +83,9 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 
     if ( page_count > FFA_MAX_RXTX_PAGE_COUNT || !page_count )
     {
-        printk(XENLOG_ERR "ffa: RXTX_MAP: error: %u pages requested (limit %u)\n",
-               page_count, FFA_MAX_RXTX_PAGE_COUNT);
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: RXTX_MAP: error: %u pages requested (limit %u)\n",
+                 page_count, FFA_MAX_RXTX_PAGE_COUNT);
         return FFA_RET_INVALID_PARAMETERS;
     }
 
@@ -303,12 +305,15 @@ int32_t ffa_rx_acquire(struct ffa_ctx *ctx, void **buf, size_t *buf_size)
     if ( !ctx->page_count )
     {
         ret = FFA_RET_DENIED;
+        gdprintk(XENLOG_DEBUG, "ffa: RX acquire denied, no RX/TX mapped\n");
         goto out;
     }
 
     if ( !ctx->rx_is_free )
     {
         ret = FFA_RET_BUSY;
+        if ( printk_ratelimit() )
+            gdprintk(XENLOG_DEBUG, "ffa: RX acquire busy\n");
         goto out;
     }
 
@@ -316,7 +321,10 @@ int32_t ffa_rx_acquire(struct ffa_ctx *ctx, void **buf, size_t *buf_size)
     {
         ret = ffa_simple_call(FFA_RX_ACQUIRE, ctx->ffa_id, 0, 0, 0);
         if ( ret != FFA_RET_OK )
+        {
+            gdprintk(XENLOG_DEBUG, "ffa: RX acquire failed: %d\n", ret);
             goto out;
+        }
     }
     ctx->rx_is_free = false;
     *buf = ctx->rx;
@@ -355,13 +363,22 @@ int32_t ffa_tx_acquire(struct ffa_ctx *ctx, const void **buf, size_t *buf_size)
     int32_t ret = FFA_RET_DENIED;
 
     if ( !spin_trylock(&ctx->tx_lock) )
+    {
+        gdprintk(XENLOG_DEBUG, "ffa: TX acquire busy\n");
         return FFA_RET_BUSY;
+    }
 
     if ( !ctx->page_count )
+    {
+        gdprintk(XENLOG_DEBUG, "ffa: TX acquire denied, no RX/TX mapped\n");
         goto err_unlock;
+    }
 
     if ( !ctx->tx )
+    {
+        gdprintk(XENLOG_DEBUG, "ffa: TX acquire denied, TX buffer missing\n");
         goto err_unlock;
+    }
 
     *buf = ctx->tx;
     *buf_size = ctx->page_count * FFA_PAGE_SIZE;
--

2.50.1 (Apple Git-155)

#34 lore raw

Re: [PATCH 11/12] xen/arm: ffa: Add MEM_SHARE page diagnostics

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 37 lines · Bertrand Marquis

MEM_SHARE failures in get_shm_pages() are silent, which makes malformed ranges and page mapping failures hard to diagnose.

Add debug logging for page validation failures:
- descriptor validation failures (unaligned, range short/overflow)
- per-page mapping failures (unmapped GFN, wrong p2m type)
- address overflow detection in range walks

Ratelimit temporary reclaim failures and log permanent reclaim failures as errors.

No functional changes.

---
 xen/arch/arm/tee/ffa_shm.c | 73 ++++++++++++++++++++++++++++++++------
 1 file changed, 63 insertions(+), 10 deletions(-)
xen/arch/arm/tee/ffa_shm.c +6 -0
index 905a64e3db01..89161753e922 100644
@@ -169,6 +169,12 @@ static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
     uint64_t addr;
     uint64_t page_count;
     uint64_t gaddr;
+    int32_t ret = FFA_RET_OK;
+    const char *reason = NULL;
+    unsigned int bad_rg = 0;
+    unsigned int bad_pg = 0;
+    unsigned long bad_addr = 0;
+    p2m_type_t bad_t = p2m_invalid;
 
     for ( n = 0; n < range_count; n++ )
     {

xen/arch/arm/tee/ffa_shm.c static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm, · line 190

Quoted 10 lines
xen/arch/arm/tee/ffa_shm.c +6 -1
@@ -176,34 +182,78 @@ static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
         addr = ACCESS_ONCE(range[n].address);
 
         if ( !IS_ALIGNED(addr, FFA_PAGE_SIZE) )
-            return FFA_RET_INVALID_PARAMETERS;
+        {
+            ret = FFA_RET_INVALID_PARAMETERS;
+            reason = "unaligned";
+            bad_rg = n;
+            bad_addr = (unsigned long)addr;
+            goto out;

The extra help variables clutter the code, and the debug message requires one to read the code to understand it. I'd prefer separate prints for each error location. For example:
gdprintk(XENLOG_DEBUG, "ffa: mem share pages invalid: unalinged range %u address %#lx\n", ...)
return FFA_RET_INVALID_PARAMETERS;

It should result in fewer lines of code and clearer debug messages.

Cheers,
Jens

Quoted 101 lines
xen/arch/arm/tee/ffa_shm.c +51 -10
+        }
 
         for ( m = 0; m < page_count; m++ )
         {
             if ( pg_idx >= shm->page_count )
-                return FFA_RET_INVALID_PARAMETERS;
+            {
+                ret = FFA_RET_INVALID_PARAMETERS;
+                reason = "range overflow";
+                bad_rg = n;
+                bad_pg = m;
+                goto out;
+            }
 
             if ( !ffa_safe_addr_add(addr, m) )
-                return FFA_RET_INVALID_PARAMETERS;
+            {
+                ret = FFA_RET_INVALID_PARAMETERS;
+                reason = "addr overflow";
+                bad_rg = n;
+                bad_pg = m;
+                bad_addr = (unsigned long)addr;
+                goto out;
+            }
 
             gaddr = addr + m * FFA_PAGE_SIZE;
             gfn = gaddr_to_gfn(gaddr);
             shm->pages[pg_idx] = get_page_from_gfn(d, gfn_x(gfn), &t,
                                                   P2M_ALLOC);
             if ( !shm->pages[pg_idx] )
-                return FFA_RET_DENIED;
+            {
+                ret = FFA_RET_DENIED;
+                reason = "gfn unmapped";
+                bad_rg = n;
+                bad_pg = m;
+                bad_addr = (unsigned long)gaddr;
+                goto out;
+            }
             /* Only normal RW RAM for now */
             if ( t != p2m_ram_rw )
-                return FFA_RET_DENIED;
+            {
+                ret = FFA_RET_DENIED;
+                reason = "p2m type";
+                bad_rg = n;
+                bad_pg = m;
+                bad_addr = (unsigned long)gaddr;
+                bad_t = t;
+                goto out;
+            }
             pg_idx++;
         }
     }
 
     /* The ranges must add up */
     if ( pg_idx < shm->page_count )
-        return FFA_RET_INVALID_PARAMETERS;
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        reason = "range short";
+        bad_pg = pg_idx;
+        goto out;
+    }
 
-    return FFA_RET_OK;
+out:
+    if ( ret )
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: mem share pages invalid: %s rg %u pg %u addr %#lx p2m %u\n",
+                 reason ? reason : "unknown", bad_rg, bad_pg, bad_addr, bad_t);
+    return ret;
 }
 
 static void put_shm_pages(struct ffa_shm_mem *shm)
@@ -759,8 +809,10 @@ bool ffa_shm_domain_destroy(struct domain *d)
              * A temporary error that may get resolved a bit later, it's
              * worth retrying.
              */
-            printk(XENLOG_G_INFO "%pd: ffa: Failed to reclaim handle %#lx : %d\n",
-                   d, shm->handle, res);
+            if ( printk_ratelimit() )
+                printk(XENLOG_G_WARNING
+                       "%pd: ffa: Failed to reclaim handle %#lx : %d\n",
+                       d, shm->handle, res);
             break; /* We will retry later */
         default:
             /*
@@ -772,7 +824,8 @@ bool ffa_shm_domain_destroy(struct domain *d)
              * FFA_RET_NO_MEMORY might be a temporary error as it it could
              * succeed if retried later, but treat it as permanent for now.
              */
-            printk(XENLOG_G_INFO "%pd: ffa: Permanent failure to reclaim handle %#lx : %d\n",
+            printk(XENLOG_G_ERR
+                   "%pd: ffa: Permanent failure to reclaim handle %#lx : %d\n",
                    d, shm->handle, res);
 
             /*
--

2.50.1 (Apple Git-155)

#37 lore raw

Re: [PATCH 12/12] xen/arm: ffa: Add message parameter diagnostics

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 73 lines · Bertrand Marquis

MSG_SEND2 and direct request validation failures are silent, making it hard to diagnose invalid IDs, oversized messages, or unsupported destination types.

Add debug logs for parameter validation failures:
- invalid endpoint IDs
- oversized messages
- unsupported destination types
- invalid sender/receiver combinations
- ratelimit MSG_SEND2 busy failures to avoid log flooding

No functional changes.

---
 xen/arch/arm/tee/ffa_msg.c | 45 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
xen/arch/arm/tee/ffa_msg.c +13 -0
index 928f269f6c3a..cc273c9a8e80 100644
@@ -4,6 +4,7 @@
  */
 
 #include <xen/const.h>
+#include <xen/lib.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
 
@@ -100,6 +101,7 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     if ( !ffa_fw_supports_fid(fid) )
     {
         ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: direct req fid %#x not supported\n", fid);
         goto out;
     }
 
@@ -108,6 +110,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
          (src_dst & GENMASK(15,0)) == ffa_get_vm_id(d) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req invalid src/dst %#x\n",
+                 (uint32_t)src_dst);
         goto out;
     }
 
@@ -115,6 +120,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
     {
         ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req to non-secure dst %#x\n",
+                 (uint32_t)(src_dst & GENMASK(15, 0)));
         goto out;
     }
 
@@ -166,7 +174,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     /* This is also checking that dest is not src */
     ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
     if ( ret )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 lookup failed: dst %#x ret %d\n",
+                 dst_id, ret);
         return ret;
+    }
 
     /* This also checks that destination has set a Rx buffer */
     ret = ffa_rx_acquire(dst_ctx , &rx_buf, &rx_size);

xen/arch/arm/tee/ffa_msg.c static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf, · line 217

Quoted 6 lines
xen/arch/arm/tee/ffa_msg.c +3 -0
@@ -199,6 +212,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     /* receiver rx buffer will be released by the receiver*/
 
 out_unlock:
+    if ( ret )
+    {
+        if ( ret != FFA_RET_BUSY || printk_ratelimit() )

Shouldn't this be && ?

Cheers,
Jens

Quoted 107 lines
xen/arch/arm/tee/ffa_msg.c +29 -1
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to %#x failed: %d\n",
+                     dst_id, ret);
+    }
     rcu_unlock_domain(dst_d);
     if ( !ret )
         ffa_raise_rx_buffer_full(dst_d);
@@ -226,7 +245,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
     ret = ffa_tx_acquire(src_ctx, &tx_buf, &tx_size);
     if ( ret != FFA_RET_OK )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 TX acquire failed: %d\n", ret);
         return ret;
+    }
 
     /* create a copy of the message header */
     memcpy(&src_msg, tx_buf, sizeof(src_msg));
@@ -238,6 +261,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
          dst_id == ffa_get_vm_id(src_d) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid src/dst src %#x dst %#x\n",
+                 src_id, dst_id);
         goto out;
     }
 
@@ -246,6 +272,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
         if (src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_1))
         {
             ret = FFA_RET_INVALID_PARAMETERS;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 invalid msg_offset %u (v1.1)\n",
+                     src_msg.msg_offset);
             goto out;
         }
         /* Set uuid to Nil UUID for v1.1 guests */
@@ -255,6 +284,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
     else if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_2) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_offset %u (v1.2)\n",
+                 src_msg.msg_offset);
         goto out;
     }
 
@@ -263,6 +295,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
             src_msg.msg_size > (tx_size - src_msg.msg_offset) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_size %u offset %u tx %zu\n",
+                 src_msg.msg_size, src_msg.msg_offset, tx_size);
         goto out;
     }
 
@@ -272,6 +307,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
         if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
         {
             ret = FFA_RET_NOT_SUPPORTED;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 to SP not supported\n");
             goto out;
         }
         /*
@@ -288,6 +325,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
         ret = ffa_simple_call(FFA_MSG_SEND2,
                               ((uint32_t)ffa_get_vm_id(src_d)) << 16, 0, 0, 0);
+        if ( ret )
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to SP failed: %d\n", ret);
     }
     else if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
@@ -295,7 +334,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
         ret = ffa_msg_send2_vm(dst_id, tx_buf, &src_msg);
     }
     else
+    {
         ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 to VM disabled (dst %#x)\n", dst_id);
+    }
 
 out:
     ffa_tx_release(src_ctx);
@@ -311,6 +354,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
     if ( !ffa_fw_supports_fid(fid) )
     {
         ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run fid %#x not supported\n", fid);
         goto out;
     }
 
@@ -322,6 +366,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
     if ( !FFA_ID_IS_SECURE(dst >> 16) )
     {
         ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run to non-secure dst %#x\n", dst);
         goto out;
     }
 
--

2.50.1 (Apple Git-155)

#23 lore raw

Re: [PATCH 07/12] xen/arm: ffa: Fix RXTX_UNMAP ownership race

In reply to Jens Wiklander

Hi Jens,

Quoted 55 lines · Jens Wiklander

Hi Bertrand,

Quoted 49 lines · Bertrand Marquis

rxtx_unmap() checks RX ownership without holding the RX/TX locks and only enforces the ownership rule when FFA_RX_ACQUIRE is supported. This allows a vCPU to acquire RX between the check and unmap, and it lets RXTX_UNMAP proceed while RX is owned when buffers are not forwarded to firmware.

Hold rx_lock/tx_lock across the ownership check and unmap, and deny RXTX_UNMAP whenever RX is owned, independent of RX_ACQUIRE support. For teardown, release RX ownership under the same lock window; use FFA_RX_RELEASE directly because rx_lock is held, and clear the local flag when the firmware path is unavailable.

Functional impact: RXTX_UNMAP now reliably returns DENIED while RX is owned, and teardown releases/clears ownership without a race.

---
xen/arch/arm/tee/ffa_rxtx.c | 36 +++++++++++++++++++++++++++++++++---

1 file changed, 33 insertions(+), 3 deletions(-)
xen/arch/arm/tee/ffa_rxtx.c +12 -1
index eff95a7955d7..450ce102cbdc 100644
@@ -220,7 +220,7 @@ err_unlock_rxtx:
    return ret;
 }
 
-static int32_t rxtx_unmap(struct domain *d)
+static int32_t rxtx_unmap(struct domain *d, bool teardown)
 {
    struct ffa_ctx *ctx = d->arch.tee;
    int32_t ret = FFA_RET_OK;
@@ -234,6 +234,36 @@ static int32_t rxtx_unmap(struct domain *d)
        goto err_unlock_rxtx;
    }
 
+    if ( !ctx->rx_is_free )
+    {
+        if ( teardown )
+        {
+            if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+            {
+                int32_t rel_ret;
+
+                /* Can't use ffa_rx_release() while holding rx_lock. */
+                rel_ret = ffa_simple_call(FFA_RX_RELEASE, ctx->ffa_id,
+                                          0, 0, 0);
Quoted 8 lines

xen/arch/arm/tee/ffa_rxtx.c static int32_t rxtx_unmap(struct domain *d) · line 253

Quoted 6 lines
xen/arch/arm/tee/ffa_rxtx.c +6 -0
+                if ( rel_ret )
+                    gdprintk(XENLOG_DEBUG,
+                             "ffa: RX release during teardown failed: %d\n",
+                             rel_ret);
+                else
+                    ctx->rx_is_free = true;

I don't see why this assignment is needed, or the one just below.

True, in the teardown case we do not care at all.
I will remove those 2.

Quoted 36 lines
Quoted 27 lines
xen/arch/arm/tee/ffa_rxtx.c +14 -1
+            }
+            else
+                ctx->rx_is_free = true;
+        }
+        else
+        {
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: RXTX_UNMAP denied, RX buffer owned by VM\n");
+            ret = FFA_RET_DENIED;
+            goto err_unlock_rxtx;
+        }
+    }
+
    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
    {
        ret = ffa_rxtx_unmap(ffa_get_vm_id(d));
@@ -261,7 +291,7 @@ err_unlock_rxtx:
 
 int32_t ffa_handle_rxtx_unmap(void)
 {
-    return rxtx_unmap(current->domain);
+    return rxtx_unmap(current->domain, false);
 }
 
 int32_t ffa_rx_acquire(struct ffa_ctx *ctx, void **buf, size_t *buf_size)

xen/arch/arm/tee/ffa_rxtx.c int32_t ffa_rxtx_domain_init(struct domain *d) · line 402

Quoted 5 lines
xen/arch/arm/tee/ffa_rxtx.c +1 -1
@@ -369,7 +399,7 @@ int32_t ffa_rxtx_domain_init(struct domain *d)
 
 void ffa_rxtx_domain_destroy(struct domain *d)
 {
-    rxtx_unmap(d);
+    rxtx_unmap(d, true);

How about adding a /* teardown */ just after true as a reminder of what true is supposed to represent.

Quoted 2 lines

Adding such comments isn't very common, but we're doing it at a few places in the source tree, and I think it helps when reading the code.

Agree i will add /* teardown */ just after true here.

Cheers
Bertrand

#26 lore raw

Re: [PATCH 08/12] xen/arm: ffa: Fix FFA_FEATURES validation

In reply to Jens Wiklander

Hi Jens,

Quoted 61 lines · Jens Wiklander

Hi Bertrand,

Quoted 55 lines · Bertrand Marquis

FFA_FEATURES currently accepts non-zero input properties (w2-w7) from guests and advertises several ABIs unconditionally, even when firmware support is missing or when the ABI is physical-instance-only. This can mislead guests about what Xen can actually provide and violates FF-A calling conventions. Some SPMCs (Hafnium v2.14 or earlier) also fail to report FFA_RX_ACQUIRE despite supporting it.

Update FFA_FEATURES validation to match spec and firmware support:
- reject non-zero w2-w7 input properties with INVALID_PARAMETERS
- reject 64-bit calling conventions from 32-bit guests with NOT_SUPPORTED
- return NOT_SUPPORTED for physical-instance-only ABIs (FFA_NOTIFICATION_BITMAP_{CREATE,DESTROY}, FFA_RX_ACQUIRE)
- advertise FFA_INTERRUPT as supported
- gate message ABIs on firmware support:
- FFA_MSG_SEND_DIRECT_REQ_{32,64}
- FFA_MSG_SEND_DIRECT_REQ2 (also requires FF-A 1.2 negotiation)
- FFA_MSG_SEND2 (or VM-to-VM enabled)
- report MEM_SHARE_{32,64} only when FFA_MEM_SHARE_64 is supported
- stop advertising FFA_MSG_YIELD (not implemented)

Update firmware probing: drop FFA_MEM_SHARE_32 checks (deprecated) and add FFA_RX_ACQUIRE to the probed set. If FFA_MSG_SEND2 is reported but FFA_RX_ACQUIRE is not, assume RX_ACQUIRE support and warn to work around the Hafnium bug.

Functional impact: guests now see ABI support that reflects firmware capabilities and Xen implementation status. When SEND2 is present but RX_ACQUIRE is not reported, Xen assumes RX_ACQUIRE support.

---
xen/arch/arm/tee/ffa.c | 62 +++++++++++++++++++++++++++++++++++++-----

1 file changed, 55 insertions(+), 7 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index 6de2b9f8ac8e..e9e020bb0cb3 100644
@@ -91,10 +91,10 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
    FW_ABI(FFA_PARTITION_INFO_GET),
    FW_ABI(FFA_NOTIFICATION_INFO_GET_64),
    FW_ABI(FFA_NOTIFICATION_GET),
+    FW_ABI(FFA_RX_ACQUIRE),
    FW_ABI(FFA_RX_RELEASE),
    FW_ABI(FFA_RXTX_MAP_64),
    FW_ABI(FFA_RXTX_UNMAP),
-    FW_ABI(FFA_MEM_SHARE_32),
    FW_ABI(FFA_MEM_SHARE_64),
    FW_ABI(FFA_MEM_RECLAIM),
    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
@@ -240,19 +240,39 @@ static void handle_features(struct cpu_user_regs *regs)
    struct ffa_ctx *ctx = d->arch.tee;
    unsigned int n;
 
Quoted 9 lines

xen/arch/arm/tee/ffa.c static void handle_features(struct cpu_user_regs *regs) · line 248

Quoted 6 lines
xen/arch/arm/tee/ffa.c +6 -0
+    /*
+     * Xen does not accept any non-zero FFA_FEATURES input properties from
+     * VMs. The spec only defines w2 input properties for FFA_MEM_RETRIEVE_REQ
+     * (NS-bit negotiation for SP/SPMC) and FFA_RXTX_MAP (buffer size and
+     * alignment), so w2 must be MBZ for our callers.
+     */

The spec (version 1.2) lists them as SBZ, except for w2, which is MBZ, for Feature IDs.

Very true, this should only check w2 which is anyway defined as MBZ when not used.
w3-w7 were MBZ in previous versions of FF-A but are in fact SBZ in 1.2 so we should ignore them

Quoted 1 line

However, if we're to return an error, invalid parameters is a better choice.

In fact the spec is actually saying the following:
If the FF-A interface or feature that was queried is not implemented or invalid, the callee completes this call with an invocation of the FFA_ERROR interface with the NOT_SUPPORTED error code.

So there is no case for INVALID_PARAMETER.

So in fact i should:
- return NOT_SUPPORTED if w2 is not 0
- ignore w3-w7

Can you confirm that you have the same reading of the spec than me ?

Cheers
Bertrand

#35 lore raw

Re: [PATCH 11/12] xen/arm: ffa: Add MEM_SHARE page diagnostics

In reply to Jens Wiklander

Hi Jens,

Quoted 61 lines · Jens Wiklander

Hi Bertrand,

Quoted 37 lines · Bertrand Marquis

MEM_SHARE failures in get_shm_pages() are silent, which makes malformed ranges and page mapping failures hard to diagnose.

Add debug logging for page validation failures:
- descriptor validation failures (unaligned, range short/overflow)
- per-page mapping failures (unmapped GFN, wrong p2m type)
- address overflow detection in range walks

Ratelimit temporary reclaim failures and log permanent reclaim failures as errors.

No functional changes.

---
xen/arch/arm/tee/ffa_shm.c | 73 ++++++++++++++++++++++++++++++++------

1 file changed, 63 insertions(+), 10 deletions(-)
xen/arch/arm/tee/ffa_shm.c +6 -0
index 905a64e3db01..89161753e922 100644
@@ -169,6 +169,12 @@ static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
    uint64_t addr;
    uint64_t page_count;
    uint64_t gaddr;
+    int32_t ret = FFA_RET_OK;
+    const char *reason = NULL;
+    unsigned int bad_rg = 0;
+    unsigned int bad_pg = 0;
+    unsigned long bad_addr = 0;
+    p2m_type_t bad_t = p2m_invalid;
 
    for ( n = 0; n < range_count; n++ )
    {

xen/arch/arm/tee/ffa_shm.c static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm, · line 190

Quoted 10 lines
xen/arch/arm/tee/ffa_shm.c +6 -1
@@ -176,34 +182,78 @@ static int32_t get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
        addr = ACCESS_ONCE(range[n].address);
 
        if ( !IS_ALIGNED(addr, FFA_PAGE_SIZE) )
-            return FFA_RET_INVALID_PARAMETERS;
+        {
+            ret = FFA_RET_INVALID_PARAMETERS;
+            reason = "unaligned";
+            bad_rg = n;
+            bad_addr = (unsigned long)addr;
+            goto out;

The extra help variables clutter the code, and the debug message requires one to read the code to understand it. I'd prefer separate prints for each error location. For example:
gdprintk(XENLOG_DEBUG, "ffa: mem share pages invalid: unalinged range %u address %#lx\n", ...)
return FFA_RET_INVALID_PARAMETERS;

Quoted 1 line

It should result in fewer lines of code and clearer debug messages.

Yes you are right, single prints on each case will be easier to find and result in less code. This was a bit useless.

I will rework it like that in v2 which will also remove to goto :-)

Cheers
Bertrand

#38 lore raw

Re: [PATCH 12/12] xen/arm: ffa: Add message parameter diagnostics

In reply to Jens Wiklander

Hi Jens,

Quoted 81 lines · Jens Wiklander

Hi Bertrand,

Quoted 75 lines · Bertrand Marquis

MSG_SEND2 and direct request validation failures are silent, making it hard to diagnose invalid IDs, oversized messages, or unsupported destination types.

Add debug logs for parameter validation failures:
- invalid endpoint IDs
- oversized messages
- unsupported destination types
- invalid sender/receiver combinations
- ratelimit MSG_SEND2 busy failures to avoid log flooding

No functional changes.

---
xen/arch/arm/tee/ffa_msg.c | 45 ++++++++++++++++++++++++++++++++++++++

1 file changed, 45 insertions(+)
xen/arch/arm/tee/ffa_msg.c +13 -0
index 928f269f6c3a..cc273c9a8e80 100644
@@ -4,6 +4,7 @@
 */
 
 #include <xen/const.h>
+#include <xen/lib.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
 
@@ -100,6 +101,7 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
    if ( !ffa_fw_supports_fid(fid) )
    {
        ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: direct req fid %#x not supported\n", fid);
        goto out;
    }
 
@@ -108,6 +110,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
         (src_dst & GENMASK(15,0)) == ffa_get_vm_id(d) )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req invalid src/dst %#x\n",
+                 (uint32_t)src_dst);
        goto out;
    }
 
@@ -115,6 +120,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
    if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
    {
        ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req to non-secure dst %#x\n",
+                 (uint32_t)(src_dst & GENMASK(15, 0)));
        goto out;
    }
 
@@ -166,7 +174,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
    /* This is also checking that dest is not src */
    ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
    if ( ret )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 lookup failed: dst %#x ret %d\n",
+                 dst_id, ret);
        return ret;
+    }
 
    /* This also checks that destination has set a Rx buffer */
    ret = ffa_rx_acquire(dst_ctx , &rx_buf, &rx_size);
@@ -199,6 +212,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
    /* receiver rx buffer will be released by the receiver*/
 
Quoted 6 lines

xen/arch/arm/tee/ffa_msg.c static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf, · line 217

Quoted 4 lines

out_unlock:

xen/arch/arm/tee/ffa_msg.c +3 -0
+    if ( ret )
+    {
+        if ( ret != FFA_RET_BUSY || printk_ratelimit() )

Shouldn't this be && ?

The intent here is to log all non BUSY errors but only log BUSY when ratelimit allows to reduce the amount of logs in case of someone polling when the receiver RX buffer is busy.

Maybe I should add a comment to make that clearer ?

Cheers
Bertrand

Quoted 111 lines

Cheers,
Jens

Quoted 107 lines
xen/arch/arm/tee/ffa_msg.c +27 -0
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to %#x failed: %d\n",
+                     dst_id, ret);
+    }
    rcu_unlock_domain(dst_d);
    if ( !ret )
        ffa_raise_rx_buffer_full(dst_d);
@@ -226,7 +245,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
    ret = ffa_tx_acquire(src_ctx, &tx_buf, &tx_size);
    if ( ret != FFA_RET_OK )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 TX acquire failed: %d\n", ret);
        return ret;
+    }
 
    /* create a copy of the message header */
    memcpy(&src_msg, tx_buf, sizeof(src_msg));
@@ -238,6 +261,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
         dst_id == ffa_get_vm_id(src_d) )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid src/dst src %#x dst %#x\n",
+                 src_id, dst_id);
        goto out;
    }
 
@@ -246,6 +272,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
        if (src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_1))
        {
            ret = FFA_RET_INVALID_PARAMETERS;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 invalid msg_offset %u (v1.1)\n",
+                     src_msg.msg_offset);
            goto out;
        }
        /* Set uuid to Nil UUID for v1.1 guests */
@@ -255,6 +284,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
    else if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_2) )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_offset %u (v1.2)\n",
+                 src_msg.msg_offset);
        goto out;
    }
 
@@ -263,6 +295,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
            src_msg.msg_size > (tx_size - src_msg.msg_offset) )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_size %u offset %u tx %zu\n",
+                 src_msg.msg_size, src_msg.msg_offset, tx_size);
        goto out;
    }
 
@@ -272,6 +307,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
        if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
        {
            ret = FFA_RET_NOT_SUPPORTED;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 to SP not supported\n");
            goto out;
        }
        /*
@@ -288,6 +325,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
        ret = ffa_simple_call(FFA_MSG_SEND2,
                              ((uint32_t)ffa_get_vm_id(src_d)) << 16, 0, 0, 0);
+        if ( ret )
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to SP failed: %d\n", ret);
    }
    else if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
    {
@@ -295,7 +334,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
        ret = ffa_msg_send2_vm(dst_id, tx_buf, &src_msg);
    }
    else
+    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 to VM disabled (dst %#x)\n", dst_id);
+    }
 

out:

    ffa_tx_release(src_ctx);
xen/arch/arm/tee/ffa_msg.c +2 -1
@@ -311,6 +354,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
    if ( !ffa_fw_supports_fid(fid) )
    {
        ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run fid %#x not supported\n", fid);
        goto out;
    }
 
@@ -322,6 +366,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
    if ( !FFA_ID_IS_SECURE(dst >> 16) )
    {
        ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run to non-secure dst %#x\n", dst);
        goto out;
    }
 
--

2.50.1 (Apple Git-155)

#27 lore raw

Re: [PATCH 08/12] xen/arm: ffa: Fix FFA_FEATURES validation

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 79 lines · Bertrand Marquis

Hi Jens,

Quoted 61 lines · Jens Wiklander

Hi Bertrand,

Quoted 55 lines · Bertrand Marquis

FFA_FEATURES currently accepts non-zero input properties (w2-w7) from guests and advertises several ABIs unconditionally, even when firmware support is missing or when the ABI is physical-instance-only. This can mislead guests about what Xen can actually provide and violates FF-A calling conventions. Some SPMCs (Hafnium v2.14 or earlier) also fail to report FFA_RX_ACQUIRE despite supporting it.

Update FFA_FEATURES validation to match spec and firmware support:
- reject non-zero w2-w7 input properties with INVALID_PARAMETERS
- reject 64-bit calling conventions from 32-bit guests with NOT_SUPPORTED
- return NOT_SUPPORTED for physical-instance-only ABIs (FFA_NOTIFICATION_BITMAP_{CREATE,DESTROY}, FFA_RX_ACQUIRE)
- advertise FFA_INTERRUPT as supported
- gate message ABIs on firmware support:
- FFA_MSG_SEND_DIRECT_REQ_{32,64}
- FFA_MSG_SEND_DIRECT_REQ2 (also requires FF-A 1.2 negotiation)
- FFA_MSG_SEND2 (or VM-to-VM enabled)
- report MEM_SHARE_{32,64} only when FFA_MEM_SHARE_64 is supported
- stop advertising FFA_MSG_YIELD (not implemented)

Update firmware probing: drop FFA_MEM_SHARE_32 checks (deprecated) and add FFA_RX_ACQUIRE to the probed set. If FFA_MSG_SEND2 is reported but FFA_RX_ACQUIRE is not, assume RX_ACQUIRE support and warn to work around the Hafnium bug.

Functional impact: guests now see ABI support that reflects firmware capabilities and Xen implementation status. When SEND2 is present but RX_ACQUIRE is not reported, Xen assumes RX_ACQUIRE support.

---
xen/arch/arm/tee/ffa.c | 62 +++++++++++++++++++++++++++++++++++++-----

1 file changed, 55 insertions(+), 7 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index 6de2b9f8ac8e..e9e020bb0cb3 100644
@@ -91,10 +91,10 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
    FW_ABI(FFA_PARTITION_INFO_GET),
    FW_ABI(FFA_NOTIFICATION_INFO_GET_64),
    FW_ABI(FFA_NOTIFICATION_GET),
+    FW_ABI(FFA_RX_ACQUIRE),
    FW_ABI(FFA_RX_RELEASE),
    FW_ABI(FFA_RXTX_MAP_64),
    FW_ABI(FFA_RXTX_UNMAP),
-    FW_ABI(FFA_MEM_SHARE_32),
    FW_ABI(FFA_MEM_SHARE_64),
    FW_ABI(FFA_MEM_RECLAIM),
    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
@@ -240,19 +240,39 @@ static void handle_features(struct cpu_user_regs *regs)
    struct ffa_ctx *ctx = d->arch.tee;
    unsigned int n;
 
Quoted 9 lines

xen/arch/arm/tee/ffa.c static void handle_features(struct cpu_user_regs *regs) · line 248

Quoted 6 lines
xen/arch/arm/tee/ffa.c +6 -0
+    /*
+     * Xen does not accept any non-zero FFA_FEATURES input properties from
+     * VMs. The spec only defines w2 input properties for FFA_MEM_RETRIEVE_REQ
+     * (NS-bit negotiation for SP/SPMC) and FFA_RXTX_MAP (buffer size and
+     * alignment), so w2 must be MBZ for our callers.
+     */

The spec (version 1.2) lists them as SBZ, except for w2, which is MBZ, for Feature IDs.

Very true, this should only check w2 which is anyway defined as MBZ when not used.
w3-w7 were MBZ in previous versions of FF-A but are in fact SBZ in 1.2 so we should ignore them

Quoted 8 lines
Quoted 1 line

However, if we're to return an error, invalid parameters is a better choice.

In fact the spec is actually saying the following:
If the FF-A interface or feature that was queried is not implemented or invalid, the callee completes this call with an invocation of the FFA_ERROR interface with the NOT_SUPPORTED error code.

So there is no case for INVALID_PARAMETER.

You're right.

Quoted 6 lines

So in fact i should:
- return NOT_SUPPORTED if w2 is not 0
- ignore w3-w7

Can you confirm that you have the same reading of the spec than me ?

The 1.2 spec only says this w2 is MBZ for Feature IDs, and that w2 is SBZ for FFA_RXTX_MAP. The 1.3 spec says the same, except that in Table 13.14: Feature IDs and properties table, it lists w2 as SBZ.

Note that FFA_MEM_RETRIEVE_REQ has bits defined in w2, and the unknown bits are SBZ.

Based on that, I'm inclined to keep it simple and ignore w2-w7.

Cheers,
Jens

#39 lore raw

Re: [PATCH 12/12] xen/arm: ffa: Add message parameter diagnostics

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 91 lines · Bertrand Marquis

Hi Jens,

Quoted 81 lines · Jens Wiklander

Hi Bertrand,

Quoted 75 lines · Bertrand Marquis

MSG_SEND2 and direct request validation failures are silent, making it hard to diagnose invalid IDs, oversized messages, or unsupported destination types.

Add debug logs for parameter validation failures:
- invalid endpoint IDs
- oversized messages
- unsupported destination types
- invalid sender/receiver combinations
- ratelimit MSG_SEND2 busy failures to avoid log flooding

No functional changes.

---
xen/arch/arm/tee/ffa_msg.c | 45 ++++++++++++++++++++++++++++++++++++++

1 file changed, 45 insertions(+)
xen/arch/arm/tee/ffa_msg.c +13 -0
index 928f269f6c3a..cc273c9a8e80 100644
@@ -4,6 +4,7 @@
 */
 
 #include <xen/const.h>
+#include <xen/lib.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
 
@@ -100,6 +101,7 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
    if ( !ffa_fw_supports_fid(fid) )
    {
        ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: direct req fid %#x not supported\n", fid);
        goto out;
    }
 
@@ -108,6 +110,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
         (src_dst & GENMASK(15,0)) == ffa_get_vm_id(d) )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req invalid src/dst %#x\n",
+                 (uint32_t)src_dst);
        goto out;
    }
 
@@ -115,6 +120,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
    if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
    {
        ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req to non-secure dst %#x\n",
+                 (uint32_t)(src_dst & GENMASK(15, 0)));
        goto out;
    }
 
@@ -166,7 +174,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
    /* This is also checking that dest is not src */
    ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
    if ( ret )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 lookup failed: dst %#x ret %d\n",
+                 dst_id, ret);
        return ret;
+    }
 
    /* This also checks that destination has set a Rx buffer */
    ret = ffa_rx_acquire(dst_ctx , &rx_buf, &rx_size);
@@ -199,6 +212,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
    /* receiver rx buffer will be released by the receiver*/
 
Quoted 6 lines

xen/arch/arm/tee/ffa_msg.c static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf, · line 217

Quoted 4 lines

out_unlock:

xen/arch/arm/tee/ffa_msg.c +3 -0
+    if ( ret )
+    {
+        if ( ret != FFA_RET_BUSY || printk_ratelimit() )

Shouldn't this be && ?

Quoted 5 lines

The intent here is to log all non BUSY errors but only log BUSY when ratelimit allows to reduce the amount of logs in case of someone polling when the receiver RX buffer is busy.

Maybe I should add a comment to make that clearer ?

Yes, please.

Cheers,
Jens

Quoted 116 lines

Cheers
Bertrand

Quoted 111 lines

Cheers,
Jens

Quoted 107 lines
xen/arch/arm/tee/ffa_msg.c +27 -0
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to %#x failed: %d\n",
+                     dst_id, ret);
+    }
    rcu_unlock_domain(dst_d);
    if ( !ret )
        ffa_raise_rx_buffer_full(dst_d);
@@ -226,7 +245,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
    ret = ffa_tx_acquire(src_ctx, &tx_buf, &tx_size);
    if ( ret != FFA_RET_OK )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 TX acquire failed: %d\n", ret);
        return ret;
+    }
 
    /* create a copy of the message header */
    memcpy(&src_msg, tx_buf, sizeof(src_msg));
@@ -238,6 +261,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
         dst_id == ffa_get_vm_id(src_d) )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid src/dst src %#x dst %#x\n",
+                 src_id, dst_id);
        goto out;
    }
 
@@ -246,6 +272,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
        if (src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_1))
        {
            ret = FFA_RET_INVALID_PARAMETERS;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 invalid msg_offset %u (v1.1)\n",
+                     src_msg.msg_offset);
            goto out;
        }
        /* Set uuid to Nil UUID for v1.1 guests */
@@ -255,6 +284,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
    else if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_2) )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_offset %u (v1.2)\n",
+                 src_msg.msg_offset);
        goto out;
    }
 
@@ -263,6 +295,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
            src_msg.msg_size > (tx_size - src_msg.msg_offset) )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_size %u offset %u tx %zu\n",
+                 src_msg.msg_size, src_msg.msg_offset, tx_size);
        goto out;
    }
 
@@ -272,6 +307,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
        if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
        {
            ret = FFA_RET_NOT_SUPPORTED;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 to SP not supported\n");
            goto out;
        }
        /*
@@ -288,6 +325,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
        ret = ffa_simple_call(FFA_MSG_SEND2,
                              ((uint32_t)ffa_get_vm_id(src_d)) << 16, 0, 0, 0);
+        if ( ret )
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to SP failed: %d\n", ret);
    }
    else if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
    {
@@ -295,7 +334,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
        ret = ffa_msg_send2_vm(dst_id, tx_buf, &src_msg);
    }
    else
+    {
        ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 to VM disabled (dst %#x)\n", dst_id);
+    }
 

out:

    ffa_tx_release(src_ctx);
xen/arch/arm/tee/ffa_msg.c +2 -1
@@ -311,6 +354,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
    if ( !ffa_fw_supports_fid(fid) )
    {
        ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run fid %#x not supported\n", fid);
        goto out;
    }
 
@@ -322,6 +366,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
    if ( !FFA_ID_IS_SECURE(dst >> 16) )
    {
        ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run to non-secure dst %#x\n", dst);
        goto out;
    }
 
--

2.50.1 (Apple Git-155)

#40 lore raw

Re: [PATCH 12/12] xen/arm: ffa: Add message parameter diagnostics

Hi Jens,

Quoted 102 lines · Jens Wiklander

Hi Bertrand,

Quoted 96 lines · Bertrand Marquis

Hi Jens,

Quoted 81 lines · Jens Wiklander

Hi Bertrand,

Quoted 75 lines · Bertrand Marquis

MSG_SEND2 and direct request validation failures are silent, making it hard to diagnose invalid IDs, oversized messages, or unsupported destination types.

Add debug logs for parameter validation failures:
- invalid endpoint IDs
- oversized messages
- unsupported destination types
- invalid sender/receiver combinations
- ratelimit MSG_SEND2 busy failures to avoid log flooding

No functional changes.

---
xen/arch/arm/tee/ffa_msg.c | 45 ++++++++++++++++++++++++++++++++++++++

1 file changed, 45 insertions(+)
xen/arch/arm/tee/ffa_msg.c +13 -0
index 928f269f6c3a..cc273c9a8e80 100644
@@ -4,6 +4,7 @@
 */
 
 #include <xen/const.h>
+#include <xen/lib.h>
 #include <xen/sizes.h>
 #include <xen/types.h>
 
@@ -100,6 +101,7 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
   if ( !ffa_fw_supports_fid(fid) )
   {
       ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: direct req fid %#x not supported\n", fid);
       goto out;
   }
 
@@ -108,6 +110,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
        (src_dst & GENMASK(15,0)) == ffa_get_vm_id(d) )
   {
       ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req invalid src/dst %#x\n",
+                 (uint32_t)src_dst);
       goto out;
   }
 
@@ -115,6 +120,9 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
   if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
   {
       ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: direct req to non-secure dst %#x\n",
+                 (uint32_t)(src_dst & GENMASK(15, 0)));
       goto out;
   }
 
@@ -166,7 +174,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
   /* This is also checking that dest is not src */
   ret = ffa_endpoint_domain_lookup(dst_id, &dst_d, &dst_ctx);
   if ( ret )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 lookup failed: dst %#x ret %d\n",
+                 dst_id, ret);
       return ret;
+    }
 
   /* This also checks that destination has set a Rx buffer */
   ret = ffa_rx_acquire(dst_ctx , &rx_buf, &rx_size);
@@ -199,6 +212,12 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
   /* receiver rx buffer will be released by the receiver*/
 
Quoted 6 lines

xen/arch/arm/tee/ffa_msg.c static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf, · line 217

Quoted 4 lines

out_unlock:

xen/arch/arm/tee/ffa_msg.c +3 -0
+    if ( ret )
+    {
+        if ( ret != FFA_RET_BUSY || printk_ratelimit() )

Shouldn't this be && ?

The intent here is to log all non BUSY errors but only log BUSY when ratelimit allows to reduce the amount of logs in case of someone polling when the receiver RX buffer is busy.

Maybe I should add a comment to make that clearer ?

I will add a comment in v2.

Cheers
Bertrand

Quoted 120 lines

Yes, please.

Cheers,
Jens

Quoted 114 lines

Cheers
Bertrand

Quoted 110 lines

Cheers,
Jens

Quoted 106 lines
xen/arch/arm/tee/ffa_msg.c +27 -0
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to %#x failed: %d\n",
+                     dst_id, ret);
+    }
   rcu_unlock_domain(dst_d);
   if ( !ret )
       ffa_raise_rx_buffer_full(dst_d);
@@ -226,7 +245,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
   ret = ffa_tx_acquire(src_ctx, &tx_buf, &tx_size);
   if ( ret != FFA_RET_OK )
+    {
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 TX acquire failed: %d\n", ret);
       return ret;
+    }
 
   /* create a copy of the message header */
   memcpy(&src_msg, tx_buf, sizeof(src_msg));
@@ -238,6 +261,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
        dst_id == ffa_get_vm_id(src_d) )
   {
       ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid src/dst src %#x dst %#x\n",
+                 src_id, dst_id);
       goto out;
   }
 
@@ -246,6 +272,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
       if (src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_1))
       {
           ret = FFA_RET_INVALID_PARAMETERS;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 invalid msg_offset %u (v1.1)\n",
+                     src_msg.msg_offset);
           goto out;
       }
       /* Set uuid to Nil UUID for v1.1 guests */
@@ -255,6 +284,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
   else if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_2) )
   {
       ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_offset %u (v1.2)\n",
+                 src_msg.msg_offset);
       goto out;
   }
 
@@ -263,6 +295,9 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
           src_msg.msg_size > (tx_size - src_msg.msg_offset) )
   {
       ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 invalid msg_size %u offset %u tx %zu\n",
+                 src_msg.msg_size, src_msg.msg_offset, tx_size);
       goto out;
   }
 
@@ -272,6 +307,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
       if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
       {
           ret = FFA_RET_NOT_SUPPORTED;
+            gdprintk(XENLOG_DEBUG,
+                     "ffa: msg_send2 to SP not supported\n");
           goto out;
       }
       /*
@@ -288,6 +325,8 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 
       ret = ffa_simple_call(FFA_MSG_SEND2,
                             ((uint32_t)ffa_get_vm_id(src_d)) << 16, 0, 0, 0);
+        if ( ret )
+            gdprintk(XENLOG_DEBUG, "ffa: msg_send2 to SP failed: %d\n", ret);
   }
   else if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
   {
@@ -295,7 +334,11 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
       ret = ffa_msg_send2_vm(dst_id, tx_buf, &src_msg);
   }
   else
+    {
       ret = FFA_RET_INVALID_PARAMETERS;
+        gdprintk(XENLOG_DEBUG,
+                 "ffa: msg_send2 to VM disabled (dst %#x)\n", dst_id);
+    }
 

out:

   ffa_tx_release(src_ctx);
xen/arch/arm/tee/ffa_msg.c +2 -1
@@ -311,6 +354,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
   if ( !ffa_fw_supports_fid(fid) )
   {
       ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run fid %#x not supported\n", fid);
       goto out;
   }
 
@@ -322,6 +366,7 @@ void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid)
   if ( !FFA_ID_IS_SECURE(dst >> 16) )
   {
       ret = FFA_RET_NOT_SUPPORTED;
+        gdprintk(XENLOG_DEBUG, "ffa: run to non-secure dst %#x\n", dst);
       goto out;
   }
 
--

2.50.1 (Apple Git-155)

#28 lore raw

Re: [PATCH 08/12] xen/arm: ffa: Fix FFA_FEATURES validation

In reply to Jens Wiklander

Hi Jens,

Quoted 103 lines · Jens Wiklander

Hi Bertrand,

Quoted 79 lines · Bertrand Marquis

Hi Jens,

Quoted 61 lines · Jens Wiklander

Hi Bertrand,

Quoted 55 lines · Bertrand Marquis

FFA_FEATURES currently accepts non-zero input properties (w2-w7) from guests and advertises several ABIs unconditionally, even when firmware support is missing or when the ABI is physical-instance-only. This can mislead guests about what Xen can actually provide and violates FF-A calling conventions. Some SPMCs (Hafnium v2.14 or earlier) also fail to report FFA_RX_ACQUIRE despite supporting it.

Update FFA_FEATURES validation to match spec and firmware support:
- reject non-zero w2-w7 input properties with INVALID_PARAMETERS
- reject 64-bit calling conventions from 32-bit guests with NOT_SUPPORTED
- return NOT_SUPPORTED for physical-instance-only ABIs (FFA_NOTIFICATION_BITMAP_{CREATE,DESTROY}, FFA_RX_ACQUIRE)
- advertise FFA_INTERRUPT as supported
- gate message ABIs on firmware support:
- FFA_MSG_SEND_DIRECT_REQ_{32,64}
- FFA_MSG_SEND_DIRECT_REQ2 (also requires FF-A 1.2 negotiation)
- FFA_MSG_SEND2 (or VM-to-VM enabled)
- report MEM_SHARE_{32,64} only when FFA_MEM_SHARE_64 is supported
- stop advertising FFA_MSG_YIELD (not implemented)

Update firmware probing: drop FFA_MEM_SHARE_32 checks (deprecated) and add FFA_RX_ACQUIRE to the probed set. If FFA_MSG_SEND2 is reported but FFA_RX_ACQUIRE is not, assume RX_ACQUIRE support and warn to work around the Hafnium bug.

Functional impact: guests now see ABI support that reflects firmware capabilities and Xen implementation status. When SEND2 is present but RX_ACQUIRE is not reported, Xen assumes RX_ACQUIRE support.

---
xen/arch/arm/tee/ffa.c | 62 +++++++++++++++++++++++++++++++++++++-----

1 file changed, 55 insertions(+), 7 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index 6de2b9f8ac8e..e9e020bb0cb3 100644
@@ -91,10 +91,10 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
   FW_ABI(FFA_PARTITION_INFO_GET),
   FW_ABI(FFA_NOTIFICATION_INFO_GET_64),
   FW_ABI(FFA_NOTIFICATION_GET),
+    FW_ABI(FFA_RX_ACQUIRE),
   FW_ABI(FFA_RX_RELEASE),
   FW_ABI(FFA_RXTX_MAP_64),
   FW_ABI(FFA_RXTX_UNMAP),
-    FW_ABI(FFA_MEM_SHARE_32),
   FW_ABI(FFA_MEM_SHARE_64),
   FW_ABI(FFA_MEM_RECLAIM),
   FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
@@ -240,19 +240,39 @@ static void handle_features(struct cpu_user_regs *regs)
   struct ffa_ctx *ctx = d->arch.tee;
   unsigned int n;
 
Quoted 9 lines

xen/arch/arm/tee/ffa.c static void handle_features(struct cpu_user_regs *regs) · line 248

Quoted 6 lines
xen/arch/arm/tee/ffa.c +6 -0
+    /*
+     * Xen does not accept any non-zero FFA_FEATURES input properties from
+     * VMs. The spec only defines w2 input properties for FFA_MEM_RETRIEVE_REQ
+     * (NS-bit negotiation for SP/SPMC) and FFA_RXTX_MAP (buffer size and
+     * alignment), so w2 must be MBZ for our callers.
+     */

The spec (version 1.2) lists them as SBZ, except for w2, which is MBZ, for Feature IDs.

Very true, this should only check w2 which is anyway defined as MBZ when not used.
w3-w7 were MBZ in previous versions of FF-A but are in fact SBZ in 1.2 so we should ignore them

Quoted 8 lines
Quoted 1 line

However, if we're to return an error, invalid parameters is a better choice.

In fact the spec is actually saying the following:
If the FF-A interface or feature that was queried is not implemented or invalid, the callee completes this call with an invocation of the FFA_ERROR interface with the NOT_SUPPORTED error code.

So there is no case for INVALID_PARAMETER.

You're right.

Quoted 6 lines

So in fact i should:
- return NOT_SUPPORTED if w2 is not 0
- ignore w3-w7

Can you confirm that you have the same reading of the spec than me ?

Quoted 8 lines

The 1.2 spec only says this w2 is MBZ for Feature IDs, and that w2 is SBZ for FFA_RXTX_MAP. The 1.3 spec says the same, except that in Table 13.14: Feature IDs and properties table, it lists w2 as SBZ.

Note that FFA_MEM_RETRIEVE_REQ has bits defined in w2, and the unknown bits are SBZ.

Based on that, I'm inclined to keep it simple and ignore w2-w7.

That would make sense, I agree.
I will check why FF-A ACS was failing when i did not have this and if it still the case and come back to you if ignoring is not acceptable (with a reason).

Cheers
Bertrand