Skip to contentSkip to conversation

xen/arm: ffa: FF-A v1.2 support

PATCH In review Bertrand Marquis 2 participants 36 messages Nov 27, 2025 – Dec 4, 2025

lore
Share & tools
lore
#0 lore raw

cover xen/arm: ffa: FF-A v1.2 support

This series updates Xen’s FF-A mediator on Arm to implement the FF-A v1.2 interface while keeping existing v1.0/v1.1 guests working.

Patches 1–5 rework the low-level plumbing:

1) add the FF-A v1.2 function IDs and probe the new ABIs
2) track per-VM FFA_VERSION state and enforce negotiation
3) harden RX/TX mapping and validation
4) rework SPMC RX/TX buffer management so access is serialized and RX buffers are always released back to the SPMC
5) switch the mediator to spec-compliant signed 32-bit status codes

Patches 6–9 update the data structures and direct-call paths:

6) add ffa_uuid helpers and rework partition-info handling
7) refactor direct requests via a common RUN/direct completion helper
8) add the v1.1/v1.2 SEND2 header layout
9) add MSG_SEND_DIRECT_REQ2/RESP2 support and marshal the extended register set for v1.2 guests

Patch 10 tightens the dispatcher and advertises FF-A v1.2 only to guests that negotiated v1.2:

- reject SMCCC64 calls from AArch32 guests
- expose the new FIDs and RX/TX capacity fields
- bump Xen's FF-A version to 1.2 once the implementation is complete

v1.0/v1.1 guests continue to use the v1.1 ABI without behaviour changes, while v1.2 guests can negotiate the wider ABI and use RUN, SEND2, and DIRECT_REQ2/RESP2 with the extended register set.

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

Bertrand Marquis (10):
xen/arm: ffa: add FF-A v1.2 function IDs xen/arm: ffa: per-VM FFA_VERSION negotiation state xen/arm: ffa: harden RX/TX mapping
xen/arm: ffa: rework SPMC RX/TX buffer management xen/arm: ffa: use signed 32-bit status codes xen/arm: ffa: add UUID helpers for partition info xen/arm: ffa: refactor direct requests via RUN helper xen/arm: ffa: add v1.2 SEND2 header layout xen/arm: ffa: add MSG_SEND_DIRECT_REQ2 support xen/arm: ffa: advertise FF-A v1.2

 xen/arch/arm/include/asm/tee/ffa.h |   2 +-
 xen/arch/arm/tee/ffa.c             | 205 +++++++++++++++++-------
 xen/arch/arm/tee/ffa_msg.c         | 203 ++++++++++++++++++-----
 xen/arch/arm/tee/ffa_notif.c       |  14 +-
 xen/arch/arm/tee/ffa_partinfo.c    | 248 ++++++++++++++++++-----------
 xen/arch/arm/tee/ffa_private.h     | 133 ++++++++++------
 xen/arch/arm/tee/ffa_rxtx.c        | 160 +++++++++++++++----
 xen/arch/arm/tee/ffa_shm.c         |  30 ++--
 8 files changed, 703 insertions(+), 292 deletions(-)
Signature · 1 line
2.51.2
#1 lore raw

PATCH 1/10 xen/arm: ffa: add FF-A v1.2 function IDs

Bring the FF-A headers up to the v1.2 baseline and fix the function-number range used for ABI discovery:

- update FFA_FNUM_MAX_VALUE so the FF-A function-number window covers the full v1.2 range, and derive the ABI bitmap bounds from FFA_FNUM_MIN_VALUE/FFA_FNUM_MAX_VALUE instead of hard-coding FFA_ERROR/FFA_MSG_SEND2
- define the new v1.2 function IDs; CONSOLE_LOG and PARTITION_INFO_GET_REGS are added for ABI discovery even though they are not implemented yet
- extend the firmware ABI table to probe RUN and MSG_SEND_DIRECT_REQ2/RESP2
- while there, fix an off-by-one in ffa_fw_supports_fid(): the computed bit index must be strictly smaller than FFA_ABI_BITMAP_SIZE, so use >= in the bounds check

Keep FFA_MY_VERSION at 1.1 for now; we only advertise v1.2 once the implementation is fully compliant.

---
 xen/arch/arm/include/asm/tee/ffa.h |  2 +-
 xen/arch/arm/tee/ffa.c             |  4 ++++
 xen/arch/arm/tee/ffa_private.h     | 18 +++++++++++-------
 3 files changed, 16 insertions(+), 8 deletions(-)
xen/arch/arm/include/asm/tee/ffa.h +1 -1
index 24cd4d99c8f9..c587f76e63ca 100644
@@ -16,7 +16,7 @@
 #include <asm/types.h>
 
 #define FFA_FNUM_MIN_VALUE              _AC(0x60,U)
-#define FFA_FNUM_MAX_VALUE              _AC(0x86,U)
+#define FFA_FNUM_MAX_VALUE              _AC(0x8F,U)
 
 static inline bool is_ffa_fid(uint32_t fid)
 {
xen/arch/arm/tee/ffa.c +4 -0
index 1d0239cf6950..2b4e24750d52 100644
@@ -11,6 +11,8 @@
  *               https://developer.arm.com/documentation/den0077/a
  * FF-A-1.1-REL0: FF-A specification version 1.1 available at
  *                https://developer.arm.com/documentation/den0077/e
+ * FF-A-1.2-REL0: FF-A specification version 1.2 available at
+ *                https://developer.arm.com/documentation/den0077/j
  * TEEC-1.0C: TEE Client API Specification version 1.0c available at
  *            https://globalplatform.org/specs-library/tee-client-api-specification/
  *
@@ -102,6 +104,8 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
     FW_ABI(FFA_MSG_SEND2),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ2),
+    FW_ABI(FFA_RUN),
 };
 
 /*
xen/arch/arm/tee/ffa_private.h +11 -7
index 6dbdb200d840..d7e6b6f5ef45 100644
@@ -15,6 +15,7 @@
 #include <xen/spinlock.h>
 #include <xen/time.h>
 #include <xen/types.h>
+#include <asm/tee/ffa.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -42,6 +43,7 @@
 
 #define FFA_VERSION_1_0         MAKE_FFA_VERSION(1, 0)
 #define FFA_VERSION_1_1         MAKE_FFA_VERSION(1, 1)
+#define FFA_VERSION_1_2         MAKE_FFA_VERSION(1, 2)
 /* The minimal FF-A version of the SPMC that can be supported */
 #define FFA_MIN_SPMC_VERSION    FFA_VERSION_1_1
 
@@ -270,6 +272,10 @@
 #define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_SPM_ID_GET                  0x84000085U
 #define FFA_MSG_SEND2                   0x84000086U
+#define FFA_CONSOLE_LOG                 0x8400008AU
+#define FFA_PARTITION_INFO_GET_REGS     0x8400008BU
+#define FFA_MSG_SEND_DIRECT_REQ2        0xC400008DU
+#define FFA_MSG_SEND_DIRECT_RESP2       0xC400008EU
 
 /**
  * Encoding of features supported or not by the fw in a bitmap:
@@ -280,11 +286,9 @@
 #define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
 #define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
 
-#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
-#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
-
-#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
-#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_FNUM_MAX_VALUE - FFA_FNUM_MIN_VALUE \
+                               + 1))
+#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_FNUM_MIN_VALUE) << 1 | \
                                FFA_ABI_CONV(id))
 
 /* Constituent memory region descriptor */
@@ -549,9 +553,9 @@ static inline int32_t ffa_hyp_rx_release(void)
 
 static inline bool ffa_fw_supports_fid(uint32_t fid)
 {
-    BUILD_BUG_ON(FFA_ABI_MIN > FFA_ABI_MAX);
+    BUILD_BUG_ON(FFA_FNUM_MIN_VALUE > FFA_FNUM_MAX_VALUE);
 
-    if ( FFA_ABI_BITNUM(fid) > FFA_ABI_BITMAP_SIZE)
+    if ( FFA_ABI_BITNUM(fid) >= FFA_ABI_BITMAP_SIZE)
         return false;
     return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
 }
Signature · 1 line
2.51.2
#2 lore raw

Re: [PATCH 01/10] xen/arm: ffa: add FF-A v1.2 function IDs

Hi Bertrand,

Quoted 32 lines · Bertrand Marquis

Bring the FF-A headers up to the v1.2 baseline and fix the function-number range used for ABI discovery:

- update FFA_FNUM_MAX_VALUE so the FF-A function-number window covers the full v1.2 range, and derive the ABI bitmap bounds from FFA_FNUM_MIN_VALUE/FFA_FNUM_MAX_VALUE instead of hard-coding FFA_ERROR/FFA_MSG_SEND2
- define the new v1.2 function IDs; CONSOLE_LOG and PARTITION_INFO_GET_REGS are added for ABI discovery even though they are not implemented yet
- extend the firmware ABI table to probe RUN and MSG_SEND_DIRECT_REQ2/RESP2
- while there, fix an off-by-one in ffa_fw_supports_fid(): the computed bit index must be strictly smaller than FFA_ABI_BITMAP_SIZE, so use >= in the bounds check

Keep FFA_MY_VERSION at 1.1 for now; we only advertise v1.2 once the implementation is fully compliant.

---
 xen/arch/arm/include/asm/tee/ffa.h |  2 +-
 xen/arch/arm/tee/ffa.c             |  4 ++++
 xen/arch/arm/tee/ffa_private.h     | 18 +++++++++++-------
 3 files changed, 16 insertions(+), 8 deletions(-)
xen/arch/arm/include/asm/tee/ffa.h +0 -0
index 24cd4d99c8f9..c587f76e63ca 100644

xen/arch/arm/include/asm/tee/ffa.h line 19

Quoted 5 lines
xen/arch/arm/include/asm/tee/ffa.h +1 -1
@@ -16,7 +16,7 @@
 #include <asm/types.h>
 
 #define FFA_FNUM_MIN_VALUE              _AC(0x60,U)
-#define FFA_FNUM_MAX_VALUE              _AC(0x86,U)
+#define FFA_FNUM_MAX_VALUE              _AC(0x8F,U)

This is MAX+1, if I'm not mistaken.

Quoted 46 lines
xen/arch/arm/include/asm/tee/ffa.h +0 -0
 static inline bool is_ffa_fid(uint32_t fid)
 {
xen/arch/arm/tee/ffa.c +4 -0
index 1d0239cf6950..2b4e24750d52 100644
@@ -11,6 +11,8 @@
  *               https://developer.arm.com/documentation/den0077/a
  * FF-A-1.1-REL0: FF-A specification version 1.1 available at
  *                https://developer.arm.com/documentation/den0077/e
+ * FF-A-1.2-REL0: FF-A specification version 1.2 available at
+ *                https://developer.arm.com/documentation/den0077/j
  * TEEC-1.0C: TEE Client API Specification version 1.0c available at
  *            https://globalplatform.org/specs-library/tee-client-api-specification/
  *
@@ -102,6 +104,8 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
     FW_ABI(FFA_MSG_SEND2),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ2),
+    FW_ABI(FFA_RUN),
 };
 
 /*
xen/arch/arm/tee/ffa_private.h +2 -0
index 6dbdb200d840..d7e6b6f5ef45 100644
@@ -15,6 +15,7 @@
 #include <xen/spinlock.h>
 #include <xen/time.h>
 #include <xen/types.h>
+#include <asm/tee/ffa.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -42,6 +43,7 @@
 
 #define FFA_VERSION_1_0         MAKE_FFA_VERSION(1, 0)
 #define FFA_VERSION_1_1         MAKE_FFA_VERSION(1, 1)
+#define FFA_VERSION_1_2         MAKE_FFA_VERSION(1, 2)
 /* The minimal FF-A version of the SPMC that can be supported */
 #define FFA_MIN_SPMC_VERSION    FFA_VERSION_1_1
 

xen/arch/arm/tee/ffa_private.h line 275

Quoted 4 lines
xen/arch/arm/tee/ffa_private.h +1 -0
@@ -270,6 +272,10 @@
 #define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_SPM_ID_GET                  0x84000085U
 #define FFA_MSG_SEND2                   0x84000086U
+#define FFA_CONSOLE_LOG                 0x8400008AU

This is the 32-bit version of the interface. There's also a 64-bit version.

Quoted 7 lines
xen/arch/arm/tee/ffa_private.h +3 -0
+#define FFA_PARTITION_INFO_GET_REGS     0x8400008BU
+#define FFA_MSG_SEND_DIRECT_REQ2        0xC400008DU
+#define FFA_MSG_SEND_DIRECT_RESP2       0xC400008EU
 
 /**
  * Encoding of features supported or not by the fw in a bitmap:

xen/arch/arm/tee/ffa_private.h line 290

Quoted 10 lines
xen/arch/arm/tee/ffa_private.h +2 -5
@@ -280,11 +286,9 @@
 #define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
 #define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
 
-#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
-#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
-
-#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
-#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_FNUM_MAX_VALUE - FFA_FNUM_MIN_VALUE \
+                               + 1))

Depending on whether FFA_FNUM_MAX_VALUE is MAX+1 or just MAX, we could drop the +1.

Cheers,
Jens

Quoted 19 lines
xen/arch/arm/tee/ffa_private.h +3 -3
+#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_FNUM_MIN_VALUE) << 1 | \
                                FFA_ABI_CONV(id))
 
 /* Constituent memory region descriptor */
@@ -549,9 +553,9 @@ static inline int32_t ffa_hyp_rx_release(void)
 
 static inline bool ffa_fw_supports_fid(uint32_t fid)
 {
-    BUILD_BUG_ON(FFA_ABI_MIN > FFA_ABI_MAX);
+    BUILD_BUG_ON(FFA_FNUM_MIN_VALUE > FFA_FNUM_MAX_VALUE);
 
-    if ( FFA_ABI_BITNUM(fid) > FFA_ABI_BITMAP_SIZE)
+    if ( FFA_ABI_BITNUM(fid) >= FFA_ABI_BITMAP_SIZE)
         return false;
     return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
 }
--

2.51.2

#3 lore raw

Re: [PATCH 01/10] xen/arm: ffa: add FF-A v1.2 function IDs

Hi Jens,

Thanks a lot for the review.
Please find my answers here after.

Quoted 40 lines · Jens Wiklander

Hi Bertrand,

Quoted 34 lines · Bertrand Marquis

Bring the FF-A headers up to the v1.2 baseline and fix the function-number range used for ABI discovery:

- update FFA_FNUM_MAX_VALUE so the FF-A function-number window covers the full v1.2 range, and derive the ABI bitmap bounds from FFA_FNUM_MIN_VALUE/FFA_FNUM_MAX_VALUE instead of hard-coding FFA_ERROR/FFA_MSG_SEND2
- define the new v1.2 function IDs; CONSOLE_LOG and PARTITION_INFO_GET_REGS are added for ABI discovery even though they are not implemented yet
- extend the firmware ABI table to probe RUN and MSG_SEND_DIRECT_REQ2/RESP2
- while there, fix an off-by-one in ffa_fw_supports_fid(): the computed bit index must be strictly smaller than FFA_ABI_BITMAP_SIZE, so use >= in the bounds check

Keep FFA_MY_VERSION at 1.1 for now; we only advertise v1.2 once the implementation is fully compliant.

---

xen/arch/arm/include/asm/tee/ffa.h |  2 +-
xen/arch/arm/tee/ffa.c             |  4 ++++
xen/arch/arm/tee/ffa_private.h     | 18 +++++++++++-------
3 files changed, 16 insertions(+), 8 deletions(-)
xen/arch/arm/include/asm/tee/ffa.h +0 -0
index 24cd4d99c8f9..c587f76e63ca 100644
@@ -16,7 +16,7 @@
 #include <asm/types.h>
 
Quoted 5 lines

xen/arch/arm/include/asm/tee/ffa.h line 19

Quoted 3 lines
xen/arch/arm/include/asm/tee/ffa.h +1 -1
 #define FFA_FNUM_MIN_VALUE              _AC(0x60,U)
-#define FFA_FNUM_MAX_VALUE              _AC(0x86,U)
+#define FFA_FNUM_MAX_VALUE              _AC(0x8F,U)

This is MAX+1, if I'm not mistaken.

It depends on how we see it:
- FFA v1.2 does not define calls over 8E so in this sense yes
- SMCCC is reserving 0x60 to 0xEF for FF-A so in this sense we are not completely covering the SMCCC space

I must admit i just used 0x90-1 to cover all 0x60 to 0x90 excluded because i did not wanted to have something to big where we would not use all bits.

So I will keep 0x8F adding a comment saying that SMCCC is reserving the space until 0xEF but only number up to 0x8E are defined in ffa v1.2 so that FNUM_MAX_VALUE is the first unused value so that I will also remove the the +1 after.

Quoted 46 lines
Quoted 46 lines
xen/arch/arm/include/asm/tee/ffa.h +0 -0
 static inline bool is_ffa_fid(uint32_t fid)
 {
xen/arch/arm/tee/ffa.c +4 -0
index 1d0239cf6950..2b4e24750d52 100644
@@ -11,6 +11,8 @@
 *               https://developer.arm.com/documentation/den0077/a
 * FF-A-1.1-REL0: FF-A specification version 1.1 available at
 *                https://developer.arm.com/documentation/den0077/e
+ * FF-A-1.2-REL0: FF-A specification version 1.2 available at
+ *                https://developer.arm.com/documentation/den0077/j
 * TEEC-1.0C: TEE Client API Specification version 1.0c available at
 *            https://globalplatform.org/specs-library/tee-client-api-specification/
 *
@@ -102,6 +104,8 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
    FW_ABI(FFA_MSG_SEND2),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ2),
+    FW_ABI(FFA_RUN),
 };
 
 /*
xen/arch/arm/tee/ffa_private.h +2 -0
index 6dbdb200d840..d7e6b6f5ef45 100644
@@ -15,6 +15,7 @@
 #include <xen/spinlock.h>
 #include <xen/time.h>
 #include <xen/types.h>
+#include <asm/tee/ffa.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -42,6 +43,7 @@
 
 #define FFA_VERSION_1_0         MAKE_FFA_VERSION(1, 0)
 #define FFA_VERSION_1_1         MAKE_FFA_VERSION(1, 1)
+#define FFA_VERSION_1_2         MAKE_FFA_VERSION(1, 2)
 /* The minimal FF-A version of the SPMC that can be supported */
 #define FFA_MIN_SPMC_VERSION    FFA_VERSION_1_1
 
Quoted 7 lines

xen/arch/arm/tee/ffa_private.h line 275

Quoted 5 lines
xen/arch/arm/tee/ffa_private.h +1 -0
@@ -270,6 +272,10 @@
 #define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_SPM_ID_GET                  0x84000085U
 #define FFA_MSG_SEND2                   0x84000086U
+#define FFA_CONSOLE_LOG                 0x8400008AU

This is the 32-bit version of the interface. There's also a 64-bit version.

Ack i will add the missing definition for 64bit and rename that one with a 32 suffix.

Quoted 11 lines
Quoted 11 lines
xen/arch/arm/tee/ffa_private.h +3 -0
+#define FFA_PARTITION_INFO_GET_REGS     0x8400008BU
+#define FFA_MSG_SEND_DIRECT_REQ2        0xC400008DU
+#define FFA_MSG_SEND_DIRECT_RESP2       0xC400008EU
 
 /**
 * Encoding of features supported or not by the fw in a bitmap:
@@ -280,11 +286,9 @@
 #define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
 #define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
 
Quoted 10 lines

xen/arch/arm/tee/ffa_private.h line 290

Quoted 7 lines
xen/arch/arm/tee/ffa_private.h +2 -5
-#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
-#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
-
-#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
-#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_FNUM_MAX_VALUE - FFA_FNUM_MIN_VALUE \
+                               + 1))

Depending on whether FFA_FNUM_MAX_VALUE is MAX+1 or just MAX, we could drop the +1.

Agree i will remove the +1 with the change above.

Cheers
Bertrand

Quoted 22 lines

Cheers,
Jens

Quoted 18 lines
xen/arch/arm/tee/ffa_private.h +3 -3
+#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_FNUM_MIN_VALUE) << 1 | \
                               FFA_ABI_CONV(id))
 
 /* Constituent memory region descriptor */
@@ -549,9 +553,9 @@ static inline int32_t ffa_hyp_rx_release(void)
 
 static inline bool ffa_fw_supports_fid(uint32_t fid)
 {
-    BUILD_BUG_ON(FFA_ABI_MIN > FFA_ABI_MAX);
+    BUILD_BUG_ON(FFA_FNUM_MIN_VALUE > FFA_FNUM_MAX_VALUE);
 
-    if ( FFA_ABI_BITNUM(fid) > FFA_ABI_BITMAP_SIZE)
+    if ( FFA_ABI_BITNUM(fid) >= FFA_ABI_BITMAP_SIZE)
        return false;
    return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
 }
--

2.51.2

#4 lore raw

Re: [PATCH 01/10] xen/arm: ffa: add FF-A v1.2 function IDs

Hi Jens,

Quoted 58 lines · Bertrand Marquis

Hi Jens,

Thanks a lot for the review.
Please find my answers here after.

Quoted 40 lines · Jens Wiklander

Hi Bertrand,

Quoted 34 lines · Bertrand Marquis

Bring the FF-A headers up to the v1.2 baseline and fix the function-number range used for ABI discovery:

- update FFA_FNUM_MAX_VALUE so the FF-A function-number window covers the full v1.2 range, and derive the ABI bitmap bounds from FFA_FNUM_MIN_VALUE/FFA_FNUM_MAX_VALUE instead of hard-coding FFA_ERROR/FFA_MSG_SEND2
- define the new v1.2 function IDs; CONSOLE_LOG and PARTITION_INFO_GET_REGS are added for ABI discovery even though they are not implemented yet
- extend the firmware ABI table to probe RUN and MSG_SEND_DIRECT_REQ2/RESP2
- while there, fix an off-by-one in ffa_fw_supports_fid(): the computed bit index must be strictly smaller than FFA_ABI_BITMAP_SIZE, so use >= in the bounds check

Keep FFA_MY_VERSION at 1.1 for now; we only advertise v1.2 once the implementation is fully compliant.

---

xen/arch/arm/include/asm/tee/ffa.h |  2 +-
xen/arch/arm/tee/ffa.c             |  4 ++++
xen/arch/arm/tee/ffa_private.h     | 18 +++++++++++-------
3 files changed, 16 insertions(+), 8 deletions(-)
xen/arch/arm/include/asm/tee/ffa.h +0 -0
index 24cd4d99c8f9..c587f76e63ca 100644
@@ -16,7 +16,7 @@
 #include <asm/types.h>
 
Quoted 5 lines

xen/arch/arm/include/asm/tee/ffa.h line 19

Quoted 3 lines
xen/arch/arm/include/asm/tee/ffa.h +1 -1
 #define FFA_FNUM_MIN_VALUE              _AC(0x60,U)
-#define FFA_FNUM_MAX_VALUE              _AC(0x86,U)
+#define FFA_FNUM_MAX_VALUE              _AC(0x8F,U)

This is MAX+1, if I'm not mistaken.

It depends on how we see it:
- FFA v1.2 does not define calls over 8E so in this sense yes
- SMCCC is reserving 0x60 to 0xEF for FF-A so in this sense we are not completely covering the SMCCC space

Quoted 7 lines

I must admit i just used 0x90-1 to cover all 0x60 to 0x90 excluded because i did not wanted to have something to big where we would not use all bits.

So I will keep 0x8F adding a comment saying that SMCCC is reserving the space until 0xEF but only number up to 0x8E are defined in ffa v1.2 so that FNUM_MAX_VALUE is the first unused value so that I will also remove the the +1 after.

Actually this would not be coherent with how psci.h is using FNUM so I will set the value to 0x8E with a comment that it is the range used as of FF-A v1.2 and ...

Quoted 80 lines
Quoted 47 lines
Quoted 47 lines
xen/arch/arm/include/asm/tee/ffa.h +0 -0
 static inline bool is_ffa_fid(uint32_t fid)
 {
xen/arch/arm/tee/ffa.c +4 -0
index 1d0239cf6950..2b4e24750d52 100644
@@ -11,6 +11,8 @@
 *               https://developer.arm.com/documentation/den0077/a
 * FF-A-1.1-REL0: FF-A specification version 1.1 available at
 *                https://developer.arm.com/documentation/den0077/e
+ * FF-A-1.2-REL0: FF-A specification version 1.2 available at
+ *                https://developer.arm.com/documentation/den0077/j
 * TEEC-1.0C: TEE Client API Specification version 1.0c available at
 *            https://globalplatform.org/specs-library/tee-client-api-specification/
 *
@@ -102,6 +104,8 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
   FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
   FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
   FW_ABI(FFA_MSG_SEND2),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ2),
+    FW_ABI(FFA_RUN),
 };
 
 /*
xen/arch/arm/tee/ffa_private.h +2 -0
index 6dbdb200d840..d7e6b6f5ef45 100644
@@ -15,6 +15,7 @@
 #include <xen/spinlock.h>
 #include <xen/time.h>
 #include <xen/types.h>
+#include <asm/tee/ffa.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -42,6 +43,7 @@
 
 #define FFA_VERSION_1_0         MAKE_FFA_VERSION(1, 0)
 #define FFA_VERSION_1_1         MAKE_FFA_VERSION(1, 1)
+#define FFA_VERSION_1_2         MAKE_FFA_VERSION(1, 2)
 /* The minimal FF-A version of the SPMC that can be supported */
 #define FFA_MIN_SPMC_VERSION    FFA_VERSION_1_1
 
Quoted 7 lines

xen/arch/arm/tee/ffa_private.h line 275

Quoted 5 lines
xen/arch/arm/tee/ffa_private.h +1 -0
@@ -270,6 +272,10 @@
 #define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_SPM_ID_GET                  0x84000085U
 #define FFA_MSG_SEND2                   0x84000086U
+#define FFA_CONSOLE_LOG                 0x8400008AU

This is the 32-bit version of the interface. There's also a 64-bit version.

Ack i will add the missing definition for 64bit and rename that one with a 32 suffix.

Quoted 11 lines
Quoted 11 lines
xen/arch/arm/tee/ffa_private.h +3 -0
+#define FFA_PARTITION_INFO_GET_REGS     0x8400008BU
+#define FFA_MSG_SEND_DIRECT_REQ2        0xC400008DU
+#define FFA_MSG_SEND_DIRECT_RESP2       0xC400008EU
 
 /**
 * Encoding of features supported or not by the fw in a bitmap:
@@ -280,11 +286,9 @@
 #define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
 #define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
 
Quoted 10 lines

xen/arch/arm/tee/ffa_private.h line 290

Quoted 7 lines
xen/arch/arm/tee/ffa_private.h +2 -5
-#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
-#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
-
-#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
-#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_FNUM_MAX_VALUE - FFA_FNUM_MIN_VALUE \
+                               + 1))

Depending on whether FFA_FNUM_MAX_VALUE is MAX+1 or just MAX, we could drop the +1.

Quoted 1 line

Agree i will remove the +1 with the change above.

I will keep this one as is as a consequence.

Cheers
Bertrand

#5 lore raw

PATCH 2/10 xen/arm: ffa: per-VM FFA_VERSION negotiation state

In reply to Bertrand Marquis

Track FF-A version negotiation per VM and enforce that no FF-A ABI (other than FFA_VERSION) is processed before a guest has selected a version.

Each ffa_ctx gains a dedicated guest_vers_lock, a negotiated version (guest_vers) and a guest_vers_negotiated flag. guest_vers records the version requested by the guest so the mediator can provide data structures compatible with older minor versions. The value returned to the guest by FFA_VERSION is always FFA_MY_VERSION, the implementation version, as required by FF-A.

FFA_VERSION may be issued multiple times. Negotiation becomes final only when a non-FFA_VERSION ABI is invoked, in accordance with the FF-A requirement that the version cannot change once any other ABI has been used. Before this point, non-FFA_VERSION ABIs are rejected if no valid version has been provided.

Once negotiation completes, the context is added to the global FF-A VM list (when VM-to-VM is enabled) and the version may not be modified for the lifetime of the VM. All VM-to-VM paths and teardown logic are updated to use the guest_vers_negotiated flag.

This prevents partially initialised contexts from using the mediator and complies with the FF-A 1.2 FFA_VERSION semantics.

---
 xen/arch/arm/tee/ffa.c         | 115 +++++++++++++++++++++++++--------
 xen/arch/arm/tee/ffa_msg.c     |   2 +-
 xen/arch/arm/tee/ffa_private.h |  21 ++++--
 3 files changed, 104 insertions(+), 34 deletions(-)
xen/arch/arm/tee/ffa.c +88 -27
index 2b4e24750d52..3309ca875ec4 100644
@@ -158,40 +158,89 @@ static bool ffa_abi_supported(uint32_t id)
     return !ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
 }
 
-static void handle_version(struct cpu_user_regs *regs)
+static bool ffa_negotiate_version(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
-    uint32_t vers = get_user_reg(regs, 1);
-    uint32_t old_vers;
+    uint32_t fid = get_user_reg(regs, 0);
+    uint32_t in_vers = get_user_reg(regs, 1);
+    uint32_t out_vers = FFA_MY_VERSION;
 
-    /*
-     * Guest will use the version it requested if it is our major and minor
-     * lower or equals to ours. If the minor is greater, our version will be
-     * used.
-     * In any case return our version to the caller.
-     */
-    if ( FFA_VERSION_MAJOR(vers) == FFA_MY_VERSION_MAJOR )
-    {
-        spin_lock(&ctx->lock);
-        old_vers = ctx->guest_vers;
+    spin_lock(&ctx->guest_vers_lock);
 
-        if ( FFA_VERSION_MINOR(vers) > FFA_MY_VERSION_MINOR )
-            ctx->guest_vers = FFA_MY_VERSION;
-        else
-            ctx->guest_vers = vers;
-        spin_unlock(&ctx->lock);
+    /* Handle FFA_VERSION races from different vCPUs. */
+    if ( ctx->guest_vers_negotiated )
+        goto out_continue;
 
-        if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !old_vers )
+    if ( fid != FFA_VERSION )
+    {
+        if ( !ctx->guest_vers )
         {
-            /* One more VM with FF-A support available */
-            inc_ffa_vm_count();
-            write_lock(&ffa_ctx_list_rwlock);
-            list_add_tail(&ctx->ctx_list, &ffa_ctx_head);
-            write_unlock(&ffa_ctx_list_rwlock);
+            out_vers = 0;
+            goto out_handled;
         }
+
+        /*
+         * A successful FFA_VERSION call does not freeze negotiation. Guests
+         * are allowed to issue multiple FFA_VERSION attempts (e.g. probing
+         * several minor versions). Negotiation becomes final only when a
+         * non-VERSION ABI is invoked, as required by the FF-A specification.
+         */
+        if ( !ctx->guest_vers_negotiated )
+        {
+            ctx->guest_vers_negotiated = true;
+
+            if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+            {
+                /* One more VM with FF-A support available */
+                inc_ffa_vm_count();
+                write_lock(&ffa_ctx_list_rwlock);
+                list_add_tail(&ctx->ctx_list, &ffa_ctx_head);
+                write_unlock(&ffa_ctx_list_rwlock);
+            }
+        }
+
+        goto out_continue;
+    }
+
+    /*
+     * guest_vers stores the version selected by the guest (lower minor may
+     * require reduced data structures). However, the value returned to the
+     * guest via FFA_VERSION is always FFA_MY_VERSION, the implementation
+     * version, as required by FF-A. The two values intentionally differ.
+     */
+
+    /*
+     * Return our highest implementation version on request different than our
+     * major and mark negotiated version as our implementation version.
+     */
+    if ( FFA_VERSION_MAJOR(in_vers) != FFA_MY_VERSION_MAJOR )
+    {
+        ctx->guest_vers = FFA_MY_VERSION;
+        goto out_handled;
     }
