xen/arm: smccc: preserve arguments before register setup
cover xen/arm: smccc: preserve arguments before register setup
Hi Andrew,
While investigating an SMC forwarding failure after updating to Xen 4.22, I found that firmware could receive different values from those passed to arm_smccc_1_1_smc().
Several public Xen callers pass get_user_reg() calls directly to this helper. This includes the SCMI, ZynqMP EEMI, and i.MX forwarding paths. The helper puts each value into its SMC argument register as the expression is evaluated. A later get_user_reg() call can then overwrite a register prepared for an earlier argument.
The change in generated code appears after:
67bcf5eae709 ("xen/arm: Simplify type handling for SMCCC declarations")
I am not sure which behavior the helper is intended to provide. Should arm_smccc_1_1_smc() continue to accept arguments such as get_user_reg() calls, or should callers save those values in local variables first?
The attached patch is a candidate for the first option. It restores temporary variables inside the helper, allowing all calls to finish before x0-x7 are prepared for the SMC instruction. I am not able to validate the public SCMI and ZynqMP paths on hardware, but their generated code now finishes every get_user_reg() call before the final register setup and smc. With this change, SMCs also work as intended on the device I'm using.
If the intent is not to accept function calls as arguments, we should probably fix the SCMI, ZynqMP EEMI, and i.MX callers.
Given the existing use cases, this seems like a bug, but your patch looks intentional. It's very possible I'm missing the intended design. I would appreciate your view on the intended contract.
Thanks for any advice or help with this.
Jan Setje-Eilers (1):
xen/arm: smccc: preserve arguments before register setup
xen/arch/arm/include/asm/smccc.h | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
Signature · 1 line
2.47.3
RFC PATCH 1/1 xen/arm: smccc: preserve arguments before register setup
Some callers of arm_smccc_1_1_smc() pass get_user_reg() calls as arguments. These include the SCMI, ZynqMP EEMI, and i.MX forwarding paths.
The argument macros currently put each value into its SMC register as the expression is evaluated. A later function call can overwrite a register prepared for an earlier argument. Firmware then receives the wrong values.
Restore temporary variables for a1-a7. Because the macros are nested, this lets every function call finish before x0-x7 are prepared for the SMC instruction. It also keeps the inferred type of each argument.
Backport: 4.22+
--- xen/arch/arm/include/asm/smccc.h | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
xen/arch/arm/include/asm/smccc.h +19 -7
@@ -108,37 +108,49 @@ struct arm_smccc_res {#define __constraint_read_6 __constraint_read_5, "r" (arg6)#define __constraint_read_7 __constraint_read_6, "r" (arg7) +/* +* Save a1-a7 in temporary variables before assigning the SMC argument +* registers. A later argument may call a function and overwrite a register +* assigned earlier. +*/ #define __declare_arg_0 (a0, res) \ struct arm_smccc_res *___res = (res); \ register unsigned long arg0 ASM_REG(0) = (uint32_t)(a0)#define __declare_arg_1 (a0, a1, res) \ + auto __a1 = (a1); \ __declare_arg_0(a0, res); \ - register auto arg1 ASM_REG(1) = (a1) + register auto arg1 ASM_REG(1) = __a1#define __declare_arg_2 (a0, a1, a2, res) \ + auto __a2 = (a2); \ __declare_arg_1(a0, a1, res); \ - register auto arg2 ASM_REG(2) = (a2) + register auto arg2 ASM_REG(2) = __a2#define __declare_arg_3 (a0, a1, a2, a3, res) \ + auto __a3 = (a3); \ __declare_arg_2(a0, a1, a2, res); \ - register auto arg3 ASM_REG(3) = (a3) + register auto arg3 ASM_REG(3) = __a3#define __declare_arg_4 (a0, a1, a2, a3, a4, res) \ + auto __a4 = (a4); \ __declare_arg_3(a0, a1, a2, a3, res); \ - register auto arg4 ASM_REG(4) = (a4) + register auto arg4 ASM_REG(4) = __a4#define __declare_arg_5 (a0, a1, a2, a3, a4, a5, res) \ + auto __a5 = (a5); \ __declare_arg_4(a0, a1, a2, a3, a4, res); \ - register auto arg5 ASM_REG(5) = (a5) + register auto arg5 ASM_REG(5) = __a5#define __declare_arg_6 (a0, a1, a2, a3, a4, a5, a6, res) \ + auto __a6 = (a6); \ __declare_arg_5(a0, a1, a2, a3, a4, a5, res); \ - register auto arg6 ASM_REG(6) = (a6) + register auto arg6 ASM_REG(6) = __a6#define __declare_arg_7 (a0, a1, a2, a3, a4, a5, a6, a7, res) \ + auto __a7 = (a7); \ __declare_arg_6(a0, a1, a2, a3, a4, a5, a6, res); \ - register auto arg7 ASM_REG(7) = (a7) + register auto arg7 ASM_REG(7) = __a7#define ___declare_args (count, ...) __declare_arg_ ## count(__VA_ARGS__)#define __declare_args (count, ...) ___declare_args(count, __VA_ARGS__)
Signature · 1 line
2.47.3
Quoted 28 lines · Jan Setje-Eilers
Hi Andrew,
While investigating an SMC forwarding failure after updating to Xen 4.22, I found that firmware could receive different values from those passed to
arm_smccc_1_1_smc().Several public Xen callers pass
get_user_reg()calls directly to this helper. This includes the SCMI, ZynqMP EEMI, and i.MX forwarding paths. The helper puts each value into its SMC argument register as the expression is evaluated. A laterget_user_reg()call can then overwrite a register prepared for an earlier argument.The change in generated code appears after:
67bcf5eae709("xen/arm: Simplify type handling for SMCCC declarations")I am not sure which behavior the helper is intended to provide. Should
arm_smccc_1_1_smc()continue to accept arguments such asget_user_reg()calls, or should callers save those values in local variables first?The attached patch is a candidate for the first option. It restores temporary variables inside the helper, allowing all calls to finish before x0-x7 are prepared for the SMC instruction. I am not able to validate the public SCMI and ZynqMP paths on hardware, but their generated code now finishes every
get_user_reg()call before the final register setup and smc. With this change, SMCs also work as intended on the device I'm using.
Quoted 8 lines
If the intent is not to accept function calls as arguments, we should probably fix the SCMI, ZynqMP EEMI, and i.MX callers.
Given the existing use cases, this seems like a bug, but your patch looks intentional. It's very possible I'm missing the intended design. I would appreciate your view on the intended contract.
Thanks for any advice or help with this.
Yes you're right. My change was buggy. It was actually part of a mutli-stage cleanup across several patches.
I'll submit my own patch. I'm afraid your AI has put in whitespace errors where a straight revert would have gotten it correct, and left the 0 case still buggy.
~Andrew
Quoted 36 lines · Andrew Cooper
Quoted 35 lines · Jan Setje-Eilers
Hi Andrew,
While investigating an SMC forwarding failure after updating to Xen 4.22, I found that firmware could receive different values from those passed to
arm_smccc_1_1_smc().Several public Xen callers pass
get_user_reg()calls directly to this helper. This includes the SCMI, ZynqMP EEMI, and i.MX forwarding paths. The helper puts each value into its SMC argument register as the expression is evaluated. A laterget_user_reg()call can then overwrite a register prepared for an earlier argument.The change in generated code appears after:
67bcf5eae709("xen/arm: Simplify type handling for SMCCC declarations")I am not sure which behavior the helper is intended to provide. Should
arm_smccc_1_1_smc()continue to accept arguments such asget_user_reg()calls, or should callers save those values in local variables first?The attached patch is a candidate for the first option. It restores temporary variables inside the helper, allowing all calls to finish before x0-x7 are prepared for the SMC instruction. I am not able to validate the public SCMI and ZynqMP paths on hardware, but their generated code now finishes every
get_user_reg()call before the final register setup and smc. With this change, SMCs also work as intended on the device I'm using.If the intent is not to accept function calls as arguments, we should probably fix the SCMI, ZynqMP EEMI, and i.MX callers.
Given the existing use cases, this seems like a bug, but your patch looks intentional. It's very possible I'm missing the intended design. I would appreciate your view on the intended contract.
Quoted 7 lines
Quoted 1 line
Thanks for any advice or help with this.
Yes you're right. My change was buggy. It was actually part of a mutli-stage cleanup across several patches.
I'll submit my own patch. I'm afraid your AI has put in whitespace errors where a straight revert would have gotten it correct, and left the 0 case still buggy.
In fact there's another bug. The SMCCC ABI states that certain registers are clobbered, yet these are missing from asm().
It's also sad that this macro maze goes to the effort of making a function-like-call which can return 4 registers by value, just to then force it all to be spilled to the stack anyway.
~Andrew
Quoted 6 lines · Andrew Cooper
On 29/08/2026 5: 53 pm, Andrew Cooper wrote: > On 29/08/2026 3: 12 am, Jan Setje-Eilers wrote: >> Hi Andrew, >> >> While investigating an SMC forwarding failure after updating to Xen 4. 22, >> I found that firmware could
Quoted 44 lines
Quoted 43 lines · Andrew Cooper
Quoted 28 lines · Jan Setje-Eilers
Hi Andrew,
While investigating an SMC forwarding failure after updating to Xen 4.22, I found that firmware could receive different values from those passed to
arm_smccc_1_1_smc().Several public Xen callers pass
get_user_reg()calls directly to this helper. This includes the SCMI, ZynqMP EEMI, and i.MX forwarding paths. The helper puts each value into its SMC argument register as the expression is evaluated. A laterget_user_reg()call can then overwrite a register prepared for an earlier argument.The change in generated code appears after:
67bcf5eae709("xen/arm: Simplify type handling for SMCCC declarations")I am not sure which behavior the helper is intended to provide. Should
arm_smccc_1_1_smc()continue to accept arguments such asget_user_reg()calls, or should callers save those values in local variables first?The attached patch is a candidate for the first option. It restores temporary variables inside the helper, allowing all calls to finish before x0-x7 are prepared for the SMC instruction. I am not able to validate the public SCMI and ZynqMP paths on hardware, but their generated code now finishes every
get_user_reg()call before the final register setup and smc. With this change, SMCs also work as intended on the device I'm using.Quoted 8 lines
If the intent is not to accept function calls as arguments, we should probably fix the SCMI, ZynqMP EEMI, and i.MX callers.
Given the existing use cases, this seems like a bug, but your patch looks intentional. It's very possible I'm missing the intended design. I would appreciate your view on the intended contract.
Thanks for any advice or help with this.
Yes you're right. My change was buggy. It was actually part of a mutli-stage cleanup across several patches.
I'll submit my own patch. I'm afraid your AI has put in whitespace errors where a straight revert would have gotten it correct, and left the 0 case still buggy.
Thank you!! Yes, a revert also makes sense.
Heh, the bulk of my AI usage was actually to try to make sure I hit all
the project rules and formatting since I'm much less used to some of the style here. Sigh. :)
Quoted 6 lines
In fact there's another bug. The SMCCC ABI states that certain registers are clobbered, yet these are missing from
asm().It's also sad that this macro maze goes to the effort of making a function-like-call which can return 4 registers by value, just to then force it all to be spilled to the stack anyway.
The other way to do this, if we're actually trying to minimize
instructions,
would actually be to create another interface that does something like get_user_reg() as part of the invocation. Then that could be a single assembly implementation.
-jan