-    ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
+
+    /*
+     * Use our minor version if a greater minor was requested or the requested
+     * minor if it is lower than ours was requested.
+     */
+    if ( FFA_VERSION_MINOR(in_vers) > FFA_MY_VERSION_MINOR )
+        ctx->guest_vers = FFA_MY_VERSION;
+    else
+        ctx->guest_vers = in_vers;
+
+out_handled:
+    spin_unlock(&ctx->guest_vers_lock);
+    if ( out_vers )
+        ffa_set_regs(regs, out_vers, 0, 0, 0, 0, 0, 0, 0);
+    else
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+    return true;
+
+out_continue:
+    spin_unlock(&ctx->guest_vers_lock);
+
+    return false;
 }
 
 static void handle_features(struct cpu_user_regs *regs)
@@ -274,10 +323,17 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     if ( !ctx )
         return false;
 
+    /* A version must be negotiated first */
+    if ( !ctx->guest_vers_negotiated )
+    {
+        if ( ffa_negotiate_version(regs) )
+            return true;
+    }
+
     switch ( fid )
     {
     case FFA_VERSION:
-        handle_version(regs);
+        ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
         return true;
     case FFA_ID_GET:
         ffa_set_regs_success(regs, ffa_get_vm_id(d), 0);
@@ -371,6 +427,11 @@ static int ffa_domain_init(struct domain *d)
     d->arch.tee = ctx;
     ctx->teardown_d = d;
     INIT_LIST_HEAD(&ctx->shm_list);
+    spin_lock_init(&ctx->lock);
+    spin_lock_init(&ctx->guest_vers_lock);
+    ctx->guest_vers = 0;
+    ctx->guest_vers_negotiated = false;
+    INIT_LIST_HEAD(&ctx->ctx_list);
 
     ctx->ffa_id = ffa_get_vm_id(d);
     ctx->num_vcpus = d->max_vcpus;
@@ -452,7 +513,7 @@ static int ffa_domain_teardown(struct domain *d)
     if ( !ctx )
         return 0;
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && ctx->guest_vers )
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && ctx->guest_vers_negotiated )
     {
         dec_ffa_vm_count();
         write_lock(&ffa_ctx_list_rwlock);
xen/arch/arm/tee/ffa_msg.c +1 -1
index c20c5bec0f76..dec429cbf160 100644
@@ -113,7 +113,7 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     }
 
     dst_ctx = dst_d->arch.tee;
-    if ( !dst_ctx->guest_vers )
+    if ( !dst_ctx->guest_vers_negotiated )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
         goto out_unlock;
xen/arch/arm/tee/ffa_private.h +15 -6
index d7e6b6f5ef45..88b85c7c453a 100644
@@ -354,12 +354,6 @@ struct ffa_ctx {
      * Global data accessed with lock locked.
      */
     spinlock_t lock;
-    /*
-     * FF-A version negotiated by the guest, only modifications to
-     * this field are done with the lock held as this is expected to
-     * be done once at init by a guest.
-     */
-    uint32_t guest_vers;
     /* Number of 4kB pages in each of rx/rx_pg and tx/tx_pg */
     unsigned int page_count;
     /* Number of allocated shared memory object */
@@ -367,6 +361,21 @@ struct ffa_ctx {
     /* Used shared memory objects, struct ffa_shm_mem */
     struct list_head shm_list;
 
+    /*
+     * FF-A version handling
+     * guest_vers and guest_vers_negotiated are only written with
+     * guest_vers_lock held. Reads do not take the lock, but ordering is
+     * guaranteed because the writer updates guest_vers first and then
+     * guest_vers_negotiated while holding the lock, ensuring any reader
+     * that observes guest_vers_negotiated == true also sees the final
+     * guest_vers value.
+     * The ffa_ctx is added to the ctx_list only when a version
+     * has been negotiated and locked.
+     */
+    spinlock_t guest_vers_lock;
+    uint32_t guest_vers;
+    bool guest_vers_negotiated;
+
     /*
      * Rx buffer, accessed with rx_lock locked.
      * rx_is_free is used to serialize access.
Signature · 1 line
2.51.2
#6 lore raw

Re: [PATCH 02/10] xen/arm: ffa: per-VM FFA_VERSION negotiation state

Hi Bertrand,

Quoted 84 lines · Bertrand Marquis

Track FF-A version negotiation per VM and enforce that no FF-A ABI (other than FFA_VERSION) is processed before a guest has selected a version.

Each ffa_ctx gains a dedicated guest_vers_lock, a negotiated version (guest_vers) and a guest_vers_negotiated flag. guest_vers records the version requested by the guest so the mediator can provide data structures compatible with older minor versions. The value returned to the guest by FFA_VERSION is always FFA_MY_VERSION, the implementation version, as required by FF-A.

FFA_VERSION may be issued multiple times. Negotiation becomes final only when a non-FFA_VERSION ABI is invoked, in accordance with the FF-A requirement that the version cannot change once any other ABI has been used. Before this point, non-FFA_VERSION ABIs are rejected if no valid version has been provided.

Once negotiation completes, the context is added to the global FF-A VM list (when VM-to-VM is enabled) and the version may not be modified for the lifetime of the VM. All VM-to-VM paths and teardown logic are updated to use the guest_vers_negotiated flag.

This prevents partially initialised contexts from using the mediator and complies with the FF-A 1.2 FFA_VERSION semantics.

---
 xen/arch/arm/tee/ffa.c         | 115 +++++++++++++++++++++++++--------
 xen/arch/arm/tee/ffa_msg.c     |   2 +-
 xen/arch/arm/tee/ffa_private.h |  21 ++++--
 3 files changed, 104 insertions(+), 34 deletions(-)
xen/arch/arm/tee/ffa.c +12 -24
index 2b4e24750d52..3309ca875ec4 100644
@@ -158,40 +158,89 @@ static bool ffa_abi_supported(uint32_t id)
     return !ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
 }
 
-static void handle_version(struct cpu_user_regs *regs)
+static bool ffa_negotiate_version(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
-    uint32_t vers = get_user_reg(regs, 1);
-    uint32_t old_vers;
+    uint32_t fid = get_user_reg(regs, 0);
+    uint32_t in_vers = get_user_reg(regs, 1);
+    uint32_t out_vers = FFA_MY_VERSION;
 
-    /*
-     * Guest will use the version it requested if it is our major and minor
-     * lower or equals to ours. If the minor is greater, our version will be
-     * used.
-     * In any case return our version to the caller.
-     */
-    if ( FFA_VERSION_MAJOR(vers) == FFA_MY_VERSION_MAJOR )
-    {
-        spin_lock(&ctx->lock);
-        old_vers = ctx->guest_vers;
+    spin_lock(&ctx->guest_vers_lock);
 
-        if ( FFA_VERSION_MINOR(vers) > FFA_MY_VERSION_MINOR )
-            ctx->guest_vers = FFA_MY_VERSION;
-        else
-            ctx->guest_vers = vers;
-        spin_unlock(&ctx->lock);
+    /* Handle FFA_VERSION races from different vCPUs. */
+    if ( ctx->guest_vers_negotiated )
+        goto out_continue;
 
-        if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !old_vers )
+    if ( fid != FFA_VERSION )
+    {
+        if ( !ctx->guest_vers )
         {
-            /* One more VM with FF-A support available */
-            inc_ffa_vm_count();
-            write_lock(&ffa_ctx_list_rwlock);
-            list_add_tail(&ctx->ctx_list, &ffa_ctx_head);
-            write_unlock(&ffa_ctx_list_rwlock);
+            out_vers = 0;

xen/arch/arm/tee/ffa.c static bool ffa_abi_supported(uint32_t id) · line 189

Quoted 10 lines
xen/arch/arm/tee/ffa.c +9 -0
+            goto out_handled;
         }
+
+        /*
+         * A successful FFA_VERSION call does not freeze negotiation. Guests
+         * are allowed to issue multiple FFA_VERSION attempts (e.g. probing
+         * several minor versions). Negotiation becomes final only when a
+         * non-VERSION ABI is invoked, as required by the FF-A specification.
+         */
+        if ( !ctx->guest_vers_negotiated )

ctx->guest_vers_negotiated is always false here, due to the check above.

xen/arch/arm/tee/ffa.c static bool ffa_abi_supported(uint32_t id) · line 191

Quoted 2 lines
xen/arch/arm/tee/ffa.c +2 -0
+        {
+            ctx->guest_vers_negotiated = true;

I'm on thin ice here, but I think that barriers or some other primitives are needed to close the gap if ffa_handle_call() is called concurrently during these conditions:
ctx->guest_vers_negotiated == false
CPU0 called with FFA_VERSION 1.1 -> sets ctx->guest_vers = 1.1 CPU1 called with a valid FF-A ID != FFA_VERSION -> sets ctx->guest_vers_negotiated = true
CPU2 called with a valid FF-A ID != FFA_VERSION -> guarantee is missing that CPU2 will observe the updated ctx->guest_vers if it observes the updated ctx->guest_vers_negotiated

Cheers,
Jens

Quoted 151 lines
xen/arch/arm/tee/ffa.c +65 -3
+
+            if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+            {
+                /* One more VM with FF-A support available */
+                inc_ffa_vm_count();
+                write_lock(&ffa_ctx_list_rwlock);
+                list_add_tail(&ctx->ctx_list, &ffa_ctx_head);
+                write_unlock(&ffa_ctx_list_rwlock);
+            }
+        }
+
+        goto out_continue;
+    }
+
+    /*
+     * guest_vers stores the version selected by the guest (lower minor may
+     * require reduced data structures). However, the value returned to the
+     * guest via FFA_VERSION is always FFA_MY_VERSION, the implementation
+     * version, as required by FF-A. The two values intentionally differ.
+     */
+
+    /*
+     * Return our highest implementation version on request different than our
+     * major and mark negotiated version as our implementation version.
+     */
+    if ( FFA_VERSION_MAJOR(in_vers) != FFA_MY_VERSION_MAJOR )
+    {
+        ctx->guest_vers = FFA_MY_VERSION;
+        goto out_handled;
     }
-    ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
+
+    /*
+     * Use our minor version if a greater minor was requested or the requested
+     * minor if it is lower than ours was requested.
+     */
+    if ( FFA_VERSION_MINOR(in_vers) > FFA_MY_VERSION_MINOR )
+        ctx->guest_vers = FFA_MY_VERSION;
+    else
+        ctx->guest_vers = in_vers;
+
+out_handled:
+    spin_unlock(&ctx->guest_vers_lock);
+    if ( out_vers )
+        ffa_set_regs(regs, out_vers, 0, 0, 0, 0, 0, 0, 0);
+    else
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+    return true;
+
+out_continue:
+    spin_unlock(&ctx->guest_vers_lock);
+
+    return false;
 }
 
 static void handle_features(struct cpu_user_regs *regs)
@@ -274,10 +323,17 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     if ( !ctx )
         return false;
 
+    /* A version must be negotiated first */
+    if ( !ctx->guest_vers_negotiated )
+    {
+        if ( ffa_negotiate_version(regs) )
+            return true;
+    }
+
     switch ( fid )
     {
     case FFA_VERSION:
-        handle_version(regs);
+        ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
         return true;
     case FFA_ID_GET:
         ffa_set_regs_success(regs, ffa_get_vm_id(d), 0);
@@ -371,6 +427,11 @@ static int ffa_domain_init(struct domain *d)
     d->arch.tee = ctx;
     ctx->teardown_d = d;
     INIT_LIST_HEAD(&ctx->shm_list);
+    spin_lock_init(&ctx->lock);
+    spin_lock_init(&ctx->guest_vers_lock);
+    ctx->guest_vers = 0;
+    ctx->guest_vers_negotiated = false;
+    INIT_LIST_HEAD(&ctx->ctx_list);
 
     ctx->ffa_id = ffa_get_vm_id(d);
     ctx->num_vcpus = d->max_vcpus;
@@ -452,7 +513,7 @@ static int ffa_domain_teardown(struct domain *d)
     if ( !ctx )
         return 0;
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && ctx->guest_vers )
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && ctx->guest_vers_negotiated )
     {
         dec_ffa_vm_count();
         write_lock(&ffa_ctx_list_rwlock);
xen/arch/arm/tee/ffa_msg.c +1 -1
index c20c5bec0f76..dec429cbf160 100644
@@ -113,7 +113,7 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     }
 
     dst_ctx = dst_d->arch.tee;
-    if ( !dst_ctx->guest_vers )
+    if ( !dst_ctx->guest_vers_negotiated )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
         goto out_unlock;
xen/arch/arm/tee/ffa_private.h +15 -7
index d7e6b6f5ef45..88b85c7c453a 100644
@@ -354,12 +354,6 @@ struct ffa_ctx {
      * Global data accessed with lock locked.
      */
     spinlock_t lock;
-    /*
-     * FF-A version negotiated by the guest, only modifications to
-     * this field are done with the lock held as this is expected to
-     * be done once at init by a guest.
-     */
-    uint32_t guest_vers;
     /* Number of 4kB pages in each of rx/rx_pg and tx/tx_pg */
     unsigned int page_count;
     /* Number of allocated shared memory object */
@@ -367,6 +361,21 @@ struct ffa_ctx {
     /* Used shared memory objects, struct ffa_shm_mem */
     struct list_head shm_list;
 
+    /*
+     * FF-A version handling
+     * guest_vers and guest_vers_negotiated are only written with
+     * guest_vers_lock held. Reads do not take the lock, but ordering is
+     * guaranteed because the writer updates guest_vers first and then
+     * guest_vers_negotiated while holding the lock, ensuring any reader
+     * that observes guest_vers_negotiated == true also sees the final
+     * guest_vers value.
+     * The ffa_ctx is added to the ctx_list only when a version
+     * has been negotiated and locked.
+     */
+    spinlock_t guest_vers_lock;
+    uint32_t guest_vers;
+    bool guest_vers_negotiated;
+
     /*
      * Rx buffer, accessed with rx_lock locked.
      * rx_is_free is used to serialize access.
--

2.51.2

#7 lore raw

Re: [PATCH 02/10] xen/arm: ffa: per-VM FFA_VERSION negotiation state

Hi Jens,

Quoted 95 lines · Jens Wiklander

Hi Bertrand,

Quoted 88 lines · Bertrand Marquis

Track FF-A version negotiation per VM and enforce that no FF-A ABI (other than FFA_VERSION) is processed before a guest has selected a version.

Each ffa_ctx gains a dedicated guest_vers_lock, a negotiated version (guest_vers) and a guest_vers_negotiated flag. guest_vers records the version requested by the guest so the mediator can provide data structures compatible with older minor versions. The value returned to the guest by FFA_VERSION is always FFA_MY_VERSION, the implementation version, as required by FF-A.

FFA_VERSION may be issued multiple times. Negotiation becomes final only when a non-FFA_VERSION ABI is invoked, in accordance with the FF-A requirement that the version cannot change once any other ABI has been used. Before this point, non-FFA_VERSION ABIs are rejected if no valid version has been provided.

Once negotiation completes, the context is added to the global FF-A VM list (when VM-to-VM is enabled) and the version may not be modified for the lifetime of the VM. All VM-to-VM paths and teardown logic are updated to use the guest_vers_negotiated flag.

This prevents partially initialised contexts from using the mediator and complies with the FF-A 1.2 FFA_VERSION semantics.

---

xen/arch/arm/tee/ffa.c         | 115 +++++++++++++++++++++++++--------
xen/arch/arm/tee/ffa_msg.c     |   2 +-
xen/arch/arm/tee/ffa_private.h |  21 ++++--
3 files changed, 104 insertions(+), 34 deletions(-)
xen/arch/arm/tee/ffa.c +15 -24
index 2b4e24750d52..3309ca875ec4 100644
@@ -158,40 +158,89 @@ static bool ffa_abi_supported(uint32_t id)
    return !ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
 }
 
-static void handle_version(struct cpu_user_regs *regs)
+static bool ffa_negotiate_version(struct cpu_user_regs *regs)
 {
    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
-    uint32_t vers = get_user_reg(regs, 1);
-    uint32_t old_vers;
+    uint32_t fid = get_user_reg(regs, 0);
+    uint32_t in_vers = get_user_reg(regs, 1);
+    uint32_t out_vers = FFA_MY_VERSION;
 
-    /*
-     * Guest will use the version it requested if it is our major and minor
-     * lower or equals to ours. If the minor is greater, our version will be
-     * used.
-     * In any case return our version to the caller.
-     */
-    if ( FFA_VERSION_MAJOR(vers) == FFA_MY_VERSION_MAJOR )
-    {
-        spin_lock(&ctx->lock);
-        old_vers = ctx->guest_vers;
+    spin_lock(&ctx->guest_vers_lock);
 
-        if ( FFA_VERSION_MINOR(vers) > FFA_MY_VERSION_MINOR )
-            ctx->guest_vers = FFA_MY_VERSION;
-        else
-            ctx->guest_vers = vers;
-        spin_unlock(&ctx->lock);
+    /* Handle FFA_VERSION races from different vCPUs. */
+    if ( ctx->guest_vers_negotiated )
+        goto out_continue;
 
-        if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !old_vers )
+    if ( fid != FFA_VERSION )
+    {
+        if ( !ctx->guest_vers )
        {
-            /* One more VM with FF-A support available */
-            inc_ffa_vm_count();
-            write_lock(&ffa_ctx_list_rwlock);
-            list_add_tail(&ctx->ctx_list, &ffa_ctx_head);
-            write_unlock(&ffa_ctx_list_rwlock);
+            out_vers = 0;
+            goto out_handled;
        }
+
+        /*
Quoted 8 lines

xen/arch/arm/tee/ffa.c static bool ffa_abi_supported(uint32_t id) · line 189

Quoted 6 lines
xen/arch/arm/tee/ffa.c +6 -0
+         * A successful FFA_VERSION call does not freeze negotiation. Guests
+         * are allowed to issue multiple FFA_VERSION attempts (e.g. probing
+         * several minor versions). Negotiation becomes final only when a
+         * non-VERSION ABI is invoked, as required by the FF-A specification.
+         */
+        if ( !ctx->guest_vers_negotiated )

ctx->guest_vers_negotiated is always false here, due to the check above.

Absolutely, I will remove the if here so that we set version to negotiated on the first pass and do not come back here after.

Quoted 4 lines

xen/arch/arm/tee/ffa.c static bool ffa_abi_supported(uint32_t id) · line 191

Quoted 3 lines
xen/arch/arm/tee/ffa.c +2 -0
+        {
+            ctx->guest_vers_negotiated = true;
Quoted 10 lines

I'm on thin ice here, but I think that barriers or some other primitives are needed to close the gap if ffa_handle_call() is called concurrently during these conditions:
ctx->guest_vers_negotiated == false
CPU0 called with FFA_VERSION 1.1 -> sets ctx->guest_vers = 1.1 CPU1 called with a valid FF-A ID != FFA_VERSION -> sets ctx->guest_vers_negotiated = true
CPU2 called with a valid FF-A ID != FFA_VERSION -> guarantee is missing that CPU2 will observe the updated ctx->guest_vers if it observes the updated ctx->guest_vers_negotiated

Definitely you are right and the combination of guest_vers and guest_vers_negotiated has an issue with ordering.

I think the following modification should solve this:
- remove guest_vers_negotiated and use guest_vers = 0 as test for version negotiated used with ACCESS_ONCE
- introduced a guest_vers_tmp only accessed under the lock to store the temporary agreed version until negotiation is done
- during negotiation done copy tmp into guest_vers with a previous write barrier before and ACCESS_ONCE to ensure visibility

Tell if that sounds right :-)

Cheers
Bertrand

Quoted 154 lines

Cheers,
Jens

Quoted 150 lines
xen/arch/arm/tee/ffa.c +65 -3
+
+            if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
+            {
+                /* One more VM with FF-A support available */
+                inc_ffa_vm_count();
+                write_lock(&ffa_ctx_list_rwlock);
+                list_add_tail(&ctx->ctx_list, &ffa_ctx_head);
+                write_unlock(&ffa_ctx_list_rwlock);
+            }
+        }
+
+        goto out_continue;
+    }
+
+    /*
+     * guest_vers stores the version selected by the guest (lower minor may
+     * require reduced data structures). However, the value returned to the
+     * guest via FFA_VERSION is always FFA_MY_VERSION, the implementation
+     * version, as required by FF-A. The two values intentionally differ.
+     */
+
+    /*
+     * Return our highest implementation version on request different than our
+     * major and mark negotiated version as our implementation version.
+     */
+    if ( FFA_VERSION_MAJOR(in_vers) != FFA_MY_VERSION_MAJOR )
+    {
+        ctx->guest_vers = FFA_MY_VERSION;
+        goto out_handled;
    }
-    ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
+
+    /*
+     * Use our minor version if a greater minor was requested or the requested
+     * minor if it is lower than ours was requested.
+     */
+    if ( FFA_VERSION_MINOR(in_vers) > FFA_MY_VERSION_MINOR )
+        ctx->guest_vers = FFA_MY_VERSION;
+    else
+        ctx->guest_vers = in_vers;
+
+out_handled:
+    spin_unlock(&ctx->guest_vers_lock);
+    if ( out_vers )
+        ffa_set_regs(regs, out_vers, 0, 0, 0, 0, 0, 0, 0);
+    else
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+    return true;
+
+out_continue:
+    spin_unlock(&ctx->guest_vers_lock);
+
+    return false;
 }
 
 static void handle_features(struct cpu_user_regs *regs)
@@ -274,10 +323,17 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
    if ( !ctx )
        return false;
 
+    /* A version must be negotiated first */
+    if ( !ctx->guest_vers_negotiated )
+    {
+        if ( ffa_negotiate_version(regs) )
+            return true;
+    }
+
    switch ( fid )
    {
    case FFA_VERSION:
-        handle_version(regs);
+        ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
        return true;
    case FFA_ID_GET:
        ffa_set_regs_success(regs, ffa_get_vm_id(d), 0);
@@ -371,6 +427,11 @@ static int ffa_domain_init(struct domain *d)
    d->arch.tee = ctx;
    ctx->teardown_d = d;
    INIT_LIST_HEAD(&ctx->shm_list);
+    spin_lock_init(&ctx->lock);
+    spin_lock_init(&ctx->guest_vers_lock);
+    ctx->guest_vers = 0;
+    ctx->guest_vers_negotiated = false;
+    INIT_LIST_HEAD(&ctx->ctx_list);
 
    ctx->ffa_id = ffa_get_vm_id(d);
    ctx->num_vcpus = d->max_vcpus;
@@ -452,7 +513,7 @@ static int ffa_domain_teardown(struct domain *d)
    if ( !ctx )
        return 0;
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && ctx->guest_vers )
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && ctx->guest_vers_negotiated )
    {
        dec_ffa_vm_count();
        write_lock(&ffa_ctx_list_rwlock);
xen/arch/arm/tee/ffa_msg.c +1 -1
index c20c5bec0f76..dec429cbf160 100644
@@ -113,7 +113,7 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
    }
 
    dst_ctx = dst_d->arch.tee;
-    if ( !dst_ctx->guest_vers )
+    if ( !dst_ctx->guest_vers_negotiated )
    {
        ret = FFA_RET_INVALID_PARAMETERS;
        goto out_unlock;
xen/arch/arm/tee/ffa_private.h +15 -7
index d7e6b6f5ef45..88b85c7c453a 100644
@@ -354,12 +354,6 @@ struct ffa_ctx {
     * Global data accessed with lock locked.
     */
    spinlock_t lock;
-    /*
-     * FF-A version negotiated by the guest, only modifications to
-     * this field are done with the lock held as this is expected to
-     * be done once at init by a guest.
-     */
-    uint32_t guest_vers;
    /* Number of 4kB pages in each of rx/rx_pg and tx/tx_pg */
    unsigned int page_count;
    /* Number of allocated shared memory object */
@@ -367,6 +361,21 @@ struct ffa_ctx {
    /* Used shared memory objects, struct ffa_shm_mem */
    struct list_head shm_list;
 
+    /*
+     * FF-A version handling
+     * guest_vers and guest_vers_negotiated are only written with
+     * guest_vers_lock held. Reads do not take the lock, but ordering is
+     * guaranteed because the writer updates guest_vers first and then
+     * guest_vers_negotiated while holding the lock, ensuring any reader
+     * that observes guest_vers_negotiated == true also sees the final
+     * guest_vers value.
+     * The ffa_ctx is added to the ctx_list only when a version
+     * has been negotiated and locked.
+     */
+    spinlock_t guest_vers_lock;
+    uint32_t guest_vers;
+    bool guest_vers_negotiated;
+
    /*
     * Rx buffer, accessed with rx_lock locked.
     * rx_is_free is used to serialize access.
--

2.51.2

#8 lore raw

Re: [PATCH 02/10] xen/arm: ffa: per-VM FFA_VERSION negotiation state

Hi,

Quoted 136 lines · Bertrand Marquis

Hi Jens,

Quoted 95 lines · Jens Wiklander

Hi Bertrand,

Quoted 88 lines · Bertrand Marquis

Track FF-A version negotiation per VM and enforce that no FF-A ABI (other than FFA_VERSION) is processed before a guest has selected a version.

Each ffa_ctx gains a dedicated guest_vers_lock, a negotiated version (guest_vers) and a guest_vers_negotiated flag. guest_vers records the version requested by the guest so the mediator can provide data structures compatible with older minor versions. The value returned to the guest by FFA_VERSION is always FFA_MY_VERSION, the implementation version, as required by FF-A.

FFA_VERSION may be issued multiple times. Negotiation becomes final only when a non-FFA_VERSION ABI is invoked, in accordance with the FF-A requirement that the version cannot change once any other ABI has been used. Before this point, non-FFA_VERSION ABIs are rejected if no valid version has been provided.

Once negotiation completes, the context is added to the global FF-A VM list (when VM-to-VM is enabled) and the version may not be modified for the lifetime of the VM. All VM-to-VM paths and teardown logic are updated to use the guest_vers_negotiated flag.

This prevents partially initialised contexts from using the mediator and complies with the FF-A 1.2 FFA_VERSION semantics.

---

xen/arch/arm/tee/ffa.c         | 115 +++++++++++++++++++++++++--------
xen/arch/arm/tee/ffa_msg.c     |   2 +-
xen/arch/arm/tee/ffa_private.h |  21 ++++--
3 files changed, 104 insertions(+), 34 deletions(-)
xen/arch/arm/tee/ffa.c +15 -24
index 2b4e24750d52..3309ca875ec4 100644
@@ -158,40 +158,89 @@ static bool ffa_abi_supported(uint32_t id)
    return !ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
 }
 
-static void handle_version(struct cpu_user_regs *regs)
+static bool ffa_negotiate_version(struct cpu_user_regs *regs)
 {
    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
-    uint32_t vers = get_user_reg(regs, 1);
-    uint32_t old_vers;
+    uint32_t fid = get_user_reg(regs, 0);
+    uint32_t in_vers = get_user_reg(regs, 1);
+    uint32_t out_vers = FFA_MY_VERSION;
 
-    /*
-     * Guest will use the version it requested if it is our major and minor
-     * lower or equals to ours. If the minor is greater, our version will be
-     * used.
-     * In any case return our version to the caller.
-     */
-    if ( FFA_VERSION_MAJOR(vers) == FFA_MY_VERSION_MAJOR )
-    {
-        spin_lock(&ctx->lock);
-        old_vers = ctx->guest_vers;
+    spin_lock(&ctx->guest_vers_lock);
 
-        if ( FFA_VERSION_MINOR(vers) > FFA_MY_VERSION_MINOR )
-            ctx->guest_vers = FFA_MY_VERSION;
-        else
-            ctx->guest_vers = vers;
-        spin_unlock(&ctx->lock);
+    /* Handle FFA_VERSION races from different vCPUs. */
+    if ( ctx->guest_vers_negotiated )
+        goto out_continue;
 
-        if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) && !old_vers )
+    if ( fid != FFA_VERSION )
+    {
+        if ( !ctx->guest_vers )
        {
-            /* One more VM with FF-A support available */
-            inc_ffa_vm_count();
-            write_lock(&ffa_ctx_list_rwlock);
-            list_add_tail(&ctx->ctx_list, &ffa_ctx_head);
-            write_unlock(&ffa_ctx_list_rwlock);
+            out_vers = 0;
+            goto out_handled;
        }
+
+        /*
Quoted 8 lines

xen/arch/arm/tee/ffa.c static bool ffa_abi_supported(uint32_t id) · line 189

Quoted 6 lines
xen/arch/arm/tee/ffa.c +6 -0
+         * A successful FFA_VERSION call does not freeze negotiation. Guests
+         * are allowed to issue multiple FFA_VERSION attempts (e.g. probing
+         * several minor versions). Negotiation becomes final only when a
+         * non-VERSION ABI is invoked, as required by the FF-A specification.
+         */
+        if ( !ctx->guest_vers_negotiated )

ctx->guest_vers_negotiated is always false here, due to the check above.

Absolutely, I will remove the if here so that we set version to negotiated on the first pass and do not come back here after.

Quoted 4 lines

xen/arch/arm/tee/ffa.c static bool ffa_abi_supported(uint32_t id) · line 191

Quoted 3 lines
xen/arch/arm/tee/ffa.c +2 -0
+        {
+            ctx->guest_vers_negotiated = true;
Quoted 10 lines

I'm on thin ice here, but I think that barriers or some other primitives are needed to close the gap if ffa_handle_call() is called concurrently during these conditions:
ctx->guest_vers_negotiated == false
CPU0 called with FFA_VERSION 1.1 -> sets ctx->guest_vers = 1.1 CPU1 called with a valid FF-A ID != FFA_VERSION -> sets ctx->guest_vers_negotiated = true
CPU2 called with a valid FF-A ID != FFA_VERSION -> guarantee is missing that CPU2 will observe the updated ctx->guest_vers if it observes the updated ctx->guest_vers_negotiated

Definitely you are right and the combination of guest_vers and guest_vers_negotiated has an issue with ordering.

I think the following modification should solve this:
- remove guest_vers_negotiated and use guest_vers = 0 as test for version negotiated used with ACCESS_ONCE
- introduced a guest_vers_tmp only accessed under the lock to store the temporary agreed version until negotiation is done
- during negotiation done copy tmp into guest_vers with a previous write barrier before and ACCESS_ONCE to ensure visibility

Quoted 1 line

Tell if that sounds right :-)

That should work. I think we may be able to skip an explicit barrier or even ACCESS_ONCE since the other CPUs will either see the updated version or enter ffa_negotiate_version() to take the lock (which includes a barrier) for synchronized access.

Cheers,
Jens

#9 lore raw

PATCH 3/10 xen/arm: ffa: harden RX/TX mapping

In reply to Bertrand Marquis

Harden the RX/TX mapping paths and keep signed FF-A return codes end-to-end.

Reject zero-length mappings and insist on page-aligned RX/TX buffer addresses before touching the P2M. The unmap plumbing is switched to use the same signed helpers so dispatcher error handling is consistent across map and unmap operations.

This avoids partially mapped or silently truncated buffers and makes the mediator behaviour match the FF-A error model more closely.

While there also introduce a domain_rxtx_init to properly initialize the rxtx buffers spinlocks.

---
 xen/arch/arm/tee/ffa.c         |  4 ++++
 xen/arch/arm/tee/ffa_private.h |  5 +++--
 xen/arch/arm/tee/ffa_rxtx.c    | 28 +++++++++++++++++++++-------
 3 files changed, 28 insertions(+), 9 deletions(-)
xen/arch/arm/tee/ffa.c +4 -0
index 3309ca875ec4..47f426e85864 100644
@@ -446,6 +446,10 @@ static int ffa_domain_init(struct domain *d)
     if ( ret )
         return ret;
 
+    ret = ffa_rxtx_domain_init(d);
+    if ( ret )
+        return ret;
+
     return ffa_notif_domain_init(d);
 }
 
xen/arch/arm/tee/ffa_private.h +3 -2
index 88b85c7c453a..4272afd37343 100644
@@ -427,10 +427,11 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs);
 
 bool ffa_rxtx_init(void);
 void ffa_rxtx_destroy(void);
+int32_t ffa_rxtx_domain_init(struct domain *d);
 void ffa_rxtx_domain_destroy(struct domain *d);
-uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
+int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 			     register_t rx_addr, uint32_t page_count);
-uint32_t ffa_handle_rxtx_unmap(void);
+int32_t ffa_handle_rxtx_unmap(void);
 int32_t ffa_rx_acquire(struct domain *d);
 int32_t ffa_rx_release(struct domain *d);
 
xen/arch/arm/tee/ffa_rxtx.c +21 -7
index a40e5b32e3a5..cd467d1dba68 100644
@@ -41,10 +41,10 @@ static int32_t ffa_rxtx_unmap(uint16_t id)
     return ffa_simple_call(FFA_RXTX_UNMAP, ((uint64_t)id) << 16, 0, 0, 0);
 }
 
-uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
+int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 			     register_t rx_addr, uint32_t page_count)
 {
-    uint32_t ret = FFA_RET_INVALID_PARAMETERS;
+    int32_t ret = FFA_RET_INVALID_PARAMETERS;
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
     struct page_info *tx_pg;
@@ -66,13 +66,17 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
         rx_addr &= UINT32_MAX;
     }
 
-    if ( page_count > FFA_MAX_RXTX_PAGE_COUNT )
+    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);
         return FFA_RET_INVALID_PARAMETERS;
     }
 
+    if ( !IS_ALIGNED(tx_addr, FFA_PAGE_SIZE) ||
+         !IS_ALIGNED(rx_addr, FFA_PAGE_SIZE) )
+        return FFA_RET_INVALID_PARAMETERS;
+
     /* Already mapped */
     if ( ctx->rx )
         return FFA_RET_DENIED;
@@ -181,7 +185,7 @@ err_put_tx_pg:
     return ret;
 }
 
-static uint32_t  rxtx_unmap(struct domain *d)
+static int32_t rxtx_unmap(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
 
@@ -190,7 +194,7 @@ static uint32_t  rxtx_unmap(struct domain *d)
 
     if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
     {
-        uint32_t ret;
+        int32_t ret;
 
         ret = ffa_rxtx_unmap(ffa_get_vm_id(d));
         if ( ret != FFA_RET_OK )
@@ -211,7 +215,7 @@ static uint32_t  rxtx_unmap(struct domain *d)
     return FFA_RET_OK;
 }
 
-uint32_t ffa_handle_rxtx_unmap(void)
+int32_t ffa_handle_rxtx_unmap(void)
 {
     return rxtx_unmap(current->domain);
 }
@@ -272,6 +276,16 @@ out:
     return ret;
 }
 
+int32_t ffa_rxtx_domain_init(struct domain *d)
+{
+    struct ffa_ctx *ctx = d->arch.tee;
+
+    spin_lock_init(&ctx->rx_lock);
+    spin_lock_init(&ctx->tx_lock);
+
+    return 0;
+}
+
 void ffa_rxtx_domain_destroy(struct domain *d)
 {
     rxtx_unmap(d);
@@ -298,7 +312,7 @@ void ffa_rxtx_destroy(void)
 
 bool ffa_rxtx_init(void)
 {
-    int e;
+    int32_t e;
 
     /* Firmware not there or not supporting */
     if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
Signature · 1 line
2.51.2
Reviewed-by on v1 3/10
#10 lore raw

Re: [PATCH 03/10] xen/arm: ffa: harden RX/TX mapping

Hi Bertrand,

Quoted 15 lines · Bertrand Marquis

Harden the RX/TX mapping paths and keep signed FF-A return codes end-to-end.

Reject zero-length mappings and insist on page-aligned RX/TX buffer addresses before touching the P2M. The unmap plumbing is switched to use the same signed helpers so dispatcher error handling is consistent across map and unmap operations.

This avoids partially mapped or silently truncated buffers and makes the mediator behaviour match the FF-A error model more closely.

While there also introduce a domain_rxtx_init to properly initialize the rxtx buffers spinlocks.

Quoted 6 lines
---
 xen/arch/arm/tee/ffa.c         |  4 ++++
 xen/arch/arm/tee/ffa_private.h |  5 +++--
 xen/arch/arm/tee/ffa_rxtx.c    | 28 +++++++++++++++++++++-------
 3 files changed, 28 insertions(+), 9 deletions(-)

Looks good

Cheers,
Jens

Quoted 126 lines
xen/arch/arm/tee/ffa.c +4 -0
index 3309ca875ec4..47f426e85864 100644
@@ -446,6 +446,10 @@ static int ffa_domain_init(struct domain *d)
     if ( ret )
         return ret;
 
+    ret = ffa_rxtx_domain_init(d);
+    if ( ret )
+        return ret;
+
     return ffa_notif_domain_init(d);
 }
 
xen/arch/arm/tee/ffa_private.h +3 -2
index 88b85c7c453a..4272afd37343 100644
@@ -427,10 +427,11 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs);
 
 bool ffa_rxtx_init(void);
 void ffa_rxtx_destroy(void);
+int32_t ffa_rxtx_domain_init(struct domain *d);
 void ffa_rxtx_domain_destroy(struct domain *d);
-uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
+int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                             register_t rx_addr, uint32_t page_count);
-uint32_t ffa_handle_rxtx_unmap(void);
+int32_t ffa_handle_rxtx_unmap(void);
 int32_t ffa_rx_acquire(struct domain *d);
 int32_t ffa_rx_release(struct domain *d);
 
xen/arch/arm/tee/ffa_rxtx.c +21 -8
index a40e5b32e3a5..cd467d1dba68 100644
@@ -41,10 +41,10 @@ static int32_t ffa_rxtx_unmap(uint16_t id)
     return ffa_simple_call(FFA_RXTX_UNMAP, ((uint64_t)id) << 16, 0, 0, 0);
 }
 
-uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
+int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                             register_t rx_addr, uint32_t page_count)
 {
-    uint32_t ret = FFA_RET_INVALID_PARAMETERS;
+    int32_t ret = FFA_RET_INVALID_PARAMETERS;
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
     struct page_info *tx_pg;
@@ -66,13 +66,17 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
         rx_addr &= UINT32_MAX;
     }
 
-    if ( page_count > FFA_MAX_RXTX_PAGE_COUNT )
+    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);
         return FFA_RET_INVALID_PARAMETERS;
     }
 
+    if ( !IS_ALIGNED(tx_addr, FFA_PAGE_SIZE) ||
+         !IS_ALIGNED(rx_addr, FFA_PAGE_SIZE) )
+        return FFA_RET_INVALID_PARAMETERS;
+
     /* Already mapped */
     if ( ctx->rx )
         return FFA_RET_DENIED;
@@ -181,7 +185,7 @@ err_put_tx_pg:
     return ret;
 }
 
-static uint32_t  rxtx_unmap(struct domain *d)
+static int32_t rxtx_unmap(struct domain *d)
 {
     struct ffa_ctx *ctx = d->arch.tee;
 
@@ -190,7 +194,7 @@ static uint32_t  rxtx_unmap(struct domain *d)
 
     if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
     {
-        uint32_t ret;
+        int32_t ret;
 
         ret = ffa_rxtx_unmap(ffa_get_vm_id(d));
         if ( ret != FFA_RET_OK )
@@ -211,7 +215,7 @@ static uint32_t  rxtx_unmap(struct domain *d)
     return FFA_RET_OK;
 }
 
-uint32_t ffa_handle_rxtx_unmap(void)
+int32_t ffa_handle_rxtx_unmap(void)
 {
     return rxtx_unmap(current->domain);
 }
@@ -272,6 +276,16 @@ out:
     return ret;
 }
 
+int32_t ffa_rxtx_domain_init(struct domain *d)
+{
+    struct ffa_ctx *ctx = d->arch.tee;
+
+    spin_lock_init(&ctx->rx_lock);
+    spin_lock_init(&ctx->tx_lock);
+
+    return 0;
+}
+
 void ffa_rxtx_domain_destroy(struct domain *d)
 {
     rxtx_unmap(d);
@@ -298,7 +312,7 @@ void ffa_rxtx_destroy(void)
 
 bool ffa_rxtx_init(void)
 {
-    int e;
+    int32_t e;
 
     /* Firmware not there or not supporting */
     if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
--

2.51.2

#11 lore raw

PATCH 4/10 xen/arm: ffa: rework SPMC RX/TX buffer management

In reply to Bertrand Marquis

Rework how Xen accesses the RX/TX buffers shared with the SPMC so that ownership and locking are handled centrally.

Move the SPMC RX/TX buffer bases into ffa_rxtx.c as ffa_spmc_rx/ffa_spmc_tx, protect them with dedicated ffa_spmc_{rx,tx}_lock spinlocks and expose ffa_rxtx_spmc_{rx,tx}_{acquire,release}() helpers instead of the global ffa_rx/ffa_tx pointers and ffa_{rx,tx}_buffer_lock.

The RX helpers now always issue FFA_RX_RELEASE when we are done consuming data from the SPMC, so partition-info enumeration and shared memory paths release the RX buffer on all exit paths. The RX/TX mapping code is updated to use the descriptor offsets (rx_region_offs and tx_region_offs) rather than hard-coded structure layout, and to use the TX acquire/release helpers instead of touching the TX buffer directly.

---
 xen/arch/arm/tee/ffa.c          |  22 +-----
 xen/arch/arm/tee/ffa_partinfo.c |  40 +++++-----
 xen/arch/arm/tee/ffa_private.h  |  18 ++---
 xen/arch/arm/tee/ffa_rxtx.c     | 132 +++++++++++++++++++++++++-------
 xen/arch/arm/tee/ffa_shm.c      |  26 ++++---
 5 files changed, 153 insertions(+), 85 deletions(-)
xen/arch/arm/tee/ffa.c +4 -18
index 47f426e85864..4c1b9a4c3b48 100644
@@ -48,8 +48,8 @@
  *     notification for secure partitions
  *   - doesn't support notifications for Xen itself
  *
- * There are some large locked sections with ffa_tx_buffer_lock and
- * ffa_rx_buffer_lock. Especially the ffa_tx_buffer_lock spinlock used
+ * There are some large locked sections with ffa_spmc_tx_lock and
+ * ffa_spmc_rx_lock. Especially the ffa_spmc_tx_lock spinlock used
  * around share_shm() is a very large locked section which can let one VM
  * affect another VM.
  */
@@ -108,20 +108,6 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
     FW_ABI(FFA_RUN),
 };
 
-/*
- * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
- * number of pages used in each of these buffers.
- *
- * The RX buffer is protected from concurrent usage with ffa_rx_buffer_lock.
- * Note that the SPMC is also tracking the ownership of our RX buffer so
- * for calls which uses our RX buffer to deliver a result we must call
- * ffa_rx_release() to let the SPMC know that we're done with the buffer.
- */
-void *ffa_rx __read_mostly;
-void *ffa_tx __read_mostly;
-DEFINE_SPINLOCK(ffa_rx_buffer_lock);
-DEFINE_SPINLOCK(ffa_tx_buffer_lock);
-
 LIST_HEAD(ffa_ctx_head);
 /* RW Lock to protect addition/removal and reading in ffa_ctx_head */
 DEFINE_RWLOCK(ffa_ctx_list_rwlock);
@@ -612,7 +598,7 @@ static bool ffa_probe_fw(void)
                    ffa_fw_abi_needed[i].name);
     }
 
-    if ( !ffa_rxtx_init() )
+    if ( !ffa_rxtx_spmc_init() )
     {
         printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
         goto err_no_fw;
@@ -626,7 +612,7 @@ static bool ffa_probe_fw(void)
     return true;
 
 err_rxtx_destroy:
-    ffa_rxtx_destroy();
+    ffa_rxtx_spmc_destroy();
 err_no_fw:
     ffa_fw_version = 0;
     bitmap_zero(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
xen/arch/arm/tee/ffa_partinfo.c +19 -21
index fa56b1587e3b..766b75dffb8c 100644
@@ -77,28 +77,24 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
 {
     int32_t ret;
     uint32_t src_size, real_sp_count;
-    void *src_buf = ffa_rx;
+    void *src_buf;
     uint32_t count = 0;
 
-    /* Do we have a RX buffer with the SPMC */
-    if ( !ffa_rx )
-        return FFA_RET_DENIED;
-
     /* We need to use the RX buffer to receive the list */
-    spin_lock(&ffa_rx_buffer_lock);
+    src_buf = ffa_rxtx_spmc_rx_acquire();
+    if ( !src_buf )
+        return FFA_RET_DENIED;
 
     ret = ffa_partition_info_get(uuid, 0, &real_sp_count, &src_size);
     if ( ret )
         goto out;
 
-    /* We now own the RX buffer */
-
     /* Validate the src_size we got */
     if ( src_size < sizeof(struct ffa_partition_info_1_0) ||
          src_size >= FFA_PAGE_SIZE )
     {
         ret = FFA_RET_NOT_SUPPORTED;
-        goto out_release;
+        goto out;
     }
 
     /*
@@ -114,7 +110,7 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
     if ( real_sp_count > (FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE) / src_size )
     {
         ret = FFA_RET_NOT_SUPPORTED;
-        goto out_release;
+        goto out;
     }
 
     for ( uint32_t sp_num = 0; sp_num < real_sp_count; sp_num++ )
@@ -127,7 +123,7 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
             if ( dst_buf > (end_buf - dst_size) )
             {
                 ret = FFA_RET_NO_MEMORY;
-                goto out_release;
+                goto out;
             }
 
             memcpy(dst_buf, src_buf, MIN(src_size, dst_size));
@@ -143,10 +139,8 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
 
     *sp_count = count;
 
-out_release:
-    ffa_hyp_rx_release();
 out:
-    spin_unlock(&ffa_rx_buffer_lock);
+    ffa_rxtx_spmc_rx_release();
     return ret;
 }
 
@@ -378,7 +372,7 @@ static void uninit_subscribers(void)
         XFREE(subscr_vm_destroyed);
 }
 
-static bool init_subscribers(uint16_t count, uint32_t fpi_size)
+static bool init_subscribers(void *buf, uint16_t count, uint32_t fpi_size)
 {
     uint16_t n;
     uint16_t c_pos;
@@ -395,7 +389,7 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
     subscr_vm_destroyed_count = 0;
     for ( n = 0; n < count; n++ )
     {
-        fpi = ffa_rx + n * fpi_size;
+        fpi = buf + n * fpi_size;
 
         /*
          * We need to have secure partitions using bit 15 set convention for
@@ -433,7 +427,7 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
 
     for ( c_pos = 0, d_pos = 0, n = 0; n < count; n++ )
     {
-        fpi = ffa_rx + n * fpi_size;
+        fpi = buf + n * fpi_size;
 
         if ( FFA_ID_IS_SECURE(fpi->id) )
         {
@@ -455,10 +449,14 @@ bool ffa_partinfo_init(void)
     uint32_t fpi_size;
     uint32_t count;
     int e;
+    void *spmc_rx;
 
     if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
-         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) ||
-         !ffa_rx || !ffa_tx )
+         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32))
+        return false;
+
+    spmc_rx = ffa_rxtx_spmc_rx_acquire();
+    if (!spmc_rx)
         return false;
 
     e = ffa_partition_info_get(NULL, 0, &count, &fpi_size);
@@ -475,10 +473,10 @@ bool ffa_partinfo_init(void)
         goto out;
     }
 
-    ret = init_subscribers(count, fpi_size);
+    ret = init_subscribers(spmc_rx, count, fpi_size);
 
 out:
-    ffa_hyp_rx_release();
+    ffa_rxtx_spmc_rx_release();
     return ret;
 }
 
xen/arch/arm/tee/ffa_private.h +7 -11
index 4272afd37343..cd35c44b8986 100644
@@ -404,10 +404,6 @@ struct ffa_ctx {
     unsigned long *vm_destroy_bitmap;
 };
 
-extern void *ffa_rx;
-extern void *ffa_tx;
-extern spinlock_t ffa_rx_buffer_lock;
-extern spinlock_t ffa_tx_buffer_lock;
 extern DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 extern struct list_head ffa_ctx_head;
@@ -425,8 +421,13 @@ int 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);
 
-bool ffa_rxtx_init(void);
-void ffa_rxtx_destroy(void);
+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);
+void *ffa_rxtx_spmc_tx_acquire(void);
+void ffa_rxtx_spmc_tx_release(void);
+
 int32_t ffa_rxtx_domain_init(struct domain *d);
 void ffa_rxtx_domain_destroy(struct domain *d);
 int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
@@ -556,11 +557,6 @@ static inline int32_t ffa_simple_call(uint32_t fid, register_t a1,
     return ffa_get_ret_code(&resp);
 }
 
-static inline int32_t ffa_hyp_rx_release(void)
-{
-    return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
-}
-
 static inline bool ffa_fw_supports_fid(uint32_t fid)
 {
     BUILD_BUG_ON(FFA_FNUM_MIN_VALUE > FFA_FNUM_MAX_VALUE);
xen/arch/arm/tee/ffa_rxtx.c +106 -26

190 lines not on this page · raw message

xen/arch/arm/tee/ffa_shm.c +17 -9
index d628c1b70609..b9022797c3bf 100644
@@ -286,9 +286,8 @@ static void init_range(struct ffa_address_range *addr_range,
 }
 
 /*
- * This function uses the ffa_tx buffer to transmit the memory transaction
- * descriptor. The function depends ffa_tx_buffer_lock to be used to guard
- * the buffer from concurrent use.
+ * This function uses the ffa_spmc tx buffer to transmit the memory transaction
+ * descriptor.
  */
 static int share_shm(struct ffa_shm_mem *shm)
 {
@@ -298,17 +297,22 @@ static int share_shm(struct ffa_shm_mem *shm)
     struct ffa_address_range *addr_range;
     struct ffa_mem_region *region_descr;
     const unsigned int region_count = 1;
-    void *buf = ffa_tx;
     uint32_t frag_len;
     uint32_t tot_len;
     paddr_t last_pa;
     unsigned int n;
     paddr_t pa;
+    int32_t ret;
+    void *buf;
 
-    ASSERT(spin_is_locked(&ffa_tx_buffer_lock));
     ASSERT(shm->page_count);
 
+    buf = ffa_rxtx_spmc_tx_acquire();
+    if ( !buf )
+        return FFA_RET_NOT_SUPPORTED;
+
     descr = buf;
+
     memset(descr, 0, sizeof(*descr));
     descr->sender_id = shm->sender_id;
     descr->handle = shm->handle;
@@ -340,7 +344,10 @@ static int share_shm(struct ffa_shm_mem *shm)
     tot_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count,
                                 region_descr->address_range_count);
     if ( tot_len > max_frag_len )
+    {
+        ffa_rxtx_spmc_tx_release();
         return FFA_RET_NOT_SUPPORTED;
+    }
 
     addr_range = region_descr->address_range_array;
     frag_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count, 1);
@@ -360,7 +367,11 @@ static int share_shm(struct ffa_shm_mem *shm)
         init_range(addr_range, pa);
     }
 
-    return ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);
+    ret = ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);
+
+    ffa_rxtx_spmc_tx_release();
+
+    return ret;
 }
 
 static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen,
@@ -578,10 +589,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     if ( ret )
         goto out;
 
-    /* Note that share_shm() uses our tx buffer */
-    spin_lock(&ffa_tx_buffer_lock);
     ret = share_shm(shm);
-    spin_unlock(&ffa_tx_buffer_lock);
     if ( ret )
         goto out;
 
Signature · 1 line
2.51.2
#12 lore raw

Re: [PATCH 04/10] xen/arm: ffa: rework SPMC RX/TX buffer management

Hi Bertrand,

Quoted 312 lines · Bertrand Marquis

Rework how Xen accesses the RX/TX buffers shared with the SPMC so that ownership and locking are handled centrally.

Move the SPMC RX/TX buffer bases into ffa_rxtx.c as ffa_spmc_rx/ffa_spmc_tx, protect them with dedicated ffa_spmc_{rx,tx}_lock spinlocks and expose ffa_rxtx_spmc_{rx,tx}_{acquire,release}() helpers instead of the global ffa_rx/ffa_tx pointers and ffa_{rx,tx}_buffer_lock.

The RX helpers now always issue FFA_RX_RELEASE when we are done consuming data from the SPMC, so partition-info enumeration and shared memory paths release the RX buffer on all exit paths. The RX/TX mapping code is updated to use the descriptor offsets (rx_region_offs and tx_region_offs) rather than hard-coded structure layout, and to use the TX acquire/release helpers instead of touching the TX buffer directly.

---
 xen/arch/arm/tee/ffa.c          |  22 +-----
 xen/arch/arm/tee/ffa_partinfo.c |  40 +++++-----
 xen/arch/arm/tee/ffa_private.h  |  18 ++---
 xen/arch/arm/tee/ffa_rxtx.c     | 132 +++++++++++++++++++++++++-------
 xen/arch/arm/tee/ffa_shm.c      |  26 ++++---
 5 files changed, 153 insertions(+), 85 deletions(-)
xen/arch/arm/tee/ffa.c +4 -18
index 47f426e85864..4c1b9a4c3b48 100644
@@ -48,8 +48,8 @@
  *     notification for secure partitions
  *   - doesn't support notifications for Xen itself
  *
- * There are some large locked sections with ffa_tx_buffer_lock and
- * ffa_rx_buffer_lock. Especially the ffa_tx_buffer_lock spinlock used
+ * There are some large locked sections with ffa_spmc_tx_lock and
+ * ffa_spmc_rx_lock. Especially the ffa_spmc_tx_lock spinlock used
  * around share_shm() is a very large locked section which can let one VM
  * affect another VM.
  */
@@ -108,20 +108,6 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
     FW_ABI(FFA_RUN),
 };
 
-/*
- * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
- * number of pages used in each of these buffers.
- *
- * The RX buffer is protected from concurrent usage with ffa_rx_buffer_lock.
- * Note that the SPMC is also tracking the ownership of our RX buffer so
- * for calls which uses our RX buffer to deliver a result we must call
- * ffa_rx_release() to let the SPMC know that we're done with the buffer.
- */
-void *ffa_rx __read_mostly;
-void *ffa_tx __read_mostly;
-DEFINE_SPINLOCK(ffa_rx_buffer_lock);
-DEFINE_SPINLOCK(ffa_tx_buffer_lock);
-
 LIST_HEAD(ffa_ctx_head);
 /* RW Lock to protect addition/removal and reading in ffa_ctx_head */
 DEFINE_RWLOCK(ffa_ctx_list_rwlock);
@@ -612,7 +598,7 @@ static bool ffa_probe_fw(void)
                    ffa_fw_abi_needed[i].name);
     }
 
-    if ( !ffa_rxtx_init() )
+    if ( !ffa_rxtx_spmc_init() )
     {
         printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
         goto err_no_fw;
@@ -626,7 +612,7 @@ static bool ffa_probe_fw(void)
     return true;
 
 err_rxtx_destroy:
-    ffa_rxtx_destroy();
+    ffa_rxtx_spmc_destroy();
 err_no_fw:
     ffa_fw_version = 0;
     bitmap_zero(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
xen/arch/arm/tee/ffa_partinfo.c +19 -21
index fa56b1587e3b..766b75dffb8c 100644
@@ -77,28 +77,24 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
 {
     int32_t ret;
     uint32_t src_size, real_sp_count;
-    void *src_buf = ffa_rx;
+    void *src_buf;
     uint32_t count = 0;
 
-    /* Do we have a RX buffer with the SPMC */
-    if ( !ffa_rx )
-        return FFA_RET_DENIED;
-
     /* We need to use the RX buffer to receive the list */
-    spin_lock(&ffa_rx_buffer_lock);
+    src_buf = ffa_rxtx_spmc_rx_acquire();
+    if ( !src_buf )
+        return FFA_RET_DENIED;
 
     ret = ffa_partition_info_get(uuid, 0, &real_sp_count, &src_size);
     if ( ret )
         goto out;
 
-    /* We now own the RX buffer */
-
     /* Validate the src_size we got */
     if ( src_size < sizeof(struct ffa_partition_info_1_0) ||
          src_size >= FFA_PAGE_SIZE )
     {
         ret = FFA_RET_NOT_SUPPORTED;
-        goto out_release;
+        goto out;
     }
 
     /*
@@ -114,7 +110,7 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
     if ( real_sp_count > (FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE) / src_size )
     {
         ret = FFA_RET_NOT_SUPPORTED;
-        goto out_release;
+        goto out;
     }
 
     for ( uint32_t sp_num = 0; sp_num < real_sp_count; sp_num++ )
@@ -127,7 +123,7 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
             if ( dst_buf > (end_buf - dst_size) )
             {
                 ret = FFA_RET_NO_MEMORY;
-                goto out_release;
+                goto out;
             }
 
             memcpy(dst_buf, src_buf, MIN(src_size, dst_size));
@@ -143,10 +139,8 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
 
     *sp_count = count;
 
-out_release:
-    ffa_hyp_rx_release();
 out:
-    spin_unlock(&ffa_rx_buffer_lock);
+    ffa_rxtx_spmc_rx_release();
     return ret;
 }
 
@@ -378,7 +372,7 @@ static void uninit_subscribers(void)
         XFREE(subscr_vm_destroyed);
 }
 
-static bool init_subscribers(uint16_t count, uint32_t fpi_size)
+static bool init_subscribers(void *buf, uint16_t count, uint32_t fpi_size)
 {
     uint16_t n;
     uint16_t c_pos;
@@ -395,7 +389,7 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
     subscr_vm_destroyed_count = 0;
     for ( n = 0; n < count; n++ )
     {
-        fpi = ffa_rx + n * fpi_size;
+        fpi = buf + n * fpi_size;
 
         /*
          * We need to have secure partitions using bit 15 set convention for
@@ -433,7 +427,7 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
 
     for ( c_pos = 0, d_pos = 0, n = 0; n < count; n++ )
     {
-        fpi = ffa_rx + n * fpi_size;
+        fpi = buf + n * fpi_size;
 
         if ( FFA_ID_IS_SECURE(fpi->id) )
         {
@@ -455,10 +449,14 @@ bool ffa_partinfo_init(void)
     uint32_t fpi_size;
     uint32_t count;
     int e;
+    void *spmc_rx;
 
     if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
-         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) ||
-         !ffa_rx || !ffa_tx )
+         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32))
+        return false;
+
+    spmc_rx = ffa_rxtx_spmc_rx_acquire();
+    if (!spmc_rx)
         return false;
 
     e = ffa_partition_info_get(NULL, 0, &count, &fpi_size);
@@ -475,10 +473,10 @@ bool ffa_partinfo_init(void)
         goto out;
     }
 
-    ret = init_subscribers(count, fpi_size);
+    ret = init_subscribers(spmc_rx, count, fpi_size);
 
 out:
-    ffa_hyp_rx_release();
+    ffa_rxtx_spmc_rx_release();
     return ret;
 }
 
xen/arch/arm/tee/ffa_private.h +7 -11
index 4272afd37343..cd35c44b8986 100644
@@ -404,10 +404,6 @@ struct ffa_ctx {
     unsigned long *vm_destroy_bitmap;
 };
 
-extern void *ffa_rx;
-extern void *ffa_tx;
-extern spinlock_t ffa_rx_buffer_lock;
-extern spinlock_t ffa_tx_buffer_lock;
 extern DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 extern struct list_head ffa_ctx_head;
@@ -425,8 +421,13 @@ int 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);
 
-bool ffa_rxtx_init(void);
-void ffa_rxtx_destroy(void);
+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);
+void *ffa_rxtx_spmc_tx_acquire(void);
+void ffa_rxtx_spmc_tx_release(void);
+
 int32_t ffa_rxtx_domain_init(struct domain *d);
 void ffa_rxtx_domain_destroy(struct domain *d);
 int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
@@ -556,11 +557,6 @@ static inline int32_t ffa_simple_call(uint32_t fid, register_t a1,
     return ffa_get_ret_code(&resp);
 }
 
-static inline int32_t ffa_hyp_rx_release(void)
-{
-    return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
-}
-
 static inline bool ffa_fw_supports_fid(uint32_t fid)
 {
     BUILD_BUG_ON(FFA_FNUM_MIN_VALUE > FFA_FNUM_MAX_VALUE);
xen/arch/arm/tee/ffa_rxtx.c +20 -5
index cd467d1dba68..07b01430d139 100644
@@ -30,6 +30,20 @@ struct ffa_endpoint_rxtx_descriptor_1_1 {
     uint32_t tx_region_offs;
 };
 
+/*
+ * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
+ * number of pages used in each of these buffers.
+ * Each buffer has its own lock to protect from concurrent usage.
+ *
+ * Note that the SPMC is also tracking the ownership of our RX buffer so
+ * for calls which uses our RX buffer to deliver a result we must do an
+ * FFA_RX_RELEASE to let the SPMC know that we're done with the buffer.
+ */
+static void *ffa_spmc_rx __read_mostly;
+static void *ffa_spmc_tx __read_mostly;
+static DEFINE_SPINLOCK(ffa_spmc_rx_lock);
+static DEFINE_SPINLOCK(ffa_spmc_tx_lock);
+
 static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
                             uint32_t page_count)
 {
@@ -120,8 +134,9 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                      sizeof(struct ffa_address_range) * 2 >
                      FFA_MAX_RXTX_PAGE_COUNT * FFA_PAGE_SIZE);
 
-        spin_lock(&ffa_tx_buffer_lock);
-        rxtx_desc = ffa_tx;
+        rxtx_desc = ffa_rxtx_spmc_tx_acquire();
+        if ( !rxtx_desc )
+            goto err_unmap_rx;
 
         /*
          * We have only one page for each so we pack everything:
@@ -138,7 +153,7 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                                              address_range_array[1]);
 
         /* rx buffer */
-        mem_reg = ffa_tx + sizeof(*rxtx_desc);
+        mem_reg = (void *)rxtx_desc + rxtx_desc->rx_region_offs;
         mem_reg->total_page_count = 1;
         mem_reg->address_range_count = 1;
         mem_reg->reserved = 0;
@@ -148,7 +163,7 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
         mem_reg->address_range_array[0].reserved = 0;
 
         /* tx buffer */
-        mem_reg = ffa_tx + rxtx_desc->tx_region_offs;
+        mem_reg = (void *)rxtx_desc + rxtx_desc->tx_region_offs;
         mem_reg->total_page_count = 1;
         mem_reg->address_range_count = 1;
         mem_reg->reserved = 0;
@@ -159,7 +174,7 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 
         ret = ffa_rxtx_map(0, 0, 0);
 
-        spin_unlock(&ffa_tx_buffer_lock);
+        ffa_rxtx_spmc_tx_release();
 
         if ( ret != FFA_RET_OK )
             goto err_unmap_rx;

xen/arch/arm/tee/ffa_rxtx.c void ffa_rxtx_domain_destroy(struct domain *d) · line 311

Quoted 7 lines
xen/arch/arm/tee/ffa_rxtx.c +3 -1
@@ -291,49 +306,114 @@ void ffa_rxtx_domain_destroy(struct domain *d)
     rxtx_unmap(d);
 }
 
-void ffa_rxtx_destroy(void)
+void *ffa_rxtx_spmc_rx_acquire(void)
+{
+    ASSERT(!spin_is_locked(&ffa_spmc_rx_lock));

Is it invalid for two CPUs to concurrently try to acquire the RX buffer?

Quoted 18 lines
xen/arch/arm/tee/ffa_rxtx.c +18 -0
+
+    spin_lock(&ffa_spmc_rx_lock);
+
+    if ( ffa_spmc_rx )
+        return ffa_spmc_rx;
+
+    spin_unlock(&ffa_spmc_rx_lock);
+    return NULL;
+}
+
+void ffa_rxtx_spmc_rx_release(void)
+{
+    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);

xen/arch/arm/tee/ffa_rxtx.c void ffa_rxtx_domain_destroy(struct domain *d) · line 338

Quoted 10 lines
xen/arch/arm/tee/ffa_rxtx.c +8 -1
+    if ( ret != FFA_RET_OK )
+        printk(XENLOG_DEBUG "Error releasing SPMC RX buffer: %d\n", ret);
+
+    spin_unlock(&ffa_spmc_rx_lock);
+}
+
+void *ffa_rxtx_spmc_tx_acquire(void)
 {
-    bool need_unmap = ffa_tx && ffa_rx;
+    ASSERT(!spin_is_locked(&ffa_spmc_tx_lock));

Is it invalid for two CPUs to concurrently try to acquire the TX buffer?

Quoted 142 lines
xen/arch/arm/tee/ffa_rxtx.c +57 -19
-    if ( ffa_tx )
+    spin_lock(&ffa_spmc_tx_lock);
+
+    if ( ffa_spmc_tx )
+        return ffa_spmc_tx;
+
+    spin_unlock(&ffa_spmc_tx_lock);
+    return NULL;
+}
+
+void ffa_rxtx_spmc_tx_release(void)
+{
+    ASSERT(spin_is_locked(&ffa_spmc_tx_lock));
+
+    spin_unlock(&ffa_spmc_tx_lock);
+}
+
+void ffa_rxtx_spmc_destroy(void)
+{
+    bool need_unmap;
+
+    spin_lock(&ffa_spmc_rx_lock);
+    spin_lock(&ffa_spmc_tx_lock);
+    need_unmap = ffa_spmc_tx && ffa_spmc_rx;
+
+    if ( ffa_spmc_tx )
     {
-        free_xenheap_pages(ffa_tx, 0);
-        ffa_tx = NULL;
+        free_xenheap_pages(ffa_spmc_tx, 0);
+        ffa_spmc_tx = NULL;
     }
-    if ( ffa_rx )
+    if ( ffa_spmc_rx )
     {
-        free_xenheap_pages(ffa_rx, 0);
-        ffa_rx = NULL;
+        free_xenheap_pages(ffa_spmc_rx, 0);
+        ffa_spmc_rx = NULL;
     }
 
     if ( need_unmap )
         ffa_rxtx_unmap(0);
+
+    spin_unlock(&ffa_spmc_tx_lock);
+    spin_unlock(&ffa_spmc_rx_lock);
 }
 
-bool ffa_rxtx_init(void)
+bool ffa_rxtx_spmc_init(void)
 {
     int32_t e;
+    bool ret = false;
 
     /* Firmware not there or not supporting */
     if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
         return false;
 
-    ffa_rx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
-    if ( !ffa_rx )
-        return false;
+    spin_lock(&ffa_spmc_rx_lock);
+    spin_lock(&ffa_spmc_tx_lock);
+
+    ffa_spmc_rx = alloc_xenheap_pages(
+                            get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
+    if ( !ffa_spmc_rx )
+        goto exit;
 
-    ffa_tx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
-    if ( !ffa_tx )
-        goto err;
+    ffa_spmc_tx = alloc_xenheap_pages(
+                            get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
+    if ( !ffa_spmc_tx )
+        goto exit;
 
-    e = ffa_rxtx_map(__pa(ffa_tx), __pa(ffa_rx), FFA_RXTX_PAGE_COUNT);
+    e = ffa_rxtx_map(__pa(ffa_spmc_tx), __pa(ffa_spmc_rx),
+                     FFA_RXTX_PAGE_COUNT);
     if ( e )
-        goto err;
+        goto exit;
 
-    return true;
+    ret = true;
 
-err:
-    ffa_rxtx_destroy();
+exit:
+    spin_unlock(&ffa_spmc_tx_lock);
+    spin_unlock(&ffa_spmc_rx_lock);
 
-    return false;
+    if ( !ret )
+        ffa_rxtx_spmc_destroy();
+
+    return ret;
 }
xen/arch/arm/tee/ffa_shm.c +9 -5
index d628c1b70609..b9022797c3bf 100644
@@ -286,9 +286,8 @@ static void init_range(struct ffa_address_range *addr_range,
 }
 
 /*
- * This function uses the ffa_tx buffer to transmit the memory transaction
- * descriptor. The function depends ffa_tx_buffer_lock to be used to guard
- * the buffer from concurrent use.
+ * This function uses the ffa_spmc tx buffer to transmit the memory transaction
+ * descriptor.
  */
 static int share_shm(struct ffa_shm_mem *shm)
 {
@@ -298,17 +297,22 @@ static int share_shm(struct ffa_shm_mem *shm)
     struct ffa_address_range *addr_range;
     struct ffa_mem_region *region_descr;
     const unsigned int region_count = 1;
-    void *buf = ffa_tx;
     uint32_t frag_len;
     uint32_t tot_len;
     paddr_t last_pa;
     unsigned int n;
     paddr_t pa;
+    int32_t ret;
+    void *buf;
 
-    ASSERT(spin_is_locked(&ffa_tx_buffer_lock));
     ASSERT(shm->page_count);
 
+    buf = ffa_rxtx_spmc_tx_acquire();
+    if ( !buf )
+        return FFA_RET_NOT_SUPPORTED;
+
     descr = buf;
+
     memset(descr, 0, sizeof(*descr));
     descr->sender_id = shm->sender_id;
     descr->handle = shm->handle;

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

Quoted 6 lines
xen/arch/arm/tee/ffa_shm.c +2 -0
@@ -340,7 +344,10 @@ static int share_shm(struct ffa_shm_mem *shm)
     tot_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count,
                                 region_descr->address_range_count);
     if ( tot_len > max_frag_len )
+    {
+        ffa_rxtx_spmc_tx_release();
         return FFA_RET_NOT_SUPPORTED;

I'd prefer adding an out label below before the
ffa_rxtx_spmc_tx_release() call and:

     ret = FFA_RET_NOT_SUPPORTED;
     goto out;

Cheers,
Jens

Quoted 31 lines
xen/arch/arm/tee/ffa_shm.c +6 -5
+    }
 
     addr_range = region_descr->address_range_array;
     frag_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count, 1);
@@ -360,7 +367,11 @@ static int share_shm(struct ffa_shm_mem *shm)
         init_range(addr_range, pa);
     }
 
-    return ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);
+    ret = ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);
+
+    ffa_rxtx_spmc_tx_release();
+
+    return ret;
 }
 
 static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen,
@@ -578,10 +589,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     if ( ret )
         goto out;
 
-    /* Note that share_shm() uses our tx buffer */
-    spin_lock(&ffa_tx_buffer_lock);
     ret = share_shm(shm);
-    spin_unlock(&ffa_tx_buffer_lock);
     if ( ret )
         goto out;
 
--

2.51.2

#13 lore raw

Re: [PATCH 04/10] xen/arm: ffa: rework SPMC RX/TX buffer management

Hi Jens,

Quoted 321 lines · Jens Wiklander

Hi Bertrand,

Quoted 315 lines · Bertrand Marquis

Rework how Xen accesses the RX/TX buffers shared with the SPMC so that ownership and locking are handled centrally.

Move the SPMC RX/TX buffer bases into ffa_rxtx.c as ffa_spmc_rx/ffa_spmc_tx, protect them with dedicated ffa_spmc_{rx,tx}_lock spinlocks and expose ffa_rxtx_spmc_{rx,tx}_{acquire,release}() helpers instead of the global ffa_rx/ffa_tx pointers and ffa_{rx,tx}_buffer_lock.

The RX helpers now always issue FFA_RX_RELEASE when we are done consuming data from the SPMC, so partition-info enumeration and shared memory paths release the RX buffer on all exit paths. The RX/TX mapping code is updated to use the descriptor offsets (rx_region_offs and tx_region_offs) rather than hard-coded structure layout, and to use the TX acquire/release helpers instead of touching the TX buffer directly.

---

xen/arch/arm/tee/ffa.c          |  22 +-----
xen/arch/arm/tee/ffa_partinfo.c |  40 +++++-----
xen/arch/arm/tee/ffa_private.h  |  18 ++---
xen/arch/arm/tee/ffa_rxtx.c     | 132 +++++++++++++++++++++++++-------
xen/arch/arm/tee/ffa_shm.c      |  26 ++++---
5 files changed, 153 insertions(+), 85 deletions(-)
xen/arch/arm/tee/ffa.c +3 -17
index 47f426e85864..4c1b9a4c3b48 100644
@@ -48,8 +48,8 @@
 *     notification for secure partitions
 *   - doesn't support notifications for Xen itself
 *
- * There are some large locked sections with ffa_tx_buffer_lock and
- * ffa_rx_buffer_lock. Especially the ffa_tx_buffer_lock spinlock used
+ * There are some large locked sections with ffa_spmc_tx_lock and
+ * ffa_spmc_rx_lock. Especially the ffa_spmc_tx_lock spinlock used
 * around share_shm() is a very large locked section which can let one VM
 * affect another VM.
 */
@@ -108,20 +108,6 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
    FW_ABI(FFA_RUN),
 };
 
-/*
- * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
- * number of pages used in each of these buffers.
- *
- * The RX buffer is protected from concurrent usage with ffa_rx_buffer_lock.
- * Note that the SPMC is also tracking the ownership of our RX buffer so
- * for calls which uses our RX buffer to deliver a result we must call
- * ffa_rx_release() to let the SPMC know that we're done with the buffer.
- */
-void *ffa_rx __read_mostly;
-void *ffa_tx __read_mostly;
-DEFINE_SPINLOCK(ffa_rx_buffer_lock);
-DEFINE_SPINLOCK(ffa_tx_buffer_lock);
-
 LIST_HEAD(ffa_ctx_head);
 /* RW Lock to protect addition/removal and reading in ffa_ctx_head */
 DEFINE_RWLOCK(ffa_ctx_list_rwlock);
@@ -612,7 +598,7 @@ static bool ffa_probe_fw(void)
                   ffa_fw_abi_needed[i].name);
    }
 
-    if ( !ffa_rxtx_init() )
+    if ( !ffa_rxtx_spmc_init() )
    {
        printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
        goto err_no_fw;
@@ -626,7 +612,7 @@ static bool ffa_probe_fw(void)
    return true;
 

err_rxtx_destroy:

xen/arch/arm/tee/ffa.c +1 -1
-    ffa_rxtx_destroy();
+    ffa_rxtx_spmc_destroy();

err_no_fw:

    ffa_fw_version = 0;
    bitmap_zero(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
xen/arch/arm/tee/ffa_partinfo.c +7 -13
index fa56b1587e3b..766b75dffb8c 100644
@@ -77,28 +77,24 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
 {
    int32_t ret;
    uint32_t src_size, real_sp_count;
-    void *src_buf = ffa_rx;
+    void *src_buf;
    uint32_t count = 0;
 
-    /* Do we have a RX buffer with the SPMC */
-    if ( !ffa_rx )
-        return FFA_RET_DENIED;
-
    /* We need to use the RX buffer to receive the list */
-    spin_lock(&ffa_rx_buffer_lock);
+    src_buf = ffa_rxtx_spmc_rx_acquire();
+    if ( !src_buf )
+        return FFA_RET_DENIED;
 
    ret = ffa_partition_info_get(uuid, 0, &real_sp_count, &src_size);
    if ( ret )
        goto out;
 
-    /* We now own the RX buffer */
-
    /* Validate the src_size we got */
    if ( src_size < sizeof(struct ffa_partition_info_1_0) ||
         src_size >= FFA_PAGE_SIZE )
    {
        ret = FFA_RET_NOT_SUPPORTED;
-        goto out_release;
+        goto out;
    }
 
    /*
@@ -114,7 +110,7 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
    if ( real_sp_count > (FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE) / src_size )
    {
        ret = FFA_RET_NOT_SUPPORTED;
-        goto out_release;
+        goto out;
    }
 
    for ( uint32_t sp_num = 0; sp_num < real_sp_count; sp_num++ )
@@ -127,7 +123,7 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
            if ( dst_buf > (end_buf - dst_size) )
            {
                ret = FFA_RET_NO_MEMORY;
-                goto out_release;
+                goto out;
            }
 
            memcpy(dst_buf, src_buf, MIN(src_size, dst_size));
@@ -143,10 +139,8 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
 
    *sp_count = count;
 
-out_release:
-    ffa_hyp_rx_release();

out:

xen/arch/arm/tee/ffa_partinfo.c +1 -1
-    spin_unlock(&ffa_rx_buffer_lock);
+    ffa_rxtx_spmc_rx_release();
    return ret;

}

xen/arch/arm/tee/ffa_partinfo.c +10 -6
@@ -378,7 +372,7 @@ static void uninit_subscribers(void)
        XFREE(subscr_vm_destroyed);
 }
 
-static bool init_subscribers(uint16_t count, uint32_t fpi_size)
+static bool init_subscribers(void *buf, uint16_t count, uint32_t fpi_size)
 {
    uint16_t n;
    uint16_t c_pos;
@@ -395,7 +389,7 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
    subscr_vm_destroyed_count = 0;
    for ( n = 0; n < count; n++ )
    {
-        fpi = ffa_rx + n * fpi_size;
+        fpi = buf + n * fpi_size;
 
        /*
         * We need to have secure partitions using bit 15 set convention for
@@ -433,7 +427,7 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
 
    for ( c_pos = 0, d_pos = 0, n = 0; n < count; n++ )
    {
-        fpi = ffa_rx + n * fpi_size;
+        fpi = buf + n * fpi_size;
 
        if ( FFA_ID_IS_SECURE(fpi->id) )
        {
@@ -455,10 +449,14 @@ bool ffa_partinfo_init(void)
    uint32_t fpi_size;
    uint32_t count;
    int e;
+    void *spmc_rx;
 
    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
-         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) ||
-         !ffa_rx || !ffa_tx )
+         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32))
+        return false;
+
+    spmc_rx = ffa_rxtx_spmc_rx_acquire();
+    if (!spmc_rx)
        return false;
 
    e = ffa_partition_info_get(NULL, 0, &count, &fpi_size);
@@ -475,10 +473,10 @@ bool ffa_partinfo_init(void)
        goto out;
    }
 
-    ret = init_subscribers(count, fpi_size);
+    ret = init_subscribers(spmc_rx, count, fpi_size);
 

out:

xen/arch/arm/tee/ffa_partinfo.c +1 -1
-    ffa_hyp_rx_release();
+    ffa_rxtx_spmc_rx_release();
    return ret;

}

xen/arch/arm/tee/ffa_private.h +7 -11
index 4272afd37343..cd35c44b8986 100644
@@ -404,10 +404,6 @@ struct ffa_ctx {
    unsigned long *vm_destroy_bitmap;
 };
 
-extern void *ffa_rx;
-extern void *ffa_tx;
-extern spinlock_t ffa_rx_buffer_lock;
-extern spinlock_t ffa_tx_buffer_lock;
 extern DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 extern struct list_head ffa_ctx_head;
@@ -425,8 +421,13 @@ int 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);
 
-bool ffa_rxtx_init(void);
-void ffa_rxtx_destroy(void);
+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);
+void *ffa_rxtx_spmc_tx_acquire(void);
+void ffa_rxtx_spmc_tx_release(void);
+
 int32_t ffa_rxtx_domain_init(struct domain *d);
 void ffa_rxtx_domain_destroy(struct domain *d);
 int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
@@ -556,11 +557,6 @@ static inline int32_t ffa_simple_call(uint32_t fid, register_t a1,
    return ffa_get_ret_code(&resp);
 }
 
-static inline int32_t ffa_hyp_rx_release(void)
-{
-    return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
-}
-
 static inline bool ffa_fw_supports_fid(uint32_t fid)
 {
    BUILD_BUG_ON(FFA_FNUM_MIN_VALUE > FFA_FNUM_MAX_VALUE);
xen/arch/arm/tee/ffa_rxtx.c +20 -5
index cd467d1dba68..07b01430d139 100644
@@ -30,6 +30,20 @@ struct ffa_endpoint_rxtx_descriptor_1_1 {
    uint32_t tx_region_offs;
 };
 
+/*
+ * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
+ * number of pages used in each of these buffers.
+ * Each buffer has its own lock to protect from concurrent usage.
+ *
+ * Note that the SPMC is also tracking the ownership of our RX buffer so
+ * for calls which uses our RX buffer to deliver a result we must do an
+ * FFA_RX_RELEASE to let the SPMC know that we're done with the buffer.
+ */
+static void *ffa_spmc_rx __read_mostly;
+static void *ffa_spmc_tx __read_mostly;
+static DEFINE_SPINLOCK(ffa_spmc_rx_lock);
+static DEFINE_SPINLOCK(ffa_spmc_tx_lock);
+
 static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
                            uint32_t page_count)
 {
@@ -120,8 +134,9 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                     sizeof(struct ffa_address_range) * 2 >
                     FFA_MAX_RXTX_PAGE_COUNT * FFA_PAGE_SIZE);
 
-        spin_lock(&ffa_tx_buffer_lock);
-        rxtx_desc = ffa_tx;
+        rxtx_desc = ffa_rxtx_spmc_tx_acquire();
+        if ( !rxtx_desc )
+            goto err_unmap_rx;
 
        /*
         * We have only one page for each so we pack everything:
@@ -138,7 +153,7 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                                             address_range_array[1]);
 
        /* rx buffer */
-        mem_reg = ffa_tx + sizeof(*rxtx_desc);
+        mem_reg = (void *)rxtx_desc + rxtx_desc->rx_region_offs;
        mem_reg->total_page_count = 1;
        mem_reg->address_range_count = 1;
        mem_reg->reserved = 0;
@@ -148,7 +163,7 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
        mem_reg->address_range_array[0].reserved = 0;
 
        /* tx buffer */
-        mem_reg = ffa_tx + rxtx_desc->tx_region_offs;
+        mem_reg = (void *)rxtx_desc + rxtx_desc->tx_region_offs;
        mem_reg->total_page_count = 1;
        mem_reg->address_range_count = 1;
        mem_reg->reserved = 0;
@@ -159,7 +174,7 @@ int32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 
        ret = ffa_rxtx_map(0, 0, 0);
 
-        spin_unlock(&ffa_tx_buffer_lock);
+        ffa_rxtx_spmc_tx_release();
 
        if ( ret != FFA_RET_OK )
            goto err_unmap_rx;
@@ -291,49 +306,114 @@ void ffa_rxtx_domain_destroy(struct domain *d)
    rxtx_unmap(d);
 }
 
Quoted 6 lines

xen/arch/arm/tee/ffa_rxtx.c void ffa_rxtx_domain_destroy(struct domain *d) · line 311

Quoted 4 lines
xen/arch/arm/tee/ffa_rxtx.c +3 -1
-void ffa_rxtx_destroy(void)
+void *ffa_rxtx_spmc_rx_acquire(void)
+{
+    ASSERT(!spin_is_locked(&ffa_spmc_rx_lock));

Is it invalid for two CPUs to concurrently try to acquire the RX buffer?

No the RX buffer or the TX buffer with the SPMC should only be used by one CPU at a time as there cannot be any concurrent operations using it.

Quoted 23 lines
Quoted 23 lines
xen/arch/arm/tee/ffa_rxtx.c +22 -0
+
+    spin_lock(&ffa_spmc_rx_lock);
+
+    if ( ffa_spmc_rx )
+        return ffa_spmc_rx;
+
+    spin_unlock(&ffa_spmc_rx_lock);
+    return NULL;
+}
+
+void ffa_rxtx_spmc_rx_release(void)
+{
+    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);
+
+    spin_unlock(&ffa_spmc_rx_lock);
Quoted 8 lines

xen/arch/arm/tee/ffa_rxtx.c void ffa_rxtx_domain_destroy(struct domain *d) · line 338

Quoted 6 lines
xen/arch/arm/tee/ffa_rxtx.c +4 -1
+}
+
+void *ffa_rxtx_spmc_tx_acquire(void)
 {
-    bool need_unmap = ffa_tx && ffa_rx;
+    ASSERT(!spin_is_locked(&ffa_spmc_tx_lock));

Is it invalid for two CPUs to concurrently try to acquire the TX buffer?

Same as above.

Quoted 151 lines
Quoted 143 lines
xen/arch/arm/tee/ffa_rxtx.c +57 -19
-    if ( ffa_tx )
+    spin_lock(&ffa_spmc_tx_lock);
+
+    if ( ffa_spmc_tx )
+        return ffa_spmc_tx;
+
+    spin_unlock(&ffa_spmc_tx_lock);
+    return NULL;
+}
+
+void ffa_rxtx_spmc_tx_release(void)
+{
+    ASSERT(spin_is_locked(&ffa_spmc_tx_lock));
+
+    spin_unlock(&ffa_spmc_tx_lock);
+}
+
+void ffa_rxtx_spmc_destroy(void)
+{
+    bool need_unmap;
+
+    spin_lock(&ffa_spmc_rx_lock);
+    spin_lock(&ffa_spmc_tx_lock);
+    need_unmap = ffa_spmc_tx && ffa_spmc_rx;
+
+    if ( ffa_spmc_tx )
    {
-        free_xenheap_pages(ffa_tx, 0);
-        ffa_tx = NULL;
+        free_xenheap_pages(ffa_spmc_tx, 0);
+        ffa_spmc_tx = NULL;
    }
-    if ( ffa_rx )
+    if ( ffa_spmc_rx )
    {
-        free_xenheap_pages(ffa_rx, 0);
-        ffa_rx = NULL;
+        free_xenheap_pages(ffa_spmc_rx, 0);
+        ffa_spmc_rx = NULL;
    }
 
    if ( need_unmap )
        ffa_rxtx_unmap(0);
+
+    spin_unlock(&ffa_spmc_tx_lock);
+    spin_unlock(&ffa_spmc_rx_lock);
 }
 
-bool ffa_rxtx_init(void)
+bool ffa_rxtx_spmc_init(void)
 {
    int32_t e;
+    bool ret = false;
 
    /* Firmware not there or not supporting */
    if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
        return false;
 
-    ffa_rx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
-    if ( !ffa_rx )
-        return false;
+    spin_lock(&ffa_spmc_rx_lock);
+    spin_lock(&ffa_spmc_tx_lock);
+
+    ffa_spmc_rx = alloc_xenheap_pages(
+                            get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
+    if ( !ffa_spmc_rx )
+        goto exit;
 
-    ffa_tx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
-    if ( !ffa_tx )
-        goto err;
+    ffa_spmc_tx = alloc_xenheap_pages(
+                            get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
+    if ( !ffa_spmc_tx )
+        goto exit;
 
-    e = ffa_rxtx_map(__pa(ffa_tx), __pa(ffa_rx), FFA_RXTX_PAGE_COUNT);
+    e = ffa_rxtx_map(__pa(ffa_spmc_tx), __pa(ffa_spmc_rx),
+                     FFA_RXTX_PAGE_COUNT);
    if ( e )
-        goto err;
+        goto exit;
 
-    return true;
+    ret = true;
 
-err:
-    ffa_rxtx_destroy();
+exit:
+    spin_unlock(&ffa_spmc_tx_lock);
+    spin_unlock(&ffa_spmc_rx_lock);
 
-    return false;
+    if ( !ret )
+        ffa_rxtx_spmc_destroy();
+
+    return ret;
 }
xen/arch/arm/tee/ffa_shm.c +9 -5
index d628c1b70609..b9022797c3bf 100644
@@ -286,9 +286,8 @@ static void init_range(struct ffa_address_range *addr_range,
 }
 
 /*
- * This function uses the ffa_tx buffer to transmit the memory transaction
- * descriptor. The function depends ffa_tx_buffer_lock to be used to guard
- * the buffer from concurrent use.
+ * This function uses the ffa_spmc tx buffer to transmit the memory transaction
+ * descriptor.
 */
 static int share_shm(struct ffa_shm_mem *shm)
 {
@@ -298,17 +297,22 @@ static int share_shm(struct ffa_shm_mem *shm)
    struct ffa_address_range *addr_range;
    struct ffa_mem_region *region_descr;
    const unsigned int region_count = 1;
-    void *buf = ffa_tx;
    uint32_t frag_len;
    uint32_t tot_len;
    paddr_t last_pa;
    unsigned int n;
    paddr_t pa;
+    int32_t ret;
+    void *buf;
 
-    ASSERT(spin_is_locked(&ffa_tx_buffer_lock));
    ASSERT(shm->page_count);
 
+    buf = ffa_rxtx_spmc_tx_acquire();
+    if ( !buf )
+        return FFA_RET_NOT_SUPPORTED;
+
    descr = buf;
+
    memset(descr, 0, sizeof(*descr));
    descr->sender_id = shm->sender_id;
    descr->handle = shm->handle;

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

Quoted 6 lines
xen/arch/arm/tee/ffa_shm.c +2 -0
@@ -340,7 +344,10 @@ static int share_shm(struct ffa_shm_mem *shm)
    tot_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count,
                                region_descr->address_range_count);
    if ( tot_len > max_frag_len )
+    {
+        ffa_rxtx_spmc_tx_release();
        return FFA_RET_NOT_SUPPORTED;

I'd prefer adding an out label below before the
ffa_rxtx_spmc_tx_release() call and:

Quoted 2 lines
    ret = FFA_RET_NOT_SUPPORTED;
    goto out;

Ack I will fix that.

Cheers
Bertrand

Quoted 34 lines

Cheers,
Jens

Quoted 30 lines
xen/arch/arm/tee/ffa_shm.c +6 -5
+    }
 
    addr_range = region_descr->address_range_array;
    frag_len = ADDR_RANGE_OFFSET(descr->mem_access_count, region_count, 1);
@@ -360,7 +367,11 @@ static int share_shm(struct ffa_shm_mem *shm)
        init_range(addr_range, pa);
    }
 
-    return ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);
+    ret = ffa_mem_share(tot_len, frag_len, 0, 0, &shm->handle);
+
+    ffa_rxtx_spmc_tx_release();
+
+    return ret;
 }
 
 static int read_mem_transaction(uint32_t ffa_vers, const void *buf, size_t blen,
@@ -578,10 +589,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    if ( ret )
        goto out;
 
-    /* Note that share_shm() uses our tx buffer */
-    spin_lock(&ffa_tx_buffer_lock);
    ret = share_shm(shm);
-    spin_unlock(&ffa_tx_buffer_lock);
    if ( ret )
        goto out;
 
--

2.51.2

#14 lore raw

Re: [PATCH 04/10] xen/arm: ffa: rework SPMC RX/TX buffer management

Hi Bertrand,

Quoted 32 lines · Bertrand Marquis

Hi Jens,

Quoted 29 lines · Jens Wiklander

Hi Bertrand,

Quoted 23 lines · Bertrand Marquis

Rework how Xen accesses the RX/TX buffers shared with the SPMC so that ownership and locking are handled centrally.

Move the SPMC RX/TX buffer bases into ffa_rxtx.c as ffa_spmc_rx/ffa_spmc_tx, protect them with dedicated ffa_spmc_{rx,tx}_lock spinlocks and expose ffa_rxtx_spmc_{rx,tx}_{acquire,release}() helpers instead of the global ffa_rx/ffa_tx pointers and ffa_{rx,tx}_buffer_lock.

The RX helpers now always issue FFA_RX_RELEASE when we are done consuming data from the SPMC, so partition-info enumeration and shared memory paths release the RX buffer on all exit paths. The RX/TX mapping code is updated to use the descriptor offsets (rx_region_offs and tx_region_offs) rather than hard-coded structure layout, and to use the TX acquire/release helpers instead of touching the TX buffer directly.

---

xen/arch/arm/tee/ffa.c          |  22 +-----
xen/arch/arm/tee/ffa_partinfo.c |  40 +++++-----
xen/arch/arm/tee/ffa_private.h  |  18 ++---
xen/arch/arm/tee/ffa_rxtx.c     | 132 +++++++++++++++++++++++++-------
xen/arch/arm/tee/ffa_shm.c      |  26 ++++---

[snip]

Quoted 10 lines
Quoted 7 lines

xen/arch/arm/tee/ffa_rxtx.c void ffa_rxtx_domain_destroy(struct domain *d) · line 311

Quoted 5 lines
xen/arch/arm/tee/ffa_rxtx.c +3 -1
-void ffa_rxtx_destroy(void)
+void *ffa_rxtx_spmc_rx_acquire(void)
+{
+    ASSERT(!spin_is_locked(&ffa_spmc_rx_lock));

Is it invalid for two CPUs to concurrently try to acquire the RX buffer?

No the RX buffer or the TX buffer with the SPMC should only be used by one CPU at a time as there cannot be any concurrent operations using it.

What if two guests call FFA_PARTITION_INFO_GET concurrently? Both can succeed in acquiring their RX buffer so they can call ffa_get_sp_partinfo() concurrently, and the assert might be triggered. We have a similar problem with FFA_RXTX_MAP_64 and ffa_rxtx_spmc_tx_acquire(). Contention on the spinlocks for the rx and tx buffers should be valid. If we can't allow a guest to block here, we should return an error and let the guest retry.

Cheers,
Jens

#15 lore raw

Re: [PATCH 04/10] xen/arm: ffa: rework SPMC RX/TX buffer management

Hi Jens,

Quoted 52 lines · Jens Wiklander

Hi Bertrand,

Quoted 32 lines · Bertrand Marquis

Hi Jens,

Quoted 29 lines · Jens Wiklander

Hi Bertrand,

Quoted 23 lines · Bertrand Marquis

Rework how Xen accesses the RX/TX buffers shared with the SPMC so that ownership and locking are handled centrally.

Move the SPMC RX/TX buffer bases into ffa_rxtx.c as ffa_spmc_rx/ffa_spmc_tx, protect them with dedicated ffa_spmc_{rx,tx}_lock spinlocks and expose ffa_rxtx_spmc_{rx,tx}_{acquire,release}() helpers instead of the global ffa_rx/ffa_tx pointers and ffa_{rx,tx}_buffer_lock.

The RX helpers now always issue FFA_RX_RELEASE when we are done consuming data from the SPMC, so partition-info enumeration and shared memory paths release the RX buffer on all exit paths. The RX/TX mapping code is updated to use the descriptor offsets (rx_region_offs and tx_region_offs) rather than hard-coded structure layout, and to use the TX acquire/release helpers instead of touching the TX buffer directly.

---

xen/arch/arm/tee/ffa.c          |  22 +-----
xen/arch/arm/tee/ffa_partinfo.c |  40 +++++-----
xen/arch/arm/tee/ffa_private.h  |  18 ++---
xen/arch/arm/tee/ffa_rxtx.c     | 132 +++++++++++++++++++++++++-------
xen/arch/arm/tee/ffa_shm.c      |  26 ++++---

[snip]

Quoted 10 lines
Quoted 7 lines

xen/arch/arm/tee/ffa_rxtx.c void ffa_rxtx_domain_destroy(struct domain *d) · line 311

Quoted 5 lines
xen/arch/arm/tee/ffa_rxtx.c +3 -1
-void ffa_rxtx_destroy(void)
+void *ffa_rxtx_spmc_rx_acquire(void)
+{
+    ASSERT(!spin_is_locked(&ffa_spmc_rx_lock));

Is it invalid for two CPUs to concurrently try to acquire the RX buffer?

No the RX buffer or the TX buffer with the SPMC should only be used by one CPU at a time as there cannot be any concurrent operations using it.

Quoted 7 lines

What if two guests call FFA_PARTITION_INFO_GET concurrently? Both can succeed in acquiring their RX buffer so they can call ffa_get_sp_partinfo() concurrently, and the assert might be triggered. We have a similar problem with FFA_RXTX_MAP_64 and ffa_rxtx_spmc_tx_acquire(). Contention on the spinlocks for the rx and tx buffers should be valid. If we can't allow a guest to block here, we should return an error and let the guest retry.

i am not sure i am following anymore.
The assert is just there to ensure that the lock is not already taken. The function is then taking the lock and not releasing it until release is called which is ensuring that only one vcpu at a time is using the rx buffer. Did i miss something here ?

for rxtx map we do call tx_acquire so we lock the buffer.

Now we might have a race condition between in rxtx_map and unmap where i should take the rx_lock and the tx_lock of the guest to prevent concurrent usage of the vm rxtx buffer. I will fix that one.

Please tell me for the spmc rxtx buffers as i am not sure i am following what you mean there :-)

Cheers
Bertrand

#16 lore raw

Re: [PATCH 04/10] xen/arm: ffa: rework SPMC RX/TX buffer management

Hi,

Quoted 63 lines · Bertrand Marquis

Hi Jens,

Quoted 52 lines · Jens Wiklander

Hi Bertrand,

Quoted 32 lines · Bertrand Marquis

Hi Jens,

Quoted 29 lines · Jens Wiklander

Hi Bertrand,

Quoted 23 lines · Bertrand Marquis

Rework how Xen accesses the RX/TX buffers shared with the SPMC so that ownership and locking are handled centrally.

Move the SPMC RX/TX buffer bases into ffa_rxtx.c as ffa_spmc_rx/ffa_spmc_tx, protect them with dedicated ffa_spmc_{rx,tx}_lock spinlocks and expose ffa_rxtx_spmc_{rx,tx}_{acquire,release}() helpers instead of the global ffa_rx/ffa_tx pointers and ffa_{rx,tx}_buffer_lock.

The RX helpers now always issue FFA_RX_RELEASE when we are done consuming data from the SPMC, so partition-info enumeration and shared memory paths release the RX buffer on all exit paths. The RX/TX mapping code is updated to use the descriptor offsets (rx_region_offs and tx_region_offs) rather than hard-coded structure layout, and to use the TX acquire/release helpers instead of touching the TX buffer directly.

---

xen/arch/arm/tee/ffa.c          |  22 +-----
xen/arch/arm/tee/ffa_partinfo.c |  40 +++++-----
xen/arch/arm/tee/ffa_private.h  |  18 ++---
xen/arch/arm/tee/ffa_rxtx.c     | 132 +++++++++++++++++++++++++-------
xen/arch/arm/tee/ffa_shm.c      |  26 ++++---

[snip]

Quoted 10 lines
Quoted 7 lines

xen/arch/arm/tee/ffa_rxtx.c void ffa_rxtx_domain_destroy(struct domain *d) · line 311

Quoted 5 lines
xen/arch/arm/tee/ffa_rxtx.c +3 -1
-void ffa_rxtx_destroy(void)
+void *ffa_rxtx_spmc_rx_acquire(void)
+{
+    ASSERT(!spin_is_locked(&ffa_spmc_rx_lock));

Is it invalid for two CPUs to concurrently try to acquire the RX buffer?

No the RX buffer or the TX buffer with the SPMC should only be used by one CPU at a time as there cannot be any concurrent operations using it.

Quoted 7 lines

What if two guests call FFA_PARTITION_INFO_GET concurrently? Both can succeed in acquiring their RX buffer so they can call ffa_get_sp_partinfo() concurrently, and the assert might be triggered. We have a similar problem with FFA_RXTX_MAP_64 and ffa_rxtx_spmc_tx_acquire(). Contention on the spinlocks for the rx and tx buffers should be valid. If we can't allow a guest to block here, we should return an error and let the guest retry.

Quoted 2 lines

i am not sure i am following anymore.
The assert is just there to ensure that the lock is not already taken.

But it could already be taken by another CPU.

Quoted 3 lines

The function is then taking the lock and not releasing it until release is called which is ensuring that only one vcpu at a time is using the rx buffer. Did i miss something here ?

Only one CPU at a time should use the spmc rx buffer, but others might try and should be blocked in spin_lock() rather than ASSERT.

Quoted 6 lines

for rxtx map we do call tx_acquire so we lock the buffer.

Now we might have a race condition between in rxtx_map and unmap where i should take the rx_lock and the tx_lock of the guest to prevent concurrent usage of the vm rxtx buffer. I will fix that one.

Yes, you're right, good catch.

Quoted 3 lines

Please tell me for the spmc rxtx buffers as i am not sure i am following what you mean there :-)

Each guest has its own rxtx buffer, so the spinlock here is just in case the guest didn't synchronize its CPUs before calling. But the SPMC rxtx buffers are for Xen, so a guest can't be sure that no other guest is holding the spinlock.

Two guests can independently call FFA_RXTX_MAP_64 and then call ffa_rxtx_spmc_tx_acquire() more or less at the same time.

I you remove the "ASSERT(!spin_is_locked(...));" from ffa_rxtx_spmc_tx_acquire() and ffa_rxtx_spmc_rx_acquire() it should be OK.

Cheers,
Jens

#17 lore raw

Re: [PATCH 04/10] xen/arm: ffa: rework SPMC RX/TX buffer management

Hi Jens,

Quoted 91 lines · Jens Wiklander

Hi,

Quoted 63 lines · Bertrand Marquis

Hi Jens,

Quoted 52 lines · Jens Wiklander

Hi Bertrand,

Quoted 32 lines · Bertrand Marquis

Hi Jens,

Quoted 29 lines · Jens Wiklander

Hi Bertrand,

Quoted 23 lines · Bertrand Marquis

Rework how Xen accesses the RX/TX buffers shared with the SPMC so that ownership and locking are handled centrally.

Move the SPMC RX/TX buffer bases into ffa_rxtx.c as ffa_spmc_rx/ffa_spmc_tx, protect them with dedicated ffa_spmc_{rx,tx}_lock spinlocks and expose ffa_rxtx_spmc_{rx,tx}_{acquire,release}() helpers instead of the global ffa_rx/ffa_tx pointers and ffa_{rx,tx}_buffer_lock.

The RX helpers now always issue FFA_RX_RELEASE when we are done consuming data from the SPMC, so partition-info enumeration and shared memory paths release the RX buffer on all exit paths. The RX/TX mapping code is updated to use the descriptor offsets (rx_region_offs and tx_region_offs) rather than hard-coded structure layout, and to use the TX acquire/release helpers instead of touching the TX buffer directly.

---

xen/arch/arm/tee/ffa.c          |  22 +-----
xen/arch/arm/tee/ffa_partinfo.c |  40 +++++-----
xen/arch/arm/tee/ffa_private.h  |  18 ++---
xen/arch/arm/tee/ffa_rxtx.c     | 132 +++++++++++++++++++++++++-------
xen/arch/arm/tee/ffa_shm.c      |  26 ++++---

[snip]

Quoted 10 lines
Quoted 7 lines

xen/arch/arm/tee/ffa_rxtx.c void ffa_rxtx_domain_destroy(struct domain *d) · line 311

Quoted 5 lines
xen/arch/arm/tee/ffa_rxtx.c +3 -1
-void ffa_rxtx_destroy(void)
+void *ffa_rxtx_spmc_rx_acquire(void)
+{
+    ASSERT(!spin_is_locked(&ffa_spmc_rx_lock));

Is it invalid for two CPUs to concurrently try to acquire the RX buffer?

No the RX buffer or the TX buffer with the SPMC should only be used by one CPU at a time as there cannot be any concurrent operations using it.

Quoted 7 lines

What if two guests call FFA_PARTITION_INFO_GET concurrently? Both can succeed in acquiring their RX buffer so they can call ffa_get_sp_partinfo() concurrently, and the assert might be triggered. We have a similar problem with FFA_RXTX_MAP_64 and ffa_rxtx_spmc_tx_acquire(). Contention on the spinlocks for the rx and tx buffers should be valid. If we can't allow a guest to block here, we should return an error and let the guest retry.

Quoted 2 lines

i am not sure i am following anymore.
The assert is just there to ensure that the lock is not already taken.

But it could already be taken by another CPU.

Quoted 3 lines

The function is then taking the lock and not releasing it until release is called which is ensuring that only one vcpu at a time is using the rx buffer. Did i miss something here ?

Only one CPU at a time should use the spmc rx buffer, but others might try and should be blocked in spin_lock() rather than ASSERT.

Quoted 6 lines

for rxtx map we do call tx_acquire so we lock the buffer.

Now we might have a race condition between in rxtx_map and unmap where i should take the rx_lock and the tx_lock of the guest to prevent concurrent usage of the vm rxtx buffer. I will fix that one.

Yes, you're right, good catch.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Please tell me for the spmc rxtx buffers as i am not sure i am following what you mean there :-)

Each guest has its own rxtx buffer, so the spinlock here is just in case the guest didn't synchronize its CPUs before calling. But the SPMC rxtx buffers are for Xen, so a guest can't be sure that no other guest is holding the spinlock.

In fact i just saw why this is wrong. I must not unlock at the end of spmc_rx/tx_acquire so that only one at a time is taking the rxtx buffers with the spmc and they become available only when current user is done.

Quoted 7 lines

Two guests can independently call FFA_RXTX_MAP_64 and then call ffa_rxtx_spmc_tx_acquire() more or less at the same time.

I you remove the "ASSERT(!spin_is_locked(...));" from ffa_rxtx_spmc_tx_acquire() and ffa_rxtx_spmc_rx_acquire() it should be OK.

My current conclusion was that i need to remove the unlock in acquire so that i spin_lock in acquire and spin_unlock in release.

In fact to prevent issues with rxtx vm buffers, I will add tx_acquire and tx_release functions for the vm tx buffer and give the buffer address and size as return to the caller.
This will prevent any access to tx or page_count outside of a locked section and remove potential race conditions.

I will rework this patch and the next one to end up with a cleaner handling of vm and spmc rxtx buffers.

Cheers
Bertrand

Quoted 3 lines

Cheers,
Jens

#18 lore raw

PATCH 5/10 xen/arm: ffa: use signed 32-bit status codes

In reply to Bertrand Marquis

The FF-A spec defines return status values as signed 32-bit integers. Align the Xen mediator with this requirement by:

- switching the FF-A helpers (ffa_handle_mem_reclaim(), partition info init, notification ops, dispatcher glue, etc.) to int32_t return types so callers cannot silently truncate negative values
- masking SMCCC responses/exits in ffa_get_version(), ffa_get_ret_code() and ffa_set_regs_error() to 32 bits before storing them in guest registers
- updating notifier, shared-memory reclaim, partition-info and dispatcher call sites to use the new prototypes so the entire FF-A path propagates spec-compliant 32-bit signed error codes

While there, tidy up the FF-A notification helpers by using GENMASK(15, 0) for endpoint extraction and fix the secure-endpoint check in ffa_handle_notification_set() to apply the mask to the endpoint ID before calling FFA_ID_IS_SECURE(), instead of testing the wrong halfword of src_dst.

Fixes 911b305e7bdab ("xen/arm: ffa: Enable VM to VM without firmware")
---
 xen/arch/arm/tee/ffa.c          | 12 +++++++-----
 xen/arch/arm/tee/ffa_notif.c    | 14 +++++++-------
 xen/arch/arm/tee/ffa_partinfo.c |  4 ++--
 xen/arch/arm/tee/ffa_private.h  | 21 +++++++++++----------
 xen/arch/arm/tee/ffa_shm.c      |  4 ++--
 5 files changed, 29 insertions(+), 26 deletions(-)
xen/arch/arm/tee/ffa.c +7 -5
index 4c1b9a4c3b48..7392bb6c3db9 100644
@@ -129,12 +129,14 @@ static bool ffa_get_version(uint32_t *vers)
         .a1 = FFA_MY_VERSION,
     };
     struct arm_smccc_1_2_regs resp;
+    int32_t ret;
 
     arm_smccc_1_2_smc(&arg, &resp);
-    if ( resp.a0 == FFA_RET_NOT_SUPPORTED )
+    ret = resp.a0 & GENMASK_ULL(31, 0);
+    if ( ret == FFA_RET_NOT_SUPPORTED )
         return false;
 
-    *vers = resp.a0;
+    *vers = resp.a0 & GENMASK_ULL(31, 0);
 
     return true;
 }
@@ -304,7 +306,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     uint32_t fid = get_user_reg(regs, 0);
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
-    int e;
+    int32_t e;
 
     if ( !ctx )
         return false;
@@ -376,8 +378,8 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
 
     default:
         gprintk(XENLOG_ERR, "ffa: unhandled fid 0x%x\n", fid);
-        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
-        return true;
+        e = FFA_RET_NOT_SUPPORTED;
+        break;
     }
 
     if ( e )
xen/arch/arm/tee/ffa_notif.c +7 -7
index 86bef6b3b2ab..37b05747cd21 100644
@@ -19,7 +19,7 @@
 static bool __ro_after_init fw_notif_enabled;
 static unsigned int __ro_after_init notif_sri_irq;
 
-int ffa_handle_notification_bind(struct cpu_user_regs *regs)
+int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
     uint32_t src_dst = get_user_reg(regs, 1);
@@ -27,7 +27,7 @@ int ffa_handle_notification_bind(struct cpu_user_regs *regs)
     uint32_t bitmap_lo = get_user_reg(regs, 3);
     uint32_t bitmap_hi = get_user_reg(regs, 4);
 
-    if ( (src_dst & 0xFFFFU) != ffa_get_vm_id(d) )
+    if ( (src_dst & GENMASK(15, 0)) != ffa_get_vm_id(d) )
         return FFA_RET_INVALID_PARAMETERS;
 
     if ( flags )    /* Only global notifications are supported */
@@ -40,14 +40,14 @@ int ffa_handle_notification_bind(struct cpu_user_regs *regs)
     return FFA_RET_NOT_SUPPORTED;
 }
 
-int ffa_handle_notification_unbind(struct cpu_user_regs *regs)
+int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
     uint32_t src_dst = get_user_reg(regs, 1);
     uint32_t bitmap_lo = get_user_reg(regs, 3);
     uint32_t bitmap_hi = get_user_reg(regs, 4);
 
-    if ( (src_dst & 0xFFFFU) != ffa_get_vm_id(d) )
+    if ( (src_dst & GENMASK(15, 0)) != ffa_get_vm_id(d) )
         return FFA_RET_INVALID_PARAMETERS;
 
     if ( FFA_ID_IS_SECURE(src_dst >> 16) && fw_notif_enabled )
@@ -106,7 +106,7 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
         return;
     }
 
-    if ( (recv & 0xFFFFU) != ffa_get_vm_id(d) )
+    if ( (recv & GENMASK(15, 0)) != ffa_get_vm_id(d) )
     {
         ffa_set_regs_error(regs, FFA_RET_INVALID_PARAMETERS);
         return;
@@ -162,7 +162,7 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs)
     ffa_set_regs(regs, FFA_SUCCESS_32, 0, w2, w3, w4, w5, w6, w7);
 }
 
-int ffa_handle_notification_set(struct cpu_user_regs *regs)
+int32_t ffa_handle_notification_set(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
     uint32_t src_dst = get_user_reg(regs, 1);
@@ -173,7 +173,7 @@ int ffa_handle_notification_set(struct cpu_user_regs *regs)
     if ( (src_dst >> 16) != ffa_get_vm_id(d) )
         return FFA_RET_INVALID_PARAMETERS;
 
-    if ( FFA_ID_IS_SECURE(src_dst >> 16) && fw_notif_enabled )
+    if ( FFA_ID_IS_SECURE(src_dst & GENMASK(15, 0)) && fw_notif_enabled )
         return ffa_simple_call(FFA_NOTIFICATION_SET, src_dst, flags, bitmap_lo,
                                bitmap_hi);
 
xen/arch/arm/tee/ffa_partinfo.c +2 -2
index 766b75dffb8c..3f4a779f4146 100644
@@ -448,7 +448,7 @@ bool ffa_partinfo_init(void)
     bool ret = false;
     uint32_t fpi_size;
     uint32_t count;
-    int e;
+    int32_t e;
     void *spmc_rx;
 
     if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
@@ -515,7 +515,7 @@ static void vm_destroy_bitmap_init(struct ffa_ctx *ctx,
     }
 }
 
-int ffa_partinfo_domain_init(struct domain *d)
+int32_t ffa_partinfo_domain_init(struct domain *d)
 {
     unsigned int count = BITS_TO_LONGS(subscr_vm_destroyed_count);
     struct ffa_ctx *ctx = d->arch.tee;
xen/arch/arm/tee/ffa_private.h +11 -10
index cd35c44b8986..9cae238f972c 100644
@@ -31,9 +31,9 @@
 
 /* FFA_VERSION helpers */
 #define FFA_VERSION_MAJOR_SHIFT         16U
-#define FFA_VERSION_MAJOR_MASK          0x7FFFU
+#define FFA_VERSION_MAJOR_MASK          GENMASK(14, 0)
 #define FFA_VERSION_MINOR_SHIFT         0U
-#define FFA_VERSION_MINOR_MASK          0xFFFFU
+#define FFA_VERSION_MINOR_MASK          GENMASK(15, 0)
 #define MAKE_FFA_VERSION(major, minor)  \
         ((((major) & FFA_VERSION_MAJOR_MASK) << FFA_VERSION_MAJOR_SHIFT) | \
          ((minor) & FFA_VERSION_MINOR_MASK))
@@ -414,10 +414,10 @@ extern atomic_t ffa_vm_count;
 
 bool ffa_shm_domain_destroy(struct domain *d);
 void ffa_handle_mem_share(struct cpu_user_regs *regs);
-int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags);
+int32_t ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags);
 
 bool ffa_partinfo_init(void);
-int ffa_partinfo_domain_init(struct domain *d);
+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);
 
@@ -441,11 +441,11 @@ void ffa_notif_init_interrupt(void);
 int ffa_notif_domain_init(struct domain *d);
 void ffa_notif_domain_destroy(struct domain *d);
 
-int ffa_handle_notification_bind(struct cpu_user_regs *regs);
-int ffa_handle_notification_unbind(struct cpu_user_regs *regs);
+int32_t ffa_handle_notification_bind(struct cpu_user_regs *regs);
+int32_t ffa_handle_notification_unbind(struct cpu_user_regs *regs);
 void ffa_handle_notification_info_get(struct cpu_user_regs *regs);
 void ffa_handle_notification_get(struct cpu_user_regs *regs);
-int ffa_handle_notification_set(struct cpu_user_regs *regs);
+int32_t ffa_handle_notification_set(struct cpu_user_regs *regs);
 
 #ifdef CONFIG_FFA_VM_TO_VM
 void ffa_raise_rx_buffer_full(struct domain *d);
@@ -511,9 +511,10 @@ static inline void ffa_set_regs(struct cpu_user_regs *regs, register_t v0,
 }
 
 static inline void ffa_set_regs_error(struct cpu_user_regs *regs,
-                                      uint32_t error_code)
+                                      int32_t error_code)
 {
-    ffa_set_regs(regs, FFA_ERROR, 0, error_code, 0, 0, 0, 0, 0);
+    ffa_set_regs(regs, FFA_ERROR, 0, error_code & GENMASK_ULL(31, 0), 0, 0, 0,
+                 0, 0);
 }
 
 static inline void ffa_set_regs_success(struct cpu_user_regs *regs,
@@ -528,7 +529,7 @@ static inline int32_t ffa_get_ret_code(const struct arm_smccc_1_2_regs *resp)
     {
     case FFA_ERROR:
         if ( resp->a2 )
-            return resp->a2;
+            return resp->a2 & GENMASK_ULL(31, 0);
         else
             return FFA_RET_NOT_SUPPORTED;
     case FFA_SUCCESS_32:
xen/arch/arm/tee/ffa_shm.c +2 -2
index b9022797c3bf..9db72269d1ea 100644
@@ -624,14 +624,14 @@ static struct ffa_shm_mem *find_shm_mem(struct ffa_ctx *ctx, uint64_t handle)
     return NULL;
 }
 
-int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
+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;
-    int ret;
+    int32_t ret;
 
     if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
         return FFA_RET_NOT_SUPPORTED;
Signature · 1 line
2.51.2
Reviewed-by on v1 5/10
#19 lore raw

Re: [PATCH 05/10] xen/arm: ffa: use signed 32-bit status codes

Hi Bertrand,

Quoted 22 lines · Bertrand Marquis

The FF-A spec defines return status values as signed 32-bit integers. Align the Xen mediator with this requirement by:

- switching the FF-A helpers (ffa_handle_mem_reclaim(), partition info init, notification ops, dispatcher glue, etc.) to int32_t return types so callers cannot silently truncate negative values
- masking SMCCC responses/exits in ffa_get_version(), ffa_get_ret_code() and ffa_set_regs_error() to 32 bits before storing them in guest registers
- updating notifier, shared-memory reclaim, partition-info and dispatcher call sites to use the new prototypes so the entire FF-A path propagates spec-compliant 32-bit signed error codes

While there, tidy up the FF-A notification helpers by using GENMASK(15, 0) for endpoint extraction and fix the secure-endpoint check in ffa_handle_notification_set() to apply the mask to the endpoint ID before calling FFA_ID_IS_SECURE(), instead of testing the wrong halfword of src_dst.

Fixes 911b305e7bdab ("xen/arm: ffa: Enable VM to VM without firmware")
Quoted 7 lines
---
 xen/arch/arm/tee/ffa.c          | 12 +++++++-----
 xen/arch/arm/tee/ffa_notif.c    | 14 +++++++-------
 xen/arch/arm/tee/ffa_partinfo.c |  4 ++--
 xen/arch/arm/tee/ffa_private.h  | 21 +++++++++++----------
 xen/arch/arm/tee/ffa_shm.c      |  4 ++--
 5 files changed, 29 insertions(+), 26 deletions(-)

Looks good.

Cheers,
Jens

#20 lore raw

PATCH 6/10 xen/arm: ffa: add UUID helpers for partition info

In reply to Bertrand Marquis

Introduce struct ffa_uuid together with nil/equality/set helpers, and use it end-to-end in the partition-info plumbing.

The SP and VM enumeration paths now build UUIDs from the guest registers, call a new ffa_copy_info() helper and ensure non-nil UUID queries only return matching SP entries, relying on firmware UUID filtering. VM entries are skipped because we do not track per-VM UUIDs.

Count requests and subscriber initialisation are updated accordingly so firmware is always called with an explicit UUID. This keeps count and listing requests aligned with the FF-A v1.1 rules while preserving the Linux compatibility workaround for v1.2 requesters.

---
 xen/arch/arm/tee/ffa_partinfo.c | 206 ++++++++++++++++++++------------
 xen/arch/arm/tee/ffa_private.h  |  21 ++++
 2 files changed, 152 insertions(+), 75 deletions(-)
xen/arch/arm/tee/ffa_partinfo.c +131 -75
index 3f4a779f4146..4adbe2736c94 100644
@@ -33,7 +33,7 @@ static uint16_t subscr_vm_created_count __read_mostly;
 static uint16_t *subscr_vm_destroyed __read_mostly;
 static uint16_t subscr_vm_destroyed_count __read_mostly;
 
-static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
+static int32_t ffa_partition_info_get(struct ffa_uuid uuid, uint32_t flags,
                                       uint32_t *count, uint32_t *fpi_size)
 {
     struct arm_smccc_1_2_regs arg = {
@@ -41,15 +41,12 @@ static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
         .a5 = flags,
     };
     struct arm_smccc_1_2_regs resp;
-    uint32_t ret;
+    int32_t ret;
 
-    if ( uuid )
-    {
-        arg.a1 = uuid[0];
-        arg.a2 = uuid[1];
-        arg.a3 = uuid[2];
-        arg.a4 = uuid[3];
-    }
+    arg.a1 = uuid.val[0] & 0xFFFFFFFFU;
+    arg.a2 = (uuid.val[0] >> 32) & 0xFFFFFFFFU;
+    arg.a3 = uuid.val[1] & 0xFFFFFFFFU;
+    arg.a4 = (uuid.val[1] >> 32) & 0xFFFFFFFFU;
 
     arm_smccc_1_2_smc(&arg, &resp);
 
@@ -63,7 +60,26 @@ static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
     return ret;
 }
 
-static int32_t ffa_get_sp_count(uint32_t *uuid, uint32_t *sp_count)
+static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src,
+                             uint32_t dst_size, uint32_t src_size)
+{
+    uint8_t *pos = *dst;
+    uint8_t *end = dst_end;
+
+    if ( pos > end - dst_size )
+        return FFA_RET_NO_MEMORY;
+
+    memcpy(pos, src, MIN(dst_size, src_size));
+
+    if ( dst_size > src_size )
+        memset(pos + src_size, 0, dst_size - src_size);
+
+    *dst = pos + dst_size;
+
+    return FFA_RET_OK;
+}
+
+static int32_t ffa_get_sp_count(struct ffa_uuid uuid, uint32_t *sp_count)
 {
     uint32_t src_size;
 
@@ -71,8 +87,8 @@ static int32_t ffa_get_sp_count(uint32_t *uuid, uint32_t *sp_count)
                                   sp_count, &src_size);
 }
 
-static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
-                                   void *dst_buf, void *end_buf,
+static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
+                                   void **dst_buf, void *end_buf,
                                    uint32_t dst_size)
 {
     int32_t ret;
@@ -120,17 +136,18 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
         /* filter out SP not following bit 15 convention if any */
         if ( FFA_ID_IS_SECURE(fpi->id) )
         {
-            if ( dst_buf > (end_buf - dst_size) )
-            {
-                ret = FFA_RET_NO_MEMORY;
-                goto out;
-            }
+            /*
+             * If VM is 1.0 but firmware is 1.1 we could have several entries
+             * with the same ID but different UUIDs. In this case the VM will
+             * get a list with several time the same ID.
+             * This is a non-compliance to the specification but 1.0 VMs should
+             * handle that on their own to simplify Xen implementation.
+             */
 
-            memcpy(dst_buf, src_buf, MIN(src_size, dst_size));
-            if ( dst_size > src_size )
-                memset(dst_buf + src_size, 0, dst_size - src_size);
+            ret = ffa_copy_info(dst_buf, end_buf, src_buf, dst_size, src_size);
+            if ( ret )
+                goto out;
 
-            dst_buf += dst_size;
             count++;
         }
 
@@ -144,69 +161,89 @@ out:
     return ret;
 }
 
-static int32_t ffa_get_vm_partinfo(uint32_t *vm_count, void *dst_buf,
-                                   void *end_buf, uint32_t dst_size)
+static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t *vm_count,
+                                   void **dst_buf, void *end_buf,
+                                   uint32_t dst_size)
 {
     struct ffa_ctx *curr_ctx = current->domain->arch.tee;
     struct ffa_ctx *dest_ctx;
     uint32_t count = 0;
     int32_t ret = FFA_RET_OK;
+    /*
+     * We do not have UUID info for VMs so use the 1.0 structure so that we set
+     * UUIDs to zero using memset
+     */
+    struct ffa_partition_info_1_0 info;
 
     /*
-     * There could potentially be a lot of VMs in the system and we could
-     * hold the CPU for long here.
-     * Right now there is no solution in FF-A specification to split
-     * the work in this case.
-     * TODO: Check how we could delay the work or have preemption checks.
+     * We do not have protocol UUIDs for VMs so if a request has non Nil UUID
+     * we must return an empty list.
      */
-    read_lock(&ffa_ctx_list_rwlock);
-    list_for_each_entry(dest_ctx, &ffa_ctx_head, ctx_list)
+    if ( !ffa_uuid_is_nil(uuid) )
+    {
+        *vm_count = 0;
+        return FFA_RET_OK;
+    }
+
+    /*
+     * Workaround for Linux FF-A Driver not accepting to have its own
+     * entry in the list before FF-A v1.2 was supported.
+     * This workaround is generally acceptable for other implementations
+     * as the specification was not completely clear on wether or not
+     * the requester endpoint information should be included or not
+     */
+    if ( curr_ctx->guest_vers >= FFA_VERSION_1_2 )
+    {
+        /* Add caller VM information */
+        info.id = curr_ctx->ffa_id;
+        info.execution_context = curr_ctx->num_vcpus;
+        info.partition_properties = FFA_PART_VM_PROP;
+        if ( curr_ctx->is_64bit )
+            info.partition_properties |= FFA_PART_PROP_AARCH64_STATE;
+
+        ret = ffa_copy_info(dst_buf, end_buf, &info, dst_size, sizeof(info));
+        if ( ret )
+            return ret;
+
+        count++;
+    }
+
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
         /*
-         * Do not include an entry for the caller VM as the spec is not
-         * clearly mandating it and it is not supported by Linux.
+         * There could potentially be a lot of VMs in the system and we could
+         * hold the CPU for long here.
+         * Right now there is no solution in FF-A specification to split
+         * the work in this case.
+         * TODO: Check how we could delay the work or have preemption checks.
          */
-        if ( dest_ctx != curr_ctx )
+        read_lock(&ffa_ctx_list_rwlock);
+        list_for_each_entry(dest_ctx, &ffa_ctx_head, ctx_list)
         {
-            /*
-             * We do not have UUID info for VMs so use
-             * the 1.0 structure so that we set UUIDs to
-             * zero using memset
-             */
-            struct ffa_partition_info_1_0 info;
-
-            if  ( dst_buf > (end_buf - dst_size) )
-            {
-                ret = FFA_RET_NO_MEMORY;
-                goto out;
-            }
+            /* Ignore the caller entry as it was already added */
+            if ( dest_ctx == curr_ctx )
+                continue;
 
-            /*
-             * Context might has been removed since we go it or being removed
-             * right now so we might return information for a VM not existing
-             * anymore. This is acceptable as we return a view of the system
-             * which could change at any time.
-             */
             info.id = dest_ctx->ffa_id;
             info.execution_context = dest_ctx->num_vcpus;
             info.partition_properties = FFA_PART_VM_PROP;
             if ( dest_ctx->is_64bit )
                 info.partition_properties |= FFA_PART_PROP_AARCH64_STATE;
 
-            memcpy(dst_buf, &info, MIN(sizeof(info), dst_size));
-
-            if ( dst_size > sizeof(info) )
-                memset(dst_buf + sizeof(info), 0,
-                       dst_size - sizeof(info));
+            ret = ffa_copy_info(dst_buf, end_buf, &info, dst_size,
+                                sizeof(info));
+            if ( ret )
+            {
+                read_unlock(&ffa_ctx_list_rwlock);
+                return ret;
+            }
 
-            dst_buf += dst_size;
             count++;
         }
+        read_unlock(&ffa_ctx_list_rwlock);
     }
-    *vm_count = count;
 
-out:
-    read_unlock(&ffa_ctx_list_rwlock);
+    *vm_count = count;
 
     return ret;
 }
@@ -217,16 +254,17 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
     uint32_t flags = get_user_reg(regs, 5);
-    uint32_t uuid[4] = {
-        get_user_reg(regs, 1),
-        get_user_reg(regs, 2),
-        get_user_reg(regs, 3),
-        get_user_reg(regs, 4),
-    };
+    struct ffa_uuid uuid;
     uint32_t dst_size = 0;
     void *dst_buf, *end_buf;
     uint32_t ffa_vm_count = 0, ffa_sp_count = 0;
 
+    ffa_uuid_set(&uuid,
+             get_user_reg(regs, 1),
+             get_user_reg(regs, 2),
+             get_user_reg(regs, 3),
+             get_user_reg(regs, 4));
+
     /*
      * If the guest is v1.0, he does not get back the entry size so we must
      * use the v1.0 structure size in the destination buffer.
@@ -259,10 +297,23 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
         }
 
         /*
-         * Do not count the caller VM as the spec is not clearly mandating it
-         * and it is not supported by Linux.
+         * We do not have protocol UUIDs for VMs so if a request has non Nil
+         * UUID we must return a vm_count of 0
          */
-        ffa_vm_count = get_ffa_vm_count() - 1;
+        if ( ffa_uuid_is_nil(uuid) )
+        {
+            ffa_vm_count = get_ffa_vm_count();
+
+            /*
+             * Workaround for Linux FF-A Driver not accepting to have its own
+             * entry in the list before FF-A v1.2 was supported.
+             * This workaround is generally acceptable for other implementations
+             * as the specification was not completely clear on wether or not
+             * the requester endpoint information should be included or not
+             */
+            if ( ctx->guest_vers < FFA_VERSION_1_2 )
+                ffa_vm_count -= 1;
+        }
 
         goto out;
     }
@@ -290,17 +341,15 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
 
     if ( ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
     {
-        ret = ffa_get_sp_partinfo(uuid, &ffa_sp_count, dst_buf, end_buf,
+        ret = ffa_get_sp_partinfo(uuid, &ffa_sp_count, &dst_buf, end_buf,
                                   dst_size);
 
         if ( ret )
             goto out_rx_release;
-
-        dst_buf += ffa_sp_count * dst_size;
     }
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
-        ret = ffa_get_vm_partinfo(&ffa_vm_count, dst_buf, end_buf, dst_size);
+    ret = ffa_get_vm_partinfo(uuid, &ffa_vm_count, &dst_buf, end_buf,
+                              dst_size);
 
 out_rx_release:
     if ( ret )
@@ -309,7 +358,13 @@ out:
     if ( ret )
         ffa_set_regs_error(regs, ret);
     else
+    {
+        /* Size should be 0 on count request and was not supported in 1.0 */
+        if ( flags || ctx->guest_vers == FFA_VERSION_1_0 )
+            dst_size = 0;
+
         ffa_set_regs_success(regs, ffa_sp_count + ffa_vm_count, dst_size);
+    }
 }
 
 static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id,
@@ -450,6 +505,7 @@ bool ffa_partinfo_init(void)
     uint32_t count;
     int32_t e;
     void *spmc_rx;
+    struct ffa_uuid nil_uuid = { .val = { 0ULL, 0ULL } };
 
     if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
          !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32))
@@ -459,7 +515,7 @@ bool ffa_partinfo_init(void)
     if (!spmc_rx)
         return false;
 
-    e = ffa_partition_info_get(NULL, 0, &count, &fpi_size);
+    e = ffa_partition_info_get(nil_uuid, 0, &count, &fpi_size);
     if ( e )
     {
         printk(XENLOG_ERR "ffa: Failed to get list of SPs: %d\n", e);
xen/arch/arm/tee/ffa_private.h +21 -0
index 9cae238f972c..c1dac09c75ca 100644
@@ -306,6 +306,10 @@ struct ffa_mem_region {
     struct ffa_address_range address_range_array[];
 };
 
+struct ffa_uuid {
+    uint64_t val[2];
+};
+
 struct ffa_ctx_notif {
     /*
      * True if domain is reported by FFA_NOTIFICATION_INFO_GET to have
@@ -567,4 +571,21 @@ static inline bool ffa_fw_supports_fid(uint32_t fid)
     return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
 }
 
+static inline bool ffa_uuid_is_nil(struct ffa_uuid id)
+{
+    return id.val[0] == 0 && id.val[1] == 0;
+}
+
+static inline bool ffa_uuid_equal(struct ffa_uuid id1, struct ffa_uuid id2)
+{
+    return id1.val[0] == id2.val[0] && id1.val[1] == id2.val[1];
+}
+
+static inline void ffa_uuid_set(struct ffa_uuid *id, uint32_t val0,
+                                uint32_t val1, uint32_t val2, uint32_t val3)
+{
+    id->val[0] = ((uint64_t)val1 << 32U) | val0;
+    id->val[1] = ((uint64_t)val3 << 32U) | val2;
+}
+
 #endif /*__FFA_PRIVATE_H__*/
Signature · 1 line
2.51.2
Reviewed-by on v1 6/10
#21 lore raw

Re: [PATCH 06/10] xen/arm: ffa: add UUID helpers for partition info

Hi Bertrand,

Quoted 41 lines · Bertrand Marquis

Introduce struct ffa_uuid together with nil/equality/set helpers, and use it end-to-end in the partition-info plumbing.

The SP and VM enumeration paths now build UUIDs from the guest registers, call a new ffa_copy_info() helper and ensure non-nil UUID queries only return matching SP entries, relying on firmware UUID filtering. VM entries are skipped because we do not track per-VM UUIDs.

Count requests and subscriber initialisation are updated accordingly so firmware is always called with an explicit UUID. This keeps count and listing requests aligned with the FF-A v1.1 rules while preserving the Linux compatibility workaround for v1.2 requesters.

---
 xen/arch/arm/tee/ffa_partinfo.c | 206 ++++++++++++++++++++------------
 xen/arch/arm/tee/ffa_private.h  |  21 ++++
 2 files changed, 152 insertions(+), 75 deletions(-)
xen/arch/arm/tee/ffa_partinfo.c +2 -3
index 3f4a779f4146..4adbe2736c94 100644
@@ -33,7 +33,7 @@ static uint16_t subscr_vm_created_count __read_mostly;
 static uint16_t *subscr_vm_destroyed __read_mostly;
 static uint16_t subscr_vm_destroyed_count __read_mostly;
 
-static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
+static int32_t ffa_partition_info_get(struct ffa_uuid uuid, uint32_t flags,
                                       uint32_t *count, uint32_t *fpi_size)
 {
     struct arm_smccc_1_2_regs arg = {
@@ -41,15 +41,12 @@ static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
         .a5 = flags,
     };
     struct arm_smccc_1_2_regs resp;
-    uint32_t ret;
+    int32_t ret;
 
-    if ( uuid )

xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags, · line 49

Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +4 -6
-    {
-        arg.a1 = uuid[0];
-        arg.a2 = uuid[1];
-        arg.a3 = uuid[2];
-        arg.a4 = uuid[3];
-    }
+    arg.a1 = uuid.val[0] & 0xFFFFFFFFU;
+    arg.a2 = (uuid.val[0] >> 32) & 0xFFFFFFFFU;
+    arg.a3 = uuid.val[1] & 0xFFFFFFFFU;
+    arg.a4 = (uuid.val[1] >> 32) & 0xFFFFFFFFU;

You were switching to GENMASK in the previous patches, so we should probably use that here too.

With that fixed:

Cheers,
Jens

Quoted 342 lines
xen/arch/arm/tee/ffa_partinfo.c +125 -66
     arm_smccc_1_2_smc(&arg, &resp);
 
@@ -63,7 +60,26 @@ static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
     return ret;
 }
 
-static int32_t ffa_get_sp_count(uint32_t *uuid, uint32_t *sp_count)
+static int32_t ffa_copy_info(void **dst, void *dst_end, const void *src,
+                             uint32_t dst_size, uint32_t src_size)
+{
+    uint8_t *pos = *dst;
+    uint8_t *end = dst_end;
+
+    if ( pos > end - dst_size )
+        return FFA_RET_NO_MEMORY;
+
+    memcpy(pos, src, MIN(dst_size, src_size));
+
+    if ( dst_size > src_size )
+        memset(pos + src_size, 0, dst_size - src_size);
+
+    *dst = pos + dst_size;
+
+    return FFA_RET_OK;
+}
+
+static int32_t ffa_get_sp_count(struct ffa_uuid uuid, uint32_t *sp_count)
 {
     uint32_t src_size;
 
@@ -71,8 +87,8 @@ static int32_t ffa_get_sp_count(uint32_t *uuid, uint32_t *sp_count)
                                   sp_count, &src_size);
 }
 
-static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
-                                   void *dst_buf, void *end_buf,
+static int32_t ffa_get_sp_partinfo(struct ffa_uuid uuid, uint32_t *sp_count,
+                                   void **dst_buf, void *end_buf,
                                    uint32_t dst_size)
 {
     int32_t ret;
@@ -120,17 +136,18 @@ static int32_t ffa_get_sp_partinfo(uint32_t *uuid, uint32_t *sp_count,
         /* filter out SP not following bit 15 convention if any */
         if ( FFA_ID_IS_SECURE(fpi->id) )
         {
-            if ( dst_buf > (end_buf - dst_size) )
-            {
-                ret = FFA_RET_NO_MEMORY;
-                goto out;
-            }
+            /*
+             * If VM is 1.0 but firmware is 1.1 we could have several entries
+             * with the same ID but different UUIDs. In this case the VM will
+             * get a list with several time the same ID.
+             * This is a non-compliance to the specification but 1.0 VMs should
+             * handle that on their own to simplify Xen implementation.
+             */
 
-            memcpy(dst_buf, src_buf, MIN(src_size, dst_size));
-            if ( dst_size > src_size )
-                memset(dst_buf + src_size, 0, dst_size - src_size);
+            ret = ffa_copy_info(dst_buf, end_buf, src_buf, dst_size, src_size);
+            if ( ret )
+                goto out;
 
-            dst_buf += dst_size;
             count++;
         }
 
@@ -144,69 +161,89 @@ out:
     return ret;
 }
 
-static int32_t ffa_get_vm_partinfo(uint32_t *vm_count, void *dst_buf,
-                                   void *end_buf, uint32_t dst_size)
+static int32_t ffa_get_vm_partinfo(struct ffa_uuid uuid, uint32_t *vm_count,
+                                   void **dst_buf, void *end_buf,
+                                   uint32_t dst_size)
 {
     struct ffa_ctx *curr_ctx = current->domain->arch.tee;
     struct ffa_ctx *dest_ctx;
     uint32_t count = 0;
     int32_t ret = FFA_RET_OK;
+    /*
+     * We do not have UUID info for VMs so use the 1.0 structure so that we set
+     * UUIDs to zero using memset
+     */
+    struct ffa_partition_info_1_0 info;
 
     /*
-     * There could potentially be a lot of VMs in the system and we could
-     * hold the CPU for long here.
-     * Right now there is no solution in FF-A specification to split
-     * the work in this case.
-     * TODO: Check how we could delay the work or have preemption checks.
+     * We do not have protocol UUIDs for VMs so if a request has non Nil UUID
+     * we must return an empty list.
      */
-    read_lock(&ffa_ctx_list_rwlock);
-    list_for_each_entry(dest_ctx, &ffa_ctx_head, ctx_list)
+    if ( !ffa_uuid_is_nil(uuid) )
+    {
+        *vm_count = 0;
+        return FFA_RET_OK;
+    }
+
+    /*
+     * Workaround for Linux FF-A Driver not accepting to have its own
+     * entry in the list before FF-A v1.2 was supported.
+     * This workaround is generally acceptable for other implementations
+     * as the specification was not completely clear on wether or not
+     * the requester endpoint information should be included or not
+     */
+    if ( curr_ctx->guest_vers >= FFA_VERSION_1_2 )
+    {
+        /* Add caller VM information */
+        info.id = curr_ctx->ffa_id;
+        info.execution_context = curr_ctx->num_vcpus;
+        info.partition_properties = FFA_PART_VM_PROP;
+        if ( curr_ctx->is_64bit )
+            info.partition_properties |= FFA_PART_PROP_AARCH64_STATE;
+
+        ret = ffa_copy_info(dst_buf, end_buf, &info, dst_size, sizeof(info));
+        if ( ret )
+            return ret;
+
+        count++;
+    }
+
+    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
         /*
-         * Do not include an entry for the caller VM as the spec is not
-         * clearly mandating it and it is not supported by Linux.
+         * There could potentially be a lot of VMs in the system and we could
+         * hold the CPU for long here.
+         * Right now there is no solution in FF-A specification to split
+         * the work in this case.
+         * TODO: Check how we could delay the work or have preemption checks.
          */
-        if ( dest_ctx != curr_ctx )
+        read_lock(&ffa_ctx_list_rwlock);
+        list_for_each_entry(dest_ctx, &ffa_ctx_head, ctx_list)
         {
-            /*
-             * We do not have UUID info for VMs so use
-             * the 1.0 structure so that we set UUIDs to
-             * zero using memset
-             */
-            struct ffa_partition_info_1_0 info;
-
-            if  ( dst_buf > (end_buf - dst_size) )
-            {
-                ret = FFA_RET_NO_MEMORY;
-                goto out;
-            }
+            /* Ignore the caller entry as it was already added */
+            if ( dest_ctx == curr_ctx )
+                continue;
 
-            /*
-             * Context might has been removed since we go it or being removed
-             * right now so we might return information for a VM not existing
-             * anymore. This is acceptable as we return a view of the system
-             * which could change at any time.
-             */
             info.id = dest_ctx->ffa_id;
             info.execution_context = dest_ctx->num_vcpus;
             info.partition_properties = FFA_PART_VM_PROP;
             if ( dest_ctx->is_64bit )
                 info.partition_properties |= FFA_PART_PROP_AARCH64_STATE;
 
-            memcpy(dst_buf, &info, MIN(sizeof(info), dst_size));
-
-            if ( dst_size > sizeof(info) )
-                memset(dst_buf + sizeof(info), 0,
-                       dst_size - sizeof(info));
+            ret = ffa_copy_info(dst_buf, end_buf, &info, dst_size,
+                                sizeof(info));
+            if ( ret )
+            {
+                read_unlock(&ffa_ctx_list_rwlock);
+                return ret;
+            }
 
-            dst_buf += dst_size;
             count++;
         }
+        read_unlock(&ffa_ctx_list_rwlock);
     }
-    *vm_count = count;
 
-out:
-    read_unlock(&ffa_ctx_list_rwlock);
+    *vm_count = count;
 
     return ret;
 }
@@ -217,16 +254,17 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
     uint32_t flags = get_user_reg(regs, 5);
-    uint32_t uuid[4] = {
-        get_user_reg(regs, 1),
-        get_user_reg(regs, 2),
-        get_user_reg(regs, 3),
-        get_user_reg(regs, 4),
-    };
+    struct ffa_uuid uuid;
     uint32_t dst_size = 0;
     void *dst_buf, *end_buf;
     uint32_t ffa_vm_count = 0, ffa_sp_count = 0;
 
+    ffa_uuid_set(&uuid,
+             get_user_reg(regs, 1),
+             get_user_reg(regs, 2),
+             get_user_reg(regs, 3),
+             get_user_reg(regs, 4));
+
     /*
      * If the guest is v1.0, he does not get back the entry size so we must
      * use the v1.0 structure size in the destination buffer.
@@ -259,10 +297,23 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
         }
 
         /*
-         * Do not count the caller VM as the spec is not clearly mandating it
-         * and it is not supported by Linux.
+         * We do not have protocol UUIDs for VMs so if a request has non Nil
+         * UUID we must return a vm_count of 0
          */
-        ffa_vm_count = get_ffa_vm_count() - 1;
+        if ( ffa_uuid_is_nil(uuid) )
+        {
+            ffa_vm_count = get_ffa_vm_count();
+
+            /*
+             * Workaround for Linux FF-A Driver not accepting to have its own
+             * entry in the list before FF-A v1.2 was supported.
+             * This workaround is generally acceptable for other implementations
+             * as the specification was not completely clear on wether or not
+             * the requester endpoint information should be included or not
+             */
+            if ( ctx->guest_vers < FFA_VERSION_1_2 )
+                ffa_vm_count -= 1;
+        }
 
         goto out;
     }
@@ -290,17 +341,15 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
 
     if ( ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
     {
-        ret = ffa_get_sp_partinfo(uuid, &ffa_sp_count, dst_buf, end_buf,
+        ret = ffa_get_sp_partinfo(uuid, &ffa_sp_count, &dst_buf, end_buf,
                                   dst_size);
 
         if ( ret )
             goto out_rx_release;
-
-        dst_buf += ffa_sp_count * dst_size;
     }
 
-    if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
-        ret = ffa_get_vm_partinfo(&ffa_vm_count, dst_buf, end_buf, dst_size);
+    ret = ffa_get_vm_partinfo(uuid, &ffa_vm_count, &dst_buf, end_buf,
+                              dst_size);
 
 out_rx_release:
     if ( ret )
@@ -309,7 +358,13 @@ out:
     if ( ret )
         ffa_set_regs_error(regs, ret);
     else
+    {
+        /* Size should be 0 on count request and was not supported in 1.0 */
+        if ( flags || ctx->guest_vers == FFA_VERSION_1_0 )
+            dst_size = 0;
+
         ffa_set_regs_success(regs, ffa_sp_count + ffa_vm_count, dst_size);
+    }
 }
 
 static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id,
@@ -450,6 +505,7 @@ bool ffa_partinfo_init(void)
     uint32_t count;
     int32_t e;
     void *spmc_rx;
+    struct ffa_uuid nil_uuid = { .val = { 0ULL, 0ULL } };
 
     if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
          !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32))
@@ -459,7 +515,7 @@ bool ffa_partinfo_init(void)
     if (!spmc_rx)
         return false;
 
-    e = ffa_partition_info_get(NULL, 0, &count, &fpi_size);
+    e = ffa_partition_info_get(nil_uuid, 0, &count, &fpi_size);
     if ( e )
     {
         printk(XENLOG_ERR "ffa: Failed to get list of SPs: %d\n", e);
xen/arch/arm/tee/ffa_private.h +21 -1
index 9cae238f972c..c1dac09c75ca 100644
@@ -306,6 +306,10 @@ struct ffa_mem_region {
     struct ffa_address_range address_range_array[];
 };
 
+struct ffa_uuid {
+    uint64_t val[2];
+};
+
 struct ffa_ctx_notif {
     /*
      * True if domain is reported by FFA_NOTIFICATION_INFO_GET to have
@@ -567,4 +571,21 @@ static inline bool ffa_fw_supports_fid(uint32_t fid)
     return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
 }
 
+static inline bool ffa_uuid_is_nil(struct ffa_uuid id)
+{
+    return id.val[0] == 0 && id.val[1] == 0;
+}
+
+static inline bool ffa_uuid_equal(struct ffa_uuid id1, struct ffa_uuid id2)
+{
+    return id1.val[0] == id2.val[0] && id1.val[1] == id2.val[1];
+}
+
+static inline void ffa_uuid_set(struct ffa_uuid *id, uint32_t val0,
+                                uint32_t val1, uint32_t val2, uint32_t val3)
+{
+    id->val[0] = ((uint64_t)val1 << 32U) | val0;
+    id->val[1] = ((uint64_t)val3 << 32U) | val2;
+}
+
 #endif /*__FFA_PRIVATE_H__*/
--

2.51.2

#22 lore raw

Re: [PATCH 06/10] xen/arm: ffa: add UUID helpers for partition info

Hi Jens,

Quoted 46 lines · Jens Wiklander

Hi Bertrand,

Quoted 40 lines · Bertrand Marquis

Introduce struct ffa_uuid together with nil/equality/set helpers, and use it end-to-end in the partition-info plumbing.

The SP and VM enumeration paths now build UUIDs from the guest registers, call a new ffa_copy_info() helper and ensure non-nil UUID queries only return matching SP entries, relying on firmware UUID filtering. VM entries are skipped because we do not track per-VM UUIDs.

Count requests and subscriber initialisation are updated accordingly so firmware is always called with an explicit UUID. This keeps count and listing requests aligned with the FF-A v1.1 rules while preserving the Linux compatibility workaround for v1.2 requesters.

---
xen/arch/arm/tee/ffa_partinfo.c | 206 ++++++++++++++++++++------------ xen/arch/arm/tee/ffa_private.h | 21 ++++

2 files changed, 152 insertions(+), 75 deletions(-)
xen/arch/arm/tee/ffa_partinfo.c +2 -2
index 3f4a779f4146..4adbe2736c94 100644
@@ -33,7 +33,7 @@ static uint16_t subscr_vm_created_count __read_mostly;
 static uint16_t *subscr_vm_destroyed __read_mostly;
 static uint16_t subscr_vm_destroyed_count __read_mostly;
 
-static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
+static int32_t ffa_partition_info_get(struct ffa_uuid uuid, uint32_t flags,
                                      uint32_t *count, uint32_t *fpi_size)
 {
    struct arm_smccc_1_2_regs arg = {
@@ -41,15 +41,12 @@ static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
        .a5 = flags,
    };
    struct arm_smccc_1_2_regs resp;
-    uint32_t ret;
+    int32_t ret;
 
Quoted 14 lines

xen/arch/arm/tee/ffa_partinfo.c static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags, · line 49

Quoted 11 lines
xen/arch/arm/tee/ffa_partinfo.c +4 -7
-    if ( uuid )
-    {
-        arg.a1 = uuid[0];
-        arg.a2 = uuid[1];
-        arg.a3 = uuid[2];
-        arg.a4 = uuid[3];
-    }
+    arg.a1 = uuid.val[0] & 0xFFFFFFFFU;
+    arg.a2 = (uuid.val[0] >> 32) & 0xFFFFFFFFU;
+    arg.a3 = uuid.val[1] & 0xFFFFFFFFU;
+    arg.a4 = (uuid.val[1] >> 32) & 0xFFFFFFFFU;

You were switching to GENMASK in the previous patches, so we should probably use that here too.

Agree, I will fix that.

Quoted 3 lines

With that fixed:

Thanks

Cheers
Bertrand

#23 lore raw

PATCH 7/10 xen/arm: ffa: refactor direct requests via RUN helper

In reply to Bertrand Marquis

Share the SMCCC plumbing used by SEND_DIRECT and RUN via a common ffa_finish_direct_req_run() helper so canonical success and error cases are handled in one place.

The dispatcher now routes FFA_RUN through ffa_handle_run(), and direct requests bail out early if a guest targets itself or a non-secure endpoint. This simplifies the direct path and prepares the mediator for the wider v1.2 register ABI.

---
 xen/arch/arm/tee/ffa.c         |   3 +
 xen/arch/arm/tee/ffa_msg.c     | 111 ++++++++++++++++++++++++---------
 xen/arch/arm/tee/ffa_private.h |   1 +
 3 files changed, 84 insertions(+), 31 deletions(-)
xen/arch/arm/tee/ffa.c +3 -0
index 7392bb6c3db9..92cb6ad7ec97 100644
@@ -347,6 +347,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     case FFA_MSG_SEND_DIRECT_REQ_64:
         ffa_handle_msg_send_direct_req(regs, fid);
         return true;
+    case FFA_RUN:
+        ffa_handle_run(regs, fid);
+        return true;
     case FFA_MSG_SEND2:
         e = ffa_handle_msg_send2(regs);
         break;
xen/arch/arm/tee/ffa_msg.c +80 -31

152 lines not on this page · raw message

xen/arch/arm/tee/ffa_private.h +1 -0
index c1dac09c75ca..a9a03c7c5d71 100644
@@ -461,6 +461,7 @@ static inline void ffa_raise_rx_buffer_full(struct domain *d)
 
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid);
 int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs);
+void ffa_handle_run(struct cpu_user_regs *regs, uint32_t fid);
 
 #ifdef CONFIG_FFA_VM_TO_VM
 static inline uint16_t get_ffa_vm_count(void)
Signature · 1 line
2.51.2
#24 lore raw

Re: [PATCH 07/10] xen/arm: ffa: refactor direct requests via RUN helper

Hi Bertrand,

Quoted 9 lines · Bertrand Marquis

Share the SMCCC plumbing used by SEND_DIRECT and RUN via a common ffa_finish_direct_req_run() helper so canonical success and error cases are handled in one place.

The dispatcher now routes FFA_RUN through ffa_handle_run(), and direct requests bail out early if a guest targets itself or a non-secure endpoint. This simplifies the direct path and prepares the mediator for the wider v1.2 register ABI.

From the above, it sounds like FFA_RUN was already supported, but I don't see how.

xen/arch/arm/tee/ffa.c

Quoted 12 lines
---
 xen/arch/arm/tee/ffa.c         |   3 +
 xen/arch/arm/tee/ffa_msg.c     | 111 ++++++++++++++++++++++++---------
 xen/arch/arm/tee/ffa_private.h |   1 +
 3 files changed, 84 insertions(+), 31 deletions(-)
xen/arch/arm/tee/ffa.c +0 -0
index 7392bb6c3db9..92cb6ad7ec97 100644

FFA_RUN should be removed from the list of unsupported FF-A interfaces close to the top of this file.

The rest looks good.

Cheers,
Jens

#25 lore raw

Re: [PATCH 07/10] xen/arm: ffa: refactor direct requests via RUN helper

Hi Jens,

Quoted 11 lines · Jens Wiklander

Hi Bertrand,

Quoted 5 lines · Bertrand Marquis

Share the SMCCC plumbing used by SEND_DIRECT and RUN via a common ffa_finish_direct_req_run() helper so canonical success and error cases are handled in one place.

Quoted 7 lines
Quoted 4 lines

The dispatcher now routes FFA_RUN through ffa_handle_run(), and direct requests bail out early if a guest targets itself or a non-secure endpoint. This simplifies the direct path and prepares the mediator for the wider v1.2 register ABI.

From the above, it sounds like FFA_RUN was already supported, but I don't see how.

Yes It seems i forgot to rework the commit message when i rework the split in my serie.

I will update the commit message in next version.

Quoted 9 lines
Quoted 9 lines

---

xen/arch/arm/tee/ffa.c         |   3 +
xen/arch/arm/tee/ffa_msg.c     | 111 ++++++++++++++++++++++++---------
xen/arch/arm/tee/ffa_private.h |   1 +
3 files changed, 84 insertions(+), 31 deletions(-)
Quoted 7 lines

xen/arch/arm/tee/ffa.c

Quoted 4 lines
xen/arch/arm/tee/ffa.c +0 -0
index 7392bb6c3db9..92cb6ad7ec97 100644

FFA_RUN should be removed from the list of unsupported FF-A interfaces close to the top of this file.

In fact i do that in the last patch of the serie with adding support for FFA_RUN in ffa_features but this does not really make sense as this is not bounded to 1.2.

I will move those from patch 10 into this one instead in next version.

Quoted 3 lines

The rest looks good.

Thanks
Cheers
Bertrand

#26 lore raw

PATCH 8/10 xen/arm: ffa: add v1.2 SEND2 header layout

In reply to Bertrand Marquis

Teach the SEND2 path about the distinct FF-A v1.1 and v1.2 RX/TX header layouts so we can propagate the 128-bit UUIDs introduced in v1.2.

VM-to-VM SEND2 calls now build the larger v1.2 header, zeroing the UUID fields for v1.1 senders, and the dispatcher validates messages using the v1.1 header layout to keep legacy guests working.

---
 xen/arch/arm/tee/ffa_msg.c | 55 +++++++++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 13 deletions(-)
xen/arch/arm/tee/ffa_msg.c +42 -13
index 8bb4bd93f724..472bfad79dd3 100644
@@ -13,7 +13,7 @@
 #include "ffa_private.h"
 
 /* Encoding of partition message in RX/TX buffer */
-struct ffa_part_msg_rxtx {
+struct ffa_part_msg_rxtx_1_1 {
     uint32_t flags;
     uint32_t reserved;
     uint32_t msg_offset;
@@ -21,6 +21,16 @@ struct ffa_part_msg_rxtx {
     uint32_t msg_size;
 };
 
+struct ffa_part_msg_rxtx_1_2 {
+    uint32_t flags;
+    uint32_t reserved;
+    uint32_t msg_offset;
+    uint32_t send_recv_id;
+    uint32_t msg_size;
+    uint32_t reserved2;
+    uint64_t uuid[2];
+};
+
 static void ffa_finish_direct_req_run(struct cpu_user_regs *regs,
                                       struct arm_smccc_1_2_regs *req)
 {
@@ -104,12 +114,12 @@ out:
     ffa_set_regs_error(regs, ret);
 }
 
-static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
-                                struct ffa_part_msg_rxtx *src_msg)
+static int32_t ffa_msg_send2_vm(struct ffa_ctx *src_ctx, uint16_t dst_id,
+                                struct ffa_part_msg_rxtx_1_2 *src_msg)
 {
     struct domain *dst_d;
     struct ffa_ctx *dst_ctx;
-    struct ffa_part_msg_rxtx *dst_msg;
+    struct ffa_part_msg_rxtx_1_2 *dst_msg;
     int err;
     int32_t ret;
 
@@ -142,7 +152,7 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
 
     /* we need to have enough space in the destination buffer */
     if ( (dst_ctx->page_count * FFA_PAGE_SIZE -
-          sizeof(struct ffa_part_msg_rxtx)) < src_msg->msg_size )
+          sizeof(struct ffa_part_msg_rxtx_1_2)) < src_msg->msg_size )
     {
         ret = FFA_RET_NO_MEMORY;
         ffa_rx_release(dst_d);
@@ -154,12 +164,24 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     /* prepare destination header */
     dst_msg->flags = 0;
     dst_msg->reserved = 0;
-    dst_msg->msg_offset = sizeof(struct ffa_part_msg_rxtx);
+    dst_msg->msg_offset = sizeof(struct ffa_part_msg_rxtx_1_2);
     dst_msg->send_recv_id = src_msg->send_recv_id;
     dst_msg->msg_size = src_msg->msg_size;
+    dst_msg->reserved2 = 0;
 
-    memcpy(dst_ctx->rx + sizeof(struct ffa_part_msg_rxtx),
-           src_buf + src_msg->msg_offset, src_msg->msg_size);
+    if ( src_ctx->guest_vers < FFA_VERSION_1_2 )
+    {
+        dst_msg->uuid[0] = 0;
+        dst_msg->uuid[1] = 0;
+    }
+    else
+    {
+        dst_msg->uuid[0] = src_msg->uuid[0];
+        dst_msg->uuid[1] = src_msg->uuid[1];
+    }
+
+    memcpy(dst_ctx->rx + sizeof(struct ffa_part_msg_rxtx_1_2),
+           src_ctx->tx + src_msg->msg_offset, src_msg->msg_size);
 
     /* receiver rx buffer will be released by the receiver*/
 
@@ -175,11 +197,17 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 {
     struct domain *src_d = current->domain;
     struct ffa_ctx *src_ctx = src_d->arch.tee;
-    struct ffa_part_msg_rxtx src_msg;
+    /*
+     * src_msg is interpreted as v1.2 header, but:
+     * - for v1.1 guests, uuid[] is ignored and may contain payload bytes
+     * - for v1.2 guests, uuid[] carries the FF-A v1.2 UUID fields
+     */
+    struct ffa_part_msg_rxtx_1_2 src_msg;
     uint16_t dst_id, src_id;
     int32_t ret;
 
-    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx) >= FFA_PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx_1_1) >= FFA_PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx_1_2) >= FFA_PAGE_SIZE);
 
     if ( !spin_trylock(&src_ctx->tx_lock) )
         return FFA_RET_BUSY;
@@ -190,14 +218,15 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
     src_id = src_msg.send_recv_id >> 16;
     dst_id = src_msg.send_recv_id & GENMASK(15,0);
 
-    if ( src_id != ffa_get_vm_id(src_d) )
+    if ( src_id != ffa_get_vm_id(src_d) ||
+         dst_id == ffa_get_vm_id(src_d) )
     {
         ret = FFA_RET_INVALID_PARAMETERS;
         goto out;
     }
 
     /* check source message fits in buffer */
-    if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx) ||
+    if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_1) ||
             src_msg.msg_size == 0 ||
             src_msg.msg_offset > src_ctx->page_count * FFA_PAGE_SIZE ||
             src_msg.msg_size > (src_ctx->page_count * FFA_PAGE_SIZE -
@@ -222,7 +251,7 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
     else if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
         /* Message for a VM */
-        ret = ffa_msg_send2_vm(dst_id, src_ctx->tx, &src_msg);
+        ret = ffa_msg_send2_vm(src_ctx, dst_id, &src_msg);
     }
     else
         ret = FFA_RET_INVALID_PARAMETERS;
Signature · 1 line
2.51.2
#27 lore raw

Re: [PATCH 08/10] xen/arm: ffa: add v1.2 SEND2 header layout

Hi Bertrand,

Quoted 117 lines · Bertrand Marquis

Teach the SEND2 path about the distinct FF-A v1.1 and v1.2 RX/TX header layouts so we can propagate the 128-bit UUIDs introduced in v1.2.

VM-to-VM SEND2 calls now build the larger v1.2 header, zeroing the UUID fields for v1.1 senders, and the dispatcher validates messages using the v1.1 header layout to keep legacy guests working.

---
 xen/arch/arm/tee/ffa_msg.c | 55 +++++++++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 13 deletions(-)
xen/arch/arm/tee/ffa_msg.c +38 -10
index 8bb4bd93f724..472bfad79dd3 100644
@@ -13,7 +13,7 @@
 #include "ffa_private.h"
 
 /* Encoding of partition message in RX/TX buffer */
-struct ffa_part_msg_rxtx {
+struct ffa_part_msg_rxtx_1_1 {
     uint32_t flags;
     uint32_t reserved;
     uint32_t msg_offset;
@@ -21,6 +21,16 @@ struct ffa_part_msg_rxtx {
     uint32_t msg_size;
 };
 
+struct ffa_part_msg_rxtx_1_2 {
+    uint32_t flags;
+    uint32_t reserved;
+    uint32_t msg_offset;
+    uint32_t send_recv_id;
+    uint32_t msg_size;
+    uint32_t reserved2;
+    uint64_t uuid[2];
+};
+
 static void ffa_finish_direct_req_run(struct cpu_user_regs *regs,
                                       struct arm_smccc_1_2_regs *req)
 {
@@ -104,12 +114,12 @@ out:
     ffa_set_regs_error(regs, ret);
 }
 
-static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
-                                struct ffa_part_msg_rxtx *src_msg)
+static int32_t ffa_msg_send2_vm(struct ffa_ctx *src_ctx, uint16_t dst_id,
+                                struct ffa_part_msg_rxtx_1_2 *src_msg)
 {
     struct domain *dst_d;
     struct ffa_ctx *dst_ctx;
-    struct ffa_part_msg_rxtx *dst_msg;
+    struct ffa_part_msg_rxtx_1_2 *dst_msg;
     int err;
     int32_t ret;
 
@@ -142,7 +152,7 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
 
     /* we need to have enough space in the destination buffer */
     if ( (dst_ctx->page_count * FFA_PAGE_SIZE -
-          sizeof(struct ffa_part_msg_rxtx)) < src_msg->msg_size )
+          sizeof(struct ffa_part_msg_rxtx_1_2)) < src_msg->msg_size )
     {
         ret = FFA_RET_NO_MEMORY;
         ffa_rx_release(dst_d);
@@ -154,12 +164,24 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
     /* prepare destination header */
     dst_msg->flags = 0;
     dst_msg->reserved = 0;
-    dst_msg->msg_offset = sizeof(struct ffa_part_msg_rxtx);
+    dst_msg->msg_offset = sizeof(struct ffa_part_msg_rxtx_1_2);
     dst_msg->send_recv_id = src_msg->send_recv_id;
     dst_msg->msg_size = src_msg->msg_size;
+    dst_msg->reserved2 = 0;
 
-    memcpy(dst_ctx->rx + sizeof(struct ffa_part_msg_rxtx),
-           src_buf + src_msg->msg_offset, src_msg->msg_size);
+    if ( src_ctx->guest_vers < FFA_VERSION_1_2 )
+    {
+        dst_msg->uuid[0] = 0;
+        dst_msg->uuid[1] = 0;
+    }
+    else
+    {
+        dst_msg->uuid[0] = src_msg->uuid[0];
+        dst_msg->uuid[1] = src_msg->uuid[1];
+    }
+
+    memcpy(dst_ctx->rx + sizeof(struct ffa_part_msg_rxtx_1_2),
+           src_ctx->tx + src_msg->msg_offset, src_msg->msg_size);
 
     /* receiver rx buffer will be released by the receiver*/
 
@@ -175,11 +197,17 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 {
     struct domain *src_d = current->domain;
     struct ffa_ctx *src_ctx = src_d->arch.tee;
-    struct ffa_part_msg_rxtx src_msg;
+    /*
+     * src_msg is interpreted as v1.2 header, but:
+     * - for v1.1 guests, uuid[] is ignored and may contain payload bytes
+     * - for v1.2 guests, uuid[] carries the FF-A v1.2 UUID fields
+     */
+    struct ffa_part_msg_rxtx_1_2 src_msg;
     uint16_t dst_id, src_id;
     int32_t ret;
 
-    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx) >= FFA_PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx_1_1) >= FFA_PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx_1_2) >= FFA_PAGE_SIZE);
 
     if ( !spin_trylock(&src_ctx->tx_lock) )
         return FFA_RET_BUSY;

xen/arch/arm/tee/ffa_msg.c int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs) · line 222

Quoted 6 lines
xen/arch/arm/tee/ffa_msg.c +2 -1
@@ -190,14 +218,15 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
     src_id = src_msg.send_recv_id >> 16;
     dst_id = src_msg.send_recv_id & GENMASK(15,0);
 
-    if ( src_id != ffa_get_vm_id(src_d) )
+    if ( src_id != ffa_get_vm_id(src_d) ||
+         dst_id == ffa_get_vm_id(src_d) )

It might be worth mentioning in the commit message that we're making it a bit more robust.

xen/arch/arm/tee/ffa_msg.c int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs) · line 229

Quoted 8 lines
xen/arch/arm/tee/ffa_msg.c +1 -1
     {
         ret = FFA_RET_INVALID_PARAMETERS;
         goto out;
     }
 
     /* check source message fits in buffer */
-    if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx) ||
+    if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_1) ||

This is for FF-A version 1.1; for version 1.2, the minimal offset must be larger.

Cheers,
Jens

Quoted 15 lines
xen/arch/arm/tee/ffa_msg.c +1 -2
             src_msg.msg_size == 0 ||
             src_msg.msg_offset > src_ctx->page_count * FFA_PAGE_SIZE ||
             src_msg.msg_size > (src_ctx->page_count * FFA_PAGE_SIZE -
@@ -222,7 +251,7 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
     else if ( IS_ENABLED(CONFIG_FFA_VM_TO_VM) )
     {
         /* Message for a VM */
-        ret = ffa_msg_send2_vm(dst_id, src_ctx->tx, &src_msg);
+        ret = ffa_msg_send2_vm(src_ctx, dst_id, &src_msg);
     }
     else
         ret = FFA_RET_INVALID_PARAMETERS;
--

2.51.2

#28 lore raw

Re: [PATCH 08/10] xen/arm: ffa: add v1.2 SEND2 header layout

HI Jens,

Quoted 126 lines · Jens Wiklander

Hi Bertrand,

Quoted 120 lines · Bertrand Marquis

Teach the SEND2 path about the distinct FF-A v1.1 and v1.2 RX/TX header layouts so we can propagate the 128-bit UUIDs introduced in v1.2.

VM-to-VM SEND2 calls now build the larger v1.2 header, zeroing the UUID fields for v1.1 senders, and the dispatcher validates messages using the v1.1 header layout to keep legacy guests working.

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

1 file changed, 42 insertions(+), 13 deletions(-)
xen/arch/arm/tee/ffa_msg.c +38 -10
index 8bb4bd93f724..472bfad79dd3 100644
@@ -13,7 +13,7 @@
 #include "ffa_private.h"
 
 /* Encoding of partition message in RX/TX buffer */
-struct ffa_part_msg_rxtx {
+struct ffa_part_msg_rxtx_1_1 {
    uint32_t flags;
    uint32_t reserved;
    uint32_t msg_offset;
@@ -21,6 +21,16 @@ struct ffa_part_msg_rxtx {
    uint32_t msg_size;
 };
 
+struct ffa_part_msg_rxtx_1_2 {
+    uint32_t flags;
+    uint32_t reserved;
+    uint32_t msg_offset;
+    uint32_t send_recv_id;
+    uint32_t msg_size;
+    uint32_t reserved2;
+    uint64_t uuid[2];
+};
+
 static void ffa_finish_direct_req_run(struct cpu_user_regs *regs,
                                      struct arm_smccc_1_2_regs *req)
 {
@@ -104,12 +114,12 @@ out:
    ffa_set_regs_error(regs, ret);
 }
 
-static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
-                                struct ffa_part_msg_rxtx *src_msg)
+static int32_t ffa_msg_send2_vm(struct ffa_ctx *src_ctx, uint16_t dst_id,
+                                struct ffa_part_msg_rxtx_1_2 *src_msg)
 {
    struct domain *dst_d;
    struct ffa_ctx *dst_ctx;
-    struct ffa_part_msg_rxtx *dst_msg;
+    struct ffa_part_msg_rxtx_1_2 *dst_msg;
    int err;
    int32_t ret;
 
@@ -142,7 +152,7 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
 
    /* we need to have enough space in the destination buffer */
    if ( (dst_ctx->page_count * FFA_PAGE_SIZE -
-          sizeof(struct ffa_part_msg_rxtx)) < src_msg->msg_size )
+          sizeof(struct ffa_part_msg_rxtx_1_2)) < src_msg->msg_size )
    {
        ret = FFA_RET_NO_MEMORY;
        ffa_rx_release(dst_d);
@@ -154,12 +164,24 @@ static int32_t ffa_msg_send2_vm(uint16_t dst_id, const void *src_buf,
    /* prepare destination header */
    dst_msg->flags = 0;
    dst_msg->reserved = 0;
-    dst_msg->msg_offset = sizeof(struct ffa_part_msg_rxtx);
+    dst_msg->msg_offset = sizeof(struct ffa_part_msg_rxtx_1_2);
    dst_msg->send_recv_id = src_msg->send_recv_id;
    dst_msg->msg_size = src_msg->msg_size;
+    dst_msg->reserved2 = 0;
 
-    memcpy(dst_ctx->rx + sizeof(struct ffa_part_msg_rxtx),
-           src_buf + src_msg->msg_offset, src_msg->msg_size);
+    if ( src_ctx->guest_vers < FFA_VERSION_1_2 )
+    {
+        dst_msg->uuid[0] = 0;
+        dst_msg->uuid[1] = 0;
+    }
+    else
+    {
+        dst_msg->uuid[0] = src_msg->uuid[0];
+        dst_msg->uuid[1] = src_msg->uuid[1];
+    }
+
+    memcpy(dst_ctx->rx + sizeof(struct ffa_part_msg_rxtx_1_2),
+           src_ctx->tx + src_msg->msg_offset, src_msg->msg_size);
 
    /* receiver rx buffer will be released by the receiver*/
 
@@ -175,11 +197,17 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
 {
    struct domain *src_d = current->domain;
    struct ffa_ctx *src_ctx = src_d->arch.tee;
-    struct ffa_part_msg_rxtx src_msg;
+    /*
+     * src_msg is interpreted as v1.2 header, but:
+     * - for v1.1 guests, uuid[] is ignored and may contain payload bytes
+     * - for v1.2 guests, uuid[] carries the FF-A v1.2 UUID fields
+     */
+    struct ffa_part_msg_rxtx_1_2 src_msg;
    uint16_t dst_id, src_id;
    int32_t ret;
 
-    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx) >= FFA_PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx_1_1) >= FFA_PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct ffa_part_msg_rxtx_1_2) >= FFA_PAGE_SIZE);
 
    if ( !spin_trylock(&src_ctx->tx_lock) )
        return FFA_RET_BUSY;
@@ -190,14 +218,15 @@ int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
    src_id = src_msg.send_recv_id >> 16;
    dst_id = src_msg.send_recv_id & GENMASK(15,0);
 
Quoted 6 lines

xen/arch/arm/tee/ffa_msg.c int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs) · line 222

Quoted 3 lines
xen/arch/arm/tee/ffa_msg.c +2 -1
-    if ( src_id != ffa_get_vm_id(src_d) )
+    if ( src_id != ffa_get_vm_id(src_d) ||
+         dst_id == ffa_get_vm_id(src_d) )

It might be worth mentioning in the commit message that we're making it a bit more robust.

Ack i will add this in the commit message.

Quoted 6 lines
Quoted 6 lines
xen/arch/arm/tee/ffa_msg.c +0 -0
    {
        ret = FFA_RET_INVALID_PARAMETERS;
        goto out;
    }
 
Quoted 6 lines

xen/arch/arm/tee/ffa_msg.c int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs) · line 229

Quoted 3 lines
xen/arch/arm/tee/ffa_msg.c +1 -1
    /* check source message fits in buffer */
-    if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx) ||
+    if ( src_msg.msg_offset < sizeof(struct ffa_part_msg_rxtx_1_1) ||

This is for FF-A version 1.1; for version 1.2, the minimal offset must be larger.

Yes you are right.

I will update the check and optimize things a little bit setting here the uuid to 0 in src_msg if sender is < 1.1. This will remove the need to transfer the context to test the version to ffa_msg_send2_vm.

Thanks for the finding :-)

Cheers
Bertrand

#29 lore raw

PATCH 9/10 xen/arm: ffa: add MSG_SEND_DIRECT_REQ2 support

In reply to Bertrand Marquis

Extend the direct-request path so FF-A v1.2 guests can issue FFA_MSG_SEND_DIRECT_REQ2 and receive the matching RESP2.

The handler now marshals registers x8–x17, and ffa_finish_direct_req_run() copies back the 17-register response used by FFA_MSG_SEND_DIRECT_RESP2. The new opcode is exposed via FFA_FEATURES and gated on guests that negotiated v1.2.

---
 xen/arch/arm/tee/ffa.c     | 20 +++++++++++++++++++
 xen/arch/arm/tee/ffa_msg.c | 39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
xen/arch/arm/tee/ffa.c +20 -0
index 92cb6ad7ec97..8b2f042287fc 100644
@@ -234,6 +234,8 @@ out_continue:
 static void handle_features(struct cpu_user_regs *regs)
 {
     uint32_t a1 = get_user_reg(regs, 1);
+    struct domain *d = current->domain;
+    struct ffa_ctx *ctx = d->arch.tee;
     unsigned int n;
 
     for ( n = 2; n <= 7; n++ )
@@ -262,6 +264,16 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_MSG_SEND2:
         ffa_set_regs_success(regs, 0, 0);
         break;
+    case FFA_MSG_SEND_DIRECT_REQ2:
+        if ( ctx->guest_vers >= FFA_VERSION_1_2 )
+        {
+            ffa_set_regs_success(regs, 0, 0);
+        }
+        else
+        {
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        }
+        break;
     case FFA_MEM_SHARE_64:
     case FFA_MEM_SHARE_32:
         /*
@@ -347,6 +359,14 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     case FFA_MSG_SEND_DIRECT_REQ_64:
         ffa_handle_msg_send_direct_req(regs, fid);
         return true;
+    case FFA_MSG_SEND_DIRECT_REQ2:
+        if ( ctx->guest_vers >= FFA_VERSION_1_2 )
+        {
+            ffa_handle_msg_send_direct_req(regs, fid);
+            return true;
+        }
+        e = FFA_RET_NOT_SUPPORTED;
+        break;
     case FFA_RUN:
         ffa_handle_run(regs, fid);
         return true;
xen/arch/arm/tee/ffa_msg.c +39 -0
index 472bfad79dd3..1f42aae1e0b6 100644
@@ -49,6 +49,30 @@ static void ffa_finish_direct_req_run(struct cpu_user_regs *regs,
     case FFA_MSG_YIELD:
     case FFA_INTERRUPT:
         break;
+    case FFA_MSG_SEND_DIRECT_RESP2:
+        /*
+         * REQ2 / RESP2 use a 17-register payload (x1–x17). Copy all of them
+         * back to the guest context.
+         */
+        set_user_reg(regs, 0, resp.a0);
+        set_user_reg(regs, 1, resp.a1);
+        set_user_reg(regs, 2, resp.a2);
+        set_user_reg(regs, 3, resp.a3);
+        set_user_reg(regs, 4, resp.a4);
+        set_user_reg(regs, 5, resp.a5);
+        set_user_reg(regs, 6, resp.a6);
+        set_user_reg(regs, 7, resp.a7);
+        set_user_reg(regs, 8, resp.a8);
+        set_user_reg(regs, 9, resp.a9);
+        set_user_reg(regs, 10, resp.a10);
+        set_user_reg(regs, 11, resp.a11);
+        set_user_reg(regs, 12, resp.a12);
+        set_user_reg(regs, 13, resp.a13);
+        set_user_reg(regs, 14, resp.a14);
+        set_user_reg(regs, 15, resp.a15);
+        set_user_reg(regs, 16, resp.a16);
+        set_user_reg(regs, 17, resp.a17);
+        return;
     default:
         /* Bad fid, report back to the caller. */
         ffa_set_regs_error(regs, FFA_RET_ABORTED);
@@ -107,6 +131,21 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     arg.a6 = get_user_reg(regs, 6) & mask;
     arg.a7 = get_user_reg(regs, 7) & mask;
 
+    if ( fid == FFA_MSG_SEND_DIRECT_REQ2 )
+    {
+        /* 17 registers are used for REQ2 */
+        arg.a8 = get_user_reg(regs, 8);
+        arg.a9 = get_user_reg(regs, 9);
+        arg.a10 = get_user_reg(regs, 10);
+        arg.a11 = get_user_reg(regs, 11);
+        arg.a12 = get_user_reg(regs, 12);
+        arg.a13 = get_user_reg(regs, 13);
+        arg.a14 = get_user_reg(regs, 14);
+        arg.a15 = get_user_reg(regs, 15);
+        arg.a16 = get_user_reg(regs, 16);
+        arg.a17 = get_user_reg(regs, 17);
+    }
+
     ffa_finish_direct_req_run(regs, &arg);
     return;
 
Signature · 1 line
2.51.2
Reviewed-by on v1 9/10
#30 lore raw

Re: [PATCH 09/10] xen/arm: ffa: add MSG_SEND_DIRECT_REQ2 support

Hi Bertrand,

Quoted 9 lines · Bertrand Marquis

Extend the direct-request path so FF-A v1.2 guests can issue FFA_MSG_SEND_DIRECT_REQ2 and receive the matching RESP2.

The handler now marshals registers x8–x17, and ffa_finish_direct_req_run() copies back the 17-register response used by FFA_MSG_SEND_DIRECT_RESP2. The new opcode is exposed via FFA_FEATURES and gated on guests that negotiated v1.2.

Quoted 5 lines
---
 xen/arch/arm/tee/ffa.c     | 20 +++++++++++++++++++
 xen/arch/arm/tee/ffa_msg.c | 39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

Looks good.

Cheers,
Jens

Quoted 106 lines
xen/arch/arm/tee/ffa.c +20 -0
index 92cb6ad7ec97..8b2f042287fc 100644
@@ -234,6 +234,8 @@ out_continue:
 static void handle_features(struct cpu_user_regs *regs)
 {
     uint32_t a1 = get_user_reg(regs, 1);
+    struct domain *d = current->domain;
+    struct ffa_ctx *ctx = d->arch.tee;
     unsigned int n;
 
     for ( n = 2; n <= 7; n++ )
@@ -262,6 +264,16 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_MSG_SEND2:
         ffa_set_regs_success(regs, 0, 0);
         break;
+    case FFA_MSG_SEND_DIRECT_REQ2:
+        if ( ctx->guest_vers >= FFA_VERSION_1_2 )
+        {
+            ffa_set_regs_success(regs, 0, 0);
+        }
+        else
+        {
+            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        }
+        break;
     case FFA_MEM_SHARE_64:
     case FFA_MEM_SHARE_32:
         /*
@@ -347,6 +359,14 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     case FFA_MSG_SEND_DIRECT_REQ_64:
         ffa_handle_msg_send_direct_req(regs, fid);
         return true;
+    case FFA_MSG_SEND_DIRECT_REQ2:
+        if ( ctx->guest_vers >= FFA_VERSION_1_2 )
+        {
+            ffa_handle_msg_send_direct_req(regs, fid);
+            return true;
+        }
+        e = FFA_RET_NOT_SUPPORTED;
+        break;
     case FFA_RUN:
         ffa_handle_run(regs, fid);
         return true;
xen/arch/arm/tee/ffa_msg.c +39 -1
index 472bfad79dd3..1f42aae1e0b6 100644
@@ -49,6 +49,30 @@ static void ffa_finish_direct_req_run(struct cpu_user_regs *regs,
     case FFA_MSG_YIELD:
     case FFA_INTERRUPT:
         break;
+    case FFA_MSG_SEND_DIRECT_RESP2:
+        /*
+         * REQ2 / RESP2 use a 17-register payload (x1–x17). Copy all of them
+         * back to the guest context.
+         */
+        set_user_reg(regs, 0, resp.a0);
+        set_user_reg(regs, 1, resp.a1);
+        set_user_reg(regs, 2, resp.a2);
+        set_user_reg(regs, 3, resp.a3);
+        set_user_reg(regs, 4, resp.a4);
+        set_user_reg(regs, 5, resp.a5);
+        set_user_reg(regs, 6, resp.a6);
+        set_user_reg(regs, 7, resp.a7);
+        set_user_reg(regs, 8, resp.a8);
+        set_user_reg(regs, 9, resp.a9);
+        set_user_reg(regs, 10, resp.a10);
+        set_user_reg(regs, 11, resp.a11);
+        set_user_reg(regs, 12, resp.a12);
+        set_user_reg(regs, 13, resp.a13);
+        set_user_reg(regs, 14, resp.a14);
+        set_user_reg(regs, 15, resp.a15);
+        set_user_reg(regs, 16, resp.a16);
+        set_user_reg(regs, 17, resp.a17);
+        return;
     default:
         /* Bad fid, report back to the caller. */
         ffa_set_regs_error(regs, FFA_RET_ABORTED);
@@ -107,6 +131,21 @@ void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     arg.a6 = get_user_reg(regs, 6) & mask;
     arg.a7 = get_user_reg(regs, 7) & mask;
 
+    if ( fid == FFA_MSG_SEND_DIRECT_REQ2 )
+    {
+        /* 17 registers are used for REQ2 */
+        arg.a8 = get_user_reg(regs, 8);
+        arg.a9 = get_user_reg(regs, 9);
+        arg.a10 = get_user_reg(regs, 10);
+        arg.a11 = get_user_reg(regs, 11);
+        arg.a12 = get_user_reg(regs, 12);
+        arg.a13 = get_user_reg(regs, 13);
+        arg.a14 = get_user_reg(regs, 14);
+        arg.a15 = get_user_reg(regs, 15);
+        arg.a16 = get_user_reg(regs, 16);
+        arg.a17 = get_user_reg(regs, 17);
+    }
+
     ffa_finish_direct_req_run(regs, &arg);
     return;
 
--

2.51.2

#31 lore raw

cover xen/arm: ffa: FF-A v1.2 support

In reply to Bertrand Marquis

This series updates Xen’s FF-A mediator on Arm to implement the FF-A v1.2 interface while keeping existing v1.0/v1.1 guests working.

Patches 1–5 rework the low-level plumbing:

1) add the FF-A v1.2 function IDs and probe the new ABIs
2) track per-VM FFA_VERSION state and enforce negotiation
3) harden RX/TX mapping and validation
4) rework SPMC RX/TX buffer management so access is serialized and RX buffers are always released back to the SPMC
5) switch the mediator to spec-compliant signed 32-bit status codes

Patches 6–9 update the data structures and direct-call paths:

6) add ffa_uuid helpers and rework partition-info handling
7) refactor direct requests via a common RUN/direct completion helper
8) add the v1.1/v1.2 SEND2 header layout
9) add MSG_SEND_DIRECT_REQ2/RESP2 support and marshal the extended register set for v1.2 guests

Patch 10 tightens the dispatcher and advertises FF-A v1.2 only to guests that negotiated v1.2:

- reject SMCCC64 calls from AArch32 guests
- expose the new FIDs and RX/TX capacity fields
- bump Xen's FF-A version to 1.2 once the implementation is complete

v1.0/v1.1 guests continue to use the v1.1 ABI without behaviour changes, while v1.2 guests can negotiate the wider ABI and use RUN, SEND2, and DIRECT_REQ2/RESP2 with the extended register set.

This serie was validated through gitlab-ci here:
https://gitlab.com/xen-project/people/bmarquis/xen-ffa-research/-/tree/ffa-v1.2/v0?ref_type=heads

Bertrand Marquis (10):
xen/arm: ffa: add FF-A v1.2 function IDs xen/arm: ffa: per-VM FFA_VERSION negotiation state xen/arm: ffa: harden RX/TX mapping
xen/arm: ffa: rework SPMC RX/TX buffer management xen/arm: ffa: use signed 32-bit status codes xen/arm: ffa: add UUID helpers for partition info xen/arm: ffa: refactor direct requests via RUN helper xen/arm: ffa: add v1.2 SEND2 header layout xen/arm: ffa: add MSG_SEND_DIRECT_REQ2 support xen/arm: ffa: advertise FF-A v1.2

 xen/arch/arm/include/asm/tee/ffa.h |   2 +-
 xen/arch/arm/tee/ffa.c             | 205 +++++++++++++++++-------
 xen/arch/arm/tee/ffa_msg.c         | 203 ++++++++++++++++++-----
 xen/arch/arm/tee/ffa_notif.c       |   6 +-
 xen/arch/arm/tee/ffa_partinfo.c    | 248 ++++++++++++++++++-----------
 xen/arch/arm/tee/ffa_private.h     | 129 ++++++++++-----
 xen/arch/arm/tee/ffa_rxtx.c        | 160 +++++++++++++++----
 xen/arch/arm/tee/ffa_shm.c         |  30 ++--
 8 files changed, 697 insertions(+), 286 deletions(-)
Signature · 1 line
2.51.2
#32 lore raw

Hi,

Please ignore this mail, cover letter was sent in duplicate with my serie and this one is an old version missing the pipeline link.

Rest of the serie is ok.

Sorry for that.

Bertrand

Quoted 62 lines · Bertrand Marquis

This series updates Xen’s FF-A mediator on Arm to implement the FF-A v1.2 interface while keeping existing v1.0/v1.1 guests working.

Patches 1–5 rework the low-level plumbing:

1) add the FF-A v1.2 function IDs and probe the new ABIs
2) track per-VM FFA_VERSION state and enforce negotiation
3) harden RX/TX mapping and validation
4) rework SPMC RX/TX buffer management so access is serialized and RX buffers are always released back to the SPMC
5) switch the mediator to spec-compliant signed 32-bit status codes

Patches 6–9 update the data structures and direct-call paths:

6) add ffa_uuid helpers and rework partition-info handling
7) refactor direct requests via a common RUN/direct completion helper
8) add the v1.1/v1.2 SEND2 header layout
9) add MSG_SEND_DIRECT_REQ2/RESP2 support and marshal the extended register set for v1.2 guests

Patch 10 tightens the dispatcher and advertises FF-A v1.2 only to guests that negotiated v1.2:

- reject SMCCC64 calls from AArch32 guests
- expose the new FIDs and RX/TX capacity fields
- bump Xen's FF-A version to 1.2 once the implementation is complete

v1.0/v1.1 guests continue to use the v1.1 ABI without behaviour changes, while v1.2 guests can negotiate the wider ABI and use RUN, SEND2, and DIRECT_REQ2/RESP2 with the extended register set.

This serie was validated through gitlab-ci here:
https://gitlab.com/xen-project/people/bmarquis/xen-ffa-research/-/tree/ffa-v1.2/v0?ref_type=heads

Bertrand Marquis (10):
xen/arm: ffa: add FF-A v1.2 function IDs xen/arm: ffa: per-VM FFA_VERSION negotiation state xen/arm: ffa: harden RX/TX mapping
xen/arm: ffa: rework SPMC RX/TX buffer management xen/arm: ffa: use signed 32-bit status codes xen/arm: ffa: add UUID helpers for partition info xen/arm: ffa: refactor direct requests via RUN helper xen/arm: ffa: add v1.2 SEND2 header layout xen/arm: ffa: add MSG_SEND_DIRECT_REQ2 support xen/arm: ffa: advertise FF-A v1.2

xen/arch/arm/include/asm/tee/ffa.h |   2 +-
xen/arch/arm/tee/ffa.c             | 205 +++++++++++++++++-------
xen/arch/arm/tee/ffa_msg.c         | 203 ++++++++++++++++++-----
xen/arch/arm/tee/ffa_notif.c       |   6 +-
xen/arch/arm/tee/ffa_partinfo.c    | 248 ++++++++++++++++++-----------
xen/arch/arm/tee/ffa_private.h     | 129 ++++++++++-----
xen/arch/arm/tee/ffa_rxtx.c        | 160 +++++++++++++++----
xen/arch/arm/tee/ffa_shm.c         |  30 ++--
8 files changed, 697 insertions(+), 286 deletions(-)
Signature · 1 line
2.51.2
#33 lore raw

PATCH 10/10 xen/arm: ffa: advertise FF-A v1.2

In reply to Bertrand Marquis

Advertise RUN/YIELD/INTERRUPT via FFA_FEATURES now that the runtime helpers are in place, and expose the RX/TX MAP capacity field only once a guest has negotiated FF-A v1.2.

While there, drop the stale <asm/tee/ffa.h> include.

To comply with the wider v1.2 register ABI, zero registers x8–x17 when responding to a v1.2 VM. The dispatcher also rejects SMCCC64 calls from AArch32 guests.

Finally, bump Xen's FF-A version to 1.2.

---
 xen/arch/arm/tee/ffa.c         | 25 ++++++++++++++++++++-----
 xen/arch/arm/tee/ffa_private.h | 28 +++++++++++++++++++---------
 2 files changed, 39 insertions(+), 14 deletions(-)
xen/arch/arm/tee/ffa.c +20 -5
index 8b2f042287fc..c8fe97856d06 100644
@@ -24,9 +24,6 @@
  * o FFA_MEM_DONATE_* and FFA_MEM_LEND_* - Used when tranferring ownership
  *   or access of a memory region
  * o FFA_MSG_SEND2 and FFA_MSG_WAIT - Used for indirect messaging
- * o FFA_MSG_YIELD
- * o FFA_INTERRUPT - Used to report preemption
- * o FFA_RUN
  *
  * Limitations in the implemented FF-A interfaces:
  * o FFA_RXTX_MAP_*:
@@ -68,7 +65,6 @@
 #include <asm/event.h>
 #include <asm/regs.h>
 #include <asm/smccc.h>
-#include <asm/tee/ffa.h>
 #include <asm/tee/tee.h>
 
 #include "ffa_private.h"
@@ -262,6 +258,9 @@ static void handle_features(struct cpu_user_regs *regs)
     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);
         break;
     case FFA_MSG_SEND_DIRECT_REQ2:
@@ -290,7 +289,16 @@ static void handle_features(struct cpu_user_regs *regs)
          * differs from FFA_PAGE_SIZE (SZ_4K).
          */
         BUILD_BUG_ON(PAGE_SIZE != FFA_PAGE_SIZE);
-        ffa_set_regs_success(regs, 0, 0);
+
+        /*
+         * From FFA v1.2, we can give the maximum number of pages we support
+         * for the RX/TX buffers.
+         */
+        if ( ctx->guest_vers < FFA_VERSION_1_2 )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_success(regs, FFA_MAX_RXTX_PAGE_COUNT << 16, 0);
+
         break;
     case FFA_FEATURE_NOTIF_PEND_INTR:
         ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
@@ -323,6 +331,13 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     if ( !ctx )
         return false;
 
+    if ( !is_64bit_domain(d) && smccc_is_conv_64(fid) )
+    {
+        /* 32bit guests should only use 32bit convention calls */
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        return true;
+    }
+
     /* A version must be negotiated first */
     if ( !ctx->guest_vers_negotiated )
     {
xen/arch/arm/tee/ffa_private.h +19 -9
index a9a03c7c5d71..6afca2ea07ef 100644
@@ -53,7 +53,7 @@
  * that particular guest or SP.
  */
 #define FFA_MY_VERSION_MAJOR    1U
-#define FFA_MY_VERSION_MINOR    1U
+#define FFA_MY_VERSION_MINOR    2U
 #define FFA_MY_VERSION          MAKE_FFA_VERSION(FFA_MY_VERSION_MAJOR, \
                                                  FFA_MY_VERSION_MINOR)
 
@@ -505,14 +505,24 @@ static inline void ffa_set_regs(struct cpu_user_regs *regs, register_t v0,
                                 register_t v4, register_t v5, register_t v6,
                                 register_t v7)
 {
-        set_user_reg(regs, 0, v0);
-        set_user_reg(regs, 1, v1);
-        set_user_reg(regs, 2, v2);
-        set_user_reg(regs, 3, v3);
-        set_user_reg(regs, 4, v4);
-        set_user_reg(regs, 5, v5);
-        set_user_reg(regs, 6, v6);
-        set_user_reg(regs, 7, v7);
+    struct domain *d = current->domain;
+    struct ffa_ctx *ctx = d->arch.tee;
+    int i;
+
+    set_user_reg(regs, 0, v0);
+    set_user_reg(regs, 1, v1);
+    set_user_reg(regs, 2, v2);
+    set_user_reg(regs, 3, v3);
+    set_user_reg(regs, 4, v4);
+    set_user_reg(regs, 5, v5);
+    set_user_reg(regs, 6, v6);
+    set_user_reg(regs, 7, v7);
+
+    if ( ctx && ctx->guest_vers >= FFA_VERSION_1_2 && ctx->is_64bit )
+    {
+        for (i = 8; i <= 17; i++)
+            set_user_reg(regs, i, 0);
+    }
 }
 
 static inline void ffa_set_regs_error(struct cpu_user_regs *regs,
Signature · 1 line
2.51.2
#34 lore raw

Re: [PATCH 10/10] xen/arm: ffa: advertise FF-A v1.2

Hi Bertrand,

Quoted 70 lines · Bertrand Marquis

Advertise RUN/YIELD/INTERRUPT via FFA_FEATURES now that the runtime helpers are in place, and expose the RX/TX MAP capacity field only once a guest has negotiated FF-A v1.2.

While there, drop the stale <asm/tee/ffa.h> include.

To comply with the wider v1.2 register ABI, zero registers x8–x17 when responding to a v1.2 VM. The dispatcher also rejects SMCCC64 calls from AArch32 guests.

Finally, bump Xen's FF-A version to 1.2.

---
 xen/arch/arm/tee/ffa.c         | 25 ++++++++++++++++++++-----
 xen/arch/arm/tee/ffa_private.h | 28 +++++++++++++++++++---------
 2 files changed, 39 insertions(+), 14 deletions(-)
xen/arch/arm/tee/ffa.c +13 -5
index 8b2f042287fc..c8fe97856d06 100644
@@ -24,9 +24,6 @@
  * o FFA_MEM_DONATE_* and FFA_MEM_LEND_* - Used when tranferring ownership
  *   or access of a memory region
  * o FFA_MSG_SEND2 and FFA_MSG_WAIT - Used for indirect messaging
- * o FFA_MSG_YIELD
- * o FFA_INTERRUPT - Used to report preemption
- * o FFA_RUN
  *
  * Limitations in the implemented FF-A interfaces:
  * o FFA_RXTX_MAP_*:
@@ -68,7 +65,6 @@
 #include <asm/event.h>
 #include <asm/regs.h>
 #include <asm/smccc.h>
-#include <asm/tee/ffa.h>
 #include <asm/tee/tee.h>
 
 #include "ffa_private.h"
@@ -262,6 +258,9 @@ static void handle_features(struct cpu_user_regs *regs)
     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);
         break;
     case FFA_MSG_SEND_DIRECT_REQ2:
@@ -290,7 +289,16 @@ static void handle_features(struct cpu_user_regs *regs)
          * differs from FFA_PAGE_SIZE (SZ_4K).
          */
         BUILD_BUG_ON(PAGE_SIZE != FFA_PAGE_SIZE);
-        ffa_set_regs_success(regs, 0, 0);
+
+        /*
+         * From FFA v1.2, we can give the maximum number of pages we support
+         * for the RX/TX buffers.
+         */
+        if ( ctx->guest_vers < FFA_VERSION_1_2 )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_success(regs, FFA_MAX_RXTX_PAGE_COUNT << 16, 0);
+
         break;
     case FFA_FEATURE_NOTIF_PEND_INTR:
         ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);

xen/arch/arm/tee/ffa.c static bool ffa_handle_call(struct cpu_user_regs *regs) · line 334

Quoted 4 lines
xen/arch/arm/tee/ffa.c +1 -0
@@ -323,6 +331,13 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     if ( !ctx )
         return false;
 
+    if ( !is_64bit_domain(d) && smccc_is_conv_64(fid) )

You use ctx->is_64bit in ffa_set_regs() below, but here is_64bit_domain(d). Which one do we prefer?

Cheers,
Jens

Quoted 58 lines
xen/arch/arm/tee/ffa.c +6 -0
+    {
+        /* 32bit guests should only use 32bit convention calls */
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        return true;
+    }
+
     /* A version must be negotiated first */
     if ( !ctx->guest_vers_negotiated )
     {
xen/arch/arm/tee/ffa_private.h +19 -10
index a9a03c7c5d71..6afca2ea07ef 100644
@@ -53,7 +53,7 @@
  * that particular guest or SP.
  */
 #define FFA_MY_VERSION_MAJOR    1U
-#define FFA_MY_VERSION_MINOR    1U
+#define FFA_MY_VERSION_MINOR    2U
 #define FFA_MY_VERSION          MAKE_FFA_VERSION(FFA_MY_VERSION_MAJOR, \
                                                  FFA_MY_VERSION_MINOR)
 
@@ -505,14 +505,24 @@ static inline void ffa_set_regs(struct cpu_user_regs *regs, register_t v0,
                                 register_t v4, register_t v5, register_t v6,
                                 register_t v7)
 {
-        set_user_reg(regs, 0, v0);
-        set_user_reg(regs, 1, v1);
-        set_user_reg(regs, 2, v2);
-        set_user_reg(regs, 3, v3);
-        set_user_reg(regs, 4, v4);
-        set_user_reg(regs, 5, v5);
-        set_user_reg(regs, 6, v6);
-        set_user_reg(regs, 7, v7);
+    struct domain *d = current->domain;
+    struct ffa_ctx *ctx = d->arch.tee;
+    int i;
+
+    set_user_reg(regs, 0, v0);
+    set_user_reg(regs, 1, v1);
+    set_user_reg(regs, 2, v2);
+    set_user_reg(regs, 3, v3);
+    set_user_reg(regs, 4, v4);
+    set_user_reg(regs, 5, v5);
+    set_user_reg(regs, 6, v6);
+    set_user_reg(regs, 7, v7);
+
+    if ( ctx && ctx->guest_vers >= FFA_VERSION_1_2 && ctx->is_64bit )
+    {
+        for (i = 8; i <= 17; i++)
+            set_user_reg(regs, i, 0);
+    }
 }
 
 static inline void ffa_set_regs_error(struct cpu_user_regs *regs,
--

2.51.2

#35 lore raw

Re: [PATCH 10/10] xen/arm: ffa: advertise FF-A v1.2

Hi Jens,

Quoted 79 lines · Jens Wiklander

Hi Bertrand,

Quoted 73 lines · Bertrand Marquis

Advertise RUN/YIELD/INTERRUPT via FFA_FEATURES now that the runtime helpers are in place, and expose the RX/TX MAP capacity field only once a guest has negotiated FF-A v1.2.

While there, drop the stale <asm/tee/ffa.h> include.

To comply with the wider v1.2 register ABI, zero registers x8–x17 when responding to a v1.2 VM. The dispatcher also rejects SMCCC64 calls from AArch32 guests.

Finally, bump Xen's FF-A version to 1.2.

---
xen/arch/arm/tee/ffa.c | 25 ++++++++++++++++++++-----
xen/arch/arm/tee/ffa_private.h | 28 +++++++++++++++++++---------

2 files changed, 39 insertions(+), 14 deletions(-)
xen/arch/arm/tee/ffa.c +13 -5
index 8b2f042287fc..c8fe97856d06 100644
@@ -24,9 +24,6 @@
 * o FFA_MEM_DONATE_* and FFA_MEM_LEND_* - Used when tranferring ownership
 *   or access of a memory region
 * o FFA_MSG_SEND2 and FFA_MSG_WAIT - Used for indirect messaging
- * o FFA_MSG_YIELD
- * o FFA_INTERRUPT - Used to report preemption
- * o FFA_RUN
 *
 * Limitations in the implemented FF-A interfaces:
 * o FFA_RXTX_MAP_*:
@@ -68,7 +65,6 @@
 #include <asm/event.h>
 #include <asm/regs.h>
 #include <asm/smccc.h>
-#include <asm/tee/ffa.h>
 #include <asm/tee/tee.h>
 
 #include "ffa_private.h"
@@ -262,6 +258,9 @@ static void handle_features(struct cpu_user_regs *regs)
    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);
        break;
    case FFA_MSG_SEND_DIRECT_REQ2:
@@ -290,7 +289,16 @@ static void handle_features(struct cpu_user_regs *regs)
         * differs from FFA_PAGE_SIZE (SZ_4K).
         */
        BUILD_BUG_ON(PAGE_SIZE != FFA_PAGE_SIZE);
-        ffa_set_regs_success(regs, 0, 0);
+
+        /*
+         * From FFA v1.2, we can give the maximum number of pages we support
+         * for the RX/TX buffers.
+         */
+        if ( ctx->guest_vers < FFA_VERSION_1_2 )
+            ffa_set_regs_success(regs, 0, 0);
+        else
+            ffa_set_regs_success(regs, FFA_MAX_RXTX_PAGE_COUNT << 16, 0);
+
        break;
    case FFA_FEATURE_NOTIF_PEND_INTR:
        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
@@ -323,6 +331,13 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
    if ( !ctx )
        return false;
 
Quoted 4 lines

xen/arch/arm/tee/ffa.c static bool ffa_handle_call(struct cpu_user_regs *regs) · line 334

Quoted 1 line
xen/arch/arm/tee/ffa.c +1 -0
+    if ( !is_64bit_domain(d) && smccc_is_conv_64(fid) )

You use ctx->is_64bit in ffa_set_regs() below, but here is_64bit_domain(d). Which one do we prefer?

It would definitely be more coherent to use ctx->is_64bit.

I will fix that in next version.

Cheers
Bertrand